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