4 * OpenH323 Channel Driver for ASTERISK PBX.
6 * For The NuFone Network
8 * This code 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.
30 #include <sys/socket.h>
31 #include <sys/signal.h>
32 #include <sys/param.h>
35 #define IPTOS_MINCOST 0x02
38 #include <arpa/inet.h>
40 #include <netinet/in.h>
41 #include <netinet/in_systm.h>
42 #include <netinet/ip.h>
53 #include <asterisk/lock.h>
54 #include <asterisk/logger.h>
55 #include <asterisk/channel.h>
56 #include <asterisk/channel_pvt.h>
57 #include <asterisk/config.h>
58 #include <asterisk/module.h>
59 #include <asterisk/pbx.h>
60 #include <asterisk/options.h>
61 #include <asterisk/utils.h>
62 #include <asterisk/lock.h>
63 #include <asterisk/sched.h>
64 #include <asterisk/io.h>
65 #include <asterisk/rtp.h>
66 #include <asterisk/acl.h>
67 #include <asterisk/callerid.h>
68 #include <asterisk/cli.h>
69 #include <asterisk/dsp.h>
73 #include "h323/chan_h323.h"
75 send_digit_cb on_send_digit;
76 on_connection_cb on_create_connection;
77 setup_incoming_cb on_incoming_call;
78 setup_outbound_cb on_outgoing_call;
79 start_logchan_cb on_start_logical_channel;
80 chan_ringing_cb on_chan_ringing;
81 con_established_cb on_connection_established;
82 clear_con_cb on_connection_cleared;
83 answer_call_cb on_answer_call;
87 /** String 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";
93 static char default_context[AST_MAX_EXTENSION];
95 /** H.323 configuration values */
96 static char gatekeeper[100];
97 static int gatekeeper_disable = 1;
98 static int gatekeeper_discover = 0;
100 static int port = 1720;
101 static int gkroute = 0;
103 /* to find user by alias is default, alternative is the incomming call's source IP address*/
104 static int userbyalias = 1;
106 static int bridge_default = 1;
108 /* Just about everybody seems to support ulaw, so make it a nice default */
109 static int capability = AST_FORMAT_ULAW;
114 static int dtmfmode = H323_DTMF_RFC2833;
116 static char secret[50];
118 /** Private structure of a OpenH323 channel */
120 ast_mutex_t lock; /* Channel private lock */
121 call_options_t calloptions; /* Options to be used during call setup */
122 int alreadygone; /* Whether or not we've already been destroyed by our peer */
123 int needdestroy; /* if we need to be destroyed */
124 call_details_t cd; /* Call details */
125 struct ast_channel *owner; /* Who owns us */
126 int capability; /* audio capability */
127 int nonCodecCapability; /* non-audio capability */
128 int outgoing; /* Outgoing or incoming call? */
129 int nat; /* Are we talking to a NAT EP?*/
130 int bridge; /* Determine of we should native bridge or not*/
131 char exten[AST_MAX_EXTENSION]; /* Requested extension */
132 char context[AST_MAX_EXTENSION]; /* Context where to start */
133 char username[81]; /* H.323 alias using this channel */
134 char accountcode[256]; /* Account code */
135 int amaflags; /* AMA Flags */
136 char callerid[80]; /* Caller*ID if available */
137 struct ast_rtp *rtp; /* RTP Session */
138 int dtmfmode; /* What DTMF Mode is being used */
139 struct ast_dsp *vad; /* Used for in-band DTMF detection */
140 struct oh323_pvt *next; /* Next channel in list */
143 static struct ast_user_list {
144 struct oh323_user *users;
148 static struct ast_peer_list {
149 struct oh323_peer *peers;
153 static struct ast_alias_list {
154 struct oh323_alias *aliases;
158 /** Asterisk RTP stuff*/
159 static struct sched_context *sched;
160 static struct io_context *io;
162 /** Protect the interface list (of oh323_pvt's) */
163 AST_MUTEX_DEFINE_STATIC(iflock);
165 /** Usage counter and associated lock */
166 static int usecnt =0;
167 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
169 /* Protect the monitoring thread, so only one process can kill or start it, and not
170 when it's doing something critical. */
171 AST_MUTEX_DEFINE_STATIC(monlock);
173 /* This is the thread for the monitor which checks for input on the channels
174 which are not currently in use. */
175 static pthread_t monitor_thread = AST_PTHREADT_NULL;
177 static int restart_monitor(void);
179 static void __oh323_destroy(struct oh323_pvt *p)
181 struct oh323_pvt *cur, *prev = NULL;
184 ast_rtp_destroy(p->rtp);
187 /* Unlink us from the owner if we have one */
189 ast_mutex_lock(&p->owner->lock);
190 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
191 p->owner->pvt->pvt = NULL;
192 ast_mutex_unlock(&p->owner->lock);
198 prev->next = cur->next;
207 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
209 ast_mutex_destroy(&p->lock);
214 static void oh323_destroy(struct oh323_pvt *p)
216 ast_mutex_lock(&iflock);
218 ast_mutex_unlock(&iflock);
221 static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
223 struct oh323_alias *alias;
225 alias = (struct oh323_alias *)malloc(sizeof(struct oh323_alias));
228 memset(alias, 0, sizeof(struct oh323_alias));
229 strncpy(alias->name, name, sizeof(alias->name)-1);
232 if (!strcasecmp(v->name, "e164")) {
233 strncpy(alias->e164, v->value, sizeof(alias->e164)-1);
234 } else if (!strcasecmp(v->name, "prefix")) {
235 strncpy(alias->prefix, v->value, sizeof(alias->prefix)-1);
236 } else if (!strcasecmp(v->name, "context")) {
237 strncpy(alias->context, v->value, sizeof(alias->context)-1);
238 } else if (!strcasecmp(v->name, "secret")) {
239 strncpy(alias->secret, v->value, sizeof(alias->secret)-1);
241 if (strcasecmp(v->value, "h323")) {
242 ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->value);
251 static struct oh323_user *build_user(char *name, struct ast_variable *v)
253 struct oh323_user *user;
256 user = (struct oh323_user *)malloc(sizeof(struct oh323_user));
258 memset(user, 0, sizeof(struct oh323_user));
259 strncpy(user->name, name, sizeof(user->name)-1);
261 /* set the usage flag to a sane starting value*/
263 /* Assume we can native bridge */
264 user->bridge = bridge_default;
267 if (!strcasecmp(v->name, "context")) {
268 strncpy(user->context, v->value, sizeof(user->context)-1);
269 } else if (!strcasecmp(v->name, "bridge")) {
270 user->bridge = ast_true(v->value);
271 } else if (!strcasecmp(v->name, "nat")) {
272 user->nat = ast_true(v->value);
273 } else if (!strcasecmp(v->name, "noFastStart")) {
274 user->noFastStart = ast_true(v->value);
275 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
276 user->noH245Tunneling = ast_true(v->value);
277 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
278 user->noSilenceSuppression = ast_true(v->value);
279 } else if (!strcasecmp(v->name, "secret")) {
280 strncpy(user->secret, v->value, sizeof(user->secret)-1);
281 } else if (!strcasecmp(v->name, "callerid")) {
282 strncpy(user->callerid, v->value, sizeof(user->callerid)-1);
283 } else if (!strcasecmp(v->name, "accountcode")) {
284 strncpy(user->accountcode, v->value, sizeof(user->accountcode)-1);
285 } else if (!strcasecmp(v->name, "incominglimit")) {
286 user->incominglimit = atoi(v->value);
287 if (user->incominglimit < 0)
288 user->incominglimit = 0;
289 } else if (!strcasecmp(v->name, "host")) {
290 if (!strcasecmp(v->value, "dynamic")) {
291 ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
294 } else if (ast_get_ip(&user->addr, v->value)) {
298 /* Let us know we need to use ip authentication */
300 } else if (!strcasecmp(v->name, "amaflags")) {
301 format = ast_cdr_amaflags2int(v->value);
303 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
305 user->amaflags = format;
315 static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
317 struct oh323_peer *peer;
318 struct oh323_peer *prev;
322 ast_mutex_lock(&peerl.lock);
326 if (!strcasecmp(peer->name, name)) {
335 /* Already in the list, remove it and it will be added back (or FREE'd) */
337 prev->next = peer->next;
339 peerl.peers = peer->next;
341 ast_mutex_unlock(&peerl.lock);
343 ast_mutex_unlock(&peerl.lock);
344 peer = (struct oh323_peer*)malloc(sizeof(struct oh323_peer));
345 memset(peer, 0, sizeof(struct oh323_peer));
349 strncpy(peer->name, name, sizeof(peer->name)-1);
352 /* set the usage flag to a sane starting value*/
356 if (!strcasecmp(v->name, "context")) {
357 strncpy(peer->context, v->value, sizeof(peer->context)-1);
358 } else if (!strcasecmp(v->name, "bridge")) {
359 peer->bridge = ast_true(v->value);
360 } else if (!strcasecmp(v->name, "noFastStart")) {
361 peer->noFastStart = ast_true(v->value);
362 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
363 peer->noH245Tunneling = ast_true(v->value);
364 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
365 peer->noSilenceSuppression = ast_true(v->value);
366 } else if (!strcasecmp(v->name, "outgoinglimit")) {
367 peer->outgoinglimit = atoi(v->value);
368 if (peer->outgoinglimit > 0)
369 peer->outgoinglimit = 0;
370 } else if (!strcasecmp(v->name, "host")) {
371 if (!strcasecmp(v->value, "dynamic")) {
372 ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
376 if (ast_get_ip(&peer->addr, v->value)) {
390 * Send (play) the specified digit to the channel.
393 static int oh323_digit(struct ast_channel *c, char digit)
395 struct oh323_pvt *p = (struct oh323_pvt *) c->pvt->pvt;
396 if (p && p->rtp && (p->dtmfmode & H323_DTMF_RFC2833)) {
397 ast_rtp_senddigit(p->rtp, digit);
399 /* If in-band DTMF is desired, send that */
400 if (p->dtmfmode & H323_DTMF_INBAND)
401 h323_send_tone(p->cd.call_token, digit);
407 * Make a call over the specified channel to the specified
408 * destination. This function will parse the destination string
409 * and determine the address-number to call.
410 * Return -1 on error, 0 on success.
412 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
415 struct oh323_pvt *p = (struct oh323_pvt *) c->pvt->pvt;
416 char called_addr[256];
417 char *tmp, *cid, *cidname, oldcid[256];
419 strtok_r(dest, "/", &(tmp));
421 ast_log(LOG_DEBUG, "dest=%s, timeout=%d.\n", dest, timeout);
423 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
424 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
431 /* Clear the call token */
432 if ((p->cd).call_token == NULL)
433 (p->cd).call_token = (char *)malloc(128);
435 memset((char *)(p->cd).call_token, 0, 128);
437 if (p->cd.call_token == NULL) {
438 ast_log(LOG_ERROR, "Not enough memory.\n");
442 /* Build the address to call */
443 memset(called_addr, 0, sizeof(dest));
444 memcpy(called_addr, dest, sizeof(called_addr));
446 /* Copy callerid, if there is any */
448 memset(oldcid, 0, sizeof(oldcid));
449 memcpy(oldcid, c->callerid, strlen(c->callerid));
450 oldcid[sizeof(oldcid)-1] = '\0';
451 ast_callerid_parse(oldcid, &cidname, &cid);
452 if (p->calloptions.callerid) {
453 free(p->calloptions.callerid);
454 p->calloptions.callerid = NULL;
456 if (p->calloptions.callername) {
457 free(p->calloptions.callername);
458 p->calloptions.callername = NULL;
460 p->calloptions.callerid = (char*)malloc(256);
461 if (p->calloptions.callerid == NULL) {
462 ast_log(LOG_ERROR, "Not enough memory.\n");
465 memset(p->calloptions.callerid, 0, 256);
466 if ((cid != NULL)&&(strlen(cid) > 0))
467 strncpy(p->calloptions.callerid, cid, 255);
469 p->calloptions.callername = (char*)malloc(256);
470 if (p->calloptions.callername == NULL) {
471 ast_log(LOG_ERROR, "Not enough memory.\n");
474 memset(p->calloptions.callername, 0, 256);
475 if ((cidname != NULL)&&(strlen(cidname) > 0))
476 strncpy(p->calloptions.callername, cidname, 255);
479 if (p->calloptions.callerid) {
480 free(p->calloptions.callerid);
481 p->calloptions.callerid = NULL;
483 if (p->calloptions.callername) {
484 free(p->calloptions.callername);
485 p->calloptions.callername = NULL;
489 res = h323_make_call(called_addr, &(p->cd), p->calloptions);
492 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
498 static int oh323_answer(struct ast_channel *c)
502 struct oh323_pvt *p = (struct oh323_pvt *) c->pvt->pvt;
504 res = h323_answering_call(p->cd.call_token, 0);
506 if (c->_state != AST_STATE_UP)
507 ast_setstate(c, AST_STATE_UP);
512 static int oh323_hangup(struct ast_channel *c)
514 struct oh323_pvt *p = (struct oh323_pvt *) c->pvt->pvt;
517 ast_log(LOG_DEBUG, "oh323_hangup(%s)\n", c->name);
519 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
522 ast_mutex_lock(&p->lock);
523 /* Determine how to disconnect */
525 ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
526 ast_mutex_unlock(&p->lock);
529 if (!c || (c->_state != AST_STATE_UP))
532 p = (struct oh323_pvt *) c->pvt->pvt;
534 /* Free dsp used for in-band DTMF detection */
536 ast_dsp_free(p->vad);
542 /* Start the process if it's not already started */
543 if (!p->alreadygone) {
544 if (h323_clear_call((p->cd).call_token)) {
545 ast_log(LOG_DEBUG, "ClearCall failed.\n");
550 /* Update usage counter */
551 ast_mutex_lock(&usecnt_lock);
554 ast_log(LOG_WARNING, "Usecnt < 0\n");
555 ast_mutex_unlock(&usecnt_lock);
556 ast_update_use_count();
558 ast_mutex_unlock(&p->lock);
562 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *p)
564 /* Retrieve audio/etc from channel. Assumes p->lock is already held. */
566 static struct ast_frame null_frame = { AST_FRAME_NULL, };
568 /* Only apply it for the first packet, we just need the correct ip/port */
571 ast_rtp_setnat(p->rtp,p->nat);
575 f = ast_rtp_read(p->rtp);
576 /* Don't send RFC2833 if we're not supposed to */
577 if (f && (f->frametype == AST_FRAME_DTMF) && !(p->dtmfmode & H323_DTMF_RFC2833))
580 /* We already hold the channel lock */
581 if (f->frametype == AST_FRAME_VOICE) {
582 if (f->subclass != p->owner->nativeformats) {
583 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
584 p->owner->nativeformats = f->subclass;
585 ast_set_read_format(p->owner, p->owner->readformat);
586 ast_set_write_format(p->owner, p->owner->writeformat);
589 /* Do in-band DTMF detection */
590 if (p->dtmfmode & H323_DTMF_INBAND) {
591 f = ast_dsp_process(p->owner,p->vad,f);
592 if (f->frametype == AST_FRAME_DTMF)
593 ast_log(LOG_DEBUG, "Got in-band digit %c.\n", f->subclass);
603 static struct ast_frame *oh323_read(struct ast_channel *c)
605 struct ast_frame *fr;
606 struct oh323_pvt *p = (struct oh323_pvt *) c->pvt->pvt;
607 ast_mutex_lock(&p->lock);
608 fr = oh323_rtp_read(p);
609 ast_mutex_unlock(&p->lock);
613 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
615 struct oh323_pvt *p = (struct oh323_pvt *) c->pvt->pvt;
617 if (frame->frametype != AST_FRAME_VOICE) {
618 if (frame->frametype == AST_FRAME_IMAGE)
621 ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
625 if (!(frame->subclass & c->nativeformats)) {
626 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
627 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
632 ast_mutex_lock(&p->lock);
634 res = ast_rtp_write(p->rtp, frame);
636 ast_mutex_unlock(&p->lock);
641 /** FIXME: Can I acutally use this or does Open H.323 take care of everything? */
642 static int oh323_indicate(struct ast_channel *c, int condition)
645 struct oh323_pvt *p = (struct oh323_pvt *) c->pvt->pvt;
648 case AST_CONTROL_RINGING:
649 if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
650 h323_send_alerting(p->cd.call_token);
654 case AST_CONTROL_PROGRESS:
655 if (c->_state != AST_STATE_UP) {
656 h323_send_progress(p->cd.call_token);
661 case AST_CONTROL_BUSY:
662 if (c->_state != AST_STATE_UP) {
663 h323_answering_call(p->cd.call_token, 1);
665 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
669 case AST_CONTROL_CONGESTION:
670 if (c->_state != AST_STATE_UP) {
671 h323_answering_call(p->cd.call_token, 1);
673 ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
677 case AST_CONTROL_PROCEEDING:
681 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
687 // FIXME: WTF is this? Do I need this???
688 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
690 struct oh323_pvt *p = (struct oh323_pvt *) newchan->pvt->pvt;
692 ast_mutex_lock(&p->lock);
693 if (p->owner != oldchan) {
694 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
698 ast_mutex_unlock(&p->lock);
702 static struct ast_channel *oh323_new(struct oh323_pvt *i, int state, const char *host)
704 struct ast_channel *ch;
706 ch = ast_channel_alloc(1);
710 snprintf(ch->name, sizeof(ch->name)-1, "H323/%s", host);
711 ch->nativeformats = i->capability;
712 if (!ch->nativeformats)
713 ch->nativeformats = capability;
714 fmt = ast_best_codec(ch->nativeformats);
716 ch->fds[0] = ast_rtp_fd(i->rtp);
718 if (state == AST_STATE_RING)
721 ch->writeformat = fmt;
722 ch->pvt->rawwriteformat = fmt;
723 ch->readformat = fmt;
724 ch->pvt->rawreadformat = fmt;
726 /* Allocate dsp for in-band DTMF support */
727 if (i->dtmfmode & H323_DTMF_INBAND) {
728 i->vad = ast_dsp_new();
729 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
732 /* Register the OpenH323 channel's functions. */
734 ch->pvt->send_digit = oh323_digit;
735 ch->pvt->call = oh323_call;
736 ch->pvt->hangup = oh323_hangup;
737 ch->pvt->answer = oh323_answer;
738 ch->pvt->read = oh323_read;
739 ch->pvt->write = oh323_write;
740 ch->pvt->indicate = oh323_indicate;
741 ch->pvt->fixup = oh323_fixup;
742 /* ch->pvt->bridge = ast_rtp_bridge; */
744 /* Set the owner of this channel */
747 ast_mutex_lock(&usecnt_lock);
749 ast_mutex_unlock(&usecnt_lock);
750 ast_update_use_count();
751 strncpy(ch->context, i->context, sizeof(ch->context)-1);
752 strncpy(ch->exten, i->exten, sizeof(ch->exten)-1);
754 if (!ast_strlen_zero(i->callerid))
755 ch->callerid = strdup(i->callerid);
756 if (!ast_strlen_zero(i->accountcode))
757 strncpy(ch->accountcode, i->accountcode, sizeof(ch->accountcode)-1);
759 ch->amaflags = i->amaflags;
760 ast_setstate(ch, state);
761 if (state != AST_STATE_DOWN) {
762 if (ast_pbx_start(ch)) {
763 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
769 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
773 static struct oh323_pvt *oh323_alloc(int callid)
777 p = (struct oh323_pvt *) malloc(sizeof(struct oh323_pvt));
779 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
783 /* Keep track of stuff */
784 memset(p, 0, sizeof(struct oh323_pvt));
785 p->rtp = ast_rtp_new(sched, io, 1, 0);
788 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
792 ast_rtp_settos(p->rtp, tos);
793 ast_mutex_init(&p->lock);
795 p->cd.call_reference = callid;
796 p->bridge = bridge_default;
798 p->dtmfmode = dtmfmode;
799 if (p->dtmfmode & H323_DTMF_RFC2833)
800 p->nonCodecCapability |= AST_RTP_DTMF;
802 /* Add to interface list */
803 ast_mutex_lock(&iflock);
806 ast_mutex_unlock(&iflock);
810 static struct oh323_pvt *find_call(int call_reference)
814 ast_mutex_lock(&iflock);
818 if ((signed int)p->cd.call_reference == call_reference) {
820 ast_mutex_unlock(&iflock);
825 ast_mutex_unlock(&iflock);
830 static struct ast_channel *oh323_request(char *type, int format, void *data)
835 struct ast_channel *tmpc = NULL;
836 char *dest = (char *) data;
842 ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
845 format &= capability;
847 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
851 strncpy(tmp, dest, sizeof(tmp) - 1);
853 host = strchr(tmp, '@');
863 strtok_r(host, "/", &(h323id));
865 if (h323id && !ast_strlen_zero(h323id)) {
872 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
876 /* Assign a default capability */
877 p->capability = capability;
880 if (p->dtmfmode & H323_DTMF_RFC2833) {
881 p->nonCodecCapability |= AST_RTP_DTMF;
883 p->nonCodecCapability &= ~AST_RTP_DTMF;
886 /* pass on our preferred codec to the H.323 stack */
887 h323_set_capability(format, dtmfmode);
890 strncpy(p->username, ext, sizeof(p->username) - 1);
892 ast_log(LOG_DEBUG, "Host: %s\tUsername: %s\n", host, p->username);
894 tmpc = oh323_new(p, AST_STATE_DOWN, host);
903 struct oh323_alias *find_alias(const char *source_aliases)
905 struct oh323_alias *a;
911 if (!strcasecmp(a->name, source_aliases)) {
919 struct oh323_user *find_user(const call_details_t cd)
921 struct oh323_user *u;
922 char iabuf[INET_ADDRSTRLEN];
924 if(userbyalias == 1){
926 if (!strcasecmp(u->name, cd.call_source_aliases)) {
934 if (!strcasecmp(cd.sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), u->addr.sin_addr))) {
946 struct oh323_peer *find_peer(char *dest_peer)
948 struct oh323_peer *p;
953 if (!strcasecmp(p->name, dest_peer)) {
963 * Callback for sending digits from H.323 up to asterisk
966 int send_digit(unsigned call_reference, char digit)
971 ast_log(LOG_DEBUG, "Recieved Digit: %c\n", digit);
972 p = find_call(call_reference);
975 ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
978 memset(&f, 0, sizeof(f));
979 f.frametype = AST_FRAME_DTMF;
986 f.src = "SEND_DIGIT";
988 return ast_queue_frame(p->owner, &f);
992 * Call-back function that gets called when any H.323 connection is made
994 * Returns the local RTP information
996 struct rtp_info *create_connection(unsigned call_reference)
999 struct sockaddr_in us;
1000 struct sockaddr_in them;
1001 struct rtp_info *info;
1002 /* XXX This is sooooo bugus. inet_ntoa is not reentrant
1003 but this function wants to return a static variable so
1004 the only way to do this will be to declare iabuf within
1005 the oh323_pvt structure XXX */
1006 static char iabuf[INET_ADDRSTRLEN];
1008 info = (struct rtp_info *) malloc(sizeof(struct rtp_info));
1010 p = find_call(call_reference);
1013 ast_log(LOG_ERROR, "Unable to allocate private structure, this is very bad.\n");
1017 /* figure out our local RTP port and tell the H.323 stack about it*/
1018 ast_rtp_get_us(p->rtp, &us);
1019 ast_rtp_get_peer(p->rtp, &them);
1021 info->addr = ast_inet_ntoa(iabuf, sizeof(iabuf), us.sin_addr);
1022 info->port = ntohs(us.sin_port);
1028 * Call-back function for incoming calls
1030 * Returns 1 on success
1033 int setup_incoming_call(call_details_t cd)
1036 struct oh323_pvt *p = NULL;
1037 /* struct ast_channel *c = NULL; */
1038 struct oh323_user *user = NULL;
1039 struct oh323_alias *alias = NULL;
1040 char iabuf[INET_ADDRSTRLEN];
1042 /* allocate the call*/
1043 p = oh323_alloc(cd.call_reference);
1046 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
1050 /* Populate the call details in the private structure */
1051 p->cd.call_token = cd.call_token;
1052 p->cd.call_source_aliases = cd.call_source_aliases;
1053 p->cd.call_dest_alias = cd.call_dest_alias;
1054 p->cd.call_source_name = cd.call_source_name;
1055 p->cd.call_source_e164 = cd.call_source_e164;
1056 p->cd.call_dest_e164 = cd.call_dest_e164;
1059 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
1060 ast_verbose(VERBOSE_PREFIX_3 " Call token: [%s]\n", p->cd.call_token);
1061 ast_verbose(VERBOSE_PREFIX_3 " Calling party name: [%s]\n", p->cd.call_source_name);
1062 ast_verbose(VERBOSE_PREFIX_3 " Calling party number: [%s]\n", p->cd.call_source_e164);
1063 ast_verbose(VERBOSE_PREFIX_3 " Called party name: [%s]\n", p->cd.call_dest_alias);
1064 ast_verbose(VERBOSE_PREFIX_3 " Called party number: [%s]\n", p->cd.call_dest_e164);
1067 /* Decide if we are allowing Gatekeeper routed calls*/
1068 if ((!strcasecmp(cd.sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk == 1)) {
1070 if (!ast_strlen_zero(cd.call_dest_e164)) {
1071 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1072 strncpy(p->context, default_context, sizeof(p->context)-1);
1074 alias = find_alias(cd.call_dest_alias);
1077 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd.call_dest_alias);
1080 strncpy(p->exten, alias->name, sizeof(p->exten)-1);
1081 strncpy(p->context, alias->context, sizeof(p->context)-1);
1083 sprintf(p->callerid, "%s <%s>", p->cd.call_source_name, p->cd.call_source_e164);
1085 /* Either this call is not from the Gatekeeper
1086 or we are not allowing gk routed calls */
1087 user = find_user(cd);
1090 sprintf(p->callerid, "%s <%s>", p->cd.call_source_name, p->cd.call_source_e164);
1091 if (!ast_strlen_zero(p->cd.call_dest_e164)) {
1092 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1094 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
1096 if (ast_strlen_zero(default_context)) {
1097 ast_log(LOG_ERROR, "Call from '%s' rejected due to no default context\n", p->cd.call_source_aliases);
1100 strncpy(p->context, default_context, sizeof(p->context)-1);
1101 ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd.call_source_aliases, p->context);
1104 if (strcasecmp(cd.sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), user->addr.sin_addr))){
1105 if (ast_strlen_zero(user->context)) {
1106 if (ast_strlen_zero(default_context)) {
1107 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd.sourceIp);
1110 strncpy(p->context, default_context, sizeof(p->context)-1);
1112 strncpy(p->context, user->context, sizeof(p->context)-1);
1114 sprintf(p->exten, "i");
1115 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd.sourceIp);
1119 if (user->incominglimit > 0) {
1120 if (user->inUse >= user->incominglimit) {
1121 ast_log(LOG_ERROR, "Call from user '%s' rejected due to usage limit of %d\n", user->name, user->incominglimit);
1125 strncpy(p->context, user->context, sizeof(p->context)-1);
1126 p->bridge = user->bridge;
1129 if (!ast_strlen_zero(user->callerid)) {
1130 strncpy(p->callerid, user->callerid, sizeof(p->callerid) - 1);
1132 sprintf(p->callerid, "%s <%s>", p->cd.call_source_name, p->cd.call_source_e164);
1134 if (!ast_strlen_zero(p->cd.call_dest_e164)) {
1135 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1137 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
1139 if (!ast_strlen_zero(user->accountcode)) {
1140 strncpy(p->accountcode, user->accountcode, sizeof(p->accountcode)-1);
1143 /* Increment the usage counter */
1150 /* allocate a channel and tell asterisk about it */
1151 c = oh323_new(p, AST_STATE_RINGING, cd.call_token);
1153 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
1161 * Call-back function to start PBX when OpenH323 ready to serve incoming call
1163 * Returns 1 on success
1165 static int answer_call(unsigned call_reference)
1167 struct oh323_pvt *p = NULL;
1168 struct ast_channel *c = NULL;
1170 /* Find the call or allocate a private structure if call not found */
1171 p = find_call(call_reference);
1174 ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
1178 /* allocate a channel and tell asterisk about it */
1179 c = oh323_new(p, AST_STATE_RINGING, p->cd.call_token);
1181 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
1188 * Call-back function to establish an outgoing H.323 call
1190 * Returns 1 on success
1192 int setup_outgoing_call(call_details_t cd)
1198 if (p->inUse >= p->outgoinglimit) {
1199 ast_log(LOG_ERROR, "Call to %s rejected due to usage limit of %d outgoing channels\n", p->name, p->inUse);
1204 ast_log(LOG_ERROR, "Rejecting call: peer %s not found\n", dest_peer);
1210 * Call-back function that gets called for each rtp channel opened
1214 void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort)
1216 struct oh323_pvt *p = NULL;
1217 struct sockaddr_in them;
1219 /* Find the call or allocate a private structure if call not found */
1220 p = find_call(call_reference);
1223 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1227 them.sin_family = AF_INET;
1228 them.sin_addr.s_addr = inet_addr(remoteIp); // only works for IPv4
1229 them.sin_port = htons(remotePort);
1230 ast_rtp_set_peer(p->rtp, &them);
1236 * Call-back function to signal asterisk that the channel has been answered
1239 void connection_made(unsigned call_reference)
1241 struct ast_channel *c = NULL;
1242 struct oh323_pvt *p = NULL;
1244 p = find_call(call_reference);
1247 ast_log(LOG_ERROR, "Something is wrong: connection\n");
1251 ast_log(LOG_ERROR, "Channel has no owner\n");
1256 ast_setstate(c, AST_STATE_UP);
1257 ast_queue_control(c, AST_CONTROL_ANSWER);
1262 * Call-back function to signal asterisk that the channel is ringing
1265 void chan_ringing(unsigned call_reference)
1267 struct ast_channel *c = NULL;
1268 struct oh323_pvt *p = NULL;
1270 p = find_call(call_reference);
1273 ast_log(LOG_ERROR, "Something is wrong: ringing\n");
1277 ast_log(LOG_ERROR, "Channel has no owner\n");
1281 ast_setstate(c, AST_STATE_RINGING);
1282 ast_queue_control(c, AST_CONTROL_RINGING);
1287 * Call-back function to cleanup communication
1290 void cleanup_connection(call_details_t cd)
1292 struct oh323_pvt *p = NULL;
1293 /* struct oh323_peer *peer = NULL; */
1294 struct oh323_user *user = NULL;
1295 struct ast_rtp *rtp = NULL;
1297 p = find_call(cd.call_reference);
1302 ast_mutex_lock(&p->lock);
1304 /* Decrement usage counter */
1306 user = find_user(cd);
1315 peer = find_peer(cd.call_dest_alias);
1318 user = find_user(cd);
1326 /* Immediately stop RTP */
1327 ast_rtp_destroy(rtp);
1334 ast_queue_hangup(p->owner);
1337 ast_mutex_unlock(&p->lock);
1341 static void *do_monitor(void *data)
1344 struct oh323_pvt *oh323 = NULL;
1347 /* Check for interfaces needing to be killed */
1348 ast_mutex_lock(&iflock);
1352 if (oh323->needdestroy) {
1353 __oh323_destroy(oh323);
1356 oh323 = oh323->next;
1358 ast_mutex_unlock(&iflock);
1360 /* Wait for sched or io */
1361 res = ast_sched_wait(sched);
1362 if ((res < 0) || (res > 1000))
1364 res = ast_io_wait(io, res);
1366 pthread_testcancel();
1368 ast_mutex_lock(&monlock);
1370 ast_sched_runq(sched);
1371 ast_mutex_unlock(&monlock);
1378 static int restart_monitor(void)
1380 /* If we're supposed to be stopped -- stay stopped */
1381 if (monitor_thread == AST_PTHREADT_STOP)
1383 if (ast_mutex_lock(&monlock)) {
1384 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1387 if (monitor_thread == pthread_self()) {
1388 ast_mutex_unlock(&monlock);
1389 ast_log(LOG_WARNING, "Cannot kill myself\n");
1392 if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
1393 /* Wake up the thread */
1394 pthread_kill(monitor_thread, SIGURG);
1396 /* Start a new monitor */
1397 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1398 ast_mutex_unlock(&monlock);
1399 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1403 ast_mutex_unlock(&monlock);
1407 static int h323_do_trace(int fd, int argc, char *argv[])
1410 return RESULT_SHOWUSAGE;
1412 h323_debug(1, atoi(argv[2]));
1413 ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
1414 return RESULT_SUCCESS;
1417 static int h323_no_trace(int fd, int argc, char *argv[])
1420 return RESULT_SHOWUSAGE;
1423 ast_cli(fd, "H.323 trace disabled\n");
1424 return RESULT_SUCCESS;
1427 static int h323_do_debug(int fd, int argc, char *argv[])
1430 return RESULT_SHOWUSAGE;
1433 ast_cli(fd, "H323 debug enabled\n");
1434 return RESULT_SUCCESS;
1437 static int h323_no_debug(int fd, int argc, char *argv[])
1440 return RESULT_SHOWUSAGE;
1443 ast_cli(fd, "H323 Debug disabled\n");
1444 return RESULT_SUCCESS;
1447 static int h323_gk_cycle(int fd, int argc, char *argv[])
1449 return RESULT_SUCCESS;
1452 return RESULT_SHOWUSAGE;
1456 /* Possibly register with a GK */
1457 if (!gatekeeper_disable) {
1458 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1459 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1462 return RESULT_SUCCESS;
1466 static int h323_ep_hangup(int fd, int argc, char *argv[])
1470 return RESULT_SHOWUSAGE;
1473 if (h323_soft_hangup(argv[2])) {
1474 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
1476 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
1479 return RESULT_SUCCESS;
1482 static int h323_tokens_show(int fd, int argc, char *argv[])
1486 return RESULT_SHOWUSAGE;
1490 return RESULT_SUCCESS;
1494 static char trace_usage[] =
1495 "Usage: h.323 trace <level num>\n"
1496 " Enables H.323 stack tracing for debugging purposes\n";
1498 static char no_trace_usage[] =
1499 "Usage: h.323 no trace\n"
1500 " Disables H.323 stack tracing for debugging purposes\n";
1502 static char debug_usage[] =
1503 "Usage: h.323 debug\n"
1504 " Enables chan_h323 debug output\n";
1506 static char no_debug_usage[] =
1507 "Usage: h.323 no debug\n"
1508 " Disables chan_h323 debug output\n";
1510 static char show_codec_usage[] =
1511 "Usage: h.323 show codec\n"
1512 " Shows all enabled codecs\n";
1514 static char show_cycle_usage[] =
1515 "Usage: h.323 gk cycle\n"
1516 " Manually re-register with the Gatekeper\n";
1518 static char show_hangup_usage[] =
1519 "Usage: h.323 hangup <token>\n"
1520 " Manually try to hang up call identified by <token>\n";
1522 static char show_tokens_usage[] =
1523 "Usage: h.323 show tokens\n"
1524 " Print out all active call tokens\n";
1526 static struct ast_cli_entry cli_trace =
1527 { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
1528 static struct ast_cli_entry cli_no_trace =
1529 { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
1530 static struct ast_cli_entry cli_debug =
1531 { { "h.323", "debug", NULL }, h323_do_debug, "Enable chan_h323 debug", debug_usage };
1532 static struct ast_cli_entry cli_no_debug =
1533 { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable chan_h323 debug", no_debug_usage };
1534 static struct ast_cli_entry cli_show_codecs =
1535 { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
1536 static struct ast_cli_entry cli_gk_cycle =
1537 { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
1538 static struct ast_cli_entry cli_hangup_call =
1539 { { "h.323", "hangup", NULL }, h323_ep_hangup, "Show all active call tokens", show_hangup_usage };
1540 static struct ast_cli_entry cli_show_tokens =
1541 { { "h.323", "show", "tokens", NULL }, h323_tokens_show, "Manually try to hang up a call", show_tokens_usage };
1545 int reload_config(void)
1549 struct ast_config *cfg;
1550 struct ast_variable *v;
1551 struct oh323_peer *peer = NULL;
1552 struct oh323_user *user = NULL;
1553 struct oh323_alias *alias = NULL;
1554 struct ast_hostent ahp; struct hostent *hp;
1558 cfg = ast_load(config);
1560 /* We *must* have a config file otherwise stop immediately */
1562 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
1566 /* fire up the H.323 Endpoint */
1567 if (!h323_end_point_exist()) {
1568 h323_end_point_create();
1571 dtmfmode = H323_DTMF_RFC2833;
1573 memset(&bindaddr, 0, sizeof(bindaddr));
1575 v = ast_variable_browse(cfg, "general");
1577 /* Create the interface list */
1578 if (!strcasecmp(v->name, "port")) {
1579 port = (int)strtol(v->value, NULL, 10);
1580 } else if (!strcasecmp(v->name, "bindaddr")) {
1581 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
1582 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
1584 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
1586 } else if (!strcasecmp(v->name, "allow")) {
1587 format = ast_getformatbyname(v->value);
1589 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
1591 capability |= format;
1592 } else if (!strcasecmp(v->name, "disallow")) {
1593 format = ast_getformatbyname(v->value);
1595 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
1597 capability &= ~format;
1598 } else if (!strcasecmp(v->name, "tos")) {
1599 if (sscanf(v->value, "%i", &format) == 1)
1600 tos = format & 0xff;
1601 else if (!strcasecmp(v->value, "lowdelay"))
1602 tos = IPTOS_LOWDELAY;
1603 else if (!strcasecmp(v->value, "throughput"))
1604 tos = IPTOS_THROUGHPUT;
1605 else if (!strcasecmp(v->value, "reliability"))
1606 tos = IPTOS_RELIABILITY;
1607 else if (!strcasecmp(v->value, "mincost"))
1608 tos = IPTOS_MINCOST;
1609 else if (!strcasecmp(v->value, "none"))
1612 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
1613 } else if (!strcasecmp(v->name, "gatekeeper")) {
1614 if (!strcasecmp(v->value, "DISABLE")) {
1615 gatekeeper_disable = 1;
1617 } else if (!strcasecmp(v->value, "DISCOVER")) {
1618 gatekeeper_disable = 0;
1619 gatekeeper_discover = 1;
1622 gatekeeper_disable = 0;
1624 strncpy(gatekeeper, v->value, sizeof(gatekeeper)-1);
1626 } else if (!strcasecmp(v->name, "secret")) {
1627 strncpy(secret, v->value, sizeof(secret)-1);
1628 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
1629 gkroute = ast_true(v->value);
1630 } else if (!strcasecmp(v->name, "context")) {
1631 strncpy(default_context, v->value, sizeof(default_context)-1);
1632 ast_verbose(VERBOSE_PREFIX_3 " == Setting default context to %s\n", default_context);
1633 } else if (!strcasecmp(v->name, "dtmfmode")) {
1634 if (!strcasecmp(v->value, "inband"))
1635 dtmfmode=H323_DTMF_INBAND;
1636 else if (!strcasecmp(v->value, "rfc2833"))
1637 dtmfmode = H323_DTMF_RFC2833;
1639 ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
1640 dtmfmode = H323_DTMF_RFC2833;
1642 } else if (!strcasecmp(v->name, "UserByAlias")) {
1643 userbyalias = ast_true(v->value);
1644 } else if (!strcasecmp(v->name, "bridge")) {
1645 bridge_default = ast_true(v->value);
1650 cat = ast_category_browse(cfg, NULL);
1652 if (strcasecmp(cat, "general")) {
1653 utype = ast_variable_retrieve(cfg, cat, "type");
1655 if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
1656 user = build_user(cat, ast_variable_browse(cfg, cat));
1658 ast_mutex_lock(&userl.lock);
1659 user->next = userl.users;
1661 ast_mutex_unlock(&userl.lock);
1663 } else if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
1664 peer = build_peer(cat, ast_variable_browse(cfg, cat));
1666 ast_mutex_lock(&peerl.lock);
1667 peer->next = peerl.peers;
1669 ast_mutex_unlock(&peerl.lock);
1671 } else if (!strcasecmp(utype, "h323")) {
1672 alias = build_alias(cat, ast_variable_browse(cfg, cat));
1674 ast_mutex_lock(&aliasl.lock);
1675 alias->next = aliasl.aliases;
1676 aliasl.aliases = alias;
1677 ast_mutex_unlock(&aliasl.lock);
1680 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
1683 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
1685 cat = ast_category_browse(cfg, cat);
1688 /* Register our H.323 aliases if any*/
1690 if (h323_set_alias(alias)) {
1691 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
1694 alias = alias->next;
1697 /* Add some capabilities */
1698 if(h323_set_capability(capability, dtmfmode)) {
1699 ast_log(LOG_ERROR, "Capabilities failure, this is bad.\n");
1707 void delete_users(void)
1709 struct oh323_user *user, *userlast;
1710 struct oh323_peer *peer;
1712 /* Delete all users */
1713 ast_mutex_lock(&userl.lock);
1714 for (user=userl.users;user;) {
1720 ast_mutex_unlock(&userl.lock);
1721 ast_mutex_lock(&peerl.lock);
1722 for (peer=peerl.peers;peer;) {
1723 /* Assume all will be deleted, and we'll find out for sure later */
1727 ast_mutex_unlock(&peerl.lock);
1730 void delete_aliases(void)
1732 struct oh323_alias *alias, *aliaslast;
1734 /* Delete all users */
1735 ast_mutex_lock(&aliasl.lock);
1736 for (alias=aliasl.aliases;alias;) {
1741 aliasl.aliases=NULL;
1742 ast_mutex_unlock(&aliasl.lock);
1745 void prune_peers(void)
1747 /* Prune peers who still are supposed to be deleted */
1748 struct oh323_peer *peer, *peerlast, *peernext;
1749 ast_mutex_lock(&peerl.lock);
1751 for (peer=peerl.peers;peer;) {
1752 peernext = peer->next;
1756 peerlast->next = peernext;
1758 peerl.peers = peernext;
1763 ast_mutex_unlock(&peerl.lock);
1773 if (!ast_strlen_zero(gatekeeper)) {
1781 /* Possibly register with a GK */
1782 if (gatekeeper_disable == 0) {
1783 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1784 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1795 static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
1797 struct oh323_pvt *p;
1798 p = (struct oh323_pvt *) chan->pvt->pvt;
1799 if (p && p->rtp && p->bridge) {
1805 static struct ast_rtp *oh323_get_vrtp_peer(struct ast_channel *chan)
1810 static char *convertcap(int cap)
1813 case AST_FORMAT_G723_1:
1815 case AST_FORMAT_GSM:
1817 case AST_FORMAT_ULAW:
1819 case AST_FORMAT_ALAW:
1821 case AST_FORMAT_ADPCM:
1823 case AST_FORMAT_G729A:
1825 case AST_FORMAT_SPEEX:
1827 case AST_FORMAT_ILBC:
1830 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
1836 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs)
1838 /* XXX Deal with Video */
1839 struct oh323_pvt *p;
1840 struct sockaddr_in them;
1841 struct sockaddr_in us;
1843 char iabuf[INET_ADDRSTRLEN];
1845 mode = convertcap(chan->writeformat);
1851 p = (struct oh323_pvt *) chan->pvt->pvt;
1853 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
1857 ast_rtp_get_peer(rtp, &them);
1858 ast_rtp_get_us(rtp, &us);
1860 h323_native_bridge(p->cd.call_token, ast_inet_ntoa(iabuf, sizeof(iabuf), them.sin_addr), mode);
1866 static struct ast_rtp_protocol oh323_rtp = {
1867 get_rtp_info: oh323_get_rtp_peer,
1868 get_vrtp_info: oh323_get_vrtp_peer,
1869 set_rtp_peer: oh323_set_rtp_peer,
1876 ast_mutex_init(&userl.lock);
1877 ast_mutex_init(&peerl.lock);
1878 ast_mutex_init(&aliasl.lock);
1880 res = reload_config();
1885 /* Make sure we can register our channel type */
1886 if (ast_channel_register(type, tdesc, capability, oh323_request)) {
1887 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1891 ast_cli_register(&cli_debug);
1892 ast_cli_register(&cli_no_debug);
1893 ast_cli_register(&cli_trace);
1894 ast_cli_register(&cli_no_trace);
1895 ast_cli_register(&cli_show_codecs);
1896 ast_cli_register(&cli_gk_cycle);
1897 ast_cli_register(&cli_hangup_call);
1898 ast_cli_register(&cli_show_tokens);
1900 oh323_rtp.type = type;
1901 ast_rtp_proto_register(&oh323_rtp);
1903 sched = sched_context_create();
1905 ast_log(LOG_WARNING, "Unable to create schedule context\n");
1907 io = io_context_create();
1909 ast_log(LOG_WARNING, "Unable to create I/O context\n");
1912 /* Register our callback functions */
1913 h323_callback_register(setup_incoming_call,
1914 setup_outgoing_call,
1916 setup_rtp_connection,
1924 /* start the h.323 listener */
1925 if (h323_start_listener(port, bindaddr)) {
1926 ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
1930 /* Possibly register with a GK */
1931 if (gatekeeper_disable == 0) {
1932 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1933 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1937 /* And start the monitor for the first time */
1946 struct oh323_pvt *p, *pl;
1948 if (!ast_mutex_lock(&iflock)) {
1949 /* hangup all interfaces if they have an owner */
1953 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1957 ast_mutex_unlock(&iflock);
1959 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1963 if (!ast_mutex_lock(&monlock)) {
1964 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
1965 pthread_cancel(monitor_thread);
1966 pthread_kill(monitor_thread, SIGURG);
1967 pthread_join(monitor_thread, NULL);
1969 monitor_thread = AST_PTHREADT_STOP;
1970 ast_mutex_unlock(&monlock);
1972 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1976 if (!ast_mutex_lock(&iflock)) {
1977 /* destroy all the interfaces and free their memory */
1982 /* free associated memory */
1983 ast_mutex_destroy(&pl->lock);
1987 ast_mutex_unlock(&iflock);
1989 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1995 /* unregister rtp */
1996 ast_rtp_proto_unregister(&oh323_rtp);
1998 /* unregister commands */
1999 ast_cli_unregister(&cli_debug);
2000 ast_cli_unregister(&cli_no_debug);
2001 ast_cli_unregister(&cli_trace);
2002 ast_cli_unregister(&cli_no_trace);
2003 ast_cli_unregister(&cli_show_codecs);
2004 ast_cli_unregister(&cli_gk_cycle);
2005 ast_cli_unregister(&cli_hangup_call);
2006 ast_cli_unregister(&cli_show_tokens);
2008 /* unregister channel type */
2009 ast_channel_unregister(type);
2017 ast_mutex_lock(&usecnt_lock);
2019 ast_mutex_unlock(&usecnt_lock);
2030 return ASTERISK_GPL_KEY;