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