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