issue #5671
[asterisk/asterisk.git] / channels / chan_misdn_config.c
1 /*
2  * Asterisk -- An open source telephony toolkit.
3  * 
4  * Copyright (C) 2005, Christian Richter
5  *
6  * Christian Richter <crich@beronet.com>
7  *
8  * See http://www.asterisk.org for more information about
9  * the Asterisk project. Please do not directly contact
10  * any of the maintainers of this project for assistance;
11  * the project provides a web site, mailing lists and IRC
12  * channels for your use.
13  *
14  * This program is free software, distributed under the terms of
15  * the GNU General Public License Version 2. See the LICENSE file
16  * at the top of the source tree.
17  *
18  */
19
20 /*!
21  * \file
22  *
23  * \brief chan_misdn configuration management
24  * \author Christian Richter <crich@beronet.com>
25  *
26  * \ingroup channel_drivers
27  */
28
29
30 #include "chan_misdn_config.h"
31
32 #include <asterisk/config.h>
33 #include <asterisk/channel.h>
34 #include <asterisk/logger.h>
35 #include <asterisk/lock.h>
36 #include <asterisk/strings.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <stdio.h>
40
41 #include <asterisk/utils.h>
42 #define AST_LOAD_CFG ast_config_load
43 #define AST_DESTROY_CFG ast_config_destroy
44
45 #define DEF_ECHOCANCEL 128
46 #define DEF_ECHOTRAINING 1
47
48 struct msn_list {
49         char *msn;
50         struct msn_list *next;
51 };
52
53 struct port_config {
54         char *name;
55         int *rxgain;
56         int *txgain;
57         int *te_choose_channel;
58         char *context;
59         char *language;
60         char *callerid;
61         char *method;
62         int *dialplan; 
63         char *nationalprefix;
64         char *internationalprefix;
65         int *pres;
66         int *always_immediate;
67         int *immediate;
68         int *hold_allowed;
69         int *early_bconnect;
70         int *use_callingpres;
71         int *echocancel;
72         int *echocancelwhenbridged;
73         int *echotraining;
74         struct msn_list *msn_list;
75         ast_group_t *callgroup;         /* Call group */
76         ast_group_t *pickupgroup;       /* Pickup group */
77 };
78
79 struct general_config {
80         int *debug;
81         char *tracefile;
82         int *trace_calls;
83         char *trace_dir;
84         int *bridging;
85         int *stop_tone_after_first_digit;
86         int *append_digits2exten;
87         int *l1_info_ok;
88         int *clear_l3;
89         int *dynamic_crypt;
90         char *crypt_prefix;
91         char *crypt_keys;
92 };
93
94 /* array of port configs, default is at position 0. */
95 static struct port_config **port_cfg;
96 /* max number of available ports, is set on init */
97 static int max_ports;
98 /* general config */
99 static struct general_config *general_cfg;
100 /* storing the ptp flag separated to save memory */
101 static int *ptp;
102
103 static ast_mutex_t config_mutex; 
104
105
106 static inline void misdn_cfg_lock (void) {
107         ast_mutex_lock(&config_mutex);
108 }
109
110 static inline void misdn_cfg_unlock (void) {
111         ast_mutex_unlock(&config_mutex);
112 }
113
114 static void free_msn_list (struct msn_list* iter) {
115         if (iter->next)
116                 free_msn_list(iter->next);
117         if (iter->msn)
118                 free(iter->msn);
119         free(iter);
120 }
121
122 static void free_port_cfg (void) {
123
124         struct port_config **free_list = (struct port_config **)calloc(max_ports + 1, sizeof(struct port_config *));
125
126         int i, j;
127
128         for (i = 0; i < max_ports; i++) {
129                 if (port_cfg[i]) {
130                         for (j = 0; j < max_ports && free_list[j]; j++) {
131                                 if (free_list[j] && free_list[j] == port_cfg[i])
132                                         continue; /* already in list */
133                                 free_list[j] = port_cfg[i];
134                         }
135                 }
136         }
137
138 #define FREE_ELEM(elem) ({ \
139                 if (free_list[i]->elem) \
140                         free(free_list[i]->elem); \
141         })
142         
143         for (i = 0; i < max_ports; i++) {
144                 if (free_list[i]) {
145                         FREE_ELEM(name);
146                         FREE_ELEM(rxgain);
147                         FREE_ELEM(txgain);
148                         FREE_ELEM(te_choose_channel);
149                         FREE_ELEM(context);
150                         FREE_ELEM(language);
151                         FREE_ELEM(callerid);
152                         FREE_ELEM(method);
153                         FREE_ELEM(dialplan);
154                         FREE_ELEM(nationalprefix);
155                         FREE_ELEM(internationalprefix);
156                         FREE_ELEM(pres);
157                         FREE_ELEM(always_immediate);
158                         FREE_ELEM(immediate);
159                         FREE_ELEM(hold_allowed);
160                         FREE_ELEM(early_bconnect);
161                         FREE_ELEM(use_callingpres);
162                         FREE_ELEM(echocancel);
163                         FREE_ELEM(echocancelwhenbridged);
164                         FREE_ELEM(echotraining);
165                         if (free_list[i]->msn_list)
166                                 free_msn_list(free_list[i]->msn_list);
167                         FREE_ELEM(callgroup);
168                         FREE_ELEM(pickupgroup);
169                         free(free_list[i]);
170                 }
171         }
172         free(free_list);
173 }
174
175 static void free_general_cfg (void) {
176
177 #define FREE_GEN_ELEM(elem) ({ \
178                 if (general_cfg->elem) \
179                         free(general_cfg->elem); \
180         })
181         
182         FREE_GEN_ELEM(debug);
183         FREE_GEN_ELEM(tracefile);
184         FREE_GEN_ELEM(trace_calls);
185         FREE_GEN_ELEM(trace_dir);
186         FREE_GEN_ELEM(bridging);
187         FREE_GEN_ELEM(stop_tone_after_first_digit);
188         FREE_GEN_ELEM(append_digits2exten);
189         FREE_GEN_ELEM(l1_info_ok);
190         FREE_GEN_ELEM(clear_l3);
191         FREE_GEN_ELEM(dynamic_crypt);
192         FREE_GEN_ELEM(crypt_prefix);
193         FREE_GEN_ELEM(crypt_keys);
194 }
195
196 #define GET_PORTCFG_STRCPY(item) ({ \
197                 char *temp; \
198                 if (port_cfg[port] && port_cfg[port]->item) \
199                         temp = port_cfg[port]->item; \
200                 else \
201                         temp = port_cfg[0]->item; \
202                 if (!temp || !memccpy((void *)buf, (void *)temp, '\0', bufsize)) \
203                         memset(buf, 0, 1); \
204         })
205
206 #define GET_GENCFG_STRCPY(item) ({ \
207                 if (general_cfg && general_cfg->item) { \
208                         if (!memccpy((void *)buf, (void *)general_cfg->item, '\0', bufsize)) \
209                                 memset(buf, 0, 1); \
210                 } else \
211                         memset(buf, 0, 1); \
212         })
213
214 #define GET_PORTCFG_MEMCPY(item) ({ \
215                 typeof(port_cfg[0]->item) temp; \
216                 if (port_cfg[port] && port_cfg[port]->item) \
217                         temp = port_cfg[port]->item; \
218                 else \
219                         temp = port_cfg[0]->item; \
220                 if (temp) { \
221                         int l = sizeof(*temp); \
222                         if (l > bufsize) \
223                                 memset(buf, 0, bufsize); \
224                         else \
225                                 memcpy(buf, temp, l); \
226                 } else \
227                         memset(buf, 0, bufsize); \
228         })
229
230 #define GET_GENCFG_MEMCPY(item) ({ \
231                 if (general_cfg && general_cfg->item) { \
232                         typeof(general_cfg->item) temp = general_cfg->item; \
233                         int l = sizeof(*temp); \
234                         if (l > bufsize) \
235                                 memset(buf, 0, bufsize); \
236                         else \
237                                 memcpy(buf, temp, l); \
238                 } else \
239                         memset(buf, 0, bufsize); \
240         })
241
242 void misdn_cfg_get(int port, enum misdn_cfg_elements elem, void *buf, int bufsize) {
243         
244         if (!(elem > MISDN_GEN_FIRST) && !misdn_cfg_is_port_valid(port)) {
245                 memset(buf, 0, bufsize);
246                 ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get! Port number %d is not valid.\n", port);
247                 return;
248         }
249
250         misdn_cfg_lock();
251         
252         switch (elem) {
253                 
254                 /* port config elements */
255                         
256                 case MISDN_CFG_PTP:             if (sizeof(ptp[port]) <= bufsize)
257                                                         memcpy(buf, &ptp[port], sizeof(ptp[port]));
258                                                 else
259                                                         buf = 0; /* error, should not happen */
260                                                 break;
261                 case MISDN_CFG_GROUPNAME:       GET_PORTCFG_STRCPY(name);
262                                                 break;
263                 case MISDN_CFG_RXGAIN:          GET_PORTCFG_MEMCPY(rxgain);
264                                                 break;
265                 case MISDN_CFG_TXGAIN:          GET_PORTCFG_MEMCPY(txgain);
266                                                 break;
267                 case MISDN_CFG_TE_CHOOSE_CHANNEL:
268                                                 GET_PORTCFG_MEMCPY(te_choose_channel);
269                                                 break;
270                 case MISDN_CFG_CONTEXT:         GET_PORTCFG_STRCPY(context);
271                                                 break;
272                 case MISDN_CFG_LANGUAGE:        GET_PORTCFG_STRCPY(language);
273                                                 break;
274                 case MISDN_CFG_CALLERID:        GET_PORTCFG_STRCPY(callerid);
275                                                 break;
276                 case MISDN_CFG_METHOD:          GET_PORTCFG_STRCPY(method);
277                                                 break;
278                 case MISDN_CFG_DIALPLAN:        GET_PORTCFG_MEMCPY(dialplan);
279                                                 break;
280                 case MISDN_CFG_NATPREFIX:       GET_PORTCFG_STRCPY(nationalprefix);
281                                                 break;
282                 case MISDN_CFG_INTERNATPREFIX:
283                                                 GET_PORTCFG_STRCPY(internationalprefix);
284                                                 break;
285                 case MISDN_CFG_PRES:            GET_PORTCFG_MEMCPY(pres);
286                                                 break;
287                 case MISDN_CFG_ALWAYS_IMMEDIATE:
288                                                 GET_PORTCFG_MEMCPY(always_immediate);
289                                                 break;
290                 case MISDN_CFG_IMMEDIATE:       GET_PORTCFG_MEMCPY(immediate);
291                                                 break;
292                 case MISDN_CFG_HOLD_ALLOWED:
293                                                 GET_PORTCFG_MEMCPY(hold_allowed);
294                                                 break;
295                 case MISDN_CFG_EARLY_BCONNECT:
296                                                 GET_PORTCFG_MEMCPY(early_bconnect);
297                                                 break;
298                 case MISDN_CFG_USE_CALLINGPRES:
299                                                 GET_PORTCFG_MEMCPY(use_callingpres);
300                                                 break;
301                 case MISDN_CFG_ECHOCANCEL:
302                                                 GET_PORTCFG_MEMCPY(echocancel );
303                                                 break;
304                 case MISDN_CFG_ECHOCANCELWHENBRIDGED:
305                                                 GET_PORTCFG_MEMCPY(echocancelwhenbridged);
306                                                 break;
307                 case MISDN_CFG_ECHOTRAINING:
308                                                 GET_PORTCFG_MEMCPY(echotraining);
309                                                 break;
310                 case MISDN_CFG_CALLGROUP:       GET_PORTCFG_MEMCPY(callgroup);
311                                                 break;
312                 case MISDN_CFG_PICKUPGROUP:     GET_PORTCFG_MEMCPY(pickupgroup);
313                                                 break;
314                 
315                 /* general config elements */
316                         
317                 case MISDN_GEN_DEBUG:           GET_GENCFG_MEMCPY(debug);
318                                                 break;
319                 case MISDN_GEN_TRACEFILE:       GET_GENCFG_STRCPY(tracefile);
320                                                 break;
321                 case MISDN_GEN_TRACE_CALLS: GET_GENCFG_MEMCPY(trace_calls);
322                                                 break;
323                 case MISDN_GEN_TRACE_DIR:       GET_GENCFG_STRCPY(trace_dir);
324                                                 break;
325                 case MISDN_GEN_BRIDGING:        GET_GENCFG_MEMCPY(bridging);
326                                                 break;
327                 case MISDN_GEN_STOP_TONE:       GET_GENCFG_MEMCPY(stop_tone_after_first_digit);
328                                                 break;
329                 case MISDN_GEN_APPEND_DIGITS2EXTEN: 
330                                                 GET_GENCFG_MEMCPY(append_digits2exten);
331                                                 break;
332                 case MISDN_GEN_L1_INFO_OK:      GET_GENCFG_MEMCPY(l1_info_ok);
333                                                 break;
334                 case MISDN_GEN_CLEAR_L3:        GET_GENCFG_MEMCPY(clear_l3);
335                                                 break;
336                 case MISDN_GEN_DYNAMIC_CRYPT:   GET_GENCFG_MEMCPY(dynamic_crypt);
337                                                 break;
338                 case MISDN_GEN_CRYPT_PREFIX:    GET_GENCFG_STRCPY(crypt_prefix);
339                                                 break;
340                 case MISDN_GEN_CRYPT_KEYS:      GET_GENCFG_STRCPY(crypt_keys);
341                                                 break;
342                 default:                        memset(buf, 0, bufsize);
343         }
344         
345         misdn_cfg_unlock();
346 }
347
348 int misdn_cfg_is_msn_valid (int port, char* msn) {
349         
350         if (!misdn_cfg_is_port_valid(port)) {
351                 ast_log(LOG_WARNING, "Invalid call to misdn_cfg_is_msn_valid! Port number %d is not valid.\n", port);
352                 return 0;
353         }
354         
355         struct msn_list *iter;
356         
357         misdn_cfg_lock();
358         
359         if (port_cfg[port]->msn_list)
360                 iter = port_cfg[port]->msn_list;
361         else
362                 iter = port_cfg[0]->msn_list;
363         for (; iter; iter = iter->next) 
364                 if (*(iter->msn) == '*' || !strcasecmp(iter->msn, msn)) {
365                         misdn_cfg_unlock();
366                         return 1;
367                 }
368         
369         misdn_cfg_unlock();
370         
371         return 0;
372 }
373
374 int misdn_cfg_is_port_valid (int port) {
375         
376         misdn_cfg_lock();
377         
378         if (port < 1 || port > max_ports) {
379                 misdn_cfg_unlock();
380                 return 0;
381         }
382
383         int valid = (port_cfg[port] != NULL);
384
385         misdn_cfg_unlock();
386
387         return valid;
388 }
389
390 int misdn_cfg_is_group_method (char *group, enum misdn_cfg_method meth) {
391
392         int i, re = 0;
393         char *method = NULL;
394
395         misdn_cfg_lock();
396         
397         for (i = 0; i < max_ports; i++) {
398                 if (port_cfg[i]) {
399                         if (!strcasecmp(port_cfg[i]->name, group))
400                                 method = port_cfg[i]->method ? port_cfg[i]->method : port_cfg[0]->method;
401                 }
402         }
403
404         if (method) {
405                 switch (meth) {
406                 case METHOD_STANDARD:           re = !strcasecmp(method, "standard");
407                                                                         break;
408                 case METHOD_ROUND_ROBIN:        re = !strcasecmp(method, "round_robin");
409                                                                         break;
410                 }
411         }
412
413         misdn_cfg_unlock();
414
415         return re;
416 }
417
418 void misdn_cfg_get_ports_string (char *ports) {
419         *ports = 0;
420         char tmp[16];
421         int l;
422         
423         misdn_cfg_lock();
424
425         int i = 1;
426         for (; i <= max_ports; i++) {
427                 if (port_cfg[i]) {
428                         if (ptp[i])
429                                 sprintf(tmp, "%dptp,", i);
430                         else
431                                 sprintf(tmp, "%d,", i);
432                         strcat(ports, tmp);
433                 }
434         }
435         
436         misdn_cfg_unlock();
437
438         if ((l = strlen(ports)))
439                 ports[l-1] = 0;
440 }
441
442 #define GET_CFG_STRING(typestr, type) ({ \
443                 if (port_cfg[port] && port_cfg[port]->type) \
444                         snprintf(buf, bufsize, "%s " #typestr ": %s", begin, port_cfg[port]->type); \
445                 else \
446                         snprintf(buf, bufsize, "%s " #typestr ": %s", begin, port_cfg[0]->type); \
447         }) \
448
449 #define GET_GEN_STRING(typestr, type) ({ \
450                 snprintf(buf, bufsize, "%s " #typestr ": %s", begin, general_cfg->type ? general_cfg->type : "not set"); \
451         }) \
452
453 #define GET_CFG_INT(typestr, type) ({ \
454                 if (port_cfg[port] && port_cfg[port]->type) \
455                         snprintf(buf, bufsize, "%s " #typestr ": %d", begin, *port_cfg[port]->type); \
456                 else \
457                         snprintf(buf, bufsize, "%s " #typestr ": %d", begin, *port_cfg[0]->type); \
458         }) \
459
460 #define GET_GEN_INT(typestr, type) ({ \
461                 snprintf(buf, bufsize, "%s " #typestr ": %d", begin, general_cfg->type ? *general_cfg->type : 0); \
462         }) \
463
464 #define GET_CFG_BOOL(typestr, type, yes, no) ({ \
465                 int bool; \
466                 if (port_cfg[port] && port_cfg[port]->type) \
467                         bool = *port_cfg[port]->type; \
468                 else \
469                         bool = *port_cfg[0]->type; \
470                 snprintf(buf, bufsize, "%s " #typestr ": %s", begin, bool ? #yes : #no); \
471         }) \
472
473 #define GET_CFG_HYBRID(typestr, type, yes, no) ({ \
474                 int bool; \
475                 if (port_cfg[port] && port_cfg[port]->type) \
476                         bool = *port_cfg[port]->type; \
477                 else \
478                         bool = *port_cfg[0]->type; \
479                 if (bool == 1 || bool == 0) \
480                         snprintf(buf, bufsize, "%s " #typestr ": %s", begin, bool ? #yes : #no); \
481                 else \
482                         snprintf(buf, bufsize, "%s " #typestr ": %d", begin, bool); \
483         }) \
484
485 #define GET_GEN_BOOL(typestr, type, yes, no) ({ \
486                 snprintf(buf, bufsize, "%s " #typestr ": %s", begin, general_cfg->type ? (*general_cfg->type ? #yes : #no) : "not set"); \
487         }) \
488
489 #define GET_CFG_AST_GROUP_T(typestr, type) ({ \
490                 ast_group_t *tmp; \
491                 if (port_cfg[port] && port_cfg[port]->type) \
492                         tmp = port_cfg[port]->type; \
493                 else \
494                         tmp = port_cfg[0]->type; \
495                 if (tmp) { \
496                         char tmpbuf[256]; \
497                         snprintf(buf, bufsize, "%s " #typestr ": %s", begin, ast_print_group(tmpbuf, sizeof(tmpbuf), *tmp)); \
498                 } else \
499                         snprintf(buf, bufsize, "%s " #typestr ": %s", begin, "none"); \
500         }) \
501
502 void misdn_cfg_get_config_string(int port, enum misdn_cfg_elements elem, char* buf, int bufsize) {
503
504         if (!(elem > MISDN_GEN_FIRST) && !misdn_cfg_is_port_valid(port)) {
505                 *buf = 0;
506                 ast_log(LOG_WARNING, "Invalid call to misdn_cfg_get_config_string! Port number %d is not valid.\n", port);
507                 return;
508         }
509         
510         char begin[] = " -> ";
511         
512         misdn_cfg_lock();
513         
514         switch (elem) {
515                 
516                 case MISDN_CFG_PTP:             snprintf(buf, bufsize, "%s PTP: %s", begin, ptp[port] ? "yes" : "no");
517                                                                         break;
518                 case MISDN_CFG_GROUPNAME:       GET_CFG_STRING(GROUPNAME, name);
519                                                                         break;
520                 case MISDN_CFG_RXGAIN:          GET_CFG_INT(RXGAIN, rxgain);
521                                                                         break;
522                 case MISDN_CFG_TXGAIN:          GET_CFG_INT(TXGAIN, txgain);
523                                                                         break;
524                 case MISDN_CFG_TE_CHOOSE_CHANNEL:
525                                                 GET_CFG_BOOL(TE_CHOOSE_CHANNEL, te_choose_channel, yes, no);
526                                                                         break;
527                 case MISDN_CFG_CONTEXT:         GET_CFG_STRING(CONTEXT, context);
528                                                                         break;
529                 case MISDN_CFG_LANGUAGE:        GET_CFG_STRING(LANGUAGE, language);
530                                                                         break;
531                 case MISDN_CFG_CALLERID:        GET_CFG_STRING(CALLERID, callerid);
532                                                                         break;
533                 case MISDN_CFG_METHOD:          GET_CFG_STRING(METHOD, method);
534                                                                         break;
535                 case MISDN_CFG_DIALPLAN:        GET_CFG_INT(DIALPLAN, dialplan);
536                                                                         break;
537                 case MISDN_CFG_NATPREFIX:       GET_CFG_STRING(NATIONALPREFIX, nationalprefix);
538                                                                         break;
539                 case MISDN_CFG_INTERNATPREFIX:
540                                                 GET_CFG_STRING(INTERNATIONALPREFIX, internationalprefix);
541                                                                         break;
542                 case MISDN_CFG_PRES:            GET_CFG_BOOL(PRESENTATION, pres, allowed, not_screened);
543                                                                         break;
544                 case MISDN_CFG_ALWAYS_IMMEDIATE:
545                                                 GET_CFG_BOOL(ALWAYS_IMMEDIATE, always_immediate, yes, no);
546                                                                         break;
547                 case MISDN_CFG_IMMEDIATE:       GET_CFG_BOOL(IMMEDIATE, immediate, yes, no);
548                                                                         break;
549                 case MISDN_CFG_HOLD_ALLOWED:
550                                                 GET_CFG_BOOL(HOLD_ALLOWED, hold_allowed, yes, no);
551                                                                         break;
552                 case MISDN_CFG_EARLY_BCONNECT:
553                                                 GET_CFG_BOOL(EARLY_BCONNECT, early_bconnect, yes, no);
554                                                                         break;
555                 case MISDN_CFG_USE_CALLINGPRES:
556                                                 GET_CFG_BOOL(USE_CALLINGPRES, use_callingpres, yes, no);
557                                                                         break;
558                 case MISDN_CFG_ECHOCANCEL:      GET_CFG_HYBRID(ECHOCANCEL, echocancel, yes, no);
559                                                                         break;
560                 case MISDN_CFG_ECHOCANCELWHENBRIDGED:
561                                                 GET_CFG_BOOL(ECHOCANCELWHENBRIDGED, echocancelwhenbridged, yes, no);
562                                                                         break;
563                 case MISDN_CFG_ECHOTRAINING:
564                                                 GET_CFG_HYBRID(ECHOTRAINING, echotraining, yes, no);
565                                                                         break;
566                 case MISDN_CFG_CALLGROUP:       GET_CFG_AST_GROUP_T(CALLINGGROUP, callgroup);
567                                                                         break;
568                 case MISDN_CFG_PICKUPGROUP:     GET_CFG_AST_GROUP_T(PICKUPGROUP, pickupgroup);
569                                                                         break;
570                 case MISDN_CFG_MSNS:            {
571                                                         char tmpbuffer[BUFFERSIZE];
572                                                         tmpbuffer[0] = 0;
573                                                         struct msn_list *iter;
574                                                         if (port_cfg[port]->msn_list)
575                                                                 iter = port_cfg[port]->msn_list;
576                                                         else
577                                                                 iter = port_cfg[0]->msn_list;
578                                                         if (iter) {
579                                                                 for (; iter; iter = iter->next)
580                                                                         sprintf(tmpbuffer, "%s%s, ", tmpbuffer, iter->msn);
581                                                                 tmpbuffer[strlen(tmpbuffer)-2] = 0;
582                                                         }
583                                                         snprintf(buf, bufsize, "%s MSNs: %s", begin, *tmpbuffer ? tmpbuffer : "none"); \
584                                                 }
585                                                 break;
586                 
587                 /* general config elements */
588                 
589                 case MISDN_GEN_DEBUG:           GET_GEN_INT(DEBUG_LEVEL, debug);
590                                                                         break;
591                 case MISDN_GEN_TRACEFILE:       GET_GEN_STRING(TRACEFILE, tracefile);
592                                                                         break;
593                 case MISDN_GEN_TRACE_CALLS: GET_GEN_BOOL(TRACE_CALLS, trace_calls, true, false);
594                                                                         break;
595                 case MISDN_GEN_TRACE_DIR:       GET_GEN_STRING(TRACE_DIR, trace_dir);
596                                                                         break;
597                 case MISDN_GEN_BRIDGING:        GET_GEN_BOOL(BRIDGING, bridging, yes, no);
598                                                                         break;
599                 case MISDN_GEN_STOP_TONE:       GET_GEN_BOOL(STOP_TONE_AFTER_FIRST_DIGIT, stop_tone_after_first_digit, yes, no);
600                                                                         break;
601                 case MISDN_GEN_APPEND_DIGITS2EXTEN: 
602                                                 GET_GEN_BOOL(APPEND_DIGITS2EXTEN, append_digits2exten, yes, no);
603                                                                         break;
604                 case MISDN_GEN_L1_INFO_OK:      GET_GEN_BOOL(L1_INFO_OK, l1_info_ok, yes, no);
605                                                                         break;
606                 case MISDN_GEN_CLEAR_L3:        GET_GEN_BOOL(CLEAR_L3, clear_l3, yes, no);
607                                                                         break;
608                 case MISDN_GEN_DYNAMIC_CRYPT:
609                                                 GET_GEN_BOOL(DYNAMIC_CRYPT,dynamic_crypt, yes, no);
610                                                                         break;
611                 case MISDN_GEN_CRYPT_PREFIX:
612                                                 GET_GEN_STRING(CRYPT_PREFIX, crypt_prefix);
613                                                                         break;
614                 case MISDN_GEN_CRYPT_KEYS:      GET_GEN_STRING(CRYPT_KEYS, crypt_keys);
615                                                                         break;
616                                                                         
617                 default:                        *buf = 0;
618                                                                         break;
619         }
620
621         misdn_cfg_unlock();
622 }
623
624 int misdn_cfg_get_next_port (int port) {
625         
626         misdn_cfg_lock();
627         
628         for (port++; port <= max_ports; port++) {
629                 if (port_cfg[port]) {
630                         misdn_cfg_unlock();
631                         return port;
632                 }
633         }
634         
635         misdn_cfg_unlock();
636         
637         return -1;
638 }
639
640 int misdn_cfg_get_next_port_spin (int port) {
641
642         int ret = misdn_cfg_get_next_port(port);
643
644         if (ret > 0)
645                 return ret;
646
647         return misdn_cfg_get_next_port(0);
648 }
649
650 #define PARSE_GEN_INT(item) ({ \
651                 if (!strcasecmp(v->name, #item)) { \
652                         int temp; \
653                         if (!sscanf(v->value, "%d", &temp)) { \
654                                 ast_log(LOG_WARNING, "Value \"%s\" for \"" #item "\" (generals section) invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", v->value); \
655                         } else { \
656                                 general_cfg->item = (int *)malloc(sizeof(int)); \
657                                 memcpy(general_cfg->item, &temp, sizeof(int)); \
658                         } \
659                         continue; \
660                 } \
661         }) \
662
663 #define PARSE_GEN_BOOL(item) ({ \
664                 if (!strcasecmp(v->name, #item)) { \
665                         general_cfg->item = (int *)malloc(sizeof(int)); \
666                         *(general_cfg->item) = ast_true(v->value)?1:0; \
667                         continue; \
668                 } \
669         })
670
671 #define PARSE_GEN_STR(item) ({ \
672                 if (!strcasecmp(v->name, #item)) { \
673                         int l = strlen(v->value); \
674                         if (l) { \
675                                 general_cfg->item = (char *)calloc(l+1, sizeof(char)); \
676                                 strncpy(general_cfg->item,v->value, l); \
677                         } \
678                         continue; \
679                 } \
680         })
681
682 static void build_general_config(struct ast_variable *v) {
683
684         if (!v) 
685                 return;
686
687         for (; v; v = v->next) {
688
689                 PARSE_GEN_INT(debug);
690                 PARSE_GEN_STR(tracefile);
691                 PARSE_GEN_BOOL(trace_calls);
692                 PARSE_GEN_STR(trace_dir);
693                 PARSE_GEN_BOOL(bridging);
694                 PARSE_GEN_BOOL(stop_tone_after_first_digit);
695                 PARSE_GEN_BOOL(append_digits2exten);
696                 PARSE_GEN_BOOL(l1_info_ok);
697                 PARSE_GEN_BOOL(clear_l3);
698                 PARSE_GEN_BOOL(dynamic_crypt);
699                 PARSE_GEN_STR(crypt_prefix);
700                 PARSE_GEN_STR(crypt_keys);
701                 
702         }
703 }
704
705 #define PARSE_CFG_HYBRID(item, def) ({ \
706                 if (!strcasecmp(v->name, #item)) { \
707                         new->item = (int *)malloc(sizeof(int)); \
708                         if (!sscanf(v->value, "%d", new->item)) { \
709                                 if (ast_true(v->value)) \
710                                         *new->item = def; \
711                                 else \
712                                         *new->item = 0; \
713                         } \
714                         continue; \
715                 } \
716         }) \
717
718 #define PARSE_CFG_INT(item) ({ \
719                 if (!strcasecmp(v->name, #item)) { \
720                         new->item = (int *)malloc(sizeof(int)); \
721                         if (!sscanf(v->value, "%d", new->item)) { \
722                                 ast_log(LOG_WARNING, "Value \"%s\" for \"" #item "\" of group \"%s\" invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", v->value, cat); \
723                                 free(new->item); \
724                                 new->item = NULL; \
725                         } \
726                         continue; \
727                 } \
728         }) \
729
730 #define PARSE_CFG_BOOL(item) ({ \
731                 if (!strcasecmp(v->name, #item)) { \
732                         new->item = (int *)malloc(sizeof(int)); \
733                         *(new->item) = ast_true(v->value)?1:0; \
734                         continue; \
735                 } \
736         })
737
738 #define PARSE_CFG_STR(item) ({ \
739                 if (!strcasecmp(v->name, #item)) { \
740                         int l = strlen(v->value); \
741                         if (l) { \
742                                 new->item = (char *)calloc(l+1, sizeof(char)); \
743                                 strncpy(new->item,v->value,l); \
744                         } \
745                         continue; \
746                 } \
747         })
748
749 static void build_port_config(struct ast_variable *v, char *cat) {
750         if (!v || !cat)
751                 return;
752
753         int cfg_for_ports[max_ports + 1];
754         int i = 0;
755         for (; i < (max_ports + 1); i++) {
756                 cfg_for_ports[i] = 0;
757         }
758         
759         /* we store the default config at position 0 */
760         if (!strcasecmp(cat, "default")) {
761                 cfg_for_ports[0] = 1;
762         }
763
764         struct port_config* new = (struct port_config *)calloc(1, sizeof(struct port_config));
765         
766         {
767                 int l = strlen(cat);
768                 new->name = (char *)calloc(l+1, sizeof(char));
769                 strncpy(new->name, cat, l);
770         }
771         
772         for (; v; v=v->next) {
773                 if (!strcasecmp(v->name, "ports")) {
774                         /* TODO check for value not beeing set, like PORTS= */
775                         char *iter;
776                         char *value = v->value;
777                         while ((iter = strchr(value, ',')) != NULL) {
778                                 *iter = 0;
779                                 /* strip spaces */
780                                 while (*value && *value == ' ') {
781                                         value++;
782                                 }
783                                 /* TODO check for char not 0-9 */
784
785                                 if (*value){
786                                         int p = atoi(value);
787                                         if (p <= max_ports && p > 0) {
788                                                 cfg_for_ports[p] = 1;
789                                                 if (strstr(value, "ptp"))
790                                                         ptp[p] = 1;
791                                         } else
792                                                 ast_log(LOG_WARNING, "Port value \"%s\" of group %s invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", value, cat);
793                                         value = ++iter;
794                                 }
795                         }
796                         /* the remaining or the only one */
797                         /* strip spaces */
798                         while (*value && *value == ' ') {
799                                 value++;
800                         }
801                         /* TODO check for char not 0-9 */
802                         if (*value) {
803                                 int p = atoi(value);
804                                 if (p <= max_ports && p > 0) {
805                                         cfg_for_ports[p] = 1;
806                                         if (strstr(value, "ptp"))
807                                                 ptp[p] = 1;
808                                 } else
809                                         ast_log(LOG_WARNING, "Port value \"%s\" of group %s invalid or out of range! Please edit your misdn.conf and then do a \"misdn reload\".\n", value, cat);
810                         }
811                         continue;
812                 }
813                 PARSE_CFG_STR(context);
814                 PARSE_CFG_INT(dialplan);
815                 PARSE_CFG_STR(nationalprefix);
816                 PARSE_CFG_STR(internationalprefix);
817                 PARSE_CFG_STR(language);
818                 if (!strcasecmp(v->name, "presentation")) {
819                         if (v->value && strlen(v->value)) {
820                                 new->pres = (int *)malloc(sizeof(int));
821                                 if (!strcasecmp(v->value, "allowed")) {
822                                         *(new->pres) = 1;
823                                 }
824                                 /* TODO: i assume if it is not "allowed", it is "not_screened" */
825                                 else {
826                                         *(new->pres) = 0;
827                                 }
828                         }
829                         continue;
830                 }
831                 PARSE_CFG_INT(rxgain);
832                 PARSE_CFG_INT(txgain);
833                 PARSE_CFG_BOOL(te_choose_channel);
834                 PARSE_CFG_BOOL(immediate);
835                 PARSE_CFG_BOOL(always_immediate);
836                 PARSE_CFG_BOOL(hold_allowed);
837                 PARSE_CFG_BOOL(early_bconnect);
838                 PARSE_CFG_BOOL(use_callingpres);
839                 PARSE_CFG_HYBRID(echocancel, DEF_ECHOCANCEL);
840                 PARSE_CFG_BOOL(echocancelwhenbridged);
841                 PARSE_CFG_HYBRID(echotraining, DEF_ECHOTRAINING);
842                 PARSE_CFG_STR(callerid);
843                 PARSE_CFG_STR(method);
844                 if (!strcasecmp(v->name, "msns")) {
845                         /* TODO check for value not beeing set, like msns= */
846                         char *iter;
847                         char *value = v->value;
848
849                         while ((iter = strchr(value, ',')) != NULL) {
850                                 *iter = 0;
851                                 /* strip spaces */
852                                 while (*value && *value == ' ') {
853                                         value++;
854                                 }
855                                 /* TODO check for char not 0-9 */
856                                 if (*value){
857                                         int l = strlen(value);
858                                         if (l) {
859                                                 struct msn_list *ml = (struct msn_list *)calloc(1, sizeof(struct msn_list));
860                                                 ml->msn = (char *)calloc(l+1, sizeof(char));
861                                                 strncpy(ml->msn,value,l);
862                                                 ml->next = new->msn_list;
863                                                 new->msn_list = ml;
864                                         }
865                                         value = ++iter;
866                                 }
867                         }
868                         /* the remaining or the only one */
869                         /* strip spaces */
870                         while (*value && *value == ' ') {
871                                 value++;
872                         }
873                         /* TODO check for char not 0-9 */
874                         if (*value) {
875                                 int l = strlen(value);
876                                 if (l) {
877                                         struct msn_list *ml = (struct msn_list *)calloc(1, sizeof(struct msn_list));
878                                         ml->msn = (char *)calloc(l+1, sizeof(char));
879                                         strncpy(ml->msn,value,l);
880                                         ml->next = new->msn_list;
881                                         new->msn_list = ml;
882                                 }
883                         }
884                         continue;
885                 }
886                 if (!strcasecmp(v->name, "callgroup")) {
887                         new->callgroup = (ast_group_t *)malloc(sizeof(ast_group_t));
888                         *(new->callgroup)=ast_get_group(v->value);
889                         continue;
890                 }
891                 if (!strcasecmp(v->name, "pickupgroup")) {
892                         new->pickupgroup = (ast_group_t *)malloc(sizeof(ast_group_t));
893                         *(new->pickupgroup)=ast_get_group(v->value);
894                         continue;
895                 }
896         }
897         /* store the new config in our array of port configs */
898         for (i = 0; i < (max_ports + 1); i++) {
899                 if (cfg_for_ports[i])
900                         port_cfg[i] = new;
901         }
902 }
903
904
905 static void fill_defaults (void) {
906         
907         /* general defaults */
908         if (!general_cfg->debug)
909                 general_cfg->debug = (int*)calloc(1, sizeof(int));
910         if (!general_cfg->trace_calls)
911                 general_cfg->trace_calls = (int*)calloc(1, sizeof(int));
912         if (!general_cfg->trace_dir) {
913                 general_cfg->trace_dir = (char *)malloc(10 * sizeof(char));
914                 sprintf(general_cfg->trace_dir, "/var/log/");
915         }
916         if (!general_cfg->bridging) {
917                 general_cfg->bridging = (int*)malloc(sizeof(int));
918                 *general_cfg->bridging = 1;
919         }
920         if (!general_cfg->stop_tone_after_first_digit) {
921                 general_cfg->stop_tone_after_first_digit = (int*)malloc(sizeof(int));
922                 *general_cfg->stop_tone_after_first_digit = 1;
923         }
924         if (!general_cfg->append_digits2exten) {
925                 general_cfg->append_digits2exten = (int*)malloc(sizeof(int));
926                 *general_cfg->append_digits2exten = 1;
927         }
928         if (!general_cfg->l1_info_ok) {
929                 general_cfg->l1_info_ok = (int*)malloc(sizeof(int));
930                 *general_cfg->l1_info_ok = 1;
931         }
932         if (!general_cfg->clear_l3)
933                 general_cfg->clear_l3 =(int*)calloc(1, sizeof(int));
934         if (!general_cfg->dynamic_crypt)
935                 general_cfg->dynamic_crypt = (int*)calloc(1, sizeof(int));
936
937         /* defaults for default port config */
938         if (!port_cfg[0])
939                 port_cfg[0] = (struct port_config*)calloc(1, sizeof(struct port_config));
940         if (!port_cfg[0]->name) {
941                 port_cfg[0]->name = (char *)malloc(8 * sizeof(char));
942                 sprintf(port_cfg[0]->name, "default");
943         }
944         if (!port_cfg[0]->rxgain)
945                 port_cfg[0]->rxgain = (int *)calloc(1, sizeof(int));
946         if (!port_cfg[0]->txgain)
947                 port_cfg[0]->txgain = (int *)calloc(1, sizeof(int));
948         if (!port_cfg[0]->te_choose_channel)
949                 port_cfg[0]->te_choose_channel = (int *)calloc(1, sizeof(int));
950         if (!port_cfg[0]->context) {
951                 port_cfg[0]->context = (char *)malloc(8 * sizeof(char));
952                 sprintf(port_cfg[0]->context, "default");
953         }
954         if (!port_cfg[0]->language) {
955                 port_cfg[0]->language = (char *)malloc(3 * sizeof(char));
956                 sprintf(port_cfg[0]->language, "en");
957         }
958         if (!port_cfg[0]->callerid)
959                 port_cfg[0]->callerid = (char *)calloc(1, sizeof(char));
960         if (!port_cfg[0]->method) {
961                 port_cfg[0]->method = (char *)malloc(9 * sizeof(char));
962                 sprintf(port_cfg[0]->method, "standard");
963         }
964         if (!port_cfg[0]->dialplan)
965                 port_cfg[0]->dialplan = (int *)calloc(1, sizeof(int));
966         if (!port_cfg[0]->nationalprefix) {
967                 port_cfg[0]->nationalprefix = (char *)malloc(2 * sizeof(char));
968                 sprintf(port_cfg[0]->nationalprefix, "0");
969         }
970         if (!port_cfg[0]->internationalprefix) {
971                 port_cfg[0]->internationalprefix = (char *)malloc(3 * sizeof(char));
972                 sprintf(port_cfg[0]->internationalprefix, "00");
973         }
974         if (!port_cfg[0]->pres) {
975                 port_cfg[0]->pres = (int *)malloc(sizeof(int));
976                 *port_cfg[0]->pres = 1;
977         }
978         if (!port_cfg[0]->always_immediate)
979                 port_cfg[0]->always_immediate = (int *)calloc(1, sizeof(int));
980         if (!port_cfg[0]->immediate)
981                 port_cfg[0]->immediate = (int *)calloc(1, sizeof(int));
982         if (!port_cfg[0]->hold_allowed)
983                 port_cfg[0]->hold_allowed = (int *)calloc(1, sizeof(int));
984         if (!port_cfg[0]->early_bconnect) {
985                 port_cfg[0]->early_bconnect = (int *)malloc(sizeof(int));
986                 *port_cfg[0]->early_bconnect = 1;
987         }
988         if (!port_cfg[0]->echocancel)
989                 port_cfg[0]->echocancel=(int *)calloc(1, sizeof(int));
990         if (!port_cfg[0]->echocancelwhenbridged)
991                 port_cfg[0]->echocancelwhenbridged=(int *)calloc(1, sizeof(int));
992         if (!port_cfg[0]->echotraining) {
993                 port_cfg[0]->echotraining=(int *)malloc(sizeof(int));
994                 *port_cfg[0]->echotraining = 1;
995         }
996         if (!port_cfg[0]->use_callingpres) {
997                 port_cfg[0]->use_callingpres = (int *)malloc(sizeof(int));
998                 *port_cfg[0]->use_callingpres = 1;
999         }
1000         if (!port_cfg[0]->msn_list) {
1001                 port_cfg[0]->msn_list = (struct msn_list *)malloc(sizeof(struct msn_list));
1002                 port_cfg[0]->msn_list->next = NULL;
1003                 port_cfg[0]->msn_list->msn = (char *)calloc(2, sizeof(char));
1004                 *(port_cfg[0]->msn_list->msn) = '*';
1005         }
1006 }
1007
1008 void misdn_cfg_reload (void) {
1009         misdn_cfg_init (0);
1010 }
1011
1012 void misdn_cfg_destroy (void) {
1013
1014         misdn_cfg_lock();
1015         
1016         free_port_cfg();
1017         free_general_cfg();
1018         
1019         free(port_cfg);
1020         free(general_cfg);
1021         free(ptp);
1022
1023         misdn_cfg_unlock();
1024         ast_mutex_destroy(&config_mutex);
1025 }
1026
1027 void misdn_cfg_init (int this_max_ports)
1028 {
1029         char config[]="misdn.conf";
1030         
1031         struct ast_config *cfg;
1032         cfg = AST_LOAD_CFG(config);
1033         if (!cfg) {
1034                 ast_log(LOG_WARNING,"no misdn.conf ?\n");
1035                 return;
1036         }
1037
1038         misdn_cfg_lock();
1039         
1040         if (this_max_ports) {
1041                 /* this is the first run */
1042                 max_ports = this_max_ports;
1043                 port_cfg = (struct port_config **)calloc(max_ports + 1, sizeof(struct port_config *));
1044                 general_cfg = (struct general_config*)calloc(1, sizeof(struct general_config));
1045                 ptp = (int *)calloc(max_ports + 1, sizeof(int));
1046         }
1047         else {
1048                 free_port_cfg();
1049                 free_general_cfg();
1050                 port_cfg = memset(port_cfg, 0, sizeof(struct port_config *) * (max_ports + 1));
1051                 general_cfg = memset(general_cfg, 0, sizeof(struct general_config));
1052                 ptp = memset(ptp, 0, sizeof(int) * (max_ports + 1));
1053         }
1054         
1055         char *cat;
1056         cat = ast_category_browse(cfg, NULL);
1057
1058         while(cat) {
1059                 struct ast_variable *v=ast_variable_browse(cfg,cat);
1060                 if (!strcasecmp(cat,"general")) {
1061                         build_general_config (v);
1062                 } else {
1063                         build_port_config (v, cat);
1064                 }
1065                 cat=ast_category_browse(cfg,cat);
1066         }
1067
1068         fill_defaults();
1069         
1070         misdn_cfg_unlock();
1071         
1072         AST_DESTROY_CFG(cfg);
1073 }