backup work
[lcr.git] / options.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** reading options.conf and filling structure                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <stdlib.h>
15 #include "main.h"
16
17 struct options options = {
18         "/usr/local/pbx/log",           /* log file */
19         {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
20          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
21          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
22          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
23          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
24          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
25          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
26          0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0},
27         0x0000,                         /* debug mode */
28         'a',                            /* a-law */
29         "0",                            /* national prefix */
30         "00",                           /* international prefix */
31         "tones_american",               /* directory of tones */
32         "",                             /* directories of tones to fetch */
33         "extensions",                   /* directory of extensions */
34         "",                             /* h323 endpoint name */
35         0,                              /* h323 ringconnect */
36         0,4, 0,2, 0, 0, 0, 0,4, 0,4, 0,64, /* h323 codecs to use */
37         0,"",1720,                      /* allow incoming h323 calls */
38         0,"",                           /* register with h323 gatekeeper */
39         5060, 5,                        /* SIP port, maxqueue */
40         0,                              /* dtmf detection on */
41         "",                             /* dummy caller id */
42         0,                              /* use tones by dsp.o */
43         0,                              /* by default use priority 0 */
44         "pbx@jolly.de"                  /* source mail adress */
45 };
46
47 /* read options
48  *
49  * read options from options.conf
50  */
51 int read_options(void)
52 {
53         FILE *fp=NULL;
54         char filename[128];
55         char *p;
56         char option[32];
57         char param[256];
58         unsigned int line,i;
59         char buffer[256];
60 #ifdef H323
61         int codecpri = 0;
62 #endif
63
64         SPRINT(filename, "%s/options.conf", INSTALL_DATA);
65
66         if (!(fp=fopen(filename,"r")))
67         {
68                 PERROR("Cannot open %s\n",filename);
69                 return(-1);
70         }
71
72         line=0;
73         while((fgets(buffer,sizeof(buffer),fp)))
74         {
75                 line++;
76                 buffer[sizeof(buffer)-1]=0;
77                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
78                 p=buffer;
79
80                 while(*p <= 32) /* skip spaces */
81                 {
82                         if (*p == 0)
83                                 break;
84                         p++;
85                 }
86                 if (*p==0 || *p=='#') /* ignore comments and empty line */
87                         continue;
88
89                 option[0]=0;
90                 i=0; /* read option */
91                 while(*p > 32)
92                 {
93                         if (i+1 >= sizeof(option))
94                         {
95                                 PERROR_RUNTIME("Error in %s (line %d): option too long.\n",filename,line);
96                                 goto error;
97                         }
98                         option[i+1] = '\0';
99                         option[i++] = *p++;
100                 }
101
102                 while(*p <= 32) /* skip spaces */
103                 {
104                         if (*p == 0)
105                                 break;
106                         p++;
107                 }
108
109                 param[0]=0;
110                 if (*p!=0 && *p!='#') /* param */
111                 {
112                         i=0; /* read param */
113                         while(*p > 31)
114                         {
115                                 if (i+1 >= sizeof(param))
116                                 {
117                                         PERROR_RUNTIME("Error in %s (line %d): param too long.\n",filename,line);
118                                         goto error;
119                                 }
120                                 param[i+1] = '\0';
121                                 param[i++] = *p++;
122                         }
123                 }
124
125                 /* at this point we have option and param */
126
127                 /* check option */
128                 if (!strcmp(option,"nt_if") || !strcmp(option,"te_if"))
129                 {
130                         PERROR_RUNTIME("Error in %s (line %d): obsolete option %s. Use multiple 'port' options to define ports to use.\n",filename,line,option);
131                         goto error;
132                 } else
133                 if (!strcmp(option,"debug"))
134                 {
135                         if (param[0]==0)
136                         {
137                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
138                                 goto error;
139                         }
140                         options.deb = strtol(param, NULL, 0);
141
142                         PDEBUG(DEBUG_CONFIG, "debugging: 0x%x\n", options.deb);
143                 } else
144                 if (!strcmp(option,"log"))
145                 {
146                         if (param[0]==0)
147                         {
148                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
149                                 goto error;
150                         }
151                         SCPY(options.log, param);
152
153                         PDEBUG(DEBUG_CONFIG, "log file: %s\n", options.log);
154                 } else
155                 if (!strcmp(option,"port"))
156                 {
157                         i = strtol(param, NULL, 0);
158                         if (i < 1 || i > sizeof(options.ports))
159                         {
160                                 PERROR_RUNTIME("Error in %s (line %d): port number %s out of range.\n", filename, line, option);
161                                 goto error;
162                         }
163                         options.ports[i] |= FLAG_PORT_USE;
164
165                         PDEBUG(DEBUG_CONFIG, "adding interface: %d (param=%s)\n", i, param);
166                         if (strstr(param, "ptp"))
167                         {
168                                 options.ports[i] |= FLAG_PORT_PTP;
169                                 PDEBUG(DEBUG_CONFIG, " -> interface shall be ptp\n");
170                         }
171                 } else
172 #if 0
173                 if (!strcmp(option,"ptp"))
174                 {
175                         options.ptp = 1;
176
177                         PDEBUG(DEBUG_CONFIG, "ptp layer-2 watch and keep established.\n");
178                 } else
179 #endif
180                 if (!strcmp(option,"alaw"))
181                 {
182                         options.law = 'a';
183
184                         PDEBUG(DEBUG_CONFIG, "isdn audio type: alaw\n");
185                 } else
186                 if (!strcmp(option,"ulaw"))
187                 {
188                         options.law = 'u';
189
190                         PDEBUG(DEBUG_CONFIG, "isdn audio type: ulaw\n");
191                 } else
192                 if (!strcmp(option,"h323_name"))
193                 {
194 #ifdef H323
195                         SCPY(options.h323_name, param);
196
197                         PDEBUG(DEBUG_CONFIG, "H323 endpoint name: '%s'\n", param);
198 #endif
199                 } else
200                 if (!strcmp(option,"h323_ringconnect"))
201                 {
202 #ifdef H323
203                         options.h323_ringconnect = 1;
204
205                         PDEBUG(DEBUG_CONFIG, "H323 ringconnect: enabled\n");
206 #endif
207                 } else
208                 if (!strcmp(option,"h323_gsm"))
209                 {
210 #ifdef H323
211                         codecpri ++;
212                         options.h323_gsm_pri = codecpri;
213                         options.h323_gsm_opt = atoi(param);
214                         if (atoi(param)<1 && atoi(param)>7)
215                         {
216                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be in range 1..7.\n",filename,line,option);
217                                 goto error;
218                         }
219
220                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: GSM, MicrosoftGSM priority %d\n", codecpri);
221 #endif
222                 } else
223                 if (!strcmp(option,"h323_g726"))
224                 {
225 #ifdef H323
226                         codecpri ++;
227                         options.h323_g726_pri = codecpri;
228                         options.h323_g726_opt = atoi(param);
229                         if (atoi(param)<2 && atoi(param)>5)
230                         {
231                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be in range 2..5.\n",filename,line,option);
232                                 goto error;
233                         }
234
235                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: G726 priority %d\n", codecpri);
236 #endif
237                 } else
238                 if (!strcmp(option,"h323_g7231"))
239                 {
240 #ifdef H323
241                         codecpri ++;
242                         options.h323_g7231_pri = codecpri;
243
244                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: G7231 priority %d\n", codecpri);
245 #endif
246                 } else
247                 if (!strcmp(option,"h323_g729a"))
248                 {
249 #ifdef H323
250                         codecpri ++;
251                         options.h323_g729a_pri = codecpri;
252
253                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: G729A priority %d\n", codecpri);
254 #endif
255                 } else
256                 if (!strcmp(option,"h323_lpc10"))
257                 {
258 #ifdef H323
259                         codecpri ++;
260                         options.h323_lpc10_pri = codecpri;
261
262                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: LPC-10 priority %d\n", codecpri);
263 #endif
264                 } else
265                 if (!strcmp(option,"h323_speex"))
266                 {
267 #ifdef H323
268                         codecpri ++;
269                         options.h323_speex_pri = codecpri;
270                         options.h323_speex_opt = atoi(param);
271                         if (atoi(param)<2 && atoi(param)>6)
272                         {
273                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be in range 2..6.\n",filename,line,option);
274                                 goto error;
275                         }
276
277                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: Speex priority %d\n", codecpri);
278 #endif
279                 } else
280                 if (!strcmp(option,"h323_xspeex"))
281                 {
282 #ifdef H323
283                         codecpri ++;
284                         options.h323_xspeex_pri = codecpri;
285                         options.h323_xspeex_opt = atoi(param);
286                         if (atoi(param)<2 && atoi(param)>6)
287                         {
288                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be in range 2..6.\n",filename,line,option);
289                                 goto error;
290                         }
291
292                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: XiphSpeex priority %d\n", codecpri);
293 #endif
294                 } else
295                 if (!strcmp(option,"h323_law"))
296                 {
297 #ifdef H323
298                         codecpri ++;
299                         options.h323_law_pri = codecpri;
300                         options.h323_law_opt = atoi(param);
301                         if (atoi(param)<10 && atoi(param)>240)
302                         {
303                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be in range 10..240.\n",filename,line,option);
304                                 goto error;
305                         }
306
307                         PDEBUG(DEBUG_CONFIG, "H323 codec to use: Alaw, muLaw priority %d\n", codecpri);
308 #endif
309                 } else
310                 if (!strcmp(option,"h323_icall"))
311                 {
312 #ifdef H323
313                         options.h323_icall = 1;
314                         SCPY(options.h323_icall_prefix, param);
315
316                         PDEBUG(DEBUG_CONFIG, "process incoming H323 call with prefix '%s'\n", param);
317 #endif
318                 } else
319                 if (!strcmp(option,"h323_port"))
320                 {
321 #ifdef H323
322                         options.h323_port = atoi(param);
323
324                         PDEBUG(DEBUG_CONFIG, "use port for incoming H323 calls: %d\n", atoi(param));
325 #endif
326                 } else
327                 if (!strcmp(option,"sip_port"))
328                 {
329 #ifdef SIP
330                         options.sip_port = atoi(param);
331
332                         PDEBUG(DEBUG_CONFIG, "use port for incoming SIP calls: %d\n", atoi(param));
333 #endif
334                 } else
335                 if (!strcmp(option,"sip_maxqueue"))
336                 {
337 #ifdef SIP
338                         options.sip_maxqueue = atoi(param);
339
340                         PDEBUG(DEBUG_CONFIG, "number of simultanious incoming sockets for SIP calls: %d\n", atoi(param));
341 #endif
342                 } else
343                 if (!strcmp(option,"h323_gatekeeper"))
344                 {
345 #ifdef H323
346                         options.h323_gatekeeper = 1;
347                         if (param[0])
348                         {
349                                 SCPY(options.h323_gatekeeper_host, param);
350                         }
351                         PDEBUG(DEBUG_CONFIG, "register with H323 gatekeeper (%s)\n", (param[0])?param:"automatically");
352 #endif
353                 } else
354                 if (!strcmp(option,"tones_dir"))
355                 {
356                         if (param[0]==0)
357                         {
358                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
359                                 goto error;
360                         }
361                         if (param[strlen(param)-1] == '/')
362                                 param[strlen(param)-1]=0;
363                         SCPY(options.tones_dir, param);
364
365                         PDEBUG(DEBUG_CONFIG, "directory of tones: %s\n",param);
366                 } else
367                 if (!strcmp(option,"fetch_tones"))
368                 {
369                         if (param[0]==0)
370                         {
371                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
372                                 goto error;
373                         }
374                         if (param[strlen(param)-1] == '/')
375                                 param[strlen(param)-1]=0;
376                         SCPY(options.fetch_tones, param);
377
378                         PDEBUG(DEBUG_CONFIG, "directories of tones to fetch: %s\n",param);
379                 } else
380                 if (!strcmp(option,"extensions_dir"))
381                 {
382                         if (param[0]==0)
383                         {
384                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
385                                 goto error;
386                         }
387                         if (param[strlen(param)-1] == '/')
388                                 param[strlen(param)-1]=0;
389                         SCPY(options.extensions_dir, param);
390
391                         PDEBUG(DEBUG_CONFIG, "directory of extensions: %s\n",param);
392                 } else
393                 if (!strcmp(option,"national"))
394                 {
395                         SCPY(options.national, param);
396
397                         PDEBUG(DEBUG_CONFIG, "national dial prefix: %s\n", param);
398                 } else
399                 if (!strcmp(option,"international"))
400                 {
401                         SCPY(options.international, param);
402
403                         PDEBUG(DEBUG_CONFIG, "inernational dial prefix: %s\n", param);
404                 } else
405                 if (!strcmp(option,"nodtmf"))
406                 {
407                         options.nodtmf = 1;
408
409                         PDEBUG(DEBUG_CONFIG, "disable dtmf detection\n");
410                 } else
411                 if (!strcmp(option,"dummyid"))
412                 {
413                         SCPY(options.dummyid, param);
414
415                         PDEBUG(DEBUG_CONFIG, "dummy caller id\n", param);
416                 } else
417                 if (!strcmp(option,"dsptones"))
418                 {
419                         if (!strcasecmp(param, "american"))
420                                 options.dsptones = DSP_AMERICAN;
421                         else if (!strcasecmp(param, "german"))
422                                 options.dsptones = DSP_GERMAN;
423                         else if (!strcasecmp(param, "oldgerman"))
424                                 options.dsptones = DSP_OLDGERMAN;
425                         else if (!strcasecmp(param, "none"))
426                                 options.dsptones = DSP_NONE;
427                         else {
428                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
429                                 goto error;
430                         }
431
432                         PDEBUG(DEBUG_CONFIG, "dsp tones = %d\n", options.dsptones);
433                 } else
434                 if (!strcmp(option,"schedule"))
435                 {
436                         options.schedule = atoi(param);
437                         if (options.schedule < 0)
438                         {
439                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be at least '0'.\n", filename,line,option);
440                                 goto error;
441                         }
442                         if (options.schedule > 99)
443                         {
444                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s must be '99' or less.\n", filename,line,option);
445                                 goto error;
446                         }
447
448                         if (atoi(param))
449                                 PDEBUG(DEBUG_CONFIG, "use real time scheduler priority: %d\n", atoi(param));
450                         else
451                                 PDEBUG(DEBUG_CONFIG, "don't use real time scheduler\n");
452                 } else
453                 if (!strcmp(option,"email"))
454                 {
455                         if (param[0]==0)
456                         {
457                                 PERROR_RUNTIME("Error in %s (line %d): parameter for option %s missing.\n", filename,line,option);
458                                 goto error;
459                         }
460                         SCPY(options.email, param);
461
462                         PDEBUG(DEBUG_CONFIG, "source mail address of pbx: %s\n", param);
463                 } else
464                 {
465                         PERROR_RUNTIME("Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
466                         goto error;
467                 }
468         }
469
470 #if 0
471         if (!options.dsptones)
472         {
473                 PERROR_RUNTIME("Error in %s (line %d): option 'dsptones' missing.\n", filename);
474                 goto error;
475         }
476 #endif
477         if (!options.tones_dir[0])
478         {
479                 PERROR_RUNTIME("Error in %s (line %d): option 'tones_dir' with parameter missing.\n", filename);
480                 goto error;
481         }
482         if (fp) fclose(fp);
483         return(1);
484 error:
485         if (fp) fclose(fp);
486         return(0);
487 }
488
489