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