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