source: trunk/lib/format_legacy.c @ 952

Last change on this file since 952 was 952, checked in by perry, 7 years ago

Fixed lots of warnings

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.8 KB
RevLine 
[575]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#define _GNU_SOURCE
31
32#include "config.h"
33#include "common.h"
34#include "libtrace.h"
35#include "libtrace_int.h"
36#include "format_helper.h"
37#include "parse_cmd.h"
38
39#include <sys/stat.h>
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
[691]47#ifdef WIN32
48#  include <io.h>
49#  include <share.h>
50#  define snprintf sprintf_s
51#endif
[575]52
53
54/* Catch undefined O_LARGEFILE on *BSD etc */
55#ifndef O_LARGEFILE
56#  define O_LARGEFILE 0
57#endif
58
[627]59#define DATA(x) ((struct legacy_format_data_t *)x->format_data)
60
61#define INPUT DATA(libtrace)->input
62
63struct legacy_format_data_t {
[575]64        union {
65                int fd;
[713]66                libtrace_io_t *file;
[575]67        } input;
68};
69
[770]70static int legacyeth_get_framing_length(const libtrace_packet_t *packet UNUSED) 
[575]71{
72        return sizeof(legacy_ether_t);
73}
74
[770]75static int legacypos_get_framing_length(const libtrace_packet_t *packet UNUSED) 
[575]76{
77        return sizeof(legacy_pos_t);
78}
79
[770]80static int legacyatm_get_framing_length(const libtrace_packet_t *packet UNUSED) 
[575]81{
82        return sizeof(legacy_cell_t);
83}
84
[770]85static int erf_init_input(libtrace_t *libtrace) 
[593]86{
[627]87        libtrace->format_data = malloc(sizeof(struct legacy_format_data_t));
[575]88
[627]89        return 0;
90}
[575]91
[627]92static int erf_start_input(libtrace_t *libtrace)
93{
94        DATA(libtrace)->input.file = trace_open_file(libtrace);
[575]95
[627]96        if (DATA(libtrace)->input.file)
97                return 0;
[593]98
[627]99        return -1;
[575]100}
101
[770]102static int erf_fin_input(libtrace_t *libtrace) {
[713]103        libtrace_io_close(INPUT.file);
[575]104        free(libtrace->format_data);
105        return 0;
106}
107
[770]108static int legacy_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
[575]109        int numbytes;
[604]110        void *buffer;
111
112        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) {
113                packet->buf_control = TRACE_CTRL_PACKET;
114                packet->buffer=malloc(LIBTRACE_PACKET_BUFSIZE);
115        }
116        buffer = packet->buffer;
[626]117
118        switch(libtrace->format->type) {
119                case TRACE_FORMAT_LEGACY_ATM:
120                        packet->type = RT_DATA_LEGACY_ATM;
121                        break;
122                case TRACE_FORMAT_LEGACY_POS:
123                        packet->type = RT_DATA_LEGACY_POS;
124                        break;
125                case TRACE_FORMAT_LEGACY_ETH:
126                        packet->type = RT_DATA_LEGACY_ETH;
127                        break;
[630]128                default:
129                        assert(0);
[626]130        }
[575]131       
[713]132        if ((numbytes=libtrace_io_read(INPUT.file,
[575]133                                        buffer,
134                                        64)) == -1) {
[621]135                trace_set_err(libtrace,errno,"read(%s)",libtrace->uridata);
[575]136                return -1;
137        }
138       
139        packet->header = packet->buffer;
[591]140        packet->payload = (void*)((char*)packet->buffer + 
[609]141                libtrace->format->get_framing_length(packet));
[575]142       
143        return 64;
144       
145}
146
[770]147static libtrace_linktype_t legacypos_get_link_type(const libtrace_packet_t *packet UNUSED) {
[720]148        return TRACE_TYPE_POS;
[575]149}
150
[770]151static libtrace_linktype_t legacyatm_get_link_type(const libtrace_packet_t *packet UNUSED) {
[720]152        return TRACE_TYPE_ATM;
[575]153}
154
[770]155static libtrace_linktype_t legacyeth_get_link_type(const libtrace_packet_t *packet UNUSED) {
[720]156        return TRACE_TYPE_ETH;
[575]157}
158
[770]159static int legacy_get_capture_length(const libtrace_packet_t *packet UNUSED) {
[575]160        return 64;
161}
162
[770]163static int legacypos_get_wire_length(const libtrace_packet_t *packet) {
[575]164        legacy_pos_t *lpos = (legacy_pos_t *)packet->header;
165        return ntohs(lpos->wlen);
166}
167
[770]168static int legacyatm_get_wire_length(const libtrace_packet_t *packet UNUSED) {
[575]169        return 53;
170}
171
[770]172static int legacyeth_get_wire_length(const libtrace_packet_t *packet) {
[575]173        legacy_ether_t *leth = (legacy_ether_t *)packet->header;
174        return ntohs(leth->wlen);
175}
176
[770]177static uint64_t legacy_get_erf_timestamp(const libtrace_packet_t *packet)
[575]178{
179        legacy_ether_t *legacy = (legacy_ether_t*)packet->header;
180        return legacy->ts;
181} 
182
183static void legacypos_help() {
184        printf("legacypos format module: $Revision$\n");
185        printf("Supported input URIs:\n");
186        printf("\tlegacypos:/path/to/file\t(uncompressed)\n");
187        printf("\tlegacypos:/path/to/file.gz\t(gzip-compressed)\n");
188        printf("\tlegacypos:-\t(stdin, either compressed or not)\n");
189        printf("\n");
190        printf("\te.g.: legacypos:/tmp/trace.gz\n");
191        printf("\n");
192}
193
194static void legacyatm_help() {
195        printf("legacyatm format module: $Revision$\n");
196        printf("Supported input URIs:\n");
197        printf("\tlegacyatm:/path/to/file\t(uncompressed)\n");
198        printf("\tlegacyatm:/path/to/file.gz\t(gzip-compressed)\n");
199        printf("\tlegacyatm:-\t(stdin, either compressed or not)\n");
200        printf("\n");
201        printf("\te.g.: legacyatm:/tmp/trace.gz\n");
202        printf("\n");
203}
204
205static void legacyeth_help() {
206        printf("legacyeth format module: $Revision$\n");
207        printf("Supported input URIs:\n");
208        printf("\tlegacyeth:/path/to/file\t(uncompressed)\n");
209        printf("\tlegacyeth:/path/to/file.gz\t(gzip-compressed)\n");
210        printf("\tlegacyeth:-\t(stdin, either compressed or not)\n");
211        printf("\n");
212        printf("\te.g.: legacyeth:/tmp/trace.gz\n");
213        printf("\n");
214}
215
216static struct libtrace_format_t legacyatm = {
217        "legacyatm",
218        "$Id$",
[602]219        TRACE_FORMAT_LEGACY_ATM,
[575]220        erf_init_input,                 /* init_input */       
221        NULL,                           /* config_input */
[627]222        erf_start_input,                /* start_input */
[590]223        NULL,                           /* pause_input */
[575]224        NULL,                           /* init_output */
225        NULL,                           /* config_output */
226        NULL,                           /* start_output */
227        erf_fin_input,                  /* fin_input */
228        NULL,                           /* fin_output */
229        legacy_read_packet,             /* read_packet */
[643]230        NULL,                           /* fin_packet */
[575]231        NULL,                           /* write_packet */
232        legacyatm_get_link_type,        /* get_link_type */
233        NULL,                           /* get_direction */
234        NULL,                           /* set_direction */
235        legacy_get_erf_timestamp,       /* get_erf_timestamp */
236        NULL,                           /* get_timeval */
237        NULL,                           /* get_seconds */
[590]238        NULL,                           /* seek_erf */
239        NULL,                           /* seek_timeval */
240        NULL,                           /* seek_seconds */
[575]241        legacy_get_capture_length,      /* get_capture_length */
242        legacyatm_get_wire_length,      /* get_wire_length */
243        legacyatm_get_framing_length,   /* get_framing_length */
244        NULL,                           /* set_capture_length */
245        NULL,                           /* get_fd */
246        trace_event_trace,              /* trace_event */
[643]247        legacyatm_help,                 /* help */
248        NULL                            /* next pointer */
[575]249};
250
251static struct libtrace_format_t legacyeth = {
252        "legacyeth",
253        "$Id$",
[602]254        TRACE_FORMAT_LEGACY_ETH,
[575]255        erf_init_input,                 /* init_input */       
256        NULL,                           /* config_input */
[627]257        erf_start_input,                /* start_input */
[590]258        NULL,                           /* pause_input */
[575]259        NULL,                           /* init_output */
260        NULL,                           /* config_output */
261        NULL,                           /* start_output */
262        erf_fin_input,                  /* fin_input */
263        NULL,                           /* fin_output */
264        legacy_read_packet,             /* read_packet */
[643]265        NULL,                           /* fin_packet */
[575]266        NULL,                           /* write_packet */
267        legacyeth_get_link_type,        /* get_link_type */
268        NULL,                           /* get_direction */
269        NULL,                           /* set_direction */
270        legacy_get_erf_timestamp,       /* get_erf_timestamp */
271        NULL,                           /* get_timeval */
272        NULL,                           /* get_seconds */
[590]273        NULL,                           /* seek_erf */
274        NULL,                           /* seek_timeval */
275        NULL,                           /* seek_seconds */
[575]276        legacy_get_capture_length,      /* get_capture_length */
277        legacyeth_get_wire_length,      /* get_wire_length */
278        legacyeth_get_framing_length,   /* get_framing_length */
279        NULL,                           /* set_capture_length */
280        NULL,                           /* get_fd */
281        trace_event_trace,              /* trace_event */
[643]282        legacyeth_help,                 /* help */
283        NULL                            /* next pointer */
[575]284};
285
286static struct libtrace_format_t legacypos = {
287        "legacypos",
288        "$Id$",
[602]289        TRACE_FORMAT_LEGACY_POS,
[575]290        erf_init_input,                 /* init_input */       
291        NULL,                           /* config_input */
[627]292        erf_start_input,                /* start_input */
[590]293        NULL,                           /* pause_input */
[575]294        NULL,                           /* init_output */
295        NULL,                           /* config_output */
296        NULL,                           /* start_output */
297        erf_fin_input,                  /* fin_input */
298        NULL,                           /* fin_output */
299        legacy_read_packet,             /* read_packet */
[643]300        NULL,                           /* fin_packet */
[575]301        NULL,                           /* write_packet */
302        legacypos_get_link_type,        /* get_link_type */
303        NULL,                           /* get_direction */
304        NULL,                           /* set_direction */
305        legacy_get_erf_timestamp,       /* get_erf_timestamp */
306        NULL,                           /* get_timeval */
307        NULL,                           /* get_seconds */
[590]308        NULL,                           /* seek_erf */
309        NULL,                           /* seek_timeval */
310        NULL,                           /* seek_seconds */
[575]311        legacy_get_capture_length,      /* get_capture_length */
312        legacypos_get_wire_length,      /* get_wire_length */
313        legacypos_get_framing_length,   /* get_framing_length */
314        NULL,                           /* set_capture_length */
315        NULL,                           /* get_fd */
316        trace_event_trace,              /* trace_event */
[643]317        legacypos_help,                 /* help */
318        NULL,                           /* next pointer */
[575]319};
320
321       
[922]322void legacy_constructor() {
[575]323        register_format(&legacypos);
324        register_format(&legacyeth);
325        register_format(&legacyatm);
326}
Note: See TracBrowser for help on using the repository browser.