chan_h323.c 102 KB

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