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 | |
---|
32 | /** @file |
---|
33 | * |
---|
34 | * @brief Trace file processing library |
---|
35 | * |
---|
36 | * @author Daniel Lawson |
---|
37 | * @author Perry Lorier |
---|
38 | * |
---|
39 | * @internal |
---|
40 | */ |
---|
41 | #define _GNU_SOURCE |
---|
42 | #include "common.h" |
---|
43 | #include "config.h" |
---|
44 | #include <assert.h> |
---|
45 | #include <errno.h> |
---|
46 | #include <fcntl.h> |
---|
47 | #include <netdb.h> |
---|
48 | #include <stdio.h> |
---|
49 | #include <stdlib.h> |
---|
50 | #include <string.h> |
---|
51 | #include <sys/stat.h> |
---|
52 | #include <sys/types.h> |
---|
53 | |
---|
54 | #ifdef HAVE_LIMITS_H |
---|
55 | # include <limits.h> |
---|
56 | #endif |
---|
57 | |
---|
58 | #ifdef HAVE_SYS_LIMITS_H |
---|
59 | # include <sys/limits.h> |
---|
60 | #endif |
---|
61 | |
---|
62 | #include <sys/socket.h> |
---|
63 | #include <sys/un.h> |
---|
64 | #include <sys/mman.h> |
---|
65 | #include <unistd.h> |
---|
66 | |
---|
67 | #ifdef HAVE_NET_IF_ARP_H |
---|
68 | # include <net/if_arp.h> |
---|
69 | #endif |
---|
70 | |
---|
71 | #ifdef HAVE_NET_IF_H |
---|
72 | # include <net/if.h> |
---|
73 | #endif |
---|
74 | |
---|
75 | #ifdef HAVE_NETINET_IN_H |
---|
76 | # include <netinet/in.h> |
---|
77 | #endif |
---|
78 | |
---|
79 | #ifdef HAVE_NET_ETHERNET_H |
---|
80 | # include <net/ethernet.h> |
---|
81 | #endif |
---|
82 | |
---|
83 | #ifdef HAVE_NETINET_IF_ETHER_H |
---|
84 | # include <netinet/if_ether.h> |
---|
85 | #endif |
---|
86 | |
---|
87 | #include <time.h> |
---|
88 | #include <sys/ioctl.h> |
---|
89 | |
---|
90 | #ifdef HAVE_INTTYPES_H |
---|
91 | # include <inttypes.h> |
---|
92 | #else |
---|
93 | # error "Can't find inttypes.h - this needs to be fixed" |
---|
94 | #endif |
---|
95 | |
---|
96 | #ifdef HAVE_STDDEF_H |
---|
97 | # include <stddef.h> |
---|
98 | #else |
---|
99 | # error "Can't find stddef.h - do you define ptrdiff_t elsewhere?" |
---|
100 | #endif |
---|
101 | |
---|
102 | #include "libtrace.h" |
---|
103 | #include "fifo.h" |
---|
104 | #include "libtrace_int.h" |
---|
105 | #include "parse_cmd.h" |
---|
106 | |
---|
107 | #if HAVE_PCAP_BPF_H |
---|
108 | # include <pcap-bpf.h> |
---|
109 | #else |
---|
110 | # ifdef HAVE_NET_BPF_H |
---|
111 | # include <net/bpf.h> |
---|
112 | # endif |
---|
113 | #endif |
---|
114 | |
---|
115 | #include "libtrace_int.h" |
---|
116 | #include "format_helper.h" |
---|
117 | #include <err.h> |
---|
118 | |
---|
119 | #define MAXOPTS 1024 |
---|
120 | |
---|
121 | #if HAVE_BPF |
---|
122 | /** A type encapsulating a bpf filter |
---|
123 | * This type covers the compiled bpf filter, as well as the original filter |
---|
124 | * string |
---|
125 | * |
---|
126 | */ |
---|
127 | struct libtrace_filter_t { |
---|
128 | struct bpf_insn *filter; |
---|
129 | char * filterstring; |
---|
130 | }; |
---|
131 | #endif |
---|
132 | |
---|
133 | |
---|
134 | |
---|
135 | struct libtrace_format_t **format_list = 0; |
---|
136 | int format_size = 0; |
---|
137 | int nformats = 0; |
---|
138 | |
---|
139 | void register_format(struct libtrace_format_t *f) { |
---|
140 | // fprintf(stderr,"Registering input format %s\n",f->name); |
---|
141 | if (format_list == 0) { |
---|
142 | format_size = 10; |
---|
143 | format_list = malloc(sizeof(struct libtrace_format_t *) * format_size); |
---|
144 | } else if (format_size == nformats) { |
---|
145 | format_size = format_size + 10; |
---|
146 | format_list = realloc(format_list, |
---|
147 | sizeof(struct libtrace_format_t *) * format_size); |
---|
148 | } |
---|
149 | format_list[nformats] = f; |
---|
150 | nformats++; |
---|
151 | } |
---|
152 | |
---|
153 | /** Prints help information for libtrace |
---|
154 | * |
---|
155 | * Function prints out some basic help information regarding libtrace, |
---|
156 | * and then prints out the help() function registered with each input module |
---|
157 | */ |
---|
158 | void trace_help() { |
---|
159 | int i = 0; |
---|
160 | printf("libtrace %s\n",PACKAGE_VERSION); |
---|
161 | for (i = 0; i < nformats; i++) { |
---|
162 | if (format_list[i]->help) { |
---|
163 | format_list[i]->help(); |
---|
164 | } |
---|
165 | } |
---|
166 | } |
---|
167 | |
---|
168 | void trace_perror(char *caller) { |
---|
169 | switch (trace_err.err_num) { |
---|
170 | case E_BAD_FORMAT: |
---|
171 | fprintf(stderr, "%s: No support for format (%s)\n", caller, trace_err.problem); |
---|
172 | break; |
---|
173 | case E_NO_INIT: |
---|
174 | fprintf(stderr, "%s: Format (%s) does not have an init_trace function defined\n", caller, trace_err.problem); |
---|
175 | break; |
---|
176 | case E_NO_INIT_OUT: |
---|
177 | fprintf(stderr, "%s: Format (%s) does not have an init_output function defined\n", caller, trace_err.problem); |
---|
178 | break; |
---|
179 | case E_URI_LONG: |
---|
180 | fprintf(stderr, "%s: uri is too long\n", caller); |
---|
181 | break; |
---|
182 | case E_URI_NOCOLON: |
---|
183 | fprintf(stderr, "%s: A uri must contain at least one colon e.g. format:destination\n", caller); |
---|
184 | break; |
---|
185 | case E_INIT_FAILED: |
---|
186 | fprintf(stderr, "%s: libtrace failed to initialise (%s)\n",caller,trace_err.problem); |
---|
187 | |
---|
188 | default: |
---|
189 | fprintf(stderr, "Unkown errcode %d\n",trace_err.err_num); |
---|
190 | break; |
---|
191 | } |
---|
192 | trace_err.err_num = E_NOERROR; |
---|
193 | } |
---|
194 | |
---|
195 | #define RP_BUFSIZE 65536 |
---|
196 | #define URI_PROTO_LINE 16 |
---|
197 | |
---|
198 | |
---|
199 | |
---|
200 | /** Create a trace file from a URI |
---|
201 | * |
---|
202 | * @returns opaque pointer to a libtrace_t |
---|
203 | * |
---|
204 | * Valid URI's are: |
---|
205 | * erf:/path/to/erf/file |
---|
206 | * erf:/path/to/erf/file.gz |
---|
207 | * erf:/path/to/rtclient/socket |
---|
208 | * erf:- (stdin) |
---|
209 | * pcapint:pcapinterface (eg: pcapint:eth0) |
---|
210 | * pcap:/path/to/pcap/file |
---|
211 | * pcap:- |
---|
212 | * rtclient:hostname |
---|
213 | * rtclient:hostname:port |
---|
214 | * wag:- |
---|
215 | * wag:/path/to/wag/file |
---|
216 | * wag:/path/to/wag/file.gz |
---|
217 | * wag:/path/to/wag/socket |
---|
218 | * wagint:/dev/device |
---|
219 | * |
---|
220 | * URIs which have yet to be implemented are: |
---|
221 | * dag:/dev/dagcard |
---|
222 | * pcap:/path/to/pcap/socket |
---|
223 | * |
---|
224 | * If an error occured when attempting to open a trace, NULL is returned |
---|
225 | * and an error is output to stdout. |
---|
226 | */ |
---|
227 | struct libtrace_t *trace_create(char *uri) { |
---|
228 | struct libtrace_t *libtrace = malloc(sizeof(struct libtrace_t)); |
---|
229 | char *scan = calloc(sizeof(char),URI_PROTO_LINE); |
---|
230 | char *uridata = 0; |
---|
231 | int i = 0; |
---|
232 | struct stat buf; |
---|
233 | |
---|
234 | trace_err.err_num = E_NOERROR; |
---|
235 | |
---|
236 | |
---|
237 | // parse the URI to determine what sort of event we are dealing with |
---|
238 | |
---|
239 | // want snippet before the : to get the uri base type. |
---|
240 | |
---|
241 | if((uridata = strchr(uri,':')) == NULL) { |
---|
242 | // badly formed URI - needs a : |
---|
243 | trace_err.err_num = E_URI_NOCOLON; |
---|
244 | return 0; |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | if ((uridata - uri) > URI_PROTO_LINE) { |
---|
249 | // badly formed URI - uri type is too long |
---|
250 | trace_err.err_num = E_URI_LONG; |
---|
251 | return 0; |
---|
252 | } |
---|
253 | strncpy(scan,uri, (uridata - uri)); |
---|
254 | |
---|
255 | libtrace->event.tdelta = 0.0; |
---|
256 | |
---|
257 | |
---|
258 | libtrace->format = 0; |
---|
259 | for (i = 0; i < nformats; i++) { |
---|
260 | if (strlen(scan) == strlen(format_list[i]->name) && |
---|
261 | !strncasecmp(scan, |
---|
262 | format_list[i]->name, |
---|
263 | strlen(scan))) { |
---|
264 | libtrace->format=format_list[i]; |
---|
265 | break; |
---|
266 | } |
---|
267 | } |
---|
268 | if (libtrace->format == 0) { |
---|
269 | trace_err.err_num = E_BAD_FORMAT; |
---|
270 | trace_err.problem = scan; |
---|
271 | return 0; |
---|
272 | } |
---|
273 | |
---|
274 | // push uridata past the delimiter |
---|
275 | uridata++; |
---|
276 | libtrace->uridata = strdup(uridata); |
---|
277 | |
---|
278 | // libtrace->format now contains the type of uri |
---|
279 | // libtrace->uridata contains the appropriate data for this |
---|
280 | |
---|
281 | if (libtrace->format->init_input) { |
---|
282 | if (!libtrace->format->init_input( libtrace)) { |
---|
283 | trace_err.err_num = E_INIT_FAILED; |
---|
284 | trace_err.problem = scan; |
---|
285 | return 0; |
---|
286 | } |
---|
287 | } else { |
---|
288 | trace_err.err_num = E_NO_INIT; |
---|
289 | trace_err.problem = scan; |
---|
290 | return 0; |
---|
291 | } |
---|
292 | |
---|
293 | |
---|
294 | libtrace->fifo = create_fifo(1048576); |
---|
295 | assert( libtrace->fifo); |
---|
296 | |
---|
297 | return libtrace; |
---|
298 | } |
---|
299 | |
---|
300 | /** Creates a libtrace_out_t structure and the socket / file through which output will be directed. |
---|
301 | * |
---|
302 | * @param uri the uri string describing the output format and the destination |
---|
303 | * @returns the newly created libtrace_out_t structure |
---|
304 | * |
---|
305 | * @author Shane Alcock |
---|
306 | * */ |
---|
307 | struct libtrace_out_t *trace_output_create(char *uri) { |
---|
308 | struct libtrace_out_t *libtrace = malloc(sizeof(struct libtrace_out_t)); |
---|
309 | |
---|
310 | char *scan = calloc(sizeof(char),URI_PROTO_LINE); |
---|
311 | char *uridata = 0; |
---|
312 | int i; |
---|
313 | |
---|
314 | trace_err.err_num = E_NOERROR; |
---|
315 | // parse the URI to determine what sort of event we are dealing with |
---|
316 | |
---|
317 | // want snippet before the : to get the uri base type. |
---|
318 | |
---|
319 | if((uridata = strchr(uri,':')) == NULL) { |
---|
320 | // badly formed URI - needs a : |
---|
321 | trace_err.err_num = E_URI_NOCOLON; |
---|
322 | return 0; |
---|
323 | } |
---|
324 | |
---|
325 | if ((uridata - uri) > URI_PROTO_LINE) { |
---|
326 | // badly formed URI - uri type is too long |
---|
327 | trace_err.err_num = E_URI_LONG; |
---|
328 | return 0; |
---|
329 | } |
---|
330 | |
---|
331 | strncpy(scan,uri, (uridata - uri)); |
---|
332 | |
---|
333 | libtrace->format = 0; |
---|
334 | for (i = 0; i < nformats; i++) { |
---|
335 | if (strlen(scan) == strlen(format_list[i]->name) && |
---|
336 | !strncasecmp(scan, |
---|
337 | format_list[i]->name, |
---|
338 | strlen(scan))) { |
---|
339 | libtrace->format=format_list[i]; |
---|
340 | break; |
---|
341 | } |
---|
342 | } |
---|
343 | if (libtrace->format == 0) { |
---|
344 | trace_err.err_num = E_BAD_FORMAT; |
---|
345 | trace_err.problem = scan; |
---|
346 | return 0; |
---|
347 | } |
---|
348 | |
---|
349 | // push uridata past the delimiter |
---|
350 | uridata++; |
---|
351 | libtrace->uridata = strdup(uridata); |
---|
352 | |
---|
353 | |
---|
354 | // libtrace->format now contains the type of uri |
---|
355 | // libtrace->uridata contains the appropriate data for this |
---|
356 | |
---|
357 | if (libtrace->format->init_output) { |
---|
358 | if(!libtrace->format->init_output( libtrace)) { |
---|
359 | return 0; |
---|
360 | } |
---|
361 | } else { |
---|
362 | trace_err.err_num = E_NO_INIT_OUT; |
---|
363 | trace_err.problem = scan; |
---|
364 | return 0; |
---|
365 | } |
---|
366 | |
---|
367 | |
---|
368 | libtrace->fifo = create_fifo(1048576); |
---|
369 | assert( libtrace->fifo); |
---|
370 | |
---|
371 | return libtrace; |
---|
372 | } |
---|
373 | |
---|
374 | /** Parses an output options string and calls the appropriate function to deal with output options. |
---|
375 | * |
---|
376 | * @param libtrace the output trace object to apply the options to |
---|
377 | * @param options the options string |
---|
378 | * @returns -1 if option configuration failed, 0 otherwise |
---|
379 | * |
---|
380 | * @author Shane Alcock |
---|
381 | */ |
---|
382 | int trace_output_config(struct libtrace_out_t *libtrace, char *options) { |
---|
383 | char *opt_string = 0; |
---|
384 | char *opt_argv[MAXOPTS]; |
---|
385 | int opt_argc = 0; |
---|
386 | |
---|
387 | assert(libtrace); |
---|
388 | |
---|
389 | if (!options) { |
---|
390 | printf("No options specified\n"); |
---|
391 | return 0; |
---|
392 | } |
---|
393 | |
---|
394 | asprintf(&opt_string, "%s %s", libtrace->format->name, options); |
---|
395 | parse_cmd(opt_string, &opt_argc, opt_argv, MAXOPTS); |
---|
396 | |
---|
397 | if (libtrace->format->config_output) |
---|
398 | return libtrace->format->config_output(libtrace, opt_argc, opt_argv); |
---|
399 | |
---|
400 | } |
---|
401 | |
---|
402 | /** Close a trace file, freeing up any resources it may have been using |
---|
403 | * |
---|
404 | */ |
---|
405 | void trace_destroy(struct libtrace_t *libtrace) { |
---|
406 | assert(libtrace); |
---|
407 | libtrace->format->fin_input(libtrace); |
---|
408 | // need to free things! |
---|
409 | free(libtrace->uridata); |
---|
410 | destroy_fifo(libtrace->fifo); |
---|
411 | free(libtrace); |
---|
412 | } |
---|
413 | |
---|
414 | /** Close an output trace file, freeing up any resources it may have been using |
---|
415 | * |
---|
416 | * @param libtrace the output trace file to be destroyed |
---|
417 | * |
---|
418 | * @author Shane Alcock |
---|
419 | * */ |
---|
420 | void trace_output_destroy(struct libtrace_out_t *libtrace) { |
---|
421 | assert(libtrace); |
---|
422 | libtrace->format->fin_output(libtrace); |
---|
423 | free(libtrace->uridata); |
---|
424 | destroy_fifo(libtrace->fifo); |
---|
425 | free(libtrace); |
---|
426 | } |
---|
427 | |
---|
428 | /** Read one packet from the trace into buffer |
---|
429 | * |
---|
430 | * @param libtrace the libtrace opaque pointer |
---|
431 | * @param packet the packet opaque pointer |
---|
432 | * @returns false if it failed to read a packet |
---|
433 | * |
---|
434 | */ |
---|
435 | int trace_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
436 | |
---|
437 | if (!libtrace) { |
---|
438 | fprintf(stderr,"You called trace_read_packet() with a NULL libtrace parameter!\n"); |
---|
439 | } |
---|
440 | assert(libtrace); |
---|
441 | assert(packet); |
---|
442 | |
---|
443 | /* Store the trace we are reading from into the packet opaque |
---|
444 | * structure */ |
---|
445 | packet->trace = libtrace; |
---|
446 | |
---|
447 | if (libtrace->format->read_packet) { |
---|
448 | return libtrace->format->read_packet(libtrace,packet); |
---|
449 | } |
---|
450 | } |
---|
451 | |
---|
452 | /** Writes a packet to the specified output |
---|
453 | * |
---|
454 | * @param libtrace describes the output format, destination, etc. |
---|
455 | * @param packet the packet to be written out |
---|
456 | * @returns the number of bytes written, -1 if write failed |
---|
457 | * |
---|
458 | * @author Shane Alcock |
---|
459 | * */ |
---|
460 | int trace_write_packet(struct libtrace_out_t *libtrace, struct libtrace_packet_t *packet) { |
---|
461 | assert(libtrace); |
---|
462 | assert(packet); |
---|
463 | |
---|
464 | if (libtrace->format->write_packet) { |
---|
465 | return libtrace->format->write_packet(libtrace, packet); |
---|
466 | } |
---|
467 | |
---|
468 | } |
---|
469 | |
---|
470 | /** get a pointer to the link layer |
---|
471 | * @param packet a pointer to a libtrace_packet structure |
---|
472 | * |
---|
473 | * @returns a pointer to the link layer, or NULL if there is no link layer |
---|
474 | * you should call trace_get_link_type() to find out what type of link layer this is |
---|
475 | */ |
---|
476 | void *trace_get_link(const struct libtrace_packet_t *packet) { |
---|
477 | const void *ethptr = 0; |
---|
478 | |
---|
479 | if (packet->trace->format->get_link) { |
---|
480 | ethptr = packet->trace->format->get_link(packet); |
---|
481 | } |
---|
482 | return (void *)ethptr; |
---|
483 | } |
---|
484 | |
---|
485 | /** get a pointer to the IP header (if any) |
---|
486 | * @param packet a pointer to a libtrace_packet structure |
---|
487 | * |
---|
488 | * @returns a pointer to the IP header, or NULL if there is not an IP packet |
---|
489 | */ |
---|
490 | struct libtrace_ip *trace_get_ip(const struct libtrace_packet_t *packet) { |
---|
491 | struct libtrace_ip *ipptr = 0; |
---|
492 | |
---|
493 | switch(trace_get_link_type(packet)) { |
---|
494 | case TRACE_TYPE_80211: |
---|
495 | { |
---|
496 | |
---|
497 | struct ieee_802_11_header *wifi = trace_get_link(packet); |
---|
498 | if (!wifi) { |
---|
499 | ipptr = NULL; |
---|
500 | break; |
---|
501 | } |
---|
502 | |
---|
503 | // Data packet? |
---|
504 | if (wifi->type != 2) { |
---|
505 | ipptr = NULL; |
---|
506 | } |
---|
507 | else { |
---|
508 | struct ieee_802_11_payload *eth = (void*)wifi->data; |
---|
509 | if (eth->type != 0x0008) { |
---|
510 | ipptr=NULL; |
---|
511 | } else { |
---|
512 | ipptr=(void*)eth->data; |
---|
513 | } |
---|
514 | } |
---|
515 | } |
---|
516 | break; |
---|
517 | case TRACE_TYPE_ETH: |
---|
518 | { |
---|
519 | struct ether_header *eth = |
---|
520 | trace_get_link(packet); |
---|
521 | if (!eth) { |
---|
522 | ipptr = NULL; |
---|
523 | break; |
---|
524 | } |
---|
525 | if (ntohs(eth->ether_type)!=0x0800) { |
---|
526 | ipptr = NULL; |
---|
527 | } |
---|
528 | else { |
---|
529 | ipptr = ((void *)eth) + 14; |
---|
530 | } |
---|
531 | break; |
---|
532 | } |
---|
533 | case TRACE_TYPE_NONE: |
---|
534 | ipptr = trace_get_link(packet); |
---|
535 | break; |
---|
536 | case TRACE_TYPE_LINUX_SLL: |
---|
537 | { |
---|
538 | struct trace_sll_header_t *sll; |
---|
539 | |
---|
540 | sll = trace_get_link(packet); |
---|
541 | if (!sll) { |
---|
542 | ipptr = NULL; |
---|
543 | break; |
---|
544 | } |
---|
545 | if (ntohs(sll->protocol)!=0x0800) { |
---|
546 | ipptr = NULL; |
---|
547 | } |
---|
548 | else { |
---|
549 | ipptr = ((void*)sll)+sizeof(*sll); |
---|
550 | } |
---|
551 | } |
---|
552 | break; |
---|
553 | case TRACE_TYPE_PFLOG: |
---|
554 | { |
---|
555 | struct trace_pflog_header_t *pflog; |
---|
556 | pflog = trace_get_link(packet); |
---|
557 | if (!pflog) { |
---|
558 | ipptr = NULL; |
---|
559 | break; |
---|
560 | } |
---|
561 | if (pflog->af != AF_INET) { |
---|
562 | ipptr = NULL; |
---|
563 | } else { |
---|
564 | ipptr = ((void*)pflog)+sizeof(*pflog); |
---|
565 | } |
---|
566 | } |
---|
567 | break; |
---|
568 | case TRACE_TYPE_ATM: |
---|
569 | { |
---|
570 | struct atm_rec *atm = |
---|
571 | trace_get_link(packet); |
---|
572 | // TODO: Find out what ATM does, and return |
---|
573 | // NULL for non IP data |
---|
574 | // Presumably it uses the normal stuff |
---|
575 | if (!atm) { |
---|
576 | ipptr = NULL; |
---|
577 | break; |
---|
578 | } |
---|
579 | ipptr = (void*)&atm->pload; |
---|
580 | break; |
---|
581 | } |
---|
582 | default: |
---|
583 | fprintf(stderr,"Don't understand link layer type %i in trace_get_ip()\n", |
---|
584 | trace_get_link_type(packet)); |
---|
585 | ipptr=NULL; |
---|
586 | break; |
---|
587 | } |
---|
588 | |
---|
589 | return ipptr; |
---|
590 | } |
---|
591 | |
---|
592 | #define SW_IP_OFFMASK 0xff1f |
---|
593 | |
---|
594 | /** get a pointer to the TCP header (if any) |
---|
595 | * @param packet a pointer to a libtrace_packet structure |
---|
596 | * |
---|
597 | * @returns a pointer to the TCP header, or NULL if there is not a TCP packet |
---|
598 | */ |
---|
599 | struct libtrace_tcp *trace_get_tcp(const struct libtrace_packet_t *packet) { |
---|
600 | struct libtrace_tcp *tcpptr = 0; |
---|
601 | struct libtrace_ip *ipptr = 0; |
---|
602 | |
---|
603 | if(!(ipptr = trace_get_ip(packet))) { |
---|
604 | return 0; |
---|
605 | } |
---|
606 | if ((ipptr->ip_p == 6) && ((ipptr->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
607 | tcpptr = (struct libtrace_tcp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
608 | } |
---|
609 | return tcpptr; |
---|
610 | } |
---|
611 | |
---|
612 | /** get a pointer to the TCP header (if any) given a pointer to the IP header |
---|
613 | * @param ip The IP header |
---|
614 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
615 | * |
---|
616 | * @returns a pointer to the TCP header, or NULL if this is not a TCP packet |
---|
617 | * |
---|
618 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
619 | */ |
---|
620 | struct libtrace_tcp *get_tcp_from_ip(const struct libtrace_ip *ip, int *skipped) |
---|
621 | { |
---|
622 | #define SW_IP_OFFMASK 0xff1f |
---|
623 | struct libtrace_tcp *tcpptr = 0; |
---|
624 | |
---|
625 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
626 | tcpptr = (struct libtrace_tcp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
627 | } |
---|
628 | |
---|
629 | if (skipped) |
---|
630 | *skipped=(ip->ip_hl*4); |
---|
631 | |
---|
632 | return tcpptr; |
---|
633 | } |
---|
634 | |
---|
635 | /** get a pointer to the UDP header (if any) |
---|
636 | * @param packet a pointer to a libtrace_packet structure |
---|
637 | * |
---|
638 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
639 | */ |
---|
640 | struct libtrace_udp *trace_get_udp(const struct libtrace_packet_t *packet) { |
---|
641 | struct libtrace_udp *udpptr = 0; |
---|
642 | struct libtrace_ip *ipptr = 0; |
---|
643 | |
---|
644 | if(!(ipptr = trace_get_ip(packet))) { |
---|
645 | return 0; |
---|
646 | } |
---|
647 | if ((ipptr->ip_p == 17) && ((ipptr->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
648 | udpptr = (struct libtrace_udp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
649 | } |
---|
650 | |
---|
651 | return udpptr; |
---|
652 | } |
---|
653 | |
---|
654 | /** get a pointer to the UDP header (if any) given a pointer to the IP header |
---|
655 | * @param ip The IP header |
---|
656 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
657 | * |
---|
658 | * @returns a pointer to the UDP header, or NULL if this is not a UDP packet |
---|
659 | * |
---|
660 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
661 | */ |
---|
662 | struct libtrace_udp *get_udp_from_ip(const struct libtrace_ip *ip, int *skipped) |
---|
663 | { |
---|
664 | struct libtrace_udp *udpptr = 0; |
---|
665 | |
---|
666 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
667 | udpptr = (struct libtrace_udp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
668 | } |
---|
669 | |
---|
670 | if (skipped) |
---|
671 | *skipped=(ip->ip_hl*4); |
---|
672 | |
---|
673 | return udpptr; |
---|
674 | } |
---|
675 | |
---|
676 | |
---|
677 | /** get a pointer to the ICMP header (if any) |
---|
678 | * @param packet a pointer to a libtrace_packet structure |
---|
679 | * |
---|
680 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
681 | */ |
---|
682 | struct libtrace_icmp *trace_get_icmp(const struct libtrace_packet_t *packet) { |
---|
683 | struct libtrace_icmp *icmpptr = 0; |
---|
684 | struct libtrace_ip *ipptr = 0; |
---|
685 | |
---|
686 | if(!(ipptr = trace_get_ip(packet))) { |
---|
687 | return 0; |
---|
688 | } |
---|
689 | if ((ipptr->ip_p == 1)&& ((ipptr->ip_off & SW_IP_OFFMASK) == 0 )){ |
---|
690 | icmpptr = (struct libtrace_icmp *)((ptrdiff_t)ipptr + (ipptr->ip_hl * 4)); |
---|
691 | } |
---|
692 | return icmpptr; |
---|
693 | } |
---|
694 | |
---|
695 | /** get a pointer to the ICMP header (if any) given a pointer to the IP header |
---|
696 | * @param ip The IP header |
---|
697 | * @param[out] skipped An output variable of the number of bytes skipped |
---|
698 | * |
---|
699 | * @returns a pointer to the ICMP header, or NULL if this is not a ICMP packet |
---|
700 | * |
---|
701 | * Skipped can be NULL, in which case it will be ignored by the program. |
---|
702 | */ |
---|
703 | struct libtrace_icmp *get_icmp_from_ip(struct libtrace_ip *ip, int *skipped) |
---|
704 | { |
---|
705 | struct libtrace_icmp *icmpptr = 0; |
---|
706 | |
---|
707 | if ((ip->ip_p == 6) && ((ip->ip_off & SW_IP_OFFMASK) == 0)) { |
---|
708 | icmpptr = (struct libtrace_icmp *)((ptrdiff_t)ip+ (ip->ip_hl * 4)); |
---|
709 | } |
---|
710 | |
---|
711 | if (skipped) |
---|
712 | *skipped=(ip->ip_hl*4); |
---|
713 | |
---|
714 | return icmpptr; |
---|
715 | } |
---|
716 | /** parse an ip or tcp option |
---|
717 | * @param[in,out] ptr the pointer to the current option |
---|
718 | * @param[in,out] len the length of the remaining buffer |
---|
719 | * @param[out] type the type of the option |
---|
720 | * @param[out] optlen the length of the option |
---|
721 | * @param[out] data the data of the option |
---|
722 | * |
---|
723 | * @returns bool true if there is another option (and the fields are filled in) |
---|
724 | * or false if this was the last option. |
---|
725 | * |
---|
726 | * This updates ptr to point to the next option after this one, and updates |
---|
727 | * len to be the number of bytes remaining in the options area. Type is updated |
---|
728 | * to be the code of this option, and data points to the data of this option, |
---|
729 | * with optlen saying how many bytes there are. |
---|
730 | * |
---|
731 | * @note Beware of fragmented packets. |
---|
732 | * @author Perry Lorier |
---|
733 | */ |
---|
734 | int trace_get_next_option(unsigned char **ptr,int *len, |
---|
735 | unsigned char *type, |
---|
736 | unsigned char *optlen, |
---|
737 | unsigned char **data) |
---|
738 | { |
---|
739 | if (*len<=0) |
---|
740 | return 0; |
---|
741 | *type=**ptr; |
---|
742 | switch(*type) { |
---|
743 | case 0: /* End of options */ |
---|
744 | return 0; |
---|
745 | case 1: /* Pad */ |
---|
746 | (*ptr)++; |
---|
747 | (*len)--; |
---|
748 | return 1; |
---|
749 | default: |
---|
750 | *optlen = *(*ptr+1); |
---|
751 | if (*optlen<2) |
---|
752 | return 0; // I have no idea wtf is going on |
---|
753 | // with these packets |
---|
754 | (*len)-=*optlen; |
---|
755 | (*data)=(*ptr+2); |
---|
756 | (*ptr)+=*optlen; |
---|
757 | if (*len<0) |
---|
758 | return 0; |
---|
759 | return 1; |
---|
760 | } |
---|
761 | assert(0); |
---|
762 | } |
---|
763 | |
---|
764 | |
---|
765 | /** Get the current time in DAG time format |
---|
766 | * @param packet a pointer to a libtrace_packet structure |
---|
767 | * @returns a 64 bit timestamp in DAG ERF format (upper 32 bits are the seconds |
---|
768 | * past 1970-01-01, the lower 32bits are partial seconds) |
---|
769 | * @author Daniel Lawson |
---|
770 | */ |
---|
771 | uint64_t trace_get_erf_timestamp(const struct libtrace_packet_t *packet) { |
---|
772 | uint64_t timestamp = 0; |
---|
773 | struct timeval ts; |
---|
774 | |
---|
775 | if (packet->trace->format->get_erf_timestamp) { |
---|
776 | timestamp = packet->trace->format->get_erf_timestamp(packet); |
---|
777 | } else if (packet->trace->format->get_timeval) { |
---|
778 | ts = packet->trace->format->get_timeval(packet); |
---|
779 | timestamp = ((((uint64_t)ts.tv_sec) << 32) + \ |
---|
780 | (((uint64_t)ts.tv_usec * UINT_MAX)/1000000)); |
---|
781 | } |
---|
782 | return timestamp; |
---|
783 | } |
---|
784 | |
---|
785 | /** Get the current time in struct timeval |
---|
786 | * @param packet a pointer to a libtrace_packet structure |
---|
787 | * |
---|
788 | * @returns time that this packet was seen in a struct timeval |
---|
789 | * @author Daniel Lawson |
---|
790 | * @author Perry Lorier |
---|
791 | */ |
---|
792 | struct timeval trace_get_timeval(const struct libtrace_packet_t *packet) { |
---|
793 | struct timeval tv; |
---|
794 | uint64_t ts = 0; |
---|
795 | |
---|
796 | if (packet->trace->format->get_timeval) { |
---|
797 | tv = packet->trace->format->get_timeval(packet); |
---|
798 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
799 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
800 | #if __BYTE_ORDER == __BIG_ENDIAN |
---|
801 | tv.tv_sec = ts & 0xFFFFFFFF; |
---|
802 | #elif __BYTE_ORDER == __LITTLE_ENDIAN |
---|
803 | tv.tv_sec = ts >> 32; |
---|
804 | #else |
---|
805 | #error "What on earth are you running this on?" |
---|
806 | #endif |
---|
807 | ts = (1000000 * (ts & 0xffffffffULL)); |
---|
808 | ts += (ts & 0x80000000ULL) << 1; |
---|
809 | tv.tv_usec = ts >> 32; |
---|
810 | if (tv.tv_usec >= 1000000) { |
---|
811 | tv.tv_usec -= 1000000; |
---|
812 | tv.tv_sec += 1; |
---|
813 | } |
---|
814 | } |
---|
815 | |
---|
816 | return tv; |
---|
817 | } |
---|
818 | |
---|
819 | /** Get the current time in floating point seconds |
---|
820 | * @param packet a pointer to a libtrace_packet structure |
---|
821 | * @returns time that this packet was seen in 64bit floating point seconds |
---|
822 | * @author Perry Lorier |
---|
823 | */ |
---|
824 | double trace_get_seconds(const struct libtrace_packet_t *packet) { |
---|
825 | double seconds; |
---|
826 | uint64_t ts; |
---|
827 | |
---|
828 | if (packet->trace->format->get_seconds) { |
---|
829 | seconds = packet->trace->format->get_seconds(packet); |
---|
830 | } else if (packet->trace->format->get_erf_timestamp) { |
---|
831 | ts = packet->trace->format->get_erf_timestamp(packet); |
---|
832 | seconds = (ts>>32) + ((ts & UINT_MAX)*1.0 / UINT_MAX); |
---|
833 | } |
---|
834 | return seconds; |
---|
835 | } |
---|
836 | |
---|
837 | /** Get the size of the packet in the trace |
---|
838 | * @param packet the packet opaque pointer |
---|
839 | * @returns the size of the packet in the trace |
---|
840 | * @author Perry Lorier |
---|
841 | * @note Due to this being a header capture, or anonymisation, this may not |
---|
842 | * be the same size as the original packet. See trace_get_wire_length() for the |
---|
843 | * original size of the packet. |
---|
844 | * @note This can (and often is) different for different packets in a trace! |
---|
845 | * @par |
---|
846 | * This is sometimes called the "snaplen". |
---|
847 | */ |
---|
848 | int trace_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
849 | |
---|
850 | if (packet->trace->format->get_capture_length) { |
---|
851 | return packet->trace->format->get_capture_length(packet); |
---|
852 | } |
---|
853 | return -1; |
---|
854 | } |
---|
855 | |
---|
856 | /** Get the size of the packet as it was seen on the wire. |
---|
857 | * @param packet a pointer to a libtrace_packet structure |
---|
858 | * |
---|
859 | * @returns the size of the packet as it was on the wire. |
---|
860 | * @author Perry Lorier |
---|
861 | * @author Daniel Lawson |
---|
862 | * @note Due to the trace being a header capture, or anonymisation this may |
---|
863 | * not be the same as the Capture Len. |
---|
864 | */ |
---|
865 | int trace_get_wire_length(const struct libtrace_packet_t *packet){ |
---|
866 | if (packet->trace->format->get_wire_length) { |
---|
867 | return packet->trace->format->get_wire_length(packet); |
---|
868 | } |
---|
869 | return -1; |
---|
870 | |
---|
871 | } |
---|
872 | |
---|
873 | /** Get the type of the link layer |
---|
874 | * @param packet a pointer to a libtrace_packet structure |
---|
875 | * @returns libtrace_linktype_t |
---|
876 | * @author Perry Lorier |
---|
877 | * @author Daniel Lawson |
---|
878 | */ |
---|
879 | libtrace_linktype_t trace_get_link_type(const struct libtrace_packet_t *packet ) { |
---|
880 | if (packet->trace->format->get_link_type) { |
---|
881 | return packet->trace->format->get_link_type(packet); |
---|
882 | } |
---|
883 | return -1; |
---|
884 | } |
---|
885 | |
---|
886 | /** Get the source MAC addres |
---|
887 | * @param packet a pointer to a libtrace_packet structure |
---|
888 | * @returns a pointer to the source mac, (or NULL if there is no source MAC) |
---|
889 | * @author Perry Lorier |
---|
890 | */ |
---|
891 | uint8_t *trace_get_source_mac(const struct libtrace_packet_t *packet) { |
---|
892 | void *link = trace_get_link(packet); |
---|
893 | struct ieee_802_11_header *wifi = link; |
---|
894 | struct ether_header *ethptr = link; |
---|
895 | if (!link) |
---|
896 | return NULL; |
---|
897 | switch (trace_get_link_type(packet)) { |
---|
898 | case TRACE_TYPE_80211: |
---|
899 | return (uint8_t*)&wifi->mac2; |
---|
900 | case TRACE_TYPE_ETH: |
---|
901 | return (uint8_t*)ðptr->ether_shost; |
---|
902 | default: |
---|
903 | fprintf(stderr,"Not implemented\n"); |
---|
904 | assert(0); |
---|
905 | } |
---|
906 | } |
---|
907 | |
---|
908 | /** Get the destination MAC addres |
---|
909 | * @param packet a libtrace_packet pointer |
---|
910 | * @returns a pointer to the destination mac, (or NULL if there is no |
---|
911 | * destination MAC) |
---|
912 | * @author Perry Lorier |
---|
913 | */ |
---|
914 | uint8_t *trace_get_destination_mac(const struct libtrace_packet_t *packet) { |
---|
915 | void *link = trace_get_link(packet); |
---|
916 | struct ieee_802_11_header *wifi = link; |
---|
917 | struct ether_header *ethptr = link; |
---|
918 | if (!link) |
---|
919 | return NULL; |
---|
920 | switch (trace_get_link_type(packet)) { |
---|
921 | case TRACE_TYPE_80211: |
---|
922 | return (uint8_t*)&wifi->mac1; |
---|
923 | case TRACE_TYPE_ETH: |
---|
924 | return (uint8_t*)ðptr->ether_dhost; |
---|
925 | default: |
---|
926 | fprintf(stderr,"Not implemented\n"); |
---|
927 | assert(0); |
---|
928 | } |
---|
929 | } |
---|
930 | |
---|
931 | |
---|
932 | /** process a libtrace event |
---|
933 | * @param trace the libtrace opaque pointer |
---|
934 | * @param packet the libtrace_packet opaque pointer |
---|
935 | * @returns |
---|
936 | * TRACE_EVENT_IOWAIT Waiting on I/O on fd |
---|
937 | * TRACE_EVENT_SLEEP Next event in seconds |
---|
938 | * TRACE_EVENT_PACKET Packet arrived in buffer with size size |
---|
939 | * TRACE_EVENT_TERMINATE Trace terminated (perhaps with an error condition) |
---|
940 | * FIXME currently keeps a copy of the packet inside the trace pointer, |
---|
941 | * which in turn is stored inside the new packet object... |
---|
942 | * @author Perry Lorier |
---|
943 | */ |
---|
944 | struct libtrace_eventobj_t trace_event(struct libtrace_t *trace, |
---|
945 | struct libtrace_packet_t *packet) { |
---|
946 | struct libtrace_eventobj_t event; |
---|
947 | |
---|
948 | if (!trace) { |
---|
949 | fprintf(stderr,"You called trace_event() with a NULL trace object!\n"); |
---|
950 | } |
---|
951 | assert(trace); |
---|
952 | assert(packet); |
---|
953 | |
---|
954 | /* Store the trace we are reading from into the packet opaque |
---|
955 | * structure */ |
---|
956 | packet->trace = trace; |
---|
957 | |
---|
958 | if (packet->trace->format->trace_event) { |
---|
959 | return packet->trace->format->trace_event(trace,packet); |
---|
960 | } else { |
---|
961 | return event; |
---|
962 | } |
---|
963 | |
---|
964 | } |
---|
965 | |
---|
966 | /** setup a BPF filter |
---|
967 | * @param filterstring a char * containing the bpf filter string |
---|
968 | * @returns opaque pointer pointer to a libtrace_filter_t object |
---|
969 | * @author Daniel Lawson |
---|
970 | */ |
---|
971 | struct libtrace_filter_t *trace_bpf_setfilter(const char *filterstring) { |
---|
972 | #if HAVE_BPF |
---|
973 | struct libtrace_filter_t *filter = malloc(sizeof(struct libtrace_filter_t)); |
---|
974 | filter->filterstring = strdup(filterstring); |
---|
975 | filter->filter = 0; |
---|
976 | return filter; |
---|
977 | #else |
---|
978 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
979 | return 0; |
---|
980 | #endif |
---|
981 | } |
---|
982 | |
---|
983 | /** apply a BPF filter |
---|
984 | * @param filter the filter opaque pointer |
---|
985 | * @param packet the packet opaque pointer |
---|
986 | * @returns 0 if the filter fails, 1 if it succeeds |
---|
987 | * @author Daniel Lawson |
---|
988 | */ |
---|
989 | int trace_bpf_filter(struct libtrace_filter_t *filter, |
---|
990 | const struct libtrace_packet_t *packet) { |
---|
991 | #if HAVE_BPF |
---|
992 | void *linkptr = 0; |
---|
993 | int clen = 0; |
---|
994 | assert(filter); |
---|
995 | assert(packet); |
---|
996 | linkptr = trace_get_link(packet); |
---|
997 | if (!linkptr) { |
---|
998 | return 0; |
---|
999 | } |
---|
1000 | |
---|
1001 | clen = trace_get_capture_length(packet); |
---|
1002 | |
---|
1003 | |
---|
1004 | if (filter->filterstring && ! filter->filter) { |
---|
1005 | pcap_t *pcap; |
---|
1006 | struct bpf_program bpfprog; |
---|
1007 | |
---|
1008 | switch (trace_get_link_type(packet)) { |
---|
1009 | case TRACE_TYPE_ETH: |
---|
1010 | pcap = (pcap_t *)pcap_open_dead(DLT_EN10MB, 1500); |
---|
1011 | break; |
---|
1012 | #ifdef DLT_LINUX_SLL |
---|
1013 | case TRACE_TYPE_LINUX_SLL: |
---|
1014 | pcap = (pcap_t *)pcap_open_dead(DLT_LINUX_SLL, 1500); |
---|
1015 | break; |
---|
1016 | #endif |
---|
1017 | #ifdef DLT_PFLOG |
---|
1018 | case TRACE_TYPE_PFLOG: |
---|
1019 | pcap = (pcap_t *)pcap_open_dead(DLT_PFLOG, 1500); |
---|
1020 | break; |
---|
1021 | #endif |
---|
1022 | default: |
---|
1023 | printf("only works for ETH and LINUX_SLL (ppp) at the moment\n"); |
---|
1024 | assert(0); |
---|
1025 | } |
---|
1026 | |
---|
1027 | // build filter |
---|
1028 | if (pcap_compile( pcap, &bpfprog, filter->filterstring, 1, 0)) { |
---|
1029 | printf("bpf compilation error: %s: %s\n", |
---|
1030 | pcap_geterr(pcap),filter->filterstring); |
---|
1031 | assert(0); |
---|
1032 | } |
---|
1033 | pcap_close(pcap); |
---|
1034 | filter->filter = bpfprog.bf_insns; |
---|
1035 | } |
---|
1036 | |
---|
1037 | assert(filter->filter); |
---|
1038 | return bpf_filter(filter->filter, linkptr, clen, clen); |
---|
1039 | #else |
---|
1040 | fprintf(stderr,"This version of libtrace does not have bpf filter support\n"); |
---|
1041 | return 0; |
---|
1042 | #endif |
---|
1043 | } |
---|
1044 | |
---|
1045 | /** Set the direction flag, if it has one |
---|
1046 | * @param packet the packet opaque pointer |
---|
1047 | * @param direction the new direction (0,1,2,3) |
---|
1048 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1049 | * @author Daniel Lawson |
---|
1050 | */ |
---|
1051 | int8_t trace_set_direction(struct libtrace_packet_t *packet, int8_t direction) { |
---|
1052 | assert(packet); |
---|
1053 | if (packet->trace->format->set_direction) { |
---|
1054 | return packet->trace->format->set_direction(packet,direction); |
---|
1055 | } |
---|
1056 | return -1; |
---|
1057 | } |
---|
1058 | |
---|
1059 | /** Get the direction flag, if it has one |
---|
1060 | * @param packet a pointer to a libtrace_packet structure |
---|
1061 | * @returns a signed value containing the direction flag, or -1 if this is not supported |
---|
1062 | * The direction is defined as 0 for packets originating locally (ie, outbound) |
---|
1063 | * and 1 for packets originating remotely (ie, inbound). |
---|
1064 | * Other values are possible, which might be overloaded to mean special things |
---|
1065 | * for a special trace. |
---|
1066 | * @author Daniel Lawson |
---|
1067 | */ |
---|
1068 | int8_t trace_get_direction(const struct libtrace_packet_t *packet) { |
---|
1069 | assert(packet); |
---|
1070 | if (packet->trace->format->get_direction) { |
---|
1071 | return packet->trace->format->get_direction(packet); |
---|
1072 | } |
---|
1073 | return -1; |
---|
1074 | } |
---|
1075 | |
---|
1076 | struct ports_t { |
---|
1077 | uint16_t src; |
---|
1078 | uint16_t dst; |
---|
1079 | }; |
---|
1080 | |
---|
1081 | /* Return the client port |
---|
1082 | */ |
---|
1083 | uint16_t trace_get_source_port(const struct libtrace_packet_t *packet) |
---|
1084 | { |
---|
1085 | struct libtrace_ip *ip = trace_get_ip(packet); |
---|
1086 | struct ports_t *port; |
---|
1087 | if (6 != ip->ip_p |
---|
1088 | && 17 != ip->ip_p) |
---|
1089 | return 0; |
---|
1090 | if (0 != (ip->ip_off & SW_IP_OFFMASK)) |
---|
1091 | return 0; |
---|
1092 | |
---|
1093 | port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4)); |
---|
1094 | |
---|
1095 | return htons(port->src); |
---|
1096 | } |
---|
1097 | |
---|
1098 | /* Same as get_source_port except use the destination port */ |
---|
1099 | uint16_t trace_get_destination_port(const struct libtrace_packet_t *packet) |
---|
1100 | { |
---|
1101 | struct libtrace_ip *ip = trace_get_ip(packet); |
---|
1102 | struct ports_t *port; |
---|
1103 | |
---|
1104 | if (6 != ip->ip_p |
---|
1105 | && 17 != ip->ip_p) |
---|
1106 | return 0; |
---|
1107 | |
---|
1108 | if (0 != (ip->ip_off & SW_IP_OFFMASK)) |
---|
1109 | return 0; |
---|
1110 | |
---|
1111 | port = (struct ports_t *)((ptrdiff_t)ip + (ip->ip_hl * 4)); |
---|
1112 | |
---|
1113 | return htons(port->dst); |
---|
1114 | } |
---|
1115 | |
---|
1116 | #define ROOT_SERVER(x) ((x) < 512) |
---|
1117 | #define ROOT_CLIENT(x) ((512 <= (x)) && ((x) < 1024)) |
---|
1118 | #define NONROOT_SERVER(x) ((x) >= 5000) |
---|
1119 | #define NONROOT_CLIENT(x) ((1024 <= (x)) && ((x) < 5000)) |
---|
1120 | #define DYNAMIC(x) ((49152 < (x)) && ((x) < 65535)) |
---|
1121 | #define SERVER(x) ROOT_SERVER(x) || NONROOT_SERVER(x) |
---|
1122 | #define CLIENT(x) ROOT_CLIENT(x) || NONROOT_CLIENT(x) |
---|
1123 | |
---|
1124 | /* Attempt to deduce the 'server' port |
---|
1125 | * @param protocol the IP protocol (eg, 6 or 17 for TCP or UDP) |
---|
1126 | * @param source the TCP or UDP source port |
---|
1127 | * @param dest the TCP or UDP destination port |
---|
1128 | * @returns a hint as to which port is the server port |
---|
1129 | * @author Daniel Lawson |
---|
1130 | */ |
---|
1131 | int8_t trace_get_server_port(uint8_t protocol, uint16_t source, uint16_t dest) { |
---|
1132 | /* |
---|
1133 | * * If the ports are equal, return DEST |
---|
1134 | * * Check for well-known ports in the given protocol |
---|
1135 | * * Root server ports: 0 - 511 |
---|
1136 | * * Root client ports: 512 - 1023 |
---|
1137 | * * non-root client ports: 1024 - 4999 |
---|
1138 | * * non-root server ports: 5000+ |
---|
1139 | * * Check for static ranges: 1024 - 49151 |
---|
1140 | * * Check for dynamic ranges: 49152 - 65535 |
---|
1141 | * * flip a coin. |
---|
1142 | */ |
---|
1143 | |
---|
1144 | uint16_t server, client; |
---|
1145 | |
---|
1146 | /* equal */ |
---|
1147 | if (source == client) |
---|
1148 | return USE_DEST; |
---|
1149 | |
---|
1150 | /* root server port, 0 - 511 */ |
---|
1151 | if (ROOT_SERVER(source) && ROOT_SERVER(dest)) { |
---|
1152 | if (source < dest) |
---|
1153 | return USE_SOURCE; |
---|
1154 | return USE_DEST; |
---|
1155 | } |
---|
1156 | |
---|
1157 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1158 | return USE_SOURCE; |
---|
1159 | if (!ROOT_SERVER(source) && ROOT_SERVER(dest)) |
---|
1160 | return USE_DEST; |
---|
1161 | |
---|
1162 | /* non-root server */ |
---|
1163 | if (NONROOT_SERVER(source) && NONROOT_SERVER(dest)) { |
---|
1164 | if (source < dest) |
---|
1165 | return USE_SOURCE; |
---|
1166 | return USE_DEST; |
---|
1167 | } |
---|
1168 | if (NONROOT_SERVER(source) && !NONROOT_SERVER(dest)) |
---|
1169 | return USE_SOURCE; |
---|
1170 | if (!NONROOT_SERVER(source) && NONROOT_SERVER(dest)) |
---|
1171 | return USE_DEST; |
---|
1172 | |
---|
1173 | /* root client */ |
---|
1174 | if (ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1175 | if (source < dest) |
---|
1176 | return USE_SOURCE; |
---|
1177 | return USE_DEST; |
---|
1178 | } |
---|
1179 | if (ROOT_CLIENT(source) && !ROOT_CLIENT(dest)) { |
---|
1180 | /* prefer root-client over nonroot-client */ |
---|
1181 | if (NONROOT_CLIENT(dest)) |
---|
1182 | return USE_SOURCE; |
---|
1183 | return USE_DEST; |
---|
1184 | } |
---|
1185 | if (!ROOT_CLIENT(source) && ROOT_CLIENT(dest)) { |
---|
1186 | /* prefer root-client over nonroot-client */ |
---|
1187 | if (NONROOT_CLIENT(source)) |
---|
1188 | return USE_DEST; |
---|
1189 | return USE_SOURCE; |
---|
1190 | } |
---|
1191 | |
---|
1192 | /* nonroot client */ |
---|
1193 | if (NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) { |
---|
1194 | if (source < dest) |
---|
1195 | return USE_SOURCE; |
---|
1196 | return USE_DEST; |
---|
1197 | } |
---|
1198 | if (NONROOT_CLIENT(source) && !NONROOT_CLIENT(dest)) |
---|
1199 | return USE_DEST; |
---|
1200 | if (!NONROOT_CLIENT(source) && NONROOT_CLIENT(dest)) |
---|
1201 | return USE_SOURCE; |
---|
1202 | |
---|
1203 | /* dynamic range */ |
---|
1204 | if (DYNAMIC(source) && DYNAMIC(dest)) |
---|
1205 | if (source < dest) |
---|
1206 | return USE_SOURCE; |
---|
1207 | return USE_DEST; |
---|
1208 | if (DYNAMIC(source) && !DYNAMIC(dest)) |
---|
1209 | return USE_DEST; |
---|
1210 | if (!DYNAMIC(source) && DYNAMIC(dest)) |
---|
1211 | return USE_SOURCE; |
---|
1212 | /* |
---|
1213 | if (SERVER(source) && CLIENT(dest)) |
---|
1214 | return USE_SOURCE; |
---|
1215 | |
---|
1216 | if (SERVER(dest) && CLIENT(source)) |
---|
1217 | return USE_DEST; |
---|
1218 | if (ROOT_SERVER(source) && !ROOT_SERVER(dest)) |
---|
1219 | return USE_SOURCE; |
---|
1220 | if (ROOT_SERVER(dest) && !ROOT_SERVER(source)) |
---|
1221 | return USE_DEST; |
---|
1222 | */ |
---|
1223 | // failing that test... |
---|
1224 | if (source < dest) { |
---|
1225 | return USE_SOURCE; |
---|
1226 | } |
---|
1227 | return USE_DEST; |
---|
1228 | |
---|
1229 | } |
---|
1230 | |
---|
1231 | /** Truncate the packet at the suggested length |
---|
1232 | * @param packet the packet opaque pointer |
---|
1233 | * @param size the new length of the packet |
---|
1234 | * @returns the new length of the packet, or the original length of the |
---|
1235 | * packet if unchanged |
---|
1236 | * NOTE: len refers to the network-level payload of the packet, and not |
---|
1237 | * any capture headers included as well. For example, to truncate a packet |
---|
1238 | * after the IP header, set scan to sizeof(ethernet_header) + sizeof(ip_header) |
---|
1239 | * @author Daniel Lawson |
---|
1240 | */ |
---|
1241 | size_t trace_set_capture_length(struct libtrace_packet_t *packet, size_t size) { |
---|
1242 | assert(packet); |
---|
1243 | |
---|
1244 | if (size > packet->size) { |
---|
1245 | // can't make a packet larger |
---|
1246 | return packet->size; |
---|
1247 | } |
---|
1248 | if (packet->trace->format->truncate_packet) { |
---|
1249 | return packet->trace->format->truncate_packet(packet,size); |
---|
1250 | } |
---|
1251 | return -1; |
---|
1252 | } |
---|
1253 | |
---|