1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501
|
/* eyesdn.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "eyesdn.h"
#include "wtap-int.h"
#include "file_wrappers.h"
#include <stdlib.h>
#include <string.h>
#include <wsutil/pint.h>
static int eyesdn_file_type_subtype = -1;
void register_eyesdn(void);
/* This module reads the output of the EyeSDN USB S0/E1 ISDN probes
* They store HDLC frames of D and B channels in a binary format
* The fileformat is
*
* 1-6 Byte: EyeSDN - Magic
* 7-n Byte: Frames
*
* Each Frame starts with the 0xff Flag byte
* - Bytes 0-2: timestamp (usec in network byte order)
* - Bytes 3-7: timestamp (40bits sec since 1970 in network byte order)
* - Byte 8: channel (0 for D channel, 1-30 for B1-B30)
* - Byte 9: Sender Bit 0(0 NT, 1 TE), Protocol in Bits 7:1, see enum
* - Byte 10-11: frame size in bytes
* - Byte 12-n: Frame Payload
*
* All multibyte values are represented in network byte order
* The frame is terminated with a flag character (0xff)
* bytes 0xff within a frame are escaped using the 0xfe escape character
* the byte following the escape character is decremented by two:
* so 0xfe 0xfd is actually a 0xff
* Characters that need to be escaped are 0xff and 0xfe
*/
static bool esc_read(FILE_T fh, uint8_t *buf, int len, int *err, char **err_info)
{
int i;
int value;
for(i=0; i<len; i++) {
value=file_getc(fh);
if(value==-1) {
/* EOF or error */
*err=file_error(fh, err_info);
if(*err==0)
*err=WTAP_ERR_SHORT_READ;
return false;
}
if(value==0xff) {
/* error !!, read into next frame */
*err=WTAP_ERR_BAD_FILE;
*err_info=g_strdup("eyesdn: No flag character seen in frame");
return false;
}
if(value==0xfe) {
/* we need to escape */
value=file_getc(fh);
if(value==-1) {
/* EOF or error */
*err=file_error(fh, err_info);
if(*err==0)
*err=WTAP_ERR_SHORT_READ;
return false;
}
value+=2;
}
buf[i]=value;
}
return true;
}
/* Magic text to check for eyesdn-ness of file */
static const unsigned char eyesdn_hdr_magic[] =
{ 'E', 'y', 'e', 'S', 'D', 'N'};
#define EYESDN_HDR_MAGIC_SIZE sizeof(eyesdn_hdr_magic)
/* Size of a record header */
#define EYESDN_HDR_LENGTH 12
static bool eyesdn_read(wtap *wth, wtap_rec *rec,
int *err, char **err_info, int64_t *data_offset);
static bool eyesdn_seek_read(wtap *wth, int64_t seek_off,
wtap_rec *rec, int *err, char **err_info);
static bool read_eyesdn_rec(FILE_T fh, wtap_rec *rec,
int *err, char **err_info);
/* Seeks to the beginning of the next packet, and returns the
byte offset. Returns -1 on failure, and sets "*err" to the error
and "*err_info" to null or an additional error string. */
static int64_t eyesdn_seek_next_packet(wtap *wth, int *err, char **err_info)
{
int byte;
int64_t cur_off;
while ((byte = file_getc(wth->fh)) != EOF) {
if (byte == 0xff) {
cur_off = file_tell(wth->fh);
if (cur_off == -1) {
/* Error. */
*err = file_error(wth->fh, err_info);
return -1;
}
return cur_off;
}
}
/* EOF or error. */
*err = file_error(wth->fh, err_info);
return -1;
}
wtap_open_return_val eyesdn_open(wtap *wth, int *err, char **err_info)
{
char magic[EYESDN_HDR_MAGIC_SIZE];
/* Look for eyesdn header */
if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
if (memcmp(magic, eyesdn_hdr_magic, EYESDN_HDR_MAGIC_SIZE) != 0)
return WTAP_OPEN_NOT_MINE;
wth->file_encap = WTAP_ENCAP_PER_PACKET;
wth->file_type_subtype = eyesdn_file_type_subtype;
wth->snapshot_length = 0; /* not known */
wth->subtype_read = eyesdn_read;
wth->subtype_seek_read = eyesdn_seek_read;
wth->file_tsprec = WTAP_TSPREC_USEC;
return WTAP_OPEN_MINE;
}
/* Find the next record and parse it; called from wtap_read(). */
static bool eyesdn_read(wtap *wth, wtap_rec *rec,
int *err, char **err_info, int64_t *data_offset)
{
int64_t offset;
/* Find the next record */
offset = eyesdn_seek_next_packet(wth, err, err_info);
if (offset < 1)
return false;
*data_offset = offset;
/* Parse the record */
return read_eyesdn_rec(wth->fh, rec, err, err_info);
}
/* Used to read packets in random-access fashion */
static bool
eyesdn_seek_read(wtap *wth, int64_t seek_off, wtap_rec *rec,
int *err, char **err_info)
{
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return false;
return read_eyesdn_rec(wth->random_fh, rec, err, err_info);
}
/* Parses a record. */
static bool
read_eyesdn_rec(FILE_T fh, wtap_rec *rec, int *err, char **err_info)
{
union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
uint8_t hdr[EYESDN_HDR_LENGTH];
time_t secs;
int usecs;
unsigned pkt_len;
uint8_t channel, direction;
uint8_t *pd;
/* Our file pointer should be at the summary information header
* for a packet. Read in that header and extract the useful
* information.
*/
if (!esc_read(fh, hdr, EYESDN_HDR_LENGTH, err, err_info))
return false;
wtap_setup_packet_rec(rec, WTAP_ENCAP_UNKNOWN);
rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
/* extract information from header */
usecs = pntohu24(&hdr[0]);
secs = pntohu40(&hdr[3]);
channel = hdr[8];
direction = hdr[9];
pkt_len = pntohu16(&hdr[10]);
switch(direction >> 1) {
default:
case EYESDN_ENCAP_ISDN: /* ISDN */
pseudo_header->isdn.uton = direction & 1;
pseudo_header->isdn.channel = channel;
if(channel) { /* bearer channels */
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_ISDN; /* recognises PPP */
pseudo_header->isdn.uton=!pseudo_header->isdn.uton; /* bug */
} else { /* D channel */
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_ISDN;
}
break;
case EYESDN_ENCAP_MSG: /* Layer 1 message */
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_LAYER1_EVENT;
pseudo_header->l1event.uton = (direction & 1);
break;
case EYESDN_ENCAP_LAPB: /* X.25 via LAPB */
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_LAPB;
pseudo_header->dte_dce.flags = (direction & 1) ? 0 : 0x80;
break;
case EYESDN_ENCAP_ATM: { /* ATM cells */
#define CELL_LEN 53
unsigned char cell[CELL_LEN];
int64_t cur_off;
if(pkt_len != CELL_LEN) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf(
"eyesdn: ATM cell has a length != 53 (%u)",
pkt_len);
return false;
}
cur_off = file_tell(fh);
if (!esc_read(fh, cell, CELL_LEN, err, err_info))
return false;
if (file_seek(fh, cur_off, SEEK_SET, err) == -1)
return false;
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
pseudo_header->atm.flags=ATM_RAW_CELL;
pseudo_header->atm.aal=AAL_UNKNOWN;
pseudo_header->atm.type=TRAF_UMTS_FP;
pseudo_header->atm.subtype=TRAF_ST_UNKNOWN;
pseudo_header->atm.vpi=((cell[0]&0xf)<<4) + (cell[0]&0xf);
pseudo_header->atm.vci=((cell[0]&0xf)<<4) + cell[0]; /* from cell */
pseudo_header->atm.channel=direction & 1;
}
break;
case EYESDN_ENCAP_MTP2: /* SS7 frames */
pseudo_header->mtp2.sent = direction & 1;
pseudo_header->mtp2.annex_a_used = MTP2_ANNEX_A_USED_UNKNOWN;
pseudo_header->mtp2.link_number = channel;
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_MTP2_WITH_PHDR;
break;
case EYESDN_ENCAP_DPNSS: /* DPNSS */
pseudo_header->isdn.uton = direction & 1;
pseudo_header->isdn.channel = channel;
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_DPNSS;
break;
case EYESDN_ENCAP_DASS2: /* DASS2 frames */
pseudo_header->isdn.uton = direction & 1;
pseudo_header->isdn.channel = channel;
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_DPNSS;
break;
case EYESDN_ENCAP_BACNET: /* BACNET async over HDLC frames */
pseudo_header->isdn.uton = direction & 1;
pseudo_header->isdn.channel = channel;
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR;
break;
case EYESDN_ENCAP_V5_EF: /* V5EF */
pseudo_header->isdn.uton = direction & 1;
pseudo_header->isdn.channel = channel;
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_V5_EF;
break;
}
if(pkt_len > WTAP_MAX_PACKET_SIZE_STANDARD) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("eyesdn: File has %u-byte packet, bigger than maximum of %u",
pkt_len, WTAP_MAX_PACKET_SIZE_STANDARD);
return false;
}
rec->presence_flags = WTAP_HAS_TS;
rec->ts.secs = secs;
rec->ts.nsecs = usecs * 1000;
rec->rec_header.packet_header.caplen = pkt_len;
rec->rec_header.packet_header.len = pkt_len;
/* Make sure we have enough room for the packet */
ws_buffer_assure_space(&rec->data, pkt_len);
pd = ws_buffer_start_ptr(&rec->data);
if (!esc_read(fh, pd, pkt_len, err, err_info))
return false;
return true;
}
static bool
esc_write(wtap_dumper *wdh, const uint8_t *buf, int len, int *err)
{
int i;
uint8_t byte;
static const uint8_t esc = 0xfe;
for(i=0; i<len; i++) {
byte=buf[i];
if(byte == 0xff || byte == 0xfe) {
/*
* Escape the frame delimiter and escape byte.
*/
if (!wtap_dump_file_write(wdh, &esc, sizeof esc, err))
return false;
byte-=2;
}
if (!wtap_dump_file_write(wdh, &byte, sizeof byte, err))
return false;
}
return true;
}
static bool eyesdn_dump(wtap_dumper *wdh, const wtap_rec *rec,
int *err, char **err_info);
static bool eyesdn_dump_open(wtap_dumper *wdh, int *err, char **err_info _U_)
{
wdh->subtype_write=eyesdn_dump;
if (!wtap_dump_file_write(wdh, eyesdn_hdr_magic,
EYESDN_HDR_MAGIC_SIZE, err))
return false;
*err=0;
return true;
}
static int eyesdn_dump_can_write_encap(int encap)
{
switch (encap) {
case WTAP_ENCAP_ISDN:
case WTAP_ENCAP_LAYER1_EVENT:
case WTAP_ENCAP_DPNSS:
case WTAP_ENCAP_ATM_PDUS_UNTRUNCATED:
case WTAP_ENCAP_LAPB:
case WTAP_ENCAP_MTP2_WITH_PHDR:
case WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR:
case WTAP_ENCAP_PER_PACKET:
return 0;
default:
return WTAP_ERR_UNWRITABLE_ENCAP;
}
}
/* Write a record for a packet to a dump file.
* Returns true on success, false on failure. */
static bool eyesdn_dump(wtap_dumper *wdh, const wtap_rec *rec,
int *err, char **err_info _U_)
{
static const uint8_t start_flag = 0xff;
const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
uint8_t buf[EYESDN_HDR_LENGTH];
int usecs;
time_t secs;
int channel;
int origin;
int protocol;
int size;
/* We can only write packet records. */
if (rec->rec_type != REC_TYPE_PACKET) {
*err = WTAP_ERR_UNWRITABLE_REC_TYPE;
*err_info = wtap_unwritable_rec_type_err_string(rec);
return false;
}
/* Don't write out anything bigger than we can read.
* (The length field in packet headers is 16 bits, which
* imposes a hard limit.) */
if (rec->rec_header.packet_header.caplen > 65535) {
*err = WTAP_ERR_PACKET_TOO_LARGE;
return false;
}
usecs=rec->ts.nsecs/1000;
secs=rec->ts.secs;
size=rec->rec_header.packet_header.caplen;
origin = pseudo_header->isdn.uton;
channel = pseudo_header->isdn.channel;
switch(rec->rec_header.packet_header.pkt_encap) {
case WTAP_ENCAP_ISDN:
protocol=EYESDN_ENCAP_ISDN; /* set depending on decoder format and mode */
break;
case WTAP_ENCAP_LAYER1_EVENT:
protocol=EYESDN_ENCAP_MSG;
break;
case WTAP_ENCAP_DPNSS:
protocol=EYESDN_ENCAP_DPNSS;
break;
#if 0
case WTAP_ENCAP_DASS2:
protocol=EYESDN_ENCAP_DASS2;
break;
#endif
case WTAP_ENCAP_ATM_PDUS_UNTRUNCATED:
protocol=EYESDN_ENCAP_ATM;
channel=0x80;
break;
case WTAP_ENCAP_LAPB:
protocol=EYESDN_ENCAP_LAPB;
break;
case WTAP_ENCAP_MTP2_WITH_PHDR:
protocol=EYESDN_ENCAP_MTP2;
break;
case WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR:
protocol=EYESDN_ENCAP_BACNET;
break;
case WTAP_ENCAP_V5_EF:
protocol=EYESDN_ENCAP_V5_EF;
break;
default:
*err=WTAP_ERR_UNWRITABLE_ENCAP;
return false;
}
phtonu24(&buf[0], usecs); /* 0-2 */
phtonu40(&buf[3], secs); /* 3-7 */
phtonu8(&buf[8], channel); /* 8 */
phtonu8(&buf[9], (origin?1:0) + (protocol << 1)); /* 9 */
phtonu16(&buf[10], size); /* 10-11 */
/* start flag */
if (!wtap_dump_file_write(wdh, &start_flag, sizeof start_flag, err))
return false;
if (!esc_write(wdh, buf, 12, err))
return false;
if (!esc_write(wdh, ws_buffer_start_ptr(&rec->data), size, err))
return false;
return true;
}
static const struct supported_block_type eyesdn_blocks_supported[] = {
/*
* We support packet blocks, with no comments or other options.
*/
{ WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED }
};
static const struct file_type_subtype_info eyesdn_info = {
"EyeSDN USB S0/E1 ISDN trace format", "eyesdn", "trc", NULL,
false, BLOCKS_SUPPORTED(eyesdn_blocks_supported),
eyesdn_dump_can_write_encap, eyesdn_dump_open, NULL
};
void register_eyesdn(void)
{
eyesdn_file_type_subtype = wtap_register_file_type_subtype(&eyesdn_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("EYESDN",
eyesdn_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: t
* End:
*
* vi: set shiftwidth=8 tabstop=8 noexpandtab:
* :indentSize=8:tabSize=8:noTabs=false:
*/
|