chan_h323.c 104 KB

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