File: jconfig.c

package info (click to toggle)
jnettop 0.13.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,048 kB
  • sloc: ansic: 4,012; sh: 775; makefile: 20
file content (419 lines) | stat: -rw-r--r-- 13,260 bytes parent folder | download | duplicates (4)
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
/*
 *    jnettop, network online traffic visualiser
 *    Copyright (C) 2002-2005 Jakub Skopal
 *
 *    This program is free software; you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation; either version 2 of the License, or
 *    (at your option) any later version.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with this program; if not, write to the Free Software
 *    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *    $Header: /cvsroot/jnettop/jnettop/jconfig.c,v 1.7 2006/04/12 07:47:01 merunka Exp $
 *
 */

#include "jbase.h"
#include "jutil.h"
#include "jcapture.h"
#include "jprocessor.h"
#include "jresolver.h"
#include "jconfig.h"
#include "jdevice.h"

jconfig_settings	jconfig_Settings;

static int parse_boolean(GScanner *s) {
	GTokenType tt;
	tt = g_scanner_get_next_token(s);
	if (tt != G_TOKEN_IDENTIFIER || (strcmp(s->value.v_identifier, "on") && strcmp(s->value.v_identifier,"off"))) {
		return -1;
	}
	return strcmp(s->value.v_identifier, "off")?TRUE:FALSE;
}

static int parse_aggregation(GScanner *s) {
	GTokenType tt;
	tt = g_scanner_get_next_token(s);
	if (tt != G_TOKEN_IDENTIFIER) {
		return AGG_UNKNOWN;
	}
	return jutil_ParseAggregation(s->value.v_identifier);
}

static int parse_resolvertype(GScanner *s) {
	GTokenType tt;
	tt = g_scanner_get_next_token(s);
	if (tt != G_TOKEN_IDENTIFIER) {
		return LOOKUPTYPE_UNKNOWN;
	}
	if (strcmp(s->value.v_identifier, "normal") && strcmp(s->value.v_identifier, "external")) {
		return LOOKUPTYPE_UNKNOWN;
	}
	switch (s->value.v_identifier[0]) {
		case 'n': return LOOKUPTYPE_NORMAL;
		case 'e': return LOOKUPTYPE_EXTERNAL;
	}
	return LOOKUPTYPE_UNKNOWN;
}

static gboolean parse_ip(GScanner *s, jbase_mutableaddress *dest, int *af) {
	GTokenType tt;
	tt = g_scanner_get_next_token(s);
	if (tt != G_TOKEN_STRING) {
		return FALSE;
	}
	if (jutil_String2Address(s->value.v_string, dest, af)) {
		return TRUE;
	}
	return FALSE;
}

gboolean jconfig_Setup() {
	jconfig_Settings.deviceName = NULL;
	jconfig_Settings._bpfFilters = g_ptr_array_new();
	jconfig_Settings.onoffContentFiltering = -1;
	jconfig_Settings.onoffPromisc = -1;
	jconfig_Settings.onoffResolver = -1;
	jconfig_Settings.localAggregation = AGG_UNKNOWN;
	jconfig_Settings.remoteAggregation = AGG_UNKNOWN;
	jconfig_Settings._selectedBpfFilter = -1;
	jconfig_Settings._adHocBpfFilter = NULL;
	jconfig_Settings._networkMaskList = NULL;
	return TRUE;
}

