4 * OpenH323 Channel Driver for ASTERISK PBX.
6 * For The NuFone Network
8 * chan_h323 has been derived from code created by
9 * Michael Manousos and Mark Spencer
11 * This file is part of the chan_h323 driver for Asterisk
13 * chan_h323 is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * chan_h323 is distributed WITHOUT ANY WARRANTY; without even
19 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 #include <sys/socket.h>
30 #include <sys/signal.h>
31 #include <sys/param.h>
34 #define IPTOS_MINCOST 0x02
37 #include <arpa/inet.h>
39 #include <netinet/in.h>
40 #include <netinet/in_systm.h>
41 #include <netinet/ip.h>
52 #include <asterisk/lock.h>
53 #include <asterisk/logger.h>
54 #include <asterisk/channel.h>
55 #include <asterisk/channel_pvt.h>
56 #include <asterisk/config.h>
57 #include <asterisk/module.h>
58 #include <asterisk/pbx.h>
59 #include <asterisk/options.h>
60 #include <asterisk/utils.h>
61 #include <asterisk/lock.h>
62 #include <asterisk/sched.h>
63 #include <asterisk/io.h>
64 #include <asterisk/rtp.h>
65 #include <asterisk/acl.h>
66 #include <asterisk/callerid.h>
67 #include <asterisk/cli.h>
68 #include <asterisk/dsp.h>
72 #include "h323/chan_h323.h"
74 send_digit_cb on_send_digit;
75 on_rtp_cb on_external_rtp_create;
76 start_rtp_cb on_start_rtp_channel;
77 setup_incoming_cb on_incoming_call;
78 setup_outbound_cb on_outgoing_call;
79 chan_ringing_cb on_chan_ringing;
80 con_established_cb on_connection_established;
81 clear_con_cb on_connection_cleared;
82 answer_call_cb on_answer_call;
84 /* global debug flag */
87 /** Variables required by Asterisk */
88 static char *type = "H323";
89 static char *desc = "The NuFone Network's Open H.323 Channel Driver";
90 static char *tdesc = "The NuFone Network's Open H.323 Channel Driver";
91 static char *config = "h323.conf";
92 static char default_context[AST_MAX_EXTENSION] = "default";
94 /** H.323 configuration values */
95 static int h323_signalling_port = 1720;
96 static char gatekeeper[100];
97 static int gatekeeper_disable = 1;
98 static int gatekeeper_discover = 0;
99 static int usingGk = 0;
100 static int gkroute = 0;
101 static int noFastStart = 0;
102 static int noH245Tunneling = 0;
103 static int noSilenceSuppression = 0;
104 /* Assume we can native bridge by default */
105 static int bridging = 1;
106 /* Find user by alias (h.323 id) is default, alternative is the incomming call's source IP address*/
107 static int userbyalias = 1;
108 /* Just about everybody seems to support ulaw, so make it a nice default */
109 static int capability = AST_FORMAT_ULAW;
112 static int dtmfmode = H323_DTMF_RFC2833;
113 static char secret[50];
115 /** Private structure of a OpenH323 channel */
117 ast_mutex_t lock; /* Channel private lock */
118 call_options_t options; /* Options to be used during call setup */
119 int alreadygone; /* Whether or not we've already been destroyed by our peer */
120 int needdestroy; /* if we need to be destroyed */
121 call_details_t cd; /* Call details */
122 struct ast_channel *owner; /* Who owns us */
123 struct sockaddr_in sa; /* Our peer */
124 struct sockaddr_in redirip; /* Where our RTP should be going if not to us */
125 int capability; /* audio capability */
126 int nonCodecCapability; /* non-audio capability */
127 int outgoing; /* Outgoing or incoming call? */
128 int nat; /* Are we talking to a NAT EP?*/
129 int bridge; /* Determine of we should native bridge or not*/
130 char exten[AST_MAX_EXTENSION]; /* Requested extension */
131 char context[AST_MAX_EXTENSION]; /* Context where to start */
132 char username[81]; /* H.323 alias using this channel */
133 char accountcode[256]; /* Account code */
134 int amaflags; /* AMA Flags */
135 char callerid[80]; /* Caller*ID if available */
136 struct ast_rtp *rtp; /* RTP Session */
137 int dtmfmode; /* What DTMF Mode is being used */
138 struct ast_dsp *vad; /* Used for in-band DTMF detection */
139 struct oh323_pvt *next; /* Next channel in list */
142 static struct ast_user_list {
143 struct oh323_user *users;
147 static struct ast_peer_list {
148 struct oh323_peer *peers;
152 static struct ast_alias_list {
153 struct oh323_alias *aliases;
157 /** Asterisk RTP stuff */
158 static struct sched_context *sched;
159 static struct io_context *io;
161 /** Protect the interface list (oh323_pvt) */
162 AST_MUTEX_DEFINE_STATIC(iflock);
164 /** Usage counter and associated lock */
165 static int usecnt =0;
166 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
168 /* Protect the monitoring thread, so only one process can kill or start it, and not
169 when it's doing something critical. */
170 AST_MUTEX_DEFINE_STATIC(monlock);
172 /* Protect the H.323 capabilities list, to avoid more than one channel to set the capabilities simultaneaously in the h323 stack. */
173 AST_MUTEX_DEFINE_STATIC(caplock);
175 /* This is the thread for the monitor which checks for input on the channels
176 which are not currently in use. */
177 static pthread_t monitor_thread = AST_PTHREADT_NULL;
179 static int restart_monitor(void);
181 static void __oh323_destroy(struct oh323_pvt *p)
183 struct oh323_pvt *cur, *prev = NULL;
186 ast_rtp_destroy(p->rtp);
189 /* Unlink us from the owner if we have one */
191 ast_mutex_lock(&p->owner->lock);
192 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
193 p->owner->pvt->pvt = NULL;
194 ast_mutex_unlock(&p->owner->lock);
200 prev->next = cur->next;
209 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
211 ast_mutex_destroy(&p->lock);
216 static void oh323_destroy(struct oh323_pvt *p)
218 ast_mutex_lock(&iflock);
220 ast_mutex_unlock(&iflock);
223 static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
225 struct oh323_alias *alias;
227 alias = (struct oh323_alias *)malloc(sizeof(struct oh323_alias));
230 memset(alias, 0, sizeof(struct oh323_alias));
231 strncpy(alias->name, name, sizeof(alias->name) - 1);
233 if (!strcasecmp(v->name, "e164")) {
234 strncpy(alias->e164, v->value, sizeof(alias->e164) - 1);
235 } else if (!strcasecmp(v->name, "prefix")) {
236 strncpy(alias->prefix, v->value, sizeof(alias->prefix) - 1);
237 } else if (!strcasecmp(v->name, "context")) {
238 strncpy(alias->context, v->value, sizeof(alias->context) - 1);
239 } else if (!strcasecmp(v->name, "secret")) {
240 strncpy(alias->secret, v->value, sizeof(alias->secret) - 1);
242 if (strcasecmp(v->value, "h323")) {
243 ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->value);
252 static struct oh323_user *build_user(char *name, struct ast_variable *v)
254 struct oh323_user *user;
257 user = (struct oh323_user *)malloc(sizeof(struct oh323_user));
259 memset(user, 0, sizeof(struct oh323_user));
260 strncpy(user->name, name, sizeof(user->name) - 1);
262 /* set the usage flag to a sane starting value*/
264 /* set the native brigding default */
265 user->bridge = bridging;
266 strncpy(user->context, default_context, sizeof(user->context) - 1);
269 if (!strcasecmp(v->name, "context")) {
270 strncpy(user->context, v->value, sizeof(user->context) - 1);
271 } else if (!strcasecmp(v->name, "bridge")) {
272 user->bridge = ast_true(v->value);
273 } else if (!strcasecmp(v->name, "nat")) {
274 user->nat = ast_true(v->value);
275 } else if (!strcasecmp(v->name, "noFastStart")) {
276 user->noFastStart = ast_true(v->value);
277 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
278 user->noH245Tunneling = ast_true(v->value);
279 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
280 user->noSilenceSuppression = ast_true(v->value);
281 } else if (!strcasecmp(v->name, "secret")) {
282 strncpy(user->secret, v->value, sizeof(user->secret) - 1);
283 } else if (!strcasecmp(v->name, "callerid")) {
284 strncpy(user->callerid, v->value, sizeof(user->callerid) - 1);
285 } else if (!strcasecmp(v->name, "accountcode")) {
286 strncpy(user->accountcode, v->value, sizeof(user->accountcode) - 1);
287 } else if (!strcasecmp(v->name, "incominglimit")) {
288 user->incominglimit = atoi(v->value);
289 if (user->incominglimit < 0)
290 user->incominglimit = 0;
291 } else if (!strcasecmp(v->name, "host")) {
292 if (!strcasecmp(v->value, "dynamic")) {
293 ast_log(LOG_ERROR, "A dynamic host on a type=user does not make any sense\n");
296 } else if (ast_get_ip(&user->addr, v->value)) {
300 /* Let us know we need to use ip authentication */
302 } else if (!strcasecmp(v->name, "amaflags")) {
303 format = ast_cdr_amaflags2int(v->value);
305 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
307 user->amaflags = format;
316 static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
318 struct oh323_peer *peer;
319 struct oh323_peer *prev;
320 struct ast_ha *oldha = NULL;
325 ast_mutex_lock(&peerl.lock);
329 if (!strcasecmp(peer->name, name)) {
338 /* Already in the list, remove it and it will be added back (or FREE'd) */
340 prev->next = peer->next;
342 peerl.peers = peer->next;
344 ast_mutex_unlock(&peerl.lock);
346 ast_mutex_unlock(&peerl.lock);
347 peer = (struct oh323_peer*)malloc(sizeof(struct oh323_peer));
348 memset(peer, 0, sizeof(struct oh323_peer));
352 strncpy(peer->name, name, sizeof(peer->name) - 1);
353 peer->addr.sin_port = htons(h323_signalling_port);
354 peer->addr.sin_family = AF_INET;
358 peer->addr.sin_family = AF_INET;
359 peer->capability = capability;
362 if (!strcasecmp(v->name, "bridge")) {
363 peer->bridge = ast_true(v->value);
364 } else if (!strcasecmp(v->name, "nat")) {
365 peer->nat = ast_true(v->value);
366 } else if (!strcasecmp(v->name, "noFastStart")) {
367 peer->noFastStart = ast_true(v->value);
368 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
369 peer->noH245Tunneling = ast_true(v->value);
370 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
371 peer->noSilenceSuppression = ast_true(v->value);
372 } else if (!strcasecmp(v->name, "dtmfmode")) {
373 if (!strcasecmp(v->value, "inband")) {
374 peer->dtmfmode = H323_DTMF_INBAND;
375 } else if (!strcasecmp(v->value, "rfc2833")) {
376 peer->dtmfmode = H323_DTMF_RFC2833;
378 ast_log(LOG_WARNING, "Unknown DTMF Mode %s, using RFC2833\n", v->value);
379 peer->dtmfmode = H323_DTMF_RFC2833;
381 } else if (!strcasecmp(v->name, "allow")) {
382 format = ast_getformatbyname(v->value);
384 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
386 peer->capability |= format;
388 } else if (!strcasecmp(v->name, "disallow")) {
389 format = ast_getformatbyname(v->value);
391 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
393 peer->capability |= ~format;
395 } else if (!strcasecmp(v->name, "outgoinglimit")) {
396 peer->outgoinglimit = atoi(v->value);
397 if (peer->outgoinglimit > 0) {
398 peer->outgoinglimit = 0;
400 } else if (!strcasecmp(v->name, "host")) {
401 if (!strcasecmp(v->value, "dynamic")) {
402 ast_log(LOG_ERROR, "Dynamic host configuration not implemented.\n");
406 if (ast_get_ip(&peer->addr, v->value)) {
407 ast_log(LOG_ERROR, "Could not determine IP for %s\n", v->value);
411 } else if (!strcasecmp(v->name, "port")) {
412 peer->addr.sin_port = htons(atoi(v->value));
421 * Send (play) the specified digit to the channel.
424 static int oh323_digit(struct ast_channel *c, char digit)
426 struct oh323_pvt *p = (struct oh323_pvt *) c->pvt->pvt;
427 if (p && p->rtp && (p->dtmfmode & H323_DTMF_RFC2833)) {
428 ast_rtp_senddigit(p->rtp, digit);
430 /* If in-band DTMF is desired, send that */
431 if (p->dtmfmode & H323_DTMF_INBAND)
432 h323_send_tone(p->cd.call_token, digit);
437 * Make a call over the specified channel to the specified
439 * Returns -1 on error, 0 on success.
441 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
444 struct oh323_pvt *pvt = (struct oh323_pvt *)c->pvt->pvt;
445 char called_addr[INET_ADDRSTRLEN];
447 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
448 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
451 /* Clear and then set the address to call */
452 memset(called_addr, 0, sizeof(called_addr));
454 memcpy(called_addr, dest, strlen(called_addr));
455 pvt->options.noFastStart = noFastStart;
456 pvt->options.noH245Tunneling = noH245Tunneling;
457 pvt->options.noSilenceSuppression = noSilenceSuppression;
458 pvt->options.port = h323_signalling_port;
460 ast_inet_ntoa(called_addr, sizeof(called_addr), pvt->sa.sin_addr);
461 pvt->options.port = htons(pvt->sa.sin_port);
463 /* indicate that this is an outgoing call */
465 ast_log(LOG_DEBUG, "Outgoing call to %s:%d\n", called_addr, pvt->options.port);
466 res = h323_make_call(called_addr, &(pvt->cd), pvt->options);
468 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
474 static int oh323_answer(struct ast_channel *c)
477 struct oh323_pvt *pvt = (struct oh323_pvt *) c->pvt->pvt;
479 res = h323_answering_call(pvt->cd.call_token, 0);
481 if (c->_state != AST_STATE_UP)
482 ast_setstate(c, AST_STATE_UP);
487 static int oh323_hangup(struct ast_channel *c)
489 struct oh323_pvt *pvt = (struct oh323_pvt *) c->pvt->pvt;
492 ast_log(LOG_DEBUG, "oh323_hangup(%s)\n", c->name);
494 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
497 ast_mutex_lock(&pvt->lock);
498 /* Determine how to disconnect */
499 if (pvt->owner != c) {
500 ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
501 ast_mutex_unlock(&pvt->lock);
504 if (!c || (c->_state != AST_STATE_UP))
507 pvt = (struct oh323_pvt *) c->pvt->pvt;
509 /* Free dsp used for in-band DTMF detection */
511 ast_dsp_free(pvt->vad);
517 /* Start the process if it's not already started */
518 if (!pvt->alreadygone) {
519 if (h323_clear_call((pvt->cd).call_token)) {
520 ast_log(LOG_DEBUG, "ClearCall failed.\n");
522 pvt->needdestroy = 1;
525 /* Update usage counter */
526 ast_mutex_lock(&usecnt_lock);
529 ast_log(LOG_WARNING, "Usecnt < 0\n");
530 ast_mutex_unlock(&usecnt_lock);
531 ast_update_use_count();
533 ast_mutex_unlock(&pvt->lock);
537 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *pvt)
539 /* Retrieve audio/etc from channel. Assumes pvt->lock is already held. */
541 static struct ast_frame null_frame = { AST_FRAME_NULL, };
543 /* Only apply it for the first packet, we just need the correct ip/port */
546 ast_rtp_setnat(pvt->rtp,pvt->nat);
550 f = ast_rtp_read(pvt->rtp);
551 /* Don't send RFC2833 if we're not supposed to */
552 if (f && (f->frametype == AST_FRAME_DTMF) && !(pvt->dtmfmode & H323_DTMF_RFC2833))
555 /* We already hold the channel lock */
556 if (f->frametype == AST_FRAME_VOICE) {
557 if (f->subclass != pvt->owner->nativeformats) {
558 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
559 pvt->owner->nativeformats = f->subclass;
560 ast_set_read_format(pvt->owner, pvt->owner->readformat);
561 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
564 /* Do in-band DTMF detection */
565 if (pvt->dtmfmode & H323_DTMF_INBAND) {
566 f = ast_dsp_process(pvt->owner,pvt->vad,f);
567 if (f->frametype == AST_FRAME_DTMF) {
568 ast_log(LOG_DEBUG, "Got in-band digit %c.\n", f->subclass);
578 static struct ast_frame *oh323_read(struct ast_channel *c)
580 struct ast_frame *fr;
581 struct oh323_pvt *pvt = (struct oh323_pvt *) c->pvt->pvt;
582 ast_mutex_lock(&pvt->lock);
583 fr = oh323_rtp_read(pvt);
584 ast_mutex_unlock(&pvt->lock);
588 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
590 struct oh323_pvt *pvt = (struct oh323_pvt *) c->pvt->pvt;
592 if (frame->frametype != AST_FRAME_VOICE) {
593 if (frame->frametype == AST_FRAME_IMAGE)
596 ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
600 if (!(frame->subclass & c->nativeformats)) {
601 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
602 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
607 ast_mutex_lock(&pvt->lock);
609 res = ast_rtp_write(pvt->rtp, frame);
611 ast_mutex_unlock(&pvt->lock);
616 static int oh323_indicate(struct ast_channel *c, int condition)
619 struct oh323_pvt *pvt = (struct oh323_pvt *) c->pvt->pvt;
622 case AST_CONTROL_RINGING:
623 if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
624 h323_send_alerting(pvt->cd.call_token);
628 case AST_CONTROL_PROGRESS:
629 if (c->_state != AST_STATE_UP) {
630 h323_send_progress(pvt->cd.call_token);
635 case AST_CONTROL_BUSY:
636 if (c->_state != AST_STATE_UP) {
637 h323_answering_call(pvt->cd.call_token, 1);
638 pvt->alreadygone = 1;
639 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
643 case AST_CONTROL_CONGESTION:
644 if (c->_state != AST_STATE_UP) {
645 h323_answering_call(pvt->cd.call_token, 1);
646 pvt->alreadygone = 1;
647 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
651 case AST_CONTROL_PROCEEDING:
655 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
661 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
663 struct oh323_pvt *pvt = (struct oh323_pvt *) newchan->pvt->pvt;
665 ast_mutex_lock(&pvt->lock);
666 if (pvt->owner != oldchan) {
667 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, pvt->owner);
670 pvt->owner = newchan;
671 ast_mutex_unlock(&pvt->lock);
675 static struct ast_channel *oh323_new(struct oh323_pvt *pvt, int state, const char *host)
677 struct ast_channel *ch;
679 ch = ast_channel_alloc(1);
683 snprintf(ch->name, sizeof(ch->name), "H323/%s", host);
684 ch->nativeformats = pvt->capability;
685 if (!ch->nativeformats)
686 ch->nativeformats = capability;
687 fmt = ast_best_codec(ch->nativeformats);
689 ch->fds[0] = ast_rtp_fd(pvt->rtp);
691 if (state == AST_STATE_RING)
694 ch->writeformat = fmt;
695 ch->pvt->rawwriteformat = fmt;
696 ch->readformat = fmt;
697 ch->pvt->rawreadformat = fmt;
699 /* Allocate dsp for in-band DTMF support */
700 if (pvt->dtmfmode & H323_DTMF_INBAND) {
701 pvt->vad = ast_dsp_new();
702 ast_dsp_set_features(pvt->vad, DSP_FEATURE_DTMF_DETECT);
705 /* Register the OpenH323 channel's functions. */
707 ch->pvt->send_digit = oh323_digit;
708 ch->pvt->call = oh323_call;
709 ch->pvt->hangup = oh323_hangup;
710 ch->pvt->answer = oh323_answer;
711 ch->pvt->read = oh323_read;
712 ch->pvt->write = oh323_write;
713 ch->pvt->indicate = oh323_indicate;
714 ch->pvt->fixup = oh323_fixup;
715 /* ch->pvt->bridge = ast_rtp_bridge; */
717 /* Set the owner of this channel */
720 ast_mutex_lock(&usecnt_lock);
722 ast_mutex_unlock(&usecnt_lock);
723 ast_update_use_count();
724 strncpy(ch->context, pvt->context, sizeof(ch->context) - 1);
725 strncpy(ch->exten, pvt->exten, sizeof(ch->exten) - 1);
727 if (!ast_strlen_zero(pvt->accountcode))
728 strncpy(ch->accountcode, pvt->accountcode, sizeof(ch->accountcode) - 1);
730 ch->amaflags = pvt->amaflags;
731 ast_setstate(ch, state);
732 if (state != AST_STATE_DOWN) {
733 if (ast_pbx_start(ch)) {
734 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
740 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
744 static struct oh323_pvt *oh323_alloc(int callid)
746 struct oh323_pvt *pvt;
748 pvt = (struct oh323_pvt *) malloc(sizeof(struct oh323_pvt));
750 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
753 /* Keep track of stuff */
754 memset(pvt, 0, sizeof(struct oh323_pvt));
755 pvt->rtp = ast_rtp_new(sched, io, 1, 0);
757 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
761 ast_rtp_settos(pvt->rtp, tos);
762 ast_mutex_init(&pvt->lock);
763 /* Ensure the call token is allocated */
764 if ((pvt->cd).call_token == NULL) {
765 (pvt->cd).call_token = (char *)malloc(128);
767 memset((char *)(pvt->cd).call_token, 0, 128);
768 if (!pvt->cd.call_token) {
769 ast_log(LOG_ERROR, "Not enough memory to alocate call token\n");
772 pvt->cd.call_reference = callid;
773 pvt->bridge = bridging;
774 pvt->dtmfmode = dtmfmode;
775 if (pvt->dtmfmode & H323_DTMF_RFC2833) {
776 pvt->nonCodecCapability |= AST_RTP_DTMF;
778 strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
779 /* Add to interface list */
780 ast_mutex_lock(&iflock);
783 ast_mutex_unlock(&iflock);
787 static struct oh323_pvt *find_call(int call_reference, const char *token)
789 struct oh323_pvt *pvt;
791 ast_mutex_lock(&iflock);
794 if ((signed int)pvt->cd.call_reference == call_reference) {
796 if ((token != NULL) && (strcmp(pvt->cd.call_token, token) == 0)) {
797 ast_mutex_unlock(&iflock);
799 } else if(token == NULL) {
800 ast_log(LOG_DEBUG, "token is NULL, skipping comparition\n");
801 ast_mutex_unlock(&iflock);
807 ast_mutex_unlock(&iflock);
811 struct oh323_user *find_user(const call_details_t cd)
813 struct oh323_user *u;
814 char iabuf[INET_ADDRSTRLEN];
816 if(userbyalias == 1){
818 if (!strcasecmp(u->name, cd.call_source_aliases)) {
826 if (!strcasecmp(cd.sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), u->addr.sin_addr))) {
838 struct oh323_peer *find_peer(char *peer, struct sockaddr_in *sin)
840 struct oh323_peer *p = NULL;
841 static char iabuf[INET_ADDRSTRLEN];
846 if (!strcasecmp(p->name, peer)) {
847 ast_log(LOG_DEBUG, "Found peer %s by name\n", peer);
855 if ((!inaddrcmp(&p->addr, sin)) ||
856 (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)) {
857 ast_log(LOG_DEBUG, "Found peer %s/%s by addr\n", peer, ast_inet_ntoa(iabuf, sizeof(iabuf), p->addr.sin_addr));
864 ast_log(LOG_DEBUG, "Could not find peer %s/%s by addr\n", peer, ast_inet_ntoa(iabuf, sizeof(iabuf), p->addr.sin_addr));
869 static int create_addr(struct oh323_pvt *pvt, char *opeer)
872 struct ast_hostent ahp;
873 struct oh323_peer *p;
880 strncpy(peer, opeer, sizeof(peer) - 1);
881 port = strchr(peer, ':');
886 pvt->sa.sin_family = AF_INET;
887 ast_mutex_lock(&peerl.lock);
888 p = find_peer(peer, NULL);
891 pvt->capability = p->capability;
894 ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", pvt->nat);
895 ast_rtp_setnat(pvt->rtp, pvt->nat);
897 pvt->options.noFastStart = p->noFastStart;
898 pvt->options.noH245Tunneling = p->noH245Tunneling;
899 pvt->options.noSilenceSuppression = p->noSilenceSuppression;
901 pvt->dtmfmode = p->dtmfmode;
902 if (pvt->dtmfmode & H323_DTMF_RFC2833) {
903 pvt->nonCodecCapability |= AST_RTP_DTMF;
905 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
908 if (p->addr.sin_addr.s_addr) {
909 pvt->sa.sin_addr = p->addr.sin_addr;
910 pvt->sa.sin_port = p->addr.sin_port;
913 ast_mutex_unlock(&peerl.lock);
919 portno = h323_signalling_port;
921 hp = ast_gethostbyname(hostn, &ahp);
923 memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
924 pvt->sa.sin_port = htons(portno);
927 ast_log(LOG_WARNING, "No such host: %s\n", peer);
936 static struct ast_channel *oh323_request(const char *type, int format, void *data)
939 struct oh323_pvt *pvt;
940 struct ast_channel *tmpc = NULL;
941 char *dest = (char *)data;
946 ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
948 pvt = oh323_alloc(0);
950 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
955 format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1);
957 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
960 /* Assign default capabilities */
961 pvt->capability = capability;
962 pvt->dtmfmode = H323_DTMF_RFC2833;
964 strncpy(tmp, dest, sizeof(tmp) - 1);
965 host = strchr(tmp, '@');
974 strtok_r(host, "/", &(h323id));
975 if (h323id && !ast_strlen_zero(h323id)) {
979 strncpy(pvt->username, ext, sizeof(pvt->username) - 1);
981 ast_log(LOG_DEBUG, "Host: %s\tUsername: %s\n", host, pvt->username);
984 if (create_addr(pvt, host)) {
989 /* pass on our capabilites to the H.323 stack */
990 ast_mutex_lock(&caplock);
991 h323_set_capability(pvt->capability, pvt->dtmfmode);
992 ast_mutex_unlock(&caplock);
994 ast_mutex_lock(&pvt->lock);
995 tmpc = oh323_new(pvt, AST_STATE_DOWN, host);
996 ast_mutex_unlock(&pvt->lock);
1000 ast_update_use_count();
1005 struct oh323_alias *find_alias(const char *source_aliases)
1007 struct oh323_alias *a;
1013 if (!strcasecmp(a->name, source_aliases)) {
1022 * Callback for sending digits from H.323 up to asterisk
1025 int send_digit(unsigned call_reference, char digit, const char *token)
1027 struct oh323_pvt *pvt;
1030 ast_log(LOG_DEBUG, "Recieved Digit: %c\n", digit);
1032 pvt = find_call(call_reference, token);
1035 ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
1038 memset(&f, 0, sizeof(f));
1039 f.frametype = AST_FRAME_DTMF;
1046 f.src = "SEND_DIGIT";
1047 return ast_queue_frame(pvt->owner, &f);
1051 * Callback function used to inform the H.323 stack of the local rtp ip/port details
1053 * Returns the local RTP information
1055 struct rtp_info *external_rtp_create(unsigned call_reference, const char * token)
1057 struct oh323_pvt *pvt;
1058 struct sockaddr_in us;
1059 struct rtp_info *info;
1060 static char iabuf[INET_ADDRSTRLEN];
1062 info = (struct rtp_info *)malloc(sizeof(struct rtp_info));
1064 ast_log(LOG_ERROR, "Unable to allocated info structure, this is very bad\n");
1067 pvt = find_call(call_reference, token);
1069 ast_log(LOG_ERROR, "Unable to find call %s(%d)\n", token, call_reference);
1072 /* figure out our local RTP port and tell the H.323 stack about it */
1073 ast_rtp_get_us(pvt->rtp, &us);
1074 /* evil hack, until I (someone?) figures out a better way */
1075 info->addr = ast_inet_ntoa(iabuf, sizeof(iabuf), bindaddr.sin_addr);
1076 info->port = ntohs(us.sin_port);
1077 ast_log(LOG_DEBUG, "Sending RTP 'US' %s:%d\n", iabuf, info->port);
1082 * Call-back function passing remote ip/port information from H.323 to asterisk
1086 void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort, const char *token)
1088 struct oh323_pvt *pvt = NULL;
1089 struct sockaddr_in them;
1091 /* Find the call or allocate a private structure if call not found */
1092 pvt = find_call(call_reference, token);
1094 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1097 them.sin_family = AF_INET;
1098 them.sin_addr.s_addr = inet_addr(remoteIp); // only works for IPv4
1099 them.sin_port = htons(remotePort);
1100 ast_rtp_set_peer(pvt->rtp, &them);
1105 * Call-back function to signal asterisk that the channel has been answered
1108 void connection_made(unsigned call_reference, const char *token)
1110 struct ast_channel *c = NULL;
1111 struct oh323_pvt *pvt = NULL;
1113 pvt = find_call(call_reference, token);
1116 ast_log(LOG_ERROR, "Something is wrong: connection\n");
1121 ast_log(LOG_ERROR, "Channel has no owner\n");
1126 ast_setstate(c, AST_STATE_UP);
1127 ast_queue_control(c, AST_CONTROL_ANSWER);
1132 * Call-back function for incoming calls
1134 * Returns 1 on success
1136 int setup_incoming_call(call_details_t cd)
1138 struct oh323_pvt *pvt = NULL;
1139 struct oh323_user *user = NULL;
1140 struct oh323_alias *alias = NULL;
1141 char iabuf[INET_ADDRSTRLEN];
1143 /* allocate the call*/
1144 pvt = oh323_alloc(cd.call_reference);
1147 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
1151 /* Populate the call details in the private structure */
1152 pvt->cd.call_token = strdup(cd.call_token);
1153 pvt->cd.call_source_aliases = strdup(cd.call_source_aliases);
1154 pvt->cd.call_dest_alias = strdup(cd.call_dest_alias);
1155 pvt->cd.call_source_name = strdup(cd.call_source_name);
1156 pvt->cd.call_source_e164 = strdup(cd.call_source_e164);
1157 pvt->cd.call_dest_e164 = strdup(cd.call_dest_e164);
1160 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
1161 ast_verbose(VERBOSE_PREFIX_3 " Call token: [%s]\n", pvt->cd.call_token);
1162 ast_verbose(VERBOSE_PREFIX_3 " Calling party name: [%s]\n", pvt->cd.call_source_name);
1163 ast_verbose(VERBOSE_PREFIX_3 " Calling party number: [%s]\n", pvt->cd.call_source_e164);
1164 ast_verbose(VERBOSE_PREFIX_3 " Called party name: [%s]\n", pvt->cd.call_dest_alias);
1165 ast_verbose(VERBOSE_PREFIX_3 " Called party number: [%s]\n", pvt->cd.call_dest_e164);
1168 /* Decide if we are allowing Gatekeeper routed calls*/
1169 if ((!strcasecmp(cd.sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk == 1)) {
1171 if (!ast_strlen_zero(cd.call_dest_e164)) {
1172 strncpy(pvt->exten, cd.call_dest_e164, sizeof(pvt->exten) - 1);
1173 strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
1175 alias = find_alias(cd.call_dest_alias);
1178 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd.call_dest_alias);
1181 strncpy(pvt->exten, alias->name, sizeof(pvt->exten) - 1);
1182 strncpy(pvt->context, alias->context, sizeof(pvt->context) - 1);
1184 snprintf(pvt->callerid, sizeof(pvt->callerid), "%s <%s>", pvt->cd.call_source_name, pvt->cd.call_source_e164);
1186 /* Either this call is not from the Gatekeeper
1187 or we are not allowing gk routed calls */
1188 user = find_user(cd);
1191 snprintf(pvt->callerid, sizeof(pvt->callerid), "%s <%s>", pvt->cd.call_source_name, pvt->cd.call_source_e164);
1192 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
1193 strncpy(pvt->exten, cd.call_dest_e164, sizeof(pvt->exten) - 1);
1195 strncpy(pvt->exten, cd.call_dest_alias, sizeof(pvt->exten) - 1);
1197 if (ast_strlen_zero(default_context)) {
1198 ast_log(LOG_ERROR, "Call from '%s' rejected due to no default context\n", pvt->cd.call_source_aliases);
1201 strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
1202 ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd.call_source_aliases, pvt->context);
1205 if (strcasecmp(cd.sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), user->addr.sin_addr))){
1206 if (ast_strlen_zero(user->context)) {
1207 if (ast_strlen_zero(default_context)) {
1208 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd.sourceIp);
1211 strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
1213 strncpy(pvt->context, user->context, sizeof(pvt->context) - 1);
1215 pvt->exten[0] = 'i';
1216 pvt->exten[1] = '\0';
1217 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd.sourceIp);
1221 if (user->incominglimit > 0) {
1222 if (user->inUse >= user->incominglimit) {
1223 ast_log(LOG_ERROR, "Call from user '%s' rejected due to usage limit of %d\n", user->name, user->incominglimit);
1227 strncpy(pvt->context, user->context, sizeof(pvt->context) - 1);
1228 pvt->bridge = user->bridge;
1229 pvt->nat = user->nat;
1231 if (!ast_strlen_zero(user->callerid)) {
1232 strncpy(pvt->callerid, user->callerid, sizeof(pvt->callerid) - 1);
1234 snprintf(pvt->callerid, sizeof(pvt->callerid), "%s <%s>", pvt->cd.call_source_name, pvt->cd.call_source_e164);
1236 if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
1237 strncpy(pvt->exten, cd.call_dest_e164, sizeof(pvt->exten) - 1);
1239 strncpy(pvt->exten, cd.call_dest_alias, sizeof(pvt->exten) - 1);
1241 if (!ast_strlen_zero(user->accountcode)) {
1242 strncpy(pvt->accountcode, user->accountcode, sizeof(pvt->accountcode) - 1);
1244 /* Increment the usage counter */
1254 * Call-back function to start PBX when OpenH323 ready to serve incoming call
1256 * Returns 1 on success
1258 static int answer_call(unsigned call_reference, const char *token)
1260 struct oh323_pvt *pvt = NULL;
1261 struct ast_channel *c = NULL;
1263 /* Find the call or allocate a private structure if call not found */
1264 pvt = find_call(call_reference, token);
1267 ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
1271 /* allocate a channel and tell asterisk about it */
1272 c = oh323_new(pvt, AST_STATE_RINGING, pvt->cd.call_token);
1274 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
1281 * Call-back function to establish an outgoing H.323 call
1283 * Returns 1 on success
1285 int setup_outgoing_call(call_details_t cd)
1291 * Call-back function to signal asterisk that the channel is ringing
1294 void chan_ringing(unsigned call_reference, const char *token)
1296 struct ast_channel *c = NULL;
1297 struct oh323_pvt *pvt = NULL;
1299 pvt = find_call(call_reference, token);
1302 ast_log(LOG_ERROR, "Something is wrong: ringing\n");
1306 ast_log(LOG_ERROR, "Channel has no owner\n");
1310 ast_setstate(c, AST_STATE_RINGING);
1311 ast_queue_control(c, AST_CONTROL_RINGING);
1316 void cleanup_call_details(call_details_t cd)
1318 if (cd.call_token) {
1319 free(cd.call_token);
1321 if (cd.call_source_aliases) {
1322 free(cd.call_source_aliases);
1324 if (cd.call_dest_alias) {
1325 free(cd.call_dest_alias);
1327 if (cd.call_source_name) {
1328 free(cd.call_source_name);
1330 if (cd.call_source_e164) {
1331 free(cd.call_source_e164);
1333 if (cd.call_dest_e164) {
1334 free(cd.call_dest_e164);
1342 * Call-back function to cleanup communication
1345 void cleanup_connection(call_details_t cd)
1347 struct oh323_pvt *pvt = NULL;
1348 struct ast_rtp *rtp = NULL;
1350 pvt = find_call(cd.call_reference, cd.call_token);
1354 ast_mutex_lock(&pvt->lock);
1358 /* Immediately stop RTP */
1359 ast_rtp_destroy(rtp);
1361 cleanup_call_details(pvt->cd);
1362 pvt->alreadygone = 1;
1365 ast_queue_hangup(pvt->owner);
1367 ast_mutex_unlock(&pvt->lock);
1371 static void *do_monitor(void *data)
1374 struct oh323_pvt *oh323 = NULL;
1377 /* Check for interfaces needing to be killed */
1378 ast_mutex_lock(&iflock);
1382 if (oh323->needdestroy) {
1383 __oh323_destroy(oh323);
1386 oh323 = oh323->next;
1388 ast_mutex_unlock(&iflock);
1390 /* Wait for sched or io */
1391 res = ast_sched_wait(sched);
1392 if ((res < 0) || (res > 1000))
1394 res = ast_io_wait(io, res);
1396 pthread_testcancel();
1398 ast_mutex_lock(&monlock);
1400 ast_sched_runq(sched);
1401 ast_mutex_unlock(&monlock);
1408 static int restart_monitor(void)
1410 /* If we're supposed to be stopped -- stay stopped */
1411 if (monitor_thread == AST_PTHREADT_STOP)
1413 if (ast_mutex_lock(&monlock)) {
1414 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1417 if (monitor_thread == pthread_self()) {
1418 ast_mutex_unlock(&monlock);
1419 ast_log(LOG_WARNING, "Cannot kill myself\n");
1422 if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
1423 /* Wake up the thread */
1424 pthread_kill(monitor_thread, SIGURG);
1426 /* Start a new monitor */
1427 if (ast_pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1428 ast_mutex_unlock(&monlock);
1429 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1433 ast_mutex_unlock(&monlock);
1437 static int h323_do_trace(int fd, int argc, char *argv[])
1440 return RESULT_SHOWUSAGE;
1442 h323_debug(1, atoi(argv[2]));
1443 ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
1444 return RESULT_SUCCESS;
1447 static int h323_no_trace(int fd, int argc, char *argv[])
1450 return RESULT_SHOWUSAGE;
1453 ast_cli(fd, "H.323 trace disabled\n");
1454 return RESULT_SUCCESS;
1457 static int h323_do_debug(int fd, int argc, char *argv[])
1460 return RESULT_SHOWUSAGE;
1463 ast_cli(fd, "H323 debug enabled\n");
1464 return RESULT_SUCCESS;
1467 static int h323_no_debug(int fd, int argc, char *argv[])
1470 return RESULT_SHOWUSAGE;
1473 ast_cli(fd, "H323 Debug disabled\n");
1474 return RESULT_SUCCESS;
1477 static int h323_gk_cycle(int fd, int argc, char *argv[])
1479 return RESULT_SUCCESS;
1482 return RESULT_SHOWUSAGE;
1486 /* Possibly register with a GK */
1487 if (!gatekeeper_disable) {
1488 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1489 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1492 return RESULT_SUCCESS;
1496 static int h323_ep_hangup(int fd, int argc, char *argv[])
1500 return RESULT_SHOWUSAGE;
1503 if (h323_soft_hangup(argv[2])) {
1504 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
1506 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
1509 return RESULT_SUCCESS;
1512 static int h323_tokens_show(int fd, int argc, char *argv[])
1516 return RESULT_SHOWUSAGE;
1520 return RESULT_SUCCESS;
1524 static char trace_usage[] =
1525 "Usage: h.323 trace <level num>\n"
1526 " Enables H.323 stack tracing for debugging purposes\n";
1528 static char no_trace_usage[] =
1529 "Usage: h.323 no trace\n"
1530 " Disables H.323 stack tracing for debugging purposes\n";
1532 static char debug_usage[] =
1533 "Usage: h.323 debug\n"
1534 " Enables chan_h323 debug output\n";
1536 static char no_debug_usage[] =
1537 "Usage: h.323 no debug\n"
1538 " Disables chan_h323 debug output\n";
1540 static char show_codec_usage[] =
1541 "Usage: h.323 show codec\n"
1542 " Shows all enabled codecs\n";
1544 static char show_cycle_usage[] =
1545 "Usage: h.323 gk cycle\n"
1546 " Manually re-register with the Gatekeper\n";
1548 static char show_hangup_usage[] =
1549 "Usage: h.323 hangup <token>\n"
1550 " Manually try to hang up call identified by <token>\n";
1552 static char show_tokens_usage[] =
1553 "Usage: h.323 show tokens\n"
1554 " Print out all active call tokens\n";
1556 static struct ast_cli_entry cli_trace =
1557 { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
1558 static struct ast_cli_entry cli_no_trace =
1559 { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
1560 static struct ast_cli_entry cli_debug =
1561 { { "h.323", "debug", NULL }, h323_do_debug, "Enable chan_h323 debug", debug_usage };
1562 static struct ast_cli_entry cli_no_debug =
1563 { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable chan_h323 debug", no_debug_usage };
1564 static struct ast_cli_entry cli_show_codecs =
1565 { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
1566 static struct ast_cli_entry cli_gk_cycle =
1567 { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
1568 static struct ast_cli_entry cli_hangup_call =
1569 { { "h.323", "hangup", NULL }, h323_ep_hangup, "Show all active call tokens", show_hangup_usage };
1570 static struct ast_cli_entry cli_show_tokens =
1571 { { "h.323", "show", "tokens", NULL }, h323_tokens_show, "Manually try to hang up a call", show_tokens_usage };
1575 int reload_config(void)
1579 struct ast_config *cfg;
1580 struct ast_variable *v;
1581 struct oh323_peer *peer = NULL;
1582 struct oh323_user *user = NULL;
1583 struct oh323_alias *alias = NULL;
1584 struct ast_hostent ahp; struct hostent *hp;
1588 cfg = ast_load(config);
1590 /* We *must* have a config file otherwise stop immediately */
1592 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
1596 /* fire up the H.323 Endpoint */
1597 if (!h323_end_point_exist()) {
1598 h323_end_point_create();
1601 dtmfmode = H323_DTMF_RFC2833;
1603 memset(&bindaddr, 0, sizeof(bindaddr));
1605 v = ast_variable_browse(cfg, "general");
1607 /* Create the interface list */
1608 if (!strcasecmp(v->name, "port")) {
1609 h323_signalling_port = (int)strtol(v->value, NULL, 10);
1610 } else if (!strcasecmp(v->name, "bindaddr")) {
1611 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
1612 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
1614 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
1616 } else if (!strcasecmp(v->name, "allow")) {
1617 format = ast_getformatbyname(v->value);
1619 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
1621 capability |= format;
1622 } else if (!strcasecmp(v->name, "disallow")) {
1623 format = ast_getformatbyname(v->value);
1625 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
1627 capability &= ~format;
1628 } else if (!strcasecmp(v->name, "tos")) {
1629 if (sscanf(v->value, "%i", &format) == 1)
1630 tos = format & 0xff;
1631 else if (!strcasecmp(v->value, "lowdelay"))
1632 tos = IPTOS_LOWDELAY;
1633 else if (!strcasecmp(v->value, "throughput"))
1634 tos = IPTOS_THROUGHPUT;
1635 else if (!strcasecmp(v->value, "reliability"))
1636 tos = IPTOS_RELIABILITY;
1637 else if (!strcasecmp(v->value, "mincost"))
1638 tos = IPTOS_MINCOST;
1639 else if (!strcasecmp(v->value, "none"))
1642 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
1643 } else if (!strcasecmp(v->name, "gatekeeper")) {
1644 if (!strcasecmp(v->value, "DISABLE")) {
1645 gatekeeper_disable = 1;
1647 } else if (!strcasecmp(v->value, "DISCOVER")) {
1648 gatekeeper_disable = 0;
1649 gatekeeper_discover = 1;
1652 gatekeeper_disable = 0;
1654 strncpy(gatekeeper, v->value, sizeof(gatekeeper) - 1);
1656 } else if (!strcasecmp(v->name, "secret")) {
1657 strncpy(secret, v->value, sizeof(secret) - 1);
1658 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
1659 gkroute = ast_true(v->value);
1660 } else if (!strcasecmp(v->name, "context")) {
1661 strncpy(default_context, v->value, sizeof(default_context) - 1);
1662 ast_verbose(VERBOSE_PREFIX_3 " == Setting default context to %s\n", default_context);
1663 } else if (!strcasecmp(v->name, "dtmfmode")) {
1664 if (!strcasecmp(v->value, "inband"))
1665 dtmfmode=H323_DTMF_INBAND;
1666 else if (!strcasecmp(v->value, "rfc2833"))
1667 dtmfmode = H323_DTMF_RFC2833;
1669 ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
1670 dtmfmode = H323_DTMF_RFC2833;
1672 } else if (!strcasecmp(v->name, "UserByAlias")) {
1673 userbyalias = ast_true(v->value);
1674 } else if (!strcasecmp(v->name, "bridge")) {
1675 bridging = ast_true(v->value);
1676 } else if (!strcasecmp(v->name, "noFastStart")) {
1677 noFastStart = ast_true(v->value);
1678 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
1679 noH245Tunneling = ast_true(v->value);
1684 cat = ast_category_browse(cfg, NULL);
1686 if (strcasecmp(cat, "general")) {
1687 utype = ast_variable_retrieve(cfg, cat, "type");
1689 if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
1690 user = build_user(cat, ast_variable_browse(cfg, cat));
1692 ast_mutex_lock(&userl.lock);
1693 user->next = userl.users;
1695 ast_mutex_unlock(&userl.lock);
1697 } else if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
1698 peer = build_peer(cat, ast_variable_browse(cfg, cat));
1700 ast_mutex_lock(&peerl.lock);
1701 peer->next = peerl.peers;
1703 ast_mutex_unlock(&peerl.lock);
1705 } else if (!strcasecmp(utype, "h323")) {
1706 alias = build_alias(cat, ast_variable_browse(cfg, cat));
1708 ast_mutex_lock(&aliasl.lock);
1709 alias->next = aliasl.aliases;
1710 aliasl.aliases = alias;
1711 ast_mutex_unlock(&aliasl.lock);
1714 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
1717 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
1719 cat = ast_category_browse(cfg, cat);
1723 /* Register our H.323 aliases if any*/
1725 if (h323_set_alias(alias)) {
1726 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
1729 alias = alias->next;
1732 /* Add some capabilities */
1733 ast_mutex_lock(&caplock);
1734 if(h323_set_capability(capability, dtmfmode)) {
1735 ast_log(LOG_ERROR, "Capabilities failure, this is bad.\n");
1736 ast_mutex_unlock(&caplock);
1739 ast_mutex_unlock(&caplock);
1743 void delete_users(void)
1745 struct oh323_user *user, *userlast;
1746 struct oh323_peer *peer;
1748 /* Delete all users */
1749 ast_mutex_lock(&userl.lock);
1750 for (user=userl.users;user;) {
1756 ast_mutex_unlock(&userl.lock);
1757 ast_mutex_lock(&peerl.lock);
1758 for (peer=peerl.peers;peer;) {
1759 /* Assume all will be deleted, and we'll find out for sure later */
1763 ast_mutex_unlock(&peerl.lock);
1766 void delete_aliases(void)
1768 struct oh323_alias *alias, *aliaslast;
1770 /* Delete all users */
1771 ast_mutex_lock(&aliasl.lock);
1772 for (alias=aliasl.aliases;alias;) {
1777 aliasl.aliases=NULL;
1778 ast_mutex_unlock(&aliasl.lock);
1781 void prune_peers(void)
1783 /* Prune peers who still are supposed to be deleted */
1784 struct oh323_peer *peer, *peerlast, *peernext;
1785 ast_mutex_lock(&peerl.lock);
1787 for (peer=peerl.peers;peer;) {
1788 peernext = peer->next;
1792 peerlast->next = peernext;
1794 peerl.peers = peernext;
1799 ast_mutex_unlock(&peerl.lock);
1809 if (!ast_strlen_zero(gatekeeper)) {
1817 /* Possibly register with a GK */
1818 if (gatekeeper_disable == 0) {
1819 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1820 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1831 static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
1833 struct oh323_pvt *p;
1834 p = (struct oh323_pvt *) chan->pvt->pvt;
1835 if (p && p->rtp && p->bridge) {
1841 static struct ast_rtp *oh323_get_vrtp_peer(struct ast_channel *chan)
1846 static char *convertcap(int cap)
1849 case AST_FORMAT_G723_1:
1851 case AST_FORMAT_GSM:
1853 case AST_FORMAT_ULAW:
1855 case AST_FORMAT_ALAW:
1857 case AST_FORMAT_ADPCM:
1859 case AST_FORMAT_G729A:
1861 case AST_FORMAT_SPEEX:
1863 case AST_FORMAT_ILBC:
1866 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
1872 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs)
1874 /* XXX Deal with Video */
1875 struct oh323_pvt *p;
1876 struct sockaddr_in them;
1877 struct sockaddr_in us;
1879 char iabuf[INET_ADDRSTRLEN];
1881 mode = convertcap(chan->writeformat);
1887 p = (struct oh323_pvt *) chan->pvt->pvt;
1889 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
1893 ast_rtp_get_peer(rtp, &them);
1894 ast_rtp_get_us(rtp, &us);
1896 h323_native_bridge(p->cd.call_token, ast_inet_ntoa(iabuf, sizeof(iabuf), them.sin_addr), mode);
1902 static struct ast_rtp_protocol oh323_rtp = {
1903 get_rtp_info: oh323_get_rtp_peer,
1904 get_vrtp_info: oh323_get_vrtp_peer,
1905 set_rtp_peer: oh323_set_rtp_peer,
1912 ast_mutex_init(&userl.lock);
1913 ast_mutex_init(&peerl.lock);
1914 ast_mutex_init(&aliasl.lock);
1916 res = reload_config();
1921 /* Make sure we can register our channel type */
1922 if (ast_channel_register(type, tdesc, capability, oh323_request)) {
1923 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1927 ast_cli_register(&cli_debug);
1928 ast_cli_register(&cli_no_debug);
1929 ast_cli_register(&cli_trace);
1930 ast_cli_register(&cli_no_trace);
1931 ast_cli_register(&cli_show_codecs);
1932 ast_cli_register(&cli_gk_cycle);
1933 ast_cli_register(&cli_hangup_call);
1934 ast_cli_register(&cli_show_tokens);
1936 oh323_rtp.type = type;
1937 ast_rtp_proto_register(&oh323_rtp);
1939 sched = sched_context_create();
1941 ast_log(LOG_WARNING, "Unable to create schedule context\n");
1943 io = io_context_create();
1945 ast_log(LOG_WARNING, "Unable to create I/O context\n");
1948 /* Register our callback functions */
1949 h323_callback_register(setup_incoming_call,
1950 setup_outgoing_call,
1951 external_rtp_create,
1952 setup_rtp_connection,
1959 /* start the h.323 listener */
1960 if (h323_start_listener(h323_signalling_port, bindaddr)) {
1961 ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
1965 /* Possibly register with a GK */
1966 if (gatekeeper_disable == 0) {
1967 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1968 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1972 /* And start the monitor for the first time */
1981 struct oh323_pvt *p, *pl;
1983 if (!ast_mutex_lock(&iflock)) {
1984 /* hangup all interfaces if they have an owner */
1988 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1992 ast_mutex_unlock(&iflock);
1994 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1998 if (!ast_mutex_lock(&monlock)) {
1999 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
2000 pthread_cancel(monitor_thread);
2001 pthread_kill(monitor_thread, SIGURG);
2002 pthread_join(monitor_thread, NULL);
2004 monitor_thread = AST_PTHREADT_STOP;
2005 ast_mutex_unlock(&monlock);
2007 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
2011 if (!ast_mutex_lock(&iflock)) {
2012 /* destroy all the interfaces and free their memory */
2017 /* free associated memory */
2018 ast_mutex_destroy(&pl->lock);
2022 ast_mutex_unlock(&iflock);
2024 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
2030 /* unregister rtp */
2031 ast_rtp_proto_unregister(&oh323_rtp);
2033 /* unregister commands */
2034 ast_cli_unregister(&cli_debug);
2035 ast_cli_unregister(&cli_no_debug);
2036 ast_cli_unregister(&cli_trace);
2037 ast_cli_unregister(&cli_no_trace);
2038 ast_cli_unregister(&cli_show_codecs);
2039 ast_cli_unregister(&cli_gk_cycle);
2040 ast_cli_unregister(&cli_hangup_call);
2041 ast_cli_unregister(&cli_show_tokens);
2043 /* unregister channel type */
2044 ast_channel_unregister(type);
2052 ast_mutex_lock(&usecnt_lock);
2054 ast_mutex_unlock(&usecnt_lock);
2065 return ASTERISK_GPL_KEY;