Debian Codegraph @

src/common/ITG.cpp

raw
1 /*
2 * Component of the D-ITG 2.8.1 (r1023) platform (http://traffic.comics.unina.it/software/ITG)
3 *
4 * Copyright : (C) 2004-2013 by Alessio Botta, Walter de Donato, Alberto Dainotti,
5 * Stefano Avallone, Antonio Pescape' (PI)
6 * COMICS (COMputer for Interaction and CommunicationS) Group
7 * Department of Electrical Engineering and Information Technologies
8 * University of Napoli "Federico II".
9 * email : [email protected], [email protected], [email protected],
10 * [email protected], [email protected]
11 *
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation, either version 3 of the License, or
15 * (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 *
25 * For commercial use please refer to D-ITG Professional.
26 */
27
28
29
30 #include "ITG.h"
31
32 #ifdef UNIX
33 struct sched_param p;
34 #endif
35
36
37 const char *meters[] = { "", "OWDM", "RTTM", " " };
38 const char *l4Protocols[] = { "", "TCP", "UDP", "ICMP", "SCTP", "DCCP", " " };
39 const char *l7Protocols[] = { "", "Telnet", "VoIP", "DNS", "AoM", "CSa", "CSi", "Quake3", " " };
40 char programName[100];
41 char DEFAULT_LOG_IP[10] = "127.0.0.1";
42
43
44
45
46
47
48 extern const int StandardMinPayloadSize = 4 * sizeof(uint32_t);
49 extern const int ShortMinPayloadSize = 2 * sizeof(uint32_t);
50 extern const int NoneMinPayloadSize = sizeof(uint32_t);
51
52
53
54 USHORT checksum(USHORT * buffer, int size)
55 {
56 unsigned long cksum = 0;
57
58 while (size > 1) {
59 cksum += *buffer++;
60 size -= sizeof(USHORT);
61 }
62
63 if (size) {
64 cksum += *(UCHAR *) buffer;
65 }
66
67 cksum = (cksum >> 16) + (cksum & 0xffff);
68 cksum += (cksum >> 16);
69 return (USHORT) (~cksum);
70 }
71
72 #ifdef WIN32
73 int InitializeWinsock(WORD wVersionRequested)
74 {
75 WSADATA wsaData;
76 int err;
77
78 err = WSAStartup(wVersionRequested, &wsaData);
79
80
81
82 if (err != 0)
83 return 0;
84
85
86
87
88
89
90
91
92
93
94
95
96
97 if (LOBYTE(wsaData.wVersion) != 1 || HIBYTE(wsaData.wVersion) != 1)
98 return 0;
99
100
101 return 1;
102 }
103
104
105
106
107 void sleep(int tempo)
108 {
109 Sleep(tempo * 1000);
110 }
111 #endif
112
113
114
115
116
117 char *putValue(void *startPos, void *data, int size)
118 {
119
120 char *endPos;
121
122 memcpy(startPos, data, size);
123
124 endPos = (char *) startPos + size;
125 return endPos;
126 }
127
128 BYTE findMeter(char *s)
129 {
130 int i = 1;
131
132 while (strcmp(meters[i], " "))
133 if (!strcasecmp(s, (char *) meters[i]))
134 return(i);
135 else
136 i++;
137 return LX_ERROR_BYTE;
138 }
139
140 BYTE findL4Proto(char *s)
141 {
142 int i = 1;
143 while (strcmp(l4Protocols[i], " "))
144 if (!strcasecmp(s, (char *) l4Protocols[i]))
145 return(i);
146 else
147 i++;
148 return LX_ERROR_BYTE;
149 }
150
151 BYTE findL7Proto(char *s)
152 {
153 int i = 1;
154 while (strcmp(l7Protocols[i], " "))
155 if (!strcasecmp(s, (char *) l7Protocols[i]))
156 return(i);
157 else
158 i++;
159 return LX_ERROR_BYTE;
160 }
161
162 const char* invFindMeter(BYTE meter)
163 {
164 return meters[meter];
165 }
166
167 const char* invFindL4Proto(BYTE proto)
168 {
169 return l4Protocols[proto];
170 }
171
172 const char* invFindL7Proto(BYTE proto)
173 {
174 return l7Protocols[proto];
175 }
176
177
178 #ifdef WIN32
179 char
180 *strtok_r (char *restrict string, const char *restrict delim, char **saveptr)
181 {
182 size_t i = 0;
183 size_t j;
184 int found_token;
185 int found_delim;
186
187
188
189
190
191 if (string == NULL)
192 {
193 if (*saveptr == NULL)
194 {
195 return NULL;
196 }
197 else
198 {
199 string = *saveptr;
200 }
201 }
202
203
204
205
206 if (string[0] == 0)
207 {
208 *saveptr = NULL;
209 return NULL;
210 }
211 else
212 {
213 if (delim[0] == 0)
214 {
215 return string;
216 }
217 }
218
219
220
221 for (i = 0, found_token = 0, j = 0; string[i] != 0 && (!found_token); i++)
222 {
223
224
225
226 for (j = 0, found_delim = 0; delim[j] != 0; j++)
227 {
228 if (string[i] == delim[j])
229 {
230 found_delim = 1;
231 }
232 }
233
234
235
236
237
238 if (!found_delim)
239 {
240 found_token = 1;
241 break;
242 }
243 }
244
245
246
247
248
249
250 if (found_token)
251 {
252 string += i;
253 }
254 else
255 {
256 *saveptr = NULL;
257 return NULL;
258 }
259
260
261
262 for (i = 0, found_delim = 0; string[i] != 0; i++)
263 {
264 for (j = 0; delim[j] != 0; j++)
265 {
266 if (string[i] == delim[j])
267 {
268 found_delim = 1;
269 break;
270 }
271 }
272 if (found_delim)
273 {
274 break;
275 }
276 }
277
278
279
280
281
282
283
284 if (found_delim)
285 {
286 string[i] = 0;
287 *saveptr = &string[i+1];
288 }
289 else
290 {
291 *saveptr = NULL;
292 }
293
294
295
296
297 return string;
298 }
299
300 #endif
301
302 void printVersion(const char* ProgramName)
303 {
304
305 printf("%s", ProgramName);
306
307 #ifdef VERSION
308
309 printf(" version %s", VERSION);
310 #endif
311
312 #ifdef REVISION
313
314 printf(" (r%s)", REVISION);
315 #endif
316 printf("\n");
317
318
319 printf("Compile-time options:");
320
321 #ifdef SCTP
322 printf(" sctp");
323 #endif
324 #ifdef DCCP
325 printf(" dccp");
326 #endif
327 #ifdef BURSTY
328 printf(" bursty");
329 #endif
330 #ifdef MULTIPORT
331 printf(" multiport");
332 #endif
333 #if defined WIN32 && not defined IPv6RECV
334 printf(" ipv4only");
335 #endif
336 #ifdef DEBUG
337 printf(" debug");
338 #endif
339 printf("\n");
340
341 }
342