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