chan_h323.c 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913
  1. /*
  2. * chan_h323.c
  3. *
  4. * OpenH323 Channel Driver for ASTERISK PBX.
  5. * By Jeremy McNamara
  6. * For The NuFone Network
  7. *
  8. * This code has been derived from code created by
  9. * Michael Manousos and Mark Spencer
  10. *
  11. * This file is part of the chan_h323 driver for Asterisk
  12. *
  13. * chan_h323 is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * chan_h323 is distributed WITHOUT ANY WARRANTY; without even
  19. * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  20. * PURPOSE. See the GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program; if not, write to the Free Software
  24. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  25. *
  26. * Version Info: $Id$
  27. */
  28. #include <stdio.h>
  29. #include <pthread.h>
  30. #include <string.h>
  31. #include <asterisk/lock.h>
  32. #include <asterisk/logger.h>
  33. #include <asterisk/channel.h>
  34. #include <asterisk/channel_pvt.h>
  35. #include <asterisk/config.h>
  36. #include <asterisk/module.h>
  37. #include <asterisk/pbx.h>
  38. #include <asterisk/options.h>
  39. #include <asterisk/lock.h>
  40. #include <asterisk/sched.h>
  41. #include <asterisk/io.h>
  42. #include <asterisk/rtp.h>
  43. #include <asterisk/acl.h>
  44. #include <asterisk/callerid.h>
  45. #include <asterisk/cli.h>
  46. #include <asterisk/dsp.h>
  47. #include <sys/socket.h>
  48. #include <net/if.h>
  49. #include <errno.h>
  50. #include <unistd.h>
  51. #include <stdlib.h>
  52. #include <fcntl.h>
  53. #include <netdb.h>
  54. #include <sys/signal.h>
  55. #include <netinet/ip.h>
  56. #include "h323/chan_h323.h"
  57. /** String variables required by ASTERISK */
  58. static char *type = "H323";
  59. static char *desc = "The NuFone Network's Open H.323 Channel Driver";
  60. static char *tdesc = "The NuFone Network's Open H.323 Channel Driver";
  61. static char *config = "h323.conf";
  62. static char default_context[AST_MAX_EXTENSION];
  63. /** H.323 configuration values */
  64. static struct sockaddr_in bindaddr;
  65. static char gatekeeper[100];
  66. static int gatekeeper_disable = 1;
  67. static int gatekeeper_discover = 0;
  68. static int usingGk;
  69. static int port = 1720;
  70. static int gkroute = 0;
  71. /* to find user by alias is default, alternative is the incomming call's source IP address*/
  72. static int userbyalias = 1;
  73. static int bridge_default = 1;
  74. /* Just about everybody seems to support ulaw, so make it a nice default */
  75. static int capability = AST_FORMAT_ULAW;
  76. /* TOS flag */
  77. static int tos = 0;
  78. static int dtmfmode = H323_DTMF_RFC2833;
  79. static char secret[50];
  80. /** Private structure of a OpenH323 channel */
  81. struct oh323_pvt {
  82. ast_mutex_t lock; /* Channel private lock */
  83. call_options_t calloptions; /* Options to be used during call setup */
  84. int alreadygone; /* Whether or not we've already been destroyed by or peer */
  85. int needdestroy; /* if we need to be destroyed */
  86. call_details_t cd; /* Call details */
  87. struct ast_channel *owner; /* Who owns us */
  88. int capability; /* Special capability */
  89. int nonCodecCapability;
  90. int outgoing; /* Outgoing or incoming call? */
  91. int nat; /* Are we talking to a NAT EP?*/
  92. int bridge; /* Determine of we should native bridge or not*/
  93. char exten[AST_MAX_EXTENSION]; /* Requested extension */
  94. char context[AST_MAX_EXTENSION]; /* Context where to start */
  95. char username[81]; /* H.323 alias using this channel */
  96. char accountcode[256]; /* Account code */
  97. int amaflags; /* AMA Flags */
  98. char callerid[80]; /* Caller*ID if available */
  99. struct ast_rtp *rtp; /* RTP Session */
  100. int dtmfmode;
  101. struct ast_dsp *vad; /* Used for in-band DTMF detection */
  102. struct oh323_pvt *next; /* Next channel in list */
  103. } *iflist = NULL;
  104. static struct ast_user_list {
  105. struct oh323_user *users;
  106. ast_mutex_t lock;
  107. } userl = { NULL, AST_MUTEX_INITIALIZER };
  108. static struct ast_peer_list {
  109. struct oh323_peer *peers;
  110. ast_mutex_t lock;
  111. } peerl = { NULL, AST_MUTEX_INITIALIZER };
  112. static struct ast_alias_list {
  113. struct oh323_alias *aliases;
  114. ast_mutex_t lock;
  115. } aliasl = { NULL, AST_MUTEX_INITIALIZER };
  116. /** Asterisk RTP stuff*/
  117. static struct sched_context *sched;
  118. static struct io_context *io;
  119. /** Protect the interface list (of oh323_pvt's) */
  120. static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
  121. /** Usage counter and associated lock */
  122. static int usecnt =0;
  123. static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
  124. /* Protect the monitoring thread, so only one process can kill or start it, and not
  125. when it's doing something critical. */
  126. static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
  127. /* This is the thread for the monitor which checks for input on the channels
  128. which are not currently in use. */
  129. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  130. static int restart_monitor(void);
  131. static void __oh323_destroy(struct oh323_pvt *p)
  132. {
  133. struct oh323_pvt *cur, *prev = NULL;
  134. if (p->rtp) {
  135. ast_rtp_destroy(p->rtp);
  136. }
  137. /* Unlink us from the owner if we have one */
  138. if (p->owner) {
  139. ast_mutex_lock(&p->owner->lock);
  140. ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
  141. p->owner->pvt->pvt = NULL;
  142. ast_mutex_unlock(&p->owner->lock);
  143. }
  144. cur = iflist;
  145. while(cur) {
  146. if (cur == p) {
  147. if (prev)
  148. prev->next = cur->next;
  149. else
  150. iflist = cur->next;
  151. break;
  152. }
  153. prev = cur;
  154. cur = cur->next;
  155. }
  156. if (!cur) {
  157. ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
  158. } else
  159. free(p);
  160. }
  161. static void oh323_destroy(struct oh323_pvt *p)
  162. {
  163. ast_mutex_lock(&iflock);
  164. __oh323_destroy(p);
  165. ast_mutex_unlock(&iflock);
  166. }
  167. static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
  168. {
  169. struct oh323_alias *alias;
  170. alias = (struct oh323_alias *)malloc(sizeof(struct oh323_alias));
  171. if (alias) {
  172. memset(alias, 0, sizeof(struct oh323_alias));
  173. strncpy(alias->name, name, sizeof(alias->name)-1);
  174. while (v) {
  175. if (!strcasecmp(v->name, "e164")) {
  176. strncpy(alias->e164, v->value, sizeof(alias->e164)-1);
  177. } else if (!strcasecmp(v->name, "prefix")) {
  178. strncpy(alias->prefix, v->value, sizeof(alias->prefix)-1);
  179. } else if (!strcasecmp(v->name, "context")) {
  180. strncpy(alias->context, v->value, sizeof(alias->context)-1);
  181. } else if (!strcasecmp(v->name, "secret")) {
  182. strncpy(alias->secret, v->value, sizeof(alias->secret)-1);
  183. } else {
  184. if (strcasecmp(v->value, "h323")) {
  185. ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->value);
  186. }
  187. }
  188. v = v->next;
  189. }
  190. }
  191. return alias;
  192. }
  193. static struct oh323_user *build_user(char *name, struct ast_variable *v)
  194. {
  195. struct oh323_user *user;
  196. int format;
  197. user = (struct oh323_user *)malloc(sizeof(struct oh323_user));
  198. if (user) {
  199. memset(user, 0, sizeof(struct oh323_user));
  200. strncpy(user->name, name, sizeof(user->name)-1);
  201. /* set the usage flag to a sane starting value*/
  202. user->inUse = 0;
  203. /* Assume we can native bridge */
  204. user->bridge = bridge_default;
  205. while(v) {
  206. if (!strcasecmp(v->name, "context")) {
  207. strncpy(user->context, v->value, sizeof(user->context)-1);
  208. } else if (!strcasecmp(v->name, "bridge")) {
  209. user->bridge = ast_true(v->value);
  210. } else if (!strcasecmp(v->name, "nat")) {
  211. user->nat = ast_true(v->value);
  212. } else if (!strcasecmp(v->name, "noFastStart")) {
  213. user->noFastStart = ast_true(v->value);
  214. } else if (!strcasecmp(v->name, "noH245Tunneling")) {
  215. user->noH245Tunneling = ast_true(v->value);
  216. } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
  217. user->noSilenceSuppression = ast_true(v->value);
  218. } else if (!strcasecmp(v->name, "secret")) {
  219. strncpy(user->secret, v->value, sizeof(user->secret)-1);
  220. } else if (!strcasecmp(v->name, "callerid")) {
  221. strncpy(user->callerid, v->value, sizeof(user->callerid)-1);
  222. } else if (!strcasecmp(v->name, "accountcode")) {
  223. strncpy(user->accountcode, v->value, sizeof(user->accountcode)-1);
  224. } else if (!strcasecmp(v->name, "incominglimit")) {
  225. user->incominglimit = atoi(v->value);
  226. if (user->incominglimit < 0)
  227. user->incominglimit = 0;
  228. } else if (!strcasecmp(v->name, "host")) {
  229. if (!strcasecmp(v->value, "dynamic")) {
  230. ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
  231. free(user);
  232. return NULL;
  233. } else if (ast_get_ip(&user->addr, v->value)) {
  234. free(user);
  235. return NULL;
  236. }
  237. /* Let us know we need to use ip authentication */
  238. user->host = 1;
  239. } else if (!strcasecmp(v->name, "amaflags")) {
  240. format = ast_cdr_amaflags2int(v->value);
  241. if (format < 0) {
  242. ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
  243. } else {
  244. user->amaflags = format;
  245. }
  246. }
  247. v = v->next;
  248. }
  249. }
  250. return user;
  251. }
  252. static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
  253. {
  254. struct oh323_peer *peer;
  255. struct oh323_peer *prev;
  256. int found=0;
  257. prev = NULL;
  258. ast_mutex_lock(&peerl.lock);
  259. peer = peerl.peers;
  260. while(peer) {
  261. if (!strcasecmp(peer->name, name)) {
  262. break;
  263. }
  264. prev = peer;
  265. peer = peer->next;
  266. }
  267. if (peer) {
  268. found++;
  269. /* Already in the list, remove it and it will be added back (or FREE'd) */
  270. if (prev) {
  271. prev->next = peer->next;
  272. } else {
  273. peerl.peers = peer->next;
  274. }
  275. ast_mutex_unlock(&peerl.lock);
  276. } else {
  277. ast_mutex_unlock(&peerl.lock);
  278. peer = malloc(sizeof(struct oh323_peer));
  279. memset(peer, 0, sizeof(struct oh323_peer));
  280. }
  281. if (peer) {
  282. if (!found) {
  283. strncpy(peer->name, name, sizeof(peer->name)-1);
  284. }
  285. /* set the usage flag to a sane starting value*/
  286. peer->inUse = 0;
  287. while(v) {
  288. if (!strcasecmp(v->name, "context")) {
  289. strncpy(peer->context, v->value, sizeof(peer->context)-1);
  290. } else if (!strcasecmp(v->name, "bridge")) {
  291. peer->bridge = ast_true(v->value);
  292. } else if (!strcasecmp(v->name, "noFastStart")) {
  293. peer->noFastStart = ast_true(v->value);
  294. } else if (!strcasecmp(v->name, "noH245Tunneling")) {
  295. peer->noH245Tunneling = ast_true(v->value);
  296. } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
  297. peer->noSilenceSuppression = ast_true(v->value);
  298. } else if (!strcasecmp(v->name, "outgoinglimit")) {
  299. peer->outgoinglimit = atoi(v->value);
  300. if (peer->outgoinglimit > 0)
  301. peer->outgoinglimit = 0;
  302. } else if (!strcasecmp(v->name, "host")) {
  303. if (!strcasecmp(v->value, "dynamic")) {
  304. ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
  305. free(peer);
  306. return NULL;
  307. }
  308. if (ast_get_ip(&peer->addr, v->value)) {
  309. free(peer);
  310. return NULL;
  311. }
  312. }
  313. v=v->next;
  314. }
  315. }
  316. return peer;
  317. }
  318. /**
  319. * Send (play) the specified digit to the channel.
  320. *
  321. */
  322. static int oh323_digit(struct ast_channel *c, char digit)
  323. {
  324. struct oh323_pvt *p = c->pvt->pvt;
  325. if (p && p->rtp && (p->dtmfmode & H323_DTMF_RFC2833)) {
  326. ast_rtp_senddigit(p->rtp, digit);
  327. }
  328. /* If in-band DTMF is desired, send that */
  329. if (p->dtmfmode & H323_DTMF_INBAND)
  330. h323_send_tone(p->cd.call_token, digit);
  331. return 0;
  332. }
  333. /**
  334. * Make a call over the specified channel to the specified
  335. * destination. This function will parse the destination string
  336. * and determine the address-number to call.
  337. * Return -1 on error, 0 on success.
  338. */
  339. static int oh323_call(struct ast_channel *c, char *dest, int timeout)
  340. {
  341. int res;
  342. struct oh323_pvt *p = c->pvt->pvt;
  343. char called_addr[256];
  344. char *tmp;
  345. strtok_r(dest, "/", &(tmp));
  346. ast_log(LOG_DEBUG, "dest=%s, timeout=%d.\n", dest, timeout);
  347. if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
  348. ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
  349. return -1;
  350. }
  351. /* outgoing call */
  352. p->outgoing = 1;
  353. /* Clear the call token */
  354. if ((p->cd).call_token == NULL)
  355. (p->cd).call_token = (char *)malloc(128);
  356. memset((char *)(p->cd).call_token, 0, 128);
  357. if (p->cd.call_token == NULL) {
  358. ast_log(LOG_ERROR, "Not enough memory.\n");
  359. return -1;
  360. }
  361. /* Build the address to call */
  362. memset(called_addr, 0, sizeof(dest));
  363. memcpy(called_addr, dest, sizeof(called_addr));
  364. /* Copy callerid, if there is any */
  365. if (c->callerid) {
  366. char *tmp = strchr(c->callerid, '"');
  367. if (!tmp) {
  368. p->calloptions.callerid = malloc(80); // evil
  369. // sprintf(p->calloptions.callerid, "\"%s\"", c->callerid);
  370. sprintf(p->calloptions.callerid, "\"\" <%s>", c->callerid);
  371. } else {
  372. p->calloptions.callerid = strdup(c->callerid);
  373. }
  374. }
  375. res = h323_make_call(called_addr, &(p->cd), p->calloptions);
  376. if (res) {
  377. ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
  378. return -1;
  379. }
  380. ast_setstate(c, AST_STATE_RINGING);
  381. return 0;
  382. }
  383. static int oh323_answer(struct ast_channel *c)
  384. {
  385. int res;
  386. struct oh323_pvt *p = c->pvt->pvt;
  387. res = h323_answering_call(p->cd.call_token, 0);
  388. if (c->_state != AST_STATE_UP)
  389. ast_setstate(c, AST_STATE_UP);
  390. return res;
  391. }
  392. static int oh323_hangup(struct ast_channel *c)
  393. {
  394. struct oh323_pvt *p = c->pvt->pvt;
  395. int needcancel = 0;
  396. if (h323debug)
  397. ast_log(LOG_DEBUG, "oh323_hangup(%s)\n", c->name);
  398. if (!c->pvt->pvt) {
  399. ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
  400. return 0;
  401. }
  402. ast_mutex_lock(&p->lock);
  403. /* Determine how to disconnect */
  404. if (p->owner != c) {
  405. ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
  406. ast_mutex_unlock(&p->lock);
  407. return 0;
  408. }
  409. if (!c || (c->_state != AST_STATE_UP))
  410. needcancel = 1;
  411. /* Disconnect */
  412. p = c->pvt->pvt;
  413. /* Free dsp used for in-band DTMF detection */
  414. if (p->vad) {
  415. ast_dsp_free(p->vad);
  416. }
  417. p->owner = NULL;
  418. c->pvt->pvt = NULL;
  419. /* Start the process if it's not already started */
  420. if (!p->alreadygone) {
  421. p->needdestroy = 1;
  422. if (h323_clear_call((p->cd).call_token)) {
  423. ast_log(LOG_DEBUG, "ClearCall failed.\n");
  424. }
  425. }
  426. /* Update usage counter */
  427. ast_mutex_lock(&usecnt_lock);
  428. usecnt--;
  429. if (usecnt < 0)
  430. ast_log(LOG_WARNING, "Usecnt < 0\n");
  431. ast_mutex_unlock(&usecnt_lock);
  432. ast_update_use_count();
  433. ast_mutex_unlock(&p->lock);
  434. return 0;
  435. }
  436. static struct ast_frame *oh323_rtp_read(struct oh323_pvt *p)
  437. {
  438. /* Retrieve audio/etc from channel. Assumes p->lock is already held. */
  439. struct ast_frame *f;
  440. static struct ast_frame null_frame = { AST_FRAME_NULL, };
  441. /* Only apply it for the first packet, we just need the correct ip/port */
  442. if(p->nat)
  443. {
  444. ast_rtp_setnat(p->rtp,p->nat);
  445. p->nat = 0;
  446. }
  447. f = ast_rtp_read(p->rtp);
  448. /* Don't send RFC2833 if we're not supposed to */
  449. if (f && (f->frametype == AST_FRAME_DTMF) && !(p->dtmfmode & H323_DTMF_RFC2833))
  450. return &null_frame;
  451. if (p->owner) {
  452. /* We already hold the channel lock */
  453. if (f->frametype == AST_FRAME_VOICE) {
  454. if (f->subclass != p->owner->nativeformats) {
  455. ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
  456. p->owner->nativeformats = f->subclass;
  457. ast_set_read_format(p->owner, p->owner->readformat);
  458. ast_set_write_format(p->owner, p->owner->writeformat);
  459. }
  460. /* Do in-band DTMF detection */
  461. if (p->dtmfmode & H323_DTMF_INBAND) {
  462. f = ast_dsp_process(p->owner,p->vad,f,0);
  463. if (f->frametype == AST_FRAME_DTMF)
  464. ast_log(LOG_DEBUG, "Got in-band digit %c.\n", f->subclass);
  465. }
  466. }
  467. }
  468. return f;
  469. }
  470. static struct ast_frame *oh323_read(struct ast_channel *c)
  471. {
  472. struct ast_frame *fr;
  473. struct oh323_pvt *p = c->pvt->pvt;
  474. ast_mutex_lock(&p->lock);
  475. fr = oh323_rtp_read(p);
  476. ast_mutex_unlock(&p->lock);
  477. return fr;
  478. }
  479. static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
  480. {
  481. struct oh323_pvt *p = c->pvt->pvt;
  482. int res = 0;
  483. if (frame->frametype != AST_FRAME_VOICE) {
  484. if (frame->frametype == AST_FRAME_IMAGE)
  485. return 0;
  486. else {
  487. ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
  488. return 0;
  489. }
  490. } else {
  491. if (!(frame->subclass & c->nativeformats)) {
  492. ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
  493. frame->subclass, c->nativeformats, c->readformat, c->writeformat);
  494. return -1;
  495. }
  496. }
  497. if (p) {
  498. ast_mutex_lock(&p->lock);
  499. if (p->rtp) {
  500. res = ast_rtp_write(p->rtp, frame);
  501. }
  502. ast_mutex_unlock(&p->lock);
  503. }
  504. return res;
  505. }
  506. /** FIXME: Can I acutally use this or does Open H.323 take care of everything? */
  507. static int oh323_indicate(struct ast_channel *c, int condition)
  508. {
  509. struct oh323_pvt *p = c->pvt->pvt;
  510. switch(condition) {
  511. case AST_CONTROL_RINGING:
  512. if (c->_state == AST_STATE_RING) {
  513. // transmit_response(p, "180 Ringing", &p->initreq);
  514. break;
  515. }
  516. return 0;
  517. case AST_CONTROL_BUSY:
  518. if (c->_state != AST_STATE_UP) {
  519. // transmit_response(p, "600 Busy everywhere", &p->initreq);
  520. p->alreadygone = 1;
  521. ast_softhangup(c, AST_SOFTHANGUP_DEV);
  522. break;
  523. }
  524. return 0;
  525. case AST_CONTROL_CONGESTION:
  526. if (c->_state != AST_STATE_UP) {
  527. // transmit_response(p, "486 Busy here", &p->initreq);
  528. p->alreadygone = 1;
  529. ast_softhangup(c, AST_SOFTHANGUP_DEV);
  530. break;
  531. }
  532. return 0;
  533. case -1:
  534. return -1;
  535. default:
  536. ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
  537. return -1;
  538. }
  539. return 0;
  540. }
  541. // FIXME: WTF is this? Do I need this???
  542. static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  543. {
  544. struct oh323_pvt *p = newchan->pvt->pvt;
  545. ast_mutex_lock(&p->lock);
  546. if (p->owner != oldchan) {
  547. ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
  548. return -1;
  549. }
  550. p->owner = newchan;
  551. ast_mutex_unlock(&p->lock);
  552. return 0;
  553. }
  554. static struct ast_channel *oh323_new(struct oh323_pvt *i, int state, const char *host)
  555. {
  556. struct ast_channel *ch;
  557. int fmt;
  558. ch = ast_channel_alloc(1);
  559. if (ch) {
  560. snprintf(ch->name, sizeof(ch->name)-1, "H323/%s", host);
  561. ch->nativeformats = i->capability;
  562. if (!ch->nativeformats)
  563. ch->nativeformats = capability;
  564. fmt = ast_best_codec(ch->nativeformats);
  565. ch->type = type;
  566. ch->fds[0] = ast_rtp_fd(i->rtp);
  567. ast_setstate(ch, state);
  568. if (state == AST_STATE_RING)
  569. ch->rings = 1;
  570. ch->writeformat = fmt;
  571. ch->pvt->rawwriteformat = fmt;
  572. ch->readformat = fmt;
  573. ch->pvt->rawreadformat = fmt;
  574. /* Allocate dsp for in-band DTMF support */
  575. if (i->dtmfmode & H323_DTMF_INBAND) {
  576. i->vad = ast_dsp_new();
  577. ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
  578. }
  579. /* Register the OpenH323 channel's functions. */
  580. ch->pvt->pvt = i;
  581. ch->pvt->send_digit = oh323_digit;
  582. ch->pvt->call = oh323_call;
  583. ch->pvt->hangup = oh323_hangup;
  584. ch->pvt->answer = oh323_answer;
  585. ch->pvt->read = oh323_read;
  586. ch->pvt->write = oh323_write;
  587. ch->pvt->indicate = oh323_indicate;
  588. ch->pvt->fixup = oh323_fixup;
  589. // ch->pvt->bridge = ast_rtp_bridge;
  590. /* Set the owner of this channel */
  591. i->owner = ch;
  592. ast_mutex_lock(&usecnt_lock);
  593. usecnt++;
  594. ast_mutex_unlock(&usecnt_lock);
  595. ast_update_use_count();
  596. strncpy(ch->context, i->context, sizeof(ch->context)-1);
  597. strncpy(ch->exten, i->exten, sizeof(ch->exten)-1);
  598. ch->priority = 1;
  599. if (strlen(i->callerid))
  600. ch->callerid = strdup(i->callerid);
  601. if (strlen(i->accountcode))
  602. strncpy(ch->accountcode, i->accountcode, sizeof(ch->accountcode)-1);
  603. if (i->amaflags)
  604. ch->amaflags = i->amaflags;
  605. if (state != AST_STATE_DOWN) {
  606. if (ast_pbx_start(ch)) {
  607. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
  608. ast_hangup(ch);
  609. ch = NULL;
  610. }
  611. }
  612. } else
  613. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  614. return ch;
  615. }
  616. static struct oh323_pvt *oh323_alloc(int callid)
  617. {
  618. struct oh323_pvt *p;
  619. p = malloc(sizeof(struct oh323_pvt));
  620. if (!p) {
  621. ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
  622. return NULL;
  623. }
  624. /* Keep track of stuff */
  625. memset(p, 0, sizeof(struct oh323_pvt));
  626. p->rtp = ast_rtp_new(sched, io, 1, 0);
  627. if (!p->rtp) {
  628. ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
  629. free(p);
  630. return NULL;
  631. }
  632. ast_rtp_settos(p->rtp, tos);
  633. ast_mutex_init(&p->lock);
  634. p->cd.call_reference = callid;
  635. p->bridge = bridge_default;
  636. p->dtmfmode = dtmfmode;
  637. if (p->dtmfmode & H323_DTMF_RFC2833)
  638. p->nonCodecCapability |= AST_RTP_DTMF;
  639. /* Add to interface list */
  640. ast_mutex_lock(&iflock);
  641. p->next = iflist;
  642. iflist = p;
  643. ast_mutex_unlock(&iflock);
  644. return p;
  645. }
  646. static struct oh323_pvt *find_call(int call_reference)
  647. {
  648. struct oh323_pvt *p;
  649. ast_mutex_lock(&iflock);
  650. p = iflist;
  651. while(p) {
  652. if (p->cd.call_reference == call_reference) {
  653. /* Found the call */
  654. ast_mutex_unlock(&iflock);
  655. return p;
  656. }
  657. p = p->next;
  658. }
  659. ast_mutex_unlock(&iflock);
  660. return NULL;
  661. }
  662. static struct ast_channel *oh323_request(char *type, int format, void *data)
  663. {
  664. int oldformat;
  665. struct oh323_pvt *p;
  666. struct ast_channel *tmpc = NULL;
  667. char *dest = data;
  668. char *ext, *host;
  669. char *h323id = NULL;
  670. char tmp[256];
  671. ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
  672. oldformat = format;
  673. format &= capability;
  674. if (!format) {
  675. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
  676. return NULL;
  677. }
  678. strncpy(tmp, dest, sizeof(tmp) - 1);
  679. host = strchr(tmp, '@');
  680. if (host) {
  681. *host = '\0';
  682. host++;
  683. ext = tmp;
  684. } else {
  685. host = tmp;
  686. ext = NULL;
  687. }
  688. strtok_r(host, "/", &(h323id));
  689. if (*h323id) {
  690. h323_set_id(h323id);
  691. }
  692. p = oh323_alloc(0);
  693. if (!p) {
  694. ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
  695. return NULL;
  696. }
  697. /* Assign a default capability */
  698. p->capability = capability;
  699. if (p->dtmfmode) {
  700. if (p->dtmfmode & H323_DTMF_RFC2833)
  701. p->nonCodecCapability |= AST_RTP_DTMF;
  702. else
  703. p->nonCodecCapability &= ~AST_RTP_DTMF;
  704. }
  705. if (ext) {
  706. strncpy(p->username, ext, sizeof(p->username) - 1);
  707. }
  708. ast_log(LOG_DEBUG, "Host: %s\tUsername: %s\n", host, p->username);
  709. tmpc = oh323_new(p, AST_STATE_DOWN, host);
  710. if (!tmpc)
  711. oh323_destroy(p);
  712. restart_monitor();
  713. return tmpc;
  714. }
  715. struct oh323_alias *find_alias(const char *source_aliases)
  716. {
  717. struct oh323_alias *a;
  718. a = aliasl.aliases;
  719. while(a) {
  720. if (!strcasecmp(a->name, source_aliases)) {
  721. break;
  722. }
  723. a = a->next;
  724. }
  725. return a;
  726. }
  727. struct oh323_user *find_user(const call_details_t cd)
  728. {
  729. struct oh323_user *u;
  730. u = userl.users;
  731. if(userbyalias == 1){
  732. while(u) {
  733. if (!strcasecmp(u->name, cd.call_source_aliases)) {
  734. break;
  735. }
  736. u = u->next;
  737. }
  738. } else {
  739. while(u) {
  740. if (!strcasecmp(cd.sourceIp, inet_ntoa(u->addr.sin_addr))) {
  741. break;
  742. }
  743. u = u->next;
  744. }
  745. }
  746. return u;
  747. }
  748. struct oh323_peer *find_peer(char *dest_peer)
  749. {
  750. struct oh323_peer *p;
  751. p = peerl.peers;
  752. while(p) {
  753. if (!strcasecmp(p->name, dest_peer)) {
  754. break;
  755. }
  756. p = p->next;
  757. }
  758. return p;
  759. }
  760. /**
  761. * Callback for sending digits from H.323 up to asterisk
  762. *
  763. */
  764. int send_digit(unsigned call_reference, char digit)
  765. {
  766. struct oh323_pvt *p;
  767. struct ast_frame f;
  768. ast_log(LOG_DEBUG, "Recieved Digit: %c\n", digit);
  769. p = find_call(call_reference);
  770. if (!p) {
  771. ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
  772. return -1;
  773. }
  774. memset(&f, 0, sizeof(f));
  775. f.frametype = AST_FRAME_DTMF;
  776. f.subclass = digit;
  777. f.datalen = 0;
  778. f.samples = 300;
  779. f.offset = 0;
  780. f.data = NULL;
  781. f.mallocd = 0;
  782. f.src = "SEND_DIGIT";
  783. return ast_queue_frame(p->owner, &f, 1);
  784. }
  785. /**
  786. * Call-back function that gets called when any H.323 connection is made
  787. *
  788. * Returns the local RTP information
  789. */
  790. struct rtp_info *create_connection(unsigned call_reference)
  791. {
  792. struct oh323_pvt *p;
  793. struct sockaddr_in us;
  794. struct sockaddr_in them;
  795. struct rtp_info *info;
  796. info = malloc(sizeof(struct rtp_info));
  797. p = find_call(call_reference);
  798. if (!p) {
  799. ast_log(LOG_ERROR, "Unable to allocate private structure, this is very bad.\n");
  800. return NULL;
  801. }
  802. /* figure out our local RTP port and tell the H.323 stack about it*/
  803. ast_rtp_get_us(p->rtp, &us);
  804. ast_rtp_get_peer(p->rtp, &them);
  805. info->addr = inet_ntoa(us.sin_addr);
  806. info->port = ntohs(us.sin_port);
  807. return info;
  808. }
  809. /**
  810. * Call-back function for incoming calls
  811. *
  812. * Returns 1 on success
  813. */
  814. int setup_incoming_call(call_details_t cd)
  815. {
  816. struct oh323_pvt *p = NULL;
  817. struct ast_channel *c = NULL;
  818. struct oh323_user *user = NULL;
  819. struct oh323_alias *alias = NULL;
  820. /* allocate the call*/
  821. p = oh323_alloc(cd.call_reference);
  822. if (!p) {
  823. ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
  824. return 0;
  825. }
  826. /* Populate the call details in the private structure */
  827. p->cd.call_token = cd.call_token;
  828. p->cd.call_source_aliases = cd.call_source_aliases;
  829. p->cd.call_dest_alias = cd.call_dest_alias;
  830. p->cd.call_source_e164 = cd.call_source_e164;
  831. p->cd.call_dest_e164 = cd.call_dest_e164;
  832. if (h323debug) {
  833. ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
  834. ast_verbose(VERBOSE_PREFIX_3 " Call token: [%s]\n", p->cd.call_token);
  835. ast_verbose(VERBOSE_PREFIX_3 " Calling party name: [%s]\n", p->cd.call_source_aliases);
  836. ast_verbose(VERBOSE_PREFIX_3 " Calling party number: [%s]\n", p->cd.call_source_e164);
  837. ast_verbose(VERBOSE_PREFIX_3 " Called party name: [%s]\n", p->cd.call_dest_alias);
  838. ast_verbose(VERBOSE_PREFIX_3 " Called party number: [%s]\n", p->cd.call_dest_e164);
  839. }
  840. /* Decide if we are allowing Gatekeeper routed calls*/
  841. if ((!strcasecmp(cd.sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk == 1)) {
  842. if (strlen(cd.call_dest_e164)) {
  843. strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
  844. strncpy(p->context, default_context, sizeof(p->context)-1);
  845. } else {
  846. alias = find_alias(cd.call_dest_alias);
  847. if (!alias) {
  848. ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd.call_dest_alias);
  849. return 0;
  850. }
  851. strncpy(p->exten, alias->name, sizeof(p->exten)-1);
  852. strncpy(p->context, alias->context, sizeof(p->context)-1);
  853. }
  854. sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
  855. } else {
  856. /* Either this call is not from the Gatekeeper
  857. or we are not allowing gk routed calls */
  858. user = find_user(cd);
  859. if (!user) {
  860. sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
  861. if (strlen(p->cd.call_dest_e164)) {
  862. strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
  863. } else {
  864. strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
  865. }
  866. if (!strlen(default_context)) {
  867. ast_log(LOG_ERROR, "Call from user '%s' rejected due to no default context\n", p->cd.call_source_aliases);
  868. return 0;
  869. }
  870. strncpy(p->context, default_context, sizeof(p->context)-1);
  871. ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd.call_source_aliases, p->context);
  872. } else {
  873. if (user->host) {
  874. if (strcasecmp(cd.sourceIp, inet_ntoa(user->addr.sin_addr))){
  875. if(!strlen(default_context)) {
  876. ast_log(LOG_ERROR, "Call from user '%s' rejected due to non-matching IP address of '%s'\n", user->name, cd.sourceIp);
  877. return 0;
  878. }
  879. strncpy(p->context, default_context, sizeof(p->context)-1);
  880. sprintf(p->exten,"i");
  881. goto exit;
  882. }
  883. }
  884. if (user->incominglimit > 0) {
  885. if (user->inUse >= user->incominglimit) {
  886. ast_log(LOG_ERROR, "Call from user '%s' rejected due to usage limit of %d\n", user->name, user->incominglimit);
  887. return 0;
  888. }
  889. }
  890. strncpy(p->context, user->context, sizeof(p->context)-1);
  891. p->bridge = user->bridge;
  892. p->nat = user->nat;
  893. if (strlen(user->callerid))
  894. strncpy(p->callerid, user->callerid, sizeof(p->callerid) - 1);
  895. else
  896. sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
  897. if (strlen(p->cd.call_dest_e164)) {
  898. strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
  899. } else {
  900. strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
  901. }
  902. if (strlen(user->accountcode)) {
  903. strncpy(p->accountcode, user->accountcode, sizeof(p->accountcode)-1);
  904. }
  905. /* Increment the usage counter */
  906. user->inUse++;
  907. }
  908. }
  909. /* I know this is horrid, don't kill me saddam */
  910. exit:
  911. /* allocate a channel and tell asterisk about it */
  912. c = oh323_new(p, AST_STATE_RINGING, cd.call_token);
  913. if (!c) {
  914. ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
  915. return 0;
  916. }
  917. return 1;
  918. }
  919. /**
  920. * Call-back function to establish an outgoing H.323 call
  921. *
  922. * Returns 1 on success
  923. */
  924. int setup_outgoing_call(call_details_t cd)
  925. {
  926. return 1;
  927. }
  928. #if 0
  929. if (p->inUse >= p->outgoinglimit) {
  930. ast_log(LOG_ERROR, "Call to %s rejected due to usage limit of %d outgoing channels\n", p->name, p->inUse);
  931. return 0;
  932. }
  933. if (!p) {
  934. ast_log(LOG_ERROR, "Rejecting call: peer %s not found\n", dest_peer);
  935. return 0;
  936. }
  937. #endif
  938. /**
  939. * Call-back function that gets called for each rtp channel opened
  940. *
  941. * Returns nothing
  942. */
  943. void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort)
  944. {
  945. struct oh323_pvt *p = NULL;
  946. struct sockaddr_in them;
  947. /* Find the call or allocate a private structure if call not found */
  948. p = find_call(call_reference);
  949. if (!p) {
  950. ast_log(LOG_ERROR, "Something is wrong: rtp\n");
  951. return;
  952. }
  953. them.sin_family = AF_INET;
  954. them.sin_addr.s_addr = inet_addr(remoteIp); // only works for IPv4
  955. them.sin_port = htons(remotePort);
  956. ast_rtp_set_peer(p->rtp, &them);
  957. return;
  958. }
  959. /**
  960. * Call-back function to signal asterisk that the channel has been answered
  961. * Returns nothing
  962. */
  963. void connection_made(unsigned call_reference)
  964. {
  965. struct ast_channel *c = NULL;
  966. struct oh323_pvt *p = NULL;
  967. p = find_call(call_reference);
  968. if (!p)
  969. ast_log(LOG_ERROR, "Something is wrong: connection\n");
  970. if (!p->owner) {
  971. ast_log(LOG_ERROR, "Channel has no owner\n");
  972. return;
  973. }
  974. c = p->owner;
  975. ast_setstate(c, AST_STATE_UP);
  976. return;
  977. }
  978. /**
  979. * Call-back function to cleanup communication
  980. * Returns nothing,
  981. */
  982. void cleanup_connection(call_details_t cd)
  983. {
  984. struct oh323_pvt *p = NULL;
  985. // struct oh323_peer *peer = NULL;
  986. struct oh323_user *user = NULL;
  987. struct ast_rtp *rtp = NULL;
  988. ast_log(LOG_DEBUG, "Cleaning up our mess\n");
  989. p = find_call(cd.call_reference);
  990. if (!p) {
  991. return;
  992. }
  993. /* Decrement usage counter */
  994. if (!p->outgoing) {
  995. user = find_user(cd);
  996. if(user)
  997. user->inUse--;
  998. }
  999. #if 0
  1000. if (p->outgoing) {
  1001. peer = find_peer(cd.call_dest_alias);
  1002. peer->inUse--;
  1003. } else {
  1004. user = find_user(cd);
  1005. user->inUse--;
  1006. }
  1007. #endif
  1008. if (p->rtp) {
  1009. rtp = p->rtp;
  1010. p->rtp = NULL;
  1011. /* Immediately stop RTP */
  1012. ast_rtp_destroy(rtp);
  1013. }
  1014. p->alreadygone = 1;
  1015. /* Send hangup */
  1016. if (p->owner)
  1017. ast_queue_hangup(p->owner, 1);
  1018. p = NULL;
  1019. return;
  1020. }
  1021. static void *do_monitor(void *data)
  1022. {
  1023. int res;
  1024. struct oh323_pvt *oh323 = NULL;
  1025. for(;;) {
  1026. /* Check for interfaces needing to be killed */
  1027. ast_mutex_lock(&iflock);
  1028. restartsearch:
  1029. oh323 = iflist;
  1030. while(oh323) {
  1031. if (oh323->needdestroy) {
  1032. __oh323_destroy(oh323);
  1033. goto restartsearch;
  1034. }
  1035. oh323 = oh323->next;
  1036. }
  1037. ast_mutex_unlock(&iflock);
  1038. /* Wait for sched or io */
  1039. res = ast_sched_wait(sched);
  1040. if ((res < 0) || (res > 1000))
  1041. res = 1000;
  1042. res = ast_io_wait(io, res);
  1043. pthread_testcancel();
  1044. ast_mutex_lock(&monlock);
  1045. if (res >= 0)
  1046. ast_sched_runq(sched);
  1047. ast_mutex_unlock(&monlock);
  1048. }
  1049. /* Never reached */
  1050. return NULL;
  1051. }
  1052. static int restart_monitor(void)
  1053. {
  1054. /* If we're supposed to be stopped -- stay stopped */
  1055. if (monitor_thread == AST_PTHREADT_STOP)
  1056. return 0;
  1057. if (ast_mutex_lock(&monlock)) {
  1058. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  1059. return -1;
  1060. }
  1061. if (monitor_thread == pthread_self()) {
  1062. ast_mutex_unlock(&monlock);
  1063. ast_log(LOG_WARNING, "Cannot kill myself\n");
  1064. return -1;
  1065. }
  1066. if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
  1067. /* Wake up the thread */
  1068. pthread_kill(monitor_thread, SIGURG);
  1069. } else {
  1070. /* Start a new monitor */
  1071. if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
  1072. ast_mutex_unlock(&monlock);
  1073. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  1074. return -1;
  1075. }
  1076. }
  1077. ast_mutex_unlock(&monlock);
  1078. return 0;
  1079. }
  1080. static int h323_do_trace(int fd, int argc, char *argv[])
  1081. {
  1082. if (argc != 3) {
  1083. return RESULT_SHOWUSAGE;
  1084. }
  1085. h323_debug(1, atoi(argv[2]));
  1086. ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
  1087. return RESULT_SUCCESS;
  1088. }
  1089. static int h323_no_trace(int fd, int argc, char *argv[])
  1090. {
  1091. if (argc != 3) {
  1092. return RESULT_SHOWUSAGE;
  1093. }
  1094. h323_debug(0,0);
  1095. ast_cli(fd, "H.323 trace disabled\n");
  1096. return RESULT_SUCCESS;
  1097. }
  1098. static int h323_do_debug(int fd, int argc, char *argv[])
  1099. {
  1100. if (argc != 2) {
  1101. return RESULT_SHOWUSAGE;
  1102. }
  1103. h323debug = 1;
  1104. ast_cli(fd, "H323 debug enabled\n");
  1105. return RESULT_SUCCESS;
  1106. }
  1107. static int h323_no_debug(int fd, int argc, char *argv[])
  1108. {
  1109. if (argc != 3) {
  1110. return RESULT_SHOWUSAGE;
  1111. }
  1112. h323debug = 0;
  1113. ast_cli(fd, "H323 Debug disabled\n");
  1114. return RESULT_SUCCESS;
  1115. }
  1116. static int h323_gk_cycle(int fd, int argc, char *argv[])
  1117. {
  1118. return RESULT_SUCCESS;
  1119. #if 0
  1120. if (argc != 3) {
  1121. return RESULT_SHOWUSAGE;
  1122. }
  1123. h323_gk_urq();
  1124. /* Possibly register with a GK */
  1125. if (!gatekeeper_disable) {
  1126. if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
  1127. ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
  1128. }
  1129. }
  1130. return RESULT_SUCCESS;
  1131. #endif
  1132. }
  1133. static int h323_ep_hangup(int fd, int argc, char *argv[])
  1134. {
  1135. if (argc != 3) {
  1136. return RESULT_SHOWUSAGE;
  1137. }
  1138. if (h323_soft_hangup(argv[2])) {
  1139. ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
  1140. } else {
  1141. ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
  1142. }
  1143. return RESULT_SUCCESS;
  1144. }
  1145. static int h323_tokens_show(int fd, int argc, char *argv[])
  1146. {
  1147. if (argc != 3) {
  1148. return RESULT_SHOWUSAGE;
  1149. }
  1150. h323_show_tokens();
  1151. return RESULT_SUCCESS;
  1152. }
  1153. static char trace_usage[] =
  1154. "Usage: h.323 trace <level num>\n"
  1155. " Enables H.323 stack tracing for debugging purposes\n";
  1156. static char no_trace_usage[] =
  1157. "Usage: h.323 no trace\n"
  1158. " Disables H.323 stack tracing for debugging purposes\n";
  1159. static char debug_usage[] =
  1160. "Usage: h.323 debug\n"
  1161. " Enables chan_h323 debug output\n";
  1162. static char no_debug_usage[] =
  1163. "Usage: h.323 no debug\n"
  1164. " Disables chan_h323 debug output\n";
  1165. static char show_codec_usage[] =
  1166. "Usage: h.323 show codec\n"
  1167. " Shows all enabled codecs\n";
  1168. static char show_cycle_usage[] =
  1169. "Usage: h.323 gk cycle\n"
  1170. " Manually re-register with the Gatekeper\n";
  1171. static char show_hangup_usage[] =
  1172. "Usage: h.323 hangup <token>\n"
  1173. " Manually try to hang up call identified by <token>\n";
  1174. static char show_tokens_usage[] =
  1175. "Usage: h.323 show tokens\n"
  1176. " Print out all active call tokens\n";
  1177. static struct ast_cli_entry cli_trace =
  1178. { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
  1179. static struct ast_cli_entry cli_no_trace =
  1180. { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
  1181. static struct ast_cli_entry cli_debug =
  1182. { { "h.323", "debug", NULL }, h323_do_debug, "Enable chan_h323 debug", debug_usage };
  1183. static struct ast_cli_entry cli_no_debug =
  1184. { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable chan_h323 debug", no_debug_usage };
  1185. static struct ast_cli_entry cli_show_codecs =
  1186. { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
  1187. static struct ast_cli_entry cli_gk_cycle =
  1188. { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
  1189. static struct ast_cli_entry cli_hangup_call =
  1190. { { "h.323", "hangup", NULL }, h323_ep_hangup, "Manually try to hang up a call", show_hangup_usage };
  1191. static struct ast_cli_entry cli_show_tokens =
  1192. { { "h.323", "show", "tokens", NULL }, h323_tokens_show, "Manually try to hang up a call", show_tokens_usage };
  1193. int reload_config(void)
  1194. {
  1195. int format;
  1196. struct ast_config *cfg;
  1197. struct ast_variable *v;
  1198. struct oh323_peer *peer = NULL;
  1199. struct oh323_user *user = NULL;
  1200. struct oh323_alias *alias = NULL;
  1201. struct ast_hostent ahp; struct hostent *hp;
  1202. char *cat;
  1203. char *utype;
  1204. cfg = ast_load(config);
  1205. /* We *must* have a config file otherwise stop immediately */
  1206. if (!cfg) {
  1207. ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
  1208. return 1;
  1209. }
  1210. /* fire up the H.323 Endpoint */
  1211. if (!h323_end_point_exist()) {
  1212. h323_end_point_create();
  1213. }
  1214. h323debug=0;
  1215. dtmfmode = H323_DTMF_RFC2833;
  1216. memset(&bindaddr, 0, sizeof(bindaddr));
  1217. v = ast_variable_browse(cfg, "general");
  1218. while(v) {
  1219. /* Create the interface list */
  1220. if (!strcasecmp(v->name, "port")) {
  1221. port = (int)strtol(v->value, NULL, 10);
  1222. } else if (!strcasecmp(v->name, "bindaddr")) {
  1223. if (!(hp = ast_gethostbyname(v->value, &ahp))) {
  1224. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  1225. } else {
  1226. memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
  1227. }
  1228. } else if (!strcasecmp(v->name, "allow")) {
  1229. format = ast_getformatbyname(v->value);
  1230. if (format < 1)
  1231. ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
  1232. else
  1233. capability |= format;
  1234. } else if (!strcasecmp(v->name, "disallow")) {
  1235. format = ast_getformatbyname(v->value);
  1236. if (format < 1)
  1237. ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
  1238. else
  1239. capability &= ~format;
  1240. } else if (!strcasecmp(v->name, "tos")) {
  1241. if (sscanf(v->value, "%i", &format) == 1)
  1242. tos = format & 0xff;
  1243. else if (!strcasecmp(v->value, "lowdelay"))
  1244. tos = IPTOS_LOWDELAY;
  1245. else if (!strcasecmp(v->value, "throughput"))
  1246. tos = IPTOS_THROUGHPUT;
  1247. else if (!strcasecmp(v->value, "reliability"))
  1248. tos = IPTOS_RELIABILITY;
  1249. else if (!strcasecmp(v->value, "mincost"))
  1250. tos = IPTOS_MINCOST;
  1251. else if (!strcasecmp(v->value, "none"))
  1252. tos = 0;
  1253. else
  1254. ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
  1255. } else if (!strcasecmp(v->name, "gatekeeper")) {
  1256. if (!strcasecmp(v->value, "DISABLE")) {
  1257. gatekeeper_disable = 1;
  1258. usingGk = 0;
  1259. } else if (!strcasecmp(v->value, "DISCOVER")) {
  1260. gatekeeper_disable = 0;
  1261. gatekeeper_discover = 1;
  1262. usingGk = 1;
  1263. } else {
  1264. gatekeeper_disable = 0;
  1265. usingGk = 1;
  1266. strncpy(gatekeeper, v->value, sizeof(gatekeeper)-1);
  1267. }
  1268. } else if (!strcasecmp(v->name, "secret")) {
  1269. strncpy(secret, v->value, sizeof(secret)-1);
  1270. } else if (!strcasecmp(v->name, "AllowGKRouted")) {
  1271. gkroute = ast_true(v->value);
  1272. } else if (!strcasecmp(v->name, "context")) {
  1273. strncpy(default_context, v->value, sizeof(default_context)-1);
  1274. ast_verbose(VERBOSE_PREFIX_3 " == Setting default context to %s\n", default_context);
  1275. } else if (!strcasecmp(v->name, "dtmfmode")) {
  1276. if (!strcasecmp(v->value, "inband"))
  1277. dtmfmode=H323_DTMF_INBAND;
  1278. else if (!strcasecmp(v->value, "rfc2833"))
  1279. dtmfmode = H323_DTMF_RFC2833;
  1280. else {
  1281. ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
  1282. dtmfmode = H323_DTMF_RFC2833;
  1283. }
  1284. } else if (!strcasecmp(v->name, "UserByAlias")) {
  1285. userbyalias = ast_true(v->value);
  1286. } else if (!strcasecmp(v->name, "bridge")) {
  1287. bridge_default = ast_true(v->value);
  1288. }
  1289. v = v->next;
  1290. }
  1291. cat = ast_category_browse(cfg, NULL);
  1292. while(cat) {
  1293. if (strcasecmp(cat, "general")) {
  1294. utype = ast_variable_retrieve(cfg, cat, "type");
  1295. if (utype) {
  1296. if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
  1297. user = build_user(cat, ast_variable_browse(cfg, cat));
  1298. if (user) {
  1299. ast_mutex_lock(&userl.lock);
  1300. user->next = userl.users;
  1301. userl.users = user;
  1302. ast_mutex_unlock(&userl.lock);
  1303. }
  1304. } else if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
  1305. peer = build_peer(cat, ast_variable_browse(cfg, cat));
  1306. if (peer) {
  1307. ast_mutex_lock(&peerl.lock);
  1308. peer->next = peerl.peers;
  1309. peerl.peers = peer;
  1310. ast_mutex_unlock(&peerl.lock);
  1311. }
  1312. } else if (!strcasecmp(utype, "h323")) {
  1313. alias = build_alias(cat, ast_variable_browse(cfg, cat));
  1314. if (alias) {
  1315. ast_mutex_lock(&aliasl.lock);
  1316. alias->next = aliasl.aliases;
  1317. aliasl.aliases = alias;
  1318. ast_mutex_unlock(&aliasl.lock);
  1319. }
  1320. } else {
  1321. ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
  1322. }
  1323. } else
  1324. ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
  1325. }
  1326. cat = ast_category_browse(cfg, cat);
  1327. }
  1328. /* Register our H.323 aliases if any*/
  1329. while (alias) {
  1330. if (h323_set_alias(alias)) {
  1331. ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
  1332. return -1;
  1333. }
  1334. alias = alias->next;
  1335. }
  1336. /* Add some capabilities */
  1337. if(h323_set_capability(capability, dtmfmode)) {
  1338. ast_log(LOG_ERROR, "Capabilities failure, this is bad.\n");
  1339. return -1;
  1340. }
  1341. ast_destroy(cfg);
  1342. return 0;
  1343. }
  1344. void delete_users(void)
  1345. {
  1346. struct oh323_user *user, *userlast;
  1347. struct oh323_peer *peer;
  1348. /* Delete all users */
  1349. ast_mutex_lock(&userl.lock);
  1350. for (user=userl.users;user;) {
  1351. userlast = user;
  1352. user=user->next;
  1353. free(userlast);
  1354. }
  1355. userl.users=NULL;
  1356. ast_mutex_unlock(&userl.lock);
  1357. ast_mutex_lock(&peerl.lock);
  1358. for (peer=peerl.peers;peer;) {
  1359. /* Assume all will be deleted, and we'll find out for sure later */
  1360. peer->delme = 1;
  1361. peer = peer->next;
  1362. }
  1363. ast_mutex_unlock(&peerl.lock);
  1364. }
  1365. void delete_aliases(void)
  1366. {
  1367. struct oh323_alias *alias, *aliaslast;
  1368. /* Delete all users */
  1369. ast_mutex_lock(&aliasl.lock);
  1370. for (alias=aliasl.aliases;alias;) {
  1371. aliaslast = alias;
  1372. alias=alias->next;
  1373. free(aliaslast);
  1374. }
  1375. aliasl.aliases=NULL;
  1376. ast_mutex_unlock(&aliasl.lock);
  1377. }
  1378. void prune_peers(void)
  1379. {
  1380. /* Prune peers who still are supposed to be deleted */
  1381. struct oh323_peer *peer, *peerlast, *peernext;
  1382. ast_mutex_lock(&peerl.lock);
  1383. peerlast = NULL;
  1384. for (peer=peerl.peers;peer;) {
  1385. peernext = peer->next;
  1386. if (peer->delme) {
  1387. free(peer);
  1388. if (peerlast)
  1389. peerlast->next = peernext;
  1390. else
  1391. peerl.peers = peernext;
  1392. } else
  1393. peerlast = peer;
  1394. peer=peernext;
  1395. }
  1396. ast_mutex_unlock(&peerl.lock);
  1397. }
  1398. int reload(void)
  1399. {
  1400. delete_users();
  1401. delete_aliases();
  1402. prune_peers();
  1403. #if 0
  1404. if (strlen(gatekeeper)) {
  1405. h323_gk_urq();
  1406. }
  1407. #endif
  1408. reload_config();
  1409. #if 0
  1410. /* Possibly register with a GK */
  1411. if (gatekeeper_disable == 0) {
  1412. if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
  1413. ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
  1414. h323_end_process();
  1415. return -1;
  1416. }
  1417. }
  1418. #endif
  1419. restart_monitor();
  1420. return 0;
  1421. }
  1422. static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
  1423. {
  1424. struct oh323_pvt *p;
  1425. p = chan->pvt->pvt;
  1426. if (p && p->rtp && p->bridge) {
  1427. return p->rtp;
  1428. }
  1429. return NULL;
  1430. }
  1431. static struct ast_rtp *oh323_get_vrtp_peer(struct ast_channel *chan)
  1432. {
  1433. return NULL;
  1434. }
  1435. static char *convertcap(int cap)
  1436. {
  1437. switch (cap) {
  1438. case AST_FORMAT_G723_1:
  1439. return "G.723";
  1440. case AST_FORMAT_GSM:
  1441. return "GSM";
  1442. case AST_FORMAT_ULAW:
  1443. return "ULAW";
  1444. case AST_FORMAT_ALAW:
  1445. return "ALAW";
  1446. case AST_FORMAT_ADPCM:
  1447. return "G.728";
  1448. case AST_FORMAT_G729A:
  1449. return "G.729";
  1450. case AST_FORMAT_SPEEX:
  1451. return "SPEEX";
  1452. case AST_FORMAT_ILBC:
  1453. return "ILBC";
  1454. default:
  1455. ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
  1456. return NULL;
  1457. }
  1458. }
  1459. static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp)
  1460. {
  1461. /* XXX Deal with Video */
  1462. struct oh323_pvt *p;
  1463. struct sockaddr_in them;
  1464. struct sockaddr_in us;
  1465. char *mode;
  1466. mode = convertcap(chan->writeformat);
  1467. if (!rtp) {
  1468. return 0;
  1469. }
  1470. p = chan->pvt->pvt;
  1471. if (!p) {
  1472. ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
  1473. return -1;
  1474. }
  1475. ast_rtp_get_peer(rtp, &them);
  1476. ast_rtp_get_us(rtp, &us);
  1477. h323_native_bridge(p->cd.call_token, inet_ntoa(them.sin_addr), mode);
  1478. return 0;
  1479. }
  1480. static struct ast_rtp_protocol oh323_rtp = {
  1481. get_rtp_info: oh323_get_rtp_peer,
  1482. get_vrtp_info: oh323_get_vrtp_peer,
  1483. set_rtp_peer: oh323_set_rtp_peer,
  1484. };
  1485. int load_module()
  1486. {
  1487. int res;
  1488. res = reload_config();
  1489. if (res) {
  1490. return 0;
  1491. } else {
  1492. /* Make sure we can register our channel type */
  1493. if (ast_channel_register(type, tdesc, capability, oh323_request)) {
  1494. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  1495. h323_end_process();
  1496. return -1;
  1497. }
  1498. ast_cli_register(&cli_debug);
  1499. ast_cli_register(&cli_no_debug);
  1500. ast_cli_register(&cli_trace);
  1501. ast_cli_register(&cli_no_trace);
  1502. ast_cli_register(&cli_show_codecs);
  1503. ast_cli_register(&cli_gk_cycle);
  1504. ast_cli_register(&cli_hangup_call);
  1505. ast_cli_register(&cli_show_tokens);
  1506. oh323_rtp.type = type;
  1507. ast_rtp_proto_register(&oh323_rtp);
  1508. sched = sched_context_create();
  1509. if (!sched) {
  1510. ast_log(LOG_WARNING, "Unable to create schedule context\n");
  1511. }
  1512. io = io_context_create();
  1513. if (!io) {
  1514. ast_log(LOG_WARNING, "Unable to create I/O context\n");
  1515. }
  1516. /* Register our callback functions */
  1517. h323_callback_register(setup_incoming_call,
  1518. setup_outgoing_call,
  1519. create_connection,
  1520. setup_rtp_connection,
  1521. cleanup_connection,
  1522. connection_made,
  1523. send_digit);
  1524. /* start the h.323 listener */
  1525. if (h323_start_listener(port, bindaddr)) {
  1526. ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
  1527. // h323_end_process();
  1528. return -1;
  1529. }
  1530. /* Possibly register with a GK */
  1531. if (gatekeeper_disable == 0) {
  1532. if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
  1533. ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
  1534. // h323_end_process();
  1535. return 0;
  1536. }
  1537. }
  1538. /* And start the monitor for the first time */
  1539. restart_monitor();
  1540. }
  1541. return res;
  1542. }
  1543. int unload_module()
  1544. {
  1545. struct oh323_pvt *p, *pl;
  1546. if (!ast_mutex_lock(&iflock)) {
  1547. /* hangup all interfaces if they have an owner */
  1548. p = iflist;
  1549. while(p) {
  1550. if (p->owner)
  1551. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  1552. p = p->next;
  1553. }
  1554. iflist = NULL;
  1555. ast_mutex_unlock(&iflock);
  1556. } else {
  1557. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  1558. return -1;
  1559. }
  1560. if (!ast_mutex_lock(&monlock)) {
  1561. if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
  1562. pthread_cancel(monitor_thread);
  1563. pthread_kill(monitor_thread, SIGURG);
  1564. pthread_join(monitor_thread, NULL);
  1565. }
  1566. monitor_thread = AST_PTHREADT_STOP;
  1567. ast_mutex_unlock(&monlock);
  1568. } else {
  1569. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  1570. return -1;
  1571. }
  1572. if (!ast_mutex_lock(&iflock)) {
  1573. /* destroy all the interfaces and free their memory */
  1574. p = iflist;
  1575. while(p) {
  1576. pl = p;
  1577. p = p->next;
  1578. /* free associated memory */
  1579. free(pl);
  1580. }
  1581. iflist = NULL;
  1582. ast_mutex_unlock(&iflock);
  1583. } else {
  1584. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  1585. return -1;
  1586. }
  1587. h323_gk_urq();
  1588. h323_end_process();
  1589. /* unregister rtp */
  1590. ast_rtp_proto_unregister(&oh323_rtp);
  1591. /* unregister commands */
  1592. ast_cli_unregister(&cli_debug);
  1593. ast_cli_unregister(&cli_no_debug);
  1594. ast_cli_unregister(&cli_trace);
  1595. ast_cli_unregister(&cli_no_trace);
  1596. ast_cli_unregister(&cli_show_codecs);
  1597. ast_cli_unregister(&cli_gk_cycle);
  1598. ast_cli_unregister(&cli_hangup_call);
  1599. ast_cli_unregister(&cli_show_tokens);
  1600. /* unregister channel type */
  1601. ast_channel_unregister(type);
  1602. return 0;
  1603. }
  1604. int usecount()
  1605. {
  1606. int res;
  1607. ast_mutex_lock(&usecnt_lock);
  1608. res = usecnt;
  1609. ast_mutex_unlock(&usecnt_lock);
  1610. return res;
  1611. }
  1612. char *description()
  1613. {
  1614. return desc;
  1615. }
  1616. char *key()
  1617. {
  1618. return ASTERISK_GPL_KEY;
  1619. }