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>
57 #include <netinet/ip.h>
60 #include "h323/chan_h323.h"
62 /** String variables required by ASTERISK */
63 static char *type = "H323";
64 static char *desc = "The NuFone Network's Open H.323 Channel Driver";
65 static char *tdesc = "The NuFone Network's Open H.323 Channel Driver";
66 static char *config = "h323.conf";
68 static char default_context[AST_MAX_EXTENSION];
70 /** H.323 configuration values */
71 static char gatekeeper[100];
72 static int gatekeeper_disable = 1;
73 static int gatekeeper_discover = 0;
75 static int port = 1720;
76 static int gkroute = 0;
78 /* to find user by alias is default, alternative is the incomming call's source IP address*/
79 static int userbyalias = 1;
81 static int bridge_default = 1;
83 /* Just about everybody seems to support ulaw, so make it a nice default */
84 static int capability = AST_FORMAT_ULAW;
89 static int dtmfmode = H323_DTMF_RFC2833;
91 static char secret[50];
93 /** Private structure of a OpenH323 channel */
95 ast_mutex_t lock; /* Channel private lock */
96 call_options_t calloptions; /* Options to be used during call setup */
97 int alreadygone; /* Whether or not we've already been destroyed by or peer */
98 int needdestroy; /* if we need to be destroyed */
99 call_details_t cd; /* Call details */
100 struct ast_channel *owner; /* Who owns us */
101 int capability; /* Special capability */
102 int nonCodecCapability;
103 int outgoing; /* Outgoing or incoming call? */
104 int nat; /* Are we talking to a NAT EP?*/
105 int bridge; /* Determine of we should native bridge or not*/
106 char exten[AST_MAX_EXTENSION]; /* Requested extension */
107 char context[AST_MAX_EXTENSION]; /* Context where to start */
108 char username[81]; /* H.323 alias using this channel */
109 char accountcode[256]; /* Account code */
110 int amaflags; /* AMA Flags */
111 char callerid[80]; /* Caller*ID if available */
112 struct ast_rtp *rtp; /* RTP Session */
114 struct ast_dsp *vad; /* Used for in-band DTMF detection */
115 struct oh323_pvt *next; /* Next channel in list */
118 static struct ast_user_list {
119 struct oh323_user *users;
121 } userl = { NULL, AST_MUTEX_INITIALIZER };
123 static struct ast_peer_list {
124 struct oh323_peer *peers;
126 } peerl = { NULL, AST_MUTEX_INITIALIZER };
128 static struct ast_alias_list {
129 struct oh323_alias *aliases;
131 } aliasl = { NULL, AST_MUTEX_INITIALIZER };
133 /** Asterisk RTP stuff*/
134 static struct sched_context *sched;
135 static struct io_context *io;
137 /** Protect the interface list (of oh323_pvt's) */
138 static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
140 /** Usage counter and associated lock */
141 static int usecnt =0;
142 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
144 /* Protect the monitoring thread, so only one process can kill or start it, and not
145 when it's doing something critical. */
146 static ast_mutex_t monlock = AST_MUTEX_INITIALIZER;
148 /* This is the thread for the monitor which checks for input on the channels
149 which are not currently in use. */
150 static pthread_t monitor_thread = 0;
152 static int restart_monitor(void);
154 static void __oh323_destroy(struct oh323_pvt *p)
156 struct oh323_pvt *cur, *prev = NULL;
159 ast_rtp_destroy(p->rtp);
162 /* Unlink us from the owner if we have one */
164 ast_mutex_lock(&p->owner->lock);
165 ast_log(LOG_DEBUG, "Detaching from %s\n", p->owner->name);
166 p->owner->pvt->pvt = NULL;
167 ast_mutex_unlock(&p->owner->lock);
173 prev->next = cur->next;
182 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
187 static void oh323_destroy(struct oh323_pvt *p)
189 ast_mutex_lock(&iflock);
191 ast_mutex_unlock(&iflock);
194 static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
196 struct oh323_alias *alias;
198 alias = (struct oh323_alias *)malloc(sizeof(struct oh323_alias));
201 memset(alias, 0, sizeof(struct oh323_alias));
202 strncpy(alias->name, name, sizeof(alias->name)-1);
205 if (!strcasecmp(v->name, "e164")) {
206 strncpy(alias->e164, v->value, sizeof(alias->e164)-1);
207 } else if (!strcasecmp(v->name, "prefix")) {
208 strncpy(alias->prefix, v->value, sizeof(alias->prefix)-1);
209 } else if (!strcasecmp(v->name, "context")) {
210 strncpy(alias->context, v->value, sizeof(alias->context)-1);
211 } else if (!strcasecmp(v->name, "secret")) {
212 strncpy(alias->secret, v->value, sizeof(alias->secret)-1);
214 if (strcasecmp(v->value, "h323")) {
215 ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->value);
224 static struct oh323_user *build_user(char *name, struct ast_variable *v)
226 struct oh323_user *user;
229 user = (struct oh323_user *)malloc(sizeof(struct oh323_user));
231 memset(user, 0, sizeof(struct oh323_user));
232 strncpy(user->name, name, sizeof(user->name)-1);
234 /* set the usage flag to a sane starting value*/
236 /* Assume we can native bridge */
237 user->bridge = bridge_default;
240 if (!strcasecmp(v->name, "context")) {
241 strncpy(user->context, v->value, sizeof(user->context)-1);
242 } else if (!strcasecmp(v->name, "bridge")) {
243 user->bridge = ast_true(v->value);
244 } else if (!strcasecmp(v->name, "nat")) {
245 user->nat = ast_true(v->value);
246 } else if (!strcasecmp(v->name, "noFastStart")) {
247 user->noFastStart = ast_true(v->value);
248 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
249 user->noH245Tunneling = ast_true(v->value);
250 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
251 user->noSilenceSuppression = ast_true(v->value);
252 } else if (!strcasecmp(v->name, "secret")) {
253 strncpy(user->secret, v->value, sizeof(user->secret)-1);
254 } else if (!strcasecmp(v->name, "callerid")) {
255 strncpy(user->callerid, v->value, sizeof(user->callerid)-1);
256 } else if (!strcasecmp(v->name, "accountcode")) {
257 strncpy(user->accountcode, v->value, sizeof(user->accountcode)-1);
258 } else if (!strcasecmp(v->name, "incominglimit")) {
259 user->incominglimit = atoi(v->value);
260 if (user->incominglimit < 0)
261 user->incominglimit = 0;
262 } else if (!strcasecmp(v->name, "host")) {
263 if (!strcasecmp(v->value, "dynamic")) {
264 ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
267 } else if (ast_get_ip(&user->addr, v->value)) {
271 /* Let us know we need to use ip authentication */
273 } else if (!strcasecmp(v->name, "amaflags")) {
274 format = ast_cdr_amaflags2int(v->value);
276 ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
278 user->amaflags = format;
288 static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
290 struct oh323_peer *peer;
291 struct oh323_peer *prev;
295 ast_mutex_lock(&peerl.lock);
299 if (!strcasecmp(peer->name, name)) {
308 /* Already in the list, remove it and it will be added back (or FREE'd) */
310 prev->next = peer->next;
312 peerl.peers = peer->next;
314 ast_mutex_unlock(&peerl.lock);
316 ast_mutex_unlock(&peerl.lock);
317 peer = malloc(sizeof(struct oh323_peer));
318 memset(peer, 0, sizeof(struct oh323_peer));
322 strncpy(peer->name, name, sizeof(peer->name)-1);
325 /* set the usage flag to a sane starting value*/
329 if (!strcasecmp(v->name, "context")) {
330 strncpy(peer->context, v->value, sizeof(peer->context)-1);
331 } else if (!strcasecmp(v->name, "bridge")) {
332 peer->bridge = ast_true(v->value);
333 } else if (!strcasecmp(v->name, "noFastStart")) {
334 peer->noFastStart = ast_true(v->value);
335 } else if (!strcasecmp(v->name, "noH245Tunneling")) {
336 peer->noH245Tunneling = ast_true(v->value);
337 } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
338 peer->noSilenceSuppression = ast_true(v->value);
339 } else if (!strcasecmp(v->name, "outgoinglimit")) {
340 peer->outgoinglimit = atoi(v->value);
341 if (peer->outgoinglimit > 0)
342 peer->outgoinglimit = 0;
343 } else if (!strcasecmp(v->name, "host")) {
344 if (!strcasecmp(v->value, "dynamic")) {
345 ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
349 if (ast_get_ip(&peer->addr, v->value)) {
363 * Send (play) the specified digit to the channel.
366 static int oh323_digit(struct ast_channel *c, char digit)
368 struct oh323_pvt *p = c->pvt->pvt;
369 if (p && p->rtp && (p->dtmfmode & H323_DTMF_RFC2833)) {
370 ast_rtp_senddigit(p->rtp, digit);
372 /* If in-band DTMF is desired, send that */
373 if (p->dtmfmode & H323_DTMF_INBAND)
374 h323_send_tone(p->cd.call_token, digit);
380 * Make a call over the specified channel to the specified
381 * destination. This function will parse the destination string
382 * and determine the address-number to call.
383 * Return -1 on error, 0 on success.
385 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
388 struct oh323_pvt *p = c->pvt->pvt;
389 char called_addr[256];
392 strtok_r(dest, "/", &(tmp));
394 ast_log(LOG_DEBUG, "dest=%s, timeout=%d.\n", dest, timeout);
396 if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
397 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
404 /* Clear the call token */
405 if ((p->cd).call_token == NULL)
406 (p->cd).call_token = (char *)malloc(128);
408 memset((char *)(p->cd).call_token, 0, 128);
410 if (p->cd.call_token == NULL) {
411 ast_log(LOG_ERROR, "Not enough memory.\n");
415 /* Build the address to call */
416 memset(called_addr, 0, sizeof(dest));
417 memcpy(called_addr, dest, sizeof(called_addr));
419 /* Copy callerid, if there is any */
421 char *tmp = strchr(c->callerid, '"');
423 p->calloptions.callerid = malloc(80); // evil
424 // sprintf(p->calloptions.callerid, "\"%s\"", c->callerid);
425 sprintf(p->calloptions.callerid, "\"\" <%s>", c->callerid);
427 p->calloptions.callerid = strdup(c->callerid);
431 res = h323_make_call(called_addr, &(p->cd), p->calloptions);
434 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
438 ast_setstate(c, AST_STATE_RINGING);
443 static int oh323_answer(struct ast_channel *c)
447 struct oh323_pvt *p = c->pvt->pvt;
449 res = h323_answering_call(p->cd.call_token, 0);
451 if (c->_state != AST_STATE_UP)
452 ast_setstate(c, AST_STATE_UP);
457 static int oh323_hangup(struct ast_channel *c)
459 struct oh323_pvt *p = c->pvt->pvt;
462 ast_log(LOG_DEBUG, "oh323_hangup(%s)\n", c->name);
464 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
467 ast_mutex_lock(&p->lock);
468 /* Determine how to disconnect */
470 ast_log(LOG_WARNING, "Huh? We aren't the owner?\n");
471 ast_mutex_unlock(&p->lock);
474 if (!c || (c->_state != AST_STATE_UP))
479 /* Free dsp used for in-band DTMF detection */
481 ast_dsp_free(p->vad);
487 /* Start the process if it's not already started */
488 if (!p->alreadygone) {
490 if (h323_clear_call((p->cd).call_token)) {
491 ast_log(LOG_DEBUG, "ClearCall failed.\n");
495 /* Update usage counter */
496 ast_mutex_lock(&usecnt_lock);
499 ast_log(LOG_WARNING, "Usecnt < 0\n");
500 ast_mutex_unlock(&usecnt_lock);
501 ast_update_use_count();
503 ast_mutex_unlock(&p->lock);
507 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *p)
509 /* Retrieve audio/etc from channel. Assumes p->lock is already held. */
511 static struct ast_frame null_frame = { AST_FRAME_NULL, };
513 /* Only apply it for the first packet, we just need the correct ip/port */
516 ast_rtp_setnat(p->rtp,p->nat);
520 f = ast_rtp_read(p->rtp);
521 /* Don't send RFC2833 if we're not supposed to */
522 if (f && (f->frametype == AST_FRAME_DTMF) && !(p->dtmfmode & H323_DTMF_RFC2833))
525 /* We already hold the channel lock */
526 if (f->frametype == AST_FRAME_VOICE) {
527 if (f->subclass != p->owner->nativeformats) {
528 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
529 p->owner->nativeformats = f->subclass;
530 ast_set_read_format(p->owner, p->owner->readformat);
531 ast_set_write_format(p->owner, p->owner->writeformat);
534 /* Do in-band DTMF detection */
535 if (p->dtmfmode & H323_DTMF_INBAND) {
536 f = ast_dsp_process(p->owner,p->vad,f,0);
537 if (f->frametype == AST_FRAME_DTMF)
538 ast_log(LOG_DEBUG, "Got in-band digit %c.\n", f->subclass);
548 static struct ast_frame *oh323_read(struct ast_channel *c)
550 struct ast_frame *fr;
551 struct oh323_pvt *p = c->pvt->pvt;
552 ast_mutex_lock(&p->lock);
553 fr = oh323_rtp_read(p);
554 ast_mutex_unlock(&p->lock);
558 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
560 struct oh323_pvt *p = c->pvt->pvt;
562 if (frame->frametype != AST_FRAME_VOICE) {
563 if (frame->frametype == AST_FRAME_IMAGE)
566 ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
570 if (!(frame->subclass & c->nativeformats)) {
571 ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
572 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
577 ast_mutex_lock(&p->lock);
579 res = ast_rtp_write(p->rtp, frame);
581 ast_mutex_unlock(&p->lock);
586 /** FIXME: Can I acutally use this or does Open H.323 take care of everything? */
587 static int oh323_indicate(struct ast_channel *c, int condition)
590 struct oh323_pvt *p = c->pvt->pvt;
593 case AST_CONTROL_RINGING:
594 if (c->_state == AST_STATE_RING) {
595 // transmit_response(p, "180 Ringing", &p->initreq);
599 case AST_CONTROL_BUSY:
600 if (c->_state != AST_STATE_UP) {
601 // transmit_response(p, "600 Busy everywhere", &p->initreq);
603 ast_softhangup(c, AST_SOFTHANGUP_DEV);
607 case AST_CONTROL_CONGESTION:
608 if (c->_state != AST_STATE_UP) {
609 // transmit_response(p, "486 Busy here", &p->initreq);
611 ast_softhangup(c, AST_SOFTHANGUP_DEV);
618 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
624 // FIXME: WTF is this? Do I need this???
625 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, int needlock)
627 struct oh323_pvt *p = newchan->pvt->pvt;
630 ast_mutex_lock(&p->lock);
631 if (p->owner != oldchan) {
632 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
637 ast_mutex_unlock(&p->lock);
641 static struct ast_channel *oh323_new(struct oh323_pvt *i, int state, const char *host)
643 struct ast_channel *ch;
645 ch = ast_channel_alloc(1);
649 snprintf(ch->name, sizeof(ch->name)-1, "H323/%s", host);
650 ch->nativeformats = i->capability;
651 if (!ch->nativeformats)
652 ch->nativeformats = capability;
653 fmt = ast_best_codec(ch->nativeformats);
655 ch->fds[0] = ast_rtp_fd(i->rtp);
656 ast_setstate(ch, state);
658 if (state == AST_STATE_RING)
661 ch->writeformat = fmt;
662 ch->pvt->rawwriteformat = fmt;
663 ch->readformat = fmt;
664 ch->pvt->rawreadformat = fmt;
666 /* Allocate dsp for in-band DTMF support */
667 if (i->dtmfmode & H323_DTMF_INBAND) {
668 i->vad = ast_dsp_new();
669 ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
672 /* Register the OpenH323 channel's functions. */
674 ch->pvt->send_digit = oh323_digit;
675 ch->pvt->call = oh323_call;
676 ch->pvt->hangup = oh323_hangup;
677 ch->pvt->answer = oh323_answer;
678 ch->pvt->read = oh323_read;
679 ch->pvt->write = oh323_write;
680 ch->pvt->indicate = oh323_indicate;
681 ch->pvt->fixup = oh323_fixup;
682 // ch->pvt->bridge = ast_rtp_bridge;
684 /* Set the owner of this channel */
687 ast_mutex_lock(&usecnt_lock);
689 ast_mutex_unlock(&usecnt_lock);
690 ast_update_use_count();
691 strncpy(ch->context, i->context, sizeof(ch->context)-1);
692 strncpy(ch->exten, i->exten, sizeof(ch->exten)-1);
694 if (strlen(i->callerid))
695 ch->callerid = strdup(i->callerid);
696 if (strlen(i->accountcode))
697 strncpy(ch->accountcode, i->accountcode, sizeof(ch->accountcode)-1);
699 ch->amaflags = i->amaflags;
700 if (state != AST_STATE_DOWN) {
701 if (ast_pbx_start(ch)) {
702 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
708 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
712 static struct oh323_pvt *oh323_alloc(int callid)
716 p = malloc(sizeof(struct oh323_pvt));
718 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
722 /* Keep track of stuff */
723 memset(p, 0, sizeof(struct oh323_pvt));
724 p->rtp = ast_rtp_new(sched, io, 1, 0);
727 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
731 ast_rtp_settos(p->rtp, tos);
732 ast_mutex_init(&p->lock);
734 p->cd.call_reference = callid;
735 p->bridge = bridge_default;
737 p->dtmfmode = dtmfmode;
738 if (p->dtmfmode & H323_DTMF_RFC2833)
739 p->nonCodecCapability |= AST_RTP_DTMF;
741 /* Add to interface list */
742 ast_mutex_lock(&iflock);
745 ast_mutex_unlock(&iflock);
749 static struct oh323_pvt *find_call(int call_reference)
753 ast_mutex_lock(&iflock);
757 if (p->cd.call_reference == call_reference) {
759 ast_mutex_unlock(&iflock);
764 ast_mutex_unlock(&iflock);
769 static struct ast_channel *oh323_request(char *type, int format, void *data)
774 struct ast_channel *tmpc = NULL;
781 ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
784 format &= capability;
786 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
790 strncpy(tmp, dest, sizeof(tmp) - 1);
792 host = strchr(tmp, '@');
802 strtok_r(host, "/", &(h323id));
812 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
816 /* Assign a default capability */
817 p->capability = capability;
820 if (p->dtmfmode & H323_DTMF_RFC2833)
821 p->nonCodecCapability |= AST_RTP_DTMF;
823 p->nonCodecCapability &= ~AST_RTP_DTMF;
827 strncpy(p->username, ext, sizeof(p->username) - 1);
829 ast_log(LOG_DEBUG, "Host: %s\tUsername: %s\n", host, p->username);
831 tmpc = oh323_new(p, AST_STATE_DOWN, host);
840 struct oh323_alias *find_alias(const char *source_aliases)
842 struct oh323_alias *a;
848 if (!strcasecmp(a->name, source_aliases)) {
856 struct oh323_user *find_user(const call_details_t cd)
858 struct oh323_user *u;
861 if(userbyalias == 1){
863 if (!strcasecmp(u->name, cd.call_source_aliases)) {
871 if (!strcasecmp(cd.sourceIp, inet_ntoa(u->addr.sin_addr))) {
883 struct oh323_peer *find_peer(char *dest_peer)
885 struct oh323_peer *p;
890 if (!strcasecmp(p->name, dest_peer)) {
900 * Callback for sending digits from H.323 up to asterisk
903 int send_digit(unsigned call_reference, char digit)
908 ast_log(LOG_DEBUG, "Recieved Digit: %c\n", digit);
909 p = find_call(call_reference);
912 ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
915 memset(&f, 0, sizeof(f));
916 f.frametype = AST_FRAME_DTMF;
923 f.src = "SEND_DIGIT";
925 return ast_queue_frame(p->owner, &f, 1);
929 * Call-back function that gets called when any H.323 connection is made
931 * Returns the local RTP information
933 struct rtp_info *create_connection(unsigned call_reference)
936 struct sockaddr_in us;
937 struct sockaddr_in them;
938 struct rtp_info *info;
940 info = malloc(sizeof(struct rtp_info));
942 p = find_call(call_reference);
945 ast_log(LOG_ERROR, "Unable to allocate private structure, this is very bad.\n");
949 /* figure out our local RTP port and tell the H.323 stack about it*/
950 ast_rtp_get_us(p->rtp, &us);
951 ast_rtp_get_peer(p->rtp, &them);
954 info->addr = inet_ntoa(us.sin_addr);
955 info->port = ntohs(us.sin_port);
962 * Call-back function for incoming calls
964 * Returns 1 on success
967 int setup_incoming_call(call_details_t cd)
970 struct oh323_pvt *p = NULL;
971 struct ast_channel *c = NULL;
972 struct oh323_user *user = NULL;
973 struct oh323_alias *alias = NULL;
975 /* allocate the call*/
976 p = oh323_alloc(cd.call_reference);
979 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
983 /* Populate the call details in the private structure */
984 p->cd.call_token = cd.call_token;
985 p->cd.call_source_aliases = cd.call_source_aliases;
986 p->cd.call_dest_alias = cd.call_dest_alias;
987 p->cd.call_source_e164 = cd.call_source_e164;
988 p->cd.call_dest_e164 = cd.call_dest_e164;
991 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
992 ast_verbose(VERBOSE_PREFIX_3 " Call token: [%s]\n", p->cd.call_token);
993 ast_verbose(VERBOSE_PREFIX_3 " Calling party name: [%s]\n", p->cd.call_source_aliases);
994 ast_verbose(VERBOSE_PREFIX_3 " Calling party number: [%s]\n", p->cd.call_source_e164);
995 ast_verbose(VERBOSE_PREFIX_3 " Called party name: [%s]\n", p->cd.call_dest_alias);
996 ast_verbose(VERBOSE_PREFIX_3 " Called party number: [%s]\n", p->cd.call_dest_e164);
999 /* Decide if we are allowing Gatekeeper routed calls*/
1000 if ((!strcasecmp(cd.sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk == 1)) {
1002 if (strlen(cd.call_dest_e164)) {
1003 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1004 strncpy(p->context, default_context, sizeof(p->context)-1);
1006 alias = find_alias(cd.call_dest_alias);
1009 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd.call_dest_alias);
1012 strncpy(p->exten, alias->name, sizeof(p->exten)-1);
1013 strncpy(p->context, alias->context, sizeof(p->context)-1);
1017 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
1020 /* Either this call is not from the Gatekeeper
1021 or we are not allowing gk routed calls */
1024 user = find_user(cd);
1028 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
1029 if (strlen(p->cd.call_dest_e164)) {
1030 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1032 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
1034 if (!strlen(default_context)) {
1035 ast_log(LOG_ERROR, "Call from user '%s' rejected due to no default context\n", p->cd.call_source_aliases);
1038 strncpy(p->context, default_context, sizeof(p->context)-1);
1039 ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd.call_source_aliases, p->context);
1042 if (strcasecmp(cd.sourceIp, inet_ntoa(user->addr.sin_addr))){
1044 if(!strlen(default_context)) {
1045 ast_log(LOG_ERROR, "Call from user '%s' rejected due to non-matching IP address of '%s'\n", user->name, cd.sourceIp);
1049 strncpy(p->context, default_context, sizeof(p->context)-1);
1050 sprintf(p->exten,"i");
1055 if (user->incominglimit > 0) {
1056 if (user->inUse >= user->incominglimit) {
1057 ast_log(LOG_ERROR, "Call from user '%s' rejected due to usage limit of %d\n", user->name, user->incominglimit);
1061 strncpy(p->context, user->context, sizeof(p->context)-1);
1062 p->bridge = user->bridge;
1065 if (strlen(user->callerid))
1066 strncpy(p->callerid, user->callerid, sizeof(p->callerid) - 1);
1068 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
1070 if (strlen(p->cd.call_dest_e164)) {
1071 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1073 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);
1075 if (strlen(user->accountcode)) {
1076 strncpy(p->accountcode, user->accountcode, sizeof(p->accountcode)-1);
1079 /* Increment the usage counter */
1084 /* I know this is horrid, don't kill me saddam */
1086 /* allocate a channel and tell asterisk about it */
1087 c = oh323_new(p, AST_STATE_RINGING, cd.call_token);
1089 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
1097 * Call-back function to establish an outgoing H.323 call
1099 * Returns 1 on success
1101 int setup_outgoing_call(call_details_t cd)
1107 if (p->inUse >= p->outgoinglimit) {
1108 ast_log(LOG_ERROR, "Call to %s rejected due to usage limit of %d outgoing channels\n", p->name, p->inUse);
1113 ast_log(LOG_ERROR, "Rejecting call: peer %s not found\n", dest_peer);
1119 * Call-back function that gets called for each rtp channel opened
1123 void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort)
1125 struct oh323_pvt *p = NULL;
1126 struct sockaddr_in them;
1128 /* Find the call or allocate a private structure if call not found */
1129 p = find_call(call_reference);
1132 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1136 them.sin_family = AF_INET;
1137 them.sin_addr.s_addr = inet_addr(remoteIp); // only works for IPv4
1138 them.sin_port = htons(remotePort);
1139 ast_rtp_set_peer(p->rtp, &them);
1145 * Call-back function to signal asterisk that the channel has been answered
1148 void connection_made(unsigned call_reference)
1150 struct ast_channel *c = NULL;
1151 struct oh323_pvt *p = NULL;
1153 p = find_call(call_reference);
1156 ast_log(LOG_ERROR, "Something is wrong: connection\n");
1160 ast_log(LOG_ERROR, "Channel has no owner\n");
1165 ast_setstate(c, AST_STATE_UP);
1170 * Call-back function to cleanup communication
1173 void cleanup_connection(call_details_t cd)
1175 struct oh323_pvt *p = NULL;
1176 // struct oh323_peer *peer = NULL;
1177 struct oh323_user *user = NULL;
1178 struct ast_rtp *rtp = NULL;
1180 ast_log(LOG_DEBUG, "Cleaning up our mess\n");
1182 p = find_call(cd.call_reference);
1188 /* Decrement usage counter */
1190 user = find_user(cd);
1198 peer = find_peer(cd.call_dest_alias);
1201 user = find_user(cd);
1209 /* Immediately stop RTP */
1210 ast_rtp_destroy(rtp);
1217 ast_queue_hangup(p->owner, 1);
1223 static void *do_monitor(void *data)
1226 struct oh323_pvt *oh323 = NULL;
1229 /* Check for interfaces needing to be killed */
1230 ast_mutex_lock(&iflock);
1234 if (oh323->needdestroy) {
1235 __oh323_destroy(oh323);
1238 oh323 = oh323->next;
1240 ast_mutex_unlock(&iflock);
1242 /* Wait for sched or io */
1243 res = ast_sched_wait(sched);
1244 if ((res < 0) || (res > 1000))
1246 res = ast_io_wait(io, res);
1248 pthread_testcancel();
1250 ast_mutex_lock(&monlock);
1252 ast_sched_runq(sched);
1253 ast_mutex_unlock(&monlock);
1260 static int restart_monitor(void)
1262 /* If we're supposed to be stopped -- stay stopped */
1263 if (monitor_thread == -2)
1265 if (ast_mutex_lock(&monlock)) {
1266 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1269 if (monitor_thread == pthread_self()) {
1270 ast_mutex_unlock(&monlock);
1271 ast_log(LOG_WARNING, "Cannot kill myself\n");
1274 if (monitor_thread && (monitor_thread != -2)) {
1275 /* Wake up the thread */
1276 pthread_kill(monitor_thread, SIGURG);
1278 /* Start a new monitor */
1279 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1280 ast_mutex_unlock(&monlock);
1281 ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1285 ast_mutex_unlock(&monlock);
1289 static int h323_do_trace(int fd, int argc, char *argv[])
1292 return RESULT_SHOWUSAGE;
1294 h323_debug(1, atoi(argv[2]));
1295 ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
1296 return RESULT_SUCCESS;
1299 static int h323_no_trace(int fd, int argc, char *argv[])
1302 return RESULT_SHOWUSAGE;
1305 ast_cli(fd, "H.323 trace disabled\n");
1306 return RESULT_SUCCESS;
1309 static int h323_do_debug(int fd, int argc, char *argv[])
1312 return RESULT_SHOWUSAGE;
1315 ast_cli(fd, "H323 debug enabled\n");
1316 return RESULT_SUCCESS;
1319 static int h323_no_debug(int fd, int argc, char *argv[])
1322 return RESULT_SHOWUSAGE;
1325 ast_cli(fd, "H323 Debug disabled\n");
1326 return RESULT_SUCCESS;
1329 static int h323_gk_cycle(int fd, int argc, char *argv[])
1331 return RESULT_SUCCESS;
1334 return RESULT_SHOWUSAGE;
1338 /* Possibly register with a GK */
1339 if (!gatekeeper_disable) {
1340 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1341 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1344 return RESULT_SUCCESS;
1348 static int h323_ep_hangup(int fd, int argc, char *argv[])
1352 return RESULT_SHOWUSAGE;
1355 if (h323_soft_hangup(argv[2])) {
1356 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
1358 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
1361 return RESULT_SUCCESS;
1364 static int h323_tokens_show(int fd, int argc, char *argv[])
1368 return RESULT_SHOWUSAGE;
1372 return RESULT_SUCCESS;
1376 static char trace_usage[] =
1377 "Usage: h.323 trace <level num>\n"
1378 " Enables H.323 stack tracing for debugging purposes\n";
1380 static char no_trace_usage[] =
1381 "Usage: h.323 no trace\n"
1382 " Disables H.323 stack tracing for debugging purposes\n";
1384 static char debug_usage[] =
1385 "Usage: h.323 debug\n"
1386 " Enables chan_h323 debug output\n";
1388 static char no_debug_usage[] =
1389 "Usage: h.323 no debug\n"
1390 " Disables chan_h323 debug output\n";
1392 static char show_codec_usage[] =
1393 "Usage: h.323 show codec\n"
1394 " Shows all enabled codecs\n";
1396 static char show_cycle_usage[] =
1397 "Usage: h.323 gk cycle\n"
1398 " Manually re-register with the Gatekeper\n";
1400 static char show_hangup_usage[] =
1401 "Usage: h.323 hangup <token>\n"
1402 " Manually try to hang up call identified by <token>\n";
1404 static char show_tokens_usage[] =
1405 "Usage: h.323 show tokens\n"
1406 " Print out all active call tokens\n";
1408 static struct ast_cli_entry cli_trace =
1409 { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
1410 static struct ast_cli_entry cli_no_trace =
1411 { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
1412 static struct ast_cli_entry cli_debug =
1413 { { "h.323", "debug", NULL }, h323_do_debug, "Enable chan_h323 debug", debug_usage };
1414 static struct ast_cli_entry cli_no_debug =
1415 { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable chan_h323 debug", no_debug_usage };
1416 static struct ast_cli_entry cli_show_codecs =
1417 { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
1418 static struct ast_cli_entry cli_gk_cycle =
1419 { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
1420 static struct ast_cli_entry cli_hangup_call =
1421 { { "h.323", "hangup", NULL }, h323_ep_hangup, "Manually try to hang up a call", show_hangup_usage };
1422 static struct ast_cli_entry cli_show_tokens =
1423 { { "h.323", "show", "tokens", NULL }, h323_tokens_show, "Manually try to hang up a call", show_tokens_usage };
1427 int reload_config(void)
1431 struct ast_config *cfg;
1432 struct ast_variable *v;
1433 struct oh323_peer *peer = NULL;
1434 struct oh323_user *user = NULL;
1435 struct oh323_alias *alias = NULL;
1440 cfg = ast_load(config);
1442 /* We *must* have a config file otherwise stop immediately */
1444 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
1448 /* fire up the H.323 Endpoint */
1449 if (!h323_end_point_exist()) {
1450 h323_end_point_create();
1453 dtmfmode = H323_DTMF_RFC2833;
1455 memset(&bindaddr, 0, sizeof(bindaddr));
1457 v = ast_variable_browse(cfg, "general");
1459 /* Create the interface list */
1460 if (!strcasecmp(v->name, "port")) {
1461 port = (int)strtol(v->value, NULL, 10);
1462 } else if (!strcasecmp(v->name, "bindaddr")) {
1463 if (!(hp = gethostbyname(v->value))) {
1464 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
1466 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
1468 } else if (!strcasecmp(v->name, "allow")) {
1469 format = ast_getformatbyname(v->value);
1471 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
1473 capability |= format;
1474 } else if (!strcasecmp(v->name, "disallow")) {
1475 format = ast_getformatbyname(v->value);
1477 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
1479 capability &= ~format;
1480 } else if (!strcasecmp(v->name, "tos")) {
1481 if (sscanf(v->value, "%i", &format) == 1)
1482 tos = format & 0xff;
1483 else if (!strcasecmp(v->value, "lowdelay"))
1484 tos = IPTOS_LOWDELAY;
1485 else if (!strcasecmp(v->value, "throughput"))
1486 tos = IPTOS_THROUGHPUT;
1487 else if (!strcasecmp(v->value, "reliability"))
1488 tos = IPTOS_RELIABILITY;
1489 else if (!strcasecmp(v->value, "mincost"))
1490 tos = IPTOS_MINCOST;
1491 else if (!strcasecmp(v->value, "none"))
1494 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
1495 } else if (!strcasecmp(v->name, "gatekeeper")) {
1496 if (!strcasecmp(v->value, "DISABLE")) {
1497 gatekeeper_disable = 1;
1499 } else if (!strcasecmp(v->value, "DISCOVER")) {
1500 gatekeeper_disable = 0;
1501 gatekeeper_discover = 1;
1504 gatekeeper_disable = 0;
1506 strncpy(gatekeeper, v->value, sizeof(gatekeeper)-1);
1508 } else if (!strcasecmp(v->name, "secret")) {
1509 strncpy(secret, v->value, sizeof(secret)-1);
1510 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
1511 gkroute = ast_true(v->value);
1512 } else if (!strcasecmp(v->name, "context")) {
1513 strncpy(default_context, v->value, sizeof(default_context)-1);
1514 ast_verbose(VERBOSE_PREFIX_3 " == Setting default context to %s\n", default_context);
1515 } else if (!strcasecmp(v->name, "dtmfmode")) {
1516 if (!strcasecmp(v->value, "inband"))
1517 dtmfmode=H323_DTMF_INBAND;
1518 else if (!strcasecmp(v->value, "rfc2833"))
1519 dtmfmode = H323_DTMF_RFC2833;
1521 ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
1522 dtmfmode = H323_DTMF_RFC2833;
1524 } else if (!strcasecmp(v->name, "UserByAlias")) {
1525 userbyalias = ast_true(v->value);
1526 } else if (!strcasecmp(v->name, "bridge")) {
1527 bridge_default = ast_true(v->value);
1532 cat = ast_category_browse(cfg, NULL);
1534 if (strcasecmp(cat, "general")) {
1535 utype = ast_variable_retrieve(cfg, cat, "type");
1537 if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
1538 user = build_user(cat, ast_variable_browse(cfg, cat));
1540 ast_mutex_lock(&userl.lock);
1541 user->next = userl.users;
1543 ast_mutex_unlock(&userl.lock);
1545 } else if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
1546 peer = build_peer(cat, ast_variable_browse(cfg, cat));
1548 ast_mutex_lock(&peerl.lock);
1549 peer->next = peerl.peers;
1551 ast_mutex_unlock(&peerl.lock);
1553 } else if (!strcasecmp(utype, "h323")) {
1554 alias = build_alias(cat, ast_variable_browse(cfg, cat));
1556 ast_mutex_lock(&aliasl.lock);
1557 alias->next = aliasl.aliases;
1558 aliasl.aliases = alias;
1559 ast_mutex_unlock(&aliasl.lock);
1562 ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
1565 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
1567 cat = ast_category_browse(cfg, cat);
1570 /* Register our H.323 aliases if any*/
1572 if (h323_set_alias(alias)) {
1573 ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
1576 alias = alias->next;
1579 /* Add some capabilities */
1580 if(h323_set_capability(capability, dtmfmode)) {
1581 ast_log(LOG_ERROR, "Capabilities failure, this is bad.\n");
1589 void delete_users(void)
1591 struct oh323_user *user, *userlast;
1592 struct oh323_peer *peer;
1594 /* Delete all users */
1595 ast_mutex_lock(&userl.lock);
1596 for (user=userl.users;user;) {
1602 ast_mutex_unlock(&userl.lock);
1603 ast_mutex_lock(&peerl.lock);
1604 for (peer=peerl.peers;peer;) {
1605 /* Assume all will be deleted, and we'll find out for sure later */
1609 ast_mutex_unlock(&peerl.lock);
1612 void delete_aliases(void)
1614 struct oh323_alias *alias, *aliaslast;
1616 /* Delete all users */
1617 ast_mutex_lock(&aliasl.lock);
1618 for (alias=aliasl.aliases;alias;) {
1623 aliasl.aliases=NULL;
1624 ast_mutex_unlock(&aliasl.lock);
1627 void prune_peers(void)
1629 /* Prune peers who still are supposed to be deleted */
1630 struct oh323_peer *peer, *peerlast, *peernext;
1631 ast_mutex_lock(&peerl.lock);
1633 for (peer=peerl.peers;peer;) {
1634 peernext = peer->next;
1638 peerlast->next = peernext;
1640 peerl.peers = peernext;
1645 ast_mutex_unlock(&peerl.lock);
1655 if (strlen(gatekeeper)) {
1663 /* Possibly register with a GK */
1664 if (gatekeeper_disable == 0) {
1665 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1666 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1677 static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
1679 struct oh323_pvt *p;
1681 if (p && p->rtp && p->bridge) {
1687 static struct ast_rtp *oh323_get_vrtp_peer(struct ast_channel *chan)
1692 static char *convertcap(int cap)
1695 case AST_FORMAT_G723_1:
1697 case AST_FORMAT_GSM:
1699 case AST_FORMAT_ULAW:
1701 case AST_FORMAT_ALAW:
1703 case AST_FORMAT_ADPCM:
1705 case AST_FORMAT_G729A:
1707 case AST_FORMAT_SPEEX:
1709 case AST_FORMAT_ILBC:
1712 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
1718 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp)
1720 /* XXX Deal with Video */
1721 struct oh323_pvt *p;
1722 struct sockaddr_in them;
1723 struct sockaddr_in us;
1726 mode = convertcap(chan->writeformat);
1734 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
1738 ast_rtp_get_peer(rtp, &them);
1739 ast_rtp_get_us(rtp, &us);
1742 h323_native_bridge(p->cd.call_token, inet_ntoa(them.sin_addr), mode);
1748 static struct ast_rtp_protocol oh323_rtp = {
1749 get_rtp_info: oh323_get_rtp_peer,
1750 get_vrtp_info: oh323_get_vrtp_peer,
1751 set_rtp_peer: oh323_set_rtp_peer,
1759 res = reload_config();
1764 /* Make sure we can register our channel type */
1765 if (ast_channel_register(type, tdesc, capability, oh323_request)) {
1766 ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1770 ast_cli_register(&cli_debug);
1771 ast_cli_register(&cli_no_debug);
1772 ast_cli_register(&cli_trace);
1773 ast_cli_register(&cli_no_trace);
1774 ast_cli_register(&cli_show_codecs);
1775 ast_cli_register(&cli_gk_cycle);
1776 ast_cli_register(&cli_hangup_call);
1777 ast_cli_register(&cli_show_tokens);
1779 oh323_rtp.type = type;
1780 ast_rtp_proto_register(&oh323_rtp);
1782 sched = sched_context_create();
1784 ast_log(LOG_WARNING, "Unable to create schedule context\n");
1786 io = io_context_create();
1788 ast_log(LOG_WARNING, "Unable to create I/O context\n");
1791 /* Register our callback functions */
1792 h323_callback_register(setup_incoming_call,
1793 setup_outgoing_call,
1795 setup_rtp_connection,
1801 /* start the h.323 listener */
1802 if (h323_start_listener(port, bindaddr)) {
1803 ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
1804 // h323_end_process();
1808 /* Possibly register with a GK */
1809 if (gatekeeper_disable == 0) {
1810 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1811 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1812 // h323_end_process();
1816 /* And start the monitor for the first time */
1825 struct oh323_pvt *p, *pl;
1827 if (!ast_mutex_lock(&iflock)) {
1828 /* hangup all interfaces if they have an owner */
1832 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1836 ast_mutex_unlock(&iflock);
1838 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1842 if (!ast_mutex_lock(&monlock)) {
1843 if (monitor_thread && (monitor_thread != -2)) {
1844 pthread_cancel(monitor_thread);
1845 pthread_kill(monitor_thread, SIGURG);
1846 pthread_join(monitor_thread, NULL);
1848 monitor_thread = (pthread_t) -2;
1849 ast_mutex_unlock(&monlock);
1851 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
1855 if (!ast_mutex_lock(&iflock)) {
1856 /* destroy all the interfaces and free their memory */
1861 /* free associated memory */
1865 ast_mutex_unlock(&iflock);
1867 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
1873 /* unregister rtp */
1874 ast_rtp_proto_unregister(&oh323_rtp);
1876 /* unregister commands */
1877 ast_cli_unregister(&cli_debug);
1878 ast_cli_unregister(&cli_no_debug);
1879 ast_cli_unregister(&cli_trace);
1880 ast_cli_unregister(&cli_no_trace);
1881 ast_cli_unregister(&cli_show_codecs);
1882 ast_cli_unregister(&cli_gk_cycle);
1883 ast_cli_unregister(&cli_hangup_call);
1884 ast_cli_unregister(&cli_show_tokens);
1886 /* unregister channel type */
1887 ast_channel_unregister(type);
1895 ast_mutex_lock(&usecnt_lock);
1897 ast_mutex_unlock(&usecnt_lock);
1908 return ASTERISK_GPL_KEY;