forked from the-tcpdump-group/libpcap
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpcap-config.c
452 lines (374 loc) · 9.97 KB
/
pcap-config.c
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
/*
* Copyright (c) 1994, 1995, 1996
* The Regents of the University of California. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the Computer Systems
* Engineering Group at Lawrence Berkeley Laboratory.
* 4. Neither the name of the University nor of the Laboratory may be used
* to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <pcap.h>
#include <pcap-int.h>
#include "pcap-config.h"
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <errno.h>
#ifdef PCAP_SUPPORT_PFQ
#include <pfq/pfq.h>
#endif
extern char **environ;
struct pcap_conf_key pcap_conf_keys[] =
{
PCAP_CONF_KEY(def_group)
, PCAP_CONF_KEY(fanout)
, PCAP_CONF_KEY(caplen)
#ifdef PCAP_SUPPORT_PFQ
, PCAP_CONF_KEY(pfq_rx_slots)
, PCAP_CONF_KEY(pfq_tx_slots)
, PCAP_CONF_KEY(pfq_tx_sync)
, PCAP_CONF_KEY(pfq_tx_hw_queue)
, PCAP_CONF_KEY(pfq_tx_idx_thread)
, PCAP_CONF_KEY(pfq_vlan)
#endif
};
static size_t pcap_conf_keys_eof = sizeof(pcap_conf_keys)/sizeof(pcap_conf_keys[0]);
struct pcap_config
pcap_config_default(pcap_t *handle)
{
return (struct pcap_config)
{
.def_group = -1
, .group_map = {{[0 ... PCAP_FANOUT_GROUP_MAX-1] = {NULL, -1}}, 0 }
, .fanout = { [0 ... PCAP_FANOUT_GROUP_DEFAULT] = NULL }
, .caplen = handle->snapshot
#ifdef PCAP_SUPPORT_PFQ
, .pfq_rx_slots = 4096
, .pfq_tx_slots = 4096
, .pfq_tx_sync = 1
, .pfq_tx_async = 0
, .pfq_tx_hw_queue = {-1, -1, -1, -1}
, .pfq_tx_idx_thread = { Q_NO_KTHREAD, Q_NO_KTHREAD, Q_NO_KTHREAD, Q_NO_KTHREAD }
, .pfq_vlan = {[0 ... PCAP_FANOUT_GROUP_DEFAULT] = NULL }
#endif
};
}
void
pcap_group_map_dump(struct pcap_group_map const *map)
{
int n = 0;
for(; n < map->size; n++)
fprintf(stderr, "libpcap: config group for dev '%s' = %d\n", map->entry[n].dev, map->entry[n].group);
}
void
pcap_group_map_free(struct pcap_group_map *map)
{
int n = 0;
for(; n < map->size; n++)
free(map->entry[n].dev);
}
int
pcap_group_map_set(struct pcap_group_map *map, const char *dev, int group)
{
int n = 0;
for(; n < map->size; n++) {
if (strcmp(map->entry[n].dev, dev) == 0)
break;
}
if (n == PCAP_FANOUT_GROUP_MAX)
return -1;
free(map->entry[n].dev);
map->entry[n].dev = strdup(dev);
map->entry[n].group = group;
if (n == map->size)
map->size++;
return 0;
}
int
pcap_group_map_get(struct pcap_group_map const *map, const char *dev)
{
int n = 0;
for(; n < map->size; n++) {
if (strcmp(map->entry[n].dev, dev) == 0)
return map->entry[n].group;
}
return -1;
}
static void
pcap_warn_if(int index, const char *filename, const char *key)
{
if (index != PCAP_FANOUT_GROUP_DEFAULT)
fprintf(stderr, "libpcap:%s: key %s: group ignored!\n", filename, key);
}
int
pcap_parse_integers(int *out, size_t max, const char *in)
{
size_t n = 0; int ret = 0;
int store_int(const char *num) {
if (n < max) {
out[n++] = atoi(num);
ret++;
}
return 0;
}
if (pcap_string_for_each_token(in, ",", store_int) < 0)
return -1;
return ret;
}
int
pcap_string_for_each_token(const char *ds, const char *sep, pcap_string_handler_t handler)
{
char * copy = strdup(ds);
char *str, *token, *saveptr;
int i, ret = 0;
for (i = 1, str = copy; ; i++, str = NULL)
{
token = strtok_r(str, sep, &saveptr);
if (token == NULL)
break;
if (handler(token) < 0) {
ret = PCAP_ERROR;
break;
}
}
free(copy);
return ret;
}
char *
pcap_string_first_token(const char *str, const char *sep)
{
char *end;
if ((end = strstr(str, sep))) {
char *ret = (char *)malloc(end - str + 1);
strncpy(ret, str, end - str);
ret[end - str] = '\0';
return ret;
}
return strdup(str);
}
char *
pcap_string_trim(char *str)
{
int i = 0, j = strlen(str) - 1;
while (isspace(str[i]) && str[i] != '\0')
i++;
while (j >= 0 && isspace(str[j]))
j--;
str[j+1] = '\0';
return str+i;
}
char *
pcap_string_append(char *str1, const char *str2)
{
char *ret;
if (str1) {
ret = (char *)realloc(str1, strlen(str1) + strlen(str2) + 1);
strcat(ret, str2);
}
else {
ret = (char *)malloc(strlen(str2) + 1);
strcpy(ret, str2);
}
return ret;
}
static const char *
pcap_conf_get_key_name(char const *key)
{
static __thread char storage[64];
char * p = strchr(key, '@');
int len;
if (p == NULL)
return key;
len = min(63, p - key);
strncpy(storage, key, len);
storage[len] = '\0';
return storage;
}
static int
pcap_conf_get_key_index(char const *key)
{
char * p = strchr(key, '@');
if (p == NULL)
return -1;
return atoi(p+1);
}
static int
pcap_conf_find_key(const char *key, int *index)
{
char const *this_key;
int n;
this_key = pcap_conf_get_key_name(key);
*index = pcap_conf_get_key_index(key);
for(n = 0; n < pcap_conf_keys_eof ; n++)
{
if (strcasecmp(pcap_conf_keys[n].value, this_key) == 0)
return n;
}
return -1;
}
char *
pcap_getenv_name(char const *var)
{
static __thread char name[64];
char * end = strchr(var, '=');
if (end) {
strncpy(name, var, (size_t)min(63,end-var));
name[min(63,end-var)] = '\0';
}
else {
strcpy(name, var);
}
return name;
}
char *
pcap_getenv_value(char const *var)
{
char *eq = strchr(var, '=');
return eq ? eq+1 : NULL;
}
char **
pcap_getenv(char const *name)
{
static __thread char *env[64];
char **cur = environ;
int size = 0;
while (*cur && size < 64) {
if (strncmp(*cur, name, strlen(name)) == 0) {
env[size++] = *cur;
}
cur++;
}
env[size] = NULL;
return env;
}
int
pcap_parse_config(struct pcap_config *opt, const char *filename)
{
char line[1024];
FILE *file;
int rc = 0, n;
file = fopen(filename, "r");
if (!file) {
fprintf(stderr, "libpcap: could not open '%s' file!\n", filename);
rc = -1; goto err;
}
for(n = 0; fgets(line, sizeof(line), file); n++) {
char *key = NULL, *value = NULL, *tkey;
int ktype, index, ret;
ret = sscanf(line, "%m[^=]=%m[^\n]",&key, &value);
if (ret < 0) {
fprintf(stderr, "libcap:%s: parse error at: %s\n", filename, key);
rc = -1; goto next;
}
if (ret == 0)
goto next;
/* ret > 0 */
tkey = pcap_string_trim(key);
if (strlen(tkey) == 0)
continue;
/* strlen > 0 */
if (line[0] == '>') {
opt->fanout[PCAP_FANOUT_GROUP_DEFAULT] = pcap_string_append(opt->fanout[PCAP_FANOUT_GROUP_DEFAULT], line+1);
opt->fanout[PCAP_FANOUT_GROUP_DEFAULT] = pcap_string_append(opt->fanout[PCAP_FANOUT_GROUP_DEFAULT], "\n");
continue;
}
if (tkey[0] == '#') /* skip comments */
continue;
if (strncasecmp(tkey, "group_", 6) == 0)
{
char *dev = strdup(pcap_getenv_name(tkey + sizeof("group_")-1));
if (pcap_group_map_set(&opt->group_map, dev, atoi(value)) < 0) {
fprintf(stderr, "libpcap:%s: '%s': group map error!\n", filename, tkey);
rc = -1;
goto next;
}
continue;
}
ktype = pcap_conf_find_key(tkey, &index);
index = index == -1 ? PCAP_FANOUT_GROUP_DEFAULT : index;
switch(ktype)
{
case PCAP_CONF_KEY_def_group: {
pcap_warn_if(index, filename, tkey);
opt->def_group = atoi(value);
} break;
case PCAP_CONF_KEY_fanout: {
free (opt->fanout[index]);
opt->fanout[index] = strdup(pcap_string_trim(value));
} break;
case PCAP_CONF_KEY_caplen: {
pcap_warn_if(index, filename, tkey);
opt->caplen = atoi(value);
} break;
#ifdef PCAP_SUPPORT_PFQ
case PCAP_CONF_KEY_pfq_rx_slots: {
pcap_warn_if(index, filename, tkey);
opt->pfq_rx_slots = atoi(value);
} break;
case PCAP_CONF_KEY_pfq_tx_slots: {
pcap_warn_if(index, filename, tkey);
opt->pfq_tx_slots = atoi(value);
} break;
case PCAP_CONF_KEY_pfq_tx_sync: {
pcap_warn_if(index, filename, tkey);
opt->pfq_tx_sync = atoi(value);
} break;
case PCAP_CONF_KEY_pfq_tx_hw_queue: {
pcap_warn_if(index, filename, tkey);
if (pcap_parse_integers(opt->pfq_tx_hw_queue, 4, value) < 0) {
fprintf(stderr, "libpcap:%s: parse error at: %s\n", filename, tkey);
rc = -1;
}
} break;
case PCAP_CONF_KEY_pfq_tx_idx_thread: {
pcap_warn_if(index, filename, tkey);
if (pcap_parse_integers(opt->pfq_tx_idx_thread, 4, value) < 0) {
fprintf(stderr, "libpcap:%s: parse error at: %s\n", filename, tkey);
rc = -1;
}
} break;
case PCAP_CONF_KEY_pfq_vlan: {
free (opt->pfq_vlan[index]);
opt->pfq_vlan[index] = strdup(pcap_string_trim(value));
} break;
#endif
case PCAP_CONF_KEY_error:
default: {
fprintf(stderr, "libpcap:%s: parse error at: %s (invalid keyword)\n", filename, tkey);
rc = -1;
} break;
}
next:
free(key);
free(value);
if (rc == -1)
break;
}
fclose(file);
err:
return rc;
}