chan_h323.c 102 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498
  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. * \author Jeremy McNamara
  28. *
  29. * \par See also
  30. * \arg Config_h323
  31. * \extref OpenH323 http://www.voxgratia.org/
  32. *
  33. * \ingroup channel_drivers
  34. */
  35. /*** MODULEINFO
  36. <depend>openh323</depend>
  37. <defaultenabled>no</defaultenabled>
  38. <support_level>deprecated</support_level>
  39. <replacement>chan_ooh323</replacement>
  40. ***/
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. #include "asterisk.h"
  45. ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
  46. #ifdef __cplusplus
  47. }
  48. #endif
  49. #include <sys/types.h>
  50. #include <sys/socket.h>
  51. #include <sys/signal.h>
  52. #include <sys/param.h>
  53. #include <arpa/inet.h>
  54. #include <net/if.h>
  55. #include <netinet/in.h>
  56. #include <netinet/in_systm.h>
  57. #include <netinet/ip.h>
  58. #include <netdb.h>
  59. #include <fcntl.h>
  60. #ifdef __cplusplus
  61. extern "C" {
  62. #endif
  63. #include "asterisk/lock.h"
  64. #include "asterisk/channel.h"
  65. #include "asterisk/config.h"
  66. #include "asterisk/module.h"
  67. #include "asterisk/musiconhold.h"
  68. #include "asterisk/pbx.h"
  69. #include "asterisk/utils.h"
  70. #include "asterisk/sched.h"
  71. #include "asterisk/io.h"
  72. #include "asterisk/rtp_engine.h"
  73. #include "asterisk/acl.h"
  74. #include "asterisk/callerid.h"
  75. #include "asterisk/cli.h"
  76. #include "asterisk/dsp.h"
  77. #include "asterisk/causes.h"
  78. #include "asterisk/stringfields.h"
  79. #include "asterisk/abstract_jb.h"
  80. #include "asterisk/astobj.h"
  81. #include "asterisk/format.h"
  82. #include "asterisk/format_cap.h"
  83. #ifdef __cplusplus
  84. }
  85. #endif
  86. #undef open
  87. #undef close
  88. #include "h323/chan_h323.h"
  89. receive_digit_cb on_receive_digit;
  90. on_rtp_cb on_external_rtp_create;
  91. start_rtp_cb on_start_rtp_channel;
  92. setup_incoming_cb on_incoming_call;
  93. setup_outbound_cb on_outgoing_call;
  94. chan_ringing_cb on_chan_ringing;
  95. con_established_cb on_connection_established;
  96. clear_con_cb on_connection_cleared;
  97. answer_call_cb on_answer_call;
  98. progress_cb on_progress;
  99. rfc2833_cb on_set_rfc2833_payload;
  100. hangup_cb on_hangup;
  101. setcapabilities_cb on_setcapabilities;
  102. setpeercapabilities_cb on_setpeercapabilities;
  103. onhold_cb on_hold;
  104. int h323debug; /*!< global debug flag */
  105. /*! \brief Global jitterbuffer configuration - by default, jb is disabled
  106. * \note Values shown here match the defaults shown in h323.conf.sample */
  107. static struct ast_jb_conf default_jbconf =
  108. {
  109. .flags = 0,
  110. .max_size = 200,
  111. .resync_threshold = 1000,
  112. .impl = "fixed",
  113. .target_extra = 40,
  114. };
  115. static struct ast_jb_conf global_jbconf;
  116. /** Variables required by Asterisk */
  117. static const char tdesc[] = "The NuFone Network's Open H.323 Channel Driver";
  118. static const char config[] = "h323.conf";
  119. static char default_context[AST_MAX_CONTEXT] = "default";
  120. static struct sockaddr_in bindaddr;
  121. #define GLOBAL_CAPABILITY (ast_format_id_to_old_bitfield(AST_FORMAT_G723_1) | \
  122. ast_format_id_to_old_bitfield(AST_FORMAT_GSM) | \
  123. ast_format_id_to_old_bitfield(AST_FORMAT_ULAW) | \
  124. ast_format_id_to_old_bitfield(AST_FORMAT_ALAW) | \
  125. ast_format_id_to_old_bitfield(AST_FORMAT_G729A) | \
  126. ast_format_id_to_old_bitfield(AST_FORMAT_G726_AAL2) | \
  127. ast_format_id_to_old_bitfield(AST_FORMAT_H261)) \
  128. /** H.323 configuration values */
  129. static int h323_signalling_port = 1720;
  130. static char gatekeeper[100];
  131. static int gatekeeper_disable = 1;
  132. static int gatekeeper_discover = 0;
  133. static int gkroute = 0;
  134. /* Find user by alias (h.323 id) is default, alternative is the incoming call's source IP address*/
  135. static int userbyalias = 1;
  136. static int acceptAnonymous = 1;
  137. static unsigned int tos = 0;
  138. static unsigned int cos = 0;
  139. static char secret[50];
  140. static unsigned int unique = 0;
  141. static call_options_t global_options;
  142. /*! \brief Private structure of a OpenH323 channel */
  143. static struct oh323_pvt {
  144. ast_mutex_t lock; /*!< Channel private lock */
  145. call_options_t options; /*!<!< Options to be used during call setup */
  146. int alreadygone; /*!< Whether or not we've already been destroyed by our peer */
  147. int needdestroy; /*!< if we need to be destroyed */
  148. call_details_t cd; /*!< Call details */
  149. struct ast_channel *owner; /*!< Who owns us */
  150. struct sockaddr_in sa; /*!< Our peer */
  151. struct sockaddr_in redirip; /*!< Where our RTP should be going if not to us */
  152. int nonCodecCapability; /*!< non-audio capability */
  153. int outgoing; /*!< Outgoing or incoming call? */
  154. char exten[AST_MAX_EXTENSION]; /*!< Requested extension */
  155. char context[AST_MAX_CONTEXT]; /*!< Context where to start */
  156. char accountcode[256]; /*!< Account code */
  157. char rdnis[80]; /*!< Referring DNIS, if available */
  158. int amaflags; /*!< AMA Flags */
  159. struct ast_rtp_instance *rtp; /*!< RTP Session */
  160. struct ast_dsp *vad; /*!< Used for in-band DTMF detection */
  161. int nativeformats; /*!< Codec formats supported by a channel */
  162. int needhangup; /*!< Send hangup when Asterisk is ready */
  163. int hangupcause; /*!< Hangup cause from OpenH323 layer */
  164. int newstate; /*!< Pending state change */
  165. int newcontrol; /*!< Pending control to send */
  166. int newdigit; /*!< Pending DTMF digit to send */
  167. int newduration; /*!< Pending DTMF digit duration to send */
  168. h323_format pref_codec; /*!< Preferred codec */
  169. h323_format peercapability; /*!< Capabilities learned from peer */
  170. h323_format jointcapability; /*!< Common capabilities for local and remote side */
  171. struct ast_codec_pref peer_prefs; /*!< Preferenced list of codecs which remote side supports */
  172. int dtmf_pt[2]; /*!< Payload code used for RFC2833/CISCO messages */
  173. int curDTMF; /*!< DTMF tone being generated to Asterisk side */
  174. int DTMFsched; /*!< Scheduler descriptor for DTMF */
  175. int update_rtp_info; /*!< Configuration of fd's array is pending */
  176. int recvonly; /*!< Peer isn't wish to receive our voice stream */
  177. int txDtmfDigit; /*!< DTMF digit being to send to H.323 side */
  178. int noInbandDtmf; /*!< Inband DTMF processing by DSP isn't available */
  179. int connection_established; /*!< Call got CONNECT message */
  180. int got_progress; /*!< Call got PROGRESS message, pass inband audio */
  181. struct oh323_pvt *next; /*!< Next channel in list */
  182. } *iflist = NULL;
  183. /*! \brief H323 User list */
  184. static struct h323_user_list {
  185. ASTOBJ_CONTAINER_COMPONENTS(struct oh323_user);
  186. } userl;
  187. /*! \brief H323 peer list */
  188. static struct h323_peer_list {
  189. ASTOBJ_CONTAINER_COMPONENTS(struct oh323_peer);
  190. } peerl;
  191. /*! \brief H323 alias list */
  192. static struct h323_alias_list {
  193. ASTOBJ_CONTAINER_COMPONENTS(struct oh323_alias);
  194. } aliasl;
  195. /* Asterisk RTP stuff */
  196. static struct ast_sched_context *sched;
  197. static struct io_context *io;
  198. AST_MUTEX_DEFINE_STATIC(iflock); /*!< Protect the interface list (oh323_pvt) */
  199. /*! \brief Protect the H.323 monitoring thread, so only one process can kill or start it, and not
  200. when it's doing something critical. */
  201. AST_MUTEX_DEFINE_STATIC(monlock);
  202. /*! \brief Protect the H.323 capabilities list, to avoid more than one channel to set the capabilities simultaneaously in the h323 stack. */
  203. AST_MUTEX_DEFINE_STATIC(caplock);
  204. /*! \brief Protect the reload process */
  205. AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
  206. static int h323_reloading = 0;
  207. /*! \brief This is the thread for the monitor which checks for input on the channels
  208. which are not currently in use. */
  209. static pthread_t monitor_thread = AST_PTHREADT_NULL;
  210. static int restart_monitor(void);
  211. static int h323_do_reload(void);
  212. static void delete_users(void);
  213. static void delete_aliases(void);
  214. static void prune_peers(void);
  215. static struct ast_channel *oh323_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause);
  216. static int oh323_digit_begin(struct ast_channel *c, char digit);
  217. static int oh323_digit_end(struct ast_channel *c, char digit, unsigned int duration);
  218. static int oh323_call(struct ast_channel *c, char *dest, int timeout);
  219. static int oh323_hangup(struct ast_channel *c);
  220. static int oh323_answer(struct ast_channel *c);
  221. static struct ast_frame *oh323_read(struct ast_channel *c);
  222. static int oh323_write(struct ast_channel *c, struct ast_frame *frame);
  223. static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen);
  224. static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
  225. static struct ast_channel_tech oh323_tech = {
  226. .type = "H323",
  227. .description = tdesc,
  228. .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
  229. .requester = oh323_request,
  230. .send_digit_begin = oh323_digit_begin,
  231. .send_digit_end = oh323_digit_end,
  232. .call = oh323_call,
  233. .hangup = oh323_hangup,
  234. .answer = oh323_answer,
  235. .read = oh323_read,
  236. .write = oh323_write,
  237. .indicate = oh323_indicate,
  238. .fixup = oh323_fixup,
  239. .bridge = ast_rtp_instance_bridge,
  240. };
  241. static const char* redirectingreason2str(int redirectingreason)
  242. {
  243. switch (redirectingreason) {
  244. case 0:
  245. return "UNKNOWN";
  246. case 1:
  247. return "BUSY";
  248. case 2:
  249. return "NO_REPLY";
  250. case 0xF:
  251. return "UNCONDITIONAL";
  252. default:
  253. return "NOREDIRECT";
  254. }
  255. }
  256. static void oh323_destroy_alias(struct oh323_alias *alias)
  257. {
  258. if (h323debug)
  259. ast_debug(1, "Destroying alias '%s'\n", alias->name);
  260. ast_free(alias);
  261. }
  262. static void oh323_destroy_user(struct oh323_user *user)
  263. {
  264. if (h323debug)
  265. ast_debug(1, "Destroying user '%s'\n", user->name);
  266. ast_free_ha(user->ha);
  267. ast_free(user);
  268. }
  269. static void oh323_destroy_peer(struct oh323_peer *peer)
  270. {
  271. if (h323debug)
  272. ast_debug(1, "Destroying peer '%s'\n", peer->name);
  273. ast_free_ha(peer->ha);
  274. ast_free(peer);
  275. }
  276. static int oh323_simulate_dtmf_end(const void *data)
  277. {
  278. struct oh323_pvt *pvt = (struct oh323_pvt *)data;
  279. if (pvt) {
  280. ast_mutex_lock(&pvt->lock);
  281. /* Don't hold pvt lock while trying to lock the channel */
  282. while (pvt->owner && ast_channel_trylock(pvt->owner)) {
  283. DEADLOCK_AVOIDANCE(&pvt->lock);
  284. }
  285. if (pvt->owner) {
  286. struct ast_frame f = {
  287. .frametype = AST_FRAME_DTMF_END,
  288. .subclass.integer = pvt->curDTMF,
  289. .samples = 0,
  290. .src = "SIMULATE_DTMF_END",
  291. };
  292. ast_queue_frame(pvt->owner, &f);
  293. ast_channel_unlock(pvt->owner);
  294. }
  295. pvt->DTMFsched = -1;
  296. ast_mutex_unlock(&pvt->lock);
  297. }
  298. return 0;
  299. }
  300. /*! \brief Channel and private structures should be already locked */
  301. static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
  302. {
  303. h323_format chan_nativeformats_bits = ast_format_cap_to_old_bitfield(c->nativeformats);
  304. if (chan_nativeformats_bits != pvt->nativeformats) {
  305. if (h323debug)
  306. ast_debug(1, "Preparing %s for new native format\n", c->name);
  307. ast_format_cap_from_old_bitfield(c->nativeformats, pvt->nativeformats);
  308. ast_set_read_format(c, &c->readformat);
  309. ast_set_write_format(c, &c->writeformat);
  310. }
  311. if (pvt->needhangup) {
  312. if (h323debug)
  313. ast_debug(1, "Process pending hangup for %s\n", c->name);
  314. c->_softhangup |= AST_SOFTHANGUP_DEV;
  315. c->hangupcause = pvt->hangupcause;
  316. ast_queue_hangup_with_cause(c, pvt->hangupcause);
  317. pvt->needhangup = 0;
  318. pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->DTMFsched = -1;
  319. }
  320. if (pvt->newstate >= 0) {
  321. ast_setstate(c, pvt->newstate);
  322. pvt->newstate = -1;
  323. }
  324. if (pvt->newcontrol >= 0) {
  325. ast_queue_control(c, pvt->newcontrol);
  326. pvt->newcontrol = -1;
  327. }
  328. if (pvt->newdigit >= 0) {
  329. struct ast_frame f = {
  330. .frametype = AST_FRAME_DTMF_END,
  331. .subclass.integer = pvt->newdigit,
  332. .samples = pvt->newduration * 8,
  333. .len = pvt->newduration,
  334. .src = "UPDATE_INFO",
  335. };
  336. if (pvt->newdigit == ' ') { /* signalUpdate message */
  337. f.subclass.integer = pvt->curDTMF;
  338. if (pvt->DTMFsched >= 0) {
  339. AST_SCHED_DEL(sched, pvt->DTMFsched);
  340. }
  341. } else { /* Regular input or signal message */
  342. if (pvt->newduration) { /* This is a signal, signalUpdate follows */
  343. f.frametype = AST_FRAME_DTMF_BEGIN;
  344. AST_SCHED_DEL(sched, pvt->DTMFsched);
  345. pvt->DTMFsched = ast_sched_add(sched, pvt->newduration, oh323_simulate_dtmf_end, pvt);
  346. if (h323debug)
  347. ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", pvt->newduration, pvt->DTMFsched);
  348. }
  349. pvt->curDTMF = pvt->newdigit;
  350. }
  351. ast_queue_frame(c, &f);
  352. pvt->newdigit = -1;
  353. }
  354. if (pvt->update_rtp_info > 0) {
  355. if (pvt->rtp) {
  356. ast_jb_configure(c, &global_jbconf);
  357. ast_channel_set_fd(c, 0, ast_rtp_instance_fd(pvt->rtp, 0));
  358. ast_channel_set_fd(c, 1, ast_rtp_instance_fd(pvt->rtp, 1));
  359. ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
  360. }
  361. pvt->update_rtp_info = -1;
  362. }
  363. }
  364. /*! \brief Only channel structure should be locked */
  365. static void oh323_update_info(struct ast_channel *c)
  366. {
  367. struct oh323_pvt *pvt = c->tech_pvt;
  368. if (pvt) {
  369. ast_mutex_lock(&pvt->lock);
  370. __oh323_update_info(c, pvt);
  371. ast_mutex_unlock(&pvt->lock);
  372. }
  373. }
  374. static void cleanup_call_details(call_details_t *cd)
  375. {
  376. if (cd->call_token) {
  377. ast_free(cd->call_token);
  378. cd->call_token = NULL;
  379. }
  380. if (cd->call_source_aliases) {
  381. ast_free(cd->call_source_aliases);
  382. cd->call_source_aliases = NULL;
  383. }
  384. if (cd->call_dest_alias) {
  385. ast_free(cd->call_dest_alias);
  386. cd->call_dest_alias = NULL;
  387. }
  388. if (cd->call_source_name) {
  389. ast_free(cd->call_source_name);
  390. cd->call_source_name = NULL;
  391. }
  392. if (cd->call_source_e164) {
  393. ast_free(cd->call_source_e164);
  394. cd->call_source_e164 = NULL;
  395. }
  396. if (cd->call_dest_e164) {
  397. ast_free(cd->call_dest_e164);
  398. cd->call_dest_e164 = NULL;
  399. }
  400. if (cd->sourceIp) {
  401. ast_free(cd->sourceIp);
  402. cd->sourceIp = NULL;
  403. }
  404. if (cd->redirect_number) {
  405. ast_free(cd->redirect_number);
  406. cd->redirect_number = NULL;
  407. }
  408. }
  409. static void __oh323_destroy(struct oh323_pvt *pvt)
  410. {
  411. struct oh323_pvt *cur, *prev = NULL;
  412. AST_SCHED_DEL(sched, pvt->DTMFsched);
  413. if (pvt->rtp) {
  414. ast_rtp_instance_destroy(pvt->rtp);
  415. }
  416. /* Free dsp used for in-band DTMF detection */
  417. if (pvt->vad) {
  418. ast_dsp_free(pvt->vad);
  419. }
  420. cleanup_call_details(&pvt->cd);
  421. /* Unlink us from the owner if we have one */
  422. if (pvt->owner) {
  423. ast_channel_lock(pvt->owner);
  424. if (h323debug)
  425. ast_debug(1, "Detaching from %s\n", pvt->owner->name);
  426. pvt->owner->tech_pvt = NULL;
  427. ast_channel_unlock(pvt->owner);
  428. }
  429. cur = iflist;
  430. while(cur) {
  431. if (cur == pvt) {
  432. if (prev)
  433. prev->next = cur->next;
  434. else
  435. iflist = cur->next;
  436. break;
  437. }
  438. prev = cur;
  439. cur = cur->next;
  440. }
  441. if (!cur) {
  442. ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
  443. } else {
  444. ast_mutex_unlock(&pvt->lock);
  445. ast_mutex_destroy(&pvt->lock);
  446. ast_free(pvt);
  447. }
  448. }
  449. static void oh323_destroy(struct oh323_pvt *pvt)
  450. {
  451. if (h323debug) {
  452. ast_debug(1, "Destroying channel %s\n", (pvt->owner ? pvt->owner->name : "<unknown>"));
  453. }
  454. ast_mutex_lock(&iflock);
  455. ast_mutex_lock(&pvt->lock);
  456. __oh323_destroy(pvt);
  457. ast_mutex_unlock(&iflock);
  458. }
  459. static int oh323_digit_begin(struct ast_channel *c, char digit)
  460. {
  461. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
  462. char *token;
  463. if (!pvt) {
  464. ast_log(LOG_ERROR, "No private structure?! This is bad\n");
  465. return -1;
  466. }
  467. ast_mutex_lock(&pvt->lock);
  468. if (pvt->rtp &&
  469. (((pvt->options.dtmfmode & H323_DTMF_RFC2833) && pvt->dtmf_pt[0])
  470. /*|| ((pvt->options.dtmfmode & H323_DTMF_CISCO) && pvt->dtmf_pt[1]))*/)) {
  471. /* out-of-band DTMF */
  472. if (h323debug) {
  473. ast_log(LOG_DTMF, "Begin sending out-of-band digit %c on %s\n", digit, c->name);
  474. }
  475. ast_rtp_instance_dtmf_begin(pvt->rtp, digit);
  476. ast_mutex_unlock(&pvt->lock);
  477. } else if (pvt->txDtmfDigit != digit) {
  478. /* in-band DTMF */
  479. if (h323debug) {
  480. ast_log(LOG_DTMF, "Begin sending inband digit %c on %s\n", digit, c->name);
  481. }
  482. pvt->txDtmfDigit = digit;
  483. token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
  484. ast_mutex_unlock(&pvt->lock);
  485. h323_send_tone(token, digit);
  486. if (token) {
  487. ast_free(token);
  488. }
  489. } else
  490. ast_mutex_unlock(&pvt->lock);
  491. oh323_update_info(c);
  492. return 0;
  493. }
  494. /*! \brief
  495. * Send (play) the specified digit to the channel.
  496. *
  497. */
  498. static int oh323_digit_end(struct ast_channel *c, char digit, unsigned int duration)
  499. {
  500. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
  501. char *token;
  502. if (!pvt) {
  503. ast_log(LOG_ERROR, "No private structure?! This is bad\n");
  504. return -1;
  505. }
  506. ast_mutex_lock(&pvt->lock);
  507. if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833) && ((pvt->dtmf_pt[0] > 0) || (pvt->dtmf_pt[0] > 0))) {
  508. /* out-of-band DTMF */
  509. if (h323debug) {
  510. ast_log(LOG_DTMF, "End sending out-of-band digit %c on %s, duration %d\n", digit, c->name, duration);
  511. }
  512. ast_rtp_instance_dtmf_end(pvt->rtp, digit);
  513. ast_mutex_unlock(&pvt->lock);
  514. } else {
  515. /* in-band DTMF */
  516. if (h323debug) {
  517. ast_log(LOG_DTMF, "End sending inband digit %c on %s, duration %d\n", digit, c->name, duration);
  518. }
  519. pvt->txDtmfDigit = ' ';
  520. token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
  521. ast_mutex_unlock(&pvt->lock);
  522. h323_send_tone(token, ' ');
  523. if (token) {
  524. ast_free(token);
  525. }
  526. }
  527. oh323_update_info(c);
  528. return 0;
  529. }
  530. /*! \brief
  531. * Make a call over the specified channel to the specified
  532. * destination.
  533. * Returns -1 on error, 0 on success.
  534. */
  535. static int oh323_call(struct ast_channel *c, char *dest, int timeout)
  536. {
  537. int res = 0;
  538. struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
  539. const char *addr;
  540. char called_addr[1024];
  541. if (h323debug) {
  542. ast_debug(1, "Calling to %s on %s\n", dest, c->name);
  543. }
  544. if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
  545. ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
  546. return -1;
  547. }
  548. ast_mutex_lock(&pvt->lock);
  549. if (!gatekeeper_disable) {
  550. if (ast_strlen_zero(pvt->exten)) {
  551. ast_copy_string(called_addr, dest, sizeof(called_addr));
  552. } else {
  553. snprintf(called_addr, sizeof(called_addr), "%s@%s", pvt->exten, dest);
  554. }
  555. } else {
  556. res = htons(pvt->sa.sin_port);
  557. addr = ast_inet_ntoa(pvt->sa.sin_addr);
  558. if (ast_strlen_zero(pvt->exten)) {
  559. snprintf(called_addr, sizeof(called_addr), "%s:%d", addr, res);
  560. } else {
  561. snprintf(called_addr, sizeof(called_addr), "%s@%s:%d", pvt->exten, addr, res);
  562. }
  563. }
  564. /* make sure null terminated */
  565. called_addr[sizeof(called_addr) - 1] = '\0';
  566. if (c->connected.id.number.valid && c->connected.id.number.str) {
  567. ast_copy_string(pvt->options.cid_num, c->connected.id.number.str, sizeof(pvt->options.cid_num));
  568. }
  569. if (c->connected.id.name.valid && c->connected.id.name.str) {
  570. ast_copy_string(pvt->options.cid_name, c->connected.id.name.str, sizeof(pvt->options.cid_name));
  571. }
  572. if (c->redirecting.from.number.valid && c->redirecting.from.number.str) {
  573. ast_copy_string(pvt->options.cid_rdnis, c->redirecting.from.number.str, sizeof(pvt->options.cid_rdnis));
  574. }
  575. pvt->options.presentation = ast_party_id_presentation(&c->connected.id);
  576. pvt->options.type_of_number = c->connected.id.number.plan;
  577. if ((addr = pbx_builtin_getvar_helper(c, "PRIREDIRECTREASON"))) {
  578. if (!strcasecmp(addr, "UNKNOWN"))
  579. pvt->options.redirect_reason = 0;
  580. else if (!strcasecmp(addr, "BUSY"))
  581. pvt->options.redirect_reason = 1;
  582. else if (!strcasecmp(addr, "NO_REPLY"))
  583. pvt->options.redirect_reason = 2;
  584. else if (!strcasecmp(addr, "UNCONDITIONAL"))
  585. pvt->options.redirect_reason = 15;
  586. else
  587. pvt->options.redirect_reason = -1;
  588. } else
  589. pvt->options.redirect_reason = -1;
  590. pvt->options.transfer_capability = c->transfercapability;
  591. /* indicate that this is an outgoing call */
  592. pvt->outgoing = 1;
  593. ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", c->transfercapability, ast_transfercapability2str(c->transfercapability));
  594. if (h323debug)
  595. ast_debug(1, "Placing outgoing call to %s, %d/%d\n", called_addr, pvt->options.dtmfcodec[0], pvt->options.dtmfcodec[1]);
  596. ast_mutex_unlock(&pvt->lock);
  597. res = h323_make_call(called_addr, &(pvt->cd), &pvt->options);
  598. if (res) {
  599. ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
  600. return -1;
  601. }
  602. oh323_update_info(c);
  603. return 0;
  604. }
  605. static int oh323_answer(struct ast_channel *c)
  606. {
  607. int res;
  608. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
  609. char *token;
  610. if (h323debug)
  611. ast_debug(1, "Answering on %s\n", c->name);
  612. ast_mutex_lock(&pvt->lock);
  613. token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
  614. ast_mutex_unlock(&pvt->lock);
  615. res = h323_answering_call(token, 0);
  616. if (token)
  617. ast_free(token);
  618. oh323_update_info(c);
  619. if (c->_state != AST_STATE_UP) {
  620. ast_setstate(c, AST_STATE_UP);
  621. }
  622. return res;
  623. }
  624. static int oh323_hangup(struct ast_channel *c)
  625. {
  626. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
  627. int q931cause = AST_CAUSE_NORMAL_CLEARING;
  628. char *call_token;
  629. if (h323debug)
  630. ast_debug(1, "Hanging up and scheduling destroy of call %s\n", c->name);
  631. if (!c->tech_pvt) {
  632. ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
  633. return 0;
  634. }
  635. ast_mutex_lock(&pvt->lock);
  636. /* Determine how to disconnect */
  637. if (pvt->owner != c) {
  638. ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
  639. ast_mutex_unlock(&pvt->lock);
  640. return 0;
  641. }
  642. pvt->owner = NULL;
  643. c->tech_pvt = NULL;
  644. if (c->hangupcause) {
  645. q931cause = c->hangupcause;
  646. } else {
  647. const char *cause = pbx_builtin_getvar_helper(c, "DIALSTATUS");
  648. if (cause) {
  649. if (!strcmp(cause, "CONGESTION")) {
  650. q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
  651. } else if (!strcmp(cause, "BUSY")) {
  652. q931cause = AST_CAUSE_USER_BUSY;
  653. } else if (!strcmp(cause, "CHANISUNVAIL")) {
  654. q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
  655. } else if (!strcmp(cause, "NOANSWER")) {
  656. q931cause = AST_CAUSE_NO_ANSWER;
  657. } else if (!strcmp(cause, "CANCEL")) {
  658. q931cause = AST_CAUSE_CALL_REJECTED;
  659. }
  660. }
  661. }
  662. /* Start the process if it's not already started */
  663. if (!pvt->alreadygone && !pvt->hangupcause) {
  664. call_token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
  665. if (call_token) {
  666. /* Release lock to eliminate deadlock */
  667. ast_mutex_unlock(&pvt->lock);
  668. if (h323_clear_call(call_token, q931cause)) {
  669. ast_log(LOG_WARNING, "ClearCall failed.\n");
  670. }
  671. ast_free(call_token);
  672. ast_mutex_lock(&pvt->lock);
  673. }
  674. }
  675. pvt->needdestroy = 1;
  676. ast_mutex_unlock(&pvt->lock);
  677. /* Update usage counter */
  678. ast_module_unref(ast_module_info->self);
  679. return 0;
  680. }
  681. /*! \brief Retrieve audio/etc from channel. Assumes pvt->lock is already held. */
  682. static struct ast_frame *oh323_rtp_read(struct oh323_pvt *pvt)
  683. {
  684. struct ast_frame *f;
  685. /* Only apply it for the first packet, we just need the correct ip/port */
  686. if (pvt->options.nat) {
  687. ast_rtp_instance_set_prop(pvt->rtp, AST_RTP_PROPERTY_NAT, pvt->options.nat);
  688. pvt->options.nat = 0;
  689. }
  690. f = ast_rtp_instance_read(pvt->rtp, 0);
  691. /* Don't send RFC2833 if we're not supposed to */
  692. if (f && (f->frametype == AST_FRAME_DTMF) && !(pvt->options.dtmfmode & (H323_DTMF_RFC2833 | H323_DTMF_CISCO))) {
  693. return &ast_null_frame;
  694. }
  695. if (f && pvt->owner) {
  696. /* We already hold the channel lock */
  697. if (f->frametype == AST_FRAME_VOICE) {
  698. if (!ast_format_cap_iscompatible(pvt->owner->nativeformats, &f->subclass.format)) {
  699. /* Try to avoid deadlock */
  700. if (ast_channel_trylock(pvt->owner)) {
  701. ast_log(LOG_NOTICE, "Format changed but channel is locked. Ignoring frame...\n");
  702. return &ast_null_frame;
  703. }
  704. if (h323debug)
  705. ast_debug(1, "Oooh, format changed to '%s'\n", ast_getformatname(&f->subclass.format));
  706. ast_format_cap_set(pvt->owner->nativeformats, &f->subclass.format);
  707. pvt->nativeformats = ast_format_to_old_bitfield(&f->subclass.format);
  708. ast_set_read_format(pvt->owner, &pvt->owner->readformat);
  709. ast_set_write_format(pvt->owner, &pvt->owner->writeformat);
  710. ast_channel_unlock(pvt->owner);
  711. }
  712. /* Do in-band DTMF detection */
  713. if ((pvt->options.dtmfmode & H323_DTMF_INBAND) && pvt->vad) {
  714. if ((pvt->nativeformats & (AST_FORMAT_SLINEAR | AST_FORMAT_ALAW | AST_FORMAT_ULAW))) {
  715. if (!ast_channel_trylock(pvt->owner)) {
  716. f = ast_dsp_process(pvt->owner, pvt->vad, f);
  717. ast_channel_unlock(pvt->owner);
  718. }
  719. else
  720. ast_log(LOG_NOTICE, "Unable to process inband DTMF while channel is locked\n");
  721. } else if (pvt->nativeformats && !pvt->noInbandDtmf) {
  722. ast_log(LOG_NOTICE, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(&f->subclass.format));
  723. pvt->noInbandDtmf = 1;
  724. }
  725. if (f &&(f->frametype == AST_FRAME_DTMF)) {
  726. if (h323debug)
  727. ast_log(LOG_DTMF, "Received in-band digit %c.\n", f->subclass.integer);
  728. }
  729. }
  730. }
  731. }
  732. return f;
  733. }
  734. static struct ast_frame *oh323_read(struct ast_channel *c)
  735. {
  736. struct ast_frame *fr;
  737. struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
  738. ast_mutex_lock(&pvt->lock);
  739. __oh323_update_info(c, pvt);
  740. switch(c->fdno) {
  741. case 0:
  742. fr = oh323_rtp_read(pvt);
  743. break;
  744. case 1:
  745. if (pvt->rtp)
  746. fr = ast_rtp_instance_read(pvt->rtp, 1);
  747. else
  748. fr = &ast_null_frame;
  749. break;
  750. default:
  751. ast_log(LOG_ERROR, "Unable to handle fd %d on channel %s\n", c->fdno, c->name);
  752. fr = &ast_null_frame;
  753. break;
  754. }
  755. ast_mutex_unlock(&pvt->lock);
  756. return fr;
  757. }
  758. static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
  759. {
  760. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
  761. int res = 0;
  762. if (frame->frametype != AST_FRAME_VOICE) {
  763. if (frame->frametype == AST_FRAME_IMAGE) {
  764. return 0;
  765. } else {
  766. ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
  767. return 0;
  768. }
  769. } else {
  770. if (!(ast_format_cap_iscompatible(c->nativeformats, &frame->subclass.format))) {
  771. char tmp[256];
  772. ast_log(LOG_WARNING, "Asked to transmit frame type '%s', while native formats is '%s' (read/write = %s/%s)\n",
  773. ast_getformatname(&frame->subclass.format), ast_getformatname_multiple(tmp, sizeof(tmp), c->nativeformats), ast_getformatname(&c->readformat), ast_getformatname(&c->writeformat));
  774. return 0;
  775. }
  776. }
  777. if (pvt) {
  778. ast_mutex_lock(&pvt->lock);
  779. if (pvt->rtp && !pvt->recvonly)
  780. res = ast_rtp_instance_write(pvt->rtp, frame);
  781. __oh323_update_info(c, pvt);
  782. ast_mutex_unlock(&pvt->lock);
  783. }
  784. return res;
  785. }
  786. static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen)
  787. {
  788. struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
  789. char *token = (char *)NULL;
  790. int res = -1;
  791. int got_progress;
  792. ast_mutex_lock(&pvt->lock);
  793. token = (pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL);
  794. got_progress = pvt->got_progress;
  795. if (condition == AST_CONTROL_PROGRESS)
  796. pvt->got_progress = 1;
  797. else if ((condition == AST_CONTROL_BUSY) || (condition == AST_CONTROL_CONGESTION))
  798. pvt->alreadygone = 1;
  799. ast_mutex_unlock(&pvt->lock);
  800. if (h323debug)
  801. ast_debug(1, "OH323: Indicating %d on %s (%s)\n", condition, token, c->name);
  802. switch(condition) {
  803. case AST_CONTROL_RINGING:
  804. if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
  805. h323_send_alerting(token);
  806. res = (got_progress ? 0 : -1); /* Do not simulate any audio tones if we got PROGRESS message */
  807. }
  808. break;
  809. case AST_CONTROL_PROGRESS:
  810. if (c->_state != AST_STATE_UP) {
  811. /* Do not send PROGRESS message more than once */
  812. if (!got_progress)
  813. h323_send_progress(token);
  814. res = 0;
  815. }
  816. break;
  817. case AST_CONTROL_BUSY:
  818. if (c->_state != AST_STATE_UP) {
  819. h323_answering_call(token, 1);
  820. ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
  821. res = 0;
  822. }
  823. break;
  824. case AST_CONTROL_INCOMPLETE:
  825. /* While h323 does support overlapped dialing, this channel driver does not
  826. * at this time. Treat a response of Incomplete as if it were congestion.
  827. */
  828. case AST_CONTROL_CONGESTION:
  829. if (c->_state != AST_STATE_UP) {
  830. h323_answering_call(token, 1);
  831. ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
  832. res = 0;
  833. }
  834. break;
  835. case AST_CONTROL_HOLD:
  836. h323_hold_call(token, 1);
  837. /* We should start MOH only if remote party isn't provide audio for us */
  838. ast_moh_start(c, data, NULL);
  839. res = 0;
  840. break;
  841. case AST_CONTROL_UNHOLD:
  842. h323_hold_call(token, 0);
  843. ast_moh_stop(c);
  844. res = 0;
  845. break;
  846. case AST_CONTROL_SRCUPDATE:
  847. ast_rtp_instance_update_source(pvt->rtp);
  848. res = 0;
  849. break;
  850. case AST_CONTROL_SRCCHANGE:
  851. ast_rtp_instance_change_source(pvt->rtp);
  852. res = 0;
  853. break;
  854. case AST_CONTROL_PROCEEDING:
  855. case -1:
  856. break;
  857. default:
  858. ast_log(LOG_WARNING, "OH323: Don't know how to indicate condition %d on %s\n", condition, token);
  859. break;
  860. }
  861. if (h323debug)
  862. ast_debug(1, "OH323: Indicated %d on %s, res=%d\n", condition, token, res);
  863. if (token)
  864. ast_free(token);
  865. oh323_update_info(c);
  866. return res;
  867. }
  868. static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
  869. {
  870. struct oh323_pvt *pvt = (struct oh323_pvt *) newchan->tech_pvt;
  871. ast_mutex_lock(&pvt->lock);
  872. if (pvt->owner != oldchan) {
  873. ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, pvt->owner);
  874. return -1;
  875. }
  876. pvt->owner = newchan;
  877. ast_mutex_unlock(&pvt->lock);
  878. return 0;
  879. }
  880. static int __oh323_rtp_create(struct oh323_pvt *pvt)
  881. {
  882. struct ast_sockaddr our_addr;
  883. if (pvt->rtp)
  884. return 0;
  885. {
  886. struct ast_sockaddr tmp;
  887. ast_sockaddr_from_sin(&tmp, &bindaddr);
  888. if (ast_find_ourip(&our_addr, &tmp, AF_INET)) {
  889. ast_mutex_unlock(&pvt->lock);
  890. ast_log(LOG_ERROR, "Unable to locate local IP address for RTP stream\n");
  891. return -1;
  892. }
  893. }
  894. our_addr.ss.ss_family = AF_INET;
  895. pvt->rtp = ast_rtp_instance_new("asterisk", sched, &our_addr, NULL);
  896. if (!pvt->rtp) {
  897. ast_mutex_unlock(&pvt->lock);
  898. ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
  899. return -1;
  900. }
  901. if (h323debug)
  902. ast_debug(1, "Created RTP channel\n");
  903. ast_rtp_instance_set_qos(pvt->rtp, tos, cos, "H323 RTP");
  904. if (h323debug)
  905. ast_debug(1, "Setting NAT on RTP to %d\n", pvt->options.nat);
  906. ast_rtp_instance_set_prop(pvt->rtp, AST_RTP_PROPERTY_NAT, pvt->options.nat);
  907. if (pvt->dtmf_pt[0] > 0)
  908. ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(pvt->rtp), pvt->rtp, pvt->dtmf_pt[0], "audio", "telephone-event", 0);
  909. if (pvt->dtmf_pt[1] > 0)
  910. ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(pvt->rtp), pvt->rtp, pvt->dtmf_pt[1], "audio", "cisco-telephone-event", 0);
  911. if (pvt->peercapability)
  912. ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(pvt->rtp), pvt->rtp, &pvt->peer_prefs);
  913. if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
  914. ast_jb_configure(pvt->owner, &global_jbconf);
  915. ast_channel_set_fd(pvt->owner, 0, ast_rtp_instance_fd(pvt->rtp, 0));
  916. ast_channel_set_fd(pvt->owner, 1, ast_rtp_instance_fd(pvt->rtp, 1));
  917. ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
  918. ast_channel_unlock(pvt->owner);
  919. } else
  920. pvt->update_rtp_info = 1;
  921. return 0;
  922. }
  923. /*! \brief Private structure should be locked on a call */
  924. static struct ast_channel *__oh323_new(struct oh323_pvt *pvt, int state, const char *host, const char *linkedid)
  925. {
  926. struct ast_channel *ch;
  927. char *cid_num, *cid_name;
  928. h323_format fmt;
  929. struct ast_format tmpfmt;
  930. if (!ast_strlen_zero(pvt->options.cid_num))
  931. cid_num = pvt->options.cid_num;
  932. else
  933. cid_num = pvt->cd.call_source_e164;
  934. if (!ast_strlen_zero(pvt->options.cid_name))
  935. cid_name = pvt->options.cid_name;
  936. else
  937. cid_name = pvt->cd.call_source_name;
  938. /* Don't hold a oh323_pvt lock while we allocate a chanel */
  939. ast_mutex_unlock(&pvt->lock);
  940. ch = ast_channel_alloc(1, state, cid_num, cid_name, pvt->accountcode, pvt->exten, pvt->context, linkedid, pvt->amaflags, "H323/%s", host);
  941. /* Update usage counter */
  942. ast_module_ref(ast_module_info->self);
  943. ast_mutex_lock(&pvt->lock);
  944. if (ch) {
  945. ch->tech = &oh323_tech;
  946. if (!(fmt = pvt->jointcapability) && !(fmt = pvt->options.capability))
  947. fmt = global_options.capability;
  948. ast_format_cap_from_old_bitfield(ch->nativeformats, fmt);
  949. ast_codec_choose(&pvt->options.prefs, ch->nativeformats, 1, &tmpfmt)/* | (pvt->jointcapability & AST_FORMAT_VIDEO_MASK)*/;
  950. ast_format_cap_set(ch->nativeformats, &tmpfmt);
  951. pvt->nativeformats = ast_format_cap_to_old_bitfield(ch->nativeformats);
  952. ast_best_codec(ch->nativeformats, &tmpfmt);
  953. ast_format_copy(&ch->writeformat, &tmpfmt);
  954. ast_format_copy(&ch->rawwriteformat, &tmpfmt);
  955. ast_format_copy(&ch->readformat, &tmpfmt);
  956. ast_format_copy(&ch->rawreadformat, &tmpfmt);
  957. if (!pvt->rtp)
  958. __oh323_rtp_create(pvt);
  959. #if 0
  960. ast_channel_set_fd(ch, 0, ast_rtp_instance_fd(pvt->rtp, 0));
  961. ast_channel_set_fd(ch, 1, ast_rtp_instance_fd(pvt->rtp, 1));
  962. #endif
  963. #ifdef VIDEO_SUPPORT
  964. if (pvt->vrtp) {
  965. ast_channel_set_fd(ch, 2, ast_rtp_instance_fd(pvt->vrtp, 0));
  966. ast_channel_set_fd(ch, 3, ast_rtp_instance_fd(pvt->vrtp, 1));
  967. }
  968. #endif
  969. #ifdef T38_SUPPORT
  970. if (pvt->udptl) {
  971. ast_channel_set_fd(ch, 4, ast_udptl_fd(pvt->udptl));
  972. }
  973. #endif
  974. if (state == AST_STATE_RING) {
  975. ch->rings = 1;
  976. }
  977. /* Allocate dsp for in-band DTMF support */
  978. if (pvt->options.dtmfmode & H323_DTMF_INBAND) {
  979. pvt->vad = ast_dsp_new();
  980. ast_dsp_set_features(pvt->vad, DSP_FEATURE_DIGIT_DETECT);
  981. }
  982. /* Register channel functions. */
  983. ch->tech_pvt = pvt;
  984. /* Set the owner of this channel */
  985. pvt->owner = ch;
  986. ast_copy_string(ch->context, pvt->context, sizeof(ch->context));
  987. ast_copy_string(ch->exten, pvt->exten, sizeof(ch->exten));
  988. ch->priority = 1;
  989. if (!ast_strlen_zero(pvt->accountcode)) {
  990. ast_string_field_set(ch, accountcode, pvt->accountcode);
  991. }
  992. if (pvt->amaflags) {
  993. ch->amaflags = pvt->amaflags;
  994. }
  995. /* Don't use ast_set_callerid() here because it will
  996. * generate a needless NewCallerID event */
  997. if (!ast_strlen_zero(cid_num)) {
  998. ch->caller.ani.number.valid = 1;
  999. ch->caller.ani.number.str = ast_strdup(cid_num);
  1000. }
  1001. if (pvt->cd.redirect_reason >= 0) {
  1002. ch->redirecting.from.number.valid = 1;
  1003. ch->redirecting.from.number.str = ast_strdup(pvt->cd.redirect_number);
  1004. pbx_builtin_setvar_helper(ch, "PRIREDIRECTREASON", redirectingreason2str(pvt->cd.redirect_reason));
  1005. }
  1006. ch->caller.id.name.presentation = pvt->cd.presentation;
  1007. ch->caller.id.number.presentation = pvt->cd.presentation;
  1008. ch->caller.id.number.plan = pvt->cd.type_of_number;
  1009. if (!ast_strlen_zero(pvt->exten) && strcmp(pvt->exten, "s")) {
  1010. ch->dialed.number.str = ast_strdup(pvt->exten);
  1011. }
  1012. if (pvt->cd.transfer_capability >= 0)
  1013. ch->transfercapability = pvt->cd.transfer_capability;
  1014. if (state != AST_STATE_DOWN) {
  1015. if (ast_pbx_start(ch)) {
  1016. ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
  1017. ast_hangup(ch);
  1018. ch = NULL;
  1019. }
  1020. }
  1021. } else {
  1022. ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
  1023. }
  1024. return ch;
  1025. }
  1026. static struct oh323_pvt *oh323_alloc(int callid)
  1027. {
  1028. struct oh323_pvt *pvt;
  1029. pvt = ast_calloc(1, sizeof(*pvt));
  1030. if (!pvt) {
  1031. ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
  1032. return NULL;
  1033. }
  1034. pvt->cd.redirect_reason = -1;
  1035. pvt->cd.transfer_capability = -1;
  1036. /* Ensure the call token is allocated for outgoing call */
  1037. if (!callid) {
  1038. if ((pvt->cd).call_token == NULL) {
  1039. (pvt->cd).call_token = ast_calloc(1, 128);
  1040. }
  1041. if (!pvt->cd.call_token) {
  1042. ast_log(LOG_ERROR, "Not enough memory to alocate call token\n");
  1043. ast_rtp_instance_destroy(pvt->rtp);
  1044. ast_free(pvt);
  1045. return NULL;
  1046. }
  1047. memset((char *)(pvt->cd).call_token, 0, 128);
  1048. pvt->cd.call_reference = callid;
  1049. }
  1050. memcpy(&pvt->options, &global_options, sizeof(pvt->options));
  1051. pvt->jointcapability = pvt->options.capability;
  1052. if (pvt->options.dtmfmode & (H323_DTMF_RFC2833 | H323_DTMF_CISCO)) {
  1053. pvt->nonCodecCapability |= AST_RTP_DTMF;
  1054. } else {
  1055. pvt->nonCodecCapability &= ~AST_RTP_DTMF;
  1056. }
  1057. ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
  1058. pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->update_rtp_info = pvt->DTMFsched = -1;
  1059. ast_mutex_init(&pvt->lock);
  1060. /* Add to interface list */
  1061. ast_mutex_lock(&iflock);
  1062. pvt->next = iflist;
  1063. iflist = pvt;
  1064. ast_mutex_unlock(&iflock);
  1065. return pvt;
  1066. }
  1067. static struct oh323_pvt *find_call_locked(int call_reference, const char *token)
  1068. {
  1069. struct oh323_pvt *pvt;
  1070. ast_mutex_lock(&iflock);
  1071. pvt = iflist;
  1072. while(pvt) {
  1073. if (!pvt->needdestroy && ((signed int)pvt->cd.call_reference == call_reference)) {
  1074. /* Found the call */
  1075. if ((token != NULL) && (pvt->cd.call_token != NULL) && (!strcmp(pvt->cd.call_token, token))) {
  1076. ast_mutex_lock(&pvt->lock);
  1077. ast_mutex_unlock(&iflock);
  1078. return pvt;
  1079. } else if (token == NULL) {
  1080. ast_log(LOG_WARNING, "Call Token is NULL\n");
  1081. ast_mutex_lock(&pvt->lock);
  1082. ast_mutex_unlock(&iflock);
  1083. return pvt;
  1084. }
  1085. }
  1086. pvt = pvt->next;
  1087. }
  1088. ast_mutex_unlock(&iflock);
  1089. return NULL;
  1090. }
  1091. static int update_state(struct oh323_pvt *pvt, int state, int signal)
  1092. {
  1093. if (!pvt)
  1094. return 0;
  1095. if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
  1096. if (state >= 0)
  1097. ast_setstate(pvt->owner, state);
  1098. if (signal >= 0)
  1099. ast_queue_control(pvt->owner, signal);
  1100. ast_channel_unlock(pvt->owner);
  1101. return 1;
  1102. }
  1103. else {
  1104. if (state >= 0)
  1105. pvt->newstate = state;
  1106. if (signal >= 0)
  1107. pvt->newcontrol = signal;
  1108. return 0;
  1109. }
  1110. }
  1111. static struct oh323_alias *build_alias(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
  1112. {
  1113. struct oh323_alias *alias;
  1114. int found = 0;
  1115. alias = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&aliasl, name, name, 0, 0, strcasecmp);
  1116. if (alias)
  1117. found++;
  1118. else {
  1119. if (!(alias = ast_calloc(1, sizeof(*alias))))
  1120. return NULL;
  1121. ASTOBJ_INIT(alias);
  1122. }
  1123. if (!found && name)
  1124. ast_copy_string(alias->name, name, sizeof(alias->name));
  1125. for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
  1126. if (!strcasecmp(v->name, "e164")) {
  1127. ast_copy_string(alias->e164, v->value, sizeof(alias->e164));
  1128. } else if (!strcasecmp(v->name, "prefix")) {
  1129. ast_copy_string(alias->prefix, v->value, sizeof(alias->prefix));
  1130. } else if (!strcasecmp(v->name, "context")) {
  1131. ast_copy_string(alias->context, v->value, sizeof(alias->context));
  1132. } else if (!strcasecmp(v->name, "secret")) {
  1133. ast_copy_string(alias->secret, v->value, sizeof(alias->secret));
  1134. } else {
  1135. if (strcasecmp(v->value, "h323")) {
  1136. ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->name);
  1137. }
  1138. }
  1139. }
  1140. ASTOBJ_UNMARK(alias);
  1141. return alias;
  1142. }
  1143. static struct oh323_alias *realtime_alias(const char *alias)
  1144. {
  1145. struct ast_variable *var, *tmp;
  1146. struct oh323_alias *a;
  1147. var = ast_load_realtime("h323", "name", alias, SENTINEL);
  1148. if (!var)
  1149. return NULL;
  1150. for (tmp = var; tmp; tmp = tmp->next) {
  1151. if (!strcasecmp(tmp->name, "type") &&
  1152. !(!strcasecmp(tmp->value, "alias") || !strcasecmp(tmp->value, "h323"))) {
  1153. ast_variables_destroy(var);
  1154. return NULL;
  1155. }
  1156. }
  1157. a = build_alias(alias, var, NULL, 1);
  1158. ast_variables_destroy(var);
  1159. return a;
  1160. }
  1161. static int h323_parse_allow_disallow(struct ast_codec_pref *pref, h323_format *formats, const char *list, int allowing)
  1162. {
  1163. int res;
  1164. struct ast_format_cap *cap = ast_format_cap_alloc_nolock();
  1165. if (!cap) {
  1166. return 1;
  1167. }
  1168. ast_format_cap_from_old_bitfield(cap, *formats);
  1169. res = ast_parse_allow_disallow(pref, cap, list, allowing);
  1170. *formats = ast_format_cap_to_old_bitfield(cap);
  1171. cap = ast_format_cap_destroy(cap);
  1172. return res;
  1173. }
  1174. static int update_common_options(struct ast_variable *v, struct call_options *options)
  1175. {
  1176. int tmp = 0;
  1177. char *val, *opt;
  1178. if (!strcasecmp(v->name, "allow")) {
  1179. h323_parse_allow_disallow(&options->prefs, &options->capability, v->value, 1);
  1180. } else if (!strcasecmp(v->name, "autoframing")) {
  1181. options->autoframing = ast_true(v->value);
  1182. } else if (!strcasecmp(v->name, "disallow")) {
  1183. h323_parse_allow_disallow(&options->prefs, &options->capability, v->value, 0);
  1184. } else if (!strcasecmp(v->name, "dtmfmode")) {
  1185. val = ast_strdupa(v->value);
  1186. if ((opt = strchr(val, ':')) != (char *)NULL) {
  1187. *opt++ = '\0';
  1188. tmp = atoi(opt);
  1189. }
  1190. if (!strcasecmp(v->value, "inband")) {
  1191. options->dtmfmode |= H323_DTMF_INBAND;
  1192. } else if (!strcasecmp(val, "rfc2833")) {
  1193. options->dtmfmode |= H323_DTMF_RFC2833;
  1194. if (!opt) {
  1195. options->dtmfcodec[0] = H323_DTMF_RFC2833_PT;
  1196. } else if ((tmp >= 96) && (tmp < 128)) {
  1197. options->dtmfcodec[0] = tmp;
  1198. } else {
  1199. options->dtmfcodec[0] = H323_DTMF_RFC2833_PT;
  1200. ast_log(LOG_WARNING, "Unknown rfc2833 payload %s specified at line %d, using default %d\n", opt, v->lineno, options->dtmfcodec[0]);
  1201. }
  1202. } else if (!strcasecmp(val, "cisco")) {
  1203. options->dtmfmode |= H323_DTMF_CISCO;
  1204. if (!opt) {
  1205. options->dtmfcodec[1] = H323_DTMF_CISCO_PT;
  1206. } else if ((tmp >= 96) && (tmp < 128)) {
  1207. options->dtmfcodec[1] = tmp;
  1208. } else {
  1209. options->dtmfcodec[1] = H323_DTMF_CISCO_PT;
  1210. ast_log(LOG_WARNING, "Unknown Cisco DTMF payload %s specified at line %d, using default %d\n", opt, v->lineno, options->dtmfcodec[1]);
  1211. }
  1212. } else if (!strcasecmp(v->value, "h245-signal")) {
  1213. options->dtmfmode |= H323_DTMF_SIGNAL;
  1214. } else {
  1215. ast_log(LOG_WARNING, "Unknown dtmf mode '%s' at line %d\n", v->value, v->lineno);
  1216. }
  1217. } else if (!strcasecmp(v->name, "dtmfcodec")) {
  1218. ast_log(LOG_NOTICE, "Option %s at line %d is deprecated. Use dtmfmode=rfc2833[:<payload>] instead.\n", v->name, v->lineno);
  1219. tmp = atoi(v->value);
  1220. if (tmp < 96)
  1221. ast_log(LOG_WARNING, "Invalid %s value %s at line %d\n", v->name, v->value, v->lineno);
  1222. else
  1223. options->dtmfcodec[0] = tmp;
  1224. } else if (!strcasecmp(v->name, "bridge")) {
  1225. options->bridge = ast_true(v->value);
  1226. } else if (!strcasecmp(v->name, "nat")) {
  1227. options->nat = ast_true(v->value);
  1228. } else if (!strcasecmp(v->name, "fastStart")) {
  1229. options->fastStart = ast_true(v->value);
  1230. } else if (!strcasecmp(v->name, "h245Tunneling")) {
  1231. options->h245Tunneling = ast_true(v->value);
  1232. } else if (!strcasecmp(v->name, "silenceSuppression")) {
  1233. options->silenceSuppression = ast_true(v->value);
  1234. } else if (!strcasecmp(v->name, "progress_setup")) {
  1235. tmp = atoi(v->value);
  1236. if ((tmp != 0) && (tmp != 1) && (tmp != 3) && (tmp != 8)) {
  1237. ast_log(LOG_WARNING, "Invalid value %s for %s at line %d, assuming 0\n", v->value, v->name, v->lineno);
  1238. tmp = 0;
  1239. }
  1240. options->progress_setup = tmp;
  1241. } else if (!strcasecmp(v->name, "progress_alert")) {
  1242. tmp = atoi(v->value);
  1243. if ((tmp != 0) && (tmp != 1) && (tmp != 8)) {
  1244. ast_log(LOG_WARNING, "Invalid value %s for %s at line %d, assuming 0\n", v->value, v->name, v->lineno);
  1245. tmp = 0;
  1246. }
  1247. options->progress_alert = tmp;
  1248. } else if (!strcasecmp(v->name, "progress_audio")) {
  1249. options->progress_audio = ast_true(v->value);
  1250. } else if (!strcasecmp(v->name, "callerid")) {
  1251. ast_callerid_split(v->value, options->cid_name, sizeof(options->cid_name), options->cid_num, sizeof(options->cid_num));
  1252. } else if (!strcasecmp(v->name, "fullname")) {
  1253. ast_copy_string(options->cid_name, v->value, sizeof(options->cid_name));
  1254. } else if (!strcasecmp(v->name, "cid_number")) {
  1255. ast_copy_string(options->cid_num, v->value, sizeof(options->cid_num));
  1256. } else if (!strcasecmp(v->name, "tunneling")) {
  1257. if (!strcasecmp(v->value, "none"))
  1258. options->tunnelOptions = 0;
  1259. else if (!strcasecmp(v->value, "cisco"))
  1260. options->tunnelOptions |= H323_TUNNEL_CISCO;
  1261. else if (!strcasecmp(v->value, "qsig"))
  1262. options->tunnelOptions |= H323_TUNNEL_QSIG;
  1263. else
  1264. ast_log(LOG_WARNING, "Invalid value %s for %s at line %d\n", v->value, v->name, v->lineno);
  1265. } else if (!strcasecmp(v->name, "hold")) {
  1266. if (!strcasecmp(v->value, "none"))
  1267. options->holdHandling = ~0;
  1268. else if (!strcasecmp(v->value, "notify"))
  1269. options->holdHandling |= H323_HOLD_NOTIFY;
  1270. else if (!strcasecmp(v->value, "q931only"))
  1271. options->holdHandling |= H323_HOLD_NOTIFY | H323_HOLD_Q931ONLY;
  1272. else if (!strcasecmp(v->value, "h450"))
  1273. options->holdHandling |= H323_HOLD_H450;
  1274. else
  1275. ast_log(LOG_WARNING, "Invalid value %s for %s at line %d\n", v->value, v->name, v->lineno);
  1276. } else
  1277. return 1;
  1278. return 0;
  1279. }
  1280. static struct oh323_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
  1281. {
  1282. struct oh323_user *user;
  1283. struct ast_ha *oldha;
  1284. int found = 0;
  1285. int format;
  1286. user = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&userl, name, name, 0, 0, strcmp);
  1287. if (user)
  1288. found++;
  1289. else {
  1290. if (!(user = ast_calloc(1, sizeof(*user))))
  1291. return NULL;
  1292. ASTOBJ_INIT(user);
  1293. }
  1294. oldha = user->ha;
  1295. user->ha = (struct ast_ha *)NULL;
  1296. memcpy(&user->options, &global_options, sizeof(user->options));
  1297. user->options.dtmfmode = 0;
  1298. user->options.holdHandling = 0;
  1299. /* Set default context */
  1300. ast_copy_string(user->context, default_context, sizeof(user->context));
  1301. if (user && !found)
  1302. ast_copy_string(user->name, name, sizeof(user->name));
  1303. #if 0 /* XXX Port channel variables functionality from chan_sip XXX */
  1304. if (user->chanvars) {
  1305. ast_variables_destroy(user->chanvars);
  1306. user->chanvars = NULL;
  1307. }
  1308. #endif
  1309. for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
  1310. if (!update_common_options(v, &user->options))
  1311. continue;
  1312. if (!strcasecmp(v->name, "context")) {
  1313. ast_copy_string(user->context, v->value, sizeof(user->context));
  1314. } else if (!strcasecmp(v->name, "secret")) {
  1315. ast_copy_string(user->secret, v->value, sizeof(user->secret));
  1316. } else if (!strcasecmp(v->name, "accountcode")) {
  1317. ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
  1318. } else if (!strcasecmp(v->name, "host")) {
  1319. if (!strcasecmp(v->value, "dynamic")) {
  1320. ast_log(LOG_ERROR, "A dynamic host on a type=user does not make any sense\n");
  1321. ASTOBJ_UNREF(user, oh323_destroy_user);
  1322. return NULL;
  1323. } else {
  1324. struct ast_sockaddr tmp;
  1325. tmp.ss.ss_family = AF_INET;
  1326. if (ast_get_ip(&tmp, v->value)) {
  1327. ASTOBJ_UNREF(user, oh323_destroy_user);
  1328. return NULL;
  1329. }
  1330. ast_sockaddr_to_sin(&tmp, &user->addr);
  1331. }
  1332. /* Let us know we need to use ip authentication */
  1333. user->host = 1;
  1334. } else if (!strcasecmp(v->name, "amaflags")) {
  1335. format = ast_cdr_amaflags2int(v->value);
  1336. if (format < 0) {
  1337. ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
  1338. } else {
  1339. user->amaflags = format;
  1340. }
  1341. } else if (!strcasecmp(v->name, "permit") ||
  1342. !strcasecmp(v->name, "deny")) {
  1343. int ha_error = 0;
  1344. user->ha = ast_append_ha(v->name, v->value, user->ha, &ha_error);
  1345. if (ha_error)
  1346. ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
  1347. }
  1348. }
  1349. if (!user->options.dtmfmode)
  1350. user->options.dtmfmode = global_options.dtmfmode;
  1351. if (user->options.holdHandling == ~0)
  1352. user->options.holdHandling = 0;
  1353. else if (!user->options.holdHandling)
  1354. user->options.holdHandling = global_options.holdHandling;
  1355. ASTOBJ_UNMARK(user);
  1356. ast_free_ha(oldha);
  1357. return user;
  1358. }
  1359. static struct oh323_user *realtime_user(const call_details_t *cd)
  1360. {
  1361. struct ast_variable *var, *tmp;
  1362. struct oh323_user *user;
  1363. const char *username;
  1364. if (userbyalias)
  1365. var = ast_load_realtime("h323", "name", username = cd->call_source_aliases, SENTINEL);
  1366. else {
  1367. username = (char *)NULL;
  1368. var = ast_load_realtime("h323", "host", cd->sourceIp, SENTINEL);
  1369. }
  1370. if (!var)
  1371. return NULL;
  1372. for (tmp = var; tmp; tmp = tmp->next) {
  1373. if (!strcasecmp(tmp->name, "type") &&
  1374. !(!strcasecmp(tmp->value, "user") || !strcasecmp(tmp->value, "friend"))) {
  1375. ast_variables_destroy(var);
  1376. return NULL;
  1377. } else if (!username && !strcasecmp(tmp->name, "name"))
  1378. username = tmp->value;
  1379. }
  1380. if (!username) {
  1381. ast_log(LOG_WARNING, "Cannot determine user name for IP address %s\n", cd->sourceIp);
  1382. ast_variables_destroy(var);
  1383. return NULL;
  1384. }
  1385. user = build_user(username, var, NULL, 1);
  1386. ast_variables_destroy(var);
  1387. return user;
  1388. }
  1389. static struct oh323_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
  1390. {
  1391. struct oh323_peer *peer;
  1392. struct ast_ha *oldha;
  1393. int found = 0;
  1394. peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
  1395. if (peer)
  1396. found++;
  1397. else {
  1398. if (!(peer = ast_calloc(1, sizeof(*peer))))
  1399. return NULL;
  1400. ASTOBJ_INIT(peer);
  1401. }
  1402. oldha = peer->ha;
  1403. peer->ha = NULL;
  1404. memcpy(&peer->options, &global_options, sizeof(peer->options));
  1405. peer->options.dtmfmode = 0;
  1406. peer->options.holdHandling = 0;
  1407. peer->addr.sin_port = htons(h323_signalling_port);
  1408. peer->addr.sin_family = AF_INET;
  1409. if (!found && name)
  1410. ast_copy_string(peer->name, name, sizeof(peer->name));
  1411. #if 0 /* XXX Port channel variables functionality from chan_sip XXX */
  1412. if (peer->chanvars) {
  1413. ast_variables_destroy(peer->chanvars);
  1414. peer->chanvars = NULL;
  1415. }
  1416. #endif
  1417. /* Default settings for mailbox */
  1418. peer->mailbox[0] = '\0';
  1419. for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
  1420. if (!update_common_options(v, &peer->options))
  1421. continue;
  1422. if (!strcasecmp(v->name, "host")) {
  1423. if (!strcasecmp(v->value, "dynamic")) {
  1424. ast_log(LOG_ERROR, "Dynamic host configuration not implemented.\n");
  1425. ASTOBJ_UNREF(peer, oh323_destroy_peer);
  1426. return NULL;
  1427. }
  1428. {
  1429. struct ast_sockaddr tmp;
  1430. tmp.ss.ss_family = AF_INET;
  1431. if (ast_get_ip(&tmp, v->value)) {
  1432. ast_log(LOG_ERROR, "Could not determine IP for %s\n", v->value);
  1433. ASTOBJ_UNREF(peer, oh323_destroy_peer);
  1434. return NULL;
  1435. }
  1436. ast_sockaddr_to_sin(&tmp, &peer->addr);
  1437. }
  1438. } else if (!strcasecmp(v->name, "port")) {
  1439. peer->addr.sin_port = htons(atoi(v->value));
  1440. } else if (!strcasecmp(v->name, "permit") ||
  1441. !strcasecmp(v->name, "deny")) {
  1442. int ha_error = 0;
  1443. peer->ha = ast_append_ha(v->name, v->value, peer->ha, &ha_error);
  1444. if (ha_error)
  1445. ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
  1446. } else if (!strcasecmp(v->name, "mailbox")) {
  1447. ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
  1448. } else if (!strcasecmp(v->name, "hasvoicemail")) {
  1449. if (ast_true(v->value) && ast_strlen_zero(peer->mailbox)) {
  1450. ast_copy_string(peer->mailbox, name, sizeof(peer->mailbox));
  1451. }
  1452. }
  1453. }
  1454. if (!peer->options.dtmfmode)
  1455. peer->options.dtmfmode = global_options.dtmfmode;
  1456. if (peer->options.holdHandling == ~0)
  1457. peer->options.holdHandling = 0;
  1458. else if (!peer->options.holdHandling)
  1459. peer->options.holdHandling = global_options.holdHandling;
  1460. ASTOBJ_UNMARK(peer);
  1461. ast_free_ha(oldha);
  1462. return peer;
  1463. }
  1464. static struct oh323_peer *realtime_peer(const char *peername, struct sockaddr_in *sin)
  1465. {
  1466. struct oh323_peer *peer;
  1467. struct ast_variable *var;
  1468. struct ast_variable *tmp;
  1469. const char *addr = NULL;
  1470. /* First check on peer name */
  1471. if (peername)
  1472. var = ast_load_realtime("h323", "name", peername, SENTINEL);
  1473. else if (sin) /* Then check on IP address for dynamic peers */
  1474. var = ast_load_realtime("h323", "host", addr = ast_inet_ntoa(sin->sin_addr), SENTINEL);
  1475. else
  1476. return NULL;
  1477. if (!var)
  1478. return NULL;
  1479. for (tmp = var; tmp; tmp = tmp->next) {
  1480. /* If this is type=user, then skip this object. */
  1481. if (!strcasecmp(tmp->name, "type") &&
  1482. !(!strcasecmp(tmp->value, "peer") || !strcasecmp(tmp->value, "friend"))) {
  1483. ast_variables_destroy(var);
  1484. return NULL;
  1485. } else if (!peername && !strcasecmp(tmp->name, "name")) {
  1486. peername = tmp->value;
  1487. }
  1488. }
  1489. if (!peername) { /* Did not find peer in realtime */
  1490. ast_log(LOG_WARNING, "Cannot determine peer name for IP address %s\n", addr);
  1491. ast_variables_destroy(var);
  1492. return NULL;
  1493. }
  1494. /* Peer found in realtime, now build it in memory */
  1495. peer = build_peer(peername, var, NULL, 1);
  1496. ast_variables_destroy(var);
  1497. return peer;
  1498. }
  1499. static int oh323_addrcmp_str(struct in_addr inaddr, char *addr)
  1500. {
  1501. return strcmp(ast_inet_ntoa(inaddr), addr);
  1502. }
  1503. static struct oh323_user *find_user(const call_details_t *cd, int realtime)
  1504. {
  1505. struct oh323_user *u;
  1506. if (userbyalias)
  1507. u = ASTOBJ_CONTAINER_FIND(&userl, cd->call_source_aliases);
  1508. else
  1509. u = ASTOBJ_CONTAINER_FIND_FULL(&userl, cd->sourceIp, addr.sin_addr, 0, 0, oh323_addrcmp_str);
  1510. if (!u && realtime)
  1511. u = realtime_user(cd);
  1512. if (!u && h323debug)
  1513. ast_debug(1, "Could not find user by name %s or address %s\n", cd->call_source_aliases, cd->sourceIp);
  1514. return u;
  1515. }
  1516. static int oh323_addrcmp(struct sockaddr_in addr, struct sockaddr_in *sin)
  1517. {
  1518. int res;
  1519. if (!sin)
  1520. res = -1;
  1521. else
  1522. res = inaddrcmp(&addr , sin);
  1523. return res;
  1524. }
  1525. static struct oh323_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime)
  1526. {
  1527. struct oh323_peer *p;
  1528. if (peer)
  1529. p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
  1530. else
  1531. p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, addr, 0, 0, oh323_addrcmp);
  1532. if (!p && realtime)
  1533. p = realtime_peer(peer, sin);
  1534. if (!p && h323debug)
  1535. ast_debug(1, "Could not find peer by name %s or address %s\n", (peer ? peer : "<NONE>"), (sin ? ast_inet_ntoa(sin->sin_addr) : "<NONE>"));
  1536. return p;
  1537. }
  1538. static int create_addr(struct oh323_pvt *pvt, char *opeer)
  1539. {
  1540. struct hostent *hp;
  1541. struct ast_hostent ahp;
  1542. struct oh323_peer *p;
  1543. int portno;
  1544. int found = 0;
  1545. char *port;
  1546. char *hostn;
  1547. char peer[256] = "";
  1548. ast_copy_string(peer, opeer, sizeof(peer));
  1549. port = strchr(peer, ':');
  1550. if (port) {
  1551. *port = '\0';
  1552. port++;
  1553. }
  1554. pvt->sa.sin_family = AF_INET;
  1555. p = find_peer(peer, NULL, 1);
  1556. if (p) {
  1557. found++;
  1558. memcpy(&pvt->options, &p->options, sizeof(pvt->options));
  1559. pvt->jointcapability = pvt->options.capability;
  1560. if (pvt->options.dtmfmode) {
  1561. if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
  1562. pvt->nonCodecCapability |= AST_RTP_DTMF;
  1563. } else {
  1564. pvt->nonCodecCapability &= ~AST_RTP_DTMF;
  1565. }
  1566. }
  1567. if (p->addr.sin_addr.s_addr) {
  1568. pvt->sa.sin_addr = p->addr.sin_addr;
  1569. pvt->sa.sin_port = p->addr.sin_port;
  1570. }
  1571. ASTOBJ_UNREF(p, oh323_destroy_peer);
  1572. }
  1573. if (!p && !found) {
  1574. hostn = peer;
  1575. if (port) {
  1576. portno = atoi(port);
  1577. } else {
  1578. portno = h323_signalling_port;
  1579. }
  1580. hp = ast_gethostbyname(hostn, &ahp);
  1581. if (hp) {
  1582. memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
  1583. pvt->sa.sin_port = htons(portno);
  1584. /* Look peer by address */
  1585. p = find_peer(NULL, &pvt->sa, 1);
  1586. memcpy(&pvt->options, (p ? &p->options : &global_options), sizeof(pvt->options));
  1587. pvt->jointcapability = pvt->options.capability;
  1588. if (p) {
  1589. ASTOBJ_UNREF(p, oh323_destroy_peer);
  1590. }
  1591. if (pvt->options.dtmfmode) {
  1592. if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
  1593. pvt->nonCodecCapability |= AST_RTP_DTMF;
  1594. } else {
  1595. pvt->nonCodecCapability &= ~AST_RTP_DTMF;
  1596. }
  1597. }
  1598. return 0;
  1599. } else {
  1600. ast_log(LOG_WARNING, "No such host: %s\n", peer);
  1601. return -1;
  1602. }
  1603. } else if (!found) {
  1604. return -1;
  1605. } else {
  1606. return 0;
  1607. }
  1608. }
  1609. static struct ast_channel *oh323_request(const char *type, struct ast_format_cap *cap, const struct ast_channel *requestor, void *data, int *cause)
  1610. {
  1611. struct oh323_pvt *pvt;
  1612. struct ast_channel *tmpc = NULL;
  1613. char *dest = (char *)data;
  1614. char *ext, *host;
  1615. char *h323id = NULL;
  1616. char tmp[256], tmp1[256];
  1617. if (h323debug)
  1618. ast_debug(1, "type=%s, format=%s, data=%s.\n", type, ast_getformatname_multiple(tmp, sizeof(tmp), cap), (char *)data);
  1619. pvt = oh323_alloc(0);
  1620. if (!pvt) {
  1621. ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
  1622. return NULL;
  1623. }
  1624. if (!(ast_format_cap_has_type(cap, AST_FORMAT_TYPE_AUDIO))) {
  1625. ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%s'\n", ast_getformatname_multiple(tmp, sizeof(tmp), cap));
  1626. oh323_destroy(pvt);
  1627. if (cause)
  1628. *cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
  1629. return NULL;
  1630. }
  1631. ast_copy_string(tmp, dest, sizeof(tmp));
  1632. host = strchr(tmp, '@');
  1633. if (host) {
  1634. *host = '\0';
  1635. host++;
  1636. ext = tmp;
  1637. } else {
  1638. ext = strrchr(tmp, '/');
  1639. if (ext)
  1640. *ext++ = '\0';
  1641. host = tmp;
  1642. }
  1643. strtok_r(host, "/", &(h323id));
  1644. if (!ast_strlen_zero(h323id)) {
  1645. h323_set_id(h323id);
  1646. }
  1647. if (ext) {
  1648. ast_copy_string(pvt->exten, ext, sizeof(pvt->exten));
  1649. }
  1650. if (h323debug)
  1651. ast_debug(1, "Extension: %s Host: %s\n", pvt->exten, host);
  1652. if (gatekeeper_disable) {
  1653. if (create_addr(pvt, host)) {
  1654. oh323_destroy(pvt);
  1655. if (cause)
  1656. *cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
  1657. return NULL;
  1658. }
  1659. }
  1660. else {
  1661. memcpy(&pvt->options, &global_options, sizeof(pvt->options));
  1662. pvt->jointcapability = pvt->options.capability;
  1663. if (pvt->options.dtmfmode) {
  1664. if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
  1665. pvt->nonCodecCapability |= AST_RTP_DTMF;
  1666. } else {
  1667. pvt->nonCodecCapability &= ~AST_RTP_DTMF;
  1668. }
  1669. }
  1670. }
  1671. ast_mutex_lock(&caplock);
  1672. /* Generate unique channel identifier */
  1673. snprintf(tmp1, sizeof(tmp1)-1, "%s-%u", host, ++unique);
  1674. tmp1[sizeof(tmp1)-1] = '\0';
  1675. ast_mutex_unlock(&caplock);
  1676. ast_mutex_lock(&pvt->lock);
  1677. tmpc = __oh323_new(pvt, AST_STATE_DOWN, tmp1, requestor ? requestor->linkedid : NULL);
  1678. ast_mutex_unlock(&pvt->lock);
  1679. if (!tmpc) {
  1680. oh323_destroy(pvt);
  1681. if (cause)
  1682. *cause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
  1683. }
  1684. ast_update_use_count();
  1685. restart_monitor();
  1686. return tmpc;
  1687. }
  1688. /*! \brief Find a call by alias */
  1689. static struct oh323_alias *find_alias(const char *source_aliases, int realtime)
  1690. {
  1691. struct oh323_alias *a;
  1692. a = ASTOBJ_CONTAINER_FIND(&aliasl, source_aliases);
  1693. if (!a && realtime)
  1694. a = realtime_alias(source_aliases);
  1695. return a;
  1696. }
  1697. /*! \brief
  1698. * Callback for sending digits from H.323 up to asterisk
  1699. *
  1700. */
  1701. static int receive_digit(unsigned call_reference, char digit, const char *token, int duration)
  1702. {
  1703. struct oh323_pvt *pvt;
  1704. int res;
  1705. pvt = find_call_locked(call_reference, token);
  1706. if (!pvt) {
  1707. ast_log(LOG_ERROR, "Received digit '%c' (%u ms) for call %s without private structure\n", digit, duration, token);
  1708. return -1;
  1709. }
  1710. if (h323debug)
  1711. ast_log(LOG_DTMF, "Received %s digit '%c' (%u ms) for call %s\n", (digit == ' ' ? "update for" : "new"), (digit == ' ' ? pvt->curDTMF : digit), duration, token);
  1712. if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
  1713. if (digit == '!')
  1714. res = ast_queue_control(pvt->owner, AST_CONTROL_FLASH);
  1715. else {
  1716. struct ast_frame f = {
  1717. .frametype = AST_FRAME_DTMF_END,
  1718. .subclass.integer = digit,
  1719. .samples = duration * 8,
  1720. .len = duration,
  1721. .src = "SEND_DIGIT",
  1722. };
  1723. if (digit == ' ') { /* signalUpdate message */
  1724. f.subclass.integer = pvt->curDTMF;
  1725. AST_SCHED_DEL(sched, pvt->DTMFsched);
  1726. } else { /* Regular input or signal message */
  1727. if (pvt->DTMFsched >= 0) {
  1728. /* We still don't send DTMF END from previous event, send it now */
  1729. AST_SCHED_DEL(sched, pvt->DTMFsched);
  1730. f.subclass.integer = pvt->curDTMF;
  1731. f.samples = f.len = 0;
  1732. ast_queue_frame(pvt->owner, &f);
  1733. /* Restore values */
  1734. f.subclass.integer = digit;
  1735. f.samples = duration * 8;
  1736. f.len = duration;
  1737. }
  1738. if (duration) { /* This is a signal, signalUpdate follows */
  1739. f.frametype = AST_FRAME_DTMF_BEGIN;
  1740. pvt->DTMFsched = ast_sched_add(sched, duration, oh323_simulate_dtmf_end, pvt);
  1741. if (h323debug)
  1742. ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", duration, pvt->DTMFsched);
  1743. }
  1744. pvt->curDTMF = digit;
  1745. }
  1746. res = ast_queue_frame(pvt->owner, &f);
  1747. }
  1748. ast_channel_unlock(pvt->owner);
  1749. } else {
  1750. if (digit == '!')
  1751. pvt->newcontrol = AST_CONTROL_FLASH;
  1752. else {
  1753. pvt->newduration = duration;
  1754. pvt->newdigit = digit;
  1755. }
  1756. res = 0;
  1757. }
  1758. ast_mutex_unlock(&pvt->lock);
  1759. return res;
  1760. }
  1761. /*! \brief
  1762. * Callback function used to inform the H.323 stack of the local rtp ip/port details
  1763. *
  1764. * \return Returns the local RTP information
  1765. */
  1766. static struct rtp_info *external_rtp_create(unsigned call_reference, const char * token)
  1767. {
  1768. struct oh323_pvt *pvt;
  1769. struct sockaddr_in us;
  1770. struct rtp_info *info;
  1771. info = ast_calloc(1, sizeof(*info));
  1772. if (!info) {
  1773. ast_log(LOG_ERROR, "Unable to allocated info structure, this is very bad\n");
  1774. return NULL;
  1775. }
  1776. pvt = find_call_locked(call_reference, token);
  1777. if (!pvt) {
  1778. ast_free(info);
  1779. ast_log(LOG_ERROR, "Unable to find call %s(%d)\n", token, call_reference);
  1780. return NULL;
  1781. }
  1782. if (!pvt->rtp)
  1783. __oh323_rtp_create(pvt);
  1784. if (!pvt->rtp) {
  1785. ast_mutex_unlock(&pvt->lock);
  1786. ast_free(info);
  1787. ast_log(LOG_ERROR, "No RTP stream is available for call %s (%d)", token, call_reference);
  1788. return NULL;
  1789. }
  1790. /* figure out our local RTP port and tell the H.323 stack about it */
  1791. {
  1792. struct ast_sockaddr tmp;
  1793. ast_rtp_instance_get_local_address(pvt->rtp, &tmp);
  1794. ast_sockaddr_to_sin(&tmp, &us);
  1795. }
  1796. ast_mutex_unlock(&pvt->lock);
  1797. ast_copy_string(info->addr, ast_inet_ntoa(us.sin_addr), sizeof(info->addr));
  1798. info->port = ntohs(us.sin_port);
  1799. if (h323debug)
  1800. ast_debug(1, "Sending RTP 'US' %s:%d\n", info->addr, info->port);
  1801. return info;
  1802. }
  1803. /*! \brief
  1804. * Call-back function passing remote ip/port information from H.323 to asterisk
  1805. *
  1806. * Returns nothing
  1807. */
  1808. static void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort, const char *token, int pt)
  1809. {
  1810. struct oh323_pvt *pvt;
  1811. struct sockaddr_in them;
  1812. int nativeformats_changed;
  1813. enum { NEED_NONE, NEED_HOLD, NEED_UNHOLD } rtp_change = NEED_NONE;
  1814. if (h323debug)
  1815. ast_debug(1, "Setting up RTP connection for %s\n", token);
  1816. /* Find the call or allocate a private structure if call not found */
  1817. pvt = find_call_locked(call_reference, token);
  1818. if (!pvt) {
  1819. ast_log(LOG_ERROR, "Something is wrong: rtp\n");
  1820. return;
  1821. }
  1822. if (pvt->alreadygone) {
  1823. ast_mutex_unlock(&pvt->lock);
  1824. return;
  1825. }
  1826. if (!pvt->rtp)
  1827. __oh323_rtp_create(pvt);
  1828. if ((pt == 2) && (pvt->jointcapability & AST_FORMAT_G726_AAL2)) {
  1829. ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(pvt->rtp), pvt->rtp, pt, "audio", "G726-32", AST_RTP_OPT_G726_NONSTANDARD);
  1830. }
  1831. them.sin_family = AF_INET;
  1832. /* only works for IPv4 */
  1833. them.sin_addr.s_addr = inet_addr(remoteIp);
  1834. them.sin_port = htons(remotePort);
  1835. if (them.sin_addr.s_addr) {
  1836. {
  1837. struct ast_sockaddr tmp;
  1838. ast_sockaddr_from_sin(&tmp, &them);
  1839. ast_rtp_instance_set_remote_address(pvt->rtp, &tmp);
  1840. }
  1841. if (pvt->recvonly) {
  1842. pvt->recvonly = 0;
  1843. rtp_change = NEED_UNHOLD;
  1844. }
  1845. } else {
  1846. ast_rtp_instance_stop(pvt->rtp);
  1847. if (!pvt->recvonly) {
  1848. pvt->recvonly = 1;
  1849. rtp_change = NEED_HOLD;
  1850. }
  1851. }
  1852. /* Change native format to reflect information taken from OLC/OLCAck */
  1853. nativeformats_changed = 0;
  1854. if (pt != 128 && pvt->rtp) { /* Payload type is invalid, so try to use previously decided */
  1855. struct ast_rtp_payload_type rtptype = ast_rtp_codecs_payload_lookup(ast_rtp_instance_get_codecs(pvt->rtp), pt);
  1856. if (rtptype.asterisk_format) {
  1857. if (pvt->nativeformats != ast_format_to_old_bitfield(&rtptype.format)) {
  1858. pvt->nativeformats = ast_format_to_old_bitfield(&rtptype.format);
  1859. nativeformats_changed = 1;
  1860. }
  1861. }
  1862. } else if (h323debug)
  1863. ast_log(LOG_NOTICE, "Payload type is unknown, formats isn't changed\n");
  1864. /* Don't try to lock the channel if nothing changed */
  1865. if (nativeformats_changed || pvt->options.progress_audio || (rtp_change != NEED_NONE)) {
  1866. if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
  1867. struct ast_format_cap *pvt_native = ast_format_cap_alloc_nolock();
  1868. ast_format_cap_from_old_bitfield(pvt_native, pvt->nativeformats);
  1869. /* Re-build translation path only if native format(s) has been changed */
  1870. if (!(ast_format_cap_identical(pvt->owner->nativeformats, pvt_native))) {
  1871. if (h323debug) {
  1872. char tmp[256], tmp2[256];
  1873. ast_debug(1, "Native format changed to '%s' from '%s', read format is %s, write format is %s\n", ast_getformatname_multiple(tmp, sizeof(tmp), pvt_native), ast_getformatname_multiple(tmp2, sizeof(tmp2), pvt->owner->nativeformats), ast_getformatname(&pvt->owner->readformat), ast_getformatname(&pvt->owner->writeformat));
  1874. }
  1875. ast_format_cap_copy(pvt->owner->nativeformats, pvt_native);
  1876. ast_set_read_format(pvt->owner, &pvt->owner->readformat);
  1877. ast_set_write_format(pvt->owner, &pvt->owner->writeformat);
  1878. }
  1879. if (pvt->options.progress_audio)
  1880. ast_queue_control(pvt->owner, AST_CONTROL_PROGRESS);
  1881. switch (rtp_change) {
  1882. case NEED_HOLD:
  1883. ast_queue_control(pvt->owner, AST_CONTROL_HOLD);
  1884. break;
  1885. case NEED_UNHOLD:
  1886. ast_queue_control(pvt->owner, AST_CONTROL_UNHOLD);
  1887. break;
  1888. default:
  1889. break;
  1890. }
  1891. ast_channel_unlock(pvt->owner);
  1892. pvt_native = ast_format_cap_destroy(pvt_native);
  1893. }
  1894. else {
  1895. if (pvt->options.progress_audio)
  1896. pvt->newcontrol = AST_CONTROL_PROGRESS;
  1897. else if (rtp_change == NEED_HOLD)
  1898. pvt->newcontrol = AST_CONTROL_HOLD;
  1899. else if (rtp_change == NEED_UNHOLD)
  1900. pvt->newcontrol = AST_CONTROL_UNHOLD;
  1901. if (h323debug)
  1902. ast_debug(1, "RTP connection preparation for %s is pending...\n", token);
  1903. }
  1904. }
  1905. ast_mutex_unlock(&pvt->lock);
  1906. if (h323debug)
  1907. ast_debug(1, "RTP connection prepared for %s\n", token);
  1908. return;
  1909. }
  1910. /*! \brief
  1911. * Call-back function to signal asterisk that the channel has been answered
  1912. * Returns nothing
  1913. */
  1914. static void connection_made(unsigned call_reference, const char *token)
  1915. {
  1916. struct oh323_pvt *pvt;
  1917. if (h323debug)
  1918. ast_debug(1, "Call %s answered\n", token);
  1919. pvt = find_call_locked(call_reference, token);
  1920. if (!pvt) {
  1921. ast_log(LOG_ERROR, "Something is wrong: connection\n");
  1922. return;
  1923. }
  1924. /* Inform asterisk about remote party connected only on outgoing calls */
  1925. if (!pvt->outgoing) {
  1926. ast_mutex_unlock(&pvt->lock);
  1927. return;
  1928. }
  1929. /* Do not send ANSWER message more than once */
  1930. if (!pvt->connection_established) {
  1931. pvt->connection_established = 1;
  1932. update_state(pvt, -1, AST_CONTROL_ANSWER);
  1933. }
  1934. ast_mutex_unlock(&pvt->lock);
  1935. return;
  1936. }
  1937. static int progress(unsigned call_reference, const char *token, int inband)
  1938. {
  1939. struct oh323_pvt *pvt;
  1940. if (h323debug)
  1941. ast_debug(1, "Received ALERT/PROGRESS message for %s tones\n", (inband ? "inband" : "self-generated"));
  1942. pvt = find_call_locked(call_reference, token);
  1943. if (!pvt) {
  1944. ast_log(LOG_ERROR, "Private structure not found in progress.\n");
  1945. return -1;
  1946. }
  1947. if (!pvt->owner) {
  1948. ast_mutex_unlock(&pvt->lock);
  1949. ast_log(LOG_ERROR, "No Asterisk channel associated with private structure.\n");
  1950. return -1;
  1951. }
  1952. update_state(pvt, -1, (inband ? AST_CONTROL_PROGRESS : AST_CONTROL_RINGING));
  1953. ast_mutex_unlock(&pvt->lock);
  1954. return 0;
  1955. }
  1956. /*! \brief
  1957. * Call-back function for incoming calls
  1958. *
  1959. * Returns 1 on success
  1960. */
  1961. static call_options_t *setup_incoming_call(call_details_t *cd)
  1962. {
  1963. struct oh323_pvt *pvt;
  1964. struct oh323_user *user = NULL;
  1965. struct oh323_alias *alias = NULL;
  1966. if (h323debug)
  1967. ast_debug(1, "Setting up incoming call for %s\n", cd->call_token);
  1968. /* allocate the call*/
  1969. pvt = oh323_alloc(cd->call_reference);
  1970. if (!pvt) {
  1971. ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
  1972. cleanup_call_details(cd);
  1973. return NULL;
  1974. }
  1975. /* Populate the call details in the private structure */
  1976. memcpy(&pvt->cd, cd, sizeof(pvt->cd));
  1977. memcpy(&pvt->options, &global_options, sizeof(pvt->options));
  1978. pvt->jointcapability = pvt->options.capability;
  1979. if (h323debug) {
  1980. ast_verb(3, "Setting up Call\n");
  1981. ast_verb(3, " \tCall token: [%s]\n", pvt->cd.call_token);
  1982. ast_verb(3, " \tCalling party name: [%s]\n", pvt->cd.call_source_name);
  1983. ast_verb(3, " \tCalling party number: [%s]\n", pvt->cd.call_source_e164);
  1984. ast_verb(3, " \tCalled party name: [%s]\n", pvt->cd.call_dest_alias);
  1985. ast_verb(3, " \tCalled party number: [%s]\n", pvt->cd.call_dest_e164);
  1986. if (pvt->cd.redirect_reason >= 0)
  1987. ast_verb(3, " \tRedirecting party number: [%s] (reason %d)\n", pvt->cd.redirect_number, pvt->cd.redirect_reason);
  1988. ast_verb(3, " \tCalling party IP: [%s]\n", pvt->cd.sourceIp);
  1989. }
  1990. /* Decide if we are allowing Gatekeeper routed calls*/
  1991. if ((!strcasecmp(cd->sourceIp, gatekeeper)) && (gkroute == -1) && !gatekeeper_disable) {
  1992. if (!ast_strlen_zero(cd->call_dest_e164)) {
  1993. ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
  1994. ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
  1995. } else {
  1996. alias = find_alias(cd->call_dest_alias, 1);
  1997. if (!alias) {
  1998. ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd->call_dest_alias);
  1999. oh323_destroy(pvt);
  2000. return NULL;
  2001. }
  2002. ast_copy_string(pvt->exten, alias->name, sizeof(pvt->exten));
  2003. ast_copy_string(pvt->context, alias->context, sizeof(pvt->context));
  2004. }
  2005. } else {
  2006. /* Either this call is not from the Gatekeeper
  2007. or we are not allowing gk routed calls */
  2008. user = find_user(cd, 1);
  2009. if (!user) {
  2010. if (!acceptAnonymous) {
  2011. ast_log(LOG_NOTICE, "Anonymous call from '%s@%s' rejected\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
  2012. oh323_destroy(pvt);
  2013. return NULL;
  2014. }
  2015. if (ast_strlen_zero(default_context)) {
  2016. ast_log(LOG_ERROR, "Call from '%s@%s' rejected due to no default context\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
  2017. oh323_destroy(pvt);
  2018. return NULL;
  2019. }
  2020. ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
  2021. if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
  2022. ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
  2023. } else {
  2024. ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
  2025. }
  2026. if (h323debug)
  2027. ast_debug(1, "Sending %s@%s to context [%s] extension %s\n", cd->call_source_aliases, cd->sourceIp, pvt->context, pvt->exten);
  2028. } else {
  2029. if (user->host) {
  2030. if (strcasecmp(cd->sourceIp, ast_inet_ntoa(user->addr.sin_addr))) {
  2031. if (ast_strlen_zero(user->context)) {
  2032. if (ast_strlen_zero(default_context)) {
  2033. ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd->sourceIp);
  2034. oh323_destroy(pvt);
  2035. ASTOBJ_UNREF(user, oh323_destroy_user);
  2036. return NULL;
  2037. }
  2038. ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
  2039. } else {
  2040. ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
  2041. }
  2042. pvt->exten[0] = 'i';
  2043. pvt->exten[1] = '\0';
  2044. ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd->sourceIp);
  2045. oh323_destroy(pvt);
  2046. ASTOBJ_UNREF(user, oh323_destroy_user);
  2047. return NULL; /* XXX: Hmmm... Why to setup context if we drop connection immediately??? */
  2048. }
  2049. }
  2050. ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
  2051. memcpy(&pvt->options, &user->options, sizeof(pvt->options));
  2052. pvt->jointcapability = pvt->options.capability;
  2053. if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
  2054. ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
  2055. } else {
  2056. ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
  2057. }
  2058. if (!ast_strlen_zero(user->accountcode)) {
  2059. ast_copy_string(pvt->accountcode, user->accountcode, sizeof(pvt->accountcode));
  2060. }
  2061. if (user->amaflags) {
  2062. pvt->amaflags = user->amaflags;
  2063. }
  2064. ASTOBJ_UNREF(user, oh323_destroy_user);
  2065. }
  2066. }
  2067. return &pvt->options;
  2068. }
  2069. /*! \brief
  2070. * Call-back function to start PBX when OpenH323 ready to serve incoming call
  2071. *
  2072. * Returns 1 on success
  2073. */
  2074. static int answer_call(unsigned call_reference, const char *token)
  2075. {
  2076. struct oh323_pvt *pvt;
  2077. struct ast_channel *c = NULL;
  2078. enum {ext_original, ext_s, ext_i, ext_notexists} try_exten;
  2079. char tmp_exten[sizeof(pvt->exten)];
  2080. if (h323debug)
  2081. ast_debug(1, "Preparing Asterisk to answer for %s\n", token);
  2082. /* Find the call or allocate a private structure if call not found */
  2083. pvt = find_call_locked(call_reference, token);
  2084. if (!pvt) {
  2085. ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
  2086. return 0;
  2087. }
  2088. /* Check if requested extension@context pair exists in the dialplan */
  2089. ast_copy_string(tmp_exten, pvt->exten, sizeof(tmp_exten));
  2090. /* Try to find best extension in specified context */
  2091. if ((tmp_exten[0] != '\0') && (tmp_exten[1] == '\0')) {
  2092. if (tmp_exten[0] == 's')
  2093. try_exten = ext_s;
  2094. else if (tmp_exten[0] == 'i')
  2095. try_exten = ext_i;
  2096. else
  2097. try_exten = ext_original;
  2098. } else
  2099. try_exten = ext_original;
  2100. do {
  2101. if (ast_exists_extension(NULL, pvt->context, tmp_exten, 1, NULL))
  2102. break;
  2103. switch (try_exten) {
  2104. case ext_original:
  2105. tmp_exten[0] = 's';
  2106. tmp_exten[1] = '\0';
  2107. try_exten = ext_s;
  2108. break;
  2109. case ext_s:
  2110. tmp_exten[0] = 'i';
  2111. try_exten = ext_i;
  2112. break;
  2113. case ext_i:
  2114. try_exten = ext_notexists;
  2115. break;
  2116. default:
  2117. break;
  2118. }
  2119. } while (try_exten != ext_notexists);
  2120. /* Drop the call if we don't have <exten>, s and i extensions */
  2121. if (try_exten == ext_notexists) {
  2122. ast_log(LOG_NOTICE, "Dropping call because extensions '%s', 's' and 'i' doesn't exists in context [%s]\n", pvt->exten, pvt->context);
  2123. ast_mutex_unlock(&pvt->lock);
  2124. h323_clear_call(token, AST_CAUSE_UNALLOCATED);
  2125. return 0;
  2126. } else if ((try_exten != ext_original) && (strcmp(pvt->exten, tmp_exten) != 0)) {
  2127. if (h323debug)
  2128. ast_debug(1, "Going to extension %s@%s because %s@%s isn't exists\n", tmp_exten, pvt->context, pvt->exten, pvt->context);
  2129. ast_copy_string(pvt->exten, tmp_exten, sizeof(pvt->exten));
  2130. }
  2131. /* allocate a channel and tell asterisk about it */
  2132. c = __oh323_new(pvt, AST_STATE_RINGING, pvt->cd.call_token, NULL);
  2133. /* And release when done */
  2134. ast_mutex_unlock(&pvt->lock);
  2135. if (!c) {
  2136. ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
  2137. return 0;
  2138. }
  2139. return 1;
  2140. }
  2141. /*! \brief
  2142. * Call-back function to establish an outgoing H.323 call
  2143. *
  2144. * Returns 1 on success
  2145. */
  2146. static int setup_outgoing_call(call_details_t *cd)
  2147. {
  2148. /* Use argument here or free it immediately */
  2149. cleanup_call_details(cd);
  2150. return 1;
  2151. }
  2152. /*! \brief
  2153. * Call-back function to signal asterisk that the channel is ringing
  2154. * Returns nothing
  2155. */
  2156. static void chan_ringing(unsigned call_reference, const char *token)
  2157. {
  2158. struct oh323_pvt *pvt;
  2159. if (h323debug)
  2160. ast_debug(1, "Ringing on %s\n", token);
  2161. pvt = find_call_locked(call_reference, token);
  2162. if (!pvt) {
  2163. ast_log(LOG_ERROR, "Something is wrong: ringing\n");
  2164. return;
  2165. }
  2166. if (!pvt->owner) {
  2167. ast_mutex_unlock(&pvt->lock);
  2168. ast_log(LOG_ERROR, "Channel has no owner\n");
  2169. return;
  2170. }
  2171. update_state(pvt, AST_STATE_RINGING, AST_CONTROL_RINGING);
  2172. ast_mutex_unlock(&pvt->lock);
  2173. return;
  2174. }
  2175. /*! \brief
  2176. * Call-back function to cleanup communication
  2177. * Returns nothing,
  2178. */
  2179. static void cleanup_connection(unsigned call_reference, const char *call_token)
  2180. {
  2181. struct oh323_pvt *pvt;
  2182. if (h323debug)
  2183. ast_debug(1, "Cleaning connection to %s\n", call_token);
  2184. while (1) {
  2185. pvt = find_call_locked(call_reference, call_token);
  2186. if (!pvt) {
  2187. if (h323debug)
  2188. ast_debug(1, "No connection for %s\n", call_token);
  2189. return;
  2190. }
  2191. if (!pvt->owner || !ast_channel_trylock(pvt->owner))
  2192. break;
  2193. #if 1
  2194. ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s\n", call_token);
  2195. #ifdef DEBUG_THREADS
  2196. /* XXX to be completed
  2197. * If we want to print more info on who is holding the lock,
  2198. * implement the relevant code in lock.h and use the routines
  2199. * supplied there.
  2200. */
  2201. #endif
  2202. #endif
  2203. ast_mutex_unlock(&pvt->lock);
  2204. usleep(1);
  2205. }
  2206. if (pvt->rtp) {
  2207. /* Immediately stop RTP */
  2208. ast_rtp_instance_destroy(pvt->rtp);
  2209. pvt->rtp = NULL;
  2210. }
  2211. /* Free dsp used for in-band DTMF detection */
  2212. if (pvt->vad) {
  2213. ast_dsp_free(pvt->vad);
  2214. pvt->vad = NULL;
  2215. }
  2216. cleanup_call_details(&pvt->cd);
  2217. pvt->alreadygone = 1;
  2218. /* Send hangup */
  2219. if (pvt->owner) {
  2220. pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  2221. ast_queue_hangup(pvt->owner);
  2222. ast_channel_unlock(pvt->owner);
  2223. }
  2224. ast_mutex_unlock(&pvt->lock);
  2225. if (h323debug)
  2226. ast_debug(1, "Connection to %s cleaned\n", call_token);
  2227. return;
  2228. }
  2229. static void hangup_connection(unsigned int call_reference, const char *token, int cause)
  2230. {
  2231. struct oh323_pvt *pvt;
  2232. if (h323debug)
  2233. ast_debug(1, "Hanging up connection to %s with cause %d\n", token, cause);
  2234. pvt = find_call_locked(call_reference, token);
  2235. if (!pvt) {
  2236. if (h323debug)
  2237. ast_debug(1, "Connection to %s already cleared\n", token);
  2238. return;
  2239. }
  2240. if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
  2241. pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
  2242. pvt->owner->hangupcause = pvt->hangupcause = cause;
  2243. ast_queue_hangup_with_cause(pvt->owner, cause);
  2244. ast_channel_unlock(pvt->owner);
  2245. }
  2246. else {
  2247. pvt->needhangup = 1;
  2248. pvt->hangupcause = cause;
  2249. if (h323debug)
  2250. ast_debug(1, "Hangup for %s is pending\n", token);
  2251. }
  2252. ast_mutex_unlock(&pvt->lock);
  2253. }
  2254. static void set_dtmf_payload(unsigned call_reference, const char *token, int payload, int is_cisco)
  2255. {
  2256. struct oh323_pvt *pvt;
  2257. if (h323debug)
  2258. ast_debug(1, "Setting %s DTMF payload to %d on %s\n", (is_cisco ? "Cisco" : "RFC2833"), payload, token);
  2259. pvt = find_call_locked(call_reference, token);
  2260. if (!pvt) {
  2261. return;
  2262. }
  2263. if (pvt->rtp) {
  2264. ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(pvt->rtp), pvt->rtp, payload, "audio", (is_cisco ? "cisco-telephone-event" : "telephone-event"), 0);
  2265. }
  2266. pvt->dtmf_pt[is_cisco ? 1 : 0] = payload;
  2267. ast_mutex_unlock(&pvt->lock);
  2268. if (h323debug)
  2269. ast_debug(1, "DTMF payload on %s set to %d\n", token, payload);
  2270. }
  2271. static void set_peer_capabilities(unsigned call_reference, const char *token, int capabilities, struct ast_codec_pref *prefs)
  2272. {
  2273. struct oh323_pvt *pvt;
  2274. if (h323debug)
  2275. ast_debug(1, "Got remote capabilities from connection %s\n", token);
  2276. pvt = find_call_locked(call_reference, token);
  2277. if (!pvt)
  2278. return;
  2279. pvt->peercapability = capabilities;
  2280. pvt->jointcapability = pvt->options.capability & capabilities;
  2281. if (prefs) {
  2282. memcpy(&pvt->peer_prefs, prefs, sizeof(pvt->peer_prefs));
  2283. if (h323debug) {
  2284. int i;
  2285. for (i = 0; i < 32; ++i) {
  2286. if (!prefs->order[i])
  2287. break;
  2288. ast_debug(1, "prefs[%d]=%s:%d\n", i, (prefs->order[i] ? ast_getformatname(&prefs->formats[i]) : "<none>"), prefs->framing[i]);
  2289. }
  2290. }
  2291. if (pvt->rtp) {
  2292. if (pvt->options.autoframing) {
  2293. ast_debug(2, "Autoframing option set, using peer's packetization settings\n");
  2294. ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(pvt->rtp), pvt->rtp, &pvt->peer_prefs);
  2295. } else {
  2296. ast_debug(2, "Autoframing option not set, ignoring peer's packetization settings\n");
  2297. ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(pvt->rtp), pvt->rtp, &pvt->options.prefs);
  2298. }
  2299. }
  2300. }
  2301. ast_mutex_unlock(&pvt->lock);
  2302. }
  2303. static void set_local_capabilities(unsigned call_reference, const char *token)
  2304. {
  2305. struct oh323_pvt *pvt;
  2306. int capability, dtmfmode, pref_codec;
  2307. struct ast_codec_pref prefs;
  2308. if (h323debug)
  2309. ast_debug(1, "Setting capabilities for connection %s\n", token);
  2310. pvt = find_call_locked(call_reference, token);
  2311. if (!pvt)
  2312. return;
  2313. capability = (pvt->jointcapability) ? pvt->jointcapability : pvt->options.capability;
  2314. dtmfmode = pvt->options.dtmfmode;
  2315. prefs = pvt->options.prefs;
  2316. pref_codec = pvt->pref_codec;
  2317. ast_mutex_unlock(&pvt->lock);
  2318. h323_set_capabilities(token, capability, dtmfmode, &prefs, pref_codec);
  2319. if (h323debug) {
  2320. int i;
  2321. for (i = 0; i < 32; i++) {
  2322. if (!prefs.order[i])
  2323. break;
  2324. ast_debug(1, "local prefs[%d]=%s:%d\n", i, (prefs.order[i] ? ast_getformatname(&prefs.formats[i]) : "<none>"), prefs.framing[i]);
  2325. }
  2326. ast_debug(1, "Capabilities for connection %s is set\n", token);
  2327. }
  2328. }
  2329. static void remote_hold(unsigned call_reference, const char *token, int is_hold)
  2330. {
  2331. struct oh323_pvt *pvt;
  2332. if (h323debug)
  2333. ast_debug(1, "Setting %shold status for connection %s\n", (is_hold ? "" : "un"), token);
  2334. pvt = find_call_locked(call_reference, token);
  2335. if (!pvt)
  2336. return;
  2337. if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
  2338. if (is_hold)
  2339. ast_queue_control(pvt->owner, AST_CONTROL_HOLD);
  2340. else
  2341. ast_queue_control(pvt->owner, AST_CONTROL_UNHOLD);
  2342. ast_channel_unlock(pvt->owner);
  2343. }
  2344. else {
  2345. if (is_hold)
  2346. pvt->newcontrol = AST_CONTROL_HOLD;
  2347. else
  2348. pvt->newcontrol = AST_CONTROL_UNHOLD;
  2349. }
  2350. ast_mutex_unlock(&pvt->lock);
  2351. }
  2352. static void *do_monitor(void *data)
  2353. {
  2354. int res;
  2355. int reloading;
  2356. struct oh323_pvt *oh323 = NULL;
  2357. for(;;) {
  2358. /* Check for a reload request */
  2359. ast_mutex_lock(&h323_reload_lock);
  2360. reloading = h323_reloading;
  2361. h323_reloading = 0;
  2362. ast_mutex_unlock(&h323_reload_lock);
  2363. if (reloading) {
  2364. ast_verb(1, "Reloading H.323\n");
  2365. h323_do_reload();
  2366. }
  2367. /* Check for interfaces needing to be killed */
  2368. if (!ast_mutex_trylock(&iflock)) {
  2369. #if 1
  2370. do {
  2371. for (oh323 = iflist; oh323; oh323 = oh323->next) {
  2372. if (!ast_mutex_trylock(&oh323->lock)) {
  2373. if (oh323->needdestroy) {
  2374. __oh323_destroy(oh323);
  2375. break;
  2376. }
  2377. ast_mutex_unlock(&oh323->lock);
  2378. }
  2379. }
  2380. } while (/*oh323*/ 0);
  2381. #else
  2382. restartsearch:
  2383. oh323 = iflist;
  2384. while(oh323) {
  2385. if (!ast_mutex_trylock(&oh323->lock)) {
  2386. if (oh323->needdestroy) {
  2387. __oh323_destroy(oh323);
  2388. goto restartsearch;
  2389. }
  2390. ast_mutex_unlock(&oh323->lock);
  2391. oh323 = oh323->next;
  2392. }
  2393. }
  2394. #endif
  2395. ast_mutex_unlock(&iflock);
  2396. } else
  2397. oh323 = (struct oh323_pvt *)1; /* Force fast loop */
  2398. pthread_testcancel();
  2399. /* Wait for sched or io */
  2400. res = ast_sched_wait(sched);
  2401. if ((res < 0) || (res > 1000)) {
  2402. res = 1000;
  2403. }
  2404. /* Do not wait if some channel(s) is destroyed, probably, more available too */
  2405. if (oh323)
  2406. res = 1;
  2407. res = ast_io_wait(io, res);
  2408. pthread_testcancel();
  2409. ast_mutex_lock(&monlock);
  2410. if (res >= 0) {
  2411. ast_sched_runq(sched);
  2412. }
  2413. ast_mutex_unlock(&monlock);
  2414. }
  2415. /* Never reached */
  2416. return NULL;
  2417. }
  2418. static int restart_monitor(void)
  2419. {
  2420. /* If we're supposed to be stopped -- stay stopped */
  2421. if (ast_mutex_lock(&monlock)) {
  2422. ast_log(LOG_WARNING, "Unable to lock monitor\n");
  2423. return -1;
  2424. }
  2425. if (monitor_thread == AST_PTHREADT_STOP) {
  2426. ast_mutex_unlock(&monlock);
  2427. return 0;
  2428. }
  2429. if (monitor_thread == pthread_self()) {
  2430. ast_mutex_unlock(&monlock);
  2431. ast_log(LOG_WARNING, "Cannot kill myself\n");
  2432. return -1;
  2433. }
  2434. if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
  2435. /* Wake up the thread */
  2436. pthread_kill(monitor_thread, SIGURG);
  2437. } else {
  2438. /* Start a new monitor */
  2439. if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
  2440. monitor_thread = AST_PTHREADT_NULL;
  2441. ast_mutex_unlock(&monlock);
  2442. ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
  2443. return -1;
  2444. }
  2445. }
  2446. ast_mutex_unlock(&monlock);
  2447. return 0;
  2448. }
  2449. static char *handle_cli_h323_set_trace(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2450. {
  2451. switch (cmd) {
  2452. case CLI_INIT:
  2453. e->command = "h323 set trace [on|off]";
  2454. e->usage =
  2455. "Usage: h323 set trace (on|off|<trace level>)\n"
  2456. " Enable/Disable H.323 stack tracing for debugging purposes\n";
  2457. return NULL;
  2458. case CLI_GENERATE:
  2459. return NULL;
  2460. }
  2461. if (a->argc != e->args)
  2462. return CLI_SHOWUSAGE;
  2463. if (!strcasecmp(a->argv[3], "off")) {
  2464. h323_debug(0, 0);
  2465. ast_cli(a->fd, "H.323 Trace Disabled\n");
  2466. } else if (!strcasecmp(a->argv[3], "on")) {
  2467. h323_debug(1, 1);
  2468. ast_cli(a->fd, "H.323 Trace Enabled\n");
  2469. } else {
  2470. int tracelevel = atoi(a->argv[3]);
  2471. h323_debug(1, tracelevel);
  2472. ast_cli(a->fd, "H.323 Trace Enabled (Trace Level: %d)\n", tracelevel);
  2473. }
  2474. return CLI_SUCCESS;
  2475. }
  2476. static char *handle_cli_h323_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2477. {
  2478. switch (cmd) {
  2479. case CLI_INIT:
  2480. e->command = "h323 set debug [on|off]";
  2481. e->usage =
  2482. "Usage: h323 set debug [on|off]\n"
  2483. " Enable/Disable H.323 debugging output\n";
  2484. return NULL;
  2485. case CLI_GENERATE:
  2486. return NULL;
  2487. }
  2488. if (a->argc != e->args)
  2489. return CLI_SHOWUSAGE;
  2490. if (strcasecmp(a->argv[3], "on") && strcasecmp(a->argv[3], "off"))
  2491. return CLI_SHOWUSAGE;
  2492. h323debug = (strcasecmp(a->argv[3], "on")) ? 0 : 1;
  2493. ast_cli(a->fd, "H.323 Debugging %s\n", h323debug ? "Enabled" : "Disabled");
  2494. return CLI_SUCCESS;
  2495. }
  2496. static char *handle_cli_h323_cycle_gk(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2497. {
  2498. switch (cmd) {
  2499. case CLI_INIT:
  2500. e->command = "h323 cycle gk";
  2501. e->usage =
  2502. "Usage: h323 cycle gk\n"
  2503. " Manually re-register with the Gatekeper (Currently Disabled)\n";
  2504. return NULL;
  2505. case CLI_GENERATE:
  2506. return NULL;
  2507. }
  2508. if (a->argc != 3)
  2509. return CLI_SHOWUSAGE;
  2510. h323_gk_urq();
  2511. /* Possibly register with a GK */
  2512. if (!gatekeeper_disable) {
  2513. if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
  2514. ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
  2515. }
  2516. }
  2517. return CLI_SUCCESS;
  2518. }
  2519. static char *handle_cli_h323_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2520. {
  2521. switch (cmd) {
  2522. case CLI_INIT:
  2523. e->command = "h323 hangup";
  2524. e->usage =
  2525. "Usage: h323 hangup <token>\n"
  2526. " Manually try to hang up the call identified by <token>\n";
  2527. return NULL;
  2528. case CLI_GENERATE:
  2529. return NULL;
  2530. }
  2531. if (a->argc != 3)
  2532. return CLI_SHOWUSAGE;
  2533. if (h323_soft_hangup(a->argv[2])) {
  2534. ast_verb(3, "Hangup succeeded on %s\n", a->argv[2]);
  2535. } else {
  2536. ast_verb(3, "Hangup failed for %s\n", a->argv[2]);
  2537. }
  2538. return CLI_SUCCESS;
  2539. }
  2540. static char *handle_cli_h323_show_tokens(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2541. {
  2542. switch (cmd) {
  2543. case CLI_INIT:
  2544. e->command = "h323 show tokens";
  2545. e->usage =
  2546. "Usage: h323 show tokens\n"
  2547. " Print out all active call tokens\n";
  2548. return NULL;
  2549. case CLI_GENERATE:
  2550. return NULL;
  2551. }
  2552. if (a->argc != 3)
  2553. return CLI_SHOWUSAGE;
  2554. h323_show_tokens();
  2555. return CLI_SUCCESS;
  2556. }
  2557. static char *handle_cli_h323_show_version(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2558. {
  2559. switch (cmd) {
  2560. case CLI_INIT:
  2561. e->command = "h323 show version";
  2562. e->usage =
  2563. "Usage: h323 show version\n"
  2564. " Show the version of the H.323 library in use\n";
  2565. return NULL;
  2566. case CLI_GENERATE:
  2567. return NULL;
  2568. }
  2569. if (a->argc != 3)
  2570. return CLI_SHOWUSAGE;
  2571. h323_show_version();
  2572. return CLI_SUCCESS;
  2573. }
  2574. static struct ast_cli_entry cli_h323[] = {
  2575. AST_CLI_DEFINE(handle_cli_h323_set_trace, "Enable/Disable H.323 Stack Tracing"),
  2576. AST_CLI_DEFINE(handle_cli_h323_set_debug, "Enable/Disable H.323 Debugging"),
  2577. AST_CLI_DEFINE(handle_cli_h323_cycle_gk, "Manually re-register with the Gatekeper"),
  2578. AST_CLI_DEFINE(handle_cli_h323_hangup, "Manually try to hang up a call"),
  2579. AST_CLI_DEFINE(handle_cli_h323_show_tokens, "Show all active call tokens"),
  2580. AST_CLI_DEFINE(handle_cli_h323_show_version, "Show the version of the H.323 library in use"),
  2581. };
  2582. static void delete_users(void)
  2583. {
  2584. int pruned = 0;
  2585. /* Delete all users */
  2586. ASTOBJ_CONTAINER_WRLOCK(&userl);
  2587. ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
  2588. ASTOBJ_RDLOCK(iterator);
  2589. ASTOBJ_MARK(iterator);
  2590. ++pruned;
  2591. ASTOBJ_UNLOCK(iterator);
  2592. } while (0) );
  2593. if (pruned) {
  2594. ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, oh323_destroy_user);
  2595. }
  2596. ASTOBJ_CONTAINER_UNLOCK(&userl);
  2597. ASTOBJ_CONTAINER_WRLOCK(&peerl);
  2598. ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
  2599. ASTOBJ_RDLOCK(iterator);
  2600. ASTOBJ_MARK(iterator);
  2601. ASTOBJ_UNLOCK(iterator);
  2602. } while (0) );
  2603. ASTOBJ_CONTAINER_UNLOCK(&peerl);
  2604. }
  2605. static void delete_aliases(void)
  2606. {
  2607. int pruned = 0;
  2608. /* Delete all aliases */
  2609. ASTOBJ_CONTAINER_WRLOCK(&aliasl);
  2610. ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
  2611. ASTOBJ_RDLOCK(iterator);
  2612. ASTOBJ_MARK(iterator);
  2613. ++pruned;
  2614. ASTOBJ_UNLOCK(iterator);
  2615. } while (0) );
  2616. if (pruned) {
  2617. ASTOBJ_CONTAINER_PRUNE_MARKED(&aliasl, oh323_destroy_alias);
  2618. }
  2619. ASTOBJ_CONTAINER_UNLOCK(&aliasl);
  2620. }
  2621. static void prune_peers(void)
  2622. {
  2623. /* Prune peers who still are supposed to be deleted */
  2624. ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, oh323_destroy_peer);
  2625. }
  2626. static int reload_config(int is_reload)
  2627. {
  2628. struct ast_config *cfg, *ucfg;
  2629. struct ast_variable *v;
  2630. struct oh323_peer *peer = NULL;
  2631. struct oh323_user *user = NULL;
  2632. struct oh323_alias *alias = NULL;
  2633. struct ast_hostent ahp; struct hostent *hp;
  2634. char *cat;
  2635. const char *utype;
  2636. int is_user, is_peer, is_alias;
  2637. char _gatekeeper[100];
  2638. int gk_discover, gk_disable, gk_changed;
  2639. struct ast_flags config_flags = { is_reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
  2640. cfg = ast_config_load(config, config_flags);
  2641. /* We *must* have a config file otherwise stop immediately */
  2642. if (!cfg) {
  2643. ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
  2644. return 1;
  2645. } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
  2646. ucfg = ast_config_load("users.conf", config_flags);
  2647. if (ucfg == CONFIG_STATUS_FILEUNCHANGED) {
  2648. return 0;
  2649. } else if (ucfg == CONFIG_STATUS_FILEINVALID) {
  2650. ast_log(LOG_ERROR, "Config file users.conf is in an invalid format. Aborting.\n");
  2651. return 0;
  2652. }
  2653. ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
  2654. if ((cfg = ast_config_load(config, config_flags)) == CONFIG_STATUS_FILEINVALID) {
  2655. ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
  2656. ast_config_destroy(ucfg);
  2657. return 0;
  2658. }
  2659. } else if (cfg == CONFIG_STATUS_FILEINVALID) {
  2660. ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
  2661. return 0;
  2662. } else {
  2663. ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
  2664. if ((ucfg = ast_config_load("users.conf", config_flags)) == CONFIG_STATUS_FILEINVALID) {
  2665. ast_log(LOG_ERROR, "Config file users.conf is in an invalid format. Aborting.\n");
  2666. ast_config_destroy(cfg);
  2667. return 0;
  2668. }
  2669. }
  2670. if (is_reload) {
  2671. delete_users();
  2672. delete_aliases();
  2673. prune_peers();
  2674. }
  2675. /* fire up the H.323 Endpoint */
  2676. if (!h323_end_point_exist()) {
  2677. h323_end_point_create();
  2678. }
  2679. ast_copy_string(_gatekeeper, gatekeeper, sizeof(_gatekeeper));
  2680. gk_discover = gatekeeper_discover;
  2681. gk_disable = gatekeeper_disable;
  2682. memset(&bindaddr, 0, sizeof(bindaddr));
  2683. memset(&global_options, 0, sizeof(global_options));
  2684. global_options.fastStart = 1;
  2685. global_options.h245Tunneling = 1;
  2686. global_options.dtmfcodec[0] = H323_DTMF_RFC2833_PT;
  2687. global_options.dtmfcodec[1] = H323_DTMF_CISCO_PT;
  2688. global_options.dtmfmode = 0;
  2689. global_options.holdHandling = 0;
  2690. global_options.capability = GLOBAL_CAPABILITY;
  2691. global_options.bridge = 1; /* Do native bridging by default */
  2692. global_options.autoframing = 0;
  2693. strcpy(default_context, "default");
  2694. h323_signalling_port = 1720;
  2695. gatekeeper_disable = 1;
  2696. gatekeeper_discover = 0;
  2697. gkroute = 0;
  2698. userbyalias = 1;
  2699. acceptAnonymous = 1;
  2700. tos = 0;
  2701. cos = 0;
  2702. /* Copy the default jb config over global_jbconf */
  2703. memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
  2704. if (ucfg) {
  2705. struct ast_variable *gen;
  2706. int genhas_h323;
  2707. const char *has_h323;
  2708. genhas_h323 = ast_true(ast_variable_retrieve(ucfg, "general", "hash323"));
  2709. gen = ast_variable_browse(ucfg, "general");
  2710. for (cat = ast_category_browse(ucfg, NULL); cat; cat = ast_category_browse(ucfg, cat)) {
  2711. if (strcasecmp(cat, "general")) {
  2712. has_h323 = ast_variable_retrieve(ucfg, cat, "hash323");
  2713. if (ast_true(has_h323) || (!has_h323 && genhas_h323)) {
  2714. user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
  2715. if (user) {
  2716. ASTOBJ_CONTAINER_LINK(&userl, user);
  2717. ASTOBJ_UNREF(user, oh323_destroy_user);
  2718. }
  2719. peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
  2720. if (peer) {
  2721. ASTOBJ_CONTAINER_LINK(&peerl, peer);
  2722. ASTOBJ_UNREF(peer, oh323_destroy_peer);
  2723. }
  2724. }
  2725. }
  2726. }
  2727. ast_config_destroy(ucfg);
  2728. }
  2729. for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
  2730. /* handle jb conf */
  2731. if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
  2732. continue;
  2733. /* Create the interface list */
  2734. if (!strcasecmp(v->name, "port")) {
  2735. h323_signalling_port = (int)strtol(v->value, NULL, 10);
  2736. } else if (!strcasecmp(v->name, "bindaddr")) {
  2737. if (!(hp = ast_gethostbyname(v->value, &ahp))) {
  2738. ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
  2739. } else {
  2740. memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
  2741. }
  2742. } else if (!strcasecmp(v->name, "tos")) { /* Needs to be removed in next release */
  2743. ast_log(LOG_WARNING, "The \"tos\" setting is deprecated in this version of Asterisk. Please change to \"tos_audio\".\n");
  2744. if (ast_str2tos(v->value, &tos)) {
  2745. ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  2746. }
  2747. } else if (!strcasecmp(v->name, "tos_audio")) {
  2748. if (ast_str2tos(v->value, &tos)) {
  2749. ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  2750. }
  2751. } else if (!strcasecmp(v->name, "cos")) {
  2752. ast_log(LOG_WARNING, "The \"cos\" setting is deprecated in this version of Asterisk. Please change to \"cos_audio\".\n");
  2753. if (ast_str2cos(v->value, &cos)) {
  2754. ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  2755. }
  2756. } else if (!strcasecmp(v->name, "cos_audio")) {
  2757. if (ast_str2cos(v->value, &cos)) {
  2758. ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
  2759. }
  2760. } else if (!strcasecmp(v->name, "gatekeeper")) {
  2761. if (!strcasecmp(v->value, "DISABLE")) {
  2762. gatekeeper_disable = 1;
  2763. } else if (!strcasecmp(v->value, "DISCOVER")) {
  2764. gatekeeper_disable = 0;
  2765. gatekeeper_discover = 1;
  2766. } else {
  2767. gatekeeper_disable = 0;
  2768. ast_copy_string(gatekeeper, v->value, sizeof(gatekeeper));
  2769. }
  2770. } else if (!strcasecmp(v->name, "secret")) {
  2771. ast_copy_string(secret, v->value, sizeof(secret));
  2772. } else if (!strcasecmp(v->name, "AllowGKRouted")) {
  2773. gkroute = ast_true(v->value);
  2774. } else if (!strcasecmp(v->name, "context")) {
  2775. ast_copy_string(default_context, v->value, sizeof(default_context));
  2776. ast_verb(2, "Setting default context to %s\n", default_context);
  2777. } else if (!strcasecmp(v->name, "UserByAlias")) {
  2778. userbyalias = ast_true(v->value);
  2779. } else if (!strcasecmp(v->name, "AcceptAnonymous")) {
  2780. acceptAnonymous = ast_true(v->value);
  2781. } else if (!update_common_options(v, &global_options)) {
  2782. /* dummy */
  2783. }
  2784. }
  2785. if (!global_options.dtmfmode)
  2786. global_options.dtmfmode = H323_DTMF_RFC2833;
  2787. if (global_options.holdHandling == ~0)
  2788. global_options.holdHandling = 0;
  2789. else if (!global_options.holdHandling)
  2790. global_options.holdHandling = H323_HOLD_H450;
  2791. for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
  2792. if (strcasecmp(cat, "general")) {
  2793. utype = ast_variable_retrieve(cfg, cat, "type");
  2794. if (utype) {
  2795. is_user = is_peer = is_alias = 0;
  2796. if (!strcasecmp(utype, "user"))
  2797. is_user = 1;
  2798. else if (!strcasecmp(utype, "peer"))
  2799. is_peer = 1;
  2800. else if (!strcasecmp(utype, "friend"))
  2801. is_user = is_peer = 1;
  2802. else if (!strcasecmp(utype, "h323") || !strcasecmp(utype, "alias"))
  2803. is_alias = 1;
  2804. else {
  2805. ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
  2806. continue;
  2807. }
  2808. if (is_user) {
  2809. user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
  2810. if (user) {
  2811. ASTOBJ_CONTAINER_LINK(&userl, user);
  2812. ASTOBJ_UNREF(user, oh323_destroy_user);
  2813. }
  2814. }
  2815. if (is_peer) {
  2816. peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
  2817. if (peer) {
  2818. ASTOBJ_CONTAINER_LINK(&peerl, peer);
  2819. ASTOBJ_UNREF(peer, oh323_destroy_peer);
  2820. }
  2821. }
  2822. if (is_alias) {
  2823. alias = build_alias(cat, ast_variable_browse(cfg, cat), NULL, 0);
  2824. if (alias) {
  2825. ASTOBJ_CONTAINER_LINK(&aliasl, alias);
  2826. ASTOBJ_UNREF(alias, oh323_destroy_alias);
  2827. }
  2828. }
  2829. } else {
  2830. ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
  2831. }
  2832. }
  2833. }
  2834. ast_config_destroy(cfg);
  2835. /* Register our H.323 aliases if any*/
  2836. ASTOBJ_CONTAINER_WRLOCK(&aliasl);
  2837. ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
  2838. ASTOBJ_RDLOCK(iterator);
  2839. if (h323_set_alias(iterator)) {
  2840. ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
  2841. ASTOBJ_UNLOCK(iterator);
  2842. continue;
  2843. }
  2844. ASTOBJ_UNLOCK(iterator);
  2845. } while (0) );
  2846. ASTOBJ_CONTAINER_UNLOCK(&aliasl);
  2847. /* Don't touch GK if nothing changed because URQ will drop all existing calls */
  2848. gk_changed = 0;
  2849. if (gatekeeper_disable != gk_disable)
  2850. gk_changed = is_reload;
  2851. else if(!gatekeeper_disable && (gatekeeper_discover != gk_discover))
  2852. gk_changed = is_reload;
  2853. else if(!gatekeeper_disable && (strncmp(_gatekeeper, gatekeeper, sizeof(_gatekeeper)) != 0))
  2854. gk_changed = is_reload;
  2855. if (gk_changed) {
  2856. if(!gk_disable)
  2857. h323_gk_urq();
  2858. if (!gatekeeper_disable) {
  2859. if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
  2860. ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
  2861. gatekeeper_disable = 1;
  2862. }
  2863. }
  2864. }
  2865. return 0;
  2866. }
  2867. static int h323_reload(void)
  2868. {
  2869. ast_mutex_lock(&h323_reload_lock);
  2870. if (h323_reloading) {
  2871. ast_verbose("Previous H.323 reload not yet done\n");
  2872. } else {
  2873. h323_reloading = 1;
  2874. }
  2875. ast_mutex_unlock(&h323_reload_lock);
  2876. restart_monitor();
  2877. return 0;
  2878. }
  2879. static char *handle_cli_h323_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
  2880. {
  2881. switch (cmd) {
  2882. case CLI_INIT:
  2883. e->command = "h323 reload";
  2884. e->usage =
  2885. "Usage: h323 reload\n"
  2886. " Reloads H.323 configuration from h323.conf\n";
  2887. return NULL;
  2888. case CLI_GENERATE:
  2889. return NULL;
  2890. }
  2891. if (a->argc != 2)
  2892. return CLI_SHOWUSAGE;
  2893. h323_reload();
  2894. return CLI_SUCCESS;
  2895. }
  2896. static int h323_do_reload(void)
  2897. {
  2898. reload_config(1);
  2899. return 0;
  2900. }
  2901. static int reload(void)
  2902. {
  2903. if (!sched || !io) {
  2904. ast_log(LOG_NOTICE, "Unload and load chan_h323.so again in order to receive configuration changes.\n");
  2905. return 0;
  2906. }
  2907. return h323_reload();
  2908. }
  2909. static struct ast_cli_entry cli_h323_reload =
  2910. AST_CLI_DEFINE(handle_cli_h323_reload, "Reload H.323 configuration");
  2911. static enum ast_rtp_glue_result oh323_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **instance)
  2912. {
  2913. struct oh323_pvt *pvt;
  2914. enum ast_rtp_glue_result res = AST_RTP_GLUE_RESULT_LOCAL;
  2915. if (!(pvt = (struct oh323_pvt *)chan->tech_pvt))
  2916. return AST_RTP_GLUE_RESULT_FORBID;
  2917. ast_mutex_lock(&pvt->lock);
  2918. *instance = pvt->rtp ? ao2_ref(pvt->rtp, +1), pvt->rtp : NULL;
  2919. #if 0
  2920. if (pvt->options.bridge) {
  2921. res = AST_RTP_GLUE_RESULT_REMOTE;
  2922. }
  2923. #endif
  2924. ast_mutex_unlock(&pvt->lock);
  2925. return res;
  2926. }
  2927. #if 0
  2928. static char *convertcap(struct ast_format *format)
  2929. {
  2930. switch (format->id) {
  2931. case AST_FORMAT_G723_1:
  2932. return "G.723";
  2933. case AST_FORMAT_GSM:
  2934. return "GSM";
  2935. case AST_FORMAT_ULAW:
  2936. return "ULAW";
  2937. case AST_FORMAT_ALAW:
  2938. return "ALAW";
  2939. case AST_FORMAT_G722:
  2940. return "G.722";
  2941. case AST_FORMAT_ADPCM:
  2942. return "G.728";
  2943. case AST_FORMAT_G729A:
  2944. return "G.729";
  2945. case AST_FORMAT_SPEEX:
  2946. return "SPEEX";
  2947. case AST_FORMAT_ILBC:
  2948. return "ILBC";
  2949. default:
  2950. ast_log(LOG_NOTICE, "Don't know how to deal with mode %s\n", ast_getformatname(format));
  2951. return NULL;
  2952. }
  2953. }
  2954. #endif
  2955. static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, const struct ast_format_cap *codecs, int nat_active)
  2956. {
  2957. /* XXX Deal with Video */
  2958. struct oh323_pvt *pvt;
  2959. struct sockaddr_in them = { 0, };
  2960. struct sockaddr_in us = { 0, };
  2961. #if 0 /* Native bridge still isn't ready */
  2962. char *mode;
  2963. #endif
  2964. if (!rtp) {
  2965. return 0;
  2966. }
  2967. #if 0 /* Native bridge still isn't ready */
  2968. mode = convertcap(&chan->writeformat);
  2969. #endif
  2970. pvt = (struct oh323_pvt *) chan->tech_pvt;
  2971. if (!pvt) {
  2972. ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
  2973. return -1;
  2974. }
  2975. {
  2976. struct ast_sockaddr tmp;
  2977. ast_rtp_instance_get_remote_address(rtp, &tmp);
  2978. ast_sockaddr_to_sin(&tmp, &them);
  2979. ast_rtp_instance_get_local_address(rtp, &tmp);
  2980. ast_sockaddr_to_sin(&tmp, &us);
  2981. }
  2982. #if 0 /* Native bridge still isn't ready */
  2983. h323_native_bridge(pvt->cd.call_token, ast_inet_ntoa(them.sin_addr), mode);
  2984. #endif
  2985. return 0;
  2986. }
  2987. static struct ast_rtp_glue oh323_rtp_glue = {
  2988. .type = "H323",
  2989. .get_rtp_info = oh323_get_rtp_peer,
  2990. .update_peer = oh323_set_rtp_peer,
  2991. };
  2992. static enum ast_module_load_result load_module(void)
  2993. {
  2994. int res;
  2995. if (!(oh323_tech.capabilities = ast_format_cap_alloc())) {
  2996. return AST_MODULE_LOAD_FAILURE;
  2997. }
  2998. ast_format_cap_add_all_by_type(oh323_tech.capabilities, AST_FORMAT_TYPE_AUDIO);
  2999. h323debug = 0;
  3000. sched = ast_sched_context_create();
  3001. if (!sched) {
  3002. ast_log(LOG_WARNING, "Unable to create schedule context\n");
  3003. return AST_MODULE_LOAD_FAILURE;
  3004. }
  3005. io = io_context_create();
  3006. if (!io) {
  3007. ast_log(LOG_WARNING, "Unable to create I/O context\n");
  3008. return AST_MODULE_LOAD_FAILURE;
  3009. }
  3010. ast_cli_register(&cli_h323_reload);
  3011. ASTOBJ_CONTAINER_INIT(&userl);
  3012. ASTOBJ_CONTAINER_INIT(&peerl);
  3013. ASTOBJ_CONTAINER_INIT(&aliasl);
  3014. res = reload_config(0);
  3015. if (res) {
  3016. /* No config entry */
  3017. ast_log(LOG_NOTICE, "Unload and load chan_h323.so again in order to receive configuration changes.\n");
  3018. ast_cli_unregister(&cli_h323_reload);
  3019. io_context_destroy(io);
  3020. io = NULL;
  3021. ast_sched_context_destroy(sched);
  3022. sched = NULL;
  3023. ASTOBJ_CONTAINER_DESTROY(&userl);
  3024. ASTOBJ_CONTAINER_DESTROY(&peerl);
  3025. ASTOBJ_CONTAINER_DESTROY(&aliasl);
  3026. return AST_MODULE_LOAD_DECLINE;
  3027. } else {
  3028. /* Make sure we can register our channel type */
  3029. if (ast_channel_register(&oh323_tech)) {
  3030. ast_log(LOG_ERROR, "Unable to register channel class 'H323'\n");
  3031. ast_cli_unregister(&cli_h323_reload);
  3032. h323_end_process();
  3033. io_context_destroy(io);
  3034. ast_sched_context_destroy(sched);
  3035. ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
  3036. ASTOBJ_CONTAINER_DESTROY(&userl);
  3037. ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
  3038. ASTOBJ_CONTAINER_DESTROY(&peerl);
  3039. ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
  3040. ASTOBJ_CONTAINER_DESTROY(&aliasl);
  3041. return AST_MODULE_LOAD_FAILURE;
  3042. }
  3043. ast_cli_register_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
  3044. ast_rtp_glue_register(&oh323_rtp_glue);
  3045. /* Register our callback functions */
  3046. h323_callback_register(setup_incoming_call,
  3047. setup_outgoing_call,
  3048. external_rtp_create,
  3049. setup_rtp_connection,
  3050. cleanup_connection,
  3051. chan_ringing,
  3052. connection_made,
  3053. receive_digit,
  3054. answer_call,
  3055. progress,
  3056. set_dtmf_payload,
  3057. hangup_connection,
  3058. set_local_capabilities,
  3059. set_peer_capabilities,
  3060. remote_hold);
  3061. /* start the h.323 listener */
  3062. if (h323_start_listener(h323_signalling_port, bindaddr)) {
  3063. ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
  3064. ast_rtp_glue_unregister(&oh323_rtp_glue);
  3065. ast_cli_unregister_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
  3066. ast_cli_unregister(&cli_h323_reload);
  3067. h323_end_process();
  3068. io_context_destroy(io);
  3069. ast_sched_context_destroy(sched);
  3070. ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
  3071. ASTOBJ_CONTAINER_DESTROY(&userl);
  3072. ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
  3073. ASTOBJ_CONTAINER_DESTROY(&peerl);
  3074. ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
  3075. ASTOBJ_CONTAINER_DESTROY(&aliasl);
  3076. return AST_MODULE_LOAD_DECLINE;
  3077. }
  3078. /* Possibly register with a GK */
  3079. if (!gatekeeper_disable) {
  3080. if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
  3081. ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
  3082. gatekeeper_disable = 1;
  3083. res = AST_MODULE_LOAD_SUCCESS;
  3084. }
  3085. }
  3086. /* And start the monitor for the first time */
  3087. restart_monitor();
  3088. }
  3089. return res;
  3090. }
  3091. static int unload_module(void)
  3092. {
  3093. struct oh323_pvt *p, *pl;
  3094. /* unregister commands */
  3095. ast_cli_unregister_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
  3096. ast_cli_unregister(&cli_h323_reload);
  3097. ast_channel_unregister(&oh323_tech);
  3098. ast_rtp_glue_unregister(&oh323_rtp_glue);
  3099. if (!ast_mutex_lock(&iflock)) {
  3100. /* hangup all interfaces if they have an owner */
  3101. p = iflist;
  3102. while(p) {
  3103. if (p->owner) {
  3104. ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
  3105. }
  3106. p = p->next;
  3107. }
  3108. iflist = NULL;
  3109. ast_mutex_unlock(&iflock);
  3110. } else {
  3111. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  3112. return -1;
  3113. }
  3114. if (!ast_mutex_lock(&monlock)) {
  3115. if ((monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
  3116. if (monitor_thread != pthread_self()) {
  3117. pthread_cancel(monitor_thread);
  3118. }
  3119. pthread_kill(monitor_thread, SIGURG);
  3120. pthread_join(monitor_thread, NULL);
  3121. }
  3122. monitor_thread = AST_PTHREADT_STOP;
  3123. ast_mutex_unlock(&monlock);
  3124. } else {
  3125. ast_log(LOG_WARNING, "Unable to lock the monitor\n");
  3126. return -1;
  3127. }
  3128. if (!ast_mutex_lock(&iflock)) {
  3129. /* destroy all the interfaces and free their memory */
  3130. p = iflist;
  3131. while(p) {
  3132. pl = p;
  3133. p = p->next;
  3134. /* free associated memory */
  3135. ast_mutex_destroy(&pl->lock);
  3136. ast_free(pl);
  3137. }
  3138. iflist = NULL;
  3139. ast_mutex_unlock(&iflock);
  3140. } else {
  3141. ast_log(LOG_WARNING, "Unable to lock the interface list\n");
  3142. return -1;
  3143. }
  3144. if (!gatekeeper_disable)
  3145. h323_gk_urq();
  3146. h323_end_process();
  3147. if (io)
  3148. io_context_destroy(io);
  3149. if (sched)
  3150. ast_sched_context_destroy(sched);
  3151. ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
  3152. ASTOBJ_CONTAINER_DESTROY(&userl);
  3153. ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
  3154. ASTOBJ_CONTAINER_DESTROY(&peerl);
  3155. ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
  3156. ASTOBJ_CONTAINER_DESTROY(&aliasl);
  3157. oh323_tech.capabilities = ast_format_cap_destroy(oh323_tech.capabilities);
  3158. return 0;
  3159. }
  3160. AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_LOAD_ORDER, "The NuFone Network's OpenH323 Channel Driver",
  3161. .load = load_module,
  3162. .unload = unload_module,
  3163. .reload = reload,
  3164. .load_pri = AST_MODPRI_CHANNEL_DRIVER,
  3165. );