source: lib/format_helper.c @ 238d50a

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 238d50a was 238d50a, checked in by Shane Alcock <salcock@…>, 12 years ago
  • Updated licensing and documentation for several format modules and the format_helper code
  • Property mode set to 100644
File size: 8.9 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007,2008,2009,2010 The University of Waikato, Hamilton,
5 * New Zealand.
6 *
7 * Authors: Daniel Lawson
8 *          Perry Lorier
9 *          Shane Alcock
10 *         
11 * All rights reserved.
12 *
13 * This code has been developed by the University of Waikato WAND
14 * research group. For further information please see http://www.wand.net.nz/
15 *
16 * libtrace is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
20 *
21 * libtrace is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24 * GNU General Public License for more details.
25 *
26 * You should have received a copy of the GNU General Public License
27 * along with libtrace; if not, write to the Free Software
28 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
29 *
30 * $Id$
31 *
32 */
33
34#include "config.h"
35#include <sys/types.h>
36#include <fcntl.h> /* for O_LARGEFILE */
37#include <math.h>
38#include "libtrace.h"
39#include "libtrace_int.h"
40#include "wandio.h"
41
42#include <stdlib.h>
43#include <stdio.h>
44#include <string.h>
45#include <errno.h>
46#include <time.h>
47#include "format_helper.h"
48
49#include <assert.h>
50#include <stdarg.h>
51
52#ifdef WIN32
53#  include <io.h>
54#  include <share.h>
55#  include <sys/timeb.h>
56
57struct libtrace_eventobj_t trace_event_device(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
58    struct libtrace_eventobj_t event = {0,0,0.0,0};
59
60    trace_set_err(trace,TRACE_ERR_OPTION_UNAVAIL, "trace_event() is not "
61            "supported on devices under windows in this version");
62
63    event.type = TRACE_EVENT_TERMINATE;
64    return event;
65}
66#else
67#  include <sys/ioctl.h>
68
69/* Generic event function for live capture devices / interfaces */
70struct libtrace_eventobj_t trace_event_device(struct libtrace_t *trace, 
71                                        struct libtrace_packet_t *packet) {
72        struct libtrace_eventobj_t event = {0,0,0.0,0};
73
74        fd_set rfds, rfds_param;
75        int ret;
76        int max_fd;
77        struct timeval tv;
78
79        assert(trace != NULL);
80        assert(packet != NULL);
81       
82        FD_ZERO(&rfds);
83        FD_ZERO(&rfds_param);
84
85        if (trace->format->get_fd) {
86                event.fd = trace->format->get_fd(trace);
87                FD_SET(event.fd, &rfds);
88                max_fd = event.fd;
89        } else {
90                event.fd = 0;
91                max_fd = -1;
92        }
93
94        /* Use select() to perform a quick poll to check that there is data
95         * available - we used to use FIONREAD here but that does not work
96         * for mmapped pcap sockets. As recent pcap on linux (e.g. Ubuntu 9.04)
97         * uses mmapped sockets by default, I've switched over to this
98         * solution. */
99
100        do {
101                tv.tv_sec = 0;
102                tv.tv_usec = 0;
103                rfds_param = rfds;
104
105                ret = select(max_fd + 1, &rfds_param, NULL, NULL, &tv);
106                if (ret == -1 && errno != EINTR) {
107                        event.type = TRACE_EVENT_TERMINATE;
108                        return event;
109                }
110        } while (ret == -1);
111
112        if (FD_ISSET(event.fd, &rfds_param)) {
113                event.size = trace_read_packet(trace,packet);
114               
115                if (event.size < 1) {
116                        /* Covers error and EOF events - terminate rather
117                         * than report a packet as available */
118                        if (trace_is_err(trace)) {
119                                trace_perror(trace, "read packet");
120                        }
121                        event.type = TRACE_EVENT_TERMINATE;
122                } else {
123
124                        event.type = TRACE_EVENT_PACKET;
125                }
126                return event;
127        }
128        event.type= TRACE_EVENT_IOWAIT;
129        return event;
130}
131#endif
132
133/* Generic event function for trace files */ 
134struct libtrace_eventobj_t trace_event_trace(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
135        struct libtrace_eventobj_t event = {0,0,0.0,0};
136        double ts;
137        double now;
138#ifdef WIN32
139        struct __timeb64 tstruct;
140#else
141        struct timeval stv;
142#endif
143
144        if (!trace->event.packet) {
145                /* There is no packet event waiting for us, so create a new
146                 * libtrace packet in the event structure and read the next
147                 * packet into that.
148                 *
149                 * If a SLEEP event is reported this time around, the read
150                 * packet can therefore be saved until the next time this
151                 * function is called. */
152
153                trace->event.packet = trace_create_packet();
154                trace->event.psize=
155                        trace_read_packet(trace,trace->event.packet);
156                if (trace->event.psize<1) {
157                        /* Return here, the test for event.size will sort out
158                         * the error  */
159                        if (trace_is_err(trace)) {
160                                trace_perror(trace, "read packet");
161                        }
162                        event.type = TRACE_EVENT_TERMINATE;
163                        trace_destroy_packet(trace->event.packet);
164                        trace->event.packet = NULL;
165                        return event;
166                }
167        }
168
169        /* The goal here is to replicate the inter-packet gaps that are
170         * present in the trace. */
171
172        ts=trace_get_seconds(trace->event.packet);
173
174        /* Get the current walltime */
175#ifdef WIN32
176        _ftime64(&tstruct);
177        now = tstruct.time + 
178                ((double)tstruct.millitm / 1000.0);
179#else
180        gettimeofday(&stv, NULL);
181        now = stv.tv_sec + 
182                ((double)stv.tv_usec / 1000000.0);
183#endif
184
185       
186        if (fabs(trace->event.tdelta)>1e-9) {
187                /* Subtract the tdelta from the walltime to get a suitable
188                 * "relative" time */
189                now -= trace->event.tdelta; 
190
191                /* If the trace timestamp is still in the future, return a
192                 * SLEEP event, otherwise return the packet */
193                if (ts > now) {
194                        event.seconds = ts - 
195                                trace->event.trace_last_ts;
196                        event.type = TRACE_EVENT_SLEEP;
197                        return event;
198                }
199        } else {
200                /* Work out the difference between the walltime at the start
201                 * of the trace replay and the timestamp of the first packet
202                 * in the trace. This will be used to convert the walltime
203                 * into a timeline that is relative to the timestamps in the
204                 * trace file.
205                 */
206                trace->event.tdelta = now - ts;
207        }
208
209        /* This is the first packet, so just fire away. */
210        /* TODO: finalise packet */
211       
212        /* XXX: Could we do this more efficiently? */
213
214        /* We do a lot of freeing and creating of packet buffers with this
215         * method, but at least it works unlike what was here previously */
216        if (packet->buf_control == TRACE_CTRL_PACKET) {
217                free(packet->buffer);
218        }
219       
220        /* The packet that we had read earlier is now ready to be returned
221         * to the user - switch all the pointers etc. over */   
222        packet->type = trace->event.packet->type;
223        packet->trace = trace->event.packet->trace;
224        packet->header = trace->event.packet->header;
225        packet->payload = trace->event.packet->payload;
226       
227        packet->buffer = trace->event.packet->buffer;
228        packet->buf_control = trace->event.packet->buf_control;
229
230        trace->event.packet->buffer = NULL;
231        trace->event.packet->buf_control = TRACE_CTRL_EXTERNAL;
232       
233        trace_destroy_packet(trace->event.packet);
234        trace->event.packet = NULL;
235
236        event.type = TRACE_EVENT_PACKET;
237
238        trace->event.trace_last_ts = ts;
239
240        return event;
241}
242
243/* Catch undefined O_LARGEFILE on *BSD etc */
244#ifndef O_LARGEFILE
245#  define O_LARGEFILE 0
246#endif
247
248/* Catching O_BINARY on all sane OS's */
249#ifndef O_BINARY
250#  define O_BINARY 0
251#endif
252
253/* Open a file for reading using the new Libtrace IO system */
254io_t *trace_open_file(libtrace_t *trace)
255{
256        io_t *io=wandio_create(trace->uridata);
257        if (!io) {
258                trace_set_err(trace,errno,"Unable to open %s",trace->uridata);
259        }
260        return io;
261}
262
263/* Open a file for writing using the new Libtrace IO system */ 
264iow_t *trace_open_file_out(libtrace_out_t *trace,int level, int fileflag)
265{
266        assert(level<10);
267        assert(level>=0);
268
269        return wandio_wcreate(trace->uridata, level, fileflag);
270}
271
272
273/** Sets the error status for an input trace
274 * @param errcode either an Econstant from libc, or a LIBTRACE_ERROR
275 * @param msg a plaintext error message
276 * @internal
277 */
278void trace_set_err(libtrace_t *trace,int errcode,const char *msg,...)
279{
280        char buf[256];
281        va_list va;
282        va_start(va,msg);
283        assert(errcode != 0 && "An error occurred, but it is unknown what it is");
284        trace->err.err_num=errcode;
285        if (errcode>0) {
286                vsnprintf(buf,sizeof(buf),msg,va);
287                snprintf(trace->err.problem,sizeof(trace->err.problem),
288                                "%s: %s",buf,strerror(errcode));
289        } else {
290                vsnprintf(trace->err.problem,sizeof(trace->err.problem),
291                                msg,va);
292        }
293        va_end(va);
294}
295
296/** Sets the error status for an output trace
297 * @param errcode either an Econstant from libc, or a LIBTRACE_ERROR
298 * @param msg a plaintext error message
299 * @internal
300 */
301void trace_set_err_out(libtrace_out_t *trace,int errcode,const char *msg,...)
302{
303        char buf[256];
304        va_list va;
305        va_start(va,msg);
306        assert(errcode != 0 && "An error occurred, but it is unknown what it is");
307        trace->err.err_num=errcode;
308        if (errcode>0) {
309                vsnprintf(buf,sizeof(buf),msg,va);
310                snprintf(trace->err.problem,sizeof(trace->err.problem),
311                                "%s: %s",buf,strerror(errno));
312        } else {
313                vsnprintf(trace->err.problem,sizeof(trace->err.problem),
314                                msg,va);
315        }
316        va_end(va);
317}
318
319/* Byte swapping functions for various inttypes */
320uint64_t byteswap64(uint64_t num)
321{
322        return (byteswap32((num&0xFFFFFFFF00000000ULL)>>32))
323              |((uint64_t)byteswap32(num&0x00000000FFFFFFFFULL)<<32);
324}
325
326uint32_t byteswap32(uint32_t num)
327{
328        return ((num&0x000000FFU)<<24)
329                | ((num&0x0000FF00U)<<8)
330                | ((num&0x00FF0000U)>>8)
331                | ((num&0xFF000000U)>>24);
332}
333
334uint16_t byteswap16(uint16_t num)
335{
336        return ((num<<8)&0xFF00)|((num>>8)&0x00FF);
337}
338
Note: See TracBrowser for help on using the repository browser.