summaryrefslogtreecommitdiffstats
path: root/gsm_conf.c
blob: a64f2ff8863b9a750f30d027eb1e2714a4c5abc9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
/*****************************************************************************\
**                                                                           **
** PBX4Linux                                                                 **
**                                                                           **
**---------------------------------------------------------------------------**
** Copyright: Andreas Eversberg                                              **
**                                                                           **
** reading options.conf and filling structure                                **
**                                                                           **
\*****************************************************************************/ 

#include "main.h"


/* read options
 *
 * read options from options.conf
 */
int gsm_conf(struct gsm_conf *gsm_conf, char *conf_error)
{
	FILE *fp=NULL;
	char filename[128];
	char *p;
	char option[32];
	char params[11][256];
	int pnum;
	unsigned int line,i;
	char buffer[256];

	/* set defaults */
	SCPY(gsm_conf->debug, "");
	SCPY(gsm_conf->interface_bsc, "mISDN_l1loop.1");
	SCPY(gsm_conf->interface_lcr, "mISDN_l1loop.2");
	SCPY(gsm_conf->hlr, "hlr.sqlite3");
	SCPY(gsm_conf->openbsc_cfg, "openbsc.cfg");
	gsm_conf->reject_cause = 0;
	gsm_conf->keep_l2 = 0;

	SPRINT(filename, "%s/gsm.conf", CONFIG_DATA);

	if (!(fp=fopen(filename,"r"))) {
		UPRINT(conf_error, "Cannot open %s\n",filename);
		return(0);
	}

	line=0;
	while((GETLINE(buffer, fp))) {
		line++;
		p=buffer;

		while(*p <= 32) { /* skip spaces */
			if (*p == 0)
				break;
			p++;
		}
		if (*p==0 || *p=='#') /* ignore comments and empty line */
			continue;

		option[0]=0;
		i=0; /* read option */
		while(*p > 32) {
			if (i+1 >= sizeof(option)) {
				UPRINT(conf_error, "Error in %s (line %d): option too long.\n",filename,line);
				goto error;
			}
			option[i+1] = '\0';
			option[i++] = *p++;
		}

		while(*p <= 32) { /* skip spaces */
			if (*p == 0)
				break;
			p++;
		}

		params[0][0] = 0;
		pnum = 0;
		while(*p!=0 && *p!='#' && pnum < 10) { /* param */
			i=0; /* read param */
			while(*p > 32) {
				if (i+1 >= sizeof(params[pnum])) {
					UPRINT(conf_error, "Error in %s (line %d): param too long.\n",filename,line);
					goto error;
				}
				params[pnum][i+1] = '\0';
				params[pnum][i++] = *p++;
			}
			while(*p <= 32) { /* skip spaces */
				if (*p == 0)
					break;
				p++;
			}
			pnum++;
			params[pnum][0] = 0;
		}

		/* at this point we have option and param */

		/* check option */
		if (!strcmp(option,"debug")) {
			if (params[0][0]==0) {
				UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line,option);
				goto error;
			}
			SCPY(gsm_conf->debug, params[0]);

		} else
		if (!strcmp(option,"interface-bsc")) {
			if (params[0][0]==0) {
				UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
				goto error;
			}
			SCPY(gsm_conf->interface_bsc, params[0]);

		} else
		if (!strcmp(option,"interface-lcr")) {
			if (params[0][0]==0) {
				UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
				goto error;
			}
			SCPY(gsm_conf->interface_lcr, params[0]);

		} else
		if (!strcmp(option,"config")) {
			if (params[0][0]==0) {
				UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
				goto error;
			}
			SCPY(gsm_conf->openbsc_cfg, params[0]);

		} else
		if (!strcmp(option,"hlr")) {
			if (params[0][0]==0) {
				UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
				goto error;
			}
			SCPY(gsm_conf->hlr, params[0]);

		} else
		if (!strcmp(option,"reject-cause")) {
			UPRINT(conf_error, "Option '%s' in gsm.conf has moved to openbsc.cfg", option);
			goto error;
		} else
		if (!strcmp(option,"allow-all")) {
			gsm_conf->allow_all = 1;
		} else
		if (!strcmp(option,"keep-l2")) {
			gsm_conf->keep_l2 = 1;

		} else
		if (!strcmp(option,"pcapfile")) {
			if (params[0][0]==0) {
				UPRINT(conf_error, "Error in %s (line %d): parameter for option %s missing.\n",filename,line, option);
				goto error;
			}
			SCPY(gsm_conf->pcapfile, params[0]);
		} else {
			UPRINT(conf_error, "Error in %s (line %d): wrong option keyword %s.\n", filename,line,option);
			goto error;
		}
	}

	if (fp) fclose(fp);
	return(1);
error:
	if (fp) fclose(fp);
	return(0);
}