summaryrefslogtreecommitdiffstats
path: root/VNFs/DPPD-PROX/cfgfile.c
blob: 2bc9e5f1f99a39ba3a161eea8172d544c52d0b54 (plain)
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/*
// Copyright (c) 2010-2017 Intel Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
*/

#include "cfgfile.h"

#include <rte_string_fns.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <unistd.h>

#include "parse_utils.h"
#include "log.h"
#include "quit.h"
#include "prox_compat.h"

#define UINT32_MAX_STR "4294967295"

/*
 * Allocate cfg_file structure.
 * Returns pointer to the allocated structure, NULL otherwise.
 */
struct cfg_file *cfg_open(const char *cfg_name)
{
	if (cfg_name == NULL) {
		plog_err("\tNo config file name provided\n");
		return NULL;
	}
	if (access(cfg_name, F_OK)) {
                plog_err("\tError opening config file '%s': %s\n", cfg_name, strerror(errno));
		return NULL;
	}

	FILE *pf = fopen(cfg_name, "rb");
	if (pf == NULL) {
		plog_err("\tError opening config file '%s'\n", cfg_name);
		return NULL;
	}

	struct cfg_file *pcfg = calloc(1, sizeof(struct cfg_file));

	if (pcfg == NULL) {
		fclose(pf);
		plog_err("\tCouldn't allocate memory for config file struct\n");
		return NULL;
	}

	pcfg->pfile = pf;
	pcfg->name = strdup(cfg_name);

	return pcfg;
}

/* Free memory allocated for cfg_file structure.
 * Returns 0 on success, -1 if the pointer to the pcfg is invalid */
int cfg_close(struct cfg_file *pcfg)
{
	if (pcfg == NULL) {
		return -1;
	}

	if (pcfg->name != NULL) {
		free(pcfg->name);
	}
	if (pcfg->err_section != NULL) {
		free(pcfg->err_section);
	}
	if (pcfg->pfile != NULL) {
		fclose(pcfg->pfile);
	}

	free(pcfg);
	return 0;
}

static int cfg_get_pos(struct cfg_file *pcfg, fpos_t *pos)
{
	pcfg->index_line = pcfg->line;
	return fgetpos(pcfg->pfile, pos);
}

static int cfg_set_pos(struct cfg_file *pcfg, fpos_t *pos)
{
	pcfg->line = pcfg->index_line;
	return fsetpos(pcfg->pfile, pos);
}

/*
 * Read a line from the configuration file.
 * Returns: on success length of the line read from the file is returned,
 *          0 to indicate End of File,
 *         -1 in case of wrong function parameters
 */
static int cfg_get_line(struct cfg_file *pcfg, char *buffer, unsigned len, int raw_lines)
{
	char *ptr;

	if (pcfg == NULL || pcfg->pfile == NULL || buffer == NULL || len == 0) {
		return -1;
	}

	do {
		ptr = fgets(buffer, len, pcfg->pfile);
		if (ptr == NULL) {
			return 0; /* end of file */
		}
		++pcfg->line;

		if (raw_lines) {
			break;
		}

		/* remove comments */
		ptr = strchr(buffer, ';');
		if (ptr != NULL) {
			*ptr = '\0';
		}
		else {
			ptr = strchr(buffer, '\0');
		}

		/* remove trailing spaces */
		if (ptr != buffer) {
			ptr--;
			while (isspace(*ptr)) {
				*ptr = '\0';
				ptr--;
			}
		}

		ptr = buffer;
		/* remove leading spaces */
		while (*ptr && isspace(*ptr)) {
			++ptr;
		}
		if (ptr != buffer) {
			strcpy(buffer, ptr);
			ptr = buffer;
		}
	}
	while (*ptr == '\0'); /* skip empty strings */

	return strlen(buffer);
}

/*
 * Checks if buffer contains section name specified by the cfg_section pointer.
 * Returns NULL if section name does not match, cfg_section pointer otherwise
 */
static struct cfg_section *cfg_check_section(char *buffer, struct cfg_section *psec)
{
	char *pend;
	unsigned len;
	static const char *valid = "0123456789,hs- \t";

	pend = strchr(buffer, ']');
	if (pend == NULL) {
		return NULL; /* ']' not found: invalid section name */
	}

	*pend = '\0';

	/* check if section is indexed */
	pend = strchr(psec->name, '#');
	if (pend == NULL) {
		return (strcmp(buffer, psec->name) == 0) ? psec : NULL;
	}

	/* get section index */
	len = pend - psec->name;
	if (strncmp(buffer, psec->name, len) != 0) {
		return NULL;
	}
	pend = buffer + len;
	if (*pend == '\0') {
		return NULL;
	}

