| [308] | 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 | */ |
|---|
| [380] | 30 | #define _GNU_SOURCE |
|---|
| [308] | 31 | |
|---|
| [336] | 32 | #include "config.h" |
|---|
| [403] | 33 | #include "common.h" |
|---|
| [308] | 34 | #include "libtrace.h" |
|---|
| [325] | 35 | #include "libtrace_int.h" |
|---|
| [333] | 36 | #include "format_helper.h" |
|---|
| [317] | 37 | #include "parse_cmd.h" |
|---|
| [308] | 38 | |
|---|
| 39 | #include <assert.h> |
|---|
| 40 | #include <errno.h> |
|---|
| [311] | 41 | #include <fcntl.h> |
|---|
| [372] | 42 | #include <stdio.h> |
|---|
| [380] | 43 | #include <string.h> |
|---|
| 44 | #include <stdlib.h> |
|---|
| [308] | 45 | |
|---|
| [896] | 46 | #ifdef HAVE_DAG |
|---|
| [734] | 47 | #include <sys/mman.h> |
|---|
| 48 | #endif |
|---|
| 49 | |
|---|
| [690] | 50 | #ifdef WIN32 |
|---|
| 51 | # include <io.h> |
|---|
| 52 | # include <share.h> |
|---|
| 53 | # define PATH_MAX _MAX_PATH |
|---|
| 54 | # define snprintf sprintf_s |
|---|
| 55 | #else |
|---|
| 56 | # include <netdb.h> |
|---|
| [796] | 57 | # ifndef PATH_MAX |
|---|
| 58 | # define PATH_MAX 4096 |
|---|
| 59 | # endif |
|---|
| [808] | 60 | # include <sys/ioctl.h> |
|---|
| [690] | 61 | #endif |
|---|
| [511] | 62 | |
|---|
| [690] | 63 | |
|---|
| [463] | 64 | #define COLLECTOR_PORT 3435 |
|---|
| 65 | |
|---|
| [511] | 66 | static struct libtrace_format_t erf; |
|---|
| 67 | static struct libtrace_format_t rtclient; |
|---|
| [896] | 68 | #ifdef HAVE_DAG |
|---|
| [511] | 69 | static struct libtrace_format_t dag; |
|---|
| [355] | 70 | #endif |
|---|
| 71 | |
|---|
| [628] | 72 | #define DATA(x) ((struct erf_format_data_t *)x->format_data) |
|---|
| 73 | #define DATAOUT(x) ((struct erf_format_data_out_t *)x->format_data) |
|---|
| 74 | |
|---|
| [627] | 75 | #define CONNINFO DATA(libtrace)->conn_info |
|---|
| 76 | #define INPUT DATA(libtrace)->input |
|---|
| 77 | #define OUTPUT DATAOUT(libtrace)->output |
|---|
| [896] | 78 | #ifdef HAVE_DAG |
|---|
| [627] | 79 | #define DAG DATA(libtrace)->dag |
|---|
| [799] | 80 | #define DUCK DATA(libtrace)->duck |
|---|
| [355] | 81 | #endif |
|---|
| [627] | 82 | #define OPTIONS DATAOUT(libtrace)->options |
|---|
| [628] | 83 | struct erf_format_data_t { |
|---|
| [325] | 84 | union { |
|---|
| 85 | struct { |
|---|
| 86 | char *hostname; |
|---|
| 87 | short port; |
|---|
| 88 | } rt; |
|---|
| 89 | } conn_info; |
|---|
| [355] | 90 | |
|---|
| 91 | union { |
|---|
| [325] | 92 | int fd; |
|---|
| [713] | 93 | libtrace_io_t *file; |
|---|
| [325] | 94 | } input; |
|---|
| 95 | |
|---|
| [633] | 96 | struct { |
|---|
| 97 | enum { INDEX_UNKNOWN=0, INDEX_NONE, INDEX_EXISTS } exists; |
|---|
| [713] | 98 | libtrace_io_t *index; |
|---|
| [633] | 99 | off_t index_len; |
|---|
| 100 | } seek; |
|---|
| 101 | |
|---|
| [896] | 102 | #ifdef HAVE_DAG |
|---|
| [325] | 103 | struct { |
|---|
| [799] | 104 | uint32_t last_duck; |
|---|
| 105 | uint32_t duck_freq; |
|---|
| 106 | uint32_t last_pkt; |
|---|
| [800] | 107 | libtrace_t *dummy_duck; |
|---|
| [799] | 108 | } duck; |
|---|
| 109 | |
|---|
| 110 | struct { |
|---|
| [325] | 111 | void *buf; |
|---|
| 112 | unsigned bottom; |
|---|
| 113 | unsigned top; |
|---|
| 114 | unsigned diff; |
|---|
| 115 | unsigned curr; |
|---|
| 116 | unsigned offset; |
|---|
| [822] | 117 | unsigned int dagstream; |
|---|
| [325] | 118 | } dag; |
|---|
| [355] | 119 | #endif |
|---|
| [325] | 120 | }; |
|---|
| 121 | |
|---|
| [628] | 122 | struct erf_format_data_out_t { |
|---|
| [325] | 123 | union { |
|---|
| 124 | struct { |
|---|
| 125 | char *hostname; |
|---|
| 126 | short port; |
|---|
| 127 | } rt; |
|---|
| 128 | char *path; |
|---|
| 129 | } conn_info; |
|---|
| 130 | |
|---|
| 131 | union { |
|---|
| 132 | struct { |
|---|
| 133 | int level; |
|---|
| [577] | 134 | int fileflag; |
|---|
| [325] | 135 | } erf; |
|---|
| 136 | |
|---|
| 137 | } options; |
|---|
| 138 | |
|---|
| 139 | union { |
|---|
| 140 | int fd; |
|---|
| 141 | struct rtserver_t * rtserver; |
|---|
| [713] | 142 | libtrace_io_t *file; |
|---|
| [325] | 143 | } output; |
|---|
| 144 | }; |
|---|
| 145 | |
|---|
| [577] | 146 | /** Structure holding status information for a packet */ |
|---|
| 147 | typedef struct libtrace_packet_status { |
|---|
| 148 | uint8_t type; |
|---|
| 149 | uint8_t reserved; |
|---|
| 150 | uint16_t message; |
|---|
| 151 | } libtrace_packet_status_t; |
|---|
| 152 | |
|---|
| [633] | 153 | typedef struct erf_index_t { |
|---|
| 154 | uint64_t timestamp; |
|---|
| 155 | uint64_t offset; |
|---|
| 156 | } erf_index_t; |
|---|
| [577] | 157 | |
|---|
| [324] | 158 | #ifdef HAVE_DAG |
|---|
| [770] | 159 | static int dag_init_input(libtrace_t *libtrace) { |
|---|
| [643] | 160 | struct stat buf; |
|---|
| 161 | |
|---|
| [635] | 162 | libtrace->format_data = (struct erf_format_data_t *) |
|---|
| 163 | malloc(sizeof(struct erf_format_data_t)); |
|---|
| [601] | 164 | if (stat(libtrace->uridata, &buf) == -1) { |
|---|
| [621] | 165 | trace_set_err(libtrace,errno,"stat(%s)",libtrace->uridata); |
|---|
| [628] | 166 | return -1; |
|---|
| [308] | 167 | } |
|---|
| [822] | 168 | |
|---|
| 169 | DAG.dagstream = 0; |
|---|
| 170 | |
|---|
| [308] | 171 | if (S_ISCHR(buf.st_mode)) { |
|---|
| [577] | 172 | /* DEVICE */ |
|---|
| [601] | 173 | if((INPUT.fd = dag_open(libtrace->uridata)) < 0) { |
|---|
| [621] | 174 | trace_set_err(libtrace,errno,"Cannot open DAG %s", |
|---|
| [601] | 175 | libtrace->uridata); |
|---|
| [628] | 176 | return -1; |
|---|
| [308] | 177 | } |
|---|
| [345] | 178 | if((DAG.buf = (void *)dag_mmap(INPUT.fd)) == MAP_FAILED) { |
|---|
| [621] | 179 | trace_set_err(libtrace,errno,"Cannot mmap DAG %s", |
|---|
| [601] | 180 | libtrace->uridata); |
|---|
| [628] | 181 | return -1; |
|---|
| [308] | 182 | } |
|---|
| 183 | } else { |
|---|
| [621] | 184 | trace_set_err(libtrace,errno,"Not a valid dag device: %s", |
|---|
| [601] | 185 | libtrace->uridata); |
|---|
| [628] | 186 | return -1; |
|---|
| [308] | 187 | } |
|---|
| [799] | 188 | |
|---|
| 189 | DUCK.last_duck = 0; |
|---|
| [800] | 190 | DUCK.duck_freq = 0; |
|---|
| [799] | 191 | DUCK.last_pkt = 0; |
|---|
| [800] | 192 | DUCK.dummy_duck = NULL; |
|---|
| [799] | 193 | |
|---|
| [628] | 194 | return 0; |
|---|
| [324] | 195 | } |
|---|
| [643] | 196 | |
|---|
| [800] | 197 | static int dag_config_input(libtrace_t *libtrace, trace_option_t option, |
|---|
| 198 | void *data) { |
|---|
| 199 | switch(option) { |
|---|
| 200 | case TRACE_META_FREQ: |
|---|
| 201 | DUCK.duck_freq = *(int *)data; |
|---|
| 202 | return 0; |
|---|
| 203 | case TRACE_OPTION_SNAPLEN: |
|---|
| 204 | /* Surely we can set this?? Fall through for now*/ |
|---|
| 205 | |
|---|
| 206 | case TRACE_OPTION_PROMISC: |
|---|
| 207 | /* DAG already operates in a promisc fashion */ |
|---|
| 208 | |
|---|
| 209 | case TRACE_OPTION_FILTER: |
|---|
| 210 | |
|---|
| 211 | default: |
|---|
| 212 | trace_set_err(libtrace, TRACE_ERR_UNKNOWN_OPTION, |
|---|
| 213 | "Unknown or unsupported option: %i", |
|---|
| 214 | option); |
|---|
| 215 | return -1; |
|---|
| 216 | } |
|---|
| 217 | assert (0); |
|---|
| 218 | } |
|---|
| [309] | 219 | #endif |
|---|
| [308] | 220 | |
|---|
| [419] | 221 | /* Dag erf ether packets have a 2 byte padding before the packet |
|---|
| 222 | * so that the ip header is aligned on a 32 bit boundary. |
|---|
| 223 | */ |
|---|
| [643] | 224 | static int erf_get_padding(const libtrace_packet_t *packet) |
|---|
| [419] | 225 | { |
|---|
| [825] | 226 | if (packet->trace->format->type==TRACE_FORMAT_ERF) { |
|---|
| 227 | dag_record_t *erfptr = (dag_record_t *)packet->header; |
|---|
| 228 | switch(erfptr->type) { |
|---|
| 229 | case TYPE_ETH: return 2; |
|---|
| 230 | default: return 0; |
|---|
| 231 | } |
|---|
| [419] | 232 | } |
|---|
| [825] | 233 | else { |
|---|
| 234 | switch(trace_get_link_type(packet)) { |
|---|
| 235 | case TYPE_ETH: return 2; |
|---|
| 236 | default: return 0; |
|---|
| 237 | } |
|---|
| 238 | } |
|---|
| [419] | 239 | } |
|---|
| 240 | |
|---|
| [643] | 241 | static int erf_get_framing_length(const libtrace_packet_t *packet) |
|---|
| [419] | 242 | { |
|---|
| 243 | return dag_record_size + erf_get_padding(packet); |
|---|
| 244 | } |
|---|
| 245 | |
|---|
| 246 | |
|---|
| [643] | 247 | static int erf_init_input(libtrace_t *libtrace) |
|---|
| [593] | 248 | { |
|---|
| [628] | 249 | libtrace->format_data = malloc(sizeof(struct erf_format_data_t)); |
|---|
| [659] | 250 | |
|---|
| [699] | 251 | INPUT.file = 0; |
|---|
| [658] | 252 | |
|---|
| [628] | 253 | return 0; /* success */ |
|---|
| [594] | 254 | } |
|---|
| 255 | |
|---|
| [633] | 256 | static int erf_start_input(libtrace_t *libtrace) |
|---|
| [594] | 257 | { |
|---|
| [637] | 258 | if (INPUT.file) |
|---|
| 259 | return 0; /* success */ |
|---|
| 260 | |
|---|
| [627] | 261 | INPUT.file = trace_open_file(libtrace); |
|---|
| [308] | 262 | |
|---|
| [627] | 263 | if (!INPUT.file) |
|---|
| 264 | return -1; |
|---|
| [403] | 265 | |
|---|
| [628] | 266 | return 0; /* success */ |
|---|
| [308] | 267 | } |
|---|
| 268 | |
|---|
| [633] | 269 | /* Binary search through the index to find the closest point before |
|---|
| 270 | * the packet. Consider in future having a btree index perhaps? |
|---|
| 271 | */ |
|---|
| 272 | static int erf_fast_seek_start(libtrace_t *libtrace,uint64_t erfts) |
|---|
| 273 | { |
|---|
| 274 | size_t max_off = DATA(libtrace)->seek.index_len/sizeof(erf_index_t); |
|---|
| 275 | size_t min_off = 0; |
|---|
| 276 | off_t current; |
|---|
| 277 | erf_index_t record; |
|---|
| 278 | do { |
|---|
| 279 | current=(max_off+min_off)>>2; |
|---|
| 280 | |
|---|
| [713] | 281 | libtrace_io_seek(DATA(libtrace)->seek.index, |
|---|
| [633] | 282 | current*sizeof(record), |
|---|
| 283 | SEEK_SET); |
|---|
| [713] | 284 | libtrace_io_read(DATA(libtrace)->seek.index, |
|---|
| [633] | 285 | &record,sizeof(record)); |
|---|
| 286 | if (record.timestamp < erfts) { |
|---|
| 287 | min_off=current; |
|---|
| 288 | } |
|---|
| 289 | if (record.timestamp > erfts) { |
|---|
| 290 | max_off=current; |
|---|
| 291 | } |
|---|
| 292 | if (record.timestamp == erfts) |
|---|
| 293 | break; |
|---|
| 294 | } while(min_off<max_off); |
|---|
| 295 | |
|---|
| 296 | /* If we've passed it, seek backwards. This loop shouldn't |
|---|
| 297 | * execute more than twice. |
|---|
| 298 | */ |
|---|
| 299 | do { |
|---|
| [713] | 300 | libtrace_io_seek(DATA(libtrace)->seek.index, |
|---|
| [633] | 301 | current*sizeof(record),SEEK_SET); |
|---|
| [713] | 302 | libtrace_io_read(DATA(libtrace)->seek.index, |
|---|
| [633] | 303 | &record,sizeof(record)); |
|---|
| 304 | current--; |
|---|
| 305 | } while(record.timestamp>erfts); |
|---|
| 306 | |
|---|
| 307 | /* We've found our location in the trace, now use it. */ |
|---|
| [713] | 308 | libtrace_io_seek(INPUT.file,record.offset,SEEK_SET); |
|---|
| [633] | 309 | |
|---|
| 310 | return 0; /* success */ |
|---|
| 311 | } |
|---|
| 312 | |
|---|
| 313 | /* There is no index. Seek through the entire trace from the start, nice |
|---|
| 314 | * and slowly. |
|---|
| 315 | */ |
|---|
| 316 | static int erf_slow_seek_start(libtrace_t *libtrace,uint64_t erfts) |
|---|
| 317 | { |
|---|
| 318 | if (INPUT.file) { |
|---|
| [713] | 319 | libtrace_io_close(INPUT.file); |
|---|
| [633] | 320 | } |
|---|
| 321 | INPUT.file = trace_open_file(libtrace); |
|---|
| 322 | if (!INPUT.file) |
|---|
| 323 | return -1; |
|---|
| 324 | return 0; |
|---|
| 325 | } |
|---|
| 326 | |
|---|
| 327 | static int erf_seek_erf(libtrace_t *libtrace,uint64_t erfts) |
|---|
| 328 | { |
|---|
| 329 | libtrace_packet_t *packet; |
|---|
| 330 | off_t off = 0; |
|---|
| 331 | |
|---|
| 332 | if (DATA(libtrace)->seek.exists==INDEX_UNKNOWN) { |
|---|
| 333 | char buffer[PATH_MAX]; |
|---|
| 334 | snprintf(buffer,sizeof(buffer),"%s.idx",libtrace->uridata); |
|---|
| [815] | 335 | DATA(libtrace)->seek.index=libtrace_io_open(buffer,"rb"); |
|---|
| [633] | 336 | if (DATA(libtrace)->seek.index) { |
|---|
| 337 | DATA(libtrace)->seek.exists=INDEX_EXISTS; |
|---|
| 338 | } |
|---|
| 339 | else { |
|---|
| 340 | DATA(libtrace)->seek.exists=INDEX_NONE; |
|---|
| 341 | } |
|---|
| 342 | } |
|---|
| 343 | |
|---|
| 344 | /* If theres an index, use it to find the nearest packet that isn't |
|---|
| 345 | * after the time we're looking for. If there is no index we need |
|---|
| 346 | * to seek slowly through the trace from the beginning. Sigh. |
|---|
| 347 | */ |
|---|
| 348 | switch(DATA(libtrace)->seek.exists) { |
|---|
| 349 | case INDEX_EXISTS: |
|---|
| 350 | erf_fast_seek_start(libtrace,erfts); |
|---|
| 351 | break; |
|---|
| 352 | case INDEX_NONE: |
|---|
| 353 | erf_slow_seek_start(libtrace,erfts); |
|---|
| 354 | break; |
|---|
| 355 | case INDEX_UNKNOWN: |
|---|
| 356 | assert(0); |
|---|
| 357 | break; |
|---|
| 358 | } |
|---|
| 359 | |
|---|
| 360 | /* Now seek forward looking for the correct timestamp */ |
|---|
| 361 | packet=trace_create_packet(); |
|---|
| 362 | do { |
|---|
| 363 | trace_read_packet(libtrace,packet); |
|---|
| 364 | if (trace_get_erf_timestamp(packet)==erfts) |
|---|
| 365 | break; |
|---|
| [713] | 366 | off=libtrace_io_tell(INPUT.file); |
|---|
| [633] | 367 | } while(trace_get_erf_timestamp(packet)<erfts); |
|---|
| 368 | |
|---|
| [713] | 369 | libtrace_io_seek(INPUT.file,off,SEEK_SET); |
|---|
| [633] | 370 | |
|---|
| 371 | return 0; |
|---|
| 372 | } |
|---|
| 373 | |
|---|
| [643] | 374 | static int rtclient_init_input(libtrace_t *libtrace) { |
|---|
| [308] | 375 | char *scan; |
|---|
| [628] | 376 | libtrace->format_data = malloc(sizeof(struct erf_format_data_t)); |
|---|
| [308] | 377 | |
|---|
| [627] | 378 | if (strlen(libtrace->uridata) == 0) { |
|---|
| [325] | 379 | CONNINFO.rt.hostname = |
|---|
| [308] | 380 | strdup("localhost"); |
|---|
| [325] | 381 | CONNINFO.rt.port = |
|---|
| [308] | 382 | COLLECTOR_PORT; |
|---|
| 383 | } else { |
|---|
| [627] | 384 | if ((scan = strchr(libtrace->uridata,':')) == NULL) { |
|---|
| [325] | 385 | CONNINFO.rt.hostname = |
|---|
| [627] | 386 | strdup(libtrace->uridata); |
|---|
| [325] | 387 | CONNINFO.rt.port = |
|---|
| [308] | 388 | COLLECTOR_PORT; |
|---|
| 389 | } else { |
|---|
| [325] | 390 | CONNINFO.rt.hostname = |
|---|
| [627] | 391 | (char *)strndup(libtrace->uridata, |
|---|
| 392 | (scan - libtrace->uridata)); |
|---|
| [325] | 393 | CONNINFO.rt.port = |
|---|
| [308] | 394 | atoi(++scan); |
|---|
| 395 | } |
|---|
| 396 | } |
|---|
| [628] | 397 | |
|---|
| 398 | return 0; /* success */ |
|---|
| [627] | 399 | } |
|---|
| 400 | |
|---|
| 401 | static int rtclient_start_input(libtrace_t *libtrace) |
|---|
| 402 | { |
|---|
| 403 | struct hostent *he; |
|---|
| 404 | struct sockaddr_in remote; |
|---|
| [325] | 405 | if ((he=gethostbyname(CONNINFO.rt.hostname)) == NULL) { |
|---|
| [741] | 406 | trace_set_err(libtrace,TRACE_ERR_INIT_FAILED,"failed to resolve %s", |
|---|
| [591] | 407 | CONNINFO.rt.hostname); |
|---|
| [627] | 408 | return -1; |
|---|
| [308] | 409 | } |
|---|
| [325] | 410 | if ((INPUT.fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
|---|
| [621] | 411 | trace_set_err(libtrace,errno,"socket(AF_INET,SOCK_STREAM)"); |
|---|
| [627] | 412 | return -1; |
|---|
| [308] | 413 | } |
|---|
| 414 | |
|---|
| 415 | remote.sin_family = AF_INET; |
|---|
| [325] | 416 | remote.sin_port = htons(CONNINFO.rt.port); |
|---|
| [308] | 417 | remote.sin_addr = *((struct in_addr *)he->h_addr); |
|---|
| [690] | 418 | memset(&(remote.sin_zero), 0, 8); |
|---|
| [308] | 419 | |
|---|
| [325] | 420 | if (connect(INPUT.fd, (struct sockaddr *)&remote, |
|---|
| [308] | 421 | sizeof(struct sockaddr)) == -1) { |
|---|
| [621] | 422 | trace_set_err(libtrace,errno,"connect(%s)", |
|---|
| [591] | 423 | CONNINFO.rt.hostname); |
|---|
| [628] | 424 | return -1; |
|---|
| [308] | 425 | } |
|---|
| [637] | 426 | return 0; /* success */ |
|---|
| [308] | 427 | } |
|---|
| 428 | |
|---|
| [637] | 429 | static int rtclient_pause_input(libtrace_t *libtrace) |
|---|
| 430 | { |
|---|
| 431 | close(INPUT.fd); |
|---|
| 432 | return 0; /* success */ |
|---|
| 433 | } |
|---|
| 434 | |
|---|
| [643] | 435 | static int erf_init_output(libtrace_out_t *libtrace) { |
|---|
| [746] | 436 | libtrace->format_data = malloc(sizeof(struct erf_format_data_out_t)); |
|---|
| [311] | 437 | |
|---|
| [354] | 438 | OPTIONS.erf.level = 0; |
|---|
| [683] | 439 | OPTIONS.erf.fileflag = O_CREAT | O_WRONLY; |
|---|
| [562] | 440 | OUTPUT.file = 0; |
|---|
| 441 | |
|---|
| [603] | 442 | return 0; |
|---|
| [562] | 443 | } |
|---|
| 444 | |
|---|
| [699] | 445 | static int erf_config_output(libtrace_out_t *libtrace, trace_option_output_t option, |
|---|
| 446 | void *value) { |
|---|
| [317] | 447 | |
|---|
| [577] | 448 | switch (option) { |
|---|
| 449 | case TRACE_OPTION_OUTPUT_COMPRESS: |
|---|
| 450 | OPTIONS.erf.level = *(int*)value; |
|---|
| 451 | return 0; |
|---|
| 452 | case TRACE_OPTION_OUTPUT_FILEFLAGS: |
|---|
| 453 | OPTIONS.erf.fileflag = *(int*)value; |
|---|
| 454 | return 0; |
|---|
| 455 | default: |
|---|
| 456 | /* Unknown option */ |
|---|
| [621] | 457 | trace_set_err_out(libtrace,TRACE_ERR_UNKNOWN_OPTION, |
|---|
| [591] | 458 | "Unknown option"); |
|---|
| [577] | 459 | return -1; |
|---|
| [317] | 460 | } |
|---|
| 461 | } |
|---|
| 462 | |
|---|
| 463 | |
|---|
| [324] | 464 | #ifdef HAVE_DAG |
|---|
| [643] | 465 | static int dag_pause_input(libtrace_t *libtrace) { |
|---|
| [896] | 466 | #ifdef DAG_VERSION_2_4 |
|---|
| [325] | 467 | dag_stop(INPUT.fd); |
|---|
| [822] | 468 | #else |
|---|
| 469 | if (dag_stop_stream(INPUT.fd, DAG.dagstream) < 0) { |
|---|
| 470 | trace_set_err(libtrace, errno, "Could not stop DAG stream"); |
|---|
| 471 | return -1; |
|---|
| 472 | } |
|---|
| 473 | if (dag_detach_stream(INPUT.fd, DAG.dagstream) < 0) { |
|---|
| 474 | trace_set_err(libtrace, errno, "Could not detach DAG stream"); |
|---|
| 475 | return -1; |
|---|
| 476 | } |
|---|
| 477 | #endif |
|---|
| [643] | 478 | return 0; /* success */ |
|---|
| 479 | } |
|---|
| 480 | |
|---|
| 481 | static int dag_fin_input(libtrace_t *libtrace) { |
|---|
| 482 | /* dag pause input implicitly called to cleanup before this */ |
|---|
| [822] | 483 | |
|---|
| [643] | 484 | dag_close(INPUT.fd); |
|---|
| [800] | 485 | if (DUCK.dummy_duck) |
|---|
| 486 | trace_destroy_dead(DUCK.dummy_duck); |
|---|
| [606] | 487 | free(libtrace->format_data); |
|---|
| [643] | 488 | return 0; /* success */ |
|---|
| [324] | 489 | } |
|---|
| [309] | 490 | #endif |
|---|
| [308] | 491 | |
|---|
| [643] | 492 | static int rtclient_fin_input(libtrace_t *libtrace) { |
|---|
| [605] | 493 | free(CONNINFO.rt.hostname); |
|---|
| [601] | 494 | close(INPUT.fd); |
|---|
| [605] | 495 | free(libtrace->format_data); |
|---|
| [601] | 496 | return 0; |
|---|
| 497 | } |
|---|
| 498 | |
|---|
| [643] | 499 | static int erf_fin_input(libtrace_t *libtrace) { |
|---|
| [844] | 500 | if (INPUT.file) |
|---|
| 501 | libtrace_io_close(INPUT.file); |
|---|
| [374] | 502 | free(libtrace->format_data); |
|---|
| [380] | 503 | return 0; |
|---|
| [308] | 504 | } |
|---|
| 505 | |
|---|
| [643] | 506 | static int erf_fin_output(libtrace_out_t *libtrace) { |
|---|
| [713] | 507 | libtrace_io_close(OUTPUT.file); |
|---|
| [374] | 508 | free(libtrace->format_data); |
|---|
| [380] | 509 | return 0; |
|---|
| [311] | 510 | } |
|---|
| 511 | |
|---|
| [896] | 512 | #ifdef HAVE_DAG |
|---|
| 513 | #ifdef DAG_VERSION_2_4 |
|---|
| [799] | 514 | static int dag_get_duckinfo(libtrace_t *libtrace, |
|---|
| 515 | libtrace_packet_t *packet) { |
|---|
| 516 | dag_inf lt_dag_inf; |
|---|
| 517 | |
|---|
| 518 | if (packet->buf_control == TRACE_CTRL_EXTERNAL || |
|---|
| 519 | !packet->buffer) { |
|---|
| 520 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
|---|
| 521 | packet->buf_control = TRACE_CTRL_PACKET; |
|---|
| 522 | if (!packet->buffer) { |
|---|
| 523 | trace_set_err(libtrace, errno, |
|---|
| 524 | "Cannot allocate packet buffer"); |
|---|
| 525 | return -1; |
|---|
| 526 | } |
|---|
| 527 | } |
|---|
| 528 | |
|---|
| 529 | packet->header = 0; |
|---|
| 530 | packet->payload = packet->buffer; |
|---|
| 531 | |
|---|
| 532 | if ((ioctl(INPUT.fd, DAG_IOINF, <_dag_inf) < 0)) { |
|---|
| 533 | trace_set_err(libtrace, errno, |
|---|
| 534 | "Error using DAG_IOINF"); |
|---|
| 535 | return -1; |
|---|
| 536 | } |
|---|
| 537 | if (!IsDUCK(<_dag_inf)) { |
|---|
| 538 | printf("WARNING: %s does not have modern clock support - No DUCK information will be gathered\n", libtrace->uridata); |
|---|
| 539 | return 0; |
|---|
| 540 | } |
|---|
| 541 | |
|---|
| 542 | if ((ioctl(INPUT.fd, DAG_IOGETDUCK, (duck_inf *)packet->payload) |
|---|
| 543 | < 0)) { |
|---|
| 544 | trace_set_err(libtrace, errno, "Error using DAG_IOGETDUCK"); |
|---|
| 545 | return -1; |
|---|
| 546 | } |
|---|
| 547 | |
|---|
| 548 | packet->type = RT_DUCK_2_4; |
|---|
| [800] | 549 | if (!DUCK.dummy_duck) |
|---|
| 550 | DUCK.dummy_duck = trace_create_dead("duck:dummy"); |
|---|
| 551 | packet->trace = DUCK.dummy_duck; |
|---|
| [1026] | 552 | return sizeof(duck_inf); |
|---|
| [799] | 553 | } |
|---|
| 554 | #else |
|---|
| 555 | static int dag_get_duckinfo(libtrace_t *libtrace, |
|---|
| 556 | libtrace_packet_t *packet) { |
|---|
| 557 | daginf_t lt_dag_inf; |
|---|
| 558 | |
|---|
| 559 | if (packet->buf_control == TRACE_CTRL_EXTERNAL || |
|---|
| 560 | !packet->buffer) { |
|---|
| 561 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
|---|
| 562 | packet->buf_control = TRACE_CTRL_PACKET; |
|---|
| 563 | if (!packet->buffer) { |
|---|
| 564 | trace_set_err(libtrace, errno, |
|---|
| 565 | "Cannot allocate packet buffer"); |
|---|
| 566 | return -1; |
|---|
| 567 | } |
|---|
| 568 | } |
|---|
| 569 | |
|---|
| 570 | packet->header = 0; |
|---|
| 571 | packet->payload = packet->buffer; |
|---|
| 572 | |
|---|
| [808] | 573 | /* No need to check if we can get DUCK or not - we're modern |
|---|
| 574 | * enough */ |
|---|
| [799] | 575 | if ((ioctl(INPUT.fd, DAGIOCDUCK, (duckinf_t *)packet->payload) |
|---|
| 576 | < 0)) { |
|---|
| 577 | trace_set_err(libtrace, errno, "Error using DAGIOCDUCK"); |
|---|
| 578 | return -1; |
|---|
| 579 | } |
|---|
| 580 | |
|---|
| 581 | packet->type = RT_DUCK_2_5; |
|---|
| [800] | 582 | if (!DUCK.dummy_duck) |
|---|
| 583 | DUCK.dummy_duck = trace_create_dead("rt:localhost:3434"); |
|---|
| 584 | packet->trace = DUCK.dummy_duck; |
|---|
| [1026] | 585 | return sizeof(duck_inf); |
|---|
| [799] | 586 | } |
|---|
| 587 | #endif |
|---|
| 588 | |
|---|
| [643] | 589 | static int dag_read(libtrace_t *libtrace, int block_flag) { |
|---|
| [309] | 590 | |
|---|
| [574] | 591 | if (DAG.diff != 0) |
|---|
| 592 | return DAG.diff; |
|---|
| 593 | |
|---|
| [325] | 594 | DAG.bottom = DAG.top; |
|---|
| [643] | 595 | |
|---|
| [325] | 596 | DAG.top = dag_offset( |
|---|
| 597 | INPUT.fd, |
|---|
| 598 | &(DAG.bottom), |
|---|
| [562] | 599 | block_flag); |
|---|
| [309] | 600 | |
|---|
| [643] | 601 | DAG.diff = DAG.top - DAG.bottom; |
|---|
| 602 | |
|---|
| [325] | 603 | DAG.offset = 0; |
|---|
| [643] | 604 | return DAG.diff; |
|---|
| [324] | 605 | } |
|---|
| [308] | 606 | |
|---|
| [633] | 607 | /* FIXME: dag_read_packet shouldn't update the pointers, dag_fin_packet |
|---|
| 608 | * should do that. |
|---|
| 609 | */ |
|---|
| [643] | 610 | static int dag_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
|---|
| [308] | 611 | int numbytes; |
|---|
| 612 | int size; |
|---|
| [799] | 613 | struct timeval tv; |
|---|
| [308] | 614 | dag_record_t *erfptr; |
|---|
| [526] | 615 | |
|---|
| [799] | 616 | if (DUCK.last_pkt - DUCK.last_duck > DUCK.duck_freq && |
|---|
| 617 | DUCK.duck_freq != 0) { |
|---|
| 618 | size = dag_get_duckinfo(libtrace, packet); |
|---|
| 619 | DUCK.last_duck = DUCK.last_pkt; |
|---|
| 620 | if (size != 0) { |
|---|
| 621 | return size; |
|---|
| 622 | } |
|---|
| 623 | /* No DUCK support, so don't waste our time anymore */ |
|---|
| 624 | DUCK.duck_freq = 0; |
|---|
| 625 | } |
|---|
| 626 | |
|---|
| [604] | 627 | if (packet->buf_control == TRACE_CTRL_PACKET) { |
|---|
| 628 | packet->buf_control = TRACE_CTRL_EXTERNAL; |
|---|
| [526] | 629 | free(packet->buffer); |
|---|
| 630 | packet->buffer = 0; |
|---|
| 631 | } |
|---|
| [626] | 632 | |
|---|
| 633 | packet->type = RT_DATA_ERF; |
|---|
| 634 | |
|---|
| [643] | 635 | if ((numbytes = dag_read(libtrace,0)) < 0) |
|---|
| [574] | 636 | return numbytes; |
|---|
| [643] | 637 | assert(numbytes>0); |
|---|
| [308] | 638 | |
|---|
| [577] | 639 | /*DAG always gives us whole packets */ |
|---|
| [643] | 640 | erfptr = (dag_record_t *) ((char *)DAG.buf + |
|---|
| [355] | 641 | (DAG.bottom + DAG.offset)); |
|---|
| [308] | 642 | size = ntohs(erfptr->rlen); |
|---|
| 643 | |
|---|
| [643] | 644 | assert( size >= dag_record_size ); |
|---|
| 645 | assert( size < LIBTRACE_PACKET_BUFSIZE); |
|---|
| [526] | 646 | |
|---|
| 647 | packet->buffer = erfptr; |
|---|
| 648 | packet->header = erfptr; |
|---|
| [601] | 649 | if (((dag_record_t *)packet->buffer)->flags.rxerror == 1) { |
|---|
| [909] | 650 | /* rxerror means the payload is corrupt - drop it |
|---|
| 651 | * by tweaking rlen */ |
|---|
| [526] | 652 | packet->payload = NULL; |
|---|
| [909] | 653 | erfptr->rlen = htons(erf_get_framing_length(packet)); |
|---|
| [526] | 654 | } else { |
|---|
| [643] | 655 | packet->payload = (char*)packet->buffer |
|---|
| 656 | + erf_get_framing_length(packet); |
|---|
| [526] | 657 | } |
|---|
| [308] | 658 | |
|---|
| [355] | 659 | DAG.offset += size; |
|---|
| 660 | DAG.diff -= size; |
|---|
| [308] | 661 | |
|---|
| [799] | 662 | tv = trace_get_timeval(packet); |
|---|
| 663 | DUCK.last_pkt = tv.tv_sec; |
|---|
| 664 | |
|---|
| [1026] | 665 | return packet->payload ? size : erf_get_framing_length(packet); |
|---|
| [324] | 666 | } |
|---|
| [643] | 667 | |
|---|
| 668 | static int dag_start_input(libtrace_t *libtrace) { |
|---|
| [896] | 669 | #ifdef DAG_VERSION_2_4 |
|---|
| [643] | 670 | if(dag_start(INPUT.fd) < 0) { |
|---|
| 671 | trace_set_err(libtrace,errno,"Cannot start DAG %s", |
|---|
| 672 | libtrace->uridata); |
|---|
| 673 | return -1; |
|---|
| 674 | } |
|---|
| [822] | 675 | #else |
|---|
| 676 | if (dag_attach_stream(INPUT.fd, DAG.dagstream, 0, 0) < 0) { |
|---|
| 677 | trace_set_err(libtrace, errno, "Cannot attach DAG stream"); |
|---|
| 678 | return -1; |
|---|
| 679 | } |
|---|
| 680 | if (dag_start_stream(INPUT.fd, DAG.dagstream) < 0) { |
|---|
| 681 | trace_set_err(libtrace, errno, "Cannot start DAG stream"); |
|---|
| 682 | return -1; |
|---|
| 683 | } |
|---|
| 684 | #endif |
|---|
| [643] | 685 | /* dags appear to have a bug where if you call dag_start after |
|---|
| 686 | * calling dag_stop, and at least one packet has arrived, bad things |
|---|
| 687 | * happen. flush the memory hole |
|---|
| 688 | */ |
|---|
| 689 | while(dag_read(libtrace,1)!=0) |
|---|
| 690 | DAG.diff=0; |
|---|
| 691 | return 0; |
|---|
| 692 | } |
|---|
| [572] | 693 | #endif |
|---|
| [562] | 694 | |
|---|
| [643] | 695 | static int erf_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
|---|
| [308] | 696 | int numbytes; |
|---|
| 697 | int size; |
|---|
| [577] | 698 | void *buffer2 = packet->buffer; |
|---|
| [308] | 699 | int rlen; |
|---|
| [526] | 700 | |
|---|
| [604] | 701 | if (!packet->buffer || packet->buf_control == TRACE_CTRL_EXTERNAL) { |
|---|
| [577] | 702 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
|---|
| [604] | 703 | packet->buf_control = TRACE_CTRL_PACKET; |
|---|
| [784] | 704 | if (!packet->buffer) { |
|---|
| 705 | trace_set_err(libtrace, errno, |
|---|
| 706 | "Cannot allocate memory"); |
|---|
| 707 | return -1; |
|---|
| 708 | } |
|---|
| [577] | 709 | } |
|---|
| [526] | 710 | |
|---|
| [784] | 711 | |
|---|
| 712 | |
|---|
| [598] | 713 | packet->header = packet->buffer; |
|---|
| [626] | 714 | packet->type = RT_DATA_ERF; |
|---|
| [577] | 715 | |
|---|
| [713] | 716 | if ((numbytes=libtrace_io_read(INPUT.file, |
|---|
| [577] | 717 | packet->buffer, |
|---|
| [308] | 718 | dag_record_size)) == -1) { |
|---|
| [621] | 719 | trace_set_err(libtrace,errno,"read(%s)", |
|---|
| [609] | 720 | libtrace->uridata); |
|---|
| [308] | 721 | return -1; |
|---|
| 722 | } |
|---|
| [989] | 723 | /* EOF */ |
|---|
| [308] | 724 | if (numbytes == 0) { |
|---|
| 725 | return 0; |
|---|
| 726 | } |
|---|
| [509] | 727 | |
|---|
| [577] | 728 | rlen = ntohs(((dag_record_t *)packet->buffer)->rlen); |
|---|
| 729 | buffer2 = (char*)packet->buffer + dag_record_size; |
|---|
| [308] | 730 | size = rlen - dag_record_size; |
|---|
| [598] | 731 | |
|---|
| [731] | 732 | assert(size < LIBTRACE_PACKET_BUFSIZE && size >= dag_record_size); |
|---|
| 733 | |
|---|
| [389] | 734 | /* Unknown/corrupt */ |
|---|
| [577] | 735 | assert(((dag_record_t *)packet->buffer)->type < 10); |
|---|
| [308] | 736 | |
|---|
| [577] | 737 | /* read in the rest of the packet */ |
|---|
| [713] | 738 | if ((numbytes=libtrace_io_read(INPUT.file, |
|---|
| [308] | 739 | buffer2, |
|---|
| [419] | 740 | size)) != size) { |
|---|
| [815] | 741 | if (numbytes==-1) { |
|---|
| 742 | trace_set_err(libtrace,errno, "read(%s)", libtrace->uridata); |
|---|
| 743 | return -1; |
|---|
| 744 | } |
|---|
| [825] | 745 | trace_set_err(libtrace,EIO,"Truncated packet (wanted %d, got %d)", size, numbytes); |
|---|
| [815] | 746 | /* Failed to read the full packet? must be EOF */ |
|---|
| [825] | 747 | return -1; |
|---|
| [308] | 748 | } |
|---|
| [577] | 749 | if (((dag_record_t *)packet->buffer)->flags.rxerror == 1) { |
|---|
| [526] | 750 | packet->payload = NULL; |
|---|
| 751 | } else { |
|---|
| [577] | 752 | packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); |
|---|
| [526] | 753 | } |
|---|
| [308] | 754 | return rlen; |
|---|
| 755 | } |
|---|
| 756 | |
|---|
| [643] | 757 | static int rtclient_read(libtrace_t *libtrace, void *buffer, size_t len) { |
|---|
| [308] | 758 | int numbytes; |
|---|
| 759 | |
|---|
| 760 | while(1) { |
|---|
| 761 | #ifndef MSG_NOSIGNAL |
|---|
| 762 | # define MSG_NOSIGNAL 0 |
|---|
| 763 | #endif |
|---|
| [325] | 764 | if ((numbytes = recv(INPUT.fd, |
|---|
| [308] | 765 | buffer, |
|---|
| 766 | len, |
|---|
| 767 | MSG_NOSIGNAL)) == -1) { |
|---|
| 768 | if (errno == EINTR) { |
|---|
| [577] | 769 | /*ignore EINTR in case |
|---|
| 770 | *a caller is using signals |
|---|
| 771 | */ |
|---|
| [308] | 772 | continue; |
|---|
| 773 | } |
|---|
| [621] | 774 | trace_set_err(libtrace,errno,"recv(%s)", |
|---|
| [591] | 775 | libtrace->uridata); |
|---|
| [308] | 776 | return -1; |
|---|
| 777 | } |
|---|
| 778 | break; |
|---|
| 779 | |
|---|
| 780 | } |
|---|
| 781 | return numbytes; |
|---|
| 782 | } |
|---|
| 783 | |
|---|
| [643] | 784 | static int rtclient_read_packet(libtrace_t *libtrace, libtrace_packet_t *packet) { |
|---|
| [328] | 785 | int numbytes = 0; |
|---|
| [308] | 786 | char buf[RP_BUFSIZE]; |
|---|
| 787 | int read_required = 0; |
|---|
| 788 | |
|---|
| 789 | void *buffer = 0; |
|---|
| [328] | 790 | |
|---|
| [604] | 791 | if (packet->buf_control == TRACE_CTRL_EXTERNAL || !packet->buffer) { |
|---|
| 792 | packet->buf_control = TRACE_CTRL_PACKET; |
|---|
| [526] | 793 | packet->buffer = malloc(LIBTRACE_PACKET_BUFSIZE); |
|---|
| 794 | } |
|---|
| 795 | |
|---|
| [308] | 796 | buffer = packet->buffer; |
|---|
| [526] | 797 | packet->header = packet->buffer; |
|---|
| [626] | 798 | |
|---|
| 799 | packet->type = RT_DATA_ERF; |
|---|
| [308] | 800 | |
|---|
| [509] | 801 | |
|---|
| [308] | 802 | do { |
|---|
| [643] | 803 | libtrace_packet_status_t status; |
|---|
| [617] | 804 | int size; |
|---|
| 805 | if (tracefifo_out_available(libtrace->fifo) == 0 |
|---|
| 806 | || read_required) { |
|---|
| [309] | 807 | if ((numbytes = rtclient_read( |
|---|
| [325] | 808 | libtrace,buf,RP_BUFSIZE))<=0) { |
|---|
| [308] | 809 | return numbytes; |
|---|
| 810 | } |
|---|
| [356] | 811 | tracefifo_write(libtrace->fifo,buf,numbytes); |
|---|
| [308] | 812 | read_required = 0; |
|---|
| 813 | } |
|---|
| [577] | 814 | /* Read status byte */ |
|---|
| [356] | 815 | if (tracefifo_out_read(libtrace->fifo, |
|---|
| [989] | 816 | &status, sizeof(uint32_t)) != sizeof(uint32_t)){ |
|---|
| [308] | 817 | read_required = 1; |
|---|
| 818 | continue; |
|---|
| 819 | } |
|---|
| [464] | 820 | tracefifo_out_update(libtrace->fifo,sizeof(uint32_t)); |
|---|
| [577] | 821 | /* Read in packet size */ |
|---|
| [495] | 822 | if (tracefifo_out_read(libtrace->fifo, |
|---|
| [989] | 823 | &size, sizeof(uint32_t)) != sizeof(uint32_t)) { |
|---|
| [495] | 824 | tracefifo_out_reset(libtrace->fifo); |
|---|
| 825 | read_required = 1; |
|---|
| 826 | continue; |
|---|
| 827 | } |
|---|
| 828 | tracefifo_out_update(libtrace->fifo, sizeof(uint32_t)); |
|---|
| 829 | |
|---|
| [608] | 830 | if (status.type == 2 /* RT_MSG */) { |
|---|
| [577] | 831 | /* Need to skip this packet as it is a message packet */ |
|---|
| [617] | 832 | tracefifo_out_update(libtrace->fifo, size); |
|---|
| 833 | tracefifo_ack_update(libtrace->fifo, size + |
|---|
| [515] | 834 | sizeof(uint32_t) + |
|---|
| 835 | sizeof(libtrace_packet_status_t)); |
|---|
| [457] | 836 | continue; |
|---|
| 837 | } |
|---|
| 838 | |
|---|
| [577] | 839 | /* read in the full packet */ |
|---|
| [356] | 840 | if ((numbytes = tracefifo_out_read(libtrace->fifo, |
|---|
| [989] | 841 | buffer, size)) != size) { |
|---|
| [356] | 842 | tracefifo_out_reset(libtrace->fifo); |
|---|
| [308] | 843 | read_required = 1; |
|---|
| 844 | continue; |
|---|
| 845 | } |
|---|
| 846 | |
|---|
| [577] | 847 | /* got in our whole packet, so... */ |
|---|
| [617] | 848 | tracefifo_out_update(libtrace->fifo,size); |
|---|
| [308] | 849 | |
|---|
| [617] | 850 | tracefifo_ack_update(libtrace->fifo,size + |
|---|
| [515] | 851 | sizeof(uint32_t) + |
|---|
| 852 | sizeof(libtrace_packet_status_t)); |
|---|
| [308] | 853 | |
|---|
| [526] | 854 | if (((dag_record_t *)buffer)->flags.rxerror == 1) { |
|---|
| 855 | packet->payload = NULL; |
|---|
| 856 | } else { |
|---|
| [577] | 857 | packet->payload = (char*)packet->buffer + erf_get_framing_length(packet); |
|---|
| [526] | 858 | } |
|---|
| [308] | 859 | return numbytes; |
|---|
| 860 | } while(1); |
|---|
| 861 | } |
|---|
| 862 | |
|---|
| [603] | 863 | static int erf_dump_packet(libtrace_out_t *libtrace, |
|---|
| [853] | 864 | dag_record_t *erfptr, int pad, void *buffer) { |
|---|
| [311] | 865 | int numbytes = 0; |
|---|
| [853] | 866 | int size; |
|---|
| [652] | 867 | |
|---|
| [713] | 868 | if ((numbytes = libtrace_io_write(OUTPUT.file, erfptr, dag_record_size + pad)) != dag_record_size+pad) { |
|---|
| [621] | 869 | trace_set_err_out(libtrace,errno, |
|---|
| 870 | "write(%s)",libtrace->uridata); |
|---|
| [311] | 871 | return -1; |
|---|
| 872 | } |
|---|
| [509] | 873 | |
|---|
| [853] | 874 | size=ntohs(erfptr->rlen)-(dag_record_size+pad); |
|---|
| 875 | |
|---|
| 876 | numbytes=libtrace_io_write(OUTPUT.file, buffer, size); |
|---|
| 877 | if (numbytes != size) { |
|---|
| [621] | 878 | trace_set_err_out(libtrace,errno, |
|---|
| 879 | "write(%s)",libtrace->uridata); |
|---|
| [598] | 880 | return -1; |
|---|
| [355] | 881 | } |
|---|
| [598] | 882 | |
|---|
| 883 | return numbytes + pad + dag_record_size; |
|---|
| [355] | 884 | } |
|---|
| [598] | 885 | |
|---|
| 886 | static int erf_start_output(libtrace_out_t *libtrace) |
|---|
| 887 | { |
|---|
| 888 | OUTPUT.file = trace_open_file_out(libtrace, |
|---|
| 889 | OPTIONS.erf.level, |
|---|
| 890 | OPTIONS.erf.fileflag); |
|---|
| 891 | if (!OUTPUT.file) { |
|---|
| 892 | return -1; |
|---|
| 893 | } |
|---|
| 894 | return 0; |
|---|
| 895 | } |
|---|
| [925] | 896 | |
|---|
| [941] | 897 | static bool find_compatible_linktype(libtrace_out_t *libtrace, |
|---|
| 898 | libtrace_packet_t *packet) |
|---|
| [925] | 899 | { |
|---|
| 900 | /* Keep trying to simplify the packet until we can find |
|---|
| 901 | * something we can do with it */ |
|---|
| 902 | do { |
|---|
| 903 | char type=libtrace_to_erf_type(trace_get_link_type(packet)); |
|---|
| 904 | |
|---|
| 905 | /* Success */ |
|---|
| 906 | if (type != (char)-1) |
|---|
| 907 | return true; |
|---|
| 908 | |
|---|
| 909 | if (!demote_packet(packet)) { |
|---|
| [941] | 910 | trace_set_err_out(libtrace, |
|---|
| [925] | 911 | TRACE_ERR_NO_CONVERSION, |
|---|
| 912 | "No erf type for packet (%i)", |
|---|
| 913 | trace_get_link_type(packet)); |
|---|
| 914 | return false; |
|---|
| 915 | } |
|---|
| 916 | |
|---|
| 917 | } while(1); |
|---|
| 918 | |
|---|
| 919 | return true; |
|---|
| 920 | } |
|---|
| [355] | 921 | |
|---|
| [598] | 922 | static int erf_write_packet(libtrace_out_t *libtrace, |
|---|
| [941] | 923 | libtrace_packet_t *packet) |
|---|
| [598] | 924 | { |
|---|
| [355] | 925 | int numbytes = 0; |
|---|
| [419] | 926 | int pad = 0; |
|---|
| [526] | 927 | dag_record_t *dag_hdr = (dag_record_t *)packet->header; |
|---|
| 928 | void *payload = packet->payload; |
|---|
| [355] | 929 | |
|---|
| [598] | 930 | assert(OUTPUT.file); |
|---|
| [562] | 931 | |
|---|
| [800] | 932 | if (!packet->header) { |
|---|
| [834] | 933 | /*trace_set_err_output(libtrace, TRACE_ERR_BAD_PACKET, |
|---|
| 934 | "Packet has no header - probably an RT packet"); |
|---|
| 935 | */ |
|---|
| [800] | 936 | return -1; |
|---|
| 937 | } |
|---|
| 938 | |
|---|
| [419] | 939 | pad = erf_get_padding(packet); |
|---|
| [509] | 940 | |
|---|
| [850] | 941 | /* If we've had an rxerror, we have no payload to write - fix |
|---|
| 942 | * rlen to be the correct length |
|---|
| 943 | */ |
|---|
| [928] | 944 | /* I Think this is bogus, we should somehow figure out |
|---|
| 945 | * a way to write out the payload even if it is gibberish -- Perry */ |
|---|
| [509] | 946 | if (payload == NULL) { |
|---|
| 947 | dag_hdr->rlen = htons(dag_record_size + pad); |
|---|
| 948 | } |
|---|
| 949 | |
|---|
| [652] | 950 | if (packet->trace->format == &erf |
|---|
| [896] | 951 | #ifdef HAVE_DAG |
|---|
| [652] | 952 | || packet->trace->format == &dag |
|---|
| [355] | 953 | #endif |
|---|
| [601] | 954 | ) { |
|---|
| [355] | 955 | numbytes = erf_dump_packet(libtrace, |
|---|
| [652] | 956 | (dag_record_t *)packet->header, |
|---|
| [419] | 957 | pad, |
|---|
| [853] | 958 | payload |
|---|
| [598] | 959 | ); |
|---|
| [355] | 960 | } else { |
|---|
| [603] | 961 | dag_record_t erfhdr; |
|---|
| [577] | 962 | /* convert format - build up a new erf header */ |
|---|
| 963 | /* Timestamp */ |
|---|
| [924] | 964 | erfhdr.ts = bswap_host_to_le64(trace_get_erf_timestamp(packet)); |
|---|
| 965 | |
|---|
| 966 | /* Flags. Can't do this */ |
|---|
| 967 | memset(&erfhdr.flags,1,sizeof(erfhdr.flags)); |
|---|
| 968 | if (trace_get_direction(packet)!=-1) |
|---|
| 969 | erfhdr.flags.iface = trace_get_direction(packet); |
|---|
| 970 | |
|---|
| [941] | 971 | if (!find_compatible_linktype(libtrace,packet)) |
|---|
| [637] | 972 | return -1; |
|---|
| [925] | 973 | |
|---|
| [928] | 974 | payload=packet->payload; |
|---|
| 975 | pad = erf_get_padding(packet); |
|---|
| 976 | |
|---|
| [925] | 977 | erfhdr.type = libtrace_to_erf_type(trace_get_link_type(packet)); |
|---|
| 978 | |
|---|
| [577] | 979 | /* Packet length (rlen includes format overhead) */ |
|---|
| [853] | 980 | assert(trace_get_capture_length(packet)>0 |
|---|
| 981 | && trace_get_capture_length(packet)<=65536); |
|---|
| 982 | assert(erf_get_framing_length(packet)>0 |
|---|
| 983 | && trace_get_framing_length(packet)<=65536); |
|---|
| 984 | assert( |
|---|
| 985 | trace_get_capture_length(packet)+erf_get_framing_length(packet)>0 |
|---|
| 986 | &&trace_get_capture_length(packet)+erf_get_framing_length(packet)<=65536); |
|---|
| [825] | 987 | erfhdr.rlen = htons(trace_get_capture_length(packet) |
|---|
| 988 | + erf_get_framing_length(packet)); |
|---|
| [577] | 989 | /* loss counter. Can't do this */ |
|---|
| [355] | 990 | erfhdr.lctr = 0; |
|---|
| [1009] | 991 | /* Wire length, does not include padding! */ |
|---|
| 992 | erfhdr.wlen = htons(trace_get_wire_length(packet)); |
|---|
| [603] | 993 | |
|---|
| [577] | 994 | /* Write it out */ |
|---|
| [355] | 995 | numbytes = erf_dump_packet(libtrace, |
|---|
| 996 | &erfhdr, |
|---|
| [419] | 997 | pad, |
|---|
| [853] | 998 | payload); |
|---|
| [355] | 999 | } |
|---|
| [311] | 1000 | return numbytes; |
|---|
| 1001 | } |
|---|
| 1002 | |
|---|
| [643] | 1003 | static libtrace_linktype_t erf_get_link_type(const libtrace_packet_t *packet) { |
|---|
| [308] | 1004 | dag_record_t *erfptr = 0; |
|---|
| [526] | 1005 | erfptr = (dag_record_t *)packet->header; |
|---|
| [577] | 1006 | return erf_type_to_libtrace(erfptr->type); |
|---|
| [308] | 1007 | } |
|---|
| 1008 | |
|---|
| [782] | 1009 | static libtrace_direction_t erf_get_direction(const libtrace_packet_t *packet) { |
|---|
| [308] | 1010 | dag_record_t *erfptr = 0; |
|---|
| [526] | 1011 | erfptr = (dag_record_t *)packet->header; |
|---|
| [308] | 1012 | return erfptr->flags.iface; |
|---|
| 1013 | } |
|---|
| 1014 | |
|---|
| [782] | 1015 | static libtrace_direction_t erf_set_direction(libtrace_packet_t *packet, libtrace_direction_t direction) { |
|---|
| [308] | 1016 | dag_record_t *erfptr = 0; |
|---|
| [526] | 1017 | erfptr = (dag_record_t *)packet->header; |
|---|
| [308] | 1018 | erfptr->flags.iface = direction; |
|---|
| 1019 | return erfptr->flags.iface; |
|---|
| 1020 | } |
|---|
| 1021 | |
|---|
| [643] | 1022 | static uint64_t erf_get_erf_timestamp(const libtrace_packet_t *packet) { |
|---|
| [308] | 1023 | dag_record_t *erfptr = 0; |
|---|
| [526] | 1024 | erfptr = (dag_record_t *)packet->header; |
|---|
| [924] | 1025 | return bswap_le_to_host64(erfptr->ts); |
|---|
| [308] | 1026 | } |
|---|
| 1027 | |
|---|
| [643] | 1028 | static int erf_get_capture_length(const libtrace_packet_t *packet) { |
|---|
| [308] | 1029 | dag_record_t *erfptr = 0; |
|---|
| [1009] | 1030 | int caplen; |
|---|
| [909] | 1031 | if (packet->payload == NULL) |
|---|
| 1032 | return 0; |
|---|
| 1033 | |
|---|
| [526] | 1034 | erfptr = (dag_record_t *)packet->header; |
|---|
| [1009] | 1035 | caplen = ntohs(erfptr->rlen) - erf_get_framing_length(packet); |
|---|
| 1036 | if (ntohs(erfptr->wlen) < caplen) |
|---|
| 1037 | return ntohs(erfptr->wlen); |
|---|
| 1038 | |
|---|
| [437] | 1039 | return (ntohs(erfptr->rlen) - erf_get_framing_length(packet)); |
|---|
| [308] | 1040 | } |
|---|
| 1041 | |
|---|
| [643] | 1042 | static int erf_get_wire_length(const libtrace_packet_t *packet) { |
|---|
| [308] | 1043 | dag_record_t *erfptr = 0; |
|---|
| [526] | 1044 | erfptr = (dag_record_t *)packet->header; |
|---|
| [1009] | 1045 | return ntohs(erfptr->wlen); |
|---|
| [308] | 1046 | } |
|---|
| 1047 | |
|---|
| [643] | 1048 | static size_t erf_set_capture_length(libtrace_packet_t *packet, size_t size) { |
|---|
| [308] | 1049 | dag_record_t *erfptr = 0; |
|---|
| 1050 | assert(packet); |
|---|
| [617] | 1051 | if(size > trace_get_capture_length(packet)) { |
|---|
| [577] | 1052 | /* can't make a packet larger */ |
|---|
| [617] | 1053 | return trace_get_capture_length(packet); |
|---|
| [308] | 1054 | } |
|---|
| [526] | 1055 | erfptr = (dag_record_t *)packet->header; |
|---|
| [437] | 1056 | erfptr->rlen = htons(size + erf_get_framing_length(packet)); |
|---|
| [617] | 1057 | return trace_get_capture_length(packet); |
|---|
| [308] | 1058 | } |
|---|
| 1059 | |
|---|
| [627] | 1060 | static int rtclient_get_fd(const libtrace_t *libtrace) { |
|---|
| 1061 | return INPUT.fd; |
|---|
| [333] | 1062 | } |
|---|
| 1063 | |
|---|
| [574] | 1064 | #ifdef HAVE_DAG |
|---|
| [643] | 1065 | libtrace_eventobj_t trace_event_dag(libtrace_t *trace, libtrace_packet_t *packet) { |
|---|
| 1066 | libtrace_eventobj_t event = {0,0,0.0,0}; |
|---|
| [574] | 1067 | int dag_fd; |
|---|
| 1068 | int data; |
|---|
| 1069 | |
|---|
| [609] | 1070 | if (trace->format->get_fd) { |
|---|
| [619] | 1071 | dag_fd = trace->format->get_fd(trace); |
|---|
| [574] | 1072 | } else { |
|---|
| 1073 | dag_fd = 0; |
|---|
| 1074 | } |
|---|
| 1075 | |
|---|
| 1076 | data = dag_read(trace, DAGF_NONBLOCK); |
|---|
| 1077 | |
|---|
| 1078 | if (data > 0) { |
|---|
| 1079 | event.size = trace_read_packet(trace,packet); |
|---|
| 1080 | event.type = TRACE_EVENT_PACKET; |
|---|
| 1081 | return event; |
|---|
| 1082 | } |
|---|
| 1083 | event.type = TRACE_EVENT_SLEEP; |
|---|
| 1084 | event.seconds = 0.0001; |
|---|
| 1085 | return event; |
|---|
| 1086 | } |
|---|
| 1087 | #endif |
|---|
| 1088 | |
|---|
| [896] | 1089 | #ifdef HAVE_DAG |
|---|
| [319] | 1090 | static void dag_help() { |
|---|
| [324] | 1091 | printf("dag format module: $Revision$\n"); |
|---|
| 1092 | printf("Supported input URIs:\n"); |
|---|
| 1093 | printf("\tdag:/dev/dagn\n"); |
|---|
| 1094 | printf("\n"); |
|---|
| 1095 | printf("\te.g.: dag:/dev/dag0\n"); |
|---|
| 1096 | printf("\n"); |
|---|
| 1097 | printf("Supported output URIs:\n"); |
|---|
| 1098 | printf("\tnone\n"); |
|---|
| 1099 | printf("\n"); |
|---|
| [319] | 1100 | } |
|---|
| [355] | 1101 | #endif |
|---|
| [319] | 1102 | |
|---|
| 1103 | static void erf_help() { |
|---|
| [324] | 1104 | printf("erf format module: $Revision$\n"); |
|---|
| 1105 | printf("Supported input URIs:\n"); |
|---|
| 1106 | printf("\terf:/path/to/file\t(uncompressed)\n"); |
|---|
| 1107 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
|---|
| [326] | 1108 | printf("\terf:-\t(stdin, either compressed or not)\n"); |
|---|
| [324] | 1109 | printf("\terf:/path/to/socket\n"); |
|---|
| 1110 | printf("\n"); |
|---|
| 1111 | printf("\te.g.: erf:/tmp/trace\n"); |
|---|
| 1112 | printf("\n"); |
|---|
| 1113 | printf("Supported output URIs:\n"); |
|---|
| [326] | 1114 | printf("\terf:path/to/file\t(uncompressed)\n"); |
|---|
| 1115 | printf("\terf:/path/to/file.gz\t(gzip-compressed)\n"); |
|---|
| 1116 | printf("\terf:-\t(stdout, either compressed or not)\n"); |
|---|
| [324] | 1117 | printf("\n"); |
|---|
| [326] | 1118 | printf("\te.g.: erf:/tmp/trace\n"); |
|---|
| 1119 | printf("\n"); |
|---|
| 1120 | printf("Supported output options:\n"); |
|---|
| 1121 | printf("\t-z\tSpecify the gzip compression, ranging from 0 (uncompressed) to 9 - defaults to 1\n"); |
|---|
| 1122 | printf("\n"); |
|---|
| [319] | 1123 | |
|---|
| [326] | 1124 | |
|---|
| [319] | 1125 | } |
|---|
| 1126 | |
|---|
| 1127 | static void rtclient_help() { |
|---|
| [601] | 1128 | printf("rtclient format module: $Revision$\n"); |
|---|
| 1129 | printf("DEPRECATED - use rt module instead\n"); |
|---|
| [324] | 1130 | printf("Supported input URIs:\n"); |
|---|
| [601] | 1131 | printf("\trtclient:host:port\n"); |
|---|
| [324] | 1132 | printf("\n"); |
|---|
| [601] | 1133 | printf("\te.g.:rtclient:localhost:3435\n"); |
|---|
| [324] | 1134 | printf("\n"); |
|---|
| [601] | 1135 | printf("Supported output URIs:\n"); |
|---|
| 1136 | printf("\tnone\n"); |
|---|
| 1137 | printf("\n"); |
|---|
| 1138 | } |
|---|
| [319] | 1139 | |
|---|
| [325] | 1140 | static struct libtrace_format_t erf = { |
|---|
| [308] | 1141 | "erf", |
|---|
| 1142 | "$Id$", |
|---|
| [601] | 1143 | TRACE_FORMAT_ERF, |
|---|
| [309] | 1144 | erf_init_input, /* init_input */ |
|---|
| [575] | 1145 | NULL, /* config_input */ |
|---|
| [594] | 1146 | erf_start_input, /* start_input */ |
|---|
| [590] | 1147 | NULL, /* pause_input */ |
|---|
| [311] | 1148 | erf_init_output, /* init_output */ |
|---|
| [317] | 1149 | erf_config_output, /* config_output */ |
|---|
| [598] | 1150 | erf_start_output, /* start_output */ |
|---|
| [309] | 1151 | erf_fin_input, /* fin_input */ |
|---|
| [311] | 1152 | erf_fin_output, /* fin_output */ |
|---|
| [309] | 1153 | erf_read_packet, /* read_packet */ |
|---|
| [643] | 1154 | NULL, /* fin_packet */ |
|---|
| [311] | 1155 | erf_write_packet, /* write_packet */ |
|---|
| [309] | 1156 | erf_get_link_type, /* get_link_type */ |
|---|
| 1157 | erf_get_direction, /* get_direction */ |
|---|
| 1158 | erf_set_direction, /* set_direction */ |
|---|
| 1159 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
|---|
| 1160 | NULL, /* get_timeval */ |
|---|
| 1161 | NULL, /* get_seconds */ |
|---|
| [633] | 1162 | erf_seek_erf, /* seek_erf */ |
|---|
| [590] | 1163 | NULL, /* seek_timeval */ |
|---|
| 1164 | NULL, /* seek_seconds */ |
|---|
| [309] | 1165 | erf_get_capture_length, /* get_capture_length */ |
|---|
| 1166 | erf_get_wire_length, /* get_wire_length */ |
|---|
| [437] | 1167 | erf_get_framing_length, /* get_framing_length */ |
|---|
| [319] | 1168 | erf_set_capture_length, /* set_capture_length */ |
|---|
| [636] | 1169 | NULL, /* get_fd */ |
|---|
| [333] | 1170 | trace_event_trace, /* trace_event */ |
|---|
| [643] | 1171 | erf_help, /* help */ |
|---|
| 1172 | NULL /* next pointer */ |
|---|
| [308] | 1173 | }; |
|---|
| 1174 | |
|---|
| [324] | 1175 | #ifdef HAVE_DAG |
|---|
| [325] | 1176 | static struct libtrace_format_t dag = { |
|---|
| [308] | 1177 | "dag", |
|---|
| 1178 | "$Id$", |
|---|
| [601] | 1179 | TRACE_FORMAT_ERF, |
|---|
| [309] | 1180 | dag_init_input, /* init_input */ |
|---|
| [800] | 1181 | dag_config_input, /* config_input */ |
|---|
| [594] | 1182 | dag_start_input, /* start_input */ |
|---|
| [643] | 1183 | dag_pause_input, /* pause_input */ |
|---|
| [309] | 1184 | NULL, /* init_output */ |
|---|
| [317] | 1185 | NULL, /* config_output */ |
|---|
| [575] | 1186 | NULL, /* start_output */ |
|---|
| [309] | 1187 | dag_fin_input, /* fin_input */ |
|---|
| 1188 | NULL, /* fin_output */ |
|---|
| 1189 | dag_read_packet, /* read_packet */ |
|---|
| [643] | 1190 | NULL, /* fin_packet */ |
|---|
| [309] | 1191 | NULL, /* write_packet */ |
|---|
| 1192 | erf_get_link_type, /* get_link_type */ |
|---|
| 1193 | erf_get_direction, /* get_direction */ |
|---|
| 1194 | erf_set_direction, /* set_direction */ |
|---|
| 1195 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
|---|
| 1196 | NULL, /* get_timeval */ |
|---|
| 1197 | NULL, /* get_seconds */ |
|---|
| [601] | 1198 | NULL, /* seek_erf */ |
|---|
| 1199 | NULL, /* seek_timeval */ |
|---|
| 1200 | NULL, /* seek_seconds */ |
|---|
| [309] | 1201 | erf_get_capture_length, /* get_capture_length */ |
|---|
| 1202 | erf_get_wire_length, /* get_wire_length */ |
|---|
| [437] | 1203 | erf_get_framing_length, /* get_framing_length */ |
|---|
| [319] | 1204 | erf_set_capture_length, /* set_capture_length */ |
|---|
| [333] | 1205 | NULL, /* get_fd */ |
|---|
| [574] | 1206 | trace_event_dag, /* trace_event */ |
|---|
| [643] | 1207 | dag_help, /* help */ |
|---|
| 1208 | NULL /* next pointer */ |
|---|
| [308] | 1209 | }; |
|---|
| [324] | 1210 | #endif |
|---|
| [308] | 1211 | |
|---|
| [325] | 1212 | static struct libtrace_format_t rtclient = { |
|---|
| [308] | 1213 | "rtclient", |
|---|
| 1214 | "$Id$", |
|---|
| [601] | 1215 | TRACE_FORMAT_ERF, |
|---|
| [309] | 1216 | rtclient_init_input, /* init_input */ |
|---|
| [575] | 1217 | NULL, /* config_input */ |
|---|
| [627] | 1218 | rtclient_start_input, /* start_input */ |
|---|
| [637] | 1219 | rtclient_pause_input, /* pause_input */ |
|---|
| [372] | 1220 | NULL, /* init_output */ |
|---|
| 1221 | NULL, /* config_output */ |
|---|
| [575] | 1222 | NULL, /* start_output */ |
|---|
| [309] | 1223 | rtclient_fin_input, /* fin_input */ |
|---|
| [372] | 1224 | NULL, /* fin_output */ |
|---|
| [309] | 1225 | rtclient_read_packet, /* read_packet */ |
|---|
| [643] | 1226 | NULL, /* fin_packet */ |
|---|
| [372] | 1227 | NULL, /* write_packet */ |
|---|
| [309] | 1228 | erf_get_link_type, /* get_link_type */ |
|---|
| 1229 | erf_get_direction, /* get_direction */ |
|---|
| 1230 | erf_set_direction, /* set_direction */ |
|---|
| 1231 | erf_get_erf_timestamp, /* get_erf_timestamp */ |
|---|
| 1232 | NULL, /* get_timeval */ |
|---|
| 1233 | NULL, /* get_seconds */ |
|---|
| [590] | 1234 | NULL, /* seek_erf */ |
|---|
| 1235 | NULL, /* seek_timeval */ |
|---|
| 1236 | NULL, /* seek_seconds */ |
|---|
| [309] | 1237 | erf_get_capture_length, /* get_capture_length */ |
|---|
| 1238 | erf_get_wire_length, /* get_wire_length */ |
|---|
| [437] | 1239 | erf_get_framing_length, /* get_framing_length */ |
|---|
| [319] | 1240 | erf_set_capture_length, /* set_capture_length */ |
|---|
| [333] | 1241 | rtclient_get_fd, /* get_fd */ |
|---|
| 1242 | trace_event_device, /* trace_event */ |
|---|
| [643] | 1243 | rtclient_help, /* help */ |
|---|
| 1244 | NULL /* next pointer */ |
|---|
| [308] | 1245 | }; |
|---|
| 1246 | |
|---|
| [922] | 1247 | void erf_constructor() { |
|---|
| [601] | 1248 | register_format(&rtclient); |
|---|
| [437] | 1249 | register_format(&erf); |
|---|
| [324] | 1250 | #ifdef HAVE_DAG |
|---|
| [437] | 1251 | register_format(&dag); |
|---|
| [324] | 1252 | #endif |
|---|
| [308] | 1253 | } |
|---|