chan_h323.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472
  1. /*
  2. * Asterisk -- An open source telephony toolkit.
  3. *
  4. * Copyright (C) 1999 - 2005
  5. *
  6. * OpenH323 Channel Driver for ASTERISK PBX.
  7. * By Jeremy McNamara
  8. * For The NuFone Network
  9. *
  10. * chan_h323 has been derived from code created by
  11. * Michael Manousos and Mark Spencer
  12. *
  13. * See http://www.asterisk.org for more information about
  14. * the Asterisk project. Please do not directly contact
  15. * any of the maintainers of this project for assistance;
  16. * the project provides a web site, mailing lists and IRC
  17. * channels for your use.
  18. *
  19. * This program is free software, distributed under the terms of
  20. * the GNU General Public License Version 2. See the LICENSE file
  21. * at the top of the source tree.
  22. */
  23. /*! \file
  24. *
  25. * \brief This file is part of the chan_h323 driver for Asterisk
  26. *
  27. * \par See also
  28. * \arg Config_h323
  29. *
  30. * \ingroup channel_drivers
  31. */
  32. #include <sys/socket.h>
  33. #include <sys/signal.h>
  34. #include <sys/param.h>
  35. #if defined(BSD)
  36. #ifndef IPTOS_MINCOST
  37. #define IPTOS_MINCOST 0x02
  38. #endif
  39. #endif
  40. #include <arpa/inet.h>
  41. #include <net/if.h>
  42. #include <netinet/in.h>
  43. #include <netinet/in_systm.h>
  44. #include <netinet/ip.h>
  45. #include <unistd.h>
  46. #include <stdlib.h>
  47. #include <netdb.h>
  48. #include <stdio.h>
  49. #include <string.h>
  50. #include <errno.h>
  51. #include <fcntl.h>
  52. #ifdef __cplusplus
  53. extern "C" {
  54. #endif
  55. #include "asterisk.h"
  56. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  57. #include "asterisk/lock.h"
  58. #include "asterisk/logger.h"
  59. #include "asterisk/channel.h"
  60. #include "asterisk/config.h"
  61. #include "asterisk/module.h"
  62. #include "asterisk/pbx.h"
  63. #include "asterisk/options.h"
  64. #include "asterisk/utils.h"
  65. #include "asterisk/lock.h"
  66. #include "asterisk/sched.h"
  67. #include "asterisk/io.h"
  68. #include "asterisk/rtp.h"
  69. #include "asterisk/acl.h"
  70. #include "asterisk/callerid.h"
  71. #include "asterisk/cli.h"
  72. #include "asterisk/dsp.h"
  73. #include "asterisk/causes.h"
  74. #ifdef __cplusplus
  75. }
  76. #endif
  77. #include "h323/chan_h323.h"
  78. send_digit_cb on_send_digit;
  79. on_rtp_cb on_external_rtp_create;
  80. start_rtp_cb on_start_rtp_channel;
  81. setup_incoming_cb on_incoming_call;
  82. setup_outbound_cb on_outgoing_call;
  83. chan_ringing_cb on_chan_ringing;
  84. con_established_cb on_connection_established;
  85. clear_con_cb on_connection_cleared;
  86. answer_call_cb on_answer_call;
  87. progress_cb on_progress;
  88. rfc2833_cb on_set_rfc2833_payload;
  89. hangup_cb on_hangup;
  90. setcapabilities_cb on_setcapabilities;
  91. /* global debug flag */
  92. int h323debug;
  93. /** Variables required by Asterisk */
  94. static const char type[] = "H323";
  95. static const char desc[] = "The NuFone Network's Open H.323 Channel Driver";
  96. static const char tdesc[] = "The NuFone Network's Open H.323 Channel Driver";
  97. static const char config[] = "h323.conf";
  98. static char default_context[AST_MAX_CONTEXT] = "default";
  99. static struct sockaddr_in bindaddr;
  100. /** H.323 configuration values */
  101. static int h323_signalling_port = 1720;
  102. static char gatekeeper[100];
  103. static int gatekeeper_disable = 1;
  104. static int gatekeeper_discover = 0;
  105. static int usingGk = 0;
  106. static int gkroute = 0;
  107. /* Find user by alias (h.323 id) is default, alternative is the incomming call's source IP address*/
  108. static int userbyalias = 1;
  109. static int tos = 0;
  110. static char secret[50];
  111. static unsigned int unique = 0;
  112. static call_options_t global_options;
  113. /** Private structure of a OpenH323 channel */
  114. struct oh323_pvt {
  115. ast_mutex_t lock; /* Channel private lock */
  116. call_options_t options; /* Options to be used during call setup */
  117. int alreadygone; /* Whether or not we've already been destroyed by our peer */
  118. int needdestroy; /* if we need to be destroyed */
  119. call_details_t cd; /* Call details */
  120. struct ast_channel *owner; /* Who owns us */
  121. struct sockaddr_in sa; /* Our peer */
  122. struct sockaddr_in redirip; /* Where our RTP should be going if not to us */
  123. int nonCodecCapability; /* non-audio capability */
  124. int outgoing; /* Outgoing or incoming call? */
  125. char exten[AST_MAX_EXTENSION]; /* Requested extension */
  126. char context[AST_MAX_CONTEXT]; /* Context where to start */
  127. char accountcode[256]; /* Account code */
  128. char cid_num[80]; /* Caller*id number, if available */
  129. char cid_name[80]; /* Caller*id name, if available */
  130. char rdnis[80]; /* Referring DNIS, if available */
  131. int amaflags; /* AMA Flags */
  132. struct ast_rtp *rtp; /* RTP Session */
  133. struct ast_dsp *vad; /* Used for in-band DTMF detection */
  134. int nativeformats; /* Codec formats supported by a channel */
  135. int needhangup; /* Send hangup when Asterisk is ready */
  136. int hangupcause; /* Hangup cause from OpenH323 layer */
  137. int newstate; /* Pending state change */
  138. int newcontrol; /* Pending control to send */
  139. struct oh323_pvt *next; /* Next channel in list */
  140. } *iflist = NULL;
  141. static struct ast_user_list {
  142. struct oh323_user *users;
  143. ast_mutex_t lock;
  144. } userl;
  145. static struct ast_peer_list {
  146. struct oh323_peer *peers;
  147. ast_mutex_t lock;
  148. } peerl;
  149. static struct ast_alias_list {
  150. struct oh323_alias *aliases;
  151. ast_mutex_t lock;
  152. } aliasl;
  153. /** Asterisk RTP stuff */
  154. static struct sched_context *sched;
  155. static struct io_context *io;
  156. /** Protect the interface list (oh323_pvt) */
  157. AST_MUTEX_DEFINE_STATIC(iflock);
  158. /** Usage counter and associated lock */
  159. static int usecnt = 0;
  160. AST_MUTEX_DEFINE_STATIC(usecnt_lock);
  161. /* Protect the monitoring thread, so only one process can kill or start it, and not
  162. when it's doing something critical. */
  163. AST_MUTEX_DEFINE_STATIC(monlock);
  164. /* Protect the H.323 capabilities list, to avoid more than one channel to set the capabilities simultaneaously in the h323 stack. */
  165. AST_MUTEX_DEFINE_STATIC(caplock);
  166. /* Protect the reload process */
  167. AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
  168. static int h323_reloading = 0;
  169. /* This is the thread for the monitor which checks for input on the channels
  170. which are not currently in use. */
  171. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  172. static int restart_monitor(void);
  173. static int h323_do_reload(void);
  174. static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause);
  175. static int oh323_digit(struct ast_channel *c, char digit);
  176. static int oh323_call(struct ast_channel *c, char *dest, int timeout);
  177. static int oh323_hangup(struct ast_channel *c);
  178. static int oh323_answer(struct ast_channel *c);
  179. static struct ast_frame *oh323_read(struct ast_channel *c);
  180. static int oh323_write(struct ast_channel *c, struct ast_frame *frame);
  181. static int oh323_indicate(struct ast_channel *c, int condition);
  182. static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  183. static const struct ast_channel_tech oh323_tech = {
  184. .type = type,
  185. .description = tdesc,
  186. .capabilities = AST_FORMAT_ULAW,
  187. .properties = AST_CHAN_TP_WANTSJITTER,
  188. .requester = oh323_request,
  189. .send_digit = oh323_digit,
  190. .call = oh323_call,
  191. .hangup = oh323_hangup,
  192. .answer = oh323_answer,
  193. .read = oh323_read,
  194. .write = oh323_write,
  195. .indicate = oh323_indicate,
  196. .fixup = oh323_fixup,
  197. /* disable, for now */
  198. #if 0
  199. .bridge = ast_rtp_bridge,
  200. #endif
  201. };
  202. /* Channel and private structures should be already locked */
  203. static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
  204. {
  205. if (c->nativeformats != pvt->nativeformats) {
  206. if (h323debug)
  207. ast_log(LOG_DEBUG, "Preparing %s for new native format\n", c->name);
  208. c->nativeformats = pvt->nativeformats;
  209. ast_set_read_format(c, c->readformat);
  210. ast_set_write_format(c, c->writeformat);
  211. }
  212. if (pvt->needhangup) {
  213. if (h323debug)
  214. ast_log(LOG_DEBUG, "Process pending hangup for %s\n", c->name);
  215. c->_softhangup |= AST_SOFTHANGUP_DEV;
  216. c->hangupcause = pvt->hangupcause;
  217. ast_queue_hangup(c);
  218. pvt->needhangup = 0;
  219. pvt->newstate = pvt->newcontrol = -1;
  220. }
  221. if (pvt->newstate >= 0) {
  222. ast_setstate(c, pvt->newstate);
  223. pvt->newstate = -1;
  224. }
  225. if (pvt->newcontrol >= 0) {
  226. ast_queue_control(c, pvt->newcontrol);
  227. pvt->newcontrol = -1;
  228. }
  229. }
  230. /* Only channel structure should be locked */
  231. static void oh323_update_info(struct ast_channel *c)
  232. {
  233. struct oh323_pvt *pvt = c->tech_pvt;
  234. if (pvt) {
  235. ast_mutex_lock(&pvt->lock);
  236. __oh323_update_info(c, pvt);
  237. ast_mutex_unlock(&pvt->lock);
  238. }
  239. }
  240. static void cleanup_call_details(call_details_t *cd)
  241. {
  242. if (cd->call_token) {
  243. free(cd->call_token);
  244. cd->call_token = NULL;
  245. }
  246. if (cd->call_source_aliases) {
  247. free(cd->call_source_aliases);
  248. cd->call_source_aliases = NULL;
  249. }
  250. if (cd->call_dest_alias) {
  251. free(cd->call_dest_alias);
  252. cd->call_dest_alias = NULL;
  253. }
  254. if (cd->call_source_name) {
  255. free(cd->call_source_name);
  256. cd->call_source_name = NULL;
  257. }
  258. if (cd->call_source_e164) {
  259. free(cd->call_source_e164);
  260. cd->call_source_e164 = NULL;
  261. }
  262. if (cd->call_dest_e164) {
  263. free(cd->call_dest_e164);
  264. cd->call_dest_e164 = NULL;
  265. }
  266. if (cd->sourceIp) {
  267. free(cd->sourceIp);
  268. cd->sourceIp = NULL;
  269. }
  270. }
  271. static void __oh323_destroy(struct oh323_pvt *pvt)
  272. {
  273. struct oh323_pvt *cur, *prev = NULL;
  274. if (pvt->rtp) {
  275. ast_rtp_destroy(pvt->rtp);
  276. }
  277. /* Free dsp used for in-band DTMF detection */
  278. if (pvt->vad) {
  279. ast_dsp_free(pvt->vad);
  280. }
  281. cleanup_call_details(&pvt->cd);
  282. /* Unlink us from the owner if we have one */
  283. if (pvt->owner) {
  284. ast_mutex_lock(&pvt->owner->lock);
  285. ast_log(LOG_DEBUG, "Detaching from %s\n", pvt->owner->name);
  286. pvt->owner->tech_pvt = NULL;
  287. ast_mutex_unlock(&pvt->owner->lock);
  288. }
  289. cur = iflist;
  290. while(cur) {
  291. if (cur == pvt) {
  292. if (prev)
  293. prev->next = cur->next;
  294. else
  295. iflist = cur->next;
  296. break;
  297. }
  298. prev = cur;
  299. cur = cur->next;
  300. }
  301. if (!cur) {
  302. ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
  303. } else {
  304. ast_mutex_destroy(&pvt->lock);
  305. free(pvt);
  306. }
  307. }
  308. static void oh323_destroy(struct oh323_pvt *pvt)
  309. {
  310. ast_mutex_lock(&iflock);
  311. __oh323_destroy(pvt);
  312. ast_mutex_unlock(&iflock);
  313. }
  314. /**
  315. * Send (play) the specified digit to the channel.
  316. *
  317. */
  318. static int oh323_digit(struct ast_channel *c, char digit)
  319. {
  320. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
  321. char *token;
  322. if (!pvt) {
  323. ast_log(LOG_ERROR, "No private structure?! This is bad\n");
  324. return -1;
  325. }
  326. ast_mutex_lock(&pvt->lock);
  327. if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833)) {
  328. /* out-of-band DTMF */
  329. if (h323debug) {
  330. ast_log(LOG_DEBUG, "Sending out-of-band digit %c on %s\n", digit, c->name);
  331. }
  332. ast_rtp_senddigit(pvt->rtp, digit);
  333. } else {
  334. /* in-band DTMF */
  335. if (h323debug) {
  336. ast_log(LOG_DEBUG, "Sending inband digit %c on %s\n", digit, c->name);
  337. }
  338. token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
  339. h323_send_tone(token, digit);
  340. if (token) {
  341. free(token);
  342. }
  343. }
  344. ast_mutex_unlock(&pvt->lock);
  345. oh323_update_info(c);
  346. return 0;
  347. }
  348. /**
  349. * Make a call over the specified channel to the specified
  350. * destination.
  351. * Returns -1 on error, 0 on success.
  352. */
  353. static int oh323_call(struct ast_channel *c, char *dest, int timeout)
  354. {
  355. int res = 0;
  356. struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
  357. char addr[INET_ADDRSTRLEN];
  358. char called_addr[1024];
  359. if (h323debug) {
  360. ast_log(LOG_DEBUG, "Calling to %s on %s\n", dest, c->name);
  361. }
  362. if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
  363. ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
  364. return -1;
  365. }
  366. ast_mutex_lock(&pvt->lock);
  367. if (usingGk) {
  368. if (ast_strlen_zero(pvt->exten)) {
  369. strncpy(called_addr, dest, sizeof(called_addr));
  370. } else {
  371. snprintf(called_addr, sizeof(called_addr), "%s@%s", pvt->exten, dest);
  372. }
  373. } else {
  374. ast_inet_ntoa(addr, sizeof(addr), pvt->sa.sin_addr);
  375. res = htons(pvt->sa.sin_port);
  376. if (ast_strlen_zero(pvt->exten)) {
  377. snprintf(called_addr, sizeof(called_addr), "%s:%d", addr, res);
  378. } else {
  379. snprintf(called_addr, sizeof(called_addr), "%s@%s:%d", pvt->exten, addr, res);
  380. }
  381. }
  382. /* make sure null terminated */
  383. called_addr[sizeof(called_addr) - 1] = '\0';
  384. if (c->cid.cid_num) {
  385. strncpy(pvt->options.cid_num, c->cid.cid_num, sizeof(pvt->options.cid_num));
  386. }
  387. if (c->cid.cid_name) {
  388. strncpy(pvt->options.cid_name, c->cid.cid_name, sizeof(pvt->options.cid_name));
  389. }
  390. /* indicate that this is an outgoing call */
  391. pvt->outgoing = 1;
  392. ast_log(LOG_DEBUG, "Placing outgoing call to %s, %d\n", called_addr, pvt->options.dtmfcodec);
  393. ast_mutex_unlock(&pvt->lock);
  394. res = h323_make_call(called_addr, &(pvt->cd), &pvt->options);
  395. if (res) {
  396. ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
  397. return -1;
  398. }
  399. oh323_update_info(c);
  400. return 0;
  401. }
  402. static int oh323_answer(struct ast_channel *c)
  403. {
  404. int res;
  405. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
  406. char *token;
  407. if (h323debug)
  408. ast_log(LOG_DEBUG, "Answering on %s\n", c->name);
  409. ast_mutex_lock(&pvt->lock);
  410. token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
  411. ast_mutex_unlock(&pvt->lock);
  412. res = h323_answering_call(token, 0);
  413. if (token)
  414. free(token);
  415. oh323_update_info(c);
  416. if (c->_state != AST_STATE_UP) {
  417. ast_setstate(c, AST_STATE_UP);
  418. }
  419. return res;
  420. }
  421. static int oh323_hangup(struct ast_channel *c)
  422. {
  423. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
  424. int needcancel = 0;
  425. int q931cause = AST_CAUSE_NORMAL_CLEARING;
  426. char *call_token;
  427. if (h323debug)
  428. ast_log(LOG_DEBUG, "Hanging up call %s\n", c->name);
  429. if (!c->tech_pvt) {
  430. ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
  431. return 0;
  432. }
  433. ast_mutex_lock(&pvt->lock);
  434. /* Determine how to disconnect */
  435. if (pvt->owner != c) {
  436. ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
  437. ast_mutex_unlock(&pvt->lock);
  438. return 0;
  439. }
  440. if (!c || (c->_state != AST_STATE_UP)) {
  441. needcancel = 1;
  442. }
  443. pvt->owner = NULL;
  444. c->tech_pvt = NULL;
  445. if (c->hangupcause) {
  446. q931cause = c->hangupcause;
  447. } else {
  448. char *cause = pbx_builtin_getvar_helper(c, "DIALSTATUS");
  449. if (cause) {
  450. if (!strcmp(cause, "CONGESTION")) {
  451. q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
  452. } else if (!strcmp(cause, "BUSY")) {
  453. q931cause = AST_CAUSE_USER_BUSY;
  454. } else if (!strcmp(cause, "CHANISUNVAIL")) {
  455. q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
  456. } else if (!strcmp(cause, "NOANSWER")) {
  457. q931cause = AST_CAUSE_NO_ANSWER;
  458. } else if (!strcmp(cause, "CANCEL")) {
  459. q931cause = AST_CAUSE_CALL_REJECTED;
  460. }
  461. }
  462. }
  463. /* Start the process if it's not already started */
  464. if (!pvt->alreadygone && !pvt->hangupcause) {
  465. call_token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
  466. if (call_token) {
  467. /* Release lock to eliminate deadlock */
  468. ast_mutex_unlock(&pvt->lock);
  469. if (h323_clear_call(call_token, q931cause)) {
  470. ast_log(LOG_DEBUG, "ClearCall failed.\n");
  471. }
  472. free(call_token);
  473. ast_mutex_lock(&pvt->lock);
  474. }
  475. }
  476. pvt->needdestroy = 1;
  477. /* Update usage counter */
  478. ast_mutex_lock(&usecnt_lock);
  479. usecnt--;
  480. if (usecnt < 0) {
  481. ast_log(LOG_WARNING, "Usecnt < 0\n");
  482. }
  483. ast_mutex_unlock(&usecnt_lock);
  484. ast_mutex_unlock(&pvt->lock);
  485. ast_update_use_count();
  486. return 0;
  487. }
  488. static struct ast_frame *oh323_rtp_read(struct oh323_pvt *pvt)
  489. {
  490. /* Retrieve audio/etc from channel. Assumes pvt->lock is already held. */
  491. struct ast_frame *f;
  492. static struct ast_frame null_frame = { AST_FRAME_NULL, };
  493. /* Only apply it for the first packet, we just need the correct ip/port */
  494. if (pvt->options.nat) {
  495. ast_rtp_setnat(pvt->rtp, pvt->options.nat);
  496. pvt->options.nat = 0;
  497. }
  498. f = ast_rtp_read(pvt->rtp);
  499. /* Don't send RFC2833 if we're not supposed to */
  500. if (f && (f->frametype == AST_FRAME_DTMF) && !(pvt->options.dtmfmode & H323_DTMF_RFC2833)) {
  501. return &null_frame;
  502. }
  503. if (pvt->owner) {
  504. /* We already hold the channel lock */
  505. if (f->frametype == AST_FRAME_VOICE) {
  506. if (f->subclass != pvt->owner->nativeformats) {
  507. /* Try to avoid deadlock */
  508. if (ast_mutex_trylock(&pvt->owner->lock)) {
  509. ast_log(LOG_NOTICE, "Format changed but channel is locked. Ignoring frame...\n");
  510. return &null_frame;
  511. }
  512. ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
  513. pvt->owner->nativeformats = f->subclass;
  514. pvt->nativeformats = f->subclass;
  515. ast_set_read_format(pvt->owner, pvt->owner->readformat);
  516. ast_set_write_format(pvt->owner, pvt->owner->writeformat);
  517. ast_mutex_unlock(&pvt->owner->lock);
  518. }
  519. /* Do in-band DTMF detection */
  520. if ((pvt->options.dtmfmode & H323_DTMF_INBAND) && pvt->vad) {
  521. if (!ast_mutex_trylock(&pvt->owner->lock)) {
  522. f = ast_dsp_process(pvt->owner,pvt->vad,f);
  523. ast_mutex_unlock(&pvt->owner->lock);
  524. }
  525. else
  526. ast_log(LOG_NOTICE, "Unable to process inband DTMF while channel is locked\n");
  527. if (f &&(f->frametype == AST_FRAME_DTMF)) {
  528. ast_log(LOG_DEBUG, "Received in-band digit %c.\n", f->subclass);
  529. }
  530. }
  531. }
  532. }
  533. return f;
  534. }
  535. static struct ast_frame *oh323_read(struct ast_channel *c)
  536. {
  537. struct ast_frame *fr;
  538. struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
  539. ast_mutex_lock(&pvt->lock);
  540. __oh323_update_info(c, pvt);
  541. fr = oh323_rtp_read(pvt);
  542. ast_mutex_unlock(&pvt->lock);
  543. return fr;
  544. }
  545. static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
  546. {
  547. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_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 0;
  561. }
  562. }
  563. if (pvt) {
  564. ast_mutex_lock(&pvt->lock);
  565. if (pvt->rtp) {
  566. res = ast_rtp_write(pvt->rtp, frame);
  567. }
  568. __oh323_update_info(c, pvt);
  569. ast_mutex_unlock(&pvt->lock);
  570. }
  571. return res;
  572. }
  573. static int oh323_indicate(struct ast_channel *c, int condition)
  574. {
  575. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
  576. char *token = (char *)NULL;
  577. ast_mutex_lock(&pvt->lock);
  578. token = (pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL);
  579. ast_mutex_unlock(&pvt->lock);
  580. if (h323debug)
  581. ast_log(LOG_DEBUG, "OH323: Indicating %d on %s\n", condition, token);
  582. switch(condition) {
  583. case AST_CONTROL_RINGING:
  584. if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
  585. h323_send_alerting(token);
  586. break;
  587. }
  588. if (token)
  589. free(token);
  590. return -1;
  591. case AST_CONTROL_PROGRESS:
  592. if (c->_state != AST_STATE_UP) {
  593. h323_send_progress(token);
  594. break;
  595. }
  596. if (token)
  597. free(token);
  598. return -1;
  599. case AST_CONTROL_BUSY:
  600. if (c->_state != AST_STATE_UP) {
  601. h323_answering_call(token, 1);
  602. ast_mutex_lock(&pvt->lock);
  603. pvt->alreadygone = 1;
  604. ast_mutex_unlock(&pvt->lock);
  605. ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
  606. break;
  607. }
  608. if (token)
  609. free(token);
  610. return -1;
  611. case AST_CONTROL_CONGESTION:
  612. if (c->_state != AST_STATE_UP) {
  613. h323_answering_call(token, 1);
  614. ast_mutex_lock(&pvt->lock);
  615. pvt->alreadygone = 1;
  616. ast_mutex_unlock(&pvt->lock);
  617. ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
  618. break;
  619. }
  620. if (token)
  621. free(token);
  622. return -1;
  623. case AST_CONTROL_PROCEEDING:
  624. case -1:
  625. if (token)
  626. free(token);
  627. return -1;
  628. default:
  629. ast_log(LOG_WARNING, "Don't know how to indicate condition %d on %s\n", condition, token);
  630. if (token)
  631. free(token);
  632. return -1;
  633. }
  634. if (h323debug)
  635. ast_log(LOG_DEBUG, "OH323: Indicated %d on %s\n", condition, token);
  636. if (token)
  637. free(token);
  638. oh323_update_info(c);
  639. return -1;
  640. }
  641. static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  642. {
  643. struct oh323_pvt *pvt = (struct oh323_pvt *) newchan->tech_pvt;
  644. ast_mutex_lock(&pvt->lock);
  645. if (pvt->owner != oldchan) {
  646. ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, pvt->owner);
  647. return -1;
  648. }
  649. pvt->owner = newchan;
  650. ast_mutex_unlock(&pvt->lock);
  651. return 0;
  652. }
  653. /* Private structure should be locked on a call */
  654. static struct ast_channel *__oh323_new(struct oh323_pvt *pvt, int state, const char *host)
  655. {
  656. struct ast_channel *ch;
  657. int fmt;
  658. /* Don't hold a oh323_pvt lock while we allocate a chanel */
  659. ast_mutex_unlock(&pvt->lock);
  660. ch = ast_channel_alloc(1);
  661. /* Update usage counter */
  662. ast_mutex_lock(&usecnt_lock);
  663. usecnt++;
  664. ast_mutex_unlock(&usecnt_lock);
  665. ast_update_use_count();
  666. ast_mutex_lock(&pvt->lock);
  667. if (ch) {
  668. ch->tech = &oh323_tech;
  669. snprintf(ch->name, sizeof(ch->name), "H323/%s", host);
  670. ch->nativeformats = pvt->options.capability;
  671. if (!ch->nativeformats) {
  672. ch->nativeformats = global_options.capability;
  673. }
  674. pvt->nativeformats = ch->nativeformats;
  675. fmt = ast_best_codec(ch->nativeformats);
  676. ch->type = type;
  677. ch->fds[0] = ast_rtp_fd(pvt->rtp);
  678. if (state == AST_STATE_RING) {
  679. ch->rings = 1;
  680. }
  681. ch->writeformat = fmt;
  682. ch->rawwriteformat = fmt;
  683. ch->readformat = fmt;
  684. ch->rawreadformat = fmt;
  685. /* Allocate dsp for in-band DTMF support */
  686. if (pvt->options.dtmfmode & H323_DTMF_INBAND) {
  687. pvt->vad = ast_dsp_new();
  688. ast_dsp_set_features(pvt->vad, DSP_FEATURE_DTMF_DETECT);
  689. }
  690. /* Register channel functions. */
  691. ch->tech_pvt = pvt;
  692. /* Set the owner of this channel */
  693. pvt->owner = ch;
  694. strncpy(ch->context, pvt->context, sizeof(ch->context) - 1);
  695. strncpy(ch->exten, pvt->exten, sizeof(ch->exten) - 1);
  696. ch->priority = 1;
  697. if (!ast_strlen_zero(pvt->accountcode)) {
  698. strncpy(ch->accountcode, pvt->accountcode, sizeof(ch->accountcode) - 1);
  699. }
  700. if (pvt->amaflags) {
  701. ch->amaflags = pvt->amaflags;
  702. }
  703. if (!ast_strlen_zero(pvt->cid_num)) {
  704. ch->cid.cid_num = strdup(pvt->cid_num);
  705. } else if (!ast_strlen_zero(pvt->cd.call_source_e164)) {
  706. ch->cid.cid_num = strdup(pvt->cd.call_source_e164);
  707. }
  708. if (!ast_strlen_zero(pvt->cid_name)) {
  709. ch->cid.cid_name = strdup(pvt->cid_name);
  710. } else if (!ast_strlen_zero(pvt->cd.call_source_name)) {
  711. ch->cid.cid_name = strdup(pvt->cd.call_source_name);
  712. }
  713. if (!ast_strlen_zero(pvt->rdnis)) {
  714. ch->cid.cid_rdnis = strdup(pvt->rdnis);
  715. }
  716. if (!ast_strlen_zero(pvt->exten) && strcmp(pvt->exten, "s")) {
  717. ch->cid.cid_dnid = strdup(pvt->exten);
  718. }
  719. ast_setstate(ch, state);
  720. if (state != AST_STATE_DOWN) {
  721. if (ast_pbx_start(ch)) {
  722. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
  723. ast_hangup(ch);
  724. ch = NULL;
  725. }
  726. }
  727. } else {
  728. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  729. }
  730. return ch;
  731. }
  732. static struct oh323_pvt *oh323_alloc(int callid)
  733. {
  734. struct oh323_pvt *pvt;
  735. pvt = (struct oh323_pvt *) malloc(sizeof(struct oh323_pvt));
  736. if (!pvt) {
  737. ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
  738. return NULL;
  739. }
  740. memset(pvt, 0, sizeof(struct oh323_pvt));
  741. pvt->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0,bindaddr.sin_addr);
  742. if (!pvt->rtp) {
  743. ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
  744. free(pvt);
  745. return NULL;
  746. }
  747. ast_rtp_settos(pvt->rtp, tos);
  748. ast_mutex_init(&pvt->lock);
  749. /* Ensure the call token is allocated */
  750. if ((pvt->cd).call_token == NULL) {
  751. (pvt->cd).call_token = (char *)malloc(128);
  752. }
  753. if (!pvt->cd.call_token) {
  754. ast_log(LOG_ERROR, "Not enough memory to alocate call token\n");
  755. return NULL;
  756. }
  757. memset((char *)(pvt->cd).call_token, 0, 128);
  758. pvt->cd.call_reference = callid;
  759. memcpy(&pvt->options, &global_options, sizeof(pvt->options));
  760. if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
  761. pvt->nonCodecCapability |= AST_RTP_DTMF;
  762. } else {
  763. pvt->nonCodecCapability &= ~AST_RTP_DTMF;
  764. }
  765. strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
  766. pvt->newstate = pvt->newcontrol = -1;
  767. /* Add to interface list */
  768. ast_mutex_lock(&iflock);
  769. pvt->next = iflist;
  770. iflist = pvt;
  771. ast_mutex_unlock(&iflock);
  772. return pvt;
  773. }
  774. static struct oh323_pvt *find_call_locked(int call_reference, const char *token)
  775. {
  776. struct oh323_pvt *pvt;
  777. ast_mutex_lock(&iflock);
  778. pvt = iflist;
  779. while(pvt) {
  780. if (!pvt->needdestroy && ((signed int)pvt->cd.call_reference == call_reference)) {
  781. /* Found the call */
  782. if ((token != NULL) && (!strcmp(pvt->cd.call_token, token))) {
  783. ast_mutex_lock(&pvt->lock);
  784. ast_mutex_unlock(&iflock);
  785. return pvt;
  786. } else if (token == NULL) {
  787. ast_log(LOG_WARNING, "Call Token is NULL\n");
  788. ast_mutex_lock(&pvt->lock);
  789. ast_mutex_unlock(&iflock);
  790. return pvt;
  791. }
  792. }
  793. pvt = pvt->next;
  794. }
  795. ast_mutex_unlock(&iflock);
  796. return NULL;
  797. }
  798. static int update_state(struct oh323_pvt *pvt, int state, int signal)
  799. {
  800. if (!pvt)
  801. return 0;
  802. if (pvt->owner && !ast_mutex_trylock(&pvt->owner->lock)) {
  803. if (state >= 0)
  804. ast_setstate(pvt->owner, state);
  805. if (signal >= 0)
  806. ast_queue_control(pvt->owner, signal);
  807. return 1;
  808. }
  809. else {
  810. if (state >= 0)
  811. pvt->newstate = state;
  812. if (signal >= 0)
  813. pvt->newcontrol = signal;
  814. return 0;
  815. }
  816. }
  817. struct oh323_user *find_user(const call_details_t *cd)
  818. {
  819. struct oh323_user *u;
  820. char iabuf[INET_ADDRSTRLEN];
  821. u = userl.users;
  822. if (userbyalias) {
  823. while(u) {
  824. if (!strcasecmp(u->name, cd->call_source_aliases)) {
  825. break;
  826. }
  827. u = u->next;
  828. }
  829. } else {
  830. while(u) {
  831. if (!strcasecmp(cd->sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), u->addr.sin_addr))) {
  832. break;
  833. }
  834. u = u->next;
  835. }
  836. }
  837. return u;
  838. }
  839. struct oh323_peer *find_peer(const char *peer, struct sockaddr_in *sin)
  840. {
  841. struct oh323_peer *p = NULL;
  842. static char iabuf[INET_ADDRSTRLEN];
  843. p = peerl.peers;
  844. if (peer) {
  845. while(p) {
  846. if (!strcasecmp(p->name, peer)) {
  847. ast_log(LOG_DEBUG, "Found peer %s by name\n", peer);
  848. break;
  849. }
  850. p = p->next;
  851. }
  852. } else {
  853. /* find by sin */
  854. if (sin) {
  855. while (p) {
  856. if ((!inaddrcmp(&p->addr, sin)) ||
  857. (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)) {
  858. ast_log(LOG_DEBUG, "Found peer %s/%s by addr\n", peer, ast_inet_ntoa(iabuf, sizeof(iabuf), p->addr.sin_addr));
  859. break;
  860. }
  861. p = p->next;
  862. }
  863. }
  864. }
  865. if (!p) {
  866. ast_log(LOG_DEBUG, "Could not find peer %s by name or address\n", peer);
  867. }
  868. return p;
  869. }
  870. static int create_addr(struct oh323_pvt *pvt, char *opeer)
  871. {
  872. struct hostent *hp;
  873. struct ast_hostent ahp;
  874. struct oh323_peer *p;
  875. int portno;
  876. int found = 0;
  877. char *port;
  878. char *hostn;
  879. char peer[256] = "";
  880. strncpy(peer, opeer, sizeof(peer) - 1);
  881. port = strchr(peer, ':');
  882. if (port) {
  883. *port = '\0';
  884. port++;
  885. }
  886. pvt->sa.sin_family = AF_INET;
  887. ast_mutex_lock(&peerl.lock);
  888. p = find_peer(peer, NULL);
  889. if (p) {
  890. found++;
  891. memcpy(&pvt->options, &p->options, sizeof(pvt->options));
  892. if (pvt->rtp) {
  893. ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", pvt->options.nat);
  894. ast_rtp_setnat(pvt->rtp, pvt->options.nat);
  895. }
  896. if (pvt->options.dtmfmode) {
  897. if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
  898. pvt->nonCodecCapability |= AST_RTP_DTMF;
  899. } else {
  900. pvt->nonCodecCapability &= ~AST_RTP_DTMF;
  901. }
  902. }
  903. if (p->addr.sin_addr.s_addr) {
  904. pvt->sa.sin_addr = p->addr.sin_addr;
  905. pvt->sa.sin_port = p->addr.sin_port;
  906. }
  907. }
  908. ast_mutex_unlock(&peerl.lock);
  909. if (!p && !found) {
  910. hostn = peer;
  911. if (port) {
  912. portno = atoi(port);
  913. } else {
  914. portno = h323_signalling_port;
  915. }
  916. hp = ast_gethostbyname(hostn, &ahp);
  917. if (hp) {
  918. memcpy(&pvt->options, &global_options, sizeof(pvt->options));
  919. memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
  920. pvt->sa.sin_port = htons(portno);
  921. return 0;
  922. } else {
  923. ast_log(LOG_WARNING, "No such host: %s\n", peer);
  924. return -1;
  925. }
  926. } else if (!p) {
  927. return -1;
  928. } else {
  929. return 0;
  930. }
  931. }
  932. static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause)
  933. {
  934. int oldformat;
  935. struct oh323_pvt *pvt;
  936. struct ast_channel *tmpc = NULL;
  937. char *dest = (char *)data;
  938. char *ext, *host;
  939. char *h323id = NULL;
  940. char tmp[256], tmp1[256];
  941. ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
  942. pvt = oh323_alloc(0);
  943. if (!pvt) {
  944. ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
  945. return NULL;
  946. }
  947. oldformat = format;
  948. format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1);
  949. if (!format) {
  950. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
  951. return NULL;
  952. }
  953. strncpy(tmp, dest, sizeof(tmp) - 1);
  954. host = strchr(tmp, '@');
  955. if (host) {
  956. *host = '\0';
  957. host++;
  958. ext = tmp;
  959. } else {
  960. host = tmp;
  961. ext = NULL;
  962. }
  963. strtok_r(host, "/", &(h323id));
  964. if (!ast_strlen_zero(h323id)) {
  965. h323_set_id(h323id);
  966. }
  967. if (ext) {
  968. strncpy(pvt->exten, ext, sizeof(pvt->exten) - 1);
  969. }
  970. ast_log(LOG_DEBUG, "Extension: %s Host: %s\n", pvt->exten, host);
  971. if (!usingGk) {
  972. if (create_addr(pvt, host)) {
  973. oh323_destroy(pvt);
  974. return NULL;
  975. }
  976. }
  977. else {
  978. memcpy(&pvt->options, &global_options, sizeof(pvt->options));
  979. if (pvt->rtp) {
  980. ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", pvt->options.nat);
  981. ast_rtp_setnat(pvt->rtp, pvt->options.nat);
  982. }
  983. if (pvt->options.dtmfmode) {
  984. if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
  985. pvt->nonCodecCapability |= AST_RTP_DTMF;
  986. } else {
  987. pvt->nonCodecCapability &= ~AST_RTP_DTMF;
  988. }
  989. }
  990. }
  991. ast_mutex_lock(&caplock);
  992. /* Generate unique channel identifier */
  993. snprintf(tmp1, sizeof(tmp1)-1, "%s-%u", host, ++unique);
  994. tmp1[sizeof(tmp1)-1] = '\0';
  995. ast_mutex_unlock(&caplock);
  996. ast_mutex_lock(&pvt->lock);
  997. tmpc = __oh323_new(pvt, AST_STATE_DOWN, tmp1);
  998. ast_mutex_unlock(&pvt->lock);
  999. if (!tmpc) {
  1000. oh323_destroy(pvt);
  1001. }
  1002. ast_update_use_count();
  1003. restart_monitor();
  1004. return tmpc;
  1005. }
  1006. /** Find a call by alias */
  1007. struct oh323_alias *find_alias(const char *source_aliases)
  1008. {
  1009. struct oh323_alias *a;
  1010. a = aliasl.aliases;
  1011. while(a) {
  1012. if (!strcasecmp(a->name, source_aliases)) {
  1013. break;
  1014. }
  1015. a = a->next;
  1016. }
  1017. return a;
  1018. }
  1019. /**
  1020. * Callback for sending digits from H.323 up to asterisk
  1021. *
  1022. */
  1023. int send_digit(unsigned call_reference, char digit, const char *token)
  1024. {
  1025. struct oh323_pvt *pvt;
  1026. struct ast_frame f;
  1027. int res;
  1028. ast_log(LOG_DEBUG, "Received Digit: %c\n", digit);
  1029. pvt = find_call_locked(call_reference, token);
  1030. if (!pvt) {
  1031. ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
  1032. return -1;
  1033. }
  1034. memset(&f, 0, sizeof(f));
  1035. f.frametype = AST_FRAME_DTMF;
  1036. f.subclass = digit;
  1037. f.datalen = 0;
  1038. f.samples = 800;
  1039. f.offset = 0;
  1040. f.data = NULL;
  1041. f.mallocd = 0;
  1042. f.src = "SEND_DIGIT";
  1043. res = ast_queue_frame(pvt->owner, &f);
  1044. ast_mutex_unlock(&pvt->lock);
  1045. return res;
  1046. }
  1047. /**
  1048. * Callback function used to inform the H.323 stack of the local rtp ip/port details
  1049. *
  1050. * Returns the local RTP information
  1051. */
  1052. struct rtp_info *external_rtp_create(unsigned call_reference, const char * token)
  1053. {
  1054. struct oh323_pvt *pvt;
  1055. struct sockaddr_in us;
  1056. struct rtp_info *info;
  1057. info = (struct rtp_info *)malloc(sizeof(struct rtp_info));
  1058. if (!info) {
  1059. ast_log(LOG_ERROR, "Unable to allocated info structure, this is very bad\n");
  1060. return NULL;
  1061. }
  1062. pvt = find_call_locked(call_reference, token);
  1063. if (!pvt) {
  1064. free(info);
  1065. ast_log(LOG_ERROR, "Unable to find call %s(%d)\n", token, call_reference);
  1066. return NULL;
  1067. }
  1068. /* figure out our local RTP port and tell the H.323 stack about it */
  1069. ast_rtp_get_us(pvt->rtp, &us);
  1070. ast_mutex_unlock(&pvt->lock);
  1071. ast_inet_ntoa(info->addr, sizeof(info->addr), us.sin_addr);
  1072. info->port = ntohs(us.sin_port);
  1073. if (h323debug)
  1074. ast_log(LOG_DEBUG, "Sending RTP 'US' %s:%d\n", info->addr, info->port);
  1075. return info;
  1076. }
  1077. /**
  1078. * Definition taken from rtp.c for rtpPayloadType because we need it here.
  1079. */
  1080. struct rtpPayloadType {
  1081. int isAstFormat; /* whether the following code is an AST_FORMAT */
  1082. int code;
  1083. };
  1084. /**
  1085. * Call-back function passing remote ip/port information from H.323 to asterisk
  1086. *
  1087. * Returns nothing
  1088. */
  1089. void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort, const char *token, int pt)
  1090. {
  1091. struct oh323_pvt *pvt;
  1092. struct sockaddr_in them;
  1093. struct rtpPayloadType rtptype;
  1094. if (h323debug)
  1095. ast_log(LOG_DEBUG, "Setting up RTP connection for %s\n", token);
  1096. /* Find the call or allocate a private structure if call not found */
  1097. pvt = find_call_locked(call_reference, token);
  1098. if (!pvt) {
  1099. ast_log(LOG_ERROR, "Something is wrong: rtp\n");
  1100. return;
  1101. }
  1102. if (pvt->alreadygone) {
  1103. ast_mutex_unlock(&pvt->lock);
  1104. return;
  1105. }
  1106. rtptype = ast_rtp_lookup_pt(pvt->rtp, pt);
  1107. pvt->nativeformats = rtptype.code;
  1108. if (pvt->owner && !ast_mutex_trylock(&pvt->owner->lock)) {
  1109. pvt->owner->nativeformats = pvt->nativeformats;
  1110. ast_set_read_format(pvt->owner, pvt->owner->readformat);
  1111. ast_set_write_format(pvt->owner, pvt->owner->writeformat);
  1112. if (pvt->options.progress_audio)
  1113. ast_queue_control(pvt->owner, AST_CONTROL_PROGRESS);
  1114. ast_mutex_unlock(&pvt->owner->lock);
  1115. }
  1116. else {
  1117. if (pvt->options.progress_audio)
  1118. pvt->newcontrol = AST_CONTROL_PROGRESS;
  1119. if (h323debug)
  1120. ast_log(LOG_DEBUG, "RTP connection preparation for %s is pending...\n", token);
  1121. }
  1122. them.sin_family = AF_INET;
  1123. /* only works for IPv4 */
  1124. them.sin_addr.s_addr = inet_addr(remoteIp);
  1125. them.sin_port = htons(remotePort);
  1126. ast_rtp_set_peer(pvt->rtp, &them);
  1127. ast_mutex_unlock(&pvt->lock);
  1128. if (h323debug)
  1129. ast_log(LOG_DEBUG, "RTP connection prepared for %s\n", token);
  1130. return;
  1131. }
  1132. /**
  1133. * Call-back function to signal asterisk that the channel has been answered
  1134. * Returns nothing
  1135. */
  1136. void connection_made(unsigned call_reference, const char *token)
  1137. {
  1138. struct ast_channel *c = NULL;
  1139. struct oh323_pvt *pvt;
  1140. if (h323debug)
  1141. ast_log(LOG_DEBUG, "Call %s answered\n", token);
  1142. pvt = find_call_locked(call_reference, token);
  1143. if (!pvt) {
  1144. ast_log(LOG_ERROR, "Something is wrong: connection\n");
  1145. return;
  1146. }
  1147. /* Inform asterisk about remote party connected only on outgoing calls */
  1148. if (!pvt->outgoing) {
  1149. ast_mutex_unlock(&pvt->lock);
  1150. return;
  1151. }
  1152. if (update_state(pvt, AST_STATE_UP, AST_CONTROL_ANSWER))
  1153. ast_mutex_unlock(&pvt->owner->lock);
  1154. ast_mutex_unlock(&pvt->lock);
  1155. return;
  1156. }
  1157. int progress(unsigned call_reference, const char *token, int inband)
  1158. {
  1159. struct oh323_pvt *pvt;
  1160. ast_log(LOG_DEBUG, "Received ALERT/PROGRESS message for %s tones\n", (inband ? "inband" : "self-generated"));
  1161. pvt = find_call_locked(call_reference, token);
  1162. if (!pvt) {
  1163. ast_log(LOG_ERROR, "Private structure not found in progress.\n");
  1164. return -1;
  1165. }
  1166. if (!pvt->owner) {
  1167. ast_mutex_unlock(&pvt->lock);
  1168. ast_log(LOG_ERROR, "No Asterisk channel associated with private structure.\n");
  1169. return -1;
  1170. }
  1171. if (update_state(pvt, -1, (inband ? AST_CONTROL_PROGRESS : AST_CONTROL_RINGING)))
  1172. ast_mutex_unlock(&pvt->owner->lock);
  1173. ast_mutex_unlock(&pvt->lock);
  1174. return 0;
  1175. }
  1176. /**
  1177. * Call-back function for incoming calls
  1178. *
  1179. * Returns 1 on success
  1180. */
  1181. call_options_t *setup_incoming_call(call_details_t *cd)
  1182. {
  1183. struct oh323_pvt *pvt;
  1184. struct oh323_user *user = NULL;
  1185. struct oh323_alias *alias = NULL;
  1186. char iabuf[INET_ADDRSTRLEN];
  1187. if (h323debug)
  1188. ast_log(LOG_DEBUG, "Setting up incoming call for %s\n", cd->call_token);
  1189. /* allocate the call*/
  1190. pvt = oh323_alloc(cd->call_reference);
  1191. if (!pvt) {
  1192. ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
  1193. return NULL;
  1194. }
  1195. /* Populate the call details in the private structure */
  1196. memcpy(&pvt->cd, cd, sizeof(pvt->cd));
  1197. memcpy(&pvt->options, &global_options, sizeof(pvt->options));
  1198. if (h323debug) {
  1199. ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
  1200. ast_verbose(VERBOSE_PREFIX_3 "\tCall token: [%s]\n", pvt->cd.call_token);
  1201. ast_verbose(VERBOSE_PREFIX_3 "\tCalling party name: [%s]\n", pvt->cd.call_source_name);
  1202. ast_verbose(VERBOSE_PREFIX_3 "\tCalling party number: [%s]\n", pvt->cd.call_source_e164);
  1203. ast_verbose(VERBOSE_PREFIX_3 "\tCalled party name: [%s]\n", pvt->cd.call_dest_alias);
  1204. ast_verbose(VERBOSE_PREFIX_3 "\tCalled party number: [%s]\n", pvt->cd.call_dest_e164);
  1205. }
  1206. /* Decide if we are allowing Gatekeeper routed calls*/
  1207. if ((!strcasecmp(cd->sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk)) {
  1208. if (!ast_strlen_zero(cd->call_dest_e164)) {
  1209. strncpy(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten) - 1);
  1210. strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
  1211. } else {
  1212. alias = find_alias(cd->call_dest_alias);
  1213. if (!alias) {
  1214. ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd->call_dest_alias);
  1215. return NULL;
  1216. }
  1217. strncpy(pvt->exten, alias->name, sizeof(pvt->exten) - 1);
  1218. strncpy(pvt->context, alias->context, sizeof(pvt->context) - 1);
  1219. }
  1220. } else {
  1221. /* Either this call is not from the Gatekeeper
  1222. or we are not allowing gk routed calls */
  1223. user = find_user(cd);
  1224. if (!user) {
  1225. if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
  1226. strncpy(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten) - 1);
  1227. } else {
  1228. strncpy(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten) - 1);
  1229. }
  1230. if (ast_strlen_zero(default_context)) {
  1231. ast_log(LOG_ERROR, "Call from '%s' rejected due to no default context\n", pvt->cd.call_source_aliases);
  1232. return NULL;
  1233. }
  1234. strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
  1235. ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd->call_source_aliases, pvt->context);
  1236. /* XXX: Is it really required??? */
  1237. #if 0
  1238. memset(&pvt->options, 0, sizeof(pvt->options));
  1239. #endif
  1240. } else {
  1241. if (user->host) {
  1242. if (strcasecmp(cd->sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), user->addr.sin_addr))) {
  1243. if (ast_strlen_zero(user->context)) {
  1244. if (ast_strlen_zero(default_context)) {
  1245. ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd->sourceIp);
  1246. return NULL;
  1247. }
  1248. strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
  1249. } else {
  1250. strncpy(pvt->context, user->context, sizeof(pvt->context) - 1);
  1251. }
  1252. pvt->exten[0] = 'i';
  1253. pvt->exten[1] = '\0';
  1254. ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd->sourceIp);
  1255. return NULL; /* XXX: Hmmm... Why to setup context if we drop connection immediately??? */
  1256. }
  1257. }
  1258. strncpy(pvt->context, user->context, sizeof(pvt->context) - 1);
  1259. memcpy(&pvt->options, &user->options, sizeof(pvt->options));
  1260. if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
  1261. strncpy(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten) - 1);
  1262. } else {
  1263. strncpy(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten) - 1);
  1264. }
  1265. if (!ast_strlen_zero(user->accountcode)) {
  1266. strncpy(pvt->accountcode, user->accountcode, sizeof(pvt->accountcode) - 1);
  1267. }
  1268. if (user->amaflags) {
  1269. pvt->amaflags = user->amaflags;
  1270. }
  1271. }
  1272. }
  1273. return &pvt->options;
  1274. }
  1275. /**
  1276. * Call-back function to start PBX when OpenH323 ready to serve incoming call
  1277. *
  1278. * Returns 1 on success
  1279. */
  1280. static int answer_call(unsigned call_reference, const char *token)
  1281. {
  1282. struct oh323_pvt *pvt;
  1283. struct ast_channel *c = NULL;
  1284. if (h323debug)
  1285. ast_log(LOG_DEBUG, "Preparing Asterisk to answer for %s\n", token);
  1286. /* Find the call or allocate a private structure if call not found */
  1287. pvt = find_call_locked(call_reference, token);
  1288. if (!pvt) {
  1289. ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
  1290. return 0;
  1291. }
  1292. /* allocate a channel and tell asterisk about it */
  1293. c = __oh323_new(pvt, AST_STATE_RINGING, pvt->cd.call_token);
  1294. /* And release when done */
  1295. ast_mutex_unlock(&pvt->lock);
  1296. if (!c) {
  1297. ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
  1298. return 0;
  1299. }
  1300. return 1;
  1301. }
  1302. /**
  1303. * Call-back function to establish an outgoing H.323 call
  1304. *
  1305. * Returns 1 on success
  1306. */
  1307. int setup_outgoing_call(call_details_t *cd)
  1308. {
  1309. /* Use argument here or free it immediately */
  1310. cleanup_call_details(cd);
  1311. return 1;
  1312. }
  1313. /**
  1314. * Call-back function to signal asterisk that the channel is ringing
  1315. * Returns nothing
  1316. */
  1317. void chan_ringing(unsigned call_reference, const char *token)
  1318. {
  1319. struct ast_channel *c = NULL;
  1320. struct oh323_pvt *pvt;
  1321. if (h323debug)
  1322. ast_log(LOG_DEBUG, "Ringing on %s\n", token);
  1323. pvt = find_call_locked(call_reference, token);
  1324. if (!pvt) {
  1325. ast_log(LOG_ERROR, "Something is wrong: ringing\n");
  1326. return;
  1327. }
  1328. if (!pvt->owner) {
  1329. ast_mutex_unlock(&pvt->lock);
  1330. ast_log(LOG_ERROR, "Channel has no owner\n");
  1331. return;
  1332. }
  1333. if (update_state(pvt, AST_STATE_RINGING, AST_CONTROL_RINGING))
  1334. ast_mutex_unlock(&pvt->owner->lock);
  1335. ast_mutex_unlock(&pvt->lock);
  1336. return;
  1337. }
  1338. /**
  1339. * Call-back function to cleanup communication
  1340. * Returns nothing,
  1341. */
  1342. static void cleanup_connection(unsigned call_reference, const char *call_token)
  1343. {
  1344. struct oh323_pvt *pvt;
  1345. ast_log(LOG_DEBUG, "Cleaning connection to %s\n", call_token);
  1346. while (1) {
  1347. pvt = find_call_locked(call_reference, call_token);
  1348. if (!pvt) {
  1349. if (h323debug)
  1350. ast_log(LOG_DEBUG, "No connection for %s\n", call_token);
  1351. return;
  1352. }
  1353. if (!pvt->owner || !ast_mutex_trylock(&pvt->owner->lock))
  1354. break;
  1355. #if 1
  1356. #ifdef DEBUG_THREADS
  1357. ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s, locked at %ld/%d by %s (%s:%d)\n", call_token, pvt->owner->lock.thread, pvt->owner->lock.reentrancy, pvt->owner->lock.func, pvt->owner->lock.file, pvt->owner->lock.lineno);
  1358. #else
  1359. ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s\n", call_token);
  1360. #endif
  1361. #endif
  1362. ast_mutex_unlock(&pvt->lock);
  1363. usleep(1);
  1364. }
  1365. if (pvt->rtp) {
  1366. /* Immediately stop RTP */
  1367. ast_rtp_destroy(pvt->rtp);
  1368. pvt->rtp = NULL;
  1369. }
  1370. /* Free dsp used for in-band DTMF detection */
  1371. if (pvt->vad) {
  1372. ast_dsp_free(pvt->vad);
  1373. pvt->vad = NULL;
  1374. }
  1375. cleanup_call_details(&pvt->cd);
  1376. pvt->alreadygone = 1;
  1377. /* Send hangup */
  1378. if (pvt->owner) {
  1379. pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  1380. ast_queue_hangup(pvt->owner);
  1381. ast_mutex_unlock(&pvt->owner->lock);
  1382. }
  1383. ast_mutex_unlock(&pvt->lock);
  1384. if (h323debug)
  1385. ast_log(LOG_DEBUG, "Connection to %s cleaned\n", call_token);
  1386. return;
  1387. }
  1388. static void hangup_connection(unsigned int call_reference, const char *token, int cause)
  1389. {
  1390. struct oh323_pvt *pvt;
  1391. ast_log(LOG_DEBUG, "Hanging up connection to %s with cause %d\n", token, cause);
  1392. pvt = find_call_locked(call_reference, token);
  1393. if (!pvt) {
  1394. return;
  1395. }
  1396. if (pvt->owner && !ast_mutex_trylock(&pvt->owner->lock)) {
  1397. pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  1398. pvt->owner->hangupcause = pvt->hangupcause = cause;
  1399. ast_queue_hangup(pvt->owner);
  1400. ast_mutex_unlock(&pvt->owner->lock);
  1401. }
  1402. else {
  1403. pvt->needhangup = 1;
  1404. pvt->hangupcause = cause;
  1405. ast_log(LOG_DEBUG, "Hangup for %s is pending\n", token);
  1406. }
  1407. ast_mutex_unlock(&pvt->lock);
  1408. }
  1409. void set_dtmf_payload(unsigned call_reference, const char *token, int payload)
  1410. {
  1411. struct oh323_pvt *pvt;
  1412. if (h323debug)
  1413. ast_log(LOG_DEBUG, "Setting DTMF payload to %d on %s\n", payload, token);
  1414. pvt = find_call_locked(call_reference, token);
  1415. if (!pvt) {
  1416. return;
  1417. }
  1418. if (pvt->rtp) {
  1419. ast_rtp_set_rtpmap_type(pvt->rtp, payload, "audio", "telephone-event");
  1420. }
  1421. ast_mutex_unlock(&pvt->lock);
  1422. if (h323debug)
  1423. ast_log(LOG_DEBUG, "DTMF payload on %s set to %d\n", token, payload);
  1424. }
  1425. static void set_local_capabilities(unsigned call_reference, const char *token)
  1426. {
  1427. struct oh323_pvt *pvt;
  1428. int capability, dtmfmode;
  1429. if (h323debug)
  1430. ast_log(LOG_DEBUG, "Setting capabilities for connection %s\n", token);
  1431. pvt = find_call_locked(call_reference, token);
  1432. if (!pvt)
  1433. return;
  1434. capability = pvt->options.capability;
  1435. dtmfmode = pvt->options.dtmfmode;
  1436. ast_mutex_unlock(&pvt->lock);
  1437. h323_set_capabilities(token, capability, dtmfmode);
  1438. if (h323debug)
  1439. ast_log(LOG_DEBUG, "Capabilities for connection %s is set\n", token);
  1440. }
  1441. static void *do_monitor(void *data)
  1442. {
  1443. int res;
  1444. int reloading;
  1445. struct oh323_pvt *oh323 = NULL;
  1446. for(;;) {
  1447. /* Check for a reload request */
  1448. ast_mutex_lock(&h323_reload_lock);
  1449. reloading = h323_reloading;
  1450. h323_reloading = 0;
  1451. ast_mutex_unlock(&h323_reload_lock);
  1452. if (reloading) {
  1453. if (option_verbose > 0) {
  1454. ast_verbose(VERBOSE_PREFIX_1 "Reloading H.323\n");
  1455. }
  1456. h323_do_reload();
  1457. }
  1458. /* Check for interfaces needing to be killed */
  1459. ast_mutex_lock(&iflock);
  1460. restartsearch:
  1461. oh323 = iflist;
  1462. while(oh323) {
  1463. if (oh323->needdestroy) {
  1464. __oh323_destroy(oh323);
  1465. goto restartsearch;
  1466. }
  1467. oh323 = oh323->next;
  1468. }
  1469. ast_mutex_unlock(&iflock);
  1470. pthread_testcancel();
  1471. /* Wait for sched or io */
  1472. res = ast_sched_wait(sched);
  1473. if ((res < 0) || (res > 1000)) {
  1474. res = 1000;
  1475. }
  1476. res = ast_io_wait(io, res);
  1477. pthread_testcancel();
  1478. ast_mutex_lock(&monlock);
  1479. if (res >= 0) {
  1480. ast_sched_runq(sched);
  1481. }
  1482. ast_mutex_unlock(&monlock);
  1483. }
  1484. /* Never reached */
  1485. return NULL;
  1486. }
  1487. static int restart_monitor(void)
  1488. {
  1489. pthread_attr_t attr;
  1490. /* If we're supposed to be stopped -- stay stopped */
  1491. if (monitor_thread == AST_PTHREADT_STOP) {
  1492. return 0;
  1493. }
  1494. if (ast_mutex_lock(&monlock)) {
  1495. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  1496. return -1;
  1497. }
  1498. if (monitor_thread == pthread_self()) {
  1499. ast_mutex_unlock(&monlock);
  1500. ast_log(LOG_WARNING, "Cannot kill myself\n");
  1501. return -1;
  1502. }
  1503. if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
  1504. /* Wake up the thread */
  1505. pthread_kill(monitor_thread, SIGURG);
  1506. } else {
  1507. pthread_attr_init(&attr);
  1508. pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
  1509. /* Start a new monitor */
  1510. if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
  1511. ast_mutex_unlock(&monlock);
  1512. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  1513. return -1;
  1514. }
  1515. }
  1516. ast_mutex_unlock(&monlock);
  1517. return 0;
  1518. }
  1519. static int h323_do_trace(int fd, int argc, char *argv[])
  1520. {
  1521. if (argc != 3) {
  1522. return RESULT_SHOWUSAGE;
  1523. }
  1524. h323_debug(1, atoi(argv[2]));
  1525. ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
  1526. return RESULT_SUCCESS;
  1527. }
  1528. static int h323_no_trace(int fd, int argc, char *argv[])
  1529. {
  1530. if (argc != 3) {
  1531. return RESULT_SHOWUSAGE;
  1532. }
  1533. h323_debug(0,0);
  1534. ast_cli(fd, "H.323 trace disabled\n");
  1535. return RESULT_SUCCESS;
  1536. }
  1537. static int h323_do_debug(int fd, int argc, char *argv[])
  1538. {
  1539. if (argc != 2) {
  1540. return RESULT_SHOWUSAGE;
  1541. }
  1542. h323debug = 1;
  1543. ast_cli(fd, "H323 debug enabled\n");
  1544. return RESULT_SUCCESS;
  1545. }
  1546. static int h323_no_debug(int fd, int argc, char *argv[])
  1547. {
  1548. if (argc != 3) {
  1549. return RESULT_SHOWUSAGE;
  1550. }
  1551. h323debug = 0;
  1552. ast_cli(fd, "H323 Debug disabled\n");
  1553. return RESULT_SUCCESS;
  1554. }
  1555. static int h323_gk_cycle(int fd, int argc, char *argv[])
  1556. {
  1557. #if 0
  1558. if (argc != 3) {
  1559. return RESULT_SHOWUSAGE;
  1560. }
  1561. h323_gk_urq();
  1562. /* Possibly register with a GK */
  1563. if (!gatekeeper_disable) {
  1564. if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
  1565. ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
  1566. }
  1567. }
  1568. #endif
  1569. return RESULT_SUCCESS;
  1570. }
  1571. static int h323_ep_hangup(int fd, int argc, char *argv[])
  1572. {
  1573. if (argc != 3) {
  1574. return RESULT_SHOWUSAGE;
  1575. }
  1576. if (h323_soft_hangup(argv[2])) {
  1577. ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
  1578. } else {
  1579. ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
  1580. }
  1581. return RESULT_SUCCESS;
  1582. }
  1583. static int h323_tokens_show(int fd, int argc, char *argv[])
  1584. {
  1585. if (argc != 3) {
  1586. return RESULT_SHOWUSAGE;
  1587. }
  1588. h323_show_tokens();
  1589. return RESULT_SUCCESS;
  1590. }
  1591. static char trace_usage[] =
  1592. "Usage: h.323 trace <level num>\n"
  1593. " Enables H.323 stack tracing for debugging purposes\n";
  1594. static char no_trace_usage[] =
  1595. "Usage: h.323 no trace\n"
  1596. " Disables H.323 stack tracing for debugging purposes\n";
  1597. static char debug_usage[] =
  1598. "Usage: h.323 debug\n"
  1599. " Enables H.323 debug output\n";
  1600. static char no_debug_usage[] =
  1601. "Usage: h.323 no debug\n"
  1602. " Disables H.323 debug output\n";
  1603. static char show_codec_usage[] =
  1604. "Usage: h.323 show codec\n"
  1605. " Shows all enabled codecs\n";
  1606. static char show_cycle_usage[] =
  1607. "Usage: h.323 gk cycle\n"
  1608. " Manually re-register with the Gatekeper (Currently Disabled)\n";
  1609. static char show_hangup_usage[] =
  1610. "Usage: h.323 hangup <token>\n"
  1611. " Manually try to hang up call identified by <token>\n";
  1612. static char show_tokens_usage[] =
  1613. "Usage: h.323 show tokens\n"
  1614. " Print out all active call tokens\n";
  1615. static char h323_reload_usage[] =
  1616. "Usage: h323 reload\n"
  1617. " Reloads H.323 configuration from sip.conf\n";
  1618. static struct ast_cli_entry cli_trace =
  1619. { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
  1620. static struct ast_cli_entry cli_no_trace =
  1621. { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
  1622. static struct ast_cli_entry cli_debug =
  1623. { { "h.323", "debug", NULL }, h323_do_debug, "Enable H.323 debug", debug_usage };
  1624. static struct ast_cli_entry cli_no_debug =
  1625. { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable H.323 debug", no_debug_usage };
  1626. static struct ast_cli_entry cli_show_codecs =
  1627. { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
  1628. static struct ast_cli_entry cli_gk_cycle =
  1629. { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
  1630. static struct ast_cli_entry cli_hangup_call =
  1631. { { "h.323", "hangup", NULL }, h323_ep_hangup, "Manually try to hang up a call", show_hangup_usage };
  1632. static struct ast_cli_entry cli_show_tokens =
  1633. { { "h.323", "show", "tokens", NULL }, h323_tokens_show, "Show all active call tokens", show_tokens_usage };
  1634. static int update_common_options(struct ast_variable *v, struct call_options *options)
  1635. {
  1636. unsigned int format;
  1637. int tmp;
  1638. if (!strcasecmp(v->name, "allow")) {
  1639. format = ast_getformatbyname(v->value);
  1640. if (format < 1)
  1641. ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
  1642. else
  1643. options->capability |= format;
  1644. } else if (!strcasecmp(v->name, "disallow")) {
  1645. format = ast_getformatbyname(v->value);
  1646. if (format < 1)
  1647. ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
  1648. else
  1649. options->capability &= ~format;
  1650. } else if (!strcasecmp(v->name, "dtmfmode")) {
  1651. if (!strcasecmp(v->value, "inband")) {
  1652. options->dtmfmode = H323_DTMF_INBAND;
  1653. } else if (!strcasecmp(v->value, "rfc2833")) {
  1654. options->dtmfmode = H323_DTMF_RFC2833;
  1655. } else {
  1656. ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
  1657. options->dtmfmode = H323_DTMF_RFC2833;
  1658. }
  1659. } else if (!strcasecmp(v->name, "dtmfcodec")) {
  1660. tmp = atoi(v->value);
  1661. if (tmp < 96)
  1662. ast_log(LOG_WARNING, "Invalid global dtmfcodec value %s\n", v->value);
  1663. else
  1664. options->dtmfcodec = tmp;
  1665. } else if (!strcasecmp(v->name, "bridge")) {
  1666. options->bridge = ast_true(v->value);
  1667. } else if (!strcasecmp(v->name, "nat")) {
  1668. options->nat = ast_true(v->value);
  1669. } else if (!strcasecmp(v->name, "noFastStart")) {
  1670. options->noFastStart = ast_true(v->value);
  1671. } else if (!strcasecmp(v->name, "noH245Tunneling")) {
  1672. options->noH245Tunneling = ast_true(v->value);
  1673. } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
  1674. options->noSilenceSuppression = ast_true(v->value);
  1675. } else if (!strcasecmp(v->name, "progress_setup")) {
  1676. tmp = atoi(v->value);
  1677. if ((tmp != 0) && (tmp != 1) && (tmp != 3) && (tmp != 8)) {
  1678. ast_log(LOG_WARNING, "Invalid value %d for progress_setup at line %d, assuming 0\n", tmp, v->lineno);
  1679. tmp = 0;
  1680. }
  1681. options->progress_setup = tmp;
  1682. } else if (!strcasecmp(v->name, "progress_alert")) {
  1683. tmp = atoi(v->value);
  1684. if ((tmp != 0) && (tmp != 8)) {
  1685. ast_log(LOG_WARNING, "Invalud value %d for progress_alert at line %d, assuming 0\n", tmp, v->lineno);
  1686. tmp = 0;
  1687. }
  1688. options->progress_alert = tmp;
  1689. } else if (!strcasecmp(v->name, "progress_audio")) {
  1690. options->progress_audio = ast_true(v->value);
  1691. } else
  1692. return 1;
  1693. return 0;
  1694. }
  1695. static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
  1696. {
  1697. struct oh323_alias *alias;
  1698. alias = (struct oh323_alias *)malloc(sizeof(struct oh323_alias));
  1699. if (alias) {
  1700. memset(alias, 0, sizeof(struct oh323_alias));
  1701. strncpy(alias->name, name, sizeof(alias->name) - 1);
  1702. while (v) {
  1703. if (!strcasecmp(v->name, "e164")) {
  1704. strncpy(alias->e164, v->value, sizeof(alias->e164) - 1);
  1705. } else if (!strcasecmp(v->name, "prefix")) {
  1706. strncpy(alias->prefix, v->value, sizeof(alias->prefix) - 1);
  1707. } else if (!strcasecmp(v->name, "context")) {
  1708. strncpy(alias->context, v->value, sizeof(alias->context) - 1);
  1709. } else if (!strcasecmp(v->name, "secret")) {
  1710. strncpy(alias->secret, v->value, sizeof(alias->secret) - 1);
  1711. } else {
  1712. if (strcasecmp(v->value, "h323")) {
  1713. ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->value);
  1714. }
  1715. }
  1716. v = v->next;
  1717. }
  1718. }
  1719. return alias;
  1720. }
  1721. static struct oh323_user *build_user(char *name, struct ast_variable *v)
  1722. {
  1723. struct oh323_user *user;
  1724. int format;
  1725. user = (struct oh323_user *)malloc(sizeof(struct oh323_user));
  1726. if (user) {
  1727. memset(user, 0, sizeof(struct oh323_user));
  1728. strncpy(user->name, name, sizeof(user->name) - 1);
  1729. memcpy(&user->options, &global_options, sizeof(user->options));
  1730. /* Set default context */
  1731. strncpy(user->context, default_context, sizeof(user->context) - 1);
  1732. while(v) {
  1733. if (!strcasecmp(v->name, "context")) {
  1734. strncpy(user->context, v->value, sizeof(user->context) - 1);
  1735. } else if (!update_common_options(v, &user->options)) {
  1736. /* dummy */
  1737. } else if (!strcasecmp(v->name, "secret")) {
  1738. strncpy(user->secret, v->value, sizeof(user->secret) - 1);
  1739. } else if (!strcasecmp(v->name, "callerid")) {
  1740. strncpy(user->callerid, v->value, sizeof(user->callerid) - 1);
  1741. } else if (!strcasecmp(v->name, "accountcode")) {
  1742. strncpy(user->accountcode, v->value, sizeof(user->accountcode) - 1);
  1743. } else if (!strcasecmp(v->name, "host")) {
  1744. if (!strcasecmp(v->value, "dynamic")) {
  1745. ast_log(LOG_ERROR, "A dynamic host on a type=user does not make any sense\n");
  1746. free(user);
  1747. return NULL;
  1748. } else if (ast_get_ip(&user->addr, v->value)) {
  1749. free(user);
  1750. return NULL;
  1751. }
  1752. /* Let us know we need to use ip authentication */
  1753. user->host = 1;
  1754. } else if (!strcasecmp(v->name, "amaflags")) {
  1755. format = ast_cdr_amaflags2int(v->value);
  1756. if (format < 0) {
  1757. ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
  1758. } else {
  1759. user->amaflags = format;
  1760. }
  1761. }
  1762. v = v->next;
  1763. }
  1764. }
  1765. return user;
  1766. }
  1767. static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
  1768. {
  1769. struct oh323_peer *peer;
  1770. struct oh323_peer *prev;
  1771. struct ast_ha *oldha = NULL;
  1772. int found=0;
  1773. prev = NULL;
  1774. ast_mutex_lock(&peerl.lock);
  1775. peer = peerl.peers;
  1776. while(peer) {
  1777. if (!strcasecmp(peer->name, name)) {
  1778. break;
  1779. }
  1780. prev = peer;
  1781. peer = peer->next;
  1782. }
  1783. if (peer) {
  1784. found++;
  1785. /* Already in the list, remove it and it will be added back (or FREE'd) */
  1786. if (prev) {
  1787. prev->next = peer->next;
  1788. } else {
  1789. peerl.peers = peer->next;
  1790. }
  1791. ast_mutex_unlock(&peerl.lock);
  1792. } else {
  1793. ast_mutex_unlock(&peerl.lock);
  1794. peer = (struct oh323_peer*)malloc(sizeof(struct oh323_peer));
  1795. if (peer)
  1796. memset(peer, 0, sizeof(struct oh323_peer));
  1797. }
  1798. if (peer) {
  1799. if (!found) {
  1800. strncpy(peer->name, name, sizeof(peer->name) - 1);
  1801. peer->addr.sin_port = htons(h323_signalling_port);
  1802. peer->addr.sin_family = AF_INET;
  1803. }
  1804. oldha = peer->ha;
  1805. peer->ha = NULL;
  1806. peer->addr.sin_family = AF_INET;
  1807. memcpy(&peer->options, &global_options, sizeof(peer->options));
  1808. while(v) {
  1809. if (!update_common_options(v, &peer->options)) {
  1810. /* dummy */
  1811. } else if (!strcasecmp(v->name, "host")) {
  1812. if (!strcasecmp(v->value, "dynamic")) {
  1813. ast_log(LOG_ERROR, "Dynamic host configuration not implemented.\n");
  1814. free(peer);
  1815. return NULL;
  1816. }
  1817. if (ast_get_ip(&peer->addr, v->value)) {
  1818. ast_log(LOG_ERROR, "Could not determine IP for %s\n", v->value);
  1819. free(peer);
  1820. return NULL;
  1821. }
  1822. } else if (!strcasecmp(v->name, "port")) {
  1823. peer->addr.sin_port = htons(atoi(v->value));
  1824. }
  1825. v=v->next;
  1826. }
  1827. }
  1828. return peer;
  1829. }
  1830. int reload_config(void)
  1831. {
  1832. int format;
  1833. struct ast_config *cfg;
  1834. struct ast_variable *v;
  1835. struct oh323_peer *peer = NULL;
  1836. struct oh323_user *user = NULL;
  1837. struct oh323_alias *alias = NULL;
  1838. struct ast_hostent ahp; struct hostent *hp;
  1839. char *cat;
  1840. char *utype;
  1841. cfg = ast_config_load(config);
  1842. /* We *must* have a config file otherwise stop immediately */
  1843. if (!cfg) {
  1844. ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
  1845. return 1;
  1846. }
  1847. /* fire up the H.323 Endpoint */
  1848. if (!h323_end_point_exist()) {
  1849. h323_end_point_create();
  1850. }
  1851. h323debug = 0;
  1852. memset(&bindaddr, 0, sizeof(bindaddr));
  1853. memset(&global_options, 0, sizeof(global_options));
  1854. global_options.dtmfcodec = 101;
  1855. global_options.dtmfmode = H323_DTMF_RFC2833;
  1856. global_options.capability = ~0; /* All capabilities */
  1857. global_options.bridge = 1; /* Do native bridging by default */
  1858. v = ast_variable_browse(cfg, "general");
  1859. while(v) {
  1860. /* Create the interface list */
  1861. if (!strcasecmp(v->name, "port")) {
  1862. h323_signalling_port = (int)strtol(v->value, NULL, 10);
  1863. } else if (!strcasecmp(v->name, "bindaddr")) {
  1864. if (!(hp = ast_gethostbyname(v->value, &ahp))) {
  1865. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  1866. } else {
  1867. memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
  1868. }
  1869. } else if (!strcasecmp(v->name, "tos")) {
  1870. if (sscanf(v->value, "%d", &format)) {
  1871. tos = format & 0xff;
  1872. } else if (!strcasecmp(v->value, "lowdelay")) {
  1873. tos = IPTOS_LOWDELAY;
  1874. } else if (!strcasecmp(v->value, "throughput")) {
  1875. tos = IPTOS_THROUGHPUT;
  1876. } else if (!strcasecmp(v->value, "reliability")) {
  1877. tos = IPTOS_RELIABILITY;
  1878. } else if (!strcasecmp(v->value, "mincost")) {
  1879. tos = IPTOS_MINCOST;
  1880. } else if (!strcasecmp(v->value, "none")) {
  1881. tos = 0;
  1882. } else {
  1883. ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
  1884. }
  1885. } else if (!strcasecmp(v->name, "gatekeeper")) {
  1886. if (!strcasecmp(v->value, "DISABLE")) {
  1887. gatekeeper_disable = 1;
  1888. usingGk = 0;
  1889. } else if (!strcasecmp(v->value, "DISCOVER")) {
  1890. gatekeeper_disable = 0;
  1891. gatekeeper_discover = 1;
  1892. usingGk = 1;
  1893. } else {
  1894. gatekeeper_disable = 0;
  1895. usingGk = 1;
  1896. strncpy(gatekeeper, v->value, sizeof(gatekeeper) - 1);
  1897. }
  1898. } else if (!strcasecmp(v->name, "secret")) {
  1899. strncpy(secret, v->value, sizeof(secret) - 1);
  1900. } else if (!strcasecmp(v->name, "AllowGKRouted")) {
  1901. gkroute = ast_true(v->value);
  1902. } else if (!strcasecmp(v->name, "context")) {
  1903. strncpy(default_context, v->value, sizeof(default_context) - 1);
  1904. ast_verbose(VERBOSE_PREFIX_2 "Setting default context to %s\n", default_context);
  1905. } else if (!strcasecmp(v->name, "UserByAlias")) {
  1906. userbyalias = ast_true(v->value);
  1907. } else if (!update_common_options(v, &global_options)) {
  1908. /* dummy */
  1909. }
  1910. v = v->next;
  1911. }
  1912. cat = ast_category_browse(cfg, NULL);
  1913. while(cat) {
  1914. if (strcasecmp(cat, "general")) {
  1915. utype = ast_variable_retrieve(cfg, cat, "type");
  1916. if (utype) {
  1917. if (!strcasecmp(utype, "user")) {
  1918. user = build_user(cat, ast_variable_browse(cfg, cat));
  1919. if (user) {
  1920. ast_mutex_lock(&userl.lock);
  1921. user->next = userl.users;
  1922. userl.users = user;
  1923. ast_mutex_unlock(&userl.lock);
  1924. }
  1925. } else if (!strcasecmp(utype, "peer")) {
  1926. peer = build_peer(cat, ast_variable_browse(cfg, cat));
  1927. if (peer) {
  1928. ast_mutex_lock(&peerl.lock);
  1929. peer->next = peerl.peers;
  1930. peerl.peers = peer;
  1931. ast_mutex_unlock(&peerl.lock);
  1932. }
  1933. } else if (!strcasecmp(utype, "friend")) {
  1934. user = build_user(cat, ast_variable_browse(cfg, cat));
  1935. peer = build_peer(cat, ast_variable_browse(cfg, cat));
  1936. if (user) {
  1937. ast_mutex_lock(&userl.lock);
  1938. user->next = userl.users;
  1939. userl.users = user;
  1940. ast_mutex_unlock(&userl.lock);
  1941. }
  1942. if (peer) {
  1943. ast_mutex_lock(&peerl.lock);
  1944. peer->next = peerl.peers;
  1945. peerl.peers = peer;
  1946. ast_mutex_unlock(&peerl.lock);
  1947. }
  1948. } else if (!strcasecmp(utype, "h323") || !strcasecmp(utype, "alias")) {
  1949. alias = build_alias(cat, ast_variable_browse(cfg, cat));
  1950. if (alias) {
  1951. ast_mutex_lock(&aliasl.lock);
  1952. alias->next = aliasl.aliases;
  1953. aliasl.aliases = alias;
  1954. ast_mutex_unlock(&aliasl.lock);
  1955. }
  1956. } else {
  1957. ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
  1958. }
  1959. } else {
  1960. ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
  1961. }
  1962. }
  1963. cat = ast_category_browse(cfg, cat);
  1964. }
  1965. ast_config_destroy(cfg);
  1966. /* Register our H.323 aliases if any*/
  1967. while (alias) {
  1968. if (h323_set_alias(alias)) {
  1969. ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
  1970. return -1;
  1971. }
  1972. alias = alias->next;
  1973. }
  1974. return 0;
  1975. }
  1976. void delete_users(void)
  1977. {
  1978. struct oh323_user *user, *userlast;
  1979. struct oh323_peer *peer;
  1980. /* Delete all users */
  1981. ast_mutex_lock(&userl.lock);
  1982. for (user=userl.users;user;) {
  1983. userlast = user;
  1984. user=user->next;
  1985. free(userlast);
  1986. }
  1987. userl.users=NULL;
  1988. ast_mutex_unlock(&userl.lock);
  1989. ast_mutex_lock(&peerl.lock);
  1990. for (peer=peerl.peers;peer;) {
  1991. /* Assume all will be deleted, and we'll find out for sure later */
  1992. peer->delme = 1;
  1993. peer = peer->next;
  1994. }
  1995. ast_mutex_unlock(&peerl.lock);
  1996. }
  1997. void delete_aliases(void)
  1998. {
  1999. struct oh323_alias *alias, *aliaslast;
  2000. /* Delete all users */
  2001. ast_mutex_lock(&aliasl.lock);
  2002. for (alias=aliasl.aliases;alias;) {
  2003. aliaslast = alias;
  2004. alias=alias->next;
  2005. free(aliaslast);
  2006. }
  2007. aliasl.aliases=NULL;
  2008. ast_mutex_unlock(&aliasl.lock);
  2009. }
  2010. void prune_peers(void)
  2011. {
  2012. /* Prune peers who still are supposed to be deleted */
  2013. struct oh323_peer *peer, *peerlast, *peernext;
  2014. ast_mutex_lock(&peerl.lock);
  2015. peerlast = NULL;
  2016. for (peer=peerl.peers;peer;) {
  2017. peernext = peer->next;
  2018. if (peer->delme) {
  2019. free(peer);
  2020. if (peerlast) {
  2021. peerlast->next = peernext;
  2022. } else {
  2023. peerl.peers = peernext;
  2024. }
  2025. } else {
  2026. peerlast = peer;
  2027. }
  2028. peer = peernext;
  2029. }
  2030. ast_mutex_unlock(&peerl.lock);
  2031. }
  2032. static int h323_reload(int fd, int argc, char *argv[])
  2033. {
  2034. ast_mutex_lock(&h323_reload_lock);
  2035. if (h323_reloading) {
  2036. ast_verbose("Previous H.323 reload not yet done\n");
  2037. } else {
  2038. h323_reloading = 1;
  2039. }
  2040. ast_mutex_unlock(&h323_reload_lock);
  2041. restart_monitor();
  2042. return 0;
  2043. }
  2044. static int h323_do_reload(void)
  2045. {
  2046. delete_users();
  2047. delete_aliases();
  2048. prune_peers();
  2049. reload_config();
  2050. restart_monitor();
  2051. return 0;
  2052. }
  2053. int reload(void)
  2054. {
  2055. return h323_reload(0, 0, NULL);
  2056. }
  2057. static struct ast_cli_entry cli_h323_reload =
  2058. { { "h.323", "reload", NULL }, h323_reload, "Reload H.323 configuration", h323_reload_usage };
  2059. static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
  2060. {
  2061. struct oh323_pvt *pvt;
  2062. pvt = (struct oh323_pvt *) chan->tech_pvt;
  2063. if (pvt && pvt->rtp && pvt->options.bridge) {
  2064. return pvt->rtp;
  2065. }
  2066. return NULL;
  2067. }
  2068. static struct ast_rtp *oh323_get_vrtp_peer(struct ast_channel *chan)
  2069. {
  2070. return NULL;
  2071. }
  2072. static char *convertcap(int cap)
  2073. {
  2074. switch (cap) {
  2075. case AST_FORMAT_G723_1:
  2076. return "G.723";
  2077. case AST_FORMAT_GSM:
  2078. return "GSM";
  2079. case AST_FORMAT_ULAW:
  2080. return "ULAW";
  2081. case AST_FORMAT_ALAW:
  2082. return "ALAW";
  2083. case AST_FORMAT_ADPCM:
  2084. return "G.728";
  2085. case AST_FORMAT_G729A:
  2086. return "G.729";
  2087. case AST_FORMAT_SPEEX:
  2088. return "SPEEX";
  2089. case AST_FORMAT_ILBC:
  2090. return "ILBC";
  2091. default:
  2092. ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
  2093. return NULL;
  2094. }
  2095. }
  2096. static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs)
  2097. {
  2098. /* XXX Deal with Video */
  2099. struct oh323_pvt *pvt;
  2100. struct sockaddr_in them;
  2101. struct sockaddr_in us;
  2102. char *mode;
  2103. char iabuf[INET_ADDRSTRLEN];
  2104. if (!rtp) {
  2105. return 0;
  2106. }
  2107. mode = convertcap(chan->writeformat);
  2108. pvt = (struct oh323_pvt *) chan->tech_pvt;
  2109. if (!pvt) {
  2110. ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
  2111. return -1;
  2112. }
  2113. ast_rtp_get_peer(rtp, &them);
  2114. ast_rtp_get_us(rtp, &us);
  2115. h323_native_bridge(pvt->cd.call_token, ast_inet_ntoa(iabuf, sizeof(iabuf), them.sin_addr), mode);
  2116. return 0;
  2117. }
  2118. static struct ast_rtp_protocol oh323_rtp = {
  2119. .type = type,
  2120. .get_rtp_info = oh323_get_rtp_peer,
  2121. .get_vrtp_info = oh323_get_vrtp_peer,
  2122. .set_rtp_peer= oh323_set_rtp_peer,
  2123. };
  2124. int load_module()
  2125. {
  2126. int res;
  2127. ast_mutex_init(&userl.lock);
  2128. ast_mutex_init(&peerl.lock);
  2129. ast_mutex_init(&aliasl.lock);
  2130. sched = sched_context_create();
  2131. if (!sched) {
  2132. ast_log(LOG_WARNING, "Unable to create schedule context\n");
  2133. }
  2134. io = io_context_create();
  2135. if (!io) {
  2136. ast_log(LOG_WARNING, "Unable to create I/O context\n");
  2137. }
  2138. res = reload_config();
  2139. if (res) {
  2140. return 0;
  2141. } else {
  2142. /* Make sure we can register our channel type */
  2143. if (ast_channel_register(&oh323_tech)) {
  2144. ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
  2145. h323_end_process();
  2146. return -1;
  2147. }
  2148. ast_cli_register(&cli_debug);
  2149. ast_cli_register(&cli_no_debug);
  2150. ast_cli_register(&cli_trace);
  2151. ast_cli_register(&cli_no_trace);
  2152. ast_cli_register(&cli_show_codecs);
  2153. ast_cli_register(&cli_gk_cycle);
  2154. ast_cli_register(&cli_hangup_call);
  2155. ast_cli_register(&cli_show_tokens);
  2156. ast_cli_register(&cli_h323_reload);
  2157. ast_rtp_proto_register(&oh323_rtp);
  2158. /* Register our callback functions */
  2159. h323_callback_register(setup_incoming_call,
  2160. setup_outgoing_call,
  2161. external_rtp_create,
  2162. setup_rtp_connection,
  2163. cleanup_connection,
  2164. chan_ringing,
  2165. connection_made,
  2166. send_digit,
  2167. answer_call,
  2168. progress,
  2169. set_dtmf_payload,
  2170. hangup_connection,
  2171. set_local_capabilities);
  2172. /* start the h.323 listener */
  2173. if (h323_start_listener(h323_signalling_port, bindaddr)) {
  2174. ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
  2175. return -1;
  2176. }
  2177. /* Possibly register with a GK */
  2178. if (!gatekeeper_disable) {
  2179. if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
  2180. ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
  2181. return 0;
  2182. }
  2183. }
  2184. /* And start the monitor for the first time */
  2185. restart_monitor();
  2186. }
  2187. return res;
  2188. }
  2189. int unload_module()
  2190. {
  2191. struct oh323_pvt *p, *pl;
  2192. /* unregister commands */
  2193. ast_cli_unregister(&cli_debug);
  2194. ast_cli_unregister(&cli_no_debug);
  2195. ast_cli_unregister(&cli_trace);
  2196. ast_cli_unregister(&cli_no_trace);
  2197. ast_cli_unregister(&cli_show_codecs);
  2198. ast_cli_unregister(&cli_gk_cycle);
  2199. ast_cli_unregister(&cli_hangup_call);
  2200. ast_cli_unregister(&cli_show_tokens);
  2201. ast_cli_unregister(&cli_h323_reload);
  2202. ast_rtp_proto_unregister(&oh323_rtp);
  2203. ast_channel_unregister(&oh323_tech);
  2204. if (!ast_mutex_lock(&iflock)) {
  2205. /* hangup all interfaces if they have an owner */
  2206. p = iflist;
  2207. while(p) {
  2208. if (p->owner) {
  2209. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  2210. }
  2211. p = p->next;
  2212. }
  2213. iflist = NULL;
  2214. ast_mutex_unlock(&iflock);
  2215. } else {
  2216. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  2217. return -1;
  2218. }
  2219. if (!ast_mutex_lock(&monlock)) {
  2220. if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
  2221. /* this causes a seg, anyone know why? */
  2222. pthread_cancel(monitor_thread);
  2223. pthread_kill(monitor_thread, SIGURG);
  2224. pthread_join(monitor_thread, NULL);
  2225. }
  2226. monitor_thread = AST_PTHREADT_STOP;
  2227. ast_mutex_unlock(&monlock);
  2228. } else {
  2229. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  2230. return -1;
  2231. }
  2232. if (!ast_mutex_lock(&iflock)) {
  2233. /* destroy all the interfaces and free their memory */
  2234. p = iflist;
  2235. while(p) {
  2236. pl = p;
  2237. p = p->next;
  2238. /* free associated memory */
  2239. ast_mutex_destroy(&pl->lock);
  2240. free(pl);
  2241. }
  2242. iflist = NULL;
  2243. ast_mutex_unlock(&iflock);
  2244. } else {
  2245. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  2246. return -1;
  2247. }
  2248. h323_gk_urq();
  2249. h323_end_process();
  2250. io_context_destroy(io);
  2251. sched_context_destroy(sched);
  2252. delete_users();
  2253. delete_aliases();
  2254. prune_peers();
  2255. ast_mutex_destroy(&aliasl.lock);
  2256. ast_mutex_destroy(&userl.lock);
  2257. ast_mutex_destroy(&peerl.lock);
  2258. return 0;
  2259. }
  2260. int usecount()
  2261. {
  2262. return usecnt;
  2263. }
  2264. char *description()
  2265. {
  2266. return (char *) desc;
  2267. }
  2268. char *key()
  2269. {
  2270. return ASTERISK_GPL_KEY;
  2271. }