define a global null_frame object so when queueing a null frame, you don't
[asterisk/asterisk.git] / channels / chan_h323.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  *
4  * Copyright (C) 1999 - 2005
5  *
6  * OpenH323 Channel Driver for ASTERISK PBX.
7  *                      By Jeremy McNamara
8  *                      For The NuFone Network 
9  *
10  * chan_h323 has been derived from code created by
11  *               Michael Manousos and Mark Spencer
12  *
13  * See http://www.asterisk.org for more information about
14  * the Asterisk project. Please do not directly contact
15  * any of the maintainers of this project for assistance;
16  * the project provides a web site, mailing lists and IRC
17  * channels for your use.
18  *
19  * This program is free software, distributed under the terms of
20  * the GNU General Public License Version 2. See the LICENSE file
21  * at the top of the source tree.
22  */
23
24 /*! \file
25  *
26  * \brief This file is part of the chan_h323 driver for Asterisk
27  *
28  * \author Jeremy McNamara
29  *
30  * \par See also
31  * \arg Config_h323
32  *
33  * \ingroup channel_drivers
34  */
35
36 #include <sys/socket.h>
37 #include <sys/signal.h>
38 #include <sys/param.h>
39 #if defined(BSD)
40 #ifndef IPTOS_MINCOST
41 #define IPTOS_MINCOST 0x02
42 #endif
43 #endif
44 #include <arpa/inet.h>
45 #include <net/if.h>
46 #include <netinet/in.h>
47 #include <netinet/in_systm.h>
48 #include <netinet/ip.h>
49 #include <unistd.h>
50 #include <stdlib.h>
51 #include <netdb.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <errno.h>
55 #include <fcntl.h>
56 #ifdef __cplusplus
57 extern "C" {
58 #endif   
59
60 #include "asterisk.h"
61
62 ASTERISK_FILE_VERSION(__FILE__, "$Revision$")
63
64 #include "asterisk/lock.h"
65 #include "asterisk/logger.h"
66 #include "asterisk/channel.h"
67 #include "asterisk/config.h"
68 #include "asterisk/module.h"
69 #include "asterisk/pbx.h"
70 #include "asterisk/options.h"
71 #include "asterisk/utils.h"
72 #include "asterisk/lock.h"
73 #include "asterisk/sched.h"
74 #include "asterisk/io.h"
75 #include "asterisk/rtp.h"
76 #include "asterisk/acl.h"
77 #include "asterisk/callerid.h"
78 #include "asterisk/cli.h"
79 #include "asterisk/dsp.h"
80 #include "asterisk/causes.h"
81 #ifdef __cplusplus
82 }
83 #endif
84 #include "h323/chan_h323.h"
85
86 send_digit_cb on_send_digit; 
87 on_rtp_cb on_external_rtp_create; 
88 start_rtp_cb on_start_rtp_channel; 
89 setup_incoming_cb on_incoming_call;
90 setup_outbound_cb on_outgoing_call; 
91 chan_ringing_cb on_chan_ringing;
92 con_established_cb on_connection_established;
93 clear_con_cb on_connection_cleared;
94 answer_call_cb on_answer_call;
95 progress_cb on_progress;
96 rfc2833_cb on_set_rfc2833_payload;
97 hangup_cb on_hangup;
98 setcapabilities_cb on_setcapabilities;
99
100 /* global debug flag */
101 int h323debug;
102
103 /** Variables required by Asterisk */
104 static const char type[] = "H323";
105 static const char desc[] = "The NuFone Network's Open H.323 Channel Driver";
106 static const char tdesc[] = "The NuFone Network's Open H.323 Channel Driver";
107 static const char config[] = "h323.conf";
108 static char default_context[AST_MAX_CONTEXT] = "default";
109 static struct sockaddr_in bindaddr;
110
111 /** H.323 configuration values */
112 static int h323_signalling_port = 1720;
113 static char gatekeeper[100];
114 static int gatekeeper_disable = 1;
115 static int gatekeeper_discover = 0;
116 static int usingGk = 0;
117 static int gkroute = 0;
118 /* Find user by alias (h.323 id) is default, alternative is the incomming call's source IP address*/
119 static int userbyalias = 1;
120 static int tos = 0;
121 static char secret[50];
122 static unsigned int unique = 0;
123
124 static call_options_t global_options;
125
126 /** Private structure of a OpenH323 channel */
127 struct oh323_pvt {
128         ast_mutex_t lock;                                       /* Channel private lock */
129         call_options_t options;                                 /* Options to be used during call setup */
130         int alreadygone;                                        /* Whether or not we've already been destroyed by our peer */
131         int needdestroy;                                        /* if we need to be destroyed */
132         call_details_t cd;                                      /* Call details */
133         struct ast_channel *owner;                              /* Who owns us */
134         struct sockaddr_in sa;                                  /* Our peer */
135         struct sockaddr_in redirip;                             /* Where our RTP should be going if not to us */
136         int nonCodecCapability;                                 /* non-audio capability */
137         int outgoing;                                           /* Outgoing or incoming call? */
138         char exten[AST_MAX_EXTENSION];                          /* Requested extension */
139         char context[AST_MAX_CONTEXT];                          /* Context where to start */
140         char accountcode[256];                                  /* Account code */
141         char cid_num[80];                                       /* Caller*id number, if available */
142         char cid_name[80];                                      /* Caller*id name, if available */
143         char rdnis[80];                                         /* Referring DNIS, if available */
144         int amaflags;                                           /* AMA Flags */
145         struct ast_rtp *rtp;                                    /* RTP Session */
146         struct ast_dsp *vad;                                    /* Used for in-band DTMF detection */
147         int nativeformats;                                      /* Codec formats supported by a channel */
148         int needhangup;                                         /* Send hangup when Asterisk is ready */
149         int hangupcause;                                        /* Hangup cause from OpenH323 layer */
150         int newstate;                                           /* Pending state change */
151         int newcontrol;                                         /* Pending control to send */
152         struct oh323_pvt *next;                         /* Next channel in list */
153 } *iflist = NULL;
154
155 static struct ast_user_list {
156         struct oh323_user *users;
157         ast_mutex_t lock;
158 } userl;
159
160 static struct ast_peer_list {
161         struct oh323_peer *peers;
162         ast_mutex_t lock;
163 } peerl;
164
165 static struct ast_alias_list {
166         struct oh323_alias *aliases;
167         ast_mutex_t lock;
168 } aliasl;
169
170 /** Asterisk RTP stuff */
171 static struct sched_context *sched;
172 static struct io_context *io;
173
174 /** Protect the interface list (oh323_pvt) */
175 AST_MUTEX_DEFINE_STATIC(iflock);
176
177 /** Usage counter and associated lock */
178 static int usecnt = 0;
179 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
180
181 /* Protect the monitoring thread, so only one process can kill or start it, and not
182    when it's doing something critical. */
183 AST_MUTEX_DEFINE_STATIC(monlock);
184
185 /* Protect the H.323 capabilities list, to avoid more than one channel to set the capabilities simultaneaously in the h323 stack. */
186 AST_MUTEX_DEFINE_STATIC(caplock);
187
188 /* Protect the reload process */
189 AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
190 static int h323_reloading = 0;
191
192 /* This is the thread for the monitor which checks for input on the channels
193    which are not currently in use.  */
194 static pthread_t monitor_thread = AST_PTHREADT_NULL;
195 static int restart_monitor(void);
196 static int h323_do_reload(void);
197
198 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause);
199 static int oh323_digit(struct ast_channel *c, char digit);
200 static int oh323_call(struct ast_channel *c, char *dest, int timeout);
201 static int oh323_hangup(struct ast_channel *c);
202 static int oh323_answer(struct ast_channel *c);
203 static struct ast_frame *oh323_read(struct ast_channel *c);
204 static int oh323_write(struct ast_channel *c, struct ast_frame *frame);
205 static int oh323_indicate(struct ast_channel *c, int condition);
206 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
207
208 static const struct ast_channel_tech oh323_tech = {
209         .type = type,
210         .description = tdesc,
211         .capabilities = AST_FORMAT_ULAW,
212         .properties = AST_CHAN_TP_WANTSJITTER,
213         .requester = oh323_request,
214         .send_digit = oh323_digit,
215         .call = oh323_call,
216         .hangup = oh323_hangup,
217         .answer = oh323_answer,
218         .read = oh323_read,
219         .write = oh323_write,
220         .indicate = oh323_indicate,
221         .fixup = oh323_fixup,
222         /* disable, for now */
223 #if 0
224         .bridge = ast_rtp_bridge,
225 #endif
226 };
227
228 /* Channel and private structures should be already locked */
229 static void __oh323_update_info(struct ast_channel *c, struct oh323_pvt *pvt)
230 {
231         if (c->nativeformats != pvt->nativeformats) {
232                 if (h323debug)
233                         ast_log(LOG_DEBUG, "Preparing %s for new native format\n", c->name);
234                 c->nativeformats = pvt->nativeformats;
235                 ast_set_read_format(c, c->readformat);
236                 ast_set_write_format(c, c->writeformat);
237         }
238         if (pvt->needhangup) {
239                 if (h323debug)
240                         ast_log(LOG_DEBUG, "Process pending hangup for %s\n", c->name);
241                 c->_softhangup |= AST_SOFTHANGUP_DEV;
242                 c->hangupcause = pvt->hangupcause;
243                 ast_queue_hangup(c);
244                 pvt->needhangup = 0;
245                 pvt->newstate = pvt->newcontrol = -1;
246         }
247         if (pvt->newstate >= 0) {
248                 ast_setstate(c, pvt->newstate);
249                 pvt->newstate = -1;
250         }
251         if (pvt->newcontrol >= 0) {
252                 ast_queue_control(c, pvt->newcontrol);
253                 pvt->newcontrol = -1;
254         }
255 }
256
257 /* Only channel structure should be locked */
258 static void oh323_update_info(struct ast_channel *c)
259 {
260         struct oh323_pvt *pvt = c->tech_pvt;
261
262         if (pvt) {
263                 ast_mutex_lock(&pvt->lock);
264                 __oh323_update_info(c, pvt);
265                 ast_mutex_unlock(&pvt->lock);
266         }
267 }
268
269 static void cleanup_call_details(call_details_t *cd) 
270 {
271         if (cd->call_token) {
272                 free(cd->call_token);
273                 cd->call_token = NULL;
274         }
275         if (cd->call_source_aliases) {
276                 free(cd->call_source_aliases);
277                 cd->call_source_aliases = NULL;
278         }
279         if (cd->call_dest_alias) {
280                 free(cd->call_dest_alias);
281                 cd->call_dest_alias = NULL;
282         }
283         if (cd->call_source_name) { 
284                 free(cd->call_source_name);
285                 cd->call_source_name = NULL;
286         }
287         if (cd->call_source_e164) {
288                 free(cd->call_source_e164);
289                 cd->call_source_e164 = NULL;
290         }
291         if (cd->call_dest_e164) {
292                 free(cd->call_dest_e164);
293                 cd->call_dest_e164 = NULL;
294         }
295         if (cd->sourceIp) {
296                 free(cd->sourceIp);
297                 cd->sourceIp = NULL;
298         }
299 }
300
301 static void __oh323_destroy(struct oh323_pvt *pvt)
302 {
303         struct oh323_pvt *cur, *prev = NULL;
304         
305         if (pvt->rtp) {
306                 ast_rtp_destroy(pvt->rtp);
307         }
308         
309         /* Free dsp used for in-band DTMF detection */
310         if (pvt->vad) {
311                 ast_dsp_free(pvt->vad);
312         }
313         cleanup_call_details(&pvt->cd);
314
315         /* Unlink us from the owner if we have one */
316         if (pvt->owner) {
317                 ast_mutex_lock(&pvt->owner->lock);
318                 ast_log(LOG_DEBUG, "Detaching from %s\n", pvt->owner->name);
319                 pvt->owner->tech_pvt = NULL;
320                 ast_mutex_unlock(&pvt->owner->lock);
321         }
322         cur = iflist;
323         while(cur) {
324                 if (cur == pvt) {
325                         if (prev)
326                                 prev->next = cur->next;
327                         else
328                                 iflist = cur->next;
329                         break;
330                 }
331                 prev = cur;
332                 cur = cur->next;
333         }
334         if (!cur) {
335                 ast_log(LOG_WARNING, "%p is not in list?!?! \n", cur);
336         } else {
337                 ast_mutex_destroy(&pvt->lock);
338                 free(pvt);
339         }
340 }
341
342 static void oh323_destroy(struct oh323_pvt *pvt)
343 {
344         ast_mutex_lock(&iflock);
345         __oh323_destroy(pvt);
346         ast_mutex_unlock(&iflock);
347 }
348
349 /**
350  * Send (play) the specified digit to the channel.
351  * 
352  */
353 static int oh323_digit(struct ast_channel *c, char digit)
354 {
355         struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
356         char *token;
357
358         if (!pvt) {
359                 ast_log(LOG_ERROR, "No private structure?! This is bad\n");
360                 return -1;
361         }
362         ast_mutex_lock(&pvt->lock);
363         if (pvt->rtp && (pvt->options.dtmfmode & H323_DTMF_RFC2833)) {
364                 /* out-of-band DTMF */
365                 if (h323debug) {
366                         ast_log(LOG_DEBUG, "Sending out-of-band digit %c on %s\n", digit, c->name);
367                 }
368                 ast_rtp_senddigit(pvt->rtp, digit);
369         } else {
370                 /* in-band DTMF */
371                 if (h323debug) {
372                         ast_log(LOG_DEBUG, "Sending inband digit %c on %s\n", digit, c->name);
373                 }
374                 token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
375                 h323_send_tone(token, digit);
376                 if (token) {
377                         free(token);
378                 }
379         }
380         ast_mutex_unlock(&pvt->lock);
381         oh323_update_info(c);
382         return 0;
383 }
384
385 /**
386  * Make a call over the specified channel to the specified 
387  * destination.
388  * Returns -1 on error, 0 on success.
389  */
390 static int oh323_call(struct ast_channel *c, char *dest, int timeout)
391 {  
392         int res = 0;
393         struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
394         char addr[INET_ADDRSTRLEN];
395         char called_addr[1024];
396
397         if (h323debug) {
398                 ast_log(LOG_DEBUG, "Calling to %s on %s\n", dest, c->name);
399         }
400         if ((c->_state != AST_STATE_DOWN) && (c->_state != AST_STATE_RESERVED)) {
401                 ast_log(LOG_WARNING, "Line is already in use (%s)\n", c->name);
402                 return -1;
403         }
404         ast_mutex_lock(&pvt->lock);
405         if (usingGk) {
406                 if (ast_strlen_zero(pvt->exten)) {
407                         strncpy(called_addr, dest, sizeof(called_addr));
408                 } else {
409                         snprintf(called_addr, sizeof(called_addr), "%s@%s", pvt->exten, dest);
410                 }
411         } else {
412                 ast_inet_ntoa(addr, sizeof(addr), pvt->sa.sin_addr);
413                 res = htons(pvt->sa.sin_port);
414                 if (ast_strlen_zero(pvt->exten)) {
415                         snprintf(called_addr, sizeof(called_addr), "%s:%d", addr, res);
416                 } else {
417                         snprintf(called_addr, sizeof(called_addr), "%s@%s:%d", pvt->exten, addr, res);
418                 }
419         }
420         /* make sure null terminated */
421         called_addr[sizeof(called_addr) - 1] = '\0'; 
422
423         if (c->cid.cid_num) {
424                 strncpy(pvt->options.cid_num, c->cid.cid_num, sizeof(pvt->options.cid_num));
425         }
426         if (c->cid.cid_name) {
427                 strncpy(pvt->options.cid_name, c->cid.cid_name, sizeof(pvt->options.cid_name));
428         }
429
430         /* indicate that this is an outgoing call */
431         pvt->outgoing = 1;
432
433         ast_log(LOG_DEBUG, "Placing outgoing call to %s, %d\n", called_addr, pvt->options.dtmfcodec);
434         ast_mutex_unlock(&pvt->lock);
435         res = h323_make_call(called_addr, &(pvt->cd), &pvt->options);
436         if (res) {
437                 ast_log(LOG_NOTICE, "h323_make_call failed(%s)\n", c->name);
438                 return -1;
439         }
440         oh323_update_info(c);
441         return 0;
442 }
443
444 static int oh323_answer(struct ast_channel *c)
445 {
446         int res;
447         struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
448         char *token;
449
450         if (h323debug)
451                 ast_log(LOG_DEBUG, "Answering on %s\n", c->name);
452
453         ast_mutex_lock(&pvt->lock);
454         token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
455         ast_mutex_unlock(&pvt->lock);
456         res = h323_answering_call(token, 0);
457         if (token)
458                 free(token);
459
460         oh323_update_info(c);
461         if (c->_state != AST_STATE_UP) {
462                 ast_setstate(c, AST_STATE_UP);
463         }
464         return res;
465 }
466
467 static int oh323_hangup(struct ast_channel *c)
468 {
469         struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
470         int needcancel = 0;
471         int q931cause = AST_CAUSE_NORMAL_CLEARING;
472         char *call_token;
473
474
475         if (h323debug)
476                 ast_log(LOG_DEBUG, "Hanging up call %s\n", c->name);
477
478         if (!c->tech_pvt) {
479                 ast_log(LOG_DEBUG, "Asked to hangup channel not connected\n");
480                 return 0;
481         }
482         ast_mutex_lock(&pvt->lock);
483         /* Determine how to disconnect */
484         if (pvt->owner != c) {
485                 ast_log(LOG_WARNING, "Huh?  We aren't the owner?\n");
486                 ast_mutex_unlock(&pvt->lock);
487                 return 0;
488         }
489         if (!c || (c->_state != AST_STATE_UP)) {
490                 needcancel = 1;
491         }
492         
493         pvt->owner = NULL;
494         c->tech_pvt = NULL;
495
496         if (c->hangupcause) {
497                 q931cause = c->hangupcause;
498         } else {
499                 char *cause = pbx_builtin_getvar_helper(c, "DIALSTATUS");
500                 if (cause) {
501                         if (!strcmp(cause, "CONGESTION")) {
502                                 q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
503                         } else if (!strcmp(cause, "BUSY")) {
504                                 q931cause = AST_CAUSE_USER_BUSY;
505                         } else if (!strcmp(cause, "CHANISUNVAIL")) {
506                                 q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
507                         } else if (!strcmp(cause, "NOANSWER")) {
508                                 q931cause = AST_CAUSE_NO_ANSWER;
509                         } else if (!strcmp(cause, "CANCEL")) {
510                                 q931cause = AST_CAUSE_CALL_REJECTED;
511                         }
512                 }
513         }
514
515         /* Start the process if it's not already started */
516         if (!pvt->alreadygone && !pvt->hangupcause) {
517                 call_token = pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL;
518                 if (call_token) {
519                         /* Release lock to eliminate deadlock */
520                         ast_mutex_unlock(&pvt->lock);
521                         if (h323_clear_call(call_token, q931cause)) { 
522                                 ast_log(LOG_DEBUG, "ClearCall failed.\n");
523                         }
524                         free(call_token);
525                         ast_mutex_lock(&pvt->lock);
526                 }
527         } 
528         pvt->needdestroy = 1;
529
530         /* Update usage counter */
531         ast_mutex_lock(&usecnt_lock);
532         usecnt--;
533         if (usecnt < 0) {
534                 ast_log(LOG_WARNING, "Usecnt < 0\n");
535         }
536         ast_mutex_unlock(&usecnt_lock);
537         ast_mutex_unlock(&pvt->lock);
538         ast_update_use_count();
539         return 0;
540 }
541
542 static struct ast_frame *oh323_rtp_read(struct oh323_pvt *pvt)
543 {
544         /* Retrieve audio/etc from channel.  Assumes pvt->lock is already held. */
545         struct ast_frame *f;
546
547         /* Only apply it for the first packet, we just need the correct ip/port */
548         if (pvt->options.nat) {
549                 ast_rtp_setnat(pvt->rtp, pvt->options.nat);
550                 pvt->options.nat = 0;
551         }
552
553         f = ast_rtp_read(pvt->rtp);
554         /* Don't send RFC2833 if we're not supposed to */
555         if (f && (f->frametype == AST_FRAME_DTMF) && !(pvt->options.dtmfmode & H323_DTMF_RFC2833)) {
556                 return &ast_null_frame;
557         }
558         if (pvt->owner) {
559                 /* We already hold the channel lock */
560                 if (f->frametype == AST_FRAME_VOICE) {
561                         if (f->subclass != pvt->owner->nativeformats) {
562                                 /* Try to avoid deadlock */
563                                 if (ast_mutex_trylock(&pvt->owner->lock)) {
564                                         ast_log(LOG_NOTICE, "Format changed but channel is locked. Ignoring frame...\n");
565                                         return &ast_null_frame;
566                                 }
567                                 ast_log(LOG_DEBUG, "Oooh, format changed to %d\n", f->subclass);
568                                 pvt->owner->nativeformats = f->subclass;
569                                 pvt->nativeformats = f->subclass;
570                                 ast_set_read_format(pvt->owner, pvt->owner->readformat);
571                                 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
572                                 ast_mutex_unlock(&pvt->owner->lock);
573                         }       
574                         /* Do in-band DTMF detection */
575                         if ((pvt->options.dtmfmode & H323_DTMF_INBAND) && pvt->vad) {
576                                 if (!ast_mutex_trylock(&pvt->owner->lock)) {
577                                         f = ast_dsp_process(pvt->owner,pvt->vad,f);
578                                         ast_mutex_unlock(&pvt->owner->lock);
579                                 }
580                                 else
581                                         ast_log(LOG_NOTICE, "Unable to process inband DTMF while channel is locked\n");
582                                 if (f &&(f->frametype == AST_FRAME_DTMF)) {
583                                         ast_log(LOG_DEBUG, "Received in-band digit %c.\n", f->subclass);
584                                 }
585                         }
586                 }
587         }
588         return f;
589 }
590
591 static struct ast_frame *oh323_read(struct ast_channel *c)
592 {
593         struct ast_frame *fr;
594         struct oh323_pvt *pvt = (struct oh323_pvt *)c->tech_pvt;
595         ast_mutex_lock(&pvt->lock);
596         __oh323_update_info(c, pvt);
597         fr = oh323_rtp_read(pvt);
598         ast_mutex_unlock(&pvt->lock);
599         return fr;
600 }
601
602 static int oh323_write(struct ast_channel *c, struct ast_frame *frame)
603 {
604         struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
605         int res = 0;
606         if (frame->frametype != AST_FRAME_VOICE) {
607                 if (frame->frametype == AST_FRAME_IMAGE) {
608                         return 0;
609                 } else {
610                         ast_log(LOG_WARNING, "Can't send %d type frames with H323 write\n", frame->frametype);
611                         return 0;
612                 }
613         } else {
614                 if (!(frame->subclass & c->nativeformats)) {
615                         ast_log(LOG_WARNING, "Asked to transmit frame type %d, while native formats is %d (read/write = %d/%d)\n",
616                                 frame->subclass, c->nativeformats, c->readformat, c->writeformat);
617                         return 0;
618                 }
619         }
620         if (pvt) {
621                 ast_mutex_lock(&pvt->lock);
622                 if (pvt->rtp) {
623                         res =  ast_rtp_write(pvt->rtp, frame);
624                 }
625                 __oh323_update_info(c, pvt);
626                 ast_mutex_unlock(&pvt->lock);
627         }
628         return res;
629 }
630
631 static int oh323_indicate(struct ast_channel *c, int condition)
632 {
633
634         struct oh323_pvt *pvt = (struct oh323_pvt *) c->tech_pvt;
635         char *token = (char *)NULL;
636
637         ast_mutex_lock(&pvt->lock);
638         token = (pvt->cd.call_token ? strdup(pvt->cd.call_token) : NULL);
639         ast_mutex_unlock(&pvt->lock);
640
641         if (h323debug)
642                 ast_log(LOG_DEBUG, "OH323: Indicating %d on %s\n", condition, token);
643
644         switch(condition) {
645         case AST_CONTROL_RINGING:
646                 if (c->_state == AST_STATE_RING || c->_state == AST_STATE_RINGING) {
647                         h323_send_alerting(token);
648                         break;
649                 }
650                 if (token)
651                         free(token);
652                 return -1;
653         case AST_CONTROL_PROGRESS:
654                 if (c->_state != AST_STATE_UP) {
655                         h323_send_progress(token);
656                         break;
657                 }
658                 if (token)
659                         free(token);
660                 return -1;
661
662         case AST_CONTROL_BUSY:
663                 if (c->_state != AST_STATE_UP) {
664                         h323_answering_call(token, 1);
665                         ast_mutex_lock(&pvt->lock);
666                         pvt->alreadygone = 1;
667                         ast_mutex_unlock(&pvt->lock);
668                         ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);                   
669                         break;
670                 }
671                 if (token)
672                         free(token);
673                 return -1;
674         case AST_CONTROL_CONGESTION:
675                 if (c->_state != AST_STATE_UP) {
676                         h323_answering_call(token, 1);
677                         ast_mutex_lock(&pvt->lock);
678                         pvt->alreadygone = 1;
679                         ast_mutex_unlock(&pvt->lock);
680                         ast_softhangup_nolock(c, AST_SOFTHANGUP_DEV);
681                         break;
682                 }
683                 if (token)
684                         free(token);
685                 return -1;
686         case AST_CONTROL_PROCEEDING:
687         case -1:
688                 if (token)
689                         free(token);
690                 return -1;
691         default:
692                 ast_log(LOG_WARNING, "Don't know how to indicate condition %d on %s\n", condition, token);
693                 if (token)
694                         free(token);
695                 return -1;
696         }
697
698         if (h323debug)
699                 ast_log(LOG_DEBUG, "OH323: Indicated %d on %s\n", condition, token);
700         if (token)
701                 free(token);
702         oh323_update_info(c);
703
704         return -1;
705 }
706
707 static int oh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
708 {
709         struct oh323_pvt *pvt = (struct oh323_pvt *) newchan->tech_pvt;
710
711         ast_mutex_lock(&pvt->lock);
712         if (pvt->owner != oldchan) {
713                 ast_log(LOG_WARNING, "old channel wasn't %p but was %p\n", oldchan, pvt->owner);
714                 return -1;
715         }
716         pvt->owner = newchan;
717         ast_mutex_unlock(&pvt->lock);
718         return 0;
719 }
720
721 /* Private structure should be locked on a call */
722 static struct ast_channel *__oh323_new(struct oh323_pvt *pvt, int state, const char *host)
723 {
724         struct ast_channel *ch;
725         int fmt;
726         
727         /* Don't hold a oh323_pvt lock while we allocate a chanel */
728         ast_mutex_unlock(&pvt->lock);
729         ch = ast_channel_alloc(1);
730         /* Update usage counter */
731         ast_mutex_lock(&usecnt_lock);
732         usecnt++;
733         ast_mutex_unlock(&usecnt_lock);
734         ast_update_use_count();
735         ast_mutex_lock(&pvt->lock);
736         if (ch) {
737                 ch->tech = &oh323_tech;
738                 snprintf(ch->name, sizeof(ch->name), "H323/%s", host);
739                 ch->nativeformats = pvt->options.capability;
740                 if (!ch->nativeformats) {
741                         ch->nativeformats = global_options.capability;
742                 }
743                 pvt->nativeformats = ch->nativeformats;
744                 fmt = ast_best_codec(ch->nativeformats);
745                 ch->type = type;
746                 ch->fds[0] = ast_rtp_fd(pvt->rtp);
747                 if (state == AST_STATE_RING) {
748                         ch->rings = 1;
749                 }
750                 ch->writeformat = fmt;
751                 ch->rawwriteformat = fmt;
752                 ch->readformat = fmt;
753                 ch->rawreadformat = fmt;
754                 /* Allocate dsp for in-band DTMF support */
755                 if (pvt->options.dtmfmode & H323_DTMF_INBAND) {
756                         pvt->vad = ast_dsp_new();
757                         ast_dsp_set_features(pvt->vad, DSP_FEATURE_DTMF_DETECT);
758                 }
759                 /* Register channel functions. */
760                 ch->tech_pvt = pvt;
761                 /*  Set the owner of this channel */
762                 pvt->owner = ch;
763                 
764                 strncpy(ch->context, pvt->context, sizeof(ch->context) - 1);
765                 strncpy(ch->exten, pvt->exten, sizeof(ch->exten) - 1);          
766                 ch->priority = 1;
767                 if (!ast_strlen_zero(pvt->accountcode)) {
768                         strncpy(ch->accountcode, pvt->accountcode, sizeof(ch->accountcode) - 1);
769                 }
770                 if (pvt->amaflags) {
771                         ch->amaflags = pvt->amaflags;
772                 }
773                 if (!ast_strlen_zero(pvt->cid_num)) {
774                         ch->cid.cid_num = strdup(pvt->cid_num);
775                 } else if (!ast_strlen_zero(pvt->cd.call_source_e164)) {
776                         ch->cid.cid_num = strdup(pvt->cd.call_source_e164);
777                 }
778                 if (!ast_strlen_zero(pvt->cid_name)) {
779                         ch->cid.cid_name = strdup(pvt->cid_name);
780                 } else if (!ast_strlen_zero(pvt->cd.call_source_name)) {
781                         ch->cid.cid_name = strdup(pvt->cd.call_source_name);
782                 }
783                 if (!ast_strlen_zero(pvt->rdnis)) {
784                         ch->cid.cid_rdnis = strdup(pvt->rdnis);
785                 }
786                 if (!ast_strlen_zero(pvt->exten) && strcmp(pvt->exten, "s")) {
787                         ch->cid.cid_dnid = strdup(pvt->exten);
788                 }
789                 ast_setstate(ch, state);
790                 if (state != AST_STATE_DOWN) {
791                         if (ast_pbx_start(ch)) {
792                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
793                                 ast_hangup(ch);
794                                 ch = NULL;
795                         }
796                 }
797         } else  {
798                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
799         }
800         return ch;
801 }
802
803 static struct oh323_pvt *oh323_alloc(int callid)
804 {
805         struct oh323_pvt *pvt;
806
807         pvt = (struct oh323_pvt *) malloc(sizeof(struct oh323_pvt));
808         if (!pvt) {
809                 ast_log(LOG_ERROR, "Couldn't allocate private structure. This is bad\n");
810                 return NULL;
811         }
812         memset(pvt, 0, sizeof(struct oh323_pvt));
813         pvt->rtp = ast_rtp_new_with_bindaddr(sched, io, 1, 0,bindaddr.sin_addr);
814         if (!pvt->rtp) {
815                 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", strerror(errno));
816                 free(pvt);
817                 return NULL;
818         }
819         ast_rtp_settos(pvt->rtp, tos);
820         ast_mutex_init(&pvt->lock);
821         /* Ensure the call token is allocated */
822         if ((pvt->cd).call_token == NULL) {
823                 (pvt->cd).call_token = (char *)malloc(128);
824         }
825         if (!pvt->cd.call_token) {
826                 ast_log(LOG_ERROR, "Not enough memory to alocate call token\n");
827                 return NULL;
828         }
829         memset((char *)(pvt->cd).call_token, 0, 128);
830         pvt->cd.call_reference = callid;
831         memcpy(&pvt->options, &global_options, sizeof(pvt->options));
832         if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
833                 pvt->nonCodecCapability |= AST_RTP_DTMF;
834         } else {
835                 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
836         }
837         strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
838         pvt->newstate = pvt->newcontrol = -1;
839         /* Add to interface list */
840         ast_mutex_lock(&iflock);
841         pvt->next = iflist;
842         iflist = pvt;
843         ast_mutex_unlock(&iflock);
844         return pvt;
845 }
846
847 static struct oh323_pvt *find_call_locked(int call_reference, const char *token)
848 {  
849         struct oh323_pvt *pvt;
850
851         ast_mutex_lock(&iflock);
852         pvt = iflist; 
853         while(pvt) {
854                 if (!pvt->needdestroy && ((signed int)pvt->cd.call_reference == call_reference)) {
855                         /* Found the call */             
856                         if ((token != NULL) && (!strcmp(pvt->cd.call_token, token))) {
857                                 ast_mutex_lock(&pvt->lock);
858                                 ast_mutex_unlock(&iflock);
859                                 return pvt;
860                         } else if (token == NULL) {
861                                 ast_log(LOG_WARNING, "Call Token is NULL\n");
862                                 ast_mutex_lock(&pvt->lock);
863                                 ast_mutex_unlock(&iflock);
864                                 return pvt;
865                         }
866                 }
867                 pvt = pvt->next; 
868         }
869         ast_mutex_unlock(&iflock);
870         return NULL;
871 }
872
873 static int update_state(struct oh323_pvt *pvt, int state, int signal)
874 {
875         if (!pvt)
876                 return 0;
877         if (pvt->owner && !ast_mutex_trylock(&pvt->owner->lock)) {
878                 if (state >= 0)
879                         ast_setstate(pvt->owner, state);
880                 if (signal >= 0)
881                         ast_queue_control(pvt->owner, signal);
882                 return 1;
883         }
884         else {
885                 if (state >= 0)
886                         pvt->newstate = state;
887                 if (signal >= 0)
888                         pvt->newcontrol = signal;
889                 return 0;
890         }
891 }
892
893 struct oh323_user *find_user(const call_details_t *cd)
894 {
895         struct oh323_user *u;
896         char iabuf[INET_ADDRSTRLEN];
897         u = userl.users;
898         if (userbyalias) {
899                 while(u) {
900                         if (!strcasecmp(u->name, cd->call_source_aliases)) {
901                                 break;
902                         }
903                         u = u->next;
904                 }
905         } else {
906                 while(u) {
907                         if (!strcasecmp(cd->sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), u->addr.sin_addr))) {
908                                 break;
909                         }
910                         u = u->next;
911                 }
912         }
913         return u;
914 }
915
916 struct oh323_peer *find_peer(const char *peer, struct sockaddr_in *sin)
917 {
918         struct oh323_peer *p = NULL;
919         static char iabuf[INET_ADDRSTRLEN];
920
921         p = peerl.peers;
922         if (peer) {
923                 while(p) {
924                         if (!strcasecmp(p->name, peer)) {
925                                 ast_log(LOG_DEBUG, "Found peer %s by name\n", peer);
926                                 break;
927                         }
928                         p = p->next;
929                 }
930         } else {
931                 /* find by sin */
932                 if (sin) {
933                         while (p) {
934                                 if ((!inaddrcmp(&p->addr, sin)) || 
935                                         (p->addr.sin_addr.s_addr == sin->sin_addr.s_addr)) {
936                                         ast_log(LOG_DEBUG, "Found peer %s/%s by addr\n", peer, ast_inet_ntoa(iabuf, sizeof(iabuf), p->addr.sin_addr));
937                                         break;
938                                 }
939                                 p = p->next;
940                         }
941                 }       
942         }
943         if (!p) {
944                 ast_log(LOG_DEBUG, "Could not find peer %s by name or address\n", peer);
945         }
946         return p;
947 }
948
949 static int create_addr(struct oh323_pvt *pvt, char *opeer)
950 {
951         struct hostent *hp;
952         struct ast_hostent ahp;
953         struct oh323_peer *p;
954         int portno;
955         int found = 0;
956         char *port;
957         char *hostn;
958         char peer[256] = "";
959
960         strncpy(peer, opeer, sizeof(peer) - 1);
961         port = strchr(peer, ':');
962         if (port) {
963                 *port = '\0';
964                 port++;
965         }
966         pvt->sa.sin_family = AF_INET;
967         ast_mutex_lock(&peerl.lock);
968         p = find_peer(peer, NULL);
969         if (p) {
970                 found++;
971                 memcpy(&pvt->options, &p->options, sizeof(pvt->options));
972                 if (pvt->rtp) {
973                         ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", pvt->options.nat);
974                         ast_rtp_setnat(pvt->rtp, pvt->options.nat);
975                 }
976                 if (pvt->options.dtmfmode) {
977                         if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
978                                 pvt->nonCodecCapability |= AST_RTP_DTMF;
979                         } else {
980                                 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
981                         }
982                 }
983                 if (p->addr.sin_addr.s_addr) {
984                         pvt->sa.sin_addr = p->addr.sin_addr;    
985                         pvt->sa.sin_port = p->addr.sin_port;    
986                 } 
987         }
988         ast_mutex_unlock(&peerl.lock);
989         if (!p && !found) {
990                 hostn = peer;
991                 if (port) {
992                         portno = atoi(port);
993                 } else {
994                         portno = h323_signalling_port;
995                 }               
996                 hp = ast_gethostbyname(hostn, &ahp);
997                 if (hp) {
998                         memcpy(&pvt->options, &global_options, sizeof(pvt->options));
999                         memcpy(&pvt->sa.sin_addr, hp->h_addr, sizeof(pvt->sa.sin_addr));
1000                         pvt->sa.sin_port = htons(portno);
1001                         return 0;       
1002                 } else {
1003                         ast_log(LOG_WARNING, "No such host: %s\n", peer);
1004                         return -1;
1005                 }
1006         } else if (!p) {
1007                 return -1;
1008         } else {        
1009                 return 0;
1010         }
1011 }
1012 static struct ast_channel *oh323_request(const char *type, int format, void *data, int *cause)
1013 {
1014         int oldformat;
1015         struct oh323_pvt *pvt;
1016         struct ast_channel *tmpc = NULL;
1017         char *dest = (char *)data;
1018         char *ext, *host;
1019         char *h323id = NULL;
1020         char tmp[256], tmp1[256];
1021         
1022         ast_log(LOG_DEBUG, "type=%s, format=%d, data=%s.\n", type, format, (char *)data);
1023         pvt = oh323_alloc(0);
1024         if (!pvt) {
1025                 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char *)data);
1026                 return NULL;
1027         }       
1028         oldformat = format;
1029         format &= ((AST_FORMAT_MAX_AUDIO << 1) - 1);
1030         if (!format) {
1031                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%d'\n", format);
1032                 return NULL;
1033         }
1034         strncpy(tmp, dest, sizeof(tmp) - 1);    
1035         host = strchr(tmp, '@');
1036         if (host) {
1037                 *host = '\0';
1038                 host++;
1039                 ext = tmp;
1040         } else {
1041                 host = tmp;
1042                 ext = NULL;
1043         }
1044         strtok_r(host, "/", &(h323id));         
1045         if (!ast_strlen_zero(h323id)) {
1046                 h323_set_id(h323id);
1047         }
1048         if (ext) {
1049                 strncpy(pvt->exten, ext, sizeof(pvt->exten) - 1);
1050         }
1051         ast_log(LOG_DEBUG, "Extension: %s Host: %s\n",  pvt->exten, host);
1052         if (!usingGk) {
1053                 if (create_addr(pvt, host)) {
1054                         oh323_destroy(pvt);
1055                         return NULL;
1056                 }
1057         }
1058         else {
1059                 memcpy(&pvt->options, &global_options, sizeof(pvt->options));
1060                 if (pvt->rtp) {
1061                         ast_log(LOG_DEBUG, "Setting NAT on RTP to %d\n", pvt->options.nat);
1062                         ast_rtp_setnat(pvt->rtp, pvt->options.nat);
1063                 }
1064                 if (pvt->options.dtmfmode) {
1065                         if (pvt->options.dtmfmode & H323_DTMF_RFC2833) {
1066                                 pvt->nonCodecCapability |= AST_RTP_DTMF;
1067                         } else {
1068                                 pvt->nonCodecCapability &= ~AST_RTP_DTMF;
1069                         }
1070                 }
1071         }
1072
1073         ast_mutex_lock(&caplock);
1074         /* Generate unique channel identifier */
1075         snprintf(tmp1, sizeof(tmp1)-1, "%s-%u", host, ++unique);
1076         tmp1[sizeof(tmp1)-1] = '\0';
1077         ast_mutex_unlock(&caplock);
1078
1079         ast_mutex_lock(&pvt->lock);
1080         tmpc = __oh323_new(pvt, AST_STATE_DOWN, tmp1);
1081         ast_mutex_unlock(&pvt->lock);
1082         if (!tmpc) {
1083                 oh323_destroy(pvt);
1084         }
1085         ast_update_use_count();
1086         restart_monitor();
1087         return tmpc;
1088 }
1089
1090 /** Find a call by alias */
1091 struct oh323_alias *find_alias(const char *source_aliases)
1092 {
1093         struct oh323_alias *a;
1094
1095         a = aliasl.aliases;
1096         while(a) {
1097                 if (!strcasecmp(a->name, source_aliases)) {
1098                         break;
1099                 }
1100                 a = a->next;
1101         }
1102         return a;
1103 }
1104
1105 /**
1106   * Callback for sending digits from H.323 up to asterisk
1107   *
1108   */
1109 int send_digit(unsigned call_reference, char digit, const char *token)
1110 {
1111         struct oh323_pvt *pvt;
1112         struct ast_frame f;
1113         int res;
1114
1115         ast_log(LOG_DEBUG, "Received Digit: %c\n", digit);
1116         pvt = find_call_locked(call_reference, token); 
1117         if (!pvt) {
1118                 ast_log(LOG_ERROR, "Private structure not found in send_digit.\n");
1119                 return -1;
1120         }
1121         memset(&f, 0, sizeof(f));
1122         f.frametype = AST_FRAME_DTMF;
1123         f.subclass = digit;
1124         f.datalen = 0;
1125         f.samples = 800;
1126         f.offset = 0;
1127         f.data = NULL;
1128         f.mallocd = 0;
1129         f.src = "SEND_DIGIT";   
1130         res = ast_queue_frame(pvt->owner, &f);
1131         ast_mutex_unlock(&pvt->lock);
1132         return res;
1133 }
1134
1135 /**
1136   * Callback function used to inform the H.323 stack of the local rtp ip/port details
1137   *
1138   * Returns the local RTP information
1139   */
1140 struct rtp_info *external_rtp_create(unsigned call_reference, const char * token)
1141 {       
1142         struct oh323_pvt *pvt;
1143         struct sockaddr_in us;
1144         struct rtp_info *info;
1145
1146         info = (struct rtp_info *)malloc(sizeof(struct rtp_info));
1147         if (!info) {
1148                 ast_log(LOG_ERROR, "Unable to allocated info structure, this is very bad\n");
1149                 return NULL;
1150         }
1151         pvt = find_call_locked(call_reference, token); 
1152         if (!pvt) {
1153                 free(info);
1154                 ast_log(LOG_ERROR, "Unable to find call %s(%d)\n", token, call_reference);
1155                 return NULL;
1156         }
1157         /* figure out our local RTP port and tell the H.323 stack about it */
1158         ast_rtp_get_us(pvt->rtp, &us);
1159         ast_mutex_unlock(&pvt->lock);
1160
1161         ast_inet_ntoa(info->addr, sizeof(info->addr), us.sin_addr);
1162         info->port = ntohs(us.sin_port);
1163         if (h323debug)
1164                 ast_log(LOG_DEBUG, "Sending RTP 'US' %s:%d\n", info->addr, info->port);
1165         return info;
1166 }
1167
1168 /**
1169  * Definition taken from rtp.c for rtpPayloadType because we need it here.
1170  */
1171 struct rtpPayloadType {
1172         int isAstFormat;        /* whether the following code is an AST_FORMAT */
1173         int code;
1174 };
1175
1176 /**
1177   * Call-back function passing remote ip/port information from H.323 to asterisk 
1178   *
1179   * Returns nothing 
1180   */
1181 void setup_rtp_connection(unsigned call_reference, const char *remoteIp, int remotePort, const char *token, int pt)
1182 {
1183         struct oh323_pvt *pvt;
1184         struct sockaddr_in them;
1185         struct rtpPayloadType rtptype;
1186
1187         if (h323debug)
1188                 ast_log(LOG_DEBUG, "Setting up RTP connection for %s\n", token);
1189
1190         /* Find the call or allocate a private structure if call not found */
1191         pvt = find_call_locked(call_reference, token); 
1192         if (!pvt) {
1193                 ast_log(LOG_ERROR, "Something is wrong: rtp\n");
1194                 return;
1195         }
1196         if (pvt->alreadygone) {
1197                 ast_mutex_unlock(&pvt->lock);
1198                 return;
1199         }
1200         rtptype = ast_rtp_lookup_pt(pvt->rtp, pt);
1201         pvt->nativeformats = rtptype.code;
1202         if (pvt->owner && !ast_mutex_trylock(&pvt->owner->lock)) {
1203                 pvt->owner->nativeformats = pvt->nativeformats;
1204                 ast_set_read_format(pvt->owner, pvt->owner->readformat);
1205                 ast_set_write_format(pvt->owner, pvt->owner->writeformat);
1206                 if (pvt->options.progress_audio)
1207                         ast_queue_control(pvt->owner, AST_CONTROL_PROGRESS);
1208                 ast_mutex_unlock(&pvt->owner->lock);
1209         }
1210         else {
1211                 if (pvt->options.progress_audio)
1212                         pvt->newcontrol = AST_CONTROL_PROGRESS;
1213                 if (h323debug)
1214                         ast_log(LOG_DEBUG, "RTP connection preparation for %s is pending...\n", token);
1215         }
1216
1217         them.sin_family = AF_INET;
1218         /* only works for IPv4 */
1219         them.sin_addr.s_addr = inet_addr(remoteIp); 
1220         them.sin_port = htons(remotePort);
1221         ast_rtp_set_peer(pvt->rtp, &them);
1222
1223         ast_mutex_unlock(&pvt->lock);
1224
1225         if (h323debug)
1226                 ast_log(LOG_DEBUG, "RTP connection prepared for %s\n", token);
1227
1228         return;
1229 }
1230
1231 /**  
1232   *     Call-back function to signal asterisk that the channel has been answered 
1233   * Returns nothing
1234   */
1235 void connection_made(unsigned call_reference, const char *token)
1236 {
1237         struct ast_channel *c = NULL;
1238         struct oh323_pvt *pvt;
1239
1240         if (h323debug)
1241                 ast_log(LOG_DEBUG, "Call %s answered\n", token);
1242
1243         pvt = find_call_locked(call_reference, token); 
1244         if (!pvt) {
1245                 ast_log(LOG_ERROR, "Something is wrong: connection\n");
1246                 return;
1247         }
1248
1249         /* Inform asterisk about remote party connected only on outgoing calls */
1250         if (!pvt->outgoing) {
1251                 ast_mutex_unlock(&pvt->lock);
1252                 return;
1253         }
1254         if (update_state(pvt, AST_STATE_UP, AST_CONTROL_ANSWER))
1255                 ast_mutex_unlock(&pvt->owner->lock);
1256         ast_mutex_unlock(&pvt->lock);
1257         return;
1258 }
1259
1260 int progress(unsigned call_reference, const char *token, int inband)
1261 {
1262         struct oh323_pvt *pvt;
1263
1264         ast_log(LOG_DEBUG, "Received ALERT/PROGRESS message for %s tones\n", (inband ? "inband" : "self-generated"));
1265
1266         pvt = find_call_locked(call_reference, token);
1267         if (!pvt) {
1268                 ast_log(LOG_ERROR, "Private structure not found in progress.\n");
1269                 return -1;
1270         }
1271         if (!pvt->owner) {
1272                 ast_mutex_unlock(&pvt->lock);
1273                 ast_log(LOG_ERROR, "No Asterisk channel associated with private structure.\n");
1274                 return -1;
1275         }
1276         if (update_state(pvt, -1, (inband ? AST_CONTROL_PROGRESS : AST_CONTROL_RINGING)))
1277                 ast_mutex_unlock(&pvt->owner->lock);
1278         ast_mutex_unlock(&pvt->lock);
1279
1280         return 0;
1281 }
1282
1283 /**
1284  *  Call-back function for incoming calls
1285  *
1286  *  Returns 1 on success
1287  */
1288 call_options_t *setup_incoming_call(call_details_t *cd)
1289 {
1290         struct oh323_pvt *pvt;
1291         struct oh323_user *user = NULL;
1292         struct oh323_alias *alias = NULL;
1293         char iabuf[INET_ADDRSTRLEN];
1294
1295         if (h323debug)
1296                 ast_log(LOG_DEBUG, "Setting up incoming call for %s\n", cd->call_token);
1297
1298         /* allocate the call*/
1299         pvt = oh323_alloc(cd->call_reference);
1300
1301         if (!pvt) {
1302                 ast_log(LOG_ERROR, "Unable to allocate private structure, this is bad.\n");
1303                 return NULL;
1304         }
1305
1306         /* Populate the call details in the private structure */
1307         memcpy(&pvt->cd, cd, sizeof(pvt->cd));
1308         memcpy(&pvt->options, &global_options, sizeof(pvt->options));
1309
1310         if (h323debug) {
1311                 ast_verbose(VERBOSE_PREFIX_3 "Setting up Call\n");
1312                 ast_verbose(VERBOSE_PREFIX_3 "\tCall token:  [%s]\n", pvt->cd.call_token);
1313                 ast_verbose(VERBOSE_PREFIX_3 "\tCalling party name:  [%s]\n", pvt->cd.call_source_name);
1314                 ast_verbose(VERBOSE_PREFIX_3 "\tCalling party number:  [%s]\n", pvt->cd.call_source_e164);
1315                 ast_verbose(VERBOSE_PREFIX_3 "\tCalled party name:  [%s]\n", pvt->cd.call_dest_alias);
1316                 ast_verbose(VERBOSE_PREFIX_3 "\tCalled party number:  [%s]\n", pvt->cd.call_dest_e164);
1317         }
1318
1319         /* Decide if we are allowing Gatekeeper routed calls*/
1320         if ((!strcasecmp(cd->sourceIp, gatekeeper)) && (gkroute == -1) && (usingGk)) {
1321                 if (!ast_strlen_zero(cd->call_dest_e164)) {
1322                         strncpy(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten) - 1);
1323                         strncpy(pvt->context, default_context, sizeof(pvt->context) - 1); 
1324                 } else {
1325                         alias = find_alias(cd->call_dest_alias);
1326                         if (!alias) {
1327                                 ast_log(LOG_ERROR, "Call for %s rejected, alias not found\n", cd->call_dest_alias);
1328                                 return NULL;
1329                         }
1330                         strncpy(pvt->exten, alias->name, sizeof(pvt->exten) - 1);
1331                         strncpy(pvt->context, alias->context, sizeof(pvt->context) - 1);
1332                 }
1333         } else {
1334                 /* Either this call is not from the Gatekeeper 
1335                    or we are not allowing gk routed calls */
1336                 user  = find_user(cd);
1337                 if (!user) {
1338                         if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
1339                                 strncpy(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten) - 1);
1340                         } else {
1341                                 strncpy(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten) - 1);
1342                         }
1343                         if (ast_strlen_zero(default_context)) {
1344                                 ast_log(LOG_ERROR, "Call from '%s' rejected due to no default context\n", pvt->cd.call_source_aliases);
1345                                 return NULL;
1346                         }
1347                         strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
1348                         ast_log(LOG_DEBUG, "Sending %s to context [%s]\n", cd->call_source_aliases, pvt->context);
1349                         /* XXX: Is it really required??? */
1350 #if 0
1351                         memset(&pvt->options, 0, sizeof(pvt->options));
1352 #endif
1353                 } else {
1354                         if (user->host) {
1355                                 if (strcasecmp(cd->sourceIp, ast_inet_ntoa(iabuf, sizeof(iabuf), user->addr.sin_addr))) {
1356                                         if (ast_strlen_zero(user->context)) {
1357                                                 if (ast_strlen_zero(default_context)) {
1358                                                         ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s) and no default context\n", user->name, cd->sourceIp);
1359                                                         return NULL;
1360                                                 }
1361                                                 strncpy(pvt->context, default_context, sizeof(pvt->context) - 1);
1362                                         } else {
1363                                                 strncpy(pvt->context, user->context, sizeof(pvt->context) - 1);
1364                                         }
1365                                         pvt->exten[0] = 'i';
1366                                         pvt->exten[1] = '\0';
1367                                         ast_log(LOG_ERROR, "Call from '%s' rejected due to non-matching IP address (%s)s\n", user->name, cd->sourceIp);
1368                                         return NULL;    /* XXX: Hmmm... Why to setup context if we drop connection immediately??? */
1369                                 }
1370                         }
1371                         strncpy(pvt->context, user->context, sizeof(pvt->context) - 1);
1372                         memcpy(&pvt->options, &user->options, sizeof(pvt->options));
1373                         if (!ast_strlen_zero(pvt->cd.call_dest_e164)) {
1374                                 strncpy(pvt->exten, cd->call_dest_e164, sizeof(pvt->exten) - 1);
1375                         } else {
1376                                 strncpy(pvt->exten, cd->call_dest_alias, sizeof(pvt->exten) - 1);
1377                         }
1378                         if (!ast_strlen_zero(user->accountcode)) {
1379                                 strncpy(pvt->accountcode, user->accountcode, sizeof(pvt->accountcode) - 1);
1380                         } 
1381                         if (user->amaflags) {
1382                                 pvt->amaflags = user->amaflags;
1383                         } 
1384                 } 
1385         }
1386         return &pvt->options;
1387 }
1388
1389 /**
1390  * Call-back function to start PBX when OpenH323 ready to serve incoming call
1391  *
1392  * Returns 1 on success
1393  */
1394 static int answer_call(unsigned call_reference, const char *token)
1395 {
1396         struct oh323_pvt *pvt;
1397         struct ast_channel *c = NULL;
1398
1399         if (h323debug)
1400                 ast_log(LOG_DEBUG, "Preparing Asterisk to answer for %s\n", token);
1401
1402         /* Find the call or allocate a private structure if call not found */
1403         pvt = find_call_locked(call_reference, token); 
1404         if (!pvt) {
1405                 ast_log(LOG_ERROR, "Something is wrong: answer_call\n");
1406                 return 0;
1407         }
1408         /* allocate a channel and tell asterisk about it */
1409         c = __oh323_new(pvt, AST_STATE_RINGING, pvt->cd.call_token);
1410
1411         /* And release when done */
1412         ast_mutex_unlock(&pvt->lock);
1413         if (!c) {
1414                 ast_log(LOG_ERROR, "Couldn't create channel. This is bad\n");
1415                 return 0;
1416         }
1417         return 1;
1418 }
1419
1420 /**
1421  * Call-back function to establish an outgoing H.323 call
1422  * 
1423  * Returns 1 on success 
1424  */
1425 int setup_outgoing_call(call_details_t *cd)
1426 {
1427         /* Use argument here or free it immediately */
1428         cleanup_call_details(cd);
1429
1430         return 1;
1431 }
1432
1433 /**
1434   *  Call-back function to signal asterisk that the channel is ringing
1435   *  Returns nothing
1436   */
1437 void chan_ringing(unsigned call_reference, const char *token)
1438 {
1439         struct ast_channel *c = NULL;
1440         struct oh323_pvt *pvt;
1441
1442         if (h323debug)
1443                 ast_log(LOG_DEBUG, "Ringing on %s\n", token);
1444
1445         pvt = find_call_locked(call_reference, token); 
1446         if (!pvt) {
1447                 ast_log(LOG_ERROR, "Something is wrong: ringing\n");
1448                 return;
1449         }
1450         if (!pvt->owner) {
1451                 ast_mutex_unlock(&pvt->lock);
1452                 ast_log(LOG_ERROR, "Channel has no owner\n");
1453                 return;
1454         }
1455         if (update_state(pvt, AST_STATE_RINGING, AST_CONTROL_RINGING))
1456                 ast_mutex_unlock(&pvt->owner->lock);
1457         ast_mutex_unlock(&pvt->lock);
1458         return;
1459 }
1460
1461 /**
1462   * Call-back function to cleanup communication
1463   * Returns nothing,
1464   */
1465 static void cleanup_connection(unsigned call_reference, const char *call_token)
1466 {       
1467         struct oh323_pvt *pvt;
1468
1469         ast_log(LOG_DEBUG, "Cleaning connection to %s\n", call_token);
1470         
1471         while (1) {
1472                 pvt = find_call_locked(call_reference, call_token); 
1473                 if (!pvt) {
1474                         if (h323debug)
1475                                 ast_log(LOG_DEBUG, "No connection for %s\n", call_token);
1476                         return;
1477                 }
1478                 if (!pvt->owner || !ast_mutex_trylock(&pvt->owner->lock))
1479                         break;
1480 #if 1
1481 #ifdef DEBUG_THREADS
1482                 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s, locked at %ld/%d by %s (%s:%d)\n", call_token, pvt->owner->lock.thread, pvt->owner->lock.reentrancy, pvt->owner->lock.func, pvt->owner->lock.file, pvt->owner->lock.lineno);
1483 #else
1484                 ast_log(LOG_NOTICE, "Avoiding H.323 destory deadlock on %s\n", call_token);
1485 #endif
1486 #endif
1487                 ast_mutex_unlock(&pvt->lock);
1488                 usleep(1);
1489         }
1490         if (pvt->rtp) {
1491                 /* Immediately stop RTP */
1492                 ast_rtp_destroy(pvt->rtp);
1493                 pvt->rtp = NULL;
1494         }
1495         /* Free dsp used for in-band DTMF detection */
1496         if (pvt->vad) {
1497                 ast_dsp_free(pvt->vad);
1498                 pvt->vad = NULL;
1499         }
1500         cleanup_call_details(&pvt->cd);
1501         pvt->alreadygone = 1;
1502         /* Send hangup */       
1503         if (pvt->owner) {
1504                 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
1505                 ast_queue_hangup(pvt->owner);
1506                 ast_mutex_unlock(&pvt->owner->lock);
1507         }
1508         ast_mutex_unlock(&pvt->lock);
1509         if (h323debug)
1510                 ast_log(LOG_DEBUG, "Connection to %s cleaned\n", call_token);
1511         return; 
1512 }
1513
1514 static void hangup_connection(unsigned int call_reference, const char *token, int cause)
1515 {
1516         struct oh323_pvt *pvt;
1517
1518         ast_log(LOG_DEBUG, "Hanging up connection to %s with cause %d\n", token, cause);
1519         
1520         pvt = find_call_locked(call_reference, token); 
1521         if (!pvt) {
1522                 return;
1523         }
1524         if (pvt->owner && !ast_mutex_trylock(&pvt->owner->lock)) {
1525                 pvt->owner->_softhangup |= AST_SOFTHANGUP_DEV;
1526                 pvt->owner->hangupcause = pvt->hangupcause = cause;
1527                 ast_queue_hangup(pvt->owner);
1528                 ast_mutex_unlock(&pvt->owner->lock);
1529         }
1530         else {
1531                 pvt->needhangup = 1;
1532                 pvt->hangupcause = cause;
1533                 ast_log(LOG_DEBUG, "Hangup for %s is pending\n", token);
1534         }
1535         ast_mutex_unlock(&pvt->lock);
1536 }
1537
1538 void set_dtmf_payload(unsigned call_reference, const char *token, int payload)
1539 {
1540         struct oh323_pvt *pvt;
1541
1542         if (h323debug)
1543                 ast_log(LOG_DEBUG, "Setting DTMF payload to %d on %s\n", payload, token);
1544
1545         pvt = find_call_locked(call_reference, token);
1546         if (!pvt) {
1547                 return;
1548         }
1549         if (pvt->rtp) {
1550                 ast_rtp_set_rtpmap_type(pvt->rtp, payload, "audio", "telephone-event");
1551         }
1552         ast_mutex_unlock(&pvt->lock);
1553         if (h323debug)
1554                 ast_log(LOG_DEBUG, "DTMF payload on %s set to %d\n", token, payload);
1555 }
1556
1557 static void set_local_capabilities(unsigned call_reference, const char *token)
1558 {
1559         struct oh323_pvt *pvt;
1560         int capability, dtmfmode;
1561
1562         if (h323debug)
1563                 ast_log(LOG_DEBUG, "Setting capabilities for connection %s\n", token);
1564
1565         pvt = find_call_locked(call_reference, token);
1566         if (!pvt)
1567                 return;
1568         capability = pvt->options.capability;
1569         dtmfmode = pvt->options.dtmfmode;
1570         ast_mutex_unlock(&pvt->lock);
1571         h323_set_capabilities(token, capability, dtmfmode);
1572
1573         if (h323debug)
1574                 ast_log(LOG_DEBUG, "Capabilities for connection %s is set\n", token);
1575 }
1576
1577 static void *do_monitor(void *data)
1578 {
1579         int res;
1580         int reloading;
1581         struct oh323_pvt *oh323 = NULL;
1582         
1583         for(;;) {
1584                 /* Check for a reload request */
1585                 ast_mutex_lock(&h323_reload_lock);
1586                 reloading = h323_reloading;
1587                 h323_reloading = 0;
1588                 ast_mutex_unlock(&h323_reload_lock);
1589                 if (reloading) {
1590                         if (option_verbose > 0) {
1591                                 ast_verbose(VERBOSE_PREFIX_1 "Reloading H.323\n");
1592                         }
1593                         h323_do_reload();
1594                 }
1595                 /* Check for interfaces needing to be killed */
1596                 ast_mutex_lock(&iflock);
1597 restartsearch:          
1598                 oh323 = iflist;
1599                 while(oh323) {
1600                         if (oh323->needdestroy) {
1601                                 __oh323_destroy(oh323);
1602                                 goto restartsearch;
1603                         }
1604                         oh323 = oh323->next;
1605                 }
1606                 ast_mutex_unlock(&iflock);
1607                 pthread_testcancel();
1608                 /* Wait for sched or io */
1609                 res = ast_sched_wait(sched);
1610                 if ((res < 0) || (res > 1000)) {
1611                         res = 1000;
1612                 }
1613                 res = ast_io_wait(io, res);
1614                 pthread_testcancel();
1615                 ast_mutex_lock(&monlock);
1616                 if (res >= 0) {
1617                         ast_sched_runq(sched);
1618                 }
1619                 ast_mutex_unlock(&monlock);
1620         }
1621         /* Never reached */
1622         return NULL;
1623 }
1624
1625 static int restart_monitor(void)
1626 {
1627         pthread_attr_t attr;
1628         /* If we're supposed to be stopped -- stay stopped */
1629         if (monitor_thread == AST_PTHREADT_STOP) {
1630                 return 0;
1631         }
1632         if (ast_mutex_lock(&monlock)) {
1633                 ast_log(LOG_WARNING, "Unable to lock monitor\n");
1634                 return -1;
1635         }
1636         if (monitor_thread == pthread_self()) {
1637                 ast_mutex_unlock(&monlock);
1638                 ast_log(LOG_WARNING, "Cannot kill myself\n");
1639                 return -1;
1640         }
1641         if (monitor_thread && (monitor_thread != AST_PTHREADT_NULL)) {
1642                 /* Wake up the thread */
1643                 pthread_kill(monitor_thread, SIGURG);
1644         } else {        
1645                 pthread_attr_init(&attr);
1646                 pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED);
1647                 /* Start a new monitor */
1648                 if (ast_pthread_create(&monitor_thread, &attr, do_monitor, NULL) < 0) {
1649                         ast_mutex_unlock(&monlock);
1650                         ast_log(LOG_ERROR, "Unable to start monitor thread.\n");
1651                         return -1;
1652                 }
1653
1654         }
1655         ast_mutex_unlock(&monlock);
1656         return 0;
1657 }
1658
1659 static int h323_do_trace(int fd, int argc, char *argv[])
1660 {
1661         if (argc != 3) {
1662                 return RESULT_SHOWUSAGE;
1663         }
1664         h323_debug(1, atoi(argv[2]));
1665         ast_cli(fd, "H.323 trace set to level %s\n", argv[2]);
1666         return RESULT_SUCCESS;
1667 }
1668
1669 static int h323_no_trace(int fd, int argc, char *argv[])
1670 {
1671         if (argc != 3) {
1672                 return RESULT_SHOWUSAGE;
1673         }
1674         h323_debug(0,0);
1675         ast_cli(fd, "H.323 trace disabled\n");
1676         return RESULT_SUCCESS;
1677 }
1678
1679 static int h323_do_debug(int fd, int argc, char *argv[])
1680 {
1681         if (argc != 2) {
1682                 return RESULT_SHOWUSAGE;
1683         }
1684         h323debug = 1;
1685         ast_cli(fd, "H323 debug enabled\n");
1686         return RESULT_SUCCESS;
1687 }
1688
1689 static int h323_no_debug(int fd, int argc, char *argv[])
1690 {
1691         if (argc != 3) {
1692                 return RESULT_SHOWUSAGE;
1693         }
1694         h323debug = 0;
1695         ast_cli(fd, "H323 Debug disabled\n");
1696         return RESULT_SUCCESS;
1697 }
1698
1699 static int h323_gk_cycle(int fd, int argc, char *argv[])
1700 {
1701 #if 0
1702         if (argc != 3) {
1703                 return RESULT_SHOWUSAGE;
1704         }       
1705         h323_gk_urq();
1706         
1707         /* Possibly register with a GK */
1708         if (!gatekeeper_disable) {
1709                 if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
1710                         ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
1711                 }
1712         }
1713 #endif
1714         return RESULT_SUCCESS;
1715 }
1716
1717 static int h323_ep_hangup(int fd, int argc, char *argv[])
1718 {
1719         if (argc != 3) {
1720                 return RESULT_SHOWUSAGE;
1721         }
1722         if (h323_soft_hangup(argv[2])) {
1723                 ast_verbose(VERBOSE_PREFIX_3 "Hangup succeeded on %s\n", argv[2]);
1724         } else { 
1725                 ast_verbose(VERBOSE_PREFIX_3 "Hangup failed for %s\n", argv[2]);
1726         }
1727         return RESULT_SUCCESS;
1728 }
1729
1730 static int h323_tokens_show(int fd, int argc, char *argv[])
1731 {
1732         if (argc != 3) {
1733                 return RESULT_SHOWUSAGE;
1734         }
1735         h323_show_tokens();
1736         return RESULT_SUCCESS;
1737 }
1738
1739 static char trace_usage[] = 
1740 "Usage: h.323 trace <level num>\n"
1741 "       Enables H.323 stack tracing for debugging purposes\n";
1742
1743 static char no_trace_usage[] = 
1744 "Usage: h.323 no trace\n"
1745 "       Disables H.323 stack tracing for debugging purposes\n";
1746
1747 static char debug_usage[] = 
1748 "Usage: h.323 debug\n"
1749 "       Enables H.323 debug output\n";
1750
1751 static char no_debug_usage[] = 
1752 "Usage: h.323 no debug\n"
1753 "       Disables H.323 debug output\n";
1754
1755 static char show_codec_usage[] = 
1756 "Usage: h.323 show codec\n"
1757 "       Shows all enabled codecs\n";
1758
1759 static char show_cycle_usage[] = 
1760 "Usage: h.323 gk cycle\n"
1761 "       Manually re-register with the Gatekeper (Currently Disabled)\n";
1762
1763 static char show_hangup_usage[] = 
1764 "Usage: h.323 hangup <token>\n"
1765 "       Manually try to hang up call identified by <token>\n";
1766
1767 static char show_tokens_usage[] = 
1768 "Usage: h.323 show tokens\n"
1769 "       Print out all active call tokens\n";
1770
1771 static char h323_reload_usage[] =
1772 "Usage: h323 reload\n"
1773 "       Reloads H.323 configuration from sip.conf\n";
1774
1775 static struct ast_cli_entry  cli_trace =
1776         { { "h.323", "trace", NULL }, h323_do_trace, "Enable H.323 Stack Tracing", trace_usage };
1777 static struct ast_cli_entry  cli_no_trace =
1778         { { "h.323", "no", "trace", NULL }, h323_no_trace, "Disable H.323 Stack Tracing", no_trace_usage };
1779 static struct ast_cli_entry  cli_debug =
1780         { { "h.323", "debug", NULL }, h323_do_debug, "Enable H.323 debug", debug_usage };
1781 static struct ast_cli_entry  cli_no_debug =
1782         { { "h.323", "no", "debug", NULL }, h323_no_debug, "Disable H.323 debug", no_debug_usage };
1783 static struct ast_cli_entry  cli_show_codecs =
1784         { { "h.323", "show", "codecs", NULL }, h323_show_codec, "Show enabled codecs", show_codec_usage };
1785 static struct ast_cli_entry  cli_gk_cycle =
1786         { { "h.323", "gk", "cycle", NULL }, h323_gk_cycle, "Manually re-register with the Gatekeper", show_cycle_usage };
1787 static struct ast_cli_entry  cli_hangup_call =
1788         { { "h.323", "hangup", NULL }, h323_ep_hangup, "Manually try to hang up a call", show_hangup_usage };
1789 static struct ast_cli_entry  cli_show_tokens =
1790         { { "h.323", "show", "tokens", NULL }, h323_tokens_show, "Show all active call tokens", show_tokens_usage };
1791
1792 static int update_common_options(struct ast_variable *v, struct call_options *options)
1793 {
1794         unsigned int format;
1795         int tmp;
1796
1797         if (!strcasecmp(v->name, "allow")) {
1798                 format = ast_getformatbyname(v->value);
1799                 if (format < 1) 
1800                         ast_log(LOG_WARNING, "Cannot allow unknown format '%s'\n", v->value);
1801                 else
1802                         options->capability |= format;
1803         } else if (!strcasecmp(v->name, "disallow")) {
1804                 format = ast_getformatbyname(v->value);
1805                 if (format < 1) 
1806                         ast_log(LOG_WARNING, "Cannot disallow unknown format '%s'\n", v->value);
1807                 else
1808                         options->capability &= ~format;
1809         } else if (!strcasecmp(v->name, "dtmfmode")) {
1810                 if (!strcasecmp(v->value, "inband")) {
1811                         options->dtmfmode = H323_DTMF_INBAND;
1812                 } else if (!strcasecmp(v->value, "rfc2833")) {
1813                         options->dtmfmode = H323_DTMF_RFC2833;
1814                 } else {
1815                         ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", v->value);
1816                         options->dtmfmode = H323_DTMF_RFC2833;
1817                 }
1818         } else if (!strcasecmp(v->name, "dtmfcodec")) {
1819                 tmp = atoi(v->value);
1820                 if (tmp < 96)
1821                         ast_log(LOG_WARNING, "Invalid global dtmfcodec value %s\n", v->value);
1822                 else
1823                         options->dtmfcodec = tmp;
1824         } else if (!strcasecmp(v->name, "bridge")) {
1825                 options->bridge = ast_true(v->value);
1826         } else if (!strcasecmp(v->name, "nat")) {
1827                 options->nat = ast_true(v->value);
1828         } else if (!strcasecmp(v->name, "noFastStart")) {
1829                 options->noFastStart = ast_true(v->value);
1830         } else if (!strcasecmp(v->name, "noH245Tunneling")) {
1831                 options->noH245Tunneling = ast_true(v->value);
1832         } else if (!strcasecmp(v->name, "noSilenceSuppression")) {
1833                 options->noSilenceSuppression = ast_true(v->value);
1834         } else if (!strcasecmp(v->name, "progress_setup")) {
1835                 tmp = atoi(v->value);
1836                 if ((tmp != 0) && (tmp != 1) && (tmp != 3) && (tmp != 8)) {
1837                         ast_log(LOG_WARNING, "Invalid value %d for progress_setup at line %d, assuming 0\n", tmp, v->lineno);
1838                         tmp = 0;
1839                 }
1840                 options->progress_setup = tmp;
1841         } else if (!strcasecmp(v->name, "progress_alert")) {
1842                 tmp = atoi(v->value);
1843                 if ((tmp != 0) && (tmp != 8)) {
1844                         ast_log(LOG_WARNING, "Invalud value %d for progress_alert at line %d, assuming 0\n", tmp, v->lineno);
1845                         tmp = 0;
1846                 }
1847                 options->progress_alert = tmp;
1848         } else if (!strcasecmp(v->name, "progress_audio")) {
1849                 options->progress_audio = ast_true(v->value);
1850         } else
1851                 return 1;
1852
1853         return 0;
1854 }
1855
1856 static struct oh323_alias *build_alias(char *name, struct ast_variable *v)
1857 {
1858         struct oh323_alias *alias;
1859
1860         alias = (struct oh323_alias *)malloc(sizeof(struct oh323_alias));
1861         if (alias) {
1862                 memset(alias, 0, sizeof(struct oh323_alias));
1863                 strncpy(alias->name, name, sizeof(alias->name) - 1);
1864                 while (v) {
1865                         if (!strcasecmp(v->name, "e164")) {
1866                                 strncpy(alias->e164,  v->value, sizeof(alias->e164) - 1);
1867                         } else if (!strcasecmp(v->name, "prefix")) {
1868                                 strncpy(alias->prefix,  v->value, sizeof(alias->prefix) - 1);
1869                         } else if (!strcasecmp(v->name, "context")) {
1870                                 strncpy(alias->context,  v->value, sizeof(alias->context) - 1);
1871                         } else if (!strcasecmp(v->name, "secret")) {
1872                                 strncpy(alias->secret,  v->value, sizeof(alias->secret) - 1);
1873                         } else {
1874                                 if (strcasecmp(v->value, "h323")) {     
1875                                         ast_log(LOG_WARNING, "Keyword %s does not make sense in type=h323\n", v->value);
1876                                 }
1877                         }
1878                         v = v->next;
1879                 }
1880         }
1881         return alias;
1882 }
1883
1884 static struct oh323_user *build_user(char *name, struct ast_variable *v)
1885 {
1886         struct oh323_user *user;
1887         int format;
1888
1889         user = (struct oh323_user *)malloc(sizeof(struct oh323_user));
1890         if (user) {
1891                 memset(user, 0, sizeof(struct oh323_user));
1892                 strncpy(user->name, name, sizeof(user->name) - 1);
1893                 memcpy(&user->options, &global_options, sizeof(user->options));
1894                 /* Set default context */
1895                 strncpy(user->context, default_context, sizeof(user->context) - 1);
1896                 while(v) {
1897                         if (!strcasecmp(v->name, "context")) {
1898                                 strncpy(user->context, v->value, sizeof(user->context) - 1);
1899                         } else if (!update_common_options(v, &user->options)) {
1900                                 /* dummy */
1901                         } else if (!strcasecmp(v->name, "secret")) {
1902                                 strncpy(user->secret, v->value, sizeof(user->secret) - 1);
1903                         } else if (!strcasecmp(v->name, "callerid")) {
1904                                 strncpy(user->callerid, v->value, sizeof(user->callerid) - 1);
1905                         } else if (!strcasecmp(v->name, "accountcode")) {
1906                                 strncpy(user->accountcode, v->value, sizeof(user->accountcode) - 1);
1907                         } else if (!strcasecmp(v->name, "host")) {
1908                                 if (!strcasecmp(v->value, "dynamic")) {
1909                                         ast_log(LOG_ERROR, "A dynamic host on a type=user does not make any sense\n");
1910                                         free(user);
1911                                         return NULL;
1912                                 } else if (ast_get_ip(&user->addr, v->value)) {
1913                                         free(user);
1914                                         return NULL;
1915                                 } 
1916                                 /* Let us know we need to use ip authentication */
1917                                 user->host = 1;
1918                         } else if (!strcasecmp(v->name, "amaflags")) {
1919                                 format = ast_cdr_amaflags2int(v->value);
1920                                 if (format < 0) {
1921                                         ast_log(LOG_WARNING, "Invalid AMA Flags: %s at line %d\n", v->value, v->lineno);
1922                                 } else {
1923                                         user->amaflags = format;
1924                                 }
1925                         }
1926                         v = v->next;
1927                 }
1928         }
1929         return user;
1930 }
1931
1932 static struct oh323_peer *build_peer(char *name, struct ast_variable *v)
1933 {
1934         struct oh323_peer *peer;
1935         struct oh323_peer *prev;
1936         struct ast_ha *oldha = NULL;
1937         int found=0;
1938
1939         prev = NULL;
1940         ast_mutex_lock(&peerl.lock);
1941         peer = peerl.peers;
1942
1943         while(peer) {
1944                 if (!strcasecmp(peer->name, name)) {    
1945                         break;
1946                 }
1947                 prev = peer;
1948                 peer = peer->next;
1949         }
1950
1951         if (peer) {
1952                 found++;
1953                 /* Already in the list, remove it and it will be added back (or FREE'd) */
1954                 if (prev) {
1955                         prev->next = peer->next;
1956                 } else {
1957                         peerl.peers = peer->next;
1958                 }
1959                 ast_mutex_unlock(&peerl.lock);
1960         } else {
1961                 ast_mutex_unlock(&peerl.lock);
1962                 peer = (struct oh323_peer*)malloc(sizeof(struct oh323_peer));
1963                 if (peer)
1964                         memset(peer, 0, sizeof(struct oh323_peer));
1965         }
1966         if (peer) {
1967                 if (!found) {
1968                         strncpy(peer->name, name, sizeof(peer->name) - 1);
1969                         peer->addr.sin_port = htons(h323_signalling_port);
1970                         peer->addr.sin_family = AF_INET;
1971                 }
1972                 oldha = peer->ha;
1973                 peer->ha = NULL;
1974                 peer->addr.sin_family = AF_INET;
1975                 memcpy(&peer->options, &global_options, sizeof(peer->options));
1976
1977                 while(v) {
1978                         if (!update_common_options(v, &peer->options)) {
1979                                 /* dummy */
1980                         } else if (!strcasecmp(v->name, "host")) {
1981                                 if (!strcasecmp(v->value, "dynamic")) {
1982                                         ast_log(LOG_ERROR, "Dynamic host configuration not implemented.\n");
1983                                         free(peer);
1984                                         return NULL;
1985                                 }
1986                                 if (ast_get_ip(&peer->addr, v->value)) {
1987                                                 ast_log(LOG_ERROR, "Could not determine IP for %s\n", v->value);
1988                                                 free(peer);
1989                                                 return NULL;
1990                                 }
1991                         } else if (!strcasecmp(v->name, "port")) {
1992                                 peer->addr.sin_port = htons(atoi(v->value));
1993                         }
1994                         v=v->next;
1995                 }
1996         }
1997         return peer;
1998 }
1999
2000 int reload_config(void)
2001 {       
2002         int format;
2003         struct ast_config *cfg;
2004         struct ast_variable *v;
2005         struct oh323_peer *peer   = NULL;
2006         struct oh323_user *user   = NULL;
2007         struct oh323_alias *alias = NULL;
2008         struct ast_hostent ahp; struct hostent *hp;
2009         char *cat;
2010         char *utype;
2011         
2012         cfg = ast_config_load(config);
2013
2014         /* We *must* have a config file otherwise stop immediately */
2015         if (!cfg) {
2016                 ast_log(LOG_NOTICE, "Unable to load config %s, H.323 disabled\n", config);
2017                 return 1;
2018         }
2019         
2020        /* fire up the H.323 Endpoint */       
2021         if (!h323_end_point_exist()) {
2022                h323_end_point_create();        
2023         }
2024         h323debug = 0;
2025         memset(&bindaddr, 0, sizeof(bindaddr));
2026         memset(&global_options, 0, sizeof(global_options));
2027         global_options.dtmfcodec = 101;
2028         global_options.dtmfmode = H323_DTMF_RFC2833;
2029         global_options.capability = ~0; /* All capabilities */
2030         global_options.bridge = 1;              /* Do native bridging by default */
2031         v = ast_variable_browse(cfg, "general");
2032         while(v) {
2033                 /* Create the interface list */
2034                 if (!strcasecmp(v->name, "port")) {
2035                         h323_signalling_port = (int)strtol(v->value, NULL, 10);
2036                 } else if (!strcasecmp(v->name, "bindaddr")) {
2037                         if (!(hp = ast_gethostbyname(v->value, &ahp))) {
2038                                 ast_log(LOG_WARNING, "Invalid address: %s\n", v->value);
2039                         } else {
2040                                 memcpy(&bindaddr.sin_addr, hp->h_addr, sizeof(bindaddr.sin_addr));
2041                         }
2042                 } else if (!strcasecmp(v->name, "tos")) {
2043                         if (sscanf(v->value, "%d", &format)) {
2044                                 tos = format & 0xff;
2045                         } else if (!strcasecmp(v->value, "lowdelay")) {
2046                                 tos = IPTOS_LOWDELAY;
2047                         } else if (!strcasecmp(v->value, "throughput")) {
2048                                 tos = IPTOS_THROUGHPUT;
2049                         } else if (!strcasecmp(v->value, "reliability")) {
2050                                 tos = IPTOS_RELIABILITY;
2051                         } else if (!strcasecmp(v->value, "mincost")) {
2052                                 tos = IPTOS_MINCOST;
2053                         } else if (!strcasecmp(v->value, "none")) {
2054                                 tos = 0;
2055                         } else {
2056                                 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be 'lowdelay', 'throughput', 'reliability', 'mincost', or 'none'\n", v->lineno);
2057                         }
2058                 } else if (!strcasecmp(v->name, "gatekeeper")) {
2059                         if (!strcasecmp(v->value, "DISABLE")) {
2060                                 gatekeeper_disable = 1;
2061                                 usingGk = 0;
2062                         } else if (!strcasecmp(v->value, "DISCOVER")) {
2063                                 gatekeeper_disable = 0;
2064                                 gatekeeper_discover = 1;
2065                                 usingGk = 1;
2066                         } else {
2067                                 gatekeeper_disable = 0;
2068                                 usingGk = 1;
2069                                 strncpy(gatekeeper, v->value, sizeof(gatekeeper) - 1);
2070                         }
2071                 } else if (!strcasecmp(v->name, "secret")) {
2072                         strncpy(secret, v->value, sizeof(secret) - 1);
2073                 } else if (!strcasecmp(v->name, "AllowGKRouted")) {
2074                         gkroute = ast_true(v->value);
2075                 } else if (!strcasecmp(v->name, "context")) {
2076                         strncpy(default_context, v->value, sizeof(default_context) - 1);
2077                         ast_verbose(VERBOSE_PREFIX_2 "Setting default context to %s\n", default_context);       
2078                 } else if (!strcasecmp(v->name, "UserByAlias")) {
2079                         userbyalias = ast_true(v->value);
2080                 } else if (!update_common_options(v, &global_options)) {
2081                         /* dummy */
2082                 }
2083                 v = v->next;    
2084         }
2085         
2086         cat = ast_category_browse(cfg, NULL);
2087         while(cat) {
2088                 if (strcasecmp(cat, "general")) {
2089                         utype = ast_variable_retrieve(cfg, cat, "type");
2090                         if (utype) {
2091                                 if (!strcasecmp(utype, "user")) {
2092                                         user = build_user(cat, ast_variable_browse(cfg, cat));
2093                                         if (user) {
2094                                                 ast_mutex_lock(&userl.lock);
2095                                                 user->next = userl.users;
2096                                                 userl.users = user;
2097                                                 ast_mutex_unlock(&userl.lock);
2098                                         }
2099                                 }  else if (!strcasecmp(utype, "peer")) {
2100                                         peer = build_peer(cat, ast_variable_browse(cfg, cat));
2101                                         if (peer) {
2102                                                 ast_mutex_lock(&peerl.lock);
2103                                                 peer->next = peerl.peers;
2104                                                 peerl.peers = peer;
2105                                                 ast_mutex_unlock(&peerl.lock);
2106                                         }
2107                                 }  else if (!strcasecmp(utype, "friend")) {
2108                                         user = build_user(cat, ast_variable_browse(cfg, cat));
2109                                         peer = build_peer(cat, ast_variable_browse(cfg, cat));
2110                                         if (user) {
2111                                                 ast_mutex_lock(&userl.lock);
2112                                                 user->next = userl.users;
2113                                                 userl.users = user;
2114                                                 ast_mutex_unlock(&userl.lock);
2115                                         }
2116                                         if (peer) {
2117                                                 ast_mutex_lock(&peerl.lock);
2118                                                 peer->next = peerl.peers;
2119                                                 peerl.peers = peer;
2120                                                 ast_mutex_unlock(&peerl.lock);
2121                                         }
2122                                 }  else if (!strcasecmp(utype, "h323") || !strcasecmp(utype, "alias")) {
2123                                         alias = build_alias(cat, ast_variable_browse(cfg, cat));
2124                                         if (alias) {
2125                                                 ast_mutex_lock(&aliasl.lock);
2126                                                 alias->next = aliasl.aliases;
2127                                                 aliasl.aliases = alias;
2128                                                 ast_mutex_unlock(&aliasl.lock);
2129                                         }
2130                                 } else {
2131                                         ast_log(LOG_WARNING, "Unknown type '%s' for '%s' in %s\n", utype, cat, config);
2132                                 }
2133                         } else {
2134                                 ast_log(LOG_WARNING, "Section '%s' lacks type\n", cat);
2135                         }
2136                 }
2137                 cat = ast_category_browse(cfg, cat);
2138         }
2139         ast_config_destroy(cfg);
2140
2141         /* Register our H.323 aliases if any*/
2142         while (alias) {         
2143                 if (h323_set_alias(alias)) {
2144                         ast_log(LOG_ERROR, "Alias %s rejected by endpoint\n", alias->name);
2145                         return -1;
2146                 }       
2147                 alias = alias->next;
2148         }
2149
2150         return 0;
2151 }
2152
2153 void delete_users(void)
2154 {
2155         struct oh323_user *user, *userlast;
2156         struct oh323_peer *peer;
2157         
2158         /* Delete all users */
2159         ast_mutex_lock(&userl.lock);
2160         for (user=userl.users;user;) {
2161                 userlast = user;
2162                 user=user->next;
2163                 free(userlast);
2164         }
2165         userl.users=NULL;
2166         ast_mutex_unlock(&userl.lock);
2167         ast_mutex_lock(&peerl.lock);
2168         for (peer=peerl.peers;peer;) {
2169                 /* Assume all will be deleted, and we'll find out for sure later */
2170                 peer->delme = 1;
2171                 peer = peer->next;
2172         }
2173         ast_mutex_unlock(&peerl.lock);
2174 }
2175
2176 void delete_aliases(void)
2177 {
2178         struct oh323_alias *alias, *aliaslast;
2179                 
2180         /* Delete all users */
2181         ast_mutex_lock(&aliasl.lock);
2182         for (alias=aliasl.aliases;alias;) {
2183                 aliaslast = alias;
2184                 alias=alias->next;
2185                 free(aliaslast);
2186         }
2187         aliasl.aliases=NULL;
2188         ast_mutex_unlock(&aliasl.lock);
2189 }
2190
2191 void prune_peers(void)
2192 {
2193         /* Prune peers who still are supposed to be deleted */
2194         struct oh323_peer *peer, *peerlast, *peernext;
2195         ast_mutex_lock(&peerl.lock);
2196         peerlast = NULL;
2197         for (peer=peerl.peers;peer;) {
2198                 peernext = peer->next;
2199                 if (peer->delme) {
2200                         free(peer);
2201                         if (peerlast) {
2202                                 peerlast->next = peernext;
2203                         } else {
2204                                 peerl.peers = peernext;
2205                         }
2206                 } else {
2207                         peerlast = peer;
2208                 }
2209                 peer = peernext;
2210         }
2211         ast_mutex_unlock(&peerl.lock);
2212 }
2213
2214 static int h323_reload(int fd, int argc, char *argv[])
2215 {
2216         ast_mutex_lock(&h323_reload_lock);
2217         if (h323_reloading) {
2218                 ast_verbose("Previous H.323 reload not yet done\n");
2219         } else {
2220                 h323_reloading = 1;
2221         }
2222         ast_mutex_unlock(&h323_reload_lock);
2223         restart_monitor();
2224         return 0;
2225 }
2226
2227 static int h323_do_reload(void)
2228 {
2229         delete_users();
2230         delete_aliases();
2231         prune_peers();
2232         reload_config();
2233         restart_monitor();
2234         return 0;
2235 }
2236
2237 int reload(void)
2238 {
2239         return h323_reload(0, 0, NULL);
2240 }
2241
2242 static struct ast_cli_entry  cli_h323_reload =
2243         { { "h.323", "reload", NULL }, h323_reload, "Reload H.323 configuration", h323_reload_usage };
2244
2245 static struct ast_rtp *oh323_get_rtp_peer(struct ast_channel *chan)
2246 {
2247         struct oh323_pvt *pvt;
2248         pvt = (struct oh323_pvt *) chan->tech_pvt;
2249         if (pvt && pvt->rtp && pvt->options.bridge) {
2250                 return pvt->rtp;
2251         }
2252         return NULL;
2253 }
2254
2255 static struct ast_rtp *oh323_get_vrtp_peer(struct ast_channel *chan)
2256 {
2257         return NULL;
2258 }
2259
2260 static char *convertcap(int cap)
2261 {
2262         switch (cap) {
2263         case AST_FORMAT_G723_1:
2264                 return "G.723";
2265         case AST_FORMAT_GSM:
2266                 return "GSM";
2267         case AST_FORMAT_ULAW:
2268                 return "ULAW";
2269         case AST_FORMAT_ALAW:
2270                 return "ALAW";
2271         case AST_FORMAT_ADPCM:
2272                 return "G.728";
2273         case AST_FORMAT_G729A:
2274                 return "G.729";
2275         case AST_FORMAT_SPEEX:
2276                 return "SPEEX";
2277         case AST_FORMAT_ILBC:
2278                 return "ILBC";
2279         default:
2280                 ast_log(LOG_NOTICE, "Don't know how to deal with mode %d\n", cap);
2281                 return NULL;
2282         }
2283 }
2284
2285 static int oh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp *rtp, struct ast_rtp *vrtp, int codecs)
2286 {
2287         /* XXX Deal with Video */
2288         struct oh323_pvt *pvt;
2289         struct sockaddr_in them;
2290         struct sockaddr_in us;
2291         char *mode;
2292         char iabuf[INET_ADDRSTRLEN];
2293
2294         if (!rtp) {
2295                 return 0;
2296         }
2297
2298         mode = convertcap(chan->writeformat); 
2299         pvt = (struct oh323_pvt *) chan->tech_pvt;
2300         if (!pvt) {
2301                 ast_log(LOG_ERROR, "No Private Structure, this is bad\n");
2302                 return -1;
2303         }
2304         ast_rtp_get_peer(rtp, &them);   
2305         ast_rtp_get_us(rtp, &us);
2306         h323_native_bridge(pvt->cd.call_token, ast_inet_ntoa(iabuf, sizeof(iabuf), them.sin_addr), mode);
2307         return 0;
2308 }
2309
2310 static struct ast_rtp_protocol oh323_rtp = {
2311         .type = type,
2312         .get_rtp_info = oh323_get_rtp_peer,
2313         .get_vrtp_info = oh323_get_vrtp_peer,
2314         .set_rtp_peer=  oh323_set_rtp_peer,
2315 };
2316
2317 int load_module()
2318 {
2319         int res;
2320         ast_mutex_init(&userl.lock);
2321         ast_mutex_init(&peerl.lock);
2322         ast_mutex_init(&aliasl.lock);
2323         sched = sched_context_create();
2324         if (!sched) {
2325                 ast_log(LOG_WARNING, "Unable to create schedule context\n");
2326         }
2327         io = io_context_create();
2328         if (!io) {
2329                 ast_log(LOG_WARNING, "Unable to create I/O context\n");
2330         }
2331         res = reload_config();
2332         if (res) {
2333                 return 0;
2334         } else {
2335                 /* Make sure we can register our channel type */
2336                 if (ast_channel_register(&oh323_tech)) {
2337                         ast_log(LOG_ERROR, "Unable to register channel class %s\n", type);
2338                         h323_end_process();
2339                         return -1;
2340                 }
2341                 ast_cli_register(&cli_debug);
2342                 ast_cli_register(&cli_no_debug);
2343                 ast_cli_register(&cli_trace);
2344                 ast_cli_register(&cli_no_trace);
2345                 ast_cli_register(&cli_show_codecs);
2346                 ast_cli_register(&cli_gk_cycle);
2347                 ast_cli_register(&cli_hangup_call);
2348                 ast_cli_register(&cli_show_tokens);
2349                 ast_cli_register(&cli_h323_reload);
2350
2351                 ast_rtp_proto_register(&oh323_rtp);
2352
2353                 /* Register our callback functions */
2354                 h323_callback_register(setup_incoming_call, 
2355                                                 setup_outgoing_call,                                                     
2356                                                 external_rtp_create, 
2357                                                 setup_rtp_connection, 
2358                                                 cleanup_connection, 
2359                                                 chan_ringing,
2360                                                 connection_made, 
2361                                                 send_digit,
2362                                                 answer_call,
2363                                                 progress,
2364                                                 set_dtmf_payload,
2365                                                 hangup_connection,
2366                                                 set_local_capabilities);
2367                 /* start the h.323 listener */
2368                 if (h323_start_listener(h323_signalling_port, bindaddr)) {
2369                         ast_log(LOG_ERROR, "Unable to create H323 listener.\n");
2370                         return -1;
2371                 }
2372                 /* Possibly register with a GK */
2373                 if (!gatekeeper_disable) {
2374                         if (h323_set_gk(gatekeeper_discover, gatekeeper, secret)) {
2375                                 ast_log(LOG_ERROR, "Gatekeeper registration failed.\n");
2376                                 return 0;
2377                         }
2378                 }
2379                 /* And start the monitor for the first time */
2380                 restart_monitor();
2381         }
2382         return res;
2383 }
2384
2385 int unload_module() 
2386 {
2387         struct oh323_pvt *p, *pl;
2388
2389         /* unregister commands */
2390         ast_cli_unregister(&cli_debug);
2391         ast_cli_unregister(&cli_no_debug);
2392         ast_cli_unregister(&cli_trace);
2393         ast_cli_unregister(&cli_no_trace);   
2394         ast_cli_unregister(&cli_show_codecs);
2395         ast_cli_unregister(&cli_gk_cycle);
2396         ast_cli_unregister(&cli_hangup_call);
2397         ast_cli_unregister(&cli_show_tokens);
2398         ast_cli_unregister(&cli_h323_reload);
2399         ast_rtp_proto_unregister(&oh323_rtp);
2400         ast_channel_unregister(&oh323_tech);
2401                 
2402         if (!ast_mutex_lock(&iflock)) {
2403                 /* hangup all interfaces if they have an owner */
2404                 p = iflist;
2405                 while(p) {
2406                         if (p->owner) {
2407                                 ast_softhangup(p->owner, AST_SOFTHANGUP_APPUNLOAD);
2408                         }
2409                         p = p->next;
2410                 }
2411                 iflist = NULL;
2412                 ast_mutex_unlock(&iflock);
2413         } else {
2414                 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
2415                 return -1;
2416         }
2417         if (!ast_mutex_lock(&monlock)) {
2418                 if (monitor_thread && (monitor_thread != AST_PTHREADT_STOP)) {
2419                         /* this causes a seg, anyone know why? */
2420                         pthread_cancel(monitor_thread);
2421                         pthread_kill(monitor_thread, SIGURG);
2422                         pthread_join(monitor_thread, NULL);
2423                 }
2424                 monitor_thread = AST_PTHREADT_STOP;
2425                 ast_mutex_unlock(&monlock);
2426         } else {
2427                 ast_log(LOG_WARNING, "Unable to lock the monitor\n");
2428                 return -1;
2429         }
2430         if (!ast_mutex_lock(&iflock)) {
2431                 /* destroy all the interfaces and free their memory */
2432                 p = iflist;
2433                 while(p) {
2434                         pl = p;
2435                         p = p->next;
2436                         /* free associated memory */
2437                         ast_mutex_destroy(&pl->lock);
2438                         free(pl);
2439                 }
2440                 iflist = NULL;
2441                 ast_mutex_unlock(&iflock);
2442         } else {
2443                 ast_log(LOG_WARNING, "Unable to lock the interface list\n");
2444                 return -1;
2445         }
2446         h323_gk_urq();
2447         h323_end_process();
2448         io_context_destroy(io);
2449         sched_context_destroy(sched);
2450         delete_users();
2451         delete_aliases();
2452         prune_peers();
2453         ast_mutex_destroy(&aliasl.lock);
2454         ast_mutex_destroy(&userl.lock);
2455         ast_mutex_destroy(&peerl.lock);
2456         return 0; 
2457
2458
2459 int usecount()
2460 {
2461         return usecnt;
2462 }
2463
2464 char *description()
2465 {
2466         return (char *) desc;
2467 }
2468
2469 char *key()
2470 {
2471         return ASTERISK_GPL_KEY;
2472 }