New Version 1.6
[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((fgets(buffer,sizeof(buffer),fp))) {
51                 line++;
52                 buffer[sizeof(buffer)-1]=0;
53                 if (buffer[0]) buffer[strlen(buffer)-1]=0;
54                 p=buffer;
55
56                 while(*p <= 32) { /* skip spaces */
57                         if (*p == 0)
58                                 break;
59                         p++;
60                 }
61                 if (*p==0 || *p=='#') /* ignore comments and empty line */
62                         continue;
63
64                 option[0]=0;
65                 i=0; /* read option */
66                 while(*p > 32) {
67                         if (i+1 >= sizeof(option)) {
68                                 SPRINT(gsm_conf_error, "Error in %s (line %d): option too long.\n",filename,line);
69                                 goto error;
70                         }
71                         option[i+1] = '\0';
72                         option[i++] = *p++;
73                 }
74
75                 while(*p <= 32) { /* skip spaces */
76                         if (*p == 0)
77                                 break;
78                         p++;
79                 }
80
81                 params[0][0] = 0;
82                 pnum = 0;
83                 while(*p!=0 && *p!='#' && pnum < 10) { /* param */
84                         i=0; /* read param */
85                         while(*p > 32) {
86                                 if (i+1 >= sizeof(params[pnum])) {
87                                         SPRINT(gsm_conf_error, "Error in %s (line %d): param too long.\n",filename,line);
88                                         goto error;
89                                 }
90                                 params[pnum][i+1] = '\0';
91                                 params[pnum][i++] = *p++;
92                         }
93                         while(*p <= 32) { /* skip spaces */
94                                 if (*p == 0)
95                                         break;
96                                 p++;
97                         }
98                         pnum++;
99                         params[pnum][0] = 0;
100                 }
101
102                 /* at this point we have option and param */
103
104                 /* check option */
105                 if (!strcmp(option,"debug")) {
106                         if (params[0][0]==0) {
107                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
108                                 goto error;
109                         }
110                         SCPY(gsm_conf->debug, params[0]);
111
112                 } else
113                 if (!strcmp(option,"interface-bsc")) {
114                         if (params[0][0]==0) {
115                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
116                                 goto error;
117                         }
118                         SCPY(gsm_conf->interface_bsc, params[0]);
119
120                 } else
121                 if (!strcmp(option,"interface-lcr")) {
122                         if (params[0][0]==0) {
123                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
124                                 goto error;
125                         }
126                         SCPY(gsm_conf->interface_lcr, params[0]);
127
128                 } else
129                 if (!strcmp(option,"config")) {
130                         if (params[0][0]==0) {
131                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
132                                 goto error;
133                         }
134                         SCPY(gsm_conf->openbsc_cfg, params[0]);
135
136                 } else
137                 if (!strcmp(option,"hlr")) {
138                         if (params[0][0]==0) {
139                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
140                                 goto error;
141                         }
142                         SCPY(gsm_conf->hlr, params[0]);
143
144                 } else
145                 if (!strcmp(option,"reject-cause")) {
146                         if (params[0][0]==0) {
147                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
148                                 goto error;
149                         }
150                         gsm_conf->reject_cause = atoi(params[0]);
151
152                 } else
153                 if (!strcmp(option,"allow-all")) {
154                         gsm_conf->allow_all = 1;
155                 } else
156                 if (!strcmp(option,"keep-l2")) {
157                         gsm_conf->keep_l2 = 1;
158
159                 } else
160                 if (!strcmp(option,"no-mergency-shutdown")) {
161                         gsm_conf->noemergshut = 1;
162                 } else
163                 if (!strcmp(option,"rtp-proxy")) {
164                         gsm_conf->rtp_proxy = 1;
165                 } else
166                 if (!strcmp(option,"pcapfile")) {
167                         if (params[0][0]==0) {
168                                 SPRINT(gsm_conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
169                                 goto error;
170                         }
171                         SCPY(gsm_conf->pcapfile, params[0]);
172                 } else {
173                         SPRINT(gsm_conf_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
174                         goto error;
175                 }
176         }
177
178         if (fp) fclose(fp);
179         return(1);
180 error:
181         if (fp) fclose(fp);
182         return(0);
183 }
184
185