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