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