forked from appneta/tcpreplay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
list.c
197 lines (160 loc) · 4.55 KB
/
list.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
/* $Id$ */
/*
* Copyright (c) 2001-2010 Aaron Turner <aturner at synfin dot net>
* Copyright (c) 2013-2017 Fred Klassen <tcpreplay at appneta dot com> - AppNeta
*
* The Tcpreplay Suite of tools 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 3 of the
* License, or with the authors permission any later version.
*
* The Tcpreplay Suite 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 the Tcpreplay Suite. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* A generic method to parse a list of integers which are
* delimited by commas and dashes to indicate individual
* numbers and ranges
* Provides both a way to process the list and determine
* if an integer exists in the list.
*/
#include "config.h"
#include "defines.h"
#include "common.h"
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <regex.h>
#include <errno.h>
/**
* Creates a new tcpr_list entry. Malloc's memory.
*/
tcpr_list_t *
new_list()
{
tcpr_list_t *newlist;
newlist = (tcpr_list_t *)safe_malloc(sizeof(tcpr_list_t));
return (newlist);
}
/**
* Processes a string (ourstr) containing the list in human readable
* format and places the data in **list and finally returns 1 for
* success, 0 for fail.
*/
int
parse_list(tcpr_list_t ** listdata, char *ourstr)
{
tcpr_list_t *listcur, *list_ptr;
char *this = NULL;
char *first, *second;
int rcode;
regex_t preg;
char ebuf[EBUF_SIZE];
char regex[] = "^[0-9]+(-[0-9]+)?$";
char *token = NULL;
u_int i;
/* compile the regex first */
if ((rcode = regcomp(&preg, regex, REG_EXTENDED | REG_NOSUB)) != 0) {
regerror(rcode, &preg, ebuf, sizeof(ebuf));
errx(-1, "Unable to compile regex (%s): %s", regex, ebuf);
}
/* first iteration */
this = strtok_r(ourstr, ",", &token);
first = this;
second = NULL;
/* regex test */
if (regexec(&preg, this, 0, NULL, 0) != 0) {
warnx("Unable to parse: %s", this);
return 0;
}
*listdata = new_list();
list_ptr = *listdata;
listcur = list_ptr;
for (i = 0; i < strlen(this); i++) {
if (this[i] == '-') {
this[i] = '\0';
second = &this[i + 1];
}
}
list_ptr->min = strtoull(first, NULL, 0);
if (second != NULL) {
list_ptr->max = strtoull(second, NULL, 0);
}
else {
list_ptr->max = list_ptr->min;
}
while (1) {
this = strtok_r(NULL, ",", &token);
if (this == NULL)
break;
first = this;
second = NULL;
/* regex test */
if (regexec(&preg, this, 0, NULL, 0) != 0) {
warnx("Unable to parse: %s", this);
return 0;
}
listcur->next = new_list();
listcur = listcur->next;
for (i = 0; i < strlen(this); i++) {
if (this[i] == '-') {
this[i] = '\0';
second = &this[i + 1];
}
}
listcur->min = strtoull(first, NULL, 0);
if (second != NULL) {
listcur->max = strtoull(second, NULL, 0);
}
else {
listcur->max = listcur->min;
}
}
return 1;
}
/**
* Checks to see if the given integer exists in the LIST.
*/
tcpr_dir_t
check_list(tcpr_list_t * list, COUNTER value)
{
tcpr_list_t *current;
current = list;
do {
if ((current->min != 0) && (current->max != 0)) {
if ((value >= current->min) && (value <= current->max))
return TCPR_DIR_C2S;
}
else if (current->min == 0) {
if (value <= current->max)
return TCPR_DIR_C2S;
}
else if (current->max == 0) {
if (value >= current->min)
return TCPR_DIR_C2S;
}
if (current->next != NULL) {
current = current->next;
}
else {
current = NULL;
}
} while (current != NULL);
return TCPR_DIR_S2C;
}
/**
* Free's all the memory associated with the given LIST
*/
void
free_list(tcpr_list_t * list)
{
/* recursively go down the list */
if (list->next != NULL)
free_list(list->next);
safe_free(list);
}