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.
33 #include <asterisk/lock.h>
34 #include <asterisk/logger.h>
35 #include <asterisk/channel.h>
36 #include <asterisk/channel_pvt.h>
37 #include <asterisk/config.h>
38 #include <asterisk/module.h>
39 #include <asterisk/pbx.h>
40 #include <asterisk/options.h>
41 #include <asterisk/lock.h>
42 #include <asterisk/sched.h>
43 #include <asterisk/io.h>
44 #include <asterisk/rtp.h>
45 #include <asterisk/acl.h>
46 #include <asterisk/callerid.h>
47 #include <asterisk/cli.h>
48 #include <asterisk/dsp.h>
49 #include <sys/socket.h>
56 #include <sys/signal.h>
58 #include <netinet/in_systm.h>
60 #define IPTOS_MINCOST 0x02
63 #include <netinet/ip.h>
65 #include "h323/chan_h323.h"
67 /** String variables required by ASTERISK */
68 static char *type = "H323";
69 static char *desc = "The NuFone Network's Open H.323 Channel Driver";
70 static char *tdesc = "The NuFone Network's Open H.323 Channel Driver";
71 static char *config = "h323.conf";
73 static char default_context[AST_MAX_EXTENSION];
75 /** H.323 configuration values */
76 static char gatekeeper[100];
77 static int gatekeeper_disable = 1;
78 static int gatekeeper_discover = 0;
80 static int port = 1720;
81 static int gkroute = 0;
83 /* to find user by alias is default, alternative is the incomming call's source IP address*/
84 static int userbyalias = 1;
86 static int bridge_default = 1;
88 /* Just about everybody seems to support ulaw, so make it a nice default */
89 static int capability = AST_FORMAT_ULAW;
94 static int dtmfmode = H323_DTMF_RFC2833;
96 static char secret[50];
98 /** Private structure of a OpenH323 channel */
100 ast_mutex_t lock; /* Channel private lock */
101 call_options_t calloptions; /* Options to be used during call setup */
102 int alreadygone; /* Whether or not we've already been destroyed by or peer */
103 int needdestroy; /* if we need to be destroyed */
104 call_details_t cd; /* Call details */
105 struct ast_channel *owner; /* Who owns us */
106 int capability; /* audio capability */
107 int nonCodecCapability; /* non-audio capability */
108 int outgoing; /* Outgoing or incoming call? */
109 int nat; /* Are we talking to a NAT EP?*/
110 int bridge; /* Determine of we should native bridge or not*/
111 char exten[AST_MAX_EXTENSION]; /* Requested extension */
112 char context[AST_MAX_EXTENSION]; /* Context where to start */
113 char username[81]; /* H.323 alias using this channel */
114 char accountcode[256]; /* Account code */
115 int amaflags; /* AMA Flags */
116 char callerid[80]; /* Caller*ID if available */
117 struct ast_rtp *rtp; /* RTP Session */
118 int dtmfmode; /* What DTMF Mode is being used */
119 struct ast_dsp *vad; /* Used for in-band DTMF detection */
120 struct oh323_pvt *next; /* Next channel in list */
123 static struct ast_user_list {
124 struct oh323_user *users;
126 } userl = { NULL, AST_MUTEX_INITIALIZER };
128 static struct ast_peer_list {
129 struct oh323_peer *peers;
131 } peerl = { NULL, AST_MUTEX_INITIALIZER };
133 static struct ast_alias_list {
134 struct oh323_alias *aliases;
136 } aliasl = { NULL, AST_MUTEX_INITIALIZER };
138 /** Asterisk RTP stuff*/
139 static struct sched_context *sched;
140 static struct io_context *io;
142 /** Protect the interface list (of oh323_pvt's) */
143 static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
145 /** Usage counter and associated lock */
146 static int usecnt =0;
147 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
149 /* Protect the monitoring thread, so only one process can kill or start it, and not
150 when it's doing something critical. */
151 static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
153 /* This is the thread for the monitor which checks for input on the channels
154 which are not currently in use. */
155 static pthread_t monitor_thread = AST_PTHREADT_NULL;
157 static int restart_monitor(void);
159 static void __oh323_destroy(struct oh323_pvt *p)
161 struct oh323_pvt *cur, *prev = NULL;
164 ast_rtp_destroy(p->rtp);
167 /* Unlink us from the owner if we have one */
169 ast_mutex_lock(&p->owner->lock);
170 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
171 p->owner->pvt->pvt = NULL;
172 ast_mutex_unlock(&p->owner->lock);
178 prev->next = cur->next;
187 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
192 static void oh323_destroy(struct oh323_pvt *p)
194 ast_mutex_lock(&iflock);
196 ast_mutex_unlock(&iflock);
199 static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
201 struct oh323_alias *alias;
203 alias = (struct oh323_alias *)malloc(sizeof(struct oh323_alias));
206 memset(alias, 0, sizeof(struct oh323_alias));
207 strncpy(alias->name, name, sizeof(alias->name)-1);
210 if (!strcasecmp(v->name, "e164")) {
211 strncpy(alias->e164, v->value, sizeof(alias->e164)-1);
212 } else if (!strcasecmp(v->name, "prefix")) {
213 strncpy(alias->prefix, v->value, sizeof(alias->prefix)-1);
214 } else if (!strcasecmp(v->name, "context")) {
215 strncpy(alias->context, v->value, sizeof(alias->context)-1);
216 } else if (!strcasecmp(v->name, "secret")) {
217 strncpy(alias->secret, v->value, sizeof(alias->secret)-1);
219 if (strcasecmp(v->value, "h323")) {
220 ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->value);
229 static struct oh323_user *build_user(char *name, struct ast_variable *v)
231 struct oh323_user *user;
234 user = (struct oh323_user *)malloc(sizeof(struct oh323_user));
236 memset(user, 0, sizeof(struct oh323_user));
237 strncpy(user->name, name, sizeof(user->name)-1);
239 /* set the usage flag to a sane starting value*/
241 /* Assume we can native bridge */
242 user->bridge = bridge_default;
245 if (!strcasecmp(v->name, "context")) {
246 strncpy(user->context, v->value, sizeof(user->context)-1);
247 } else if (!strcasecmp(v->name, "bridge")) {
248 user->bridge = ast_true(v->value);
249 } else if (!strcasecmp(v->name, "nat")) {
250 user->nat = ast_true(v->value);
251 } else if (!strcasecmp(v->name, "noFastStart")) {
252 user->noFastStart = ast_true(v->value);
253 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
254 user->noH245Tunneling = ast_true(v->value);
255 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
256 user->noSilenceSuppression = ast_true(v->value);
257 } else if (!strcasecmp(v->name, "secret")) {
258 strncpy(user->secret, v->value, sizeof(user->secret)-1);
259 } else if (!strcasecmp(v->name, "callerid")) {
260 strncpy(user->callerid, v->value, sizeof(user->callerid)-1);
261 } else if (!strcasecmp(v->name, "accountcode")) {
262 strncpy(user->accountcode, v->value, sizeof(user->accountcode)-1);
263 } else if (!strcasecmp(v->name, "incominglimit")) {
264 user->incominglimit = atoi(v->value);
265 if (user->incominglimit < 0)
266 user->incominglimit = 0;
267 } else if (!strcasecmp(v->name, "host")) {
268 if (!strcasecmp(v->value, "dynamic")) {
269 ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
272 } else if (ast_get_ip(&user->addr, v->value)) {
276 /* Let us know we need to use ip authentication */
278 } else if (!strcasecmp(v->name, "amaflags")) {
279 format = ast_cdr_amaflags2int(v->value);
281 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
283 user->amaflags = format;
293 static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
295 struct oh323_peer *peer;
296 struct oh323_peer *prev;
300 ast_mutex_lock(&peerl.lock);
304 if (!strcasecmp(peer->name, name)) {
313 /* Already in the list, remove it and it will be added back (or FREE'd) */
315 prev->next = peer->next;
317 peerl.peers = peer->next;
319 ast_mutex_unlock(&peerl.lock);
321 ast_mutex_unlock(&peerl.lock);
322 peer = malloc(sizeof(struct oh323_peer));
323 memset(peer, 0, sizeof(struct oh323_peer));
327 strncpy(peer->name, name, sizeof(peer->name)-1);
330 /* set the usage flag to a sane starting value*/
334 if (!strcasecmp(v->name, "context")) {
335 strncpy(peer->context, v->value, sizeof(peer->context)-1);
336 } else if (!strcasecmp(v->name, "bridge")) {
337 peer->bridge = ast_true(v->value);
338 } else if (!strcasecmp(v->name, "noFastStart")) {
339 peer->noFastStart = ast_true(v->value);
340 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
341 peer->noH245Tunneling = ast_true(v->value);
342 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
343 peer->noSilenceSuppression = ast_true(v->value);
344 } else if (!strcasecmp(v->name, "outgoinglimit")) {
345 peer->outgoinglimit = atoi(v->value);
346 if (peer->outgoinglimit > 0)
347 peer->outgoinglimit = 0;
348 } else if (!strcasecmp(v->name, "host")) {
349 if (!strcasecmp(v->value, "dynamic")) {
350 ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
354 if (ast_get_ip(&peer->addr, v->value)) {
368 * Send (play) the specified digit to the channel.
371 static int oh323_digit(struct ast_channel *c, char digit)
373 struct oh323_pvt *p = c->pvt->pvt;
374 if (p && p->rtp && (p->dtmfmode & H323_DTMF_RFC2833)) {
375 ast_rtp_senddigit(p->rtp, digit);
377 /* If in-band DTMF is desired, send that */
378 if (p->dtmfmode & H323_DTMF_INBAND)
379 h323_send_tone(p->cd.call_token, digit);
385 * Make a call over the specified channel to the specified
386 * destination. This function will parse the destination string
387 * and determine the address-number to call.
388 * Return -1 on error, 0 on success.
390 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
393 struct oh323_pvt *p = c->pvt->pvt;
394 char called_addr[256];
397 strtok_r(dest, "/", &(tmp));
399 ast_log(LOG_DEBUG, "dest=%s, timeout=%d.\n", dest, timeout);
401 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
402 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
409 /* Clear the call token */
410 if ((p->cd).call_token == NULL)
411 (p->cd).call_token = (char *)malloc(128);
413 memset((char *)(p->cd).call_token, 0, 128);
415 if (p->cd.call_token == NULL) {
416 ast_log(LOG_ERROR, "Not enough memory.\n");
420 /* Build the address to call */
421 memset(called_addr, 0, sizeof(dest));
422 memcpy(called_addr, dest, sizeof(called_addr));
424 /* Copy callerid, if there is any */
426 char *tmp = strchr(c->callerid, '"');
428 p->calloptions.callerid = malloc(80); // evil
429 // sprintf(p->calloptions.callerid, "\"%s\"", c->callerid);
430 sprintf(p->calloptions.callerid, "\"\" <%s>", c->callerid);
432 p->calloptions.callerid = strdup(c->callerid);
436 res = h323_make_call(called_addr, &(p->cd), p->calloptions);
439 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
443 ast_setstate(c, AST_STATE_RINGING);
448 static int oh323_answer(struct ast_channel *c)
452 struct oh323_pvt *p = c->pvt->pvt;
454 res = h323_answering_call(p->cd.call_token, 0);
456 if (c->_state != AST_STATE_UP)
457 ast_setstate(c, AST_STATE_UP);
462 static int oh323_hangup(struct ast_channel *c)
464 struct oh323_pvt *p = c->pvt->pvt;
467 ast_log(LOG_DEBUG, "oh323_hangup(%s)\n", c->name);
469 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
472 ast_mutex_lock(&p->lock);
473 /* Determine how to disconnect */
475 ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
476 ast_mutex_unlock(&p->lock);
479 if (!c || (c->_state != AST_STATE_UP))
484 /* Free dsp used for in-band DTMF detection */
486 ast_dsp_free(p->vad);
492 /* Start the process if it's not already started */
493 if (!p->alreadygone) {
495 if (h323_clear_call((p->cd).call_token)) {
496 ast_log(LOG_DEBUG, "ClearCall failed.\n");
500 /* Update usage counter */
501 ast_mutex_lock(&usecnt_lock);
504 ast_log(LOG_WARNING, "Usecnt < 0\n");
505 ast_mutex_unlock(&usecnt_lock);
506 ast_update_use_count();
508 ast_mutex_unlock(&p->lock);
512 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *p)
514 /* Retrieve audio/etc from channel. Assumes p->lock is already held. */
516 static struct ast_frame null_frame = { AST_FRAME_NULL, };
518 /* Only apply it for the first packet, we just need the correct ip/port */
521 ast_rtp_setnat(p->rtp,p->nat);
525 f = ast_rtp_read(p->rtp);
526 /* Don't send RFC2833 if we're not supposed to */
527 if (f && (f->frametype == AST_FRAME_DTMF) && !(p->dtmfmode & H323_DTMF_RFC2833))
530 /* We already hold the channel lock */
531 if (f->frametype == AST_FRAME_VOICE) {
532 if (f->subclass != p->owner->nativeformats) {
533 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
534 p->owner->nativeformats = f->subclass;
535 ast_set_read_format(p->owner, p->owner->readformat);
536 ast_set_write_format(p->owner, p->owner->writeformat);
539 /* Do in-band DTMF detection */
540 if (p->dtmfmode & H323_DTMF_INBAND) {
541 f = ast_dsp_process(p->owner,p->vad,f);
542 if (f->frametype == AST_FRAME_DTMF)
543 ast_log(LOG_DEBUG, "Got in-band digit %c.\n", f->subclass);
553 static struct ast_frame *oh323_read(struct ast_channel *c)
555 struct ast_frame *fr;
556 struct oh323_pvt *p = c->pvt->pvt;
557 ast_mutex_lock(&p->lock);
558 fr = oh323_rtp_read(p);
559 ast_mutex_unlock(&p->lock);
563 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
565 struct oh323_pvt *p = c->pvt->pvt;
567 if (frame->frametype != AST_FRAME_VOICE) {
568 if (frame->frametype == AST_FRAME_IMAGE)
571 ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
575 if (!(frame->subclass & c->nativeformats)) {
576 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
577 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
582 ast_mutex_lock(&p->lock);
584 res = ast_rtp_write(p->rtp, frame);
586 ast_mutex_unlock(&p->lock);
591 /** FIXME: Can I acutally use this or does Open H.323 take care of everything? */
592 static int oh323_indicate(struct ast_channel *c, int condition)
595 struct oh323_pvt *p = c->pvt->pvt;
598 case AST_CONTROL_RINGING:
599 if (c->_state == AST_STATE_RING) {
600 // transmit_response(p, "180 Ringing", &p->initreq);
604 case AST_CONTROL_BUSY:
605 if (c->_state != AST_STATE_UP) {
606 // transmit_response(p, "600 Busy everywhere", &p->initreq);
608 ast_softhangup(c, AST_SOFTHANGUP_DEV);
612 case AST_CONTROL_CONGESTION:
613 if (c->_state != AST_STATE_UP) {
614 // transmit_response(p, "486 Busy here", &p->initreq);
616 ast_softhangup(c, AST_SOFTHANGUP_DEV);
623 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
629 // FIXME: WTF is this? Do I need this???
630 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
632 struct oh323_pvt *p = newchan->pvt->pvt;
634 ast_mutex_lock(&p->lock);
635 if (p->owner != oldchan) {
636 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
640 ast_mutex_unlock(&p->lock);
644 static struct ast_channel *oh323_new(struct oh323_pvt *i, int state, const char *host)
646 struct ast_channel *ch;
648 ch = ast_channel_alloc(1);
652 snprintf(ch->name, sizeof(ch->name)-1, "H323/%s", host);
653 ch->nativeformats = i->capability;
654 if (!ch->nativeformats)
655 ch->nativeformats = capability;
656 fmt = ast_best_codec(ch->nativeformats);
658 ch->fds[0] = ast_rtp_fd(i->rtp);
660 if (state == AST_STATE_RING)
663 ch->writeformat = fmt;
664 ch->pvt->rawwriteformat = fmt;
665 ch->readformat = fmt;
666 ch->pvt->rawreadformat = fmt;
668 /* Allocate dsp for in-band DTMF support */
669 if (i->dtmfmode & H323_DTMF_INBAND) {
670 i->vad = ast_dsp_new();
671 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
674 /* Register the OpenH323 channel's functions. */
676 ch->pvt->send_digit = oh323_digit;
677 ch->pvt->call = oh323_call;
678 ch->pvt->hangup = oh323_hangup;
679 ch->pvt->answer = oh323_answer;
680 ch->pvt->read = oh323_read;
681 ch->pvt->write = oh323_write;
682 ch->pvt->indicate = oh323_indicate;
683 ch->pvt->fixup = oh323_fixup;
684 // ch->pvt->bridge = ast_rtp_bridge;
686 /* Set the owner of this channel */
689 ast_mutex_lock(&usecnt_lock);
691 ast_mutex_unlock(&usecnt_lock);
692 ast_update_use_count();
693 strncpy(ch->context, i->context, sizeof(ch->context)-1);
694 strncpy(ch->exten, i->exten, sizeof(ch->exten)-1);
696 if (strlen(i->callerid))
697 ch->callerid = strdup(i->callerid);
698 if (strlen(i->accountcode))
699 strncpy(ch->accountcode, i->accountcode, sizeof(ch->accountcode)-1);
701 ch->amaflags = i->amaflags;
702 ast_setstate(ch, state);
703 if (state != AST_STATE_DOWN) {
704 if (ast_pbx_start(ch)) {
705 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
711 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
715 static struct oh323_pvt *oh323_alloc(int callid)
719 p = malloc(sizeof(struct oh323_pvt));
721 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
725 /* Keep track of stuff */
726 memset(p, 0, sizeof(struct oh323_pvt));
727 p->rtp = ast_rtp_new(sched, io, 1, 0);
730 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
734 ast_rtp_settos(p->rtp, tos);
735 ast_mutex_init(&p->lock);
737 p->cd.call_reference = callid;
738 p->bridge = bridge_default;
740 p->dtmfmode = dtmfmode;
741 if (p->dtmfmode & H323_DTMF_RFC2833)
742 p->nonCodecCapability |= AST_RTP_DTMF;
744 /* Add to interface list */
745 ast_mutex_lock(&iflock);
748 ast_mutex_unlock(&iflock);
752 static struct oh323_pvt *find_call(int call_reference)
756 ast_mutex_lock(&iflock);
760 if (p->cd.call_reference == call_reference) {
762 ast_mutex_unlock(&iflock);
767 ast_mutex_unlock(&iflock);
772 static struct ast_channel *oh323_request(char *type, int format, void *data)
777 struct ast_channel *tmpc = NULL;
784 ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
787 format &= capability;
789 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
793 strncpy(tmp, dest, sizeof(tmp) - 1);
795 host = strchr(tmp, '@');
805 strtok_r(host, "/", &(h323id));
814 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
818 /* Assign a default capability */
819 p->capability = capability;
822 if (p->dtmfmode & H323_DTMF_RFC2833)
823 p->nonCodecCapability |= AST_RTP_DTMF;
825 p->nonCodecCapability &= ~AST_RTP_DTMF;
827 /* pass on our preferred codec to the H.323 stack */
828 h323_set_capability(format, dtmfmode);
831 strncpy(p->username, ext, sizeof(p->username) - 1);
833 ast_log(LOG_DEBUG, "Host: %s\tUsername: %s\n", host, p->username);
835 tmpc = oh323_new(p, AST_STATE_DOWN, host);
844 struct oh323_alias *find_alias(const char *source_aliases)
846 struct oh323_alias *a;
852 if (!strcasecmp(a->name, source_aliases)) {
860 struct oh323_user *find_user(const call_details_t cd)
862 struct oh323_user *u;
865 if(userbyalias == 1){
867 if (!strcasecmp(u->name, cd.call_source_aliases)) {
875 if (!strcasecmp(cd.sourceIp, inet_ntoa(u->addr.sin_addr))) {
887 struct oh323_peer *find_peer(char *dest_peer)
889 struct oh323_peer *p;
894 if (!strcasecmp(p->name, dest_peer)) {
904 * Callback for sending digits from H.323 up to asterisk
907 int send_digit(unsigned call_reference, char digit)
912 ast_log(LOG_DEBUG, "Recieved Digit: %c\n", digit);
913 p = find_call(call_reference);
916 ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
919 memset(&f, 0, sizeof(f));
920 f.frametype = AST_FRAME_DTMF;
927 f.src = "SEND_DIGIT";
929 return ast_queue_frame(p->owner, &f);
933 * Call-back function that gets called when any H.323 connection is made
935 * Returns the local RTP information
937 struct rtp_info *create_connection(unsigned call_reference)
940 struct sockaddr_in us;
941 struct sockaddr_in them;
942 struct rtp_info *info;
944 info = malloc(sizeof(struct rtp_info));
946 p = find_call(call_reference);
949 ast_log(LOG_ERROR, "Unable to allocate private structure, this is very bad.\n");
953 /* figure out our local RTP port and tell the H.323 stack about it*/
954 ast_rtp_get_us(p->rtp, &us);
955 ast_rtp_get_peer(p->rtp, &them);
958 info->addr = inet_ntoa(us.sin_addr);
959 info->port = ntohs(us.sin_port);
966 * Call-back function for incoming calls
968 * Returns 1 on success
971 int setup_incoming_call(call_details_t cd)
974 struct oh323_pvt *p = NULL;
975 struct ast_channel *c = NULL;
976 struct oh323_user *user = NULL;
977 struct oh323_alias *alias = NULL;
979 /* allocate the call*/
980 p = oh323_alloc(cd.call_reference);
983 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
987 /* Populate the call details in the private structure */
988 p->cd.call_token = cd.call_token;
989 p->cd.call_source_aliases = cd.call_source_aliases;
990 p->cd.call_dest_alias = cd.call_dest_alias;
991 p->cd.call_source_e164 = cd.call_source_e164;
992 p->cd.call_dest_e164 = cd.call_dest_e164;
995 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
996 ast_verbose(VERBOSE_PREFIX_3 " Call token: [%s]\n", p->cd.call_token);
997 ast_verbose(VERBOSE_PREFIX_3 " Calling party name: [%s]\n", p->cd.call_source_aliases);
998 ast_verbose(VERBOSE_PREFIX_3 " Calling party number: [%s]\n", p->cd.call_source_e164);
999 ast_verbose(VERBOSE_PREFIX_3 " Called party name: [%s]\n", p->cd.call_dest_alias);
1000 ast_verbose(VERBOSE_PREFIX_3 " Called party number: [%s]\n", p->cd.call_dest_e164);
1003 /* Decide if we are allowing Gatekeeper routed calls*/
1004 if ((!strcasecmp(cd.sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk == 1)) {
1006 if (strlen(cd.call_dest_e164)) {
1007 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1008 strncpy(p->context, default_context, sizeof(p->context)-1);
1010 alias = find_alias(cd.call_dest_alias);
1013 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd.call_dest_alias);
1016 strncpy(p->exten, alias->name, sizeof(p->exten)-1);
1017 strncpy(p->context, alias->context, sizeof(p->context)-1);
1021 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
1024 /* Either this call is not from the Gatekeeper
1025 or we are not allowing gk routed calls */
1028 user = find_user(cd);
1032 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
1033 if (strlen(p->cd.call_dest_e164)) {
1034 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1036 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
1038 if (!strlen(default_context)) {
1039 ast_log(LOG_ERROR, "Call from user '%s' rejected due to no default context\n", p->cd.call_source_aliases);
1042 strncpy(p->context, default_context, sizeof(p->context)-1);
1043 ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd.call_source_aliases, p->context);
1046 if (strcasecmp(cd.sourceIp, inet_ntoa(user->addr.sin_addr))){
1048 if(!strlen(default_context)) {
1049 ast_log(LOG_ERROR, "Call from user '%s' rejected due to non-matching IP address of '%s'\n", user->name, cd.sourceIp);
1053 strncpy(p->context, default_context, sizeof(p->context)-1);
1054 sprintf(p->exten,"i");
1059 if (user->incominglimit > 0) {
1060 if (user->inUse >= user->incominglimit) {
1061 ast_log(LOG_ERROR, "Call from user '%s' rejected due to usage limit of %d\n", user->name, user->incominglimit);
1065 strncpy(p->context, user->context, sizeof(p->context)-1);
1066 p->bridge = user->bridge;
1069 if (strlen(user->callerid))
1070 strncpy(p->callerid, user->callerid, sizeof(p->callerid) - 1);
1072 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
1074 if (strlen(p->cd.call_dest_e164)) {
1075 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1077 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
1079 if (strlen(user->accountcode)) {
1080 strncpy(p->accountcode, user->accountcode, sizeof(p->accountcode)-1);
1083 /* Increment the usage counter */
1088 /* I know this is horrid, don't kill me saddam */
1090 /* allocate a channel and tell asterisk about it */
1091 c = oh323_new(p, AST_STATE_RINGING, cd.call_token);
1093 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
1101 * Call-back function to establish an outgoing H.323 call
1103 * Returns 1 on success
1105 int setup_outgoing_call(call_details_t cd)
1111 if (p->inUse >= p->outgoinglimit) {
1112 ast_log(LOG_ERROR, "Call to %s rejected due to usage limit of %d outgoing channels\n", p->name, p->inUse);
1117 ast_log(LOG_ERROR, "Rejecting call: peer %s not found\n", dest_peer);
1123 * Call-back function that gets called for each rtp channel opened
1127 void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort)
1129 struct oh323_pvt *p = NULL;
1130 struct sockaddr_in them;
1132 /* Find the call or allocate a private structure if call not found */
1133 p = find_call(call_reference);
1136 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1140 them.sin_family = AF_INET;
1141 them.sin_addr.s_addr = inet_addr(remoteIp); // only works for IPv4
1142 them.sin_port = htons(remotePort);
1143 ast_rtp_set_peer(p->rtp, &them);
1149 * Call-back function to signal asterisk that the channel has been answered
1152 void connection_made(unsigned call_reference)
1154 struct ast_channel *c = NULL;
1155 struct oh323_pvt *p = NULL;
1157 p = find_call(call_reference);
1160 ast_log(LOG_ERROR, "Something is wrong: connection\n");
1164 ast_log(LOG_ERROR, "Channel has no owner\n");
1169 ast_setstate(c, AST_STATE_UP);
1174 * Call-back function to cleanup communication
1177 void cleanup_connection(call_details_t cd)
1179 struct oh323_pvt *p = NULL;
1180 // struct oh323_peer *peer = NULL;
1181 struct oh323_user *user = NULL;
1182 struct ast_rtp *rtp = NULL;
1184 ast_log(LOG_DEBUG, "Cleaning up our mess\n");
1186 p = find_call(cd.call_reference);
1192 /* Decrement usage counter */
1194 user = find_user(cd);
1202 peer = find_peer(cd.call_dest_alias);
1205 user = find_user(cd);
1213 /* Immediately stop RTP */
1214 ast_rtp_destroy(rtp);
1221 ast_queue_hangup(p->owner);
1227 static void *do_monitor(void *data)
1230 struct oh323_pvt *oh323 = NULL;
1233 /* Check for interfaces needing to be killed */
1234 ast_mutex_lock(&iflock);
1238 if (oh323->needdestroy) {
1239 __oh323_destroy(oh323);
1242 oh323 = oh323->next;
1244 ast_mutex_unlock(&iflock);
1246 /* Wait for sched or io */
1247 res = ast_sched_wait(sched);
1248 if ((res < 0) || (res > 1000))
1250 res = ast_io_wait(io, res);
1252 pthread_testcancel();
1254 ast_mutex_lock(&monlock);
1256 ast_sched_runq(sched);
1257 ast_mutex_unlock(&monlock);
1264 static int restart_monitor(void)
1266 /* If we're supposed to be stopped -- stay stopped */
1267 if (monitor_thread == AST_PTHREADT_STOP)
1269 if (ast_mutex_lock(&monlock)) {
1270 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1273 if (monitor_thread == pthread_self()) {
1274 ast_mutex_unlock(&monlock);
1275 ast_log(LOG_WARNING, "Cannot kill myself\n");
1278 if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
1279 /* Wake up the thread */
1280 pthread_kill(monitor_thread, SIGURG);
1282 /* Start a new monitor */
1283 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1284 ast_mutex_unlock(&monlock);
1285 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1289 ast_mutex_unlock(&monlock);
1293 static int h323_do_trace(int fd, int argc, char *argv[])
1296 return RESULT_SHOWUSAGE;
1298 h323_debug(1, atoi(argv[2]));
1299 ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
1300 return RESULT_SUCCESS;
1303 static int h323_no_trace(int fd, int argc, char *argv[])
1306 return RESULT_SHOWUSAGE;
1309 ast_cli(fd, "H.323 trace disabled\n");
1310 return RESULT_SUCCESS;
1313 static int h323_do_debug(int fd, int argc, char *argv[])
1316 return RESULT_SHOWUSAGE;
1319 ast_cli(fd, "H323 debug enabled\n");
1320 return RESULT_SUCCESS;
1323 static int h323_no_debug(int fd, int argc, char *argv[])
1326 return RESULT_SHOWUSAGE;
1329 ast_cli(fd, "H323 Debug disabled\n");
1330 return RESULT_SUCCESS;
1333 static int h323_gk_cycle(int fd, int argc, char *argv[])
1335 return RESULT_SUCCESS;
1338 return RESULT_SHOWUSAGE;
1342 /* Possibly register with a GK */
1343 if (!gatekeeper_disable) {
1344 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1345 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1348 return RESULT_SUCCESS;
1352 static int h323_ep_hangup(int fd, int argc, char *argv[])
1356 return RESULT_SHOWUSAGE;
1359 if (h323_soft_hangup(argv[2])) {
1360 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
1362 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
1365 return RESULT_SUCCESS;
1368 static int h323_tokens_show(int fd, int argc, char *argv[])
1372 return RESULT_SHOWUSAGE;
1376 return RESULT_SUCCESS;
1380 static char trace_usage[] =
1381 "Usage: h.323 trace <level num>\n"
1382 " Enables H.323 stack tracing for debugging purposes\n";
1384 static char no_trace_usage[] =
1385 "Usage: h.323 no trace\n"
1386 " Disables H.323 stack tracing for debugging purposes\n";
1388 static char debug_usage[] =
1389 "Usage: h.323 debug\n"
1390 " Enables chan_h323 debug output\n";
1392 static char no_debug_usage[] =
1393 "Usage: h.323 no debug\n"
1394 " Disables chan_h323 debug output\n";
1396 static char show_codec_usage[] =
1397 "Usage: h.323 show codec\n"
1398 " Shows all enabled codecs\n";
1400 static char show_cycle_usage[] =
1401 "Usage: h.323 gk cycle\n"
1402 " Manually re-register with the Gatekeper\n";
1404 static char show_hangup_usage[] =
1405 "Usage: h.323 hangup <token>\n"
1406 " Manually try to hang up call identified by <token>\n";
1408 static char show_tokens_usage[] =
1409 "Usage: h.323 show tokens\n"
1410 " Print out all active call tokens\n";
1412 static struct ast_cli_entry cli_trace =
1413 { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
1414 static struct ast_cli_entry cli_no_trace =
1415 { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
1416 static struct ast_cli_entry cli_debug =
1417 { { "h.323", "debug", NULL }, h323_do_debug, "Enable chan_h323 debug", debug_usage };
1418 static struct ast_cli_entry cli_no_debug =
1419 { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable chan_h323 debug", no_debug_usage };
1420 static struct ast_cli_entry cli_show_codecs =
1421 { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
1422 static struct ast_cli_entry cli_gk_cycle =
1423 { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
1424 static struct ast_cli_entry cli_hangup_call =
1425 { { "h.323", "hangup", NULL }, h323_ep_hangup, "Manually try to hang up a call", show_hangup_usage };
1426 static struct ast_cli_entry cli_show_tokens =
1427 { { "h.323", "show", "tokens", NULL }, h323_tokens_show, "Manually try to hang up a call", show_tokens_usage };
1431 int reload_config(void)
1435 struct ast_config *cfg;
1436 struct ast_variable *v;
1437 struct oh323_peer *peer = NULL;
1438 struct oh323_user *user = NULL;
1439 struct oh323_alias *alias = NULL;
1440 struct ast_hostent ahp; struct hostent *hp;
1444 cfg = ast_load(config);
1446 /* We *must* have a config file otherwise stop immediately */
1448 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
1452 /* fire up the H.323 Endpoint */
1453 if (!h323_end_point_exist()) {
1454 h323_end_point_create();
1457 dtmfmode = H323_DTMF_RFC2833;
1459 memset(&bindaddr, 0, sizeof(bindaddr));
1461 v = ast_variable_browse(cfg, "general");
1463 /* Create the interface list */
1464 if (!strcasecmp(v->name, "port")) {
1465 port = (int)strtol(v->value, NULL, 10);
1466 } else if (!strcasecmp(v->name, "bindaddr")) {
1467 if (!(hp = ast_gethostbyname(v->value, &ahp))) {
1468 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
1470 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
1472 } else if (!strcasecmp(v->name, "allow")) {
1473 format = ast_getformatbyname(v->value);
1475 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
1477 capability |= format;
1478 } else if (!strcasecmp(v->name, "disallow")) {
1479 format = ast_getformatbyname(v->value);
1481 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
1483 capability &= ~format;
1484 } else if (!strcasecmp(v->name, "tos")) {
1485 if (sscanf(v->value, "%i", &format) == 1)
1486 tos = format & 0xff;
1487 else if (!strcasecmp(v->value, "lowdelay"))
1488 tos = IPTOS_LOWDELAY;
1489 else if (!strcasecmp(v->value, "throughput"))
1490 tos = IPTOS_THROUGHPUT;
1491 else if (!strcasecmp(v->value, "reliability"))
1492 tos = IPTOS_RELIABILITY;
1493 else if (!strcasecmp(v->value, "mincost"))
1494 tos = IPTOS_MINCOST;
1495 else if (!strcasecmp(v->value, "none"))
1498 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
1499 } else if (!strcasecmp(v->name, "gatekeeper")) {
1500 if (!strcasecmp(v->value, "DISABLE")) {
1501 gatekeeper_disable = 1;
1503 } else if (!strcasecmp(v->value, "DISCOVER")) {
1504 gatekeeper_disable = 0;
1505 gatekeeper_discover = 1;
1508 gatekeeper_disable = 0;
1510 strncpy(gatekeeper, v->value, sizeof(gatekeeper)-1);
1512 } else if (!strcasecmp(v->name, "secret")) {
1513 strncpy(secret, v->value, sizeof(secret)-1);
1514 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
1515 gkroute = ast_true(v->value);
1516 } else if (!strcasecmp(v->name, "context")) {
1517 strncpy(default_context, v->value, sizeof(default_context)-1);
1518 ast_verbose(VERBOSE_PREFIX_3 " == Setting default context to %s\n", default_context);
1519 } else if (!strcasecmp(v->name, "dtmfmode")) {
1520 if (!strcasecmp(v->value, "inband"))
1521 dtmfmode=H323_DTMF_INBAND;
1522 else if (!strcasecmp(v->value, "rfc2833"))
1523 dtmfmode = H323_DTMF_RFC2833;
1525 ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
1526 dtmfmode = H323_DTMF_RFC2833;
1528 } else if (!strcasecmp(v->name, "UserByAlias")) {
1529 userbyalias = ast_true(v->value);
1530 } else if (!strcasecmp(v->name, "bridge")) {
1531 bridge_default = ast_true(v->value);
1536 cat = ast_category_browse(cfg, NULL);
1538 if (strcasecmp(cat, "general")) {
1539 utype = ast_variable_retrieve(cfg, cat, "type");
1541 if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
1542 user = build_user(cat, ast_variable_browse(cfg, cat));
1544 ast_mutex_lock(&userl.lock);
1545 user->next = userl.users;
1547 ast_mutex_unlock(&userl.lock);
1549 } else if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
1550 peer = build_peer(cat, ast_variable_browse(cfg, cat));
1552 ast_mutex_lock(&peerl.lock);
1553 peer->next = peerl.peers;
1555 ast_mutex_unlock(&peerl.lock);
1557 } else if (!strcasecmp(utype, "h323")) {
1558 alias = build_alias(cat, ast_variable_browse(cfg, cat));
1560 ast_mutex_lock(&aliasl.lock);
1561 alias->next = aliasl.aliases;
1562 aliasl.aliases = alias;
1563 ast_mutex_unlock(&aliasl.lock);
1566 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
1569 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
1571 cat = ast_category_browse(cfg, cat);
1574 /* Register our H.323 aliases if any*/
1576 if (h323_set_alias(alias)) {
1577 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
1580 alias = alias->next;
1583 /* Add some capabilities */
1584 if(h323_set_capability(capability, dtmfmode)) {
1585 ast_log(LOG_ERROR, "Capabilities failure, this is bad.\n");
1593 void delete_users(void)
1595 struct oh323_user *user, *userlast;
1596 struct oh323_peer *peer;
1598 /* Delete all users */
1599 ast_mutex_lock(&userl.lock);
1600 for (user=userl.users;user;) {
1606 ast_mutex_unlock(&userl.lock);
1607 ast_mutex_lock(&peerl.lock);
1608 for (peer=peerl.peers;peer;) {
1609 /* Assume all will be deleted, and we'll find out for sure later */
1613 ast_mutex_unlock(&peerl.lock);
1616 void delete_aliases(void)
1618 struct oh323_alias *alias, *aliaslast;
1620 /* Delete all users */
1621 ast_mutex_lock(&aliasl.lock);
1622 for (alias=aliasl.aliases;alias;) {
1627 aliasl.aliases=NULL;
1628 ast_mutex_unlock(&aliasl.lock);
1631 void prune_peers(void)
1633 /* Prune peers who still are supposed to be deleted */
1634 struct oh323_peer *peer, *peerlast, *peernext;
1635 ast_mutex_lock(&peerl.lock);
1637 for (peer=peerl.peers;peer;) {
1638 peernext = peer->next;
1642 peerlast->next = peernext;
1644 peerl.peers = peernext;
1649 ast_mutex_unlock(&peerl.lock);
1659 if (strlen(gatekeeper)) {
1667 /* Possibly register with a GK */
1668 if (gatekeeper_disable == 0) {
1669 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1670 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1681 static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
1683 struct oh323_pvt *p;
1685 if (p && p->rtp && p->bridge) {
1691 static struct ast_rtp *oh323_get_vrtp_peer(struct ast_channel *chan)
1696 static char *convertcap(int cap)
1699 case AST_FORMAT_G723_1:
1701 case AST_FORMAT_GSM:
1703 case AST_FORMAT_ULAW:
1705 case AST_FORMAT_ALAW:
1707 case AST_FORMAT_ADPCM:
1709 case AST_FORMAT_G729A:
1711 case AST_FORMAT_SPEEX:
1713 case AST_FORMAT_ILBC:
1716 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
1722 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp)
1724 /* XXX Deal with Video */
1725 struct oh323_pvt *p;
1726 struct sockaddr_in them;
1727 struct sockaddr_in us;
1730 mode = convertcap(chan->writeformat);
1738 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
1742 ast_rtp_get_peer(rtp, &them);
1743 ast_rtp_get_us(rtp, &us);
1746 h323_native_bridge(p->cd.call_token, inet_ntoa(them.sin_addr), mode);
1752 static struct ast_rtp_protocol oh323_rtp = {
1753 get_rtp_info: oh323_get_rtp_peer,
1754 get_vrtp_info: oh323_get_vrtp_peer,
1755 set_rtp_peer: oh323_set_rtp_peer,
1763 res = reload_config();
1768 /* Make sure we can register our channel type */
1769 if (ast_channel_register(type, tdesc, capability, oh323_request)) {
1770 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1774 ast_cli_register(&cli_debug);
1775 ast_cli_register(&cli_no_debug);
1776 ast_cli_register(&cli_trace);
1777 ast_cli_register(&cli_no_trace);
1778 ast_cli_register(&cli_show_codecs);
1779 ast_cli_register(&cli_gk_cycle);
1780 ast_cli_register(&cli_hangup_call);
1781 ast_cli_register(&cli_show_tokens);
1783 oh323_rtp.type = type;
1784 ast_rtp_proto_register(&oh323_rtp);
1786 sched = sched_context_create();
1788 ast_log(LOG_WARNING, "Unable to create schedule context\n");
1790 io = io_context_create();
1792 ast_log(LOG_WARNING, "Unable to create I/O context\n");
1795 /* Register our callback functions */
1796 h323_callback_register(setup_incoming_call,
1797 setup_outgoing_call,
1799 setup_rtp_connection,
1805 /* start the h.323 listener */
1806 if (h323_start_listener(port, bindaddr)) {
1807 ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
1808 // h323_end_process();
1812 /* Possibly register with a GK */
1813 if (gatekeeper_disable == 0) {
1814 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1815 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1816 // h323_end_process();
1820 /* And start the monitor for the first time */
1829 struct oh323_pvt *p, *pl;
1831 if (!ast_mutex_lock(&iflock)) {
1832 /* hangup all interfaces if they have an owner */
1836 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1840 ast_mutex_unlock(&iflock);
1842 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1846 if (!ast_mutex_lock(&monlock)) {
1847 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
1848 pthread_cancel(monitor_thread);
1849 pthread_kill(monitor_thread, SIGURG);
1850 pthread_join(monitor_thread, NULL);
1852 monitor_thread = AST_PTHREADT_STOP;
1853 ast_mutex_unlock(&monlock);
1855 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1859 if (!ast_mutex_lock(&iflock)) {
1860 /* destroy all the interfaces and free their memory */
1865 /* free associated memory */
1869 ast_mutex_unlock(&iflock);
1871 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1877 /* unregister rtp */
1878 ast_rtp_proto_unregister(&oh323_rtp);
1880 /* unregister commands */
1881 ast_cli_unregister(&cli_debug);
1882 ast_cli_unregister(&cli_no_debug);
1883 ast_cli_unregister(&cli_trace);
1884 ast_cli_unregister(&cli_no_trace);
1885 ast_cli_unregister(&cli_show_codecs);
1886 ast_cli_unregister(&cli_gk_cycle);
1887 ast_cli_unregister(&cli_hangup_call);
1888 ast_cli_unregister(&cli_show_tokens);
1890 /* unregister channel type */
1891 ast_channel_unregister(type);
1899 ast_mutex_lock(&usecnt_lock);
1901 ast_mutex_unlock(&usecnt_lock);
1912 return ASTERISK_GPL_KEY;