gboolean jconfig_ParseFile(char *configFileName) {
	FILE *f;
	GScanner *s;
	GHashTable *variables;
	char *homeDir;

	variables = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);

	if (!configFileName) {
		homeDir = getenv("HOME");
		if (!homeDir) {
			configFileName = ".jnettop";
		} else {
			configFileName = g_new0(char, strlen(homeDir) + 10);
			sprintf(configFileName, "%s/.jnettop", homeDir);
		}
	}

	f = fopen(configFileName, "r");
	if (!f) {
		fprintf(stderr, "Could not read/find config file %s: %s.\n", configFileName, strerror(errno));
		return TRUE;
	}

	s = g_scanner_new(NULL);
	/* Add + to accepted identifier characters: */
	s->config->cset_identifier_nth = g_strjoin(NULL,
		s->config->cset_identifier_nth, "+", NULL);

	g_scanner_input_file(s, fileno(f));
	while (!g_scanner_eof(s)) {
		GTokenType tt;
		int line;

		line = s->line;
		tt = g_scanner_get_next_token(s);
		if (tt == G_TOKEN_EOF) {
			break;
		}
		if (tt != G_TOKEN_IDENTIFIER) {
			fprintf(stderr, "Parse error on line %d: identifier expected.\n", line);
			return FALSE;
		}
		if (!g_ascii_strcasecmp(s->value.v_identifier, "variable")) {
			char * variableName, * variableValue;
			tt = g_scanner_get_next_token(s);
			if (tt != G_TOKEN_STRING) {
				fprintf(stderr, "Parse error on line %d: variable name as string expected.\n", line);
				return FALSE;
			}
			variableName = g_strdup(s->value.v_string);
			tt = g_scanner_get_next_token(s);
			if (tt != G_TOKEN_STRING) {
				fprintf(stderr, "Parse error on line %d: variable value as string expected.\n", line);
				return FALSE;
			}
			variableValue = g_strdup(s->value.v_string);
			g_hash_table_insert(variables, variableName, variableValue);
			continue;
		}
		if (!g_ascii_strcasecmp(s->value.v_identifier, "rule")) {
			char * ruleName;
			char * c;
			GString *str;
			tt = g_scanner_get_next_token(s);
			if (tt != G_TOKEN_STRING) {
				fprintf(stderr, "Parse error on line %d: rule name as string expected.\n", line);
				return FALSE;
			}
			ruleName = g_strdup(s->value.v_string);
			tt = g_scanner_get_next_token(s);
			if (tt != G_TOKEN_STRING) {
				fprintf(stderr, "Parse error on line %d: rule expression as string expected.\n", line);
				return FALSE;
			}
			str = g_string_new("");
			for (c=s->value.v_string; *c; c++) {
				char * rightBracket;
				char * variableValue;
				if (*c == '$' && *(c+1) == '{') {
					rightBracket = strchr(c, '}');
					c += 2;
					if (!rightBracket) {
						fprintf(stderr, "Wrong variable substitution on line %d!\n", line);
						return FALSE;
					}
					*rightBracket = '\0';
					variableValue = g_hash_table_lookup(variables, c);
					if (!variableValue) {
						fprintf(stderr, "Undefined variable %s on line %d!\n", c, line);
						return FALSE;
					}
					g_string_append(str, variableValue);
					c = rightBracket;
				} else {
					g_string_append_c(str, *c);
				}
			}
			jconfig_AddBpfFilter(ruleName, str->str);
			g_string_free(str, FALSE);
			continue;
		}
		if (!g_ascii_strcasecmp(s->value.v_identifier, "interface")) {
			tt = g_scanner_get_next_token(s);
			if (tt != G_TOKEN_STRING) {
				fprintf(stderr, "Parse error on line %d: interface name as string expected.\n", line);
				return FALSE;
			}
			if (jconfig_Settings.deviceName == NULL)
				jconfig_Settings.deviceName = g_strdup(s->value.v_string);
			continue;
		}
		if (!g_ascii_strcasecmp(s->value.v_identifier, "promisc")) {
			int val = parse_boolean(s);
			if (val == -1) {
				fprintf(stderr, "Parse error on line %d: expecting on or off value.\n", line);
				return FALSE;
			}
			if (jconfig_Settings.onoffPromisc == -1)
				jconfig_Settings.onoffPromisc = val;
			continue;
		}
		if (!g_ascii_strcasecmp(s->value.v_identifier, "local_aggregation")) {
			int val = parse_aggregation(s);
			if (val == AGG_UNKNOWN) {
				fprintf(stderr, "Parse error on line %d: expecting none, host, port, or host+port.\n", line);
				return FALSE;
			}
			if (jconfig_Settings.localAggregation == AGG_UNKNOWN)
				jconfig_Settings.localAggregation = val;
			continue;
		}
		if (!g_ascii_strcasecmp(s->value.v_identifier, "remote_aggregation")) {
			int val = parse_aggregation(s);
			if (val == AGG_UNKNOWN) {
				fprintf(stderr, "Parse error on line %d: expecting none or host or port.\n", line);
				return FALSE;
			}
			if (jconfig_Settings.remoteAggregation == AGG_UNKNOWN)
				jconfig_Settings.remoteAggregation = val;
			continue;
		}
		if (!g_ascii_strcasecmp(s->value.v_identifier, "select_rule")) {
			int i;
			tt = g_scanner_get_next_token(s);
			if (tt != G_TOKEN_STRING) {
				fprintf(stderr, "Parse error on line %d: rule name as string expected.\n", line);
				return FALSE;
			}
			if (JCONFIG_BPFFILTERS_SELECTEDISNONE) {
				i = jconfig_FindBpfFilterByName(s->value.v_string);
				if (i==-1) {
					fprintf(stderr, "Parse error on line %d: rule %s not defined so far.\n", line, s->value.v_string);
					return FALSE;
				}
				JCONFIG_BPFFILTERS_SETSELECTEDFILTER(i);
			}
			continue;
		}
		if (!g_ascii_strcasecmp(s->value.v_identifier, "resolve")) {
			int val = parse_boolean(s);
			if (val == -1) {
				fprintf(stderr, "Parse error on line %d: expecting on or off value.\n", line);
				return FALSE;
			}
			if (jconfig_Settings.onoffResolver == -1)
				jconfig_Settings.onoffResolver = val;
		}
		if (!g_ascii_strcasecmp(s->value.v_identifier, "resolve_rule")) {
			int af1, af2;
			jbase_mutableaddress mask;
			jbase_mutableaddress value;
			int resolvertype;
			if (!parse_ip(s, &value, &af1)) {
				fprintf(stderr, "Parse error on line %d: expecting ip address.\n", line);
				return FALSE;
			}
			if (!parse_ip(s, &mask, &af2)) {
				fprintf(stderr, "Parse error on line %d: expecting ip mask.\n", line);
				return FALSE;
			}
			if (af1 != af2) {
				fprintf(stderr, "Parse error on line %d: ip mask and ip address must be from the same family.\n", line);
				return FALSE;
			}
			if ((resolvertype = parse_resolvertype(s)) == LOOKUPTYPE_UNKNOWN) {
				fprintf(stderr, "Parse error on line %d: expecint resolver type.\n", line);
				return FALSE;
			}
			switch (resolvertype) {
				case LOOKUPTYPE_NORMAL:
					jresolver_AddNormalLookup(af1, &mask, &value);
					break;
				case LOOKUPTYPE_EXTERNAL:
					tt = g_scanner_get_next_token(s);
					if (tt != G_TOKEN_STRING) {
						fprintf(stderr, "Parse error on line %d: expecting external resolver path.\n", line);
						return FALSE;
					}
					jresolver_AddExternalLookupScript(af1, &mask, &value, g_strdup(s->value.v_string));
					break;
			}
			continue;
		}
		if (!g_ascii_strcasecmp(s->value.v_identifier, "local_network")) {
			int af1, af2;
			jbase_mutableaddress mask;
			jbase_mutableaddress value;
			if (!parse_ip(s, &value, &af1)) {
				fprintf(stderr, "Parse error on line %d: expecting ip address.\n", line);
				return FALSE;
			}
			if (!parse_ip(s, &mask, &af2)) {
				fprintf(stderr, "Parse error on line %d: expecting ip mask.\n", line);
				return FALSE;
			}
			if (af1 != af2) {
				fprintf(stderr, "Parse error on line %d: ip mask and ip address must be from the same family.\n", line);
				return FALSE;
			}

			jconfig_AddLocalNetwork(&value, &mask, af1);
			continue;
		}
	}

	g_hash_table_destroy(variables);
	return TRUE;
}

