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));
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;
885 /* pass on our preferred codec to the H.323 stack */
886 h323_set_capability(format, dtmfmode);
889 strncpy(p->username, ext, sizeof(p->username) - 1);
891 ast_log(LOG_DEBUG, "Host: %s\tUsername: %s\n", host, p->username);
893 tmpc = oh323_new(p, AST_STATE_DOWN, host);
902 struct oh323_alias *find_alias(const char *source_aliases)
904 struct oh323_alias *a;
910 if (!strcasecmp(a->name, source_aliases)) {
918 struct oh323_user *find_user(const call_details_t cd)
920 struct oh323_user *u;
921 char iabuf[INET_ADDRSTRLEN];
923 if(userbyalias == 1){
925 if (!strcasecmp(u->name, cd.call_source_aliases)) {
933 if (!strcasecmp(cd.sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), u->addr.sin_addr))) {
945 struct oh323_peer *find_peer(char *dest_peer)
947 struct oh323_peer *p;
952 if (!strcasecmp(p->name, dest_peer)) {
962 * Callback for sending digits from H.323 up to asterisk
965 int send_digit(unsigned call_reference, char digit)
970 ast_log(LOG_DEBUG, "Recieved Digit: %c\n", digit);
971 p = find_call(call_reference);
974 ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
977 memset(&f, 0, sizeof(f));
978 f.frametype = AST_FRAME_DTMF;
985 f.src = "SEND_DIGIT";
987 return ast_queue_frame(p->owner, &f);
991 * Call-back function that gets called when any H.323 connection is made
993 * Returns the local RTP information
995 struct rtp_info *create_connection(unsigned call_reference)
998 struct sockaddr_in us;
999 struct sockaddr_in them;
1000 struct rtp_info *info;
1001 /* XXX This is sooooo bugus. inet_ntoa is not reentrant
1002 but this function wants to return a static variable so
1003 the only way to do this will be to declare iabuf within
1004 the oh323_pvt structure XXX */
1005 static char iabuf[INET_ADDRSTRLEN];
1007 info = (struct rtp_info *) malloc(sizeof(struct rtp_info));
1009 p = find_call(call_reference);
1012 ast_log(LOG_ERROR, "Unable to allocate private structure, this is very bad.\n");
1016 /* figure out our local RTP port and tell the H.323 stack about it*/
1017 ast_rtp_get_us(p->rtp, &us);
1018 ast_rtp_get_peer(p->rtp, &them);
1020 info->addr = ast_inet_ntoa(iabuf, sizeof(iabuf), us.sin_addr);
1021 info->port = ntohs(us.sin_port);
1027 * Call-back function for incoming calls
1029 * Returns 1 on success
1032 int setup_incoming_call(call_details_t cd)
1035 struct oh323_pvt *p = NULL;
1036 /* struct ast_channel *c = NULL; */
1037 struct oh323_user *user = NULL;
1038 struct oh323_alias *alias = NULL;
1039 char iabuf[INET_ADDRSTRLEN];
1041 /* allocate the call*/
1042 p = oh323_alloc(cd.call_reference);
1045 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
1049 /* Populate the call details in the private structure */
1050 p->cd.call_token = cd.call_token;
1051 p->cd.call_source_aliases = cd.call_source_aliases;
1052 p->cd.call_dest_alias = cd.call_dest_alias;
1053 p->cd.call_source_name = cd.call_source_name;
1054 p->cd.call_source_e164 = cd.call_source_e164;
1055 p->cd.call_dest_e164 = cd.call_dest_e164;
1058 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
1059 ast_verbose(VERBOSE_PREFIX_3 " Call token: [%s]\n", p->cd.call_token);
1060 ast_verbose(VERBOSE_PREFIX_3 " Calling party name: [%s]\n", p->cd.call_source_name);
1061 ast_verbose(VERBOSE_PREFIX_3 " Calling party number: [%s]\n", p->cd.call_source_e164);
1062 ast_verbose(VERBOSE_PREFIX_3 " Called party name: [%s]\n", p->cd.call_dest_alias);
1063 ast_verbose(VERBOSE_PREFIX_3 " Called party number: [%s]\n", p->cd.call_dest_e164);
1066 /* Decide if we are allowing Gatekeeper routed calls*/
1067 if ((!strcasecmp(cd.sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk == 1)) {
1069 if (!ast_strlen_zero(cd.call_dest_e164)) {
1070 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1071 strncpy(p->context, default_context, sizeof(p->context)-1);
1073 alias = find_alias(cd.call_dest_alias);
1076 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd.call_dest_alias);
1079 strncpy(p->exten, alias->name, sizeof(p->exten)-1);
1080 strncpy(p->context, alias->context, sizeof(p->context)-1);
1082 sprintf(p->callerid, "%s <%s>", p->cd.call_source_name, p->cd.call_source_e164);
1084 /* Either this call is not from the Gatekeeper
1085 or we are not allowing gk routed calls */
1086 user = find_user(cd);
1089 sprintf(p->callerid, "%s <%s>", p->cd.call_source_name, p->cd.call_source_e164);
1090 if (!ast_strlen_zero(p->cd.call_dest_e164)) {
1091 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1093 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
1095 if (ast_strlen_zero(default_context)) {
1096 ast_log(LOG_ERROR, "Call from '%s' rejected due to no default context\n", p->cd.call_source_aliases);
1099 strncpy(p->context, default_context, sizeof(p->context)-1);
1100 ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd.call_source_aliases, p->context);
1103 if (strcasecmp(cd.sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), user->addr.sin_addr))){
1104 if (ast_strlen_zero(user->context)) {
1105 if (ast_strlen_zero(default_context)) {
1106 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd.sourceIp);
1109 strncpy(p->context, default_context, sizeof(p->context)-1);
1111 strncpy(p->context, user->context, sizeof(p->context)-1);
1113 sprintf(p->exten, "i");
1114 ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd.sourceIp);
1118 if (user->incominglimit > 0) {
1119 if (user->inUse >= user->incominglimit) {
1120 ast_log(LOG_ERROR, "Call from user '%s' rejected due to usage limit of %d\n", user->name, user->incominglimit);
1124 strncpy(p->context, user->context, sizeof(p->context)-1);
1125 p->bridge = user->bridge;
1128 if (!ast_strlen_zero(user->callerid)) {
1129 strncpy(p->callerid, user->callerid, sizeof(p->callerid) - 1);
1131 sprintf(p->callerid, "%s <%s>", p->cd.call_source_name, p->cd.call_source_e164);
1133 if (!ast_strlen_zero(p->cd.call_dest_e164)) {
1134 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1136 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
1138 if (!ast_strlen_zero(user->accountcode)) {
1139 strncpy(p->accountcode, user->accountcode, sizeof(p->accountcode)-1);
1142 /* Increment the usage counter */
1149 /* allocate a channel and tell asterisk about it */
1150 c = oh323_new(p, AST_STATE_RINGING, cd.call_token);
1152 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
1160 * Call-back function to start PBX when OpenH323 ready to serve incoming call
1162 * Returns 1 on success
1164 static int answer_call(unsigned call_reference)
1166 struct oh323_pvt *p = NULL;
1167 struct ast_channel *c = NULL;
1169 /* Find the call or allocate a private structure if call not found */
1170 p = find_call(call_reference);
1173 ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
1177 /* allocate a channel and tell asterisk about it */
1178 c = oh323_new(p, AST_STATE_RINGING, p->cd.call_token);
1180 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
1187 * Call-back function to establish an outgoing H.323 call
1189 * Returns 1 on success
1191 int setup_outgoing_call(call_details_t cd)
1197 if (p->inUse >= p->outgoinglimit) {
1198 ast_log(LOG_ERROR, "Call to %s rejected due to usage limit of %d outgoing channels\n", p->name, p->inUse);
1203 ast_log(LOG_ERROR, "Rejecting call: peer %s not found\n", dest_peer);
1209 * Call-back function that gets called for each rtp channel opened
1213 void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort)
1215 struct oh323_pvt *p = NULL;
1216 struct sockaddr_in them;
1218 /* Find the call or allocate a private structure if call not found */
1219 p = find_call(call_reference);
1222 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1226 them.sin_family = AF_INET;
1227 them.sin_addr.s_addr = inet_addr(remoteIp); // only works for IPv4
1228 them.sin_port = htons(remotePort);
1229 ast_rtp_set_peer(p->rtp, &them);
1235 * Call-back function to signal asterisk that the channel has been answered
1238 void connection_made(unsigned call_reference)
1240 struct ast_channel *c = NULL;
1241 struct oh323_pvt *p = NULL;
1243 p = find_call(call_reference);
1246 ast_log(LOG_ERROR, "Something is wrong: connection\n");
1250 ast_log(LOG_ERROR, "Channel has no owner\n");
1255 ast_setstate(c, AST_STATE_UP);
1256 ast_queue_control(c, AST_CONTROL_ANSWER);
1261 * Call-back function to signal asterisk that the channel is ringing
1264 void chan_ringing(unsigned call_reference)
1266 struct ast_channel *c = NULL;
1267 struct oh323_pvt *p = NULL;
1269 p = find_call(call_reference);
1272 ast_log(LOG_ERROR, "Something is wrong: ringing\n");
1276 ast_log(LOG_ERROR, "Channel has no owner\n");
1280 ast_setstate(c, AST_STATE_RINGING);
1281 ast_queue_control(c, AST_CONTROL_RINGING);
1286 * Call-back function to cleanup communication
1289 void cleanup_connection(call_details_t cd)
1291 struct oh323_pvt *p = NULL;
1292 /* struct oh323_peer *peer = NULL; */
1293 struct oh323_user *user = NULL;
1294 struct ast_rtp *rtp = NULL;
1296 p = find_call(cd.call_reference);
1301 ast_mutex_lock(&p->lock);
1303 /* Decrement usage counter */
1305 user = find_user(cd);
1314 peer = find_peer(cd.call_dest_alias);
1317 user = find_user(cd);
1325 /* Immediately stop RTP */
1326 ast_rtp_destroy(rtp);
1333 ast_queue_hangup(p->owner);
1336 ast_mutex_unlock(&p->lock);
1340 static void *do_monitor(void *data)
1343 struct oh323_pvt *oh323 = NULL;
1346 /* Check for interfaces needing to be killed */
1347 ast_mutex_lock(&iflock);
1351 if (oh323->needdestroy) {
1352 __oh323_destroy(oh323);
1355 oh323 = oh323->next;
1357 ast_mutex_unlock(&iflock);
1359 /* Wait for sched or io */
1360 res = ast_sched_wait(sched);
1361 if ((res < 0) || (res > 1000))
1363 res = ast_io_wait(io, res);
1365 pthread_testcancel();
1367 ast_mutex_lock(&monlock);
1369 ast_sched_runq(sched);
1370 ast_mutex_unlock(&monlock);
1377 static int restart_monitor(void)
1379 /* If we're supposed to be stopped -- stay stopped */
1380 if (monitor_thread == AST_PTHREADT_STOP)
1382 if (ast_mutex_lock(&monlock)) {
1383 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1386 if (monitor_thread == pthread_self()) {
1387 ast_mutex_unlock(&monlock);
1388 ast_log(LOG_WARNING, "Cannot kill myself\n");
1391 if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
1392 /* Wake up the thread */
1393 pthread_kill(monitor_thread, SIGURG);
1395 /* Start a new monitor */
1396 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1397 ast_mutex_unlock(&monlock);
1398 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1402 ast_mutex_unlock(&monlock);
1406 static int h323_do_trace(int fd, int argc, char *argv[])
1409 return RESULT_SHOWUSAGE;
1411 h323_debug(1, atoi(argv[2]));
1412 ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
1413 return RESULT_SUCCESS;
1416 static int h323_no_trace(int fd, int argc, char *argv[])
1419 return RESULT_SHOWUSAGE;
1422 ast_cli(fd, "H.323 trace disabled\n");
1423 return RESULT_SUCCESS;
1426 static int h323_do_debug(int fd, int argc, char *argv[])
1429 return RESULT_SHOWUSAGE;
1432 ast_cli(fd, "H323 debug enabled\n");
1433 return RESULT_SUCCESS;
1436 static int h323_no_debug(int fd, int argc, char *argv[])
1439 return RESULT_SHOWUSAGE;
1442 ast_cli(fd, "H323 Debug disabled\n");
1443 return RESULT_SUCCESS;
1446 static int h323_gk_cycle(int fd, int argc, char *argv[])
1448 return RESULT_SUCCESS;
1451 return RESULT_SHOWUSAGE;
1455 /* Possibly register with a GK */
1456 if (!gatekeeper_disable) {
1457 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1458 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1461 return RESULT_SUCCESS;
1465 static int h323_ep_hangup(int fd, int argc, char *argv[])
1469 return RESULT_SHOWUSAGE;
1472 if (h323_soft_hangup(argv[2])) {
1473 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
1475 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
1478 return RESULT_SUCCESS;
1481 static int h323_tokens_show(int fd, int argc, char *argv[])
1485 return RESULT_SHOWUSAGE;
1489 return RESULT_SUCCESS;
1493 static char trace_usage[] =
1494 "Usage: h.323 trace <level num>\n"
1495 " Enables H.323 stack tracing for debugging purposes\n";
1497 static char no_trace_usage[] =
1498 "Usage: h.323 no trace\n"
1499 " Disables H.323 stack tracing for debugging purposes\n";
1501 static char debug_usage[] =
1502 "Usage: h.323 debug\n"
1503 " Enables chan_h323 debug output\n";
1505 static char no_debug_usage[] =
1506 "Usage: h.323 no debug\n"
1507 " Disables chan_h323 debug output\n";
1509 static char show_codec_usage[] =
1510 "Usage: h.323 show codec\n"
1511 " Shows all enabled codecs\n";
1513 static char show_cycle_usage[] =
1514 "Usage: h.323 gk cycle\n"
1515 " Manually re-register with the Gatekeper\n";
1517 static char show_hangup_usage[] =
1518 "Usage: h.323 hangup <token>\n"
1519 " Manually try to hang up call identified by <token>\n";
1521 static char show_tokens_usage[] =
1522 "Usage: h.323 show tokens\n"
1523 " Print out all active call tokens\n";
1525 static struct ast_cli_entry cli_trace =
1526 { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
1527 static struct ast_cli_entry cli_no_trace =
1528 { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
1529 static struct ast_cli_entry cli_debug =
1530 { { "h.323", "debug", NULL }, h323_do_debug, "Enable chan_h323 debug", debug_usage };
1531 static struct ast_cli_entry cli_no_debug =
1532 { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable chan_h323 debug", no_debug_usage };
1533 static struct ast_cli_entry cli_show_codecs =
1534 { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
1535 static struct ast_cli_entry cli_gk_cycle =
1536 { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
1537 static struct ast_cli_entry cli_hangup_call =
1538 { { "h.323", "hangup", NULL }, h323_ep_hangup, "Show all active call tokens", show_hangup_usage };
1539 static struct ast_cli_entry cli_show_tokens =
1540 { { "h.323", "show", "tokens", NULL }, h323_tokens_show, "Manually try to hang up a call", show_tokens_usage };
1544 int reload_config(void)
1548 struct ast_config *cfg;
1549 struct ast_variable *v;
1550 struct oh323_peer *peer = NULL;
1551 struct oh323_user *user = NULL;
1552 struct oh323_alias *alias = NULL;
1553 struct ast_hostent ahp; struct hostent *hp;
1557 cfg = ast_load(config);
1559 /* We *must* have a config file otherwise stop immediately */
1561 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
1565 /* fire up the H.323 Endpoint */
1566 if (!h323_end_point_exist()) {
1567 h323_end_point_create();
1570 dtmfmode = H323_DTMF_RFC2833;
1572 memset(&bindaddr, 0, sizeof(bindaddr));
1574 v = ast_variable_browse(cfg, "general");
1576 /* Create the interface list */
1577 if (!strcasecmp(v->name, "port")) {
1578 port = (int)strtol(v->value, NULL, 10);
1579 } else if (!strcasecmp(v->name, "bindaddr")) {
1580 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
1581 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
1583 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
1585 } else if (!strcasecmp(v->name, "allow")) {
1586 format = ast_getformatbyname(v->value);
1588 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
1590 capability |= format;
1591 } else if (!strcasecmp(v->name, "disallow")) {
1592 format = ast_getformatbyname(v->value);
1594 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
1596 capability &= ~format;
1597 } else if (!strcasecmp(v->name, "tos")) {
1598 if (sscanf(v->value, "%i", &format) == 1)
1599 tos = format & 0xff;
1600 else if (!strcasecmp(v->value, "lowdelay"))
1601 tos = IPTOS_LOWDELAY;
1602 else if (!strcasecmp(v->value, "throughput"))
1603 tos = IPTOS_THROUGHPUT;
1604 else if (!strcasecmp(v->value, "reliability"))
1605 tos = IPTOS_RELIABILITY;
1606 else if (!strcasecmp(v->value, "mincost"))
1607 tos = IPTOS_MINCOST;
1608 else if (!strcasecmp(v->value, "none"))
1611 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
1612 } else if (!strcasecmp(v->name, "gatekeeper")) {
1613 if (!strcasecmp(v->value, "DISABLE")) {
1614 gatekeeper_disable = 1;
1616 } else if (!strcasecmp(v->value, "DISCOVER")) {
1617 gatekeeper_disable = 0;
1618 gatekeeper_discover = 1;
1621 gatekeeper_disable = 0;
1623 strncpy(gatekeeper, v->value, sizeof(gatekeeper)-1);
1625 } else if (!strcasecmp(v->name, "secret")) {
1626 strncpy(secret, v->value, sizeof(secret)-1);
1627 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
1628 gkroute = ast_true(v->value);
1629 } else if (!strcasecmp(v->name, "context")) {
1630 strncpy(default_context, v->value, sizeof(default_context)-1);
1631 ast_verbose(VERBOSE_PREFIX_3 " == Setting default context to %s\n", default_context);
1632 } else if (!strcasecmp(v->name, "dtmfmode")) {
1633 if (!strcasecmp(v->value, "inband"))
1634 dtmfmode=H323_DTMF_INBAND;
1635 else if (!strcasecmp(v->value, "rfc2833"))
1636 dtmfmode = H323_DTMF_RFC2833;
1638 ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
1639 dtmfmode = H323_DTMF_RFC2833;
1641 } else if (!strcasecmp(v->name, "UserByAlias")) {
1642 userbyalias = ast_true(v->value);
1643 } else if (!strcasecmp(v->name, "bridge")) {
1644 bridge_default = ast_true(v->value);
1649 cat = ast_category_browse(cfg, NULL);
1651 if (strcasecmp(cat, "general")) {
1652 utype = ast_variable_retrieve(cfg, cat, "type");
1654 if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
1655 user = build_user(cat, ast_variable_browse(cfg, cat));
1657 ast_mutex_lock(&userl.lock);
1658 user->next = userl.users;
1660 ast_mutex_unlock(&userl.lock);
1662 } else if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
1663 peer = build_peer(cat, ast_variable_browse(cfg, cat));
1665 ast_mutex_lock(&peerl.lock);
1666 peer->next = peerl.peers;
1668 ast_mutex_unlock(&peerl.lock);
1670 } else if (!strcasecmp(utype, "h323")) {
1671 alias = build_alias(cat, ast_variable_browse(cfg, cat));
1673 ast_mutex_lock(&aliasl.lock);
1674 alias->next = aliasl.aliases;
1675 aliasl.aliases = alias;
1676 ast_mutex_unlock(&aliasl.lock);
1679 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
1682 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
1684 cat = ast_category_browse(cfg, cat);
1687 /* Register our H.323 aliases if any*/
1689 if (h323_set_alias(alias)) {
1690 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
1693 alias = alias->next;
1696 /* Add some capabilities */
1697 if(h323_set_capability(capability, dtmfmode)) {
1698 ast_log(LOG_ERROR, "Capabilities failure, this is bad.\n");
1706 void delete_users(void)
1708 struct oh323_user *user, *userlast;
1709 struct oh323_peer *peer;
1711 /* Delete all users */
1712 ast_mutex_lock(&userl.lock);
1713 for (user=userl.users;user;) {
1719 ast_mutex_unlock(&userl.lock);
1720 ast_mutex_lock(&peerl.lock);
1721 for (peer=peerl.peers;peer;) {
1722 /* Assume all will be deleted, and we'll find out for sure later */
1726 ast_mutex_unlock(&peerl.lock);
1729 void delete_aliases(void)
1731 struct oh323_alias *alias, *aliaslast;
1733 /* Delete all users */
1734 ast_mutex_lock(&aliasl.lock);
1735 for (alias=aliasl.aliases;alias;) {
1740 aliasl.aliases=NULL;
1741 ast_mutex_unlock(&aliasl.lock);
1744 void prune_peers(void)
1746 /* Prune peers who still are supposed to be deleted */
1747 struct oh323_peer *peer, *peerlast, *peernext;
1748 ast_mutex_lock(&peerl.lock);
1750 for (peer=peerl.peers;peer;) {
1751 peernext = peer->next;
1755 peerlast->next = peernext;
1757 peerl.peers = peernext;
1762 ast_mutex_unlock(&peerl.lock);
1772 if (!ast_strlen_zero(gatekeeper)) {
1780 /* Possibly register with a GK */
1781 if (gatekeeper_disable == 0) {
1782 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1783 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1794 static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
1796 struct oh323_pvt *p;
1797 p = (struct oh323_pvt *) chan->pvt->pvt;
1798 if (p && p->rtp && p->bridge) {
1804 static struct ast_rtp *oh323_get_vrtp_peer(struct ast_channel *chan)
1809 static char *convertcap(int cap)
1812 case AST_FORMAT_G723_1:
1814 case AST_FORMAT_GSM:
1816 case AST_FORMAT_ULAW:
1818 case AST_FORMAT_ALAW:
1820 case AST_FORMAT_ADPCM:
1822 case AST_FORMAT_G729A:
1824 case AST_FORMAT_SPEEX:
1826 case AST_FORMAT_ILBC:
1829 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
1835 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs)
1837 /* XXX Deal with Video */
1838 struct oh323_pvt *p;
1839 struct sockaddr_in them;
1840 struct sockaddr_in us;
1842 char iabuf[INET_ADDRSTRLEN];
1844 mode = convertcap(chan->writeformat);
1850 p = (struct oh323_pvt *) chan->pvt->pvt;
1852 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
1856 ast_rtp_get_peer(rtp, &them);
1857 ast_rtp_get_us(rtp, &us);
1859 h323_native_bridge(p->cd.call_token, ast_inet_ntoa(iabuf, sizeof(iabuf), them.sin_addr), mode);
1865 static struct ast_rtp_protocol oh323_rtp = {
1866 get_rtp_info: oh323_get_rtp_peer,
1867 get_vrtp_info: oh323_get_vrtp_peer,
1868 set_rtp_peer: oh323_set_rtp_peer,
1875 ast_mutex_init(&userl.lock);
1876 ast_mutex_init(&peerl.lock);
1877 ast_mutex_init(&aliasl.lock);
1879 res = reload_config();
1884 /* Make sure we can register our channel type */
1885 if (ast_channel_register(type, tdesc, capability, oh323_request)) {
1886 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1890 ast_cli_register(&cli_debug);
1891 ast_cli_register(&cli_no_debug);
1892 ast_cli_register(&cli_trace);
1893 ast_cli_register(&cli_no_trace);
1894 ast_cli_register(&cli_show_codecs);
1895 ast_cli_register(&cli_gk_cycle);
1896 ast_cli_register(&cli_hangup_call);
1897 ast_cli_register(&cli_show_tokens);
1899 oh323_rtp.type = type;
1900 ast_rtp_proto_register(&oh323_rtp);
1902 sched = sched_context_create();
1904 ast_log(LOG_WARNING, "Unable to create schedule context\n");
1906 io = io_context_create();
1908 ast_log(LOG_WARNING, "Unable to create I/O context\n");
1911 /* Register our callback functions */
1912 h323_callback_register(setup_incoming_call,
1913 setup_outgoing_call,
1915 setup_rtp_connection,
1923 /* start the h.323 listener */
1924 if (h323_start_listener(port, bindaddr)) {
1925 ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
1929 /* Possibly register with a GK */
1930 if (gatekeeper_disable == 0) {
1931 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1932 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1936 /* And start the monitor for the first time */
1945 struct oh323_pvt *p, *pl;
1947 if (!ast_mutex_lock(&iflock)) {
1948 /* hangup all interfaces if they have an owner */
1952 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1956 ast_mutex_unlock(&iflock);
1958 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1962 if (!ast_mutex_lock(&monlock)) {
1963 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
1964 pthread_cancel(monitor_thread);
1965 pthread_kill(monitor_thread, SIGURG);
1966 pthread_join(monitor_thread, NULL);
1968 monitor_thread = AST_PTHREADT_STOP;
1969 ast_mutex_unlock(&monlock);
1971 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1975 if (!ast_mutex_lock(&iflock)) {
1976 /* destroy all the interfaces and free their memory */
1981 /* free associated memory */
1982 ast_mutex_destroy(&pl->lock);
1986 ast_mutex_unlock(&iflock);
1988 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1994 /* unregister rtp */
1995 ast_rtp_proto_unregister(&oh323_rtp);
1997 /* unregister commands */
1998 ast_cli_unregister(&cli_debug);
1999 ast_cli_unregister(&cli_no_debug);
2000 ast_cli_unregister(&cli_trace);
2001 ast_cli_unregister(&cli_no_trace);
2002 ast_cli_unregister(&cli_show_codecs);
2003 ast_cli_unregister(&cli_gk_cycle);
2004 ast_cli_unregister(&cli_hangup_call);
2005 ast_cli_unregister(&cli_show_tokens);
2007 /* unregister channel type */
2008 ast_channel_unregister(type);
2016 ast_mutex_lock(&usecnt_lock);
2018 ast_mutex_unlock(&usecnt_lock);
2029 return ASTERISK_GPL_KEY;