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