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 | #define _GNU_SOURCE |
---|
32 | #include "config.h" |
---|
33 | #include "common.h" |
---|
34 | #include "libtrace.h" |
---|
35 | #include "libtrace_int.h" |
---|
36 | #include "format_helper.h" |
---|
37 | #include "wag.h" |
---|
38 | |
---|
39 | #include <sys/stat.h> |
---|
40 | #include <fcntl.h> |
---|
41 | #include <assert.h> |
---|
42 | #include <errno.h> |
---|
43 | #include <stdio.h> |
---|
44 | #include <string.h> |
---|
45 | #include <stdlib.h> |
---|
46 | |
---|
47 | #ifdef HAVE_LIMITS_H |
---|
48 | # include <limits.h> |
---|
49 | #endif |
---|
50 | |
---|
51 | #ifdef HAVE_SYS_LIMITS_H |
---|
52 | # include <sys/limits.h> |
---|
53 | #endif |
---|
54 | |
---|
55 | #ifdef WIN32 |
---|
56 | # include <io.h> |
---|
57 | # include <share.h> |
---|
58 | #endif |
---|
59 | |
---|
60 | static struct libtrace_format_t wag; |
---|
61 | static struct libtrace_format_t wag_trace; |
---|
62 | |
---|
63 | #define DATA(x) ((struct wag_format_data_t *)x->format_data) |
---|
64 | #define DATAOUT(x) ((struct wag_format_data_out_t *)x->format_data) |
---|
65 | |
---|
66 | #define INPUT DATA(libtrace)->input |
---|
67 | #define OUTPUT DATAOUT(libtrace)->output |
---|
68 | #define OPTIONS DATAOUT(libtrace)->options |
---|
69 | |
---|
70 | struct wag_format_data_t { |
---|
71 | /** Information about the current state of the input device */ |
---|
72 | union { |
---|
73 | int fd; |
---|
74 | libtrace_io_t *file; |
---|
75 | } input; |
---|
76 | }; |
---|
77 | |
---|
78 | struct wag_format_data_out_t { |
---|
79 | union { |
---|
80 | struct { |
---|
81 | int level; |
---|
82 | int filemode; |
---|
83 | } zlib; |
---|
84 | } options; |
---|
85 | union { |
---|
86 | int fd; |
---|
87 | libtrace_io_t *file; |
---|
88 | } output; |
---|
89 | }; |
---|
90 | |
---|
91 | static int wag_init_input(libtrace_t *libtrace) { |
---|
92 | libtrace->format_data = calloc(1, sizeof(struct wag_format_data_t)); |
---|
93 | |
---|
94 | return 0; |
---|
95 | } |
---|
96 | |
---|
97 | static int wag_start_input(libtrace_t *libtrace) |
---|
98 | { |
---|
99 | struct stat buf; |
---|
100 | if (stat(libtrace->uridata,&buf) == -1 ) { |
---|
101 | trace_set_err(libtrace,errno,"stat(%s)",libtrace->uridata); |
---|
102 | return -1; |
---|
103 | } |
---|
104 | #ifndef WIN32 |
---|
105 | if (S_ISCHR(buf.st_mode)) { |
---|
106 | INPUT.fd = open(libtrace->uridata, O_RDONLY); |
---|
107 | return 0; |
---|
108 | } |
---|
109 | #endif |
---|
110 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED, |
---|
111 | "%s is not a valid char device", |
---|
112 | libtrace->uridata); |
---|
113 | return -1; |
---|
114 | } |
---|
115 | |
---|
116 | static int wtf_init_input(struct libtrace_t *libtrace) |
---|
117 | { |
---|
118 | libtrace->format_data = calloc(1,sizeof(struct wag_format_data_t)); |
---|
119 | return 0; |
---|
120 | } |
---|
121 | |
---|
122 | static int wtf_start_input(libtrace_t *libtrace) |
---|
123 | { |
---|
124 | if (DATA(libtrace)->input.file) |
---|
125 | return 0; /* success */ |
---|
126 | DATA(libtrace)->input.file = trace_open_file(libtrace); |
---|
127 | |
---|
128 | if (!DATA(libtrace)->input.file) |
---|
129 | return -1; |
---|
130 | |
---|
131 | return 0; /* success */ |
---|
132 | } |
---|
133 | |
---|
134 | static int wtf_init_output(struct libtrace_out_t *libtrace) { |
---|
135 | libtrace->format_data = malloc(sizeof(struct wag_format_data_out_t)); |
---|
136 | |
---|
137 | OUTPUT.file = 0; |
---|
138 | OPTIONS.zlib.level = 0; |
---|
139 | OPTIONS.zlib.filemode = O_CREAT | O_WRONLY; |
---|
140 | |
---|
141 | return 0; |
---|
142 | } |
---|
143 | |
---|
144 | static int wtf_start_output(libtrace_out_t *libtrace) { |
---|
145 | OUTPUT.file = trace_open_file_out(libtrace, |
---|
146 | OPTIONS.zlib.level, |
---|
147 | OPTIONS.zlib.filemode); |
---|
148 | if (!OUTPUT.file) { |
---|
149 | return -1; |
---|
150 | } |
---|
151 | return 0; |
---|
152 | } |
---|
153 | |
---|
154 | static int wtf_config_output(struct libtrace_out_t *libtrace, |
---|
155 | trace_option_output_t option, |
---|
156 | void *value) { |
---|
157 | switch(option) { |
---|
158 | #if HAVE_ZLIB |
---|
159 | case TRACE_OPTION_OUTPUT_COMPRESS: |
---|
160 | OPTIONS.zlib.level = *(int*)value; |
---|
161 | assert(OPTIONS.zlib.level>=0 |
---|
162 | && OPTIONS.zlib.level<=9); |
---|
163 | return 0; |
---|
164 | #else |
---|
165 | case TRACE_OPTION_OUTPUT_COMPRESS: |
---|
166 | /* E feature unavailable */ |
---|
167 | trace_set_err_out(libtrace,TRACE_ERR_OPTION_UNAVAIL, |
---|
168 | "zlib not supported"); |
---|
169 | return -1; |
---|
170 | #endif |
---|
171 | default: |
---|
172 | /* E unknown feature */ |
---|
173 | trace_set_err_out(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
---|
174 | "Unknown option"); |
---|
175 | return -1; |
---|
176 | } |
---|
177 | } |
---|
178 | |
---|
179 | static int wag_pause_input(libtrace_t *libtrace) |
---|
180 | { |
---|
181 | close(INPUT.fd); |
---|
182 | return 0; |
---|
183 | } |
---|
184 | |
---|
185 | static int wag_fin_input(struct libtrace_t *libtrace) { |
---|
186 | free(libtrace->format_data); |
---|
187 | return 0; |
---|
188 | } |
---|
189 | |
---|
190 | static int wtf_fin_input(struct libtrace_t *libtrace) { |
---|
191 | libtrace_io_close(INPUT.file); |
---|
192 | free(libtrace->format_data); |
---|
193 | return 0; |
---|
194 | } |
---|
195 | |
---|
196 | static int wtf_fin_output(struct libtrace_out_t *libtrace) { |
---|
197 | libtrace_io_close(OUTPUT.file); |
---|
198 | free(libtrace->format_data); |
---|
199 | return 0; |
---|
200 | } |
---|
201 | |
---|
202 | static int wag_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
203 | size_t framesize; |
---|
204 | char *buf_ptr = (char *)buffer; |
---|
205 | int to_read = 0; |
---|
206 | uint16_t magic = 0; |
---|
207 | |
---|
208 | assert(libtrace); |
---|
209 | |
---|
210 | to_read = sizeof(struct frame_t); |
---|
211 | |
---|
212 | while (to_read>0) { |
---|
213 | int ret=read(INPUT.fd,buf_ptr,to_read); |
---|
214 | |
---|
215 | if (ret == -1) { |
---|
216 | if (errno == EINTR || errno==EAGAIN) |
---|
217 | continue; |
---|
218 | |
---|
219 | trace_set_err(libtrace,errno,"read(%s)",libtrace->uridata); |
---|
220 | return -1; |
---|
221 | } |
---|
222 | |
---|
223 | assert(ret>0); |
---|
224 | |
---|
225 | to_read = to_read - ret; |
---|
226 | buf_ptr = buf_ptr + ret; |
---|
227 | } |
---|
228 | |
---|
229 | |
---|
230 | framesize = ntohs(((struct frame_t *)buffer)->size); |
---|
231 | magic = ntohs(((struct frame_t *)buffer)->magic); |
---|
232 | |
---|
233 | if (magic != 0xdaa1) { |
---|
234 | trace_set_err(libtrace, |
---|
235 | TRACE_ERR_BAD_PACKET,"magic number bad or missing"); |
---|
236 | return -1; |
---|
237 | } |
---|
238 | |
---|
239 | /* We should deal. this is called "snapping", but we don't yet */ |
---|
240 | assert(framesize>len); |
---|
241 | |
---|
242 | buf_ptr = (void*)((char*)buffer + sizeof (struct frame_t)); |
---|
243 | to_read = framesize - sizeof(struct frame_t); |
---|
244 | |
---|
245 | while (to_read>0) { |
---|
246 | int ret=read(INPUT.fd,buf_ptr,to_read); |
---|
247 | |
---|
248 | if (ret == -1) { |
---|
249 | if (errno == EINTR || errno==EAGAIN) |
---|
250 | continue; |
---|
251 | trace_set_err(libtrace,errno,"read(%s)",libtrace->uridata); |
---|
252 | return -1; |
---|
253 | } |
---|
254 | |
---|
255 | to_read = to_read - ret; |
---|
256 | buf_ptr = buf_ptr + ret; |
---|
257 | } |
---|
258 | return framesize; |
---|
259 | } |
---|
260 | |
---|
261 | |
---|
262 | static int wag_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
263 | int numbytes; |
---|
264 | |
---|
265 | if (packet->buf_control == TRACE_CTRL_EXTERNAL || !packet->buffer) { |
---|
266 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
267 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
268 | } |
---|
269 | |
---|
270 | |
---|
271 | packet->trace = libtrace; |
---|
272 | packet->type = RT_DATA_WAG; |
---|
273 | |
---|
274 | if ((numbytes = wag_read(libtrace, (void *)packet->buffer, RP_BUFSIZE)) <= 0) { |
---|
275 | |
---|
276 | return numbytes; |
---|
277 | } |
---|
278 | |
---|
279 | |
---|
280 | packet->header = packet->buffer; |
---|
281 | packet->payload=(char*)packet->buffer+trace_get_framing_length(packet); |
---|
282 | return numbytes; |
---|
283 | } |
---|
284 | |
---|
285 | static int wtf_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
286 | int numbytes; |
---|
287 | void *buffer; |
---|
288 | void *buffer2; |
---|
289 | int framesize; |
---|
290 | int size; |
---|
291 | |
---|
292 | if (packet->buf_control == TRACE_CTRL_EXTERNAL || !packet->buffer) { |
---|
293 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
294 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
295 | } |
---|
296 | packet->type = RT_DATA_WAG; |
---|
297 | buffer2 = buffer = packet->buffer; |
---|
298 | |
---|
299 | numbytes = libtrace_io_read(INPUT.file, buffer, sizeof(struct frame_t)); |
---|
300 | |
---|
301 | if (numbytes == 0) { |
---|
302 | return 0; |
---|
303 | } |
---|
304 | |
---|
305 | if (numbytes != sizeof(struct frame_t)) { |
---|
306 | int err=errno; |
---|
307 | trace_set_err(libtrace,err, |
---|
308 | "read(%s,frame_t)",packet->trace->uridata); |
---|
309 | printf("failed to read header=%i\n",err); |
---|
310 | return -1; |
---|
311 | } |
---|
312 | |
---|
313 | if (htons(((struct frame_t *)buffer)->magic) != 0xdaa1) { |
---|
314 | trace_set_err(libtrace, |
---|
315 | TRACE_ERR_BAD_PACKET,"Insufficient magic (%04x)",htons(((struct frame_t *)buffer)->magic)); |
---|
316 | return -1; |
---|
317 | } |
---|
318 | |
---|
319 | framesize = ntohs(((struct frame_t *)buffer)->size); |
---|
320 | buffer2 = (char*)buffer + sizeof(struct frame_t); |
---|
321 | size = framesize - sizeof(struct frame_t); |
---|
322 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
323 | assert(size > 0); |
---|
324 | |
---|
325 | if ((numbytes=libtrace_io_read(INPUT.file, buffer2, size)) != size) { |
---|
326 | trace_set_err(libtrace, |
---|
327 | errno,"read(%s,buffer)",packet->trace->uridata); |
---|
328 | return -1; |
---|
329 | } |
---|
330 | |
---|
331 | packet->header = packet->buffer; |
---|
332 | packet->payload=(char*)packet->buffer+trace_get_framing_length(packet); |
---|
333 | return framesize; |
---|
334 | |
---|
335 | } |
---|
336 | |
---|
337 | static int wtf_write_packet(struct libtrace_out_t *libtrace, const struct libtrace_packet_t *packet) { |
---|
338 | int numbytes =0 ; |
---|
339 | if (packet->trace->format != &wag_trace) { |
---|
340 | trace_set_err_out(libtrace,TRACE_ERR_NO_CONVERSION, |
---|
341 | "Cannot convert to wag trace format from %s format yet", |
---|
342 | packet->trace->format->name); |
---|
343 | return -1; |
---|
344 | } |
---|
345 | |
---|
346 | /* We could just read from packet->buffer, but I feel it is more |
---|
347 | * technically correct to read from the header and payload pointers |
---|
348 | */ |
---|
349 | if ((numbytes = libtrace_io_write(OUTPUT.file, packet->header, |
---|
350 | trace_get_framing_length(packet))) == -1) { |
---|
351 | trace_set_err_out(libtrace,errno, |
---|
352 | "write(%s)",packet->trace->uridata); |
---|
353 | return -1; |
---|
354 | } |
---|
355 | if ((numbytes = libtrace_io_write(OUTPUT.file, packet->payload, |
---|
356 | trace_get_capture_length(packet)) == -1)) { |
---|
357 | trace_set_err_out(libtrace, |
---|
358 | errno,"write(%s)",packet->trace->uridata); |
---|
359 | return -1; |
---|
360 | } |
---|
361 | return numbytes; |
---|
362 | } |
---|
363 | |
---|
364 | static libtrace_linktype_t wag_get_link_type(const struct libtrace_packet_t *packet UNUSED) { |
---|
365 | return TRACE_TYPE_80211; |
---|
366 | } |
---|
367 | |
---|
368 | static int8_t wag_get_direction(const struct libtrace_packet_t *packet) { |
---|
369 | struct frame_data_rx_t *wagptr = (struct frame_data_rx_t *)packet->buffer; |
---|
370 | if (wagptr->hdr.type == 0) { |
---|
371 | return wagptr->hdr.subtype; |
---|
372 | } |
---|
373 | return -1; |
---|
374 | } |
---|
375 | |
---|
376 | static uint64_t wag_get_erf_timestamp(const struct libtrace_packet_t *packet) { |
---|
377 | struct frame_data_rx_t *wagptr = (struct frame_data_rx_t *)packet->buffer; |
---|
378 | uint64_t timestamp = 0; |
---|
379 | timestamp = ((uint64_t)(ntohl(wagptr->ts.secs)) << 32) | (uint64_t)(ntohl(wagptr->ts.subsecs)); |
---|
380 | return timestamp; |
---|
381 | } |
---|
382 | |
---|
383 | static int wag_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
384 | return ntohs(((struct frame_t *)packet->header)->size) |
---|
385 | -sizeof(struct frame_data_rx_t); |
---|
386 | } |
---|
387 | |
---|
388 | static int wag_get_wire_length(const struct libtrace_packet_t *packet) { |
---|
389 | return ntohs(((struct frame_t *)packet->header)->size) |
---|
390 | -sizeof(struct frame_data_rx_t); |
---|
391 | } |
---|
392 | |
---|
393 | static int wag_get_framing_length(UNUSED const libtrace_packet_t *packet) { |
---|
394 | return sizeof(struct frame_data_rx_t); |
---|
395 | } |
---|
396 | |
---|
397 | static int wag_get_fd(const libtrace_t *trace) { |
---|
398 | return DATA(trace)->input.fd; |
---|
399 | } |
---|
400 | |
---|
401 | static void wag_help() { |
---|
402 | printf("wag format module: $Revision$\n"); |
---|
403 | printf("Supported input URIs:\n"); |
---|
404 | printf("\twag:/dev/wagn\n"); |
---|
405 | printf("\n"); |
---|
406 | printf("\te.g.: wag:/dev/wag0\n"); |
---|
407 | printf("\n"); |
---|
408 | printf("Supported output URIs:\n"); |
---|
409 | printf("\tNone\n"); |
---|
410 | printf("\n"); |
---|
411 | } |
---|
412 | |
---|
413 | static void wtf_help() { |
---|
414 | printf("wag trace format module: $Revision$\n"); |
---|
415 | printf("Supported input URIs:\n"); |
---|
416 | printf("\twtf:/path/to/trace.wag\n"); |
---|
417 | printf("\twtf:/path/to/trace.wag.gz\n"); |
---|
418 | printf("\n"); |
---|
419 | printf("\te.g.: wtf:/tmp/trace.wag.gz\n"); |
---|
420 | printf("\n"); |
---|
421 | printf("Supported output URIs:\n"); |
---|
422 | printf("\twtf:/path/to/trace.wag\n"); |
---|
423 | printf("\twtf:/path/to/trace.wag.gz\n"); |
---|
424 | printf("\n"); |
---|
425 | printf("\te.g.: wtf:/tmp/trace.wag.gz\n"); |
---|
426 | printf("\n"); |
---|
427 | } |
---|
428 | |
---|
429 | static struct libtrace_format_t wag = { |
---|
430 | "wag", |
---|
431 | "$Id$", |
---|
432 | TRACE_FORMAT_WAG, |
---|
433 | wag_init_input, /* init_input */ |
---|
434 | NULL, /* config_input */ |
---|
435 | wag_start_input, /* start_input */ |
---|
436 | wag_pause_input, /* pause_input */ |
---|
437 | NULL, /* init_output */ |
---|
438 | NULL, /* config_output */ |
---|
439 | NULL, /* start_output */ |
---|
440 | wag_fin_input, /* fin_input */ |
---|
441 | NULL, /* fin_output */ |
---|
442 | wag_read_packet, /* read_packet */ |
---|
443 | NULL, /* fin_packet */ |
---|
444 | NULL, /* write_packet */ |
---|
445 | wag_get_link_type, /* get_link_type */ |
---|
446 | wag_get_direction, /* get_direction */ |
---|
447 | NULL, /* set_direction */ |
---|
448 | wag_get_erf_timestamp, /* get_erf_timestamp */ |
---|
449 | NULL, /* get_timeval */ |
---|
450 | NULL, /* get_seconds */ |
---|
451 | NULL, /* seek_erf */ |
---|
452 | NULL, /* seek_timeval */ |
---|
453 | NULL, /* seek_seconds */ |
---|
454 | wag_get_capture_length, /* get_capture_length */ |
---|
455 | wag_get_wire_length, /* get_wire_length */ |
---|
456 | wag_get_framing_length, /* get_framing_length */ |
---|
457 | NULL, /* set_capture_length */ |
---|
458 | wag_get_fd, /* get_fd */ |
---|
459 | trace_event_device, /* trace_event */ |
---|
460 | wag_help, /* help */ |
---|
461 | NULL /* next pointer */ |
---|
462 | }; |
---|
463 | |
---|
464 | /* wtf stands for Wag Trace Format */ |
---|
465 | |
---|
466 | static struct libtrace_format_t wag_trace = { |
---|
467 | "wtf", |
---|
468 | "$Id$", |
---|
469 | TRACE_FORMAT_WAG, |
---|
470 | wtf_init_input, /* init_input */ |
---|
471 | NULL, /* config input */ |
---|
472 | wtf_start_input, /* start input */ |
---|
473 | NULL, /* pause_input */ |
---|
474 | wtf_init_output, /* init_output */ |
---|
475 | wtf_config_output, /* config_output */ |
---|
476 | wtf_start_output, /* start output */ |
---|
477 | wtf_fin_input, /* fin_input */ |
---|
478 | wtf_fin_output, /* fin_output */ |
---|
479 | wtf_read_packet, /* read_packet */ |
---|
480 | NULL, /* fin_packet */ |
---|
481 | wtf_write_packet, /* write_packet */ |
---|
482 | wag_get_link_type, /* get_link_type */ |
---|
483 | wag_get_direction, /* get_direction */ |
---|
484 | NULL, /* set_direction */ |
---|
485 | wag_get_erf_timestamp, /* get_erf_timestamp */ |
---|
486 | NULL, /* get_timeval */ |
---|
487 | NULL, /* get_seconds */ |
---|
488 | NULL, /* seek_erf */ |
---|
489 | NULL, /* seek_timeval */ |
---|
490 | NULL, /* seek_seconds */ |
---|
491 | wag_get_capture_length, /* get_capture_length */ |
---|
492 | wag_get_wire_length, /* get_wire_length */ |
---|
493 | wag_get_framing_length, /* get_framing_length */ |
---|
494 | NULL, /* set_capture_length */ |
---|
495 | NULL, /* get_fd */ |
---|
496 | trace_event_trace, /* trace_event */ |
---|
497 | wtf_help, /* help */ |
---|
498 | NULL /* next pointer */ |
---|
499 | }; |
---|
500 | |
---|
501 | |
---|
502 | void CONSTRUCTOR wag_constructor() { |
---|
503 | register_format(&wag); |
---|
504 | register_format(&wag_trace); |
---|
505 | } |
---|