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 "format.h" |
---|
33 | #include "wag.h" |
---|
34 | |
---|
35 | #ifdef HAVE_INTTYPES_H |
---|
36 | # include <inttypes.h> |
---|
37 | #else |
---|
38 | # error "Can't find inttypes.h - this needs to be fixed" |
---|
39 | #endif |
---|
40 | |
---|
41 | #ifdef HAVE_STDDEF_H |
---|
42 | # include <stddef.h> |
---|
43 | #else |
---|
44 | # error "Can't find stddef.h - do you define ptrdiff_t elsewhere?" |
---|
45 | #endif |
---|
46 | #include <sys/types.h> |
---|
47 | #include <sys/socket.h> |
---|
48 | #include <sys/un.h> |
---|
49 | #include <sys/mman.h> |
---|
50 | #include <sys/stat.h> |
---|
51 | #include <fcntl.h> |
---|
52 | #include <unistd.h> |
---|
53 | #include <assert.h> |
---|
54 | #include <errno.h> |
---|
55 | #include <netdb.h> |
---|
56 | |
---|
57 | #ifndef O_LARGEFILE |
---|
58 | #define O_LARGEFILE 0 |
---|
59 | #endif |
---|
60 | static int wag_init_input(struct libtrace_t *libtrace) { |
---|
61 | struct stat buf; |
---|
62 | struct hostent *he; |
---|
63 | struct sockaddr_in remote; |
---|
64 | struct sockaddr_un unix_sock; |
---|
65 | if (!strncmp(libtrace->conn_info.path,"-",1)) { |
---|
66 | // STDIN |
---|
67 | #if HAVE_ZLIB |
---|
68 | libtrace->input.file = gzdopen(STDIN, "r"); |
---|
69 | #else |
---|
70 | libtrace->input.file = stdin; |
---|
71 | #endif |
---|
72 | |
---|
73 | } else { |
---|
74 | if (stat(libtrace->conn_info.path,&buf) == -1 ) { |
---|
75 | perror("stat"); |
---|
76 | return 0; |
---|
77 | } |
---|
78 | if (S_ISSOCK(buf.st_mode)) { |
---|
79 | // SOCKET |
---|
80 | if ((libtrace->input.fd = socket( |
---|
81 | AF_UNIX, SOCK_STREAM, 0)) == -1) { |
---|
82 | perror("socket"); |
---|
83 | return 0; |
---|
84 | } |
---|
85 | unix_sock.sun_family = AF_UNIX; |
---|
86 | bzero(unix_sock.sun_path,108); |
---|
87 | snprintf(unix_sock.sun_path, |
---|
88 | 108,"%s" |
---|
89 | ,libtrace->conn_info.path); |
---|
90 | |
---|
91 | if (connect(libtrace->input.fd, |
---|
92 | (struct sockaddr *)&unix_sock, |
---|
93 | sizeof(struct sockaddr)) == -1) { |
---|
94 | perror("connect (unix)"); |
---|
95 | return 0; |
---|
96 | } |
---|
97 | } else { |
---|
98 | // TRACE |
---|
99 | #if HAVE_ZLIB |
---|
100 | // using gzdopen means we can set O_LARGEFILE |
---|
101 | // ourselves. However, this way is messy and |
---|
102 | // we lose any error checking on "open" |
---|
103 | libtrace->input.file = |
---|
104 | gzdopen(open( |
---|
105 | libtrace->conn_info.path, |
---|
106 | O_LARGEFILE), "r"); |
---|
107 | #else |
---|
108 | libtrace->input.file = |
---|
109 | fdopen(open( |
---|
110 | libtrace->conn_info.path, |
---|
111 | O_LARGEFILE), "r"); |
---|
112 | #endif |
---|
113 | |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | |
---|
118 | static int wag_fin_input(struct libtrace_t *libtrace) { |
---|
119 | #if HAVE_ZLIB |
---|
120 | gzclose(libtrace->input.file); |
---|
121 | #else |
---|
122 | fclose(libtrace->input.file); |
---|
123 | #endif |
---|
124 | } |
---|
125 | |
---|
126 | static int wag_read(struct libtrace_t *libtrace, void *buffer, size_t len) { |
---|
127 | int numbytes; |
---|
128 | static short lctr = 0; |
---|
129 | int rlen; |
---|
130 | assert(libtrace); |
---|
131 | assert(len >= 0); |
---|
132 | |
---|
133 | if (buffer == 0) |
---|
134 | buffer = malloc(len); |
---|
135 | |
---|
136 | while(1) { |
---|
137 | switch(libtrace->sourcetype) { |
---|
138 | case DEVICE: |
---|
139 | if ((numbytes=read(libtrace->input.fd, |
---|
140 | buffer, |
---|
141 | len)) == -1) { |
---|
142 | perror("read"); |
---|
143 | return -1; |
---|
144 | } |
---|
145 | break; |
---|
146 | default: |
---|
147 | #if HAVE_ZLIB |
---|
148 | if ((numbytes=gzread(libtrace->input.file, |
---|
149 | buffer, |
---|
150 | len)) == -1) { |
---|
151 | perror("gzread"); |
---|
152 | return -1; |
---|
153 | } |
---|
154 | #else |
---|
155 | if ((numbytes=fread(buffer,len,1, |
---|
156 | libtrace->input.file)) == 0 ) { |
---|
157 | if(feof(libtrace->input.file)) { |
---|
158 | return 0; |
---|
159 | } |
---|
160 | if(ferror(libtrace->input.file)) { |
---|
161 | perror("fread"); |
---|
162 | return -1; |
---|
163 | } |
---|
164 | return 0; |
---|
165 | } |
---|
166 | #endif |
---|
167 | } |
---|
168 | break; |
---|
169 | } |
---|
170 | return numbytes; |
---|
171 | |
---|
172 | } |
---|
173 | |
---|
174 | |
---|
175 | static int wag_read_packet(struct libtrace_t *libtrace, struct libtrace_packet_t *packet) { |
---|
176 | int numbytes; |
---|
177 | int size; |
---|
178 | char buf[RP_BUFSIZE]; |
---|
179 | int read_required = 0; |
---|
180 | struct wag_frame_hdr *waghdr = 0; |
---|
181 | |
---|
182 | void *buffer = 0; |
---|
183 | |
---|
184 | packet->trace = libtrace; |
---|
185 | buffer = packet->buffer; |
---|
186 | |
---|
187 | |
---|
188 | do { |
---|
189 | if (fifo_out_available(libtrace->fifo) == 0 || read_required) { |
---|
190 | if ((numbytes = wag_read(libtrace,buf,RP_BUFSIZE)) <= 0) { |
---|
191 | return numbytes; |
---|
192 | } |
---|
193 | assert(libtrace->fifo); |
---|
194 | fifo_write(libtrace->fifo,buf,numbytes); |
---|
195 | read_required = 0; |
---|
196 | } |
---|
197 | // read in wag_frame_hdr |
---|
198 | if ((numbytes = fifo_out_read(libtrace->fifo, |
---|
199 | buffer, |
---|
200 | sizeof(struct wag_frame_hdr))) |
---|
201 | == 0 ) { |
---|
202 | fifo_out_reset(libtrace->fifo); |
---|
203 | read_required = 1; |
---|
204 | continue; |
---|
205 | } |
---|
206 | |
---|
207 | size = ntohs(((struct wag_frame_hdr *)buffer)->size); |
---|
208 | |
---|
209 | // wag isn't in network byte order yet |
---|
210 | size = htons(size); |
---|
211 | //printf("%d %d\n",size,htons(size)); |
---|
212 | |
---|
213 | // read in full packet |
---|
214 | if((numbytes = fifo_out_read(libtrace->fifo,buffer,size)) == 0) { |
---|
215 | fifo_out_reset(libtrace->fifo); |
---|
216 | read_required = 1; |
---|
217 | continue; |
---|
218 | } |
---|
219 | |
---|
220 | // have the whole packet |
---|
221 | fifo_out_update(libtrace->fifo,size); |
---|
222 | fifo_ack_update(libtrace->fifo,size); |
---|
223 | |
---|
224 | packet->size = numbytes; |
---|
225 | return numbytes; |
---|
226 | } while(1); |
---|
227 | } |
---|
228 | |
---|
229 | static void *wag_get_link(const struct libtrace_packet_t *packet) { |
---|
230 | struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; |
---|
231 | void *payload = wagptr->data; |
---|
232 | return (void*)payload; |
---|
233 | } |
---|
234 | |
---|
235 | static libtrace_linktype_t wag_get_link_type(const struct libtrace_packet_t *packet) { |
---|
236 | return TRACE_TYPE_80211; |
---|
237 | } |
---|
238 | |
---|
239 | static int8_t wag_get_direction(const struct libtrace_packet_t *packet) { |
---|
240 | struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; |
---|
241 | if (wagptr->hdr.type == 0) { |
---|
242 | return wagptr->hdr.subtype; |
---|
243 | } |
---|
244 | return -1; |
---|
245 | } |
---|
246 | |
---|
247 | static uint64_t wag_get_erf_timestamp(const struct libtrace_packet_t *packet) { |
---|
248 | struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; |
---|
249 | uint64_t timestamp = 0; |
---|
250 | timestamp = wagptr->ts.subsecs; |
---|
251 | timestamp |= (uint64_t)wagptr->ts.secs<<32; |
---|
252 | timestamp = ((timestamp%44000000)*(UINT_MAX/44000000)) |
---|
253 | | ((timestamp/44000000)<<32); |
---|
254 | return timestamp; |
---|
255 | } |
---|
256 | |
---|
257 | static int wag_get_capture_length(const struct libtrace_packet_t *packet) { |
---|
258 | struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; |
---|
259 | return (wagptr->hdr.size); |
---|
260 | //return ntohs(wagptr->hdr.size); |
---|
261 | } |
---|
262 | |
---|
263 | static int wag_get_wire_length(const struct libtrace_packet_t *packet) { |
---|
264 | struct wag_data_frame *wagptr = (struct wag_data_frame *)packet->buffer; |
---|
265 | return (wagptr->hdr.size); |
---|
266 | //return ntohs(wagptr->hdr.size); |
---|
267 | } |
---|
268 | |
---|
269 | static int wag_help() { |
---|
270 | |
---|
271 | } |
---|
272 | |
---|
273 | static struct format_t wag = { |
---|
274 | "wag", |
---|
275 | "$Id$", |
---|
276 | wag_init_input, /* init_input */ |
---|
277 | NULL, /* init_output */ |
---|
278 | NULL, /* config_output */ |
---|
279 | wag_fin_input, /* fin_input */ |
---|
280 | NULL, /* fin_output */ |
---|
281 | NULL, /* read */ |
---|
282 | wag_read_packet, /* read_packet */ |
---|
283 | NULL, /* write_packet */ |
---|
284 | wag_get_link, /* get_link */ |
---|
285 | wag_get_link_type, /* get_link_type */ |
---|
286 | wag_get_direction, /* get_direction */ |
---|
287 | NULL, /* set_direction */ |
---|
288 | wag_get_erf_timestamp, /* get_wag_timestamp */ |
---|
289 | NULL, /* get_timeval */ |
---|
290 | NULL, /* get_seconds */ |
---|
291 | wag_get_capture_length, /* get_capture_length */ |
---|
292 | wag_get_wire_length, /* get_wire_length */ |
---|
293 | NULL, /* set_capture_length */ |
---|
294 | wag_help /* help */ |
---|
295 | }; |
---|
296 | |
---|
297 | void __attribute__((constructor)) wag_constructor() { |
---|
298 | register_format(&wag); |
---|
299 | } |
---|