void jconfig_SetDefaults() {
	if (jconfig_Settings.onoffContentFiltering == -1)
		jconfig_Settings.onoffContentFiltering = TRUE;
	if (jconfig_Settings.onoffPromisc == -1)
		jconfig_Settings.onoffPromisc = FALSE;
	if (jconfig_Settings.onoffResolver == -1)
		jconfig_Settings.onoffResolver = TRUE;
	if (jconfig_Settings.localAggregation == AGG_UNKNOWN)
		jconfig_Settings.localAggregation = AGG_NONE;
	if (jconfig_Settings.remoteAggregation == AGG_UNKNOWN)
		jconfig_Settings.remoteAggregation = AGG_NONE;
}

void jconfig_ConfigureModules() {
	jcapture_SetPromisc(jconfig_Settings.onoffPromisc);
	jprocessor_SetLocalAggregation(jconfig_Settings.localAggregation);
	jprocessor_SetRemoteAggregation(jconfig_Settings.remoteAggregation);
	jprocessor_SetContentFiltering(jconfig_Settings.onoffContentFiltering);
	jresolver_SetEnabled(jconfig_Settings.onoffResolver);
}

const char * jconfig_GetSelectedBpfFilterText() {
	if (jconfig_Settings._selectedBpfFilter == -2 && jconfig_Settings._adHocBpfFilter)
		return jconfig_Settings._adHocBpfFilter;
	if (jconfig_Settings._selectedBpfFilter <= -1)
		return NULL;
	return (const char *) g_ptr_array_index(jconfig_Settings._bpfFilters, jconfig_Settings._selectedBpfFilter*2+1);
}

