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