	/* only numeric characters are valid for section index */
	char val[MAX_CFG_STRING_LEN];
	if (pend[0] == '$') {
		if (parse_vars(val, sizeof(val), pend))
			return NULL;
	} else
		prox_strncpy(val, pend, sizeof(val));

	for (len = 0; val[len] != '\0'; ++len) {
		if (strchr(valid, val[len]) == NULL) {
			return NULL;
		}
	}

	psec->nbindex = parse_list_set(psec->indexp, pend, MAX_INDEX);
	PROX_PANIC(psec->nbindex == -1, "\t\tError in cfg_check_section('%s'): %s\n", buffer, get_parse_err());

	for (int i = 0; i < psec->nbindex; ++i) {
		psec->indexp[i] |= CFG_INDEXED;
	}

	return psec;
}

static char *cfg_get_section_name(struct cfg_section *psec)
{
	char *name;

	if (!(psec->indexp[0] & CFG_INDEXED)) {
		return strdup(psec->name);
	}

	name = malloc(strlen(psec->name) + strlen(UINT32_MAX_STR));
	if (name != NULL) {
		strcpy(name, psec->name);
		char *pidx = strchr(name, '#');
		if (pidx != NULL) {
			sprintf(pidx, "%u", psec->indexp[0] & ~CFG_INDEXED);
		}
	}
	return name;
}

/*
 * Reads configuration file and parses section specified by psec pointer.
 * Returns 0 on success, -1 otherwise
 */
int cfg_parse(struct cfg_file *pcfg, struct cfg_section *psec)
{
	int error;
	unsigned entry = 0;
	fpos_t pos;
	int index_count = 0;
	struct cfg_section *section = NULL;
	char buffer[sizeof(pcfg->cur_line)] = {0};

	if (pcfg == NULL || psec == NULL) {
		return -1;
	}

	pcfg->line = 0;
	fseek(pcfg->pfile, 0, SEEK_SET);

	/* read configuration file and parse section specified by psec pointer */
	while (1) {
		if (psec->raw_lines) {
			/* skip until section starts */
			char *lines = pcfg->cur_line;
			size_t max_len = sizeof(pcfg->cur_line);
			char *ret;

			do {
				ret = fgets(lines, max_len, pcfg->pfile);
				if (ret && *ret == '[') {
					section = cfg_check_section(lines + 1, psec);
				}
			} while (!section && ret);

			if (!ret)
				return 0;

			do {

				ret = fgets(buffer, sizeof(buffer), pcfg->pfile);
				/* remove comments */
				if (*ret == ';') {
					*ret = '\0';
				}

				if (ret && *ret != '[') {
					size_t l = strlen(buffer);
					prox_strncpy(lines, buffer, max_len);
					max_len -= l;
					lines += l;
				}
			} while ((ret && *ret != '['));

			if (section != NULL) {
				error = section->parser(section->indexp[index_count], pcfg->cur_line, section->data);
				if (error != 0) {
					section->error = error;
					/* log only the very first error */
					if (!pcfg->err_section) {
						pcfg->err_line = pcfg->line;
						pcfg->err_entry = entry;
						pcfg->err_section = cfg_get_section_name(section);
					}
					return 0;
				}
				++entry;
			}
			return 0;
		}

		while (cfg_get_line(pcfg, buffer, MAX_CFG_STRING_LEN, psec->raw_lines) > 0) {
			prox_strncpy(pcfg->cur_line, buffer, sizeof(pcfg->cur_line));
			if (*buffer == '[') {
				if (index_count + 1 < psec->nbindex) {
					// Need to loop - go back to recorded postion in file
					cfg_set_pos(pcfg, &pos);
					++index_count;
					continue;
				}
				else {
					section = cfg_check_section(buffer + 1, psec);
					entry = 0;
					index_count = 0;
					cfg_get_pos(pcfg, &pos);
					continue;
				}
			}
			/* call parser procedure for each line in the section */
			if (section != NULL) {
				error = section->parser(section->indexp[index_count], buffer, section->data);
				if (error != 0) {
					section->error = error;
					/* log only the very first error */
					if (!pcfg->err_section) {
						pcfg->err_line = pcfg->line;
						pcfg->err_entry = entry;
						pcfg->err_section = cfg_get_section_name(section);
					}
					return 0;
				}
				++entry;
			}
		}
		if (index_count + 1 < psec->nbindex) {
			// Last core config contained multiple cores - loop back
			cfg_set_pos(pcfg, &pos);
			++index_count;
		}
		else {
			break;
		}
	}
	return 0;
}