Merge branch 'master' of ssh://jolly@www.mISDN.org/var/git/lcr
[lcr.git] / gsm_conf.c
1 /*****************************************************************************\
2 **                                                                           **
3 ** PBX4Linux                                                                 **
4 **                                                                           **
5 **---------------------------------------------------------------------------**
6 ** Copyright: Andreas Eversberg                                              **
7 **                                                                           **
8 ** reading options.conf and filling structure                                **
9 **                                                                           **
10 \*****************************************************************************/ 
11
12 #include "main.h"
13
14
15 char *gsm_conf_error = (char *)"";
16
17 /* read options
18  *
19  * read options from options.conf
20  */
21 int gsm_conf(struct gsm_conf *gsm_conf)
22 {
23         FILE *fp=NULL;
24         char filename[128];
25         char *p;
26         char option[32];
27         char params[11][256];
28         int pnum;
29         unsigned int line,i;
30         char buffer[256];
31
32         /* set defaults */
33         SCPY(gsm_conf->debug, "");
34         SCPY(gsm_conf->interface_bsc, "mISDN_l1loop.1");
35         SCPY(gsm_conf->interface_lcr, "mISDN_l1loop.2");
36         SCPY(gsm_conf->hlr, "hlr.sqlite3");
37         SCPY(gsm_conf->openbsc_cfg, "openbsc.cfg");
38         gsm_conf->reject_cause = 0;
39         gsm_conf->keep_l2 = 0;
40         gsm_conf->noemergshut = 0;
41
42         SPRINT(filename, "%s/gsm.conf", CONFIG_DATA);
43
44         if (!(fp=fopen(filename,"r"))) {
45                 SPRINT(gsm_conf_error, "Cannot open %s\n",filename);
46                 return(0);
47         }
48
49         line=0;
50         while((GETLINE(buffer, fp))) {
51                 line++;
52                 p=buffer;
53
54                 while(*p <= 32) { /* skip spaces */
55                         if (*p == 0)
56                                 break;
57                         p++;
58                 }
59                 if (*p==0 || *p=='#') /* ignore comments and empty line */
60                         continue;
61
62                 option[0]=0;
63                 i=0; /* read option */
64                 while(*p > 32) {
65                         if (i+1 >= sizeof(option)) {
66                                 SPRINT(gsm_conf_error, "Error in %s (line %d): option too long.\n",filename,line);
67                                 goto error;
68                         }
69                         option[i+1] = '\0';
70                         option[i++] = *p++;
71                 }
72
73                 while(*p <= 32) { /* skip spaces */
74                         if (*p == 0)
75                                 break;
76                         p++;
77                 }
78
79                 params[0][0] = 0;
80                 pnum = 0;
81                 while(*p!=0 && *p!='#' && pnum < 10) { /* param */
82                         i=0; /* read param */
83                         while(*p > 32) {
84                                 if (i+1 >= sizeof(params[pnum])) {
85                                         SPRINT(gsm_conf_error, "Error in %s (line %d): param too long.\n",filename,line);
86                                         goto error;
87                                 }
88                                 params[pnum][i+1] = '\0';
89                                 params[pnum][i++] = *p++;
90                         }
91                         while(*p <= 32) { /* skip spaces */
92                                 if (*p == 0)
93                                         break;
94                                 p++;
95                         }
96                         pnum++;
97                         params[pnum][0] = 0;
98                 }
99
100                 /* at this point we have option and param */
101
102                 /* check option */
103                 if (!strcmp(option,"debug")) {
104                         if (params[0][0]==0) {
105                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
106                                 goto error;
107                         }
108                         SCPY(gsm_conf->debug, params[0]);
109
110                 } else
111                 if (!strcmp(option,"interface-bsc")) {
112                         if (params[0][0]==0) {
113                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
114                                 goto error;
115                         }
116                         SCPY(gsm_conf->interface_bsc, params[0]);
117
118                 } else
119                 if (!strcmp(option,"interface-lcr")) {
120                         if (params[0][0]==0) {
121                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
122                                 goto error;
123                         }
124                         SCPY(gsm_conf->interface_lcr, params[0]);
125
126                 } else
127                 if (!strcmp(option,"config")) {
128                         if (params[0][0]==0) {
129                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
130                                 goto error;
131                         }
132                         SCPY(gsm_conf->openbsc_cfg, params[0]);
133
134                 } else
135                 if (!strcmp(option,"hlr")) {
136                         if (params[0][0]==0) {
137                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
138                                 goto error;
139                         }
140                         SCPY(gsm_conf->hlr, params[0]);
141
142                 } else
143                 if (!strcmp(option,"reject-cause")) {
144                         if (params[0][0]==0) {
145                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
146                                 goto error;
147                         }
148                         gsm_conf->reject_cause = atoi(params[0]);
149
150                 } else
151                 if (!strcmp(option,"allow-all")) {
152                         gsm_conf->allow_all = 1;
153                 } else
154                 if (!strcmp(option,"keep-l2")) {
155                         gsm_conf->keep_l2 = 1;
156
157                 } else
158                 if (!strcmp(option,"no-mergency-shutdown")) {
159                         gsm_conf->noemergshut = 1;
160                 } else
161                 if (!strcmp(option,"rtp-proxy")) {
162                         gsm_conf->rtp_proxy = 1;
163                 } else
164                 if (!strcmp(option,"pcapfile")) {
165                         if (params[0][0]==0) {
166                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
167                                 goto error;
168                         }
169                         SCPY(gsm_conf->pcapfile, params[0]);
170                 } else {
171                         SPRINT(gsm_conf_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
172                         goto error;
173                 }
174         }
175
176         if (fp) fclose(fp);
177         return(1);
178 error:
179         if (fp) fclose(fp);
180         return(0);
181 }
182
183