| 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 | |
|---|
| 47 | #ifdef WIN32 |
|---|
| 48 | # include <io.h> |
|---|
| 49 | # include <share.h> |
|---|
| 50 | # define snprintf sprintf_s |
|---|
| 51 | #endif |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | /* Catch undefined O_LARGEFILE on *BSD etc */ |
|---|
| 55 | #ifndef O_LARGEFILE |
|---|
| 56 | # define O_LARGEFILE 0 |
|---|
| 57 | #endif |
|---|
| 58 | |
|---|
| 59 | #define DATA(x) ((struct legacy_format_data_t *)x->format_data) |
|---|
| 60 | |
|---|
| 61 | #define INPUT DATA(libtrace)->input |
|---|
| 62 | |
|---|
| 63 | struct legacy_format_data_t { |
|---|
| 64 | union { |
|---|
| 65 | int fd; |
|---|
| 66 | libtrace_io_t *file; |
|---|
| 67 | } input; |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | static int legacyeth_get_framing_length(const libtrace_packet_t *packet UNUSED) |
|---|
| 71 | { |
|---|
| 72 | return sizeof(legacy_ether_t); |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | static int legacypos_get_framing_length(const libtrace_packet_t *packet UNUSED) |
|---|
| 76 | { |
|---|
| 77 | return sizeof(legacy_pos_t); |
|---|
| 78 | } |
|---|
| 79 | |
|---|
| 80 | static int legacyatm_get_framing_length(const libtrace_packet_t *packet UNUSED) |
|---|
| 81 | { |
|---|
| 82 | return sizeof(legacy_cell_t); |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | static int erf_init_input(libtrace_t *libtrace) |
|---|
| 86 | { |
|---|
| 87 | libtrace->format_data = malloc(sizeof(struct legacy_format_data_t)); |
|---|
| 88 | |
|---|
| 89 | return 0; |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | static int erf_start_input(libtrace_t *libtrace) |
|---|
| 93 | { |
|---|
| 94 | DATA(libtrace)->input.file = trace_open_file(libtrace); |
|---|
| 95 | |
|---|
| 96 | if (DATA(libtrace)->input.file) |
|---|
| 97 | return 0; |
|---|
| 98 | |
|---|
| 99 | return -1; |
|---|
| 100 | } |
|---|
| 101 | |
|---|
| 102 | static int erf_fin_input(libtrace_t *libtrace) { |
|---|
| 103 | libtrace_io_close(INPUT.file); |
|---|
| 104 | free(libtrace->format_data); |
|---|
| 105 | return 0; |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | static int legacy_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
|---|
| 109 | int numbytes; |
|---|
| 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; |
|---|
| 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; |
|---|
| 128 | default: |
|---|
| 129 | assert(0); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | if ((numbytes=libtrace_io_read(INPUT.file, |
|---|
| 133 | buffer, |
|---|
| 134 | 64)) == -1) { |
|---|
| 135 | trace_set_err(libtrace,errno,"read(%s)",libtrace->uridata); |
|---|
| 136 | return -1; |
|---|
| 137 | } |
|---|
| 138 | |
|---|
| 139 | packet->header = packet->buffer; |
|---|
| 140 | packet->payload = (void*)((char*)packet->buffer + |
|---|
| 141 | libtrace->format->get_framing_length(packet)); |
|---|
| 142 | |
|---|
| 143 | return 64; |
|---|
| 144 | |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | static libtrace_linktype_t legacypos_get_link_type(const libtrace_packet_t *packet UNUSED) { |
|---|
| 148 | return TRACE_TYPE_POS; |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | static libtrace_linktype_t legacyatm_get_link_type(const libtrace_packet_t *packet UNUSED) { |
|---|
| 152 | return TRACE_TYPE_ATM; |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | static libtrace_linktype_t legacyeth_get_link_type(const libtrace_packet_t *packet UNUSED) { |
|---|
| 156 | return TRACE_TYPE_ETH; |
|---|
| 157 | } |
|---|
| 158 | |
|---|
| 159 | static int legacy_get_capture_length(const libtrace_packet_t *packet UNUSED) { |
|---|
| 160 | return 64; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | static int legacypos_get_wire_length(const libtrace_packet_t *packet) { |
|---|
| 164 | legacy_pos_t *lpos = (legacy_pos_t *)packet->header; |
|---|
| 165 | return ntohs(lpos->wlen); |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | static int legacyatm_get_wire_length(const libtrace_packet_t *packet UNUSED) { |
|---|
| 169 | return 53; |
|---|
| 170 | } |
|---|
| 171 | |
|---|
| 172 | static int legacyeth_get_wire_length(const libtrace_packet_t *packet) { |
|---|
| 173 | legacy_ether_t *leth = (legacy_ether_t *)packet->header; |
|---|
| 174 | return ntohs(leth->wlen); |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | static uint64_t legacy_get_erf_timestamp(const libtrace_packet_t *packet) |
|---|
| 178 | { |
|---|
| 179 | legacy_ether_t *legacy = (legacy_ether_t*)packet->header; |
|---|
| 180 | return legacy->ts; |
|---|
| 181 | } |
|---|
| 182 | |
|---|
| 183 | static 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 | |
|---|
| 194 | static 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 | |
|---|
| 205 | static 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 | |
|---|
| 216 | static struct libtrace_format_t legacyatm = { |
|---|
| 217 | "legacyatm", |
|---|
| 218 | "$Id$", |
|---|
| 219 | TRACE_FORMAT_LEGACY_ATM, |
|---|
| 220 | erf_init_input, /* init_input */ |
|---|
| 221 | NULL, /* config_input */ |
|---|
| 222 | erf_start_input, /* start_input */ |
|---|
| 223 | NULL, /* pause_input */ |
|---|
| 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 */ |
|---|
| 230 | NULL, /* fin_packet */ |
|---|
| 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 */ |
|---|
| 238 | NULL, /* seek_erf */ |
|---|
| 239 | NULL, /* seek_timeval */ |
|---|
| 240 | NULL, /* seek_seconds */ |
|---|
| 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 */ |
|---|
| 247 | legacyatm_help, /* help */ |
|---|
| 248 | NULL /* next pointer */ |
|---|
| 249 | }; |
|---|
| 250 | |
|---|
| 251 | static struct libtrace_format_t legacyeth = { |
|---|
| 252 | "legacyeth", |
|---|
| 253 | "$Id$", |
|---|
| 254 | TRACE_FORMAT_LEGACY_ETH, |
|---|
| 255 | erf_init_input, /* init_input */ |
|---|
| 256 | NULL, /* config_input */ |
|---|
| 257 | erf_start_input, /* start_input */ |
|---|
| 258 | NULL, /* pause_input */ |
|---|
| 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 */ |
|---|
| 265 | NULL, /* fin_packet */ |
|---|
| 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 */ |
|---|
| 273 | NULL, /* seek_erf */ |
|---|
| 274 | NULL, /* seek_timeval */ |
|---|
| 275 | NULL, /* seek_seconds */ |
|---|
| 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 */ |
|---|
| 282 | legacyeth_help, /* help */ |
|---|
| 283 | NULL /* next pointer */ |
|---|
| 284 | }; |
|---|
| 285 | |
|---|
| 286 | static struct libtrace_format_t legacypos = { |
|---|
| 287 | "legacypos", |
|---|
| 288 | "$Id$", |
|---|
| 289 | TRACE_FORMAT_LEGACY_POS, |
|---|
| 290 | erf_init_input, /* init_input */ |
|---|
| 291 | NULL, /* config_input */ |
|---|
| 292 | erf_start_input, /* start_input */ |
|---|
| 293 | NULL, /* pause_input */ |
|---|
| 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 */ |
|---|
| 300 | NULL, /* fin_packet */ |
|---|
| 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 */ |
|---|
| 308 | NULL, /* seek_erf */ |
|---|
| 309 | NULL, /* seek_timeval */ |
|---|
| 310 | NULL, /* seek_seconds */ |
|---|
| 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 */ |
|---|
| 317 | legacypos_help, /* help */ |
|---|
| 318 | NULL, /* next pointer */ |
|---|
| 319 | }; |
|---|
| 320 | |
|---|
| 321 | |
|---|
| 322 | void legacy_constructor() { |
|---|
| 323 | register_format(&legacypos); |
|---|
| 324 | register_format(&legacyeth); |
|---|
| 325 | register_format(&legacyatm); |
|---|
| 326 | } |
|---|