chan_h323.c 54 KB

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