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 "libtrace.h" |
---|
32 | #include "libtrace_int.h" |
---|
33 | #include "format_helper.h" |
---|
34 | #include "wag.h" |
---|
35 | #include "config.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 | #ifdef HAVE_STDDEF_H |
---|
44 | # include <stddef.h> |
---|
45 | #else |
---|
46 | # error "Can't find stddef.h - do you define ptrdiff_t elsewhere?" |
---|
47 | #endif |
---|
48 | #include <sys/types.h> |
---|
49 | #include <sys/time.h> |
---|
50 | #include <time.h> |
---|
51 | #include <sys/socket.h> |
---|
52 | #include <sys/un.h> |
---|
53 | #include <sys/mman.h> |
---|
54 | #include <sys/stat.h> |
---|
55 | #include <fcntl.h> |
---|
56 | #include <unistd.h> |
---|
57 | #include <assert.h> |
---|
58 | #include <errno.h> |
---|
59 | #include <netdb.h> |
---|
60 | |
---|
61 | #ifdef HAVE_LIMITS_H |
---|
62 | # include <limits.h> |
---|
63 | #endif |
---|
64 | |
---|
65 | #ifdef HAVE_SYS_LIMITS_H |
---|
66 | # include <sys/limits.h> |
---|
67 | #endif |
---|
68 | |
---|
69 | #ifndef O_LARGEFILE |
---|
70 | #define O_LARGEFILE 0 |
---|
71 | #endif |
---|
72 | |
---|
73 | static struct libtrace_format_t *wag_ptr = 0; |
---|
74 | |
---|
75 | #define CONNINFO libtrace->format_data->conn_info |
---|
76 | #define INPUT libtrace->format_data->input |
---|
77 | #define OUTPUT libtrace->format_data->output |
---|
78 | #define OPTIONS libtrace->format_data->options |
---|
79 | |
---|
80 | struct libtrace_format_data_t { |
---|
81 | union { |
---|
82 | /** Information about rtclients */ |
---|
83 | struct { |
---|
84 | char *hostname; |
---|
85 | short port; |
---|
86 | } rt; |
---|
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 | FILE *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 | } zlib; |
---|
108 | } options; |
---|
109 | union { |
---|
110 | int fd; |
---|
111 | #if HAVE_ZLIB |
---|
112 | gzFile *file; |
---|
113 | #else |
---|
114 | FILE *file; |
---|
115 | #endif |
---|
116 | } output; |
---|
117 | }; |
---|
118 | |
---|
119 | static int wag_init_input(struct libtrace_t *libtrace) { |
---|
120 | struct stat buf; |
---|
121 | struct hostent *he; |
---|
122 | struct sockaddr_in remote; |
---|
123 | struct sockaddr_un unix_sock; |
---|
124 | libtrace->format_data = (struct libtrace_format_data_t *) |
---|
125 | calloc(1,sizeof(struct libtrace_format_data_t)); |
---|
126 | CONNINFO.path = libtrace->uridata; |
---|
127 | if (!strncmp(CONNINFO.path,"-",1)) { |
---|
128 | libtrace->sourcetype = STDIN; |
---|
129 | // STDIN |
---|
130 | #if HAVE_ZLIB |
---|
131 | INPUT.file = gzdopen(STDIN, "r"); |
---|
132 | #else |
---|
133 | INPUT.file = stdin; |
---|
134 | #endif |
---|
135 | |
---|
136 | } else { |
---|
137 | if (stat(CONNINFO.path,&buf) == -1 ) { |
---|
138 | perror("stat"); |
---|
139 | return 0; |
---|
140 | } |
---|
141 | if (S_ISSOCK(buf.st_mode)) { |
---|
142 | libtrace->sourcetype = SOCKET; |
---|
143 | // SOCKET |
---|
144 | if ((INPUT.fd = socket( |
---|
145 | AF_UNIX, SOCK_STREAM, 0)) == -1) { |
---|
146 | perror("socket"); |
---|
147 | return 0; |
---|
148 | } |
---|
149 | unix_sock.sun_family = AF_UNIX; |
---|
150 | bzero(unix_sock.sun_path,108); |
---|
151 | snprintf(unix_sock.sun_path, |
---|
152 | 108,"%s" |
---|
153 | ,CONNINFO.path); |
---|
154 | |
---|
155 | if (connect(INPUT.fd, |
---|
156 | (struct sockaddr *)&unix_sock, |
---|
157 | sizeof(struct sockaddr)) == -1) { |
---|
158 | perror("connect (unix)"); |
---|
159 | return 0; |
---|
160 | } |
---|
161 | } else { |
---|
162 | // TRACE |
---|
163 | libtrace->sourcetype = TRACE; |
---|
164 | #if HAVE_ZLIB |
---|
165 | // using gzdopen means we can set O_LARGEFILE |
---|
166 | // ourselves. However, this way is messy and |
---|
167 | // we lose any error checking on "open" |
---|
168 | INPUT.file = |
---|
169 | gzdopen(open( |
---|
170 | CONNINFO.path, |
---|
171 | O_LARGEFILE), "r"); |
---|
172 | #else |
---|
173 | INPUT.file = |
---|
174 | fdopen(open( |
---|
175 | CONNINFO.path, |
---|
176 | O_LARGEFILE), "r"); |
---|
177 | #endif |
---|
178 | |
---|
179 | } |
---|
180 | } |
---|
181 | return 1; |
---|
182 | } |
---|
183 | |
---|
184 | static int wag_init_output(struct libtrace_out_t *libtrace) { |
---|
185 | char *filemode = 0; |
---|
186 | libtrace->format_data = (struct libtrace_format_data_out_t *) |
---|
187 | calloc(1,sizeof(struct libtrace_format_data_out_t)); |
---|
188 | |
---|
189 | OPTIONS.zlib.level = 0; |
---|
190 | asprintf(&filemode,"wb%d",OPTIONS.zlib.level); |
---|
191 | if (!strncmp(libtrace->uridata,"-",1)) { |
---|
192 | // STDOUT |
---|
193 | #if HAVE_ZLIB |
---|
194 | OUTPUT.file = gzdopen(dup(1), filemode); |
---|
195 | #else |
---|
196 | OUTPUT.file = stdout; |
---|
197 | #endif |
---|
198 | } else { |
---|
199 | // TRACE |
---|
200 | #if HAVE_ZLIB |
---|
201 | OUTPUT.file = gzdopen(open( |
---|
202 | libtrace->uridata, |
---|
203 | O_CREAT | O_LARGEFILE | O_WRONLY, |
---|
204 | S_IRUSR | S_IWUSR), filemode); |
---|
205 | #else |
---|
206 | OUTPUT.file = fdopen(open( |
---|
207 | O_CREAT | O_LARGEFILE | O_WRONLY, |
---|
208 | S_IRUSR | S_IWUSR), "w"); |
---|
209 | #endif |
---|
210 | } |
---|
211 | |
---|
212 | return 1; |
---|
213 | } |
---|
214 | |
---|
215 | static int wag_config_output(struct libtrace_out_t *libtrace, int argc, char *argv[]) { |
---|
216 | #if HAVE_ZLIB |
---|
217 | int opt; |
---|
218 | int level = OPTIONS.zlib.level; |
---|
219 | optind = 1; |
---|
220 | while ((opt = getopt(argc, argv, "z:")) != EOF) { |
---|
221 | switch (opt) { |
---|
222 | case 'z': |
---|
223 | level = atoi(optarg); |
---|
224 | break; |
---|
225 | default: |
---|
226 | printf("Bad argument to wag: %s\n", opt); |
---|
227 | return -1; |
---|
228 | } |
---|
229 | } |
---|
230 | if (level != OPTIONS.zlib.level) { |
---|
231 | if (level > 9 || level < 0) { |
---|
232 | // retarded level choice |
---|
233 | printf("Compression level must be between 0 and 9 inclusive - you selected %i \n", level); |
---|
234 | } else { |
---|
235 | OPTIONS.zlib.level = level; |
---|
236 | return gzsetparams(OUTPUT.file, level, Z_DEFAULT_STRATEGY); |
---|
237 | } |
---|
238 | } |
---|
239 | #endif |
---|
240 | return 0; |
---|
241 | } |
---|
242 | |
---|
243 | static int wag_fin_input(struct libtrace_t *libtrace) { |
---|
244 | #if HAVE_ZLIB |
---|
245 | gzclose(INPUT.file); |
---|
246 | #else |
---|
247 | fclose(INPUT.file); |
---|
248 | #endif |
---|
249 | } |
---|
250 | |
---|
251 | static int wag_fin_output(struct libtrace_out_t *libtrace) { |
---|
252 | #if HAVE_ZLIB |
---|
253 | gzclose(OUTPUT.file); |
---|
254 | #else |
---|
255 | fclose(OUTPUT.file); |
---|
256 | #endif |
---|
257 | } |
---|
258 | |
---|
259 | static int wag_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
260 | int numbytes; |
---|
261 | static short lctr = 0; |
---|
262 | int rlen; |
---|
263 | assert(libtrace); |
---|
264 | assert(len >= 0); |
---|
265 | |
---|
266 | if (buffer == 0) |
---|
267 | buffer = malloc(len); |
---|
268 | |
---|
269 | while(1) { |
---|
270 | switch(libtrace->sourcetype) { |
---|
271 | case DEVICE: |
---|
272 | if ((numbytes=read(INPUT.fd, |
---|
273 | buffer, |
---|
274 | len)) == -1) { |
---|
275 | perror("read"); |
---|
276 | return -1; |
---|
277 | } |
---|
278 | break; |
---|
279 | default: |
---|
280 | #if HAVE_ZLIB |
---|
281 | if ((numbytes=gzread(INPUT.file, |
---|
282 | buffer, |
---|
283 | len)) == -1) { |
---|
284 | perror("gzread"); |
---|
285 | return -1; |
---|
286 | } |
---|
287 | #else |
---|
288 | if ((numbytes=fread(buffer,len,1, |
---|
289 | INPUT.file)) == 0 ) { |
---|
290 | if(feof(INPUT.file)) { |
---|
291 | return 0; |
---|
292 | } |
---|
293 | if(ferror(INPUT.file)) { |
---|
294 | perror("fread"); |
---|
295 | return -1; |
---|
296 | } |
---|
297 | return 0; |
---|
298 | } |
---|
299 | #endif |
---|
300 | } |
---|
301 | break; |
---|
302 | } |
---|
303 | return numbytes; |
---|
304 | |
---|
305 | } |
---|
306 | |
---|
307 | |
---|
308 | static int wag_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
309 | int numbytes; |
---|
310 | int size; |
---|
311 | char buf[RP_BUFSIZE]; |
---|
312 | int read_required = 0; |
---|
313 | struct wag_frame_hdr *waghdr = 0; |
---|
314 | |
---|
315 | void *buffer = 0; |
---|
316 | |
---|
317 | packet->trace = libtrace; |
---|
318 | buffer = packet->buffer; |
---|
319 | |
---|
320 | |
---|
321 | do { |
---|
322 | if (tracefifo_out_available(libtrace->fifo) == 0 || read_required) { |
---|
323 | if ((numbytes = wag_read(libtrace,buf,RP_BUFSIZE)) <= 0) { |
---|
324 | return numbytes; |
---|
325 | } |
---|
326 | assert(libtrace->fifo); |
---|
327 | tracefifo_write(libtrace->fifo,buf,numbytes); |
---|
328 | read_required = 0; |
---|
329 | } |
---|
330 | // read in wag_frame_hdr |
---|
331 | if ((numbytes = tracefifo_out_read(libtrace->fifo, |
---|
332 | buffer, |
---|
333 | sizeof(struct wag_frame_hdr))) |
---|
334 | == 0 ) { |
---|
335 | tracefifo_out_reset(libtrace->fifo); |
---|
336 | read_required = 1; |
---|
337 | continue; |
---|
338 | } |
---|
339 | |
---|
340 | size = ntohs(((struct wag_frame_hdr *)buffer)->size); |
---|
341 | |
---|
342 | // wag isn't in network byte order yet |
---|
343 | //size = htons(size); |
---|
344 | //printf("%d %d\n",size,htons(size)); |
---|
345 | |
---|
346 | // read in full packet |
---|
347 | if((numbytes = tracefifo_out_read(libtrace->fifo,buffer,size)) == 0) { |
---|
348 | tracefifo_out_reset(libtrace->fifo); |
---|
349 | read_required = 1; |
---|
350 | continue; |
---|
351 | } |
---|
352 | |
---|
353 | // have the whole packet |
---|
354 | tracefifo_out_update(libtrace->fifo,size); |
---|
355 | tracefifo_ack_update(libtrace->fifo,size); |
---|
356 | |
---|
357 | packet->size = numbytes; |
---|
358 | return numbytes; |
---|
359 | } while(1); |
---|
360 | } |
---|
361 | |
---|
362 | static int wag_write_packet(struct libtrace_out_t *libtrace, struct libtrace_packet_t *packet) { |
---|
363 | int numbytes =0 ; |
---|
364 | if (packet->trace->format != wag_ptr) { |
---|
365 | fprintf(stderr,"Cannot convert from wag to %s format yet\n", |
---|
366 | packet->trace->format->name); |
---|
367 | return -1; |
---|
368 | } |
---|
369 | #if HAVE_ZLIB |
---|
370 | if ((numbytes = gzwrite(OUTPUT.file, packet->buffer, packet->size)) == 0) { |
---|
371 | perror("gzwrite"); |
---|
372 | return -1; |
---|
373 | } |
---|
374 | #else |
---|
375 | if ((numbytes = write(OUTPUT.file, packet->buffer, packet->size)) == 0) { |
---|
376 | perror("write"); |
---|
377 | return -1; |
---|
378 | } |
---|
379 | #endif |
---|
380 | return numbytes; |
---|
381 | } |
---|
382 | |
---|
383 | static void *wag_get_link(const struct libtrace_packet_t *packet) { |
---|
384 | struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; |
---|
385 | void *payload = wagptr->data; |
---|
386 | return (void*)payload; |
---|
387 | } |
---|
388 | |
---|
389 | static libtrace_linktype_t wag_get_link_type(const struct libtrace_packet_t *packet) { |
---|
390 | return TRACE_TYPE_80211; |
---|
391 | } |
---|
392 | |
---|
393 | static int8_t wag_get_direction(const struct libtrace_packet_t *packet) { |
---|
394 | struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; |
---|
395 | if (wagptr->hdr.type == 0) { |
---|
396 | return wagptr->hdr.subtype; |
---|
397 | } |
---|
398 | return -1; |
---|
399 | } |
---|
400 | |
---|
401 | static uint64_t wag_get_erf_timestamp(const struct libtrace_packet_t *packet) { |
---|
402 | struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; |
---|
403 | uint64_t timestamp = 0; |
---|
404 | timestamp = wagptr->ts.subsecs; |
---|
405 | //timestamp |= (uint64_t)wagptr->ts.secs<<32; |
---|
406 | timestamp = ((timestamp%44000000)*(UINT_MAX/44000000)) |
---|
407 | | ((timestamp/44000000)<<32); |
---|
408 | return timestamp; |
---|
409 | } |
---|
410 | |
---|
411 | static int wag_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
412 | struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; |
---|
413 | //return (wagptr->hdr.size); |
---|
414 | return ntohs(wagptr->hdr.size); |
---|
415 | } |
---|
416 | |
---|
417 | static int wag_get_wire_length(const struct libtrace_packet_t *packet) { |
---|
418 | struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; |
---|
419 | //return (wagptr->hdr.size); |
---|
420 | return ntohs(wagptr->hdr.size); |
---|
421 | } |
---|
422 | |
---|
423 | static int wag_get_fd(const struct libtrace_packet_t *packet) { |
---|
424 | return packet->trace->format_data->input.fd; |
---|
425 | } |
---|
426 | |
---|
427 | static struct libtrace_eventobj_t wag_event_trace(struct libtrace_t *trace, struct libtrace_packet_t *packet) { |
---|
428 | switch(trace->sourcetype) { |
---|
429 | case DEVICE: |
---|
430 | return trace_event_device(trace,packet); |
---|
431 | default: |
---|
432 | return trace_event_trace(trace,packet); |
---|
433 | } |
---|
434 | } |
---|
435 | static int wag_help() { |
---|
436 | printf("wag format module: $Revision$\n"); |
---|
437 | printf("Supported input URIs:\n"); |
---|
438 | printf("\twag:/dev/wagn\n"); |
---|
439 | printf("\twag:/path/to/trace.wag\n"); |
---|
440 | printf("\twag:/path/to/trace.wag.gz\n"); |
---|
441 | printf("\n"); |
---|
442 | printf("\te.g.: wag:/dev/wag0\n"); |
---|
443 | printf("\te.g.: wag:/tmp/trace.wag.gz\n"); |
---|
444 | printf("\n"); |
---|
445 | printf("Supported output URIs:\n"); |
---|
446 | printf("\tnone\n"); |
---|
447 | printf("\n"); |
---|
448 | |
---|
449 | } |
---|
450 | |
---|
451 | static struct libtrace_format_t wag = { |
---|
452 | "wag", |
---|
453 | "$Id$", |
---|
454 | "wag", |
---|
455 | wag_init_input, /* init_input */ |
---|
456 | wag_init_output, /* init_output */ |
---|
457 | wag_config_output, /* config_output */ |
---|
458 | wag_fin_input, /* fin_input */ |
---|
459 | wag_fin_output, /* fin_output */ |
---|
460 | wag_read_packet, /* read_packet */ |
---|
461 | wag_write_packet, /* write_packet */ |
---|
462 | wag_get_link, /* get_link */ |
---|
463 | wag_get_link_type, /* get_link_type */ |
---|
464 | wag_get_direction, /* get_direction */ |
---|
465 | NULL, /* set_direction */ |
---|
466 | wag_get_erf_timestamp, /* get_erf_timestamp */ |
---|
467 | NULL, /* get_timeval */ |
---|
468 | NULL, /* get_seconds */ |
---|
469 | wag_get_capture_length, /* get_capture_length */ |
---|
470 | wag_get_wire_length, /* get_wire_length */ |
---|
471 | NULL, /* set_capture_length */ |
---|
472 | wag_get_fd, /* get_fd */ |
---|
473 | wag_event_trace, /* trace_event */ |
---|
474 | wag_help /* help */ |
---|
475 | }; |
---|
476 | |
---|
477 | void __attribute__((constructor)) wag_constructor() { |
---|
478 | wag_ptr = &wag; |
---|
479 | register_format(wag_ptr); |
---|
480 | } |
---|