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 | #include "common.h" |
---|
32 | #include "config.h" |
---|
33 | #include "libtrace.h" |
---|
34 | #include "libtrace_int.h" |
---|
35 | #include "format_helper.h" |
---|
36 | |
---|
37 | #ifdef HAVE_INTTYPES_H |
---|
38 | # include <inttypes.h> |
---|
39 | #else |
---|
40 | # error "Can't find inttypes.h - this needs to be fixed" |
---|
41 | #endif |
---|
42 | |
---|
43 | #include <sys/types.h> |
---|
44 | #include <sys/stat.h> |
---|
45 | #include <unistd.h> |
---|
46 | #include <assert.h> |
---|
47 | #include <stdio.h> |
---|
48 | #include <stdlib.h> |
---|
49 | #include <string.h> |
---|
50 | |
---|
51 | #if HAVE_PCAP_BPF_H |
---|
52 | # include <pcap-bpf.h> |
---|
53 | #else |
---|
54 | # ifdef HAVE_NET_BPF_H |
---|
55 | # include <net/bpf.h> |
---|
56 | # endif |
---|
57 | #endif |
---|
58 | |
---|
59 | #if HAVE_PCAP_H |
---|
60 | # include <pcap.h> |
---|
61 | # ifdef HAVE_PCAP_INT_H |
---|
62 | # include <pcap-int.h> |
---|
63 | # endif |
---|
64 | #endif |
---|
65 | |
---|
66 | #if HAVE_PCAP |
---|
67 | |
---|
68 | static struct libtrace_format_t *pcap_ptr = 0; |
---|
69 | static struct libtrace_format_t *pcapint_ptr = 0; |
---|
70 | |
---|
71 | #define CONNINFO libtrace->format_data->conn_info |
---|
72 | #define INPUT libtrace->format_data->input |
---|
73 | #define OUTPUT libtrace->format_data->output |
---|
74 | struct libtrace_format_data_t { |
---|
75 | union { |
---|
76 | char *path; /**< information for local sockets */ |
---|
77 | char *interface; /**< intormation for reading of network |
---|
78 | interfaces */ |
---|
79 | } conn_info; |
---|
80 | /** Information about the current state of the input device */ |
---|
81 | union { |
---|
82 | pcap_t *pcap; |
---|
83 | } input; |
---|
84 | }; |
---|
85 | |
---|
86 | struct libtrace_format_data_out_t { |
---|
87 | union { |
---|
88 | char *path; |
---|
89 | char *interface; |
---|
90 | } conn_info; |
---|
91 | union { |
---|
92 | struct { |
---|
93 | pcap_t *pcap; |
---|
94 | pcap_dumper_t *dump; |
---|
95 | } trace; |
---|
96 | |
---|
97 | } output; |
---|
98 | }; |
---|
99 | |
---|
100 | static int linktype_to_dlt(libtrace_linktype_t t) { |
---|
101 | static int table[] = { |
---|
102 | -1, /* LEGACY */ |
---|
103 | -1, /* HDLC over POS */ |
---|
104 | DLT_EN10MB, /* Ethernet */ |
---|
105 | -1, /* ATM */ |
---|
106 | DLT_IEEE802_11, /* 802.11 */ |
---|
107 | -1 /* END OF TABLE */ |
---|
108 | }; |
---|
109 | if (t>sizeof(table)/sizeof(*table)) { |
---|
110 | return -1; |
---|
111 | } |
---|
112 | return table[t]; |
---|
113 | } |
---|
114 | |
---|
115 | static int pcap_init_input(struct libtrace_t *libtrace) { |
---|
116 | char errbuf[PCAP_ERRBUF_SIZE]; |
---|
117 | struct stat buf; |
---|
118 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
119 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
120 | CONNINFO.path = libtrace->uridata; |
---|
121 | |
---|
122 | libtrace->sourcetype = TRACE; |
---|
123 | if (!strncmp(CONNINFO.path,"-",1)) { |
---|
124 | if ((INPUT.pcap = |
---|
125 | pcap_open_offline(CONNINFO.path, |
---|
126 | errbuf)) == NULL) { |
---|
127 | fprintf(stderr,"%s\n",errbuf); |
---|
128 | return 0; |
---|
129 | } |
---|
130 | } else { |
---|
131 | if (stat(CONNINFO.path,&buf) == -1) { |
---|
132 | perror("stat"); |
---|
133 | return 0; |
---|
134 | } |
---|
135 | if (S_ISCHR(buf.st_mode)) { |
---|
136 | if ((INPUT.pcap = |
---|
137 | pcap_open_live(CONNINFO.path, |
---|
138 | 4096, |
---|
139 | 1, |
---|
140 | 1, |
---|
141 | errbuf)) == NULL) { |
---|
142 | fprintf(stderr,"%s\n",errbuf); |
---|
143 | return 0; |
---|
144 | } |
---|
145 | } else { |
---|
146 | if ((INPUT.pcap = |
---|
147 | pcap_open_offline(CONNINFO.path, |
---|
148 | errbuf)) == NULL) { |
---|
149 | fprintf(stderr,"%s\n",errbuf); |
---|
150 | return 0; |
---|
151 | } |
---|
152 | } |
---|
153 | } |
---|
154 | //fprintf(stderr, "Unsupported scheme (%s) for format pcap\n", |
---|
155 | // CONNINFO.path); |
---|
156 | return 1; |
---|
157 | |
---|
158 | } |
---|
159 | |
---|
160 | static int pcap_init_output(struct libtrace_out_t *libtrace) { |
---|
161 | libtrace->format_data = (struct libtrace_format_data_out_t *) |
---|
162 | malloc(sizeof(struct libtrace_format_data_out_t)); |
---|
163 | CONNINFO.path = libtrace->uridata; |
---|
164 | OUTPUT.trace.pcap = NULL; |
---|
165 | OUTPUT.trace.dump = NULL; |
---|
166 | return 1; |
---|
167 | } |
---|
168 | |
---|
169 | static int pcapint_init_input(struct libtrace_t *libtrace) { |
---|
170 | char errbuf[PCAP_ERRBUF_SIZE]; |
---|
171 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
172 | malloc(sizeof(struct libtrace_format_data_t)); |
---|
173 | CONNINFO.path = libtrace->uridata; |
---|
174 | libtrace->sourcetype = INTERFACE; |
---|
175 | if ((INPUT.pcap = |
---|
176 | pcap_open_live(CONNINFO.path, |
---|
177 | 4096, |
---|
178 | 1, |
---|
179 | 1, |
---|
180 | errbuf)) == NULL) { |
---|
181 | fprintf(stderr,"%s\n",errbuf); |
---|
182 | return 0; |
---|
183 | } |
---|
184 | return 1; |
---|
185 | } |
---|
186 | |
---|
187 | static int pcapint_init_output(struct libtrace_out_t *libtrace __attribute__((unused))) { |
---|
188 | return -1; |
---|
189 | } |
---|
190 | |
---|
191 | static int pcap_fin_input(struct libtrace_t *libtrace) { |
---|
192 | pcap_close(INPUT.pcap); |
---|
193 | free(libtrace->format_data); |
---|
194 | return 0; |
---|
195 | } |
---|
196 | |
---|
197 | static int pcap_fin_output(struct libtrace_out_t *libtrace) { |
---|
198 | pcap_dump_flush(OUTPUT.trace.dump); |
---|
199 | pcap_dump_close(OUTPUT.trace.dump); |
---|
200 | return 0; |
---|
201 | } |
---|
202 | |
---|
203 | static int pcapint_fin_output(struct libtrace_out_t *libtrace __attribute__((unused))) { |
---|
204 | return -1; |
---|
205 | } |
---|
206 | |
---|
207 | static void trace_pcap_handler(u_char *user, const struct pcap_pkthdr *pcaphdr, const u_char *pcappkt) { |
---|
208 | struct libtrace_packet_t *packet = (struct libtrace_packet_t *)user; |
---|
209 | void *buffer = packet->buffer; |
---|
210 | int numbytes = 0; |
---|
211 | |
---|
212 | memcpy(buffer,pcaphdr,sizeof(struct pcap_pkthdr)); |
---|
213 | numbytes = pcaphdr->len; |
---|
214 | memcpy(buffer + sizeof(struct pcap_pkthdr),pcappkt,numbytes); |
---|
215 | |
---|
216 | packet->size = numbytes + sizeof(struct pcap_pkthdr); |
---|
217 | } |
---|
218 | |
---|
219 | static int pcap_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
220 | int pcapbytes = 0; |
---|
221 | |
---|
222 | pcapbytes = pcap_dispatch(INPUT.pcap, |
---|
223 | 1, /* number of packets */ |
---|
224 | &trace_pcap_handler, |
---|
225 | (u_char *)packet); |
---|
226 | |
---|
227 | if (pcapbytes <= 0) { |
---|
228 | return pcapbytes; |
---|
229 | } |
---|
230 | packet->status = 0; |
---|
231 | return (packet->size - sizeof(struct pcap_pkthdr)); |
---|
232 | } |
---|
233 | |
---|
234 | static int pcap_write_packet(struct libtrace_out_t *libtrace, const struct libtrace_packet_t *packet) { |
---|
235 | struct pcap_pkthdr pcap_pkt_hdr; |
---|
236 | void *link = trace_get_link(packet); |
---|
237 | |
---|
238 | if (!OUTPUT.trace.pcap) { |
---|
239 | OUTPUT.trace.pcap = pcap_open_dead( |
---|
240 | linktype_to_dlt(trace_get_link_type(packet)), |
---|
241 | 65536); |
---|
242 | OUTPUT.trace.dump = pcap_dump_open(OUTPUT.trace.pcap,CONNINFO.path); |
---|
243 | fflush((FILE *)OUTPUT.trace.dump); |
---|
244 | } |
---|
245 | if (packet->trace->format == pcap_ptr || |
---|
246 | packet->trace->format == pcapint_ptr) { |
---|
247 | //if (!strncasecmp(packet->trace->format->name,"pcap",4)) { |
---|
248 | // this is a pcap trace anyway |
---|
249 | |
---|
250 | pcap_dump((u_char*)OUTPUT.trace.dump,(struct pcap_pkthdr *)packet->buffer,link); |
---|
251 | } else { |
---|
252 | pcap_pkt_hdr.ts = trace_get_timeval(packet); |
---|
253 | pcap_pkt_hdr.caplen = trace_get_capture_length(packet); |
---|
254 | pcap_pkt_hdr.len = trace_get_wire_length(packet); |
---|
255 | |
---|
256 | pcap_dump((u_char*)OUTPUT.trace.dump, &pcap_pkt_hdr, link); |
---|
257 | } |
---|
258 | return 0; |
---|
259 | } |
---|
260 | |
---|
261 | static int pcapint_write_packet(struct libtrace_out_t *libtrace __attribute__((unused)), struct libtrace_packet_t *packet __attribute__((unused))) { |
---|
262 | // void *link = trace_get_link(packet); |
---|
263 | |
---|
264 | return 0; |
---|
265 | } |
---|
266 | |
---|
267 | static void *pcap_get_link(const struct libtrace_packet_t *packet) { |
---|
268 | return (void *) (packet->buffer + sizeof(struct pcap_pkthdr)); |
---|
269 | } |
---|
270 | |
---|
271 | static libtrace_linktype_t pcap_get_link_type(const struct libtrace_packet_t *packet) { |
---|
272 | struct pcap_pkthdr *pcapptr = 0; |
---|
273 | int linktype = 0; |
---|
274 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
275 | linktype = pcap_datalink(packet->trace->format_data->input.pcap); |
---|
276 | switch(linktype) { |
---|
277 | case DLT_NULL: |
---|
278 | return TRACE_TYPE_NONE; |
---|
279 | case DLT_EN10MB: |
---|
280 | return TRACE_TYPE_ETH; |
---|
281 | case DLT_ATM_RFC1483: |
---|
282 | return TRACE_TYPE_ATM; |
---|
283 | case DLT_IEEE802_11: |
---|
284 | return TRACE_TYPE_80211; |
---|
285 | #ifdef DLT_LINUX_SLL |
---|
286 | case DLT_LINUX_SLL: |
---|
287 | return TRACE_TYPE_LINUX_SLL; |
---|
288 | #endif |
---|
289 | #ifdef DLT_PFLOG |
---|
290 | case DLT_PFLOG: |
---|
291 | return TRACE_TYPE_PFLOG; |
---|
292 | #endif |
---|
293 | } |
---|
294 | return -1; |
---|
295 | } |
---|
296 | |
---|
297 | static int8_t pcap_get_direction(const struct libtrace_packet_t *packet) { |
---|
298 | int8_t direction = -1; |
---|
299 | switch(pcap_get_link_type(packet)) { |
---|
300 | case TRACE_TYPE_LINUX_SLL: |
---|
301 | { |
---|
302 | struct trace_sll_header_t *sll; |
---|
303 | sll = trace_get_link(packet); |
---|
304 | if (!sll) { |
---|
305 | return -1; |
---|
306 | } |
---|
307 | /* 0 == LINUX_SLL_HOST */ |
---|
308 | /* the Waikato Capture point defines "packets |
---|
309 | * originating locally" (ie, outbound), with a |
---|
310 | * direction of 0, and "packets destined locally" |
---|
311 | * (ie, inbound), with a direction of 1. |
---|
312 | * This is kind-of-opposite to LINUX_SLL. |
---|
313 | * We return consistent values here, however |
---|
314 | * |
---|
315 | * Note that in recent versions of pcap, you can |
---|
316 | * use "inbound" and "outbound" on ppp in linux |
---|
317 | */ |
---|
318 | if (ntohs(sll->pkttype == 0)) { |
---|
319 | direction = 1; |
---|
320 | } else { |
---|
321 | direction = 0; |
---|
322 | } |
---|
323 | break; |
---|
324 | |
---|
325 | } |
---|
326 | case TRACE_TYPE_PFLOG: |
---|
327 | { |
---|
328 | struct trace_pflog_header_t *pflog; |
---|
329 | pflog = trace_get_link(packet); |
---|
330 | if (!pflog) { |
---|
331 | return -1; |
---|
332 | } |
---|
333 | /* enum { PF_IN=0, PF_OUT=1 }; */ |
---|
334 | if (ntohs(pflog->dir==0)) { |
---|
335 | |
---|
336 | direction = 1; |
---|
337 | } |
---|
338 | else { |
---|
339 | direction = 0; |
---|
340 | } |
---|
341 | break; |
---|
342 | } |
---|
343 | default: |
---|
344 | break; |
---|
345 | } |
---|
346 | return direction; |
---|
347 | } |
---|
348 | |
---|
349 | |
---|
350 | static struct timeval pcap_get_timeval(const struct libtrace_packet_t *packet) { |
---|
351 | struct pcap_pkthdr *pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
352 | return pcapptr->ts; |
---|
353 | } |
---|
354 | |
---|
355 | |
---|
356 | static int pcap_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
357 | struct pcap_pkthdr *pcapptr = 0; |
---|
358 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
359 | return pcapptr->caplen; |
---|
360 | } |
---|
361 | |
---|
362 | static int pcap_get_wire_length(const struct libtrace_packet_t *packet) { |
---|
363 | struct pcap_pkthdr *pcapptr = 0; |
---|
364 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
365 | return ntohs(pcapptr->len); |
---|
366 | } |
---|
367 | |
---|
368 | static size_t pcap_set_capture_length(struct libtrace_packet_t *packet,size_t size) { |
---|
369 | struct pcap_pkthdr *pcapptr = 0; |
---|
370 | assert(packet); |
---|
371 | if ((size + sizeof(struct pcap_pkthdr)) > packet->size) { |
---|
372 | // can't make a packet larger |
---|
373 | return (packet->size - sizeof(struct pcap_pkthdr)); |
---|
374 | } |
---|
375 | pcapptr = (struct pcap_pkthdr *)packet->buffer; |
---|
376 | pcapptr->caplen = size; |
---|
377 | packet->size = size + sizeof(struct pcap_pkthdr); |
---|
378 | return size; |
---|
379 | } |
---|
380 | |
---|
381 | static int pcap_get_fd(const struct libtrace_packet_t *packet) { |
---|
382 | return pcap_fileno(packet->trace->format_data->input.pcap); |
---|
383 | } |
---|
384 | |
---|
385 | static void pcap_help() { |
---|
386 | printf("pcap format module: $Revision$\n"); |
---|
387 | printf("Supported input URIs:\n"); |
---|
388 | printf("\tpcap:/path/to/file\n"); |
---|
389 | printf("\n"); |
---|
390 | printf("\te.g.: pcap:/tmp/trace.pcap\n"); |
---|
391 | printf("\n"); |
---|
392 | printf("Supported output URIs:\n"); |
---|
393 | printf("\tnone\n"); |
---|
394 | printf("\n"); |
---|
395 | } |
---|
396 | |
---|
397 | static void pcapint_help() { |
---|
398 | printf("pcapint format module: $Revision$\n"); |
---|
399 | printf("Supported input URIs:\n"); |
---|
400 | printf("\tpcapint:interface\n"); |
---|
401 | printf("\n"); |
---|
402 | printf("\te.g.: pcapint:eth0\n"); |
---|
403 | printf("\n"); |
---|
404 | printf("Supported output URIs:\n"); |
---|
405 | printf("\tnone\n"); |
---|
406 | printf("\n"); |
---|
407 | } |
---|
408 | |
---|
409 | |
---|
410 | static struct libtrace_format_t pcap = { |
---|
411 | "pcap", |
---|
412 | "$Id$", |
---|
413 | "pcap", |
---|
414 | pcap_init_input, /* init_input */ |
---|
415 | pcap_init_output, /* init_output */ |
---|
416 | NULL, /* config_output */ |
---|
417 | pcap_fin_input, /* fin_input */ |
---|
418 | pcap_fin_output, /* fin_output */ |
---|
419 | pcap_read_packet, /* read_packet */ |
---|
420 | pcap_write_packet, /* write_packet */ |
---|
421 | pcap_get_link, /* get_link */ |
---|
422 | pcap_get_link_type, /* get_link_type */ |
---|
423 | pcap_get_direction, /* get_direction */ |
---|
424 | NULL, /* set_direction */ |
---|
425 | NULL, /* get_erf_timestamp */ |
---|
426 | pcap_get_timeval, /* get_timeval */ |
---|
427 | NULL, /* get_seconds */ |
---|
428 | pcap_get_capture_length, /* get_capture_length */ |
---|
429 | pcap_get_wire_length, /* get_wire_length */ |
---|
430 | pcap_set_capture_length, /* set_capture_length */ |
---|
431 | NULL, /* get_fd */ |
---|
432 | trace_event_trace, /* trace_event */ |
---|
433 | pcap_help /* help */ |
---|
434 | }; |
---|
435 | |
---|
436 | static struct libtrace_format_t pcapint = { |
---|
437 | "pcapint", |
---|
438 | "$Id$", |
---|
439 | "pcap", |
---|
440 | pcapint_init_input, /* init_input */ |
---|
441 | pcapint_init_output, /* init_output */ |
---|
442 | NULL, /* config_output */ |
---|
443 | pcap_fin_input, /* fin_input */ |
---|
444 | pcapint_fin_output, /* fin_output */ |
---|
445 | pcap_read_packet, /* read_packet */ |
---|
446 | pcapint_write_packet, /* write_packet */ |
---|
447 | pcap_get_link, /* get_link */ |
---|
448 | pcap_get_link_type, /* get_link_type */ |
---|
449 | pcap_get_direction, /* get_direction */ |
---|
450 | NULL, /* set_direction */ |
---|
451 | NULL, /* get_erf_timestamp */ |
---|
452 | pcap_get_timeval, /* get_timeval */ |
---|
453 | NULL, /* get_seconds */ |
---|
454 | pcap_get_capture_length, /* get_capture_length */ |
---|
455 | pcap_get_wire_length, /* get_wire_length */ |
---|
456 | pcap_set_capture_length, /* set_capture_length */ |
---|
457 | pcap_get_fd, /* get_fd */ |
---|
458 | trace_event_device, /* trace_event */ |
---|
459 | pcapint_help /* help */ |
---|
460 | }; |
---|
461 | |
---|
462 | //pcap_ptr = &pcap; |
---|
463 | //pcapint_ptr = &pcapint; |
---|
464 | |
---|
465 | void __attribute__((constructor)) pcap_constructor() { |
---|
466 | pcap_ptr = &pcap; |
---|
467 | pcapint_ptr = &pcapint; |
---|
468 | register_format(pcap_ptr); |
---|
469 | register_format(pcapint_ptr); |
---|
470 | } |
---|
471 | |
---|
472 | |
---|
473 | #endif |
---|