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