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