chan_h323.c 101 KB

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