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