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