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