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