source: lib/format_helper.c @ 74ecbc7

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since 74ecbc7 was 74ecbc7, checked in by Shane Alcock <salcock@…>, 7 years ago

Add more user-friendly checks for output file compression options

  • No longer assert fail if the level is outside the allowed range
  • Actually check that a valid compression method has been provided
  • Failing either check will result in NULL being returned by trace_open_file_out and the libtrace error being set appropriately
  • Added TRACE_OPTION_COMPRESSTYPE_LAST to compression method enum, so we can always easily tell if the value is outside the expected range.
  • Property mode set to 100644
File size: 9.0 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                trace->event.packet = trace_create_packet();
146        }
147
148        if (!trace->event.waiting) {
149                /* There is no packet event waiting for us, so create a new
150                 * libtrace packet in the event structure and read the next
151                 * packet into that.
152                 *
153                 * If a SLEEP event is reported this time around, the read
154                 * packet can therefore be saved until the next time this
155                 * function is called. */
156
157                trace->event.psize=
158                        trace_read_packet(trace,trace->event.packet);
159                if (trace->event.psize<1) {
160                        /* Return here, the test for event.size will sort out
161                         * the error  */
162                        if (trace_is_err(trace)) {
163                                trace_perror(trace, "read packet");
164                        }
165                        event.type = TRACE_EVENT_TERMINATE;
166                        trace_destroy_packet(trace->event.packet);
167                        trace->event.packet = NULL;
168                        packet->buffer = NULL;
169                        packet->header = NULL;
170                        packet->payload = NULL;
171                        packet->buf_control = TRACE_CTRL_EXTERNAL;
172                        return event;
173                }
174        }
175
176        /* The goal here is to replicate the inter-packet gaps that are
177         * present in the trace. */
178
179        ts=trace_get_seconds(trace->event.packet);
180
181        /* Get the current walltime */
182#ifdef WIN32
183        _ftime64(&tstruct);
184        now = tstruct.time + 
185                ((double)tstruct.millitm / 1000.0);
186#else
187        gettimeofday(&stv, NULL);
188        now = stv.tv_sec + 
189                ((double)stv.tv_usec / 1000000.0);
190#endif
191
192       
193        if (fabs(trace->event.tdelta)>1e-9) {
194                /* Subtract the tdelta from the walltime to get a suitable
195                 * "relative" time */
196                now -= trace->event.tdelta; 
197
198                /* If the trace timestamp is still in the future, return a
199                 * SLEEP event, otherwise return the packet */
200                if (ts > now) {
201                        event.seconds = ts - 
202                                trace->event.trace_last_ts;
203                        trace->event.trace_last_ts = ts;
204                        event.type = TRACE_EVENT_SLEEP;
205                        trace->event.waiting = true;
206                        return event;
207                }
208        } else {
209                /* Work out the difference between the walltime at the start
210                 * of the trace replay and the timestamp of the first packet
211                 * in the trace. This will be used to convert the walltime
212                 * into a timeline that is relative to the timestamps in the
213                 * trace file.
214                 */
215                trace->event.tdelta = now - ts;
216        }
217
218        /* The packet that we had read earlier is now ready to be returned
219         * to the user - switch all the pointers etc. over */   
220        packet->type = trace->event.packet->type;
221        packet->trace = trace->event.packet->trace;
222        packet->header = trace->event.packet->header;
223        packet->payload = trace->event.packet->payload;
224       
225        packet->buffer = trace->event.packet->buffer;
226        packet->buf_control = trace->event.packet->buf_control;
227
228        event.type = TRACE_EVENT_PACKET;
229
230        trace->event.trace_last_ts = ts;
231        trace->event.waiting = false;
232
233        return event;
234}
235
236/* Catch undefined O_LARGEFILE on *BSD etc */
237#ifndef O_LARGEFILE
238#  define O_LARGEFILE 0
239#endif
240
241/* Catching O_BINARY on all sane OS's */
242#ifndef O_BINARY
243#  define O_BINARY 0
244#endif
245
246/* Open a file for reading using the new Libtrace IO system */
247io_t *trace_open_file(libtrace_t *trace)
248{
249        io_t *io=wandio_create(trace->uridata);
250
251        if (!io) {
252                if (errno != 0) {
253                        trace_set_err(trace,errno,"Unable to open %s",trace->uridata);
254                } else {
255                        trace_set_err(trace,TRACE_ERR_UNSUPPORTED_COMPRESS,"Unsupported compression error: %s", trace->uridata);
256                }
257        }
258        return io;
259}
260
261/* Open a file for writing using the new Libtrace IO system */ 
262iow_t *trace_open_file_out(libtrace_out_t *trace, int compress_type, int level, int fileflag)
263{
264        iow_t *io = NULL;
265
266        if (level < 0 || level > 9) {
267                trace_set_err_out(trace, TRACE_ERR_UNSUPPORTED_COMPRESS, 
268                                "Compression level %d is invalid, must be between 0 and 9 inclusive", 
269                                level);
270                return NULL;
271        }
272
273        if (compress_type < 0 || 
274                        compress_type >= TRACE_OPTION_COMPRESSTYPE_LAST) {
275                trace_set_err_out(trace, TRACE_ERR_UNSUPPORTED_COMPRESS,
276                                "Invalid compression type %d", compress_type);
277                return NULL;
278        }
279
280        io = wandio_wcreate(trace->uridata, compress_type, level, fileflag);
281
282        if (!io) {
283                trace_set_err_out(trace, errno, "Unable to create output file %s", trace->uridata);
284        }
285        return io;
286}
287
288
289/** Sets the error status for an input trace
290 * @param errcode either an Econstant from libc, or a LIBTRACE_ERROR
291 * @param msg a plaintext error message
292 * @internal
293 */
294void trace_set_err(libtrace_t *trace,int errcode,const char *msg,...)
295{
296        char buf[256];
297        va_list va;
298        va_start(va,msg);
299        assert(errcode != 0 && "An error occurred, but it is unknown what it is");
300        trace->err.err_num=errcode;
301        if (errcode>0) {
302                vsnprintf(buf,sizeof(buf),msg,va);
303                snprintf(trace->err.problem,sizeof(trace->err.problem),
304                                "%s: %s",buf,strerror(errcode));
305        } else {
306                vsnprintf(trace->err.problem,sizeof(trace->err.problem),
307                                msg,va);
308        }
309        va_end(va);
310}
311
312/** Sets the error status for an output trace
313 * @param errcode either an Econstant from libc, or a LIBTRACE_ERROR
314 * @param msg a plaintext error message
315 * @internal
316 */
317void trace_set_err_out(libtrace_out_t *trace,int errcode,const char *msg,...)
318{
319        char buf[256];
320        va_list va;
321        va_start(va,msg);
322        assert(errcode != 0 && "An error occurred, but it is unknown what it is");
323        trace->err.err_num=errcode;
324        if (errcode>0) {
325                vsnprintf(buf,sizeof(buf),msg,va);
326                snprintf(trace->err.problem,sizeof(trace->err.problem),
327                                "%s: %s",buf,strerror(errno));
328        } else {
329                vsnprintf(trace->err.problem,sizeof(trace->err.problem),
330                                msg,va);
331        }
332        va_end(va);
333}
Note: See TracBrowser for help on using the repository browser.