Properly deal with E.164 and Prefix routing
[asterisk/asterisk.git] / channels / chan_h323.c
1 /*
2  * chan_h323.c
3  *
4  * OpenH323 Channel Driver for ASTERISK PBX.
5  *                      By Jeremy McNamara
6  *                      For The NuFone Network 
7  *
8  * This code has been derived from code created by
9  *              Michael Manousos and Mark Spencer
10  *
11  * This file is part of the chan_h323 driver for Asterisk
12  *
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. 
17  *
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. 
21  *
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. 
25  *
26  * Version Info: $Id$
27  */
28
29
30 #include <stdio.h>
31 #include <pthread.h>
32 #include <string.h>
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>
50 #include <net/if.h>
51 #include <errno.h>
52 #include <unistd.h>
53 #include <stdlib.h>
54 #include <fcntl.h>
55 #include <netdb.h>
56 #include <sys/signal.h>
57 #include <netinet/ip.h>
58
59
60 #include "h323/chan_h323.h"
61
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";
67
68 static char default_context[AST_MAX_EXTENSION];
69
70 /** H.323 configuration values */
71 static char gatekeeper[100];
72 static int  gatekeeper_disable = 1;
73 static int  gatekeeper_discover = 0;
74 static int  usingGk;
75 static int  port = 1720;
76 static int  gkroute = 0;
77
78 /* to find user by alias is default, alternative is the incomming call's source IP address*/
79 static int  userbyalias = 1;
80
81 static int  bridge_default = 1;
82
83 /* Just about everybody seems to support ulaw, so make it a nice default */
84 static int capability = AST_FORMAT_ULAW;
85
86 /* TOS flag */
87 static int tos = 0;
88
89 static int dtmfmode = H323_DTMF_RFC2833;
90
91 static char secret[50];
92
93 /** Private structure of a OpenH323 channel */
94 struct oh323_pvt {
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 */
113         int dtmfmode;
114         struct ast_dsp *vad;                                    /* Used for in-band DTMF detection */
115         struct oh323_pvt *next;                                 /* Next channel in list */
116 } *iflist = NULL;
117
118 static struct ast_user_list {
119         struct oh323_user *users;
120         ast_mutex_t lock;
121 } userl = { NULL, AST_MUTEX_INITIALIZER };
122
123 static struct ast_peer_list {
124         struct oh323_peer *peers;
125         ast_mutex_t lock;
126 } peerl = { NULL, AST_MUTEX_INITIALIZER };
127
128 static struct ast_alias_list {
129         struct oh323_alias *aliases;
130         ast_mutex_t lock;
131 } aliasl = { NULL, AST_MUTEX_INITIALIZER };
132
133 /** Asterisk RTP stuff*/
134 static struct sched_context *sched;
135 static struct io_context *io;
136
137 /** Protect the interface list (of oh323_pvt's) */
138 static ast_mutex_t iflock = AST_MUTEX_INITIALIZER;
139
140 /** Usage counter and associated lock */
141 static int usecnt =0;
142 static ast_mutex_t usecnt_lock = AST_MUTEX_INITIALIZER;
143
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;
147
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 = AST_PTHREADT_NULL;
151
152 static int restart_monitor(void);
153
154 static void __oh323_destroy(struct oh323_pvt *p)
155 {
156         struct oh323_pvt *cur, *prev = NULL;
157         
158         if (p->rtp) {
159                 ast_rtp_destroy(p->rtp);
160         }
161         
162         /* Unlink us from the owner if we have one */
163         if (p->owner) {
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);
168         }
169         cur = iflist;
170         while(cur) {
171                 if (cur == p) {
172                         if (prev)
173                                 prev->next = cur->next;
174                         else
175                                 iflist = cur->next;
176                         break;
177                 }
178                 prev = cur;
179                 cur = cur->next;
180         }
181         if (!cur) {
182                 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
183         } else
184                 free(p);
185 }
186
187 static void oh323_destroy(struct oh323_pvt *p)
188 {
189         ast_mutex_lock(&iflock);
190         __oh323_destroy(p);
191         ast_mutex_unlock(&iflock);
192 }
193
194 static void alias_add_e164(struct oh323_alias *alias, char *val)
195 {
196         struct e164_number *tmp = alias->e164;
197
198         /* Create a new e164 number structure and chain it */
199         alias->e164 = (struct e164_number *)calloc(1, sizeof(struct e164_number));
200         alias->e164->next = tmp;
201
202         strncpy(alias->e164->number, val, E164_MAX_LENGTH-1);
203 }
204
205 static void alias_add_prefix(struct oh323_alias *alias, char *val)
206 {
207         struct e164_number *tmp = alias->prefix;
208
209         /* Create a new e164 number structure and chain it */
210         alias->prefix = (struct e164_number *)calloc(1, sizeof(struct e164_number));
211         alias->prefix->next = tmp;
212
213         strncpy(alias->prefix->number, val, E164_MAX_LENGTH-1);
214 }
215
216 static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
217 {
218         struct oh323_alias *alias;
219         char *p, *n;
220
221         alias = (struct oh323_alias *)malloc(sizeof(struct oh323_alias));
222
223         if (alias) {
224                 memset(alias, 0, sizeof(struct oh323_alias));
225                 strncpy(alias->name, name, sizeof(alias->name)-1);
226
227                 while (v) {
228                         if (!strcasecmp(v->name, "e164")) {
229                                 p = v->value;
230                                 n = strsep(&p, ",");
231                                 while(n) {
232                                         alias_add_e164(alias, n);
233                                         n = strsep(&p, ",");
234                                 }
235                         } else if (!strcasecmp(v->name, "prefix")) {
236                                 p = v->value;
237                                 n = strsep(&p, ",");
238                                 while(n) {
239                                         alias_add_prefix(alias, n);
240                                         n = strsep(&p, ",");
241                                 }
242                         } else if (!strcasecmp(v->name, "context")) {
243                                 strncpy(alias->context,  v->value, sizeof(alias->context)-1);
244                         } else if (!strcasecmp(v->name, "secret")) {
245                                 strncpy(alias->secret,  v->value, sizeof(alias->secret)-1);
246                         } else {
247                                 if (strcasecmp(v->value, "h323")) {     
248                                         ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->value);
249                                 }
250                         }
251                         v = v->next;
252                 }
253         }
254
255         return alias;
256 }
257
258 static struct oh323_user *build_user(char *name, struct ast_variable *v)
259 {
260         struct oh323_user *user;
261         int format;
262         
263         user = (struct oh323_user *)malloc(sizeof(struct oh323_user));
264         if (user) {
265                 memset(user, 0, sizeof(struct oh323_user));
266                 strncpy(user->name, name, sizeof(user->name)-1);
267                 
268                 /* set the usage flag to a sane starting value*/
269                 user->inUse = 0;
270                 /* Assume we can native bridge */
271                 user->bridge = bridge_default; 
272
273                 while(v) {
274                         if (!strcasecmp(v->name, "context")) {
275                                 strncpy(user->context, v->value, sizeof(user->context)-1);
276                         } else if (!strcasecmp(v->name, "bridge")) {
277                                 user->bridge = ast_true(v->value);
278                       } else if (!strcasecmp(v->name, "nat")) {
279                               user->nat = ast_true(v->value);
280                         } else if (!strcasecmp(v->name, "noFastStart")) {
281                                 user->noFastStart = ast_true(v->value);
282                         } else if (!strcasecmp(v->name, "noH245Tunneling")) {
283                                 user->noH245Tunneling = ast_true(v->value);
284                         } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
285                                 user->noSilenceSuppression = ast_true(v->value);
286                         } else if (!strcasecmp(v->name, "secret")) {
287                                 strncpy(user->secret, v->value, sizeof(user->secret)-1);
288                         } else if (!strcasecmp(v->name, "callerid")) {
289                                 strncpy(user->callerid, v->value, sizeof(user->callerid)-1);
290                         } else if (!strcasecmp(v->name, "accountcode")) {
291                                 strncpy(user->accountcode, v->value, sizeof(user->accountcode)-1);
292                         } else if (!strcasecmp(v->name, "incominglimit")) {
293                                 user->incominglimit = atoi(v->value);
294                                 if (user->incominglimit < 0)
295                                         user->incominglimit = 0;
296                         } else if (!strcasecmp(v->name, "host")) {
297                                 if (!strcasecmp(v->value, "dynamic")) {
298                                         ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
299                                         free(user);
300                                         return NULL;
301                                 } else if (ast_get_ip(&user->addr, v->value)) {
302                                         free(user);
303                                         return NULL;
304                                 } 
305                                 /* Let us know we need to use ip authentication */
306                                 user->host = 1;
307                         } else if (!strcasecmp(v->name, "amaflags")) {
308                                 format = ast_cdr_amaflags2int(v->value);
309                                 if (format < 0) {
310                                         ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
311                                 } else {
312                                         user->amaflags = format;
313                                 }
314                         }
315                         v = v->next;
316                 }
317         }
318         return user;
319 }
320
321
322 static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
323 {
324         struct oh323_peer *peer;
325         struct oh323_peer *prev;
326         int found=0;
327         
328         prev = NULL;
329         ast_mutex_lock(&peerl.lock);
330         peer = peerl.peers;
331
332         while(peer) {
333                 if (!strcasecmp(peer->name, name)) {    
334                         break;
335                 }
336                 prev = peer;
337                 peer = peer->next;
338         }
339
340         if (peer) {
341                 found++;
342                 /* Already in the list, remove it and it will be added back (or FREE'd) */
343                 if (prev) {
344                         prev->next = peer->next;
345                 } else {
346                         peerl.peers = peer->next;
347                 }
348                 ast_mutex_unlock(&peerl.lock);
349         } else {
350                 ast_mutex_unlock(&peerl.lock);
351                 peer = malloc(sizeof(struct oh323_peer));
352                 memset(peer, 0, sizeof(struct oh323_peer));
353         }
354         if (peer) {
355                 if (!found) {
356                         strncpy(peer->name, name, sizeof(peer->name)-1);
357                 }
358                 
359                 /* set the usage flag to a sane starting value*/
360                 peer->inUse = 0;
361
362                 while(v) {
363                         if (!strcasecmp(v->name, "context")) {
364                                 strncpy(peer->context, v->value, sizeof(peer->context)-1);
365                         }  else if (!strcasecmp(v->name, "bridge")) {
366                                 peer->bridge = ast_true(v->value);
367                         } else if (!strcasecmp(v->name, "noFastStart")) {
368                                 peer->noFastStart = ast_true(v->value);
369                         } else if (!strcasecmp(v->name, "noH245Tunneling")) {
370                                 peer->noH245Tunneling = ast_true(v->value);
371                         } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
372                                 peer->noSilenceSuppression = ast_true(v->value);
373                         } else if (!strcasecmp(v->name, "outgoinglimit")) {
374                                 peer->outgoinglimit = atoi(v->value);
375                                 if (peer->outgoinglimit > 0)
376                                         peer->outgoinglimit = 0;
377                         } else if (!strcasecmp(v->name, "host")) {
378                                 if (!strcasecmp(v->value, "dynamic")) {
379                                         ast_log(LOG_ERROR, "Dynamic host configuration not implemented, yet!\n");
380                                         free(peer);
381                                         return NULL;
382                                 }
383                                 if (ast_get_ip(&peer->addr, v->value)) {
384                                                 free(peer);
385                                                 return NULL;
386                                 }
387                         } 
388                         v=v->next;
389                 }
390         }
391         return peer;
392 }
393
394
395
396 /**
397  * Send (play) the specified digit to the channel.
398  * 
399  */
400 static int oh323_digit(struct ast_channel *c, char digit)
401 {
402         struct oh323_pvt *p = c->pvt->pvt;
403         if (p && p->rtp && (p->dtmfmode & H323_DTMF_RFC2833)) {
404                 ast_rtp_senddigit(p->rtp, digit);
405         }
406         /* If in-band DTMF is desired, send that */
407         if (p->dtmfmode & H323_DTMF_INBAND)
408                 h323_send_tone(p->cd.call_token, digit);
409         return 0;
410 }
411
412
413 /**
414  * Make a call over the specified channel to the specified 
415  * destination. This function will parse the destination string
416  * and determine the address-number to call.
417  * Return -1 on error, 0 on success.
418  */
419 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
420 {
421         int res;
422         struct oh323_pvt *p = c->pvt->pvt;
423         char called_addr[256];
424         char *tmp;
425
426         strtok_r(dest, "/", &(tmp));
427
428         ast_log(LOG_DEBUG, "dest=%s, timeout=%d.\n", dest, timeout);
429
430         if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
431                 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
432                 return -1;
433         }
434         
435         /* outgoing call */
436         p->outgoing = 1;
437
438         /* Clear the call token */
439         if ((p->cd).call_token == NULL)
440                 (p->cd).call_token = (char *)malloc(128);
441
442         memset((char *)(p->cd).call_token, 0, 128);
443         
444         if (p->cd.call_token == NULL) {
445                 ast_log(LOG_ERROR, "Not enough memory.\n");
446                 return -1;
447         }
448
449         /* Build the address to call */
450         memset(called_addr, 0, sizeof(dest));
451         memcpy(called_addr, dest, sizeof(called_addr));
452
453         /* Copy callerid, if there is any */
454         if (c->callerid) {
455                 char *tmp = strchr(c->callerid, '"');
456                 if (!tmp) {
457                         p->calloptions.callerid = malloc(80); // evil
458                         // sprintf(p->calloptions.callerid, "\"%s\"", c->callerid);
459                         sprintf(p->calloptions.callerid, "\"\" <%s>", c->callerid);
460                 } else {
461                         p->calloptions.callerid = strdup(c->callerid);
462                 }       
463          }
464
465         res = h323_make_call(called_addr, &(p->cd), p->calloptions);
466
467         if (res) {
468                 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
469                 return -1;
470         }
471
472         ast_setstate(c, AST_STATE_RINGING);
473         return 0;
474 }
475
476
477 static int oh323_answer(struct ast_channel *c)
478 {
479         int res;
480
481         struct oh323_pvt *p = c->pvt->pvt;
482
483         res = h323_answering_call(p->cd.call_token, 0);
484         
485         if (c->_state != AST_STATE_UP)
486                 ast_setstate(c, AST_STATE_UP);
487
488         return res;
489 }
490
491 static int oh323_hangup(struct ast_channel *c)
492 {
493         struct oh323_pvt *p = c->pvt->pvt;
494         int needcancel = 0;
495         if (h323debug)
496                 ast_log(LOG_DEBUG, "oh323_hangup(%s)\n", c->name);
497         if (!c->pvt->pvt) {
498                 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
499                 return 0;
500         }
501         ast_mutex_lock(&p->lock);
502         /* Determine how to disconnect */
503         if (p->owner != c) {
504                 ast_log(LOG_WARNING, "Huh?  We aren't the owner?\n");
505                 ast_mutex_unlock(&p->lock);
506                 return 0;
507         }
508         if (!c || (c->_state != AST_STATE_UP))
509                 needcancel = 1;
510         /* Disconnect */
511         p = c->pvt->pvt;
512         
513         /* Free dsp used for in-band DTMF detection */
514         if (p->vad) {
515                 ast_dsp_free(p->vad);
516         }
517
518         p->owner = NULL;
519         c->pvt->pvt = NULL;
520
521         /* Start the process if it's not already started */
522         if (!p->alreadygone) {
523                 p->needdestroy = 1;
524                 if (h323_clear_call((p->cd).call_token)) { 
525                         ast_log(LOG_DEBUG, "ClearCall failed.\n");
526                 }
527         }
528
529         /* Update usage counter */
530         ast_mutex_lock(&usecnt_lock);
531         usecnt--;
532         if (usecnt < 0)
533                 ast_log(LOG_WARNING, "Usecnt < 0\n");
534         ast_mutex_unlock(&usecnt_lock);
535         ast_update_use_count();
536
537         ast_mutex_unlock(&p->lock);
538         return 0;
539 }
540
541 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *p)
542 {
543         /* Retrieve audio/etc from channel.  Assumes p->lock is already held. */
544         struct ast_frame *f;
545         static struct ast_frame null_frame = { AST_FRAME_NULL, };
546
547       /* Only apply it for the first packet, we just need the correct ip/port */
548       if(p->nat)
549       {
550               ast_rtp_setnat(p->rtp,p->nat);
551               p->nat = 0;
552       }
553
554         f = ast_rtp_read(p->rtp);
555         /* Don't send RFC2833 if we're not supposed to */
556         if (f && (f->frametype == AST_FRAME_DTMF) && !(p->dtmfmode & H323_DTMF_RFC2833))
557                 return &null_frame;
558         if (p->owner) {
559                 /* We already hold the channel lock */
560                 if (f->frametype == AST_FRAME_VOICE) {
561                         if (f->subclass != p->owner->nativeformats) {
562                                 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
563                                 p->owner->nativeformats = f->subclass;
564                                 ast_set_read_format(p->owner, p->owner->readformat);
565                                 ast_set_write_format(p->owner, p->owner->writeformat);
566                         }
567                 
568                         /* Do in-band DTMF detection */
569                         if (p->dtmfmode & H323_DTMF_INBAND) {
570                    f = ast_dsp_process(p->owner,p->vad,f,0);
571                                    if (f->frametype == AST_FRAME_DTMF)
572                                         ast_log(LOG_DEBUG, "Got in-band digit %c.\n", f->subclass);
573             }
574                         
575                         
576                 }
577         }
578         return f;
579 }
580
581
582 static struct ast_frame  *oh323_read(struct ast_channel *c)
583 {
584         struct ast_frame *fr;
585         struct oh323_pvt *p = c->pvt->pvt;
586         ast_mutex_lock(&p->lock);
587         fr = oh323_rtp_read(p);
588         ast_mutex_unlock(&p->lock);
589         return fr;
590 }
591
592 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
593 {
594         struct oh323_pvt *p = c->pvt->pvt;
595         int res = 0;
596         if (frame->frametype != AST_FRAME_VOICE) {
597                 if (frame->frametype == AST_FRAME_IMAGE)
598                         return 0;
599                 else {
600                         ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
601                         return 0;
602                 }
603         } else {
604                 if (!(frame->subclass & c->nativeformats)) {
605                         ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
606                                 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
607                         return -1;
608                 }
609         }
610         if (p) {
611                 ast_mutex_lock(&p->lock);
612                 if (p->rtp) {
613                         res =  ast_rtp_write(p->rtp, frame);
614                 }
615                 ast_mutex_unlock(&p->lock);
616         }
617         return res;
618 }
619
620 /** FIXME: Can I acutally use this or does Open H.323 take care of everything? */
621 static int oh323_indicate(struct ast_channel *c, int condition)
622 {
623
624         struct oh323_pvt *p = c->pvt->pvt;
625         
626         switch(condition) {
627         case AST_CONTROL_RINGING:
628                 if (c->_state == AST_STATE_RING) {
629                 //      transmit_response(p, "180 Ringing", &p->initreq);
630                         break;
631                 }
632                 return 0;
633         case AST_CONTROL_BUSY:
634                 if (c->_state != AST_STATE_UP) {
635                 //      transmit_response(p, "600 Busy everywhere", &p->initreq);
636                         p->alreadygone = 1;
637                         ast_softhangup(c, AST_SOFTHANGUP_DEV);
638                         break;
639                 }
640                 return 0;
641         case AST_CONTROL_CONGESTION:
642                 if (c->_state != AST_STATE_UP) {
643                 //      transmit_response(p, "486 Busy here", &p->initreq);
644                         p->alreadygone = 1;
645                         ast_softhangup(c, AST_SOFTHANGUP_DEV);
646                         break;
647                 }
648                 return 0;
649         case -1:
650                 return -1;
651         default:
652                 ast_log(LOG_WARNING, "Don't know how to indicate condition %d\n", condition);
653                 return -1;
654         }
655         return 0;
656 }
657
658 // FIXME: WTF is this? Do I need this???
659 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan, int needlock)
660 {
661         struct oh323_pvt *p = newchan->pvt->pvt;
662
663         if (needlock)
664                 ast_mutex_lock(&p->lock);
665         if (p->owner != oldchan) {
666                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, p->owner);
667                 return -1;
668         }
669         p->owner = newchan;
670         if (needlock)
671                 ast_mutex_unlock(&p->lock);
672         return 0;
673 }
674
675 static struct ast_channel *oh323_new(struct oh323_pvt *i, int state, const char *host)
676 {
677         struct ast_channel *ch;
678         int fmt;
679         ch = ast_channel_alloc(1);
680         
681         if (ch) {
682                 
683                 snprintf(ch->name, sizeof(ch->name)-1, "H323/%s", host);
684                 ch->nativeformats = i->capability;
685                 if (!ch->nativeformats)
686                         ch->nativeformats = capability;
687                 fmt = ast_best_codec(ch->nativeformats);
688                 ch->type = type;
689                 ch->fds[0] = ast_rtp_fd(i->rtp);
690                 ast_setstate(ch, state);
691                 
692                 if (state == AST_STATE_RING)
693                         ch->rings = 1;
694                 
695                 ch->writeformat = fmt;
696                 ch->pvt->rawwriteformat = fmt;
697                 ch->readformat = fmt;
698                 ch->pvt->rawreadformat = fmt;
699                 
700                 /* Allocate dsp for in-band DTMF support */
701                 if (i->dtmfmode & H323_DTMF_INBAND) {
702                         i->vad = ast_dsp_new();
703                         ast_dsp_set_features(i->vad, DSP_FEATURE_DTMF_DETECT);
704                 }
705
706                 /* Register the OpenH323 channel's functions. */
707                 ch->pvt->pvt = i;
708                 ch->pvt->send_digit = oh323_digit;
709                 ch->pvt->call = oh323_call;
710                 ch->pvt->hangup = oh323_hangup;
711                 ch->pvt->answer = oh323_answer;
712                 ch->pvt->read = oh323_read;
713                 ch->pvt->write = oh323_write;
714                 ch->pvt->indicate = oh323_indicate;
715                 ch->pvt->fixup = oh323_fixup;
716 //              ch->pvt->bridge = ast_rtp_bridge;
717
718                 /*  Set the owner of this channel */
719                 i->owner = ch;
720                 
721                 ast_mutex_lock(&usecnt_lock);
722                 usecnt++;
723                 ast_mutex_unlock(&usecnt_lock);
724                 ast_update_use_count();
725                 strncpy(ch->context, i->context, sizeof(ch->context)-1);
726                 strncpy(ch->exten, i->exten, sizeof(ch->exten)-1);              
727                 ch->priority = 1;
728                 if (strlen(i->callerid))
729                         ch->callerid = strdup(i->callerid);
730                 if (strlen(i->accountcode))
731                         strncpy(ch->accountcode, i->accountcode, sizeof(ch->accountcode)-1);
732                 if (i->amaflags)
733                         ch->amaflags = i->amaflags;
734                 if (state != AST_STATE_DOWN) {
735                         if (ast_pbx_start(ch)) {
736                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
737                                 ast_hangup(ch);
738                                 ch = NULL;
739                         }
740                 }
741         } else
742                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
743         return ch;
744 }
745
746 static struct oh323_pvt *oh323_alloc(int callid)
747 {
748         struct oh323_pvt *p;
749
750         p = malloc(sizeof(struct oh323_pvt));
751         if (!p) {
752                 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
753                 return NULL;
754         }
755
756         /* Keep track of stuff */
757         memset(p, 0, sizeof(struct oh323_pvt));
758         p->rtp = ast_rtp_new(sched, io, 1, 0);
759
760         if (!p->rtp) {
761                 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
762                 free(p);
763                 return NULL;
764         }
765         ast_rtp_settos(p->rtp, tos);
766         ast_mutex_init(&p->lock);
767         
768         p->cd.call_reference = callid;
769         p->bridge = bridge_default;
770         
771         p->dtmfmode = dtmfmode;
772         if (p->dtmfmode & H323_DTMF_RFC2833)
773                 p->nonCodecCapability |= AST_RTP_DTMF;
774
775         /* Add to interface list */
776         ast_mutex_lock(&iflock);
777         p->next = iflist;
778         iflist = p;
779         ast_mutex_unlock(&iflock);
780         return p;
781 }
782
783 static struct oh323_pvt *find_call(int call_reference)
784 {  
785         struct oh323_pvt *p;
786
787         ast_mutex_lock(&iflock);
788         p = iflist; 
789
790         while(p) {
791                 if (p->cd.call_reference == call_reference) {
792                         /* Found the call */                                            
793                         ast_mutex_unlock(&iflock);
794                         return p;
795                 }
796                 p = p->next; 
797         }
798         ast_mutex_unlock(&iflock);
799         return NULL;
800 }
801
802 static struct ast_channel *oh323_request(char *type, int format, void *data)
803 {
804
805         int oldformat;
806         struct oh323_pvt *p;
807         struct ast_channel *tmpc = NULL;
808         char *dest = data;
809         char *ext, *host;
810         char *h323id = NULL;
811         char tmp[256];
812
813         
814         ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
815
816         oldformat = format;
817         format &= capability;
818         if (!format) {
819                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
820                 return NULL;
821         }
822         
823         strncpy(tmp, dest, sizeof(tmp) - 1);
824                 
825         host = strchr(tmp, '@');
826         if (host) {
827                 *host = '\0';
828                 host++;
829                 ext = tmp;
830         } else {
831                 host = tmp;
832                 ext = NULL;
833         }
834
835         strtok_r(host, "/", &(h323id));
836                 
837         if (*h323id) {
838                 h323_set_id(h323id);
839         }
840                 
841         
842         p = oh323_alloc(0);
843
844         if (!p) {
845                 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
846                 return NULL;
847         }
848
849         /* Assign a default capability */
850         p->capability = capability;
851
852         if (p->dtmfmode) {
853                 if (p->dtmfmode & H323_DTMF_RFC2833)
854                         p->nonCodecCapability |= AST_RTP_DTMF;
855                 else
856                         p->nonCodecCapability &= ~AST_RTP_DTMF;
857         }
858
859         if (ext) {
860                 strncpy(p->username, ext, sizeof(p->username) - 1);
861         }
862         ast_log(LOG_DEBUG, "Host: %s\tUsername: %s\n", host, p->username);
863
864         tmpc = oh323_new(p, AST_STATE_DOWN, host);
865         if (!tmpc)
866                 oh323_destroy(p);
867         
868         restart_monitor();
869         
870         return tmpc;
871 }
872
873 struct oh323_alias *find_alias(const char *source_aliases)
874 {
875         struct oh323_alias *a;
876
877         ast_mutex_lock(&aliasl.lock);
878         a = aliasl.aliases;
879
880         while(a) {
881
882                 if (!strcasecmp(a->name, source_aliases)) {
883                         break;
884                 }
885                 a = a->next;
886         }
887
888         ast_mutex_unlock(&aliasl.lock);
889         return a;
890 }
891
892 struct oh323_alias *find_e164(const char *source_aliases)
893 {
894         struct oh323_alias *a;
895         struct e164_number *num;
896         int found = 0;
897
898         ast_mutex_lock(&aliasl.lock);
899         a = aliasl.aliases;
900
901         while(a && !found) {
902                 if(a->e164) {
903                         num = a->e164;
904                         while(num) {
905                                 if(!strncmp(num->number, source_aliases, E164_MAX_LENGTH)) {
906                                         found = 1;
907                                         break;
908                                 }
909                         num = num->next;
910                 }
911         }
912         if(!found)
913                 a = a->next;
914         }
915         ast_mutex_unlock(&aliasl.lock);
916
917         return a;
918 }
919
920 struct oh323_alias *find_prefix(const char *source_aliases)
921 {
922         struct oh323_alias *a;
923         struct e164_number *num;
924         int found = 0;
925
926         ast_mutex_lock(&aliasl.lock);
927
928         a = aliasl.aliases;
929
930         while(a && !found) {
931         if(a->prefix) {
932                 num = a->prefix;
933                 while(num) {
934                         if(strlen(source_aliases) >= strlen(num->number) &&
935                            !strncmp(num->number, source_aliases, strlen(num->number))) {
936                                 found = 1;
937                                 break;
938                         }
939                         num = num->next;
940                 }
941         }
942         if(!found)
943                 a = a->next;
944         }
945         ast_mutex_unlock(&aliasl.lock);
946
947         return a;
948 }
949
950 struct oh323_user *find_user(const call_details_t cd)
951 {
952         struct oh323_user *u;
953
954         u = userl.users;
955         if(userbyalias == 1){
956                 while(u) {
957                         if (!strcasecmp(u->name, cd.call_source_aliases)) {
958                                 break;
959                         }
960                         u = u->next;
961                 }
962
963         } else {
964                 while(u) {
965                         if (!strcasecmp(cd.sourceIp, inet_ntoa(u->addr.sin_addr))) {
966                                 break;
967                         }
968                         u = u->next;
969                 }
970
971         
972         }
973         return u;
974
975 }
976
977 struct oh323_peer *find_peer(char *dest_peer)
978 {
979         struct oh323_peer *p;
980
981         p = peerl.peers;
982
983         while(p) {
984                 if (!strcasecmp(p->name, dest_peer)) {
985                         break;
986                 }
987                 p = p->next;
988         }
989         return p;
990
991 }
992
993 /**
994   * Callback for sending digits from H.323 up to asterisk
995   *
996   */
997 int send_digit(unsigned call_reference, char digit)
998 {
999         struct oh323_pvt *p;
1000         struct ast_frame f;
1001
1002         ast_log(LOG_DEBUG, "Recieved Digit: %c\n", digit);
1003         p = find_call(call_reference);
1004
1005         if (!p) {
1006                 ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
1007                 return -1;
1008         }
1009         memset(&f, 0, sizeof(f));
1010         f.frametype = AST_FRAME_DTMF;
1011     f.subclass = digit;
1012     f.datalen = 0;
1013     f.samples = 300;
1014         f.offset = 0;
1015         f.data = NULL;
1016     f.mallocd = 0;
1017     f.src = "SEND_DIGIT";
1018         
1019         return ast_queue_frame(p->owner, &f, 1);        
1020 }
1021
1022 /**
1023   * Call-back function that gets called when any H.323 connection is made
1024   *
1025   * Returns the local RTP information
1026   */
1027 struct rtp_info *create_connection(unsigned call_reference)
1028 {       
1029         struct oh323_pvt *p;
1030         struct sockaddr_in us;
1031         struct sockaddr_in them;
1032         struct rtp_info *info;
1033
1034         info = malloc(sizeof(struct rtp_info));
1035
1036         p = find_call(call_reference);
1037
1038         if (!p) {
1039                 ast_log(LOG_ERROR, "Unable to allocate private structure, this is very bad.\n");
1040                 return NULL;
1041         }
1042
1043         /* figure out our local RTP port and tell the H.323 stack about it*/
1044         ast_rtp_get_us(p->rtp, &us);
1045         ast_rtp_get_peer(p->rtp, &them);
1046
1047
1048         info->addr = inet_ntoa(us.sin_addr);
1049         info->port = ntohs(us.sin_port);
1050
1051
1052         return info;
1053 }
1054
1055 /**
1056  *  Call-back function for incoming calls
1057  *
1058  *  Returns 1 on success
1059  */
1060
1061 int setup_incoming_call(call_details_t cd)
1062 {
1063         
1064         struct oh323_pvt *p = NULL;
1065         struct ast_channel *c = NULL;
1066         struct oh323_user *user = NULL;
1067         struct oh323_alias *alias = NULL;
1068
1069         /* allocate the call*/
1070         p = oh323_alloc(cd.call_reference);
1071
1072         if (!p) {
1073                 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
1074                 return 0;
1075         }
1076
1077         /* Populate the call details in the private structure */
1078         p->cd.call_token = cd.call_token;
1079         p->cd.call_source_aliases = cd.call_source_aliases;
1080         p->cd.call_dest_alias = cd.call_dest_alias;
1081         p->cd.call_source_e164 = cd.call_source_e164;
1082         p->cd.call_dest_e164 = cd.call_dest_e164;
1083
1084         if (h323debug) {
1085                 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
1086                 ast_verbose(VERBOSE_PREFIX_3 "     Call token:  [%s]\n", p->cd.call_token);
1087                 ast_verbose(VERBOSE_PREFIX_3 "     Calling party name:  [%s]\n", p->cd.call_source_aliases);
1088                 ast_verbose(VERBOSE_PREFIX_3 "     Calling party number:  [%s]\n", p->cd.call_source_e164);
1089                 ast_verbose(VERBOSE_PREFIX_3 "     Called  party name:  [%s]\n", p->cd.call_dest_alias);
1090                 ast_verbose(VERBOSE_PREFIX_3 "     Called  party number:  [%s]\n", p->cd.call_dest_e164);
1091         }
1092
1093         /* Decide if we are allowing Gatekeeper routed calls*/
1094         if ((!strcasecmp(cd.sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk == 1)) {
1095                 
1096                 if (strlen(cd.call_dest_e164)) {
1097                         alias = find_e164(cd.call_dest_e164);
1098                         if(!alias)
1099                                 alias = find_prefix(cd.call_dest_e164);
1100             
1101                         if(!alias)
1102                                 ast_log(LOG_WARNING, "Call for '%s' could not be routed to a context, sending to default.\n", cd.call_dest_e164);
1103
1104                         strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1105                         strncpy(p->context, (alias?alias->context:default_context), sizeof(p->context)-1); 
1106                 } else {
1107                         alias = find_alias(cd.call_dest_alias);
1108                 
1109                         if (!alias) {
1110                                 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd.call_dest_alias);
1111                                 return 0;
1112                         }
1113                         strncpy(p->exten, alias->name, sizeof(p->exten)-1);
1114                         strncpy(p->context, alias->context, sizeof(p->context)-1);
1115                 }
1116
1117
1118                 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164);
1119
1120         } else { 
1121                 /* Either this call is not from the Gatekeeper 
1122                    or we are not allowing gk routed calls */
1123                 
1124
1125                 user  = find_user(cd);
1126
1127
1128                 if (!user) {
1129                         sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164); 
1130                         if (strlen(p->cd.call_dest_e164)) {
1131                                 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1132                         } else {
1133                                 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);              
1134                         }
1135                         if (!strlen(default_context)) {
1136                                 ast_log(LOG_ERROR, "Call from user '%s' rejected due to no default context\n", p->cd.call_source_aliases);
1137                                 return 0;
1138                         }
1139                         strncpy(p->context, default_context, sizeof(p->context)-1);
1140                         ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd.call_source_aliases, p->context);
1141                 } else {                                        
1142                         if (user->host) {
1143                                 if (strcasecmp(cd.sourceIp, inet_ntoa(user->addr.sin_addr))){
1144                                         
1145                                         if(!strlen(default_context)) {
1146                                                 ast_log(LOG_ERROR, "Call from user '%s' rejected due to non-matching IP address of '%s'\n", user->name, cd.sourceIp);
1147                                                 return 0;
1148                                         }
1149                                         
1150                                         strncpy(p->context, default_context, sizeof(p->context)-1);
1151                                         sprintf(p->exten,"i");
1152
1153                                         goto exit;                                      
1154                                 }
1155                         }
1156                         if (user->incominglimit > 0) {
1157                                 if (user->inUse >= user->incominglimit) {
1158                                         ast_log(LOG_ERROR, "Call from user '%s' rejected due to usage limit of %d\n", user->name, user->incominglimit);
1159                                         return 0;
1160                                 }
1161                         }
1162                         strncpy(p->context, user->context, sizeof(p->context)-1);
1163                         p->bridge = user->bridge;
1164                       p->nat = user->nat;
1165
1166                         if (strlen(user->callerid)) 
1167                                 strncpy(p->callerid, user->callerid, sizeof(p->callerid) - 1);
1168                         else
1169                                 sprintf(p->callerid, "%s <%s>", p->cd.call_source_aliases, p->cd.call_source_e164); 
1170
1171                         if (strlen(p->cd.call_dest_e164)) {
1172                                 strncpy(p->exten, cd.call_dest_e164, sizeof(p->exten)-1);
1173                         } else {
1174                                 strncpy(p->exten, cd.call_dest_alias, sizeof(p->exten)-1);              
1175                         }
1176                         if (strlen(user->accountcode)) {
1177                                 strncpy(p->accountcode, user->accountcode, sizeof(p->accountcode)-1);
1178                         } 
1179
1180                         /* Increment the usage counter */
1181                         user->inUse++;
1182                 } 
1183         }
1184
1185 /* I know this is horrid, don't kill me saddam */
1186 exit:
1187         /* allocate a channel and tell asterisk about it */
1188         c = oh323_new(p, AST_STATE_RINGING, cd.call_token);
1189         if (!c) {
1190                 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
1191                 return 0;
1192         }
1193
1194         return 1;
1195 }
1196
1197 /**
1198  * Call-back function to establish an outgoing H.323 call
1199  * 
1200  * Returns 1 on success 
1201  */
1202 int setup_outgoing_call(call_details_t cd)
1203 {       
1204         return 1;
1205 }
1206
1207 #if 0
1208 if (p->inUse >= p->outgoinglimit) {
1209         ast_log(LOG_ERROR, "Call to %s rejected due to usage limit of %d outgoing channels\n", p->name, p->inUse);
1210         return 0;
1211 }
1212
1213 if (!p) {
1214         ast_log(LOG_ERROR, "Rejecting call: peer %s not found\n", dest_peer);
1215         return 0;
1216 }
1217 #endif
1218
1219 /**
1220   * Call-back function that gets called for each rtp channel opened 
1221   *
1222   * Returns nothing 
1223   */
1224 void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort)
1225 {
1226         struct oh323_pvt *p = NULL;
1227         struct sockaddr_in them;
1228
1229         /* Find the call or allocate a private structure if call not found */
1230         p = find_call(call_reference);
1231
1232         if (!p) {
1233                 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1234                 return;
1235         }
1236
1237         them.sin_family = AF_INET;
1238         them.sin_addr.s_addr = inet_addr(remoteIp); // only works for IPv4
1239         them.sin_port = htons(remotePort);
1240         ast_rtp_set_peer(p->rtp, &them);
1241
1242         return;
1243 }
1244
1245 /**  
1246   *     Call-back function to signal asterisk that the channel has been answered 
1247   * Returns nothing
1248   */
1249 void connection_made(unsigned call_reference)
1250 {
1251         struct ast_channel *c = NULL;
1252         struct oh323_pvt *p = NULL;
1253         
1254         p = find_call(call_reference);
1255         
1256         if (!p)
1257                 ast_log(LOG_ERROR, "Something is wrong: connection\n");
1258
1259
1260         if (!p->owner) {
1261                 ast_log(LOG_ERROR, "Channel has no owner\n");
1262                 return;
1263         }
1264         c = p->owner;   
1265
1266         ast_setstate(c, AST_STATE_UP);
1267         return;
1268 }
1269
1270 /**
1271   * Call-back function to cleanup communication
1272   * Returns nothing,
1273   */
1274 void cleanup_connection(call_details_t cd)
1275 {       
1276         struct oh323_pvt *p = NULL;
1277 //      struct oh323_peer *peer = NULL;
1278         struct oh323_user *user = NULL;
1279         struct ast_rtp *rtp = NULL;
1280
1281         ast_log(LOG_DEBUG, "Cleaning up our mess\n");
1282         
1283         p = find_call(cd.call_reference);
1284
1285         if (!p) {
1286                 return;
1287         }
1288
1289         /* Decrement usage counter */
1290         if (!p->outgoing) {
1291                 user = find_user(cd);
1292                 
1293                 if(user)
1294                         user->inUse--;
1295         }
1296
1297 #if 0
1298                 if (p->outgoing) {
1299                 peer = find_peer(cd.call_dest_alias);
1300                 peer->inUse--;
1301         } else {
1302                 user = find_user(cd);
1303                 user->inUse--;
1304         }
1305 #endif
1306         
1307         if (p->rtp) {
1308                 rtp = p->rtp;
1309                 p->rtp = NULL;
1310                 /* Immediately stop RTP */
1311                 ast_rtp_destroy(rtp);
1312         }
1313         
1314         p->alreadygone = 1;
1315
1316         /* Send hangup */       
1317         if (p->owner)
1318                 ast_queue_hangup(p->owner, 1);
1319
1320         p = NULL;
1321         return; 
1322 }
1323
1324 static void *do_monitor(void *data)
1325 {
1326         int res;
1327         struct oh323_pvt *oh323 = NULL;
1328         
1329                 for(;;) {
1330                 /* Check for interfaces needing to be killed */
1331                 ast_mutex_lock(&iflock);
1332 restartsearch:          
1333                 oh323 = iflist;
1334                 while(oh323) {
1335                         if (oh323->needdestroy) {
1336                                 __oh323_destroy(oh323);
1337                                 goto restartsearch;
1338                         }
1339                         oh323 = oh323->next;
1340                 }
1341                 ast_mutex_unlock(&iflock);
1342
1343                 /* Wait for sched or io */
1344                 res = ast_sched_wait(sched);
1345                 if ((res < 0) || (res > 1000))
1346                         res = 1000;
1347                 res = ast_io_wait(io, res);
1348
1349                 pthread_testcancel();
1350
1351                 ast_mutex_lock(&monlock);
1352                 if (res >= 0) 
1353                         ast_sched_runq(sched);
1354                 ast_mutex_unlock(&monlock);
1355         }
1356         /* Never reached */
1357         return NULL;
1358         
1359 }
1360
1361 static int restart_monitor(void)
1362 {
1363         /* If we're supposed to be stopped -- stay stopped */
1364         if (monitor_thread == AST_PTHREADT_STOP)
1365                 return 0;
1366         if (ast_mutex_lock(&monlock)) {
1367                 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1368                 return -1;
1369         }
1370         if (monitor_thread == pthread_self()) {
1371                 ast_mutex_unlock(&monlock);
1372                 ast_log(LOG_WARNING, "Cannot kill myself\n");
1373                 return -1;
1374         }
1375         if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
1376                 /* Wake up the thread */
1377                 pthread_kill(monitor_thread, SIGURG);
1378         } else {
1379                 /* Start a new monitor */
1380                 if (pthread_create(&monitor_thread, NULL, do_monitor, NULL) < 0) {
1381                         ast_mutex_unlock(&monlock);
1382                         ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1383                         return -1;
1384                 }
1385         }
1386         ast_mutex_unlock(&monlock);
1387         return 0;
1388 }
1389
1390 static int h323_do_trace(int fd, int argc, char *argv[])
1391 {
1392         if (argc != 3) {
1393                 return RESULT_SHOWUSAGE;
1394         }
1395         h323_debug(1, atoi(argv[2]));
1396         ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
1397         return RESULT_SUCCESS;
1398 }
1399
1400 static int h323_no_trace(int fd, int argc, char *argv[])
1401 {
1402         if (argc != 3) {
1403                 return RESULT_SHOWUSAGE;
1404         }
1405         h323_debug(0,0);
1406         ast_cli(fd, "H.323 trace disabled\n");
1407         return RESULT_SUCCESS;
1408 }
1409
1410 static int h323_do_debug(int fd, int argc, char *argv[])
1411 {
1412         if (argc != 2) {
1413                 return RESULT_SHOWUSAGE;
1414         }
1415         h323debug = 1;
1416         ast_cli(fd, "H323 debug enabled\n");
1417         return RESULT_SUCCESS;
1418 }
1419
1420 static int h323_no_debug(int fd, int argc, char *argv[])
1421 {
1422         if (argc != 3) {
1423                 return RESULT_SHOWUSAGE;
1424         }
1425         h323debug = 0;
1426         ast_cli(fd, "H323 Debug disabled\n");
1427         return RESULT_SUCCESS;
1428 }
1429
1430 static int h323_gk_cycle(int fd, int argc, char *argv[])
1431 {
1432         return RESULT_SUCCESS;
1433 #if 0
1434         if (argc != 3) {
1435                 return RESULT_SHOWUSAGE;
1436         }       
1437         h323_gk_urq();
1438         
1439         /* Possibly register with a GK */
1440         if (!gatekeeper_disable) {
1441                 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1442                         ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1443                 }
1444         }
1445         return RESULT_SUCCESS;
1446 #endif
1447 }
1448
1449 static int h323_ep_hangup(int fd, int argc, char *argv[])
1450 {
1451
1452         if (argc != 3) {
1453                 return RESULT_SHOWUSAGE;
1454         }
1455
1456         if (h323_soft_hangup(argv[2])) {
1457                 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
1458         } else { 
1459                 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
1460         }
1461
1462         return RESULT_SUCCESS;
1463 }
1464
1465 static int h323_tokens_show(int fd, int argc, char *argv[])
1466 {
1467
1468         if (argc != 3) {
1469                 return RESULT_SHOWUSAGE;
1470         }
1471         h323_show_tokens();
1472
1473         return RESULT_SUCCESS;
1474 }
1475
1476 static int h323_show_aliases(int fd, int argc, char *argv[])
1477 {
1478         struct oh323_alias *alias;
1479         struct e164_number *num;
1480
1481         if (argc != 3) {
1482                 return RESULT_SHOWUSAGE;
1483         }
1484         ast_cli(fd, "H323 Configured Aliases/E164/Prefixes:\n");
1485
1486         ast_mutex_lock(&aliasl.lock);
1487         alias = aliasl.aliases;
1488
1489         if(!alias) {
1490                 ast_cli(fd, "  Nothing configured!\n");
1491                 ast_mutex_unlock(&aliasl.lock);
1492                 return RESULT_SUCCESS;
1493         }
1494
1495         ast_cli(fd, "%-20s %-5s %-20s %-20s\n", "Alias", "Type", "E164", "Context");
1496
1497         while(alias) {
1498                 ast_cli(fd, "%-20s %-5s %-20s %-20s\n", alias->name, "id", "", alias->context);
1499
1500                 num = alias->e164;
1501                 while(num) {
1502                         ast_cli(fd, "%-20s %-5s %-20s %-20s\n", alias->name, "e164", num->number, alias->context);
1503                         num = num->next;
1504                 }
1505
1506                 num = alias->prefix;
1507                 while(num) {
1508                         ast_cli(fd, "%-20s %-5s %-20s %-20s\n", alias->name, "pfx", num->number, alias->context);
1509                         num = num->next;
1510                 }
1511
1512                 alias = alias->next;
1513         }
1514
1515         ast_mutex_unlock(&aliasl.lock);
1516         return RESULT_SUCCESS;
1517 }
1518
1519 static char trace_usage[] = 
1520 "Usage: h.323 trace <level num>\n"
1521 "       Enables H.323 stack tracing for debugging purposes\n";
1522
1523 static char no_trace_usage[] = 
1524 "Usage: h.323 no trace\n"
1525 "       Disables H.323 stack tracing for debugging purposes\n";
1526
1527 static char debug_usage[] = 
1528 "Usage: h.323 debug\n"
1529 "       Enables chan_h323 debug output\n";
1530
1531 static char no_debug_usage[] = 
1532 "Usage: h.323 no debug\n"
1533 "       Disables chan_h323 debug output\n";
1534
1535 static char show_codec_usage[] = 
1536 "Usage: h.323 show codec\n"
1537 "       Shows all enabled codecs\n";
1538
1539 static char show_cycle_usage[] = 
1540 "Usage: h.323 gk cycle\n"
1541 "       Manually re-register with the Gatekeper\n";
1542
1543 static char show_hangup_usage[] = 
1544 "Usage: h.323 hangup <token>\n"
1545 "       Manually try to hang up call identified by <token>\n";
1546
1547 static char show_tokens_usage[] = 
1548 "Usage: h.323 show tokens\n"
1549 "       Print out all active call tokens\n";
1550
1551 static char show_aliases_usage[] =
1552 "Usage: h.323 show aliases\n"
1553 "       Print out all configured aliases\n";
1554
1555 static struct ast_cli_entry  cli_trace =
1556         { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
1557 static struct ast_cli_entry  cli_no_trace =
1558         { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
1559 static struct ast_cli_entry  cli_debug =
1560         { { "h.323", "debug", NULL }, h323_do_debug, "Enable chan_h323 debug", debug_usage };
1561 static struct ast_cli_entry  cli_no_debug =
1562         { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable chan_h323 debug", no_debug_usage };
1563 static struct ast_cli_entry  cli_show_codecs =
1564         { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
1565 static struct ast_cli_entry  cli_gk_cycle =
1566         { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
1567 static struct ast_cli_entry  cli_hangup_call =
1568         { { "h.323", "hangup", NULL }, h323_ep_hangup, "Manually try to hang up a call", show_hangup_usage };
1569 static struct ast_cli_entry  cli_show_tokens =
1570         { { "h.323", "show", "tokens", NULL }, h323_tokens_show, "Manually try to hang up a call", show_tokens_usage };
1571 static struct ast_cli_entry  cli_show_aliases =
1572     { { "h.323", "show", "aliases", NULL }, h323_show_aliases, "Show configured aliases", show_aliases_usage };
1573
1574
1575 int reload_config(void)
1576 {
1577         
1578         int format;
1579         struct ast_config *cfg;
1580         struct ast_variable *v;
1581         struct oh323_peer *peer   = NULL;
1582         struct oh323_user *user   = NULL;
1583         struct oh323_alias *alias = NULL;
1584         struct hostent *hp;
1585         char *cat;
1586     char *utype;
1587         
1588         cfg = ast_load(config);
1589
1590         /* We *must* have a config file otherwise stop immediately */
1591         if (!cfg) {
1592                 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
1593                 return 1;
1594         }
1595         
1596        /* fire up the H.323 Endpoint */       
1597         if (!h323_end_point_exist()) {
1598                h323_end_point_create();        
1599         }
1600         h323debug=0;
1601         dtmfmode = H323_DTMF_RFC2833;
1602
1603         memset(&bindaddr, 0, sizeof(bindaddr));
1604
1605         v = ast_variable_browse(cfg, "general");
1606         while(v) {
1607                 /* Create the interface list */
1608                 if (!strcasecmp(v->name, "port")) {
1609                         port = (int)strtol(v->value, NULL, 10);
1610                 } else if (!strcasecmp(v->name, "bindaddr")) {
1611                         if (!(hp = gethostbyname(v->value))) {
1612                                 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
1613                         } else {
1614                                 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
1615                         }
1616                 } else if (!strcasecmp(v->name, "allow")) {
1617                         format = ast_getformatbyname(v->value);
1618                         if (format < 1) 
1619                                 ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
1620                         else
1621                                 capability |= format;
1622                 } else if (!strcasecmp(v->name, "disallow")) {
1623                         format = ast_getformatbyname(v->value);
1624                         if (format < 1) 
1625                                 ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
1626                         else
1627                                 capability &= ~format;
1628                 } else if (!strcasecmp(v->name, "tos")) {
1629                         if (sscanf(v->value, "%i", &format) == 1)
1630                                 tos = format & 0xff;
1631                         else if (!strcasecmp(v->value, "lowdelay"))
1632                                 tos = IPTOS_LOWDELAY;
1633                         else if (!strcasecmp(v->value, "throughput"))
1634                                 tos = IPTOS_THROUGHPUT;
1635                         else if (!strcasecmp(v->value, "reliability"))
1636                                 tos = IPTOS_RELIABILITY;
1637                         else if (!strcasecmp(v->value, "mincost"))
1638                                 tos = IPTOS_MINCOST;
1639                         else if (!strcasecmp(v->value, "none"))
1640                                 tos = 0;
1641                         else
1642                                 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
1643                 } else if (!strcasecmp(v->name, "gatekeeper")) {
1644                         if (!strcasecmp(v->value, "DISABLE")) {
1645                                 gatekeeper_disable = 1;
1646                                 usingGk = 0;
1647                         } else if (!strcasecmp(v->value, "DISCOVER")) {
1648                                 gatekeeper_disable = 0;
1649                                 gatekeeper_discover = 1;
1650                                 usingGk = 1;
1651                         } else {
1652                                 gatekeeper_disable = 0;
1653                                 usingGk = 1;
1654                                 strncpy(gatekeeper, v->value, sizeof(gatekeeper)-1);
1655                         }
1656                 } else if (!strcasecmp(v->name, "secret")) {
1657                                 strncpy(secret, v->value, sizeof(secret)-1);
1658                 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
1659                                 gkroute = ast_true(v->value);
1660                 } else if (!strcasecmp(v->name, "context")) {
1661                         strncpy(default_context, v->value, sizeof(default_context)-1);
1662                         ast_verbose(VERBOSE_PREFIX_3 "  == Setting default context to %s\n", default_context);  
1663                 } else if (!strcasecmp(v->name, "dtmfmode")) {
1664                         if (!strcasecmp(v->value, "inband"))
1665                                 dtmfmode=H323_DTMF_INBAND;
1666                         else if (!strcasecmp(v->value, "rfc2833"))
1667                                 dtmfmode = H323_DTMF_RFC2833;
1668                         else {
1669                                 ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
1670                                 dtmfmode = H323_DTMF_RFC2833;
1671                         }
1672                 } else if (!strcasecmp(v->name, "UserByAlias")) {
1673                         userbyalias = ast_true(v->value);
1674                 } else if (!strcasecmp(v->name, "bridge")) {
1675                         bridge_default = ast_true(v->value);
1676                 }
1677                 v = v->next;    
1678         }
1679         
1680         cat = ast_category_browse(cfg, NULL);
1681         while(cat) {
1682                 if (strcasecmp(cat, "general")) {
1683                         utype = ast_variable_retrieve(cfg, cat, "type");
1684                         if (utype) {
1685                                 if (!strcasecmp(utype, "user") || !strcasecmp(utype, "friend")) {
1686                                         user = build_user(cat, ast_variable_browse(cfg, cat));
1687                                         if (user) {
1688                                                 ast_mutex_lock(&userl.lock);
1689                                                 user->next = userl.users;
1690                                                 userl.users = user;
1691                                                 ast_mutex_unlock(&userl.lock);
1692                                         }
1693                                 }  else if (!strcasecmp(utype, "peer") || !strcasecmp(utype, "friend")) {
1694                                         peer = build_peer(cat, ast_variable_browse(cfg, cat));
1695                                         if (peer) {
1696                                                 ast_mutex_lock(&peerl.lock);
1697                                                 peer->next = peerl.peers;
1698                                                 peerl.peers = peer;
1699                                                 ast_mutex_unlock(&peerl.lock);
1700                                         }
1701                                 }  else if (!strcasecmp(utype, "h323")) {                       
1702                                         alias = build_alias(cat, ast_variable_browse(cfg, cat));
1703                                         if (alias) {
1704                                                 ast_mutex_lock(&aliasl.lock);
1705                                                 alias->next = aliasl.aliases;
1706                                                 aliasl.aliases = alias;
1707                                                 ast_mutex_unlock(&aliasl.lock);
1708                                         }
1709                                 } else {
1710                                         ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
1711                                 }
1712                         } else
1713                                 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
1714                 }
1715                 cat = ast_category_browse(cfg, cat);
1716         }
1717
1718         /* Register our H.323 aliases if any*/
1719         while (alias) {         
1720                 if (h323_set_alias(alias)) {
1721                         ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
1722                         return -1;
1723                 }       
1724                 alias = alias->next;
1725         }
1726
1727         /* Add some capabilities */
1728         if(h323_set_capability(capability, dtmfmode)) {
1729                 ast_log(LOG_ERROR, "Capabilities failure, this is bad.\n");
1730                 return -1;
1731         }       
1732         ast_destroy(cfg);
1733
1734         return 0;
1735 }
1736
1737 void delete_users(void)
1738 {
1739         struct oh323_user *user, *userlast;
1740         struct oh323_peer *peer;
1741         
1742         /* Delete all users */
1743         ast_mutex_lock(&userl.lock);
1744         for (user=userl.users;user;) {
1745                 userlast = user;
1746                 user=user->next;
1747                 free(userlast);
1748         }
1749         userl.users=NULL;
1750         ast_mutex_unlock(&userl.lock);
1751         ast_mutex_lock(&peerl.lock);
1752         for (peer=peerl.peers;peer;) {
1753                 /* Assume all will be deleted, and we'll find out for sure later */
1754                 peer->delme = 1;
1755                 peer = peer->next;
1756         }
1757         ast_mutex_unlock(&peerl.lock);
1758 }
1759
1760 void delete_aliases(void)
1761 {
1762         struct oh323_alias *alias, *aliaslast;
1763         struct e164_number *num, *numlast;
1764     
1765         /* Delete all users */
1766         ast_mutex_lock(&aliasl.lock);
1767         for (alias=aliasl.aliases;alias;) {
1768                 aliaslast = alias;
1769                 alias=alias->next;
1770         
1771                 for(num=aliaslast->e164;num;) {
1772                         numlast = num;
1773                         num = num->next;
1774                         free(num);
1775                 }
1776
1777                 for(num=aliaslast->prefix;num;) {
1778                         numlast = num;
1779                         num = num->next;
1780                         free(num);
1781                 }
1782
1783                 free(aliaslast);
1784         }
1785         aliasl.aliases=NULL;
1786         ast_mutex_unlock(&aliasl.lock);
1787 }
1788
1789 void prune_peers(void)
1790 {
1791         /* Prune peers who still are supposed to be deleted */
1792         struct oh323_peer *peer, *peerlast, *peernext;
1793         ast_mutex_lock(&peerl.lock);
1794         peerlast = NULL;
1795         for (peer=peerl.peers;peer;) {
1796                 peernext = peer->next;
1797                 if (peer->delme) {
1798                         free(peer);
1799                         if (peerlast)
1800                                 peerlast->next = peernext;
1801                         else
1802                                 peerl.peers = peernext;
1803                 } else
1804                         peerlast = peer;
1805                 peer=peernext;
1806         }
1807         ast_mutex_unlock(&peerl.lock);
1808 }
1809
1810 int reload(void)
1811 {
1812         delete_users();
1813         delete_aliases();
1814         prune_peers();
1815
1816 #if 0
1817         if (strlen(gatekeeper)) {
1818                 h323_gk_urq();
1819         }
1820 #endif
1821
1822         reload_config();
1823
1824 #if 0
1825         /* Possibly register with a GK */
1826         if (gatekeeper_disable == 0) {
1827                 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1828                         ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1829                         h323_end_process();
1830                         return -1;
1831                 }
1832         }
1833 #endif
1834         restart_monitor();
1835         return 0;
1836 }
1837
1838
1839 static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
1840 {
1841         struct oh323_pvt *p;
1842         p = chan->pvt->pvt;
1843         if (p && p->rtp && p->bridge) {
1844                 return p->rtp;
1845         }
1846         return NULL;
1847 }
1848
1849 static struct ast_rtp *oh323_get_vrtp_peer(struct ast_channel *chan)
1850 {
1851         return NULL;
1852 }
1853
1854 static char *convertcap(int cap)
1855 {
1856         switch (cap) {
1857         case AST_FORMAT_G723_1:
1858                 return "G.723";
1859         case AST_FORMAT_GSM:
1860                 return "GSM";
1861         case AST_FORMAT_ULAW:
1862                 return "ULAW";
1863         case AST_FORMAT_ALAW:
1864                 return "ALAW";
1865         case AST_FORMAT_ADPCM:
1866                 return "G.728";
1867         case AST_FORMAT_G729A:
1868                 return "G.729";
1869         case AST_FORMAT_SPEEX:
1870                 return "SPEEX";
1871         case AST_FORMAT_ILBC:
1872                 return "ILBC";
1873         default:
1874                 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
1875                 return NULL;
1876         }
1877
1878 }
1879
1880 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp)
1881 {
1882         /* XXX Deal with Video */
1883         struct oh323_pvt *p;
1884         struct sockaddr_in them;
1885         struct sockaddr_in us;
1886         char *mode;
1887
1888         mode = convertcap(chan->writeformat); 
1889
1890         if (!rtp) {
1891                 return 0;
1892         }
1893
1894         p = chan->pvt->pvt;
1895         if (!p) {
1896                 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
1897                 return -1;
1898         }
1899
1900         ast_rtp_get_peer(rtp, &them);   
1901         ast_rtp_get_us(rtp, &us);
1902
1903
1904         h323_native_bridge(p->cd.call_token, inet_ntoa(them.sin_addr), mode);
1905         
1906         return 0;
1907         
1908 }
1909
1910 static struct ast_rtp_protocol oh323_rtp = {
1911         get_rtp_info: oh323_get_rtp_peer,
1912         get_vrtp_info: oh323_get_vrtp_peer,
1913         set_rtp_peer: oh323_set_rtp_peer,
1914 };
1915
1916
1917 int load_module()
1918 {
1919         int res;
1920
1921         res = reload_config();
1922
1923         if (res) {
1924                 return 0;
1925         } else {
1926                 /* Make sure we can register our channel type */
1927                 if (ast_channel_register(type, tdesc, capability, oh323_request)) {
1928                         ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
1929                         h323_end_process();
1930                         return -1;
1931                 }
1932                 ast_cli_register(&cli_debug);
1933                 ast_cli_register(&cli_no_debug);
1934                 ast_cli_register(&cli_trace);
1935                 ast_cli_register(&cli_no_trace);
1936                 ast_cli_register(&cli_show_codecs);
1937                 ast_cli_register(&cli_gk_cycle);
1938                 ast_cli_register(&cli_hangup_call);
1939                 ast_cli_register(&cli_show_tokens);
1940                 ast_cli_register(&cli_show_aliases);
1941         
1942                 oh323_rtp.type = type;
1943                 ast_rtp_proto_register(&oh323_rtp);
1944
1945                 sched = sched_context_create();
1946                 if (!sched) {
1947                         ast_log(LOG_WARNING, "Unable to create schedule context\n");
1948                 }
1949                 io = io_context_create();
1950                 if (!io) {
1951                         ast_log(LOG_WARNING, "Unable to create I/O context\n");
1952                 }
1953                 
1954                 /* Register our callback functions */
1955                 h323_callback_register(setup_incoming_call, 
1956                                        setup_outgoing_call,                                                      
1957                                        create_connection, 
1958                                        setup_rtp_connection, 
1959                                        cleanup_connection, 
1960                                        connection_made, 
1961                                        send_digit);     
1962         
1963
1964                 /* start the h.323 listener */
1965                 if (h323_start_listener(port, bindaddr)) {
1966                         ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
1967 //                      h323_end_process();
1968                         return -1;
1969                 }
1970
1971                 /* Possibly register with a GK */
1972                 if (gatekeeper_disable == 0) {
1973                         if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1974                                 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1975 //                              h323_end_process();
1976                                 return 0;
1977                         }
1978                 }
1979                 /* And start the monitor for the first time */
1980                 restart_monitor();
1981         }
1982         return res;
1983 }
1984
1985
1986 int unload_module() 
1987 {
1988         struct oh323_pvt *p, *pl;
1989                 
1990         if (!ast_mutex_lock(&iflock)) {
1991         /* hangup all interfaces if they have an owner */
1992         p = iflist;
1993         while(p) {
1994                 if (p->owner)
1995                         ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
1996                 p = p->next;
1997         }
1998         iflist = NULL;
1999         ast_mutex_unlock(&iflock);
2000         } else {
2001                 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
2002                 return -1;
2003         }
2004
2005         if (!ast_mutex_lock(&monlock)) {
2006                 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
2007                         pthread_cancel(monitor_thread);
2008                         pthread_kill(monitor_thread, SIGURG);
2009                         pthread_join(monitor_thread, NULL);
2010                  }
2011                 monitor_thread = AST_PTHREADT_STOP;
2012                 ast_mutex_unlock(&monlock);
2013         } else {
2014                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
2015                 return -1;
2016         }
2017                 
2018         if (!ast_mutex_lock(&iflock)) {
2019                 /* destroy all the interfaces and free their memory */
2020                 p = iflist;
2021                 while(p) {
2022                         pl = p;
2023                         p = p->next;
2024                         /* free associated memory */
2025                         free(pl);
2026                 }
2027                 iflist = NULL;
2028                 ast_mutex_unlock(&iflock);
2029         } else {
2030                 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
2031                 return -1;
2032         }
2033         h323_gk_urq();
2034         h323_end_process();
2035
2036         /* unregister rtp */
2037         ast_rtp_proto_unregister(&oh323_rtp);
2038         
2039         /* unregister commands */
2040         ast_cli_unregister(&cli_debug);
2041         ast_cli_unregister(&cli_no_debug);
2042         ast_cli_unregister(&cli_trace);
2043         ast_cli_unregister(&cli_no_trace);   
2044         ast_cli_unregister(&cli_show_codecs);
2045         ast_cli_unregister(&cli_gk_cycle);
2046         ast_cli_unregister(&cli_hangup_call);
2047         ast_cli_unregister(&cli_show_tokens);
2048                         
2049         /* unregister channel type */
2050         ast_channel_unregister(type);
2051
2052         return 0; 
2053
2054
2055 int usecount()
2056 {
2057         int res;
2058         ast_mutex_lock(&usecnt_lock);
2059         res = usecnt;
2060         ast_mutex_unlock(&usecnt_lock);
2061         return res;
2062 }
2063
2064 char *description()
2065 {
2066         return desc;
2067 }
2068
2069 char *key()
2070 {
2071         return ASTERISK_GPL_KEY;
2072 }