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 | #include <sys/stat.h> |
---|
38 | #include <assert.h> |
---|
39 | #include <stdio.h> |
---|
40 | #include <stdlib.h> |
---|
41 | #include <string.h> |
---|
42 | #include <errno.h> |
---|
43 | #include <fcntl.h> |
---|
44 | |
---|
45 | #define DATA(x) ((struct pcapfile_format_data_t*)((x)->format_data)) |
---|
46 | #define DATAOUT(x) ((struct pcapfile_format_data_out_t*)((x)->format_data)) |
---|
47 | |
---|
48 | typedef struct pcapfile_header_t { |
---|
49 | uint32_t magic_number; /* magic number */ |
---|
50 | uint16_t version_major; /* major version number */ |
---|
51 | uint16_t version_minor; /* minor version number */ |
---|
52 | int32_t thiszone; /* GMT to local correction */ |
---|
53 | uint32_t sigfigs; /* timestamp accuracy */ |
---|
54 | uint32_t snaplen; /* aka "wirelen" */ |
---|
55 | uint32_t network; /* data link type */ |
---|
56 | } pcapfile_header_t; |
---|
57 | |
---|
58 | struct pcapfile_format_data_t { |
---|
59 | libtrace_io_t *file; |
---|
60 | pcapfile_header_t header; |
---|
61 | }; |
---|
62 | |
---|
63 | struct pcapfile_format_data_out_t { |
---|
64 | libtrace_io_t *file; |
---|
65 | int level; |
---|
66 | int flag; |
---|
67 | |
---|
68 | }; |
---|
69 | |
---|
70 | static int pcapfile_init_input(libtrace_t *libtrace) { |
---|
71 | libtrace->format_data = malloc(sizeof(struct pcapfile_format_data_t)); |
---|
72 | |
---|
73 | if (libtrace->format_data == NULL) { |
---|
74 | trace_set_err(libtrace,ENOMEM,"Out of memory"); |
---|
75 | return -1; |
---|
76 | } |
---|
77 | |
---|
78 | DATA(libtrace)->file=NULL; |
---|
79 | |
---|
80 | return 0; |
---|
81 | } |
---|
82 | |
---|
83 | static int pcapfile_init_output(libtrace_out_t *libtrace) { |
---|
84 | libtrace->format_data = |
---|
85 | malloc(sizeof(struct pcapfile_format_data_out_t)); |
---|
86 | |
---|
87 | DATAOUT(libtrace)->file=NULL; |
---|
88 | DATAOUT(libtrace)->level=0; |
---|
89 | DATAOUT(libtrace)->flag=O_CREAT|O_WRONLY; |
---|
90 | |
---|
91 | return 0; |
---|
92 | } |
---|
93 | |
---|
94 | static uint16_t swaps(libtrace_t *libtrace, uint16_t num) |
---|
95 | { |
---|
96 | /* to deal with open_dead traces that might try and use this |
---|
97 | * if we don't have any per trace data, assume host byte order |
---|
98 | */ |
---|
99 | if (!DATA(libtrace)) |
---|
100 | return num; |
---|
101 | if (DATA(libtrace)->header.magic_number == 0xd4c3b2a1) |
---|
102 | return byteswap16(num); |
---|
103 | |
---|
104 | return num; |
---|
105 | } |
---|
106 | |
---|
107 | static uint32_t swapl(libtrace_t *libtrace, uint32_t num) |
---|
108 | { |
---|
109 | /* to deal with open_dead traces that might try and use this |
---|
110 | * if we don't have any per trace data, assume host byte order |
---|
111 | */ |
---|
112 | if (!DATA(libtrace)) |
---|
113 | return num; |
---|
114 | if (DATA(libtrace)->header.magic_number == 0xd4c3b2a1) |
---|
115 | return byteswap32(num); |
---|
116 | |
---|
117 | return num; |
---|
118 | } |
---|
119 | |
---|
120 | |
---|
121 | static int pcapfile_start_input(libtrace_t *libtrace) |
---|
122 | { |
---|
123 | int err; |
---|
124 | |
---|
125 | if (!DATA(libtrace)->file) { |
---|
126 | DATA(libtrace)->file=trace_open_file(libtrace); |
---|
127 | |
---|
128 | if (!DATA(libtrace)->file) |
---|
129 | return -1; |
---|
130 | |
---|
131 | err=libtrace_io_read(DATA(libtrace)->file, |
---|
132 | &DATA(libtrace)->header, |
---|
133 | sizeof(DATA(libtrace)->header)); |
---|
134 | |
---|
135 | if (err<1) |
---|
136 | return -1; |
---|
137 | |
---|
138 | if (swapl(libtrace,DATA(libtrace)->header.magic_number) != 0xa1b2c3d4) { |
---|
139 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"Not a pcap tracefile\n"); |
---|
140 | return -1; /* Not a pcap file */ |
---|
141 | } |
---|
142 | |
---|
143 | if (swaps(libtrace,DATA(libtrace)->header.version_major)!=2 |
---|
144 | && swaps(libtrace,DATA(libtrace)->header.version_minor)!=4) { |
---|
145 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"Unknown pcap tracefile version %d.%d\n", |
---|
146 | swaps(libtrace, |
---|
147 | DATA(libtrace)->header.version_major), |
---|
148 | swaps(libtrace, |
---|
149 | DATA(libtrace)->header.version_minor)); |
---|
150 | return -1; |
---|
151 | } |
---|
152 | |
---|
153 | } |
---|
154 | |
---|
155 | return 0; |
---|
156 | } |
---|
157 | |
---|
158 | static int pcapfile_start_output(libtrace_out_t *libtrace) |
---|
159 | { |
---|
160 | return 0; |
---|
161 | } |
---|
162 | |
---|
163 | static int pcapfile_config_input(libtrace_t *libtrace, |
---|
164 | trace_option_t option, |
---|
165 | void *data) |
---|
166 | { |
---|
167 | trace_set_err(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
---|
168 | "Unknown option %i", option); |
---|
169 | return -1; |
---|
170 | } |
---|
171 | |
---|
172 | static int pcapfile_fin_input(libtrace_t *libtrace) |
---|
173 | { |
---|
174 | libtrace_io_close(DATA(libtrace)->file); |
---|
175 | free(libtrace->format_data); |
---|
176 | return 0; /* success */ |
---|
177 | } |
---|
178 | |
---|
179 | static int pcapfile_fin_output(libtrace_out_t *libtrace) |
---|
180 | { |
---|
181 | libtrace_io_close(DATA(libtrace)->file); |
---|
182 | free(libtrace->format_data); |
---|
183 | libtrace->format_data=NULL; |
---|
184 | return 0; /* success */ |
---|
185 | } |
---|
186 | |
---|
187 | static int pcapfile_config_output(libtrace_out_t *libtrace, |
---|
188 | trace_option_output_t option, |
---|
189 | void *value) |
---|
190 | { |
---|
191 | switch (option) { |
---|
192 | case TRACE_OPTION_OUTPUT_COMPRESS: |
---|
193 | DATAOUT(libtrace)->level = *(int*)value; |
---|
194 | return 0; |
---|
195 | case TRACE_OPTION_OUTPUT_FILEFLAGS: |
---|
196 | DATAOUT(libtrace)->flag = *(int*)value; |
---|
197 | return 0; |
---|
198 | default: |
---|
199 | /* Unknown option */ |
---|
200 | trace_set_err_out(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
---|
201 | "Unknown option"); |
---|
202 | return -1; |
---|
203 | } |
---|
204 | return -1; |
---|
205 | } |
---|
206 | |
---|
207 | static int pcapfile_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) |
---|
208 | { |
---|
209 | int err; |
---|
210 | |
---|
211 | assert(libtrace->format_data); |
---|
212 | |
---|
213 | packet->type = pcap_dlt_to_rt(swapl(libtrace, |
---|
214 | DATA(libtrace)->header.network)); |
---|
215 | |
---|
216 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
---|
217 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
218 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
219 | } |
---|
220 | |
---|
221 | err=libtrace_io_read(DATA(libtrace)->file, |
---|
222 | packet->buffer, |
---|
223 | sizeof(libtrace_pcapfile_pkt_hdr_t)); |
---|
224 | |
---|
225 | if (err<0) { |
---|
226 | trace_set_err(libtrace,errno,"reading packet"); |
---|
227 | return -1; |
---|
228 | } |
---|
229 | if (err==0) { |
---|
230 | /* EOF */ |
---|
231 | return 0; |
---|
232 | } |
---|
233 | |
---|
234 | packet->header = packet->buffer; |
---|
235 | |
---|
236 | |
---|
237 | err=libtrace_io_read(DATA(libtrace)->file, |
---|
238 | (char*)packet->buffer+sizeof(libtrace_pcapfile_pkt_hdr_t), |
---|
239 | swapl(libtrace,((libtrace_pcapfile_pkt_hdr_t*)packet->buffer)->caplen) |
---|
240 | ); |
---|
241 | |
---|
242 | |
---|
243 | if (err<0) { |
---|
244 | trace_set_err(libtrace,errno,"reading packet"); |
---|
245 | return -1; |
---|
246 | } |
---|
247 | if (err==0) { |
---|
248 | return 0; |
---|
249 | } |
---|
250 | |
---|
251 | packet->payload = (char*)packet->buffer |
---|
252 | + sizeof(libtrace_pcapfile_pkt_hdr_t); |
---|
253 | |
---|
254 | return sizeof(libtrace_pcapfile_pkt_hdr_t) |
---|
255 | +swapl(libtrace,((libtrace_pcapfile_pkt_hdr_t*)packet->buffer)->caplen); |
---|
256 | } |
---|
257 | |
---|
258 | static int pcapfile_write_packet(libtrace_out_t *out, |
---|
259 | libtrace_packet_t *packet) |
---|
260 | { |
---|
261 | struct libtrace_pcapfile_pkt_hdr_t hdr; |
---|
262 | struct timeval tv = trace_get_timeval(packet); |
---|
263 | int numbytes; |
---|
264 | int ret; |
---|
265 | |
---|
266 | /* If this packet cannot be converted to a pcap linktype then |
---|
267 | * pop off the top header until it can be converted |
---|
268 | */ |
---|
269 | while (libtrace_to_pcap_dlt(trace_get_link_type(packet))==~0) { |
---|
270 | if (!demote_packet(packet)) { |
---|
271 | trace_set_err_out(out, |
---|
272 | TRACE_ERR_NO_CONVERSION, |
---|
273 | "pcap does not support this format"); |
---|
274 | return -1; |
---|
275 | } |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | /* Now we know the link type write out a header if we've not done |
---|
280 | * so already |
---|
281 | */ |
---|
282 | if (!DATAOUT(out)->file) { |
---|
283 | struct pcapfile_header_t hdr; |
---|
284 | |
---|
285 | DATAOUT(out)->file=trace_open_file_out(out, |
---|
286 | DATAOUT(out)->level, |
---|
287 | DATAOUT(out)->flag); |
---|
288 | if (!DATAOUT(out)->file) |
---|
289 | return -1; |
---|
290 | |
---|
291 | hdr.magic_number = 0xa1b2c3d4; |
---|
292 | hdr.version_major = 2; |
---|
293 | hdr.version_minor = 4; |
---|
294 | hdr.thiszone = 0; |
---|
295 | hdr.sigfigs = 0; |
---|
296 | hdr.snaplen = 65536; |
---|
297 | hdr.network = |
---|
298 | libtrace_to_pcap_dlt(trace_get_link_type(packet)); |
---|
299 | |
---|
300 | libtrace_io_write(DATAOUT(out)->file, &hdr, sizeof(hdr)); |
---|
301 | } |
---|
302 | |
---|
303 | hdr.ts_sec = tv.tv_sec; |
---|
304 | hdr.ts_usec = tv.tv_usec; |
---|
305 | hdr.caplen = trace_get_capture_length(packet); |
---|
306 | /* PCAP doesn't include the FCS, we do */ |
---|
307 | if (trace_get_link_type(packet)==TRACE_TYPE_ETH) |
---|
308 | if (trace_get_wire_length(packet) >= 4) { |
---|
309 | hdr.wirelen = |
---|
310 | trace_get_wire_length(packet)-4; |
---|
311 | } |
---|
312 | else { |
---|
313 | hdr.wirelen = 0; |
---|
314 | } |
---|
315 | else |
---|
316 | hdr.wirelen = trace_get_wire_length(packet); |
---|
317 | |
---|
318 | |
---|
319 | numbytes=libtrace_io_write(DATAOUT(out)->file, |
---|
320 | &hdr, sizeof(hdr)); |
---|
321 | |
---|
322 | if (numbytes!=sizeof(hdr)) |
---|
323 | return -1; |
---|
324 | |
---|
325 | ret=libtrace_io_write(DATAOUT(out)->file, |
---|
326 | trace_get_link(packet), |
---|
327 | trace_get_capture_length(packet)); |
---|
328 | |
---|
329 | if (ret!=(int)trace_get_capture_length(packet)) |
---|
330 | return -1; |
---|
331 | |
---|
332 | return numbytes+ret; |
---|
333 | } |
---|
334 | |
---|
335 | static libtrace_linktype_t pcapfile_get_link_type( |
---|
336 | const libtrace_packet_t *packet) |
---|
337 | { |
---|
338 | #if 0 |
---|
339 | return pcap_dlt_to_libtrace( |
---|
340 | swapl(packet->trace, |
---|
341 | DATA(packet->trace)->header.network |
---|
342 | ) |
---|
343 | ); |
---|
344 | #endif |
---|
345 | return pcap_dlt_to_libtrace(rt_to_pcap_dlt(packet->type)); |
---|
346 | } |
---|
347 | |
---|
348 | static libtrace_direction_t pcapfile_get_direction(const libtrace_packet_t *packet) |
---|
349 | { |
---|
350 | libtrace_direction_t direction = -1; |
---|
351 | switch(pcapfile_get_link_type(packet)) { |
---|
352 | case TRACE_TYPE_LINUX_SLL: |
---|
353 | { |
---|
354 | libtrace_sll_header_t *sll; |
---|
355 | sll = trace_get_link(packet); |
---|
356 | if (!sll) { |
---|
357 | trace_set_err(packet->trace, |
---|
358 | TRACE_ERR_BAD_PACKET, |
---|
359 | "Bad or missing packet"); |
---|
360 | return -1; |
---|
361 | } |
---|
362 | /* 0 == LINUX_SLL_HOST */ |
---|
363 | /* the Waikato Capture point defines "packets |
---|
364 | * originating locally" (ie, outbound), with a |
---|
365 | * direction of 0, and "packets destined locally" |
---|
366 | * (ie, inbound), with a direction of 1. |
---|
367 | * This is kind-of-opposite to LINUX_SLL. |
---|
368 | * We return consistent values here, however |
---|
369 | * |
---|
370 | * Note that in recent versions of pcap, you can |
---|
371 | * use "inbound" and "outbound" on ppp in linux |
---|
372 | */ |
---|
373 | if (ntohs(sll->pkttype == 0)) { |
---|
374 | direction = TRACE_DIR_INCOMING; |
---|
375 | } else { |
---|
376 | direction = TRACE_DIR_OUTGOING; |
---|
377 | } |
---|
378 | break; |
---|
379 | |
---|
380 | } |
---|
381 | case TRACE_TYPE_PFLOG: |
---|
382 | { |
---|
383 | libtrace_pflog_header_t *pflog; |
---|
384 | pflog = trace_get_link(packet); |
---|
385 | if (!pflog) { |
---|
386 | trace_set_err(packet->trace, |
---|
387 | TRACE_ERR_BAD_PACKET, |
---|
388 | "Bad or missing packet"); |
---|
389 | return -1; |
---|
390 | } |
---|
391 | /* enum { PF_IN=0, PF_OUT=1 }; */ |
---|
392 | if (ntohs(pflog->dir==0)) { |
---|
393 | |
---|
394 | direction = TRACE_DIR_INCOMING; |
---|
395 | } |
---|
396 | else { |
---|
397 | direction = TRACE_DIR_OUTGOING; |
---|
398 | } |
---|
399 | break; |
---|
400 | } |
---|
401 | default: |
---|
402 | break; |
---|
403 | } |
---|
404 | return direction; |
---|
405 | } |
---|
406 | |
---|
407 | |
---|
408 | static struct timeval pcapfile_get_timeval( |
---|
409 | const libtrace_packet_t *packet) |
---|
410 | { |
---|
411 | libtrace_pcapfile_pkt_hdr_t *hdr = |
---|
412 | (libtrace_pcapfile_pkt_hdr_t*)packet->header; |
---|
413 | struct timeval ts; |
---|
414 | ts.tv_sec = swapl(packet->trace,hdr->ts_sec); |
---|
415 | ts.tv_usec = swapl(packet->trace,hdr->ts_usec); |
---|
416 | return ts; |
---|
417 | } |
---|
418 | |
---|
419 | |
---|
420 | static int pcapfile_get_capture_length(const libtrace_packet_t *packet) { |
---|
421 | libtrace_pcapfile_pkt_hdr_t *pcapptr |
---|
422 | = (libtrace_pcapfile_pkt_hdr_t *)packet->header; |
---|
423 | |
---|
424 | return swapl(packet->trace,pcapptr->caplen); |
---|
425 | } |
---|
426 | |
---|
427 | static int pcapfile_get_wire_length(const libtrace_packet_t *packet) { |
---|
428 | libtrace_pcapfile_pkt_hdr_t *pcapptr |
---|
429 | = (libtrace_pcapfile_pkt_hdr_t *)packet->header; |
---|
430 | if (packet->type==pcap_dlt_to_rt(TRACE_DLT_EN10MB)) |
---|
431 | /* Include the missing FCS */ |
---|
432 | return swapl(packet->trace,pcapptr->wirelen)+4; |
---|
433 | else |
---|
434 | return swapl(packet->trace,pcapptr->wirelen); |
---|
435 | } |
---|
436 | |
---|
437 | static int pcapfile_get_framing_length(const libtrace_packet_t *packet UNUSED) { |
---|
438 | return sizeof(libtrace_pcapfile_pkt_hdr_t); |
---|
439 | } |
---|
440 | |
---|
441 | static size_t pcapfile_set_capture_length(libtrace_packet_t *packet,size_t size) { |
---|
442 | libtrace_pcapfile_pkt_hdr_t *pcapptr = 0; |
---|
443 | assert(packet); |
---|
444 | if (size > trace_get_capture_length(packet)) { |
---|
445 | /* can't make a packet larger */ |
---|
446 | return trace_get_capture_length(packet); |
---|
447 | } |
---|
448 | pcapptr = (libtrace_pcapfile_pkt_hdr_t *)packet->header; |
---|
449 | pcapptr->caplen = swapl(packet->trace,size); |
---|
450 | return trace_get_capture_length(packet); |
---|
451 | } |
---|
452 | |
---|
453 | static void pcapfile_help() { |
---|
454 | printf("pcapfile format module: $Revision$\n"); |
---|
455 | printf("Supported input URIs:\n"); |
---|
456 | printf("\tpcapfile:/path/to/file\n"); |
---|
457 | printf("\tpcapfile:/path/to/file.gz\n"); |
---|
458 | printf("\n"); |
---|
459 | printf("\te.g.: pcapfile:/tmp/trace.pcap\n"); |
---|
460 | printf("\n"); |
---|
461 | } |
---|
462 | |
---|
463 | static struct libtrace_format_t pcapfile = { |
---|
464 | "pcapfile", |
---|
465 | "$Id$", |
---|
466 | TRACE_FORMAT_PCAPFILE, |
---|
467 | pcapfile_init_input, /* init_input */ |
---|
468 | pcapfile_config_input, /* config_input */ |
---|
469 | pcapfile_start_input, /* start_input */ |
---|
470 | NULL, /* pause_input */ |
---|
471 | pcapfile_init_output, /* init_output */ |
---|
472 | pcapfile_config_output, /* config_output */ |
---|
473 | pcapfile_start_output, /* start_output */ |
---|
474 | pcapfile_fin_input, /* fin_input */ |
---|
475 | pcapfile_fin_output, /* fin_output */ |
---|
476 | pcapfile_read_packet, /* read_packet */ |
---|
477 | NULL, /* fin_packet */ |
---|
478 | pcapfile_write_packet, /* write_packet */ |
---|
479 | pcapfile_get_link_type, /* get_link_type */ |
---|
480 | pcapfile_get_direction, /* get_direction */ |
---|
481 | NULL, /* set_direction */ |
---|
482 | NULL, /* get_erf_timestamp */ |
---|
483 | pcapfile_get_timeval, /* get_timeval */ |
---|
484 | NULL, /* get_seconds */ |
---|
485 | NULL, /* seek_erf */ |
---|
486 | NULL, /* seek_timeval */ |
---|
487 | NULL, /* seek_seconds */ |
---|
488 | pcapfile_get_capture_length, /* get_capture_length */ |
---|
489 | pcapfile_get_wire_length, /* get_wire_length */ |
---|
490 | pcapfile_get_framing_length, /* get_framing_length */ |
---|
491 | pcapfile_set_capture_length, /* set_capture_length */ |
---|
492 | NULL, /* get_fd */ |
---|
493 | trace_event_trace, /* trace_event */ |
---|
494 | pcapfile_help, /* help */ |
---|
495 | NULL /* next pointer */ |
---|
496 | }; |
---|
497 | |
---|
498 | |
---|
499 | void pcapfile_constructor() { |
---|
500 | register_format(&pcapfile); |
---|
501 | } |
---|
502 | |
---|
503 | |
---|