1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2004 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * |
---|
8 | * All rights reserved. |
---|
9 | * |
---|
10 | * This code has been developed by the University of Waikato WAND |
---|
11 | * research group. For further information please see http://www.wand.net.nz/ |
---|
12 | * |
---|
13 | * libtrace is free software; you can redistribute it and/or modify |
---|
14 | * it under the terms of the GNU General Public License as published by |
---|
15 | * the Free Software Foundation; either version 2 of the License, or |
---|
16 | * (at your option) any later version. |
---|
17 | * |
---|
18 | * libtrace is distributed in the hope that it will be useful, |
---|
19 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | * GNU General Public License for more details. |
---|
22 | * |
---|
23 | * You should have received a copy of the GNU General Public License |
---|
24 | * along with libtrace; if not, write to the Free Software |
---|
25 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
26 | * |
---|
27 | * $Id$ |
---|
28 | * |
---|
29 | */ |
---|
30 | |
---|
31 | |
---|
32 | /** @file |
---|
33 | * |
---|
34 | * @brief Trace file processing library |
---|
35 | * |
---|
36 | * @author Daniel Lawson |
---|
37 | * @author Perry Lorier |
---|
38 | * |
---|
39 | * @internal |
---|
40 | */ |
---|
41 | #define _GNU_SOURCE |
---|
42 | #include "common.h" |
---|
43 | #include "config.h" |
---|
44 | #include <assert.h> |
---|
45 | #include <errno.h> |
---|
46 | #include <fcntl.h> |
---|
47 | #include <netdb.h> |
---|
48 | #include <stdio.h> |
---|
49 | #include <stdlib.h> |
---|
50 | #include <string.h> |
---|
51 | #include <sys/stat.h> |
---|
52 | #include <sys/types.h> |
---|
53 | |
---|
54 | #ifdef HAVE_LIMITS_H |
---|
55 | # include <limits.h> |
---|
56 | #endif |
---|
57 | |
---|
58 | #ifdef HAVE_SYS_LIMITS_H |
---|
59 | # include <sys/limits.h> |
---|
60 | #endif |
---|
61 | |
---|
62 | #include <sys/socket.h> |
---|
63 | #include <sys/un.h> |
---|
64 | #include <sys/mman.h> |
---|
65 | #include <unistd.h> |
---|
66 | #include <net/ethernet.h> |
---|
67 | #include <time.h> |
---|
68 | #include <sys/ioctl.h> |
---|
69 | |
---|
70 | #ifdef HAVE_STDINT_H |
---|
71 | # include <stdint.h> |
---|
72 | #else |
---|
73 | # error "Can't find stdint.h - you need to replace this" |
---|
74 | #endif |
---|
75 | |
---|
76 | #ifdef HAVE_STDDEF_H |
---|
77 | # include <stddef.h> |
---|
78 | #else |
---|
79 | # error "Can't find stddef.h - do you define ptrdiff_t elsewhere?" |
---|
80 | #endif |
---|
81 | |
---|
82 | #include "libtrace.h" |
---|
83 | #include "fifo.h" |
---|
84 | |
---|
85 | #if HAVE_PCAP_BPF_H |
---|
86 | # include <pcap-bpf.h> |
---|
87 | # ifndef HAVE_BPF |
---|
88 | # define HAVE_BPF 1 |
---|
89 | # endif |
---|
90 | #else |
---|
91 | # ifdef HAVE_NET_BPF_H |
---|
92 | # include <net/bpf.h> |
---|
93 | # ifndef HAVE_BPF |
---|
94 | # define HAVE_BPF 1 |
---|
95 | # endif |
---|
96 | # endif |
---|
97 | #endif |
---|
98 | |
---|
99 | #if HAVE_PCAP_H |
---|
100 | # include <pcap.h> |
---|
101 | # ifndef HAVE_PCAP |
---|
102 | # define HAVE_PCAP 1 |
---|
103 | # endif |
---|
104 | #endif |
---|
105 | |
---|
106 | #ifdef HAVE_ZLIB_H |
---|
107 | # include <zlib.h> |
---|
108 | # ifndef HAVE_ZLIB |
---|
109 | # define HAVE_ZLIB 1 |
---|
110 | # endif |
---|
111 | #endif |
---|
112 | |
---|
113 | |
---|
114 | #include "wag.h" |
---|
115 | |
---|
116 | #ifdef HAVE_DAG_API |
---|
117 | # include "dagnew.h" |
---|
118 | # include "dagapi.h" |
---|
119 | # ifndef DAGDEVICE |
---|
120 | # define DAGDEVICE 1 |
---|
121 | # endif |
---|
122 | #else |
---|
123 | # include "dagformat.h" |
---|
124 | #endif |
---|
125 | |
---|
126 | |
---|
127 | typedef enum {SOCKET, TRACE, STDIN, DEVICE, INTERFACE, RT } source_t; |
---|
128 | |
---|
129 | typedef enum {ERF, PCAP, PCAPINT, DAG, RTCLIENT, WAG, WAGINT } format_t; |
---|
130 | |
---|
131 | #if HAVE_BPF |
---|
132 | /** A type encapsulating a bpf filter |
---|
133 | * This type covers the compiled bpf filter, as well as the original filter |
---|
134 | * string |
---|
135 | * |
---|
136 | */ |
---|
137 | struct libtrace_filter_t { |
---|
138 | struct bpf_insn *filter; |
---|
139 | char * filterstring; |
---|
140 | }; |
---|
141 | #endif |
---|
142 | |
---|
143 | /** The information about traces that are open |
---|
144 | * @internal |
---|
145 | */ |
---|
146 | struct libtrace_t { |
---|
147 | format_t format; /**< The format that this trace is in */ |
---|
148 | source_t sourcetype; /**< The type (device,file, etc */ |
---|
149 | union { |
---|
150 | /** Information about rtclients */ |
---|
151 | struct { |
---|
152 | char *hostname; |
---|
153 | short port; |
---|
154 | } rt; |
---|
155 | char *path; /**< information for local sockets */ |
---|
156 | char *interface; /**< intormation for reading of network |
---|
157 | interfaces */ |
---|
158 | } conn_info; |
---|
159 | /** Information about the current state of the input device */ |
---|
160 | union { |
---|
161 | int fd; |
---|
162 | #if HAVE_ZLIB |
---|
163 | gzFile *file; |
---|
164 | #else |
---|
165 | FILE *file; |
---|
166 | #endif |
---|
167 | #if HAVE_PCAP |
---|
168 | pcap_t *pcap; |
---|
169 | #endif |
---|
170 | } input; |
---|
171 | struct fifo_t *fifo; |
---|
172 | struct { |
---|
173 | void *buf; |
---|
174 | unsigned bottom; |
---|
175 | unsigned top; |
---|
176 | unsigned diff; |
---|
177 | unsigned curr; |
---|
178 | unsigned offset; |
---|
179 | } dag; |
---|
180 | struct { |
---|
181 | void *buffer; |
---|
182 | int size; |
---|
183 | } packet; |
---|
184 | double last_ts; |
---|
185 | double start_ts; |
---|
186 | }; |
---|
187 | |
---|
188 | #define RP_BUFSIZE 65536 |
---|
189 | |
---|
190 | #define URI_PROTO_LINE 16 |
---|
191 | static int init_trace(struct libtrace_t **libtrace, char *uri) { |
---|
192 | char *scan = calloc(sizeof(char),URI_PROTO_LINE); |
---|
193 | char *uridata = 0; |
---|
194 | struct stat buf; |
---|
195 | |
---|
196 | // parse the URI to determine what sort of event we are dealing with |
---|
197 | |
---|
198 | // want snippet before the : to get the uri base type. |
---|
199 | |
---|
200 | if((uridata = strchr(uri,':')) == NULL) { |
---|
201 | // badly formed URI - needs a : |
---|
202 | return 0; |
---|
203 | } |
---|
204 | |
---|
205 | if ((*uridata - *uri) > URI_PROTO_LINE) { |
---|
206 | // badly formed URI - uri type is too long |
---|
207 | return 0; |
---|
208 | } |
---|
209 | strncpy(scan,uri, (uridata - uri)); |
---|
210 | |
---|
211 | if (!strncasecmp(scan,"erf",3)) { |
---|
212 | (*libtrace)->format=ERF; |
---|
213 | #if HAVE_PCAP |
---|
214 | } else if (!strncasecmp(scan,"pcapint",7)) { |
---|
215 | (*libtrace)->format=PCAPINT; |
---|
216 | } else if (!strncasecmp(scan,"pcap",4)) { |
---|
217 | (*libtrace)->format=PCAP; |
---|
218 | #else |
---|
219 | } else if (!strncasecmp(scan,"pcap",4)) { // also catches pcapint |
---|
220 | fprintf(stderr,"This version of libtrace has been compiled without PCAP support\n"); |
---|
221 | return 0; |
---|
222 | #endif |
---|
223 | |
---|
224 | #ifdef DAGDEVICE |
---|
225 | } else if (!strncasecmp(scan,"dag",3)) { |
---|
226 | (*libtrace)->format=DAG; |
---|
227 | #else |
---|
228 | } else if (!strncasecmp(scan,"dag",3)) { |
---|
229 | fprintf(stderr,"This version of libtrace has been compiled without DAG support\n"); |
---|
230 | return 0; |
---|
231 | #endif |
---|
232 | } else if (!strncasecmp(scan,"rtclient",7)) { |
---|
233 | (*libtrace)->format=RTCLIENT; |
---|
234 | } else if (!strncasecmp(scan,"wagint",6)) { |
---|
235 | (*libtrace)->format=WAGINT; |
---|
236 | } else if (!strncasecmp(scan,"wag",3)) { |
---|
237 | (*libtrace)->format=WAG; |
---|
238 | } else { |
---|
239 | //badly formed URI |
---|
240 | return 0; |
---|
241 | } |
---|
242 | |
---|
243 | // push uridata past the delimiter |
---|
244 | uridata++; |
---|
245 | |
---|
246 | // libtrace->format now contains the type of uri |
---|
247 | // libtrace->uridata contains the appropriate data for this |
---|
248 | |
---|
249 | switch((*libtrace)->format) { |
---|
250 | #if HAVE_PCAP |
---|
251 | case PCAPINT: |
---|
252 | #endif |
---|
253 | case WAGINT: |
---|
254 | /* Can have uridata of the following format |
---|
255 | * eth0 |
---|
256 | * etc |
---|
257 | */ |
---|
258 | // We basically assume this is correct. |
---|
259 | (*libtrace)->sourcetype = INTERFACE; |
---|
260 | (*libtrace)->conn_info.path = strdup(uridata); |
---|
261 | break; |
---|
262 | #if HAVE_PCAP |
---|
263 | case PCAP: |
---|
264 | #endif |
---|
265 | case ERF: |
---|
266 | case WAG: |
---|
267 | /* |
---|
268 | * Can have uridata of the following format |
---|
269 | * /path/to/socket (probably not PCAP) |
---|
270 | * /path/to/file |
---|
271 | * /path/to/file.gz (not PCAP) |
---|
272 | * /dev/device (use PCAPINT) |
---|
273 | * - |
---|
274 | */ |
---|
275 | if (!strncmp(uridata,"-",1)) { |
---|
276 | (*libtrace)->sourcetype = STDIN; |
---|
277 | } else { |
---|
278 | if (stat(uridata,&buf) == -1) { |
---|
279 | perror("stat"); |
---|
280 | return 0; |
---|
281 | } |
---|
282 | if (S_ISSOCK(buf.st_mode)) { |
---|
283 | (*libtrace)->sourcetype = SOCKET; |
---|
284 | } else if (S_ISCHR(buf.st_mode)) { |
---|
285 | (*libtrace)->sourcetype = DEVICE; |
---|
286 | } else { |
---|
287 | (*libtrace)->sourcetype = TRACE; |
---|
288 | } |
---|
289 | (*libtrace)->conn_info.path = strdup(uridata); |
---|
290 | } |
---|
291 | break; |
---|
292 | case DAG: |
---|
293 | #ifdef DAGDEVICE |
---|
294 | /* |
---|
295 | * Can have uridata of the following format: |
---|
296 | * /dev/device |
---|
297 | */ |
---|
298 | if (stat(uridata,&buf) == -1) { |
---|
299 | perror("stat"); |
---|
300 | return 0; |
---|
301 | } |
---|
302 | if (S_ISCHR(buf.st_mode)) { |
---|
303 | (*libtrace)->sourcetype = DEVICE; |
---|
304 | } else { |
---|
305 | fprintf(stderr,"%s isn't a valid char device, exiting\n",uridata); |
---|
306 | exit(1); |
---|
307 | } |
---|
308 | (*libtrace)->conn_info.path = strdup(uridata); |
---|
309 | #endif |
---|
310 | break; |
---|
311 | |
---|
312 | case RTCLIENT: |
---|
313 | /* |
---|
314 | * Can have the uridata in the format |
---|
315 | * hostname |
---|
316 | * hostname:port |
---|
317 | */ |
---|
318 | (*libtrace)->sourcetype = RT; |
---|
319 | if (strlen(uridata) == 0) { |
---|
320 | (*libtrace)->conn_info.rt.hostname = |
---|
321 | strdup("localhost"); |
---|
322 | (*libtrace)->conn_info.rt.port = |
---|
323 | COLLECTOR_PORT; |
---|
324 | break; |
---|
325 | } |
---|
326 | if ((scan = strchr(uridata,':')) == NULL) { |
---|
327 | (*libtrace)->conn_info.rt.hostname = |
---|
328 | strdup(uridata); |
---|
329 | (*libtrace)->conn_info.rt.port = |
---|
330 | COLLECTOR_PORT; |
---|
331 | } else { |
---|
332 | (*libtrace)->conn_info.rt.hostname = |
---|
333 | (char *)strndup(uridata,(scan - uridata)); |
---|
334 | |
---|
335 | (*libtrace)->conn_info.rt.port = |
---|
336 | atoi(++scan); |
---|
337 | } |
---|
338 | break; |
---|
339 | } |
---|
340 | |
---|
341 | |
---|
342 | (*libtrace)->fifo = create_fifo(1048576); |
---|
343 | assert( (*libtrace)->fifo); |
---|
344 | //(*libtrace)->packet.buffer = 0; |
---|
345 | //(*libtrace)->packet.size = 0; |
---|
346 | |
---|
347 | return 1; |
---|
348 | } |
---|
349 | |
---|
350 | /** Create a trace file from a URI |
---|
351 | * |
---|
352 | * @returns opaque pointer to a libtrace_t |
---|
353 | * |
---|
354 | * Valid URI's are: |
---|
355 | * erf:/path/to/erf/file |
---|
356 | * erf:/path/to/erf/file.gz |
---|
357 | * erf:/path/to/rtclient/socket |
---|
358 | * erf:- (stdin) |
---|
359 | * pcapint:pcapinterface (eg: pcapint:eth0) |
---|
360 | * pcap:/path/to/pcap/file |
---|
361 | * pcap:- |
---|
362 | * rtclient:hostname |
---|
363 | * rtclient:hostname:port |
---|
364 | * wag:- |
---|
365 | * wag:/path/to/wag/file |
---|
366 | * wag:/path/to/wag/file.gz |
---|
367 | * wag:/path/to/wag/socket |
---|
368 | * wagint:/dev/device |
---|
369 | * |
---|
370 | * URIs which have yet to be implemented are: |
---|
371 | * dag:/dev/dagcard |
---|
372 | * pcap:/path/to/pcap/socket |
---|
373 | * |
---|
374 | * If an error occured when attempting to open a trace, NULL is returned |
---|
375 | * and an error is output to stdout. |
---|
376 | */ |
---|
377 | struct libtrace_t *trace_create(char *uri) { |
---|
378 | struct libtrace_t *libtrace = malloc(sizeof(struct libtrace_t)); |
---|
379 | struct hostent *he; |
---|
380 | struct sockaddr_in remote; |
---|
381 | struct sockaddr_un unix_sock; |
---|
382 | #if HAVE_PCAP |
---|
383 | char errbuf[PCAP_ERRBUF_SIZE]; |
---|
384 | #endif |
---|
385 | |
---|
386 | if(init_trace(&libtrace,uri) == 0) { |
---|
387 | return 0; |
---|
388 | } |
---|
389 | |
---|
390 | switch(libtrace->sourcetype) { |
---|
391 | case RT: |
---|
392 | if ((he=gethostbyname(libtrace->conn_info.rt.hostname)) == NULL) { |
---|
393 | perror("gethostbyname"); |
---|
394 | return 0; |
---|
395 | } |
---|
396 | if ((libtrace->input.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
---|
397 | perror("socket"); |
---|
398 | return 0; |
---|
399 | } |
---|
400 | |
---|
401 | remote.sin_family = AF_INET; |
---|
402 | remote.sin_port = htons(libtrace->conn_info.rt.port); |
---|
403 | remote.sin_addr = *((struct in_addr *)he->h_addr); |
---|
404 | bzero(&(remote.sin_zero), 8); |
---|
405 | |
---|
406 | if (connect(libtrace->input.fd, (struct sockaddr *)&remote, |
---|
407 | sizeof(struct sockaddr)) == -1) { |
---|
408 | perror("connect (inet)"); |
---|
409 | return 0; |
---|
410 | } |
---|
411 | break; |
---|
412 | case TRACE: |
---|
413 | #if HAVE_PCAP |
---|
414 | if (libtrace->format == PCAP) { |
---|
415 | if ((libtrace->input.pcap = pcap_open_offline(libtrace->conn_info.path, errbuf)) == NULL) { |
---|
416 | fprintf(stderr,"%s\n",errbuf); |
---|
417 | return 0; |
---|
418 | } |
---|
419 | } else { |
---|
420 | #else |
---|
421 | { |
---|
422 | #endif |
---|
423 | #if HAVE_ZLIB |
---|
424 | libtrace->input.file = gzopen(libtrace->conn_info.path, "r"); |
---|
425 | #else |
---|
426 | libtrace->input.file = fopen(libtrace->conn_info.path, "r"); |
---|
427 | #endif |
---|
428 | } |
---|
429 | break; |
---|
430 | case STDIN: |
---|
431 | #if HAVE_PCAP |
---|
432 | if (libtrace->format == PCAP) { |
---|
433 | libtrace->input.pcap = pcap_open_offline("-",errbuf); |
---|
434 | } else { |
---|
435 | #else |
---|
436 | { |
---|
437 | #endif |
---|
438 | #if HAVE_ZLIB |
---|
439 | libtrace->input.file = gzdopen(STDIN, "r"); |
---|
440 | #else |
---|
441 | libtrace->input.file = stdin; |
---|
442 | #endif |
---|
443 | } |
---|
444 | break; |
---|
445 | case SOCKET: |
---|
446 | /* Pcap doesn't work */ |
---|
447 | if (libtrace->format != PCAP) { |
---|
448 | if ((libtrace->input.fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { |
---|
449 | perror("socket"); |
---|
450 | return 0; |
---|
451 | } |
---|
452 | unix_sock.sun_family = AF_UNIX; |
---|
453 | bzero(unix_sock.sun_path,108); |
---|
454 | snprintf(unix_sock.sun_path,108,"%s",libtrace->conn_info.path); |
---|
455 | |
---|
456 | if (connect(libtrace->input.fd, (struct sockaddr *)&unix_sock, |
---|
457 | sizeof(struct sockaddr)) == -1) { |
---|
458 | perror("connect (unix)"); |
---|
459 | return 0; |
---|
460 | } |
---|
461 | } |
---|
462 | break; |
---|
463 | case DEVICE: |
---|
464 | case INTERFACE: |
---|
465 | switch (libtrace->format) { |
---|
466 | #if HAVE_PCAP |
---|
467 | case PCAPINT: |
---|
468 | case PCAP: |
---|
469 | libtrace->input.pcap = pcap_open_live( |
---|
470 | libtrace->conn_info.path, |
---|
471 | 4096, |
---|
472 | 1, |
---|
473 | 0, |
---|
474 | errbuf); |
---|
475 | break; |
---|
476 | #endif |
---|
477 | case WAGINT: |
---|
478 | case WAG: |
---|
479 | libtrace->input.fd = open( |
---|
480 | libtrace->conn_info.path, |
---|
481 | O_RDONLY); |
---|
482 | break; |
---|
483 | #ifdef DAGDEVICE |
---|
484 | case DAG: |
---|
485 | if((libtrace->input.fd = dag_open(libtrace->conn_info.path)) < 0) { |
---|
486 | fprintf(stderr,"Cannot open DAG %s: %m\n", libtrace->conn_info.path,errno); |
---|
487 | exit(0); |
---|
488 | } |
---|
489 | if((libtrace->dag.buf = dag_mmap(libtrace->input.fd)) == MAP_FAILED) { |
---|
490 | fprintf(stderr,"Cannot mmap DAG %s: %m\n", libtrace->conn_info.path,errno); |
---|
491 | exit(0); |
---|
492 | } |
---|
493 | if(dag_start(libtrace->input.fd) < 0) { |
---|
494 | fprintf(stderr,"Cannot start DAG %s: %m\n", libtrace->conn_info.path,errno); |
---|
495 | exit(0); |
---|
496 | } |
---|
497 | break; |
---|
498 | #endif |
---|
499 | default: |
---|
500 | fprintf(stderr,"Unknown format trace, hoping I can just read\n"); |
---|
501 | break; |
---|
502 | |
---|
503 | } |
---|
504 | break; |
---|
505 | default: |
---|
506 | fprintf(stderr,"Unsupported source type for libtrace, terminating (%i)\n",libtrace->sourcetype); |
---|
507 | exit(0); |
---|
508 | |
---|
509 | } |
---|
510 | return libtrace; |
---|
511 | } |
---|
512 | |
---|
513 | /** Close a trace file, freeing up any resources it may have been using |
---|
514 | * |
---|
515 | */ |
---|
516 | void trace_destroy(struct libtrace_t *libtrace) { |
---|
517 | assert(libtrace); |
---|
518 | #if HAVE_PCAP |
---|
519 | if (libtrace->format == PCAP || libtrace->format == PCAPINT) { |
---|
520 | pcap_close(libtrace->input.pcap); |
---|
521 | #else |
---|
522 | if (0) { |
---|
523 | #endif |
---|
524 | } else if (libtrace->sourcetype == SOCKET || libtrace->sourcetype == RT) { |
---|
525 | close(libtrace->input.fd); |
---|
526 | #ifdef DAGDEVICE |
---|
527 | } else if (libtrace->format == DAG) { |
---|
528 | dag_stop(libtrace->input.fd); |
---|
529 | #endif |
---|
530 | } else { |
---|
531 | #if HAVE_ZLIB |
---|
532 | gzclose(libtrace->input.file); |
---|
533 | #else |
---|
534 | fclose(libtrace->input.file); |
---|
535 | #endif |
---|
536 | } |
---|
537 | // need to free things! |
---|
538 | destroy_fifo(libtrace->fifo); |
---|
539 | free(libtrace); |
---|
540 | } |
---|
541 | |
---|
542 | static int trace_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
543 | int numbytes; |
---|
544 | static short lctr = 0; |
---|
545 | struct dag_record_t *recptr = 0; |
---|
546 | int rlen; |
---|
547 | assert(libtrace); |
---|
548 | assert(len >= 0); |
---|
549 | |
---|
550 | if (buffer == 0) |
---|
551 | buffer = malloc(len); |
---|
552 | |
---|
553 | while(1) { |
---|
554 | switch(libtrace->sourcetype) { |
---|
555 | case SOCKET: |
---|
556 | case RT: |
---|
557 | |
---|
558 | #ifndef MSG_NOSIGNAL |
---|
559 | #define MSG_NOSIGNAL 0 |
---|
560 | #endif |
---|
561 | // read from the network |
---|
562 | if ((numbytes=recv(libtrace->input.fd, |
---|
563 | buffer, |
---|
564 | len, |
---|
565 | MSG_NOSIGNAL)) == -1) { |
---|
566 | if (errno == EINTR) { |
---|
567 | // ignore EINTR in case |
---|
568 | // a caller is using signals |
---|
569 | continue; |
---|
570 | } |
---|
571 | perror("recv"); |
---|
572 | return -1; |
---|
573 | } |
---|
574 | break; |
---|
575 | case DEVICE: |
---|
576 | switch(libtrace->format) { |
---|
577 | #ifdef DAGDEVICE |
---|
578 | case DAG: |
---|
579 | |
---|
580 | libtrace->dag.bottom = libtrace->dag.top; |
---|
581 | libtrace->dag.top = dag_offset( |
---|
582 | libtrace->input.fd, |
---|
583 | &(libtrace->dag.bottom), |
---|
584 | 0); |
---|
585 | libtrace->dag.diff = libtrace->dag.top - |
---|
586 | libtrace->dag.bottom; |
---|
587 | |
---|
588 | numbytes=libtrace->dag.diff; |
---|
589 | libtrace->dag.offset = 0; |
---|
590 | |
---|
591 | break; |
---|
592 | #endif |
---|
593 | default: |
---|
594 | if ((numbytes=read(libtrace->input.fd, |
---|
595 | buffer, |
---|
596 | len)) == -1) { |
---|
597 | perror("read"); |
---|
598 | return -1; |
---|
599 | } |
---|
600 | } |
---|
601 | break; |
---|
602 | default: |
---|
603 | #if HAVE_ZLIB |
---|
604 | if ((numbytes=gzread(libtrace->input.file, |
---|
605 | buffer, |
---|
606 | len)) == -1) { |
---|
607 | perror("gzread"); |
---|
608 | return -1; |
---|
609 | } |
---|
610 | #else |
---|
611 | if ((numbytes=fread(buffer,len,1,libtrace->input.file)) == 0 ) { |
---|
612 | if(feof(libtrace->input.file)) { |
---|
613 | return 0; |
---|
614 | } |
---|
615 | if(ferror(libtrace->input.file)) { |
---|
616 | perror("fread"); |
---|
617 | return -1; |
---|
618 | } |
---|
619 | return 0; |
---|
620 | } |
---|
621 | #endif |
---|
622 | } |
---|
623 | break; |
---|
624 | } |
---|
625 | return numbytes; |
---|
626 | |
---|
627 | } |
---|
628 | |
---|
629 | /** Read one packet from the trace into buffer |
---|
630 | * |
---|
631 | * @param libtrace the libtrace opaque pointer |
---|
632 | * @param packet the packet opaque pointer |
---|
633 | * @returns false if it failed to read a packet |
---|
634 | * |
---|
635 | */ |
---|
636 | int trace_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
637 | int numbytes; |
---|
638 | int size; |
---|
639 | char buf[RP_BUFSIZE]; |
---|
640 | #if HAVE_PCAP |
---|
641 | struct pcap_pkthdr pcaphdr; |
---|
642 | const u_char *pcappkt; |
---|
643 | #endif |
---|
644 | dag_record_t *erfptr; |
---|
645 | int read_required = 0; |
---|
646 | |
---|
647 | void *buffer = 0; |
---|
648 | if (!libtrace) { |
---|
649 | fprintf(stderr,"Oi! You called trace_read_packet() with a NULL libtrace parameter!\n"); |
---|
650 | } |
---|
651 | assert(libtrace); |
---|
652 | assert(packet); |
---|
653 | |
---|
654 | /* Store the trace we are reading from into the packet opaque |
---|
655 | * structure */ |
---|
656 | packet->trace = libtrace; |
---|
657 | |
---|
658 | buffer = packet->buffer; |
---|
659 | #if HAVE_PCAP |
---|
660 | /* PCAP gives us it's own per-packet interface. Let's use it */ |
---|
661 | if (libtrace->format == PCAP || libtrace->format == PCAPINT) { |
---|
662 | if ((pcappkt = pcap_next(libtrace->input.pcap, &pcaphdr)) == NULL) { |
---|
663 | return -1; |
---|
664 | } |
---|
665 | memcpy(buffer,&pcaphdr,sizeof(struct pcap_pkthdr)); |
---|
666 | memcpy(buffer + sizeof(struct pcap_pkthdr),pcappkt,pcaphdr.len); |
---|
667 | numbytes = pcaphdr.len; |
---|
668 | |
---|
669 | packet->size = numbytes + sizeof(struct pcap_pkthdr); |
---|
670 | return numbytes; |
---|
671 | } |
---|
672 | #endif |
---|
673 | |
---|
674 | /* If we're reading from an ERF input, it's an offline trace. We can make some assumptions */ |
---|
675 | if (libtrace->format == ERF) { |
---|
676 | void *buffer2 = buffer; |
---|
677 | int rlen; |
---|
678 | // read in the trace header |
---|
679 | if ((numbytes=gzread(libtrace->input.file, |
---|
680 | buffer, |
---|
681 | dag_record_size)) == -1) { |
---|
682 | perror("gzread"); |
---|
683 | return -1; |
---|
684 | } |
---|
685 | if (numbytes == 0) { |
---|
686 | return 0; |
---|
687 | } |
---|
688 | rlen = ntohs(((dag_record_t *)buffer)->rlen); |
---|
689 | size = rlen - dag_record_size; |
---|
690 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
691 | buffer2 = buffer + dag_record_size; |
---|
692 | |
---|
693 | // read in the rest of the packet |
---|
694 | if ((numbytes=gzread(libtrace->input.file, |
---|
695 | buffer2, |
---|
696 | size)) == -1) { |
---|
697 | perror("gzread"); |
---|
698 | return -1; |
---|
699 | } |
---|
700 | //if ((numbytes + dag_record_size) != rlen) { |
---|
701 | // printf("read %d wanted %d\n",numbytes +dag_record_size, rlen); |
---|
702 | //} |
---|
703 | packet->size = rlen; |
---|
704 | |
---|
705 | return rlen; |
---|
706 | } |
---|
707 | |
---|
708 | #ifdef DAGDEVICE |
---|
709 | if (libtrace->format == DAG) { |
---|
710 | if (libtrace->dag.diff == 0) { |
---|
711 | if ((numbytes = trace_read(libtrace,buf,RP_BUFSIZE)) <= 0) |
---|
712 | return numbytes; |
---|
713 | } |
---|
714 | // DAG always gives us whole packets. |
---|
715 | |
---|
716 | erfptr = (dag_record_t *) ((void *)libtrace->dag.buf + (libtrace->dag.bottom + libtrace->dag.offset)); |
---|
717 | size = ntohs(erfptr->rlen); |
---|
718 | |
---|
719 | if ( size > LIBTRACE_PACKET_BUFSIZE) { |
---|
720 | printf("%d\n",size); |
---|
721 | assert( size < LIBTRACE_PACKET_BUFSIZE); |
---|
722 | } |
---|
723 | |
---|
724 | // have to copy it out of the memory hole at this stage: |
---|
725 | memcpy(packet->buffer, erfptr, size); |
---|
726 | |
---|
727 | packet->size = size; |
---|
728 | libtrace->dag.offset += size; |
---|
729 | libtrace->dag.diff -= size; |
---|
730 | |
---|
731 | assert(libtrace->dag.diff >= 0); |
---|
732 | //assert(libtrace->dag.offset <= libtrace->dag.top); |
---|
733 | return (size); |
---|
734 | |
---|
735 | } |
---|
736 | #endif |
---|
737 | do { |
---|
738 | if (fifo_out_available(libtrace->fifo) == 0 || read_required) { |
---|
739 | if ((numbytes = trace_read(libtrace,buf,RP_BUFSIZE))<=0){ |
---|
740 | return numbytes; |
---|
741 | } |
---|
742 | assert(libtrace->fifo); |
---|
743 | fifo_write(libtrace->fifo,buf,numbytes); |
---|
744 | |
---|
745 | read_required = 0; |
---|
746 | } |
---|
747 | |
---|
748 | switch (libtrace->format) { |
---|
749 | case RTCLIENT: |
---|
750 | // only do this if we're reading from the RT interface |
---|
751 | if (fifo_out_read(libtrace->fifo, &packet->status, sizeof(int)) == 0) { |
---|
752 | read_required = 1; |
---|
753 | continue; |
---|
754 | } |
---|
755 | |
---|
756 | fifo_out_update(libtrace->fifo,sizeof(int)); |
---|
757 | |
---|
758 | /* FALL THRU */ |
---|
759 | case ERF: |
---|
760 | //case DAG: |
---|
761 | // read in the erf header |
---|
762 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, sizeof(dag_record_t))) == 0) { |
---|
763 | fifo_out_reset(libtrace->fifo); |
---|
764 | read_required = 1; |
---|
765 | continue; |
---|
766 | } |
---|
767 | |
---|
768 | size = ntohs(((dag_record_t *)buffer)->rlen); |
---|
769 | break; |
---|
770 | case WAG: |
---|
771 | if ((numbytes = fifo_out_read(libtrace->fifo, |
---|
772 | &size, |
---|
773 | sizeof(size))) |
---|
774 | == 0) { |
---|
775 | fifo_out_reset(libtrace->fifo); |
---|
776 | read_required = 1; |
---|
777 | continue; |
---|
778 | } |
---|
779 | size*=4; |
---|
780 | break; |
---|
781 | default: |
---|
782 | fprintf(stderr,"Unknown type in _read()\n"); |
---|
783 | assert(0); |
---|
784 | } |
---|
785 | |
---|
786 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
787 | |
---|
788 | // read in the full packet |
---|
789 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, size)) == 0) { |
---|
790 | fifo_out_reset(libtrace->fifo); |
---|
791 | read_required = 1; |
---|
792 | continue; |
---|
793 | } |
---|
794 | |
---|
795 | // got in our whole packet, so... |
---|
796 | fifo_out_update(libtrace->fifo,size); |
---|
797 | |
---|
798 | if (libtrace->sourcetype == SOCKET || libtrace->sourcetype == RT) { |
---|
799 | fifo_ack_update(libtrace->fifo,size + sizeof(int)); |
---|
800 | } else { |
---|
801 | fifo_ack_update(libtrace->fifo,size); |
---|
802 | } |
---|
803 | |
---|
804 | packet->size = numbytes; |
---|
805 | return numbytes; |
---|
806 | |
---|
807 | } while (1); |
---|
808 | } |
---|
809 | |
---|
810 | |
---|
811 | /** get a pointer to the link layer |
---|
812 | * @param packet a pointer to a libtrace_packet structure |
---|
813 | * |
---|
814 | * @returns a pointer to the link layer, or NULL if there is no link layer |
---|
815 | * you should call trace_get_link_type() to find out what type of link layer this is |
---|
816 | */ |
---|
817 | void *trace_get_link(struct libtrace_packet_t *packet) { |
---|
818 | void *ethptr = 0; |
---|
819 | |
---|
820 | struct wag_event_t *event = (struct wag_event_t *)packet->buffer; |
---|
821 | struct wag_data_event_t *data_event; |
---|
822 | |
---|
823 | |
---|
824 | switch(packet->trace->format) { |
---|
825 | case ERF: |
---|
826 | case DAG: |
---|
827 | case RTCLIENT: |
---|
828 | if (trace_get_link_type(packet)==TRACE_TYPE_ETH) |
---|
829 | ethptr = ((uint8_t *)packet->buffer + |
---|
830 | dag_record_size + 2); |
---|
831 | else |
---|
832 | ethptr = ((uint8_t *)packet->buffer + |
---|
833 | dag_record_size + 2); |
---|
834 | break; |
---|
835 | #if HAVE_PCAP |
---|
836 | case PCAPINT: |
---|
837 | case PCAP: |
---|
838 | ethptr = (struct ether_header *)(packet->buffer + sizeof(struct pcap_pkthdr)); |
---|
839 | break; |
---|
840 | #endif |
---|
841 | case WAGINT: |
---|
842 | case WAG: |
---|
843 | switch (event->type) { |
---|
844 | case 0x0: |
---|
845 | data_event = (void*)&(event->payload); |
---|
846 | return data_event->data; |
---|
847 | default: |
---|
848 | fprintf(stderr,"Unknown WAG Event (0x%08x)\n",event->type); |
---|
849 | return NULL; |
---|
850 | } |
---|
851 | |
---|
852 | default: |
---|
853 | fprintf(stderr,"Dunno this trace format\n"); |
---|
854 | assert(0); |
---|
855 | } |
---|
856 | return ethptr; |
---|
857 | } |
---|
858 | |
---|
859 | /** get a pointer to the IP header (if any) |
---|
860 | * @param packet a pointer to a libtrace_packet structure |
---|
861 | * |
---|
862 | * @returns a pointer to the IP header, or NULL if there is not an IP packet |
---|
863 | */ |
---|
864 | struct libtrace_ip *trace_get_ip(struct libtrace_packet_t *packet) { |
---|
865 | struct libtrace_ip *ipptr = 0; |
---|
866 | |
---|
867 | switch(trace_get_link_type(packet)) { |
---|
868 | case TRACE_TYPE_80211: |
---|
869 | { |
---|
870 | |
---|
871 | struct ieee_802_11_header *wifi = trace_get_link(packet); |
---|
872 | |
---|
873 | // Data packet? |
---|
874 | if (wifi->type != 2) { |
---|
875 | ipptr = NULL; |
---|
876 | } |
---|
877 | else { |
---|
878 | struct ieee_802_11_payload *eth = (void*)wifi->data; |
---|
879 | if (eth->type != 0x0008) { |
---|
880 | ipptr=NULL; |
---|
881 | } else { |
---|
882 | ipptr=(void*)eth->data; |
---|
883 | } |
---|
884 | } |
---|
885 | } |
---|
886 | break; |
---|
887 | case TRACE_TYPE_ETH: |
---|
888 | { |
---|
889 | struct ether_header *eth = |
---|
890 | trace_get_link(packet); |
---|
891 | if (ntohs(eth->ether_type)!=0x0800) { |
---|
892 | ipptr = NULL; |
---|
893 | } |
---|
894 | else { |
---|
895 | ipptr = ((void *)eth) + 14; |
---|
896 | } |
---|
897 | break; |
---|
898 | } |
---|
899 | case TRACE_TYPE_ATM: |
---|
900 | { |
---|
901 | struct atm_rec *atm = |
---|
902 | trace_get_link(packet); |
---|
903 | // TODO: Find out what ATM does, and return |
---|
904 | // NULL for non IP data |
---|
905 | // Presumably it uses the normal stuff |
---|
906 | ipptr = (void*)&atm->pload; |
---|
907 | break; |
---|
908 | } |
---|
909 | default: |
---|
910 | fprintf(stderr,"Don't understand link layer type %i in trace_get_ip()\n", |
---|
911 | trace_get_link_type(packet)); |
---|
912 | ipptr=NULL; |
---|
913 | break; |
---|
914 | } |
---|
915 | |
---|
916 | return ipptr; |
---|
917 | } |
---|
918 | |
---|
919 | #define SW_IP_OFFMASK 0xff1f |
---|
920 | |
---|
921 | /** get a pointer to the TCP header (if any) |
---|
922 | * @param packet a pointer to a libtrace_packet structure |
---|
923 | * |
---|
924 | * @returns a pointer to the TCP header, or NULL if there is not a TCP packet |
---|
925 | */ |
---|
926 | struct libtrace_tcp *trace_get_tcp(struct libtrace_packet_t *packet) { |
---|
927 | struct libtrace_tcp *tcpptr = 0; |
---|
928 | struct libtrace_ip *ipptr = 0; |
---|
929 | |
---|
930 | if(!(ipptr = trace_get_ip(packet))) { |
---|
931 | return 0; |
---|
932 | } |
---|
933 | if ((ipptr->ip_p == 6) && ((ipptr->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
934 | tcpptr = (struct libtrace_tcp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
935 | } |
---|
936 | return tcpptr; |
---|
937 | } |
---|
938 | |
---|
939 | /** get a pointer to the TCP header (if any) given a pointer to the IP header |
---|
940 | * @param ip The IP header |
---|
941 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
942 | * |
---|
943 | * @returns a pointer to the TCP header, or NULL if this is not a TCP packet |
---|
944 | * |
---|
945 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
946 | */ |
---|
947 | struct libtrace_tcp *get_tcp_from_ip(struct libtrace_ip *ip, int *skipped) |
---|
948 | { |
---|
949 | #define SW_IP_OFFMASK 0xff1f |
---|
950 | struct libtrace_tcp *tcpptr = 0; |
---|
951 | |
---|
952 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
953 | tcpptr = (struct libtrace_tcp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
954 | } |
---|
955 | |
---|
956 | if (skipped) |
---|
957 | *skipped=(ip->ip_hl*4); |
---|
958 | |
---|
959 | return tcpptr; |
---|
960 | } |
---|
961 | |
---|
962 | /** get a pointer to the UDP header (if any) |
---|
963 | * @param packet a pointer to a libtrace_packet structure |
---|
964 | * |
---|
965 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
966 | */ |
---|
967 | struct libtrace_udp *trace_get_udp(struct libtrace_packet_t *packet) { |
---|
968 | struct libtrace_udp *udpptr = 0; |
---|
969 | struct libtrace_ip *ipptr = 0; |
---|
970 | |
---|
971 | if(!(ipptr = trace_get_ip(packet))) { |
---|
972 | return 0; |
---|
973 | } |
---|
974 | if ((ipptr->ip_p == 17) && ((ipptr->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
975 | udpptr = (struct libtrace_udp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
976 | } |
---|
977 | |
---|
978 | return udpptr; |
---|
979 | } |
---|
980 | |
---|
981 | /** get a pointer to the UDP header (if any) given a pointer to the IP header |
---|
982 | * @param ip The IP header |
---|
983 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
984 | * |
---|
985 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
986 | * |
---|
987 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
988 | */ |
---|
989 | struct libtrace_udp *get_udp_from_ip(struct libtrace_ip *ip, int *skipped) |
---|
990 | { |
---|
991 | struct libtrace_udp *udpptr = 0; |
---|
992 | |
---|
993 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
994 | udpptr = (struct libtrace_udp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
995 | } |
---|
996 | |
---|
997 | if (skipped) |
---|
998 | *skipped=(ip->ip_hl*4); |
---|
999 | |
---|
1000 | return udpptr; |
---|
1001 | } |
---|
1002 | |
---|
1003 | |
---|
1004 | /** get a pointer to the ICMP header (if any) |
---|
1005 | * @param packet a pointer to a libtrace_packet structure |
---|
1006 | * |
---|
1007 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
1008 | */ |
---|
1009 | struct libtrace_icmp *trace_get_icmp(struct libtrace_packet_t *packet) { |
---|
1010 | struct libtrace_icmp *icmpptr = 0; |
---|
1011 | struct libtrace_ip *ipptr = 0; |
---|
1012 | |
---|
1013 | if(!(ipptr = trace_get_ip(packet))) { |
---|
1014 | return 0; |
---|
1015 | } |
---|
1016 | if ((ipptr->ip_p == 1)&& ((ipptr->ip_off & SW_IP_OFFMASK) == 0 )){ |
---|
1017 | icmpptr = (struct libtrace_icmp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
1018 | } |
---|
1019 | return icmpptr; |
---|
1020 | } |
---|
1021 | |
---|
1022 | /** get a pointer to the ICMP header (if any) given a pointer to the IP header |
---|
1023 | * @param ip The IP header |
---|
1024 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
1025 | * |
---|
1026 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
1027 | * |
---|
1028 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
1029 | */ |
---|
1030 | struct libtrace_icmp *get_icmp_from_ip(struct libtrace_ip *ip, int *skipped) |
---|
1031 | { |
---|
1032 | struct libtrace_icmp *icmpptr = 0; |
---|
1033 | |
---|
1034 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
1035 | icmpptr = (struct libtrace_icmp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
1036 | } |
---|
1037 | |
---|
1038 | if (skipped) |
---|
1039 | *skipped=(ip->ip_hl*4); |
---|
1040 | |
---|
1041 | return icmpptr; |
---|
1042 | } |
---|
1043 | /** parse an ip or tcp option |
---|
1044 | * @param[in,out] ptr the pointer to the current option |
---|
1045 | * @param[in,out] len the length of the remaining buffer |
---|
1046 | * @param[out] type the type of the option |
---|
1047 | * @param[out] optlen the length of the option |
---|
1048 | * @param[out] data the data of the option |
---|
1049 | * |
---|
1050 | * @returns bool true if there is another option (and the fields are filled in) |
---|
1051 | * or false if this was the last option. |
---|
1052 | * |
---|
1053 | * This updates ptr to point to the next option after this one, and updates |
---|
1054 | * len to be the number of bytes remaining in the options area. Type is updated |
---|
1055 | * to be the code of this option, and data points to the data of this option, |
---|
1056 | * with optlen saying how many bytes there are. |
---|
1057 | * |
---|
1058 | * @note Beware of fragmented packets. |
---|
1059 | * @author Perry Lorier |
---|
1060 | */ |
---|
1061 | int trace_get_next_option(unsigned char **ptr,int *len, |
---|
1062 | unsigned char *type, |
---|
1063 | unsigned char *optlen, |
---|
1064 | unsigned char **data) |
---|
1065 | { |
---|
1066 | if (*len<=0) |
---|
1067 | return 0; |
---|
1068 | *type=**ptr; |
---|
1069 | switch(*type) { |
---|
1070 | case 0: /* End of options */ |
---|
1071 | return 0; |
---|
1072 | case 1: /* Pad */ |
---|
1073 | (*ptr)++; |
---|
1074 | (*len)--; |
---|
1075 | return 1; |
---|
1076 | default: |
---|
1077 | *optlen = *(*ptr+1); |
---|
1078 | if (*optlen<2) |
---|
1079 | return 0; // I have no idea wtf is going on |
---|
1080 | // with these packets |
---|
1081 | (*len)-=*optlen; |
---|
1082 | (*data)=(*ptr+2); |
---|
1083 | (*ptr)+=*optlen; |
---|
1084 | if (*len<0) |
---|
1085 | return 0; |
---|
1086 | return 1; |
---|
1087 | } |
---|
1088 | assert(0); |
---|
1089 | } |
---|
1090 | |
---|
1091 | |
---|
1092 | /** Get the current time in DAG time format |
---|
1093 | * @param packet a pointer to a libtrace_packet structure |
---|
1094 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
1095 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
1096 | * @author Daniel Lawson |
---|
1097 | */ |
---|
1098 | uint64_t trace_get_erf_timestamp(struct libtrace_packet_t *packet) { |
---|
1099 | uint64_t timestamp = 0; |
---|
1100 | dag_record_t *erfptr = 0; |
---|
1101 | struct pcap_pkthdr *pcapptr = 0; |
---|
1102 | struct wag_event_t *wagptr = 0; |
---|
1103 | switch (packet->trace->format) { |
---|
1104 | case DAG: |
---|
1105 | case ERF: |
---|
1106 | case RTCLIENT: |
---|
1107 | erfptr = (dag_record_t *)packet->buffer; |
---|
1108 | timestamp = erfptr->ts; |
---|
1109 | break; |
---|
1110 | #if HAVE_PCAP |
---|
1111 | case PCAPINT: |
---|
1112 | case PCAP: |
---|
1113 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1114 | timestamp = ((((uint64_t)pcapptr->ts.tv_sec) << 32) + \ |
---|
1115 | (pcapptr->ts.tv_usec*UINT_MAX/1000000)); |
---|
1116 | break; |
---|
1117 | #endif |
---|
1118 | case WAGINT: |
---|
1119 | case WAG: |
---|
1120 | wagptr = (struct wag_event_t *)packet->buffer; |
---|
1121 | timestamp = wagptr->timestamp_lo; |
---|
1122 | timestamp |= (uint64_t)wagptr->timestamp_hi<<32; |
---|
1123 | timestamp = ((timestamp%44000000)*(UINT_MAX/44000000)) |
---|
1124 | | ((timestamp/44000000)<<32); |
---|
1125 | break; |
---|
1126 | default: |
---|
1127 | fprintf(stderr,"Unknown format in trace_get_erf_timestamp\n"); |
---|
1128 | timestamp = 0; |
---|
1129 | } |
---|
1130 | return timestamp; |
---|
1131 | } |
---|
1132 | |
---|
1133 | /** Get the current time in struct timeval |
---|
1134 | * @param packet a pointer to a libtrace_packet structure |
---|
1135 | * |
---|
1136 | * @returns time that this packet was seen in a struct timeval |
---|
1137 | * @author Daniel Lawson |
---|
1138 | * @author Perry Lorier |
---|
1139 | */ |
---|
1140 | struct timeval trace_get_timeval(struct libtrace_packet_t *packet) { |
---|
1141 | struct timeval tv; |
---|
1142 | #if HAVE_PCAP |
---|
1143 | struct pcap_pkthdr *pcapptr = 0; |
---|
1144 | #endif |
---|
1145 | uint64_t ts; |
---|
1146 | //uint32_t seconds; |
---|
1147 | switch (packet->trace->format) { |
---|
1148 | #if HAVE_PCAP |
---|
1149 | case PCAPINT: |
---|
1150 | case PCAP: |
---|
1151 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1152 | tv = pcapptr->ts; |
---|
1153 | break; |
---|
1154 | #endif |
---|
1155 | case WAGINT: |
---|
1156 | case WAG: |
---|
1157 | case DAG: |
---|
1158 | case ERF: |
---|
1159 | case RTCLIENT: |
---|
1160 | default: |
---|
1161 | // FIXME: This isn't portable to big-endian machines |
---|
1162 | ts = trace_get_erf_timestamp(packet); |
---|
1163 | tv.tv_sec = ts >> 32; |
---|
1164 | ts = (1000000 * (ts & 0xffffffffULL)); |
---|
1165 | ts += (ts & 0x80000000ULL) << 1; |
---|
1166 | tv.tv_usec = ts >> 32; |
---|
1167 | if (tv.tv_usec >= 1000000) { |
---|
1168 | tv.tv_usec -= 1000000; |
---|
1169 | tv.tv_sec += 1; |
---|
1170 | } |
---|
1171 | break; |
---|
1172 | } |
---|
1173 | return tv; |
---|
1174 | } |
---|
1175 | |
---|
1176 | /** Get the current time in floating point seconds |
---|
1177 | * @param packet a pointer to a libtrace_packet structure |
---|
1178 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
1179 | * @author Perry Lorier |
---|
1180 | */ |
---|
1181 | double trace_get_seconds(struct libtrace_packet_t *packet) { |
---|
1182 | uint64_t ts; |
---|
1183 | ts = trace_get_erf_timestamp(packet); |
---|
1184 | return (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
1185 | } |
---|
1186 | |
---|
1187 | /** Get the size of the packet in the trace |
---|
1188 | * @param packet the packet opaque pointer |
---|
1189 | * @returns the size of the packet in the trace |
---|
1190 | * @author Perry Lorier |
---|
1191 | * @note Due to this being a header capture, or anonymisation, this may not |
---|
1192 | * be the same size as the original packet. See trace_get_wire_length() for the |
---|
1193 | * original size of the packet. |
---|
1194 | * @note This can (and often is) different for different packets in a trace! |
---|
1195 | * @par |
---|
1196 | * This is sometimes called the "snaplen". |
---|
1197 | */ |
---|
1198 | int trace_get_capture_length(struct libtrace_packet_t *packet) { |
---|
1199 | dag_record_t *erfptr = 0; |
---|
1200 | #if HAVE_PCAP |
---|
1201 | struct pcap_pkthdr *pcapptr = 0; |
---|
1202 | #endif |
---|
1203 | struct wag_event_t *wag_event; |
---|
1204 | switch (packet->trace->format) { |
---|
1205 | case DAG: |
---|
1206 | case ERF: |
---|
1207 | case RTCLIENT: |
---|
1208 | erfptr = (dag_record_t *)packet->buffer; |
---|
1209 | return ntohs(erfptr->rlen); |
---|
1210 | #if HAVE_PCAP |
---|
1211 | case PCAPINT: |
---|
1212 | case PCAP: |
---|
1213 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1214 | //return ntohs(pcapptr->caplen); |
---|
1215 | return pcapptr->caplen; |
---|
1216 | #endif |
---|
1217 | case WAGINT: |
---|
1218 | case WAG: |
---|
1219 | wag_event = (struct wag_event_t *)packet->buffer; |
---|
1220 | switch(wag_event->type) { |
---|
1221 | case 0: |
---|
1222 | return wag_event->length*4-( |
---|
1223 | sizeof(struct wag_event_t)+ |
---|
1224 | sizeof(struct wag_data_event_t) |
---|
1225 | ); |
---|
1226 | default: |
---|
1227 | assert(0); |
---|
1228 | } |
---|
1229 | default: |
---|
1230 | assert(0); |
---|
1231 | } |
---|
1232 | return -1; |
---|
1233 | } |
---|
1234 | |
---|
1235 | /** Get the size of the packet as it was seen on the wire. |
---|
1236 | * @param packet a pointer to a libtrace_packet structure |
---|
1237 | * |
---|
1238 | * @returns the size of the packet as it was on the wire. |
---|
1239 | * @author Perry Lorier |
---|
1240 | * @author Daniel Lawson |
---|
1241 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
1242 | * not be the same as the Capture Len. |
---|
1243 | */ |
---|
1244 | int trace_get_wire_length(struct libtrace_packet_t *packet){ |
---|
1245 | dag_record_t *erfptr = 0; |
---|
1246 | #if HAVE_PCAP |
---|
1247 | struct pcap_pkthdr *pcapptr = 0; |
---|
1248 | #endif |
---|
1249 | struct wag_event_t *wag_event = 0; |
---|
1250 | switch (packet->trace->format) { |
---|
1251 | case DAG: |
---|
1252 | case ERF: |
---|
1253 | case RTCLIENT: |
---|
1254 | erfptr = (dag_record_t *)packet->buffer; |
---|
1255 | return ntohs(erfptr->wlen); |
---|
1256 | break; |
---|
1257 | #if HAVE_PCAP |
---|
1258 | case PCAPINT: |
---|
1259 | case PCAP: |
---|
1260 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1261 | return ntohs(pcapptr->len); |
---|
1262 | break; |
---|
1263 | #endif |
---|
1264 | case WAGINT: |
---|
1265 | case WAG: |
---|
1266 | wag_event = (struct wag_event_t *)packet->buffer; |
---|
1267 | switch(wag_event->type) { |
---|
1268 | case 0: |
---|
1269 | return ((struct wag_data_event_t *)(&wag_event->payload))->frame_length; |
---|
1270 | default: |
---|
1271 | assert(0); |
---|
1272 | } |
---|
1273 | } |
---|
1274 | return -1; |
---|
1275 | |
---|
1276 | } |
---|
1277 | |
---|
1278 | /** Get the type of the link layer |
---|
1279 | * @param packet a pointer to a libtrace_packet structure |
---|
1280 | * @returns libtrace_linktype_t |
---|
1281 | * @author Perry Lorier |
---|
1282 | * @author Daniel Lawson |
---|
1283 | */ |
---|
1284 | libtrace_linktype_t trace_get_link_type(struct libtrace_packet_t *packet ) { |
---|
1285 | dag_record_t *erfptr = 0; |
---|
1286 | #if HAVE_PCAP |
---|
1287 | struct pcap_pkthdr *pcapptr = 0; |
---|
1288 | #endif |
---|
1289 | int linktype = 0; |
---|
1290 | switch (packet->trace->format) { |
---|
1291 | case DAG: |
---|
1292 | case ERF: |
---|
1293 | case RTCLIENT: |
---|
1294 | erfptr = (dag_record_t *)packet->buffer; |
---|
1295 | switch (erfptr->type) { |
---|
1296 | case TYPE_ETH: return TRACE_TYPE_ETH; |
---|
1297 | case TYPE_ATM: return TRACE_TYPE_ATM; |
---|
1298 | default: assert(0); |
---|
1299 | } |
---|
1300 | return erfptr->type; |
---|
1301 | |
---|
1302 | break; |
---|
1303 | #if HAVE_PCAP |
---|
1304 | case PCAPINT: |
---|
1305 | case PCAP: |
---|
1306 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
1307 | linktype = pcap_datalink(packet->trace->input.pcap); |
---|
1308 | switch (linktype) { |
---|
1309 | case 1: |
---|
1310 | return TRACE_TYPE_ETH; |
---|
1311 | case 11: |
---|
1312 | return TRACE_TYPE_ATM; |
---|
1313 | case DLT_IEEE802_11: |
---|
1314 | return TRACE_TYPE_80211; |
---|
1315 | } |
---|
1316 | break; |
---|
1317 | #endif |
---|
1318 | case WAGINT: |
---|
1319 | case WAG: |
---|
1320 | return TRACE_TYPE_80211; |
---|
1321 | } |
---|
1322 | return -1; |
---|
1323 | } |
---|
1324 | |
---|
1325 | /** Get the source MAC addres |
---|
1326 | * @param packet a pointer to a libtrace_packet structure |
---|
1327 | * @returns a pointer to the source mac, (or NULL if there is no source MAC) |
---|
1328 | * @author Perry Lorier |
---|
1329 | */ |
---|
1330 | uint8_t *trace_get_source_mac(struct libtrace_packet_t *packet) { |
---|
1331 | void *link = trace_get_link(packet); |
---|
1332 | struct ieee_802_11_header *wifi = link; |
---|
1333 | struct ether_header *ethptr = link; |
---|
1334 | if (!link) |
---|
1335 | return NULL; |
---|
1336 | switch (trace_get_link_type(packet)) { |
---|
1337 | case TRACE_TYPE_80211: |
---|
1338 | return (uint8_t*)&wifi->mac2; |
---|
1339 | case TRACE_TYPE_ETH: |
---|
1340 | return (uint8_t*)ðptr->ether_shost; |
---|
1341 | default: |
---|
1342 | fprintf(stderr,"Not implemented\n"); |
---|
1343 | assert(0); |
---|
1344 | } |
---|
1345 | } |
---|
1346 | |
---|
1347 | /** Get the destination MAC addres |
---|
1348 | * @param packet a libtrace_packet pointer |
---|
1349 | * @returns a pointer to the destination mac, (or NULL if there is no |
---|
1350 | * destination MAC) |
---|
1351 | * @author Perry Lorier |
---|
1352 | */ |
---|
1353 | uint8_t *trace_get_destination_mac(struct libtrace_packet_t *packet) { |
---|
1354 | void *link = trace_get_link(packet); |
---|
1355 | struct ieee_802_11_header *wifi = link; |
---|
1356 | struct ether_header *ethptr = link; |
---|
1357 | if (!link) |
---|
1358 | return NULL; |
---|
1359 | switch (trace_get_link_type(packet)) { |
---|
1360 | case TRACE_TYPE_80211: |
---|
1361 | return (uint8_t*)&wifi->mac1; |
---|
1362 | case TRACE_TYPE_ETH: |
---|
1363 | return (uint8_t*)ðptr->ether_dhost; |
---|
1364 | default: |
---|
1365 | fprintf(stderr,"Not implemented\n"); |
---|
1366 | assert(0); |
---|
1367 | } |
---|
1368 | } |
---|
1369 | |
---|
1370 | |
---|
1371 | /** process a libtrace event |
---|
1372 | * @param trace the libtrace opaque pointer |
---|
1373 | * @param packet the libtrace_packet opaque pointer |
---|
1374 | * @returns |
---|
1375 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
---|
1376 | * TRACE_EVENT_SLEEP Next event in seconds |
---|
1377 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
---|
1378 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
1379 | * which in turn is stored inside the new packet object... |
---|
1380 | * @author Perry Lorier |
---|
1381 | */ |
---|
1382 | struct libtrace_eventobj_t trace_event(struct libtrace_t *trace, |
---|
1383 | struct libtrace_packet_t *packet) { |
---|
1384 | struct libtrace_eventobj_t event; |
---|
1385 | |
---|
1386 | if (!trace) { |
---|
1387 | fprintf(stderr,"You called trace_event() with a NULL trace object!\n"); |
---|
1388 | } |
---|
1389 | assert(trace); |
---|
1390 | assert(packet); |
---|
1391 | |
---|
1392 | /* Store the trace we are reading from into the packet opaque |
---|
1393 | * structure */ |
---|
1394 | packet->trace = trace; |
---|
1395 | |
---|
1396 | /* Is there a packet ready? */ |
---|
1397 | switch (trace->sourcetype) { |
---|
1398 | #if HAVE_PCAP |
---|
1399 | case INTERFACE: |
---|
1400 | { |
---|
1401 | int data; |
---|
1402 | event.fd = pcap_fileno(trace->input.pcap); |
---|
1403 | if(ioctl(event.fd,FIONREAD,&data)==-1){ |
---|
1404 | perror("ioctl(FIONREAD)"); |
---|
1405 | } |
---|
1406 | if (data>0) { |
---|
1407 | trace_read_packet(trace,packet); |
---|
1408 | event.type = TRACE_EVENT_PACKET; |
---|
1409 | return event; |
---|
1410 | } |
---|
1411 | event.type = TRACE_EVENT_IOWAIT; |
---|
1412 | return event; |
---|
1413 | } |
---|
1414 | #endif |
---|
1415 | case SOCKET: |
---|
1416 | case DEVICE: |
---|
1417 | case RT: |
---|
1418 | { |
---|
1419 | int data; |
---|
1420 | event.fd = trace->input.fd; |
---|
1421 | if(ioctl(event.fd,FIONREAD,&data)==-1){ |
---|
1422 | perror("ioctl(FIONREAD)"); |
---|
1423 | } |
---|
1424 | if (data>0) { |
---|
1425 | trace_read_packet(trace,packet); |
---|
1426 | event.type = TRACE_EVENT_PACKET; |
---|
1427 | return event; |
---|
1428 | } |
---|
1429 | event.type = TRACE_EVENT_IOWAIT; |
---|
1430 | return event; |
---|
1431 | } |
---|
1432 | case STDIN: |
---|
1433 | case TRACE: |
---|
1434 | { |
---|
1435 | double ts; |
---|
1436 | /* "Prime" the pump */ |
---|
1437 | if (!trace->packet.buffer) { |
---|
1438 | trace->packet.buffer = malloc(4096); |
---|
1439 | trace->packet.size= |
---|
1440 | trace_read_packet(trace,packet); |
---|
1441 | } |
---|
1442 | ts=trace_get_seconds(packet); |
---|
1443 | if (trace->last_ts!=0) { |
---|
1444 | event.seconds = ts - trace->last_ts; |
---|
1445 | if (event.seconds>time(NULL)-trace->start_ts) { |
---|
1446 | event.type = TRACE_EVENT_SLEEP; |
---|
1447 | return event; |
---|
1448 | } |
---|
1449 | |
---|
1450 | } |
---|
1451 | else { |
---|
1452 | trace->start_ts = time(NULL); |
---|
1453 | trace->last_ts = ts; |
---|
1454 | } |
---|
1455 | |
---|
1456 | packet->size = trace->packet.size; |
---|
1457 | memcpy(packet->buffer,trace->packet.buffer,trace->packet.size); |
---|
1458 | |
---|
1459 | free(trace->packet.buffer); |
---|
1460 | trace->packet.buffer = 0; |
---|
1461 | event.type = TRACE_EVENT_PACKET; |
---|
1462 | return event; |
---|
1463 | } |
---|
1464 | default: |
---|
1465 | assert(0); |
---|
1466 | } |
---|
1467 | assert(0); |
---|
1468 | } |
---|
1469 | |
---|
1470 | /** setup a BPF filter |
---|
1471 | * @param filterstring a char * containing the bpf filter string |
---|
1472 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
1473 | * @author Daniel Lawson |
---|
1474 | */ |
---|
1475 | struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring) { |
---|
1476 | #if HAVE_BPF |
---|
1477 | struct libtrace_filter_t *filter = malloc(sizeof(struct libtrace_filter_t)); |
---|
1478 | filter->filterstring = strdup(filterstring); |
---|
1479 | filter->filter = 0; |
---|
1480 | return filter; |
---|
1481 | #else |
---|
1482 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1483 | return 0; |
---|
1484 | #endif |
---|
1485 | } |
---|
1486 | |
---|
1487 | /** apply a BPF filter |
---|
1488 | * @param filter the filter opaque pointer |
---|
1489 | * @param packet the packet opaque pointer |
---|
1490 | * @returns 0 if the filter fails, 1 if it succeeds |
---|
1491 | * @author Daniel Lawson |
---|
1492 | */ |
---|
1493 | int trace_bpf_filter(struct libtrace_filter_t *filter, |
---|
1494 | struct libtrace_packet_t *packet) { |
---|
1495 | #if HAVE_BPF |
---|
1496 | void *linkptr = 0; |
---|
1497 | int clen = 0; |
---|
1498 | assert(filter); |
---|
1499 | assert(packet); |
---|
1500 | linkptr = trace_get_link(packet); |
---|
1501 | assert(linkptr); |
---|
1502 | clen = trace_get_capture_length(packet); |
---|
1503 | |
---|
1504 | |
---|
1505 | if (filter->filterstring && ! filter->filter) { |
---|
1506 | pcap_t *pcap; |
---|
1507 | struct bpf_program bpfprog; |
---|
1508 | |
---|
1509 | switch (trace_get_link_type(packet)) { |
---|
1510 | case TRACE_TYPE_ETH: |
---|
1511 | pcap = pcap_open_dead(DLT_EN10MB, 1500); |
---|
1512 | break; |
---|
1513 | default: |
---|
1514 | printf("only works for ETH at the moment\n"); |
---|
1515 | assert(0); |
---|
1516 | } |
---|
1517 | |
---|
1518 | // build filter |
---|
1519 | if (pcap_compile( pcap, &bpfprog, filter->filterstring, 1, 0)) { |
---|
1520 | printf("bpf compilation error: %s\n", |
---|
1521 | pcap_geterr(pcap)); |
---|
1522 | assert(0); |
---|
1523 | } |
---|
1524 | pcap_close(pcap); |
---|
1525 | filter->filter = bpfprog.bf_insns; |
---|
1526 | } |
---|
1527 | |
---|
1528 | assert(filter->filter); |
---|
1529 | return bpf_filter(filter->filter, linkptr, clen, clen); |
---|
1530 | #else |
---|
1531 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1532 | return 0; |
---|
1533 | #endif |
---|
1534 | } |
---|
1535 | |
---|
1536 | /** Set the direction flag, if it has one |
---|
1537 | * @param packet the packet opaque pointer |
---|
1538 | * @param direction the new direction (0,1,2,3) |
---|
1539 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1540 | * @author Daniel Lawson |
---|
1541 | */ |
---|
1542 | int8_t trace_set_direction(struct libtrace_packet_t *packet, int8_t direction) { |
---|
1543 | |
---|
1544 | dag_record_t *erfptr = 0; |
---|
1545 | assert(packet); |
---|
1546 | |
---|
1547 | switch(packet->trace->format) { |
---|
1548 | case DAG: |
---|
1549 | case ERF: |
---|
1550 | case RTCLIENT: |
---|
1551 | erfptr = (dag_record_t *)packet->buffer; |
---|
1552 | erfptr->flags.iface = direction; |
---|
1553 | break; |
---|
1554 | default: |
---|
1555 | direction = -1; |
---|
1556 | } |
---|
1557 | |
---|
1558 | return direction; |
---|
1559 | |
---|
1560 | |
---|
1561 | } |
---|
1562 | |
---|
1563 | /** Get the direction flag, if it has one |
---|
1564 | * @param packet a pointer to a libtrace_packet structure |
---|
1565 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1566 | * @author Daniel Lawson |
---|
1567 | */ |
---|
1568 | int8_t trace_get_direction(struct libtrace_packet_t *packet) { |
---|
1569 | |
---|
1570 | int8_t direction; |
---|
1571 | dag_record_t *erfptr = 0; |
---|
1572 | assert(packet); |
---|
1573 | |
---|
1574 | switch(packet->trace->format) { |
---|
1575 | case DAG: |
---|
1576 | case ERF: |
---|
1577 | case RTCLIENT: |
---|
1578 | erfptr = (dag_record_t *)packet->buffer; |
---|
1579 | direction = erfptr->flags.iface; |
---|
1580 | break; |
---|
1581 | default: |
---|
1582 | direction = -1; |
---|
1583 | } |
---|
1584 | |
---|
1585 | return direction; |
---|
1586 | |
---|
1587 | |
---|
1588 | } |
---|
1589 | |
---|
1590 | #define ROOT_SERVER(x) ((x) < 512) |
---|
1591 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
---|
1592 | #define NONROOT_SERVER(x) ((x) >= 5000) |
---|
1593 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
---|
1594 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
---|
1595 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
---|
1596 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
---|
1597 | |
---|
1598 | |
---|
1599 | /** Attempt to deduce the 'server' port |
---|
1600 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
---|
1601 | * @param source the TCP or UDP source port |
---|
1602 | * @param dest the TCP or UDP destination port |
---|
1603 | * @returns a hint as to which port is the server port |
---|
1604 | * @author Daniel Lawson |
---|
1605 | */ |
---|
1606 | int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest) { |
---|
1607 | /* |
---|
1608 | * * If the ports are equal, return DEST |
---|
1609 | * * Check for well-known ports in the given protocol |
---|
1610 | * * Root server ports: 0 - 511 |
---|
1611 | * * Root client ports: 512 - 1023 |
---|
1612 | * * non-root client ports: 1024 - 4999 |
---|
1613 | * * non-root server ports: 5000+ |
---|
1614 | * * Check for static ranges: 1024 - 49151 |
---|
1615 | * * Check for dynamic ranges: 49152 - 65535 |
---|
1616 | * * flip a coin. |
---|
1617 | */ |
---|
1618 | |
---|
1619 | uint16_t server, client; |
---|
1620 | |
---|
1621 | /* equal */ |
---|
1622 | if (source == client) |
---|
1623 | return USE_DEST; |
---|
1624 | |
---|
1625 | /* root server port, 0 - 511 */ |
---|
1626 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
---|
1627 | if (source < dest) |
---|
1628 | return USE_SOURCE; |
---|
1629 | return USE_DEST; |
---|
1630 | } |
---|
1631 | |
---|
1632 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1633 | return USE_SOURCE; |
---|
1634 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
---|
1635 | return USE_DEST; |
---|
1636 | |
---|
1637 | /* non-root server */ |
---|
1638 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
---|
1639 | if (source < dest) |
---|
1640 | return USE_SOURCE; |
---|
1641 | return USE_DEST; |
---|
1642 | } |
---|
1643 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
---|
1644 | return USE_SOURCE; |
---|
1645 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
---|
1646 | return USE_DEST; |
---|
1647 | |
---|
1648 | /* root client */ |
---|
1649 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1650 | if (source < dest) |
---|
1651 | return USE_SOURCE; |
---|
1652 | return USE_DEST; |
---|
1653 | } |
---|
1654 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
---|
1655 | /* prefer root-client over nonroot-client */ |
---|
1656 | if (NONROOT_CLIENT(dest)) |
---|
1657 | return USE_SOURCE; |
---|
1658 | return USE_DEST; |
---|
1659 | } |
---|
1660 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1661 | /* prefer root-client over nonroot-client */ |
---|
1662 | if (NONROOT_CLIENT(source)) |
---|
1663 | return USE_DEST; |
---|
1664 | return USE_SOURCE; |
---|
1665 | } |
---|
1666 | |
---|
1667 | /* nonroot client */ |
---|
1668 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
---|
1669 | if (source < dest) |
---|
1670 | return USE_SOURCE; |
---|
1671 | return USE_DEST; |
---|
1672 | } |
---|
1673 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
---|
1674 | return USE_DEST; |
---|
1675 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
---|
1676 | return USE_SOURCE; |
---|
1677 | |
---|
1678 | /* dynamic range */ |
---|
1679 | if (DYNAMIC(source) && DYNAMIC(dest)) |
---|
1680 | if (source < dest) |
---|
1681 | return USE_SOURCE; |
---|
1682 | return USE_DEST; |
---|
1683 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
---|
1684 | return USE_DEST; |
---|
1685 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
---|
1686 | return USE_SOURCE; |
---|
1687 | /* |
---|
1688 | if (SERVER(source) && CLIENT(dest)) |
---|
1689 | return USE_SOURCE; |
---|
1690 | |
---|
1691 | if (SERVER(dest) && CLIENT(source)) |
---|
1692 | return USE_DEST; |
---|
1693 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1694 | return USE_SOURCE; |
---|
1695 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
---|
1696 | return USE_DEST; |
---|
1697 | */ |
---|
1698 | // failing that test... |
---|
1699 | if (source < dest) { |
---|
1700 | return USE_SOURCE; |
---|
1701 | } |
---|
1702 | return USE_DEST; |
---|
1703 | |
---|
1704 | } |
---|
1705 | |
---|
1706 | /** Truncate the packet at the suggested length |
---|
1707 | * @param packet the packet opaque pointer |
---|
1708 | * @param size the new length of the packet |
---|
1709 | * @returns the new length of the packet, or the original length of the |
---|
1710 | * packet if unchanged |
---|
1711 | * NOTE: len refers to the network-level payload of the packet, and not |
---|
1712 | * any capture headers included as well. For example, to truncate a packet |
---|
1713 | * after the IP header, set scan to sizeof(ethernet_header) + sizeof(ip_header) |
---|
1714 | * @author Daniel Lawson |
---|
1715 | */ |
---|
1716 | size_t trace_truncate_packet(struct libtrace_packet_t *packet, size_t size) { |
---|
1717 | dag_record_t *erfptr; |
---|
1718 | #if HAVE_PCAP |
---|
1719 | struct pcap_pkthdr *pcaphdr; |
---|
1720 | #endif |
---|
1721 | |
---|
1722 | assert(packet); |
---|
1723 | |
---|
1724 | if (size > packet->size) { |
---|
1725 | // can't make a packet larger |
---|
1726 | return packet->size; |
---|
1727 | } |
---|
1728 | switch (packet->trace->format) { |
---|
1729 | #if HAVE_PCAP |
---|
1730 | case PCAPINT: |
---|
1731 | case PCAP: |
---|
1732 | pcaphdr = (struct pcap_pkthdr *)packet->buffer; |
---|
1733 | pcaphdr->caplen = size + sizeof(struct pcap_pkthdr); |
---|
1734 | packet->size = pcaphdr->caplen; |
---|
1735 | break; |
---|
1736 | #endif |
---|
1737 | case ERF: |
---|
1738 | case DAG: |
---|
1739 | case RTCLIENT: |
---|
1740 | erfptr = (dag_record_t *)packet->buffer; |
---|
1741 | erfptr->rlen = ntohs(size + sizeof(dag_record_t)); |
---|
1742 | packet->size = size + sizeof(dag_record_t); |
---|
1743 | break; |
---|
1744 | case WAGINT: |
---|
1745 | case WAG: |
---|
1746 | // don't know how to do this? |
---|
1747 | break; |
---|
1748 | } |
---|
1749 | return packet->size; |
---|
1750 | } |
---|
1751 | |
---|