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
33 * \ingroup channel_drivers
37 <depend>openh323</depend>
38 <defaultenabled>no</defaultenabled>
47 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
53 #include <sys/socket.h>
54 #include <sys/signal.h>
55 #include <sys/param.h>
58 #define IPTOS_MINCOST 0x02
61 #include <arpa/inet.h>
63 #include <netinet/in.h>
64 #include <netinet/in_systm.h>
65 #include <netinet/ip.h>
78 #include "asterisk/lock.h"
79 #include "asterisk/logger.h"
80 #include "asterisk/channel.h"
81 #include "asterisk/config.h"
82 #include "asterisk/module.h"
83 #include "asterisk/musiconhold.h"
84 #include "asterisk/pbx.h"
85 #include "asterisk/options.h"
86 #include "asterisk/utils.h"
87 #include "asterisk/lock.h"
88 #include "asterisk/sched.h"
89 #include "asterisk/io.h"
90 #include "asterisk/rtp.h"
91 #include "asterisk/acl.h"
92 #include "asterisk/callerid.h"
93 #include "asterisk/cli.h"
94 #include "asterisk/dsp.h"
95 #include "asterisk/causes.h"
96 #include "asterisk/stringfields.h"
97 #include "asterisk/abstract_jb.h"
98 #include "asterisk/astobj.h"
104 #include "h323/chan_h323.h"
106 receive_digit_cb on_receive_digit;
107 on_rtp_cb on_external_rtp_create;
108 start_rtp_cb on_start_rtp_channel;
109 setup_incoming_cb on_incoming_call;
110 setup_outbound_cb on_outgoing_call;
111 chan_ringing_cb on_chan_ringing;
112 con_established_cb on_connection_established;
113 clear_con_cb on_connection_cleared;
114 answer_call_cb on_answer_call;
115 progress_cb on_progress;
116 rfc2833_cb on_set_rfc2833_payload;
118 setcapabilities_cb on_setcapabilities;
119 setpeercapabilities_cb on_setpeercapabilities;
122 /* global debug flag */
125 /*! Global jitterbuffer configuration - by default, jb is disabled */
126 static struct ast_jb_conf default_jbconf =
130 .resync_threshold = -1,
133 static struct ast_jb_conf global_jbconf;
135 /** Variables required by Asterisk */
136 static const char tdesc[] = "The NuFone Network's Open H.323 Channel Driver";
137 static const char config[] = "h323.conf";
138 static char default_context[AST_MAX_CONTEXT] = "default";
139 static struct sockaddr_in bindaddr;
141 #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)
143 /** H.323 configuration values */
144 static int h323_signalling_port = 1720;
145 static char gatekeeper[100];
146 static int gatekeeper_disable = 1;
147 static int gatekeeper_discover = 0;
148 static int gkroute = 0;
149 /* Find user by alias (h.323 id) is default, alternative is the incomming call's source IP address*/
150 static int userbyalias = 1;
151 static int acceptAnonymous = 1;
153 static char secret[50];
154 static unsigned int unique = 0;
156 static call_options_t global_options;
158 /** Private structure of a OpenH323 channel */
160 ast_mutex_t lock; /* Channel private lock */
161 call_options_t options; /* Options to be used during call setup */
162 int alreadygone; /* Whether or not we've already been destroyed by our peer */
163 int needdestroy; /* if we need to be destroyed */
164 call_details_t cd; /* Call details */
165 struct ast_channel *owner; /* Who owns us */
166 struct sockaddr_in sa; /* Our peer */
167 struct sockaddr_in redirip; /* Where our RTP should be going if not to us */
168 int nonCodecCapability; /* non-audio capability */
169 int outgoing; /* Outgoing or incoming call? */
170 char exten[AST_MAX_EXTENSION]; /* Requested extension */
171 char context[AST_MAX_CONTEXT]; /* Context where to start */
172 char accountcode[256]; /* Account code */
173 char rdnis[80]; /* Referring DNIS, if available */
174 int amaflags; /* AMA Flags */
175 struct ast_rtp *rtp; /* RTP Session */
176 struct ast_dsp *vad; /* Used for in-band DTMF detection */
177 int nativeformats; /* Codec formats supported by a channel */
178 int needhangup; /* Send hangup when Asterisk is ready */
179 int hangupcause; /* Hangup cause from OpenH323 layer */
180 int newstate; /* Pending state change */
181 int newcontrol; /* Pending control to send */
182 int newdigit; /* Pending DTMF digit to send */
183 int newduration; /* Pending DTMF digit duration to send */
184 int pref_codec; /* Preferred codec */
185 int peercapability; /* Capabilities learned from peer */
186 int jointcapability; /* Common capabilities for local and remote side */
187 struct ast_codec_pref peer_prefs; /* Preferenced list of codecs which remote side supports */
188 int dtmf_pt[2]; /* Payload code used for RFC2833/CISCO messages */
189 int curDTMF; /* DTMF tone being generated to Asterisk side */
190 int DTMFsched; /* Scheduler descriptor for DTMF */
191 int update_rtp_info; /* Configuration of fd's array is pending */
192 int recvonly; /* Peer isn't wish to receive our voice stream */
193 int txDtmfDigit; /* DTMF digit being to send to H.323 side */
194 int noInbandDtmf; /* Inband DTMF processing by DSP isn't available */
195 int connection_established; /* Call got CONNECT message */
196 int got_progress; /* Call got PROGRESS message, pass inband audio */
197 struct oh323_pvt *next; /* Next channel in list */
200 static struct ast_user_list {
201 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_user);
204 static struct ast_peer_list {
205 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_peer);
208 static struct ast_alias_list {
209 ASTOBJ_CONTAINER_COMPONENTS(struct oh323_alias);
212 /** Asterisk RTP stuff */
213 static struct sched_context *sched;
214 static struct io_context *io;
216 /** Protect the interface list (oh323_pvt) */
217 AST_MUTEX_DEFINE_STATIC(iflock);
219 /* Protect the monitoring thread, so only one process can kill or start it, and not
220 when it's doing something critical. */
221 AST_MUTEX_DEFINE_STATIC(monlock);
223 /* Protect the H.323 capabilities list, to avoid more than one channel to set the capabilities simultaneaously in the h323 stack. */
224 AST_MUTEX_DEFINE_STATIC(caplock);
226 /* Protect the reload process */
227 AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
228 static int h323_reloading = 0;
230 /* This is the thread for the monitor which checks for input on the channels
231 which are not currently in use. */
232 static pthread_t monitor_thread = AST_PTHREADT_NULL;
233 static int restart_monitor(void);
234 static int h323_do_reload(void);
236 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause);
237 static int oh323_digit_begin(struct ast_channel *c, char digit);
238 static int oh323_digit_end(struct ast_channel *c, char digit);
239 static int oh323_call(struct ast_channel *c, char *dest, int timeout);
240 static int oh323_hangup(struct ast_channel *c);
241 static int oh323_answer(struct ast_channel *c);
242 static struct ast_frame *oh323_read(struct ast_channel *c);
243 static int oh323_write(struct ast_channel *c, struct ast_frame *frame);
244 static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen);
245 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
247 static const struct ast_channel_tech oh323_tech = {
249 .description = tdesc,
250 .capabilities = ((AST_FORMAT_MAX_AUDIO << 1) - 1),
251 .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
252 .requester = oh323_request,
253 .send_digit_begin = oh323_digit_begin,
254 .send_digit_end = oh323_digit_end,
256 .hangup = oh323_hangup,
257 .answer = oh323_answer,
259 .write = oh323_write,
260 .indicate = oh323_indicate,
261 .fixup = oh323_fixup,
262 /* disable, for now */
264 .bridge = ast_rtp_bridge,
268 static const char* redirectingreason2str(int redirectingreason)
270 switch (redirectingreason) {
278 return "UNCONDITIONAL";
284 static void oh323_destroy_alias(struct oh323_alias *alias)
287 ast_log(LOG_DEBUG, "Destroying alias '%s'\n", alias->name);
291 static void oh323_destroy_user(struct oh323_user *user)
294 ast_log(LOG_DEBUG, "Destroying user '%s'\n", user->name);
295 ast_free_ha(user->ha);
299 static void oh323_destroy_peer(struct oh323_peer *peer)
302 ast_log(LOG_DEBUG, "Destroying peer '%s'\n", peer->name);
303 ast_free_ha(peer->ha);
307 static int oh323_simulate_dtmf_end(void *data)
309 struct oh323_pvt *pvt = data;
312 ast_mutex_lock(&pvt->lock);
313 /* Don't hold pvt lock while trying to lock the channel */
314 while(pvt->owner && ast_channel_trylock(pvt->owner)) {
315 ast_mutex_unlock(&pvt->lock);
317 ast_mutex_lock(&pvt->lock);
321 struct ast_frame f = {
322 .frametype = AST_FRAME_DTMF_END,
323 .subclass = pvt->curDTMF,
325 .src = "SIMULATE_DTMF_END",
327 ast_queue_frame(pvt->owner, &f);
328 ast_channel_unlock(pvt->owner);
332 ast_mutex_unlock(&pvt->lock);
338 /* Channel and private structures should be already locked */
339 static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
341 if (c->nativeformats != pvt->nativeformats) {
343 ast_log(LOG_DEBUG, "Preparing %s for new native format\n", c->name);
344 c->nativeformats = pvt->nativeformats;
345 ast_set_read_format(c, c->readformat);
346 ast_set_write_format(c, c->writeformat);
348 if (pvt->needhangup) {
350 ast_log(LOG_DEBUG, "Process pending hangup for %s\n", c->name);
351 c->_softhangup |= AST_SOFTHANGUP_DEV;
352 c->hangupcause = pvt->hangupcause;
355 pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->DTMFsched = -1;
357 if (pvt->newstate >= 0) {
358 ast_setstate(c, pvt->newstate);
361 if (pvt->newcontrol >= 0) {
362 ast_queue_control(c, pvt->newcontrol);
363 pvt->newcontrol = -1;
365 if (pvt->newdigit >= 0) {
366 struct ast_frame f = {
367 .frametype = AST_FRAME_DTMF_END,
368 .subclass = pvt->newdigit,
369 .samples = pvt->newduration * 8,
370 .src = "UPDATE_INFO",
372 if (pvt->newdigit == ' ') { /* signalUpdate message */
373 f.subclass = pvt->curDTMF;
374 if (pvt->DTMFsched >= 0) {
375 ast_sched_del(sched, pvt->DTMFsched);
378 } else { /* Regular input or signal message */
379 if (pvt->newduration) { /* This is a signal, signalUpdate follows */
380 f.frametype = AST_FRAME_DTMF_BEGIN;
381 if (pvt->DTMFsched >= 0)
382 ast_sched_del(sched, pvt->DTMFsched);
383 pvt->DTMFsched = ast_sched_add(sched, pvt->newduration, oh323_simulate_dtmf_end, pvt);
385 ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", pvt->newduration, pvt->DTMFsched);
387 pvt->curDTMF = pvt->newdigit;
389 ast_queue_frame(c, &f);
392 if (pvt->update_rtp_info > 0) {
394 ast_jb_configure(c, &global_jbconf);
395 c->fds[0] = ast_rtp_fd(pvt->rtp);
396 c->fds[1] = ast_rtcp_fd(pvt->rtp);
397 ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
399 pvt->update_rtp_info = -1;
403 /* Only channel structure should be locked */
404 static void oh323_update_info(struct ast_channel *c)
406 struct oh323_pvt *pvt = c->tech_pvt;
409 ast_mutex_lock(&pvt->lock);
410 __oh323_update_info(c, pvt);
411 ast_mutex_unlock(&pvt->lock);
415 static void cleanup_call_details(call_details_t *cd)
417 if (cd->call_token) {
418 free(cd->call_token);
419 cd->call_token = NULL;
421 if (cd->call_source_aliases) {
422 free(cd->call_source_aliases);
423 cd->call_source_aliases = NULL;
425 if (cd->call_dest_alias) {
426 free(cd->call_dest_alias);
427 cd->call_dest_alias = NULL;
429 if (cd->call_source_name) {
430 free(cd->call_source_name);
431 cd->call_source_name = NULL;
433 if (cd->call_source_e164) {
434 free(cd->call_source_e164);
435 cd->call_source_e164 = NULL;
437 if (cd->call_dest_e164) {
438 free(cd->call_dest_e164);
439 cd->call_dest_e164 = NULL;
445 if (cd->redirect_number) {
446 free(cd->redirect_number);
447 cd->redirect_number = NULL;
451 static void __oh323_destroy(struct oh323_pvt *pvt)
453 struct oh323_pvt *cur, *prev = NULL;
455 if (pvt->DTMFsched >= 0) {
456 ast_sched_del(sched, pvt->DTMFsched);
461 ast_rtp_destroy(pvt->rtp);
464 /* Free dsp used for in-band DTMF detection */
466 ast_dsp_free(pvt->vad);
468 cleanup_call_details(&pvt->cd);
470 /* Unlink us from the owner if we have one */
472 ast_channel_lock(pvt->owner);
474 ast_log(LOG_DEBUG, "Detaching from %s\n", pvt->owner->name);
475 pvt->owner->tech_pvt = NULL;
476 ast_channel_unlock(pvt->owner);
482 prev->next = cur->next;
491 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
493 ast_mutex_unlock(&pvt->lock);
494 ast_mutex_destroy(&pvt->lock);
499 static void oh323_destroy(struct oh323_pvt *pvt)
502 ast_log(LOG_DEBUG, "Destroying channel %s\n", (pvt->owner ? pvt->owner->name : "<unknown>"));
504 ast_mutex_lock(&iflock);
505 ast_mutex_lock(&pvt->lock);
506 __oh323_destroy(pvt);
507 ast_mutex_unlock(&iflock);
510 static int oh323_digit_begin(struct ast_channel *c, char digit)
512 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
516 ast_log(LOG_ERROR, "No private structure?! This is bad\n");
519 ast_mutex_lock(&pvt->lock);
521 (((pvt->options.dtmfmode & H323_DTMF_RFC2833) && pvt->dtmf_pt[0])
522 /*|| ((pvt->options.dtmfmode & H323_DTMF_CISCO) && pvt->dtmf_pt[1]))*/)) {
523 /* out-of-band DTMF */
525 ast_log(LOG_DTMF, "Begin sending out-of-band digit %c on %s\n", digit, c->name);
527 ast_rtp_senddigit_begin(pvt->rtp, digit);
528 ast_mutex_unlock(&pvt->lock);
529 } else if (pvt->txDtmfDigit != digit) {
532 ast_log(LOG_DTMF, "Begin sending inband digit %c on %s\n", digit, c->name);
534 pvt->txDtmfDigit = digit;
535 token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
536 ast_mutex_unlock(&pvt->lock);
537 h323_send_tone(token, digit);
542 ast_mutex_unlock(&pvt->lock);
543 oh323_update_info(c);
548 * Send (play) the specified digit to the channel.
551 static int oh323_digit_end(struct ast_channel *c, char digit)
553 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
557 ast_log(LOG_ERROR, "No private structure?! This is bad\n");
560 ast_mutex_lock(&pvt->lock);
561 if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833) && ((pvt->dtmf_pt[0] > 0) || (pvt->dtmf_pt[0] > 0))) {
562 /* out-of-band DTMF */
564 ast_log(LOG_DTMF, "End sending out-of-band digit %c on %s\n", digit, c->name);
566 ast_rtp_senddigit_end(pvt->rtp, digit);
567 ast_mutex_unlock(&pvt->lock);
571 ast_log(LOG_DTMF, "End sending inband digit %c on %s\n", digit, c->name);
573 pvt->txDtmfDigit = ' ';
574 token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
575 ast_mutex_unlock(&pvt->lock);
576 h323_send_tone(token, ' ');
581 oh323_update_info(c);
586 * Make a call over the specified channel to the specified
588 * Returns -1 on error, 0 on success.
590 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
593 struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
595 char called_addr[1024];
598 ast_log(LOG_DEBUG, "Calling to %s on %s\n", dest, c->name);
600 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
601 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
604 ast_mutex_lock(&pvt->lock);
605 if (!gatekeeper_disable) {
606 if (ast_strlen_zero(pvt->exten)) {
607 ast_copy_string(called_addr, dest, sizeof(called_addr));
609 snprintf(called_addr, sizeof(called_addr), "%s@%s", pvt->exten, dest);
612 res = htons(pvt->sa.sin_port);
613 addr = ast_inet_ntoa(pvt->sa.sin_addr);
614 if (ast_strlen_zero(pvt->exten)) {
615 snprintf(called_addr, sizeof(called_addr), "%s:%d", addr, res);
617 snprintf(called_addr, sizeof(called_addr), "%s@%s:%d", pvt->exten, addr, res);
620 /* make sure null terminated */
621 called_addr[sizeof(called_addr) - 1] = '\0';
624 ast_copy_string(pvt->options.cid_num, c->cid.cid_num, sizeof(pvt->options.cid_num));
627 ast_copy_string(pvt->options.cid_name, c->cid.cid_name, sizeof(pvt->options.cid_name));
629 if (c->cid.cid_rdnis) {
630 ast_copy_string(pvt->options.cid_rdnis, c->cid.cid_rdnis, sizeof(pvt->options.cid_rdnis));
633 pvt->options.presentation = c->cid.cid_pres;
634 pvt->options.type_of_number = c->cid.cid_ton;
636 if ((addr = pbx_builtin_getvar_helper(c, "PRIREDIRECTREASON"))) {
637 if (!strcasecmp(addr, "UNKNOWN"))
638 pvt->options.redirect_reason = 0;
639 else if (!strcasecmp(addr, "BUSY"))
640 pvt->options.redirect_reason = 1;
641 else if (!strcasecmp(addr, "NO_REPLY"))
642 pvt->options.redirect_reason = 2;
643 else if (!strcasecmp(addr, "UNCONDITIONAL"))
644 pvt->options.redirect_reason = 15;
646 pvt->options.redirect_reason = -1;
648 pvt->options.redirect_reason = -1;
650 pvt->options.transfer_capability = c->transfercapability;
652 /* indicate that this is an outgoing call */
655 if (option_verbose > 2)
656 ast_verbose(VERBOSE_PREFIX_3 "Requested transfer capability: 0x%.2x - %s\n", c->transfercapability, ast_transfercapability2str(c->transfercapability));
658 ast_log(LOG_DEBUG, "Placing outgoing call to %s, %d/%d\n", called_addr, pvt->options.dtmfcodec[0], pvt->options.dtmfcodec[1]);
659 ast_mutex_unlock(&pvt->lock);
660 res = h323_make_call(called_addr, &(pvt->cd), &pvt->options);
662 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
665 oh323_update_info(c);
669 static int oh323_answer(struct ast_channel *c)
672 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
676 ast_log(LOG_DEBUG, "Answering on %s\n", c->name);
678 ast_mutex_lock(&pvt->lock);
679 token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
680 ast_mutex_unlock(&pvt->lock);
681 res = h323_answering_call(token, 0);
685 oh323_update_info(c);
686 if (c->_state != AST_STATE_UP) {
687 ast_setstate(c, AST_STATE_UP);
692 static int oh323_hangup(struct ast_channel *c)
694 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
695 int q931cause = AST_CAUSE_NORMAL_CLEARING;
700 ast_log(LOG_DEBUG, "Hanging up and scheduling destroy of call %s\n", c->name);
703 ast_log(LOG_WARNING, "Asked to hangup channel not connected\n");
706 ast_mutex_lock(&pvt->lock);
707 /* Determine how to disconnect */
708 if (pvt->owner != c) {
709 ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
710 ast_mutex_unlock(&pvt->lock);
717 if (c->hangupcause) {
718 q931cause = c->hangupcause;
720 const char *cause = pbx_builtin_getvar_helper(c, "DIALSTATUS");
722 if (!strcmp(cause, "CONGESTION")) {
723 q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
724 } else if (!strcmp(cause, "BUSY")) {
725 q931cause = AST_CAUSE_USER_BUSY;
726 } else if (!strcmp(cause, "CHANISUNVAIL")) {
727 q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
728 } else if (!strcmp(cause, "NOANSWER")) {
729 q931cause = AST_CAUSE_NO_ANSWER;
730 } else if (!strcmp(cause, "CANCEL")) {
731 q931cause = AST_CAUSE_CALL_REJECTED;
736 /* Start the process if it's not already started */
737 if (!pvt->alreadygone && !pvt->hangupcause) {
738 call_token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
740 /* Release lock to eliminate deadlock */
741 ast_mutex_unlock(&pvt->lock);
742 if (h323_clear_call(call_token, q931cause)) {
743 ast_log(LOG_WARNING, "ClearCall failed.\n");
746 ast_mutex_lock(&pvt->lock);
749 pvt->needdestroy = 1;
750 ast_mutex_unlock(&pvt->lock);
752 /* Update usage counter */
753 ast_module_unref(ast_module_info->self);
758 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *pvt)
760 /* Retrieve audio/etc from channel. Assumes pvt->lock is already held. */
763 /* Only apply it for the first packet, we just need the correct ip/port */
764 if (pvt->options.nat) {
765 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
766 pvt->options.nat = 0;
769 f = ast_rtp_read(pvt->rtp);
770 /* Don't send RFC2833 if we're not supposed to */
771 if (f && (f->frametype == AST_FRAME_DTMF) && !(pvt->options.dtmfmode & (H323_DTMF_RFC2833 | H323_DTMF_CISCO))) {
772 return &ast_null_frame;
775 /* We already hold the channel lock */
776 if (f->frametype == AST_FRAME_VOICE) {
777 if (f->subclass != pvt->owner->nativeformats) {
778 /* Try to avoid deadlock */
779 if (ast_channel_trylock(pvt->owner)) {
780 ast_log(LOG_NOTICE, "Format changed but channel is locked. Ignoring frame...\n");
781 return &ast_null_frame;
784 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
785 pvt->owner->nativeformats = f->subclass;
786 pvt->nativeformats = f->subclass;
787 ast_set_read_format(pvt->owner, pvt->owner->readformat);
788 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
789 ast_channel_unlock(pvt->owner);
791 /* Do in-band DTMF detection */
792 if ((pvt->options.dtmfmode & H323_DTMF_INBAND) && pvt->vad) {
793 if ((pvt->nativeformats & (AST_FORMAT_SLINEAR | AST_FORMAT_ALAW | AST_FORMAT_ULAW))) {
794 if (!ast_channel_trylock(pvt->owner)) {
795 f = ast_dsp_process(pvt->owner, pvt->vad, f);
796 ast_channel_unlock(pvt->owner);
799 ast_log(LOG_NOTICE, "Unable to process inband DTMF while channel is locked\n");
800 } else if (pvt->nativeformats && !pvt->noInbandDtmf) {
801 ast_log(LOG_NOTICE, "Inband DTMF is not supported on codec %s. Use RFC2833\n", ast_getformatname(f->subclass));
802 pvt->noInbandDtmf = 1;
804 if (f &&(f->frametype == AST_FRAME_DTMF)) {
806 ast_log(LOG_DTMF, "Received in-band digit %c.\n", f->subclass);
814 static struct ast_frame *oh323_read(struct ast_channel *c)
816 struct ast_frame *fr;
817 struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
818 ast_mutex_lock(&pvt->lock);
819 __oh323_update_info(c, pvt);
822 fr = oh323_rtp_read(pvt);
826 fr = ast_rtcp_read(pvt->rtp);
828 fr = &ast_null_frame;
831 ast_log(LOG_ERROR, "Unable to handle fd %d on channel %s\n", c->fdno, c->name);
832 fr = &ast_null_frame;
835 ast_mutex_unlock(&pvt->lock);
839 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
841 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
843 if (frame->frametype != AST_FRAME_VOICE) {
844 if (frame->frametype == AST_FRAME_IMAGE) {
847 ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
851 if (!(frame->subclass & c->nativeformats)) {
852 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
853 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
858 ast_mutex_lock(&pvt->lock);
859 if (pvt->rtp && !pvt->recvonly)
860 res = ast_rtp_write(pvt->rtp, frame);
861 __oh323_update_info(c, pvt);
862 ast_mutex_unlock(&pvt->lock);
867 static int oh323_indicate(struct ast_channel *c, int condition, const void *data, size_t datalen)
870 struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
871 char *token = (char *)NULL;
875 ast_mutex_lock(&pvt->lock);
876 token = (pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL);
877 got_progress = pvt->got_progress;
878 if (condition == AST_CONTROL_PROGRESS)
879 pvt->got_progress = 1;
880 else if ((condition == AST_CONTROL_BUSY) || (condition == AST_CONTROL_CONGESTION))
881 pvt->alreadygone = 1;
882 ast_mutex_unlock(&pvt->lock);
885 ast_log(LOG_DEBUG, "OH323: Indicating %d on %s (%s)\n", condition, token, c->name);
888 case AST_CONTROL_RINGING:
889 if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
890 h323_send_alerting(token);
891 res = (got_progress ? 0 : -1); /* Do not simulate any audio tones if we got PROGRESS message */
894 case AST_CONTROL_PROGRESS:
895 if (c->_state != AST_STATE_UP) {
896 /* Do not send PROGRESS message more than once */
898 h323_send_progress(token);
902 case AST_CONTROL_BUSY:
903 if (c->_state != AST_STATE_UP) {
904 h323_answering_call(token, 1);
905 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
909 case AST_CONTROL_CONGESTION:
910 if (c->_state != AST_STATE_UP) {
911 h323_answering_call(token, 1);
912 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
916 case AST_CONTROL_HOLD:
917 h323_hold_call(token, 1);
918 /* We should start MOH only if remote party isn't provide audio for us */
919 ast_moh_start(c, data, NULL);
922 case AST_CONTROL_UNHOLD:
923 h323_hold_call(token, 0);
927 case AST_CONTROL_PROCEEDING:
931 ast_log(LOG_WARNING, "OH323: Don't know how to indicate condition %d on %s\n", condition, token);
936 ast_log(LOG_DEBUG, "OH323: Indicated %d on %s, res=%d\n", condition, token, res);
939 oh323_update_info(c);
944 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
946 struct oh323_pvt *pvt = (struct oh323_pvt *) newchan->tech_pvt;
948 ast_mutex_lock(&pvt->lock);
949 if (pvt->owner != oldchan) {
950 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, pvt->owner);
953 pvt->owner = newchan;
954 ast_mutex_unlock(&pvt->lock);
958 static int __oh323_rtp_create(struct oh323_pvt *pvt)
960 struct in_addr our_addr;
965 if (ast_find_ourip(&our_addr, bindaddr)) {
966 ast_mutex_unlock(&pvt->lock);
967 ast_log(LOG_ERROR, "Unable to locate local IP address for RTP stream\n");
970 pvt->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0, our_addr);
972 ast_mutex_unlock(&pvt->lock);
973 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
977 ast_log(LOG_DEBUG, "Created RTP channel\n");
979 ast_rtp_settos(pvt->rtp, tos);
982 ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", pvt->options.nat);
983 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
985 if (pvt->dtmf_pt[0] > 0)
986 ast_rtp_set_rtpmap_type(pvt->rtp, pvt->dtmf_pt[0], "audio", "telephone-event", 0);
987 if (pvt->dtmf_pt[1] > 0)
988 ast_rtp_set_rtpmap_type(pvt->rtp, pvt->dtmf_pt[1], "audio", "cisco-telephone-event", 0);
990 if (pvt->peercapability)
991 ast_rtp_codec_setpref(pvt->rtp, &pvt->peer_prefs);
993 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
994 ast_jb_configure(pvt->owner, &global_jbconf);
995 pvt->owner->fds[0] = ast_rtp_fd(pvt->rtp);
996 pvt->owner->fds[1] = ast_rtcp_fd(pvt->rtp);
997 ast_queue_frame(pvt->owner, &ast_null_frame); /* Tell Asterisk to apply changes */
998 ast_channel_unlock(pvt->owner);
1000 pvt->update_rtp_info = 1;
1005 /* Private structure should be locked on a call */
1006 static struct ast_channel *__oh323_new(struct oh323_pvt *pvt, int state, const char *host)
1008 struct ast_channel *ch;
1009 char *cid_num, *cid_name;
1012 if (!ast_strlen_zero(pvt->options.cid_num))
1013 cid_num = pvt->options.cid_num;
1015 cid_num = pvt->cd.call_source_e164;
1017 if (!ast_strlen_zero(pvt->options.cid_name))
1018 cid_name = pvt->options.cid_name;
1020 cid_name = pvt->cd.call_source_name;
1022 /* Don't hold a oh323_pvt lock while we allocate a chanel */
1023 ast_mutex_unlock(&pvt->lock);
1024 ch = ast_channel_alloc(1, state, cid_num, cid_name, "H323/%s", host);
1025 /* Update usage counter */
1026 ast_module_ref(ast_module_info->self);
1027 ast_mutex_lock(&pvt->lock);
1029 ch->tech = &oh323_tech;
1030 if (!(fmt = pvt->jointcapability) && !(fmt = pvt->options.capability))
1031 fmt = global_options.capability;
1032 ch->nativeformats = ast_codec_choose(&pvt->options.prefs, fmt, 1)/* | (pvt->jointcapability & AST_FORMAT_VIDEO_MASK)*/;
1033 pvt->nativeformats = ch->nativeformats;
1034 fmt = ast_best_codec(ch->nativeformats);
1035 ch->writeformat = fmt;
1036 ch->rawwriteformat = fmt;
1037 ch->readformat = fmt;
1038 ch->rawreadformat = fmt;
1040 ch->fds[0] = ast_rtp_fd(pvt->rtp);
1041 ch->fds[1] = ast_rtcp_fd(pvt->rtp);
1043 #ifdef VIDEO_SUPPORT
1045 ch->fds[2] = ast_rtp_fd(pvt->vrtp);
1046 ch->fds[3] = ast_rtcp_fd(pvt->vrtp);
1051 ch->fds[4] = ast_udptl_fd(pvt->udptl);
1054 if (state == AST_STATE_RING) {
1057 /* Allocate dsp for in-band DTMF support */
1058 if (pvt->options.dtmfmode & H323_DTMF_INBAND) {
1059 pvt->vad = ast_dsp_new();
1060 ast_dsp_set_features(pvt->vad, DSP_FEATURE_DTMF_DETECT);
1062 /* Register channel functions. */
1064 /* Set the owner of this channel */
1067 ast_copy_string(ch->context, pvt->context, sizeof(ch->context));
1068 ast_copy_string(ch->exten, pvt->exten, sizeof(ch->exten));
1070 if (!ast_strlen_zero(pvt->accountcode)) {
1071 ast_string_field_set(ch, accountcode, pvt->accountcode);
1073 if (pvt->amaflags) {
1074 ch->amaflags = pvt->amaflags;
1077 /* Don't use ast_set_callerid() here because it will
1078 * generate a needless NewCallerID event */
1079 ch->cid.cid_num = ast_strdup(cid_num);
1080 ch->cid.cid_ani = ast_strdup(cid_num);
1081 ch->cid.cid_name = ast_strdup(cid_name);
1083 if (pvt->cd.redirect_reason >= 0) {
1084 ch->cid.cid_rdnis = ast_strdup(pvt->cd.redirect_number);
1085 pbx_builtin_setvar_helper(ch, "PRIREDIRECTREASON", redirectingreason2str(pvt->cd.redirect_reason));
1087 ch->cid.cid_pres = pvt->cd.presentation;
1088 ch->cid.cid_ton = pvt->cd.type_of_number;
1090 if (!ast_strlen_zero(pvt->exten) && strcmp(pvt->exten, "s")) {
1091 ch->cid.cid_dnid = strdup(pvt->exten);
1093 if (pvt->cd.transfer_capability >= 0)
1094 ch->transfercapability = pvt->cd.transfer_capability;
1095 if (state != AST_STATE_DOWN) {
1096 if (ast_pbx_start(ch)) {
1097 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
1103 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
1108 static struct oh323_pvt *oh323_alloc(int callid)
1110 struct oh323_pvt *pvt;
1112 pvt = (struct oh323_pvt *) malloc(sizeof(struct oh323_pvt));
1114 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
1117 memset(pvt, 0, sizeof(struct oh323_pvt));
1118 pvt->cd.redirect_reason = -1;
1119 pvt->cd.transfer_capability = -1;
1120 /* Ensure the call token is allocated for outgoing call */
1122 if ((pvt->cd).call_token == NULL) {
1123 (pvt->cd).call_token = (char *)malloc(128);
1125 if (!pvt->cd.call_token) {
1126 ast_log(LOG_ERROR, "Not enough memory to alocate call token\n");
1127 ast_rtp_destroy(pvt->rtp);
1131 memset((char *)(pvt->cd).call_token, 0, 128);
1132 pvt->cd.call_reference = callid;
1134 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
1135 pvt->jointcapability = pvt->options.capability;
1136 if (pvt->options.dtmfmode & (H323_DTMF_RFC2833 | H323_DTMF_CISCO)) {
1137 pvt->nonCodecCapability |= AST_RTP_DTMF;
1139 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1141 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
1142 pvt->newstate = pvt->newcontrol = pvt->newdigit = pvt->update_rtp_info = pvt->DTMFsched = -1;
1143 ast_mutex_init(&pvt->lock);
1144 /* Add to interface list */
1145 ast_mutex_lock(&iflock);
1148 ast_mutex_unlock(&iflock);
1152 static struct oh323_pvt *find_call_locked(int call_reference, const char *token)
1154 struct oh323_pvt *pvt;
1156 ast_mutex_lock(&iflock);
1159 if (!pvt->needdestroy && ((signed int)pvt->cd.call_reference == call_reference)) {
1160 /* Found the call */
1161 if ((token != NULL) && (!strcmp(pvt->cd.call_token, token))) {
1162 ast_mutex_lock(&pvt->lock);
1163 ast_mutex_unlock(&iflock);
1165 } else if (token == NULL) {
1166 ast_log(LOG_WARNING, "Call Token is NULL\n");
1167 ast_mutex_lock(&pvt->lock);
1168 ast_mutex_unlock(&iflock);
1174 ast_mutex_unlock(&iflock);
1178 static int update_state(struct oh323_pvt *pvt, int state, int signal)
1182 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1184 ast_setstate(pvt->owner, state);
1186 ast_queue_control(pvt->owner, signal);
1187 ast_channel_unlock(pvt->owner);
1192 pvt->newstate = state;
1194 pvt->newcontrol = signal;
1199 static struct oh323_alias *build_alias(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1201 struct oh323_alias *alias;
1204 alias = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&aliasl, name, name, 0, 0, strcasecmp);
1209 if (!(alias = (struct oh323_alias *)calloc(1, sizeof(*alias))))
1214 ast_copy_string(alias->name, name, sizeof(alias->name));
1215 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1216 if (!strcasecmp(v->name, "e164")) {
1217 ast_copy_string(alias->e164, v->value, sizeof(alias->e164));
1218 } else if (!strcasecmp(v->name, "prefix")) {
1219 ast_copy_string(alias->prefix, v->value, sizeof(alias->prefix));
1220 } else if (!strcasecmp(v->name, "context")) {
1221 ast_copy_string(alias->context, v->value, sizeof(alias->context));
1222 } else if (!strcasecmp(v->name, "secret")) {
1223 ast_copy_string(alias->secret, v->value, sizeof(alias->secret));
1225 if (strcasecmp(v->value, "h323")) {
1226 ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->name);
1230 ASTOBJ_UNMARK(alias);
1234 static struct oh323_alias *realtime_alias(const char *alias)
1236 struct ast_variable *var, *tmp;
1237 struct oh323_alias *a;
1239 var = ast_load_realtime("h323", "name", alias, NULL);
1244 for (tmp = var; tmp; tmp = tmp->next) {
1245 if (!strcasecmp(tmp->name, "type") &&
1246 !(!strcasecmp(tmp->value, "alias") || !strcasecmp(tmp->value, "h323"))) {
1247 ast_variables_destroy(var);
1252 a = build_alias(alias, var, NULL, 1);
1254 ast_variables_destroy(var);
1259 static int update_common_options(struct ast_variable *v, struct call_options *options)
1264 if (!strcasecmp(v->name, "allow")) {
1265 ast_parse_allow_disallow(&options->prefs, &options->capability, v->value, 1);
1266 } else if (!strcasecmp(v->name, "disallow")) {
1267 ast_parse_allow_disallow(&options->prefs, &options->capability, v->value, 0);
1268 } else if (!strcasecmp(v->name, "dtmfmode")) {
1269 val = ast_strdupa(v->value);
1270 if ((opt = strchr(val, ':')) != (char *)NULL) {
1274 if (!strcasecmp(v->value, "inband")) {
1275 options->dtmfmode |= H323_DTMF_INBAND;
1276 } else if (!strcasecmp(val, "rfc2833")) {
1277 options->dtmfmode |= H323_DTMF_RFC2833;
1279 options->dtmfcodec[0] = H323_DTMF_RFC2833_PT;
1280 } else if ((tmp >= 96) && (tmp < 128)) {
1281 options->dtmfcodec[0] = tmp;
1283 options->dtmfcodec[0] = H323_DTMF_RFC2833_PT;
1284 ast_log(LOG_WARNING, "Unknown rfc2833 payload %s specified at line %d, using default %d\n", opt, v->lineno, options->dtmfcodec[0]);
1286 } else if (!strcasecmp(val, "cisco")) {
1287 options->dtmfmode |= H323_DTMF_CISCO;
1289 options->dtmfcodec[1] = H323_DTMF_CISCO_PT;
1290 } else if ((tmp >= 96) && (tmp < 128)) {
1291 options->dtmfcodec[1] = tmp;
1293 options->dtmfcodec[1] = H323_DTMF_CISCO_PT;
1294 ast_log(LOG_WARNING, "Unknown Cisco DTMF payload %s specified at line %d, using default %d\n", opt, v->lineno, options->dtmfcodec[1]);
1296 } else if (!strcasecmp(v->value, "h245-signal")) {
1297 options->dtmfmode |= H323_DTMF_SIGNAL;
1299 ast_log(LOG_WARNING, "Unknown dtmf mode '%s' at line %d\n", v->value, v->lineno);
1301 } else if (!strcasecmp(v->name, "dtmfcodec")) {
1302 ast_log(LOG_NOTICE, "Option %s at line %d is deprecated. Use dtmfmode=rfc2833[:<payload>] instead.\n", v->name, v->lineno);
1303 tmp = atoi(v->value);
1305 ast_log(LOG_WARNING, "Invalid %s value %s at line %d\n", v->name, v->value, v->lineno);
1307 options->dtmfcodec[0] = tmp;
1308 } else if (!strcasecmp(v->name, "bridge")) {
1309 options->bridge = ast_true(v->value);
1310 } else if (!strcasecmp(v->name, "nat")) {
1311 options->nat = ast_true(v->value);
1312 } else if (!strcasecmp(v->name, "fastStart")) {
1313 options->fastStart = ast_true(v->value);
1314 } else if (!strcasecmp(v->name, "h245Tunneling")) {
1315 options->h245Tunneling = ast_true(v->value);
1316 } else if (!strcasecmp(v->name, "silenceSuppression")) {
1317 options->silenceSuppression = ast_true(v->value);
1318 } else if (!strcasecmp(v->name, "progress_setup")) {
1319 tmp = atoi(v->value);
1320 if ((tmp != 0) && (tmp != 1) && (tmp != 3) && (tmp != 8)) {
1321 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d, assuming 0\n", v->value, v->name, v->lineno);
1324 options->progress_setup = tmp;
1325 } else if (!strcasecmp(v->name, "progress_alert")) {
1326 tmp = atoi(v->value);
1327 if ((tmp != 0) && (tmp != 1) && (tmp != 8)) {
1328 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d, assuming 0\n", v->value, v->name, v->lineno);
1331 options->progress_alert = tmp;
1332 } else if (!strcasecmp(v->name, "progress_audio")) {
1333 options->progress_audio = ast_true(v->value);
1334 } else if (!strcasecmp(v->name, "callerid")) {
1335 ast_callerid_split(v->value, options->cid_name, sizeof(options->cid_name), options->cid_num, sizeof(options->cid_num));
1336 } else if (!strcasecmp(v->name, "fullname")) {
1337 ast_copy_string(options->cid_name, v->value, sizeof(options->cid_name));
1338 } else if (!strcasecmp(v->name, "cid_number")) {
1339 ast_copy_string(options->cid_num, v->value, sizeof(options->cid_num));
1340 } else if (!strcasecmp(v->name, "tunneling")) {
1341 if (!strcasecmp(v->value, "none"))
1342 options->tunnelOptions = 0;
1343 else if (!strcasecmp(v->value, "cisco"))
1344 options->tunnelOptions |= H323_TUNNEL_CISCO;
1345 else if (!strcasecmp(v->value, "qsig"))
1346 options->tunnelOptions |= H323_TUNNEL_QSIG;
1348 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d\n", v->value, v->name, v->lineno);
1349 } else if (!strcasecmp(v->name, "hold")) {
1350 if (!strcasecmp(v->value, "none"))
1351 options->holdHandling = ~0;
1352 else if (!strcasecmp(v->value, "notify"))
1353 options->holdHandling |= H323_HOLD_NOTIFY;
1354 else if (!strcasecmp(v->value, "q931only"))
1355 options->holdHandling |= H323_HOLD_NOTIFY | H323_HOLD_Q931ONLY;
1356 else if (!strcasecmp(v->value, "h450"))
1357 options->holdHandling |= H323_HOLD_H450;
1359 ast_log(LOG_WARNING, "Invalid value %s for %s at line %d\n", v->value, v->name, v->lineno);
1366 static struct oh323_user *build_user(char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1368 struct oh323_user *user;
1369 struct ast_ha *oldha;
1373 user = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&userl, name, name, 0, 0, strcmp);
1378 if (!(user = (struct oh323_user *)calloc(1, sizeof(*user))))
1383 user->ha = (struct ast_ha *)NULL;
1384 memcpy(&user->options, &global_options, sizeof(user->options));
1385 user->options.dtmfmode = 0;
1386 user->options.holdHandling = 0;
1387 /* Set default context */
1388 ast_copy_string(user->context, default_context, sizeof(user->context));
1390 ast_copy_string(user->name, name, sizeof(user->name));
1392 #if 0 /* XXX Port channel variables functionality from chan_sip XXX */
1393 if (user->chanvars) {
1394 ast_variables_destroy(user->chanvars);
1395 user->chanvars = NULL;
1399 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1400 if (!update_common_options(v, &user->options))
1402 if (!strcasecmp(v->name, "context")) {
1403 ast_copy_string(user->context, v->value, sizeof(user->context));
1404 } else if (!strcasecmp(v->name, "secret")) {
1405 ast_copy_string(user->secret, v->value, sizeof(user->secret));
1406 } else if (!strcasecmp(v->name, "accountcode")) {
1407 ast_copy_string(user->accountcode, v->value, sizeof(user->accountcode));
1408 } else if (!strcasecmp(v->name, "host")) {
1409 if (!strcasecmp(v->value, "dynamic")) {
1410 ast_log(LOG_ERROR, "A dynamic host on a type=user does not make any sense\n");
1411 ASTOBJ_UNREF(user, oh323_destroy_user);
1413 } else if (ast_get_ip(&user->addr, v->value)) {
1414 ASTOBJ_UNREF(user, oh323_destroy_user);
1417 /* Let us know we need to use ip authentication */
1419 } else if (!strcasecmp(v->name, "amaflags")) {
1420 format = ast_cdr_amaflags2int(v->value);
1422 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
1424 user->amaflags = format;
1426 } else if (!strcasecmp(v->name, "permit") ||
1427 !strcasecmp(v->name, "deny")) {
1428 user->ha = ast_append_ha(v->name, v->value, user->ha);
1431 if (!user->options.dtmfmode)
1432 user->options.dtmfmode = global_options.dtmfmode;
1433 if (user->options.holdHandling == ~0)
1434 user->options.holdHandling = 0;
1435 else if (!user->options.holdHandling)
1436 user->options.holdHandling = global_options.holdHandling;
1437 ASTOBJ_UNMARK(user);
1442 static struct oh323_user *realtime_user(const call_details_t *cd)
1444 struct ast_variable *var, *tmp;
1445 struct oh323_user *user;
1449 var = ast_load_realtime("h323", "name", username = cd->call_source_aliases, NULL);
1451 username = (char *)NULL;
1452 var = ast_load_realtime("h323", "host", cd->sourceIp, NULL);
1458 for (tmp = var; tmp; tmp = tmp->next) {
1459 if (!strcasecmp(tmp->name, "type") &&
1460 !(!strcasecmp(tmp->value, "user") || !strcasecmp(tmp->value, "friend"))) {
1461 ast_variables_destroy(var);
1463 } else if (!username && !strcasecmp(tmp->name, "name"))
1464 username = tmp->value;
1468 ast_log(LOG_WARNING, "Cannot determine user name for IP address %s\n", cd->sourceIp);
1469 ast_variables_destroy(var);
1473 user = build_user(username, var, NULL, 1);
1475 ast_variables_destroy(var);
1480 static struct oh323_peer *build_peer(const char *name, struct ast_variable *v, struct ast_variable *alt, int realtime)
1482 struct oh323_peer *peer;
1483 struct ast_ha *oldha;
1486 peer = ASTOBJ_CONTAINER_FIND_UNLINK_FULL(&peerl, name, name, 0, 0, strcmp);
1491 if (!(peer = (struct oh323_peer*)calloc(1, sizeof(*peer))))
1497 memcpy(&peer->options, &global_options, sizeof(peer->options));
1498 peer->options.dtmfmode = 0;
1499 peer->options.holdHandling = 0;
1500 peer->addr.sin_port = htons(h323_signalling_port);
1501 peer->addr.sin_family = AF_INET;
1503 ast_copy_string(peer->name, name, sizeof(peer->name));
1505 #if 0 /* XXX Port channel variables functionality from chan_sip XXX */
1506 if (peer->chanvars) {
1507 ast_variables_destroy(peer->chanvars);
1508 peer->chanvars = NULL;
1511 /* Default settings for mailbox */
1512 peer->mailbox[0] = '\0';
1514 for (; v || ((v = alt) && !(alt = NULL)); v = v->next) {
1515 if (!update_common_options(v, &peer->options))
1517 if (!strcasecmp(v->name, "host")) {
1518 if (!strcasecmp(v->value, "dynamic")) {
1519 ast_log(LOG_ERROR, "Dynamic host configuration not implemented.\n");
1520 ASTOBJ_UNREF(peer, oh323_destroy_peer);
1523 if (ast_get_ip(&peer->addr, v->value)) {
1524 ast_log(LOG_ERROR, "Could not determine IP for %s\n", v->value);
1525 ASTOBJ_UNREF(peer, oh323_destroy_peer);
1528 } else if (!strcasecmp(v->name, "port")) {
1529 peer->addr.sin_port = htons(atoi(v->value));
1530 } else if (!strcasecmp(v->name, "permit") ||
1531 !strcasecmp(v->name, "deny")) {
1532 peer->ha = ast_append_ha(v->name, v->value, peer->ha);
1533 } else if (!strcasecmp(v->name, "mailbox")) {
1534 ast_copy_string(peer->mailbox, v->value, 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;
1555 /* First check on peer name */
1557 var = ast_load_realtime("h323", "name", peername, addr = NULL);
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), NULL);
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_log(LOG_DEBUG, "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_log(LOG_DEBUG, "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_log(LOG_DEBUG, "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_MAX_AUDIO << 1) - 1);
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_log(LOG_DEBUG, "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 /** 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,
1842 .src = "SEND_DIGIT",
1844 if (digit == ' ') { /* signalUpdate message */
1845 f.subclass = pvt->curDTMF;
1846 if (pvt->DTMFsched >= 0) {
1847 ast_sched_del(sched, pvt->DTMFsched);
1848 pvt->DTMFsched = -1;
1850 } else { /* Regular input or signal message */
1851 if (duration) { /* This is a signal, signalUpdate follows */
1852 f.frametype = AST_FRAME_DTMF_BEGIN;
1853 if (pvt->DTMFsched >= 0)
1854 ast_sched_del(sched, pvt->DTMFsched);
1855 pvt->DTMFsched = ast_sched_add(sched, duration, oh323_simulate_dtmf_end, pvt);
1857 ast_log(LOG_DTMF, "Scheduled DTMF END simulation for %d ms, id=%d\n", duration, pvt->DTMFsched);
1859 pvt->curDTMF = digit;
1861 res = ast_queue_frame(pvt->owner, &f);
1863 ast_channel_unlock(pvt->owner);
1866 pvt->newcontrol = AST_CONTROL_FLASH;
1868 pvt->newduration = duration;
1869 pvt->newdigit = digit;
1873 ast_mutex_unlock(&pvt->lock);
1878 * Callback function used to inform the H.323 stack of the local rtp ip/port details
1880 * Returns the local RTP information
1882 static struct rtp_info *external_rtp_create(unsigned call_reference, const char * token)
1884 struct oh323_pvt *pvt;
1885 struct sockaddr_in us;
1886 struct rtp_info *info;
1888 info = (struct rtp_info *)malloc(sizeof(struct rtp_info));
1890 ast_log(LOG_ERROR, "Unable to allocated info structure, this is very bad\n");
1893 pvt = find_call_locked(call_reference, token);
1896 ast_log(LOG_ERROR, "Unable to find call %s(%d)\n", token, call_reference);
1900 __oh323_rtp_create(pvt);
1902 ast_mutex_unlock(&pvt->lock);
1904 ast_log(LOG_ERROR, "No RTP stream is available for call %s (%d)", token, call_reference);
1907 /* figure out our local RTP port and tell the H.323 stack about it */
1908 ast_rtp_get_us(pvt->rtp, &us);
1909 ast_mutex_unlock(&pvt->lock);
1911 ast_copy_string(info->addr, ast_inet_ntoa(us.sin_addr), sizeof(info->addr));
1912 info->port = ntohs(us.sin_port);
1914 ast_log(LOG_DEBUG, "Sending RTP 'US' %s:%d\n", info->addr, info->port);
1919 * Definition taken from rtp.c for rtpPayloadType because we need it here.
1921 struct rtpPayloadType {
1922 int isAstFormat; /* whether the following code is an AST_FORMAT */
1927 * Call-back function passing remote ip/port information from H.323 to asterisk
1931 static void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort, const char *token, int pt)
1933 struct oh323_pvt *pvt;
1934 struct sockaddr_in them;
1935 struct rtpPayloadType rtptype;
1936 int nativeformats_changed;
1937 enum { NEED_NONE, NEED_HOLD, NEED_UNHOLD } rtp_change = NEED_NONE;
1940 ast_log(LOG_DEBUG, "Setting up RTP connection for %s\n", token);
1942 /* Find the call or allocate a private structure if call not found */
1943 pvt = find_call_locked(call_reference, token);
1945 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1948 if (pvt->alreadygone) {
1949 ast_mutex_unlock(&pvt->lock);
1954 __oh323_rtp_create(pvt);
1956 if ((pt == 2) && (pvt->jointcapability & AST_FORMAT_G726_AAL2)) {
1957 ast_rtp_set_rtpmap_type(pvt->rtp, pt, "audio", "G726-32", AST_RTP_OPT_G726_NONSTANDARD);
1960 them.sin_family = AF_INET;
1961 /* only works for IPv4 */
1962 them.sin_addr.s_addr = inet_addr(remoteIp);
1963 them.sin_port = htons(remotePort);
1965 if (them.sin_addr.s_addr) {
1966 ast_rtp_set_peer(pvt->rtp, &them);
1967 if (pvt->recvonly) {
1969 rtp_change = NEED_UNHOLD;
1972 ast_rtp_stop(pvt->rtp);
1973 if (!pvt->recvonly) {
1975 rtp_change = NEED_HOLD;
1979 /* Change native format to reflect information taken from OLC/OLCAck */
1980 nativeformats_changed = 0;
1981 if (pt != 128 && pvt->rtp) { /* Payload type is invalid, so try to use previously decided */
1982 rtptype = ast_rtp_lookup_pt(pvt->rtp, pt);
1984 ast_log(LOG_DEBUG, "Native format is set to %d from %d by RTP payload type %d\n", rtptype.code, pvt->nativeformats, pt);
1985 if (pvt->nativeformats != rtptype.code) {
1986 pvt->nativeformats = rtptype.code;
1987 nativeformats_changed = 1;
1989 } else if (h323debug)
1990 ast_log(LOG_NOTICE, "Payload type is unknown, formats isn't changed\n");
1992 /* Don't try to lock the channel if nothing changed */
1993 if (nativeformats_changed || pvt->options.progress_audio || (rtp_change != NEED_NONE)) {
1994 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
1995 /* Re-build translation path only if native format(s) has been changed */
1996 if (pvt->owner->nativeformats != pvt->nativeformats) {
1998 ast_log(LOG_DEBUG, "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);
1999 pvt->owner->nativeformats = pvt->nativeformats;
2000 ast_set_read_format(pvt->owner, pvt->owner->readformat);
2001 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
2003 if (pvt->options.progress_audio)
2004 ast_queue_control(pvt->owner, AST_CONTROL_PROGRESS);
2005 switch (rtp_change) {
2007 ast_queue_control(pvt->owner, AST_CONTROL_HOLD);
2010 ast_queue_control(pvt->owner, AST_CONTROL_UNHOLD);
2015 ast_channel_unlock(pvt->owner);
2018 if (pvt->options.progress_audio)
2019 pvt->newcontrol = AST_CONTROL_PROGRESS;
2020 else if (rtp_change == NEED_HOLD)
2021 pvt->newcontrol = AST_CONTROL_HOLD;
2022 else if (rtp_change == NEED_UNHOLD)
2023 pvt->newcontrol = AST_CONTROL_UNHOLD;
2025 ast_log(LOG_DEBUG, "RTP connection preparation for %s is pending...\n", token);
2028 ast_mutex_unlock(&pvt->lock);
2031 ast_log(LOG_DEBUG, "RTP connection prepared for %s\n", token);
2037 * Call-back function to signal asterisk that the channel has been answered
2040 static void connection_made(unsigned call_reference, const char *token)
2042 struct oh323_pvt *pvt;
2045 ast_log(LOG_DEBUG, "Call %s answered\n", token);
2047 pvt = find_call_locked(call_reference, token);
2049 ast_log(LOG_ERROR, "Something is wrong: connection\n");
2053 /* Inform asterisk about remote party connected only on outgoing calls */
2054 if (!pvt->outgoing) {
2055 ast_mutex_unlock(&pvt->lock);
2058 /* Do not send ANSWER message more than once */
2059 if (!pvt->connection_established) {
2060 pvt->connection_established = 1;
2061 update_state(pvt, -1, AST_CONTROL_ANSWER);
2063 ast_mutex_unlock(&pvt->lock);
2067 static int progress(unsigned call_reference, const char *token, int inband)
2069 struct oh323_pvt *pvt;
2072 ast_log(LOG_DEBUG, "Received ALERT/PROGRESS message for %s tones\n", (inband ? "inband" : "self-generated"));
2074 pvt = find_call_locked(call_reference, token);
2076 ast_log(LOG_ERROR, "Private structure not found in progress.\n");
2080 ast_mutex_unlock(&pvt->lock);
2081 ast_log(LOG_ERROR, "No Asterisk channel associated with private structure.\n");
2084 update_state(pvt, -1, (inband ? AST_CONTROL_PROGRESS : AST_CONTROL_RINGING));
2085 ast_mutex_unlock(&pvt->lock);
2091 * Call-back function for incoming calls
2093 * Returns 1 on success
2095 static call_options_t *setup_incoming_call(call_details_t *cd)
2097 struct oh323_pvt *pvt;
2098 struct oh323_user *user = NULL;
2099 struct oh323_alias *alias = NULL;
2102 ast_log(LOG_DEBUG, "Setting up incoming call for %s\n", cd->call_token);
2104 /* allocate the call*/
2105 pvt = oh323_alloc(cd->call_reference);
2108 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
2109 cleanup_call_details(cd);
2113 /* Populate the call details in the private structure */
2114 memcpy(&pvt->cd, cd, sizeof(pvt->cd));
2115 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
2116 pvt->jointcapability = pvt->options.capability;
2119 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
2120 ast_verbose(VERBOSE_PREFIX_3 " \tCall token: [%s]\n", pvt->cd.call_token);
2121 ast_verbose(VERBOSE_PREFIX_3 " \tCalling party name: [%s]\n", pvt->cd.call_source_name);
2122 ast_verbose(VERBOSE_PREFIX_3 " \tCalling party number: [%s]\n", pvt->cd.call_source_e164);
2123 ast_verbose(VERBOSE_PREFIX_3 " \tCalled party name: [%s]\n", pvt->cd.call_dest_alias);
2124 ast_verbose(VERBOSE_PREFIX_3 " \tCalled party number: [%s]\n", pvt->cd.call_dest_e164);
2125 if (pvt->cd.redirect_reason >= 0)
2126 ast_verbose(VERBOSE_PREFIX_3 " \tRedirecting party number: [%s] (reason %d)\n", pvt->cd.redirect_number, pvt->cd.redirect_reason);
2127 ast_verbose(VERBOSE_PREFIX_3 " \tCalling party IP: [%s]\n", pvt->cd.sourceIp);
2130 /* Decide if we are allowing Gatekeeper routed calls*/
2131 if ((!strcasecmp(cd->sourceIp, gatekeeper)) && (gkroute == -1) && !gatekeeper_disable) {
2132 if (!ast_strlen_zero(cd->call_dest_e164)) {
2133 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2134 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2136 alias = find_alias(cd->call_dest_alias, 1);
2138 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd->call_dest_alias);
2142 ast_copy_string(pvt->exten, alias->name, sizeof(pvt->exten));
2143 ast_copy_string(pvt->context, alias->context, sizeof(pvt->context));
2146 /* Either this call is not from the Gatekeeper
2147 or we are not allowing gk routed calls */
2148 user = find_user(cd, 1);
2150 if (!acceptAnonymous) {
2151 ast_log(LOG_NOTICE, "Anonymous call from '%s@%s' rejected\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
2155 if (ast_strlen_zero(default_context)) {
2156 ast_log(LOG_ERROR, "Call from '%s@%s' rejected due to no default context\n", pvt->cd.call_source_aliases, pvt->cd.sourceIp);
2160 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2161 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
2162 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2164 ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
2167 ast_log(LOG_DEBUG, "Sending %s@%s to context [%s] extension %s\n", cd->call_source_aliases, cd->sourceIp, pvt->context, pvt->exten);
2170 if (strcasecmp(cd->sourceIp, ast_inet_ntoa(user->addr.sin_addr))) {
2171 if (ast_strlen_zero(user->context)) {
2172 if (ast_strlen_zero(default_context)) {
2173 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd->sourceIp);
2175 ASTOBJ_UNREF(user, oh323_destroy_user);
2178 ast_copy_string(pvt->context, default_context, sizeof(pvt->context));
2180 ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
2182 pvt->exten[0] = 'i';
2183 pvt->exten[1] = '\0';
2184 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd->sourceIp);
2186 ASTOBJ_UNREF(user, oh323_destroy_user);
2187 return NULL; /* XXX: Hmmm... Why to setup context if we drop connection immediately??? */
2190 ast_copy_string(pvt->context, user->context, sizeof(pvt->context));
2191 memcpy(&pvt->options, &user->options, sizeof(pvt->options));
2192 pvt->jointcapability = pvt->options.capability;
2193 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
2194 ast_copy_string(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten));
2196 ast_copy_string(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten));
2198 if (!ast_strlen_zero(user->accountcode)) {
2199 ast_copy_string(pvt->accountcode, user->accountcode, sizeof(pvt->accountcode));
2201 if (user->amaflags) {
2202 pvt->amaflags = user->amaflags;
2204 ASTOBJ_UNREF(user, oh323_destroy_user);
2207 return &pvt->options;
2211 * Call-back function to start PBX when OpenH323 ready to serve incoming call
2213 * Returns 1 on success
2215 static int answer_call(unsigned call_reference, const char *token)
2217 struct oh323_pvt *pvt;
2218 struct ast_channel *c = NULL;
2219 enum {ext_original, ext_s, ext_i, ext_notexists} try_exten;
2220 char tmp_exten[sizeof(pvt->exten)];
2223 ast_log(LOG_DEBUG, "Preparing Asterisk to answer for %s\n", token);
2225 /* Find the call or allocate a private structure if call not found */
2226 pvt = find_call_locked(call_reference, token);
2228 ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
2231 /* Check if requested extension@context pair exists in the dialplan */
2232 ast_copy_string(tmp_exten, pvt->exten, sizeof(tmp_exten));
2234 /* Try to find best extension in specified context */
2235 if ((tmp_exten[0] != '\0') && (tmp_exten[1] == '\0')) {
2236 if (tmp_exten[0] == 's')
2238 else if (tmp_exten[0] == 'i')
2241 try_exten = ext_original;
2243 try_exten = ext_original;
2245 if (ast_exists_extension(NULL, pvt->context, tmp_exten, 1, NULL))
2247 switch (try_exten) {
2250 tmp_exten[1] = '\0';
2258 try_exten = ext_notexists;
2263 } while (try_exten != ext_notexists);
2265 /* Drop the call if we don't have <exten>, s and i extensions */
2266 if (try_exten == ext_notexists) {
2267 ast_log(LOG_NOTICE, "Dropping call because extensions '%s', 's' and 'i' doesn't exists in context [%s]\n", pvt->exten, pvt->context);
2268 ast_mutex_unlock(&pvt->lock);
2269 h323_clear_call(token, AST_CAUSE_UNALLOCATED);
2271 } else if ((try_exten != ext_original) && (strcmp(pvt->exten, tmp_exten) != 0)) {
2273 ast_log(LOG_DEBUG, "Going to extension %s@%s because %s@%s isn't exists\n", tmp_exten, pvt->context, pvt->exten, pvt->context);
2274 ast_copy_string(pvt->exten, tmp_exten, sizeof(pvt->exten));
2277 /* allocate a channel and tell asterisk about it */
2278 c = __oh323_new(pvt, AST_STATE_RINGING, pvt->cd.call_token);
2280 /* And release when done */
2281 ast_mutex_unlock(&pvt->lock);
2283 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
2290 * Call-back function to establish an outgoing H.323 call
2292 * Returns 1 on success
2294 static int setup_outgoing_call(call_details_t *cd)
2296 /* Use argument here or free it immediately */
2297 cleanup_call_details(cd);
2303 * Call-back function to signal asterisk that the channel is ringing
2306 static void chan_ringing(unsigned call_reference, const char *token)
2308 struct oh323_pvt *pvt;
2311 ast_log(LOG_DEBUG, "Ringing on %s\n", token);
2313 pvt = find_call_locked(call_reference, token);
2315 ast_log(LOG_ERROR, "Something is wrong: ringing\n");
2319 ast_mutex_unlock(&pvt->lock);
2320 ast_log(LOG_ERROR, "Channel has no owner\n");
2323 update_state(pvt, AST_STATE_RINGING, AST_CONTROL_RINGING);
2324 ast_mutex_unlock(&pvt->lock);
2329 * Call-back function to cleanup communication
2332 static void cleanup_connection(unsigned call_reference, const char *call_token)
2334 struct oh323_pvt *pvt;
2337 ast_log(LOG_DEBUG, "Cleaning connection to %s\n", call_token);
2340 pvt = find_call_locked(call_reference, call_token);
2343 ast_log(LOG_DEBUG, "No connection for %s\n", call_token);
2346 if (!pvt->owner || !ast_channel_trylock(pvt->owner))
2349 #ifdef DEBUG_THREADS
2350 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s, locked at %ld/%d by %s (%s:%d)\n", call_token, pvt->owner->lock.thread[0], pvt->owner->lock.reentrancy, pvt->owner->lock.func[0], pvt->owner->lock.file[0], pvt->owner->lock.lineno[0]);
2352 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s\n", call_token);
2355 ast_mutex_unlock(&pvt->lock);
2359 /* Immediately stop RTP */
2360 ast_rtp_destroy(pvt->rtp);
2363 /* Free dsp used for in-band DTMF detection */
2365 ast_dsp_free(pvt->vad);
2368 cleanup_call_details(&pvt->cd);
2369 pvt->alreadygone = 1;
2372 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2373 ast_queue_hangup(pvt->owner);
2374 ast_channel_unlock(pvt->owner);
2376 ast_mutex_unlock(&pvt->lock);
2378 ast_log(LOG_DEBUG, "Connection to %s cleaned\n", call_token);
2382 static void hangup_connection(unsigned int call_reference, const char *token, int cause)
2384 struct oh323_pvt *pvt;
2387 ast_log(LOG_DEBUG, "Hanging up connection to %s with cause %d\n", token, cause);
2390 pvt = find_call_locked(call_reference, token);
2393 ast_log(LOG_DEBUG, "Connection to %s already cleared\n", token);
2397 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
2398 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
2399 pvt->owner->hangupcause = pvt->hangupcause = cause;
2400 ast_queue_hangup(pvt->owner);
2401 ast_channel_unlock(pvt->owner);
2404 pvt->needhangup = 1;
2405 pvt->hangupcause = cause;
2407 ast_log(LOG_DEBUG, "Hangup for %s is pending\n", token);
2409 ast_mutex_unlock(&pvt->lock);
2412 static void set_dtmf_payload(unsigned call_reference, const char *token, int payload, int is_cisco)
2414 struct oh323_pvt *pvt;
2417 ast_log(LOG_DEBUG, "Setting %s DTMF payload to %d on %s\n", (is_cisco ? "Cisco" : "RFC2833"), payload, token);
2419 pvt = find_call_locked(call_reference, token);
2424 ast_rtp_set_rtpmap_type(pvt->rtp, payload, "audio", (is_cisco ? "cisco-telephone-event" : "telephone-event"), 0);
2426 pvt->dtmf_pt[is_cisco ? 1 : 0] = payload;
2427 ast_mutex_unlock(&pvt->lock);
2429 ast_log(LOG_DEBUG, "DTMF payload on %s set to %d\n", token, payload);
2432 static void set_peer_capabilities(unsigned call_reference, const char *token, int capabilities, struct ast_codec_pref *prefs)
2434 struct oh323_pvt *pvt;
2437 ast_log(LOG_DEBUG, "Got remote capabilities from connection %s\n", token);
2439 pvt = find_call_locked(call_reference, token);
2442 pvt->peercapability = capabilities;
2443 pvt->jointcapability = pvt->options.capability & capabilities;
2445 memcpy(&pvt->peer_prefs, prefs, sizeof(pvt->peer_prefs));
2448 for (i = 0; i < 32; ++i) {
2449 ast_log(LOG_DEBUG, "prefs[%d]=%s:%d\n", i, (prefs->order[i] ? ast_getformatname(1 << (prefs->order[i]-1)) : "<none>"), prefs->framing[i]);
2453 ast_rtp_codec_setpref(pvt->rtp, &pvt->peer_prefs);
2455 ast_mutex_unlock(&pvt->lock);
2458 static void set_local_capabilities(unsigned call_reference, const char *token)
2460 struct oh323_pvt *pvt;
2461 int capability, dtmfmode, pref_codec;
2462 struct ast_codec_pref prefs;
2465 ast_log(LOG_DEBUG, "Setting capabilities for connection %s\n", token);
2467 pvt = find_call_locked(call_reference, token);
2470 capability = (pvt->jointcapability) ? pvt->jointcapability : pvt->options.capability;
2471 dtmfmode = pvt->options.dtmfmode;
2472 prefs = pvt->options.prefs;
2473 pref_codec = pvt->pref_codec;
2474 ast_mutex_unlock(&pvt->lock);
2475 h323_set_capabilities(token, capability, dtmfmode, &prefs, pref_codec);
2478 ast_log(LOG_DEBUG, "Capabilities for connection %s is set\n", token);
2481 static void remote_hold(unsigned call_reference, const char *token, int is_hold)
2483 struct oh323_pvt *pvt;
2486 ast_log(LOG_DEBUG, "Setting %shold status for connection %s\n", (is_hold ? "" : "un"), token);
2488 pvt = find_call_locked(call_reference, token);
2491 if (pvt->owner && !ast_channel_trylock(pvt->owner)) {
2493 ast_queue_control(pvt->owner, AST_CONTROL_HOLD);
2495 ast_queue_control(pvt->owner, AST_CONTROL_UNHOLD);
2496 ast_channel_unlock(pvt->owner);
2500 pvt->newcontrol = AST_CONTROL_HOLD;
2502 pvt->newcontrol = AST_CONTROL_UNHOLD;
2504 ast_mutex_unlock(&pvt->lock);
2507 static void *do_monitor(void *data)
2511 struct oh323_pvt *oh323 = NULL;
2514 /* Check for a reload request */
2515 ast_mutex_lock(&h323_reload_lock);
2516 reloading = h323_reloading;
2518 ast_mutex_unlock(&h323_reload_lock);
2520 if (option_verbose > 0) {
2521 ast_verbose(VERBOSE_PREFIX_1 "Reloading H.323\n");
2525 /* Check for interfaces needing to be killed */
2526 if (!ast_mutex_trylock(&iflock)) {
2529 for (oh323 = iflist; oh323; oh323 = oh323->next) {
2530 if (!ast_mutex_trylock(&oh323->lock)) {
2531 if (oh323->needdestroy) {
2532 __oh323_destroy(oh323);
2535 ast_mutex_unlock(&oh323->lock);
2538 } while (/*oh323*/ 0);
2543 if (!ast_mutex_trylock(&oh323->lock)) {
2544 if (oh323->needdestroy) {
2545 __oh323_destroy(oh323);
2548 ast_mutex_unlock(&oh323->lock);
2549 oh323 = oh323->next;
2553 ast_mutex_unlock(&iflock);
2555 oh323 = (struct oh323_pvt *)1; /* Force fast loop */
2556 pthread_testcancel();
2557 /* Wait for sched or io */
2558 res = ast_sched_wait(sched);
2559 if ((res < 0) || (res > 1000)) {
2562 /* Do not wait if some channel(s) is destroyed, probably, more available too */
2565 res = ast_io_wait(io, res);
2566 pthread_testcancel();
2567 ast_mutex_lock(&monlock);
2569 ast_sched_runq(sched);
2571 ast_mutex_unlock(&monlock);
2577 static int restart_monitor(void)
2579 pthread_attr_t attr;
2580 /* If we're supposed to be stopped -- stay stopped */
2581 if (ast_mutex_lock(&monlock)) {
2582 ast_log(LOG_WARNING, "Unable to lock monitor\n");
2585 if (monitor_thread == AST_PTHREADT_STOP) {
2586 ast_mutex_unlock(&monlock);
2589 if (monitor_thread == pthread_self()) {
2590 ast_mutex_unlock(&monlock);
2591 ast_log(LOG_WARNING, "Cannot kill myself\n");
2594 if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
2595 /* Wake up the thread */
2596 pthread_kill(monitor_thread, SIGURG);
2598 pthread_attr_init(&attr);
2599 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
2600 /* Start a new monitor */
2601 if (ast_pthread_create_background(&monitor_thread, &attr, do_monitor, NULL) < 0) {
2602 monitor_thread = AST_PTHREADT_NULL;
2603 ast_mutex_unlock(&monlock);
2604 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
2608 ast_mutex_unlock(&monlock);
2612 static int h323_do_trace(int fd, int argc, char *argv[])
2615 return RESULT_SHOWUSAGE;
2617 h323_debug(1, atoi(argv[2]));
2618 ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
2619 return RESULT_SUCCESS;
2622 static int h323_no_trace(int fd, int argc, char *argv[])
2625 return RESULT_SHOWUSAGE;
2628 ast_cli(fd, "H.323 trace disabled\n");
2629 return RESULT_SUCCESS;
2632 static int h323_do_debug(int fd, int argc, char *argv[])
2635 return RESULT_SHOWUSAGE;
2638 ast_cli(fd, "H.323 debug enabled\n");
2639 return RESULT_SUCCESS;
2642 static int h323_no_debug(int fd, int argc, char *argv[])
2645 return RESULT_SHOWUSAGE;
2648 ast_cli(fd, "H.323 debug disabled\n");
2649 return RESULT_SUCCESS;
2652 static int h323_gk_cycle(int fd, int argc, char *argv[])
2655 return RESULT_SHOWUSAGE;
2659 /* Possibly register with a GK */
2660 if (!gatekeeper_disable) {
2661 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
2662 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
2665 return RESULT_SUCCESS;
2668 static int h323_ep_hangup(int fd, int argc, char *argv[])
2671 return RESULT_SHOWUSAGE;
2673 if (h323_soft_hangup(argv[2])) {
2674 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
2676 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
2678 return RESULT_SUCCESS;
2681 static int h323_tokens_show(int fd, int argc, char *argv[])
2684 return RESULT_SHOWUSAGE;
2687 return RESULT_SUCCESS;
2690 static char trace_usage[] =
2691 "Usage: h323 trace <level num>\n"
2692 " Enables H.323 stack tracing for debugging purposes\n";
2694 static char no_trace_usage[] =
2695 "Usage: h323 no trace\n"
2696 " Disables H.323 stack tracing for debugging purposes\n";
2698 static char debug_usage[] =
2699 "Usage: h323 debug\n"
2700 " Enables H.323 debug output\n";
2702 static char no_debug_usage[] =
2703 "Usage: h323 no debug\n"
2704 " Disables H.323 debug output\n";
2706 static char show_cycle_usage[] =
2707 "Usage: h323 gk cycle\n"
2708 " Manually re-register with the Gatekeper (Currently Disabled)\n";
2710 static char show_hangup_usage[] =
2711 "Usage: h323 hangup <token>\n"
2712 " Manually try to hang up call identified by <token>\n";
2714 static char show_tokens_usage[] =
2715 "Usage: h323 show tokens\n"
2716 " Print out all active call tokens\n";
2718 static char h323_reload_usage[] =
2719 "Usage: h323 reload\n"
2720 " Reloads H.323 configuration from h323.conf\n";
2722 static struct ast_cli_entry cli_h323_trace_deprecated =
2723 { { "h.323", "trace", NULL },
2724 h323_do_trace, "Enable H.323 Stack Tracing",
2727 static struct ast_cli_entry cli_h323_no_trace_deprecated =
2728 { { "h.323", "no", "trace", NULL },
2729 h323_no_trace, "Disable H.323 Stack Tracing",
2733 static struct ast_cli_entry cli_h323_debug_deprecated =
2734 { { "h.323", "debug", NULL },
2735 h323_do_debug, "Enable H.323 debug",
2738 static struct ast_cli_entry cli_h323_no_debug_deprecated =
2739 { { "h.323", "no", "debug", NULL },
2740 h323_no_debug, "Disable H.323 debug",
2744 static struct ast_cli_entry cli_h323_gk_cycle_deprecated =
2745 { { "h.323", "gk", "cycle", NULL },
2746 h323_gk_cycle, "Manually re-register with the Gatekeper",
2749 static struct ast_cli_entry cli_h323_hangup_deprecated =
2750 { { "h.323", "hangup", NULL },
2751 h323_ep_hangup, "Manually try to hang up a call",
2752 show_hangup_usage };
2754 static struct ast_cli_entry cli_h323_show_tokens_deprecated =
2755 { { "h.323", "show", "tokens", NULL },
2756 h323_tokens_show, "Show all active call tokens",
2757 show_tokens_usage };
2759 static struct ast_cli_entry cli_h323[] = {
2760 { { "h323", "trace", NULL },
2761 h323_do_trace, "Enable H.323 Stack Tracing",
2762 trace_usage, NULL, &cli_h323_trace_deprecated },
2764 { { "h323", "trace", "off", NULL },
2765 h323_no_trace, "Disable H.323 Stack Tracing",
2766 no_trace_usage, NULL, &cli_h323_no_trace_deprecated },
2768 { { "h323", "debug", NULL },
2769 h323_do_debug, "Enable H.323 debug",
2770 debug_usage, NULL, &cli_h323_debug_deprecated },
2772 { { "h323", "debug", "off", NULL },
2773 h323_no_debug, "Disable H.323 debug",
2774 no_debug_usage, NULL, &cli_h323_no_debug_deprecated },
2776 { { "h323", "cycle", "gk", NULL },
2777 h323_gk_cycle, "Manually re-register with the Gatekeper",
2778 show_cycle_usage, NULL, &cli_h323_gk_cycle_deprecated },
2780 { { "h323", "hangup", NULL },
2781 h323_ep_hangup, "Manually try to hang up a call",
2782 show_hangup_usage, NULL, &cli_h323_hangup_deprecated },
2784 { { "h323", "show", "tokens", NULL },
2785 h323_tokens_show, "Show all active call tokens",
2786 show_tokens_usage, NULL, &cli_h323_show_tokens_deprecated },
2789 static int reload_config(int is_reload)
2792 struct ast_config *cfg, *ucfg;
2793 struct ast_variable *v;
2794 struct oh323_peer *peer = NULL;
2795 struct oh323_user *user = NULL;
2796 struct oh323_alias *alias = NULL;
2797 struct ast_hostent ahp; struct hostent *hp;
2800 int is_user, is_peer, is_alias;
2801 char _gatekeeper[100];
2802 int gk_discover, gk_disable, gk_changed;
2804 cfg = ast_config_load(config);
2806 /* We *must* have a config file otherwise stop immediately */
2808 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
2812 /* fire up the H.323 Endpoint */
2813 if (!h323_end_point_exist()) {
2814 h323_end_point_create();
2816 ast_copy_string(_gatekeeper, gatekeeper, sizeof(_gatekeeper));
2817 gk_discover = gatekeeper_discover;
2818 gk_disable = gatekeeper_disable;
2819 memset(&bindaddr, 0, sizeof(bindaddr));
2820 memset(&global_options, 0, sizeof(global_options));
2821 global_options.fastStart = 1;
2822 global_options.h245Tunneling = 1;
2823 global_options.dtmfcodec[0] = H323_DTMF_RFC2833_PT;
2824 global_options.dtmfcodec[1] = H323_DTMF_CISCO_PT;
2825 global_options.dtmfmode = 0;
2826 global_options.holdHandling = 0;
2827 global_options.capability = GLOBAL_CAPABILITY;
2828 global_options.bridge = 1; /* Do native bridging by default */
2829 strcpy(default_context, "default");
2830 h323_signalling_port = 1720;
2831 gatekeeper_disable = 1;
2832 gatekeeper_discover = 0;
2835 acceptAnonymous = 1;
2838 /* Copy the default jb config over global_jbconf */
2839 memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2841 /* Load configuration from users.conf */
2842 ucfg = ast_config_load("users.conf");
2844 struct ast_variable *gen;
2846 const char *has_h323;
2848 genhas_h323 = ast_true(ast_variable_retrieve(ucfg, "general", "hash323"));
2849 gen = ast_variable_browse(ucfg, "general");
2850 for (cat = ast_category_browse(ucfg, NULL); cat; cat = ast_category_browse(ucfg, cat)) {
2851 if (strcasecmp(cat, "general")) {
2852 has_h323 = ast_variable_retrieve(ucfg, cat, "hash323");
2853 if (ast_true(has_h323) || (!has_h323 && genhas_h323)) {
2854 user = build_user(cat, gen, ast_variable_browse(ucfg, cat), 0);
2856 ASTOBJ_CONTAINER_LINK(&userl, user);
2857 ASTOBJ_UNREF(user, oh323_destroy_user);
2862 ast_config_destroy(ucfg);
2865 for (v = ast_variable_browse(cfg, "general"); v; v = v->next) {
2866 /* handle jb conf */
2867 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value))
2869 /* Create the interface list */
2870 if (!strcasecmp(v->name, "port")) {
2871 h323_signalling_port = (int)strtol(v->value, NULL, 10);
2872 } else if (!strcasecmp(v->name, "bindaddr")) {
2873 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
2874 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
2876 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
2878 } else if (!strcasecmp(v->name, "tos")) {
2879 if (sscanf(v->value, "%d", &format)) {
2880 tos = format & 0xff;
2881 } else if (!strcasecmp(v->value, "lowdelay")) {
2882 tos = IPTOS_LOWDELAY;
2883 } else if (!strcasecmp(v->value, "throughput")) {
2884 tos = IPTOS_THROUGHPUT;
2885 } else if (!strcasecmp(v->value, "reliability")) {
2886 tos = IPTOS_RELIABILITY;
2887 } else if (!strcasecmp(v->value, "mincost")) {
2888 tos = IPTOS_MINCOST;
2889 } else if (!strcasecmp(v->value, "none")) {
2892 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
2894 } else if (!strcasecmp(v->name, "gatekeeper")) {
2895 if (!strcasecmp(v->value, "DISABLE")) {
2896 gatekeeper_disable = 1;
2897 } else if (!strcasecmp(v->value, "DISCOVER")) {
2898 gatekeeper_disable = 0;
2899 gatekeeper_discover = 1;
2901 gatekeeper_disable = 0;
2902 ast_copy_string(gatekeeper, v->value, sizeof(gatekeeper));
2904 } else if (!strcasecmp(v->name, "secret")) {
2905 ast_copy_string(secret, v->value, sizeof(secret));
2906 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
2907 gkroute = ast_true(v->value);
2908 } else if (!strcasecmp(v->name, "context")) {
2909 ast_copy_string(default_context, v->value, sizeof(default_context));
2910 ast_verbose(VERBOSE_PREFIX_2 "Setting default context to %s\n", default_context);
2911 } else if (!strcasecmp(v->name, "UserByAlias")) {
2912 userbyalias = ast_true(v->value);
2913 } else if (!strcasecmp(v->name, "AcceptAnonymous")) {
2914 acceptAnonymous = ast_true(v->value);
2915 } else if (!update_common_options(v, &global_options)) {
2919 if (!global_options.dtmfmode)
2920 global_options.dtmfmode = H323_DTMF_RFC2833;
2921 if (global_options.holdHandling == ~0)
2922 global_options.holdHandling = 0;
2923 else if (!global_options.holdHandling)
2924 global_options.holdHandling = H323_HOLD_H450;
2926 for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
2927 if (strcasecmp(cat, "general")) {
2928 utype = ast_variable_retrieve(cfg, cat, "type");
2930 is_user = is_peer = is_alias = 0;
2931 if (!strcasecmp(utype, "user"))
2933 else if (!strcasecmp(utype, "peer"))
2935 else if (!strcasecmp(utype, "friend"))
2936 is_user = is_peer = 1;
2937 else if (!strcasecmp(utype, "h323") || !strcasecmp(utype, "alias"))
2940 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
2944 user = build_user(cat, ast_variable_browse(cfg, cat), NULL, 0);
2946 ASTOBJ_CONTAINER_LINK(&userl, user);
2947 ASTOBJ_UNREF(user, oh323_destroy_user);
2951 peer = build_peer(cat, ast_variable_browse(cfg, cat), NULL, 0);
2953 ASTOBJ_CONTAINER_LINK(&peerl, peer);
2954 ASTOBJ_UNREF(peer, oh323_destroy_peer);
2958 alias = build_alias(cat, ast_variable_browse(cfg, cat), NULL, 0);
2960 ASTOBJ_CONTAINER_LINK(&aliasl, alias);
2961 ASTOBJ_UNREF(alias, oh323_destroy_alias);
2965 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
2969 ast_config_destroy(cfg);
2971 /* Register our H.323 aliases if any*/
2972 ASTOBJ_CONTAINER_WRLOCK(&aliasl);
2973 ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
2974 ASTOBJ_RDLOCK(iterator);
2975 if (h323_set_alias(iterator)) {
2976 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
2977 ASTOBJ_UNLOCK(iterator);
2980 ASTOBJ_UNLOCK(iterator);
2982 ASTOBJ_CONTAINER_UNLOCK(&aliasl);
2984 /* Don't touch GK if nothing changed because URQ will drop all existing calls */
2986 if (gatekeeper_disable != gk_disable)
2987 gk_changed = is_reload;
2988 else if(!gatekeeper_disable && (gatekeeper_discover != gk_discover))
2989 gk_changed = is_reload;
2990 else if(!gatekeeper_disable && (strncmp(_gatekeeper, gatekeeper, sizeof(_gatekeeper)) != 0))
2991 gk_changed = is_reload;
2995 if (!gatekeeper_disable) {
2996 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
2997 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
2998 gatekeeper_disable = 1;
3005 static void delete_users(void)
3009 /* Delete all users */
3010 ASTOBJ_CONTAINER_WRLOCK(&userl);
3011 ASTOBJ_CONTAINER_TRAVERSE(&userl, 1, do {
3012 ASTOBJ_RDLOCK(iterator);
3013 ASTOBJ_MARK(iterator);
3015 ASTOBJ_UNLOCK(iterator);
3018 ASTOBJ_CONTAINER_PRUNE_MARKED(&userl, oh323_destroy_user);
3020 ASTOBJ_CONTAINER_UNLOCK(&userl);
3022 ASTOBJ_CONTAINER_WRLOCK(&peerl);
3023 ASTOBJ_CONTAINER_TRAVERSE(&peerl, 1, do {
3024 ASTOBJ_RDLOCK(iterator);
3025 ASTOBJ_MARK(iterator);
3026 ASTOBJ_UNLOCK(iterator);
3028 ASTOBJ_CONTAINER_UNLOCK(&peerl);
3031 static void delete_aliases(void)
3035 /* Delete all aliases */
3036 ASTOBJ_CONTAINER_WRLOCK(&aliasl);
3037 ASTOBJ_CONTAINER_TRAVERSE(&aliasl, 1, do {
3038 ASTOBJ_RDLOCK(iterator);
3039 ASTOBJ_MARK(iterator);
3041 ASTOBJ_UNLOCK(iterator);
3044 ASTOBJ_CONTAINER_PRUNE_MARKED(&aliasl, oh323_destroy_alias);
3046 ASTOBJ_CONTAINER_UNLOCK(&aliasl);
3049 static void prune_peers(void)
3051 /* Prune peers who still are supposed to be deleted */
3052 ASTOBJ_CONTAINER_PRUNE_MARKED(&peerl, oh323_destroy_peer);
3055 static int h323_reload(int fd, int argc, char *argv[])
3057 ast_mutex_lock(&h323_reload_lock);
3058 if (h323_reloading) {
3059 ast_verbose("Previous H.323 reload not yet done\n");
3063 ast_mutex_unlock(&h323_reload_lock);
3068 static int h323_do_reload(void)
3077 static int reload(void)
3079 return h323_reload(0, 0, NULL);
3082 static struct ast_cli_entry cli_h323_reload_deprecated =
3083 { { "h.323", "reload", NULL },
3084 h323_reload, "Reload H.323 configuration",
3088 static struct ast_cli_entry cli_h323_reload =
3089 { { "h323", "reload", NULL },
3090 h323_reload, "Reload H.323 configuration",
3091 h323_reload_usage, NULL, &cli_h323_reload_deprecated
3094 static enum ast_rtp_get_result oh323_get_rtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
3096 struct oh323_pvt *pvt;
3097 enum ast_rtp_get_result res = AST_RTP_GET_FAILED;
3099 if (!(pvt = (struct oh323_pvt *)chan->tech_pvt))
3102 ast_mutex_lock(&pvt->lock);
3103 if (pvt->rtp && pvt->options.bridge) {
3105 res = AST_RTP_TRY_NATIVE;
3107 ast_mutex_unlock(&pvt->lock);
3112 static enum ast_rtp_get_result oh323_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp **rtp)
3114 return AST_RTP_GET_FAILED;
3117 static char *convertcap(int cap)
3120 case AST_FORMAT_G723_1:
3122 case AST_FORMAT_GSM:
3124 case AST_FORMAT_ULAW:
3126 case AST_FORMAT_ALAW:
3128 case AST_FORMAT_G722:
3130 case AST_FORMAT_ADPCM:
3132 case AST_FORMAT_G729A:
3134 case AST_FORMAT_SPEEX:
3136 case AST_FORMAT_ILBC:
3139 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
3144 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs, int nat_active)
3146 /* XXX Deal with Video */
3147 struct oh323_pvt *pvt;
3148 struct sockaddr_in them;
3149 struct sockaddr_in us;
3156 mode = convertcap(chan->writeformat);
3157 pvt = (struct oh323_pvt *) chan->tech_pvt;
3159 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
3162 ast_rtp_get_peer(rtp, &them);
3163 ast_rtp_get_us(rtp, &us);
3164 #if 0 /* Native bridge still isn't ready */
3165 h323_native_bridge(pvt->cd.call_token, ast_inet_ntoa(them.sin_addr), mode);
3170 static struct ast_rtp_protocol oh323_rtp = {
3172 .get_rtp_info = oh323_get_rtp_peer,
3173 .get_vrtp_info = oh323_get_vrtp_peer,
3174 .set_rtp_peer = oh323_set_rtp_peer,
3177 static enum ast_module_load_result load_module(void)
3182 sched = sched_context_create();
3184 ast_log(LOG_WARNING, "Unable to create schedule context\n");
3185 return AST_MODULE_LOAD_FAILURE;
3187 io = io_context_create();
3189 ast_log(LOG_WARNING, "Unable to create I/O context\n");
3190 return AST_MODULE_LOAD_FAILURE;
3192 ast_cli_register(&cli_h323_reload);
3193 ASTOBJ_CONTAINER_INIT(&userl);
3194 ASTOBJ_CONTAINER_INIT(&peerl);
3195 ASTOBJ_CONTAINER_INIT(&aliasl);
3196 res = reload_config(0);
3198 ast_cli_unregister(&cli_h323_reload);
3199 io_context_destroy(io);
3200 sched_context_destroy(sched);
3201 ASTOBJ_CONTAINER_DESTROY(&userl);
3202 ASTOBJ_CONTAINER_DESTROY(&peerl);
3203 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3204 return /*AST_MODULE_LOAD_DECLINE*/AST_MODULE_LOAD_FAILURE;
3206 /* Make sure we can register our channel type */
3207 if (ast_channel_register(&oh323_tech)) {
3208 ast_log(LOG_ERROR, "Unable to register channel class 'H323'\n");
3209 ast_cli_unregister(&cli_h323_reload);
3211 io_context_destroy(io);
3212 sched_context_destroy(sched);
3214 ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
3215 ASTOBJ_CONTAINER_DESTROY(&userl);
3216 ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
3217 ASTOBJ_CONTAINER_DESTROY(&peerl);
3218 ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
3219 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3221 return AST_MODULE_LOAD_FAILURE;
3223 ast_cli_register_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
3225 ast_rtp_proto_register(&oh323_rtp);
3227 /* Register our callback functions */
3228 h323_callback_register(setup_incoming_call,
3229 setup_outgoing_call,
3230 external_rtp_create,
3231 setup_rtp_connection,
3240 set_local_capabilities,
3241 set_peer_capabilities,
3243 /* start the h.323 listener */
3244 if (h323_start_listener(h323_signalling_port, bindaddr)) {
3245 ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
3246 ast_rtp_proto_unregister(&oh323_rtp);
3247 ast_cli_unregister_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
3248 ast_cli_unregister(&cli_h323_reload);
3250 io_context_destroy(io);
3251 sched_context_destroy(sched);
3253 ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
3254 ASTOBJ_CONTAINER_DESTROY(&userl);
3255 ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
3256 ASTOBJ_CONTAINER_DESTROY(&peerl);
3257 ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
3258 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3260 return AST_MODULE_LOAD_FAILURE;
3262 /* Possibly register with a GK */
3263 if (!gatekeeper_disable) {
3264 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
3265 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
3266 gatekeeper_disable = 1;
3267 res = AST_MODULE_LOAD_SUCCESS;
3270 /* And start the monitor for the first time */
3276 static int unload_module(void)
3278 struct oh323_pvt *p, *pl;
3280 /* unregister commands */
3281 ast_cli_unregister_multiple(cli_h323, sizeof(cli_h323) / sizeof(struct ast_cli_entry));
3282 ast_cli_unregister(&cli_h323_reload);
3284 ast_channel_unregister(&oh323_tech);
3285 ast_rtp_proto_unregister(&oh323_rtp);
3287 if (!ast_mutex_lock(&iflock)) {
3288 /* hangup all interfaces if they have an owner */
3292 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
3297 ast_mutex_unlock(&iflock);
3299 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
3302 if (!ast_mutex_lock(&monlock)) {
3303 if ((monitor_thread != AST_PTHREADT_STOP) && (monitor_thread != AST_PTHREADT_NULL)) {
3304 /* this causes a seg, anyone know why? */
3305 if (monitor_thread != pthread_self())
3306 pthread_cancel(monitor_thread);
3307 pthread_kill(monitor_thread, SIGURG);
3308 pthread_join(monitor_thread, NULL);
3310 monitor_thread = AST_PTHREADT_STOP;
3311 ast_mutex_unlock(&monlock);
3313 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
3316 if (!ast_mutex_lock(&iflock)) {
3317 /* destroy all the interfaces and free their memory */
3322 /* free associated memory */
3323 ast_mutex_destroy(&pl->lock);
3327 ast_mutex_unlock(&iflock);
3329 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
3332 if (!gatekeeper_disable)
3335 io_context_destroy(io);
3336 sched_context_destroy(sched);
3338 ASTOBJ_CONTAINER_DESTROYALL(&userl, oh323_destroy_user);
3339 ASTOBJ_CONTAINER_DESTROY(&userl);
3340 ASTOBJ_CONTAINER_DESTROYALL(&peerl, oh323_destroy_peer);
3341 ASTOBJ_CONTAINER_DESTROY(&peerl);
3342 ASTOBJ_CONTAINER_DESTROYALL(&aliasl, oh323_destroy_alias);
3343 ASTOBJ_CONTAINER_DESTROY(&aliasl);
3348 AST_MODULE_INFO(ASTERISK_GPL_KEY, AST_MODFLAG_DEFAULT, "The NuFone Network's OpenH323 Channel Driver",
3349 .load = load_module,
3350 .unload = unload_module,