1 | /* |
---|
2 | * This file is part of libtrace |
---|
3 | * |
---|
4 | * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand. |
---|
5 | * Authors: Daniel Lawson |
---|
6 | * Perry Lorier |
---|
7 | * Shane Alcock |
---|
8 | * |
---|
9 | * All rights reserved. |
---|
10 | * |
---|
11 | * This code has been developed by the University of Waikato WAND |
---|
12 | * research group. For further information please see http://www.wand.net.nz/ |
---|
13 | * |
---|
14 | * libtrace is free software; you can redistribute it and/or modify |
---|
15 | * it under the terms of the GNU General Public License as published by |
---|
16 | * the Free Software Foundation; either version 2 of the License, or |
---|
17 | * (at your option) any later version. |
---|
18 | * |
---|
19 | * libtrace is distributed in the hope that it will be useful, |
---|
20 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
21 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
22 | * GNU General Public License for more details. |
---|
23 | * |
---|
24 | * You should have received a copy of the GNU General Public License |
---|
25 | * along with libtrace; if not, write to the Free Software |
---|
26 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
27 | * |
---|
28 | * $Id: format_dag24.c 0 2006-12-14 21:13:09Z spa1 $ |
---|
29 | * |
---|
30 | */ |
---|
31 | #define _GNU_SOURCE |
---|
32 | |
---|
33 | #include "config.h" |
---|
34 | #include "common.h" |
---|
35 | #include "libtrace.h" |
---|
36 | #include "libtrace_int.h" |
---|
37 | #include "format_helper.h" |
---|
38 | #include "format_erf.h" |
---|
39 | |
---|
40 | #include <assert.h> |
---|
41 | #include <errno.h> |
---|
42 | #include <fcntl.h> |
---|
43 | #include <stdio.h> |
---|
44 | #include <string.h> |
---|
45 | #include <stdlib.h> |
---|
46 | |
---|
47 | #include <sys/mman.h> |
---|
48 | #ifdef WIN32 |
---|
49 | # include <io.h> |
---|
50 | # include <share.h> |
---|
51 | # define PATH_MAX _MAX_PATH |
---|
52 | # define snprintf sprintf_s |
---|
53 | #else |
---|
54 | # include <netdb.h> |
---|
55 | # ifndef PATH_MAX |
---|
56 | # define PATH_MAX 4096 |
---|
57 | # endif |
---|
58 | # include <sys/ioctl.h> |
---|
59 | #endif |
---|
60 | |
---|
61 | |
---|
62 | static struct libtrace_format_t dag; |
---|
63 | |
---|
64 | #define DATA(x) ((struct dag_format_data_t *)x->format_data) |
---|
65 | #define DUCK DATA(libtrace)->duck |
---|
66 | #define FORMAT_DATA DATA(libtrace) |
---|
67 | |
---|
68 | struct dag_format_data_t { |
---|
69 | struct { |
---|
70 | uint32_t last_duck; |
---|
71 | uint32_t duck_freq; |
---|
72 | uint32_t last_pkt; |
---|
73 | libtrace_t *dummy_duck; |
---|
74 | } duck; |
---|
75 | |
---|
76 | int fd; |
---|
77 | void *buf; |
---|
78 | uint32_t diff; |
---|
79 | uint32_t offset; |
---|
80 | uint32_t bottom; |
---|
81 | uint32_t top; |
---|
82 | }; |
---|
83 | |
---|
84 | static int dag_available(libtrace_t *libtrace) { |
---|
85 | |
---|
86 | if (FORMAT_DATA->diff > 0) |
---|
87 | return FORMAT_DATA->diff; |
---|
88 | |
---|
89 | FORMAT_DATA->bottom = FORMAT_DATA->top; |
---|
90 | FORMAT_DATA->top = dag_offset( |
---|
91 | FORMAT_DATA->fd, |
---|
92 | &(FORMAT_DATA->bottom), |
---|
93 | DAGF_NONBLOCK); |
---|
94 | FORMAT_DATA->diff = FORMAT_DATA->top - FORMAT_DATA->bottom; |
---|
95 | FORMAT_DATA->offset = 0; |
---|
96 | return FORMAT_DATA->diff; |
---|
97 | } |
---|
98 | |
---|
99 | static int dag_init_input(libtrace_t *libtrace) { |
---|
100 | struct stat buf; |
---|
101 | libtrace->format_data = (struct dag_format_data_t *) |
---|
102 | malloc(sizeof(struct dag_format_data_t)); |
---|
103 | if (stat(libtrace->uridata, &buf) == -1) { |
---|
104 | trace_set_err(libtrace,errno,"stat(%s)",libtrace->uridata); |
---|
105 | return -1; |
---|
106 | } |
---|
107 | |
---|
108 | FORMAT_DATA->top = 0; |
---|
109 | FORMAT_DATA->bottom = 0; |
---|
110 | if (S_ISCHR(buf.st_mode)) { |
---|
111 | /* DEVICE */ |
---|
112 | if((FORMAT_DATA->fd = dag_open(libtrace->uridata)) < 0) { |
---|
113 | trace_set_err(libtrace,errno,"Cannot open DAG %s", |
---|
114 | libtrace->uridata); |
---|
115 | return -1; |
---|
116 | } |
---|
117 | if((FORMAT_DATA->buf = (void *)dag_mmap(FORMAT_DATA->fd)) == MAP_FAILED) { |
---|
118 | trace_set_err(libtrace,errno,"Cannot mmap DAG %s", |
---|
119 | libtrace->uridata); |
---|
120 | return -1; |
---|
121 | } |
---|
122 | } else { |
---|
123 | trace_set_err(libtrace,errno,"Not a valid dag device: %s", |
---|
124 | libtrace->uridata); |
---|
125 | return -1; |
---|
126 | } |
---|
127 | |
---|
128 | DUCK.last_duck = 0; |
---|
129 | DUCK.duck_freq = 0; |
---|
130 | DUCK.last_pkt = 0; |
---|
131 | DUCK.dummy_duck = NULL; |
---|
132 | |
---|
133 | return 0; |
---|
134 | } |
---|
135 | |
---|
136 | static int dag_config_input(libtrace_t *libtrace, trace_option_t option, |
---|
137 | void *data) { |
---|
138 | switch(option) { |
---|
139 | case TRACE_META_FREQ: |
---|
140 | DUCK.duck_freq = *(int *)data; |
---|
141 | return 0; |
---|
142 | case TRACE_OPTION_SNAPLEN: |
---|
143 | /* Surely we can set this?? Fall through for now*/ |
---|
144 | return -1; |
---|
145 | case TRACE_OPTION_PROMISC: |
---|
146 | /* DAG already operates in a promisc fashion */ |
---|
147 | return -1; |
---|
148 | case TRACE_OPTION_FILTER: |
---|
149 | return -1; |
---|
150 | default: |
---|
151 | trace_set_err(libtrace, TRACE_ERR_UNKNOWN_OPTION, |
---|
152 | "Unknown or unsupported option: %i", |
---|
153 | option); |
---|
154 | return -1; |
---|
155 | } |
---|
156 | assert (0); |
---|
157 | } |
---|
158 | |
---|
159 | static int dag_start_input(libtrace_t *libtrace) { |
---|
160 | if(dag_start(FORMAT_DATA->fd) < 0) { |
---|
161 | trace_set_err(libtrace,errno,"Cannot start DAG %s", |
---|
162 | libtrace->uridata); |
---|
163 | return -1; |
---|
164 | } |
---|
165 | |
---|
166 | /* Flush the memory hole */ |
---|
167 | while(dag_available(libtrace) != 0) |
---|
168 | FORMAT_DATA->diff = 0; |
---|
169 | return 0; |
---|
170 | } |
---|
171 | |
---|
172 | static int dag_pause_input(libtrace_t *libtrace) { |
---|
173 | dag_stop(FORMAT_DATA->fd); |
---|
174 | return 0; |
---|
175 | } |
---|
176 | |
---|
177 | static int dag_fin_input(libtrace_t *libtrace) { |
---|
178 | dag_close(FORMAT_DATA->fd); |
---|
179 | if (DUCK.dummy_duck) |
---|
180 | trace_destroy_dead(DUCK.dummy_duck); |
---|
181 | free(libtrace->format_data); |
---|
182 | return 0; /* success */ |
---|
183 | } |
---|
184 | |
---|
185 | static int dag_get_duckinfo(libtrace_t *libtrace, |
---|
186 | libtrace_packet_t *packet) { |
---|
187 | dag_inf lt_dag_inf; |
---|
188 | |
---|
189 | if (packet->buf_control == TRACE_CTRL_EXTERNAL || |
---|
190 | !packet->buffer) { |
---|
191 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
---|
192 | packet->buf_control = TRACE_CTRL_PACKET; |
---|
193 | if (!packet->buffer) { |
---|
194 | trace_set_err(libtrace, errno, |
---|
195 | "Cannot allocate packet buffer"); |
---|
196 | return -1; |
---|
197 | } |
---|
198 | } |
---|
199 | |
---|
200 | packet->header = 0; |
---|
201 | packet->payload = packet->buffer; |
---|
202 | |
---|
203 | if ((ioctl(FORMAT_DATA->fd, DAG_IOINF, <_dag_inf) < 0)) { |
---|
204 | trace_set_err(libtrace, errno, |
---|
205 | "Error using DAG_IOINF"); |
---|
206 | return -1; |
---|
207 | } |
---|
208 | if (!IsDUCK(<_dag_inf)) { |
---|
209 | printf("WARNING: %s does not have modern clock support - No DUCK information will be gathered\n", libtrace->uridata); |
---|
210 | return 0; |
---|
211 | } |
---|
212 | |
---|
213 | if ((ioctl(FORMAT_DATA->fd, DAG_IOGETDUCK, (duck_inf *)packet->payload) |
---|
214 | < 0)) { |
---|
215 | trace_set_err(libtrace, errno, "Error using DAG_IOGETDUCK"); |
---|
216 | return -1; |
---|
217 | } |
---|
218 | |
---|
219 | packet->type = TRACE_RT_DUCK_2_4; |
---|
220 | if (!DUCK.dummy_duck) |
---|
221 | DUCK.dummy_duck = trace_create_dead("duck:dummy"); |
---|
222 | packet->trace = DUCK.dummy_duck; |
---|
223 | return sizeof(duck_inf); |
---|
224 | } |
---|
225 | |
---|
226 | |
---|
227 | dag_record_t *dag_get_record(libtrace_t *libtrace) { |
---|
228 | dag_record_t *erfptr = NULL; |
---|
229 | uint16_t size; |
---|
230 | erfptr = (dag_record_t *) ((char *)FORMAT_DATA->buf + |
---|
231 | (FORMAT_DATA->bottom + FORMAT_DATA->offset)); |
---|
232 | |
---|
233 | if (!erfptr) |
---|
234 | return NULL; |
---|
235 | size = ntohs(erfptr->rlen); |
---|
236 | assert( size >= dag_record_size ); |
---|
237 | FORMAT_DATA->offset += size; |
---|
238 | FORMAT_DATA->diff -= size; |
---|
239 | return erfptr; |
---|
240 | } |
---|
241 | |
---|
242 | void dag_form_packet(dag_record_t *erfptr, libtrace_packet_t *packet) { |
---|
243 | packet->buffer = erfptr; |
---|
244 | packet->header = erfptr; |
---|
245 | packet->type = TRACE_RT_DATA_ERF; |
---|
246 | if (erfptr->flags.rxerror == 1) { |
---|
247 | /* rxerror means the payload is corrupt - drop it |
---|
248 | * by tweaking rlen */ |
---|
249 | packet->payload = NULL; |
---|
250 | erfptr->rlen = htons(erf_get_framing_length(packet)); |
---|
251 | } else { |
---|
252 | packet->payload = (char*)packet->buffer |
---|
253 | + erf_get_framing_length(packet); |
---|
254 | } |
---|
255 | |
---|
256 | } |
---|
257 | |
---|
258 | static int dag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
---|
259 | int numbytes; |
---|
260 | int size = 0; |
---|
261 | struct timeval tv; |
---|
262 | dag_record_t *erfptr = NULL; |
---|
263 | |
---|
264 | if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq && |
---|
265 | DUCK.duck_freq != 0) { |
---|
266 | size = dag_get_duckinfo(libtrace, packet); |
---|
267 | DUCK.last_duck = DUCK.last_pkt; |
---|
268 | if (size != 0) { |
---|
269 | return size; |
---|
270 | } |
---|
271 | /* No DUCK support, so don't waste our time anymore */ |
---|
272 | DUCK.duck_freq = 0; |
---|
273 | } |
---|
274 | |
---|
275 | if (packet->buf_control == TRACE_CTRL_PACKET) { |
---|
276 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
---|
277 | free(packet->buffer); |
---|
278 | packet->buffer = 0; |
---|
279 | } |
---|
280 | |
---|
281 | |
---|
282 | do { |
---|
283 | numbytes = dag_available(libtrace); |
---|
284 | if (numbytes < 0) |
---|
285 | return numbytes; |
---|
286 | if (numbytes == 0) |
---|
287 | continue; |
---|
288 | erfptr = dag_get_record(libtrace); |
---|
289 | } while (erfptr == NULL); |
---|
290 | dag_form_packet(erfptr, packet); |
---|
291 | tv = trace_get_timeval(packet); |
---|
292 | DUCK.last_pkt = tv.tv_sec; |
---|
293 | return packet->payload ? htons(erfptr->rlen) : erf_get_framing_length(packet); |
---|
294 | } |
---|
295 | |
---|
296 | static libtrace_eventobj_t trace_event_dag(libtrace_t *trace, |
---|
297 | libtrace_packet_t *packet) { |
---|
298 | libtrace_eventobj_t event = {0,0,0.0,0}; |
---|
299 | int data; |
---|
300 | |
---|
301 | data = dag_available(trace); |
---|
302 | if (data > 0) { |
---|
303 | event.size = dag_read_packet(trace,packet); |
---|
304 | //DATA(trace)->dag.diff -= event.size; |
---|
305 | if (trace->filter) { |
---|
306 | if (trace_apply_filter(trace->filter, packet)) { |
---|
307 | event.type = TRACE_EVENT_PACKET; |
---|
308 | } else { |
---|
309 | event.type = TRACE_EVENT_SLEEP; |
---|
310 | event.seconds = 0.000001; |
---|
311 | return event; |
---|
312 | } |
---|
313 | } else { |
---|
314 | event.type = TRACE_EVENT_PACKET; |
---|
315 | } |
---|
316 | if (trace->snaplen > 0) { |
---|
317 | trace_set_capture_length(packet, trace->snaplen); |
---|
318 | } |
---|
319 | |
---|
320 | return event; |
---|
321 | } |
---|
322 | assert(data == 0); |
---|
323 | event.type = TRACE_EVENT_SLEEP; |
---|
324 | event.seconds = 0.0001; |
---|
325 | return event; |
---|
326 | } |
---|
327 | |
---|
328 | static void dag_help(void) { |
---|
329 | printf("dag format module: $Revision: 1110 $\n"); |
---|
330 | printf("Supported input URIs:\n"); |
---|
331 | printf("\tdag:/dev/dagn\n"); |
---|
332 | printf("\n"); |
---|
333 | printf("\te.g.: dag:/dev/dag0\n"); |
---|
334 | printf("\n"); |
---|
335 | printf("Supported output URIs:\n"); |
---|
336 | printf("\tnone\n"); |
---|
337 | printf("\n"); |
---|
338 | } |
---|
339 | |
---|
340 | static struct libtrace_format_t dag = { |
---|
341 | "dag", |
---|
342 | "$Id: format_dag24.c 0 2006-12-14 21:13:09Z spa1 $", |
---|
343 | TRACE_FORMAT_ERF, |
---|
344 | dag_init_input, /* init_input */ |
---|
345 | dag_config_input, /* config_input */ |
---|
346 | dag_start_input, /* start_input */ |
---|
347 | dag_pause_input, /* pause_input */ |
---|
348 | NULL, /* init_output */ |
---|
349 | NULL, /* config_output */ |
---|
350 | NULL, /* start_output */ |
---|
351 | dag_fin_input, /* fin_input */ |
---|
352 | NULL, /* fin_output */ |
---|
353 | dag_read_packet, /* read_packet */ |
---|
354 | NULL, /* fin_packet */ |
---|
355 | NULL, /* write_packet */ |
---|
356 | erf_get_link_type, /* get_link_type */ |
---|
357 | erf_get_direction, /* get_direction */ |
---|
358 | erf_set_direction, /* set_direction */ |
---|
359 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
---|
360 | NULL, /* get_timeval */ |
---|
361 | NULL, /* get_seconds */ |
---|
362 | NULL, /* seek_erf */ |
---|
363 | NULL, /* seek_timeval */ |
---|
364 | NULL, /* seek_seconds */ |
---|
365 | erf_get_capture_length, /* get_capture_length */ |
---|
366 | erf_get_wire_length, /* get_wire_length */ |
---|
367 | erf_get_framing_length, /* get_framing_length */ |
---|
368 | erf_set_capture_length, /* set_capture_length */ |
---|
369 | NULL, /* get_fd */ |
---|
370 | trace_event_dag, /* trace_event */ |
---|
371 | dag_help, /* help */ |
---|
372 | NULL /* next pointer */ |
---|
373 | }; |
---|
374 | |
---|
375 | void dag_constructor(void) { |
---|
376 | register_format(&dag); |
---|
377 | } |
---|