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