source: lib/format_helper.c @ 9836f12

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

Fixed get_fd call in format_helper to use trace as a parameter instead of packet
pcap_get_wire_length no longer ntohs() the len field of the pcap header
rt_read_packet no longer requires the tracefifo

  • Property mode set to 100644
File size: 5.2 KB
Line 
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 "config.h"
34
35#include <stdlib.h>
36#include <stdio.h>
37#include <string.h>
38#include <errno.h>
39#include <fcntl.h> /* for O_LARGEFILE */
40#ifdef HAVE_INTTYPES_H
41#  include <inttypes.h>
42#else
43#  error "Can't find inttypes.h - this needs to be fixed"
44#endif
45#include  "format_helper.h"
46
47#include <sys/ioctl.h>
48#include <assert.h>
49
50struct libtrace_eventobj_t trace_event_device(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
51        struct libtrace_eventobj_t event = {0,0,0.0,0};
52        int data;
53
54        assert(trace);
55        assert(packet);
56       
57        if (trace->format->get_fd) {
58                event.fd = trace->format->get_fd(trace);
59        } else {
60                event.fd = 0;
61        }
62        if (ioctl(event.fd,FIONREAD,&data)==-1) {
63                event.type = TRACE_EVENT_TERMINATE;
64                return event;
65        }
66        if (data>0) {
67                event.size = trace_read_packet(trace,packet);
68                event.type = TRACE_EVENT_PACKET;
69                return event;
70        }
71        event.type= TRACE_EVENT_IOWAIT;
72        return event;
73}
74
75struct libtrace_eventobj_t trace_event_trace(struct libtrace_t *trace, struct libtrace_packet_t *packet) {
76        struct libtrace_eventobj_t event = {0,0,0.0,0};
77        double ts;
78        double now;
79        struct timeval stv;
80
81        if (!trace->event.packet.buffer) {
82                trace->event.packet.buffer = (void *)malloc(4096);
83                trace->event.packet.size=
84                        trace_read_packet(trace,packet);
85                event.size = trace->event.packet.size = \
86                             trace->event.packet.size;
87                if (trace->event.packet.size > 0 ) {
88                        memcpy(trace->event.packet.buffer,
89                                        packet->buffer,
90                                        trace->event.packet.size);
91                } else {
92                        /* return here, the test for
93                         * event.size will sort out the error
94                         */
95                        event.type = TRACE_EVENT_TERMINATE;
96                        return event;
97                }
98        }
99
100        ts=trace_get_seconds(packet);
101        if (trace->event.tdelta!=0) {
102                /* Get the adjusted current time */
103                gettimeofday(&stv, NULL);
104                now = stv.tv_sec + 
105                        ((double)stv.tv_usec / 1000000.0);
106                /* adjust for trace delta */
107                now -= trace->event.tdelta; 
108
109                /*if the trace timestamp is still in the
110                //future, return a SLEEP event,
111                //otherwise fire the packet
112                 */
113                if (ts > now) {
114                        event.seconds = ts - 
115                                trace->event.trace_last_ts;
116                        event.type = TRACE_EVENT_SLEEP;
117                        return event;
118                }
119        } else {
120                gettimeofday(&stv, NULL);
121                /* work out the difference between the
122                // start of trace replay, and the first
123                // packet in the trace
124                 */
125                trace->event.tdelta = stv.tv_sec + 
126                        ((double)stv.tv_usec / 1000000.0);
127                trace->event.tdelta -= ts;
128        }
129
130        /* This is the first packet, so just fire away. */
131        memcpy(packet->buffer,
132                        trace->event.packet.buffer,
133                        trace->event.packet.size);
134        free(trace->event.packet.buffer);
135        trace->event.packet.buffer = 0;
136        event.type = TRACE_EVENT_PACKET;
137
138        trace->event.trace_last_ts = ts;
139
140        return event;
141       
142}
143
144/* Catch undefined O_LARGEFILE on *BSD etc */
145#ifndef O_LARGEFILE
146#  define O_LARGEFILE 0
147#endif
148
149/* open a file or stdin using gzip compression if necessary (and supported)
150 * @internal
151 */
152LIBTRACE_FILE trace_open_file(libtrace_t *trace)
153{
154        int fd;
155        LIBTRACE_FILE ret;
156
157
158        if (strcmp(trace->uridata,"-")==0) {
159                ret=LIBTRACE_FDOPEN(fileno(stdin),"r");
160                return ret;
161        }
162
163        /* We open the file with open(2), so we can provide O_LARGEFILE
164         * as zlib doesn't always do it itself
165         */
166        fd=open(trace->uridata,O_LARGEFILE);
167        if (fd==-1) {
168                trace_set_err(errno,"Unable to open %s",trace->uridata);
169                return 0;
170        }
171        ret=LIBTRACE_FDOPEN(fd,"r");
172        return ret;
173}
174
175/* Create a file or write to stdout using compression if requested
176 * @internal
177 */
178LIBTRACE_FILE trace_open_file_out(libtrace_out_t *trace,int level, int fileflag)
179{
180        int fd;
181        LIBTRACE_FILE ret;
182        char filemode[4]; /* wb9\0 */
183        assert(level<10);
184        assert(level>=0);
185#if HAVE_ZLIB
186        sprintf(filemode,"wb%d",level);
187#else
188        sprintf(filemode,"w");
189#endif
190
191        if (strcmp(trace->uridata,"-")==0) {
192                ret=LIBTRACE_FDOPEN(fileno(stdout),filemode);
193                return ret;
194        }
195
196        /* We open the file with open(2), so we can provide O_LARGEFILE
197         * as zlib doesn't always do it itself
198         */
199        fd=open(trace->uridata,fileflag,0666);
200        if (fd==-1) {
201                trace_set_err(errno,"Unable to open %s",trace->uridata);
202                return 0;
203        }
204        ret=LIBTRACE_FDOPEN(fd,filemode);
205        if (ret==NULL) {
206                printf("%s\n",filemode);
207                trace_set_err(TRACE_ERR_INIT_FAILED,"gz out of memory");
208        }
209        return ret;
210}
211
212
213
Note: See TracBrowser for help on using the repository browser.