2 * Asterisk -- An open source telephony toolkit.
4 * Copyright (C) 1999 - 2005
6 * OpenH323 Channel Driver for ASTERISK PBX.
8 * For The NuFone Network
10 * chan_h323 has been derived from code created by
11 * Michael Manousos and Mark Spencer
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.
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.
26 * \brief This file is part of the chan_h323 driver for Asterisk
28 * \author Jeremy McNamara
32 * \extref OpenH323 http://www.voxgratia.org/
34 * \ingroup channel_drivers
38 <depend>openh323</depend>
39 <defaultenabled>yes</defaultenabled>
48 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
54 #include <sys/types.h>
55 #include <sys/socket.h>
56 #include <sys/signal.h>
57 #include <sys/param.h>
58 #include <arpa/inet.h>
60 #include <netinet/in.h>
61 #include <netinet/in_systm.h>
62 #include <netinet/ip.h>
70 #include "asterisk/lock.h"
71 #include "asterisk/channel.h"
72 #include "asterisk/config.h"
73 #include "asterisk/module.h"
74 #include "asterisk/musiconhold.h"
75 #include "asterisk/pbx.h"
76 #include "asterisk/utils.h"
77 #include "asterisk/sched.h"
78 #include "asterisk/io.h"
79 #include "asterisk/rtp.h"
80 #include "asterisk/acl.h"
81 #include "asterisk/callerid.h"
82 #include "asterisk/cli.h"
83 #include "asterisk/dsp.h"
84 #include "asterisk/causes.h"
85 #include "asterisk/stringfields.h"
86 #include "asterisk/abstract_jb.h"
87 #include "asterisk/astobj.h"
93 #include "h323/chan_h323.h"
95 receive_digit_cb on_receive_digit;
96 on_rtp_cb on_external_rtp_create;
97 start_rtp_cb on_start_rtp_channel;
98 setup_incoming_cb on_incoming_call;
99 setup_outbound_cb on_outgoing_call;
100 chan_ringing_cb on_chan_ringing;
101 con_established_cb on_connection_established;
102 clear_con_cb on_connection_cleared;
103 answer_call_cb on_answer_call;
104 progress_cb on_progress;
105 rfc2833_cb on_set_rfc2833_payload;
107 setcapabilities_cb on_setcapabilities;
108 setpeercapabilities_cb on_setpeercapabilities;
111 int h323debug; /*!< global debug flag */
113 /*! \brief Global jitterbuffer configuration - by default, jb is disabled */
114 static struct ast_jb_conf default_jbconf =
118 .resync_threshold = -1,
121 static struct ast_jb_conf global_jbconf;
123 /** Variables required by Asterisk */
124 static const char tdesc[] = "The NuFone Network's Open H.323 Channel Driver";
125 static const char config[] = "h323.conf";
126 static char default_context[AST_MAX_CONTEXT] = "default";
127 static struct sockaddr_in bindaddr;
129 #define GLOBAL_CAPABILITY (AST_FORMAT_G723_1 | AST_FORMAT_GSM | AST_FORMAT_ULAW | AST_FORMAT_ALAW | AST_FORMAT_G729A | AST_FORMAT_G726_AAL2 | AST_FORMAT_H261)
131 /** H.323 configuration values */
132 static int h323_signalling_port = 1720;
133 static char gatekeeper[100];
134 static int gatekeeper_disable = 1;
135 static int gatekeeper_discover = 0;
136 static int gkroute = 0;
137 /* Find user by alias (h.323 id) is default, alternative is the incomming call's source IP address*/
138 static int userbyalias = 1;
139 static int acceptAnonymous = 1;
140 static unsigned int tos = 0;
141 static unsigned int cos = 0;
142 static char secret[50];
143 static unsigned int unique = 0;
145 static call_options_t global_options;
147 /*! \brief Private structure of a OpenH323 channel */
149 ast_mutex_t lock; /*!< Channel private lock */
150 call_options_t options; /*!<!< Options to be used during call setup */
151 int alreadygone; /*!< Whether or not we've already been destroyed by our peer */
152 int needdestroy; /*!< if we need to be destroyed */
153 call_details_t cd; /*!< Call details */
154 struct ast_channel *owner; /*!< Who owns us */
155 struct sockaddr_in sa; /*!< Our peer */
156 struct sockaddr_in redirip; /*!< Where our RTP should be going if not to us */
157 int nonCodecCapability; /*!< non-audio capability */
158 int outgoing; /*!< Outgoing or incoming call? */
159 char exten[AST_MAX_EXTENSION]; /*!< Requested extension */
160 char context[AST_MAX_CONTEXT]; /*!< Context where to start */
161 char accountcode[256]; /*!< Account code */
162 char rdnis[80]; /*!< Referring DNIS, if available */
163 int amaflags; /*!< AMA Flags */
164 struct ast_rtp *rtp; /*!< RTP Session */
165 struct ast_dsp *vad; /*!< Used for in-band DTMF detection */
166 int nativeformats; /*!< Codec formats supported by a channel */
167 int needhangup; /*!< Send hangup when Asterisk is ready */
168 int hangupcause; /*!< Hangup cause from OpenH323 layer */
169 int newstate; /*!< Pending state change */
170 int newcontrol; /*!< Pending control to send */
171 int newdigit; /*!< Pending DTMF digit to send */
172 int newduration; /*!< Pending DTMF digit duration to send */
173 int pref_codec; /*!< Preferred codec */
174 int peercapability; /*!< Capabilities learned from peer */
175 int jointcapability; /*!< Common capabilities for local and remote side */
176 struct ast_codec_pref peer_prefs; /*!< Preferenced list of codecs which remote side supports */
177 int dtmf_pt[2]; /*!< Payload code used for RFC2833/CISCO messages */
178 int curDTMF; /*!< DTMF tone being generated to Asterisk side */
179 int DTMFsched; /*!< Scheduler descriptor for DTMF */
180 int update_rtp_info; /*!< Configuration of fd's array is pending */
181 int recvonly; /*!< Peer isn't wish to receive our voice stream */
182 int txDtmfDigit; /*!< DTMF digit being to send to H.323 side */
183 int noInbandDtmf; /*!< Inband DTMF processing by DSP isn't available */
184 int connection_established; /*!< Call got CONNECT message */
185 int got_progress; /*!< Call got PROGRESS message, pass inband audio */
186 struct oh323_pvt *next; /*!< Next channel in list */
189 /*! \brief H323 User list */
190 static struct h323_user_list {
191 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_user);
194 /*! \brief H323 peer list */
195 static struct h323_peer_list {
196 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_peer);
199 /*! \brief H323 alias list */
200 static struct h323_alias_list {
201 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_alias);
204 /* Asterisk RTP stuff */
205 static struct sched_context *sched;
206 static struct io_context *io;
208 AST_MUTEX_DEFINE_STATIC(iflock); /*!< Protect the interface list (oh323_pvt) */
210 /*! \brief Protect the H.323 monitoring thread, so only one process can kill or start it, and not
211 when it's doing something critical. */
212 AST_MUTEX_DEFINE_STATIC(monlock);
214 /*! \brief Protect the H.323 capabilities list, to avoid more than one channel to set the capabilities simultaneaously in the h323 stack. */
215 AST_MUTEX_DEFINE_STATIC(caplock);
217 /*! \brief Protect the reload process */
218 AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
219 static int h323_reloading = 0;
221 /*! \brief This is the thread for the monitor which checks for input on the channels
222 which are not currently in use. */
223 static pthread_t monitor_thread = AST_PTHREADT_NULL;
224 static int restart_monitor(void);
225 static int h323_do_reload(void);
227 static void delete_users(void);
228 static void delete_aliases(void);
229 static void prune_peers(void);
231 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause);
232 static int oh323_digit_begin(struct ast_channel *c, char digit);
233 static int oh323_digit_end(struct ast_channel *c, char digit, unsigned int duration);
234 static int oh323_call(struct ast_channel *c, char *dest, int timeout);
235 static int oh323_hangup(struct ast_channel *c);
236 static int oh323_answer(struct ast_channel *c);
237 static struct ast_frame *oh323_read(struct ast_channel *c);
238 static int oh323_write(struct ast_channel *c, struct ast_frame *frame);
239 static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen);
240 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
242 static const struct ast_channel_tech oh323_tech = {
244 .description = tdesc,
245 .capabilities = AST_FORMAT_AUDIO_MASK,
246 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
247 .requester = oh323_request,
248 .send_digit_begin = oh323_digit_begin,
249 .send_digit_end = oh323_digit_end,
251 .hangup = oh323_hangup,
252 .answer = oh323_answer,
254 .write = oh323_write,
255 .indicate = oh323_indicate,
256 .fixup = oh323_fixup,
257 .bridge = ast_rtp_bridge,
260 static const char* redirectingreason2str(int redirectingreason)
262 switch (redirectingreason) {
270 return "UNCONDITIONAL";
276 static void oh323_destroy_alias(struct oh323_alias *alias)
279 ast_debug(1, "Destroying alias '%s'\n", alias->name);
283 static void oh323_destroy_user(struct oh323_user *user)
286 ast_debug(1, "Destroying user '%s'\n", user->name);
287 ast_free_ha(user->ha);
291 static void oh323_destroy_peer(struct oh323_peer *peer)
294 ast_debug(1, "Destroying peer '%s'\n", peer->name);
295 ast_free_ha(peer->ha);
299 static int oh323_simulate_dtmf_end(const void *data)
301 struct oh323_pvt *pvt = (struct oh323_pvt *)data;
304 ast_mutex_lock(&pvt->lock);
305 /* Don't hold pvt lock while trying to lock the channel */
306 while(pvt->owner && ast_channel_trylock(pvt->owner)) {
307 DEADLOCK_AVOIDANCE(&pvt->lock);
311 struct ast_frame f = {
312 .frametype = AST_FRAME_DTMF_END,
313 .subclass = pvt->curDTMF,
315 .src = "SIMULATE_DTMF_END",
317 ast_queue_frame(pvt->owner, &f);
318 ast_channel_unlock(pvt->owner);
322 ast_mutex_unlock(&pvt->lock);
328 /*! \brief Channel and private structures should be already locked */
329 static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
331 if (c->nativeformats != pvt->nativeformats) {
333 ast_debug(1, "Preparing %s for new native format\n", c->name);
334 c->nativeformats = pvt->nativeformats;
335 ast_set_read_format(c, c->readformat);
336 ast_set_write_format(c, c->writeformat);
338 if (pvt->needhangup) {
340 ast_debug(1, "Process pending hangup for %s\n", c->name);
341 c->_softhangup |= AST_SOFTHANGUP_DEV;
342 c->hangupcause = pvt->hangupcause;
343 ast_queue_hangup_with_cause(c, pvt->hangupcause);
345 pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->DTMFsched = -1;
347 if (pvt->newstate >= 0) {
348 ast_setstate(c, pvt->newstate);
351 if (pvt->newcontrol >= 0) {
352 ast_queue_control(c, pvt->newcontrol);
353 pvt->newcontrol = -1;
355 if (pvt->newdigit >= 0) {
356 struct ast_frame f = {
357 .frametype = AST_FRAME_DTMF_END,
358 .subclass = pvt->newdigit,
359 .samples = pvt->newduration * 8,
360 .len = pvt->newduration,
361 .src = "UPDATE_INFO",
363 if (pvt->newdigit == ' ') { /* signalUpdate message */
364 f.subclass = pvt->curDTMF;
365 if (pvt->DTMFsched >= 0) {
366 AST_SCHED_DEL(sched, pvt->DTMFsched);
368 } else { /* Regular input or signal message */
369 if (pvt->newduration) { /* This is a signal, signalUpdate follows */
370 f.frametype = AST_FRAME_DTMF_BEGIN;
371 AST_SCHED_DEL(sched, pvt->DTMFsched);
372 pvt->DTMFsched = ast_sched_add(sched, pvt->newduration, oh323_simulate_dtmf_end, pvt);
374 ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", pvt->newduration, pvt->DTMFsched);
376 pvt->curDTMF = pvt->newdigit;
378 ast_queue_frame(c, &f);
381 if (pvt->update_rtp_info > 0) {
383 ast_jb_configure(c, &global_jbconf);
384 ast_channel_set_fd(c, 0, ast_rtp_fd(pvt->rtp));
385 ast_channel_set_fd(c, 1, ast_rtcp_fd(pvt->rtp));
386 ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
388 pvt->update_rtp_info = -1;
392 /*! \brief Only channel structure should be locked */
393 static void oh323_update_info(struct ast_channel *c)
395 struct oh323_pvt *pvt = c->tech_pvt;
398 ast_mutex_lock(&pvt->lock);
399 __oh323_update_info(c, pvt);
400 ast_mutex_unlock(&pvt->lock);
404 static void cleanup_call_details(call_details_t *cd)
406 if (cd->call_token) {
407 ast_free(cd->call_token);
408 cd->call_token = NULL;
410 if (cd->call_source_aliases) {
411 ast_free(cd->call_source_aliases);
412 cd->call_source_aliases = NULL;
414 if (cd->call_dest_alias) {
415 ast_free(cd->call_dest_alias);
416 cd->call_dest_alias = NULL;
418 if (cd->call_source_name) {
419 ast_free(cd->call_source_name);
420 cd->call_source_name = NULL;
422 if (cd->call_source_e164) {
423 ast_free(cd->call_source_e164);
424 cd->call_source_e164 = NULL;
426 if (cd->call_dest_e164) {
427 ast_free(cd->call_dest_e164);
428 cd->call_dest_e164 = NULL;
431 ast_free(cd->sourceIp);
434 if (cd->redirect_number) {
435 ast_free(cd->redirect_number);
436 cd->redirect_number = NULL;
440 static void __oh323_destroy(struct oh323_pvt *pvt)
442 struct oh323_pvt *cur, *prev = NULL;
444 AST_SCHED_DEL(sched, pvt->DTMFsched);
447 ast_rtp_destroy(pvt->rtp);
450 /* Free dsp used for in-band DTMF detection */
452 ast_dsp_free(pvt->vad);
454 cleanup_call_details(&pvt->cd);
456 /* Unlink us from the owner if we have one */
458 ast_channel_lock(pvt->owner);
460 ast_debug(1, "Detaching from %s\n", pvt->owner->name);
461 pvt->owner->tech_pvt = NULL;
462 ast_channel_unlock(pvt->owner);
468 prev->next = cur->next;
477 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
479 ast_mutex_unlock(&pvt->lock);
480 ast_mutex_destroy(&pvt->lock);
485 static void oh323_destroy(struct oh323_pvt *pvt)
488 ast_debug(1, "Destroying channel %s\n", (pvt->owner ? pvt->owner->name : "<unknown>"));
490 ast_mutex_lock(&iflock);
491 ast_mutex_lock(&pvt->lock);
492 __oh323_destroy(pvt);
493 ast_mutex_unlock(&iflock);
496 static int oh323_digit_begin(struct ast_channel *c, char digit)
498 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
502 ast_log(LOG_ERROR, "No private structure?! This is bad\n");
505 ast_mutex_lock(&pvt->lock);
507 (((pvt->options.dtmfmode & H323_DTMF_RFC2833) && pvt->dtmf_pt[0])
508 /*|| ((pvt->options.dtmfmode & H323_DTMF_CISCO) && pvt->dtmf_pt[1]))*/)) {
509 /* out-of-band DTMF */
511 ast_log(LOG_DTMF, "Begin sending out-of-band digit %c on %s\n", digit, c->name);
513 ast_rtp_senddigit_begin(pvt->rtp, digit);
514 ast_mutex_unlock(&pvt->lock);
515 } else if (pvt->txDtmfDigit != digit) {
518 ast_log(LOG_DTMF, "Begin sending inband digit %c on %s\n", digit, c->name);
520 pvt->txDtmfDigit = digit;
521 token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
522 ast_mutex_unlock(&pvt->lock);
523 h323_send_tone(token, digit);
528 ast_mutex_unlock(&pvt->lock);
529 oh323_update_info(c);
534 * Send (play) the specified digit to the channel.
537 static int oh323_digit_end(struct ast_channel *c, char digit, unsigned int duration)
539 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
543 ast_log(LOG_ERROR, "No private structure?! This is bad\n");
546 ast_mutex_lock(&pvt->lock);
547 if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833) && ((pvt->dtmf_pt[0] > 0) || (pvt->dtmf_pt[0] > 0))) {
548 /* out-of-band DTMF */
550 ast_log(LOG_DTMF, "End sending out-of-band digit %c on %s, duration %d\n", digit, c->name, duration);
552 ast_rtp_senddigit_end(pvt->rtp, digit);
553 ast_mutex_unlock(&pvt->lock);
557 ast_log(LOG_DTMF, "End sending inband digit %c on %s, duration %d\n", digit, c->name, duration);
559 pvt->txDtmfDigit = ' ';
560 token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
561 ast_mutex_unlock(&pvt->lock);
562 h323_send_tone(token, ' ');
567 oh323_update_info(c);
572 * Make a call over the specified channel to the specified
574 * Returns -1 on error, 0 on success.
576 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
579 struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
581 char called_addr[1024];
584 ast_debug(1, "Calling to %s on %s\n", dest, c->name);
586 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
587 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
590 ast_mutex_lock(&pvt->lock);
591 if (!gatekeeper_disable) {
592 if (ast_strlen_zero(pvt->exten)) {
593 ast_copy_string(called_addr, dest, sizeof(called_addr));
595 snprintf(called_addr, sizeof(called_addr), "%s@%s", pvt->exten, dest);
598 res = htons(pvt->sa.sin_port);
599 addr = ast_inet_ntoa(pvt->sa.sin_addr);
600 if (ast_strlen_zero(pvt->exten)) {
601 snprintf(called_addr, sizeof(called_addr), "%s:%d", addr, res);
603 snprintf(called_addr, sizeof(called_addr), "%s@%s:%d", pvt->exten, addr, res);
606 /* make sure null terminated */
607 called_addr[sizeof(called_addr) - 1] = '\0';
610 ast_copy_string(pvt->options.cid_num, c->cid.cid_num, sizeof(pvt->options.cid_num));
613 ast_copy_string(pvt->options.cid_name, c->cid.cid_name, sizeof(pvt->options.cid_name));
615 if (c->cid.cid_rdnis) {
616 ast_copy_string(pvt->options.cid_rdnis, c->cid.cid_rdnis, sizeof(pvt->options.cid_rdnis));
619 pvt->options.presentation = c->cid.cid_pres;
620 pvt->options.type_of_number = c->cid.cid_ton;
622 if ((addr = pbx_builtin_getvar_helper(c, "PRIREDIRECTREASON"))) {
623 if (!strcasecmp(addr, "UNKNOWN"))
624 pvt->options.redirect_reason = 0;
625 else if (!strcasecmp(addr, "BUSY"))
626 pvt->options.redirect_reason = 1;
627 else if (!strcasecmp(addr, "NO_REPLY"))
628 pvt->options.redirect_reason = 2;
629 else if (!strcasecmp(addr, "UNCONDITIONAL"))
630 pvt->options.redirect_reason = 15;
632 pvt->options.redirect_reason = -1;
634 pvt->options.redirect_reason = -1;
636 pvt->options.transfer_capability = c->transfercapability;
638 /* indicate that this is an outgoing call */
641 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", c->transfercapability, ast_transfercapability2str(c->transfercapability));
643 ast_debug(1, "Placing outgoing call to %s, %d/%d\n", called_addr, pvt->options.dtmfcodec[0], pvt->options.dtmfcodec[1]);
644 ast_mutex_unlock(&pvt->lock);
645 res = h323_make_call(called_addr, &(pvt->cd), &pvt->options);
647 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
650 oh323_update_info(c);
654 static int oh323_answer(struct ast_channel *c)
657 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
661 ast_debug(1, "Answering on %s\n", c->name);
663 ast_mutex_lock(&pvt->lock);
664 token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
665 ast_mutex_unlock(&pvt->lock);
666 res = h323_answering_call(token, 0);
670 oh323_update_info(c);
671 if (c->_state != AST_STATE_UP) {
672 ast_setstate(c, AST_STATE_UP);
677 static int oh323_hangup(struct ast_channel *c)
679 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
680 int q931cause = AST_CAUSE_NORMAL_CLEARING;
685 ast_debug(1, "Hanging up and scheduling destroy of call %s\n", c->name);
688 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
691 ast_mutex_lock(&pvt->lock);
692 /* Determine how to disconnect */
693 if (pvt->owner != c) {
694 ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
695 ast_mutex_unlock(&pvt->lock);
702 if (c->hangupcause) {
703 q931cause = c->hangupcause;
705 const char *cause = pbx_builtin_getvar_helper(c, "DIALSTATUS");
707 if (!strcmp(cause, "CONGESTION")) {
708 q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
709 } else if (!strcmp(cause, "BUSY")) {
710 q931cause = AST_CAUSE_USER_BUSY;
711 } else if (!strcmp(cause, "CHANISUNVAIL")) {
712 q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
713 } else if (!strcmp(cause, "NOANSWER")) {
714 q931cause = AST_CAUSE_NO_ANSWER;
715 } else if (!strcmp(cause, "CANCEL")) {
716 q931cause = AST_CAUSE_CALL_REJECTED;
721 /* Start the process if it's not already started */
722 if (!pvt->alreadygone && !pvt->hangupcause) {
723 call_token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
725 /* Release lock to eliminate deadlock */
726 ast_mutex_unlock(&pvt->lock);
727 if (h323_clear_call(call_token, q931cause)) {
728 ast_log(LOG_WARNING, "ClearCall failed.\n");
730 ast_free(call_token);
731 ast_mutex_lock(&pvt->lock);
734 pvt->needdestroy = 1;
735 ast_mutex_unlock(&pvt->lock);
737 /* Update usage counter */
738 ast_module_unref(ast_module_info->self);
743 /*! \brief Retrieve audio/etc from channel. Assumes pvt->lock is already held. */
744 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *pvt)
748 /* Only apply it for the first packet, we just need the correct ip/port */
749 if (pvt->options.nat) {
750 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
751 pvt->options.nat = 0;
754 f = ast_rtp_read(pvt->rtp);
755 /* Don't send RFC2833 if we're not supposed to */
756 if (f && (f->frametype == AST_FRAME_DTMF) && !(pvt->options.dtmfmode & (H323_DTMF_RFC2833 | H323_DTMF_CISCO))) {
757 return &ast_null_frame;
760 /* We already hold the channel lock */
761 if (f->frametype == AST_FRAME_VOICE) {
762 if (f->subclass != pvt->owner->nativeformats) {
763 /* Try to avoid deadlock */
764 if (ast_channel_trylock(pvt->owner)) {
765 ast_log(LOG_NOTICE, "Format changed but channel is locked. Ignoring frame...\n");
766 return &ast_null_frame;
769 ast_debug(1, "Oooh, format changed to %d\n", f->subclass);
770 pvt->owner->nativeformats = f->subclass;
771 pvt->nativeformats = f->subclass;
772 ast_set_read_format(pvt->owner, pvt->owner->readformat);
773 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
774 ast_channel_unlock(pvt->owner);
776 /* Do in-band DTMF detection */
777 if ((pvt->options.dtmfmode & H323_DTMF_INBAND) && pvt->vad) {
778 if ((pvt->nativeformats & (AST_FORMAT_SLINEAR | AST_FORMAT_ALAW | AST_FORMAT_ULAW))) {
779 if (!ast_channel_trylock(pvt->owner)) {
780 f = ast_dsp_process(pvt->owner, pvt->vad, f);
781 ast_channel_unlock(pvt->owner);
784 ast_log(LOG_NOTICE, "Unable to process inband DTMF while channel is locked\n");
785 } else if (pvt->nativeformats && !pvt->noInbandDtmf) {
786 ast_log(LOG_NOTICE, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(f->subclass));
787 pvt->noInbandDtmf = 1;
789 if (f &&(f->frametype == AST_FRAME_DTMF)) {
791 ast_log(LOG_DTMF, "Received in-band digit %c.\n", f->subclass);
799 static struct ast_frame *oh323_read(struct ast_channel *c)
801 struct ast_frame *fr;
802 struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
803 ast_mutex_lock(&pvt->lock);
804 __oh323_update_info(c, pvt);
807 fr = oh323_rtp_read(pvt);
811 fr = ast_rtcp_read(pvt->rtp);
813 fr = &ast_null_frame;
816 ast_log(LOG_ERROR, "Unable to handle fd %d on channel %s\n", c->fdno, c->name);
817 fr = &ast_null_frame;
820 ast_mutex_unlock(&pvt->lock);
824 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
826 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
828 if (frame->frametype != AST_FRAME_VOICE) {
829 if (frame->frametype == AST_FRAME_IMAGE) {
832 ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
836 if (!(frame->subclass & c->nativeformats)) {
837 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
838 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
843 ast_mutex_lock(&pvt->lock);
844 if (pvt->rtp && !pvt->recvonly)
845 res = ast_rtp_write(pvt->rtp, frame);
846 __oh323_update_info(c, pvt);
847 ast_mutex_unlock(&pvt->lock);
852 static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen)
855 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
856 char *token = (char *)NULL;
860 ast_mutex_lock(&pvt->lock);
861 token = (pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL);
862 got_progress = pvt->got_progress;
863 if (condition == AST_CONTROL_PROGRESS)
864 pvt->got_progress = 1;
865 else if ((condition == AST_CONTROL_BUSY) || (condition == AST_CONTROL_CONGESTION))
866 pvt->alreadygone = 1;
867 ast_mutex_unlock(&pvt->lock);
870 ast_debug(1, "OH323: Indicating %d on %s (%s)\n", condition, token, c->name);
873 case AST_CONTROL_RINGING:
874 if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
875 h323_send_alerting(token);
876 res = (got_progress ? 0 : -1); /* Do not simulate any audio tones if we got PROGRESS message */
879 case AST_CONTROL_PROGRESS:
880 if (c->_state != AST_STATE_UP) {
881 /* Do not send PROGRESS message more than once */
883 h323_send_progress(token);
887 case AST_CONTROL_BUSY:
888 if (c->_state != AST_STATE_UP) {
889 h323_answering_call(token, 1);
890 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
894 case AST_CONTROL_CONGESTION:
895 if (c->_state != AST_STATE_UP) {
896 h323_answering_call(token, 1);
897 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
901 case AST_CONTROL_HOLD:
902 h323_hold_call(token, 1);
903 /* We should start MOH only if remote party isn't provide audio for us */
904 ast_moh_start(c, data, NULL);
907 case AST_CONTROL_UNHOLD:
908 h323_hold_call(token, 0);
912 case AST_CONTROL_SRCUPDATE:
913 ast_rtp_new_source(pvt->rtp);
916 case AST_CONTROL_PROCEEDING:
920 ast_log(LOG_WARNING, "OH323: Don't know how to indicate condition %d on %s\n", condition, token);
925 ast_debug(1, "OH323: Indicated %d on %s, res=%d\n", condition, token, res);
928 oh323_update_info(c);
933 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
935 struct oh323_pvt *pvt = (struct oh323_pvt *) newchan->tech_pvt;
937 ast_mutex_lock(&pvt->lock);
938 if (pvt->owner != oldchan) {
939 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, pvt->owner);
942 pvt->owner = newchan;
943 ast_mutex_unlock(&pvt->lock);
947 static int __oh323_rtp_create(struct oh323_pvt *pvt)
949 struct in_addr our_addr;
954 if (ast_find_ourip(&our_addr, bindaddr)) {
955 ast_mutex_unlock(&pvt->lock);
956 ast_log(LOG_ERROR, "Unable to locate local IP address for RTP stream\n");
959 pvt->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, our_addr);
961 ast_mutex_unlock(&pvt->lock);
962 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
966 ast_debug(1, "Created RTP channel\n");
968 ast_rtp_setqos(pvt->rtp, tos, cos, "H323 RTP");
971 ast_debug(1, "Setting NAT on RTP to %d\n", pvt->options.nat);
972 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
974 if (pvt->dtmf_pt[0] > 0)
975 ast_rtp_set_rtpmap_type(pvt->rtp, pvt->dtmf_pt[0], "audio", "telephone-event", 0);
976 if (pvt->dtmf_pt[1] > 0)
977 ast_rtp_set_rtpmap_type(pvt->rtp, pvt->dtmf_pt[1], "audio", "cisco-telephone-event", 0);
979 if (pvt->peercapability)
980 ast_rtp_codec_setpref(pvt->rtp, &pvt->peer_prefs);
982 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
983 ast_jb_configure(pvt->owner, &global_jbconf);
984 ast_channel_set_fd(pvt->owner, 0, ast_rtp_fd(pvt->rtp));
985 ast_channel_set_fd(pvt->owner, 1, ast_rtcp_fd(pvt->rtp));
986 ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
987 ast_channel_unlock(pvt->owner);
989 pvt->update_rtp_info = 1;
994 /*! \brief Private structure should be locked on a call */
995 static struct ast_channel *__oh323_new(struct oh323_pvt *pvt, int state, const char *host)
997 struct ast_channel *ch;
998 char *cid_num, *cid_name;
1001 if (!ast_strlen_zero(pvt->options.cid_num))
1002 cid_num = pvt->options.cid_num;
1004 cid_num = pvt->cd.call_source_e164;
1006 if (!ast_strlen_zero(pvt->options.cid_name))
1007 cid_name = pvt->options.cid_name;
1009 cid_name = pvt->cd.call_source_name;
1011 /* Don't hold a oh323_pvt lock while we allocate a chanel */
1012 ast_mutex_unlock(&pvt->lock);
1013 ch = ast_channel_alloc(1, state, cid_num, cid_name, pvt->accountcode, pvt->exten, pvt->context, pvt->amaflags, "H323/%s", host);
1014 /* Update usage counter */
1015 ast_module_ref(ast_module_info->self);
1016 ast_mutex_lock(&pvt->lock);
1018 ch->tech = &oh323_tech;
1019 if (!(fmt = pvt->jointcapability) && !(fmt = pvt->options.capability))
1020 fmt = global_options.capability;
1021 ch->nativeformats = ast_codec_choose(&pvt->options.prefs, fmt, 1)/* | (pvt->jointcapability & AST_FORMAT_VIDEO_MASK)*/;
1022 pvt->nativeformats = ch->nativeformats;
1023 fmt = ast_best_codec(ch->nativeformats);
1024 ch->writeformat = fmt;
1025 ch->rawwriteformat = fmt;
1026 ch->readformat = fmt;
1027 ch->rawreadformat = fmt;
1029 __oh323_rtp_create(pvt);
1031 ast_channel_set_fd(ch, 0, ast_rtp_fd(pvt->rtp));
1032 ast_channel_set_fd(ch, 1, ast_rtcp_fd(pvt->rtp));
1034 #ifdef VIDEO_SUPPORT
1036 ast_channel_set_fd(ch, 2, ast_rtp_fd(pvt->vrtp));
1037 ast_channel_set_fd(ch, 3, ast_rtcp_fd(pvt->vrtp));
1042 ast_channel_set_fd(ch, 4, ast_udptl_fd(pvt->udptl));
1045 if (state == AST_STATE_RING) {
1048 /* Allocate dsp for in-band DTMF support */
1049 if (pvt->options.dtmfmode & H323_DTMF_INBAND) {
1050 pvt->vad = ast_dsp_new();
1051 ast_dsp_set_features(pvt->vad, DSP_FEATURE_DIGIT_DETECT);
1053 /* Register channel functions. */
1055 /* Set the owner of this channel */
1058 ast_copy_string(ch->context, pvt->context, sizeof(ch->context));
1059 ast_copy_string(ch->exten, pvt->exten, sizeof(ch->exten));
1061 if (!ast_strlen_zero(pvt->accountcode)) {
1062 ast_string_field_set(ch, accountcode, pvt->accountcode);
1064 if (pvt->amaflags) {
1065 ch->amaflags = pvt->amaflags;
1068 /* Don't use ast_set_callerid() here because it will
1069 * generate a needless NewCallerID event */
1070 ch->cid.cid_ani = ast_strdup(cid_num);
1072 if (pvt->cd.redirect_reason >= 0) {
1073 ch->cid.cid_rdnis = ast_strdup(pvt->cd.redirect_number);
1074 pbx_builtin_setvar_helper(ch, "PRIREDIRECTREASON", redirectingreason2str(pvt->cd.redirect_reason));
1076 ch->cid.cid_pres = pvt->cd.presentation;
1077 ch->cid.cid_ton = pvt->cd.type_of_number;
1079 if (!ast_strlen_zero(pvt->exten) && strcmp(pvt->exten, "s")) {
1080 ch->cid.cid_dnid = ast_strdup(pvt->exten);
1082 if (pvt->cd.transfer_capability >= 0)
1083 ch->transfercapability = pvt->cd.transfer_capability;
1084 if (state != AST_STATE_DOWN) {
1085 if (ast_pbx_start(ch)) {
1086 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
1092 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1097 static struct oh323_pvt *oh323_alloc(int callid)
1099 struct oh323_pvt *pvt;
1101 pvt = ast_calloc(1, sizeof(*pvt));
1103 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
1106 pvt->cd.redirect_reason = -1;
1107 pvt->cd.transfer_capability = -1;
1108 /* Ensure the call token is allocated for outgoing call */
1110 if ((pvt->cd).call_token == NULL) {
1111 (pvt->cd).call_token = ast_calloc(1, 128);
1113 if (!pvt->cd.call_token) {
1114 ast_log(LOG_ERROR, "Not enough memory to alocate call token\n");
1115 ast_rtp_destroy(pvt->rtp);
1119 memset((char *)(pvt->cd).call_token, 0, 128);
1120 pvt->cd.call_reference = callid;
1122 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
1123 pvt->jointcapability = pvt->options.capability;
1124 if (pvt->options.dtmfmode & (H323_DTMF_RFC2833 | H323_DTMF_CISCO)) {
1125 pvt->nonCodecCapability |= AST_RTP_DTMF;
1127 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1129 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
1130 pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->update_rtp_info = pvt->DTMFsched = -1;
1131 ast_mutex_init(&pvt->lock);
1132 /* Add to interface list */
1133 ast_mutex_lock(&iflock);
1136 ast_mutex_unlock(&iflock);
1140 static struct oh323_pvt *find_call_locked(int call_reference, const char *token)
1142 struct oh323_pvt *pvt;
1144 ast_mutex_lock(&iflock);
1147 if (!pvt->needdestroy && ((signed int)pvt->cd.call_reference == call_reference)) {
1148 /* Found the call */
1149 if ((token != NULL) && (pvt->cd.call_token != NULL) && (!strcmp(pvt->cd.call_token, token))) {
1150 ast_mutex_lock(&pvt->lock);
1151 ast_mutex_unlock(&iflock);
1153 } else if (token == NULL) {
1154 ast_log(LOG_WARNING, "Call Token is NULL\n");
1155 ast_mutex_lock(&pvt->lock);
1156 ast_mutex_unlock(&iflock);
1162 ast_mutex_unlock(&iflock);
1166 static int update_state(struct oh323_pvt *pvt, int state, int signal)
1170 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1172 ast_setstate(pvt->owner, state);
1174 ast_queue_control(pvt->owner, signal);
1175 ast_channel_unlock(pvt->owner);
1180 pvt->newstate = state;
1182 pvt->newcontrol = signal;
1187 static struct oh323_alias *build_alias(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1189 struct oh323_alias *alias;
1192 alias = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&aliasl, name, name, 0, 0, strcasecmp);
1197 if (!(alias = ast_calloc(1, sizeof(*alias))))
1202 ast_copy_string(alias->name, name, sizeof(alias->name));
1203 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1204 if (!strcasecmp(v->name, "e164")) {
1205 ast_copy_string(alias->e164, v->value, sizeof(alias->e164));
1206 } else if (!strcasecmp(v->name, "prefix")) {
1207 ast_copy_string(alias->prefix, v->value, sizeof(alias->prefix));
1208 } else if (!strcasecmp(v->name, "context")) {
1209 ast_copy_string(alias->context, v->value, sizeof(alias->context));
1210 } else if (!strcasecmp(v->name, "secret")) {
1211 ast_copy_string(alias->secret, v->value, sizeof(alias->secret));
1213 if (strcasecmp(v->value, "h323")) {
1214 ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->name);
1218 ASTOBJ_UNMARK(alias);
1222 static struct oh323_alias *realtime_alias(const char *alias)
1224 struct ast_variable *var, *tmp;
1225 struct oh323_alias *a;
1227 var = ast_load_realtime("h323", "name", alias, SENTINEL);
1232 for (tmp = var; tmp; tmp = tmp->next) {
1233 if (!strcasecmp(tmp->name, "type") &&
1234 !(!strcasecmp(tmp->value, "alias") || !strcasecmp(tmp->value, "h323"))) {
1235 ast_variables_destroy(var);
1240 a = build_alias(alias, var, NULL, 1);
1242 ast_variables_destroy(var);
1247 static int update_common_options(struct ast_variable *v, struct call_options *options)
1252 if (!strcasecmp(v->name, "allow")) {
1253 ast_parse_allow_disallow(&options->prefs, &options->capability, v->value, 1);
1254 } else if (!strcasecmp(v->name, "disallow")) {
1255 ast_parse_allow_disallow(&options->prefs, &options->capability, v->value, 0);
1256 } else if (!strcasecmp(v->name, "dtmfmode")) {
1257 val = ast_strdupa(v->value);
1258 if ((opt = strchr(val, ':')) != (char *)NULL) {
1262 if (!strcasecmp(v->value, "inband")) {
1263 options->dtmfmode |= H323_DTMF_INBAND;
1264 } else if (!strcasecmp(val, "rfc2833")) {
1265 options->dtmfmode |= H323_DTMF_RFC2833;
1267 options->dtmfcodec[0] = H323_DTMF_RFC2833_PT;
1268 } else if ((tmp >= 96) && (tmp < 128)) {
1269 options->dtmfcodec[0] = tmp;
1271 options->dtmfcodec[0] = H323_DTMF_RFC2833_PT;
1272 ast_log(LOG_WARNING, "Unknown rfc2833 payload %s specified at line %d, using default %d\n", opt, v->lineno, options->dtmfcodec[0]);
1274 } else if (!strcasecmp(val, "cisco")) {
1275 options->dtmfmode |= H323_DTMF_CISCO;
1277 options->dtmfcodec[1] = H323_DTMF_CISCO_PT;
1278 } else if ((tmp >= 96) && (tmp < 128)) {
1279 options->dtmfcodec[1] = tmp;
1281 options->dtmfcodec[1] = H323_DTMF_CISCO_PT;
1282 ast_log(LOG_WARNING, "Unknown Cisco DTMF payload %s specified at line %d, using default %d\n", opt, v->lineno, options->dtmfcodec[1]);
1284 } else if (!strcasecmp(v->value, "h245-signal")) {
1285 options->dtmfmode |= H323_DTMF_SIGNAL;
1287 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' at line %d\n", v->value, v->lineno);
1289 } else if (!strcasecmp(v->name, "dtmfcodec")) {
1290 ast_log(LOG_NOTICE, "Option %s at line %d is deprecated. Use dtmfmode=rfc2833[:<payload>] instead.\n", v->name, v->lineno);
1291 tmp = atoi(v->value);
1293 ast_log(LOG_WARNING, "Invalid %s value %s at line %d\n", v->name, v->value, v->lineno);
1295 options->dtmfcodec[0] = tmp;
1296 } else if (!strcasecmp(v->name, "bridge")) {
1297 options->bridge = ast_true(v->value);
1298 } else if (!strcasecmp(v->name, "nat")) {
1299 options->nat = ast_true(v->value);
1300 } else if (!strcasecmp(v->name, "fastStart")) {
1301 options->fastStart = ast_true(v->value);
1302 } else if (!strcasecmp(v->name, "h245Tunneling")) {
1303 options->h245Tunneling = ast_true(v->value);
1304 } else if (!strcasecmp(v->name, "silenceSuppression")) {
1305 options->silenceSuppression = ast_true(v->value);
1306 } else if (!strcasecmp(v->name, "progress_setup")) {
1307 tmp = atoi(v->value);
1308 if ((tmp != 0) && (tmp != 1) && (tmp != 3) && (tmp != 8)) {
1309 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d, assuming 0\n", v->value, v->name, v->lineno);
1312 options->progress_setup = tmp;
1313 } else if (!strcasecmp(v->name, "progress_alert")) {
1314 tmp = atoi(v->value);
1315 if ((tmp != 0) && (tmp != 1) && (tmp != 8)) {
1316 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d, assuming 0\n", v->value, v->name, v->lineno);
1319 options->progress_alert = tmp;
1320 } else if (!strcasecmp(v->name, "progress_audio")) {
1321 options->progress_audio = ast_true(v->value);
1322 } else if (!strcasecmp(v->name, "callerid")) {
1323 ast_callerid_split(v->value, options->cid_name, sizeof(options->cid_name), options->cid_num, sizeof(options->cid_num));
1324 } else if (!strcasecmp(v->name, "fullname")) {
1325 ast_copy_string(options->cid_name, v->value, sizeof(options->cid_name));
1326 } else if (!strcasecmp(v->name, "cid_number")) {
1327 ast_copy_string(options->cid_num, v->value, sizeof(options->cid_num));
1328 } else if (!strcasecmp(v->name, "tunneling")) {
1329 if (!strcasecmp(v->value, "none"))
1330 options->tunnelOptions = 0;
1331 else if (!strcasecmp(v->value, "cisco"))
1332 options->tunnelOptions |= H323_TUNNEL_CISCO;
1333 else if (!strcasecmp(v->value, "qsig"))
1334 options->tunnelOptions |= H323_TUNNEL_QSIG;
1336 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d\n", v->value, v->name, v->lineno);
1337 } else if (!strcasecmp(v->name, "hold")) {
1338 if (!strcasecmp(v->value, "none"))
1339 options->holdHandling = ~0;
1340 else if (!strcasecmp(v->value, "notify"))
1341 options->holdHandling |= H323_HOLD_NOTIFY;
1342 else if (!strcasecmp(v->value, "q931only"))
1343 options->holdHandling |= H323_HOLD_NOTIFY | H323_HOLD_Q931ONLY;
1344 else if (!strcasecmp(v->value, "h450"))
1345 options->holdHandling |= H323_HOLD_H450;
1347 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d\n", v->value, v->name, v->lineno);
1354 static struct oh323_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1356 struct oh323_user *user;
1357 struct ast_ha *oldha;
1361 user = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&userl, name, name, 0, 0, strcmp);
1366 if (!(user = ast_calloc(1, sizeof(*user))))
1371 user->ha = (struct ast_ha *)NULL;
1372 memcpy(&user->options, &global_options, sizeof(user->options));
1373 user->options.dtmfmode = 0;
1374 user->options.holdHandling = 0;
1375 /* Set default context */
1376 ast_copy_string(user->context, default_context, sizeof(user->context));
1378 ast_copy_string(user->name, name, sizeof(user->name));
1380 #if 0 /* XXX Port channel variables functionality from chan_sip XXX */
1381 if (user->chanvars) {
1382 ast_variables_destroy(user->chanvars);
1383 user->chanvars = NULL;
1387 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1388 if (!update_common_options(v, &user->options))
1390 if (!strcasecmp(v->name, "context")) {
1391 ast_copy_string(user->context, v->value, sizeof(user->context));
1392 } else if (!strcasecmp(v->name, "secret")) {
1393 ast_copy_string(user->secret, v->value, sizeof(user->secret));
1394 } else if (!strcasecmp(v->name, "accountcode")) {
1395 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
1396 } else if (!strcasecmp(v->name, "host")) {
1397 if (!strcasecmp(v->value, "dynamic")) {
1398 ast_log(LOG_ERROR, "A dynamic host on a type=user does not make any sense\n");
1399 ASTOBJ_UNREF(user, oh323_destroy_user);
1401 } else if (ast_get_ip(&user->addr, v->value)) {
1402 ASTOBJ_UNREF(user, oh323_destroy_user);
1405 /* Let us know we need to use ip authentication */
1407 } else if (!strcasecmp(v->name, "amaflags")) {
1408 format = ast_cdr_amaflags2int(v->value);
1410 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
1412 user->amaflags = format;
1414 } else if (!strcasecmp(v->name, "permit") ||
1415 !strcasecmp(v->name, "deny")) {
1418 user->ha = ast_append_ha(v->name, v->value, user->ha, &ha_error);
1420 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
1423 if (!user->options.dtmfmode)
1424 user->options.dtmfmode = global_options.dtmfmode;
1425 if (user->options.holdHandling == ~0)
1426 user->options.holdHandling = 0;
1427 else if (!user->options.holdHandling)
1428 user->options.holdHandling = global_options.holdHandling;
1429 ASTOBJ_UNMARK(user);
1434 static struct oh323_user *realtime_user(const call_details_t *cd)
1436 struct ast_variable *var, *tmp;
1437 struct oh323_user *user;
1438 const char *username;
1441 var = ast_load_realtime("h323", "name", username = cd->call_source_aliases, SENTINEL);
1443 username = (char *)NULL;
1444 var = ast_load_realtime("h323", "host", cd->sourceIp, SENTINEL);
1450 for (tmp = var; tmp; tmp = tmp->next) {
1451 if (!strcasecmp(tmp->name, "type") &&
1452 !(!strcasecmp(tmp->value, "user") || !strcasecmp(tmp->value, "friend"))) {
1453 ast_variables_destroy(var);
1455 } else if (!username && !strcasecmp(tmp->name, "name"))
1456 username = tmp->value;
1460 ast_log(LOG_WARNING, "Cannot determine user name for IP address %s\n", cd->sourceIp);
1461 ast_variables_destroy(var);
1465 user = build_user(username, var, NULL, 1);
1467 ast_variables_destroy(var);
1472 static struct oh323_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1474 struct oh323_peer *peer;
1475 struct ast_ha *oldha;
1478 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
1483 if (!(peer = ast_calloc(1, sizeof(*peer))))
1489 memcpy(&peer->options, &global_options, sizeof(peer->options));
1490 peer->options.dtmfmode = 0;
1491 peer->options.holdHandling = 0;
1492 peer->addr.sin_port = htons(h323_signalling_port);
1493 peer->addr.sin_family = AF_INET;
1495 ast_copy_string(peer->name, name, sizeof(peer->name));
1497 #if 0 /* XXX Port channel variables functionality from chan_sip XXX */
1498 if (peer->chanvars) {
1499 ast_variables_destroy(peer->chanvars);
1500 peer->chanvars = NULL;
1503 /* Default settings for mailbox */
1504 peer->mailbox[0] = '\0';
1506 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1507 if (!update_common_options(v, &peer->options))
1509 if (!strcasecmp(v->name, "host")) {
1510 if (!strcasecmp(v->value, "dynamic")) {
1511 ast_log(LOG_ERROR, "Dynamic host configuration not implemented.\n");
1512 ASTOBJ_UNREF(peer, oh323_destroy_peer);
1515 if (ast_get_ip(&peer->addr, v->value)) {
1516 ast_log(LOG_ERROR, "Could not determine IP for %s\n", v->value);
1517 ASTOBJ_UNREF(peer, oh323_destroy_peer);
1520 } else if (!strcasecmp(v->name, "port")) {
1521 peer->addr.sin_port = htons(atoi(v->value));
1522 } else if (!strcasecmp(v->name, "permit") ||
1523 !strcasecmp(v->name, "deny")) {
1526 peer->ha = ast_append_ha(v->name, v->value, peer->ha, &ha_error);
1528 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
1529 } else if (!strcasecmp(v->name, "mailbox")) {
1530 ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
1531 } else if (!strcasecmp(v->name, "hasvoicemail")) {
1532 if (ast_true(v->value) && ast_strlen_zero(peer->mailbox)) {
1533 ast_copy_string(peer->mailbox, name, sizeof(peer->mailbox));
1537 if (!peer->options.dtmfmode)
1538 peer->options.dtmfmode = global_options.dtmfmode;
1539 if (peer->options.holdHandling == ~0)
1540 peer->options.holdHandling = 0;
1541 else if (!peer->options.holdHandling)
1542 peer->options.holdHandling = global_options.holdHandling;
1543 ASTOBJ_UNMARK(peer);
1548 static struct oh323_peer *realtime_peer(const char *peername, struct sockaddr_in *sin)
1550 struct oh323_peer *peer;
1551 struct ast_variable *var;
1552 struct ast_variable *tmp;
1553 const char *addr = NULL;
1555 /* First check on peer name */
1557 var = ast_load_realtime("h323", "name", peername, SENTINEL);
1558 else if (sin) /* Then check on IP address for dynamic peers */
1559 var = ast_load_realtime("h323", "host", addr = ast_inet_ntoa(sin->sin_addr), SENTINEL);
1566 for (tmp = var; tmp; tmp = tmp->next) {
1567 /* If this is type=user, then skip this object. */
1568 if (!strcasecmp(tmp->name, "type") &&
1569 !(!strcasecmp(tmp->value, "peer") || !strcasecmp(tmp->value, "friend"))) {
1570 ast_variables_destroy(var);
1572 } else if (!peername && !strcasecmp(tmp->name, "name")) {
1573 peername = tmp->value;
1577 if (!peername) { /* Did not find peer in realtime */
1578 ast_log(LOG_WARNING, "Cannot determine peer name for IP address %s\n", addr);
1579 ast_variables_destroy(var);
1583 /* Peer found in realtime, now build it in memory */
1584 peer = build_peer(peername, var, NULL, 1);
1586 ast_variables_destroy(var);
1591 static int oh323_addrcmp_str(struct in_addr inaddr, char *addr)
1593 return strcmp(ast_inet_ntoa(inaddr), addr);
1596 static struct oh323_user *find_user(const call_details_t *cd, int realtime)
1598 struct oh323_user *u;
1601 u = ASTOBJ_CONTAINER_FIND(&userl, cd->call_source_aliases);
1603 u = ASTOBJ_CONTAINER_FIND_FULL(&userl, cd->sourceIp, addr.sin_addr, 0, 0, oh323_addrcmp_str);
1606 u = realtime_user(cd);
1608 if (!u && h323debug)
1609 ast_debug(1, "Could not find user by name %s or address %s\n", cd->call_source_aliases, cd->sourceIp);
1614 static int oh323_addrcmp(struct sockaddr_in addr, struct sockaddr_in *sin)
1621 res = inaddrcmp(&addr , sin);
1626 static struct oh323_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime)
1628 struct oh323_peer *p;
1631 p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
1633 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, addr, 0, 0, oh323_addrcmp);
1636 p = realtime_peer(peer, sin);
1638 if (!p && h323debug)
1639 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>"));
1644 static int create_addr(struct oh323_pvt *pvt, char *opeer)
1647 struct ast_hostent ahp;
1648 struct oh323_peer *p;
1653 char peer[256] = "";
1655 ast_copy_string(peer, opeer, sizeof(peer));
1656 port = strchr(peer, ':');
1661 pvt->sa.sin_family = AF_INET;
1662 p = find_peer(peer, NULL, 1);
1665 memcpy(&pvt->options, &p->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;
1671 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1674 if (p->addr.sin_addr.s_addr) {
1675 pvt->sa.sin_addr = p->addr.sin_addr;
1676 pvt->sa.sin_port = p->addr.sin_port;
1678 ASTOBJ_UNREF(p, oh323_destroy_peer);
1683 portno = atoi(port);
1685 portno = h323_signalling_port;
1687 hp = ast_gethostbyname(hostn, &ahp);
1689 memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
1690 pvt->sa.sin_port = htons(portno);
1691 /* Look peer by address */
1692 p = find_peer(NULL, &pvt->sa, 1);
1693 memcpy(&pvt->options, (p ? &p->options : &global_options), sizeof(pvt->options));
1694 pvt->jointcapability = pvt->options.capability;
1696 ASTOBJ_UNREF(p, oh323_destroy_peer);
1698 if (pvt->options.dtmfmode) {
1699 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1700 pvt->nonCodecCapability |= AST_RTP_DTMF;
1702 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1707 ast_log(LOG_WARNING, "No such host: %s\n", peer);
1710 } else if (!found) {
1716 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause)
1719 struct oh323_pvt *pvt;
1720 struct ast_channel *tmpc = NULL;
1721 char *dest = (char *)data;
1723 char *h323id = NULL;
1724 char tmp[256], tmp1[256];
1727 ast_debug(1, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
1729 pvt = oh323_alloc(0);
1731 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
1735 format &= AST_FORMAT_AUDIO_MASK;
1737 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
1740 *cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
1743 ast_copy_string(tmp, dest, sizeof(tmp));
1744 host = strchr(tmp, '@');
1750 ext = strrchr(tmp, '/');
1755 strtok_r(host, "/", &(h323id));
1756 if (!ast_strlen_zero(h323id)) {
1757 h323_set_id(h323id);
1760 ast_copy_string(pvt->exten, ext, sizeof(pvt->exten));
1763 ast_debug(1, "Extension: %s Host: %s\n", pvt->exten, host);
1765 if (gatekeeper_disable) {
1766 if (create_addr(pvt, host)) {
1769 *cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
1774 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
1775 pvt->jointcapability = pvt->options.capability;
1776 if (pvt->options.dtmfmode) {
1777 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1778 pvt->nonCodecCapability |= AST_RTP_DTMF;
1780 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1785 ast_mutex_lock(&caplock);
1786 /* Generate unique channel identifier */
1787 snprintf(tmp1, sizeof(tmp1)-1, "%s-%u", host, ++unique);
1788 tmp1[sizeof(tmp1)-1] = '\0';
1789 ast_mutex_unlock(&caplock);
1791 ast_mutex_lock(&pvt->lock);
1792 tmpc = __oh323_new(pvt, AST_STATE_DOWN, tmp1);
1793 ast_mutex_unlock(&pvt->lock);
1797 *cause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
1799 ast_update_use_count();
1804 /*! \brief Find a call by alias */
1805 static struct oh323_alias *find_alias(const char *source_aliases, int realtime)
1807 struct oh323_alias *a;
1809 a = ASTOBJ_CONTAINER_FIND(&aliasl, source_aliases);
1812 a = realtime_alias(source_aliases);
1818 * Callback for sending digits from H.323 up to asterisk
1821 static int receive_digit(unsigned call_reference, char digit, const char *token, int duration)
1823 struct oh323_pvt *pvt;
1826 pvt = find_call_locked(call_reference, token);
1828 ast_log(LOG_ERROR, "Received digit '%c' (%u ms) for call %s without private structure\n", digit, duration, token);
1832 ast_log(LOG_DTMF, "Received %s digit '%c' (%u ms) for call %s\n", (digit == ' ' ? "update for" : "new"), (digit == ' ' ? pvt->curDTMF : digit), duration, token);
1834 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1836 res = ast_queue_control(pvt->owner, AST_CONTROL_FLASH);
1838 struct ast_frame f = {
1839 .frametype = AST_FRAME_DTMF_END,
1841 .samples = duration * 8,
1843 .src = "SEND_DIGIT",
1845 if (digit == ' ') { /* signalUpdate message */
1846 f.subclass = pvt->curDTMF;
1847 AST_SCHED_DEL(sched, pvt->DTMFsched);
1848 } else { /* Regular input or signal message */
1849 if (pvt->DTMFsched >= 0) {
1850 /* We still don't send DTMF END from previous event, send it now */
1851 AST_SCHED_DEL(sched, pvt->DTMFsched);
1852 f.subclass = pvt->curDTMF;
1853 f.samples = f.len = 0;
1854 ast_queue_frame(pvt->owner, &f);
1855 /* Restore values */
1857 f.samples = duration * 8;
1860 if (duration) { /* This is a signal, signalUpdate follows */
1861 f.frametype = AST_FRAME_DTMF_BEGIN;
1862 pvt->DTMFsched = ast_sched_add(sched, duration, oh323_simulate_dtmf_end, pvt);
1864 ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", duration, pvt->DTMFsched);
1866 pvt->curDTMF = digit;
1868 res = ast_queue_frame(pvt->owner, &f);
1870 ast_channel_unlock(pvt->owner);
1873 pvt->newcontrol = AST_CONTROL_FLASH;
1875 pvt->newduration = duration;
1876 pvt->newdigit = digit;
1880 ast_mutex_unlock(&pvt->lock);
1885 * Callback function used to inform the H.323 stack of the local rtp ip/port details
1887 * \return Returns the local RTP information
1889 static struct rtp_info *external_rtp_create(unsigned call_reference, const char * token)
1891 struct oh323_pvt *pvt;
1892 struct sockaddr_in us;
1893 struct rtp_info *info;
1895 info = ast_calloc(1, sizeof(*info));
1897 ast_log(LOG_ERROR, "Unable to allocated info structure, this is very bad\n");
1900 pvt = find_call_locked(call_reference, token);
1903 ast_log(LOG_ERROR, "Unable to find call %s(%d)\n", token, call_reference);
1907 __oh323_rtp_create(pvt);
1909 ast_mutex_unlock(&pvt->lock);
1911 ast_log(LOG_ERROR, "No RTP stream is available for call %s (%d)", token, call_reference);
1914 /* figure out our local RTP port and tell the H.323 stack about it */
1915 ast_rtp_get_us(pvt->rtp, &us);
1916 ast_mutex_unlock(&pvt->lock);
1918 ast_copy_string(info->addr, ast_inet_ntoa(us.sin_addr), sizeof(info->addr));
1919 info->port = ntohs(us.sin_port);
1921 ast_debug(1, "Sending RTP 'US' %s:%d\n", info->addr, info->port);
1926 * Definition taken from rtp.c for rtpPayloadType because we need it here.
1929 struct rtpPayloadType {
1930 int isAstFormat; /* whether the following code is an AST_FORMAT */
1935 * Call-back function passing remote ip/port information from H.323 to asterisk
1939 static void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort, const char *token, int pt)
1941 struct oh323_pvt *pvt;
1942 struct sockaddr_in them;
1943 struct rtpPayloadType rtptype;
1944 int nativeformats_changed;
1945 enum { NEED_NONE, NEED_HOLD, NEED_UNHOLD } rtp_change = NEED_NONE;
1948 ast_debug(1, "Setting up RTP connection for %s\n", token);
1950 /* Find the call or allocate a private structure if call not found */
1951 pvt = find_call_locked(call_reference, token);
1953 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1956 if (pvt->alreadygone) {
1957 ast_mutex_unlock(&pvt->lock);
1962 __oh323_rtp_create(pvt);
1964 if ((pt == 2) && (pvt->jointcapability & AST_FORMAT_G726_AAL2)) {
1965 ast_rtp_set_rtpmap_type(pvt->rtp, pt, "audio", "G726-32", AST_RTP_OPT_G726_NONSTANDARD);
1968 them.sin_family = AF_INET;
1969 /* only works for IPv4 */
1970 them.sin_addr.s_addr = inet_addr(remoteIp);
1971 them.sin_port = htons(remotePort);
1973 if (them.sin_addr.s_addr) {
1974 ast_rtp_set_peer(pvt->rtp, &them);
1975 if (pvt->recvonly) {
1977 rtp_change = NEED_UNHOLD;
1980 ast_rtp_stop(pvt->rtp);
1981 if (!pvt->recvonly) {
1983 rtp_change = NEED_HOLD;
1987 /* Change native format to reflect information taken from OLC/OLCAck */
1988 nativeformats_changed = 0;
1989 if (pt != 128 && pvt->rtp) { /* Payload type is invalid, so try to use previously decided */
1990 rtptype = ast_rtp_lookup_pt(pvt->rtp, pt);
1992 ast_debug(1, "Native format is set to %d from %d by RTP payload type %d\n", rtptype.code, pvt->nativeformats, pt);
1993 if (pvt->nativeformats != rtptype.code) {
1994 pvt->nativeformats = rtptype.code;
1995 nativeformats_changed = 1;
1997 } else if (h323debug)
1998 ast_log(LOG_NOTICE, "Payload type is unknown, formats isn't changed\n");
2000 /* Don't try to lock the channel if nothing changed */
2001 if (nativeformats_changed || pvt->options.progress_audio || (rtp_change != NEED_NONE)) {
2002 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
2003 /* Re-build translation path only if native format(s) has been changed */
2004 if (pvt->owner->nativeformats != pvt->nativeformats) {
2006 ast_debug(1, "Native format changed to %d from %d, read format is %d, write format is %d\n", pvt->nativeformats, pvt->owner->nativeformats, pvt->owner->readformat, pvt->owner->writeformat);
2007 pvt->owner->nativeformats = pvt->nativeformats;
2008 ast_set_read_format(pvt->owner, pvt->owner->readformat);
2009 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
2011 if (pvt->options.progress_audio)
2012 ast_queue_control(pvt->owner, AST_CONTROL_PROGRESS);
2013 switch (rtp_change) {
2015 ast_queue_control(pvt->owner, AST_CONTROL_HOLD);
2018 ast_queue_control(pvt->owner, AST_CONTROL_UNHOLD);
2023 ast_channel_unlock(pvt->owner);
2026 if (pvt->options.progress_audio)
2027 pvt->newcontrol = AST_CONTROL_PROGRESS;
2028 else if (rtp_change == NEED_HOLD)
2029 pvt->newcontrol = AST_CONTROL_HOLD;
2030 else if (rtp_change == NEED_UNHOLD)
2031 pvt->newcontrol = AST_CONTROL_UNHOLD;
2033 ast_debug(1, "RTP connection preparation for %s is pending...\n", token);
2036 ast_mutex_unlock(&pvt->lock);
2039 ast_debug(1, "RTP connection prepared for %s\n", token);
2045 * Call-back function to signal asterisk that the channel has been answered
2048 static void connection_made(unsigned call_reference, const char *token)
2050 struct oh323_pvt *pvt;
2053 ast_debug(1, "Call %s answered\n", token);
2055 pvt = find_call_locked(call_reference, token);
2057 ast_log(LOG_ERROR, "Something is wrong: connection\n");
2061 /* Inform asterisk about remote party connected only on outgoing calls */
2062 if (!pvt->outgoing) {
2063 ast_mutex_unlock(&pvt->lock);
2066 /* Do not send ANSWER message more than once */
2067 if (!pvt->connection_established) {
2068 pvt->connection_established = 1;
2069 update_state(pvt, -1, AST_CONTROL_ANSWER);
2071 ast_mutex_unlock(&pvt->lock);
2075 static int progress(unsigned call_reference, const char *token, int inband)
2077 struct oh323_pvt *pvt;
2080 ast_debug(1, "Received ALERT/PROGRESS message for %s tones\n", (inband ? "inband" : "self-generated"));
2082 pvt = find_call_locked(call_reference, token);
2084 ast_log(LOG_ERROR, "Private structure not found in progress.\n");
2088 ast_mutex_unlock(&pvt->lock);
2089 ast_log(LOG_ERROR, "No Asterisk channel associated with private structure.\n");
2092 update_state(pvt, -1, (inband ? AST_CONTROL_PROGRESS : AST_CONTROL_RINGING));
2093 ast_mutex_unlock(&pvt->lock);
2099 * Call-back function for incoming calls
2101 * Returns 1 on success
2103 static call_options_t *setup_incoming_call(call_details_t *cd)
2105 struct oh323_pvt *pvt;
2106 struct oh323_user *user = NULL;
2107 struct oh323_alias *alias = NULL;
2110 ast_debug(1, "Setting up incoming call for %s\n", cd->call_token);
2112 /* allocate the call*/
2113 pvt = oh323_alloc(cd->call_reference);
2116 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
2117 cleanup_call_details(cd);
2121 /* Populate the call details in the private structure */
2122 memcpy(&pvt->cd, cd, sizeof(pvt->cd));
2123 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
2124 pvt->jointcapability = pvt->options.capability;
2127 ast_verb(3, "Setting up Call\n");
2128 ast_verb(3, " \tCall token: [%s]\n", pvt->cd.call_token);
2129 ast_verb(3, " \tCalling party name: [%s]\n", pvt->cd.call_source_name);
2130 ast_verb(3, " \tCalling party number: [%s]\n", pvt->cd.call_source_e164);
2131 ast_verb(3, " \tCalled party name: [%s]\n", pvt->cd.call_dest_alias);
2132 ast_verb(3, " \tCalled party number: [%s]\n", pvt->cd.call_dest_e164);
2133 if (pvt->cd.redirect_reason >= 0)
2134 ast_verb(3, " \tRedirecting party number: [%s] (reason %d)\n", pvt->cd.redirect_number, pvt->cd.redirect_reason);
2135 ast_verb(3, " \tCalling party IP: [%s]\n", pvt->cd.sourceIp);
2138 /* Decide if we are allowing Gatekeeper routed calls*/
2139 if ((!strcasecmp(cd->sourceIp, gatekeeper)) && (gkroute == -1) && !gatekeeper_disable) {
2140 if (!ast_strlen_zero(cd->call_dest_e164)) {
2141 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2142 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2144 alias = find_alias(cd->call_dest_alias, 1);
2146 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd->call_dest_alias);
2150 ast_copy_string(pvt->exten, alias->name, sizeof(pvt->exten));
2151 ast_copy_string(pvt->context, alias->context, sizeof(pvt->context));
2154 /* Either this call is not from the Gatekeeper
2155 or we are not allowing gk routed calls */
2156 user = find_user(cd, 1);
2158 if (!acceptAnonymous) {
2159 ast_log(LOG_NOTICE, "Anonymous call from '%s@%s' rejected\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
2163 if (ast_strlen_zero(default_context)) {
2164 ast_log(LOG_ERROR, "Call from '%s@%s' rejected due to no default context\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
2168 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2169 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
2170 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2172 ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
2175 ast_debug(1, "Sending %s@%s to context [%s] extension %s\n", cd->call_source_aliases, cd->sourceIp, pvt->context, pvt->exten);
2178 if (strcasecmp(cd->sourceIp, ast_inet_ntoa(user->addr.sin_addr))) {
2179 if (ast_strlen_zero(user->context)) {
2180 if (ast_strlen_zero(default_context)) {
2181 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd->sourceIp);
2183 ASTOBJ_UNREF(user, oh323_destroy_user);
2186 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2188 ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
2190 pvt->exten[0] = 'i';
2191 pvt->exten[1] = '\0';
2192 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd->sourceIp);
2194 ASTOBJ_UNREF(user, oh323_destroy_user);
2195 return NULL; /* XXX: Hmmm... Why to setup context if we drop connection immediately??? */
2198 ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
2199 memcpy(&pvt->options, &user->options, sizeof(pvt->options));
2200 pvt->jointcapability = pvt->options.capability;
2201 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
2202 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2204 ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
2206 if (!ast_strlen_zero(user->accountcode)) {
2207 ast_copy_string(pvt->accountcode, user->accountcode, sizeof(pvt->accountcode));
2209 if (user->amaflags) {
2210 pvt->amaflags = user->amaflags;
2212 ASTOBJ_UNREF(user, oh323_destroy_user);
2215 return &pvt->options;
2219 * Call-back function to start PBX when OpenH323 ready to serve incoming call
2221 * Returns 1 on success
2223 static int answer_call(unsigned call_reference, const char *token)
2225 struct oh323_pvt *pvt;
2226 struct ast_channel *c = NULL;
2227 enum {ext_original, ext_s, ext_i, ext_notexists} try_exten;
2228 char tmp_exten[sizeof(pvt->exten)];
2231 ast_debug(1, "Preparing Asterisk to answer for %s\n", token);
2233 /* Find the call or allocate a private structure if call not found */
2234 pvt = find_call_locked(call_reference, token);
2236 ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
2239 /* Check if requested extension@context pair exists in the dialplan */
2240 ast_copy_string(tmp_exten, pvt->exten, sizeof(tmp_exten));
2242 /* Try to find best extension in specified context */
2243 if ((tmp_exten[0] != '\0') && (tmp_exten[1] == '\0')) {
2244 if (tmp_exten[0] == 's')
2246 else if (tmp_exten[0] == 'i')
2249 try_exten = ext_original;
2251 try_exten = ext_original;
2253 if (ast_exists_extension(NULL, pvt->context, tmp_exten, 1, NULL))
2255 switch (try_exten) {
2258 tmp_exten[1] = '\0';
2266 try_exten = ext_notexists;
2271 } while (try_exten != ext_notexists);
2273 /* Drop the call if we don't have <exten>, s and i extensions */
2274 if (try_exten == ext_notexists) {
2275 ast_log(LOG_NOTICE, "Dropping call because extensions '%s', 's' and 'i' doesn't exists in context [%s]\n", pvt->exten, pvt->context);
2276 ast_mutex_unlock(&pvt->lock);
2277 h323_clear_call(token, AST_CAUSE_UNALLOCATED);
2279 } else if ((try_exten != ext_original) && (strcmp(pvt->exten, tmp_exten) != 0)) {
2281 ast_debug(1, "Going to extension %s@%s because %s@%s isn't exists\n", tmp_exten, pvt->context, pvt->exten, pvt->context);
2282 ast_copy_string(pvt->exten, tmp_exten, sizeof(pvt->exten));
2285 /* allocate a channel and tell asterisk about it */
2286 c = __oh323_new(pvt, AST_STATE_RINGING, pvt->cd.call_token);
2288 /* And release when done */
2289 ast_mutex_unlock(&pvt->lock);
2291 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
2298 * Call-back function to establish an outgoing H.323 call
2300 * Returns 1 on success
2302 static int setup_outgoing_call(call_details_t *cd)
2304 /* Use argument here or free it immediately */
2305 cleanup_call_details(cd);
2311 * Call-back function to signal asterisk that the channel is ringing
2314 static void chan_ringing(unsigned call_reference, const char *token)
2316 struct oh323_pvt *pvt;
2319 ast_debug(1, "Ringing on %s\n", token);
2321 pvt = find_call_locked(call_reference, token);
2323 ast_log(LOG_ERROR, "Something is wrong: ringing\n");
2327 ast_mutex_unlock(&pvt->lock);
2328 ast_log(LOG_ERROR, "Channel has no owner\n");
2331 update_state(pvt, AST_STATE_RINGING, AST_CONTROL_RINGING);
2332 ast_mutex_unlock(&pvt->lock);
2337 * Call-back function to cleanup communication
2340 static void cleanup_connection(unsigned call_reference, const char *call_token)
2342 struct oh323_pvt *pvt;
2345 ast_debug(1, "Cleaning connection to %s\n", call_token);
2348 pvt = find_call_locked(call_reference, call_token);
2351 ast_debug(1, "No connection for %s\n", call_token);
2354 if (!pvt->owner || !ast_channel_trylock(pvt->owner))
2357 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s\n", call_token);
2358 #ifdef DEBUG_THREADS
2359 /* XXX to be completed
2360 * If we want to print more info on who is holding the lock,
2361 * implement the relevant code in lock.h and use the routines
2366 ast_mutex_unlock(&pvt->lock);
2370 /* Immediately stop RTP */
2371 ast_rtp_destroy(pvt->rtp);
2374 /* Free dsp used for in-band DTMF detection */
2376 ast_dsp_free(pvt->vad);
2379 cleanup_call_details(&pvt->cd);
2380 pvt->alreadygone = 1;
2383 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2384 ast_queue_hangup(pvt->owner);
2385 ast_channel_unlock(pvt->owner);
2387 ast_mutex_unlock(&pvt->lock);
2389 ast_debug(1, "Connection to %s cleaned\n", call_token);
2393 static void hangup_connection(unsigned int call_reference, const char *token, int cause)
2395 struct oh323_pvt *pvt;
2398 ast_debug(1, "Hanging up connection to %s with cause %d\n", token, cause);
2400 pvt = find_call_locked(call_reference, token);
2403 ast_debug(1, "Connection to %s already cleared\n", token);
2406 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
2407 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2408 pvt->owner->hangupcause = pvt->hangupcause = cause;
2409 ast_queue_hangup_with_cause(pvt->owner, cause);
2410 ast_channel_unlock(pvt->owner);
2413 pvt->needhangup = 1;
2414 pvt->hangupcause = cause;
2416 ast_debug(1, "Hangup for %s is pending\n", token);
2418 ast_mutex_unlock(&pvt->lock);
2421 static void set_dtmf_payload(unsigned call_reference, const char *token, int payload, int is_cisco)
2423 struct oh323_pvt *pvt;
2426 ast_debug(1, "Setting %s DTMF payload to %d on %s\n", (is_cisco ? "Cisco" : "RFC2833"), payload, token);
2428 pvt = find_call_locked(call_reference, token);
2433 ast_rtp_set_rtpmap_type(pvt->rtp, payload, "audio", (is_cisco ? "cisco-telephone-event" : "telephone-event"), 0);
2435 pvt->dtmf_pt[is_cisco ? 1 : 0] = payload;
2436 ast_mutex_unlock(&pvt->lock);
2438 ast_debug(1, "DTMF payload on %s set to %d\n", token, payload);
2441 static void set_peer_capabilities(unsigned call_reference, const char *token, int capabilities, struct ast_codec_pref *prefs)
2443 struct oh323_pvt *pvt;
2446 ast_debug(1, "Got remote capabilities from connection %s\n", token);
2448 pvt = find_call_locked(call_reference, token);
2451 pvt->peercapability = capabilities;
2452 pvt->jointcapability = pvt->options.capability & capabilities;
2454 memcpy(&pvt->peer_prefs, prefs, sizeof(pvt->peer_prefs));
2457 for (i = 0; i < 32; ++i) {
2458 if (!prefs->order[i])
2460 ast_debug(1, "prefs[%d]=%s:%d\n", i, (prefs->order[i] ? ast_getformatname(1 << (prefs->order[i]-1)) : "<none>"), prefs->framing[i]);
2464 ast_rtp_codec_setpref(pvt->rtp, &pvt->peer_prefs);
2466 ast_mutex_unlock(&pvt->lock);
2469 static void set_local_capabilities(unsigned call_reference, const char *token)
2471 struct oh323_pvt *pvt;
2472 int capability, dtmfmode, pref_codec;
2473 struct ast_codec_pref prefs;
2476 ast_debug(1, "Setting capabilities for connection %s\n", token);
2478 pvt = find_call_locked(call_reference, token);
2481 capability = (pvt->jointcapability) ? pvt->jointcapability : pvt->options.capability;
2482 dtmfmode = pvt->options.dtmfmode;
2483 prefs = pvt->options.prefs;
2484 pref_codec = pvt->pref_codec;
2485 ast_mutex_unlock(&pvt->lock);
2486 h323_set_capabilities(token, capability, dtmfmode, &prefs, pref_codec);
2489 ast_debug(1, "Capabilities for connection %s is set\n", token);
2492 static void remote_hold(unsigned call_reference, const char *token, int is_hold)
2494 struct oh323_pvt *pvt;
2497 ast_debug(1, "Setting %shold status for connection %s\n", (is_hold ? "" : "un"), token);
2499 pvt = find_call_locked(call_reference, token);
2502 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
2504 ast_queue_control(pvt->owner, AST_CONTROL_HOLD);
2506 ast_queue_control(pvt->owner, AST_CONTROL_UNHOLD);
2507 ast_channel_unlock(pvt->owner);
2511 pvt->newcontrol = AST_CONTROL_HOLD;
2513 pvt->newcontrol = AST_CONTROL_UNHOLD;
2515 ast_mutex_unlock(&pvt->lock);
2518 static void *do_monitor(void *data)
2522 struct oh323_pvt *oh323 = NULL;
2525 /* Check for a reload request */
2526 ast_mutex_lock(&h323_reload_lock);
2527 reloading = h323_reloading;
2529 ast_mutex_unlock(&h323_reload_lock);
2531 ast_verb(1, "Reloading H.323\n");
2534 /* Check for interfaces needing to be killed */
2535 if (!ast_mutex_trylock(&iflock)) {
2538 for (oh323 = iflist; oh323; oh323 = oh323->next) {
2539 if (!ast_mutex_trylock(&oh323->lock)) {
2540 if (oh323->needdestroy) {
2541 __oh323_destroy(oh323);
2544 ast_mutex_unlock(&oh323->lock);
2547 } while (/*oh323*/ 0);
2552 if (!ast_mutex_trylock(&oh323->lock)) {
2553 if (oh323->needdestroy) {
2554 __oh323_destroy(oh323);
2557 ast_mutex_unlock(&oh323->lock);
2558 oh323 = oh323->next;
2562 ast_mutex_unlock(&iflock);
2564 oh323 = (struct oh323_pvt *)1; /* Force fast loop */
2565 pthread_testcancel();
2566 /* Wait for sched or io */
2567 res = ast_sched_wait(sched);
2568 if ((res < 0) || (res > 1000)) {
2571 /* Do not wait if some channel(s) is destroyed, probably, more available too */
2574 res = ast_io_wait(io, res);
2575 pthread_testcancel();
2576 ast_mutex_lock(&monlock);
2578 ast_sched_runq(sched);
2580 ast_mutex_unlock(&monlock);
2586 static int restart_monitor(void)
2588 /* If we're supposed to be stopped -- stay stopped */
2589 if (ast_mutex_lock(&monlock)) {
2590 ast_log(LOG_WARNING, "Unable to lock monitor\n");
2593 if (monitor_thread == AST_PTHREADT_STOP) {
2594 ast_mutex_unlock(&monlock);
2597 if (monitor_thread == pthread_self()) {
2598 ast_mutex_unlock(&monlock);
2599 ast_log(LOG_WARNING, "Cannot kill myself\n");
2602 if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
2603 /* Wake up the thread */
2604 pthread_kill(monitor_thread, SIGURG);
2606 /* Start a new monitor */
2607 if (ast_pthread_create_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
2608 monitor_thread = AST_PTHREADT_NULL;
2609 ast_mutex_unlock(&monlock);
2610 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
2614 ast_mutex_unlock(&monlock);
2618 static char *handle_cli_h323_set_trace(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2622 e->command = "h323 set trace [on|off]";
2624 "Usage: h323 set trace (on|off|<trace level>)\n"
2625 " Enable/Disable H.323 stack tracing for debugging purposes\n";
2631 if (a->argc != e->args)
2632 return CLI_SHOWUSAGE;
2633 if (!strcasecmp(a->argv[3], "off")) {
2635 ast_cli(a->fd, "H.323 Trace Disabled\n");
2636 } else if (!strcasecmp(a->argv[3], "on")) {
2638 ast_cli(a->fd, "H.323 Trace Enabled\n");
2640 int tracelevel = atoi(a->argv[3]);
2641 h323_debug(1, tracelevel);
2642 ast_cli(a->fd, "H.323 Trace Enabled (Trace Level: %d)\n", tracelevel);
2647 static char *handle_cli_h323_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2651 e->command = "h323 set debug [on|off]";
2653 "Usage: h323 set debug [on|off]\n"
2654 " Enable/Disable H.323 debugging output\n";
2660 if (a->argc != e->args)
2661 return CLI_SHOWUSAGE;
2662 if (strcasecmp(a->argv[3], "on") && strcasecmp(a->argv[3], "off"))
2663 return CLI_SHOWUSAGE;
2665 h323debug = (strcasecmp(a->argv[3], "on")) ? 0 : 1;
2666 ast_cli(a->fd, "H.323 Debugging %s\n", h323debug ? "Enabled" : "Disabled");
2670 static char *handle_cli_h323_cycle_gk(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2674 e->command = "h323 cycle gk";
2676 "Usage: h323 cycle gk\n"
2677 " Manually re-register with the Gatekeper (Currently Disabled)\n";
2684 return CLI_SHOWUSAGE;
2688 /* Possibly register with a GK */
2689 if (!gatekeeper_disable) {
2690 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
2691 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
2697 static char *handle_cli_h323_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2701 e->command = "h323 hangup";
2703 "Usage: h323 hangup <token>\n"
2704 " Manually try to hang up the call identified by <token>\n";
2711 return CLI_SHOWUSAGE;
2712 if (h323_soft_hangup(a->argv[2])) {
2713 ast_verb(3, "Hangup succeeded on %s\n", a->argv[2]);
2715 ast_verb(3, "Hangup failed for %s\n", a->argv[2]);
2720 static char *handle_cli_h323_show_tokens(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2724 e->command = "h323 show tokens";
2726 "Usage: h323 show tokens\n"
2727 " Print out all active call tokens\n";
2734 return CLI_SHOWUSAGE;
2741 static struct ast_cli_entry cli_h323[] = {
2742 AST_CLI_DEFINE(handle_cli_h323_set_trace, "Enable/Disable H.323 Stack Tracing"),
2743 AST_CLI_DEFINE(handle_cli_h323_set_debug, "Enable/Disable H.323 Debugging"),
2744 AST_CLI_DEFINE(handle_cli_h323_cycle_gk, "Manually re-register with the Gatekeper"),
2745 AST_CLI_DEFINE(handle_cli_h323_hangup, "Manually try to hang up a call"),
2746 AST_CLI_DEFINE(handle_cli_h323_show_tokens, "Show all active call tokens"),
2749 static void delete_users(void)
2753 /* Delete all users */
2754 ASTOBJ_CONTAINER_WRLOCK(&userl);
2755 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
2756 ASTOBJ_RDLOCK(iterator);
2757 ASTOBJ_MARK(iterator);
2759 ASTOBJ_UNLOCK(iterator);
2762 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, oh323_destroy_user);
2764 ASTOBJ_CONTAINER_UNLOCK(&userl);
2766 ASTOBJ_CONTAINER_WRLOCK(&peerl);
2767 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
2768 ASTOBJ_RDLOCK(iterator);
2769 ASTOBJ_MARK(iterator);
2770 ASTOBJ_UNLOCK(iterator);
2772 ASTOBJ_CONTAINER_UNLOCK(&peerl);
2775 static void delete_aliases(void)
2779 /* Delete all aliases */
2780 ASTOBJ_CONTAINER_WRLOCK(&aliasl);
2781 ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
2782 ASTOBJ_RDLOCK(iterator);
2783 ASTOBJ_MARK(iterator);
2785 ASTOBJ_UNLOCK(iterator);
2788 ASTOBJ_CONTAINER_PRUNE_MARKED(&aliasl, oh323_destroy_alias);
2790 ASTOBJ_CONTAINER_UNLOCK(&aliasl);
2793 static void prune_peers(void)
2795 /* Prune peers who still are supposed to be deleted */
2796 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, oh323_destroy_peer);
2799 static int reload_config(int is_reload)
2801 struct ast_config *cfg, *ucfg;
2802 struct ast_variable *v;
2803 struct oh323_peer *peer = NULL;
2804 struct oh323_user *user = NULL;
2805 struct oh323_alias *alias = NULL;
2806 struct ast_hostent ahp; struct hostent *hp;
2809 int is_user, is_peer, is_alias;
2810 char _gatekeeper[100];
2811 int gk_discover, gk_disable, gk_changed;
2812 struct ast_flags config_flags = { is_reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
2814 cfg = ast_config_load(config, config_flags);
2816 /* We *must* have a config file otherwise stop immediately */
2818 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
2820 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
2821 ucfg = ast_config_load("users.conf", config_flags);
2822 if (ucfg == CONFIG_STATUS_FILEUNCHANGED) {
2824 } else if (ucfg == CONFIG_STATUS_FILEINVALID) {
2825 ast_log(LOG_ERROR, "Config file users.conf is in an invalid format. Aborting.\n");
2828 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
2829 if ((cfg = ast_config_load(config, config_flags))) {
2830 ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
2831 ast_config_destroy(ucfg);
2834 } else if (cfg == CONFIG_STATUS_FILEINVALID) {
2835 ast_log(LOG_ERROR, "Config file %s is in an invalid format. Aborting.\n", config);
2838 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
2839 if ((ucfg = ast_config_load("users.conf", config_flags)) == CONFIG_STATUS_FILEINVALID) {
2840 ast_log(LOG_ERROR, "Config file users.conf is in an invalid format. Aborting.\n");
2841 ast_config_destroy(cfg);
2852 /* fire up the H.323 Endpoint */
2853 if (!h323_end_point_exist()) {
2854 h323_end_point_create();
2856 ast_copy_string(_gatekeeper, gatekeeper, sizeof(_gatekeeper));
2857 gk_discover = gatekeeper_discover;
2858 gk_disable = gatekeeper_disable;
2859 memset(&bindaddr, 0, sizeof(bindaddr));
2860 memset(&global_options, 0, sizeof(global_options));
2861 global_options.fastStart = 1;
2862 global_options.h245Tunneling = 1;
2863 global_options.dtmfcodec[0] = H323_DTMF_RFC2833_PT;
2864 global_options.dtmfcodec[1] = H323_DTMF_CISCO_PT;
2865 global_options.dtmfmode = 0;
2866 global_options.holdHandling = 0;
2867 global_options.capability = GLOBAL_CAPABILITY;
2868 global_options.bridge = 1; /* Do native bridging by default */
2869 strcpy(default_context, "default");
2870 h323_signalling_port = 1720;
2871 gatekeeper_disable = 1;
2872 gatekeeper_discover = 0;
2875 acceptAnonymous = 1;
2879 /* Copy the default jb config over global_jbconf */
2880 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2883 struct ast_variable *gen;
2885 const char *has_h323;
2887 genhas_h323 = ast_true(ast_variable_retrieve(ucfg, "general", "hash323"));
2888 gen = ast_variable_browse(ucfg, "general");
2889 for (cat = ast_category_browse(ucfg, NULL); cat; cat = ast_category_browse(ucfg, cat)) {
2890 if (strcasecmp(cat, "general")) {
2891 has_h323 = ast_variable_retrieve(ucfg, cat, "hash323");
2892 if (ast_true(has_h323) || (!has_h323 && genhas_h323)) {
2893 user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
2895 ASTOBJ_CONTAINER_LINK(&userl, user);
2896 ASTOBJ_UNREF(user, oh323_destroy_user);
2898 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
2900 ASTOBJ_CONTAINER_LINK(&peerl, peer);
2901 ASTOBJ_UNREF(peer, oh323_destroy_peer);
2906 ast_config_destroy(ucfg);
2909 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
2910 /* handle jb conf */
2911 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
2913 /* Create the interface list */
2914 if (!strcasecmp(v->name, "port")) {
2915 h323_signalling_port = (int)strtol(v->value, NULL, 10);
2916 } else if (!strcasecmp(v->name, "bindaddr")) {
2917 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
2918 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
2920 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
2922 } else if (!strcasecmp(v->name, "tos")) { /* Needs to be removed in next release */
2923 ast_log(LOG_WARNING, "The \"tos\" setting is deprecated in this version of Asterisk. Please change to \"tos_audio\".\n");
2924 if (ast_str2tos(v->value, &tos)) {
2925 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
2927 } else if (!strcasecmp(v->name, "tos_audio")) {
2928 if (ast_str2tos(v->value, &tos)) {
2929 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
2931 } else if (!strcasecmp(v->name, "cos")) {
2932 ast_log(LOG_WARNING, "The \"cos\" setting is deprecated in this version of Asterisk. Please change to \"cos_audio\".\n");
2933 if (ast_str2cos(v->value, &cos)) {
2934 ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
2936 } else if (!strcasecmp(v->name, "cos_audio")) {
2937 if (ast_str2cos(v->value, &cos)) {
2938 ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
2940 } else if (!strcasecmp(v->name, "gatekeeper")) {
2941 if (!strcasecmp(v->value, "DISABLE")) {
2942 gatekeeper_disable = 1;
2943 } else if (!strcasecmp(v->value, "DISCOVER")) {
2944 gatekeeper_disable = 0;
2945 gatekeeper_discover = 1;
2947 gatekeeper_disable = 0;
2948 ast_copy_string(gatekeeper, v->value, sizeof(gatekeeper));
2950 } else if (!strcasecmp(v->name, "secret")) {
2951 ast_copy_string(secret, v->value, sizeof(secret));
2952 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
2953 gkroute = ast_true(v->value);
2954 } else if (!strcasecmp(v->name, "context")) {
2955 ast_copy_string(default_context, v->value, sizeof(default_context));
2956 ast_verb(2, "Setting default context to %s\n", default_context);
2957 } else if (!strcasecmp(v->name, "UserByAlias")) {
2958 userbyalias = ast_true(v->value);
2959 } else if (!strcasecmp(v->name, "AcceptAnonymous")) {
2960 acceptAnonymous = ast_true(v->value);
2961 } else if (!update_common_options(v, &global_options)) {
2965 if (!global_options.dtmfmode)
2966 global_options.dtmfmode = H323_DTMF_RFC2833;
2967 if (global_options.holdHandling == ~0)
2968 global_options.holdHandling = 0;
2969 else if (!global_options.holdHandling)
2970 global_options.holdHandling = H323_HOLD_H450;
2972 for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
2973 if (strcasecmp(cat, "general")) {
2974 utype = ast_variable_retrieve(cfg, cat, "type");
2976 is_user = is_peer = is_alias = 0;
2977 if (!strcasecmp(utype, "user"))
2979 else if (!strcasecmp(utype, "peer"))
2981 else if (!strcasecmp(utype, "friend"))
2982 is_user = is_peer = 1;
2983 else if (!strcasecmp(utype, "h323") || !strcasecmp(utype, "alias"))
2986 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
2990 user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
2992 ASTOBJ_CONTAINER_LINK(&userl, user);
2993 ASTOBJ_UNREF(user, oh323_destroy_user);
2997 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
2999 ASTOBJ_CONTAINER_LINK(&peerl, peer);
3000 ASTOBJ_UNREF(peer, oh323_destroy_peer);
3004 alias = build_alias(cat, ast_variable_browse(cfg, cat), NULL, 0);
3006 ASTOBJ_CONTAINER_LINK(&aliasl, alias);
3007 ASTOBJ_UNREF(alias, oh323_destroy_alias);
3011 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
3015 ast_config_destroy(cfg);
3017 /* Register our H.323 aliases if any*/
3018 ASTOBJ_CONTAINER_WRLOCK(&aliasl);
3019 ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
3020 ASTOBJ_RDLOCK(iterator);
3021 if (h323_set_alias(iterator)) {
3022 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
3023 ASTOBJ_UNLOCK(iterator);
3026 ASTOBJ_UNLOCK(iterator);
3028 ASTOBJ_CONTAINER_UNLOCK(&aliasl);
3030 /* Don't touch GK if nothing changed because URQ will drop all existing calls */
3032 if (gatekeeper_disable != gk_disable)
3033 gk_changed = is_reload;
3034 else if(!gatekeeper_disable && (gatekeeper_discover != gk_discover))
3035 gk_changed = is_reload;
3036 else if(!gatekeeper_disable && (strncmp(_gatekeeper, gatekeeper, sizeof(_gatekeeper)) != 0))
3037 gk_changed = is_reload;
3041 if (!gatekeeper_disable) {
3042 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
3043 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
3044 gatekeeper_disable = 1;