const char * jconfig_GetSelectedBpfFilterName() {
	if (jconfig_Settings._selectedBpfFilter == -2 && jconfig_Settings._adHocBpfFilter)
		return jconfig_Settings._adHocBpfFilter;
	if (jconfig_Settings._selectedBpfFilter <= -1)
		return "none";
	return (const char *) g_ptr_array_index(jconfig_Settings._bpfFilters, jconfig_Settings._selectedBpfFilter*2);
}

void jconfig_AddBpfFilter(char *filterName, char *filterText) {
	g_ptr_array_add(jconfig_Settings._bpfFilters, filterName);
	g_ptr_array_add(jconfig_Settings._bpfFilters, filterText);
}

int jconfig_FindBpfFilterByName(char *filterName) {
	int i;
	for (i=0; i<JCONFIG_BPFFILTERS_LEN; i++) {
		if (!strcmp(JCONFIG_BPFFILTERS_GETNAME(i), filterName)) {
			return i;
		}
	}
	return -1;
}

void jconfig_AddLocalNetwork(const jbase_mutableaddress *network, const jbase_mutableaddress *netmask, int af) {
	jbase_network_mask_list * current = NULL;
	current = g_new0(jbase_network_mask_list, 1);
	memcpy(&current->network, network, sizeof(jbase_mutableaddress));
	memcpy(&current->netmask, netmask, sizeof(jbase_mutableaddress));
	current->af = af;
	current->next = jconfig_Settings._networkMaskList;
	jconfig_Settings._networkMaskList = current;
}

int jconfig_FindMatchingLocalNetworkIndex(const jbase_mutableaddress *network, int af) {
	jbase_network_mask_list * current;
	int priority = 64000;

	current = jconfig_Settings._networkMaskList;
	while (current) {
		priority --;

		if (jutil_IsInNetwork(network, af, &current->network, &current->netmask, current->af)) {
			return priority;
		}

		current = current->next;
	}

	return 64000;
}

void jconfig_SelectDevice(const char *deviceName) {
	int i;

	jconfig_Settings.deviceName = deviceName;

	for (i=0; i<jdevice_DevicesCount; i++) {
		if (!strcmp(jdevice_Devices[i].name, jconfig_Settings.deviceName)) {
			jconfig_Settings.device = jdevice_Devices + i;
			break;
		}
	}

	if (i >= jdevice_DevicesCount) {
		jconfig_Settings.device = jdevice_CreateSingleDevice(jconfig_Settings.deviceName);
	}
}