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 | return 1; |
---|
149 | } |
---|
150 | |
---|
151 | static int wtf_start_input(libtrace_t *libtrace) |
---|
152 | { |
---|
153 | libtrace->format_data->input.file = trace_open_file(libtrace); |
---|
154 | |
---|
155 | if (libtrace->format_data->input.file) |
---|
156 | return 1; |
---|
157 | |
---|
158 | return 0; |
---|
159 | } |
---|
160 | |
---|
161 | static int wtf_init_output(struct libtrace_out_t *libtrace) { |
---|
162 | libtrace->format_data = (struct libtrace_format_data_out_t *) |
---|
163 | calloc(1,sizeof(struct libtrace_format_data_out_t)); |
---|
164 | |
---|
165 | return 0; |
---|
166 | } |
---|
167 | |
---|
168 | static int wtf_start_output(libtrace_out_t *libtrace) { |
---|
169 | OUTPUT.file = trace_open_file_out(libtrace, |
---|
170 | OPTIONS.zlib.level, |
---|
171 | OPTIONS.zlib.filemode); |
---|
172 | if (!OUTPUT.file) { |
---|
173 | return -1; |
---|
174 | } |
---|
175 | return 0; |
---|
176 | } |
---|
177 | |
---|
178 | static int wtf_config_output(struct libtrace_out_t *libtrace, |
---|
179 | trace_option_output_t option, |
---|
180 | void *value) { |
---|
181 | switch(option) { |
---|
182 | #if HAVE_ZLIB |
---|
183 | case TRACE_OPTION_OUTPUT_COMPRESS: |
---|
184 | OPTIONS.zlib.level = *(int*)value; |
---|
185 | assert(OPTIONS.zlib.level>=0 |
---|
186 | && OPTIONS.zlib.level<=9); |
---|
187 | break; |
---|
188 | #else |
---|
189 | case TRACE_OPTION_OUTPUT_COMPRESS: |
---|
190 | /* E feature unavailable */ |
---|
191 | trace_set_err(TRACE_ERR_OPTION_UNAVAIL, |
---|
192 | "zlib not supported"); |
---|
193 | return -1; |
---|
194 | #endif |
---|
195 | default: |
---|
196 | /* E unknown feature */ |
---|
197 | trace_set_err(TRACE_ERR_UNKNOWN_OPTION, |
---|
198 | "Unknown option"); |
---|
199 | return -1; |
---|
200 | } |
---|
201 | } |
---|
202 | |
---|
203 | static int wag_fin_input(struct libtrace_t *libtrace) { |
---|
204 | close(INPUT.fd); |
---|
205 | return 0; |
---|
206 | } |
---|
207 | |
---|
208 | static int wtf_fin_input(struct libtrace_t *libtrace) { |
---|
209 | LIBTRACE_CLOSE(INPUT.file); |
---|
210 | return 0; |
---|
211 | } |
---|
212 | |
---|
213 | static int wtf_fin_output(struct libtrace_out_t *libtrace) { |
---|
214 | LIBTRACE_CLOSE(OUTPUT.file); |
---|
215 | return 0; |
---|
216 | } |
---|
217 | |
---|
218 | static int wag_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
219 | int numbytes; |
---|
220 | int framesize; |
---|
221 | char *buf_ptr = (char *)buffer; |
---|
222 | int to_read = 0; |
---|
223 | uint16_t magic = 0; |
---|
224 | uint16_t lctr = 0; |
---|
225 | |
---|
226 | assert(libtrace); |
---|
227 | |
---|
228 | to_read = sizeof(struct frame_t); |
---|
229 | |
---|
230 | while (to_read>0) { |
---|
231 | int ret=read(INPUT.fd,buf_ptr,to_read); |
---|
232 | |
---|
233 | if (ret == -1) { |
---|
234 | if (errno == EINTR || errno==EAGAIN) |
---|
235 | continue; |
---|
236 | |
---|
237 | trace_set_err(errno,"read(%s)",libtrace->uridata); |
---|
238 | return -1; |
---|
239 | } |
---|
240 | |
---|
241 | assert(ret>0); |
---|
242 | |
---|
243 | to_read = to_read - ret; |
---|
244 | buf_ptr = buf_ptr + ret; |
---|
245 | } |
---|
246 | |
---|
247 | |
---|
248 | framesize = ntohs(((struct frame_t *)buffer)->size); |
---|
249 | magic = ntohs(((struct frame_t *)buffer)->magic); |
---|
250 | |
---|
251 | if (magic != 0xdaa1) { |
---|
252 | trace_set_err(TRACE_ERR_BAD_PACKET,"magic number bad or missing"); |
---|
253 | return -1; |
---|
254 | } |
---|
255 | |
---|
256 | /* We should deal. this is called "snapping", but we don't yet */ |
---|
257 | assert(framesize>len); |
---|
258 | |
---|
259 | buf_ptr = (void*)((char*)buffer + sizeof (struct frame_t)); |
---|
260 | to_read = framesize - sizeof(struct frame_t); |
---|
261 | |
---|
262 | while (to_read>0) { |
---|
263 | int ret=read(INPUT.fd,buf_ptr,to_read); |
---|
264 | |
---|
265 | if (ret == -1) { |
---|
266 | if (errno == EINTR || errno==EAGAIN) |
---|
267 | continue; |
---|
268 | trace_set_err(errno,"read(%s)",libtrace->uridata); |
---|
269 | return -1; |
---|
270 | } |
---|
271 | |
---|
272 | to_read = to_read - ret; |
---|
273 | buf_ptr = buf_ptr + ret; |
---|
274 | } |
---|
275 | return framesize; |
---|
276 | } |
---|
277 | |
---|
278 | |
---|
279 | static int wag_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
280 | int numbytes; |
---|
281 | |
---|
282 | if (packet->buf_control == EXTERNAL || !packet->buffer) { |
---|
283 | packet->buf_control = PACKET; |
---|
284 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
285 | } |
---|
286 | |
---|
287 | |
---|
288 | packet->trace = libtrace; |
---|
289 | |
---|
290 | if ((numbytes = wag_read(libtrace, (void *)packet->buffer, RP_BUFSIZE)) <= 0) { |
---|
291 | |
---|
292 | return numbytes; |
---|
293 | } |
---|
294 | |
---|
295 | |
---|
296 | packet->size = numbytes; |
---|
297 | packet->header = packet->buffer; |
---|
298 | packet->payload=(char*)packet->buffer+trace_get_framing_length(packet); |
---|
299 | return numbytes; |
---|
300 | } |
---|
301 | |
---|
302 | static int wtf_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
303 | int numbytes; |
---|
304 | void *buffer; |
---|
305 | void *buffer2; |
---|
306 | int framesize; |
---|
307 | int size; |
---|
308 | |
---|
309 | if (packet->buf_control == EXTERNAL || !packet->buffer) { |
---|
310 | packet->buf_control = PACKET; |
---|
311 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
312 | } |
---|
313 | |
---|
314 | buffer2 = buffer = packet->buffer; |
---|
315 | |
---|
316 | if ((numbytes = LIBTRACE_READ(INPUT.file, buffer, sizeof(struct frame_t))) == -1) { |
---|
317 | trace_set_err(errno,"read(%s,frame_t)",packet->trace->uridata); |
---|
318 | return -1; |
---|
319 | } |
---|
320 | |
---|
321 | if (numbytes == 0) { |
---|
322 | return 0; |
---|
323 | } |
---|
324 | |
---|
325 | if (htons(((struct frame_t *)buffer)->magic) != 0xdaa1) { |
---|
326 | trace_set_err(TRACE_ERR_BAD_PACKET,"Insufficient magic"); |
---|
327 | return -1; |
---|
328 | } |
---|
329 | |
---|
330 | framesize = ntohs(((struct frame_t *)buffer)->size); |
---|
331 | buffer2 = (char*)buffer + sizeof(struct frame_t); |
---|
332 | size = framesize - sizeof(struct frame_t); |
---|
333 | assert(size < LIBTRACE_PACKET_BUFSIZE); |
---|
334 | |
---|
335 | |
---|
336 | if ((numbytes=LIBTRACE_READ(INPUT.file, buffer2, size)) != size) { |
---|
337 | trace_set_err(errno,"read(%s,buffer)",packet->trace->uridata); |
---|
338 | return -1; |
---|
339 | } |
---|
340 | |
---|
341 | packet->size = framesize; |
---|
342 | packet->header = packet->buffer; |
---|
343 | packet->payload=(char*)packet->buffer+trace_get_framing_length(packet); |
---|
344 | return framesize; |
---|
345 | |
---|
346 | } |
---|
347 | |
---|
348 | static int wtf_write_packet(struct libtrace_out_t *libtrace, const struct libtrace_packet_t *packet) { |
---|
349 | int numbytes =0 ; |
---|
350 | if (packet->trace->format != &wag_trace) { |
---|
351 | trace_set_err(TRACE_ERR_NO_CONVERSION, |
---|
352 | "Cannot convert from wag trace format to %s format yet", |
---|
353 | packet->trace->format->name); |
---|
354 | return -1; |
---|
355 | } |
---|
356 | |
---|
357 | /* We could just read from packet->buffer, but I feel it is more |
---|
358 | * technically correct to read from the header and payload pointers |
---|
359 | */ |
---|
360 | if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->header, |
---|
361 | trace_get_framing_length(packet))) == -1) { |
---|
362 | trace_set_err(errno,"write(%s)",packet->trace->uridata); |
---|
363 | return -1; |
---|
364 | } |
---|
365 | if ((numbytes = LIBTRACE_WRITE(OUTPUT.file, packet->payload, |
---|
366 | packet->size - trace_get_framing_length(packet))) == -1) { |
---|
367 | trace_set_err(errno,"write(%s)",packet->trace->uridata); |
---|
368 | return -1; |
---|
369 | } |
---|
370 | return numbytes; |
---|
371 | } |
---|
372 | |
---|
373 | static libtrace_linktype_t wag_get_link_type(const struct libtrace_packet_t *packet __attribute__((unused))) { |
---|
374 | return TRACE_TYPE_80211; |
---|
375 | } |
---|
376 | |
---|
377 | static int8_t wag_get_direction(const struct libtrace_packet_t *packet) { |
---|
378 | struct frame_data_rx_t *wagptr = (struct frame_data_rx_t *)packet->buffer; |
---|
379 | if (wagptr->hdr.type == 0) { |
---|
380 | return wagptr->hdr.subtype; |
---|
381 | } |
---|
382 | return -1; |
---|
383 | } |
---|
384 | |
---|
385 | static uint64_t wag_get_erf_timestamp(const struct libtrace_packet_t *packet) { |
---|
386 | struct frame_data_rx_t *wagptr = (struct frame_data_rx_t *)packet->buffer; |
---|
387 | uint64_t timestamp = 0; |
---|
388 | timestamp = ((uint64_t)(ntohl(wagptr->ts.secs)) << 32) | (uint64_t)(ntohl(wagptr->ts.subsecs)); |
---|
389 | return timestamp; |
---|
390 | } |
---|
391 | |
---|
392 | static int wag_get_capture_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_wire_length(const struct libtrace_packet_t *packet) { |
---|
398 | struct frame_data_rx_t *wagptr = (struct frame_data_rx_t *)packet->buffer; |
---|
399 | return ntohs(wagptr->hdr.size); |
---|
400 | } |
---|
401 | |
---|
402 | static int wag_get_framing_length(const struct libtrace_packet_t *packet) { |
---|
403 | return sizeof(struct frame_data_rx_t); |
---|
404 | } |
---|
405 | |
---|
406 | static int wag_get_fd(const struct libtrace_packet_t *packet) { |
---|
407 | return packet->trace->format_data->input.fd; |
---|
408 | } |
---|
409 | |
---|
410 | static void wag_help() { |
---|
411 | printf("wag format module: $Revision$\n"); |
---|
412 | printf("Supported input URIs:\n"); |
---|
413 | printf("\twag:/dev/wagn\n"); |
---|
414 | printf("\n"); |
---|
415 | printf("\te.g.: wag:/dev/wag0\n"); |
---|
416 | printf("\n"); |
---|
417 | printf("Supported output URIs:\n"); |
---|
418 | printf("\tNone\n"); |
---|
419 | printf("\n"); |
---|
420 | } |
---|
421 | |
---|
422 | static void wtf_help() { |
---|
423 | printf("wag trace format module: $Revision$\n"); |
---|
424 | printf("Supported input URIs:\n"); |
---|
425 | printf("\twtf:/path/to/trace.wag\n"); |
---|
426 | printf("\twtf:/path/to/trace.wag.gz\n"); |
---|
427 | printf("\n"); |
---|
428 | printf("\te.g.: wtf:/tmp/trace.wag.gz\n"); |
---|
429 | printf("\n"); |
---|
430 | printf("Supported output URIs:\n"); |
---|
431 | printf("\twtf:/path/to/trace.wag\n"); |
---|
432 | printf("\twtf:/path/to/trace.wag.gz\n"); |
---|
433 | printf("\n"); |
---|
434 | printf("\te.g.: wtf:/tmp/trace.wag.gz\n"); |
---|
435 | printf("\n"); |
---|
436 | } |
---|
437 | |
---|
438 | static struct libtrace_format_t wag = { |
---|
439 | "wag", |
---|
440 | "$Id$", |
---|
441 | TRACE_FORMAT_WAG, |
---|
442 | wag_init_input, /* init_input */ |
---|
443 | NULL, /* config_input */ |
---|
444 | NULL, /* start_input */ |
---|
445 | NULL, /* pause_input */ |
---|
446 | NULL, /* init_output */ |
---|
447 | NULL, /* config_output */ |
---|
448 | NULL, /* start_output */ |
---|
449 | wag_fin_input, /* fin_input */ |
---|
450 | NULL, /* fin_output */ |
---|
451 | wag_read_packet, /* read_packet */ |
---|
452 | NULL, /* write_packet */ |
---|
453 | wag_get_link_type, /* get_link_type */ |
---|
454 | wag_get_direction, /* get_direction */ |
---|
455 | NULL, /* set_direction */ |
---|
456 | wag_get_erf_timestamp, /* get_erf_timestamp */ |
---|
457 | NULL, /* get_timeval */ |
---|
458 | NULL, /* get_seconds */ |
---|
459 | NULL, /* seek_erf */ |
---|
460 | NULL, /* seek_timeval */ |
---|
461 | NULL, /* seek_seconds */ |
---|
462 | wag_get_capture_length, /* get_capture_length */ |
---|
463 | wag_get_wire_length, /* get_wire_length */ |
---|
464 | wag_get_framing_length, /* get_framing_length */ |
---|
465 | NULL, /* set_capture_length */ |
---|
466 | wag_get_fd, /* get_fd */ |
---|
467 | trace_event_device, /* trace_event */ |
---|
468 | wag_help /* help */ |
---|
469 | }; |
---|
470 | |
---|
471 | /* wtf stands for Wag Trace Format */ |
---|
472 | |
---|
473 | static struct libtrace_format_t wag_trace = { |
---|
474 | "wtf", |
---|
475 | "$Id$", |
---|
476 | TRACE_FORMAT_WAG, |
---|
477 | wtf_init_input, /* init_input */ |
---|
478 | NULL, /* config input */ |
---|
479 | wtf_start_input, /* start input */ |
---|
480 | NULL, /* pause_input */ |
---|
481 | wtf_init_output, /* init_output */ |
---|
482 | wtf_config_output, /* config_output */ |
---|
483 | wtf_start_output, /* start output */ |
---|
484 | wtf_fin_input, /* fin_input */ |
---|
485 | wtf_fin_output, /* fin_output */ |
---|
486 | wtf_read_packet, /* read_packet */ |
---|
487 | wtf_write_packet, /* write_packet */ |
---|
488 | wag_get_link_type, /* get_link_type */ |
---|
489 | wag_get_direction, /* get_direction */ |
---|
490 | NULL, /* set_direction */ |
---|
491 | wag_get_erf_timestamp, /* get_erf_timestamp */ |
---|
492 | NULL, /* get_timeval */ |
---|
493 | NULL, /* get_seconds */ |
---|
494 | NULL, /* seek_erf */ |
---|
495 | NULL, /* seek_timeval */ |
---|
496 | NULL, /* seek_seconds */ |
---|
497 | wag_get_capture_length, /* get_capture_length */ |
---|
498 | wag_get_wire_length, /* get_wire_length */ |
---|
499 | wag_get_framing_length, /* get_framing_length */ |
---|
500 | NULL, /* set_capture_length */ |
---|
501 | wag_get_fd, /* get_fd */ |
---|
502 | trace_event_trace, /* trace_event */ |
---|
503 | wtf_help /* help */ |
---|
504 | }; |
---|
505 | |
---|
506 | |
---|
507 | void __attribute__((constructor)) wag_constructor() { |
---|
508 | register_format(&wag); |
---|
509 | register_format(&wag_trace); |
---|
510 | } |
---|