| 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: format_template.c 691 2006-04-08 04:31:40Z perry $ |
|---|
| 28 | * |
|---|
| 29 | */ |
|---|
| 30 | |
|---|
| 31 | #include "libtrace.h" |
|---|
| 32 | #include "libtrace_int.h" |
|---|
| 33 | #include "format_helper.h" |
|---|
| 34 | #include "config.h" |
|---|
| 35 | #include <stdlib.h> |
|---|
| 36 | #include "libtraceio.h" |
|---|
| 37 | #include "rt_protocol.h" |
|---|
| 38 | |
|---|
| 39 | #include <errno.h> |
|---|
| 40 | #include <assert.h> |
|---|
| 41 | #include <stdio.h> |
|---|
| 42 | #include <fcntl.h> |
|---|
| 43 | |
|---|
| 44 | #define DATA(x) ((struct duck_format_data_t *)x->format_data) |
|---|
| 45 | #define DATAOUT(x) ((struct duck_format_data_out_t *)x->format_data) |
|---|
| 46 | |
|---|
| 47 | #define INPUT DATA(libtrace) |
|---|
| 48 | #define OUTPUT DATAOUT(libtrace) |
|---|
| 49 | |
|---|
| 50 | |
|---|
| 51 | static struct libtrace_format_t duck; |
|---|
| 52 | |
|---|
| 53 | struct duck_format_data_t { |
|---|
| 54 | char *path; |
|---|
| 55 | libtrace_io_t *file; |
|---|
| 56 | int dag_version; |
|---|
| 57 | }; |
|---|
| 58 | |
|---|
| 59 | struct duck_format_data_out_t { |
|---|
| 60 | char *path; |
|---|
| 61 | int level; |
|---|
| 62 | int fileflag; |
|---|
| 63 | libtrace_io_t *file; |
|---|
| 64 | int dag_version; |
|---|
| 65 | }; |
|---|
| 66 | |
|---|
| 67 | static int duck_init_input(libtrace_t *libtrace) { |
|---|
| 68 | libtrace->format_data = malloc(sizeof(struct duck_format_data_t)); |
|---|
| 69 | |
|---|
| 70 | INPUT->file = 0; |
|---|
| 71 | INPUT->dag_version = 0; |
|---|
| 72 | return 0; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | static int duck_init_output(libtrace_out_t *libtrace) { |
|---|
| 76 | libtrace->format_data = malloc(sizeof(struct duck_format_data_out_t)); |
|---|
| 77 | |
|---|
| 78 | OUTPUT->level = 0; |
|---|
| 79 | OUTPUT->fileflag = O_CREAT | O_WRONLY; |
|---|
| 80 | OUTPUT->file = 0; |
|---|
| 81 | OUTPUT->dag_version = 0; |
|---|
| 82 | return 0; |
|---|
| 83 | } |
|---|
| 84 | |
|---|
| 85 | static int duck_config_output(libtrace_out_t *libtrace, |
|---|
| 86 | trace_option_output_t option, |
|---|
| 87 | void *data) { |
|---|
| 88 | switch (option) { |
|---|
| 89 | case TRACE_OPTION_OUTPUT_COMPRESS: |
|---|
| 90 | OUTPUT->level = *(int *)data; |
|---|
| 91 | return 0; |
|---|
| 92 | case TRACE_OPTION_OUTPUT_FILEFLAGS: |
|---|
| 93 | OUTPUT->fileflag = *(int *)data; |
|---|
| 94 | return 0; |
|---|
| 95 | default: |
|---|
| 96 | trace_set_err_out(libtrace, TRACE_ERR_UNKNOWN_OPTION, |
|---|
| 97 | "Unknown option"); |
|---|
| 98 | return -1; |
|---|
| 99 | } |
|---|
| 100 | assert(0); |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | static int duck_start_input(libtrace_t *libtrace) { |
|---|
| 104 | |
|---|
| 105 | if (INPUT->file) |
|---|
| 106 | /* File already open */ |
|---|
| 107 | return 0; |
|---|
| 108 | |
|---|
| 109 | INPUT->file = trace_open_file(libtrace); |
|---|
| 110 | if (!INPUT->file) |
|---|
| 111 | return -1; |
|---|
| 112 | |
|---|
| 113 | return 0; |
|---|
| 114 | } |
|---|
| 115 | |
|---|
| 116 | static int duck_start_output(libtrace_out_t *libtrace) { |
|---|
| 117 | OUTPUT->file = trace_open_file_out(libtrace, OUTPUT->level, |
|---|
| 118 | OUTPUT->fileflag); |
|---|
| 119 | if (!OUTPUT->file) { |
|---|
| 120 | return -1; |
|---|
| 121 | } |
|---|
| 122 | return 0; |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | static int duck_fin_input(libtrace_t *libtrace) { |
|---|
| 126 | libtrace_io_close(INPUT->file); |
|---|
| 127 | free(libtrace->format_data); |
|---|
| 128 | |
|---|
| 129 | return 0; |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | static int duck_fin_output(libtrace_out_t *libtrace) { |
|---|
| 133 | libtrace_io_close(OUTPUT->file); |
|---|
| 134 | free(libtrace->format_data); |
|---|
| 135 | return 0; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | static int duck_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
|---|
| 139 | |
|---|
| 140 | int numbytes = 0; |
|---|
| 141 | uint32_t version = 0; |
|---|
| 142 | int duck_size; |
|---|
| 143 | |
|---|
| 144 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
|---|
| 145 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
|---|
| 146 | packet->buf_control = TRACE_CTRL_PACKET; |
|---|
| 147 | if (!packet->buffer) { |
|---|
| 148 | trace_set_err(libtrace, errno, |
|---|
| 149 | "Cannot allocate memory"); |
|---|
| 150 | return -1; |
|---|
| 151 | } |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | if (INPUT->dag_version == 0) { |
|---|
| 155 | /* Read in the duck version from the start of the trace */ |
|---|
| 156 | if ((numbytes = libtrace_io_read(INPUT->file, &version, |
|---|
| 157 | sizeof(uint32_t))) == -1) { |
|---|
| 158 | trace_set_err(libtrace, errno, |
|---|
| 159 | "Reading DUCK version failed"); |
|---|
| 160 | return -1; |
|---|
| 161 | } |
|---|
| 162 | if (numbytes == 0) { |
|---|
| 163 | return 0; |
|---|
| 164 | } |
|---|
| 165 | INPUT->dag_version = version; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | |
|---|
| 169 | packet->header = 0; |
|---|
| 170 | packet->payload = packet->buffer; |
|---|
| 171 | |
|---|
| 172 | if (INPUT->dag_version == RT_DUCK_2_4) { |
|---|
| 173 | duck_size = sizeof(duck2_4_t); |
|---|
| 174 | packet->type = RT_DUCK_2_4; |
|---|
| 175 | } else if (INPUT->dag_version == RT_DUCK_2_5) { |
|---|
| 176 | duck_size = sizeof(duck2_5_t); |
|---|
| 177 | packet->type = RT_DUCK_2_5; |
|---|
| 178 | } else { |
|---|
| 179 | trace_set_err(libtrace, TRACE_ERR_BAD_PACKET, |
|---|
| 180 | "Unrecognised DUCK version %i", |
|---|
| 181 | INPUT->dag_version); |
|---|
| 182 | return -1; |
|---|
| 183 | } |
|---|
| 184 | |
|---|
| 185 | if ((numbytes = libtrace_io_read(INPUT->file, packet->payload, |
|---|
| 186 | duck_size)) != duck_size) { |
|---|
| 187 | if (numbytes != 0) { |
|---|
| 188 | trace_set_err(libtrace, errno, "Reading DUCK failed"); |
|---|
| 189 | return -1; |
|---|
| 190 | } |
|---|
| 191 | return 0; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | packet->size = duck_size; |
|---|
| 195 | return numbytes; |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | static int duck_write_packet(libtrace_out_t *libtrace, |
|---|
| 199 | const libtrace_packet_t *packet) { |
|---|
| 200 | |
|---|
| 201 | int numbytes = 0; |
|---|
| 202 | if (packet->type != RT_DUCK_2_4 && packet->type != RT_DUCK_2_5) { |
|---|
| 203 | trace_set_err_out(libtrace, TRACE_ERR_BAD_PACKET, |
|---|
| 204 | "Only DUCK packets may be written to a DUCK file"); |
|---|
| 205 | return -1; |
|---|
| 206 | } |
|---|
| 207 | |
|---|
| 208 | assert(OUTPUT->file); |
|---|
| 209 | /* Common sense size checking */ |
|---|
| 210 | assert(packet->size < 400); |
|---|
| 211 | |
|---|
| 212 | if (OUTPUT->dag_version == 0) { |
|---|
| 213 | /* Writing the DUCK version will help with reading it back in later! */ |
|---|
| 214 | if ((numbytes = libtrace_io_write(OUTPUT->file, &packet->type, |
|---|
| 215 | sizeof(uint32_t))) != sizeof(uint32_t)){ |
|---|
| 216 | trace_set_err_out(libtrace, errno, |
|---|
| 217 | "Writing DUCK version failed"); |
|---|
| 218 | return -1; |
|---|
| 219 | } |
|---|
| 220 | OUTPUT->dag_version = packet->type; |
|---|
| 221 | } |
|---|
| 222 | |
|---|
| 223 | if ((numbytes = libtrace_io_write(OUTPUT->file, packet->payload, |
|---|
| 224 | trace_get_capture_length(packet))) != |
|---|
| 225 | trace_get_capture_length(packet)) { |
|---|
| 226 | trace_set_err_out(libtrace, errno, "Writing DUCK failed"); |
|---|
| 227 | return -1; |
|---|
| 228 | } |
|---|
| 229 | return numbytes; |
|---|
| 230 | } |
|---|
| 231 | |
|---|
| 232 | static int duck_get_capture_length(const libtrace_packet_t *packet) { |
|---|
| 233 | switch(packet->type) { |
|---|
| 234 | case RT_DUCK_2_4: |
|---|
| 235 | return sizeof(duck2_4_t); |
|---|
| 236 | case RT_DUCK_2_5: |
|---|
| 237 | return sizeof(duck2_5_t); |
|---|
| 238 | } |
|---|
| 239 | return 0; |
|---|
| 240 | } |
|---|
| 241 | |
|---|
| 242 | static int duck_get_framing_length(const libtrace_packet_t *packet) { |
|---|
| 243 | return 0; |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | static int duck_get_wire_length(const libtrace_packet_t *packet) { |
|---|
| 247 | return 0; |
|---|
| 248 | } |
|---|
| 249 | |
|---|
| 250 | static libtrace_linktype_t duck_get_link_type(const libtrace_packet_t *packet) { |
|---|
| 251 | return TRACE_TYPE_DUCK; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | static void duck_help() { |
|---|
| 255 | printf("Endace DUCK format module\n"); |
|---|
| 256 | printf("Supported input uris:\n"); |
|---|
| 257 | printf("\tduck:/path/to/input/file\n"); |
|---|
| 258 | printf("Supported output uris:\n"); |
|---|
| 259 | printf("\tduck:/path/to/output/file\n"); |
|---|
| 260 | printf("\n"); |
|---|
| 261 | return; |
|---|
| 262 | } |
|---|
| 263 | static struct libtrace_format_t duck = { |
|---|
| 264 | "duck", |
|---|
| 265 | "$Id: format_duck.c 799 2006-05-12 05:07:59Z spa1 $", |
|---|
| 266 | TRACE_FORMAT_DUCK, |
|---|
| 267 | duck_init_input, /* init_input */ |
|---|
| 268 | NULL, /* config_input */ |
|---|
| 269 | duck_start_input, /* start_input */ |
|---|
| 270 | NULL, /* pause_input */ |
|---|
| 271 | duck_init_output, /* init_output */ |
|---|
| 272 | duck_config_output, /* config_output */ |
|---|
| 273 | duck_start_output, /* start_output */ |
|---|
| 274 | duck_fin_input, /* fin_input */ |
|---|
| 275 | duck_fin_output, /* fin_output */ |
|---|
| 276 | duck_read_packet, /* read_packet */ |
|---|
| 277 | NULL, /* fin_packet */ |
|---|
| 278 | duck_write_packet, /* write_packet */ |
|---|
| 279 | duck_get_link_type, /* get_link_type */ |
|---|
| 280 | NULL, /* get_direction */ |
|---|
| 281 | NULL, /* set_direction */ |
|---|
| 282 | NULL, /* get_erf_timestamp */ |
|---|
| 283 | NULL, /* get_timeval */ |
|---|
| 284 | NULL, /* get_seconds */ |
|---|
| 285 | NULL, /* seek_erf */ |
|---|
| 286 | NULL, /* seek_timeval */ |
|---|
| 287 | NULL, /* seek_seconds */ |
|---|
| 288 | duck_get_capture_length, /* get_capture_length */ |
|---|
| 289 | duck_get_wire_length, /* get_wire_length */ |
|---|
| 290 | duck_get_framing_length, /* get_framing_length */ |
|---|
| 291 | NULL, /* set_capture_length */ |
|---|
| 292 | NULL, /* get_fd */ |
|---|
| 293 | NULL, /* trace_event */ |
|---|
| 294 | duck_help, /* help */ |
|---|
| 295 | NULL /* next pointer */ |
|---|
| 296 | }; |
|---|
| 297 | |
|---|
| 298 | void CONSTRUCTOR duck_constructor() { |
|---|
| 299 | register_format(&duck); |
|---|
| 300 | } |
|---|