source: lib/format_atmhdr.c @ 254c926

develop
Last change on this file since 254c926 was 254c926, checked in by Jacob Van Walraven <jcv9@…>, 21 months ago

Cleanup some duplicate code, Added datatype/option_name for libtrace_meta_t structure

  • Property mode set to 100644
File size: 7.6 KB
Line 
1/*
2 *
3 * Copyright (c) 2007-2016 The University of Waikato, Hamilton, New Zealand.
4 * All rights reserved.
5 *
6 * This file is part of libtrace.
7 *
8 * This code has been developed by the University of Waikato WAND
9 * research group. For further information please see http://www.wand.net.nz/
10 *
11 * libtrace is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * libtrace is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 * GNU Lesser General Public License for more details.
20 *
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 *
24 *
25 */
26#define _GNU_SOURCE
27
28#include "config.h"
29#include "common.h"
30#include "libtrace.h"
31#include "libtrace_int.h"
32#include "format_helper.h"
33#include "wandio.h"
34
35#include <sys/stat.h>
36#include <errno.h>
37#include <fcntl.h>
38#include <stdio.h>
39#include <string.h>
40#include <stdlib.h>
41
42/* This particular format covers the ATM cell header capture format used to
43 * take the Auckland VII trace set.
44 *
45 * Each capture record contains only a timestamp and the first four bytes of
46 * the ATM header - nothing else.
47 *
48 * As a result, there isn't a lot you can actually do with these traces!
49 *
50 * Libtrace does not support writing using this format, because it is so
51 * useless :)
52 */
53
54/* Returns the size of the ATM cell framing header */
55static int atmhdr_get_framing_length(const libtrace_packet_t *packet UNUSED)
56{
57        return sizeof(atmhdr_t);
58}
59
60/* Initialise an input trace to read an ATM cell header capture */
61static int atmhdr_init_input(libtrace_t *libtrace) {
62        libtrace->format_data = NULL; /* No format data needed */
63        return 0;
64}
65
66/* Start an ATM cell header input trace */
67static int atmhdr_start_input(libtrace_t *libtrace)
68{
69        if (libtrace->io) /* Already open? */
70                return 0;
71        libtrace->io = trace_open_file(libtrace);
72        if (libtrace->io)
73                return 0;
74        return -1;
75}
76
77/* Close an ATM cell header input trace */
78static int atmhdr_fin_input(libtrace_t *libtrace)
79{
80        wandio_destroy(libtrace->io);
81        return 0;
82}
83
84
85/* Converts a buffer containing a recently read ATM cell header record into
86 * a libtrace packet */
87static int atmhdr_prepare_packet(libtrace_t *libtrace, 
88                libtrace_packet_t *packet, void *buffer, 
89                libtrace_rt_types_t rt_type, uint32_t flags) {
90
91        /* If the packet previously owned a buffer that was not the buffer
92         * containing the new packet data, we need to free the old one to
93         * avoid leaking memory */
94        if (packet->buffer != buffer &&
95                        packet->buf_control == TRACE_CTRL_PACKET) {
96                free(packet->buffer);
97        }
98
99        /* Set the buffer owner appropriately */
100        if ((flags & TRACE_PREP_OWN_BUFFER) == TRACE_PREP_OWN_BUFFER) {
101                packet->buf_control = TRACE_CTRL_PACKET;
102        } else
103                packet->buf_control = TRACE_CTRL_EXTERNAL;
104
105        /* Update the packet pointers appropriately */
106        packet->buffer = buffer;
107        packet->header = buffer;
108        packet->payload = (void*)((char*)packet->buffer + 
109                        libtrace->format->get_framing_length(packet));
110
111        /* Set the packet type */
112        packet->type = rt_type;
113
114        return 0;
115}
116
117/* Reads the next ATM cell header record from the given trace and writes it
118 * into a libtrace packet */
119static int atmhdr_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) {
120        int numbytes;
121        void *buffer;
122        uint32_t flags = 0;
123       
124        /* Make sure we have a buffer available to read the next record into */
125        if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) {
126                packet->buffer=malloc((size_t)LIBTRACE_PACKET_BUFSIZE);
127        }
128        buffer = packet->buffer;
129        flags |= TRACE_PREP_OWN_BUFFER;
130       
131        packet->type = TRACE_RT_DATA_ATMHDR;
132
133        /* The records are a fixed size so we can read the entire record in
134         * one go */
135        if ((numbytes=wandio_read(libtrace->io, buffer, (size_t)12)) != 12)
136        {
137                if (numbytes != 0) {
138                        trace_set_err(libtrace,TRACE_ERR_WANDIO_FAILED,"read(%s)",libtrace->uridata);
139                }
140                return numbytes;
141        }
142
143        /* Update all our packet pointers appropriately */
144        if (atmhdr_prepare_packet(libtrace, packet, buffer, 
145                                TRACE_RT_DATA_ATMHDR, flags)) {
146                return -1;
147        }
148                               
149       
150        return 12;
151}
152
153/* Get the link type for an ATM cell header record */
154static libtrace_linktype_t atmhdr_get_link_type(const libtrace_packet_t *packet UNUSED) {
155        /* Unsurprisingly, we're always going to be an ATM header */
156        return TRACE_TYPE_ATM;
157}
158
159/* Get the capture length for an ATM cell header record */
160static int atmhdr_get_capture_length(const libtrace_packet_t *packet UNUSED) {
161        /* There is always 4 bytes of ATM header retained by this format */
162        return 4;
163}
164
165/* Get the wire length for an ATM cell header record */
166static int atmhdr_get_wire_length(const libtrace_packet_t *packet UNUSED) {
167        /* ATM packets are 53 byte fixed length records */
168        return 53;
169}
170
171/* Returns the timestamp for an ATM cell header record in the ERF timestamp
172 * format */
173static uint64_t atmhdr_get_erf_timestamp(const libtrace_packet_t *packet) {
174        uint64_t ts;
175        atmhdr_t *atm = (atmhdr_t *)packet->header;
176       
177        /* Basically, the capture format header is an ERF timestamp except
178         * the two 32-bit segments are reversed */
179        ts = (uint64_t)atm->ts_fraction + ((uint64_t)atm->ts_sec << 32);
180
181        return ts;
182}
183
184static struct libtrace_format_t atmhdr = {
185        "atmhdr",
186        "$Id$",
187        TRACE_FORMAT_ATMHDR,
188        NULL,                           /* probe filename */
189        NULL,                           /* probe magic */
190        atmhdr_init_input,              /* init_input */
191        NULL,                           /* config_input */
192        atmhdr_start_input,             /* start_input */
193        NULL,                           /* pause_input */
194        NULL,                           /* init_output */
195        NULL,                           /* config_output */
196        NULL,                           /* start_output */
197        atmhdr_fin_input,               /* fin_input */
198        NULL,                           /* fin_output */
199        atmhdr_read_packet,             /* read_packet */
200        atmhdr_prepare_packet,          /* prepare_packet */
201        NULL,                           /* fin_packet */
202        NULL,                           /* write_packet */
203        NULL,                           /* flush_output */
204        atmhdr_get_link_type,           /* get_link_type */
205        NULL,                           /* get_direction */
206        NULL,                           /* set_direction */
207        atmhdr_get_erf_timestamp,       /* get_erf_timestamp */
208        NULL,                           /* get_timeval */
209        NULL,                           /* get_timespec */
210        NULL,                           /* get_seconds */
211        NULL,                           /* get_meta_section */
212        NULL,                           /* seek_erf */
213        NULL,                           /* seek_timeval */
214        NULL,                           /* seek_seconds */
215        atmhdr_get_capture_length,      /* get_capture_length */
216        atmhdr_get_wire_length,         /* get_wire_length */
217        atmhdr_get_framing_length,      /* get_framing_length */
218        NULL,                           /* set_capture_length */
219        NULL,                           /* get_received_packets */
220        NULL,                           /* get_filtered_packets */
221        NULL,                           /* get_dropped_packets */
222        NULL,                           /* get_statistics */
223        NULL,                           /* get_fd */
224        trace_event_trace,              /* trace_event */
225        NULL,                           /* help */
226        NULL,                            /* next pointer */
227        NON_PARALLEL(false)
228};
229       
230
231void atmhdr_constructor(void) {
232        register_format(&atmhdr);
233}
Note: See TracBrowser for help on using the repository browser.