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 | /** @file |
---|
32 | * |
---|
33 | * @brief Trace file processing library |
---|
34 | * |
---|
35 | * @author Daniel Lawson |
---|
36 | * @author Perry Lorier |
---|
37 | * |
---|
38 | */ |
---|
39 | #define _GNU_SOURCE |
---|
40 | #include "common.h" |
---|
41 | #include "config.h" |
---|
42 | #include <assert.h> |
---|
43 | #include <errno.h> |
---|
44 | #include <fcntl.h> |
---|
45 | #include <netdb.h> |
---|
46 | #include <pcap.h> |
---|
47 | #include <stdio.h> |
---|
48 | #include <stdlib.h> |
---|
49 | #include <string.h> |
---|
50 | #include <sys/stat.h> |
---|
51 | #include <sys/types.h> |
---|
52 | #ifdef HAVE_SYS_LIMITS_H |
---|
53 | // For BSD, apparently |
---|
54 | # include <sys/limits.h> |
---|
55 | #endif |
---|
56 | #include <sys/socket.h> |
---|
57 | #include <sys/un.h> |
---|
58 | #include <unistd.h> |
---|
59 | #include <net/ethernet.h> |
---|
60 | #include <time.h> |
---|
61 | #include <sys/ioctl.h> |
---|
62 | |
---|
63 | #include "libtrace.h" |
---|
64 | #include "fifo.h" |
---|
65 | |
---|
66 | #ifdef HAVE_PCAP_BPF_H |
---|
67 | # include <pcap-bpf.h> |
---|
68 | #else |
---|
69 | # ifdef HAVE_NET_BPF_H |
---|
70 | # include <net/bpf.h> |
---|
71 | # endif |
---|
72 | #endif |
---|
73 | |
---|
74 | #include <pcap.h> |
---|
75 | |
---|
76 | #include "dagformat.h" |
---|
77 | |
---|
78 | #include "wag.h" |
---|
79 | |
---|
80 | #include <zlib.h> |
---|
81 | |
---|
82 | |
---|
83 | typedef enum {SOCKET, TRACE, STDIN, DEVICE, INTERFACE, RT } source_t; |
---|
84 | |
---|
85 | typedef enum {ERF, PCAP, PCAPINT, DAG, RTCLIENT, WAG, WAGINT } format_t; |
---|
86 | |
---|
87 | struct libtrace_filter_t { |
---|
88 | struct bpf_insn *filter; |
---|
89 | char * filterstring; |
---|
90 | }; |
---|
91 | |
---|
92 | struct libtrace_t { |
---|
93 | format_t format; |
---|
94 | source_t sourcetype; |
---|
95 | union { |
---|
96 | struct { |
---|
97 | char *hostname; |
---|
98 | short port; |
---|
99 | } rt; |
---|
100 | char *path; |
---|
101 | char *interface; |
---|
102 | } conn_info; |
---|
103 | union { |
---|
104 | int fd; |
---|
105 | gzFile *file; |
---|
106 | pcap_t *pcap; |
---|
107 | } input; |
---|
108 | struct fifo_t *fifo; |
---|
109 | struct { |
---|
110 | void *buffer; |
---|
111 | int size; |
---|
112 | } packet; |
---|
113 | double last_ts; |
---|
114 | double start_ts; |
---|
115 | }; |
---|
116 | |
---|
117 | #define URI_PROTO_LINE 16 |
---|
118 | static int init_trace(struct libtrace_t **libtrace, char *uri) { |
---|
119 | char *scan = calloc(sizeof(char),URI_PROTO_LINE); |
---|
120 | char *uridata = 0; |
---|
121 | |
---|
122 | // parse the URI to determine what sort of event we are dealing with |
---|
123 | |
---|
124 | // want snippet before the : to get the uri base type. |
---|
125 | |
---|
126 | if((uridata = strchr(uri,':')) == NULL) { |
---|
127 | // badly formed URI - needs a : |
---|
128 | return 0; |
---|
129 | } |
---|
130 | |
---|
131 | if ((*uridata - *uri) > URI_PROTO_LINE) { |
---|
132 | // badly formed URI - uri type is too long |
---|
133 | return 0; |
---|
134 | } |
---|
135 | strncpy(scan,uri, (uridata - uri)); |
---|
136 | |
---|
137 | if (!strncasecmp(scan,"erf",3)) { |
---|
138 | (*libtrace)->format=ERF; |
---|
139 | } else if (!strncasecmp(scan,"pcapint",7)) { |
---|
140 | (*libtrace)->format=PCAPINT; |
---|
141 | } else if (!strncasecmp(scan,"pcap",4)) { |
---|
142 | (*libtrace)->format=PCAP; |
---|
143 | } else if (!strncasecmp(scan,"dag",3)) { |
---|
144 | (*libtrace)->format=DAG; |
---|
145 | } else if (!strncasecmp(scan,"rtclient",7)) { |
---|
146 | (*libtrace)->format=RTCLIENT; |
---|
147 | } else if (!strncasecmp(scan,"wagint",6)) { |
---|
148 | (*libtrace)->format=WAGINT; |
---|
149 | } else if (!strncasecmp(scan,"wag",3)) { |
---|
150 | (*libtrace)->format=WAG; |
---|
151 | } else { |
---|
152 | //badly formed URI |
---|
153 | return 0; |
---|
154 | } |
---|
155 | |
---|
156 | // push uridata past the delimiter |
---|
157 | uridata++; |
---|
158 | |
---|
159 | // libtrace->format now contains the type of uri |
---|
160 | // libtrace->uridata contains the appropriate data for this |
---|
161 | |
---|
162 | switch((*libtrace)->format) { |
---|
163 | case PCAPINT: |
---|
164 | case WAGINT: |
---|
165 | /* Can have uridata of the following format |
---|
166 | * eth0 |
---|
167 | * etc |
---|
168 | */ |
---|
169 | // We basically assume this is correct. |
---|
170 | (*libtrace)->sourcetype = INTERFACE; |
---|
171 | (*libtrace)->conn_info.path = strdup(uridata); |
---|
172 | break; |
---|
173 | case PCAP: |
---|
174 | case ERF: |
---|
175 | case WAG: |
---|
176 | case DAG: |
---|
177 | /* |
---|
178 | * Can have uridata of the following format |
---|
179 | * /path/to/socket |
---|
180 | * /path/to/file |
---|
181 | * /path/to/file.gz |
---|
182 | * /dev/device |
---|
183 | * - |
---|
184 | */ |
---|
185 | if (!strncmp(uridata,"-",1)) { |
---|
186 | (*libtrace)->sourcetype = STDIN; |
---|
187 | } else { |
---|
188 | struct stat buf; |
---|
189 | if (stat(uridata,&buf) == -1) { |
---|
190 | perror("stat"); |
---|
191 | return 0; |
---|
192 | } |
---|
193 | if (S_ISSOCK(buf.st_mode)) { |
---|
194 | (*libtrace)->sourcetype = SOCKET; |
---|
195 | } else if (S_ISCHR(buf.st_mode)) { |
---|
196 | (*libtrace)->sourcetype = DEVICE; |
---|
197 | } else { |
---|
198 | (*libtrace)->sourcetype = TRACE; |
---|
199 | } |
---|
200 | (*libtrace)->conn_info.path = strdup(uridata); |
---|
201 | } |
---|
202 | break; |
---|
203 | |
---|
204 | case RTCLIENT: |
---|
205 | /* |
---|
206 | * Can have the uridata in the format |
---|
207 | * hostname |
---|
208 | * hostname:port |
---|
209 | */ |
---|
210 | (*libtrace)->sourcetype = RT; |
---|
211 | if (strlen(uridata) == 0) { |
---|
212 | (*libtrace)->conn_info.rt.hostname = |
---|
213 | strdup("localhost"); |
---|
214 | (*libtrace)->conn_info.rt.port = |
---|
215 | COLLECTOR_PORT; |
---|
216 | break; |
---|
217 | } |
---|
218 | if ((scan = strchr(uridata,':')) == NULL) { |
---|
219 | (*libtrace)->conn_info.rt.hostname = |
---|
220 | strdup(uridata); |
---|
221 | (*libtrace)->conn_info.rt.port = |
---|
222 | COLLECTOR_PORT; |
---|
223 | } else { |
---|
224 | (*libtrace)->conn_info.rt.hostname = |
---|
225 | strndup(uridata,(scan - uridata)); |
---|
226 | |
---|
227 | (*libtrace)->conn_info.rt.port = |
---|
228 | atoi(++scan); |
---|
229 | } |
---|
230 | break; |
---|
231 | } |
---|
232 | |
---|
233 | |
---|
234 | (*libtrace)->fifo = create_fifo(1048576); |
---|
235 | (*libtrace)->packet.buffer = 0; |
---|
236 | (*libtrace)->packet.size = 0; |
---|
237 | |
---|
238 | return 1; |
---|
239 | } |
---|
240 | |
---|
241 | /** Create a trace file from a URI |
---|
242 | * |
---|
243 | * @returns opaque pointer to a libtrace_t |
---|
244 | * |
---|
245 | * Valid URI's are: |
---|
246 | * erf:/path/to/erf/file |
---|
247 | * erf:/path/to/erf/file.gz |
---|
248 | * erf:/path/to/rtclient/socket |
---|
249 | * erf:- (stdin) |
---|
250 | * pcap:pcapinterface (eg: pcap:eth0) |
---|
251 | * pcap:/path/to/pcap/file |
---|
252 | * pcap:/path/to/pcap/file.gz |
---|
253 | * pcap:- |
---|
254 | * rtclient:hostname |
---|
255 | * rtclient:hostname:port |
---|
256 | * wag:- |
---|
257 | * wag:/path/to/wag/file |
---|
258 | * wag:/path/to/wag/file.gz |
---|
259 | * wag:/path/to/wag/socket |
---|
260 | * wag:/dev/device |
---|
261 | * |
---|
262 | * URIs which have yet to be implemented are: |
---|
263 | * dag:/dev/dagcard |
---|
264 | * pcap:/path/to/pcap/socket |
---|
265 | * |
---|
266 | * If an error occured when attempting to open a trace, NULL is returned |
---|
267 | * and an error is output to stdout. |
---|
268 | */ |
---|
269 | struct libtrace_t *trace_create(char *uri) { |
---|
270 | struct libtrace_t *libtrace = malloc(sizeof(struct libtrace_t)); |
---|
271 | struct hostent *he; |
---|
272 | struct sockaddr_in remote; |
---|
273 | struct sockaddr_un unix_sock; |
---|
274 | char errbuf[PCAP_ERRBUF_SIZE]; |
---|
275 | |
---|
276 | if(init_trace(&libtrace,uri) == 0) { |
---|
277 | return 0; |
---|
278 | } |
---|
279 | |
---|
280 | switch(libtrace->sourcetype) { |
---|
281 | case RT: |
---|
282 | if ((he=gethostbyname(libtrace->conn_info.rt.hostname)) == NULL) { |
---|
283 | perror("gethostbyname"); |
---|
284 | return 0; |
---|
285 | } |
---|
286 | if ((libtrace->input.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
---|
287 | perror("socket"); |
---|
288 | return 0; |
---|
289 | } |
---|
290 | |
---|
291 | remote.sin_family = AF_INET; |
---|
292 | remote.sin_port = htons(libtrace->conn_info.rt.port); |
---|
293 | remote.sin_addr = *((struct in_addr *)he->h_addr); |
---|
294 | bzero(&(remote.sin_zero), 8); |
---|
295 | |
---|
296 | if (connect(libtrace->input.fd, (struct sockaddr *)&remote, |
---|
297 | sizeof(struct sockaddr)) == -1) { |
---|
298 | perror("connect (inet)"); |
---|
299 | return 0; |
---|
300 | } |
---|
301 | break; |
---|
302 | case TRACE: |
---|
303 | if (libtrace->format == PCAP) { |
---|
304 | libtrace->input.pcap = pcap_open_offline(libtrace->conn_info.path, errbuf); |
---|
305 | //TODO Check that the open succeeded |
---|
306 | } else { |
---|
307 | libtrace->input.file = gzopen(libtrace->conn_info.path, "r"); |
---|
308 | } |
---|
309 | break; |
---|
310 | case STDIN: |
---|
311 | if (libtrace->format == PCAP) { |
---|
312 | libtrace->input.pcap = pcap_open_offline("-",errbuf); |
---|
313 | } else { |
---|
314 | libtrace->input.file = gzdopen(STDIN, "r"); |
---|
315 | } |
---|
316 | break; |
---|
317 | case SOCKET: |
---|
318 | /* Pcap doesn't work */ |
---|
319 | if (libtrace->format != PCAP) { |
---|
320 | if ((libtrace->input.fd = socket(AF_UNIX, SOCK_STREAM, 0)) == -1) { |
---|
321 | perror("socket"); |
---|
322 | return 0; |
---|
323 | } |
---|
324 | unix_sock.sun_family = AF_UNIX; |
---|
325 | bzero(unix_sock.sun_path,108); |
---|
326 | snprintf(unix_sock.sun_path,108,"%s",libtrace->conn_info.path); |
---|
327 | |
---|
328 | if (connect(libtrace->input.fd, (struct sockaddr *)&unix_sock, |
---|
329 | sizeof(struct sockaddr)) == -1) { |
---|
330 | perror("connect (unix)"); |
---|
331 | return 0; |
---|
332 | } |
---|
333 | } |
---|
334 | break; |
---|
335 | case DEVICE: |
---|
336 | case INTERFACE: |
---|
337 | switch (libtrace->format) { |
---|
338 | case PCAPINT: |
---|
339 | case PCAP: |
---|
340 | libtrace->input.pcap = pcap_open_live( |
---|
341 | libtrace->conn_info.path, |
---|
342 | 4096, |
---|
343 | 1, |
---|
344 | 0, |
---|
345 | errbuf); |
---|
346 | break; |
---|
347 | default: |
---|
348 | fprintf(stderr,"Unknown format trace, hoping I can just read\n"); |
---|
349 | case WAGINT: |
---|
350 | case WAG: |
---|
351 | libtrace->input.fd = open( |
---|
352 | libtrace->conn_info.path, |
---|
353 | O_RDONLY); |
---|
354 | break; |
---|
355 | } |
---|
356 | break; |
---|
357 | default: |
---|
358 | fprintf(stderr,"Unsupported source type for libtrace, terminating (%i)\n",libtrace->sourcetype); |
---|
359 | exit(0); |
---|
360 | |
---|
361 | } |
---|
362 | return libtrace; |
---|
363 | } |
---|
364 | |
---|
365 | /** Close a trace file, freeing up any resources it may have been using |
---|
366 | * |
---|
367 | */ |
---|
368 | void trace_destroy(struct libtrace_t *libtrace) { |
---|
369 | assert(libtrace); |
---|
370 | if (libtrace->format == PCAP || libtrace->format == PCAPINT) { |
---|
371 | pcap_close(libtrace->input.pcap); |
---|
372 | } else if (libtrace->sourcetype == SOCKET || libtrace->sourcetype == RT) { |
---|
373 | close(libtrace->input.fd); |
---|
374 | } else { |
---|
375 | gzclose(libtrace->input.file); |
---|
376 | } |
---|
377 | // need to free things! |
---|
378 | destroy_fifo(libtrace->fifo); |
---|
379 | free(libtrace); |
---|
380 | } |
---|
381 | |
---|
382 | static int trace_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
383 | int numbytes; |
---|
384 | assert(libtrace); |
---|
385 | assert(len >= 0); |
---|
386 | |
---|
387 | if (buffer == 0) |
---|
388 | buffer = malloc(len); |
---|
389 | |
---|
390 | switch(libtrace->sourcetype) { |
---|
391 | case SOCKET: |
---|
392 | case RT: |
---|
393 | // read from the network |
---|
394 | if ((numbytes=recv(libtrace->input.fd, |
---|
395 | buffer, |
---|
396 | len, |
---|
397 | 0)) == -1) { |
---|
398 | perror("recv"); |
---|
399 | return -1; |
---|
400 | } |
---|
401 | break; |
---|
402 | case DEVICE: |
---|
403 | if ((numbytes=read(libtrace->input.fd, |
---|
404 | buffer, |
---|
405 | len)) == -1) { |
---|
406 | perror("read"); |
---|
407 | return -1; |
---|
408 | } |
---|
409 | break; |
---|
410 | default: |
---|
411 | if ((numbytes=gzread(libtrace->input.file, |
---|
412 | buffer, |
---|
413 | len)) == -1) { |
---|
414 | perror("gzread"); |
---|
415 | return -1; |
---|
416 | } |
---|
417 | } |
---|
418 | return numbytes; |
---|
419 | |
---|
420 | } |
---|
421 | |
---|
422 | /** Read one packet from the trace into buffer |
---|
423 | * |
---|
424 | * @param libtrace the libtrace opaque pointer |
---|
425 | * @param packet the packet opaque pointer |
---|
426 | * @returns false if it failed to read a packet |
---|
427 | * |
---|
428 | */ |
---|
429 | int trace_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
430 | int numbytes; |
---|
431 | int size; |
---|
432 | char buf[4096]; |
---|
433 | struct pcap_pkthdr pcaphdr; |
---|
434 | const u_char *pcappkt; |
---|
435 | int read_required = 0; |
---|
436 | |
---|
437 | void *buffer = 0; |
---|
438 | if (!libtrace) { |
---|
439 | fprintf(stderr,"Oi! You called trace_read_packet() with a NULL libtrace parameter!\n"); |
---|
440 | } |
---|
441 | assert(libtrace); |
---|
442 | assert(packet); |
---|
443 | |
---|
444 | //bzero(buffer,len); |
---|
445 | |
---|
446 | /* Store the trace we are reading from into the packet opaque |
---|
447 | * structure */ |
---|
448 | packet->trace = libtrace; |
---|
449 | |
---|
450 | buffer = packet->buffer; |
---|
451 | /* PCAP gives us it's own per-packet interface. Let's use it */ |
---|
452 | if (libtrace->format == PCAP || libtrace->format == PCAPINT) { |
---|
453 | if ((pcappkt = pcap_next(libtrace->input.pcap, &pcaphdr)) == NULL) { |
---|
454 | return -1; |
---|
455 | } |
---|
456 | memcpy(buffer,&pcaphdr,sizeof(struct pcap_pkthdr)); |
---|
457 | memcpy(buffer + sizeof(struct pcap_pkthdr),pcappkt,pcaphdr.len); |
---|
458 | numbytes = pcaphdr.len; |
---|
459 | |
---|
460 | packet->size = numbytes; |
---|
461 | return numbytes; |
---|
462 | } |
---|
463 | |
---|
464 | /* If we're reading from an ERF input, it's an offline trace. We can make some assumptions */ |
---|
465 | |
---|
466 | if (libtrace->format == ERF) { |
---|
467 | void *buffer2 = buffer; |
---|
468 | // read in the trace header |
---|
469 | if ((numbytes=gzread(libtrace->input.file, |
---|
470 | buffer, |
---|
471 | sizeof(dag_record_t))) == -1) { |
---|
472 | perror("gzread"); |
---|
473 | return -1; |
---|
474 | } |
---|
475 | if (numbytes == 0) { |
---|
476 | return 0; |
---|
477 | } |
---|
478 | size = ntohs(((dag_record_t *)buffer)->rlen) - sizeof(dag_record_t); |
---|
479 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
480 | buffer2 += sizeof(dag_record_t); |
---|
481 | |
---|
482 | // read in the rest of the packet |
---|
483 | if ((numbytes=gzread(libtrace->input.file, |
---|
484 | buffer2, |
---|
485 | size)) == -1) { |
---|
486 | perror("gzread"); |
---|
487 | return -1; |
---|
488 | } |
---|
489 | packet->size = numbytes + sizeof(dag_record_t); |
---|
490 | return sizeof(dag_record_t) + numbytes; |
---|
491 | } |
---|
492 | do { |
---|
493 | if (fifo_out_available(libtrace->fifo) == 0 || read_required) { |
---|
494 | if ((numbytes = trace_read(libtrace,buf,4096))<=0){ |
---|
495 | return numbytes; |
---|
496 | } |
---|
497 | fifo_write(libtrace->fifo,buf,numbytes); |
---|
498 | |
---|
499 | read_required = 0; |
---|
500 | } |
---|
501 | |
---|
502 | switch (libtrace->format) { |
---|
503 | case RTCLIENT: |
---|
504 | // only do this if we're reading from the RT interface |
---|
505 | if (fifo_out_read(libtrace->fifo, &packet->status, sizeof(int)) == 0) { |
---|
506 | read_required = 1; |
---|
507 | continue; |
---|
508 | } |
---|
509 | |
---|
510 | fifo_out_update(libtrace->fifo,sizeof(int)); |
---|
511 | |
---|
512 | /* FALL THRU */ |
---|
513 | case ERF: |
---|
514 | case DAG: |
---|
515 | // read in the erf header |
---|
516 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, sizeof(dag_record_t))) == 0) { |
---|
517 | fifo_out_reset(libtrace->fifo); |
---|
518 | read_required = 1; |
---|
519 | continue; |
---|
520 | } |
---|
521 | |
---|
522 | size = ntohs(((dag_record_t *)buffer)->rlen); |
---|
523 | break; |
---|
524 | case WAG: |
---|
525 | if ((numbytes = fifo_out_read(libtrace->fifo, |
---|
526 | &size, |
---|
527 | sizeof(size))) |
---|
528 | == 0) { |
---|
529 | fifo_out_reset(libtrace->fifo); |
---|
530 | read_required = 1; |
---|
531 | continue; |
---|
532 | } |
---|
533 | size*=4; |
---|
534 | break; |
---|
535 | default: |
---|
536 | fprintf(stderr,"Unknown type in _read()\n"); |
---|
537 | assert(0); |
---|
538 | } |
---|
539 | |
---|
540 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
541 | |
---|
542 | // read in the full packet |
---|
543 | if ((numbytes = fifo_out_read(libtrace->fifo, buffer, size)) == 0) { |
---|
544 | fifo_out_reset(libtrace->fifo); |
---|
545 | read_required = 1; |
---|
546 | continue; |
---|
547 | } |
---|
548 | |
---|
549 | // got in our whole packet, so... |
---|
550 | fifo_out_update(libtrace->fifo,size); |
---|
551 | |
---|
552 | if (libtrace->sourcetype == SOCKET || libtrace->sourcetype == RT) { |
---|
553 | fifo_ack_update(libtrace->fifo,size + sizeof(int)); |
---|
554 | } else { |
---|
555 | fifo_ack_update(libtrace->fifo,size); |
---|
556 | } |
---|
557 | |
---|
558 | packet->size = numbytes; |
---|
559 | return numbytes; |
---|
560 | |
---|
561 | } while (1); |
---|
562 | } |
---|
563 | |
---|
564 | |
---|
565 | /** get a pointer to the link layer |
---|
566 | * @param libtrace a pointer to the trace object returned from gettrace |
---|
567 | * @param buffer a pointer to a filled in buffer |
---|
568 | * @param buflen a pointer to the size of the buffer |
---|
569 | * |
---|
570 | * @returns a pointer to the link layer, or NULL if there is no link layer |
---|
571 | * you should call trace_get_link_type() to find out what type of link layer this is |
---|
572 | */ |
---|
573 | void *trace_get_link(struct libtrace_packet_t *packet) { |
---|
574 | void *ethptr = 0; |
---|
575 | |
---|
576 | struct wag_event_t *event = (struct wag_event_t *)packet->buffer; |
---|
577 | struct wag_data_event_t *data_event; |
---|
578 | |
---|
579 | |
---|
580 | switch(packet->trace->format) { |
---|
581 | case ERF: |
---|
582 | case DAG: |
---|
583 | case RTCLIENT: |
---|
584 | if (trace_get_link_type(packet)==TRACE_TYPE_ETH) |
---|
585 | ethptr = ((uint8_t *)packet->buffer + |
---|
586 | dag_record_size + 2); |
---|
587 | else |
---|
588 | ethptr = ((uint8_t *)packet->buffer + |
---|
589 | dag_record_size + 2); |
---|
590 | break; |
---|
591 | case PCAPINT: |
---|
592 | case PCAP: |
---|
593 | ethptr = (struct ether_header *)(packet->buffer + sizeof(struct pcap_pkthdr)); |
---|
594 | break; |
---|
595 | case WAGINT: |
---|
596 | case WAG: |
---|
597 | switch (event->type) { |
---|
598 | case 0x0: |
---|
599 | data_event = (void*)&(event->payload); |
---|
600 | return data_event->data; |
---|
601 | default: |
---|
602 | fprintf(stderr,"Unknown WAG Event (0x%08x)\n",event->type); |
---|
603 | return NULL; |
---|
604 | } |
---|
605 | |
---|
606 | default: |
---|
607 | fprintf(stderr,"Dunno this trace format\n"); |
---|
608 | assert(0); |
---|
609 | } |
---|
610 | return ethptr; |
---|
611 | } |
---|
612 | |
---|
613 | /** get a pointer to the IP header (if any) |
---|
614 | * @param libtrace a pointer to the trace object returned from gettrace |
---|
615 | * @param buffer a pointer to a filled in buffer |
---|
616 | * @param buflen a pointer to the size of the buffer |
---|
617 | * |
---|
618 | * @returns a pointer to the IP header, or NULL if there is not an IP packet |
---|
619 | */ |
---|
620 | struct libtrace_ip *trace_get_ip(struct libtrace_packet_t *packet) { |
---|
621 | struct libtrace_ip *ipptr = 0; |
---|
622 | |
---|
623 | switch(trace_get_link_type(packet)) { |
---|
624 | case TRACE_TYPE_80211: |
---|
625 | { |
---|
626 | |
---|
627 | struct ieee_802_11_header *wifi = trace_get_link(packet); |
---|
628 | |
---|
629 | // Data packet? |
---|
630 | if (wifi->type != 2) { |
---|
631 | ipptr = NULL; |
---|
632 | } |
---|
633 | else { |
---|
634 | struct ieee_802_11_payload *eth = (void*)wifi->data; |
---|
635 | if (eth->type != 0x0008) { |
---|
636 | ipptr=NULL; |
---|
637 | } else { |
---|
638 | ipptr=(void*)eth->data; |
---|
639 | } |
---|
640 | } |
---|
641 | } |
---|
642 | break; |
---|
643 | case TRACE_TYPE_ETH: |
---|
644 | { |
---|
645 | struct ether_header *eth = |
---|
646 | trace_get_link(packet); |
---|
647 | if (ntohs(eth->ether_type)!=0x0800) { |
---|
648 | ipptr = NULL; |
---|
649 | } |
---|
650 | else { |
---|
651 | ipptr = ((void *)eth) + 14; |
---|
652 | } |
---|
653 | break; |
---|
654 | } |
---|
655 | case TRACE_TYPE_ATM: |
---|
656 | { |
---|
657 | struct atm_rec *atm = |
---|
658 | trace_get_link(packet); |
---|
659 | // TODO: Find out what ATM does, and return |
---|
660 | // NULL for non IP data |
---|
661 | // Presumably it uses the normal stuff |
---|
662 | ipptr = (void*)&atm->pload; |
---|
663 | break; |
---|
664 | } |
---|
665 | default: |
---|
666 | fprintf(stderr,"Don't understand link layer type %i in trace_get_ip()\n", |
---|
667 | trace_get_link_type(packet)); |
---|
668 | ipptr=NULL; |
---|
669 | break; |
---|
670 | } |
---|
671 | |
---|
672 | return ipptr; |
---|
673 | } |
---|
674 | |
---|
675 | |
---|
676 | /** get a pointer to the TCP header (if any) |
---|
677 | * @param libtrace a pointer to the trace object returned from gettrace |
---|
678 | * @param buffer a pointer to a filled in buffer |
---|
679 | * @param buflen a pointer to the size of the buffer |
---|
680 | * |
---|
681 | * @returns a pointer to the TCP header, or NULL if there is not a TCP packet |
---|
682 | */ |
---|
683 | struct libtrace_tcp *trace_get_tcp(struct libtrace_packet_t *packet) { |
---|
684 | struct libtrace_tcp *tcpptr = 0; |
---|
685 | struct libtrace_ip *ipptr = 0; |
---|
686 | |
---|
687 | if(!(ipptr = trace_get_ip(packet))) { |
---|
688 | return 0; |
---|
689 | } |
---|
690 | if (ipptr->ip_p == 6) { |
---|
691 | tcpptr = (struct libtrace_tcp *)((int)ipptr + (ipptr->ip_hl * 4)); |
---|
692 | } |
---|
693 | return tcpptr; |
---|
694 | } |
---|
695 | |
---|
696 | /** get a pointer to the UDP header (if any) |
---|
697 | * @param libtrace a pointer to the trace object returned from gettrace |
---|
698 | * @param buffer a pointer to a filled in buffer |
---|
699 | * @param buflen a pointer to the size of the buffer |
---|
700 | * |
---|
701 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
702 | */ |
---|
703 | struct libtrace_udp *trace_get_udp(struct libtrace_packet_t *packet) { |
---|
704 | struct libtrace_udp *udpptr = 0; |
---|
705 | struct libtrace_ip *ipptr = 0; |
---|
706 | |
---|
707 | if(!(ipptr = trace_get_ip(packet))) { |
---|
708 | return 0; |
---|
709 | } |
---|
710 | if (ipptr->ip_p == 17) { |
---|
711 | udpptr = (struct libtrace_udp *)((int)ipptr + (ipptr->ip_hl * 4)); |
---|
712 | } |
---|
713 | return udpptr; |
---|
714 | } |
---|
715 | |
---|
716 | /** get a pointer to the ICMP header (if any) |
---|
717 | * @param libtrace a pointer to the trace object returned from gettrace |
---|
718 | * @param buffer a pointer to a filled in buffer |
---|
719 | * @param buflen a pointer to the size of the buffer |
---|
720 | * |
---|
721 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
722 | */ |
---|
723 | struct libtrace_icmp *trace_get_icmp(struct libtrace_packet_t *packet) { |
---|
724 | struct libtrace_icmp *icmpptr = 0; |
---|
725 | struct libtrace_ip *ipptr = 0; |
---|
726 | |
---|
727 | if(!(ipptr = trace_get_ip(packet))) { |
---|
728 | return 0; |
---|
729 | } |
---|
730 | if (ipptr->ip_p == 1) { |
---|
731 | icmpptr = (struct libtrace_icmp *)((int)ipptr + (ipptr->ip_hl * 4)); |
---|
732 | } |
---|
733 | return icmpptr; |
---|
734 | } |
---|
735 | |
---|
736 | /** Get the current time in DAG time format |
---|
737 | * @param libtrace the libtrace opaque pointer |
---|
738 | * @param buffer a pointer to a filled in buffer |
---|
739 | * @param buflen the length of the buffer |
---|
740 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
741 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
742 | * @author Daniel Lawson |
---|
743 | */ |
---|
744 | uint64_t trace_get_erf_timestamp(struct libtrace_packet_t *packet) { |
---|
745 | uint64_t timestamp = 0; |
---|
746 | dag_record_t *erfptr = 0; |
---|
747 | struct pcap_pkthdr *pcapptr = 0; |
---|
748 | struct wag_event_t *wagptr = 0; |
---|
749 | switch (packet->trace->format) { |
---|
750 | case DAG: |
---|
751 | case ERF: |
---|
752 | case RTCLIENT: |
---|
753 | erfptr = (dag_record_t *)packet->buffer; |
---|
754 | timestamp = erfptr->ts; |
---|
755 | break; |
---|
756 | case PCAPINT: |
---|
757 | case PCAP: |
---|
758 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
759 | timestamp = ((((uint64_t)pcapptr->ts.tv_sec) << 32) + \ |
---|
760 | (pcapptr->ts.tv_usec*UINT_MAX/1000000)); |
---|
761 | break; |
---|
762 | case WAGINT: |
---|
763 | case WAG: |
---|
764 | wagptr = (struct wag_event_t *)packet->buffer; |
---|
765 | timestamp = wagptr->timestamp_lo; |
---|
766 | timestamp |= (uint64_t)wagptr->timestamp_hi<<32; |
---|
767 | timestamp = ((timestamp%44000000)*(UINT_MAX/44000000)) |
---|
768 | | ((timestamp/44000000)<<32); |
---|
769 | break; |
---|
770 | default: |
---|
771 | fprintf(stderr,"Unknown format in trace_get_erf_timestamp\n"); |
---|
772 | timestamp = 0; |
---|
773 | } |
---|
774 | return timestamp; |
---|
775 | } |
---|
776 | |
---|
777 | /** Get the current time in struct timeval |
---|
778 | * @param libtrace the libtrace opaque pointer |
---|
779 | * @param buffer a pointer to a filled in buffer |
---|
780 | * @param buflen the length of the buffer |
---|
781 | * @returns time that this packet was seen in a struct timeval |
---|
782 | * @author Daniel Lawson |
---|
783 | * @author Perry Lorier |
---|
784 | */ |
---|
785 | struct timeval trace_get_timeval(struct libtrace_packet_t *packet) { |
---|
786 | struct timeval tv; |
---|
787 | struct pcap_pkthdr *pcapptr = 0; |
---|
788 | uint64_t ts; |
---|
789 | //uint32_t seconds; |
---|
790 | switch (packet->trace->format) { |
---|
791 | case PCAPINT: |
---|
792 | case PCAP: |
---|
793 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
794 | tv = pcapptr->ts; |
---|
795 | break; |
---|
796 | case WAGINT: |
---|
797 | case WAG: |
---|
798 | case DAG: |
---|
799 | case ERF: |
---|
800 | case RTCLIENT: |
---|
801 | default: |
---|
802 | // FIXME: This isn't portable to big-endian machines |
---|
803 | ts = trace_get_erf_timestamp(packet); |
---|
804 | tv.tv_sec = ts >> 32; |
---|
805 | ts = (1000000 * (ts & 0xffffffffULL)); |
---|
806 | ts += (ts & 0x80000000ULL) << 1; |
---|
807 | tv.tv_usec = ts >> 32; |
---|
808 | if (tv.tv_usec >= 1000000) { |
---|
809 | tv.tv_usec -= 1000000; |
---|
810 | tv.tv_sec += 1; |
---|
811 | } |
---|
812 | break; |
---|
813 | } |
---|
814 | return tv; |
---|
815 | } |
---|
816 | |
---|
817 | /** Get the current time in floating point seconds |
---|
818 | * @param libtrace the libtrace opaque pointer |
---|
819 | * @param buffer a pointer to a filled in buffer |
---|
820 | * @param buflen the length of the buffer |
---|
821 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
822 | * @author Perry Lorier |
---|
823 | */ |
---|
824 | double trace_get_seconds(struct libtrace_packet_t *packet) { |
---|
825 | uint64_t ts; |
---|
826 | ts = trace_get_erf_timestamp(packet); |
---|
827 | return (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
828 | } |
---|
829 | |
---|
830 | /** Get the size of the packet in the trace |
---|
831 | * @param packet the packet opaque pointer |
---|
832 | * @returns the size of the packet in the trace |
---|
833 | * @author Perry Lorier |
---|
834 | * @note Due to this being a header capture, or anonymisation, this may not |
---|
835 | * be the same size as the original packet. See trace_get_wire_length() for the |
---|
836 | * original size of the packet. |
---|
837 | * @note This can (and often is) different for different packets in a trace! |
---|
838 | * @par |
---|
839 | * This is sometimes called the "snaplen". |
---|
840 | */ |
---|
841 | int trace_get_capture_length(struct libtrace_packet_t *packet) { |
---|
842 | dag_record_t *erfptr = 0; |
---|
843 | struct pcap_pkthdr *pcapptr = 0; |
---|
844 | struct wag_event_t *wag_event; |
---|
845 | switch (packet->trace->format) { |
---|
846 | case DAG: |
---|
847 | case ERF: |
---|
848 | case RTCLIENT: |
---|
849 | erfptr = (dag_record_t *)packet->buffer; |
---|
850 | return ntohs(erfptr->rlen); |
---|
851 | case PCAPINT: |
---|
852 | case PCAP: |
---|
853 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
854 | //return ntohs(pcapptr->caplen); |
---|
855 | return pcapptr->caplen; |
---|
856 | case WAGINT: |
---|
857 | case WAG: |
---|
858 | wag_event = (struct wag_event_t *)packet->buffer; |
---|
859 | switch(wag_event->type) { |
---|
860 | case 0: |
---|
861 | return wag_event->length*4-( |
---|
862 | sizeof(struct wag_event_t)+ |
---|
863 | sizeof(struct wag_data_event_t) |
---|
864 | ); |
---|
865 | default: |
---|
866 | assert(0); |
---|
867 | } |
---|
868 | default: |
---|
869 | assert(0); |
---|
870 | } |
---|
871 | return -1; |
---|
872 | } |
---|
873 | |
---|
874 | /** Get the size of the packet as it was seen on the wire. |
---|
875 | * @param libtrace the libtrace opaque pointer |
---|
876 | * @param buffer a pointer to a filled in buffer |
---|
877 | * @param buflen the length of the buffer |
---|
878 | * @returns the size of the packet as it was on the wire. |
---|
879 | * @author Perry Lorier |
---|
880 | * @author Daniel Lawson |
---|
881 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
882 | * not be the same as the Capture Len. |
---|
883 | */ |
---|
884 | int trace_get_wire_length(struct libtrace_packet_t *packet){ |
---|
885 | dag_record_t *erfptr = 0; |
---|
886 | struct pcap_pkthdr *pcapptr = 0; |
---|
887 | struct wag_event_t *wag_event = 0; |
---|
888 | switch (packet->trace->format) { |
---|
889 | case DAG: |
---|
890 | case ERF: |
---|
891 | case RTCLIENT: |
---|
892 | erfptr = (dag_record_t *)packet->buffer; |
---|
893 | return ntohs(erfptr->wlen); |
---|
894 | break; |
---|
895 | case PCAPINT: |
---|
896 | case PCAP: |
---|
897 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
898 | return ntohs(pcapptr->len); |
---|
899 | break; |
---|
900 | case WAGINT: |
---|
901 | case WAG: |
---|
902 | wag_event = (struct wag_event_t *)packet->buffer; |
---|
903 | switch(wag_event->type) { |
---|
904 | case 0: |
---|
905 | return ((struct wag_data_event_t *)(&wag_event->payload))->frame_length; |
---|
906 | default: |
---|
907 | assert(0); |
---|
908 | } |
---|
909 | } |
---|
910 | return -1; |
---|
911 | |
---|
912 | } |
---|
913 | |
---|
914 | /** Get the type of the link layer |
---|
915 | * @param libtrace the libtrace opaque pointer |
---|
916 | * @param buffer a pointer to a filled in buffer |
---|
917 | * @param buflen the length of the buffer |
---|
918 | * @returns libtrace_linktype_t |
---|
919 | * @author Perry Lorier |
---|
920 | * @author Daniel Lawson |
---|
921 | */ |
---|
922 | libtrace_linktype_t trace_get_link_type(struct libtrace_packet_t *packet ) { |
---|
923 | dag_record_t *erfptr = 0; |
---|
924 | struct pcap_pkthdr *pcapptr = 0; |
---|
925 | int linktype = 0; |
---|
926 | switch (packet->trace->format) { |
---|
927 | case DAG: |
---|
928 | case ERF: |
---|
929 | case RTCLIENT: |
---|
930 | erfptr = (dag_record_t *)packet->buffer; |
---|
931 | switch (erfptr->type) { |
---|
932 | case TYPE_ETH: return TRACE_TYPE_ETH; |
---|
933 | case TYPE_ATM: return TRACE_TYPE_ATM; |
---|
934 | default: assert(0); |
---|
935 | } |
---|
936 | return erfptr->type; |
---|
937 | |
---|
938 | break; |
---|
939 | case PCAPINT: |
---|
940 | case PCAP: |
---|
941 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
942 | linktype = pcap_datalink(packet->trace->input.pcap); |
---|
943 | switch (linktype) { |
---|
944 | case 1: |
---|
945 | return TRACE_TYPE_ETH; |
---|
946 | case 11: |
---|
947 | return TRACE_TYPE_ATM; |
---|
948 | case DLT_IEEE802_11: |
---|
949 | return TRACE_TYPE_80211; |
---|
950 | } |
---|
951 | break; |
---|
952 | case WAGINT: |
---|
953 | case WAG: |
---|
954 | return TRACE_TYPE_80211; |
---|
955 | } |
---|
956 | return -1; |
---|
957 | } |
---|
958 | |
---|
959 | /** Get the source MAC addres |
---|
960 | * @param libtrace the libtrace opaque pointer |
---|
961 | * @param buffer a pointer to a filled in buffer |
---|
962 | * @param buflen the length of the buffer |
---|
963 | * @returns a pointer to the source mac, (or NULL if there is no source MAC) |
---|
964 | * @author Perry Lorier |
---|
965 | */ |
---|
966 | uint8_t *trace_get_source_mac(struct libtrace_packet_t *packet) { |
---|
967 | void *link = trace_get_link(packet); |
---|
968 | struct ieee_802_11_header *wifi = link; |
---|
969 | struct ether_header *ethptr = link; |
---|
970 | if (!link) |
---|
971 | return NULL; |
---|
972 | switch (trace_get_link_type(packet)) { |
---|
973 | case TRACE_TYPE_80211: |
---|
974 | return (uint8_t*)&wifi->mac2; |
---|
975 | case TRACE_TYPE_ETH: |
---|
976 | return (uint8_t*)ðptr->ether_shost; |
---|
977 | default: |
---|
978 | fprintf(stderr,"Not implemented\n"); |
---|
979 | assert(0); |
---|
980 | } |
---|
981 | } |
---|
982 | |
---|
983 | /** Get the destination MAC addres |
---|
984 | * @param libtrace the libtrace opaque pointer |
---|
985 | * @param buffer a pointer to a filled in buffer |
---|
986 | * @param buflen the length of the buffer |
---|
987 | * @returns a pointer to the destination mac, (or NULL if there is no |
---|
988 | * destination MAC) |
---|
989 | * @author Perry Lorier |
---|
990 | */ |
---|
991 | uint8_t *trace_get_destination_mac(struct libtrace_packet_t *packet) { |
---|
992 | void *link = trace_get_link(packet); |
---|
993 | struct ieee_802_11_header *wifi = link; |
---|
994 | struct ether_header *ethptr = link; |
---|
995 | if (!link) |
---|
996 | return NULL; |
---|
997 | switch (trace_get_link_type(packet)) { |
---|
998 | case TRACE_TYPE_80211: |
---|
999 | return (uint8_t*)&wifi->mac1; |
---|
1000 | case TRACE_TYPE_ETH: |
---|
1001 | return (uint8_t*)ðptr->ether_dhost; |
---|
1002 | default: |
---|
1003 | fprintf(stderr,"Not implemented\n"); |
---|
1004 | assert(0); |
---|
1005 | } |
---|
1006 | } |
---|
1007 | |
---|
1008 | |
---|
1009 | /** process a libtrace event |
---|
1010 | * @param libtrace the libtrace opaque pointer |
---|
1011 | * @param fd a pointer to a file descriptor to listen on |
---|
1012 | * @param seconds a pointer the time in seconds since to the next event |
---|
1013 | * @param buffer a pointer to a filled in buffer |
---|
1014 | * @param len the length of the buffer |
---|
1015 | * @param size the size of the event |
---|
1016 | * @returns |
---|
1017 | * TRACE_EVENT_IOWAIT Waiting on I/O on <fd> |
---|
1018 | * TRACE_EVENT_SLEEP Next event in <seconds> |
---|
1019 | * TRACE_EVENT_PACKET Packet arrived in <buffer> with size <size> |
---|
1020 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
1021 | * which in turn is stored inside the new packet object... |
---|
1022 | * @author Perry Lorier |
---|
1023 | */ |
---|
1024 | libtrace_event_t libtrace_event(struct libtrace_t *trace, |
---|
1025 | struct libtrace_packet_t *packet, |
---|
1026 | int *fd,double *seconds) { |
---|
1027 | *seconds = 0; |
---|
1028 | *fd = 0; |
---|
1029 | /* Is there a packet ready? */ |
---|
1030 | switch (trace->sourcetype) { |
---|
1031 | case INTERFACE: |
---|
1032 | { |
---|
1033 | int data; |
---|
1034 | *fd = pcap_fileno(trace->input.pcap); |
---|
1035 | if(ioctl(*fd,FIONREAD,&data)==-1){ |
---|
1036 | perror("ioctl(FIONREAD)"); |
---|
1037 | } |
---|
1038 | if (data>0) { |
---|
1039 | return TRACE_EVENT_PACKET; |
---|
1040 | } |
---|
1041 | return TRACE_EVENT_IOWAIT; |
---|
1042 | } |
---|
1043 | case SOCKET: |
---|
1044 | case DEVICE: |
---|
1045 | case RT: |
---|
1046 | { |
---|
1047 | int data; |
---|
1048 | if(ioctl(trace->input.fd,FIONREAD,&data)==-1){ |
---|
1049 | perror("ioctl(FIONREAD)"); |
---|
1050 | } |
---|
1051 | if (data>0) { |
---|
1052 | return TRACE_EVENT_PACKET; |
---|
1053 | } |
---|
1054 | *fd = trace->input.fd; |
---|
1055 | return TRACE_EVENT_IOWAIT; |
---|
1056 | } |
---|
1057 | case STDIN: |
---|
1058 | case TRACE: |
---|
1059 | { |
---|
1060 | double ts; |
---|
1061 | /* "Prime" the pump */ |
---|
1062 | if (!trace->packet.buffer) { |
---|
1063 | trace->packet.buffer = malloc(4096); |
---|
1064 | trace->packet.size= |
---|
1065 | trace_read_packet(trace,packet); |
---|
1066 | } |
---|
1067 | ts=trace_get_seconds(packet); |
---|
1068 | if (trace->last_ts!=0) { |
---|
1069 | *seconds = ts - trace->last_ts; |
---|
1070 | if (*seconds>time(NULL)-trace->start_ts) |
---|
1071 | return TRACE_EVENT_SLEEP; |
---|
1072 | } |
---|
1073 | else { |
---|
1074 | trace->start_ts = time(NULL); |
---|
1075 | trace->last_ts = ts; |
---|
1076 | } |
---|
1077 | |
---|
1078 | packet->size = trace->packet.size; |
---|
1079 | memcpy(packet->buffer,trace->packet.buffer,trace->packet.size); |
---|
1080 | |
---|
1081 | free(trace->packet.buffer); |
---|
1082 | trace->packet.buffer = 0; |
---|
1083 | return TRACE_EVENT_PACKET; |
---|
1084 | } |
---|
1085 | default: |
---|
1086 | assert(0); |
---|
1087 | } |
---|
1088 | /* Shouldn't get here */ |
---|
1089 | assert(0); |
---|
1090 | } |
---|
1091 | |
---|
1092 | /** setup a BPF filter |
---|
1093 | * @param filterstring a char * containing the bpf filter string |
---|
1094 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
1095 | * @author Daniel Lawson |
---|
1096 | */ |
---|
1097 | struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring) { |
---|
1098 | struct libtrace_filter_t *filter = malloc(sizeof(struct libtrace_filter_t)); |
---|
1099 | filter->filterstring = strdup(filterstring); |
---|
1100 | filter->filter = 0; |
---|
1101 | return filter; |
---|
1102 | } |
---|
1103 | |
---|
1104 | /** apply a BPF filter |
---|
1105 | * @param libtrace the libtrace opaque pointer |
---|
1106 | * @param filter the filter opaque pointer |
---|
1107 | * @param buffer a pointer to a filled buffer |
---|
1108 | * @param buflen the length of the buffer |
---|
1109 | * @returns 0 if the filter fails, 1 if it succeeds |
---|
1110 | * @author Daniel Lawson |
---|
1111 | */ |
---|
1112 | int trace_bpf_filter(struct libtrace_filter_t *filter, |
---|
1113 | struct libtrace_packet_t *packet) { |
---|
1114 | |
---|
1115 | void *linkptr = 0; |
---|
1116 | int clen = 0; |
---|
1117 | assert(filter); |
---|
1118 | assert(packet); |
---|
1119 | linkptr = trace_get_link(packet); |
---|
1120 | assert(linkptr); |
---|
1121 | clen = trace_get_capture_length(packet); |
---|
1122 | |
---|
1123 | |
---|
1124 | if (filter->filterstring && ! filter->filter) { |
---|
1125 | pcap_t *pcap; |
---|
1126 | struct bpf_program bpfprog; |
---|
1127 | |
---|
1128 | switch (trace_get_link_type(packet)) { |
---|
1129 | case TRACE_TYPE_ETH: |
---|
1130 | pcap = pcap_open_dead(DLT_EN10MB, 1500); |
---|
1131 | break; |
---|
1132 | default: |
---|
1133 | printf("only works for ETH at the moment\n"); |
---|
1134 | assert(0); |
---|
1135 | } |
---|
1136 | |
---|
1137 | // build filter |
---|
1138 | if (pcap_compile( pcap, &bpfprog, filter->filterstring, 1, 0)) { |
---|
1139 | printf("bpf compilation error: %s\n", |
---|
1140 | pcap_geterr(pcap)); |
---|
1141 | assert(0); |
---|
1142 | } |
---|
1143 | pcap_close(pcap); |
---|
1144 | filter->filter = bpfprog.bf_insns; |
---|
1145 | } |
---|
1146 | |
---|
1147 | assert(filter->filter); |
---|
1148 | return bpf_filter(filter->filter, linkptr, clen, clen); |
---|
1149 | |
---|
1150 | } |
---|
1151 | |
---|
1152 | /** Get the direction flag, if it has one |
---|
1153 | * @param libtrace the libtrace opaque pointer |
---|
1154 | * @param buffer a point to a fille in buffer |
---|
1155 | * @param buflen the length of the buffer |
---|
1156 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1157 | * @author Daniel Lawson |
---|
1158 | */ |
---|
1159 | int8_t trace_get_direction(struct libtrace_packet_t *packet) { |
---|
1160 | |
---|
1161 | int8_t direction; |
---|
1162 | dag_record_t *erfptr = 0; |
---|
1163 | assert(packet); |
---|
1164 | |
---|
1165 | switch(packet->trace->format) { |
---|
1166 | case DAG: |
---|
1167 | case ERF: |
---|
1168 | case RTCLIENT: |
---|
1169 | erfptr = (dag_record_t *)packet->buffer; |
---|
1170 | direction = erfptr->flags.iface; |
---|
1171 | break; |
---|
1172 | default: |
---|
1173 | direction = -1; |
---|
1174 | } |
---|
1175 | |
---|
1176 | return direction; |
---|
1177 | |
---|
1178 | |
---|
1179 | } |
---|
1180 | |
---|