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 /* disable, for now */
259 .bridge = ast_rtp_bridge,
263 static const char* redirectingreason2str(int redirectingreason)
265 switch (redirectingreason) {
273 return "UNCONDITIONAL";
279 static void oh323_destroy_alias(struct oh323_alias *alias)
282 ast_debug(1, "Destroying alias '%s'\n", alias->name);
286 static void oh323_destroy_user(struct oh323_user *user)
289 ast_debug(1, "Destroying user '%s'\n", user->name);
290 ast_free_ha(user->ha);
294 static void oh323_destroy_peer(struct oh323_peer *peer)
297 ast_debug(1, "Destroying peer '%s'\n", peer->name);
298 ast_free_ha(peer->ha);
302 static int oh323_simulate_dtmf_end(const void *data)
304 struct oh323_pvt *pvt = (struct oh323_pvt *)data;
307 ast_mutex_lock(&pvt->lock);
308 /* Don't hold pvt lock while trying to lock the channel */
309 while(pvt->owner && ast_channel_trylock(pvt->owner)) {
310 ast_mutex_unlock(&pvt->lock);
312 ast_mutex_lock(&pvt->lock);
316 struct ast_frame f = {
317 .frametype = AST_FRAME_DTMF_END,
318 .subclass = pvt->curDTMF,
320 .src = "SIMULATE_DTMF_END",
322 ast_queue_frame(pvt->owner, &f);
323 ast_channel_unlock(pvt->owner);
327 ast_mutex_unlock(&pvt->lock);
333 /*! \brief Channel and private structures should be already locked */
334 static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
336 if (c->nativeformats != pvt->nativeformats) {
338 ast_debug(1, "Preparing %s for new native format\n", c->name);
339 c->nativeformats = pvt->nativeformats;
340 ast_set_read_format(c, c->readformat);
341 ast_set_write_format(c, c->writeformat);
343 if (pvt->needhangup) {
345 ast_debug(1, "Process pending hangup for %s\n", c->name);
346 c->_softhangup |= AST_SOFTHANGUP_DEV;
347 c->hangupcause = pvt->hangupcause;
350 pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->DTMFsched = -1;
352 if (pvt->newstate >= 0) {
353 ast_setstate(c, pvt->newstate);
356 if (pvt->newcontrol >= 0) {
357 ast_queue_control(c, pvt->newcontrol);
358 pvt->newcontrol = -1;
360 if (pvt->newdigit >= 0) {
361 struct ast_frame f = {
362 .frametype = AST_FRAME_DTMF_END,
363 .subclass = pvt->newdigit,
364 .samples = pvt->newduration * 8,
365 .len = pvt->newduration,
366 .src = "UPDATE_INFO",
368 if (pvt->newdigit == ' ') { /* signalUpdate message */
369 f.subclass = pvt->curDTMF;
370 if (pvt->DTMFsched >= 0) {
371 ast_sched_del(sched, pvt->DTMFsched);
374 } else { /* Regular input or signal message */
375 if (pvt->newduration) { /* This is a signal, signalUpdate follows */
376 f.frametype = AST_FRAME_DTMF_BEGIN;
377 if (pvt->DTMFsched >= 0)
378 ast_sched_del(sched, pvt->DTMFsched);
379 pvt->DTMFsched = ast_sched_add(sched, pvt->newduration, oh323_simulate_dtmf_end, pvt);
381 ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", pvt->newduration, pvt->DTMFsched);
383 pvt->curDTMF = pvt->newdigit;
385 ast_queue_frame(c, &f);
388 if (pvt->update_rtp_info > 0) {
390 ast_jb_configure(c, &global_jbconf);
391 ast_channel_set_fd(c, 0, ast_rtp_fd(pvt->rtp));
392 ast_channel_set_fd(c, 1, ast_rtcp_fd(pvt->rtp));
393 ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
395 pvt->update_rtp_info = -1;
399 /*! \brief Only channel structure should be locked */
400 static void oh323_update_info(struct ast_channel *c)
402 struct oh323_pvt *pvt = c->tech_pvt;
405 ast_mutex_lock(&pvt->lock);
406 __oh323_update_info(c, pvt);
407 ast_mutex_unlock(&pvt->lock);
411 static void cleanup_call_details(call_details_t *cd)
413 if (cd->call_token) {
414 ast_free(cd->call_token);
415 cd->call_token = NULL;
417 if (cd->call_source_aliases) {
418 ast_free(cd->call_source_aliases);
419 cd->call_source_aliases = NULL;
421 if (cd->call_dest_alias) {
422 ast_free(cd->call_dest_alias);
423 cd->call_dest_alias = NULL;
425 if (cd->call_source_name) {
426 ast_free(cd->call_source_name);
427 cd->call_source_name = NULL;
429 if (cd->call_source_e164) {
430 ast_free(cd->call_source_e164);
431 cd->call_source_e164 = NULL;
433 if (cd->call_dest_e164) {
434 ast_free(cd->call_dest_e164);
435 cd->call_dest_e164 = NULL;
438 ast_free(cd->sourceIp);
441 if (cd->redirect_number) {
442 ast_free(cd->redirect_number);
443 cd->redirect_number = NULL;
447 static void __oh323_destroy(struct oh323_pvt *pvt)
449 struct oh323_pvt *cur, *prev = NULL;
451 if (pvt->DTMFsched >= 0) {
452 ast_sched_del(sched, pvt->DTMFsched);
457 ast_rtp_destroy(pvt->rtp);
460 /* Free dsp used for in-band DTMF detection */
462 ast_dsp_free(pvt->vad);
464 cleanup_call_details(&pvt->cd);
466 /* Unlink us from the owner if we have one */
468 ast_channel_lock(pvt->owner);
470 ast_debug(1, "Detaching from %s\n", pvt->owner->name);
471 pvt->owner->tech_pvt = NULL;
472 ast_channel_unlock(pvt->owner);
478 prev->next = cur->next;
487 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
489 ast_mutex_unlock(&pvt->lock);
490 ast_mutex_destroy(&pvt->lock);
495 static void oh323_destroy(struct oh323_pvt *pvt)
498 ast_debug(1, "Destroying channel %s\n", (pvt->owner ? pvt->owner->name : "<unknown>"));
500 ast_mutex_lock(&iflock);
501 ast_mutex_lock(&pvt->lock);
502 __oh323_destroy(pvt);
503 ast_mutex_unlock(&iflock);
506 static int oh323_digit_begin(struct ast_channel *c, char digit)
508 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
512 ast_log(LOG_ERROR, "No private structure?! This is bad\n");
515 ast_mutex_lock(&pvt->lock);
517 (((pvt->options.dtmfmode & H323_DTMF_RFC2833) && pvt->dtmf_pt[0])
518 /*|| ((pvt->options.dtmfmode & H323_DTMF_CISCO) && pvt->dtmf_pt[1]))*/)) {
519 /* out-of-band DTMF */
521 ast_log(LOG_DTMF, "Begin sending out-of-band digit %c on %s\n", digit, c->name);
523 ast_rtp_senddigit_begin(pvt->rtp, digit);
524 ast_mutex_unlock(&pvt->lock);
525 } else if (pvt->txDtmfDigit != digit) {
528 ast_log(LOG_DTMF, "Begin sending inband digit %c on %s\n", digit, c->name);
530 pvt->txDtmfDigit = digit;
531 token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
532 ast_mutex_unlock(&pvt->lock);
533 h323_send_tone(token, digit);
538 ast_mutex_unlock(&pvt->lock);
539 oh323_update_info(c);
544 * Send (play) the specified digit to the channel.
547 static int oh323_digit_end(struct ast_channel *c, char digit, unsigned int duration)
549 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
553 ast_log(LOG_ERROR, "No private structure?! This is bad\n");
556 ast_mutex_lock(&pvt->lock);
557 if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833) && ((pvt->dtmf_pt[0] > 0) || (pvt->dtmf_pt[0] > 0))) {
558 /* out-of-band DTMF */
560 ast_log(LOG_DTMF, "End sending out-of-band digit %c on %s, duration %d\n", digit, c->name, duration);
562 ast_rtp_senddigit_end(pvt->rtp, digit);
563 ast_mutex_unlock(&pvt->lock);
567 ast_log(LOG_DTMF, "End sending inband digit %c on %s, duration %d\n", digit, c->name, duration);
569 pvt->txDtmfDigit = ' ';
570 token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
571 ast_mutex_unlock(&pvt->lock);
572 h323_send_tone(token, ' ');
577 oh323_update_info(c);
582 * Make a call over the specified channel to the specified
584 * Returns -1 on error, 0 on success.
586 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
589 struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
591 char called_addr[1024];
594 ast_debug(1, "Calling to %s on %s\n", dest, c->name);
596 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
597 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
600 ast_mutex_lock(&pvt->lock);
601 if (!gatekeeper_disable) {
602 if (ast_strlen_zero(pvt->exten)) {
603 ast_copy_string(called_addr, dest, sizeof(called_addr));
605 snprintf(called_addr, sizeof(called_addr), "%s@%s", pvt->exten, dest);
608 res = htons(pvt->sa.sin_port);
609 addr = ast_inet_ntoa(pvt->sa.sin_addr);
610 if (ast_strlen_zero(pvt->exten)) {
611 snprintf(called_addr, sizeof(called_addr), "%s:%d", addr, res);
613 snprintf(called_addr, sizeof(called_addr), "%s@%s:%d", pvt->exten, addr, res);
616 /* make sure null terminated */
617 called_addr[sizeof(called_addr) - 1] = '\0';
620 ast_copy_string(pvt->options.cid_num, c->cid.cid_num, sizeof(pvt->options.cid_num));
623 ast_copy_string(pvt->options.cid_name, c->cid.cid_name, sizeof(pvt->options.cid_name));
625 if (c->cid.cid_rdnis) {
626 ast_copy_string(pvt->options.cid_rdnis, c->cid.cid_rdnis, sizeof(pvt->options.cid_rdnis));
629 pvt->options.presentation = c->cid.cid_pres;
630 pvt->options.type_of_number = c->cid.cid_ton;
632 if ((addr = pbx_builtin_getvar_helper(c, "PRIREDIRECTREASON"))) {
633 if (!strcasecmp(addr, "UNKNOWN"))
634 pvt->options.redirect_reason = 0;
635 else if (!strcasecmp(addr, "BUSY"))
636 pvt->options.redirect_reason = 1;
637 else if (!strcasecmp(addr, "NO_REPLY"))
638 pvt->options.redirect_reason = 2;
639 else if (!strcasecmp(addr, "UNCONDITIONAL"))
640 pvt->options.redirect_reason = 15;
642 pvt->options.redirect_reason = -1;
644 pvt->options.redirect_reason = -1;
646 pvt->options.transfer_capability = c->transfercapability;
648 /* indicate that this is an outgoing call */
651 ast_verb(3, "Requested transfer capability: 0x%.2x - %s\n", c->transfercapability, ast_transfercapability2str(c->transfercapability));
653 ast_debug(1, "Placing outgoing call to %s, %d/%d\n", called_addr, pvt->options.dtmfcodec[0], pvt->options.dtmfcodec[1]);
654 ast_mutex_unlock(&pvt->lock);
655 res = h323_make_call(called_addr, &(pvt->cd), &pvt->options);
657 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
660 oh323_update_info(c);
664 static int oh323_answer(struct ast_channel *c)
667 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
671 ast_debug(1, "Answering on %s\n", c->name);
673 ast_mutex_lock(&pvt->lock);
674 token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
675 ast_mutex_unlock(&pvt->lock);
676 res = h323_answering_call(token, 0);
680 oh323_update_info(c);
681 if (c->_state != AST_STATE_UP) {
682 ast_setstate(c, AST_STATE_UP);
687 static int oh323_hangup(struct ast_channel *c)
689 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
690 int q931cause = AST_CAUSE_NORMAL_CLEARING;
695 ast_debug(1, "Hanging up and scheduling destroy of call %s\n", c->name);
698 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
701 ast_mutex_lock(&pvt->lock);
702 /* Determine how to disconnect */
703 if (pvt->owner != c) {
704 ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
705 ast_mutex_unlock(&pvt->lock);
712 if (c->hangupcause) {
713 q931cause = c->hangupcause;
715 const char *cause = pbx_builtin_getvar_helper(c, "DIALSTATUS");
717 if (!strcmp(cause, "CONGESTION")) {
718 q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
719 } else if (!strcmp(cause, "BUSY")) {
720 q931cause = AST_CAUSE_USER_BUSY;
721 } else if (!strcmp(cause, "CHANISUNVAIL")) {
722 q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
723 } else if (!strcmp(cause, "NOANSWER")) {
724 q931cause = AST_CAUSE_NO_ANSWER;
725 } else if (!strcmp(cause, "CANCEL")) {
726 q931cause = AST_CAUSE_CALL_REJECTED;
731 /* Start the process if it's not already started */
732 if (!pvt->alreadygone && !pvt->hangupcause) {
733 call_token = pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL;
735 /* Release lock to eliminate deadlock */
736 ast_mutex_unlock(&pvt->lock);
737 if (h323_clear_call(call_token, q931cause)) {
738 ast_log(LOG_WARNING, "ClearCall failed.\n");
740 ast_free(call_token);
741 ast_mutex_lock(&pvt->lock);
744 pvt->needdestroy = 1;
745 ast_mutex_unlock(&pvt->lock);
747 /* Update usage counter */
748 ast_module_unref(ast_module_info->self);
753 /*! \brief Retrieve audio/etc from channel. Assumes pvt->lock is already held. */
754 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *pvt)
758 /* Only apply it for the first packet, we just need the correct ip/port */
759 if (pvt->options.nat) {
760 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
761 pvt->options.nat = 0;
764 f = ast_rtp_read(pvt->rtp);
765 /* Don't send RFC2833 if we're not supposed to */
766 if (f && (f->frametype == AST_FRAME_DTMF) && !(pvt->options.dtmfmode & (H323_DTMF_RFC2833 | H323_DTMF_CISCO))) {
767 return &ast_null_frame;
770 /* We already hold the channel lock */
771 if (f->frametype == AST_FRAME_VOICE) {
772 if (f->subclass != pvt->owner->nativeformats) {
773 /* Try to avoid deadlock */
774 if (ast_channel_trylock(pvt->owner)) {
775 ast_log(LOG_NOTICE, "Format changed but channel is locked. Ignoring frame...\n");
776 return &ast_null_frame;
779 ast_debug(1, "Oooh, format changed to %d\n", f->subclass);
780 pvt->owner->nativeformats = f->subclass;
781 pvt->nativeformats = f->subclass;
782 ast_set_read_format(pvt->owner, pvt->owner->readformat);
783 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
784 ast_channel_unlock(pvt->owner);
786 /* Do in-band DTMF detection */
787 if ((pvt->options.dtmfmode & H323_DTMF_INBAND) && pvt->vad) {
788 if ((pvt->nativeformats & (AST_FORMAT_SLINEAR | AST_FORMAT_ALAW | AST_FORMAT_ULAW))) {
789 if (!ast_channel_trylock(pvt->owner)) {
790 f = ast_dsp_process(pvt->owner, pvt->vad, f);
791 ast_channel_unlock(pvt->owner);
794 ast_log(LOG_NOTICE, "Unable to process inband DTMF while channel is locked\n");
795 } else if (pvt->nativeformats && !pvt->noInbandDtmf) {
796 ast_log(LOG_NOTICE, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(f->subclass));
797 pvt->noInbandDtmf = 1;
799 if (f &&(f->frametype == AST_FRAME_DTMF)) {
801 ast_log(LOG_DTMF, "Received in-band digit %c.\n", f->subclass);
809 static struct ast_frame *oh323_read(struct ast_channel *c)
811 struct ast_frame *fr;
812 struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
813 ast_mutex_lock(&pvt->lock);
814 __oh323_update_info(c, pvt);
817 fr = oh323_rtp_read(pvt);
821 fr = ast_rtcp_read(pvt->rtp);
823 fr = &ast_null_frame;
826 ast_log(LOG_ERROR, "Unable to handle fd %d on channel %s\n", c->fdno, c->name);
827 fr = &ast_null_frame;
830 ast_mutex_unlock(&pvt->lock);
834 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
836 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
838 if (frame->frametype != AST_FRAME_VOICE) {
839 if (frame->frametype == AST_FRAME_IMAGE) {
842 ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
846 if (!(frame->subclass & c->nativeformats)) {
847 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
848 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
853 ast_mutex_lock(&pvt->lock);
854 if (pvt->rtp && !pvt->recvonly)
855 res = ast_rtp_write(pvt->rtp, frame);
856 __oh323_update_info(c, pvt);
857 ast_mutex_unlock(&pvt->lock);
862 static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen)
865 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
866 char *token = (char *)NULL;
870 ast_mutex_lock(&pvt->lock);
871 token = (pvt->cd.call_token ? ast_strdup(pvt->cd.call_token) : NULL);
872 got_progress = pvt->got_progress;
873 if (condition == AST_CONTROL_PROGRESS)
874 pvt->got_progress = 1;
875 else if ((condition == AST_CONTROL_BUSY) || (condition == AST_CONTROL_CONGESTION))
876 pvt->alreadygone = 1;
877 ast_mutex_unlock(&pvt->lock);
880 ast_debug(1, "OH323: Indicating %d on %s (%s)\n", condition, token, c->name);
883 case AST_CONTROL_RINGING:
884 if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
885 h323_send_alerting(token);
886 res = (got_progress ? 0 : -1); /* Do not simulate any audio tones if we got PROGRESS message */
889 case AST_CONTROL_PROGRESS:
890 if (c->_state != AST_STATE_UP) {
891 /* Do not send PROGRESS message more than once */
893 h323_send_progress(token);
897 case AST_CONTROL_BUSY:
898 if (c->_state != AST_STATE_UP) {
899 h323_answering_call(token, 1);
900 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
904 case AST_CONTROL_CONGESTION:
905 if (c->_state != AST_STATE_UP) {
906 h323_answering_call(token, 1);
907 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
911 case AST_CONTROL_HOLD:
912 h323_hold_call(token, 1);
913 /* We should start MOH only if remote party isn't provide audio for us */
914 ast_moh_start(c, data, NULL);
917 case AST_CONTROL_UNHOLD:
918 h323_hold_call(token, 0);
922 case AST_CONTROL_PROCEEDING:
926 ast_log(LOG_WARNING, "OH323: Don't know how to indicate condition %d on %s\n", condition, token);
931 ast_debug(1, "OH323: Indicated %d on %s, res=%d\n", condition, token, res);
934 oh323_update_info(c);
939 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
941 struct oh323_pvt *pvt = (struct oh323_pvt *) newchan->tech_pvt;
943 ast_mutex_lock(&pvt->lock);
944 if (pvt->owner != oldchan) {
945 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, pvt->owner);
948 pvt->owner = newchan;
949 ast_mutex_unlock(&pvt->lock);
953 static int __oh323_rtp_create(struct oh323_pvt *pvt)
955 struct in_addr our_addr;
960 if (ast_find_ourip(&our_addr, bindaddr)) {
961 ast_mutex_unlock(&pvt->lock);
962 ast_log(LOG_ERROR, "Unable to locate local IP address for RTP stream\n");
965 pvt->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, our_addr);
967 ast_mutex_unlock(&pvt->lock);
968 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
972 ast_debug(1, "Created RTP channel\n");
974 ast_rtp_setqos(pvt->rtp, tos, cos, "H323 RTP");
977 ast_debug(1, "Setting NAT on RTP to %d\n", pvt->options.nat);
978 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
980 if (pvt->dtmf_pt[0] > 0)
981 ast_rtp_set_rtpmap_type(pvt->rtp, pvt->dtmf_pt[0], "audio", "telephone-event", 0);
982 if (pvt->dtmf_pt[1] > 0)
983 ast_rtp_set_rtpmap_type(pvt->rtp, pvt->dtmf_pt[1], "audio", "cisco-telephone-event", 0);
985 if (pvt->peercapability)
986 ast_rtp_codec_setpref(pvt->rtp, &pvt->peer_prefs);
988 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
989 ast_jb_configure(pvt->owner, &global_jbconf);
990 ast_channel_set_fd(pvt->owner, 0, ast_rtp_fd(pvt->rtp));
991 ast_channel_set_fd(pvt->owner, 1, ast_rtcp_fd(pvt->rtp));
992 ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
993 ast_channel_unlock(pvt->owner);
995 pvt->update_rtp_info = 1;
1000 /*! \brief Private structure should be locked on a call */
1001 static struct ast_channel *__oh323_new(struct oh323_pvt *pvt, int state, const char *host)
1003 struct ast_channel *ch;
1004 char *cid_num, *cid_name;
1007 if (!ast_strlen_zero(pvt->options.cid_num))
1008 cid_num = pvt->options.cid_num;
1010 cid_num = pvt->cd.call_source_e164;
1012 if (!ast_strlen_zero(pvt->options.cid_name))
1013 cid_name = pvt->options.cid_name;
1015 cid_name = pvt->cd.call_source_name;
1017 /* Don't hold a oh323_pvt lock while we allocate a chanel */
1018 ast_mutex_unlock(&pvt->lock);
1019 ch = ast_channel_alloc(1, state, cid_num, cid_name, pvt->accountcode, pvt->exten, pvt->context, pvt->amaflags, "H323/%s", host);
1020 /* Update usage counter */
1021 ast_module_ref(ast_module_info->self);
1022 ast_mutex_lock(&pvt->lock);
1024 ch->tech = &oh323_tech;
1025 if (!(fmt = pvt->jointcapability) && !(fmt = pvt->options.capability))
1026 fmt = global_options.capability;
1027 ch->nativeformats = ast_codec_choose(&pvt->options.prefs, fmt, 1)/* | (pvt->jointcapability & AST_FORMAT_VIDEO_MASK)*/;
1028 pvt->nativeformats = ch->nativeformats;
1029 fmt = ast_best_codec(ch->nativeformats);
1030 ch->writeformat = fmt;
1031 ch->rawwriteformat = fmt;
1032 ch->readformat = fmt;
1033 ch->rawreadformat = fmt;
1035 __oh323_rtp_create(pvt);
1037 ast_channel_set_fd(ch, 0, ast_rtp_fd(pvt->rtp));
1038 ast_channel_set_fd(ch, 1, ast_rtcp_fd(pvt->rtp));
1040 #ifdef VIDEO_SUPPORT
1042 ast_channel_set_fd(ch, 2, ast_rtp_fd(pvt->vrtp));
1043 ast_channel_set_fd(ch, 3, ast_rtcp_fd(pvt->vrtp));
1048 ast_channel_set_fd(ch, 4, ast_udptl_fd(pvt->udptl));
1051 if (state == AST_STATE_RING) {
1054 /* Allocate dsp for in-band DTMF support */
1055 if (pvt->options.dtmfmode & H323_DTMF_INBAND) {
1056 pvt->vad = ast_dsp_new();
1057 ast_dsp_set_features(pvt->vad, DSP_FEATURE_DTMF_DETECT);
1059 /* Register channel functions. */
1061 /* Set the owner of this channel */
1064 ast_copy_string(ch->context, pvt->context, sizeof(ch->context));
1065 ast_copy_string(ch->exten, pvt->exten, sizeof(ch->exten));
1067 if (!ast_strlen_zero(pvt->accountcode)) {
1068 ast_string_field_set(ch, accountcode, pvt->accountcode);
1070 if (pvt->amaflags) {
1071 ch->amaflags = pvt->amaflags;
1074 /* Don't use ast_set_callerid() here because it will
1075 * generate a needless NewCallerID event */
1076 ch->cid.cid_ani = ast_strdup(cid_num);
1078 if (pvt->cd.redirect_reason >= 0) {
1079 ch->cid.cid_rdnis = ast_strdup(pvt->cd.redirect_number);
1080 pbx_builtin_setvar_helper(ch, "PRIREDIRECTREASON", redirectingreason2str(pvt->cd.redirect_reason));
1082 ch->cid.cid_pres = pvt->cd.presentation;
1083 ch->cid.cid_ton = pvt->cd.type_of_number;
1085 if (!ast_strlen_zero(pvt->exten) && strcmp(pvt->exten, "s")) {
1086 ch->cid.cid_dnid = ast_strdup(pvt->exten);
1088 if (pvt->cd.transfer_capability >= 0)
1089 ch->transfercapability = pvt->cd.transfer_capability;
1090 if (state != AST_STATE_DOWN) {
1091 if (ast_pbx_start(ch)) {
1092 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
1098 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1103 static struct oh323_pvt *oh323_alloc(int callid)
1105 struct oh323_pvt *pvt;
1107 pvt = ast_calloc(1, sizeof(*pvt));
1109 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
1112 pvt->cd.redirect_reason = -1;
1113 pvt->cd.transfer_capability = -1;
1114 /* Ensure the call token is allocated for outgoing call */
1116 if ((pvt->cd).call_token == NULL) {
1117 (pvt->cd).call_token = ast_calloc(1, 128);
1119 if (!pvt->cd.call_token) {
1120 ast_log(LOG_ERROR, "Not enough memory to alocate call token\n");
1121 ast_rtp_destroy(pvt->rtp);
1125 memset((char *)(pvt->cd).call_token, 0, 128);
1126 pvt->cd.call_reference = callid;
1128 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
1129 pvt->jointcapability = pvt->options.capability;
1130 if (pvt->options.dtmfmode & (H323_DTMF_RFC2833 | H323_DTMF_CISCO)) {
1131 pvt->nonCodecCapability |= AST_RTP_DTMF;
1133 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1135 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
1136 pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->update_rtp_info = pvt->DTMFsched = -1;
1137 ast_mutex_init(&pvt->lock);
1138 /* Add to interface list */
1139 ast_mutex_lock(&iflock);
1142 ast_mutex_unlock(&iflock);
1146 static struct oh323_pvt *find_call_locked(int call_reference, const char *token)
1148 struct oh323_pvt *pvt;
1150 ast_mutex_lock(&iflock);
1153 if (!pvt->needdestroy && ((signed int)pvt->cd.call_reference == call_reference)) {
1154 /* Found the call */
1155 if ((token != NULL) && (!strcmp(pvt->cd.call_token, token))) {
1156 ast_mutex_lock(&pvt->lock);
1157 ast_mutex_unlock(&iflock);
1159 } else if (token == NULL) {
1160 ast_log(LOG_WARNING, "Call Token is NULL\n");
1161 ast_mutex_lock(&pvt->lock);
1162 ast_mutex_unlock(&iflock);
1168 ast_mutex_unlock(&iflock);
1172 static int update_state(struct oh323_pvt *pvt, int state, int signal)
1176 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1178 ast_setstate(pvt->owner, state);
1180 ast_queue_control(pvt->owner, signal);
1181 ast_channel_unlock(pvt->owner);
1186 pvt->newstate = state;
1188 pvt->newcontrol = signal;
1193 static struct oh323_alias *build_alias(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1195 struct oh323_alias *alias;
1198 alias = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&aliasl, name, name, 0, 0, strcasecmp);
1203 if (!(alias = ast_calloc(1, sizeof(*alias))))
1208 ast_copy_string(alias->name, name, sizeof(alias->name));
1209 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1210 if (!strcasecmp(v->name, "e164")) {
1211 ast_copy_string(alias->e164, v->value, sizeof(alias->e164));
1212 } else if (!strcasecmp(v->name, "prefix")) {
1213 ast_copy_string(alias->prefix, v->value, sizeof(alias->prefix));
1214 } else if (!strcasecmp(v->name, "context")) {
1215 ast_copy_string(alias->context, v->value, sizeof(alias->context));
1216 } else if (!strcasecmp(v->name, "secret")) {
1217 ast_copy_string(alias->secret, v->value, sizeof(alias->secret));
1219 if (strcasecmp(v->value, "h323")) {
1220 ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->name);
1224 ASTOBJ_UNMARK(alias);
1228 static struct oh323_alias *realtime_alias(const char *alias)
1230 struct ast_variable *var, *tmp;
1231 struct oh323_alias *a;
1233 var = ast_load_realtime("h323", "name", alias, NULL);
1238 for (tmp = var; tmp; tmp = tmp->next) {
1239 if (!strcasecmp(tmp->name, "type") &&
1240 !(!strcasecmp(tmp->value, "alias") || !strcasecmp(tmp->value, "h323"))) {
1241 ast_variables_destroy(var);
1246 a = build_alias(alias, var, NULL, 1);
1248 ast_variables_destroy(var);
1253 static int update_common_options(struct ast_variable *v, struct call_options *options)
1258 if (!strcasecmp(v->name, "allow")) {
1259 ast_parse_allow_disallow(&options->prefs, &options->capability, v->value, 1);
1260 } else if (!strcasecmp(v->name, "disallow")) {
1261 ast_parse_allow_disallow(&options->prefs, &options->capability, v->value, 0);
1262 } else if (!strcasecmp(v->name, "dtmfmode")) {
1263 val = ast_strdupa(v->value);
1264 if ((opt = strchr(val, ':')) != (char *)NULL) {
1268 if (!strcasecmp(v->value, "inband")) {
1269 options->dtmfmode |= H323_DTMF_INBAND;
1270 } else if (!strcasecmp(val, "rfc2833")) {
1271 options->dtmfmode |= H323_DTMF_RFC2833;
1273 options->dtmfcodec[0] = H323_DTMF_RFC2833_PT;
1274 } else if ((tmp >= 96) && (tmp < 128)) {
1275 options->dtmfcodec[0] = tmp;
1277 options->dtmfcodec[0] = H323_DTMF_RFC2833_PT;
1278 ast_log(LOG_WARNING, "Unknown rfc2833 payload %s specified at line %d, using default %d\n", opt, v->lineno, options->dtmfcodec[0]);
1280 } else if (!strcasecmp(val, "cisco")) {
1281 options->dtmfmode |= H323_DTMF_CISCO;
1283 options->dtmfcodec[1] = H323_DTMF_CISCO_PT;
1284 } else if ((tmp >= 96) && (tmp < 128)) {
1285 options->dtmfcodec[1] = tmp;
1287 options->dtmfcodec[1] = H323_DTMF_CISCO_PT;
1288 ast_log(LOG_WARNING, "Unknown Cisco DTMF payload %s specified at line %d, using default %d\n", opt, v->lineno, options->dtmfcodec[1]);
1290 } else if (!strcasecmp(v->value, "h245-signal")) {
1291 options->dtmfmode |= H323_DTMF_SIGNAL;
1293 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' at line %d\n", v->value, v->lineno);
1295 } else if (!strcasecmp(v->name, "dtmfcodec")) {
1296 ast_log(LOG_NOTICE, "Option %s at line %d is deprecated. Use dtmfmode=rfc2833[:<payload>] instead.\n", v->name, v->lineno);
1297 tmp = atoi(v->value);
1299 ast_log(LOG_WARNING, "Invalid %s value %s at line %d\n", v->name, v->value, v->lineno);
1301 options->dtmfcodec[0] = tmp;
1302 } else if (!strcasecmp(v->name, "bridge")) {
1303 options->bridge = ast_true(v->value);
1304 } else if (!strcasecmp(v->name, "nat")) {
1305 options->nat = ast_true(v->value);
1306 } else if (!strcasecmp(v->name, "fastStart")) {
1307 options->fastStart = ast_true(v->value);
1308 } else if (!strcasecmp(v->name, "h245Tunneling")) {
1309 options->h245Tunneling = ast_true(v->value);
1310 } else if (!strcasecmp(v->name, "silenceSuppression")) {
1311 options->silenceSuppression = ast_true(v->value);
1312 } else if (!strcasecmp(v->name, "progress_setup")) {
1313 tmp = atoi(v->value);
1314 if ((tmp != 0) && (tmp != 1) && (tmp != 3) && (tmp != 8)) {
1315 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d, assuming 0\n", v->value, v->name, v->lineno);
1318 options->progress_setup = tmp;
1319 } else if (!strcasecmp(v->name, "progress_alert")) {
1320 tmp = atoi(v->value);
1321 if ((tmp != 0) && (tmp != 1) && (tmp != 8)) {
1322 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d, assuming 0\n", v->value, v->name, v->lineno);
1325 options->progress_alert = tmp;
1326 } else if (!strcasecmp(v->name, "progress_audio")) {
1327 options->progress_audio = ast_true(v->value);
1328 } else if (!strcasecmp(v->name, "callerid")) {
1329 ast_callerid_split(v->value, options->cid_name, sizeof(options->cid_name), options->cid_num, sizeof(options->cid_num));
1330 } else if (!strcasecmp(v->name, "fullname")) {
1331 ast_copy_string(options->cid_name, v->value, sizeof(options->cid_name));
1332 } else if (!strcasecmp(v->name, "cid_number")) {
1333 ast_copy_string(options->cid_num, v->value, sizeof(options->cid_num));
1334 } else if (!strcasecmp(v->name, "tunneling")) {
1335 if (!strcasecmp(v->value, "none"))
1336 options->tunnelOptions = 0;
1337 else if (!strcasecmp(v->value, "cisco"))
1338 options->tunnelOptions |= H323_TUNNEL_CISCO;
1339 else if (!strcasecmp(v->value, "qsig"))
1340 options->tunnelOptions |= H323_TUNNEL_QSIG;
1342 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d\n", v->value, v->name, v->lineno);
1343 } else if (!strcasecmp(v->name, "hold")) {
1344 if (!strcasecmp(v->value, "none"))
1345 options->holdHandling = ~0;
1346 else if (!strcasecmp(v->value, "notify"))
1347 options->holdHandling |= H323_HOLD_NOTIFY;
1348 else if (!strcasecmp(v->value, "q931only"))
1349 options->holdHandling |= H323_HOLD_NOTIFY | H323_HOLD_Q931ONLY;
1350 else if (!strcasecmp(v->value, "h450"))
1351 options->holdHandling |= H323_HOLD_H450;
1353 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d\n", v->value, v->name, v->lineno);
1360 static struct oh323_user *build_user(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1362 struct oh323_user *user;
1363 struct ast_ha *oldha;
1367 user = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&userl, name, name, 0, 0, strcmp);
1372 if (!(user = ast_calloc(1, sizeof(*user))))
1377 user->ha = (struct ast_ha *)NULL;
1378 memcpy(&user->options, &global_options, sizeof(user->options));
1379 user->options.dtmfmode = 0;
1380 user->options.holdHandling = 0;
1381 /* Set default context */
1382 ast_copy_string(user->context, default_context, sizeof(user->context));
1384 ast_copy_string(user->name, name, sizeof(user->name));
1386 #if 0 /* XXX Port channel variables functionality from chan_sip XXX */
1387 if (user->chanvars) {
1388 ast_variables_destroy(user->chanvars);
1389 user->chanvars = NULL;
1393 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1394 if (!update_common_options(v, &user->options))
1396 if (!strcasecmp(v->name, "context")) {
1397 ast_copy_string(user->context, v->value, sizeof(user->context));
1398 } else if (!strcasecmp(v->name, "secret")) {
1399 ast_copy_string(user->secret, v->value, sizeof(user->secret));
1400 } else if (!strcasecmp(v->name, "accountcode")) {
1401 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
1402 } else if (!strcasecmp(v->name, "host")) {
1403 if (!strcasecmp(v->value, "dynamic")) {
1404 ast_log(LOG_ERROR, "A dynamic host on a type=user does not make any sense\n");
1405 ASTOBJ_UNREF(user, oh323_destroy_user);
1407 } else if (ast_get_ip(&user->addr, v->value)) {
1408 ASTOBJ_UNREF(user, oh323_destroy_user);
1411 /* Let us know we need to use ip authentication */
1413 } else if (!strcasecmp(v->name, "amaflags")) {
1414 format = ast_cdr_amaflags2int(v->value);
1416 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
1418 user->amaflags = format;
1420 } else if (!strcasecmp(v->name, "permit") ||
1421 !strcasecmp(v->name, "deny")) {
1424 user->ha = ast_append_ha(v->name, v->value, user->ha, &ha_error);
1426 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
1429 if (!user->options.dtmfmode)
1430 user->options.dtmfmode = global_options.dtmfmode;
1431 if (user->options.holdHandling == ~0)
1432 user->options.holdHandling = 0;
1433 else if (!user->options.holdHandling)
1434 user->options.holdHandling = global_options.holdHandling;
1435 ASTOBJ_UNMARK(user);
1440 static struct oh323_user *realtime_user(const call_details_t *cd)
1442 struct ast_variable *var, *tmp;
1443 struct oh323_user *user;
1444 const char *username;
1447 var = ast_load_realtime("h323", "name", username = cd->call_source_aliases, NULL);
1449 username = (char *)NULL;
1450 var = ast_load_realtime("h323", "host", cd->sourceIp, NULL);
1456 for (tmp = var; tmp; tmp = tmp->next) {
1457 if (!strcasecmp(tmp->name, "type") &&
1458 !(!strcasecmp(tmp->value, "user") || !strcasecmp(tmp->value, "friend"))) {
1459 ast_variables_destroy(var);
1461 } else if (!username && !strcasecmp(tmp->name, "name"))
1462 username = tmp->value;
1466 ast_log(LOG_WARNING, "Cannot determine user name for IP address %s\n", cd->sourceIp);
1467 ast_variables_destroy(var);
1471 user = build_user(username, var, NULL, 1);
1473 ast_variables_destroy(var);
1478 static struct oh323_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1480 struct oh323_peer *peer;
1481 struct ast_ha *oldha;
1484 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
1489 if (!(peer = ast_calloc(1, sizeof(*peer))))
1495 memcpy(&peer->options, &global_options, sizeof(peer->options));
1496 peer->options.dtmfmode = 0;
1497 peer->options.holdHandling = 0;
1498 peer->addr.sin_port = htons(h323_signalling_port);
1499 peer->addr.sin_family = AF_INET;
1501 ast_copy_string(peer->name, name, sizeof(peer->name));
1503 #if 0 /* XXX Port channel variables functionality from chan_sip XXX */
1504 if (peer->chanvars) {
1505 ast_variables_destroy(peer->chanvars);
1506 peer->chanvars = NULL;
1509 /* Default settings for mailbox */
1510 peer->mailbox[0] = '\0';
1512 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1513 if (!update_common_options(v, &peer->options))
1515 if (!strcasecmp(v->name, "host")) {
1516 if (!strcasecmp(v->value, "dynamic")) {
1517 ast_log(LOG_ERROR, "Dynamic host configuration not implemented.\n");
1518 ASTOBJ_UNREF(peer, oh323_destroy_peer);
1521 if (ast_get_ip(&peer->addr, v->value)) {
1522 ast_log(LOG_ERROR, "Could not determine IP for %s\n", v->value);
1523 ASTOBJ_UNREF(peer, oh323_destroy_peer);
1526 } else if (!strcasecmp(v->name, "port")) {
1527 peer->addr.sin_port = htons(atoi(v->value));
1528 } else if (!strcasecmp(v->name, "permit") ||
1529 !strcasecmp(v->name, "deny")) {
1532 peer->ha = ast_append_ha(v->name, v->value, peer->ha, &ha_error);
1534 ast_log(LOG_ERROR, "Bad ACL entry in configuration line %d : %s\n", v->lineno, v->value);
1535 } else if (!strcasecmp(v->name, "mailbox")) {
1536 ast_copy_string(peer->mailbox, v->value, sizeof(peer->mailbox));
1539 if (!peer->options.dtmfmode)
1540 peer->options.dtmfmode = global_options.dtmfmode;
1541 if (peer->options.holdHandling == ~0)
1542 peer->options.holdHandling = 0;
1543 else if (!peer->options.holdHandling)
1544 peer->options.holdHandling = global_options.holdHandling;
1545 ASTOBJ_UNMARK(peer);
1550 static struct oh323_peer *realtime_peer(const char *peername, struct sockaddr_in *sin)
1552 struct oh323_peer *peer;
1553 struct ast_variable *var;
1554 struct ast_variable *tmp;
1557 /* First check on peer name */
1559 var = ast_load_realtime("h323", "name", peername, addr = NULL);
1560 else if (sin) /* Then check on IP address for dynamic peers */
1561 var = ast_load_realtime("h323", "host", addr = ast_inet_ntoa(sin->sin_addr), NULL);
1568 for (tmp = var; tmp; tmp = tmp->next) {
1569 /* If this is type=user, then skip this object. */
1570 if (!strcasecmp(tmp->name, "type") &&
1571 !(!strcasecmp(tmp->value, "peer") || !strcasecmp(tmp->value, "friend"))) {
1572 ast_variables_destroy(var);
1574 } else if (!peername && !strcasecmp(tmp->name, "name")) {
1575 peername = tmp->value;
1579 if (!peername) { /* Did not find peer in realtime */
1580 ast_log(LOG_WARNING, "Cannot determine peer name for IP address %s\n", addr);
1581 ast_variables_destroy(var);
1585 /* Peer found in realtime, now build it in memory */
1586 peer = build_peer(peername, var, NULL, 1);
1588 ast_variables_destroy(var);
1593 static int oh323_addrcmp_str(struct in_addr inaddr, char *addr)
1595 return strcmp(ast_inet_ntoa(inaddr), addr);
1598 static struct oh323_user *find_user(const call_details_t *cd, int realtime)
1600 struct oh323_user *u;
1603 u = ASTOBJ_CONTAINER_FIND(&userl, cd->call_source_aliases);
1605 u = ASTOBJ_CONTAINER_FIND_FULL(&userl, cd->sourceIp, addr.sin_addr, 0, 0, oh323_addrcmp_str);
1608 u = realtime_user(cd);
1610 if (!u && h323debug)
1611 ast_debug(1, "Could not find user by name %s or address %s\n", cd->call_source_aliases, cd->sourceIp);
1616 static int oh323_addrcmp(struct sockaddr_in addr, struct sockaddr_in *sin)
1623 res = inaddrcmp(&addr , sin);
1628 static struct oh323_peer *find_peer(const char *peer, struct sockaddr_in *sin, int realtime)
1630 struct oh323_peer *p;
1633 p = ASTOBJ_CONTAINER_FIND(&peerl, peer);
1635 p = ASTOBJ_CONTAINER_FIND_FULL(&peerl, sin, addr, 0, 0, oh323_addrcmp);
1638 p = realtime_peer(peer, sin);
1640 if (!p && h323debug)
1641 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>"));
1646 static int create_addr(struct oh323_pvt *pvt, char *opeer)
1649 struct ast_hostent ahp;
1650 struct oh323_peer *p;
1655 char peer[256] = "";
1657 ast_copy_string(peer, opeer, sizeof(peer));
1658 port = strchr(peer, ':');
1663 pvt->sa.sin_family = AF_INET;
1664 p = find_peer(peer, NULL, 1);
1667 memcpy(&pvt->options, &p->options, sizeof(pvt->options));
1668 pvt->jointcapability = pvt->options.capability;
1669 if (pvt->options.dtmfmode) {
1670 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1671 pvt->nonCodecCapability |= AST_RTP_DTMF;
1673 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1676 if (p->addr.sin_addr.s_addr) {
1677 pvt->sa.sin_addr = p->addr.sin_addr;
1678 pvt->sa.sin_port = p->addr.sin_port;
1680 ASTOBJ_UNREF(p, oh323_destroy_peer);
1685 portno = atoi(port);
1687 portno = h323_signalling_port;
1689 hp = ast_gethostbyname(hostn, &ahp);
1691 memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
1692 pvt->sa.sin_port = htons(portno);
1693 /* Look peer by address */
1694 p = find_peer(NULL, &pvt->sa, 1);
1695 memcpy(&pvt->options, (p ? &p->options : &global_options), sizeof(pvt->options));
1696 pvt->jointcapability = pvt->options.capability;
1698 ASTOBJ_UNREF(p, oh323_destroy_peer);
1700 if (pvt->options.dtmfmode) {
1701 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1702 pvt->nonCodecCapability |= AST_RTP_DTMF;
1704 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1709 ast_log(LOG_WARNING, "No such host: %s\n", peer);
1712 } else if (!found) {
1718 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause)
1721 struct oh323_pvt *pvt;
1722 struct ast_channel *tmpc = NULL;
1723 char *dest = (char *)data;
1725 char *h323id = NULL;
1726 char tmp[256], tmp1[256];
1729 ast_debug(1, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
1731 pvt = oh323_alloc(0);
1733 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
1737 format &= AST_FORMAT_AUDIO_MASK;
1739 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
1742 *cause = AST_CAUSE_INCOMPATIBLE_DESTINATION;
1745 ast_copy_string(tmp, dest, sizeof(tmp));
1746 host = strchr(tmp, '@');
1752 ext = strrchr(tmp, '/');
1757 strtok_r(host, "/", &(h323id));
1758 if (!ast_strlen_zero(h323id)) {
1759 h323_set_id(h323id);
1762 ast_copy_string(pvt->exten, ext, sizeof(pvt->exten));
1765 ast_debug(1, "Extension: %s Host: %s\n", pvt->exten, host);
1767 if (gatekeeper_disable) {
1768 if (create_addr(pvt, host)) {
1771 *cause = AST_CAUSE_DESTINATION_OUT_OF_ORDER;
1776 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
1777 pvt->jointcapability = pvt->options.capability;
1778 if (pvt->options.dtmfmode) {
1779 if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1780 pvt->nonCodecCapability |= AST_RTP_DTMF;
1782 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1787 ast_mutex_lock(&caplock);
1788 /* Generate unique channel identifier */
1789 snprintf(tmp1, sizeof(tmp1)-1, "%s-%u", host, ++unique);
1790 tmp1[sizeof(tmp1)-1] = '\0';
1791 ast_mutex_unlock(&caplock);
1793 ast_mutex_lock(&pvt->lock);
1794 tmpc = __oh323_new(pvt, AST_STATE_DOWN, tmp1);
1795 ast_mutex_unlock(&pvt->lock);
1799 *cause = AST_CAUSE_NORMAL_TEMPORARY_FAILURE;
1801 ast_update_use_count();
1806 /*! \brief Find a call by alias */
1807 static struct oh323_alias *find_alias(const char *source_aliases, int realtime)
1809 struct oh323_alias *a;
1811 a = ASTOBJ_CONTAINER_FIND(&aliasl, source_aliases);
1814 a = realtime_alias(source_aliases);
1820 * Callback for sending digits from H.323 up to asterisk
1823 static int receive_digit(unsigned call_reference, char digit, const char *token, int duration)
1825 struct oh323_pvt *pvt;
1828 pvt = find_call_locked(call_reference, token);
1830 ast_log(LOG_ERROR, "Received digit '%c' (%u ms) for call %s without private structure\n", digit, duration, token);
1834 ast_log(LOG_DTMF, "Received %s digit '%c' (%u ms) for call %s\n", (digit == ' ' ? "update for" : "new"), (digit == ' ' ? pvt->curDTMF : digit), duration, token);
1836 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1838 res = ast_queue_control(pvt->owner, AST_CONTROL_FLASH);
1840 struct ast_frame f = {
1841 .frametype = AST_FRAME_DTMF_END,
1843 .samples = duration * 8,
1845 .src = "SEND_DIGIT",
1847 if (digit == ' ') { /* signalUpdate message */
1848 f.subclass = pvt->curDTMF;
1849 if (pvt->DTMFsched >= 0) {
1850 ast_sched_del(sched, pvt->DTMFsched);
1851 pvt->DTMFsched = -1;
1853 } else { /* Regular input or signal message */
1854 if (pvt->DTMFsched >= 0) {
1855 /* We still don't send DTMF END from previous event, send it now */
1856 ast_sched_del(sched, pvt->DTMFsched);
1857 pvt->DTMFsched = -1;
1858 f.subclass = pvt->curDTMF;
1859 f.samples = f.len = 0;
1860 ast_queue_frame(pvt->owner, &f);
1861 /* Restore values */
1863 f.samples = duration * 8;
1866 if (duration) { /* This is a signal, signalUpdate follows */
1867 f.frametype = AST_FRAME_DTMF_BEGIN;
1868 pvt->DTMFsched = ast_sched_add(sched, duration, oh323_simulate_dtmf_end, pvt);
1870 ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", duration, pvt->DTMFsched);
1872 pvt->curDTMF = digit;
1874 res = ast_queue_frame(pvt->owner, &f);
1876 ast_channel_unlock(pvt->owner);
1879 pvt->newcontrol = AST_CONTROL_FLASH;
1881 pvt->newduration = duration;
1882 pvt->newdigit = digit;
1886 ast_mutex_unlock(&pvt->lock);
1891 * Callback function used to inform the H.323 stack of the local rtp ip/port details
1893 * \return Returns the local RTP information
1895 static struct rtp_info *external_rtp_create(unsigned call_reference, const char * token)
1897 struct oh323_pvt *pvt;
1898 struct sockaddr_in us;
1899 struct rtp_info *info;
1901 info = ast_calloc(1, sizeof(*info));
1903 ast_log(LOG_ERROR, "Unable to allocated info structure, this is very bad\n");
1906 pvt = find_call_locked(call_reference, token);
1909 ast_log(LOG_ERROR, "Unable to find call %s(%d)\n", token, call_reference);
1913 __oh323_rtp_create(pvt);
1915 ast_mutex_unlock(&pvt->lock);
1917 ast_log(LOG_ERROR, "No RTP stream is available for call %s (%d)", token, call_reference);
1920 /* figure out our local RTP port and tell the H.323 stack about it */
1921 ast_rtp_get_us(pvt->rtp, &us);
1922 ast_mutex_unlock(&pvt->lock);
1924 ast_copy_string(info->addr, ast_inet_ntoa(us.sin_addr), sizeof(info->addr));
1925 info->port = ntohs(us.sin_port);
1927 ast_debug(1, "Sending RTP 'US' %s:%d\n", info->addr, info->port);
1932 * Definition taken from rtp.c for rtpPayloadType because we need it here.
1935 struct rtpPayloadType {
1936 int isAstFormat; /* whether the following code is an AST_FORMAT */
1941 * Call-back function passing remote ip/port information from H.323 to asterisk
1945 static void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort, const char *token, int pt)
1947 struct oh323_pvt *pvt;
1948 struct sockaddr_in them;
1949 struct rtpPayloadType rtptype;
1950 int nativeformats_changed;
1951 enum { NEED_NONE, NEED_HOLD, NEED_UNHOLD } rtp_change = NEED_NONE;
1954 ast_debug(1, "Setting up RTP connection for %s\n", token);
1956 /* Find the call or allocate a private structure if call not found */
1957 pvt = find_call_locked(call_reference, token);
1959 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1962 if (pvt->alreadygone) {
1963 ast_mutex_unlock(&pvt->lock);
1968 __oh323_rtp_create(pvt);
1970 if ((pt == 2) && (pvt->jointcapability & AST_FORMAT_G726_AAL2)) {
1971 ast_rtp_set_rtpmap_type(pvt->rtp, pt, "audio", "G726-32", AST_RTP_OPT_G726_NONSTANDARD);
1974 them.sin_family = AF_INET;
1975 /* only works for IPv4 */
1976 them.sin_addr.s_addr = inet_addr(remoteIp);
1977 them.sin_port = htons(remotePort);
1979 if (them.sin_addr.s_addr) {
1980 ast_rtp_set_peer(pvt->rtp, &them);
1981 if (pvt->recvonly) {
1983 rtp_change = NEED_UNHOLD;
1986 ast_rtp_stop(pvt->rtp);
1987 if (!pvt->recvonly) {
1989 rtp_change = NEED_HOLD;
1993 /* Change native format to reflect information taken from OLC/OLCAck */
1994 nativeformats_changed = 0;
1995 if (pt != 128 && pvt->rtp) { /* Payload type is invalid, so try to use previously decided */
1996 rtptype = ast_rtp_lookup_pt(pvt->rtp, pt);
1998 ast_debug(1, "Native format is set to %d from %d by RTP payload type %d\n", rtptype.code, pvt->nativeformats, pt);
1999 if (pvt->nativeformats != rtptype.code) {
2000 pvt->nativeformats = rtptype.code;
2001 nativeformats_changed = 1;
2003 } else if (h323debug)
2004 ast_log(LOG_NOTICE, "Payload type is unknown, formats isn't changed\n");
2006 /* Don't try to lock the channel if nothing changed */
2007 if (nativeformats_changed || pvt->options.progress_audio || (rtp_change != NEED_NONE)) {
2008 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
2009 /* Re-build translation path only if native format(s) has been changed */
2010 if (pvt->owner->nativeformats != pvt->nativeformats) {
2012 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);
2013 pvt->owner->nativeformats = pvt->nativeformats;
2014 ast_set_read_format(pvt->owner, pvt->owner->readformat);
2015 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
2017 if (pvt->options.progress_audio)
2018 ast_queue_control(pvt->owner, AST_CONTROL_PROGRESS);
2019 switch (rtp_change) {
2021 ast_queue_control(pvt->owner, AST_CONTROL_HOLD);
2024 ast_queue_control(pvt->owner, AST_CONTROL_UNHOLD);
2029 ast_channel_unlock(pvt->owner);
2032 if (pvt->options.progress_audio)
2033 pvt->newcontrol = AST_CONTROL_PROGRESS;
2034 else if (rtp_change == NEED_HOLD)
2035 pvt->newcontrol = AST_CONTROL_HOLD;
2036 else if (rtp_change == NEED_UNHOLD)
2037 pvt->newcontrol = AST_CONTROL_UNHOLD;
2039 ast_debug(1, "RTP connection preparation for %s is pending...\n", token);
2042 ast_mutex_unlock(&pvt->lock);
2045 ast_debug(1, "RTP connection prepared for %s\n", token);
2051 * Call-back function to signal asterisk that the channel has been answered
2054 static void connection_made(unsigned call_reference, const char *token)
2056 struct oh323_pvt *pvt;
2059 ast_debug(1, "Call %s answered\n", token);
2061 pvt = find_call_locked(call_reference, token);
2063 ast_log(LOG_ERROR, "Something is wrong: connection\n");
2067 /* Inform asterisk about remote party connected only on outgoing calls */
2068 if (!pvt->outgoing) {
2069 ast_mutex_unlock(&pvt->lock);
2072 /* Do not send ANSWER message more than once */
2073 if (!pvt->connection_established) {
2074 pvt->connection_established = 1;
2075 update_state(pvt, -1, AST_CONTROL_ANSWER);
2077 ast_mutex_unlock(&pvt->lock);
2081 static int progress(unsigned call_reference, const char *token, int inband)
2083 struct oh323_pvt *pvt;
2086 ast_debug(1, "Received ALERT/PROGRESS message for %s tones\n", (inband ? "inband" : "self-generated"));
2088 pvt = find_call_locked(call_reference, token);
2090 ast_log(LOG_ERROR, "Private structure not found in progress.\n");
2094 ast_mutex_unlock(&pvt->lock);
2095 ast_log(LOG_ERROR, "No Asterisk channel associated with private structure.\n");
2098 update_state(pvt, -1, (inband ? AST_CONTROL_PROGRESS : AST_CONTROL_RINGING));
2099 ast_mutex_unlock(&pvt->lock);
2105 * Call-back function for incoming calls
2107 * Returns 1 on success
2109 static call_options_t *setup_incoming_call(call_details_t *cd)
2111 struct oh323_pvt *pvt;
2112 struct oh323_user *user = NULL;
2113 struct oh323_alias *alias = NULL;
2116 ast_debug(1, "Setting up incoming call for %s\n", cd->call_token);
2118 /* allocate the call*/
2119 pvt = oh323_alloc(cd->call_reference);
2122 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
2123 cleanup_call_details(cd);
2127 /* Populate the call details in the private structure */
2128 memcpy(&pvt->cd, cd, sizeof(pvt->cd));
2129 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
2130 pvt->jointcapability = pvt->options.capability;
2133 ast_verb(3, "Setting up Call\n");
2134 ast_verb(3, " \tCall token: [%s]\n", pvt->cd.call_token);
2135 ast_verb(3, " \tCalling party name: [%s]\n", pvt->cd.call_source_name);
2136 ast_verb(3, " \tCalling party number: [%s]\n", pvt->cd.call_source_e164);
2137 ast_verb(3, " \tCalled party name: [%s]\n", pvt->cd.call_dest_alias);
2138 ast_verb(3, " \tCalled party number: [%s]\n", pvt->cd.call_dest_e164);
2139 if (pvt->cd.redirect_reason >= 0)
2140 ast_verb(3, " \tRedirecting party number: [%s] (reason %d)\n", pvt->cd.redirect_number, pvt->cd.redirect_reason);
2141 ast_verb(3, " \tCalling party IP: [%s]\n", pvt->cd.sourceIp);
2144 /* Decide if we are allowing Gatekeeper routed calls*/
2145 if ((!strcasecmp(cd->sourceIp, gatekeeper)) && (gkroute == -1) && !gatekeeper_disable) {
2146 if (!ast_strlen_zero(cd->call_dest_e164)) {
2147 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2148 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2150 alias = find_alias(cd->call_dest_alias, 1);
2152 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd->call_dest_alias);
2156 ast_copy_string(pvt->exten, alias->name, sizeof(pvt->exten));
2157 ast_copy_string(pvt->context, alias->context, sizeof(pvt->context));
2160 /* Either this call is not from the Gatekeeper
2161 or we are not allowing gk routed calls */
2162 user = find_user(cd, 1);
2164 if (!acceptAnonymous) {
2165 ast_log(LOG_NOTICE, "Anonymous call from '%s@%s' rejected\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
2169 if (ast_strlen_zero(default_context)) {
2170 ast_log(LOG_ERROR, "Call from '%s@%s' rejected due to no default context\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
2174 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2175 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
2176 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2178 ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
2181 ast_debug(1, "Sending %s@%s to context [%s] extension %s\n", cd->call_source_aliases, cd->sourceIp, pvt->context, pvt->exten);
2184 if (strcasecmp(cd->sourceIp, ast_inet_ntoa(user->addr.sin_addr))) {
2185 if (ast_strlen_zero(user->context)) {
2186 if (ast_strlen_zero(default_context)) {
2187 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd->sourceIp);
2189 ASTOBJ_UNREF(user, oh323_destroy_user);
2192 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2194 ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
2196 pvt->exten[0] = 'i';
2197 pvt->exten[1] = '\0';
2198 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd->sourceIp);
2200 ASTOBJ_UNREF(user, oh323_destroy_user);
2201 return NULL; /* XXX: Hmmm... Why to setup context if we drop connection immediately??? */
2204 ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
2205 memcpy(&pvt->options, &user->options, sizeof(pvt->options));
2206 pvt->jointcapability = pvt->options.capability;
2207 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
2208 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2210 ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
2212 if (!ast_strlen_zero(user->accountcode)) {
2213 ast_copy_string(pvt->accountcode, user->accountcode, sizeof(pvt->accountcode));
2215 if (user->amaflags) {
2216 pvt->amaflags = user->amaflags;
2218 ASTOBJ_UNREF(user, oh323_destroy_user);
2221 return &pvt->options;
2225 * Call-back function to start PBX when OpenH323 ready to serve incoming call
2227 * Returns 1 on success
2229 static int answer_call(unsigned call_reference, const char *token)
2231 struct oh323_pvt *pvt;
2232 struct ast_channel *c = NULL;
2233 enum {ext_original, ext_s, ext_i, ext_notexists} try_exten;
2234 char tmp_exten[sizeof(pvt->exten)];
2237 ast_debug(1, "Preparing Asterisk to answer for %s\n", token);
2239 /* Find the call or allocate a private structure if call not found */
2240 pvt = find_call_locked(call_reference, token);
2242 ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
2245 /* Check if requested extension@context pair exists in the dialplan */
2246 ast_copy_string(tmp_exten, pvt->exten, sizeof(tmp_exten));
2248 /* Try to find best extension in specified context */
2249 if ((tmp_exten[0] != '\0') && (tmp_exten[1] == '\0')) {
2250 if (tmp_exten[0] == 's')
2252 else if (tmp_exten[0] == 'i')
2255 try_exten = ext_original;
2257 try_exten = ext_original;
2259 if (ast_exists_extension(NULL, pvt->context, tmp_exten, 1, NULL))
2261 switch (try_exten) {
2264 tmp_exten[1] = '\0';
2272 try_exten = ext_notexists;
2277 } while (try_exten != ext_notexists);
2279 /* Drop the call if we don't have <exten>, s and i extensions */
2280 if (try_exten == ext_notexists) {
2281 ast_log(LOG_NOTICE, "Dropping call because extensions '%s', 's' and 'i' doesn't exists in context [%s]\n", pvt->exten, pvt->context);
2282 ast_mutex_unlock(&pvt->lock);
2283 h323_clear_call(token, AST_CAUSE_UNALLOCATED);
2285 } else if ((try_exten != ext_original) && (strcmp(pvt->exten, tmp_exten) != 0)) {
2287 ast_debug(1, "Going to extension %s@%s because %s@%s isn't exists\n", tmp_exten, pvt->context, pvt->exten, pvt->context);
2288 ast_copy_string(pvt->exten, tmp_exten, sizeof(pvt->exten));
2291 /* allocate a channel and tell asterisk about it */
2292 c = __oh323_new(pvt, AST_STATE_RINGING, pvt->cd.call_token);
2294 /* And release when done */
2295 ast_mutex_unlock(&pvt->lock);
2297 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
2304 * Call-back function to establish an outgoing H.323 call
2306 * Returns 1 on success
2308 static int setup_outgoing_call(call_details_t *cd)
2310 /* Use argument here or free it immediately */
2311 cleanup_call_details(cd);
2317 * Call-back function to signal asterisk that the channel is ringing
2320 static void chan_ringing(unsigned call_reference, const char *token)
2322 struct oh323_pvt *pvt;
2325 ast_debug(1, "Ringing on %s\n", token);
2327 pvt = find_call_locked(call_reference, token);
2329 ast_log(LOG_ERROR, "Something is wrong: ringing\n");
2333 ast_mutex_unlock(&pvt->lock);
2334 ast_log(LOG_ERROR, "Channel has no owner\n");
2337 update_state(pvt, AST_STATE_RINGING, AST_CONTROL_RINGING);
2338 ast_mutex_unlock(&pvt->lock);
2343 * Call-back function to cleanup communication
2346 static void cleanup_connection(unsigned call_reference, const char *call_token)
2348 struct oh323_pvt *pvt;
2351 ast_debug(1, "Cleaning connection to %s\n", call_token);
2354 pvt = find_call_locked(call_reference, call_token);
2357 ast_debug(1, "No connection for %s\n", call_token);
2360 if (!pvt->owner || !ast_channel_trylock(pvt->owner))
2363 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s\n", call_token);
2364 #ifdef DEBUG_THREADS
2365 /* XXX to be completed
2366 * If we want to print more info on who is holding the lock,
2367 * implement the relevant code in lock.h and use the routines
2372 ast_mutex_unlock(&pvt->lock);
2376 /* Immediately stop RTP */
2377 ast_rtp_destroy(pvt->rtp);
2380 /* Free dsp used for in-band DTMF detection */
2382 ast_dsp_free(pvt->vad);
2385 cleanup_call_details(&pvt->cd);
2386 pvt->alreadygone = 1;
2389 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2390 ast_queue_hangup(pvt->owner);
2391 ast_channel_unlock(pvt->owner);
2393 ast_mutex_unlock(&pvt->lock);
2395 ast_debug(1, "Connection to %s cleaned\n", call_token);
2399 static void hangup_connection(unsigned int call_reference, const char *token, int cause)
2401 struct oh323_pvt *pvt;
2404 ast_debug(1, "Hanging up connection to %s with cause %d\n", token, cause);
2406 pvt = find_call_locked(call_reference, token);
2409 ast_debug(1, "Connection to %s already cleared\n", token);
2412 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
2413 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2414 pvt->owner->hangupcause = pvt->hangupcause = cause;
2415 ast_queue_hangup(pvt->owner);
2416 ast_channel_unlock(pvt->owner);
2419 pvt->needhangup = 1;
2420 pvt->hangupcause = cause;
2422 ast_debug(1, "Hangup for %s is pending\n", token);
2424 ast_mutex_unlock(&pvt->lock);
2427 static void set_dtmf_payload(unsigned call_reference, const char *token, int payload, int is_cisco)
2429 struct oh323_pvt *pvt;
2432 ast_debug(1, "Setting %s DTMF payload to %d on %s\n", (is_cisco ? "Cisco" : "RFC2833"), payload, token);
2434 pvt = find_call_locked(call_reference, token);
2439 ast_rtp_set_rtpmap_type(pvt->rtp, payload, "audio", (is_cisco ? "cisco-telephone-event" : "telephone-event"), 0);
2441 pvt->dtmf_pt[is_cisco ? 1 : 0] = payload;
2442 ast_mutex_unlock(&pvt->lock);
2444 ast_debug(1, "DTMF payload on %s set to %d\n", token, payload);
2447 static void set_peer_capabilities(unsigned call_reference, const char *token, int capabilities, struct ast_codec_pref *prefs)
2449 struct oh323_pvt *pvt;
2452 ast_debug(1, "Got remote capabilities from connection %s\n", token);
2454 pvt = find_call_locked(call_reference, token);
2457 pvt->peercapability = capabilities;
2458 pvt->jointcapability = pvt->options.capability & capabilities;
2460 memcpy(&pvt->peer_prefs, prefs, sizeof(pvt->peer_prefs));
2463 for (i = 0; i < 32; ++i) {
2464 if (!prefs->order[i])
2466 ast_debug(1, "prefs[%d]=%s:%d\n", i, (prefs->order[i] ? ast_getformatname(1 << (prefs->order[i]-1)) : "<none>"), prefs->framing[i]);
2470 ast_rtp_codec_setpref(pvt->rtp, &pvt->peer_prefs);
2472 ast_mutex_unlock(&pvt->lock);
2475 static void set_local_capabilities(unsigned call_reference, const char *token)
2477 struct oh323_pvt *pvt;
2478 int capability, dtmfmode, pref_codec;
2479 struct ast_codec_pref prefs;
2482 ast_debug(1, "Setting capabilities for connection %s\n", token);
2484 pvt = find_call_locked(call_reference, token);
2487 capability = (pvt->jointcapability) ? pvt->jointcapability : pvt->options.capability;
2488 dtmfmode = pvt->options.dtmfmode;
2489 prefs = pvt->options.prefs;
2490 pref_codec = pvt->pref_codec;
2491 ast_mutex_unlock(&pvt->lock);
2492 h323_set_capabilities(token, capability, dtmfmode, &prefs, pref_codec);
2495 ast_debug(1, "Capabilities for connection %s is set\n", token);
2498 static void remote_hold(unsigned call_reference, const char *token, int is_hold)
2500 struct oh323_pvt *pvt;
2503 ast_debug(1, "Setting %shold status for connection %s\n", (is_hold ? "" : "un"), token);
2505 pvt = find_call_locked(call_reference, token);
2508 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
2510 ast_queue_control(pvt->owner, AST_CONTROL_HOLD);
2512 ast_queue_control(pvt->owner, AST_CONTROL_UNHOLD);
2513 ast_channel_unlock(pvt->owner);
2517 pvt->newcontrol = AST_CONTROL_HOLD;
2519 pvt->newcontrol = AST_CONTROL_UNHOLD;
2521 ast_mutex_unlock(&pvt->lock);
2524 static void *do_monitor(void *data)
2528 struct oh323_pvt *oh323 = NULL;
2531 /* Check for a reload request */
2532 ast_mutex_lock(&h323_reload_lock);
2533 reloading = h323_reloading;
2535 ast_mutex_unlock(&h323_reload_lock);
2537 ast_verb(1, "Reloading H.323\n");
2540 /* Check for interfaces needing to be killed */
2541 if (!ast_mutex_trylock(&iflock)) {
2544 for (oh323 = iflist; oh323; oh323 = oh323->next) {
2545 if (!ast_mutex_trylock(&oh323->lock)) {
2546 if (oh323->needdestroy) {
2547 __oh323_destroy(oh323);
2550 ast_mutex_unlock(&oh323->lock);
2553 } while (/*oh323*/ 0);
2558 if (!ast_mutex_trylock(&oh323->lock)) {
2559 if (oh323->needdestroy) {
2560 __oh323_destroy(oh323);
2563 ast_mutex_unlock(&oh323->lock);
2564 oh323 = oh323->next;
2568 ast_mutex_unlock(&iflock);
2570 oh323 = (struct oh323_pvt *)1; /* Force fast loop */
2571 pthread_testcancel();
2572 /* Wait for sched or io */
2573 res = ast_sched_wait(sched);
2574 if ((res < 0) || (res > 1000)) {
2577 /* Do not wait if some channel(s) is destroyed, probably, more available too */
2580 res = ast_io_wait(io, res);
2581 pthread_testcancel();
2582 ast_mutex_lock(&monlock);
2584 ast_sched_runq(sched);
2586 ast_mutex_unlock(&monlock);
2592 static int restart_monitor(void)
2594 /* If we're supposed to be stopped -- stay stopped */
2595 if (ast_mutex_lock(&monlock)) {
2596 ast_log(LOG_WARNING, "Unable to lock monitor\n");
2599 if (monitor_thread == AST_PTHREADT_STOP) {
2600 ast_mutex_unlock(&monlock);
2603 if (monitor_thread == pthread_self()) {
2604 ast_mutex_unlock(&monlock);
2605 ast_log(LOG_WARNING, "Cannot kill myself\n");
2608 if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
2609 /* Wake up the thread */
2610 pthread_kill(monitor_thread, SIGURG);
2612 /* Start a new monitor */
2613 if (ast_pthread_create_detached_background(&monitor_thread, NULL, do_monitor, NULL) < 0) {
2614 monitor_thread = AST_PTHREADT_NULL;
2615 ast_mutex_unlock(&monlock);
2616 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
2620 ast_mutex_unlock(&monlock);
2624 static char *handle_cli_h323_set_trace(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2628 e->command = "h323 set trace [off]";
2630 "Usage: h323 set trace (off|<trace level>)\n"
2631 " Enable/Disable H.323 stack tracing for debugging purposes\n";
2638 return CLI_SHOWUSAGE;
2639 if (!strcasecmp(a->argv[3], "off")) {
2641 ast_cli(a->fd, "H.323 Trace Disabled\n");
2643 int tracelevel = atoi(a->argv[3]);
2644 h323_debug(1, tracelevel);
2645 ast_cli(a->fd, "H.323 Trace Enabled (Trace Level: %d)\n", tracelevel);
2650 static char *handle_cli_h323_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2654 e->command = "h323 set debug [off]";
2656 "Usage: h323 set debug [off]\n"
2657 " Enable/Disable H.323 debugging output\n";
2663 if (a->argc < 3 || a->argc > 4)
2664 return CLI_SHOWUSAGE;
2665 if (a->argc == 4 && strcasecmp(a->argv[3], "off"))
2666 return CLI_SHOWUSAGE;
2668 h323debug = (a->argc == 3) ? 1 : 0;
2669 ast_cli(a->fd, "H.323 Debugging %s\n", h323debug ? "Enabled" : "Disabled");
2673 static char *handle_cli_h323_cycle_gk(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2677 e->command = "h323 cycle gk";
2679 "Usage: h323 cycle gk\n"
2680 " Manually re-register with the Gatekeper (Currently Disabled)\n";
2687 return CLI_SHOWUSAGE;
2691 /* Possibly register with a GK */
2692 if (!gatekeeper_disable) {
2693 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
2694 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
2700 static char *handle_cli_h323_hangup(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2704 e->command = "h323 hangup";
2706 "Usage: h323 hangup <token>\n"
2707 " Manually try to hang up the call identified by <token>\n";
2714 return CLI_SHOWUSAGE;
2715 if (h323_soft_hangup(a->argv[2])) {
2716 ast_verb(3, "Hangup succeeded on %s\n", a->argv[2]);
2718 ast_verb(3, "Hangup failed for %s\n", a->argv[2]);
2723 static char *handle_cli_h323_show_tokens(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2727 e->command = "h323 show tokens";
2729 "Usage: h323 show tokens\n"
2730 " Print out all active call tokens\n";
2737 return CLI_SHOWUSAGE;
2744 static struct ast_cli_entry cli_h323[] = {
2745 AST_CLI_DEFINE(handle_cli_h323_set_trace, "Enable/Disable H.323 Stack Tracing"),
2746 AST_CLI_DEFINE(handle_cli_h323_set_debug, "Enable/Disable H.323 Debugging"),
2747 AST_CLI_DEFINE(handle_cli_h323_cycle_gk, "Manually re-register with the Gatekeper"),
2748 AST_CLI_DEFINE(handle_cli_h323_hangup, "Manually try to hang up a call"),
2749 AST_CLI_DEFINE(handle_cli_h323_show_tokens, "Show all active call tokens"),
2752 static void delete_users(void)
2756 /* Delete all users */
2757 ASTOBJ_CONTAINER_WRLOCK(&userl);
2758 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
2759 ASTOBJ_RDLOCK(iterator);
2760 ASTOBJ_MARK(iterator);
2762 ASTOBJ_UNLOCK(iterator);
2765 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, oh323_destroy_user);
2767 ASTOBJ_CONTAINER_UNLOCK(&userl);
2769 ASTOBJ_CONTAINER_WRLOCK(&peerl);
2770 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
2771 ASTOBJ_RDLOCK(iterator);
2772 ASTOBJ_MARK(iterator);
2773 ASTOBJ_UNLOCK(iterator);
2775 ASTOBJ_CONTAINER_UNLOCK(&peerl);
2778 static void delete_aliases(void)
2782 /* Delete all aliases */
2783 ASTOBJ_CONTAINER_WRLOCK(&aliasl);
2784 ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
2785 ASTOBJ_RDLOCK(iterator);
2786 ASTOBJ_MARK(iterator);
2788 ASTOBJ_UNLOCK(iterator);
2791 ASTOBJ_CONTAINER_PRUNE_MARKED(&aliasl, oh323_destroy_alias);
2793 ASTOBJ_CONTAINER_UNLOCK(&aliasl);
2796 static void prune_peers(void)
2798 /* Prune peers who still are supposed to be deleted */
2799 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, oh323_destroy_peer);
2802 static int reload_config(int is_reload)
2804 struct ast_config *cfg, *ucfg;
2805 struct ast_variable *v;
2806 struct oh323_peer *peer = NULL;
2807 struct oh323_user *user = NULL;
2808 struct oh323_alias *alias = NULL;
2809 struct ast_hostent ahp; struct hostent *hp;
2812 int is_user, is_peer, is_alias;
2813 char _gatekeeper[100];
2814 int gk_discover, gk_disable, gk_changed;
2815 struct ast_flags config_flags = { is_reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
2817 cfg = ast_config_load(config, config_flags);
2819 /* We *must* have a config file otherwise stop immediately */
2821 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
2823 } else if (cfg == CONFIG_STATUS_FILEUNCHANGED) {
2824 ucfg = ast_config_load("users.conf", config_flags);
2825 if (ucfg == CONFIG_STATUS_FILEUNCHANGED)
2827 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
2828 cfg = ast_config_load(config, config_flags);
2830 ast_clear_flag(&config_flags, CONFIG_FLAG_FILEUNCHANGED);
2831 ucfg = ast_config_load("users.conf", config_flags);
2840 /* fire up the H.323 Endpoint */
2841 if (!h323_end_point_exist()) {
2842 h323_end_point_create();
2844 ast_copy_string(_gatekeeper, gatekeeper, sizeof(_gatekeeper));
2845 gk_discover = gatekeeper_discover;
2846 gk_disable = gatekeeper_disable;
2847 memset(&bindaddr, 0, sizeof(bindaddr));
2848 memset(&global_options, 0, sizeof(global_options));
2849 global_options.fastStart = 1;
2850 global_options.h245Tunneling = 1;
2851 global_options.dtmfcodec[0] = H323_DTMF_RFC2833_PT;
2852 global_options.dtmfcodec[1] = H323_DTMF_CISCO_PT;
2853 global_options.dtmfmode = 0;
2854 global_options.holdHandling = 0;
2855 global_options.capability = GLOBAL_CAPABILITY;
2856 global_options.bridge = 1; /* Do native bridging by default */
2857 strcpy(default_context, "default");
2858 h323_signalling_port = 1720;
2859 gatekeeper_disable = 1;
2860 gatekeeper_discover = 0;
2863 acceptAnonymous = 1;
2867 /* Copy the default jb config over global_jbconf */
2868 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2871 struct ast_variable *gen;
2873 const char *has_h323;
2875 genhas_h323 = ast_true(ast_variable_retrieve(ucfg, "general", "hash323"));
2876 gen = ast_variable_browse(ucfg, "general");
2877 for (cat = ast_category_browse(ucfg, NULL); cat; cat = ast_category_browse(ucfg, cat)) {
2878 if (strcasecmp(cat, "general")) {
2879 has_h323 = ast_variable_retrieve(ucfg, cat, "hash323");
2880 if (ast_true(has_h323) || (!has_h323 && genhas_h323)) {
2881 user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
2883 ASTOBJ_CONTAINER_LINK(&userl, user);
2884 ASTOBJ_UNREF(user, oh323_destroy_user);
2886 peer = build_peer(cat, gen, ast_variable_browse(ucfg, cat), 0);
2888 ASTOBJ_CONTAINER_LINK(&peerl, peer);
2889 ASTOBJ_UNREF(peer, oh323_destroy_peer);
2894 ast_config_destroy(ucfg);
2897 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
2898 /* handle jb conf */
2899 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
2901 /* Create the interface list */
2902 if (!strcasecmp(v->name, "port")) {
2903 h323_signalling_port = (int)strtol(v->value, NULL, 10);
2904 } else if (!strcasecmp(v->name, "bindaddr")) {
2905 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
2906 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
2908 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
2910 } else if (!strcasecmp(v->name, "tos")) { /* Needs to be removed in next release */
2911 ast_log(LOG_WARNING, "The \"tos\" setting is deprecated in this version of Asterisk. Please change to \"tos_audio\".\n");
2912 if (ast_str2tos(v->value, &tos)) {
2913 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
2915 } else if (!strcasecmp(v->name, "tos_audio")) {
2916 if (ast_str2tos(v->value, &tos)) {
2917 ast_log(LOG_WARNING, "Invalid tos_audio value at line %d, refer to QoS documentation\n", v->lineno);
2919 } else if (!strcasecmp(v->name, "cos")) {
2920 ast_log(LOG_WARNING, "The \"cos\" setting is deprecated in this version of Asterisk. Please change to \"cos_audio\".\n");
2921 if (ast_str2cos(v->value, &cos)) {
2922 ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
2924 } else if (!strcasecmp(v->name, "cos_audio")) {
2925 if (ast_str2cos(v->value, &cos)) {
2926 ast_log(LOG_WARNING, "Invalid cos_audio value at line %d, refer to QoS documentation\n", v->lineno);
2928 } else if (!strcasecmp(v->name, "gatekeeper")) {
2929 if (!strcasecmp(v->value, "DISABLE")) {
2930 gatekeeper_disable = 1;
2931 } else if (!strcasecmp(v->value, "DISCOVER")) {
2932 gatekeeper_disable = 0;
2933 gatekeeper_discover = 1;
2935 gatekeeper_disable = 0;
2936 ast_copy_string(gatekeeper, v->value, sizeof(gatekeeper));
2938 } else if (!strcasecmp(v->name, "secret")) {
2939 ast_copy_string(secret, v->value, sizeof(secret));
2940 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
2941 gkroute = ast_true(v->value);
2942 } else if (!strcasecmp(v->name, "context")) {
2943 ast_copy_string(default_context, v->value, sizeof(default_context));
2944 ast_verb(2, "Setting default context to %s\n", default_context);
2945 } else if (!strcasecmp(v->name, "UserByAlias")) {
2946 userbyalias = ast_true(v->value);
2947 } else if (!strcasecmp(v->name, "AcceptAnonymous")) {
2948 acceptAnonymous = ast_true(v->value);
2949 } else if (!update_common_options(v, &global_options)) {
2953 if (!global_options.dtmfmode)
2954 global_options.dtmfmode = H323_DTMF_RFC2833;
2955 if (global_options.holdHandling == ~0)
2956 global_options.holdHandling = 0;
2957 else if (!global_options.holdHandling)
2958 global_options.holdHandling = H323_HOLD_H450;
2960 for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
2961 if (strcasecmp(cat, "general")) {
2962 utype = ast_variable_retrieve(cfg, cat, "type");
2964 is_user = is_peer = is_alias = 0;
2965 if (!strcasecmp(utype, "user"))
2967 else if (!strcasecmp(utype, "peer"))
2969 else if (!strcasecmp(utype, "friend"))
2970 is_user = is_peer = 1;
2971 else if (!strcasecmp(utype, "h323") || !strcasecmp(utype, "alias"))
2974 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
2978 user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
2980 ASTOBJ_CONTAINER_LINK(&userl, user);
2981 ASTOBJ_UNREF(user, oh323_destroy_user);
2985 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
2987 ASTOBJ_CONTAINER_LINK(&peerl, peer);
2988 ASTOBJ_UNREF(peer, oh323_destroy_peer);
2992 alias = build_alias(cat, ast_variable_browse(cfg, cat), NULL, 0);
2994 ASTOBJ_CONTAINER_LINK(&aliasl, alias);
2995 ASTOBJ_UNREF(alias, oh323_destroy_alias);
2999 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
3003 ast_config_destroy(cfg);
3005 /* Register our H.323 aliases if any*/
3006 ASTOBJ_CONTAINER_WRLOCK(&aliasl);
3007 ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
3008 ASTOBJ_RDLOCK(iterator);
3009 if (h323_set_alias(iterator)) {
3010 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
3011 ASTOBJ_UNLOCK(iterator);
3014 ASTOBJ_UNLOCK(iterator);
3016 ASTOBJ_CONTAINER_UNLOCK(&aliasl);
3018 /* Don't touch GK if nothing changed because URQ will drop all existing calls */
3020 if (gatekeeper_disable != gk_disable)
3021 gk_changed = is_reload;
3022 else if(!gatekeeper_disable && (gatekeeper_discover != gk_discover))
3023 gk_changed = is_reload;
3024 else if(!gatekeeper_disable && (strncmp(_gatekeeper, gatekeeper, sizeof(_gatekeeper)) != 0))
3025 gk_changed = is_reload;
3029 if (!gatekeeper_disable) {
3030 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
3031 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
3032 gatekeeper_disable = 1;
3039 static int h323_reload(void)
3041 ast_mutex_lock(&h323_reload_lock);
3042 if (h323_reloading) {
3043 ast_verbose("Previous H.323 reload not yet done\n");
3047 ast_mutex_unlock(&h323_reload_lock);