Create iterative method for querying SRV results, and use that for finding AGI servers.
[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 {
906                 ast->connected.id.name = strdup(gCallerID);
907                 if(p->callerid_name)
908                         free(p->callerid_name);
909                 p->callerid_name = strdup(ast->connected.id.name);
910         }
911
912         /* Retrieve vars */
913
914
915         if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323ID"))) {
916                 ast_copy_string(p->caller_h323id, val, sizeof(p->caller_h323id));
917         }
918         
919         if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323DIALEDDIGITS"))) {
920                 ast_copy_string(p->caller_dialedDigits, val, sizeof(p->caller_dialedDigits));
921                 if(!p->callerid_num)
922                         p->callerid_num = strdup(val);
923         }
924
925         if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323EMAIL"))) {
926                 ast_copy_string(p->caller_email, val, sizeof(p->caller_email));
927         }
928
929         if ((val = pbx_builtin_getvar_helper(ast, "CALLER_H323URL"))) {
930                 ast_copy_string(p->caller_url, val, sizeof(p->caller_url));
931         }
932
933         if (p->host && p->port != 0)
934                 snprintf(destination, sizeof(destination), "%s:%d", p->host, p->port);
935         else if (p->host)
936                 snprintf(destination, sizeof(destination), "%s", p->host);
937         else
938                 ast_copy_string(destination, dest, sizeof(destination));
939
940    destination[sizeof(destination)-1]='\0';
941
942    opts.transfercap = ast->transfercapability;
943
944    for (i=0;i<480 && !isRunning(p->callToken);i++) usleep(12000);
945
946    if(OO_TESTFLAG(p->flags, H323_DISABLEGK)) {
947       res = ooRunCall(destination, p->callToken, AST_MAX_EXTENSION, &opts);
948    } else {
949       res = ooRunCall(destination, p->callToken, AST_MAX_EXTENSION, NULL);
950    }
951
952         ast_mutex_unlock(&p->lock);
953         if (res != OO_OK) {
954                 ast_log(LOG_ERROR, "Failed to make call\n");
955                 return -1; /* ToDO: cleanup */
956         }
957         if (gH323Debug)
958                 ast_verbose("+++   ooh323_call\n");
959
960   return 0;
961 }
962
963 static int ooh323_hangup(struct ast_channel *ast)
964 {
965         struct ooh323_pvt *p = ast->tech_pvt;
966         int q931cause = AST_CAUSE_NORMAL_CLEARING;
967
968         if (gH323Debug)
969                 ast_verbose("---   ooh323_hangup\n");
970
971         if (p) {
972                 ast_mutex_lock(&p->lock);
973
974         if (ast->hangupcause) {
975                 q931cause = ast->hangupcause;
976         } else {
977                 const char *cause = pbx_builtin_getvar_helper(ast, "DIALSTATUS");
978                 if (cause) {
979                         if (!strcmp(cause, "CONGESTION")) {
980                                 q931cause = AST_CAUSE_NORMAL_CIRCUIT_CONGESTION;
981                         } else if (!strcmp(cause, "BUSY")) {
982                                 q931cause = AST_CAUSE_USER_BUSY;
983                         } else if (!strcmp(cause, "CHANISUNVAIL")) {
984                                 q931cause = AST_CAUSE_REQUESTED_CHAN_UNAVAIL;
985                         } else if (!strcmp(cause, "NOANSWER")) {
986                                 q931cause = AST_CAUSE_NO_ANSWER;
987                         } else if (!strcmp(cause, "CANCEL")) {
988                                 q931cause = AST_CAUSE_CALL_REJECTED;
989                         }
990                 }
991         }
992
993
994
995                 if (gH323Debug)
996                         ast_verbose("    hanging %s with cause: %d\n", p->username, q931cause);
997                 ast->tech_pvt = NULL; 
998                 if (!ast_test_flag(p, H323_ALREADYGONE)) {
999                         ooHangCall(p->callToken, 
1000                                 ooh323_convert_hangupcause_asteriskToH323(q931cause), q931cause);
1001                         ast_set_flag(p, H323_ALREADYGONE);
1002                         /* ast_mutex_unlock(&p->lock); */
1003                 } else 
1004                         ast_set_flag(p, H323_NEEDDESTROY);
1005                 /* detach channel here */
1006                 if (p->owner) {
1007                         p->owner->tech_pvt = NULL;
1008                         p->owner = NULL;
1009                         ast_module_unref(myself);
1010                 }
1011
1012                 ast_mutex_unlock(&p->lock);
1013                 ast_mutex_lock(&usecnt_lock);
1014                 usecnt--;
1015                 ast_mutex_unlock(&usecnt_lock);
1016
1017                 /* Notify the module monitors that use count for resource has changed */
1018                 ast_update_use_count();
1019           
1020         } else {
1021                 ast_debug(1, "No call to hangup\n" );
1022         }
1023         
1024         if (gH323Debug)
1025                 ast_verbose("+++   ooh323_hangup\n");
1026
1027   return 0;
1028 }
1029
1030 static int ooh323_answer(struct ast_channel *ast)
1031 {
1032         struct ooh323_pvt *p = ast->tech_pvt;
1033
1034         if (gH323Debug)
1035                 ast_verbose("--- ooh323_answer\n");
1036
1037         if (p) {
1038
1039                 ast_mutex_lock(&p->lock);
1040                 if (ast->_state != AST_STATE_UP) {
1041                         ast_channel_lock(ast);
1042                         ast_setstate(ast, AST_STATE_UP);
1043                         if (option_debug)
1044                                 ast_debug(1, "ooh323_answer(%s)\n", ast->name);
1045                         ast_channel_unlock(ast);
1046                         ooAnswerCall(p->callToken);
1047                 }
1048                 ast_mutex_unlock(&p->lock);
1049         }
1050
1051         if (gH323Debug)
1052                 ast_verbose("+++ ooh323_answer\n");
1053
1054   return 0;
1055 }
1056
1057 static struct ast_frame *ooh323_read(struct ast_channel *ast)
1058 {
1059         struct ast_frame *fr;
1060         static struct ast_frame null_frame = { AST_FRAME_NULL, };
1061         struct ooh323_pvt *p = ast->tech_pvt;
1062
1063         if (!p) return &null_frame;
1064
1065         ast_mutex_lock(&p->lock);
1066         if (p->rtp)
1067                 fr = ooh323_rtp_read(ast, p);
1068         else
1069                 fr = &null_frame;
1070         /* time(&p->lastrtprx); */
1071         ast_mutex_unlock(&p->lock);
1072         return fr;
1073 }
1074
1075 static int ooh323_write(struct ast_channel *ast, struct ast_frame *f)
1076 {
1077         struct ooh323_pvt *p = ast->tech_pvt;
1078         int res = 0;
1079         char buf[256];
1080
1081         if (p) {
1082                 ast_mutex_lock(&p->lock);
1083
1084                 if (f->frametype == AST_FRAME_MODEM) {
1085                         ast_debug(1, "Send UDPTL %d/%d len %d for %s\n",
1086                                 f->frametype, f->subclass.integer, f->datalen, ast->name);
1087                         if (p->udptl)
1088                                 res = ast_udptl_write(p->udptl, f);
1089                         ast_mutex_unlock(&p->lock);
1090                         return res;
1091                 }
1092
1093         
1094                 if (f->frametype == AST_FRAME_VOICE) {
1095 /* sending progress for first */
1096                         if (!ast_test_flag(p, H323_OUTGOING) && !p->progsent &&
1097                                         p->callToken) {
1098                                 ooManualProgress(p->callToken);
1099                                 p->progsent = 1;
1100                         }
1101
1102
1103                         if (!(f->subclass.codec & ast->nativeformats)) {
1104                                 if (ast->nativeformats != 0) {
1105                                         ast_log(LOG_WARNING,
1106                                                         "Asked to transmit frame type %s, while native formats is %s (read/write = %s/%s)\n",
1107                                                         ast_getformatname(f->subclass.codec),
1108                                                         ast_getformatname_multiple(buf, sizeof(buf), ast->nativeformats),
1109                                                         ast_getformatname(ast->readformat),
1110                                                         ast_getformatname(ast->writeformat));
1111
1112                                         ast_set_write_format(ast, f->subclass.codec);
1113                                 } else {
1114                                         /* ast_set_write_format(ast, f->subclass);
1115                                         ast->nativeformats = f->subclass; */
1116                                 }
1117                         ast_mutex_unlock(&p->lock);
1118                         return 0;
1119                         }
1120
1121                 if (p->rtp)
1122                         res = ast_rtp_instance_write(p->rtp, f);
1123
1124                 ast_mutex_unlock(&p->lock);
1125
1126                 } else if (f->frametype == AST_FRAME_IMAGE) {
1127                         ast_mutex_unlock(&p->lock);
1128                         return 0;
1129                 } else {
1130                         ast_log(LOG_WARNING, "Can't send %d type frames with OOH323 write\n", 
1131                                                                          f->frametype);
1132                         ast_mutex_unlock(&p->lock);
1133                         return 0;
1134                 }
1135
1136         }
1137
1138         return res;
1139 }
1140
1141 static int ooh323_indicate(struct ast_channel *ast, int condition, const void *data, size_t datalen)
1142 {
1143
1144         struct ooh323_pvt *p = (struct ooh323_pvt *) ast->tech_pvt;
1145         char *callToken = (char *)NULL;
1146
1147         if (!p) return -1;
1148
1149         ast_mutex_lock(&p->lock);
1150         callToken = (p->callToken ? strdup(p->callToken) : NULL);
1151         ast_mutex_unlock(&p->lock);
1152
1153         if (!callToken) {
1154                 if (gH323Debug)
1155                         ast_verbose("   ooh323_indicate - No callToken\n");
1156                 return -1;
1157         }
1158
1159         if (gH323Debug)
1160                 ast_verbose("----- ooh323_indicate %d on call %s\n", condition, callToken);
1161          
1162         ast_mutex_lock(&p->lock);
1163         switch (condition) {
1164         case AST_CONTROL_CONGESTION:
1165                 if (!ast_test_flag(p, H323_ALREADYGONE)) {
1166                         ooHangCall(callToken, OO_REASON_LOCAL_CONGESTED, 
1167                                                 AST_CAUSE_SWITCH_CONGESTION);
1168                         ast_set_flag(p, H323_ALREADYGONE);
1169                 }
1170                 break;
1171         case AST_CONTROL_BUSY:
1172                 if (!ast_test_flag(p, H323_ALREADYGONE)) {
1173                         ooHangCall(callToken, OO_REASON_LOCAL_BUSY, AST_CAUSE_USER_BUSY);
1174                         ast_set_flag(p, H323_ALREADYGONE);
1175                 }
1176                 break;
1177         case AST_CONTROL_HOLD:
1178                 ast_moh_start(ast, data, NULL);         
1179                 break;
1180         case AST_CONTROL_UNHOLD:
1181                 ast_moh_stop(ast);
1182                 break;
1183         case AST_CONTROL_PROGRESS:
1184                 if (ast->_state != AST_STATE_UP) {
1185                         if (!p->progsent) {
1186                                 if (gH323Debug) 
1187                                         ast_log(LOG_DEBUG,"Sending manual progress for %s, res = %d\n", callToken,
1188                                         ooManualProgress(callToken));   
1189                                 else
1190                                         ooManualProgress(callToken);
1191                                 p->progsent = 1;
1192                         }
1193                 }
1194             break;
1195       case AST_CONTROL_RINGING:
1196             if (ast->_state == AST_STATE_RING || ast->_state == AST_STATE_RINGING) {
1197                 if (gH323Debug) 
1198                         ast_log(LOG_DEBUG,"Sending manual ringback for %s, res = %d\n", 
1199                         callToken,
1200                         ooManualRingback(callToken));
1201                  else
1202                         ooManualRingback(callToken);
1203             }
1204          break;
1205       case AST_CONTROL_SRCUPDATE:
1206                 ast_rtp_instance_new_source(p->rtp);
1207                 break;
1208
1209       case AST_CONTROL_T38_PARAMETERS:
1210                 if (p->t38support != T38_ENABLED) {
1211                         struct ast_control_t38_parameters parameters = { .request_response = 0 };
1212                         parameters.request_response = AST_T38_REFUSED;
1213                         ast_queue_control_data(ast, AST_CONTROL_T38_PARAMETERS,
1214                                                  &parameters, sizeof(parameters));
1215                         break;
1216                 }
1217                 if (datalen != sizeof(struct ast_control_t38_parameters)) {
1218                         ast_log(LOG_ERROR, "Invalid datalen for AST_CONTROL_T38. "
1219                                            "Expected %d, got %d\n",
1220                                 (int)sizeof(enum ast_control_t38), (int)datalen);
1221                 } else {
1222                         const struct ast_control_t38_parameters *parameters = data;
1223                         enum ast_control_t38 message = parameters->request_response;
1224                         switch (message) {
1225
1226                         case AST_T38_REQUEST_NEGOTIATE:
1227
1228                                 if (!p->chmodepend && !p->faxmode) {
1229                                         ooRequestChangeMode(p->callToken, 1);
1230                                         p->chmodepend = 1;
1231                                 }
1232                                 break;
1233
1234                         case AST_T38_REQUEST_TERMINATE:
1235
1236                                 if (!p->chmodepend && p->faxmode) {
1237                                         ooRequestChangeMode(p->callToken, 0);
1238                                         p->chmodepend = 1;
1239                                 }
1240                                 break;
1241
1242
1243                         default:
1244                                 ;
1245
1246                         }
1247
1248                 }
1249                 break;
1250       case AST_CONTROL_PROCEEDING:
1251         case -1:
1252                 break;
1253         default:
1254                 ast_log(LOG_WARNING, "Don't know how to indicate condition %d on %s\n",
1255                                                                         condition, callToken);
1256         }
1257
1258         ast_mutex_unlock(&p->lock);
1259
1260         if (gH323Debug)
1261                 ast_verbose("++++  ooh323_indicate %d on %s\n", condition, callToken);
1262
1263         free(callToken);
1264         return -1;
1265 }
1266
1267 static int ooh323_queryoption(struct ast_channel *ast, int option, void *data, int *datalen)
1268 {
1269
1270         struct ooh323_pvt *p = (struct ooh323_pvt *) ast->tech_pvt;
1271         int res = -1;
1272         enum ast_t38_state state = T38_STATE_UNAVAILABLE;
1273         char* cp;
1274
1275         if (!p) return -1;
1276
1277         ast_mutex_lock(&p->lock);
1278
1279         if (gH323Debug)
1280                 ast_verbose("----- ooh323_queryoption %d on channel %s\n", option, ast->name);
1281          
1282         switch (option) {
1283
1284                 case AST_OPTION_T38_STATE:
1285
1286                         if (*datalen != sizeof(enum ast_t38_state)) {
1287                                 ast_log(LOG_ERROR, "Invalid datalen for AST_OPTION_T38_STATE option."
1288                                 " Expected %d, got %d\n", (int)sizeof(enum ast_t38_state), *datalen);
1289                                 break;
1290                         }
1291                         if (p->t38support != T38_DISABLED)
1292                                 state = T38_STATE_UNKNOWN;
1293                         if (p->faxmode)
1294                                 state = (p->chmodepend) ? T38_STATE_UNKNOWN : T38_STATE_NEGOTIATED;
1295                         else if (p->chmodepend)
1296                                 state = T38_STATE_NEGOTIATING;
1297
1298
1299                         *((enum ast_t38_state *) data) = state;
1300                         res = 0;
1301                         break;
1302
1303
1304                 case AST_OPTION_DIGIT_DETECT:
1305
1306                         cp = (char *) data;
1307                         *cp = p->vad ? 1 : 0;
1308                         ast_debug(1, "Reporting digit detection %sabled on %s\n",
1309                                                          *cp ? "en" : "dis", ast->name);
1310
1311                         res = 0;
1312                         break;
1313
1314                 default:        ;
1315
1316         }
1317
1318         if (gH323Debug)
1319                 ast_verbose("+++++ ooh323_queryoption %d on channel %s\n", option, ast->name);
1320          
1321         ast_mutex_unlock(&p->lock);
1322
1323         return res;
1324 }
1325
1326
1327
1328 static int ooh323_fixup(struct ast_channel *oldchan, struct ast_channel *newchan)
1329 {
1330         struct ooh323_pvt *p = newchan->tech_pvt;
1331
1332         if (!p) return -1;
1333
1334         if (gH323Debug)
1335                 ast_verbose("--- ooh323c ooh323_fixup\n");
1336
1337         ast_mutex_lock(&p->lock);
1338         if (p->owner != oldchan) {
1339                 ast_log(LOG_WARNING, "Old channel wasn't %p but was %p\n", oldchan, p->owner);
1340                 ast_mutex_unlock(&p->lock);
1341                 return -1;
1342         }
1343
1344         if (p->owner == oldchan) {
1345                 p->owner = newchan;
1346         } else {
1347                 p->owner = oldchan;
1348         }
1349
1350         ast_mutex_unlock(&p->lock);
1351
1352         if (gH323Debug)
1353                 ast_verbose("+++ ooh323c ooh323_fixup \n");
1354
1355         return 0;
1356 }
1357
1358
1359 void ooh323_set_write_format(ooCallData *call, int fmt, int txframes)
1360 {
1361         struct ooh323_pvt *p = NULL;
1362         char formats[FORMAT_STRING_SIZE];
1363
1364         if (gH323Debug)
1365                 ast_verbose("---   ooh323_update_writeformat %s/%d\n", 
1366                                 ast_getformatname_multiple(formats,FORMAT_STRING_SIZE, fmt), txframes);
1367         
1368         p = find_call(call);
1369         if (!p) {
1370                 ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
1371                 return;
1372         }
1373
1374         ast_mutex_lock(&p->lock);
1375
1376         p->writeformat = fmt;
1377
1378         if (p->owner) {
1379                 while (p->owner && ast_channel_trylock(p->owner)) {
1380                         ast_debug(1,"Failed to grab lock, trying again\n");
1381                         DEADLOCK_AVOIDANCE(&p->lock);
1382                 }
1383                 if (!p->owner) {
1384                         ast_mutex_unlock(&p->lock);
1385                         ast_log(LOG_ERROR, "Channel has no owner\n");
1386                         return;
1387                 }
1388                 if (gH323Debug)
1389                         ast_verbose("Writeformat before update %s/%s\n", 
1390                           ast_getformatname_multiple(formats,FORMAT_STRING_SIZE, p->owner->writeformat),
1391                           ast_getformatname_multiple(formats,FORMAT_STRING_SIZE, p->owner->nativeformats));
1392                 if (txframes)
1393                         ast_codec_pref_setsize(&p->prefs, fmt, txframes);
1394                 ast_rtp_codecs_packetization_set(ast_rtp_instance_get_codecs(p->rtp), p->rtp, &p->prefs);
1395                 if (p->dtmfmode & H323_DTMF_RFC2833 && p->dtmfcodec) {
1396                         ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
1397                                  p->rtp, p->dtmfcodec, "audio", "telephone-event", 0);
1398                 }
1399                 if (p->dtmfmode & H323_DTMF_CISCO && p->dtmfcodec) {
1400                         ast_rtp_codecs_payloads_set_rtpmap_type(ast_rtp_instance_get_codecs(p->rtp),
1401                                  p->rtp, p->dtmfcodec, "audio", "cisco-telephone-event", 0);
1402                 }
1403
1404                 p->owner->nativeformats = fmt;
1405                 ast_set_write_format(p->owner, p->owner->writeformat);
1406                 ast_set_read_format(p->owner, p->owner->readformat);
1407                 ast_channel_unlock(p->owner);
1408         } else
1409                 ast_log(LOG_ERROR, "No owner found\n");
1410
1411
1412         ast_mutex_unlock(&p->lock);
1413
1414         if (gH323Debug)
1415                 ast_verbose("+++   ooh323_update_writeformat\n");
1416 }
1417
1418 void ooh323_set_read_format(ooCallData *call, int fmt)
1419 {
1420         struct ooh323_pvt *p = NULL;
1421         char formats[FORMAT_STRING_SIZE];
1422
1423         if (gH323Debug)
1424                 ast_verbose("---   ooh323_update_readformat %s\n", 
1425                                 ast_getformatname_multiple(formats,FORMAT_STRING_SIZE, fmt));
1426         
1427         p = find_call(call);
1428         if (!p) {
1429                 ast_log(LOG_ERROR, "No matching call found for %s\n", call->callToken);
1430                 return;
1431         }
1432
1433         ast_mutex_lock(&p->lock);
1434
1435         p->readformat = fmt;
1436
1437
1438         if (p->owner) {
1439                 while (p->owner && ast_channel_trylock(p->owner)) {
1440                         ast_debug(1,"Failed to grab lock, trying again\n");
1441                         DEADLOCK_AVOIDANCE(&p->lock);
1442                 }
1443                 if (!p->owner) {
1444                         ast_mutex_unlock(&p->lock);
1445                         ast_log(LOG_ERROR, "Channel has no owner\n");
1446                         return;
1447                 }
1448
1449                 if (gH323Debug)
1450                         ast_verbose("Readformat before update %s\n", 
1451                                   ast_getformatname_multiple(formats,FORMAT_STRING_SIZE, p->owner->readformat));
1452                 p->owner->nativeformats = fmt;
1453                 ast_set_read_format(p->owner, p->owner->readformat);
1454                 ast_channel_unlock(p->owner);
1455         } else
1456                 ast_log(LOG_ERROR, "No owner found\n");
1457
1458         ast_mutex_unlock(&p->lock);
1459
1460         if (gH323Debug)
1461                 ast_verbose("+++   ooh323_update_readformat\n");
1462 }
1463
1464
1465 int onAlerting(ooCallData *call)
1466 {
1467         struct ooh323_pvt *p = NULL;
1468         struct ast_channel *c = NULL;
1469
1470         if (gH323Debug)
1471                 ast_verbose("--- onAlerting %s\n", call->callToken);
1472
1473         p = find_call(call);
1474
1475         if(!p) {
1476                 ast_log(LOG_ERROR, "No matching call found\n");
1477                 return -1;
1478         }  
1479         ast_mutex_lock(&p->lock);
1480         if (!p->owner) {
1481                 ast_mutex_unlock(&p->lock);
1482                 ast_log(LOG_ERROR, "Channel has no owner\n");
1483                 return 0;
1484         }
1485         while (p->owner && ast_channel_trylock(p->owner)) {
1486                 ast_debug(1,"Failed to grab lock, trying again\n");
1487                 DEADLOCK_AVOIDANCE(&p->lock);
1488         }
1489         if (!p->owner) {
1490                 ast_mutex_unlock(&p->lock);
1491                 ast_log(LOG_ERROR, "Channel has no owner\n");
1492                 return 0;
1493         }
1494         c = p->owner;
1495         if (c->_state != AST_STATE_UP)
1496                 ast_setstate(c, AST_STATE_RINGING);
1497
1498         ast_queue_control(c, AST_CONTROL_RINGING);
1499         ast_channel_unlock(c);
1500         ast_mutex_unlock(&p->lock);
1501
1502         if (gH323Debug)
1503                 ast_verbose("+++ onAlerting %s\n", call->callToken);
1504
1505         return OO_OK;
1506 }
1507
1508 int onProgress(ooCallData *call)
1509 {
1510         struct ooh323_pvt *p = NULL;
1511         struct ast_channel *c = NULL;
1512
1513         if (gH323Debug)
1514                 ast_verbose("--- onProgress %s\n", call->callToken);
1515
1516         p = find_call(call);
1517
1518         if(!p) {
1519                 ast_log(LOG_ERROR, "No matching call found\n");
1520                 return -1;
1521         }  
1522         ast_mutex_lock(&p->lock);
1523         if (!p->owner) {
1524                 ast_mutex_unlock(&p->lock);
1525                 ast_log(LOG_ERROR, "Channel has no owner\n");
1526                 return 0;
1527         }
1528         while (p->owner && ast_channel_trylock(p->owner)) {
1529                 ast_debug(1,"Failed to grab lock, trying again\n");
1530                 DEADLOCK_AVOIDANCE(&p->lock);
1531         }
1532         if (!p->owner) {
1533                 ast_mutex_unlock(&p->lock);
1534                 ast_log(LOG_ERROR, "Channel has no owner\n");
1535                 return 0;
1536         }
1537         c = p->owner;
1538         if (c->_state != AST_STATE_UP)
1539                 ast_setstate(c, AST_STATE_RINGING);
1540
1541         ast_queue_control(c, AST_CONTROL_PROGRESS);
1542         ast_channel_unlock(c);
1543         ast_mutex_unlock(&p->lock);
1544
1545         if (gH323Debug)
1546                 ast_verbose("+++ onProgress %s\n", call->callToken);
1547
1548         return OO_OK;
1549 }
1550
1551 /**
1552   * Callback for sending digits from H.323 up to asterisk
1553   *
1554   */
1555 int ooh323_onReceivedDigit(OOH323CallData *call, const char *digit)
1556 {
1557         struct ooh323_pvt *p = NULL;
1558         struct ast_frame f;
1559         int res;
1560
1561         ast_debug(1, "Received Digit: %c\n", digit[0]);
1562         p = find_call(call);
1563         if (!p) {
1564                 ast_log(LOG_ERROR, "Failed to find a matching call.\n");
1565                 return -1;
1566         }
1567         if (!p->owner) {
1568                 ast_log(LOG_ERROR, "Channel has no owner\n");
1569                 return -1;
1570         }
1571         ast_mutex_lock(&p->lock);
1572         memset(&f, 0, sizeof(f));
1573         f.frametype = AST_FRAME_DTMF;
1574         f.subclass.integer = digit[0];
1575         f.datalen = 0;
1576         f.samples = 800;
1577         f.offset = 0;
1578         f.data.ptr = NULL;
1579         f.mallocd = 0;
1580         f.src = "SEND_DIGIT";
1581
1582         while (p->owner && ast_channel_trylock(p->owner)) {
1583                 ast_debug(1,"Failed to grab lock, trying again\n");
1584                 DEADLOCK_AVOIDANCE(&p->lock);
1585         }
1586         if (!p->owner) {
1587                 ast_mutex_unlock(&p->lock);
1588                 ast_log(LOG_ERROR, "Channel has no owner\n");
1589                 return 0;
1590         }
1591         res = ast_queue_frame(p->owner, &f);
1592         ast_channel_unlock(p->owner);
1593         ast_mutex_unlock(&p->lock);
1594         return res;
1595 }
1596
1597 int ooh323_onReceivedSetup(ooCallData *call, Q931Message *pmsg)
1598 {
1599         struct ooh323_pvt *p = NULL;
1600         struct ooh323_user *user = NULL;
1601         struct ast_channel *c = NULL;
1602         ooAliases *alias = NULL;
1603         char *at = NULL;
1604         char number [OO_MAX_NUMBER_LENGTH];
1605
1606         if (gH323Debug)
1607                 ast_verbose("---   ooh323_onReceivedSetup %s\n", call->callToken);
1608
1609
1610         if (!(p = ooh323_alloc(call->callReference, call->callToken))) {
1611                 ast_log(LOG_ERROR, "Failed to create a new call.\n");
1612                 return -1;
1613         }
1614         ast_mutex_lock(&p->lock);
1615         ast_clear_flag(p, H323_OUTGOING);
1616   
1617
1618         if (call->remoteDisplayName) {
1619                 p->callerid_name = strdup(call->remoteDisplayName);
1620         }
1621
1622         if (ooCallGetCallingPartyNumber(call, number, OO_MAX_NUMBER_LENGTH) == OO_OK) {
1623                 p->callerid_num = strdup(number);
1624         }
1625
1626         if (call->remoteAliases) {
1627                 for (alias = call->remoteAliases; alias; alias = alias->next) {
1628                         if (alias->type == T_H225AliasAddress_h323_ID) {
1629                                 if (!p->callerid_name) {
1630                                         p->callerid_name = strdup(alias->value);
1631                                 }
1632                                 ast_copy_string(p->caller_h323id, alias->value, sizeof(p->caller_h323id));
1633                                 }
1634          else if(alias->type == T_H225AliasAddress_dialedDigits)
1635          {
1636             if(!p->callerid_num)
1637                p->callerid_num = strdup(alias->value);
1638                                 ast_copy_string(p->caller_dialedDigits, alias->value, 
1639                                                                                                                         sizeof(p->caller_dialedDigits));
1640          }
1641          else if(alias->type == T_H225AliasAddress_email_ID)
1642          {
1643                                 ast_copy_string(p->caller_email, alias->value, sizeof(p->caller_email));
1644          }
1645          else if(alias->type == T_H225AliasAddress_url_ID)
1646          {
1647                                 ast_copy_string(p->caller_url, alias->value, sizeof(p->caller_url));
1648                         }
1649                 }
1650         }
1651
1652         number[0] = '\0';
1653         if(ooCallGetCalledPartyNumber(call, number, OO_MAX_NUMBER_LENGTH)== OO_OK) {
1654                 strncpy(p->exten, number, sizeof(p->exten)-1);
1655         } else {
1656                 update_our_aliases(call, p);
1657                 if (!ast_strlen_zero(p->callee_dialedDigits)) {
1658                         ast_copy_string(p->exten, p->callee_dialedDigits, sizeof(p->exten));
1659                 } else if(!ast_strlen_zero(p->callee_h323id)) {
1660                         ast_copy_string(p->exten, p->callee_h323id, sizeof(p->exten));
1661                 } else if(!ast_strlen_zero(p->callee_email)) {
1662                         ast_copy_string(p->exten, p->callee_email, sizeof(p->exten));
1663                         if ((at = strchr(p->exten, '@'))) {
1664                                 *at = '\0';
1665                         }
1666                 }
1667         }
1668
1669         /* if no extension found, set to default 's' */
1670         if (ast_strlen_zero(p->exten)) {
1671                 p->exten[0]='s';
1672                 p->exten[1]='\0';
1673         }
1674
1675         user = find_user(p->callerid_name, call->remoteIP);
1676         if(user && (user->incominglimit == 0 || user->inUse < user->incominglimit)) {
1677                 ast_mutex_lock(&user->lock);
1678                 p->username = strdup(user->name);
1679                 p->neighbor.user = user->mUseIP ? ast_strdup(user->mIP) :
1680                                                   ast_strdup(user->name);
1681                 ast_copy_string(p->context, user->context, sizeof(p->context));
1682                 ast_copy_string(p->accountcode, user->accountcode, sizeof(p->accountcode));
1683                 p->amaflags = user->amaflags;
1684                 p->capability = user->capability;
1685                 memcpy(&p->prefs, &user->prefs, sizeof(struct ast_codec_pref));
1686                 p->dtmfmode |= user->dtmfmode;
1687                 p->dtmfcodec = user->dtmfcodec;
1688                 p->t38support = user->t38support;
1689                 p->rtptimeout = user->rtptimeout;
1690                 if (user->rtpmask && user->rtpmaskstr[0]) {
1691                         p->rtpmask = user->rtpmask;
1692                         ast_copy_string(p->rtpmaskstr, user->rtpmaskstr, 
1693                                                          sizeof(p->rtpmaskstr));
1694                 }
1695                 if (user->incominglimit) user->inUse++;
1696                 ast_mutex_unlock(&user->lock);
1697         } else {
1698          if (!OO_TESTFLAG(p->flags,H323_DISABLEGK)) {
1699                 p->username = strdup(call->remoteIP);
1700         } else {
1701           ast_mutex_unlock(&p->lock);
1702           ast_log(LOG_ERROR, "Unacceptable ip %s\n", call->remoteIP);
1703           if (!user) 
1704            ooHangCall(call->callToken, ooh323_convert_hangupcause_asteriskToH323(AST_CAUSE_CALL_REJECTED), AST_CAUSE_CALL_REJECTED);
1705           else
1706            ooHangCall(call->callToken, ooh323_convert_hangupcause_asteriskToH323(AST_CAUSE_NORMAL_CIRCUIT_CONGESTION), AST_CAUSE_NORMAL_CIRCUIT_CONGESTION);
1707           ast_set_flag(p, H323_NEEDDESTROY);
1708           return -1;
1709          }
1710         }
1711
1712         ooh323c_set_capability_for_call(call, &p->prefs, p->capability, p->dtmfmode, p->dtmfcodec,
1713                                          p->t38support);
1714         configure_local_rtp(p, call);
1715
1716 /* Incoming call */
1717         c = ooh323_new(p, AST_STATE_RING, p->username, 0, NULL);
1718         if(!c) {
1719         ast_mutex_unlock(&p->lock);
1720         ast_log(LOG_ERROR, "Could not create ast_channel\n");
1721          return -1;
1722   }
1723         ast_mutex_unlock(&p->lock);
1724
1725         if (gH323Debug)
1726                 ast_verbose("+++   ooh323_onReceivedSetup - Determined context %s, "
1727                                                 "extension %s\n", p->context, p->exten);
1728
1729         return OO_OK;
1730 }
1731
1732
1733
1734 int onOutgoingCall(ooCallData *call)
1735 {
1736         struct ooh323_pvt *p = NULL;
1737         int i = 0;
1738
1739         if (gH323Debug)
1740                 ast_verbose("---   onOutgoingCall %lx: %s\n", (long unsigned int) call, call->callToken);
1741
1742         if (!strcmp(call->callType, "outgoing")) {
1743                 p = find_call(call);
1744                 if (!p) {
1745                         ast_log(LOG_ERROR, "Failed to find a matching call.\n");
1746                         return -1;
1747                 }
1748                 ast_mutex_lock(&p->lock);
1749
1750                 if (!ast_strlen_zero(p->callerid_name)) {
1751                         ooCallSetCallerId(call, p->callerid_name);
1752                 }
1753                 if (!ast_strlen_zero(p->callerid_num)) {
1754                         i = 0;
1755                         while (*(p->callerid_num + i) != '\0') {
1756                                 if(!isdigit(*(p->callerid_num+i))) { break; }
1757                                 i++;
1758                         }
1759                         if(*(p->callerid_num+i) == '\0')
1760                                 ooCallSetCallingPartyNumber(call, p->callerid_num);
1761                         else {
1762                                 if(!p->callerid_name)
1763                                         ooCallSetCallerId(call, p->callerid_num);
1764                         }
1765                 }
1766                 
1767                 if (!ast_strlen_zero(p->caller_h323id))
1768                         ooCallAddAliasH323ID(call, p->caller_h323id);
1769
1770                 if (!ast_strlen_zero(p->caller_dialedDigits)) {
1771                         if (gH323Debug) {
1772                                 ast_verbose("Setting dialed digits %s\n", p->caller_dialedDigits);
1773                         }
1774                         ooCallAddAliasDialedDigits(call, p->caller_dialedDigits);
1775                 } else if (!ast_strlen_zero(p->callerid_num)) {
1776                         if (ooIsDailedDigit(p->callerid_num)) {
1777                                 if (gH323Debug) {
1778                                         ast_verbose("setting callid number %s\n", p->callerid_num);
1779                                 }
1780                                 ooCallAddAliasDialedDigits(call, p->callerid_num);
1781                         } else if (ast_strlen_zero(p->caller_h323id)) {
1782                                 ooCallAddAliasH323ID(call, p->callerid_num);
1783                         }
1784                 }
1785                 if (p->rtpmask && p->rtpmaskstr[0]) {
1786                         call->rtpMask = p->rtpmask;
1787                         ast_mutex_lock(&call->rtpMask->lock);
1788                         call->rtpMask->inuse++;
1789                         ast_mutex_unlock(&call->rtpMask->lock);
1790                         ast_copy_string(call->rtpMaskStr, p->rtpmaskstr, sizeof(call->rtpMaskStr));
1791                 }
1792
1793                 ast_mutex_unlock(&p->lock);
1794         }
1795
1796         if (gH323Debug)
1797                 ast_verbose("+++   onOutgoingCall %s\n", call->callToken);
1798         return OO_OK;
1799 }
1800
1801
1802 int onNewCallCreated(ooCallData *call)
1803 {
1804         struct ooh323_pvt *p = NULL;
1805         int i = 0;
1806
1807         if (gH323Debug)
1808                 ast_verbose("---   onNewCallCreated %lx: %s\n", (long unsigned int) call, call->callToken);
1809
1810         ast_mutex_lock(&call->Lock);
1811         if (ooh323c_start_call_thread(call)) {
1812                 ast_log(LOG_ERROR,"Failed to create call thread.\n");
1813                 ast_mutex_unlock(&call->Lock);
1814                 return -1;
1815         }
1816
1817         if (!strcmp(call->callType, "outgoing")) {
1818                 p = find_call(call);
1819                 if (!p) {
1820                         ast_log(LOG_ERROR, "Failed to find a matching call.\n");
1821                         ast_mutex_unlock(&call->Lock);
1822                         return -1;
1823                 }
1824                 ast_mutex_lock(&p->lock);
1825
1826                 if (p->callerid_name) {
1827                         ooCallSetCallerId(call, p->callerid_name);
1828                 }
1829                 if (p->callerid_num) {
1830                         i = 0;
1831                         while (*(p->callerid_num + i) != '\0') {
1832                                 if(!isdigit(*(p->callerid_num+i))) { break; }
1833                                 i++;
1834                         }
1835                         if(*(p->callerid_num+i) == '\0')
1836                                 ooCallSetCallingPartyNumber(call, p->callerid_num);
1837                         else {
1838                                 if(!p->callerid_name)
1839                                         ooCallSetCallerId(call, p->callerid_num);
1840                         }
1841                 }
1842                 
1843                 if (!ast_strlen_zero(p->caller_h323id))
1844                         ooCallAddAliasH323ID(call, p->caller_h323id);
1845
1846                 if (!ast_strlen_zero(p->caller_dialedDigits)) {
1847                         if (gH323Debug) {
1848                                 ast_verbose("Setting dialed digits %s\n", p->caller_dialedDigits);
1849                         }
1850                         ooCallAddAliasDialedDigits(call, p->caller_dialedDigits);
1851                 } else if (p->callerid_num) {
1852                         if (ooIsDailedDigit(p->callerid_num)) {
1853                                 if (gH323Debug) {
1854                                         ast_verbose("setting callid number %s\n", p->callerid_num);
1855                                 }
1856                                 ooCallAddAliasDialedDigits(call, p->callerid_num);
1857                         } else if (ast_strlen_zero(p->caller_h323id)) {
1858                                 ooCallAddAliasH323ID(call, p->callerid_num);
1859                         }
1860                 }
1861   
1862
1863                 if (!ast_strlen_zero(p->exten))  {
1864                         if (ooIsDailedDigit(p->exten)) {
1865                                 ooCallSetCalledPartyNumber(call, p->exten);
1866                                 ooCallAddRemoteAliasDialedDigits(call, p->exten);
1867                         } else {
1868                           ooCallAddRemoteAliasH323ID(call, p->exten);
1869                         }
1870                 }
1871
1872                 if (gH323Debug) {
1873                         char prefsBuf[256];
1874                         ast_codec_pref_string(&p->prefs, prefsBuf, sizeof(prefsBuf));
1875                         ast_verbose(" Outgoing call %s(%s) - Codec prefs - %s\n", 
1876                                 p->username?p->username:"NULL", call->callToken, prefsBuf);
1877                 }
1878
1879                 ooh323c_set_capability_for_call(call, &p->prefs, p->capability, 
1880                                      p->dtmfmode, p->dtmfcodec, p->t38support);
1881
1882                 configure_local_rtp(p, call);
1883                 ast_mutex_unlock(&p->lock);
1884         }
1885
1886         ast_mutex_unlock(&call->Lock);
1887         if (gH323Debug)
1888                 ast_verbose("+++   onNewCallCreated %s\n", call->callToken);
1889         return OO_OK;
1890 }
1891
1892 int onCallEstablished(ooCallData *call)
1893 {
1894         struct ooh323_pvt *p = NULL;
1895
1896         if (gH323Debug)
1897                 ast_verbose("---   onCallEstablished %s\n", call->callToken);
1898
1899
1900         if (!(p = find_call(call))) {
1901                 ast_log(LOG_ERROR, "Failed to find a matching call.\n");
1902                 return -1;
1903         }
1904
1905         if(ast_test_flag(p, H323_OUTGOING)) {
1906                 ast_mutex_lock(&p->lock);
1907                 if (!p->owner) {
1908                         ast_mutex_unlock(&p->lock);
1909                         ast_log(LOG_ERROR, "Channel has no owner\n");
1910                         return -1;
1911                 }
1912         
1913                 while (p->owner && ast_channel_trylock(p->owner)) {
1914                         ast_debug(1,"Failed to grab lock, trying again\n");
1915                         DEADLOCK_AVOIDANCE(&p->lock);
1916                 }
1917                 if (p->owner) {
1918                         struct ast_channel* c = p->owner;
1919                         ast_queue_control(c, AST_CONTROL_ANSWER);
1920                         ast_channel_unlock(p->owner);
1921                         manager_event(EVENT_FLAG_SYSTEM,"ChannelUpdate","Channel: %s\r\nChanneltype: %s\r\n"
1922                                 "CallRef: %d\r\n", c->name, "OOH323", p->call_reference);
1923                 }
1924                 ast_mutex_unlock(&p->lock);
1925
1926         }
1927
1928         if (gH323Debug)
1929                 ast_verbose("+++   onCallEstablished %s\n", call->callToken);
1930
1931         return OO_OK;
1932 }
1933
1934 int onCallCleared(ooCallData *call)
1935 {
1936         struct ooh323_pvt *p = NULL;
1937         int ownerLock = 0;
1938
1939         if (gH323Debug)
1940                 ast_verbose("---   onCallCleared %s \n", call->callToken);
1941
1942
1943    if ((p = find_call(call))) {
1944         ast_mutex_lock(&p->lock);
1945   
1946         while (p->owner) {
1947                 if (ast_channel_trylock(p->owner)) {
1948                         ooTrace(OOTRCLVLINFO, "Failed to grab lock, trying again\n");
1949                         ast_log(LOG_DEBUG,"Failed to grab lock, trying again\n");
1950                         DEADLOCK_AVOIDANCE(&p->lock);
1951                 } else {
1952                         ownerLock = 1; break;
1953                 }
1954         }
1955
1956         if (ownerLock) {
1957                 if (!ast_test_flag(p, H323_ALREADYGONE)) { 
1958
1959                         ast_set_flag(p, H323_ALREADYGONE);
1960                         p->owner->hangupcause = call->q931cause;
1961                         p->owner->_softhangup |= AST_SOFTHANGUP_DEV;
1962                         ast_queue_hangup_with_cause(p->owner,call->q931cause);
1963                 }
1964         }
1965
1966         if(p->owner) {
1967                 p->owner->tech_pvt = NULL;
1968                 ast_channel_unlock(p->owner);
1969                 p->owner = NULL;
1970                 ast_module_unref(myself);
1971         }
1972
1973         ast_set_flag(p, H323_NEEDDESTROY);
1974
1975         ooh323c_stop_call_thread(call);
1976
1977         ast_mutex_unlock(&p->lock);
1978         ast_mutex_lock(&usecnt_lock);
1979         usecnt--;
1980         ast_mutex_unlock(&usecnt_lock);
1981
1982     }
1983
1984         if (gH323Debug)
1985                 ast_verbose("+++   onCallCleared\n");
1986
1987         return OO_OK;
1988 }
1989
1990 /* static void ooh323_delete_user(struct ooh323_user *user)
1991 {
1992         struct ooh323_user *prev = NULL, *cur = NULL;
1993
1994         if (gH323Debug)
1995                 ast_verbose("---   ooh323_delete_user\n");
1996
1997         if (user) {     
1998                 cur = userl.users;
1999                 ast_mutex_lock(&userl.lock);
2000                 while (cur) {
2001                         if (cur == user) break;
2002                         prev = cur;
2003                         cur = cur->next;
2004                 }
2005
2006                 if (cur) {
2007                         if (prev)
2008                                 prev->next = cur->next;
2009                         else
2010                                 userl.users = cur->next;
2011                 }
2012                 ast_mutex_unlock(&userl.lock);
2013
2014                 free(user);
2015         }  
2016
2017         if (gH323Debug)
2018                 ast_verbose("+++   ooh323_delete_user\n");
2019
2020 } */
2021
2022 void ooh323_delete_peer(struct ooh323_peer *peer)
2023 {
2024         struct ooh323_peer *prev = NULL, *cur = NULL;
2025
2026         if (gH323Debug)
2027                 ast_verbose("---   ooh323_delete_peer\n");
2028
2029         if (peer) {     
2030       cur = peerl.peers;
2031                 ast_mutex_lock(&peerl.lock);
2032       while(cur) {
2033          if(cur==peer) break;
2034          prev = cur;
2035          cur = cur->next;
2036                 }
2037
2038                 if (cur) {
2039          if(prev)
2040                                 prev->next = cur->next;
2041          else
2042                                 peerl.peers = cur->next;
2043                         }
2044                 ast_mutex_unlock(&peerl.lock);
2045
2046       if(peer->h323id)   free(peer->h323id);
2047       if(peer->email)    free(peer->email);
2048       if(peer->url)      free(peer->url);
2049       if(peer->e164)     free(peer->e164);
2050
2051                 free(peer);
2052         }  
2053
2054         if (gH323Debug)
2055                 ast_verbose("+++   ooh323_delete_peer\n");
2056
2057 }
2058
2059
2060
2061 static struct ooh323_user *build_user(const char *name, struct ast_variable *v)
2062 {
2063         struct ooh323_user *user = NULL;
2064
2065         if (gH323Debug)
2066                 ast_verbose("---   build_user\n");
2067
2068         user = ast_calloc(1,sizeof(struct ooh323_user));
2069         if (user) {
2070                 memset(user, 0, sizeof(struct ooh323_user));
2071                 ast_mutex_init(&user->lock);
2072                 ast_copy_string(user->name, name, sizeof(user->name));
2073                 user->capability = gCapability;
2074                 memcpy(&user->prefs, &gPrefs, sizeof(user->prefs));
2075                 user->rtptimeout = gRTPTimeout;
2076                 user->dtmfmode = gDTMFMode;
2077                 user->dtmfcodec = gDTMFCodec;
2078                 user->t38support = gT38Support;
2079                 /* set default context */
2080                 ast_copy_string(user->context, gContext, sizeof(user->context));
2081                 ast_copy_string(user->accountcode, gAccountcode, sizeof(user->accountcode));
2082                 user->amaflags = gAMAFLAGS;
2083
2084                 while (v) {
2085                         if (!strcasecmp(v->name, "context")) {
2086                                 ast_copy_string(user->context, v->value, sizeof(user->context));
2087                         } else if (!strcasecmp(v->name, "incominglimit")) {
2088                                 user->incominglimit = atoi(v->value);
2089                                 if (user->incominglimit < 0)
2090                                         user->incominglimit = 0;
2091                         } else if (!strcasecmp(v->name, "accountcode")) {
2092                                 strncpy(user->accountcode, v->value, 
2093                                                 sizeof(user->accountcode)-1);
2094                         } else if (!strcasecmp(v->name, "rtptimeout")) {
2095                                 user->rtptimeout = atoi(v->value);
2096                                 if (user->rtptimeout < 0)
2097                                         user->rtptimeout = gRTPTimeout;
2098                         } else if (!strcasecmp(v->name, "rtpmask")) {
2099                                 if ((user->rtpmask = malloc(sizeof(struct OOH323Regex))) &&
2100                                         (regcomp(&user->rtpmask->regex, v->value, REG_EXTENDED) 
2101                                                                                         == 0)) {
2102                                         ast_mutex_init(&user->rtpmask->lock);
2103                                         user->rtpmask->inuse = 1;
2104                                         ast_copy_string(user->rtpmaskstr, v->value, 
2105                                                                 sizeof(user->rtpmaskstr));
2106                                 } else user->rtpmask = NULL;
2107                         } else if (!strcasecmp(v->name, "disallow")) {
2108                                 ast_parse_allow_disallow(&user->prefs, 
2109                                         &user->capability,  v->value, 0);
2110                         } else if (!strcasecmp(v->name, "allow")) {
2111                                 const char* tcodecs = v->value;
2112                                 if (!strcasecmp(v->value, "all")) {
2113                                         tcodecs = "ulaw,alaw,g729,g723,gsm";
2114                                 }
2115                                 ast_parse_allow_disallow(&user->prefs,
2116                                          &user->capability,  tcodecs, 1);
2117                         } else if (!strcasecmp(v->name, "amaflags")) {
2118                                 user->amaflags = ast_cdr_amaflags2int(v->value);
2119                         } else if (!strcasecmp(v->name, "ip")) {
2120                                 strncpy(user->mIP, v->value, sizeof(user->mIP)-1);
2121                                 user->mUseIP = 1;
2122                         } else if (!strcasecmp(v->name, "host")) {
2123                                 strncpy(user->mIP, v->value, sizeof(user->mIP)-1);
2124                                 user->mUseIP = 1;
2125                         } else if (!strcasecmp(v->name, "dtmfmode")) {
2126                                 if (!strcasecmp(v->value, "rfc2833"))
2127                                         user->dtmfmode = H323_DTMF_RFC2833;
2128                                 if (!strcasecmp(v->value, "cisco"))
2129                                         user->dtmfmode = H323_DTMF_CISCO;
2130                                 else if (!strcasecmp(v->value, "q931keypad"))
2131                                         user->dtmfmode = H323_DTMF_Q931;
2132                                 else if (!strcasecmp(v->value, "h245alphanumeric"))
2133                                         user->dtmfmode = H323_DTMF_H245ALPHANUMERIC;
2134                                 else if (!strcasecmp(v->value, "h245signal"))
2135                                         user->dtmfmode = H323_DTMF_H245SIGNAL;
2136                                 else if (!strcasecmp(v->value, "inband"))
2137                                         user->dtmfmode = H323_DTMF_INBAND;
2138                         } else if (!strcasecmp(v->name, "relaxdtmf")) {
2139                                 user->dtmfmode |= ast_true(v->value) ? H323_DTMF_INBANDRELAX : 0;
2140                         } else if (!strcasecmp(v->name, "dtmfcodec") && atoi(v->value)) {
2141                                 user->dtmfcodec = atoi(v->value);
2142                         } else if (!strcasecmp(v->name, "t38support")) {
2143                                 if (!strcasecmp(v->value, "disabled"))
2144                                         user->t38support = T38_DISABLED;
2145                                 if (!strcasecmp(v->value, "no"))
2146                                         user->t38support = T38_DISABLED;
2147                                 else if (!strcasecmp(v->value, "faxgw"))
2148                                         user->t38support = T38_FAXGW;
2149                                 else if (!strcasecmp(v->value, "yes"))
2150                                         user->t38support = T38_ENABLED;
2151                         }
2152                         v = v->next;
2153                 }
2154         }
2155
2156         if (gH323Debug)
2157                 ast_verbose("+++   build_user\n");
2158
2159         return user;
2160 }
2161
2162 static struct ooh323_peer *build_peer(const char *name, struct ast_variable *v, int friend_type)
2163 {
2164         struct ooh323_peer *peer = NULL;
2165
2166         if (gH323Debug)
2167                 ast_verbose("---   build_peer\n");
2168
2169         peer = ast_calloc(1, sizeof(*peer));
2170         if (peer) {
2171                 memset(peer, 0, sizeof(struct ooh323_peer));
2172                 ast_mutex_init(&peer->lock);
2173                 ast_copy_string(peer->name, name, sizeof(peer->name));
2174                 peer->capability = gCapability;
2175                 memcpy(&peer->prefs, &gPrefs, sizeof(peer->prefs));
2176                 peer->rtptimeout = gRTPTimeout;
2177                 ast_copy_string(peer->accountcode, gAccountcode, sizeof(peer->accountcode));
2178                 peer->amaflags = gAMAFLAGS;
2179                 peer->dtmfmode = gDTMFMode;
2180                 peer->dtmfcodec = gDTMFCodec;
2181                 peer->t38support = gT38Support;
2182                 peer->port = 1720;
2183                 if (0 == friend_type) {
2184                         peer->mFriend = 1;
2185                 }
2186
2187                 while (v) {
2188                         if (!strcasecmp(v->name, "h323id")) {
2189             if (!(peer->h323id = ast_strdup(v->value))) {
2190                                         ast_log(LOG_ERROR, "Could not allocate memory for h323id of "
2191                                                                                          "peer %s\n", name);
2192                                         ooh323_delete_peer(peer);
2193                                         return NULL;
2194                                 }
2195                         } else if (!strcasecmp(v->name, "e164")) {
2196                                 if (!(peer->e164 = ast_strdup(v->value))) {
2197                                         ast_log(LOG_ERROR, "Could not allocate memory for e164 of "
2198                                                                                          "peer %s\n", name);
2199                                         ooh323_delete_peer(peer);
2200                                         return NULL;
2201                                 }
2202                         } else  if (!strcasecmp(v->name, "email")) {
2203                                 if (!(peer->email = ast_strdup(v->value))) {
2204                                         ast_log(LOG_ERROR, "Could not allocate memory for email of "
2205                                                                                          "peer %s\n", name);
2206                                         ooh323_delete_peer(peer);
2207                                         return NULL;
2208                                 }
2209                         } else if (!strcasecmp(v->name, "url")) {
2210                                 if (!(peer->url = ast_strdup(v->value))) {
2211                                         ast_log(LOG_ERROR, "Could not allocate memory for h323id of "
2212                                                                                          "peer %s\n", name);
2213                                         ooh323_delete_peer(peer);
2214                                         return NULL;
2215                                 }
2216                         } else if (!strcasecmp(v->name, "port")) {
2217                                 peer->port = atoi(v->value);
2218                         } else if (!strcasecmp(v->name, "ip")) {
2219                                 ast_copy_string(peer->ip, v->value, sizeof(peer->ip));
2220                         } else if (!strcasecmp(v->name, "host")) {
2221                                 ast_copy_string(peer->ip, v->value, sizeof(peer->ip));
2222                         } else if (!strcasecmp(v->name, "outgoinglimit")) {
2223                                 peer->outgoinglimit = atoi(v->value);
2224                                 if (peer->outgoinglimit < 0)
2225                                         peer->outgoinglimit = 0;
2226                         } else if (!strcasecmp(v->name, "accountcode")) {
2227                                 ast_copy_string(peer->accountcode, v->value, sizeof(peer->accountcode));
2228                         } else if (!strcasecmp(v->name, "rtptimeout")) {
2229                                 peer->rtptimeout = atoi(v->value);
2230                                 if(peer->rtptimeout < 0)
2231                                         peer->rtptimeout = gRTPTimeout;
2232                         } else if (!strcasecmp(v->name, "rtpmask")) {
2233                                 if ((peer->rtpmask = malloc(sizeof(struct OOH323Regex))) &&
2234                                         (regcomp(&peer->rtpmask->regex, v->value, REG_EXTENDED) 
2235                                                                                         == 0)) {
2236                                         ast_mutex_init(&peer->rtpmask->lock);
2237                                         peer->rtpmask->inuse = 1;
2238                                         ast_copy_string(peer->rtpmaskstr, v->value, 
2239                                                                 sizeof(peer->rtpmaskstr));
2240                                 } else peer->rtpmask = NULL;
2241                         } else if (!strcasecmp(v->name, "disallow")) {
2242                                 ast_parse_allow_disallow(&peer->prefs, &peer->capability, 
2243                                                                                                  v->value, 0); 
2244                         } else if (!strcasecmp(v->name, "allow")) {
2245                                 const char* tcodecs = v->value;
2246                                 if (!strcasecmp(v->value, "all")) {
2247                                         tcodecs = "ulaw,alaw,g729,g723,gsm";
2248                                 }
2249                                 ast_parse_allow_disallow(&peer->prefs, &peer->capability, 
2250                                                                                                  tcodecs, 1);                            
2251                         } else if (!strcasecmp(v->name,  "amaflags")) {
2252                                 peer->amaflags = ast_cdr_amaflags2int(v->value);
2253                         } else if (!strcasecmp(v->name, "dtmfmode")) {
2254                                 if (!strcasecmp(v->value, "rfc2833"))
2255                                         peer->dtmfmode = H323_DTMF_RFC2833;
2256                                 if (!strcasecmp(v->value, "cisco"))
2257                                         peer->dtmfmode = H323_DTMF_CISCO;
2258                                 else if (!strcasecmp(v->value, "q931keypad"))
2259                                         peer->dtmfmode = H323_DTMF_Q931;
2260                                 else if (!strcasecmp(v->value, "h245alphanumeric"))
2261                                         peer->dtmfmode = H323_DTMF_H245ALPHANUMERIC;
2262                                 else if (!strcasecmp(v->value, "h245signal"))
2263                                         peer->dtmfmode = H323_DTMF_H245SIGNAL;
2264                                 else if (!strcasecmp(v->value, "inband"))
2265                                         peer->dtmfmode = H323_DTMF_INBAND;
2266                         } else if (!strcasecmp(v->name, "relaxdtmf")) {
2267                                 peer->dtmfmode |= ast_true(v->value) ? H323_DTMF_INBANDRELAX : 0;
2268                         } else if (!strcasecmp(v->name, "dtmfcodec") && atoi(v->value)) {
2269                                 peer->dtmfcodec = atoi(v->value);
2270                         } else if (!strcasecmp(v->name, "t38support")) {
2271                                 if (!strcasecmp(v->value, "disabled"))
2272                                         peer->t38support = T38_DISABLED;
2273                                 if (!strcasecmp(v->value, "no"))
2274                                         peer->t38support = T38_DISABLED;
2275                                 else if (!strcasecmp(v->value, "faxgw"))
2276                                         peer->t38support = T38_FAXGW;
2277                                 else if (!strcasecmp(v->value, "yes"))
2278                                         peer->t38support = T38_ENABLED;
2279                         }
2280                         v = v->next;
2281                 }
2282         }
2283
2284         if (gH323Debug)
2285                 ast_verbose("+++   build_peer\n");
2286
2287         return peer;
2288 }
2289
2290 static int ooh323_do_reload(void)
2291 {
2292         if (gH323Debug) {
2293                 ast_verbose("---   ooh323_do_reload\n");
2294         }
2295
2296         reload_config(1);
2297
2298         if (gH323Debug) {
2299                 ast_verbose("+++   ooh323_do_reload\n");
2300         }
2301
2302         return 0;
2303 }
2304
2305 /*--- h323_reload: Force reload of module from cli ---*/
2306
2307 char *handle_cli_ooh323_reload(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2308 {
2309
2310        switch (cmd) {
2311        case CLI_INIT:
2312                e->command = "ooh323 reload";
2313                e->usage =
2314                        "Usage: ooh323 reload\n"
2315                        "                Reload OOH323 config.\n";
2316                return NULL;
2317        case CLI_GENERATE:
2318                return NULL;
2319        }
2320
2321        if (a->argc != 2)
2322                return CLI_SHOWUSAGE;
2323
2324         if (gH323Debug)
2325                 ast_verbose("---   ooh323_reload\n");
2326
2327         ast_mutex_lock(&h323_reload_lock);
2328         if (h323_reloading) {
2329                 ast_verbose("Previous OOH323 reload not yet done\n");
2330    } else {
2331                 h323_reloading = 1;
2332         }
2333         ast_mutex_unlock(&h323_reload_lock);
2334         restart_monitor();
2335
2336         if (gH323Debug)
2337                 ast_verbose("+++   ooh323_reload\n");
2338
2339         return 0;
2340 }
2341
2342 int reload_config(int reload)
2343 {
2344         int format;
2345         struct ooAliases  *pNewAlias = NULL, *cur, *prev;
2346         struct ast_config *cfg;
2347         struct ast_variable *v;
2348         struct ast_flags config_flags = { reload ? CONFIG_FLAG_FILEUNCHANGED : 0 };
2349         struct ooh323_user *user = NULL;
2350         struct ooh323_peer *peer = NULL;
2351         char *cat;
2352         const char *utype;
2353
2354         if (gH323Debug)
2355                 ast_verbose("---   reload_config\n");
2356
2357         cfg = ast_config_load((char*)config, config_flags);
2358
2359         /* We *must* have a config file otherwise stop immediately */
2360         if (!cfg) {
2361                 ast_log(LOG_NOTICE, "Unable to load config %s, OOH323 disabled\n", config);
2362                 return 1;
2363         } else if (cfg == CONFIG_STATUS_FILEUNCHANGED)
2364                 return RESULT_SUCCESS;
2365
2366         if (reload) {
2367                 delete_users();
2368                 delete_peers();
2369                 if (gH323Debug) {
2370                         ast_verbose("  reload_config - Freeing up alias list\n");
2371                 }
2372                 cur = gAliasList;
2373                 while (cur) {
2374                         prev = cur;
2375                         cur = cur->next;
2376                         free(prev->value);
2377                         free(prev);
2378                 }
2379                 gAliasList = NULL;
2380         }
2381
2382         /* Inintialize everything to default */
2383         strcpy(gLogFile, DEFAULT_LOGFILE);
2384         gPort = 1720;
2385         gIP[0] = '\0';
2386         strcpy(gCallerID, DEFAULT_H323ID);
2387         gCapability = AST_FORMAT_ALAW;
2388         memset(&gPrefs, 0, sizeof(struct ast_codec_pref));
2389         gDTMFMode = H323_DTMF_RFC2833;
2390         gDTMFCodec = 101;
2391         gT38Support = T38_FAXGW;
2392         gTRCLVL = OOTRCLVLERR;
2393         gRasGkMode = RasNoGatekeeper;
2394         gGatekeeper[0] = '\0';
2395         gRTPTimeout = 60;
2396         strcpy(gAccountcode, DEFAULT_H323ACCNT);
2397         gFastStart = 1;
2398         gTunneling = 1;
2399         gTOS = 0;
2400         strcpy(gContext, DEFAULT_CONTEXT);
2401         gAliasList = NULL;
2402         gMediaWaitForConnect = 0;
2403         ooconfig.mTCPPortStart = 12030;
2404         ooconfig.mTCPPortEnd = 12230;
2405         memcpy(&global_jbconf, &default_jbconf, sizeof(struct ast_jb_conf));
2406
2407         v = ast_variable_browse(cfg, "general");
2408         while (v) {
2409
2410                 if (!ast_jb_read_conf(&global_jbconf, v->name, v->value)) {
2411                         v = v->next;
2412                         continue;
2413                 }
2414         
2415                 if (!strcasecmp(v->name, "port")) {
2416                         gPort = (int)strtol(v->value, NULL, 10);
2417                 } else if (!strcasecmp(v->name, "bindaddr")) {
2418                         ast_copy_string(gIP, v->value, sizeof(gIP));
2419                 } else if (!strcasecmp(v->name, "h225portrange")) {
2420                         char* endlimit = 0;
2421                         char temp[512];
2422                         ast_copy_string(temp, v->value, sizeof(temp));
2423                         endlimit = strchr(temp, ',');
2424                         if (endlimit) {
2425                                 *endlimit = '\0';
2426                                 endlimit++;
2427                                 ooconfig.mTCPPortStart = atoi(temp);
2428                                 ooconfig.mTCPPortEnd = atoi(endlimit);
2429
2430                         } else {
2431                                 ast_log(LOG_ERROR, "h225portrange: Invalid format, separate port range with \",\"\n");
2432                         }
2433                 } else if (!strcasecmp(v->name, "gateway")) {
2434                         gIsGateway = ast_true(v->value);
2435                 } else if (!strcasecmp(v->name, "faststart")) {
2436                         gFastStart = ast_true(v->value);
2437                         if (gFastStart)
2438                                 ooH323EpEnableFastStart();
2439                         else
2440                                 ooH323EpDisableFastStart();
2441                 } else if (!strcasecmp(v->name, "mediawaitforconnect")) {
2442                         gMediaWaitForConnect = ast_true(v->value);
2443                         if (gMediaWaitForConnect)
2444                                 ooH323EpEnableMediaWaitForConnect();
2445                         else 
2446                                 ooH323EpDisableMediaWaitForConnect();
2447                 } else if (!strcasecmp(v->name, "h245tunneling")) {
2448                         gTunneling = ast_true(v->value);
2449                         if (gTunneling)
2450                                 ooH323EpEnableH245Tunneling();
2451                         else
2452                                 ooH323EpDisableH245Tunneling();
2453                 } else if (!strcasecmp(v->name, "trybemaster")) {
2454                         gBeMaster = ast_true(v->value);
2455                         if (gBeMaster)
2456                                 ooH323EpTryBeMaster(1);
2457                         else 
2458                                 ooH323EpTryBeMaster(0);
2459                 } else if (!strcasecmp(v->name, "h323id")) {
2460                         pNewAlias = malloc(sizeof(struct ooAliases));
2461                         if (!pNewAlias) {
2462                                 ast_log(LOG_ERROR, "Failed to allocate memory for h323id alias\n");
2463                                 return 1;
2464                         }
2465                         if (gAliasList == NULL) { /* first h323id - set as callerid if callerid is not set */
2466                                 ast_copy_string(gCallerID, v->value, sizeof(gCallerID));
2467                         }
2468                         pNewAlias->type =  T_H225AliasAddress_h323_ID;
2469                         pNewAlias->value = strdup(v->value);
2470                         pNewAlias->next = gAliasList;
2471                         gAliasList = pNewAlias;
2472                         pNewAlias = NULL;
2473                 } else if (!strcasecmp(v->name, "e164")) {
2474                         pNewAlias = malloc(sizeof(struct ooAliases));
2475                         if (!pNewAlias) {
2476                                 ast_log(LOG_ERROR, "Failed to allocate memory for e164 alias\n");
2477                                 return 1;
2478                         }
2479                         pNewAlias->type =  T_H225AliasAddress_dialedDigits;
2480                         pNewAlias->value = strdup(v->value);
2481                         pNewAlias->next = gAliasList;
2482                         gAliasList = pNewAlias;
2483                         pNewAlias = NULL;
2484                 } else if (!strcasecmp(v->name, "email")) {
2485                         pNewAlias = malloc(sizeof(struct ooAliases));
2486                         if (!pNewAlias) {
2487                                 ast_log(LOG_ERROR, "Failed to allocate memory for email alias\n");
2488                                 return 1;
2489                         }
2490                         pNewAlias->type =  T_H225AliasAddress_email_ID;
2491                         pNewAlias->value = strdup(v->value);
2492                         pNewAlias->next = gAliasList;
2493                         gAliasList = pNewAlias;
2494                         pNewAlias = NULL;
2495       } else if (!strcasecmp(v->name, "t35country")) {
2496          t35countrycode = atoi(v->value);
2497       } else if (!strcasecmp(v->name, "t35extensions")) {
2498          t35extensions = atoi(v->value);
2499       } else if (!strcasecmp(v->name, "manufacturer")) {
2500          manufacturer = atoi(v->value);
2501       } else if (!strcasecmp(v->name, "vendorid")) {
2502          ast_copy_string(vendor, v->value, sizeof(vendor));
2503       } else if (!strcasecmp(v->name, "versionid")) {
2504          ast_copy_string(version, v->value, sizeof(version));
2505                 } else if (!strcasecmp(v->name, "callerid")) {
2506                         ast_copy_string(gCallerID, v->value, sizeof(gCallerID));
2507                 } else if (!strcasecmp(v->name, "incominglimit")) {
2508                         gIncomingLimit = atoi(v->value);
2509                 } else if (!strcasecmp(v->name, "outgoinglimit")) {
2510                         gOutgoingLimit = atoi(v->value);
2511                 } else if (!strcasecmp(v->name, "gatekeeper")) {
2512                         if (!strcasecmp(v->value, "DISABLE")) {
2513                                 gRasGkMode = RasNoGatekeeper;
2514                         } else if (!strcasecmp(v->value, "DISCOVER")) {
2515                                 gRasGkMode = RasDiscoverGatekeeper;
2516                         } else {
2517                                 gRasGkMode = RasUseSpecificGatekeeper;
2518                                 strncpy(gGatekeeper, v->value, sizeof(gGatekeeper)-1);
2519                         }
2520                 } else if (!strcasecmp(v->name, "logfile")) {
2521          strncpy(gLogFile, v->value, sizeof(gLogFile)-1);
2522                 } else if (!strcasecmp(v->name, "context")) {
2523          strncpy(gContext, v->value, sizeof(gContext)-1);
2524          ast_verbose(VERBOSE_PREFIX_3 "  == Setting default context to %s\n", 
2525                                                       gContext);
2526                 } else if (!strcasecmp(v->name, "rtptimeout")) {
2527                         gRTPTimeout = atoi(v->value);
2528                         if (gRTPTimeout <= 0)
2529                                 gRTPTimeout = 60;
2530                 } else if (!strcasecmp(v->name, "tos")) {
2531                         if (sscanf(v->value, "%30i", &format) == 1)
2532                                 gTOS = format & 0xff;
2533                         else if (!strcasecmp(v->value, "lowdelay"))
2534                                 gTOS = IPTOS_LOWDELAY;
2535                         else if (!strcasecmp(v->value, "throughput"))
2536                                 gTOS = IPTOS_THROUGHPUT;
2537                         else if (!strcasecmp(v->value, "reliability"))
2538                                 gTOS = IPTOS_RELIABILITY;
2539                         else if (!strcasecmp(v->value, "mincost"))
2540                                 gTOS = IPTOS_MINCOST;
2541                         else if (!strcasecmp(v->value, "none"))
2542                                 gTOS = 0;
2543                         else
2544                                 ast_log(LOG_WARNING, "Invalid tos value at line %d, should be "
2545                                                                                         "'lowdelay', 'throughput', 'reliability', "
2546                                                                                         "'mincost', or 'none'\n", v->lineno);
2547                 } else if (!strcasecmp(v->name, "amaflags")) {
2548                         gAMAFLAGS = ast_cdr_amaflags2int(v->value);
2549                 } else if (!strcasecmp(v->name, "accountcode")) {
2550          ast_copy_string(gAccountcode, v->value, sizeof(gAccountcode));
2551                 } else if (!strcasecmp(v->name, "disallow")) {
2552                         ast_parse_allow_disallow(&gPrefs, &gCapability, v->value, 0);
2553                 } else if (!strcasecmp(v->name, "allow")) {
2554                         const char* tcodecs = v->value;
2555                         if (!strcasecmp(v->value, "all")) {
2556                                 tcodecs = "ulaw,alaw,g729,g723,gsm";
2557                         }
2558                         ast_parse_allow_disallow(&gPrefs, &gCapability, tcodecs, 1);
2559                 } else if (!strcasecmp(v->name, "dtmfmode")) {
2560                         if (!strcasecmp(v->value, "inband"))
2561                                 gDTMFMode = H323_DTMF_INBAND;
2562                         else if (!strcasecmp(v->value, "rfc2833"))
2563                                 gDTMFMode = H323_DTMF_RFC2833;
2564                         else if (!strcasecmp(v->value, "cisco"))
2565                                 gDTMFMode = H323_DTMF_CISCO;
2566                         else if (!strcasecmp(v->value, "q931keypad"))
2567                                 gDTMFMode = H323_DTMF_Q931;
2568                         else if (!strcasecmp(v->value, "h245alphanumeric"))
2569                                 gDTMFMode = H323_DTMF_H245ALPHANUMERIC;
2570                         else if (!strcasecmp(v->value, "h245signal"))
2571                                 gDTMFMode = H323_DTMF_H245SIGNAL;
2572                         else {
2573             ast_log(LOG_WARNING, "Unknown dtmf mode '%s', using rfc2833\n", 
2574                                                                     v->value);
2575                                 gDTMFMode = H323_DTMF_RFC2833;
2576                         }
2577                 } else if (!strcasecmp(v->name, "relaxdtmf")) {
2578                         gDTMFMode |= ast_true(v->value) ? H323_DTMF_INBANDRELAX : 0;
2579                 } else if (!strcasecmp(v->name, "dtmfcodec") && atoi(v->value)) {
2580                         gDTMFCodec = atoi(v->value);
2581                 } else if (!strcasecmp(v->name, "t38support")) {
2582                         if (!strcasecmp(v->value, "disabled"))
2583                                 gT38Support = T38_DISABLED;
2584                         if (!strcasecmp(v->value, "no"))
2585                                 gT38Support = T38_DISABLED;
2586                         else if (!strcasecmp(v->value, "faxgw"))
2587                                 gT38Support = T38_FAXGW;
2588                         else if (!strcasecmp(v->value, "yes"))
2589                                 gT38Support = T38_ENABLED;
2590                 } else if (!strcasecmp(v->name, "tracelevel")) {
2591                         gTRCLVL = atoi(v->value);
2592                         ooH323EpSetTraceLevel(gTRCLVL);
2593                 }
2594                 v = v->next;
2595         }
2596         
2597         for (cat = ast_category_browse(cfg, NULL); cat; cat = ast_category_browse(cfg, cat)) {
2598                 if (strcasecmp(cat, "general")) {
2599                         int friend_type = 0;
2600                         utype = ast_variable_retrieve(cfg, cat, "type");
2601                         if (utype) {
2602                                 friend_type = strcasecmp(utype, "friend");
2603                                 if (!strcmp(utype, "user") || 0 == friend_type) {
2604                                         user = build_user(cat, ast_variable_browse(cfg, cat));
2605                                         if (user) {
2606                                                 ast_mutex_lock(&userl.lock);
2607                                                 user->next = userl.users;
2608                                                 userl.users = user;
2609                                                 ast_mutex_unlock(&userl.lock);
2610                                         } else {
2611                                                 ast_log(LOG_WARNING, "Failed to build user %s\n", cat);
2612                                         }
2613                                 }
2614                                 if (!strcasecmp(utype, "peer") || 0 == friend_type) {
2615                                         peer = build_peer(cat, ast_variable_browse(cfg, cat), friend_type);
2616                                         if (peer) {
2617                                                 ast_mutex_lock(&peerl.lock);
2618                                                 peer->next = peerl.peers;
2619                                                 peerl.peers = peer;
2620                                                 ast_mutex_unlock(&peerl.lock);
2621                                         } else {
2622                                                 ast_log(LOG_WARNING, "Failed to build peer %s\n", cat);
2623                                         }
2624                                 }
2625                         }
2626                 }
2627         }
2628         ast_config_destroy(cfg);
2629
2630
2631         /* Determine ip address if neccessary */
2632         if (ast_strlen_zero(gIP)) {
2633                 ooGetLocalIPAddress(gIP);
2634                 if (!strcmp(gIP, "127.0.0.1")) {
2635                         ast_log(LOG_NOTICE, "Failed to determine local ip address. Please "
2636                                                                          "specify it in ooh323.conf. OOH323 Disabled\n");
2637                         return 1;
2638                 }
2639         }
2640
2641         if (gH323Debug)
2642                 ast_verbose("+++   reload_config\n");
2643
2644         return 0;
2645
2646 }
2647
2648
2649 static char *handle_cli_ooh323_show_peer(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2650 {
2651         char ip_port[30];
2652         struct ooh323_peer *prev = NULL, *peer = NULL;
2653         
2654         switch (cmd) {
2655         case CLI_INIT:
2656                 e->command = "ooh323 show peer";
2657                 e->usage =
2658                         "Usage: ooh323 show peer <name>\n"
2659                         "                List details of specific OOH323 peer.\n";
2660                 return NULL;
2661         case CLI_GENERATE:
2662                 return NULL;
2663         }
2664
2665         if (a->argc != 4)
2666                 return CLI_SHOWUSAGE;
2667
2668  
2669         ast_mutex_lock(&peerl.lock);
2670         peer = peerl.peers;
2671         while (peer) {
2672                 ast_mutex_lock(&peer->lock);
2673                 if(!strcmp(peer->name, a->argv[3]))
2674                         break;
2675                 else {
2676                         prev = peer;
2677                         peer = peer->next;
2678                         ast_mutex_unlock(&prev->lock);
2679                 }
2680         }
2681
2682         if (peer) {
2683       sprintf(ip_port, "%s:%d", peer->ip, peer->port);
2684       ast_cli(a->fd, "%-15.15s%s\n", "Name: ", peer->name);
2685       ast_cli(a->fd, "%-15.15s%s", "Format Prefs: ", "(");
2686       print_codec_to_cli(a->fd, &peer->prefs);
2687       ast_cli(a->fd, ")\n");
2688       ast_cli(a->fd, "%-15.15s", "DTMF Mode: ");
2689                 if (peer->dtmfmode & H323_DTMF_CISCO) {
2690          ast_cli(a->fd, "%s\n", "cisco");
2691          ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", peer->dtmfcodec);
2692                 } else if (peer->dtmfmode & H323_DTMF_RFC2833) {
2693          ast_cli(a->fd, "%s\n", "rfc2833");
2694          ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", peer->dtmfcodec);
2695                 } else if (peer->dtmfmode & H323_DTMF_Q931)
2696          ast_cli(a->fd, "%s\n", "q931keypad");
2697                 else if (peer->dtmfmode & H323_DTMF_H245ALPHANUMERIC)
2698          ast_cli(a->fd, "%s\n", "h245alphanumeric");
2699                 else if (peer->dtmfmode & H323_DTMF_H245SIGNAL)
2700          ast_cli(a->fd, "%s\n", "h245signal");
2701                 else if (peer->dtmfmode & H323_DTMF_INBAND && peer->dtmfmode & H323_DTMF_INBANDRELAX)
2702          ast_cli(a->fd, "%s\n", "inband-relaxed");
2703                 else if (peer->dtmfmode & H323_DTMF_INBAND)
2704          ast_cli(a->fd, "%s\n", "inband");
2705                 else
2706          ast_cli(a->fd, "%s\n", "unknown");
2707
2708         ast_cli(a->fd,"%-15s", "T.38 Mode: ");
2709         if (peer->t38support == T38_DISABLED)
2710                 ast_cli(a->fd, "%s\n", "disabled");
2711         else if (peer->t38support == T38_FAXGW)
2712                 ast_cli(a->fd, "%s\n", "faxgw/chan_sip compatible");
2713
2714         ast_cli(a->fd, "%-15.15s%s\n", "AccountCode: ", peer->accountcode);
2715         ast_cli(a->fd, "%-15.15s%s\n", "AMA flags: ", 
2716                 ast_cdr_flags2str(peer->amaflags));
2717         ast_cli(a->fd, "%-15.15s%s\n", "IP:Port: ", ip_port);
2718         ast_cli(a->fd, "%-15.15s%d\n", "OutgoingLimit: ", peer->outgoinglimit);
2719         ast_cli(a->fd, "%-15.15s%d\n", "rtptimeout: ", peer->rtptimeout);
2720         if (peer->rtpmaskstr[0])
2721                 ast_cli(a->fd, "%-15.15s%s\n", "rtpmask: ", peer->rtpmaskstr);
2722         ast_mutex_unlock(&peer->lock);
2723         } else {
2724         ast_cli(a->fd, "Peer %s not found\n", a->argv[3]);
2725         ast_cli(a->fd, "\n");
2726         }
2727         ast_mutex_unlock(&peerl.lock);
2728
2729         return CLI_SUCCESS;
2730 }
2731
2732 static char *handle_cli_ooh323_show_peers(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2733 {
2734         struct ooh323_peer *prev = NULL, *peer = NULL;
2735    char formats[FORMAT_STRING_SIZE];
2736    char ip_port[30];
2737 #define FORMAT  "%-15.15s  %-15.15s  %-23.23s  %-s\n"
2738
2739         switch (cmd) {
2740         case CLI_INIT:
2741                 e->command = "ooh323 show peers";
2742                 e->usage =
2743                         "Usage: ooh323 show peers\n"
2744                         "                Lists all known OOH323 peers.\n";
2745                 return NULL;
2746         case CLI_GENERATE:
2747                 return NULL;
2748         }
2749
2750         if (a->argc != 3)
2751                 return CLI_SHOWUSAGE;
2752
2753    ast_cli(a->fd, FORMAT, "Name", "Accountcode", "ip:port", "Formats");
2754
2755         ast_mutex_lock(&peerl.lock);
2756         peer = peerl.peers;
2757         while (peer) {
2758                 ast_mutex_lock(&peer->lock);
2759                 snprintf(ip_port, sizeof(ip_port), "%s:%d", peer->ip, peer->port);
2760      ast_cli(a->fd, FORMAT, peer->name, 
2761                                         peer->accountcode,
2762                                         ip_port,
2763                  ast_getformatname_multiple(formats,FORMAT_STRING_SIZE,peer->capability));
2764                 prev = peer;
2765                 peer = peer->next;
2766                 ast_mutex_unlock(&prev->lock);
2767
2768         }
2769         ast_mutex_unlock(&peerl.lock);
2770 #undef FORMAT
2771         return CLI_SUCCESS;
2772 }
2773
2774 /*! \brief Print codec list from preference to CLI/manager */
2775 static void print_codec_to_cli(int fd, struct ast_codec_pref *pref)
2776 {
2777         int x, codec;
2778
2779         for (x = 0; x < 32; x++) {
2780                 codec = ast_codec_pref_index(pref, x);
2781                 if (!codec)
2782                         break;
2783                 ast_cli(fd, "%s", ast_getformatname(codec));
2784                 ast_cli(fd, ":%d", pref->framing[x]);
2785                 if (x < 31 && ast_codec_pref_index(pref, x + 1))
2786                         ast_cli(fd, ",");
2787         }
2788         if (!x)
2789                 ast_cli(fd, "none");
2790 }
2791
2792 static char *handle_cli_ooh323_show_user(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2793 {
2794         struct ooh323_user *prev = NULL, *user = NULL;
2795
2796         switch (cmd) {
2797         case CLI_INIT:
2798                 e->command = "ooh323 show user";
2799                 e->usage =
2800                         "Usage: ooh323 show user <name>\n"
2801                         "                List details of specific OOH323 user.\n";
2802                 return NULL;
2803         case CLI_GENERATE:
2804                 return NULL;
2805         }
2806
2807         if (a->argc != 4)
2808                 return CLI_SHOWUSAGE;
2809
2810
2811         ast_mutex_lock(&userl.lock);
2812         user = userl.users;
2813         while (user) {
2814                 ast_mutex_lock(&user->lock);
2815       if(!strcmp(user->name, a->argv[3])) {
2816                         break;
2817       } else {
2818                         prev = user;
2819                         user = user->next;
2820                         ast_mutex_unlock(&prev->lock);
2821                 }
2822         }
2823
2824         if (user) {
2825       ast_cli(a->fd, "%-15.15s%s\n", "Name: ", user->name);
2826       ast_cli(a->fd, "%-15.15s%s", "Format Prefs: ", "(");
2827       print_codec_to_cli(a->fd, &user->prefs);
2828       ast_cli(a->fd, ")\n");
2829       ast_cli(a->fd, "%-15.15s", "DTMF Mode: ");
2830                 if (user->dtmfmode & H323_DTMF_CISCO) {
2831          ast_cli(a->fd, "%s\n", "cisco");
2832          ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", user->dtmfcodec);
2833                 } else if (user->dtmfmode & H323_DTMF_RFC2833) {
2834          ast_cli(a->fd, "%s\n", "rfc2833");
2835          ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", user->dtmfcodec);
2836                 } else if (user->dtmfmode & H323_DTMF_Q931)
2837          ast_cli(a->fd, "%s\n", "q931keypad");
2838                 else if (user->dtmfmode & H323_DTMF_H245ALPHANUMERIC)
2839          ast_cli(a->fd, "%s\n", "h245alphanumeric");
2840                 else if (user->dtmfmode & H323_DTMF_H245SIGNAL)
2841          ast_cli(a->fd, "%s\n", "h245signal");
2842                 else if (user->dtmfmode & H323_DTMF_INBAND && user->dtmfmode & H323_DTMF_INBANDRELAX)
2843          ast_cli(a->fd, "%s\n", "inband-relaxed");
2844                 else if (user->dtmfmode & H323_DTMF_INBAND)
2845          ast_cli(a->fd, "%s\n", "inband");
2846                 else
2847          ast_cli(a->fd, "%s\n", "unknown");
2848
2849         ast_cli(a->fd,"%-15s", "T.38 Mode: ");
2850         if (user->t38support == T38_DISABLED)
2851                 ast_cli(a->fd, "%s\n", "disabled");
2852         else if (user->t38support == T38_FAXGW)
2853                 ast_cli(a->fd, "%s\n", "faxgw/chan_sip compatible");
2854
2855       ast_cli(a->fd, "%-15.15s%s\n", "AccountCode: ", user->accountcode);
2856       ast_cli(a->fd, "%-15.15s%s\n", "AMA flags: ", 
2857                                             ast_cdr_flags2str(user->amaflags));
2858       ast_cli(a->fd, "%-15.15s%s\n", "Context: ", user->context);
2859       ast_cli(a->fd, "%-15.15s%d\n", "IncomingLimit: ", user->incominglimit);
2860       ast_cli(a->fd, "%-15.15s%d\n", "InUse: ", user->inUse);
2861       ast_cli(a->fd, "%-15.15s%d\n", "rtptimeout: ", user->rtptimeout);
2862         if (user->rtpmaskstr[0])
2863                 ast_cli(a->fd, "%-15.15s%s\n", "rtpmask: ", user->rtpmaskstr);
2864                 ast_mutex_unlock(&user->lock);
2865         } else {
2866      ast_cli(a->fd, "User %s not found\n", a->argv[3]);
2867      ast_cli(a->fd, "\n");
2868         }
2869         ast_mutex_unlock(&userl.lock);
2870
2871         return CLI_SUCCESS;
2872 }
2873
2874 static char *handle_cli_ooh323_show_users(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2875 {
2876         struct ooh323_user *prev = NULL, *user = NULL;
2877    char formats[FORMAT_STRING_SIZE];
2878 #define FORMAT1  "%-15.15s  %-15.15s  %-15.15s  %-s\n"
2879
2880         switch (cmd) {
2881         case CLI_INIT:
2882                 e->command = "ooh323 show users";
2883                 e->usage =
2884                         "Usage: ooh323 show users \n"
2885                         "                Lists all known OOH323 users.\n";
2886                 return NULL;
2887         case CLI_GENERATE:
2888                 return NULL;
2889         }
2890
2891         if (a->argc != 3)
2892                 return CLI_SHOWUSAGE;
2893
2894
2895    ast_cli(a->fd, FORMAT1, "Username", "Accountcode", "Context", "Formats");
2896
2897         ast_mutex_lock(&userl.lock);
2898         user = userl.users;
2899    while(user)
2900    {
2901                 ast_mutex_lock(&user->lock);
2902                 ast_cli(a->fd, FORMAT1, user->name, 
2903                                         user->accountcode, user->context,
2904                                         ast_getformatname_multiple(formats, FORMAT_STRING_SIZE, user->capability));
2905                 prev = user;
2906                 user = user->next;
2907                 ast_mutex_unlock(&prev->lock);
2908
2909         }
2910         ast_mutex_unlock(&userl.lock);
2911 #undef FORMAT1
2912    return RESULT_SUCCESS;
2913
2914 }
2915
2916 static char *handle_cli_ooh323_set_debug(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2917 {
2918         switch (cmd) {
2919         case CLI_INIT:
2920                 e->command = "ooh323 set debug [off]";
2921                 e->usage =
2922                         "Usage: ooh323 set debug [off]\n"
2923                         "                Enables/Disables debugging of OOH323 channel driver\n";
2924                 return NULL;
2925         case CLI_GENERATE:
2926                 return NULL;
2927         }
2928
2929         if (a->argc < 3 || a->argc > 4)
2930                 return CLI_SHOWUSAGE;
2931         if (a->argc == 4 && strcasecmp(a->argv[3], "off"))
2932                 return CLI_SHOWUSAGE;
2933
2934         gH323Debug = (a->argc == 4) ? FALSE : TRUE;
2935         ast_cli(a->fd, "OOH323 Debugging %s\n", gH323Debug ? "Enabled" : "Disabled");
2936
2937         return CLI_SUCCESS;
2938 }
2939
2940 #if 0
2941 static int ooh323_show_channels(int fd, int argc, char *argv[])
2942 {
2943         return RESULT_SUCCESS;
2944 }
2945 #endif
2946
2947 static char *handle_cli_ooh323_show_config(struct ast_cli_entry *e, int cmd, struct ast_cli_args *a)
2948 {
2949         char value[FORMAT_STRING_SIZE];
2950         ooAliases *pAlias = NULL, *pAliasNext = NULL;;
2951
2952         switch (cmd) {
2953         case CLI_INIT:
2954                 e->command = "ooh323 show config";
2955                 e->usage =
2956                         "Usage: ooh323 show config\n"
2957                         "                Shows global configuration of H.323 channel driver\n";
2958                 return NULL;
2959         case CLI_GENERATE:
2960                 return NULL;
2961         }
2962
2963         if (a->argc != 3)
2964                 return CLI_SHOWUSAGE;
2965
2966
2967
2968    ast_cli(a->fd, "\nObjective Open H.323 Channel Driver's Config:\n");
2969         snprintf(value, sizeof(value), "%s:%d", gIP, gPort);
2970    ast_cli(a->fd, "%-20s%s\n", "IP:Port: ", value);
2971    ast_cli(a->fd, "%-20s%d-%d\n", "H.225 port range: ", 
2972       ooconfig.mTCPPortStart, ooconfig.mTCPPortEnd);
2973    ast_cli(a->fd, "%-20s%s\n", "FastStart", gFastStart?"yes":"no");
2974    ast_cli(a->fd, "%-20s%s\n", "Tunneling", gTunneling?"yes":"no");
2975    ast_cli(a->fd, "%-20s%s\n", "CallerId", gCallerID);
2976    ast_cli(a->fd, "%-20s%s\n", "MediaWaitForConnect", 
2977       gMediaWaitForConnect?"yes":"no");
2978
2979 #if (0)
2980                 extern OOH323EndPoint gH323ep;
2981    ast_cli(a->fd, "%-20s%s\n", "FASTSTART", 
2982                         (OO_TESTFLAG(gH323ep.flags, OO_M_FASTSTART) != 0) ? "yes" : "no");
2983    ast_cli(a->fd, "%-20s%s\n", "TUNNELING", 
2984                         (OO_TESTFLAG(gH323ep.flags, OO_M_TUNNELING) != 0) ? "yes" : "no");
2985    ast_cli(a->fd, "%-20s%s\n", "MEDIAWAITFORCONN",
2986                         (OO_TESTFLAG(gH323ep.flags, OO_M_MEDIAWAITFORCONN) != 0) ? "yes" : "no");
2987 #endif
2988
2989         if (gRasGkMode == RasNoGatekeeper)
2990                 snprintf(value, sizeof(value), "%s", "No Gatekeeper");
2991         else if (gRasGkMode == RasDiscoverGatekeeper)
2992                 snprintf(value, sizeof(value), "%s", "Discover");
2993         else
2994                 snprintf(value, sizeof(value), "%s", gGatekeeper);
2995
2996    ast_cli(a->fd,  "%-20s%s\n", "Gatekeeper:", value);
2997
2998    ast_cli(a->fd,  "%-20s%s\n", "H.323 LogFile:", gLogFile);   
2999
3000    ast_cli(a->fd,  "%-20s%s\n", "Context:", gContext);
3001    
3002    ast_cli(a->fd,  "%-20s%s\n", "Capability:", 
3003            ast_getformatname_multiple(value,FORMAT_STRING_SIZE,gCapability));
3004
3005    ast_cli(a->fd, "%-20s", "DTMF Mode: ");
3006         if (gDTMFMode & H323_DTMF_CISCO) {
3007       ast_cli(a->fd, "%s\n", "cisco");
3008       ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", gDTMFCodec);
3009         } else if (gDTMFMode & H323_DTMF_RFC2833) {
3010       ast_cli(a->fd, "%s\n", "rfc2833");
3011       ast_cli(a->fd, "%-15.15s%d\n", "DTMF Codec: ", gDTMFCodec);
3012         } else if (gDTMFMode & H323_DTMF_Q931)
3013       ast_cli(a->fd, "%s\n", "q931keypad");
3014         else if (gDTMFMode & H323_DTMF_H245ALPHANUMERIC)
3015       ast_cli(a->fd, "%s\n", "h245alphanumeric");
3016         else if (gDTMFMode & H323_DTMF_H245SIGNAL)
3017       ast_cli(a->fd, "%s\n", "h245signal");
3018                 else if (gDTMFMode & H323_DTMF_INBAND && gDTMFMode & H323_DTMF_INBANDRELAX)
3019          ast_cli(a->fd, "%s\n", "inband-relaxed");
3020                 else if (gDTMFMode & H323_DTMF_INBAND)
3021          ast_cli(a->fd, "%s\n", "inband");
3022         else
3023                 ast_cli(a->fd, "%s\n", "unknown");
3024
3025         ast_cli(a->fd,"%-20s", "T.38 Mode: ");
3026         if (gT38Support == T38_DISABLED)
3027                 ast_cli(a->fd, "%s\n", "disabled");
3028         else if (gT38Support == T38_FAXGW)
3029                 ast_cli(a->fd, "%s\n", "faxgw/chan_sip compatible");
3030
3031    ast_cli(a->fd, "%-20s%ld\n", "Call counter: ", callnumber);
3032    ast_cli(a->fd, "%-20s%s\n", "AccountCode: ", gAccountcode);
3033
3034    ast_cli(a->fd, "%-20s%s\n", "AMA flags: ", ast_cdr_flags2str(gAMAFLAGS));
3035
3036         pAlias = gAliasList;
3037    if(pAlias) {
3038      ast_cli(a->fd, "%-20s\n", "Aliases: ");
3039    }
3040         while (pAlias) {
3041                 pAliasNext = pAlias->next;
3042                 if (pAliasNext) {
3043          ast_cli(a->fd,"\t%-30s\t%-30s\n",pAlias->value, pAliasNext->value);
3044                         pAlias = pAliasNext->next;
3045       }
3046       else{
3047          ast_cli(a->fd,"\t%-30s\n",pAlias->value);
3048                         pAlias = pAlias->next;
3049                 }
3050         }
3051         return CLI_SUCCESS;
3052 }
3053
3054 static struct ast_cli_entry cli_ooh323[] = {
3055         AST_CLI_DEFINE(handle_cli_ooh323_set_debug,     "Enable/Disable OOH323 debugging"),
3056         AST_CLI_DEFINE(handle_cli_ooh323_show_config, "Show details on global configuration of H.323 channel driver"),
3057         AST_CLI_DEFINE(handle_cli_ooh323_show_peer,     "Show details on specific OOH323 peer"),
3058         AST_CLI_DEFINE(handle_cli_ooh323_show_peers,  "Show defined OOH323 peers"),
3059         AST_CLI_DEFINE(handle_cli_ooh323_show_user,     "Show details on specific OOH323 user"),
3060         AST_CLI_DEFINE(handle_cli_ooh323_show_users,  "Show defined OOH323 users"),