generate connected line info update from info in h.323 packets
[asterisk/asterisk.git] / addons / chan_ooh323.c
1 /*
2  * Copyright (C) 2004-2005 by Objective Systems, Inc.
3  *
4  * This software is furnished under an open source license and may be 
5  * used and copied only in accordance with the terms of this license. 
6  * The text of the license may generally be found in the root 
7  * directory of this installation in the COPYING file.  It 
8  * can also be viewed online at the following URL:
9  *
10  *   http://www.obj-sys.com/open/license.html
11  *
12  * Any redistributions of this file including modified versions must 
13  * maintain this copyright notice.
14  *
15  *****************************************************************************/
16
17 /* Reworked version I, Nov-2009, by Alexandr Anikin, may@telecom-service.ru */
18
19
20 /*** MODULEINFO
21         <defaultenabled>no</defaultenabled>
22  ***/
23
24 #include "chan_ooh323.h"
25 #include <math.h>
26
27 #define FORMAT_STRING_SIZE      512
28
29 /* Defaults */
30 #define DEFAULT_CONTEXT "default"
31 #define DEFAULT_H323ID "Asterisk PBX"
32 #define DEFAULT_LOGFILE "/var/log/asterisk/h323_log"
33 #define DEFAULT_H323ACCNT "ast_h323"
34
35 /* Flags */
36 #define H323_SILENCESUPPRESSION (1<<0)
37 #define H323_GKROUTED           (1<<1)
38 #define H323_TUNNELING          (1<<2)
39 #define H323_FASTSTART          (1<<3)
40 #define H323_OUTGOING           (1<<4)
41 #define H323_ALREADYGONE        (1<<5)
42 #define H323_NEEDDESTROY        (1<<6)
43 #define H323_DISABLEGK          (1<<7)
44 #define H323_NEEDSTART          (1<<8)
45
46 #define MAXT30  240
47 #define T38TOAUDIOTIMEOUT 30
48 #define T38_DISABLED 0
49 #define T38_ENABLED 1
50 #define T38_FAXGW 1
51
52 /* Channel description */
53 static const char type[] = "OOH323";
54 static const char tdesc[] = "Objective Systems H323 Channel Driver";
55 static const char config[] = "ooh323.conf";
56
57 struct ast_module *myself;
58
59 static struct ast_jb_conf default_jbconf =
60 {
61         .flags = 0,
62         .max_size = -1,
63         .resync_threshold = -1,
64         .impl = ""
65 };
66 static struct ast_jb_conf global_jbconf;
67
68 /* Channel Definition */
69 static struct ast_channel *ooh323_request(const char *type, format_t format, 
70                         const struct ast_channel *requestor,  void *data, int *cause);
71 static int ooh323_digit_begin(struct ast_channel *ast, char digit);
72 static int ooh323_digit_end(struct ast_channel *ast, char digit, unsigned int duration);
73 static int ooh323_call(struct ast_channel *ast, char *dest, int timeout);
74 static int ooh323_hangup(struct ast_channel *ast);
75 static int ooh323_answer(struct ast_channel *ast);
76 static struct ast_frame *ooh323_read(struct ast_channel *ast);
77 static int ooh323_write(struct ast_channel *ast, struct ast_frame *f);
78 static int ooh323_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen);
79 static int ooh323_queryoption(struct ast_channel *ast, int option, void *data, int *datalen);
80 static int ooh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan);
81
82 static enum ast_rtp_glue_result ooh323_get_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance **rtp);
83 static enum ast_rtp_glue_result ooh323_get_vrtp_peer(struct ast_channel *chan, struct ast_rtp_instance **rtp);
84 static int ooh323_set_rtp_peer(struct ast_channel *chan, struct ast_rtp_instance *rtp, 
85           struct ast_rtp_instance *vrtp, struct ast_rtp_instance *trtp, format_t codecs, int nat_active);
86
87 static struct ast_udptl *ooh323_get_udptl_peer(struct ast_channel *chan);
88 static int ooh323_set_udptl_peer(struct ast_channel *chan, struct ast_udptl *udptl);
89
90 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref);
91
92 struct ooh323_peer *find_friend(const char *name, int port);
93
94
95 static const struct ast_channel_tech ooh323_tech = {
96         .type = type,
97         .description = tdesc,
98         .capabilities = -1,
99         .properties = AST_CHAN_TP_WANTSJITTER | AST_CHAN_TP_CREATESJITTER,
100         .requester = ooh323_request,
101         .send_digit_begin = ooh323_digit_begin,
102         .send_digit_end = ooh323_digit_end,
103         .call = ooh323_call,
104         .hangup = ooh323_hangup,
105         .answer = ooh323_answer,
106         .read = ooh323_read,
107         .write = ooh323_write,
108         .exception = ooh323_read,
109         .indicate = ooh323_indicate,
110         .fixup = ooh323_fixup,
111         .send_html = 0,
112         .queryoption = ooh323_queryoption,
113         .bridge = ast_rtp_instance_bridge,              /* XXX chan unlocked ? */
114         .early_bridge = ast_rtp_instance_early_bridge,
115
116 };
117
118 static struct ast_rtp_glue ooh323_rtp = {
119         .type = type,
120         .get_rtp_info = ooh323_get_rtp_peer,
121         .get_vrtp_info = ooh323_get_vrtp_peer,
122         .update_peer = ooh323_set_rtp_peer,
123 };
124
125 static struct ast_udptl_protocol ooh323_udptl = {
126         type: "H323",
127         get_udptl_info: ooh323_get_udptl_peer,
128         set_udptl_peer: ooh323_set_udptl_peer,
129 };
130
131
132
133 struct ooh323_user;
134
135 /* H.323 channel private structure */
136 static struct ooh323_pvt {
137         ast_mutex_t lock;               /* Channel private lock */
138         struct ast_rtp_instance *rtp;
139         struct ast_rtp_instance *vrtp; /* Placeholder for now */
140
141         int t38support;                 /* T.38 mode - disable, transparent, faxgw */
142         int rtptimeout;
143         struct ast_udptl *udptl;
144         int faxmode;
145         int t38_tx_enable;
146         int t38_init;
147         struct sockaddr_in udptlredirip;
148         time_t lastTxT38;
149         int chmodepend;
150
151         struct ast_channel *owner;      /* Master Channel */
152         union {
153                 char  *user;    /* cooperating user/peer */
154                 char  *peer;
155         } neighbor;
156         time_t lastrtptx;
157         time_t lastrtprx;
158         unsigned int flags;
159         unsigned int call_reference;
160         char *callToken;
161         char *username;
162         char *host;
163         char *callerid_name;
164         char *callerid_num;
165         char caller_h323id[AST_MAX_EXTENSION];
166         char caller_dialedDigits[AST_MAX_EXTENSION];
167         char caller_email[AST_MAX_EXTENSION];
168         char caller_url[256];
169         char callee_h323id[AST_MAX_EXTENSION];
170         char callee_dialedDigits[AST_MAX_EXTENSION];
171         char callee_email[AST_MAX_EXTENSION];
172         char callee_url[AST_MAX_EXTENSION];
173  
174         int port;
175         format_t readformat;   /* negotiated read format */
176         format_t writeformat;  /* negotiated write format */
177         format_t capability;
178         struct ast_codec_pref prefs;
179         int dtmfmode;
180         int dtmfcodec;
181         char exten[AST_MAX_EXTENSION];  /* Requested extension */
182         char context[AST_MAX_EXTENSION];        /* Context where to start */
183         char accountcode[256];  /* Account code */
184         int nat;
185         int amaflags;
186         int progsent;                   /* progress is sent */
187         struct ast_dsp *vad;
188         struct OOH323Regex *rtpmask;    /* rtp ip regexp */
189         char rtpmaskstr[120];
190         struct ooh323_pvt *next;        /* Next entity */
191 } *iflist = NULL;
192
193 /* Protect the channel/interface list (ooh323_pvt) */
194 AST_MUTEX_DEFINE_STATIC(iflock);
195
196 /* Profile of H.323 user registered with PBX*/
197 struct ooh323_user{
198         ast_mutex_t lock;
199         char        name[256];
200         char        context[AST_MAX_EXTENSION];
201         int         incominglimit;
202         unsigned    inUse;
203         char        accountcode[20];
204         int         amaflags;
205         format_t    capability;
206         struct ast_codec_pref prefs;
207         int         dtmfmode;
208         int         dtmfcodec;
209         int         t38support;
210         int         rtptimeout;
211         int         mUseIP;        /* Use IP address or H323-ID to search user */
212         char        mIP[20];
213         struct OOH323Regex          *rtpmask;
214         char        rtpmaskstr[120];
215         struct ooh323_user *next;
216 };
217
218 /* Profile of valid asterisk peers */
219 struct ooh323_peer{
220         ast_mutex_t lock;
221         char        name[256];
222         unsigned    outgoinglimit;
223         unsigned    outUse;
224         format_t    capability;
225         struct ast_codec_pref prefs;
226         char        accountcode[20];
227         int         amaflags;
228         int         dtmfmode;
229         int         dtmfcodec;
230         int         t38support;
231         int         mFriend;    /* indicates defined as friend */
232         char        ip[20];
233         int         port;
234         char        *h323id;    /* H323-ID alias, which asterisk will register with gk to reach this peer*/
235         char        *email;     /* Email alias, which asterisk will register with gk to reach this peer*/
236         char        *url;       /* url alias, which asterisk will register with gk to reach this peer*/
237         char        *e164;      /* e164 alias, which asterisk will register with gk to reach this peer*/
238         int         rtptimeout;
239         struct OOH323Regex          *rtpmask;
240         char        rtpmaskstr[120];
241         struct ooh323_peer *next;
242 };
243
244
245 /* List of H.323 users known to PBX */
246 static struct ast_user_list {
247         struct ooh323_user *users;
248         ast_mutex_t lock;
249 } userl;
250
251 static struct ast_peer_list {
252         struct ooh323_peer *peers;
253         ast_mutex_t lock;
254 } peerl;
255
256 /* Mutex to protect H.323 reload process */
257 static int h323_reloading = 0;
258 AST_MUTEX_DEFINE_STATIC(h323_reload_lock);
259
260 /* Mutex to protect usage counter */
261 static int usecnt = 0;
262 AST_MUTEX_DEFINE_STATIC(usecnt_lock);
263
264 AST_MUTEX_DEFINE_STATIC(ooh323c_cmd_lock);
265
266 static long callnumber = 0;
267 AST_MUTEX_DEFINE_STATIC(ooh323c_cn_lock);
268
269 /* stack callbacks */
270 int onAlerting(ooCallData *call);
271 int onProgress(ooCallData *call);
272 int onNewCallCreated(ooCallData *call);
273 int onOutgoingCall(ooCallData *call);
274 int onCallEstablished(ooCallData *call);
275 int onCallCleared(ooCallData *call);
276 void onModeChanged(ooCallData *call, int t38mode);
277
278 static char gLogFile[256] = DEFAULT_LOGFILE;
279 static int  gPort = 1720;
280 static char gIP[20];
281 static char gCallerID[AST_MAX_EXTENSION] = "";
282 static struct ooAliases *gAliasList;
283 static format_t gCapability = AST_FORMAT_ULAW;
284 static struct ast_codec_pref gPrefs;
285 static int  gDTMFMode = H323_DTMF_RFC2833;
286 static int  gDTMFCodec = 101;
287 static int  gT38Support = T38_FAXGW;
288 static char gGatekeeper[100];
289 static enum RasGatekeeperMode gRasGkMode = RasNoGatekeeper;
290
291 static int  gIsGateway = 0;
292 static int  gFastStart = 1;
293 static int  gTunneling = 1;
294 static int  gBeMaster = 0;
295 static int  gMediaWaitForConnect = 0;
296 static int  gTOS = 0;
297 static int  gRTPTimeout = 60;
298 static char gAccountcode[80] = DEFAULT_H323ACCNT;
299 static int  gAMAFLAGS;
300 static char gContext[AST_MAX_EXTENSION] = DEFAULT_CONTEXT;
301 static int  gIncomingLimit = 1024;
302 static int  gOutgoingLimit = 1024;
303 OOBOOL gH323Debug = FALSE;
304 static int gTRCLVL = OOTRCLVLERR;
305
306 static int t35countrycode = 0;
307 static int t35extensions = 0;
308 static int manufacturer = 0;
309 static char vendor[AST_MAX_EXTENSION] =  "";
310 static char version[AST_MAX_EXTENSION] = "";
311
312 static struct ooh323_config
313 {
314    int  mTCPPortStart;
315    int  mTCPPortEnd;
316 } ooconfig;
317
318 /** Asterisk RTP stuff*/
319 static struct sched_context *sched;
320 static struct io_context *io;
321
322 /* Protect the monitoring thread, so only one process can kill or start it, 
323    and not when it's doing something critical. */
324 AST_MUTEX_DEFINE_STATIC(monlock);
325
326
327 /* This is the thread for the monitor which checks for input on the channels
328    which are not currently in use.  */
329 static pthread_t monitor_thread = AST_PTHREADT_NULL;
330
331
332 static struct ast_channel *ooh323_new(struct ooh323_pvt *i, int state,
333                                              const char *host, int capability, const char *linkedid) 
334 {
335         struct ast_channel *ch = NULL;
336         int fmt = 0;
337         if (gH323Debug)
338                 ast_verbose("---   ooh323_new - %s, %d\n", host, capability);
339
340
341         /* Don't hold a h323 pvt lock while we allocate a channel */
342         ast_mutex_unlock(&i->lock);
343         ch = ast_channel_alloc(1, state, i->callerid_num, i->callerid_name, 
344                                 i->accountcode, i->exten, i->context, linkedid, i->amaflags,
345                                 "OOH323/%s-%ld", host, callnumber);
346         ast_mutex_lock(&ooh323c_cn_lock);
347         callnumber++;
348         ast_mutex_unlock(&ooh323c_cn_lock);
349    
350         ast_mutex_lock(&i->lock);
351
352         if (ch) {
353                 ast_channel_lock(ch);
354                 ch->tech = &ooh323_tech;
355
356                 if (capability)
357                         fmt = ast_best_codec(capability);
358                 if (!fmt) 
359                         fmt = ast_codec_pref_index(&i->prefs, 0);
360
361                 ch->nativeformats = ch->rawwriteformat = ch->rawreadformat = fmt;
362
363                 ast_channel_set_fd(ch, 0, ast_rtp_instance_fd(i->rtp, 0));
364                 ast_channel_set_fd(ch, 1, ast_rtp_instance_fd(i->rtp, 1));
365                 ast_channel_set_fd(ch, 5, ast_udptl_fd(i->udptl));
366
367                 ast_jb_configure(ch, &global_jbconf);
368
369                 if (state == AST_STATE_RING)
370                         ch->rings = 1;
371
372                 ch->adsicpe = AST_ADSI_UNAVAILABLE;
373                 ast_set_write_format(ch, fmt);
374                 ast_set_read_format(ch, fmt);
375                 ch->tech_pvt = i;
376                 i->owner = ch;
377                 ast_module_ref(myself);
378
379                 /* Allocate dsp for in-band DTMF support */
380                 if (i->dtmfmode & H323_DTMF_INBAND) {
381                         i->vad = ast_dsp_new();
382                         ast_dsp_set_features(i->vad, DSP_FEATURE_DIGIT_DETECT);
383                         ast_dsp_set_features(i->vad,
384                                                 DSP_FEATURE_DIGIT_DETECT | DSP_FEATURE_FAX_DETECT);
385                         ast_dsp_set_faxmode(i->vad,
386                                                 DSP_FAXMODE_DETECT_CNG | DSP_FAXMODE_DETECT_CED);
387
388                         if (i->dtmfmode & H323_DTMF_INBANDRELAX)
389                                 ast_dsp_set_digitmode(i->vad, DSP_DIGITMODE_DTMF | DSP_DIGITMODE_RELAXDTMF);
390                 }
391
392                 ast_mutex_lock(&usecnt_lock);
393                 usecnt++;
394                 ast_mutex_unlock(&usecnt_lock);
395
396                 /* Notify the module monitors that use count for resource has changed*/
397                 ast_update_use_count();
398
399                 ast_copy_string(ch->context, i->context, sizeof(ch->context));
400                 ast_copy_string(ch->exten, i->exten, sizeof(ch->exten));
401
402                 ch->priority = 1;
403
404                 if(!ast_test_flag(i, H323_OUTGOING)) {
405                 
406                         if (!ast_strlen_zero(i->caller_h323id)) {
407                                 pbx_builtin_setvar_helper(ch, "_CALLER_H323ID", i->caller_h323id);
408
409                         }
410                         if (!ast_strlen_zero(i->caller_dialedDigits)) {
411                                 pbx_builtin_setvar_helper(ch, "_CALLER_H323DIALEDDIGITS", 
412                                 i->caller_dialedDigits);
413                         }
414                         if (!ast_strlen_zero(i->caller_email)) {
415                                 pbx_builtin_setvar_helper(ch, "_CALLER_H323EMAIL", 
416                                 i->caller_email);
417                         }
418                         if (!ast_strlen_zero(i->caller_url)) {
419                                 pbx_builtin_setvar_helper(ch, "_CALLER_H323URL", i->caller_url);
420                         }
421                 }
422
423                 if (!ast_strlen_zero(i->accountcode))
424                         ast_string_field_set(ch, accountcode, i->accountcode);
425                 
426                 if (i->amaflags)
427                         ch->amaflags = i->amaflags;
428
429                 ast_setstate(ch, state);
430                 if (state != AST_STATE_DOWN) {
431                         if (ast_pbx_start(ch)) {
432                                 ast_log(LOG_WARNING, "Unable to start PBX on %s\n", ch->name);
433                                 ast_channel_unlock(ch);
434                                 ast_hangup(ch);
435                                 ch = NULL;
436                         } 
437                 }
438
439                 manager_event(EVENT_FLAG_SYSTEM, "ChannelUpdate", "Channel: %s\r\nChanneltype: %s\r\n"
440                                 "CallRef: %d\r\n", ch->name, "OOH323", i->call_reference);
441         } else
442                 ast_log(LOG_WARNING, "Unable to allocate channel structure\n");
443
444
445         if(ch)   ast_channel_unlock(ch);
446
447         if (gH323Debug)
448                 ast_verbose("+++   h323_new\n");
449
450         return ch;
451 }
452
453
454
455 static struct ooh323_pvt *ooh323_alloc(int callref, char *callToken) 
456 {
457         struct ooh323_pvt *pvt = NULL;
458         struct sockaddr_in ouraddr;
459         struct in_addr ipAddr;
460         if (gH323Debug)
461                 ast_verbose("---   ooh323_alloc\n");
462
463         if (!(pvt = ast_calloc(1, sizeof(*pvt)))) {
464                 ast_log(LOG_ERROR, "Couldn't allocate private ooh323 structure\n");
465                 return NULL;
466         }
467
468         ast_mutex_init(&pvt->lock);
469         ast_mutex_lock(&pvt->lock);
470
471
472         if (!inet_aton(gIP, &ipAddr)) {
473                 ast_log(LOG_ERROR, "Invalid OOH323 driver ip address\n");
474                 ast_mutex_unlock(&pvt->lock);
475                 ast_mutex_destroy(&pvt->lock);
476                 ast_free(pvt);
477                 return NULL;
478         }
479
480         ouraddr.sin_addr = ipAddr;
481         if (!(pvt->rtp = ast_rtp_instance_new("asterisk", sched, &ouraddr, NULL))) {
482                 ast_log(LOG_WARNING, "Unable to create RTP session: %s\n", 
483                                   strerror(errno));
484                 ast_mutex_unlock(&pvt->lock);
485                 ast_mutex_destroy(&pvt->lock);
486                 ast_free(pvt);
487                 return NULL;
488         }
489  
490         ast_rtp_instance_set_qos(pvt->rtp, gTOS, 0, "ooh323-rtp");
491
492         if (!(pvt->udptl = ast_udptl_new_with_bindaddr(sched, io, 0, ipAddr))) {
493                 ast_log(LOG_WARNING, "Unable to create UDPTL session: %s\n",
494                                 strerror(errno));
495                 ast_mutex_unlock(&pvt->lock);
496                 ast_mutex_destroy(&pvt->lock);
497                 ast_free(pvt);
498                 return NULL;
499         }
500
501         ast_udptl_set_error_correction_scheme(pvt->udptl, UDPTL_ERROR_CORRECTION_NONE);
502         pvt->faxmode = 0;
503         pvt->t38support = gT38Support;
504         pvt->rtptimeout = gRTPTimeout;
505
506         pvt->call_reference = callref;
507         if (callToken)
508                 pvt->callToken = strdup(callToken);
509
510         /* whether to use gk for this call */
511         if (gRasGkMode == RasNoGatekeeper)
512                 OO_SETFLAG(pvt->flags, H323_DISABLEGK);
513
514         pvt->dtmfmode = gDTMFMode;
515         pvt->dtmfcodec = gDTMFCodec;
516         ast_copy_string(pvt->context, gContext, sizeof(pvt->context));
517         ast_copy_string(pvt->accountcode, gAccountcode, sizeof(pvt->accountcode));
518
519         pvt->amaflags = gAMAFLAGS;
520         pvt->capability = gCapability;
521         memcpy(&pvt->prefs, &gPrefs, sizeof(pvt->prefs));
522
523         ast_mutex_unlock(&pvt->lock); 
524         /* Add to interface list */
525         ast_mutex_lock(&iflock);
526         pvt->next = iflist;
527         iflist = pvt;
528         ast_mutex_unlock(&iflock);
529
530         if (gH323Debug)
531                 ast_verbose("+++   ooh323_alloc\n");
532
533         return pvt;
534 }
535
536
537 /*
538         Possible data values - peername, exten/peername, exten@ip
539  */
540 static struct ast_channel *ooh323_request(const char *type, format_t format,
541                 const struct ast_channel *requestor, void *data, int *cause)
542
543 {
544         struct ast_channel *chan = NULL;
545         struct ooh323_pvt *p = NULL;
546         struct ooh323_peer *peer = NULL;
547         char *dest = NULL; 
548         char *ext = NULL;
549         char tmp[256];
550         char formats[FORMAT_STRING_SIZE];
551         int oldformat;
552         int port = 0;
553
554         if (gH323Debug)
555                 ast_verbose("---   ooh323_request - data %s format %s\n", (char*)data,  
556                                                                                 ast_getformatname_multiple(formats,FORMAT_STRING_SIZE,format));
557
558         oldformat = format;
559         format &= AST_FORMAT_AUDIO_MASK;
560         if (!format) {
561                 ast_log(LOG_NOTICE, "Asked to get a channel of unsupported format '%lld'\n", (long long) format);
562                 return NULL;
563         }
564
565         p = ooh323_alloc(0,0); /* Initial callRef is zero */
566
567         if (!p) {
568                 ast_log(LOG_WARNING, "Unable to build pvt data for '%s'\n", (char*)data);
569                 return NULL;
570         }
571         ast_mutex_lock(&p->lock);
572
573         /* This is an outgoing call, since ooh323_request is called */
574         ast_set_flag(p, H323_OUTGOING);
575
576
577         ast_copy_string(tmp, data, sizeof(tmp));
578
579         dest = strchr(tmp, '/');
580
581         if (dest) {  
582                 *dest = '\0';
583                 dest++;
584                 ext = dest;
585                 dest = tmp;
586         } else if ((dest = strchr(tmp, '@'))) {
587                 *dest = '\0';
588                 dest++;
589                 ext = tmp;
590         } else {
591                 dest = tmp;
592                 ext = NULL;
593         }
594
595 #if 0
596         if ((sport = strchr(dest, ':'))) {
597                 *sport = '\0';
598                 sport++;
599                 port = atoi(sport);
600         }
601 #endif
602
603         if (dest) {
604                 peer = find_peer(dest, port);
605         } else{
606                 ast_log(LOG_ERROR, "Destination format is not supported\n");
607                 return NULL;
608         }
609
610         if (peer) {
611                 p->username = strdup(peer->name);
612                 p->host = strdup(peer->ip);
613                 p->port = peer->port;
614                 /* Disable gk as we are going to call a known peer*/
615                 /* OO_SETFLAG(p->flags, H323_DISABLEGK); */
616
617                 if (ext)
618                         ast_copy_string(p->exten, ext, sizeof(p->exten));
619
620                 p->capability = peer->capability;
621                 memcpy(&p->prefs, &peer->prefs, sizeof(struct ast_codec_pref));
622                 p->dtmfmode |= peer->dtmfmode;
623                 p->dtmfcodec  = peer->dtmfcodec;
624                 p->t38support = peer->t38support;
625                 p->rtptimeout = peer->rtptimeout;
626                 if (peer->rtpmask && peer->rtpmaskstr[0]) {
627                         p->rtpmask = peer->rtpmask;
628                         ast_copy_string(p->rtpmaskstr, peer->rtpmaskstr, sizeof(p->rtpmaskstr));
629                 }
630                 ast_copy_string(p->accountcode, peer->accountcode, sizeof(p->accountcode));
631                 p->amaflags = peer->amaflags;
632         } else {
633                 p->dtmfmode = gDTMFMode;
634                 p->dtmfcodec = gDTMFCodec;
635                 p->t38support = gT38Support;
636                 p->rtptimeout = gRTPTimeout;
637                 p->capability = gCapability;
638
639                 memcpy(&p->prefs, &gPrefs, sizeof(struct ast_codec_pref));
640                 p->username = strdup(dest);
641
642                 p->host = strdup(dest);
643                 if (port > 0) {
644                         p->port = port;
645                 }
646                 if (ext) {
647                         ast_copy_string(p->exten, ext, sizeof(p->exten));
648                 }
649         }
650
651
652         chan = ooh323_new(p, AST_STATE_DOWN, p->username, format,
653                                  requestor ? requestor->linkedid : NULL);
654         
655         ast_mutex_unlock(&p->lock);
656
657         if (!chan) {
658                 ast_mutex_lock(&iflock);
659                 ooh323_destroy(p);
660                 ast_mutex_unlock(&iflock);
661    } else {
662       ast_mutex_lock(&p->lock);
663       p->callToken = (char*)ast_malloc(AST_MAX_EXTENSION);
664       if(!p->callToken)
665       {
666        ast_mutex_unlock(&p->lock);
667        ast_mutex_lock(&iflock);
668        ooh323_destroy(p);
669        ast_mutex_unlock(&iflock);
670        ast_log(LOG_ERROR, "Failed to allocate memory for callToken\n");
671        return NULL;
672       }
673
674       ast_mutex_unlock(&p->lock);
675       ast_mutex_lock(&ooh323c_cmd_lock);
676       ooMakeCall(data, p->callToken, AST_MAX_EXTENSION, NULL);
677       ast_mutex_unlock(&ooh323c_cmd_lock);
678         }
679
680         restart_monitor();
681         if (gH323Debug)
682                 ast_verbose("+++   ooh323_request\n");
683
684         return chan;
685
686 }
687
688
689 static struct ooh323_pvt* find_call(ooCallData *call)
690 {
691         struct ooh323_pvt *p;
692
693         if (gH323Debug)
694                 ast_verbose("---   find_call\n");
695
696         ast_mutex_lock(&iflock);
697
698         for (p = iflist; p; p = p->next) {
699                 if (p->callToken && !strcmp(p->callToken, call->callToken)) {
700                         break;
701                 }
702         }
703         ast_mutex_unlock(&iflock);
704
705         if (gH323Debug)
706                 ast_verbose("+++   find_call\n");
707
708         return p;
709 }
710
711 struct ooh323_user *find_user(const char * name, const char* ip)
712 {
713         struct ooh323_user *user;
714
715         if (gH323Debug)
716       ast_verbose("---   find_user: %s, %s\n",name,ip);
717
718         ast_mutex_lock(&userl.lock);
719
720         for (user = userl.users; user; user = user->next) {
721                 if (ip && user->mUseIP && !strcmp(user->mIP, ip)) {
722                         break;
723                 }
724                 if (name && !strcmp(user->name, name)) {
725                         break;
726                 }
727         }
728
729         ast_mutex_unlock(&userl.lock);
730
731         if (gH323Debug)
732                 ast_verbose("+++   find_user\n");
733
734         return user;
735 }
736
737 struct ooh323_peer *find_friend(const char *name, int port)
738 {
739         struct ooh323_peer *peer;  
740
741         if (gH323Debug)
742                 ast_verbose("---   find_friend \"%s\"\n", name);
743
744
745         ast_mutex_lock(&peerl.lock);
746         for (peer = peerl.peers; peer; peer = peer->next) {
747                 if (gH323Debug) {
748                         ast_verbose("           comparing with \"%s\"\n", peer->ip);
749                 }
750                 if (!strcmp(peer->ip, name)) {
751                         if (port <= 0 || (port > 0 && peer->port == port)) {
752                                 break;
753                         }
754                 }
755         }
756         ast_mutex_unlock(&peerl.lock);
757
758         if (gH323Debug) {
759                 if (peer) {
760                         ast_verbose("           found matching friend\n");
761                 }
762                 ast_verbose("+++   find_friend \"%s\"\n", name);
763         }
764
765         return peer;            
766 }
767
768
769 struct ooh323_peer *find_peer(const char * name, int port)
770 {
771         struct ooh323_peer *peer;
772
773         if (gH323Debug)
774                 ast_verbose("---   find_peer \"%s\"\n", name);
775
776
777         ast_mutex_lock(&peerl.lock);
778         for (peer = peerl.peers; peer; peer = peer->next) {
779                 if (gH323Debug) {
780                         ast_verbose("           comparing with \"%s\"\n", peer->ip);
781                 }
782                 if (!strcasecmp(peer->name, name))
783                         break;
784                 if (peer->h323id && !strcasecmp(peer->h323id, name))
785                         break;
786                 if (peer->e164 && !strcasecmp(peer->e164, name))
787                         break;
788                 /*
789                 if (!strcmp(peer->ip, name)) {
790                         if (port > 0 && peer->port == port) { break; }
791                         else if (port <= 0) { break; }
792                 }
793                 */
794         }
795         ast_mutex_unlock(&peerl.lock);
796
797         if (gH323Debug) {
798                 if (peer) {
799                         ast_verbose("           found matching peer\n");
800                 }
801                 ast_verbose("+++   find_peer \"%s\"\n", name);
802         }
803
804         return peer;            
805 }
806
807 static int ooh323_digit_begin(struct ast_channel *chan, char digit)
808 {
809         char dtmf[2];
810         struct ooh323_pvt *p = (struct ooh323_pvt *) chan->tech_pvt;
811         
812         if (gH323Debug)
813                 ast_verbose("---   ooh323_digit_begin\n");
814
815         if (!p) {
816                 ast_log(LOG_ERROR, "No private structure for call\n");
817                 return -1;
818         }
819         ast_mutex_lock(&p->lock);
820
821
822         if (digit == 'e' && !p->faxmode && p->t38support != T38_DISABLED)  {
823                 if (!p->chmodepend) {
824                         if (gH323Debug)
825                                 ast_verbose("request to change %s to t.38 because fax cng\n",
826                                                 p->callToken);
827                         p->chmodepend = 1;
828                         ooRequestChangeMode(p->callToken, 1);
829                 }
830
831         } else if (p->rtp && ((p->dtmfmode & H323_DTMF_RFC2833) || (p->dtmfmode & H323_DTMF_CISCO))) {
832                 ast_rtp_instance_dtmf_begin(p->rtp, digit);
833         } else if (((p->dtmfmode & H323_DTMF_Q931) ||
834                                                  (p->dtmfmode & H323_DTMF_H245ALPHANUMERIC) ||
835                                                  (p->dtmfmode & H323_DTMF_H245SIGNAL))) {
836                 dtmf[0] = digit;
837                 dtmf[1] = '\0';
838                 ooSendDTMFDigit(p->callToken, dtmf);
839         }
840         ast_mutex_unlock(&p->lock);
841         if (gH323Debug)
842                 ast_verbose("+++   ooh323_digit_begin\n");
843
844         return 0;
845 }
846
847 static int ooh323_digit_end(struct ast_channel *chan, char digit, unsigned int duration)
848 {
849         struct ooh323_pvt *p = (struct ooh323_pvt *) chan->tech_pvt;
850
851         if (gH323Debug)
852                 ast_verbose("---   ooh323_digit_end\n");
853
854         if (!p) {
855                 ast_log(LOG_ERROR, "No private structure for call\n");
856                 return -1;
857         }
858         ast_mutex_lock(&p->lock);
859         if (p->rtp && ((p->dtmfmode & H323_DTMF_RFC2833) || (p->dtmfmode & H323_DTMF_CISCO)) ) 
860                 ast_rtp_instance_dtmf_end(p->rtp, digit);
861
862         ast_mutex_unlock(&p->lock);
863         if (gH323Debug)
864                 ast_verbose("+++   ooh323_digit_end\n");
865
866         return 0;
867 }
868
869
870 static int ooh323_call(struct ast_channel *ast, char *dest, int timeout)
871 {
872         struct ooh323_pvt *p = ast->tech_pvt;
873         char destination[256];
874         int res=0, i;
875         const char *val = NULL;
876         ooCallOptions opts = {
877                 .fastStart = TRUE,
878                 .tunneling = TRUE,
879                 .disableGk = TRUE,
880                 .callMode = OO_CALLMODE_AUDIOCALL,
881                 .transfercap = 0
882         };
883
884         if (gH323Debug)
885                 ast_verbose("---   ooh323_call- %s\n", dest);
886
887
888    if ((ast->_state != AST_STATE_DOWN) && (ast->_state != AST_STATE_RESERVED)) 
889    {
890                 ast_log(LOG_WARNING, "ooh323_call called on %s, neither down nor "
891                                                                         "reserved\n", ast->name);
892                 return -1;
893         }
894         ast_mutex_lock(&p->lock);
895         ast_set_flag(p, H323_OUTGOING);
896         if (ast->connected.id.number) {
897                 if(p->callerid_num)   free(p->callerid_num);
898                 p->callerid_num = strdup(ast->connected.id.number);
899         }
900
901         if (ast->connected.id.name) {
902                 if(p->callerid_name)
903                         free(p->callerid_name);
904                 p->callerid_name = strdup(ast->connected.id.name);
905         } else if (ast->connected.id.number) {
906                 if(p->callerid_name)
907                         free(p->callerid_name);
908                 p->callerid_name = strdup(ast->connected.id.number);
909         } else {
910                 ast->connected.id.name = strdup(gCallerID);
911                 if(p->callerid_name)
912                         free(p->callerid_name);
913                 p->callerid_name = strdup(ast->connected.id.name);
914         }
915
916         /* Retrieve vars */
917
918
919         if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323ID"))) {
920                 ast_copy_string(p->caller_h323id, val, sizeof(p->caller_h323id));
921         }
922         
923         if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323DIALEDDIGITS"))) {
924                 ast_copy_string(p->caller_dialedDigits, val, sizeof(p->caller_dialedDigits));
925                 if(!p->callerid_num)
926                         p->callerid_num = strdup(val);
927         }
928
929         if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323EMAIL"))) {
930                 ast_copy_string(p->caller_email, val, sizeof(p->caller_email));
931         }
932
933         if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323URL"))) {
934                 ast_copy_string(p->caller_url, val, sizeof(p->caller_url));
935         }
936
937         if (p->host && p->port != 0)
938                 snprintf(destination, sizeof(destination), "%s:%d", p->host, p->port);
939         else if (p->host)
940                 snprintf(destination, sizeof(destination), "%s", p->host);
941         else
942                 ast_copy_string(destination, dest, sizeof(destination));
943
944    destination[sizeof(destination)-1]='\0';
945
946    opts.transfercap = ast->transfercapability;
947
948    for (i=0;i<480 && !isRunning(p->callToken);i++) usleep(12000);
949
950    if(OO_TESTFLAG(p->flags, H323_DISABLEGK)) {
951       res = ooRunCall(destination, p->callToken, AST_MAX_EXTENSION, &opts);
952    } else {
953       res = ooRunCall(destination, p->callToken, AST_MAX_EXTENSION, NULL);
954    }
955
956         ast_mutex_unlock(&p->lock);
957         if (res != OO_OK) {
958                 ast_log(LOG_ERROR, "Failed to make call\n");
959                 return -1; /* ToDO: cleanup */
960         }
961         if (gH323Debug)
962                 ast_verbose("+++   ooh323_call\n");
963
964   return 0;
965 }
966
967 static int ooh323_hangup(struct ast_channel *ast)
968 {
969         struct ooh323_pvt *p = ast->tech_pvt;
970         int q931cause = AST_CAUSE_NORMAL_CLEARING;
971
972         if (gH323Debug)
973                 ast_verbose("---   ooh323_hangup\n");
974
975         if (p) {
976                 ast_mutex_lock(&p->lock);
977
978         if (ast->hangupcause) {
979                 q931cause = ast->hangupcause;
980         } else {
981                 const char *cause = pbx_builtin_getvar_helper(ast, "DIALSTATUS");
982                 if (cause) {
983                         if (!strcmp(cause, "CONGESTION")) {
984                                 q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
985                         } else if (!strcmp(cause, "BUSY")) {
986                                 q931cause = AST_CAUSE_USER_BUSY;
987                         } else if (!strcmp(cause, "CHANISUNVAIL")) {
988                                 q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
989                         } else if (!strcmp(cause, "NOANSWER")) {
990                                 q931cause = AST_CAUSE_NO_ANSWER;
991                         } else if (!strcmp(cause, "CANCEL")) {
992                                 q931cause = AST_CAUSE_CALL_REJECTED;
993                         }
994                 }
995         }
996
997
998
999                 if (gH323Debug)
1000                         ast_verbose("    hanging %s with cause: %d\n", p->username, q931cause);
1001                 ast->tech_pvt = NULL; 
1002                 if (!ast_test_flag(p, H323_ALREADYGONE)) {
1003                         ooHangCall(p->callToken, 
1004                                 ooh323_convert_hangupcause_asteriskToH323(q931cause), q931cause);
1005                         ast_set_flag(p, H323_ALREADYGONE);
1006                         /* ast_mutex_unlock(&p->lock); */
1007                 } else 
1008                         ast_set_flag(p, H323_NEEDDESTROY);
1009                 /* detach channel here */
1010                 if (p->owner) {
1011                         p->owner->tech_pvt = NULL;
1012                         p->owner = NULL;
1013                         ast_module_unref(myself);
1014                 }
1015
1016                 ast_mutex_unlock(&p->lock);
1017                 ast_mutex_lock(&usecnt_lock);
1018                 usecnt--;
1019                 ast_mutex_unlock(&usecnt_lock);
1020
1021                 /* Notify the module monitors that use count for resource has changed */
1022                 ast_update_use_count();
1023           
1024         } else {
1025                 ast_debug(1, "No call to hangup\n" );
1026         }
1027         
1028         if (gH323Debug)
1029                 ast_verbose("+++   ooh323_hangup\n");
1030
1031   return 0;
1032 }
1033
1034 static int ooh323_answer(struct ast_channel *ast)
1035 {
1036         struct ooh323_pvt *p = ast->tech_pvt;
1037
1038         if (gH323Debug)
1039                 ast_verbose("--- ooh323_answer\n");
1040
1041         if (p) {
1042
1043                 ast_mutex_lock(&p->lock);
1044                 if (ast->_state != AST_STATE_UP) {
1045                         ast_channel_lock(ast);
1046                         ast_setstate(ast, AST_STATE_UP);
1047                         if (option_debug)
1048                                 ast_debug(1, "ooh323_answer(%s)\n", ast->name);
1049                         ast_channel_unlock(ast);
1050                         ooAnswerCall(p->callToken);
1051                 }
1052                 ast_mutex_unlock(&p->lock);
1053         }
1054
1055         if (gH323Debug)
1056                 ast_verbose("+++ ooh323_answer\n");
1057
1058   return 0;
1059 }
1060
1061 static struct ast_frame *ooh323_read(struct ast_channel *ast)
1062 {
1063         struct ast_frame *fr;
1064         static struct ast_frame null_frame = { AST_FRAME_NULL, };
1065         struct ooh323_pvt *p = ast->tech_pvt;
1066
1067         if (!p) return &null_frame;
1068
1069         ast_mutex_lock(&p->lock);
1070         if (p->rtp)
1071                 fr = ooh323_rtp_read(ast, p);
1072         else
1073                 fr = &null_frame;
1074         /* time(&p->lastrtprx); */
1075         ast_mutex_unlock(&p->lock);
1076         return fr;
1077 }
1078
1079 static int ooh323_write(struct ast_channel *ast, struct ast_frame *f)
1080 {
1081         struct ooh323_pvt *p = ast->tech_pvt;
1082         int res = 0;
1083         char buf[256];
1084
1085         if (p) {
1086                 ast_mutex_lock(&p->lock);
1087
1088                 if (f->frametype == AST_FRAME_MODEM) {
1089                         ast_debug(1, "Send UDPTL %d/%d len %d for %s\n",
1090                                 f->frametype, f->subclass.integer, f->datalen, ast->name);
1091                         if (p->udptl)
1092                                 res = ast_udptl_write(p->udptl, f);
1093                         ast_mutex_unlock(&p->lock);
1094                         return res;
1095                 }
1096
1097         
1098                 if (f->frametype == AST_FRAME_VOICE) {
1099 /* sending progress for first */
1100                         if (!ast_test_flag(p, H323_OUTGOING) && !p->progsent &&
1101                                         p->callToken) {
1102                                 ooManualProgress(p->callToken);
1103                                 p->progsent = 1;
1104                         }
1105
1106
1107                         if (!(f->subclass.codec & ast->nativeformats)) {
1108                                 if (ast->nativeformats != 0) {
1109                                         ast_log(LOG_WARNING,
1110                                                         "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
1111                                                         ast_getformatname(f->subclass.codec),
1112                                                         ast_getformatname_multiple(buf, sizeof(buf), ast->nativeformats),
1113                                                         ast_getformatname(ast->readformat),
1114                                                         ast_getformatname(ast->writeformat));
1115
1116                                         ast_set_write_format(ast, f->subclass.codec);
1117                                 } else {
1118                                         /* ast_set_write_format(ast, f->subclass);
1119                                         ast->nativeformats = f->subclass; */
1120                                 }
1121                         ast_mutex_unlock(&p->lock);
1122                         return 0;
1123                         }
1124
1125                 if (p->rtp)
1126                         res = ast_rtp_instance_write(p->rtp, f);
1127
1128                 ast_mutex_unlock(&p->lock);
1129
1130                 } else if (f->frametype == AST_FRAME_IMAGE) {
1131                         ast_mutex_unlock(&p->lock);
1132                         return 0;
1133                 } else {
1134                         ast_log(LOG_WARNING, "Can't send %d type frames with OOH323 write\n", 
1135                                                                          f->frametype);
1136                         ast_mutex_unlock(&p->lock);
1137                         return 0;
1138                 }
1139
1140         }
1141
1142         return res;
1143 }
1144
1145 static int ooh323_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
1146 {
1147
1148         struct ooh323_pvt *p = (struct ooh323_pvt *) ast->tech_pvt;
1149         char *callToken = (char *)NULL;
1150
1151         if (!p) return -1;
1152
1153         ast_mutex_lock(&p->lock);
1154         callToken = (p->callToken ? strdup(p->callToken) : NULL);
1155         ast_mutex_unlock(&p->lock);
1156
1157         if (!callToken) {
1158                 if (gH323Debug)
1159                         ast_verbose("   ooh323_indicate - No callToken\n");
1160                 return -1;
1161         }
1162
1163         if (gH323Debug)
1164                 ast_verbose("----- ooh323_indicate %d on call %s\n", condition, callToken);
1165          
1166         ast_mutex_lock(&p->lock);
1167         switch (condition) {
1168         case AST_CONTROL_CONGESTION:
1169                 if (!ast_test_flag(p, H323_ALREADYGONE)) {
1170                         ooHangCall(callToken, OO_REASON_LOCAL_CONGESTED, 
1171                                                 AST_CAUSE_SWITCH_CONGESTION);
1172                         ast_set_flag(p, H323_ALREADYGONE);
1173                 }
1174                 break;
1175         case AST_CONTROL_BUSY:
1176                 if (!ast_test_flag(p, H323_ALREADYGONE)) {
1177                         ooHangCall(callToken, OO_REASON_LOCAL_BUSY, AST_CAUSE_USER_BUSY);
1178                         ast_set_flag(p, H323_ALREADYGONE);
1179                 }
1180                 break;
1181         case AST_CONTROL_HOLD:
1182                 ast_moh_start(ast, data, NULL);         
1183                 break;
1184         case AST_CONTROL_UNHOLD:
1185                 ast_moh_stop(ast);
1186                 break;
1187         case AST_CONTROL_PROGRESS:
1188                 if (ast->_state != AST_STATE_UP) {
1189                         if (!p->progsent) {
1190                                 if (gH323Debug) 
1191                                         ast_log(LOG_DEBUG,"Sending manual progress for %s, res = %d\n", callToken,
1192                                         ooManualProgress(callToken));   
1193                                 else
1194                                         ooManualProgress(callToken);
1195                                 p->progsent = 1;
1196                         }
1197                 }
1198             break;
1199       case AST_CONTROL_RINGING:
1200             if (ast->_state == AST_STATE_RING || ast->_state == AST_STATE_RINGING) {
1201                 if (gH323Debug) 
1202                         ast_log(LOG_DEBUG,"Sending manual ringback for %s, res = %d\n", 
1203                         callToken,
1204                         ooManualRingback(callToken));
1205                  else
1206                         ooManualRingback(callToken);
1207             }
1208          break;
1209       case AST_CONTROL_SRCUPDATE:
1210                 ast_rtp_instance_new_source(p->rtp);
1211                 break;
1212
1213       case AST_CONTROL_CONNECTED_LINE:
1214                 if (gH323Debug)
1215                         ast_log(LOG_DEBUG, "Sending connected line info for %s (%s)\n",
1216                                 callToken, ast->connected.id.name);
1217                 ooSetANI(callToken, ast->connected.id.name);
1218                 break;
1219
1220       case AST_CONTROL_T38_PARAMETERS:
1221                 if (p->t38support != T38_ENABLED) {
1222                         struct ast_control_t38_parameters parameters = { .request_response = 0 };
1223                         parameters.request_response = AST_T38_REFUSED;
1224                         ast_queue_control_data(ast, AST_CONTROL_T38_PARAMETERS,
1225                                                  &parameters, sizeof(parameters));
1226                         break;
1227                 }
1228                 if (datalen != sizeof(struct ast_control_t38_parameters)) {
1229                         ast_log(LOG_ERROR, "Invalid datalen for AST_CONTROL_T38. "
1230                                            "Expected %d, got %d\n",
1231                                 (int)sizeof(enum ast_control_t38), (int)datalen);
1232                 } else {
1233                         const struct ast_control_t38_parameters *parameters = data;
1234                         enum ast_control_t38 message = parameters->request_response;
1235                         switch (message) {
1236
1237                         case AST_T38_REQUEST_NEGOTIATE:
1238
1239                                 if (!p->chmodepend && !p->faxmode) {
1240                                         ooRequestChangeMode(p->callToken, 1);
1241                                         p->chmodepend = 1;
1242                                 }
1243                                 break;
1244
1245                         case AST_T38_REQUEST_TERMINATE:
1246
1247                                 if (!p->chmodepend && p->faxmode) {
1248                                         ooRequestChangeMode(p->callToken, 0);
1249                                         p->chmodepend = 1;
1250                                 }
1251                                 break;
1252
1253
1254                         default:
1255                                 ;
1256
1257                         }
1258
1259                 }
1260                 break;
1261       case AST_CONTROL_PROCEEDING:
1262         case -1:
1263                 break;
1264         default:
1265                 ast_log(LOG_WARNING, "Don't know how to indicate condition %d on %s\n",
1266                                                                         condition, callToken);
1267         }
1268
1269         ast_mutex_unlock(&p->lock);
1270
1271         if (gH323Debug)
1272                 ast_verbose("++++  ooh323_indicate %d on %s\n", condition, callToken);
1273
1274         free(callToken);
1275         return -1;
1276 }
1277
1278 static int ooh323_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
1279 {
1280
1281         struct ooh323_pvt *p = (struct ooh323_pvt *) ast->tech_pvt;
1282         int res = -1;
1283         enum ast_t38_state state = T38_STATE_UNAVAILABLE;
1284         char* cp;
1285
1286         if (!p) return -1;
1287
1288         ast_mutex_lock(&p->lock);
1289
1290         if (gH323Debug)
1291                 ast_verbose("----- ooh323_queryoption %d on channel %s\n", option, ast->name);
1292          
1293         switch (option) {
1294
1295                 case AST_OPTION_T38_STATE:
1296
1297                         if (*datalen != sizeof(enum ast_t38_state)) {
1298                                 ast_log(LOG_ERROR, "Invalid datalen for AST_OPTION_T38_STATE option."
1299                                 " Expected %d, got %d\n", (int)sizeof(enum ast_t38_state), *datalen);
1300                                 break;
1301                         }
1302                         if (p->t38support != T38_DISABLED)
1303                                 state = T38_STATE_UNKNOWN;
1304                         if (p->faxmode)
1305                                 state = (p->chmodepend) ? T38_STATE_UNKNOWN : T38_STATE_NEGOTIATED;
1306                         else if (p->chmodepend)
1307                                 state = T38_STATE_NEGOTIATING;
1308
1309
1310                         *((enum ast_t38_state *) data) = state;
1311                         res = 0;
1312                         break;
1313
1314
1315                 case AST_OPTION_DIGIT_DETECT:
1316
1317                         cp = (char *) data;
1318                         *cp = p->vad ? 1 : 0;
1319                         ast_debug(1, "Reporting digit detection %sabled on %s\n",
1320                                                          *cp ? "en" : "dis", ast->name);
1321
1322                         res = 0;
1323                         break;
1324
1325                 default:        ;
1326
1327         }
1328
1329         if (gH323Debug)
1330                 ast_verbose("+++++ ooh323_queryoption %d on channel %s\n", option, ast->name);
1331          
1332         ast_mutex_unlock(&p->lock);
1333
1334         return res;
1335 }
1336
1337
1338
1339 static int ooh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1340 {
1341         struct ooh323_pvt *p = newchan->tech_pvt;
1342
1343         if (!p) return -1;
1344
1345         if (gH323Debug)
1346                 ast_verbose("--- ooh323c ooh323_fixup\n");
1347
1348         ast_mutex_lock(&p->lock);
1349         if (p->owner != oldchan) {
1350                 ast_log(LOG_WARNING, "Old channel wasn't %p but was %p\n", oldchan, p->owner);
1351                 ast_mutex_unlock(&p->lock);
1352                 return -1;
1353         }
1354
1355         if (p->owner == oldchan) {
1356                 p->owner = newchan;
1357         } else {
1358                 p->owner = oldchan;
1359         }
1360
1361         ast_mutex_unlock(&p->lock);
1362
1363         if (gH323Debug)
1364                 ast_verbose("+++ ooh323c ooh323_fixup \n");
1365
1366         return 0;
1367 }
1368
1369
1370 void ooh323_set_write_format(ooCallData *call, int fmt, int txframes)
1371 {
1372         struct ooh323_pvt *p = NULL;
1373         char formats[FORMAT_STRING_SIZE];
1374
1375         if (gH323Debug)
1376                 ast_verbose("---   ooh323_update_writeformat %s/%d\n", 
1377                                 ast_getformatname_multiple(formats,FORMAT_STRING_SIZE, fmt), txframes);
1378         
1379         p = find_call(call);
1380         if (!p) {
1381                 ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
1382                 return;
1383         }
1384
1385         ast_mutex_lock(&p->lock);
1386
1387         p->writeformat = fmt;
1388
1389         if (p->owner) {
1390                 while (p->owner && ast_channel_trylock(p->owner)) {
1391                         ast_debug(1,"Failed to grab lock, trying again\n");
1392                         DEADLOCK_AVOIDANCE(&p->lock);
1393                 }
1394                 if (!p->owner) {
1395                         ast_mutex_unlock(&p->lock);
1396                         ast_log(LOG_ERROR, "Channel has no owner\n");
1397                         return;
1398                 }
1399                 if (gH323Debug)
1400                         ast_verbose("Writeformat before update %s/%s\n", 
1401                           ast_getformatname_multiple(formats,FORMAT_STRING_SIZE, p->owner->writeformat),
1402                           ast_getformatname_multiple(formats,FORMAT_STRING_SIZE, p->owner->nativeformats));
1403                 if (txframes)
1404                         ast_codec_pref_setsize(&p->prefs, fmt, txframes);
1405                 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &p->prefs);
1406                 if (p->dtmfmode & H323_DTMF_RFC2833 && p->dtmfcodec) {
1407                         ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
1408                                  p->rtp, p->dtmfcodec, "audio", "telephone-event", 0);
1409                 }
1410                 if (p->dtmfmode & H323_DTMF_CISCO && p->dtmfcodec) {
1411                         ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
1412                                  p->rtp, p->dtmfcodec, "audio", "cisco-telephone-event", 0);
1413                 }
1414
1415                 p->owner->nativeformats = fmt;
1416                 ast_set_write_format(p->owner, p->owner->writeformat);
1417                 ast_set_read_format(p->owner, p->owner->readformat);
1418                 ast_channel_unlock(p->owner);
1419         } else
1420                 ast_log(LOG_ERROR, "No owner found\n");
1421
1422
1423         ast_mutex_unlock(&p->lock);
1424
1425         if (gH323Debug)
1426                 ast_verbose("+++   ooh323_update_writeformat\n");
1427 }
1428
1429 void ooh323_set_read_format(ooCallData *call, int fmt)
1430 {
1431         struct ooh323_pvt *p = NULL;
1432         char formats[FORMAT_STRING_SIZE];
1433
1434         if (gH323Debug)
1435                 ast_verbose("---   ooh323_update_readformat %s\n", 
1436                                 ast_getformatname_multiple(formats,FORMAT_STRING_SIZE, fmt));
1437         
1438         p = find_call(call);
1439         if (!p) {
1440                 ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
1441                 return;
1442         }
1443
1444         ast_mutex_lock(&p->lock);
1445
1446         p->readformat = fmt;
1447
1448
1449         if (p->owner) {
1450                 while (p->owner && ast_channel_trylock(p->owner)) {
1451                         ast_debug(1,"Failed to grab lock, trying again\n");
1452                         DEADLOCK_AVOIDANCE(&p->lock);
1453                 }
1454                 if (!p->owner) {
1455                         ast_mutex_unlock(&p->lock);
1456                         ast_log(LOG_ERROR, "Channel has no owner\n");
1457                         return;
1458                 }
1459
1460                 if (gH323Debug)
1461                         ast_verbose("Readformat before update %s\n", 
1462                                   ast_getformatname_multiple(formats,FORMAT_STRING_SIZE, p->owner->readformat));
1463                 p->owner->nativeformats = fmt;
1464                 ast_set_read_format(p->owner, p->owner->readformat);
1465                 ast_channel_unlock(p->owner);
1466         } else
1467                 ast_log(LOG_ERROR, "No owner found\n");
1468
1469         ast_mutex_unlock(&p->lock);
1470
1471         if (gH323Debug)
1472                 ast_verbose("+++   ooh323_update_readformat\n");
1473 }
1474
1475
1476 int onAlerting(ooCallData *call)
1477 {
1478         struct ooh323_pvt *p = NULL;
1479         struct ast_channel *c = NULL;
1480
1481         if (gH323Debug)
1482                 ast_verbose("--- onAlerting %s\n", call->callToken);
1483
1484         p = find_call(call);
1485
1486         if(!p) {
1487                 ast_log(LOG_ERROR, "No matching call found\n");
1488                 return -1;
1489         }  
1490         ast_mutex_lock(&p->lock);
1491         if (!p->owner) {
1492                 ast_mutex_unlock(&p->lock);
1493                 ast_log(LOG_ERROR, "Channel has no owner\n");
1494                 return 0;
1495         }
1496         while (p->owner && ast_channel_trylock(p->owner)) {
1497                 ast_debug(1,"Failed to grab lock, trying again\n");
1498                 DEADLOCK_AVOIDANCE(&p->lock);
1499         }
1500         if (!p->owner) {
1501                 ast_mutex_unlock(&p->lock);
1502                 ast_log(LOG_ERROR, "Channel has no owner\n");
1503                 return 0;
1504         }
1505         c = p->owner;
1506
1507         if (call->remoteDisplayName) {
1508                 struct ast_party_connected_line connected;
1509                 ast_party_connected_line_init(&connected);
1510                 connected.id.name = (char *) call->remoteDisplayName;
1511                 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
1512                 ast_channel_queue_connected_line_update(c, &connected);
1513         }
1514         if (c->_state != AST_STATE_UP)
1515                 ast_setstate(c, AST_STATE_RINGING);
1516
1517         ast_queue_control(c, AST_CONTROL_RINGING);
1518         ast_channel_unlock(c);
1519         ast_mutex_unlock(&p->lock);
1520
1521         if (gH323Debug)
1522                 ast_verbose("+++ onAlerting %s\n", call->callToken);
1523
1524         return OO_OK;
1525 }
1526
1527 int onProgress(ooCallData *call)
1528 {
1529         struct ooh323_pvt *p = NULL;
1530         struct ast_channel *c = NULL;
1531
1532         if (gH323Debug)
1533                 ast_verbose("--- onProgress %s\n", call->callToken);
1534
1535         p = find_call(call);
1536
1537         if(!p) {
1538                 ast_log(LOG_ERROR, "No matching call found\n");
1539                 return -1;
1540         }  
1541         ast_mutex_lock(&p->lock);
1542         if (!p->owner) {
1543                 ast_mutex_unlock(&p->lock);
1544                 ast_log(LOG_ERROR, "Channel has no owner\n");
1545                 return 0;
1546         }
1547         while (p->owner && ast_channel_trylock(p->owner)) {
1548                 ast_debug(1,"Failed to grab lock, trying again\n");
1549                 DEADLOCK_AVOIDANCE(&p->lock);
1550         }
1551         if (!p->owner) {
1552                 ast_mutex_unlock(&p->lock);
1553                 ast_log(LOG_ERROR, "Channel has no owner\n");
1554                 return 0;
1555         }
1556         c = p->owner;
1557
1558         if (call->remoteDisplayName) {
1559                 struct ast_party_connected_line connected;
1560                 ast_party_connected_line_init(&connected);
1561                 connected.id.name = (char *) call->remoteDisplayName;
1562                 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
1563                 ast_channel_queue_connected_line_update(c, &connected);
1564         }
1565         if (c->_state != AST_STATE_UP)
1566                 ast_setstate(c, AST_STATE_RINGING);
1567
1568         ast_queue_control(c, AST_CONTROL_PROGRESS);
1569         ast_channel_unlock(c);
1570         ast_mutex_unlock(&p->lock);
1571
1572         if (gH323Debug)
1573                 ast_verbose("+++ onProgress %s\n", call->callToken);
1574
1575         return OO_OK;
1576 }
1577
1578 /**
1579   * Callback for sending digits from H.323 up to asterisk
1580   *
1581   */
1582 int ooh323_onReceivedDigit(OOH323CallData *call, const char *digit)
1583 {
1584         struct ooh323_pvt *p = NULL;
1585         struct ast_frame f;
1586         int res;
1587
1588         ast_debug(1, "Received Digit: %c\n", digit[0]);
1589         p = find_call(call);
1590         if (!p) {
1591                 ast_log(LOG_ERROR, "Failed to find a matching call.\n");
1592                 return -1;
1593         }
1594         if (!p->owner) {
1595                 ast_log(LOG_ERROR, "Channel has no owner\n");
1596                 return -1;
1597         }
1598         ast_mutex_lock(&p->lock);
1599         memset(&f, 0, sizeof(f));
1600         f.frametype = AST_FRAME_DTMF;
1601         f.subclass.integer = digit[0];
1602         f.datalen = 0;
1603         f.samples = 800;
1604         f.offset = 0;
1605         f.data.ptr = NULL;
1606         f.mallocd = 0;
1607         f.src = "SEND_DIGIT";
1608
1609         while (p->owner && ast_channel_trylock(p->owner)) {
1610                 ast_debug(1,"Failed to grab lock, trying again\n");
1611                 DEADLOCK_AVOIDANCE(&p->lock);
1612         }
1613         if (!p->owner) {
1614                 ast_mutex_unlock(&p->lock);
1615                 ast_log(LOG_ERROR, "Channel has no owner\n");
1616                 return 0;
1617         }
1618         res = ast_queue_frame(p->owner, &f);
1619         ast_channel_unlock(p->owner);
1620         ast_mutex_unlock(&p->lock);
1621         return res;
1622 }
1623
1624 int ooh323_onReceivedSetup(ooCallData *call, Q931Message *pmsg)
1625 {
1626         struct ooh323_pvt *p = NULL;
1627         struct ooh323_user *user = NULL;
1628         struct ast_channel *c = NULL;
1629         ooAliases *alias = NULL;
1630         char *at = NULL;
1631         char number [OO_MAX_NUMBER_LENGTH];
1632
1633         if (gH323Debug)
1634                 ast_verbose("---   ooh323_onReceivedSetup %s\n", call->callToken);
1635
1636
1637         if (!(p = ooh323_alloc(call->callReference, call->callToken))) {
1638                 ast_log(LOG_ERROR, "Failed to create a new call.\n");
1639                 return -1;
1640         }
1641         ast_mutex_lock(&p->lock);
1642         ast_clear_flag(p, H323_OUTGOING);
1643   
1644
1645         if (call->remoteDisplayName) {
1646                 p->callerid_name = strdup(call->remoteDisplayName);
1647         }
1648
1649         if (ooCallGetCallingPartyNumber(call, number, OO_MAX_NUMBER_LENGTH) == OO_OK) {
1650                 p->callerid_num = strdup(number);
1651         }
1652
1653         if (call->remoteAliases) {
1654                 for (alias = call->remoteAliases; alias; alias = alias->next) {
1655                         if (alias->type == T_H225AliasAddress_h323_ID) {
1656                                 if (!p->callerid_name) {
1657                                         p->callerid_name = strdup(alias->value);
1658                                 }
1659                                 ast_copy_string(p->caller_h323id, alias->value, sizeof(p->caller_h323id));
1660                                 }
1661          else if(alias->type == T_H225AliasAddress_dialedDigits)
1662          {
1663             if(!p->callerid_num)
1664                p->callerid_num = strdup(alias->value);
1665                                 ast_copy_string(p->caller_dialedDigits, alias->value, 
1666                                                                                                                         sizeof(p->caller_dialedDigits));
1667          }
1668          else if(alias->type == T_H225AliasAddress_email_ID)
1669          {
1670                                 ast_copy_string(p->caller_email, alias->value, sizeof(p->caller_email));
1671          }
1672          else if(alias->type == T_H225AliasAddress_url_ID)
1673          {
1674                                 ast_copy_string(p->caller_url, alias->value, sizeof(p->caller_url));
1675                         }
1676                 }
1677         }
1678
1679         number[0] = '\0';
1680         if(ooCallGetCalledPartyNumber(call, number, OO_MAX_NUMBER_LENGTH)== OO_OK) {
1681                 strncpy(p->exten, number, sizeof(p->exten)-1);
1682         } else {
1683                 update_our_aliases(call, p);
1684                 if (!ast_strlen_zero(p->callee_dialedDigits)) {
1685                         ast_copy_string(p->exten, p->callee_dialedDigits, sizeof(p->exten));
1686                 } else if(!ast_strlen_zero(p->callee_h323id)) {
1687                         ast_copy_string(p->exten, p->callee_h323id, sizeof(p->exten));
1688                 } else if(!ast_strlen_zero(p->callee_email)) {
1689                         ast_copy_string(p->exten, p->callee_email, sizeof(p->exten));
1690                         if ((at = strchr(p->exten, '@'))) {
1691                                 *at = '\0';
1692                         }
1693                 }
1694         }
1695
1696         /* if no extension found, set to default 's' */
1697         if (ast_strlen_zero(p->exten)) {
1698                 p->exten[0]='s';
1699                 p->exten[1]='\0';
1700         }
1701
1702         user = find_user(p->callerid_name, call->remoteIP);
1703         if(user && (user->incominglimit == 0 || user->inUse < user->incominglimit)) {
1704                 ast_mutex_lock(&user->lock);
1705                 p->username = strdup(user->name);
1706                 p->neighbor.user = user->mUseIP ? ast_strdup(user->mIP) :
1707                                                   ast_strdup(user->name);
1708                 ast_copy_string(p->context, user->context, sizeof(p->context));
1709                 ast_copy_string(p->accountcode, user->accountcode, sizeof(p->accountcode));
1710                 p->amaflags = user->amaflags;
1711                 p->capability = user->capability;
1712                 memcpy(&p->prefs, &user->prefs, sizeof(struct ast_codec_pref));
1713                 p->dtmfmode |= user->dtmfmode;
1714                 p->dtmfcodec = user->dtmfcodec;
1715                 p->t38support = user->t38support;
1716                 p->rtptimeout = user->rtptimeout;
1717                 if (user->rtpmask && user->rtpmaskstr[0]) {
1718                         p->rtpmask = user->rtpmask;
1719                         ast_copy_string(p->rtpmaskstr, user->rtpmaskstr, 
1720                                                          sizeof(p->rtpmaskstr));
1721                 }
1722                 if (user->incominglimit) user->inUse++;
1723                 ast_mutex_unlock(&user->lock);
1724         } else {
1725          if (!OO_TESTFLAG(p->flags,H323_DISABLEGK)) {
1726                 p->username = strdup(call->remoteIP);
1727         } else {
1728           ast_mutex_unlock(&p->lock);
1729           ast_log(LOG_ERROR, "Unacceptable ip %s\n", call->remoteIP);
1730           if (!user) 
1731            ooHangCall(call->callToken, ooh323_convert_hangupcause_asteriskToH323(AST_CAUSE_CALL_REJECTED), AST_CAUSE_CALL_REJECTED);
1732           else
1733            ooHangCall(call->callToken, ooh323_convert_hangupcause_asteriskToH323(AST_CAUSE_NORMAL_CIRCUIT_CONGESTION), AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1734           ast_set_flag(p, H323_NEEDDESTROY);
1735           return -1;
1736          }
1737         }
1738
1739         ooh323c_set_capability_for_call(call, &p->prefs, p->capability, p->dtmfmode, p->dtmfcodec,
1740                                          p->t38support);
1741         configure_local_rtp(p, call);
1742
1743 /* Incoming call */
1744         c = ooh323_new(p, AST_STATE_RING, p->username, 0, NULL);
1745         if(!c) {
1746         ast_mutex_unlock(&p->lock);
1747         ast_log(LOG_ERROR, "Could not create ast_channel\n");
1748          return -1;
1749   }
1750         ast_mutex_unlock(&p->lock);
1751
1752         if (gH323Debug)
1753                 ast_verbose("+++   ooh323_onReceivedSetup - Determined context %s, "
1754                                                 "extension %s\n", p->context, p->exten);
1755
1756         return OO_OK;
1757 }
1758
1759
1760
1761 int onOutgoingCall(ooCallData *call)
1762 {
1763         struct ooh323_pvt *p = NULL;
1764         int i = 0;
1765
1766         if (gH323Debug)
1767                 ast_verbose("---   onOutgoingCall %lx: %s\n", (long unsigned int) call, call->callToken);
1768
1769         if (!strcmp(call->callType, "outgoing")) {
1770                 p = find_call(call);
1771                 if (!p) {
1772                         ast_log(LOG_ERROR, "Failed to find a matching call.\n");
1773                         return -1;
1774                 }
1775                 ast_mutex_lock(&p->lock);
1776
1777                 if (!ast_strlen_zero(p->callerid_name)) {
1778                         ooCallSetCallerId(call, p->callerid_name);
1779                 }
1780                 if (!ast_strlen_zero(p->callerid_num)) {
1781                         i = 0;
1782                         while (*(p->callerid_num + i) != '\0') {
1783                                 if(!isdigit(*(p->callerid_num+i))) { break; }
1784                                 i++;
1785                         }
1786                         if(*(p->callerid_num+i) == '\0')
1787                                 ooCallSetCallingPartyNumber(call, p->callerid_num);
1788                         else {
1789                                 if(!p->callerid_name)
1790                                         ooCallSetCallerId(call, p->callerid_num);
1791                         }
1792                 }
1793                 
1794                 if (!ast_strlen_zero(p->caller_h323id))
1795                         ooCallAddAliasH323ID(call, p->caller_h323id);
1796
1797                 if (!ast_strlen_zero(p->caller_dialedDigits)) {
1798                         if (gH323Debug) {
1799                                 ast_verbose("Setting dialed digits %s\n", p->caller_dialedDigits);
1800                         }
1801                         ooCallAddAliasDialedDigits(call, p->caller_dialedDigits);
1802                 } else if (!ast_strlen_zero(p->callerid_num)) {
1803                         if (ooIsDailedDigit(p->callerid_num)) {
1804                                 if (gH323Debug) {
1805                                         ast_verbose("setting callid number %s\n", p->callerid_num);
1806                                 }
1807                                 ooCallAddAliasDialedDigits(call, p->callerid_num);
1808                         } else if (ast_strlen_zero(p->caller_h323id)) {
1809                                 ooCallAddAliasH323ID(call, p->callerid_num);
1810                         }
1811                 }
1812                 if (p->rtpmask && p->rtpmaskstr[0]) {
1813                         call->rtpMask = p->rtpmask;
1814                         ast_mutex_lock(&call->rtpMask->lock);
1815                         call->rtpMask->inuse++;
1816                         ast_mutex_unlock(&call->rtpMask->lock);
1817                         ast_copy_string(call->rtpMaskStr, p->rtpmaskstr, sizeof(call->rtpMaskStr));
1818                 }
1819
1820                 ast_mutex_unlock(&p->lock);
1821         }
1822
1823         if (gH323Debug)
1824                 ast_verbose("+++   onOutgoingCall %s\n", call->callToken);
1825         return OO_OK;
1826 }
1827
1828
1829 int onNewCallCreated(ooCallData *call)
1830 {
1831         struct ooh323_pvt *p = NULL;
1832         int i = 0;
1833
1834         if (gH323Debug)
1835                 ast_verbose("---   onNewCallCreated %lx: %s\n", (long unsigned int) call, call->callToken);
1836
1837         ast_mutex_lock(&call->Lock);
1838         if (ooh323c_start_call_thread(call)) {
1839                 ast_log(LOG_ERROR,"Failed to create call thread.\n");
1840                 ast_mutex_unlock(&call->Lock);
1841                 return -1;
1842         }
1843
1844         if (!strcmp(call->callType, "outgoing")) {
1845                 p = find_call(call);
1846                 if (!p) {
1847                         ast_log(LOG_ERROR, "Failed to find a matching call.\n");
1848                         ast_mutex_unlock(&call->Lock);
1849                         return -1;
1850                 }
1851                 ast_mutex_lock(&p->lock);
1852
1853                 if (p->callerid_name) {
1854                         ooCallSetCallerId(call, p->callerid_name);
1855                 }
1856                 if (p->callerid_num) {
1857                         i = 0;
1858                         while (*(p->callerid_num + i) != '\0') {
1859                                 if(!isdigit(*(p->callerid_num+i))) { break; }
1860                                 i++;
1861                         }
1862                         if(*(p->callerid_num+i) == '\0')
1863                                 ooCallSetCallingPartyNumber(call, p->callerid_num);
1864                         else {
1865                                 if(!p->callerid_name)
1866                                         ooCallSetCallerId(call, p->callerid_num);
1867                         }
1868                 }
1869                 
1870                 if (!ast_strlen_zero(p->caller_h323id))
1871                         ooCallAddAliasH323ID(call, p->caller_h323id);
1872
1873                 if (!ast_strlen_zero(p->caller_dialedDigits)) {
1874                         if (gH323Debug) {
1875                                 ast_verbose("Setting dialed digits %s\n", p->caller_dialedDigits);
1876                         }
1877                         ooCallAddAliasDialedDigits(call, p->caller_dialedDigits);
1878                 } else if (p->callerid_num) {
1879                         if (ooIsDailedDigit(p->callerid_num)) {
1880                                 if (gH323Debug) {
1881                                         ast_verbose("setting callid number %s\n", p->callerid_num);
1882                                 }
1883                                 ooCallAddAliasDialedDigits(call, p->callerid_num);
1884                         } else if (ast_strlen_zero(p->caller_h323id)) {
1885                                 ooCallAddAliasH323ID(call, p->callerid_num);
1886                         }
1887                 }
1888   
1889
1890                 if (!ast_strlen_zero(p->exten))  {
1891                         if (ooIsDailedDigit(p->exten)) {
1892                                 ooCallSetCalledPartyNumber(call, p->exten);
1893                                 ooCallAddRemoteAliasDialedDigits(call, p->exten);
1894                         } else {
1895                           ooCallAddRemoteAliasH323ID(call, p->exten);
1896                         }
1897                 }
1898
1899                 if (gH323Debug) {
1900                         char prefsBuf[256];
1901                         ast_codec_pref_string(&p->prefs, prefsBuf, sizeof(prefsBuf));
1902                         ast_verbose(" Outgoing call %s(%s) - Codec prefs - %s\n", 
1903                                 p->username?p->username:"NULL", call->callToken, prefsBuf);
1904                 }
1905
1906                 ooh323c_set_capability_for_call(call, &p->prefs, p->capability, 
1907                                      p->dtmfmode, p->dtmfcodec, p->t38support);
1908
1909                 configure_local_rtp(p, call);
1910                 ast_mutex_unlock(&p->lock);
1911         }
1912
1913         ast_mutex_unlock(&call->Lock);
1914         if (gH323Debug)
1915                 ast_verbose("+++   onNewCallCreated %s\n", call->callToken);
1916         return OO_OK;
1917 }
1918
1919 int onCallEstablished(ooCallData *call)
1920 {
1921         struct ooh323_pvt *p = NULL;
1922
1923         if (gH323Debug)
1924                 ast_verbose("---   onCallEstablished %s\n", call->callToken);
1925
1926
1927         if (!(p = find_call(call))) {
1928                 ast_log(LOG_ERROR, "Failed to find a matching call.\n");
1929                 return -1;
1930         }
1931
1932         if(ast_test_flag(p, H323_OUTGOING)) {
1933                 ast_mutex_lock(&p->lock);
1934                 if (!p->owner) {
1935                         ast_mutex_unlock(&p->lock);
1936                         ast_log(LOG_ERROR, "Channel has no owner\n");
1937                         return -1;
1938                 }
1939         
1940                 while (p->owner && ast_channel_trylock(p->owner)) {
1941                         ast_debug(1,"Failed to grab lock, trying again\n");
1942                         DEADLOCK_AVOIDANCE(&p->lock);
1943                 }
1944                 if (p->owner) {
1945                         struct ast_channel* c = p->owner;
1946
1947                         if (call->remoteDisplayName) {
1948                                 struct ast_party_connected_line connected;
1949                                 ast_party_connected_line_init(&connected);
1950                                 connected.id.name = (char *) call->remoteDisplayName;
1951                                 connected.source = AST_CONNECTED_LINE_UPDATE_SOURCE_ANSWER;
1952                                 ast_channel_queue_connected_line_update(c, &connected);
1953                         }
1954
1955                         ast_queue_control(c, AST_CONTROL_ANSWER);
1956                         ast_channel_unlock(p->owner);
1957                         manager_event(EVENT_FLAG_SYSTEM,"ChannelUpdate","Channel: %s\r\nChanneltype: %s\r\n"
1958                                 "CallRef: %d\r\n", c->name, "OOH323", p->call_reference);
1959                 }
1960                 ast_mutex_unlock(&p->lock);
1961
1962         }
1963
1964         if (gH323Debug)
1965                 ast_verbose("+++   onCallEstablished %s\n", call->callToken);
1966
1967         return OO_OK;
1968 }
1969
1970 int onCallCleared(ooCallData *call)
1971 {
1972         struct ooh323_pvt *p = NULL;
1973         int ownerLock = 0;
1974
1975         if (gH323Debug)
1976                 ast_verbose("---   onCallCleared %s \n", call->callToken);
1977
1978
1979    if ((p = find_call(call))) {
1980         ast_mutex_lock(&p->lock);
1981   
1982         while (p->owner) {
1983                 if (ast_channel_trylock(p->owner)) {
1984                         ooTrace(OOTRCLVLINFO, "Failed to grab lock, trying again\n");
1985                         ast_log(LOG_DEBUG,"Failed to grab lock, trying again\n");
1986                         DEADLOCK_AVOIDANCE(&p->lock);
1987                 } else {
1988                         ownerLock = 1; break;
1989                 }
1990         }
1991
1992         if (ownerLock) {
1993                 if (!ast_test_flag(p, H323_ALREADYGONE)) { 
1994
1995                         ast_set_flag(p, H323_ALREADYGONE);
1996                         p->owner->hangupcause = call->q931cause;
1997                         p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
1998                         ast_queue_hangup_with_cause(p->owner,call->q931cause);
1999                 }
2000         }
2001
2002         if(p->owner) {
2003                 p->owner->tech_pvt = NULL;
2004                 ast_channel_unlock(p->owner);
2005                 p->owner = NULL;
2006                 ast_module_unref(myself);
2007         }
2008
2009         ast_set_flag(p, H323_NEEDDESTROY);
2010
2011         ooh323c_stop_call_thread(call);
2012
2013         ast_mutex_unlock(&p->lock);
2014         ast_mutex_lock(&usecnt_lock);
2015         usecnt--;
2016         ast_mutex_unlock(&usecnt_lock);
2017
2018     }
2019
2020         if (gH323Debug)
2021                 ast_verbose("+++   onCallCleared\n");
2022
2023         return OO_OK;
2024 }
2025
2026 /* static void ooh323_delete_user(struct ooh323_user *user)
2027 {
2028         struct ooh323_user *prev = NULL, *cur = NULL;
2029
2030         if (gH323Debug)
2031                 ast_verbose("---   ooh323_delete_user\n");
2032
2033         if (user) {     
2034                 cur = userl.users;
2035                 ast_mutex_lock(&userl.lock);
2036                 while (cur) {
2037                         if (cur == user) break;
2038                         prev = cur;
2039                         cur = cur->next;
2040                 }
2041
2042                 if (cur) {
2043                         if (prev)
2044                                 prev->next = cur->next;
2045                         else
2046                                 userl.users = cur->next;
2047                 }
2048                 ast_mutex_unlock(&userl.lock);
2049
2050                 free(user);
2051         }  
2052
2053         if (gH323Debug)
2054                 ast_verbose("+++   ooh323_delete_user\n");
2055
2056 } */
2057
2058 void ooh323_delete_peer(struct ooh323_peer *peer)
2059 {
2060         struct ooh323_peer *prev = NULL, *cur = NULL;
2061
2062         if (gH323Debug)
2063                 ast_verbose("---   ooh323_delete_peer\n");
2064
2065         if (peer) {     
2066       cur = peerl.peers;
2067                 ast_mutex_lock(&peerl.lock);
2068       while(cur) {
2069          if(cur==peer) break;
2070          prev = cur;
2071          cur = cur->next;
2072                 }
2073
2074                 if (cur) {
2075          if(prev)
2076                                 prev->next = cur->next;
2077          else
2078                                 peerl.peers = cur->next;
2079                         }
2080                 ast_mutex_unlock(&peerl.lock);
2081
2082       if(peer->h323id)   free(peer->h323id);
2083       if(peer->email)    free(peer->email);
2084       if(peer->url)      free(peer->url);
2085       if(peer->e164)     free(peer->e164);
2086
2087                 free(peer);
2088         }  
2089
2090         if (gH323Debug)
2091                 ast_verbose("+++   ooh323_delete_peer\n");
2092
2093 }
2094
2095
2096
2097 static struct ooh323_user *build_user(const char *name, struct ast_variable *v)
2098 {
2099         struct ooh323_user *user = NULL;
2100
2101         if (gH323Debug)
2102                 ast_verbose("---   build_user\n");
2103
2104         user = ast_calloc(1,sizeof(struct ooh323_user));
2105         if (user) {
2106                 memset(user, 0, sizeof(struct ooh323_user));
2107                 ast_mutex_init(&user->lock);
2108                 ast_copy_string(user->name, name, sizeof(user->name));
2109                 user->capability = gCapability;
2110                 memcpy(&user->prefs, &gPrefs, sizeof(user->prefs));
2111                 user->rtptimeout = gRTPTimeout;
2112                 user->dtmfmode = gDTMFMode;
2113                 user->dtmfcodec = gDTMFCodec;
2114                 user->t38support = gT38Support;
2115                 /* set default context */
2116                 ast_copy_string(user->context, gContext, sizeof(user->context));
2117                 ast_copy_string(user->accountcode, gAccountcode, sizeof(user->accountcode));
2118                 user->amaflags = gAMAFLAGS;
2119
2120                 while (v) {
2121                         if (!strcasecmp(v->name, "context")) {
2122                                 ast_copy_string(user->context, v->value, sizeof(user->context));
2123                         } else if (!strcasecmp(v->name, "incominglimit")) {
2124                                 user->incominglimit = atoi(v->value);
2125                                 if (user->incominglimit < 0)
2126                                         user->incominglimit = 0;
2127                         } else if (!strcasecmp(v->name, "accountcode")) {
2128                                 strncpy(user->accountcode, v->value, 
2129                                                 sizeof(user->accountcode)-1);
2130                         } else if (!strcasecmp(v->name, "rtptimeout")) {
2131                                 user->rtptimeout = atoi(v->value);
2132                                 if (user->rtptimeout < 0)
2133                                         user->rtptimeout = gRTPTimeout;
2134                         } else if (!strcasecmp(v->name, "rtpmask")) {
2135                                 if ((user->rtpmask = malloc(sizeof(struct OOH323Regex))) &&
2136                                         (regcomp(&user->rtpmask->regex, v->value, REG_EXTENDED) 
2137                                                                                         == 0)) {
2138                                         ast_mutex_init(&user->rtpmask->lock);
2139                                         user->rtpmask->inuse = 1;
2140                                         ast_copy_string(user->rtpmaskstr, v->value, 
2141                                                                 sizeof(user->rtpmaskstr));
2142                                 } else user->rtpmask = NULL;
2143                         } else if (!strcasecmp(v->name, "disallow")) {
2144                                 ast_parse_allow_disallow(&user->prefs, 
2145                                         &user->capability,  v->value, 0);
2146                         } else if (!strcasecmp(v->name, "allow")) {
2147                                 const char* tcodecs = v->value;
2148                                 if (!strcasecmp(v->value, "all")) {
2149                                         tcodecs = "ulaw,alaw,g729,g723,gsm";
2150                                 }
2151                                 ast_parse_allow_disallow(&user->prefs,
2152                                          &user->capability,  tcodecs, 1);
2153                         } else if (!strcasecmp(v->name, "amaflags")) {
2154                                 user->amaflags = ast_cdr_amaflags2int(v->value);
2155                         } else if (!strcasecmp(v->name, "ip")) {
2156                                 strncpy(user->mIP, v->value, sizeof(user->mIP)-1);
2157                                 user->mUseIP = 1;
2158                         } else if (!strcasecmp(v->name, "host")) {
2159                                 strncpy(user->mIP, v->value, sizeof(user->mIP)-1);
2160                                 user->mUseIP = 1;
2161                         } else if (!strcasecmp(v->name, "dtmfmode")) {
2162                                 if (!strcasecmp(v->value, "rfc2833"))
2163                                         user->dtmfmode = H323_DTMF_RFC2833;
2164                                 if (!strcasecmp(v->value, "cisco"))
2165                                         user->dtmfmode = H323_DTMF_CISCO;
2166                                 else if (!strcasecmp(v->value, "q931keypad"))
2167                                         user->dtmfmode = H323_DTMF_Q931;
2168                                 else if (!strcasecmp(v->value, "h245alphanumeric"))
2169                                         user->dtmfmode = H323_DTMF_H245ALPHANUMERIC;
2170                                 else if (!strcasecmp(v->value, "h245signal"))
2171                                         user->dtmfmode = H323_DTMF_H245SIGNAL;
2172                                 else if (!strcasecmp(v->value, "inband"))
2173                                         user->dtmfmode = H323_DTMF_INBAND;
2174                         } else if (!strcasecmp(v->name, "relaxdtmf")) {
2175                                 user->dtmfmode |= ast_true(v->value) ? H323_DTMF_INBANDRELAX : 0;
2176                         } else if (!strcasecmp(v->name, "dtmfcodec") && atoi(v->value)) {
2177                                 user->dtmfcodec = atoi(v->value);
2178                         } else if (!strcasecmp(v->name, "t38support")) {
2179                                 if (!strcasecmp(v->value, "disabled"))
2180                                         user->t38support = T38_DISABLED;
2181                                 if (!strcasecmp(v->value, "no"))
2182                                         user->t38support = T38_DISABLED;
2183                                 else if (!strcasecmp(v->value, "faxgw"))
2184                                         user->t38support = T38_FAXGW;
2185                                 else if (!strcasecmp(v->value, "yes"))
2186                                         user->t38support = T38_ENABLED;
2187                         }
2188                         v = v->next;
2189                 }
2190         }
2191
2192         if (gH323Debug)
2193                 ast_verbose("+++   build_user\n");
2194
2195         return user;
2196 }
2197
2198 static struct ooh323_peer *build_peer(const char *name, struct ast_variable *v, int friend_type)
2199 {
2200         struct ooh323_peer *peer = NULL;
2201
2202         if (gH323Debug)
2203                 ast_verbose("---   build_peer\n");
2204
2205         peer = ast_calloc(1, sizeof(*peer));
2206         if (peer) {
2207                 memset(peer, 0, sizeof(struct ooh323_peer));
2208                 ast_mutex_init(&peer->lock);
2209                 ast_copy_string(peer->name, name, sizeof(peer->name));
2210                 peer->capability = gCapability;
2211                 memcpy(&peer->prefs, &gPrefs, sizeof(peer->prefs));
2212                 peer->rtptimeout = gRTPTimeout;
2213                 ast_copy_string(peer->accountcode, gAccountcode, sizeof(peer->accountcode));
2214                 peer->amaflags = gAMAFLAGS;
2215                 peer->dtmfmode = gDTMFMode;
2216                 peer->dtmfcodec = gDTMFCodec;
2217                 peer->t38support = gT38Support;
2218                 peer->port = 1720;
2219                 if (0 == friend_type) {
2220                         peer->mFriend = 1;
2221                 }
2222
2223                 while (v) {
2224                         if (!strcasecmp(v->name, "h323id")) {
2225             if (!(peer->h323id = ast_strdup(v->value))) {
2226                                         ast_log(LOG_ERROR, "Could not allocate memory for h323id of "
2227                                                                                          "peer %s\n", name);
2228                                         ooh323_delete_peer(peer);
2229                                         return NULL;
2230                                 }
2231                         } else if (!strcasecmp(v->name, "e164")) {
2232                                 if (!(peer->e164 = ast_strdup(v->value))) {
2233                                         ast_log(LOG_ERROR, "Could not allocate memory for e164 of "
2234                                                                                          "peer %s\n", name);
2235                                         ooh323_delete_peer(peer);
2236                                         return NULL;
2237                                 }
2238                         } else  if (!strcasecmp(v->name, "email")) {
2239                                 if (!(peer->email = ast_strdup(v->value))) {
2240                                         ast_log(LOG_ERROR, "Could not allocate memory for email of "
2241                                                                                          "peer %s\n", name);
2242                                         ooh323_delete_peer(peer);
2243                                         return NULL;
2244                                 }
2245                         } else if (!strcasecmp(v->name, "url")) {
2246                                 if (!(peer->url = ast_strdup(v->value))) {
2247                                         ast_log(LOG_ERROR, "Could not allocate memory for h323id of "
2248                                                                                          "peer %s\n", name);
2249                                         ooh323_delete_peer(peer);
2250                                         return NULL;
2251                                 }
2252                         } else if (!strcasecmp(v->name, "port")) {
2253                                 peer->port = atoi(v->value);
2254                         } else if (!strcasecmp(v->name, "ip")) {
2255                                 ast_copy_string(peer->ip, v->value, sizeof(peer->ip));
2256                         } else if (!strcasecmp(v->name, "host")) {
2257                                 ast_copy_string(peer->ip, v->value, sizeof(peer->ip));
2258                         } else if (!strcasecmp(v->name, "outgoinglimit")) {
2259                                 peer->outgoinglimit = atoi(v->value);
2260                                 if (peer->outgoinglimit < 0)
2261                                         peer->outgoinglimit = 0;
2262                         } else if (!strcasecmp(v->name, "accountcode")) {
2263                                 ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
2264                         } else if (!strcasecmp(v->name, "rtptimeout")) {
2265                                 peer->rtptimeout = atoi(v->value);
2266                                 if(peer->rtptimeout < 0)
2267                                         peer->rtptimeout = gRTPTimeout;
2268                         } else if (!strcasecmp(v->name, "rtpmask")) {
2269                                 if ((peer->rtpmask = malloc(sizeof(struct OOH323Regex))) &&
2270                                         (regcomp(&peer->rtpmask->regex, v->value, REG_EXTENDED) 
2271                                                                                         == 0)) {
2272                                         ast_mutex_init(&peer->rtpmask->lock);
2273                                         peer->rtpmask->inuse = 1;
2274                                         ast_copy_string(peer->rtpmaskstr, v->value, 
2275                                                                 sizeof(peer->rtpmaskstr));
2276                                 } else peer->rtpmask = NULL;
2277                         } else if (!strcasecmp(v->name, "disallow")) {
2278                                 ast_parse_allow_disallow(&peer->prefs, &peer->capability, 
2279                                                                                                  v->value, 0); 
2280                         } else if (!strcasecmp(v->name, "allow")) {
2281                                 const char* tcodecs = v->value;
2282                                 if (!strcasecmp(v->value, "all")) {
2283                                         tcodecs = "ulaw,alaw,g729,g723,gsm";
2284                                 }
2285                                 ast_parse_allow_disallow(&peer->prefs, &peer->capability, 
2286                                                                                                  tcodecs, 1);                            
2287                         } else if (!strcasecmp(v->name,  "amaflags")) {
2288                                 peer->amaflags = ast_cdr_amaflags2int(v->value);
2289                         } else if (!strcasecmp(v->name, "dtmfmode")) {
2290                                 if (!strcasecmp(v->value, "rfc2833"))
2291                                         peer->dtmfmode = H323_DTMF_RFC2833;
2292                                 if (!strcasecmp(v->value, "cisco"))
2293                                         peer->dtmfmode = H323_DTMF_CISCO;
2294                                 else if (!strcasecmp(v->value, "q931keypad"))
2295                                         peer->dtmfmode = H323_DTMF_Q931;
2296                                 else if (!strcasecmp(v->value, "h245alphanumeric"))
2297                                         peer->dtmfmode = H323_DTMF_H245ALPHANUMERIC;
2298                                 else if (!strcasecmp(v->value, "h245signal"))
2299                                         peer->dtmfmode = H323_DTMF_H245SIGNAL;
2300                                 else if (!strcasecmp(v->value, "inband"))
2301                                         peer->dtmfmode = H323_DTMF_INBAND;
2302                         } else if (!strcasecmp(v->name, "relaxdtmf")) {
2303                                 peer->dtmfmode |= ast_true(v->value) ? H323_DTMF_INBANDRELAX : 0;
2304                         } else if (!strcasecmp(v->name, "dtmfcodec") && atoi(v->value)) {
2305                                 peer->dtmfcodec = atoi(v->value);
2306                         } else if (!strcasecmp(v->name, "t38support")) {
2307                                 if (!strcasecmp(v->value, "disabled"))
2308                                         peer->t38support = T38_DISABLED;
2309                                 if (!strcasecmp(v->value, "no"))
2310                                         peer->t38support = T38_DISABLED;
2311                                 else if (!strcasecmp(v->value, "faxgw"))
2312                                         peer->t38support = T38_FAXGW;
2313                                 else if (!strcasecmp(v->value, "yes"))
2314                                         peer->t38support = T38_ENABLED;
2315                         }
2316                         v = v->next;
2317                 }
2318         }
2319
2320         if (gH323Debug)
2321                 ast_verbose("+++   build_peer\n");
2322
2323         return peer;
2324 }
2325
2326 static int ooh323_do_reload(void)
2327 {
2328         if (gH323Debug) {
2329                 ast_verbose("---   ooh323_do_reload\n");
2330         }
2331
2332         reload_config(1);
2333
2334         if (gH323Debug) {
2335                 ast_verbose("+++   ooh323_do_reload\n");
2336         }
2337
2338         return 0;
2339 }
2340
2341 /*--- h323_reload: Force reload of module from cli ---*/
2342
2343 char *handle_cli_ooh323_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2344 {
2345
2346        switch (cmd) {
2347        case CLI_INIT:
2348                e->command = "ooh323 reload";
2349                e->usage =
2350                        "Usage: ooh323 reload\n"
2351                        "                Reload OOH323 config.\n";
2352                return NULL;
2353        case CLI_GENERATE:
2354                return NULL;
2355        }
2356
2357        if (a->argc != 2)
2358                return CLI_SHOWUSAGE;
2359
2360         if (gH323Debug)
2361                 ast_verbose("---   ooh323_reload\n");
2362
2363         ast_mutex_lock(&h323_reload_lock);
2364         if (h323_reloading) {
2365                 ast_verbose("Previous OOH323 reload not yet done\n");
2366    } else {
2367                 h323_reloading = 1;
2368         }
2369         ast_mutex_unlock(&h323_reload_lock);
2370         restart_monitor();
2371
2372         if (gH323Debug)
2373                 ast_verbose("+++   ooh323_reload\n");
2374
2375         return 0;
2376 }
2377
2378 int reload_config(int reload)
2379 {
2380         int format;
2381         struct ooAliases  *pNewAlias = NULL, *cur, *prev;
2382         struct ast_config *cfg;
2383         struct ast_variable *v;
2384         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
2385         struct ooh323_user *user = NULL;
2386         struct ooh323_peer *peer = NULL;
2387         char *cat;
2388         const char *utype;
2389
2390         if (gH323Debug)
2391                 ast_verbose("---   reload_config\n");
2392
2393         cfg = ast_config_load((char*)config, config_flags);
2394
2395         /* We *must* have a config file otherwise stop immediately */
2396         if (!cfg) {
2397                 ast_log(LOG_NOTICE, "Unable to load config %s, OOH323 disabled\n", config);
2398                 return 1;
2399         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
2400                 return RESULT_SUCCESS;
2401
2402         if (reload) {
2403                 delete_users();
2404                 delete_peers();
2405                 if (gH323Debug) {
2406                         ast_verbose("  reload_config - Freeing up alias list\n");
2407                 }
2408                 cur = gAliasList;
2409                 while (cur) {
2410                         prev = cur;
2411                         cur = cur->next;
2412                         free(prev->value);
2413                         free(prev);
2414                 }
2415                 gAliasList = NULL;
2416         }
2417
2418         /* Inintialize everything to default */
2419         strcpy(gLogFile, DEFAULT_LOGFILE);
2420         gPort = 1720;
2421         gIP[0] = '\0';
2422         strcpy(gCallerID, DEFAULT_H323ID);
2423         gCapability = AST_FORMAT_ALAW;
2424         memset(&gPrefs, 0, sizeof(struct ast_codec_pref));
2425         gDTMFMode = H323_DTMF_RFC2833;
2426         gDTMFCodec = 101;
2427         gT38Support = T38_FAXGW;
2428         gTRCLVL = OOTRCLVLERR;
2429         gRasGkMode = RasNoGatekeeper;
2430         gGatekeeper[0] = '\0';
2431         gRTPTimeout = 60;
2432         strcpy(gAccountcode, DEFAULT_H323ACCNT);
2433         gFastStart = 1;
2434         gTunneling = 1;
2435         gTOS = 0;
2436         strcpy(gContext, DEFAULT_CONTEXT);
2437         gAliasList = NULL;
2438         gMediaWaitForConnect = 0;
2439         ooconfig.mTCPPortStart = 12030;
2440         ooconfig.mTCPPortEnd = 12230;
2441         memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2442
2443         v = ast_variable_browse(cfg, "general");
2444         while (v) {
2445
2446                 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
2447                         v = v->next;
2448                         continue;
2449                 }
2450         
2451                 if (!strcasecmp(v->name, "port")) {
2452                         gPort = (int)strtol(v->value, NULL, 10);
2453                 } else if (!strcasecmp(v->name, "bindaddr")) {
2454                         ast_copy_string(gIP, v->value, sizeof(gIP));
2455                 } else if (!strcasecmp(v->name, "h225portrange")) {
2456                         char* endlimit = 0;
2457                         char temp[512];
2458                         ast_copy_string(temp, v->value, sizeof(temp));
2459                         endlimit = strchr(temp, ',');
2460                         if (endlimit) {
2461                                 *endlimit = '\0';
2462                                 endlimit++;
2463                                 ooconfig.mTCPPortStart = atoi(temp);
2464                                 ooconfig.mTCPPortEnd = atoi(endlimit);
2465
2466                         } else {
2467                                 ast_log(LOG_ERROR, "h225portrange: Invalid format, separate port range with \",\"\n");
2468                         }
2469                 } else if (!strcasecmp(v->name, "gateway")) {
2470                         gIsGateway = ast_true(v->value);
2471                 } else if (!strcasecmp(v->name, "faststart")) {
2472                         gFastStart = ast_true(v->value);
2473                         if (gFastStart)
2474                                 ooH323EpEnableFastStart();
2475                         else
2476                                 ooH323EpDisableFastStart();
2477                 } else if (!strcasecmp(v->name, "mediawaitforconnect")) {
2478                         gMediaWaitForConnect = ast_true(v->value);
2479                         if (gMediaWaitForConnect)
2480                                 ooH323EpEnableMediaWaitForConnect();
2481                         else 
2482                                 ooH323EpDisableMediaWaitForConnect();
2483                 } else if (!strcasecmp(v->name, "h245tunneling")) {
2484                         gTunneling = ast_true(v->value);
2485                         if (gTunneling)
2486                                 ooH323EpEnableH245Tunneling();
2487                         else
2488                                 ooH323EpDisableH245Tunneling();
2489                 } else if (!strcasecmp(v->name, "trybemaster")) {
2490                         gBeMaster = ast_true(v->value);
2491                         if (gBeMaster)
2492                                 ooH323EpTryBeMaster(1);
2493                         else 
2494                                 ooH323EpTryBeMaster(0);
2495                 } else if (!strcasecmp(v->name, "h323id")) {
2496                         pNewAlias = malloc(sizeof(struct ooAliases));
2497                         if (!pNewAlias) {
2498                                 ast_log(LOG_ERROR, "Failed to allocate memory for h323id alias\n");
2499                                 return 1;
2500                         }
2501                         if (gAliasList == NULL) { /* first h323id - set as callerid if callerid is not set */
2502                                 ast_copy_string(gCallerID, v->value, sizeof(gCallerID));
2503                         }
2504                         pNewAlias->type =  T_H225AliasAddress_h323_ID;
2505                         pNewAlias->value = strdup(v->value);
2506                         pNewAlias->next = gAliasList;
2507                         gAliasList = pNewAlias;
2508                         pNewAlias = NULL;
2509                 } else if (!strcasecmp(v->name, "e164")) {
2510                         pNewAlias = malloc(sizeof(struct ooAliases));
2511                         if (!pNewAlias) {
2512                                 ast_log(LOG_ERROR, "Failed to allocate memory for e164 alias\n");
2513                                 return 1;
2514                         }
2515                         pNewAlias->type =  T_H225AliasAddress_dialedDigits;
2516                         pNewAlias->value = strdup(v->value);
2517                         pNewAlias->next = gAliasList;
2518                         gAliasList = pNewAlias;
2519                         pNewAlias = NULL;
2520                 } else if (!strcasecmp(v->name, "email")) {
2521                         pNewAlias = malloc(sizeof(struct ooAliases));
2522                         if (!pNewAlias) {
2523                                 ast_log(LOG_ERROR, "Failed to allocate memory for email alias\n");
2524                                 return 1;
2525                         }
2526                         pNewAlias->type =  T_H225AliasAddress_email_ID;
2527                         pNewAlias->value = strdup(v->value);
2528                         pNewAlias->next = gAliasList;
2529                         gAliasList = pNewAlias;
2530                         pNewAlias = NULL;
2531       } else if (!strcasecmp(v->name, "t35country")) {
2532          t35countrycode = atoi(v->value);
2533       } else if (!strcasecmp(v->name, "t35extensions")) {
2534          t35extensions = atoi(v->value);
2535       } else if (!strcasecmp(v->name, "manufacturer")) {
2536          manufacturer = atoi(v->value);
2537       } else if (!strcasecmp(v->name, "vendorid")) {
2538          ast_copy_string(vendor, v->value, sizeof(vendor));
2539       } else if (!strcasecmp(v->name, "versionid")) {
2540          ast_copy_string(version, v->value, sizeof(version));
2541                 } else if (!strcasecmp(v->name, "callerid")) {
2542                         ast_copy_string(gCallerID, v->value, sizeof(gCallerID));
2543                 } else if (!strcasecmp(v->name, "incominglimit")) {
2544                         gIncomingLimit = atoi(v->value);
2545                 } else if (!strcasecmp(v->name, "outgoinglimit")) {
2546                         gOutgoingLimit = atoi(v->value);
2547                 } else if (!strcasecmp(v->name, "gatekeeper")) {
2548                         if (!strcasecmp(v->value, "DISABLE")) {
2549                                 gRasGkMode = RasNoGatekeeper;
2550                         } else if (!strcasecmp(v->value, "DISCOVER")) {
2551                                 gRasGkMode = RasDiscoverGatekeeper;
2552                         } else {
2553                                 gRasGkMode = RasUseSpecificGatekeeper;
2554                                 strncpy(gGatekeeper, v->value, sizeof(gGatekeeper)-1);
2555                         }
2556                 } else if (!strcasecmp(v->name, "logfile")) {
2557          strncpy(gLogFile, v->value, sizeof(gLogFile)-1);
2558                 } else if (!strcasecmp(v->name, "context")) {
2559          strncpy(gContext, v->value, sizeof(gContext)-1);
2560          ast_verbose(VERBOSE_PREFIX_3 "  == Setting default context to %s\n", 
2561                                                       gContext);
2562                 } else if (!strcasecmp(v->name, "rtptimeout")) {
2563                         gRTPTimeout = atoi(v->value);
2564                         if (gRTPTimeout <= 0)
2565                                 gRTPTimeout = 60;
2566                 } else if (!strcasecmp(v->name, "tos")) {
2567                         if (sscanf(v->value, "%30i", &format) == 1)
2568                                 gTOS = format & 0xff;
2569                         else if (!strcasecmp(v->value, "lowdelay"))
2570                                 gTOS = IPTOS_LOWDELAY;
2571                         else if (!strcasecmp(v->value, "throughput"))
2572                                 gTOS = IPTOS_THROUGHPUT;
2573                         else if (!strcasecmp(v->value, "reliability"))
2574                                 gTOS = IPTOS_RELIABILITY;
2575                         else if (!strcasecmp(v->value, "mincost"))
2576                                 gTOS = IPTOS_MINCOST;
2577                         else if (!strcasecmp(v->value, "none"))
2578                                 gTOS = 0;
2579                         else
2580                                 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be "
2581                                                                                         "'lowdelay', 'throughput', 'reliability', "
2582                                                                                         "'mincost', or 'none'\n", v->lineno);
2583                 } else if (!strcasecmp(v->name, "amaflags")) {
2584                         gAMAFLAGS = ast_cdr_amaflags2int(v->value);
2585                 } else if (!strcasecmp(v->name, "accountcode")) {
2586          ast_copy_string(gAccountcode, v->value, sizeof(gAccountcode));
2587                 } else if (!strcasecmp(v->name, "disallow")) {
2588                         ast_parse_allow_disallow(&gPrefs, &gCapability, v->value, 0);
2589                 } else if (!strcasecmp(v->name, "allow")) {
2590                         const char* tcodecs = v->value;
2591                         if (!strcasecmp(v->value, "all")) {
2592                                 tcodecs = "ulaw,alaw,g729,g723,gsm";
2593                         }
2594                         ast_parse_allow_disallow(&gPrefs, &gCapability, tcodecs, 1);
2595                 } else if (!strcasecmp(v->name, "dtmfmode")) {
2596                         if (!strcasecmp(v->value, "inband"))
2597                                 gDTMFMode = H323_DTMF_INBAND;
2598                         else if (!strcasecmp(v->value, "rfc2833"))
2599                                 gDTMFMode = H323_DTMF_RFC2833;
2600                         else if (!strcasecmp(v->value, "cisco"))
2601                                 gDTMFMode = H323_DTMF_CISCO;
2602                         else if (!strcasecmp(v->value, "q931keypad"))
2603                                 gDTMFMode = H323_DTMF_Q931;
2604                         else if (!strcasecmp(v->value, "h245alphanumeric"))
2605                                 gDTMFMode = H323_DTMF_H245ALPHANUMERIC;
2606                         else if (!strcasecmp(v->value, "h245signal"))
2607                                 gDTMFMode = H323_DTMF_H245SIGNAL;
2608                         else {
2609             ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", 
2610                                                                     v->value);
2611                                 gDTMFMode = H323_DTMF_RFC2833;
2612                         }
2613                 } else if (!strcasecmp(v->name, "relaxdtmf")) {
2614                         gDTMFMode |= ast_true(v->value) ? H323_DTMF_INBANDRELAX : 0;
2615                 } else if (!strcasecmp(v->name, "dtmfcodec") && atoi(v->value)) {
2616                         gDTMFCodec = atoi(v->value);
2617                 } else if (!strcasecmp(v->name, "t38support")) {
2618                         if (!strcasecmp(v->value, "disabled"))
2619                                 gT38Support = T38_DISABLED;
2620                         if (!strcasecmp(v->value, "no"))
2621                                 gT38Support = T38_DISABLED;
2622                         else if (!strcasecmp(v->value, "faxgw"))
2623                                 gT38Support = T38_FAXGW;
2624                         else if (!strcasecmp(v->value, "yes"))
2625                                 gT38Support = T38_ENABLED;
2626                 } else if (!strcasecmp(v->name, "tracelevel")) {
2627                         gTRCLVL = atoi(v->value);
2628                         ooH323EpSetTraceLevel(gTRCLVL);
2629                 }
2630                 v = v->next;
2631         }
2632         
2633         for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
2634                 if (strcasecmp(cat, "general")) {
2635                         int friend_type = 0;
2636                         utype = ast_variable_retrieve(cfg, cat, "type");
2637                         if (utype) {
2638                                 friend_type = strcasecmp(utype, "friend");
2639                                 if (!strcmp(utype, "user") || 0 == friend_type) {
2640                                         user = build_user(cat, ast_variable_browse(cfg, cat));
2641                                         if (user) {
2642                                                 ast_mutex_lock(&userl.lock);
2643                                                 user->next = userl.users;
2644                                                 userl.users = user;
2645                                                 ast_mutex_unlock(&userl.lock);
2646                                         } else {
2647                                                 ast_log(LOG_WARNING, "Failed to build user %s\n", cat);
2648                                         }
2649                                 }
2650                                 if (!strcasecmp(utype, "peer") || 0 == friend_type) {
2651                                         peer = build_peer(cat, ast_variable_browse(cfg, cat), friend_type);
2652                                         if (peer) {
2653                                                 ast_mutex_lock(&peerl.lock);
2654                                                 peer->next = peerl.peers;
2655                                                 peerl.peers = peer;
2656                                                 ast_mutex_unlock(&peerl.lock);
2657                                         } else {
2658                                                 ast_log(LOG_WARNING, "Failed to build peer %s\n", cat);
2659                                         }
2660                                 }
2661                         }
2662                 }
2663         }
2664         ast_config_destroy(cfg);
2665
2666
2667         /* Determine ip address if neccessary */
2668         if (ast_strlen_zero(gIP)) {
2669                 ooGetLocalIPAddress(gIP);
2670                 if (!strcmp(gIP, "127.0.0.1")) {
2671                         ast_log(LOG_NOTICE, "Failed to determine local ip address. Please "
2672                                                                          "specify it in ooh323.conf. OOH323 Disabled\n");
2673                         return 1;
2674                 }
2675         }
2676
2677         if (gH323Debug)
2678                 ast_verbose("+++   reload_config\n");
2679
2680         return 0;
2681
2682 }
2683
2684
2685 static char *handle_cli_ooh323_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2686 {
2687         char ip_port[30];
2688         struct ooh323_peer *prev = NULL, *peer = NULL;
2689         
2690         switch (cmd) {
2691         case CLI_INIT:
2692                 e->command = "ooh323 show peer";
2693                 e->usage =
2694                         "Usage: ooh323 show peer <name>\n"
2695                         "                List details of specific OOH323 peer.\n";
2696                 return NULL;
2697         case CLI_GENERATE:
2698                 return NULL;
2699         }
2700
2701         if (a->argc != 4)
2702                 return CLI_SHOWUSAGE;
2703
2704  
2705         ast_mutex_lock(&peerl.lock);
2706         peer = peerl.peers;
2707         while (peer) {
2708                 ast_mutex_lock(&peer->lock);
2709                 if(!strcmp(peer->name, a->argv[3]))
2710                         break;
2711                 else {
2712                         prev = peer;
2713                         peer = peer->next;
2714                         ast_mutex_unlock(&prev->lock);
2715                 }
2716         }
2717
2718         if (peer) {
2719       sprintf(ip_port, "%s:%d", peer->ip, peer->port);
2720       ast_cli(a->fd, "%-15.15s%s\n", "Name: ", peer->name);
2721       ast_cli(a->fd, "%-15.15s%s", "Format Prefs: ", "(");
2722       print_codec_to_cli(a->fd, &peer->prefs);
2723       ast_cli(a->fd, ")\n");
2724       ast_cli(a->fd, "%-15.15s", "DTMF Mode: ");
2725                 if (peer->dtmfmode & H323_DTMF_CISCO) {
2726          ast_cli(a->fd, "%s\n", "cisco");
2727          ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", peer->dtmfcodec);
2728                 } else if (peer->dtmfmode & H323_DTMF_RFC2833) {
2729          ast_cli(a->fd, "%s\n", "rfc2833");
2730          ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", peer->dtmfcodec);
2731                 } else if (peer->dtmfmode & H323_DTMF_Q931)
2732          ast_cli(a->fd, "%s\n", "q931keypad");
2733                 else if (peer->dtmfmode & H323_DTMF_H245ALPHANUMERIC)
2734          ast_cli(a->fd, "%s\n", "h245alphanumeric");
2735                 else if (peer->dtmfmode & H323_DTMF_H245SIGNAL)
2736          ast_cli(a->fd, "%s\n", "h245signal");
2737                 else if (peer->dtmfmode & H323_DTMF_INBAND && peer->dtmfmode & H323_DTMF_INBANDRELAX)
2738          ast_cli(a->fd, "%s\n", "inband-relaxed");
2739                 else if (peer->dtmfmode & H323_DTMF_INBAND)
2740          ast_cli(a->fd, "%s\n", "inband");
2741                 else
2742          ast_cli(a->fd, "%s\n", "unknown");
2743
2744         ast_cli(a->fd,"%-15s", "T.38 Mode: ");
2745         if (peer->t38support == T38_DISABLED)
2746                 ast_cli(a->fd, "%s\n", "disabled");
2747         else if (peer->t38support == T38_FAXGW)
2748                 ast_cli(a->fd, "%s\n", "faxgw/chan_sip compatible");
2749
2750         ast_cli(a->fd, "%-15.15s%s\n", "AccountCode: ", peer->accountcode);
2751         ast_cli(a->fd, "%-15.15s%s\n", "AMA flags: ", 
2752                 ast_cdr_flags2str(peer->amaflags));
2753         ast_cli(a->fd, "%-15.15s%s\n", "IP:Port: ", ip_port);
2754         ast_cli(a->fd, "%-15.15s%d\n", "OutgoingLimit: ", peer->outgoinglimit);
2755         ast_cli(a->fd, "%-15.15s%d\n", "rtptimeout: ", peer->rtptimeout);
2756         if (peer->rtpmaskstr[0])
2757                 ast_cli(a->fd, "%-15.15s%s\n", "rtpmask: ", peer->rtpmaskstr);
2758         ast_mutex_unlock(&peer->lock);
2759         } else {
2760         ast_cli(a->fd, "Peer %s not found\n", a->argv[3]);
2761         ast_cli(a->fd, "\n");
2762         }
2763         ast_mutex_unlock(&peerl.lock);
2764
2765         return CLI_SUCCESS;
2766 }
2767
2768 static char *handle_cli_ooh323_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2769 {
2770         struct ooh323_peer *prev = NULL, *peer = NULL;
2771    char formats[FORMAT_STRING_SIZE];
2772    char ip_port[30];
2773 #define FORMAT  "%-15.15s  %-15.15s  %-23.23s  %-s\n"
2774
2775         switch (cmd) {
2776         case CLI_INIT:
2777                 e->command = "ooh323 show peers";
2778                 e->usage =
2779                         "Usage: ooh323 show peers\n"
2780                         "                Lists all known OOH323 peers.\n";
2781                 return NULL;
2782         case CLI_GENERATE:
2783                 return NULL;
2784         }
2785
2786         if (a->argc != 3)
2787                 return CLI_SHOWUSAGE;
2788
2789    ast_cli(a->fd, FORMAT, "Name", "Accountcode", "ip:port", "Formats");
2790
2791         ast_mutex_lock(&peerl.lock);
2792         peer = peerl.peers;
2793         while (peer) {
2794                 ast_mutex_lock(&peer->lock);
2795                 snprintf(ip_port, sizeof(ip_port), "%s:%d", peer->ip, peer->port);
2796      ast_cli(a->fd, FORMAT, peer->name, 
2797                                         peer->accountcode,
2798                                         ip_port,
2799                  ast_getformatname_multiple(formats,FORMAT_STRING_SIZE,peer->capability));
2800                 prev = peer;
2801                 peer = peer->next;
2802                 ast_mutex_unlock(&prev->lock);
2803
2804         }
2805         ast_mutex_unlock(&peerl.lock);
2806 #undef FORMAT
2807         return CLI_SUCCESS;
2808 }
2809
2810 /*! \brief Print codec list from preference to CLI/manager */
2811 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
2812 {
2813         int x, codec;
2814
2815         for (x = 0; x < 32; x++) {
2816                 codec = ast_codec_pref_index(pref, x);
2817                 if (!codec)
2818                         break;
2819                 ast_cli(fd, "%s", ast_getformatname(codec));
2820                 ast_cli(fd, ":%d", pref->framing[x]);
2821                 if (x < 31 && ast_codec_pref_index(pref, x + 1))
2822                         ast_cli(fd, ",");
2823         }
2824         if (!x)
2825                 ast_cli(fd, "none");
2826 }
2827
2828 static char *handle_cli_ooh323_show_user(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2829 {
2830         struct ooh323_user *prev = NULL, *user = NULL;
2831
2832         switch (cmd) {
2833         case CLI_INIT:
2834                 e->command = "ooh323 show user";
2835                 e->usage =
2836                         "Usage: ooh323 show user <name>\n"
2837                         "                List details of specific OOH323 user.\n";
2838                 return NULL;
2839         case CLI_GENERATE:
2840                 return NULL;
2841         }
2842
2843         if (a->argc != 4)
2844                 return CLI_SHOWUSAGE;
2845
2846
2847         ast_mutex_lock(&userl.lock);
2848         user = userl.users;
2849         while (user) {
2850                 ast_mutex_lock(&user->lock);
2851       if(!strcmp(user->name, a->argv[3])) {
2852                         break;
2853       } else {
2854                         prev = user;
2855                         user = user->next;
2856                         ast_mutex_unlock(&prev->lock);
2857                 }
2858         }
2859
2860         if (user) {
2861       ast_cli(a->fd, "%-15.15s%s\n", "Name: ", user->name);
2862       ast_cli(a->fd, "%-15.15s%s", "Format Prefs: ", "(");
2863       print_codec_to_cli(a->fd, &user->prefs);
2864       ast_cli(a->fd, ")\n");
2865       ast_cli(a->fd, "%-15.15s", "DTMF Mode: ");
2866                 if (user->dtmfmode & H323_DTMF_CISCO) {
2867          ast_cli(a->fd, "%s\n", "cisco");
2868          ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", user->dtmfcodec);
2869                 } else if (user->dtmfmode & H323_DTMF_RFC2833) {
2870          ast_cli(a->fd, "%s\n", "rfc2833");
2871          ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", user->dtmfcodec);
2872                 } else if (user->dtmfmode & H323_DTMF_Q931)
2873          ast_cli(a->fd, "%s\n", "q931keypad");
2874                 else if (user->dtmfmode & H323_DTMF_H245ALPHANUMERIC)
2875          ast_cli(a->fd, "%s\n", "h245alphanumeric");
2876                 else if (user->dtmfmode & H323_DTMF_H245SIGNAL)
2877          ast_cli(a->fd, "%s\n", "h245signal");
2878                 else if (user->dtmfmode & H323_DTMF_INBAND && user->dtmfmode & H323_DTMF_INBANDRELAX)
2879          ast_cli(a->fd, "%s\n", "inband-relaxed");
2880                 else if (user->dtmfmode & H323_DTMF_INBAND)
2881          ast_cli(a->fd, "%s\n", "inband");
2882                 else
2883          ast_cli(a->fd, "%s\n", "unknown");
2884
2885         ast_cli(a->fd,"%-15s", "T.38 Mode: ");
2886         if (user->t38support == T38_DISABLED)
2887                 ast_cli(a->fd, "%s\n", "disabled");
2888         else if (user->t38support == T38_FAXGW)
2889                 ast_cli(a->fd, "%s\n", "faxgw/chan_sip compatible");
2890
2891       ast_cli(a->fd, "%-15.15s%s\n", "AccountCode: ", user->accountcode);
2892       ast_cli(a->fd, "%-15.15s%s\n", "AMA flags: ", 
2893                                             ast_cdr_flags2str(user->amaflags));
2894       ast_cli(a->fd, "%-15.15s%s\n", "Context: ", user->context);
2895       ast_cli(a->fd, "%-15.15s%d\n", "IncomingLimit: ", user->incominglimit);
2896       ast_cli(a->fd, "%-15.15s%d\n", "InUse: ", user->inUse);
2897       ast_cli(a->fd, "%-15.15s%d\n", "rtptimeout: ", user->rtptimeout);
2898         if (user->rtpmaskstr[0])
2899                 ast_cli(a->fd, "%-15.15s%s\n", "rtpmask: ", user->rtpmaskstr);
2900                 ast_mutex_unlock(&user->lock);
2901         } else {
2902      ast_cli(a->fd, "User %s not found\n", a->argv[3]);
2903      ast_cli(a->fd, "\n");
2904         }
2905         ast_mutex_unlock(&userl.lock);
2906
2907         return CLI_SUCCESS;
2908 }
2909
2910 static char *handle_cli_ooh323_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2911 {
2912         struct ooh323_user *prev = NULL, *user = NULL;
2913    char formats[FORMAT_STRING_SIZE];
2914 #define FORMAT1  "%-15.15s  %-15.15s  %-15.15s  %-s\n"
2915
2916         switch (cmd) {
2917         case CLI_INIT:
2918                 e->command = "ooh323 show users";
2919                 e->usage =
2920                         "Usage: ooh323 show users \n"
2921                         "                Lists all known OOH323 users.\n";
2922                 return NULL;
2923         case CLI_GENERATE:
2924                 return NULL;
2925         }
2926
2927         if (a->argc != 3)
2928                 return CLI_SHOWUSAGE;
2929
2930
2931    ast_cli(a->fd, FORMAT1, "Username", "Accountcode", "Context", "Formats");
2932
2933         ast_mutex_lock(&userl.lock);
2934         user = userl.users;
2935    while(user)
2936    {
2937                 ast_mutex_lock(&user->lock);
2938                 ast_cli(a->fd, FORMAT1, user->name, 
2939                                         user->accountcode, user->context,
2940                                         ast_getformatname_multiple(formats, FORMAT_STRING_SIZE, user->capability));
2941                 prev = user;
2942                 user = user->next;
2943                 ast_mutex_unlock(&prev->lock);
2944
2945         }
2946         ast_mutex_unlock(&userl.lock);
2947 #undef FORMAT1
2948    return RESULT_SUCCESS;
2949
2950 }
2951
2952 static char *handle_cli_ooh323_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2953 {
2954         switch (cmd) {
2955         case CLI_INIT:
2956                 e->command = "ooh323 set debug [off]";
2957                 e->usage =
2958                         "Usage: ooh323 set debug [off]\n"
2959                         "                Enables/Disables debugging of OOH323 channel driver\n";
2960                 return NULL;
2961         case CLI_GENERATE:
2962                 return NULL;
2963         }
2964
2965         if (a->argc < 3 || a->argc > 4)
2966                 return CLI_SHOWUSAGE;
2967         if (a->argc == 4 && strcasecmp(a->argv[3], "off"))
2968                 return CLI_SHOWUSAGE;
2969
2970         gH323Debug = (a->argc == 4) ? FALSE : TRUE;
2971         ast_cli(a->fd, "OOH323 Debugging %s\n", gH323Debug ? "Enabled" : "Disabled");
2972
2973         return CLI_SUCCESS;
2974 }
2975
2976 #if 0
2977 static int ooh323_show_channels(int fd, int argc, char *argv[])
2978 {
2979         return RESULT_SUCCESS;
2980 }
2981 #endif
2982
2983 static char *handle_cli_ooh323_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2984 {
2985         char value[FORMAT_STRING_SIZE];
2986         ooAliases *pAlias = NULL, *pAliasNext = NULL;;
2987
2988         switch (cmd) {
2989         case CLI_INIT:
2990                 e->command = "ooh323 show config";
2991                 e->usage =
2992                         "Usage: ooh323 show config\n"
2993                         "                Shows global configuration of H.323 channel driver\n";
2994                 return NULL;
2995         case CLI_GENERATE:
2996                 return NULL;
2997         }
2998
2999         if (a->argc != 3)
3000                 return CLI_SHOWUSAGE;
3001
3002
3003
3004    ast_cli(a->fd, "\nObjective Open H.323 Channel Driver's Config:\n");
3005         snprintf(value, sizeof(value), "%s:%d", gIP, gPort);
3006    ast_cli(a->fd, "%-20s%s\n", "IP:Port: ", value);
3007    ast_cli(a->fd, "%-20s%d-%d\n", "H.225 port range: ", 
3008       ooconfig.mTCPPortStart, ooconfig.mTCPPortEnd);
3009    ast_cli(a->fd, "%-20s%s\n", "FastStart", gFastStart?"yes":"no");
3010    ast_cli(a->fd, "%-20s%s\n", "Tunneling", gTunneling?"yes":"no");
3011    ast_cli(a->fd, "%-20s%s\n", "CallerId", gCallerID);
3012    ast_cli(a->fd, "%-20s%s\n", "MediaWaitForConnect", 
3013       gMediaWaitForConnect?"yes":"no");
3014
3015 #if (0)
3016                 extern OOH323EndPoint gH323ep;
3017    ast_cli(a->fd, "%-20s%s\n", "FASTSTART", 
3018                         (OO_TESTFLAG(gH323ep.flags, OO_M_FASTSTART) != 0) ? "yes" : "no");
3019    ast_cli(a->fd, "%-20s%s\n", "TUNNELING", 
3020                         (OO_TESTFLAG(gH323ep.flags, OO_M_TUNNELING) != 0) ? "yes" : "no");
3021    ast_cli(a->fd, "%-20s%s\n", "MEDIAWAITFORCONN",
3022                         (OO_TESTFLAG(gH323ep.flags, OO_M_MEDIAWAITFORCONN) != 0) ? "yes" : "no");
3023 #endif
3024
3025         if (gRasGkMode == RasNoGatekeeper)
3026                 snprintf(value, sizeof(value), "%s", "No Gatekeeper");
3027         else if (gRasGkMode == RasDiscoverGatekeeper)
3028                 snprintf(value, sizeof(value), "%s", "Discover");
3029         else
3030                 snprintf(value, sizeof(value), "%s", gGatekeeper);
3031
3032    ast_cli(a->fd,  "%-20s%s\n", "Gatekeeper:", value);
3033
3034    ast_cli(a->fd,  "%-20s%s\n", "H.323 LogFile:", gLogFile);   
3035
3036    ast_cli(a->fd,  "%-20s%s\n", "Context:", gContext);
3037    
3038    ast_cli(a->fd,  "%-20s%s\n", "Capability:", 
3039            ast_getformatname_multiple(value,FORMAT_STRING_SIZE,gCapability));
3040
3041    ast_cli(a->fd, "%-20s", "DTMF Mode: ");
3042         if (gDTMFMode & H323_DTMF_CISCO) {
3043       ast_cli(a->fd, "%s\n", "cisco");
3044       ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", gDTMFCodec);
3045         } else if (gDTMFMode & H323_DTMF_RFC2833) {
3046       ast_cli(a->fd, "%s\n", "rfc2833");
3047       ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", gDTMFCodec);
3048         } else if (gDTMFMode & H323_DTMF_Q931)
3049       ast_cli(a->fd, "%s\n", "q931keypad");
3050         else if (gDTMFMode & H323_DTMF_H245ALPHANUMERIC)
3051       ast_cli(a->fd, "%s\n", "h245alphanumeric");
3052         else if (gDTMFMode & H323_DTMF_H245SIGNAL)
3053       ast_cli(a->fd, "%s\n", "h245signal");
3054                 else if (gDTMFMode & H323_DTMF_INBAND && gDTMFMode & H323_DTMF_INBANDRELAX)
3055          ast_cli(a->fd, "%s\n", "inband-relaxed");
3056                 else if (gDTMFMode & H323_DTMF_INBAND)
3057  &n