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