language
stringlengths 0
24
| filename
stringlengths 9
214
| code
stringlengths 99
9.93M
|
---|---|---|
C | wireshark/wiretap/peektagged.c | /* peektagged.c
* Routines for opening files in what Savvius (formerly WildPackets) calls
* the tagged file format in the description of their "PeekRdr Sample
* Application" (C++ source code to read their capture files, downloading
* of which requires a maintenance contract, so it's not free as in beer
* and probably not as in speech, either).
*
* As that description says, it's used by AiroPeek and AiroPeek NX 2.0
* and later, EtherPeek 6.0 and later, EtherPeek NX 3.0 and later,
* EtherPeek VX 1.0 and later, GigaPeek NX 1.0 and later, Omni3 1.0
* and later (both OmniPeek and the Remote Engine), and WANPeek NX
* 1.0 and later. They also say it'll be used by future Savvius
* products.
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <string.h>
#include <stdlib.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "peektagged.h"
#include <wsutil/802_11-utils.h>
/* CREDITS
*
* This file decoder could not have been written without examining
* http://www.varsanofiev.com/inside/airopeekv9.htm, the help from
* Martin Regner and Guy Harris, and the etherpeek.c file (as it
* was called before renaming it to peekclassic.c).
*/
/*
* Section header.
*
* A Peek tagged file consists of multiple sections, each of which begins
* with a header in the following format.
*
* The section ID is a 4-character string saying what type of section
* it is. The section length is a little-endian field giving the
* length of the section, in bytes, including the section header
* itself. The other field of the section header is a little-endian
* constant that always appears to be 0x00000200.
*
* Files we've seen have the following sections, in order:
*
* "\177vers" - version information. The contents are XML, giving
* the file format version and application version information.
*
* "sess" - capture session information. The contents are XML, giving
* various information about the capture session.
*
* "pkts" - captured packets. The contents are binary records, one for
* each packet, with the record being a list of tagged values followed
* by the raw packet data.
*/
typedef struct peektagged_section_header {
gint8 section_id[4]; /* string identifying the section */
guint32 section_len; /* little-endian section length */
guint32 section_const; /* little-endian 0x00000200 */
} peektagged_section_header_t;
/*
* Network subtype values.
*
* XXX - do different network subtype values for 802.11 indicate different
* network adapter types, with some adapters supplying the FCS and others
* not supplying the FCS?
*/
#define PEEKTAGGED_NST_ETHERNET 0
#define PEEKTAGGED_NST_802_11 1 /* 802.11 with 0's at the end */
#define PEEKTAGGED_NST_802_11_2 2 /* 802.11 with 0's at the end */
#define PEEKTAGGED_NST_802_11_WITH_FCS 3 /* 802.11 with FCS at the end */
/* tags for fields in packet header */
#define TAG_PEEKTAGGED_LENGTH 0x0000
#define TAG_PEEKTAGGED_TIMESTAMP_LOWER 0x0001
#define TAG_PEEKTAGGED_TIMESTAMP_UPPER 0x0002
#define TAG_PEEKTAGGED_FLAGS_AND_STATUS 0x0003 /* upper 24 bits unused? */
#define TAG_PEEKTAGGED_CHANNEL 0x0004
#define TAG_PEEKTAGGED_DATA_RATE_OR_MCS_INDEX 0x0005
#define TAG_PEEKTAGGED_SIGNAL_PERC 0x0006
#define TAG_PEEKTAGGED_SIGNAL_DBM 0x0007
#define TAG_PEEKTAGGED_NOISE_PERC 0x0008
#define TAG_PEEKTAGGED_NOISE_DBM 0x0009
#define TAG_PEEKTAGGED_UNKNOWN_0x000A 0x000A
#define TAG_PEEKTAGGED_CENTER_FREQUENCY 0x000D /* Frequency */
#define TAG_PEEKTAGGED_UNKNOWN_0x000E 0x000E /* "Band"? */
#define TAG_PEEKTAGGED_UNKNOWN_0x000F 0x000F /* antenna 2 signal dBm? */
#define TAG_PEEKTAGGED_UNKNOWN_0x0010 0x0010 /* antenna 3 signal dBm? */
#define TAG_PEEKTAGGED_UNKNOWN_0x0011 0x0011 /* antenna 4 signal dBm? */
#define TAG_PEEKTAGGED_UNKNOWN_0x0012 0x0012 /* antenna 2 noise dBm? */
#define TAG_PEEKTAGGED_UNKNOWN_0x0013 0x0013 /* antenna 3 noise dBm? */
#define TAG_PEEKTAGGED_UNKNOWN_0x0014 0x0014 /* antenna 4 noise dBm? */
#define TAG_PEEKTAGGED_EXT_FLAGS 0x0015 /* Extended flags for 802.11n and beyond */
#define TAG_PEEKTAGGED_SLICE_LENGTH 0xffff
/*
* Flags.
*
* We're assuming here that the "remote Peek" flags from bug 9586 are
* the same as the "Peek tagged" flags.
*
* Are these the same as in "Peek classic"? The first three are.
*/
#define FLAGS_CONTROL_FRAME 0x01 /* Frame is a control frame */
#define FLAGS_HAS_CRC_ERROR 0x02 /* Frame has a CRC error */
#define FLAGS_HAS_FRAME_ERROR 0x04 /* Frame has a frame error */
/*
* Status.
*
* Is this in the next 8 bits of the "flags and status" field?
*/
#define STATUS_PROTECTED 0x0400 /* Frame is protected (encrypted) */
#define STATUS_DECRYPT_ERROR 0x0800 /* Error decrypting protected frame */
#define STATUS_SHORT_PREAMBLE 0x4000 /* Short preamble */
/*
* Extended flags.
*
* Some determined from bug 10637, some determined from bug 9586,
* and the ones present in both agree, so we're assuming that
* the "remote Peek" protocol and the "Peek tagged" file format
* use the same bits (which wouldn't be too surprising, as they
* both come from Wildpackets).
*/
#define EXT_FLAG_20_MHZ_LOWER 0x00000001
#define EXT_FLAG_20_MHZ_UPPER 0x00000002
#define EXT_FLAG_40_MHZ 0x00000004
#define EXT_FLAGS_BANDWIDTH 0x00000007
#define EXT_FLAG_HALF_GI 0x00000008
#define EXT_FLAG_FULL_GI 0x00000010
#define EXT_FLAGS_GI 0x00000018
#define EXT_FLAG_AMPDU 0x00000020
#define EXT_FLAG_AMSDU 0x00000040
#define EXT_FLAG_802_11ac 0x00000080
#define EXT_FLAG_MCS_INDEX_USED 0x00000100
/* 64-bit time in nanoseconds from the (Windows FILETIME) epoch */
typedef struct peektagged_utime {
guint32 upper;
guint32 lower;
} peektagged_utime;
typedef struct {
gboolean has_fcs;
} peektagged_t;
static gboolean peektagged_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset);
static gboolean peektagged_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
static int peektagged_file_type_subtype = -1;
void register_peektagged(void);
static int wtap_file_read_pattern (wtap *wth, const char *pattern, int *err,
gchar **err_info)
{
int c;
const char *cp;
cp = pattern;
while (*cp)
{
c = file_getc(wth->fh);
if (c == EOF)
{
*err = file_error(wth->fh, err_info);
if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
return -1; /* error */
return 0; /* EOF */
}
if (c == *cp)
cp++;
else
{
if (c == pattern[0])
cp = &pattern[1];
else
cp = pattern;
}
}
return (*cp == '\0' ? 1 : 0);
}
static int wtap_file_read_till_separator (wtap *wth, char *buffer, int buflen,
const char *separators, int *err,
gchar **err_info)
{
int c;
char *cp;
int i;
for (cp = buffer, i = 0; i < buflen; i++, cp++)
{
c = file_getc(wth->fh);
if (c == EOF)
{
*err = file_error(wth->fh, err_info);
if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
return -1; /* error */
return 0; /* EOF */
}
if (strchr (separators, c) != NULL)
{
*cp = '\0';
break;
}
else
*cp = c;
}
return i;
}
static int wtap_file_read_number (wtap *wth, guint32 *num, int *err,
gchar **err_info)
{
int ret;
char str_num[12];
unsigned long value;
char *p;
ret = wtap_file_read_till_separator (wth, str_num, sizeof (str_num)-1, "<",
err, err_info);
if (ret == 0 || ret == -1) {
/* 0 means EOF, which means "not a valid Peek tagged file";
-1 means error, and "err" has been set. */
return ret;
}
value = strtoul (str_num, &p, 10);
if (p == str_num || value > G_MAXUINT32)
return 0;
*num = (guint32)value;
return 1;
}
wtap_open_return_val peektagged_open(wtap *wth, int *err, gchar **err_info)
{
peektagged_section_header_t ap_hdr;
int ret;
guint32 fileVersion = 0;
guint32 mediaType;
guint32 mediaSubType = 0;
int file_encap;
static const int peektagged_encap[] = {
WTAP_ENCAP_ETHERNET,
WTAP_ENCAP_IEEE_802_11_WITH_RADIO,
WTAP_ENCAP_IEEE_802_11_WITH_RADIO,
WTAP_ENCAP_IEEE_802_11_WITH_RADIO
};
#define NUM_PEEKTAGGED_ENCAPS (sizeof peektagged_encap / sizeof peektagged_encap[0])
peektagged_t *peektagged;
if (!wtap_read_bytes(wth->fh, &ap_hdr, (int)sizeof(ap_hdr), err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
if (memcmp (ap_hdr.section_id, "\177ver", sizeof(ap_hdr.section_id)) != 0)
return WTAP_OPEN_NOT_MINE; /* doesn't begin with a "\177ver" section */
/*
* XXX - we should get the length of the "\177ver" section, check
* that it's followed by a little-endian 0x00000200, and then,
* when reading the XML, make sure we don't go past the end of
* that section, and skip to the end of that section when
* we have the file version (and possibly check to make sure all
* tags are properly opened and closed).
*/
ret = wtap_file_read_pattern (wth, "<FileVersion>", err, err_info);
if (ret == -1)
return WTAP_OPEN_ERROR;
if (ret == 0) {
/* 0 means EOF, which means "not a valid Peek tagged file" */
return WTAP_OPEN_NOT_MINE;
}
ret = wtap_file_read_number (wth, &fileVersion, err, err_info);
if (ret == -1)
return WTAP_OPEN_ERROR;
if (ret == 0) {
/* 0 means EOF, which means "not a valid Peek tagged file" */
return WTAP_OPEN_NOT_MINE;
}
/* If we got this far, we assume it's a Peek tagged file. */
if (fileVersion != 9) {
/* We only support version 9. */
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("peektagged: version %u unsupported",
fileVersion);
return WTAP_OPEN_ERROR;
}
/*
* XXX - once we've skipped the "\177ver" section, we should
* check for a "sess" section and fail if we don't see it.
* Then we should get the length of the "sess" section, check
* that it's followed by a little-endian 0x00000200, and then,
* when reading the XML, make sure we don't go past the end of
* that section, and skip to the end of the section when
* we have the file version (and possibly check to make sure all
* tags are properly opened and closed).
*/
ret = wtap_file_read_pattern (wth, "<MediaType>", err, err_info);
if (ret == -1)
return WTAP_OPEN_ERROR;
if (ret == 0) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: <MediaType> tag not found");
return WTAP_OPEN_ERROR;
}
/* XXX - this appears to be 0 in both the EtherPeek and AiroPeek
files we've seen; should we require it to be 0? */
ret = wtap_file_read_number (wth, &mediaType, err, err_info);
if (ret == -1)
return WTAP_OPEN_ERROR;
if (ret == 0) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: <MediaType> value not found");
return WTAP_OPEN_ERROR;
}
ret = wtap_file_read_pattern (wth, "<MediaSubType>", err, err_info);
if (ret == -1)
return WTAP_OPEN_ERROR;
if (ret == 0) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: <MediaSubType> tag not found");
return WTAP_OPEN_ERROR;
}
ret = wtap_file_read_number (wth, &mediaSubType, err, err_info);
if (ret == -1)
return WTAP_OPEN_ERROR;
if (ret == 0) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: <MediaSubType> value not found");
return WTAP_OPEN_ERROR;
}
if (mediaSubType >= NUM_PEEKTAGGED_ENCAPS
|| peektagged_encap[mediaSubType] == WTAP_ENCAP_UNKNOWN) {
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("peektagged: network type %u unknown or unsupported",
mediaSubType);
return WTAP_OPEN_ERROR;
}
ret = wtap_file_read_pattern (wth, "pkts", err, err_info);
if (ret == -1)
return WTAP_OPEN_ERROR;
if (ret == 0) {
*err = WTAP_ERR_SHORT_READ;
return WTAP_OPEN_ERROR;
}
/* skip 8 zero bytes */
if (!wtap_read_bytes (wth->fh, NULL, 8, err, err_info)) {
return WTAP_OPEN_ERROR;
}
/*
* This is an Peek tagged file.
*/
file_encap = peektagged_encap[mediaSubType];
wth->file_type_subtype = peektagged_file_type_subtype;
wth->file_encap = file_encap;
wth->subtype_read = peektagged_read;
wth->subtype_seek_read = peektagged_seek_read;
wth->file_tsprec = WTAP_TSPREC_NSEC;
peektagged = g_new(peektagged_t, 1);
wth->priv = (void *)peektagged;
switch (mediaSubType) {
case PEEKTAGGED_NST_ETHERNET:
case PEEKTAGGED_NST_802_11:
case PEEKTAGGED_NST_802_11_2:
peektagged->has_fcs = FALSE;
break;
case PEEKTAGGED_NST_802_11_WITH_FCS:
peektagged->has_fcs = TRUE;
break;
}
wth->snapshot_length = 0; /* not available in header */
/*
* Add an IDB; we don't know how many interfaces were involved,
* so we just say one interface, about which we only know
* the link-layer type, snapshot length, and time stamp
* resolution.
*/
wtap_add_generated_idb(wth);
return WTAP_OPEN_MINE;
}
/*
* Read the packet.
*
* XXX - we should supply the additional radio information;
* the pseudo-header should probably be supplied in a fashion
* similar to the radiotap radio header, so that the 802.11
* dissector can determine which, if any, information items
* are present.
*/
static int
peektagged_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info)
{
peektagged_t *peektagged = (peektagged_t *)wth->priv;
gboolean read_a_tag = FALSE;
guint8 tag_value[6];
guint16 tag;
gboolean saw_length = FALSE;
guint32 length = 0;
guint32 sliceLength = 0;
gboolean saw_timestamp_lower = FALSE;
gboolean saw_timestamp_upper = FALSE;
gboolean saw_flags_and_status = FALSE;
peektagged_utime timestamp;
guint32 flags_and_status = 0;
guint32 ext_flags = 0;
gboolean saw_data_rate_or_mcs_index = FALSE;
guint32 data_rate_or_mcs_index = 0;
gint channel;
guint frequency;
struct ieee_802_11_phdr ieee_802_11 = {0};
guint i;
int skip_len = 0;
guint64 t;
timestamp.upper = 0;
timestamp.lower = 0;
ieee_802_11.fcs_len = -1; /* Unknown */
ieee_802_11.decrypted = FALSE;
ieee_802_11.datapad = FALSE;
ieee_802_11.phy = PHDR_802_11_PHY_UNKNOWN;
/* Extract the fields from the packet header */
do {
/* Get the tag and value.
XXX - this assumes all values are 4 bytes long. */
if (!wtap_read_bytes_or_eof(fh, tag_value, sizeof tag_value, err, err_info)) {
if (*err == 0) {
/*
* Short read if we've read something already;
* just an EOF if we haven't.
*/
if (read_a_tag)
*err = WTAP_ERR_SHORT_READ;
}
return -1;
}
read_a_tag = TRUE;
tag = pletoh16(&tag_value[0]);
switch (tag) {
case TAG_PEEKTAGGED_LENGTH:
if (saw_length) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: record has two length fields");
return -1;
}
length = pletoh32(&tag_value[2]);
saw_length = TRUE;
break;
case TAG_PEEKTAGGED_TIMESTAMP_LOWER:
if (saw_timestamp_lower) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: record has two timestamp-lower fields");
return -1;
}
timestamp.lower = pletoh32(&tag_value[2]);
saw_timestamp_lower = TRUE;
break;
case TAG_PEEKTAGGED_TIMESTAMP_UPPER:
if (saw_timestamp_upper) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: record has two timestamp-upper fields");
return -1;
}
timestamp.upper = pletoh32(&tag_value[2]);
saw_timestamp_upper = TRUE;
break;
case TAG_PEEKTAGGED_FLAGS_AND_STATUS:
saw_flags_and_status = TRUE;
flags_and_status = pletoh32(&tag_value[2]);
break;
case TAG_PEEKTAGGED_CHANNEL:
ieee_802_11.has_channel = TRUE;
ieee_802_11.channel = pletoh32(&tag_value[2]);
break;
case TAG_PEEKTAGGED_DATA_RATE_OR_MCS_INDEX:
data_rate_or_mcs_index = pletoh32(&tag_value[2]);
saw_data_rate_or_mcs_index = TRUE;
break;
case TAG_PEEKTAGGED_SIGNAL_PERC:
ieee_802_11.has_signal_percent = TRUE;
ieee_802_11.signal_percent = pletoh32(&tag_value[2]);
break;
case TAG_PEEKTAGGED_SIGNAL_DBM:
ieee_802_11.has_signal_dbm = TRUE;
ieee_802_11.signal_dbm = pletoh32(&tag_value[2]);
break;
case TAG_PEEKTAGGED_NOISE_PERC:
ieee_802_11.has_noise_percent = TRUE;
ieee_802_11.noise_percent = pletoh32(&tag_value[2]);
break;
case TAG_PEEKTAGGED_NOISE_DBM:
ieee_802_11.has_noise_dbm = TRUE;
ieee_802_11.noise_dbm = pletoh32(&tag_value[2]);
break;
case TAG_PEEKTAGGED_UNKNOWN_0x000A:
/*
* XXX - seen in some 802.11 captures.
* Always seems to have the value 0 or 5.
*/
break;
case TAG_PEEKTAGGED_CENTER_FREQUENCY:
/* XXX - also seen in an EtherPeek capture; value unknown */
ieee_802_11.has_frequency = TRUE;
ieee_802_11.frequency = pletoh32(&tag_value[2]);
break;
case TAG_PEEKTAGGED_UNKNOWN_0x000E:
/*
* XXX - seen in some 802.11 captures.
* Usually has the value 4, but, in some packets, has the
* values 6 or 302.
*
* Is this the mysterious "band" field that shows up in
* some "Peek remote" protocol captures, with values in
* the 30x or 40x ranges? It's not always associated
* with the "extended flags" tag for HT/VHT information,
* so it's probably not 11n/11ac-specific. Values other
* than 4 appear, in my captures, only in packets with
* the "extended flags" tag. 302 appeared in a packet
* with EXT_FLAG_MCS_INDEX_USED; 6 appeared in packets
* without EXT_FLAG_MCS_INDEX_USED.
*/
break;
case TAG_PEEKTAGGED_UNKNOWN_0x000F:
/*
* XXX - seen in some 802.11 captures; dB or dBm value?
* Multiple antennas?
*/
break;
case TAG_PEEKTAGGED_UNKNOWN_0x0010:
/*
* XXX - seen in some 802.11 captures; dB or dBm value?
* Multiple antennas?
*/
break;
case TAG_PEEKTAGGED_UNKNOWN_0x0011:
/*
* XXX - seen in some 802.11 captures; dB or dBm value?
* Multiple antennas?
*/
break;
case TAG_PEEKTAGGED_UNKNOWN_0x0012:
/*
* XXX - seen in some 802.11 captures; dB or dBm value?
* Multiple antennas?
*/
break;
case TAG_PEEKTAGGED_UNKNOWN_0x0013:
/*
* XXX - seen in some 802.11 captures; dB or dBm value?
* Multiple antennas?
*/
break;
case TAG_PEEKTAGGED_UNKNOWN_0x0014:
/*
* XXX - seen in some 802.11 captures; dB or dBm value?
* Multiple antennas?
*/
break;
case TAG_PEEKTAGGED_EXT_FLAGS:
/*
* We assume this is present for HT and VHT frames and absent
* for other frames.
*/
ext_flags = pletoh32(&tag_value[2]);
if (ext_flags & EXT_FLAG_802_11ac) {
ieee_802_11.phy = PHDR_802_11_PHY_11AC;
/*
* XXX - this probably has only one user, so only
* one MCS index and only one NSS, but where's the
* NSS?
*/
for (i = 0; i < 4; i++)
ieee_802_11.phy_info.info_11ac.nss[i] = 0;
switch (ext_flags & EXT_FLAGS_GI) {
case EXT_FLAG_HALF_GI:
ieee_802_11.phy_info.info_11ac.has_short_gi = TRUE;
ieee_802_11.phy_info.info_11ac.short_gi = 1;
break;
case EXT_FLAG_FULL_GI:
ieee_802_11.phy_info.info_11ac.has_short_gi = TRUE;
ieee_802_11.phy_info.info_11ac.short_gi = 0;
break;
default:
/* Mutually exclusive flags set or nothing set */
break;
}
} else {
ieee_802_11.phy = PHDR_802_11_PHY_11N;
switch (ext_flags & EXT_FLAGS_BANDWIDTH) {
case 0:
ieee_802_11.phy_info.info_11n.has_bandwidth = TRUE;
ieee_802_11.phy_info.info_11n.bandwidth = PHDR_802_11_BANDWIDTH_20_MHZ;
break;
case EXT_FLAG_20_MHZ_LOWER:
ieee_802_11.phy_info.info_11n.has_bandwidth = TRUE;
ieee_802_11.phy_info.info_11n.bandwidth = PHDR_802_11_BANDWIDTH_20_20L;
break;
case EXT_FLAG_20_MHZ_UPPER:
ieee_802_11.phy_info.info_11n.has_bandwidth = TRUE;
ieee_802_11.phy_info.info_11n.bandwidth = PHDR_802_11_BANDWIDTH_20_20U;
break;
case EXT_FLAG_40_MHZ:
ieee_802_11.phy_info.info_11n.has_bandwidth = TRUE;
ieee_802_11.phy_info.info_11n.bandwidth = PHDR_802_11_BANDWIDTH_40_MHZ;
break;
default:
/* Mutually exclusive flags set */
break;
}
switch (ext_flags & EXT_FLAGS_GI) {
case EXT_FLAG_HALF_GI:
ieee_802_11.phy_info.info_11n.has_short_gi = TRUE;
ieee_802_11.phy_info.info_11n.short_gi = 1;
break;
case EXT_FLAG_FULL_GI:
ieee_802_11.phy_info.info_11n.has_short_gi = TRUE;
ieee_802_11.phy_info.info_11n.short_gi = 0;
break;
default:
/* Mutually exclusive flags set or nothing set */
break;
}
}
break;
case TAG_PEEKTAGGED_SLICE_LENGTH:
sliceLength = pletoh32(&tag_value[2]);
break;
default:
break;
}
} while (tag != TAG_PEEKTAGGED_SLICE_LENGTH); /* last tag */
if (!saw_length) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: record has no length field");
return -1;
}
if (!saw_timestamp_lower) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: record has no timestamp-lower field");
return -1;
}
if (!saw_timestamp_upper) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: record has no timestamp-upper field");
return -1;
}
/*
* If sliceLength is 0, force it to be the actual length of the packet.
*/
if (sliceLength == 0)
sliceLength = length;
if (sliceLength > WTAP_MAX_PACKET_SIZE_STANDARD) {
/*
* Probably a corrupt capture file; don't blow up trying
* to allocate space for an immensely-large packet.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("peektagged: File has %u-byte packet, bigger than maximum of %u",
sliceLength, WTAP_MAX_PACKET_SIZE_STANDARD);
return -1;
}
rec->rec_type = REC_TYPE_PACKET;
rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
rec->rec_header.packet_header.len = length;
rec->rec_header.packet_header.caplen = sliceLength;
if (saw_flags_and_status) {
guint32 flags = 0;
if (flags_and_status & FLAGS_HAS_CRC_ERROR)
flags |= PACK_FLAGS_CRC_ERROR;
wtap_block_add_uint32_option(rec->block, OPT_PKT_FLAGS, flags);
}
/* calculate and fill in packet time stamp */
t = (((guint64) timestamp.upper) << 32) + timestamp.lower;
if (!nsfiletime_to_nstime(&rec->ts, t)) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("peektagged: time stamp outside supported range");
return -1;
}
switch (wth->file_encap) {
case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
if (saw_data_rate_or_mcs_index) {
if (ext_flags & EXT_FLAG_MCS_INDEX_USED) {
/*
* It's an MCS index.
*
* XXX - what about 11ac?
*/
if (!(ext_flags & EXT_FLAG_802_11ac)) {
ieee_802_11.phy_info.info_11n.has_mcs_index = TRUE;
ieee_802_11.phy_info.info_11n.mcs_index = data_rate_or_mcs_index;
}
} else {
/* It's a data rate. */
ieee_802_11.has_data_rate = TRUE;
ieee_802_11.data_rate = data_rate_or_mcs_index;
if (ieee_802_11.phy == PHDR_802_11_PHY_UNKNOWN) {
/*
* We don't know they PHY; try to guess it based
* on the data rate and channel/center frequency.
*/
if (RATE_IS_DSSS(ieee_802_11.data_rate)) {
/* 11b */
ieee_802_11.phy = PHDR_802_11_PHY_11B;
if (saw_flags_and_status) {
ieee_802_11.phy_info.info_11b.has_short_preamble = TRUE;
ieee_802_11.phy_info.info_11b.short_preamble =
(flags_and_status & STATUS_SHORT_PREAMBLE) ? TRUE : FALSE;;
} else
ieee_802_11.phy_info.info_11b.has_short_preamble = FALSE;
} else if (RATE_IS_OFDM(ieee_802_11.data_rate)) {
/* 11a or 11g, depending on the band. */
if (ieee_802_11.has_channel) {
if (CHAN_IS_BG(ieee_802_11.channel)) {
/* 11g */
ieee_802_11.phy = PHDR_802_11_PHY_11G;
} else {
/* 11a */
ieee_802_11.phy = PHDR_802_11_PHY_11A;
}
} else if (ieee_802_11.has_frequency) {
if (FREQ_IS_BG(ieee_802_11.frequency)) {
/* 11g */
ieee_802_11.phy = PHDR_802_11_PHY_11G;
} else {
/* 11a */
ieee_802_11.phy = PHDR_802_11_PHY_11A;
}
}
if (ieee_802_11.phy == PHDR_802_11_PHY_11G) {
/* Set 11g metadata */
ieee_802_11.phy_info.info_11g.has_mode = FALSE;
} else if (ieee_802_11.phy == PHDR_802_11_PHY_11A) {
/* Set 11a metadata */
ieee_802_11.phy_info.info_11a.has_channel_type = FALSE;
ieee_802_11.phy_info.info_11a.has_turbo_type = FALSE;
}
/* Otherwise we don't know the PHY */
}
}
}
}
if (ieee_802_11.has_frequency && !ieee_802_11.has_channel) {
/* Frequency, but no channel; try to calculate the channel. */
channel = ieee80211_mhz_to_chan(ieee_802_11.frequency);
if (channel != -1) {
ieee_802_11.has_channel = TRUE;
ieee_802_11.channel = channel;
}
} else if (ieee_802_11.has_channel && !ieee_802_11.has_frequency) {
/*
* If it's 11 legacy DHSS, 11b, or 11g, it's 2.4 GHz,
* so we can calculate the frequency.
*
* If it's 11a, it's 5 GHz, so we can calculate the
* frequency.
*/
switch (ieee_802_11.phy) {
case PHDR_802_11_PHY_11_DSSS:
case PHDR_802_11_PHY_11B:
case PHDR_802_11_PHY_11G:
frequency = ieee80211_chan_to_mhz(ieee_802_11.channel, TRUE);
break;
case PHDR_802_11_PHY_11A:
frequency = ieee80211_chan_to_mhz(ieee_802_11.channel, FALSE);
break;
default:
/* We don't know the band. */
frequency = 0;
break;
}
if (frequency != 0) {
ieee_802_11.has_frequency = TRUE;
ieee_802_11.frequency = frequency;
}
}
rec->rec_header.packet_header.pseudo_header.ieee_802_11 = ieee_802_11;
if (peektagged->has_fcs)
rec->rec_header.packet_header.pseudo_header.ieee_802_11.fcs_len = 4;
else {
if (rec->rec_header.packet_header.len < 4 || rec->rec_header.packet_header.caplen < 4) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("peektagged: 802.11 packet has length < 4");
return FALSE;
}
rec->rec_header.packet_header.pseudo_header.ieee_802_11.fcs_len = 0;
rec->rec_header.packet_header.len -= 4;
rec->rec_header.packet_header.caplen -= 4;
skip_len = 4;
}
rec->rec_header.packet_header.pseudo_header.ieee_802_11.decrypted = FALSE;
rec->rec_header.packet_header.pseudo_header.ieee_802_11.datapad = FALSE;
break;
case WTAP_ENCAP_ETHERNET:
/*
* The last 4 bytes appear to be 0 in the captures I've seen;
* are there any captures where it's an FCS?
*/
if (rec->rec_header.packet_header.len < 4 || rec->rec_header.packet_header.caplen < 4) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("peektagged: Ethernet packet has length < 4");
return FALSE;
}
rec->rec_header.packet_header.pseudo_header.eth.fcs_len = 0;
rec->rec_header.packet_header.len -= 4;
rec->rec_header.packet_header.caplen -= 4;
skip_len = 4;
break;
}
/* Read the packet data. */
if (!wtap_read_packet_bytes(fh, buf, rec->rec_header.packet_header.caplen, err, err_info))
return -1;
return skip_len;
}
static gboolean peektagged_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset)
{
int skip_len;
*data_offset = file_tell(wth->fh);
/* Read the packet. */
skip_len = peektagged_read_packet(wth, wth->fh, rec, buf, err, err_info);
if (skip_len == -1)
return FALSE;
if (skip_len != 0) {
/* Skip extra junk at the end of the packet data. */
if (!wtap_read_bytes(wth->fh, NULL, skip_len, err, err_info))
return FALSE;
}
return TRUE;
}
static gboolean
peektagged_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
{
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return FALSE;
/* Read the packet. */
if (peektagged_read_packet(wth, wth->random_fh, rec, buf, err, err_info) == -1) {
if (*err == 0)
*err = WTAP_ERR_SHORT_READ;
return FALSE;
}
return TRUE;
}
static const struct supported_block_type peektagged_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 peektagged_info = {
"Savvius tagged", "peektagged", "pkt", "tpc;apc;wpz",
FALSE, BLOCKS_SUPPORTED(peektagged_blocks_supported),
NULL, NULL, NULL
};
void register_peektagged(void)
{
peektagged_file_type_subtype = wtap_register_file_type_subtype(&peektagged_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("PEEKTAGGED",
peektagged_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/peektagged.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __W_PEEKTAGGED_H__
#define __W_PEEKTAGGED_H__
#include <glib.h>
#include "ws_symbol_export.h"
wtap_open_return_val peektagged_open(wtap *wth, int *err, gchar **err_info);
#endif |
C | wireshark/wiretap/pppdump.c | /* pppdump.c
*
* Copyright (c) 2000 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "wtap-int.h"
#include "pppdump.h"
#include "file_wrappers.h"
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <wsutil/ws_assert.h>
/*
pppdump records
Daniel Thompson (STMicroelectronics) <[email protected]>
+------+
| 0x07 | Reset time
+------+------+------+------+
| t3 | t2 | t1 | t0 | t = time_t
+------+------+------+------+
+------+
| 0x06 | Time step (short)
+------+
| ts | ts = time step (tenths of seconds)
+------+
+------+
| 0x05 | Time step (long)
+------+------+------+------+
| ts3 | ts2 | ts1 | ts0 | ts = time step (tenths of seconds)
+------+------+------+------+
+------+
| 0x04 | Receive deliminator (not seen in practice)
+------+
+------+
| 0x03 | Send deliminator (not seen in practice)
+------+
+------+
| 0x02 | Received data
+------+------+
| n1 | n0 | n = number of bytes following
+------+------+
| data |
| |
+------+
| 0x01 | Sent data
+------+------+
| n1 | n0 | n = number of bytes following
+------+------+
| data |
| |
*/
#define PPPD_SENT_DATA 0x01
#define PPPD_RECV_DATA 0x02
#define PPPD_SEND_DELIM 0x03
#define PPPD_RECV_DELIM 0x04
#define PPPD_TIME_STEP_LONG 0x05
#define PPPD_TIME_STEP_SHORT 0x06
#define PPPD_RESET_TIME 0x07
/* this buffer must be at least (2*PPPD_MTU) + sizeof(ppp_header) +
* sizeof(lcp_header) + sizeof(ipcp_header). PPPD_MTU is *very* rarely
* larger than 1500 so this value is fine.
*
* It's less than WTAP_MAX_PACKET_SIZE_STANDARD, so we don't have to worry about
* too-large packets.
*/
#define PPPD_BUF_SIZE 8192
typedef enum {
DIRECTION_SENT,
DIRECTION_RECV
} direction_enum;
static gboolean pppdump_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset);
static gboolean pppdump_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
static int pppdump_file_type_subtype = -1;
void register_pppdump(void);
/*
* Information saved about a packet, during the initial sequential pass
* through the file, to allow us to later re-read it when randomly
* reading packets.
*
* "offset" is the offset in the file of the first data chunk containing data
* from that packet; note that it may also contain data from previous
* packets.
*
* "num_bytes_to_skip" is the number of bytes from previous packets in that
* first data chunk.
*
* "dir" is the direction of the packet.
*/
typedef struct {
gint64 offset;
gint64 num_bytes_to_skip;
direction_enum dir;
} pkt_id;
/*
* Information about a packet currently being processed. There is one of
* these for the sent packet being processed and one of these for the
* received packet being processed, as we could be in the middle of
* processing both a received packet and a sent packet.
*
* "dir" is the direction of the packet.
*
* "cnt" is the number of bytes of packet data we've accumulated.
*
* "esc" is TRUE if the next byte we see is escaped (and thus must be XORed
* with 0x20 before saving it), FALSE otherwise.
*
* "buf" is a buffer containing the packet data we've accumulated.
*
* "id_offset" is the offset in the file of the first data chunk
* containing data from the packet we're processing.
*
* "sd_offset" is the offset in the file of the first data byte from
* the packet we're processing - which isn't necessarily right after
* the header of the first data chunk, as we may already have assembled
* packets from that chunk.
*
* "cd_offset" is the offset in the file of the current data chunk we're
* processing.
*/
typedef struct {
direction_enum dir;
int cnt;
gboolean esc;
guint8 buf[PPPD_BUF_SIZE];
gint64 id_offset;
gint64 sd_offset;
gint64 cd_offset;
} pkt_t;
/*
* This keeps state used while processing records.
*
* "timestamp" is the seconds portion of the current time stamp value,
* as updated from PPPD_RESET_TIME, PPPD_TIME_STEP_LONG, and
* PPPD_TIME_STEP_SHORT records. "tenths" is the tenths-of-seconds
* portion.
*
* "spkt" and "rpkt" are "pkt_t" structures for the sent and received
* packets we're currently working on.
*
* "offset" is the current offset in the file.
*
* "num_bytes" and "pkt" are information saved when we finish accumulating
* the data for a packet, if the data chunk we're working on still has more
* data in it:
*
* "num_bytes" is the number of bytes of additional data remaining
* in the chunk after we've finished accumulating the data for the
* packet.
*
* "pkt" is the "pkt_t" for the type of packet the data chunk is for
* (sent or received packet).
*
* "seek_state" is another state structure used while processing records
* when doing a seek-and-read. (That structure doesn't itself have a
* "seek_state" structure.)
*
* "pids" is a GPtrArray of pointers to "pkt_id" structures for all the
* packets we've seen during the initial sequential pass, to allow us to
* later retrieve them with random accesses.
*
* "pkt_cnt" is the number of packets we've seen up to this point in the
* sequential pass.
*/
typedef struct _pppdump_t {
time_t timestamp;
guint tenths;
pkt_t spkt;
pkt_t rpkt;
gint64 offset;
int num_bytes;
pkt_t *pkt;
struct _pppdump_t *seek_state;
GPtrArray *pids;
guint pkt_cnt;
} pppdump_t;
static int
process_data(pppdump_t *state, FILE_T fh, pkt_t *pkt, int n, guint8 *pd,
int *err, gchar **err_info, pkt_id *pid);
static gboolean
collate(pppdump_t *state, FILE_T fh, int *err, gchar **err_info, guint8 *pd,
int *num_bytes, direction_enum *direction, pkt_id *pid,
gint64 num_bytes_to_skip);
static void
pppdump_close(wtap *wth);
static void
init_state(pppdump_t *state)
{
state->num_bytes = 0;
state->pkt = NULL;
state->spkt.dir = DIRECTION_SENT;
state->spkt.cnt = 0;
state->spkt.esc = FALSE;
state->spkt.id_offset = 0;
state->spkt.sd_offset = 0;
state->spkt.cd_offset = 0;
state->rpkt.dir = DIRECTION_RECV;
state->rpkt.cnt = 0;
state->rpkt.esc = FALSE;
state->rpkt.id_offset = 0;
state->rpkt.sd_offset = 0;
state->rpkt.cd_offset = 0;
state->seek_state = NULL;
state->offset = 0x100000; /* to detect errors during development */
}
wtap_open_return_val
pppdump_open(wtap *wth, int *err, gchar **err_info)
{
guint8 buffer[6]; /* Looking for: 0x07 t3 t2 t1 t0 ID */
pppdump_t *state;
/* There is no file header, only packet records. Fortunately for us,
* timestamp records are separated from packet records, so we should
* find an "initial time stamp" (i.e., a "reset time" record, or
* record type 0x07) at the beginning of the file. We'll check for
* that, plus a valid record following the 0x07 and the four bytes
* representing the timestamp.
*/
if (!wtap_read_bytes(wth->fh, buffer, sizeof(buffer),
err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
if (buffer[0] == PPPD_RESET_TIME &&
(buffer[5] == PPPD_SENT_DATA ||
buffer[5] == PPPD_RECV_DATA ||
buffer[5] == PPPD_TIME_STEP_LONG ||
buffer[5] == PPPD_TIME_STEP_SHORT ||
buffer[5] == PPPD_RESET_TIME)) {
goto my_file_type;
}
else {
return WTAP_OPEN_NOT_MINE;
}
my_file_type:
if (file_seek(wth->fh, 5, SEEK_SET, err) == -1)
return WTAP_OPEN_ERROR;
state = g_new(pppdump_t, 1);
wth->priv = (void *)state;
state->timestamp = pntoh32(&buffer[1]);
state->tenths = 0;
init_state(state);
state->offset = 5;
wth->file_encap = WTAP_ENCAP_PPP_WITH_PHDR;
wth->file_type_subtype = pppdump_file_type_subtype;
wth->snapshot_length = PPPD_BUF_SIZE; /* just guessing */
wth->subtype_read = pppdump_read;
wth->subtype_seek_read = pppdump_seek_read;
wth->subtype_close = pppdump_close;
wth->file_tsprec = WTAP_TSPREC_DSEC;
state->seek_state = g_new(pppdump_t,1);
/* If we have a random stream open, we're going to be reading
the file randomly; set up a GPtrArray of pointers to
information about how to retrieve the data for each packet. */
if (wth->random_fh != NULL)
state->pids = g_ptr_array_new();
else
state->pids = NULL;
state->pkt_cnt = 0;
/*
* Add an IDB; we don't know how many interfaces were
* involved, so we just say one interface, about which
* we only know the link-layer type, snapshot length,
* and time stamp resolution.
*/
wtap_add_generated_idb(wth);
return WTAP_OPEN_MINE;
}
/* Set part of the struct wtap_rec. */
static void
pppdump_set_phdr(wtap_rec *rec, int num_bytes,
direction_enum direction)
{
rec->rec_type = REC_TYPE_PACKET;
rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
rec->rec_header.packet_header.len = num_bytes;
rec->rec_header.packet_header.caplen = num_bytes;
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_PPP_WITH_PHDR;
rec->rec_header.packet_header.pseudo_header.p2p.sent = (direction == DIRECTION_SENT ? TRUE : FALSE);
}
/* Find the next packet and parse it; called from wtap_read(). */
static gboolean
pppdump_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err, gchar **err_info,
gint64 *data_offset)
{
int num_bytes;
direction_enum direction;
pppdump_t *state;
pkt_id *pid;
state = (pppdump_t *)wth->priv;
/* If we have a random stream open, allocate a structure to hold
the information needed to read this packet's data again. */
if (wth->random_fh != NULL) {
pid = g_new(pkt_id, 1);
if (!pid) {
*err = errno; /* assume a malloc failed and set "errno" */
return FALSE;
}
pid->offset = 0;
} else
pid = NULL; /* sequential only */
ws_buffer_assure_space(buf, PPPD_BUF_SIZE);
if (!collate(state, wth->fh, err, err_info, ws_buffer_start_ptr(buf),
&num_bytes, &direction, pid, 0)) {
g_free(pid);
return FALSE;
}
if (pid != NULL)
pid->dir = direction;
if (pid != NULL)
g_ptr_array_add(state->pids, pid);
/* The user's data_offset is not really an offset, but a packet number. */
*data_offset = state->pkt_cnt;
state->pkt_cnt++;
pppdump_set_phdr(rec, num_bytes, direction);
rec->presence_flags = WTAP_HAS_TS;
rec->ts.secs = state->timestamp;
rec->ts.nsecs = state->tenths * 100000000;
return TRUE;
}
/* Returns number of bytes copied for record, -1 if failure.
*
* This is modeled after pppdump.c, the utility to parse pppd log files; it
* comes with the ppp distribution.
*/
static int
process_data(pppdump_t *state, FILE_T fh, pkt_t *pkt, int n, guint8 *pd,
int *err, gchar **err_info, pkt_id *pid)
{
int c;
int num_bytes = n;
int num_written;
for (; num_bytes > 0; --num_bytes) {
c = file_getc(fh);
if (c == EOF) {
*err = file_error(fh, err_info);
if (*err == 0) {
*err = WTAP_ERR_SHORT_READ;
}
return -1;
}
state->offset++;
switch (c) {
case 0x7e:
/*
* Flag Sequence for RFC 1662 HDLC-like
* framing.
*
* As this is a raw trace of octets going
* over the wire, and that might include
* the login sequence, there is no
* guarantee that *only* PPP traffic
* appears in this file, so there is no
* guarantee that the first 0x7e we see is
* a start flag sequence, and therefore we
* cannot safely ignore all characters up
* to the first 0x7e, and therefore we
* might end up with some bogus PPP
* packets.
*/
if (pkt->cnt > 0) {
/*
* We've seen stuff before this,
* so this is the end of a frame.
* Make a frame out of that stuff.
*/
pkt->esc = FALSE;
num_written = pkt->cnt;
pkt->cnt = 0;
if (num_written <= 0) {
return 0;
}
if (num_written > PPPD_BUF_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pppdump: File has %u-byte packet, bigger than maximum of %u",
num_written, PPPD_BUF_SIZE);
return -1;
}
memcpy(pd, pkt->buf, num_written);
/*
* Remember the offset of the
* first record containing data
* for this packet, and how far
* into that record to skip to
* get to the beginning of the
* data for this packet; the number
* of bytes to skip into that record
* is the file offset of the first
* byte of this packet minus the
* file offset of the first byte of
* this record, minus 3 bytes for the
* header of this record (which, if
* we re-read this record, we will
* process, not skip).
*/
if (pid) {
pid->offset = pkt->id_offset;
pid->num_bytes_to_skip =
pkt->sd_offset - pkt->id_offset - 3;
ws_assert(pid->num_bytes_to_skip >= 0);
}
num_bytes--;
if (num_bytes > 0) {
/*
* There's more data in this
* record.
* Set the initial data offset
* for the next packet.
*/
pkt->id_offset = pkt->cd_offset;
pkt->sd_offset = state->offset;
} else {
/*
* There is no more data in
* this record.
* Thus, we don't have the
* initial data offset for
* the next packet.
*/
pkt->id_offset = 0;
pkt->sd_offset = 0;
}
state->num_bytes = num_bytes;
state->pkt = pkt;
return num_written;
}
break;
case 0x7d:
/*
* Control Escape octet for octet-stuffed
* RFC 1662 HDLC-like framing.
*/
if (!pkt->esc) {
/*
* Control Escape not preceded by
* Control Escape; discard it
* but XOR the next octet with
* 0x20.
*/
pkt->esc = TRUE;
break;
}
/*
* Control Escape preceded by Control Escape;
* treat it as an ordinary character,
* by falling through.
*/
/* FALL THROUGH */
default:
if (pkt->esc) {
/*
* This character was preceded by
* Control Escape, so XOR it with
* 0x20, as per RFC 1662's octet-
* stuffed framing, and clear
* the flag saying that the
* character should be escaped.
*/
c ^= 0x20;
pkt->esc = FALSE;
}
if (pkt->cnt >= PPPD_BUF_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pppdump: File has %u-byte packet, bigger than maximum of %u",
pkt->cnt - 1, PPPD_BUF_SIZE);
return -1;
}
pkt->buf[pkt->cnt++] = c;
break;
}
}
/* we could have run out of bytes to read */
return 0;
}
/* Returns TRUE if packet data copied, FALSE if error occurred or EOF (no more records). */
static gboolean
collate(pppdump_t* state, FILE_T fh, int *err, gchar **err_info, guint8 *pd,
int *num_bytes, direction_enum *direction, pkt_id *pid,
gint64 num_bytes_to_skip)
{
int id;
pkt_t *pkt = NULL;
int byte0, byte1;
int n, num_written = 0;
gint64 start_offset;
guint32 time_long;
guint8 time_short;
/*
* Process any data left over in the current record when doing
* sequential processing.
*/
if (state->num_bytes > 0) {
ws_assert(num_bytes_to_skip == 0);
pkt = state->pkt;
num_written = process_data(state, fh, pkt, state->num_bytes,
pd, err, err_info, pid);
if (num_written < 0) {
return FALSE;
}
else if (num_written > 0) {
*num_bytes = num_written;
*direction = pkt->dir;
return TRUE;
}
/* if 0 bytes written, keep processing */
} else {
/*
* We didn't have any data left over, so the packet will
* start at the beginning of a record.
*/
if (pid)
pid->num_bytes_to_skip = 0;
}
/*
* That didn't get all the data for this packet, so process
* subsequent records.
*/
start_offset = state->offset;
while ((id = file_getc(fh)) != EOF) {
state->offset++;
switch (id) {
case PPPD_SENT_DATA:
case PPPD_RECV_DATA:
pkt = id == PPPD_SENT_DATA ? &state->spkt : &state->rpkt;
/*
* Save the offset of the beginning of
* the current record.
*/
pkt->cd_offset = state->offset - 1;
/*
* Get the length of the record.
*/
byte0 = file_getc(fh);
if (byte0 == EOF)
goto done;
state->offset++;
byte1 = file_getc(fh);
if (byte1 == EOF)
goto done;
state->offset++;
n = (byte0 << 8) | byte1;
if (pkt->id_offset == 0) {
/*
* We don't have the initial data
* offset for this packet, which
* means this is the first
* data record for that packet.
* Save the offset of the
* beginning of that record and
* the offset of the first data
* byte in the packet, which is
* the first data byte in the
* record.
*/
pkt->id_offset = pkt->cd_offset;
pkt->sd_offset = state->offset;
}
if (n == 0)
continue;
ws_assert(num_bytes_to_skip < n);
while (num_bytes_to_skip) {
if (file_getc(fh) == EOF)
goto done;
state->offset++;
num_bytes_to_skip--;
n--;
}
num_written = process_data(state, fh, pkt, n,
pd, err, err_info, pid);
if (num_written < 0) {
return FALSE;
}
else if (num_written > 0) {
*num_bytes = num_written;
*direction = pkt->dir;
return TRUE;
}
/* if 0 bytes written, keep looping */
break;
case PPPD_SEND_DELIM:
case PPPD_RECV_DELIM:
/* What can we do? */
break;
case PPPD_RESET_TIME:
if (!wtap_read_bytes(fh, &time_long, sizeof(guint32), err, err_info))
return FALSE;
state->offset += sizeof(guint32);
state->timestamp = pntoh32(&time_long);
state->tenths = 0;
break;
case PPPD_TIME_STEP_LONG:
if (!wtap_read_bytes(fh, &time_long, sizeof(guint32), err, err_info))
return FALSE;
state->offset += sizeof(guint32);
state->tenths += pntoh32(&time_long);
if (state->tenths >= 10) {
state->timestamp += state->tenths / 10;
state->tenths = state->tenths % 10;
}
break;
case PPPD_TIME_STEP_SHORT:
if (!wtap_read_bytes(fh, &time_short, sizeof(guint8), err, err_info))
return FALSE;
state->offset += sizeof(guint8);
state->tenths += time_short;
if (state->tenths >= 10) {
state->timestamp += state->tenths / 10;
state->tenths = state->tenths % 10;
}
break;
default:
/* XXX - bad file */
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("pppdump: bad ID byte 0x%02x", id);
return FALSE;
}
}
done:
*err = file_error(fh, err_info);
if (*err == 0) {
if (state->offset != start_offset) {
/*
* We read at least one byte, so we were working
* on a record; an EOF means that record was
* cut short.
*/
*err = WTAP_ERR_SHORT_READ;
}
}
return FALSE;
}
/* Used to read packets in random-access fashion */
static gboolean
pppdump_seek_read(wtap *wth,
gint64 seek_off,
wtap_rec *rec,
Buffer *buf,
int *err,
gchar **err_info)
{
int num_bytes;
guint8 *pd;
direction_enum direction;
pppdump_t *state;
pkt_id *pid;
gint64 num_bytes_to_skip;
state = (pppdump_t *)wth->priv;
pid = (pkt_id *)g_ptr_array_index(state->pids, seek_off);
if (!pid) {
*err = WTAP_ERR_BAD_FILE; /* XXX - better error? */
*err_info = g_strdup("pppdump: PID not found for record");
return FALSE;
}
if (file_seek(wth->random_fh, pid->offset, SEEK_SET, err) == -1)
return FALSE;
init_state(state->seek_state);
state->seek_state->offset = pid->offset;
ws_buffer_assure_space(buf, PPPD_BUF_SIZE);
pd = ws_buffer_start_ptr(buf);
/*
* We'll start reading at the first record containing data from
* this packet; however, that doesn't mean "collate()" will
* stop only when we've read that packet, as there might be
* data for packets going in the other direction as well, and
* we might finish processing one of those packets before we
* finish processing the packet we're reading.
*
* Therefore, we keep reading until we get a packet that's
* going in the direction we want.
*/
num_bytes_to_skip = pid->num_bytes_to_skip;
do {
if (!collate(state->seek_state, wth->random_fh, err, err_info,
pd, &num_bytes, &direction, NULL, num_bytes_to_skip))
return FALSE;
num_bytes_to_skip = 0;
} while (direction != pid->dir);
pppdump_set_phdr(rec, num_bytes, pid->dir);
return TRUE;
}
static void
pppdump_close(wtap *wth)
{
pppdump_t *state;
state = (pppdump_t *)wth->priv;
if (state->seek_state) { /* should always be TRUE */
g_free(state->seek_state);
}
if (state->pids) {
unsigned int i;
for (i = 0; i < g_ptr_array_len(state->pids); i++) {
g_free(g_ptr_array_index(state->pids, i));
}
g_ptr_array_free(state->pids, TRUE);
}
}
static const struct supported_block_type pppdump_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 pppdump_info = {
"pppd log (pppdump format)", "pppd", NULL, NULL,
FALSE, BLOCKS_SUPPORTED(pppdump_blocks_supported),
NULL, NULL, NULL
};
void register_pppdump(void)
{
pppdump_file_type_subtype = wtap_register_file_type_subtype(&pppdump_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("PPPDUMP",
pppdump_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:
*/ |
C/C++ | wireshark/wiretap/pppdump.h | /** @file
*
* Copyright (c) 2000 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#ifndef __PPPDUMP_H__
#define __PPPDUMP_H__
#include <glib.h>
#include "wtap.h"
#include "ws_symbol_export.h"
wtap_open_return_val pppdump_open(wtap *wth, int *err, gchar **err_info);
#endif |
C | wireshark/wiretap/radcom.c | /* radcom.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "radcom.h"
struct frame_date {
guint16 year;
guint8 month;
guint8 day;
guint32 sec; /* seconds since midnight */
guint32 usec;
};
struct unaligned_frame_date {
char year[2];
char month;
char day;
char sec[4]; /* seconds since midnight */
char usec[4];
};
/* Found at the beginning of the file. Bytes 2 and 3 (D2:00) seem to be
* different in some captures */
static const guint8 radcom_magic[8] = {
0x42, 0xD2, 0x00, 0x34, 0x12, 0x66, 0x22, 0x88
};
static const guint8 encap_magic[4] = {
0x00, 0x42, 0x43, 0x09
};
static const guint8 active_time_magic[11] = {
'A', 'c', 't', 'i', 'v', 'e', ' ', 'T', 'i', 'm', 'e'
};
/* RADCOM record header - followed by frame data (perhaps including FCS).
"data_length" appears to be the length of packet data following
the record header. It's 0 in the last record.
"length" appears to be the amount of captured packet data, and
"real_length" might be the actual length of the frame on the wire -
in some captures, it's the same as "length", and, in others,
it's greater than "length". In the last record, however, those
may have bogus values (or is that some kind of trailer record?).
"xxx" appears to be all-zero in all but the last record in one
capture; if so, perhaps this indicates that the last record is,
in fact, a trailer of some sort, and some field in the header
is a record type. */
struct radcomrec_hdr {
char xxx[4]; /* unknown */
char data_length[2]; /* packet length? */
char xxy[5]; /* unknown */
struct unaligned_frame_date date; /* date/time stamp of packet */
char real_length[2]; /* actual length of packet */
char length[2]; /* captured length of packet */
char xxz[2]; /* unknown */
char dce; /* DCE/DTE flag (and other flags?) */
char xxw[9]; /* unknown */
};
static gboolean radcom_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset);
static gboolean radcom_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
static gboolean radcom_read_rec(wtap *wth, FILE_T fh, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info);
static int radcom_file_type_subtype = -1;
void register_radcom(void);
wtap_open_return_val radcom_open(wtap *wth, int *err, gchar **err_info)
{
guint8 r_magic[8], t_magic[11], search_encap[7];
struct frame_date start_date;
#if 0
guint32 sec;
struct tm tm;
#endif
/* Read in the string that should be at the start of a RADCOM file */
if (!wtap_read_bytes(wth->fh, r_magic, 8, err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
/* XXX: bytes 2 and 3 of the "magic" header seem to be different in some
* captures. We force them to our standard value so that the test
* succeeds (until we find if they have a special meaning, perhaps a
* version number ?) */
r_magic[1] = 0xD2;
r_magic[2] = 0x00;
if (memcmp(r_magic, radcom_magic, 8) != 0) {
return WTAP_OPEN_NOT_MINE;
}
/* Look for the "Active Time" string. The "frame_date" structure should
* be located 32 bytes before the beginning of this string */
if (!wtap_read_bytes(wth->fh, t_magic, 11, err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
while (memcmp(t_magic, active_time_magic, 11) != 0)
{
if (file_seek(wth->fh, -10, SEEK_CUR, err) == -1)
return WTAP_OPEN_ERROR;
if (!wtap_read_bytes(wth->fh, t_magic, 11, err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
}
if (file_seek(wth->fh, -43, SEEK_CUR, err) == -1)
return WTAP_OPEN_ERROR;
/* Get capture start time */
if (!wtap_read_bytes(wth->fh, &start_date, sizeof(struct frame_date),
err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
/* So what time is this? */
if (!wtap_read_bytes(wth->fh, NULL, sizeof(struct frame_date),
err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
for (;;) {
if (!wtap_read_bytes(wth->fh, search_encap, 4,
err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
if (memcmp(encap_magic, search_encap, 4) == 0)
break;
/*
* OK, that's not it, go forward 1 byte - reading
* the magic moved us forward 4 bytes, so seeking
* backward 3 bytes moves forward 1 byte - and
* try the 4 bytes at that offset.
*/
if (file_seek(wth->fh, -3, SEEK_CUR, err) == -1)
return WTAP_OPEN_ERROR;
}
if (!wtap_read_bytes(wth->fh, NULL, 12, err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
if (!wtap_read_bytes(wth->fh, search_encap, 4, err, err_info)) {
if (*err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
/* This is a radcom file */
wth->file_type_subtype = radcom_file_type_subtype;
wth->subtype_read = radcom_read;
wth->subtype_seek_read = radcom_seek_read;
wth->snapshot_length = 0; /* not available in header, only in frame */
wth->file_tsprec = WTAP_TSPREC_USEC;
#if 0
tm.tm_year = pletoh16(&start_date.year)-1900;
tm.tm_mon = start_date.month-1;
tm.tm_mday = start_date.day;
sec = pletoh32(&start_date.sec);
tm.tm_hour = sec/3600;
tm.tm_min = (sec%3600)/60;
tm.tm_sec = sec%60;
tm.tm_isdst = -1;
#endif
if (memcmp(search_encap, "LAPB", 4) == 0)
wth->file_encap = WTAP_ENCAP_LAPB;
else if (memcmp(search_encap, "Ethe", 4) == 0)
wth->file_encap = WTAP_ENCAP_ETHERNET;
else if (memcmp(search_encap, "ATM/", 4) == 0)
wth->file_encap = WTAP_ENCAP_ATM_RFC1483;
else {
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("radcom: network type \"%.4s\" unknown", search_encap);
return WTAP_OPEN_ERROR;
}
#if 0
if (!wtap_read_bytes(wth->fh, &next_date, sizeof(struct frame_date),
err, err_info))
return WTAP_OPEN_ERROR;
while (memcmp(&start_date, &next_date, 4)) {
if (file_seek(wth->fh, 1-sizeof(struct frame_date), SEEK_CUR, err) == -1)
return WTAP_OPEN_ERROR;
if (!wtap_read_bytes(wth->fh, &next_date, sizeof(struct frame_date),
err, err_info))
return WTAP_OPEN_ERROR;
}
#endif
if (wth->file_encap == WTAP_ENCAP_ETHERNET) {
if (!wtap_read_bytes(wth->fh, NULL, 294, err, err_info))
return WTAP_OPEN_ERROR;
} else if (wth->file_encap == WTAP_ENCAP_LAPB) {
if (!wtap_read_bytes(wth->fh, NULL, 297, err, err_info))
return WTAP_OPEN_ERROR;
} else if (wth->file_encap == WTAP_ENCAP_ATM_RFC1483) {
if (!wtap_read_bytes(wth->fh, NULL, 504, err, err_info))
return WTAP_OPEN_ERROR;
}
/*
* Add an IDB; we don't know how many interfaces were involved,
* so we just say one interface, about which we only know
* the link-layer type, snapshot length, and time stamp
* resolution.
*/
wtap_add_generated_idb(wth);
return WTAP_OPEN_MINE;
}
/* Read the next packet */
static gboolean radcom_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset)
{
char fcs[2];
*data_offset = file_tell(wth->fh);
/* Read record. */
if (!radcom_read_rec(wth, wth->fh, rec, buf, err, err_info)) {
/* Read error or EOF */
return FALSE;
}
if (wth->file_encap == WTAP_ENCAP_LAPB) {
/* Read the FCS.
XXX - should we have some way of indicating the
presence and size of an FCS to our caller?
That'd let us handle other file types as well. */
if (!wtap_read_bytes(wth->fh, &fcs, sizeof fcs, err, err_info))
return FALSE;
}
return TRUE;
}
static gboolean
radcom_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info)
{
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return FALSE;
/* Read record. */
if (!radcom_read_rec(wth, wth->random_fh, rec, buf, err,
err_info)) {
/* Read error or EOF */
if (*err == 0) {
/* EOF means "short read" in random-access mode */
*err = WTAP_ERR_SHORT_READ;
}
return FALSE;
}
return TRUE;
}
static gboolean
radcom_read_rec(wtap *wth, FILE_T fh, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info)
{
struct radcomrec_hdr hdr;
guint16 data_length, real_length, length;
guint32 sec;
struct tm tm;
guint8 atmhdr[8];
if (!wtap_read_bytes_or_eof(fh, &hdr, sizeof hdr, err, err_info))
return FALSE;
data_length = pletoh16(&hdr.data_length);
if (data_length == 0) {
/*
* The last record appears to have 0 in its "data_length"
* field, but non-zero values in other fields, so we
* check for that and treat it as an EOF indication.
*/
*err = 0;
return FALSE;
}
length = pletoh16(&hdr.length);
real_length = pletoh16(&hdr.real_length);
/*
* The maximum value of length is 65535, which is less than
* WTAP_MAX_PACKET_SIZE_STANDARD will ever be, so we don't need to check
* it.
*/
rec->rec_type = REC_TYPE_PACKET;
rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
tm.tm_year = pletoh16(&hdr.date.year)-1900;
tm.tm_mon = (hdr.date.month&0x0f)-1;
tm.tm_mday = hdr.date.day;
sec = pletoh32(&hdr.date.sec);
tm.tm_hour = sec/3600;
tm.tm_min = (sec%3600)/60;
tm.tm_sec = sec%60;
tm.tm_isdst = -1;
rec->ts.secs = mktime(&tm);
rec->ts.nsecs = pletoh32(&hdr.date.usec) * 1000;
switch (wth->file_encap) {
case WTAP_ENCAP_ETHERNET:
/* XXX - is there an FCS? */
rec->rec_header.packet_header.pseudo_header.eth.fcs_len = -1;
break;
case WTAP_ENCAP_LAPB:
rec->rec_header.packet_header.pseudo_header.dte_dce.flags = (hdr.dce & 0x1) ?
0x00 : FROM_DCE;
length -= 2; /* FCS */
real_length -= 2;
break;
case WTAP_ENCAP_ATM_RFC1483:
/*
* XXX - is this stuff a pseudo-header?
* The direction appears to be in the "hdr.dce" field.
*/
if (!wtap_read_bytes(fh, atmhdr, sizeof atmhdr, err,
err_info))
return FALSE; /* Read error */
length -= 8;
real_length -= 8;
break;
}
rec->rec_header.packet_header.len = real_length;
rec->rec_header.packet_header.caplen = length;
/*
* Read the packet data.
*/
if (!wtap_read_packet_bytes(fh, buf, length, err, err_info))
return FALSE; /* Read error */
return TRUE;
}
static const struct supported_block_type radcom_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 radcom_info = {
"RADCOM WAN/LAN analyzer", "radcom", NULL, NULL,
FALSE, BLOCKS_SUPPORTED(radcom_blocks_supported),
NULL, NULL, NULL
};
void register_radcom(void)
{
radcom_file_type_subtype = wtap_register_file_type_subtype(&radcom_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("RADCOM",
radcom_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:
*/ |
C/C++ | wireshark/wiretap/radcom.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#ifndef __RADCOM_H__
#define __RADCOM_H__
#include <glib.h>
#include "wtap.h"
#include "ws_symbol_export.h"
wtap_open_return_val radcom_open(wtap *wth, int *err, gchar **err_info);
#endif |
wireshark/wiretap/README | NOTE: this documents the original intent behind libwiretap. Currently,
it is being developed solely as a library for reading capture files,
rather than packet capture. The list of file formats is also
out-of-date.
Wiretap is a library that is being developed as a future replacement for
libpcap, the current standard Unix library for packet capturing. Libpcap
is great in that it is very platform independent and has a wonderful
BPF optimizing engine. But it has some shortcomings as well. These
shortcomings came to a head during the development of Wireshark
(https://www.wireshark.org/), a packet analyzer. As such, I began developing
wiretap so that:
1. The library can easily be amended with new packet filtering objects.
Libpcap is very TCP/IP-oriented. I want to filter on IPX objects, SNA objects,
etc. I also want any decent programmer to be able to add new filters to the
library.
2. The library can read file formats from many packet-capturing utilities.
Libpcap only reads Libpcap files.
3. The library can capture on more than one network interface at a time, and
save this trace in one file.
4. Network names can be resolved immediately after a trace and saved in the
trace file. That way, I can ship a trace of my firewall-protected network to a
colleague, and he'll see the proper hostnames for the IP addresses in the
packet capture, even though he doesn't have access to the DNS server behind my
LAN's firewall.
5. I want to look into the possibility of compressing packet data when saved
to a file, like Sniffer.
6. The packet-filter can be optimized for the host OS. Not all OSes have BPF;
SunOS has NIT and Solaris has DLPI, which both use the CMU/Stanford
packet-filter pseudomachine. RMON has another type of packet-filter syntax
which we could support.
Wiretap is very good at reading many file formats, as per #2
above. Wiretap has no filter capability at present; it currently doesn't
support packet capture, so it wouldn't be useful there, and filtering
when reading a capture file is done by Wireshark, using a more powerful
filtering mechanism than that provided by BPF.
File Formats
============
Libpcap
-------
The "libpcap" file format was determined by reading the "libpcap" code;
wiretap reads the "libpcap" file format with its own code, rather than
using the "libpcap" library's code to read it.
Sniffer (compressed and uncompressed)
-------
The uncompressed Sniffer format is documented in the Sniffer manual.
Unfortunately, Sniffer manuals tend to document only the format for
the Sniffer model they document. Token-Ring and ethernet seems to work
well, though. If you have an ATM Sniffer file, both Guy and Gilbert
would be *very* interested in receiving a sample. (see 'AUTHORS' file
for our e-mail addresses).
LANalyzer
---------
The LANalyzer format is available from http://www.novell.com. Search
their knowledge base for "Trace File Format".
Network Monitor
---------------
Microsoft's Network Monitor file format is supported, at least under
Ethernet and token-ring. If you have capture files of other datalink
types, please send them to Guy.
"snoop"
-------
The Solaris 2.x "snoop" program's format is documented in RFC 1761.
"iptrace"
---------
This is the capture program that comes with AIX 3.x and 4.x. AIX 3 uses
the iptrace 1.0 file format, while AIX4 uses iptrace 2.0. iptrace has
an undocumented, yet very simple, file format. The interesting thing
about iptrace is that it will record packets coming in from all network
interfaces; a single iptrace file can contain multiple datalink types.
Sniffer Basic (NetXRay)/Windows Sniffer Pro
-------------------------------------------
Network Associates' Sniffer Basic (formerly NetXRay from Cinco Networks)
file format is now supported, at least for Ethernet and token-ring.
Network Associates' Windows Sniffer Pro appears to use a variant of that
format; it's supported to the same extent.
RADCOM WAN/LAN Analyzers
------------------------
Olivier Abad has added code to read Ethernet and LAPB captures from
RADCOM WAN/LAN Analyzers (see https://web.archive.org/web/20031231213434/http://www.radcom-inc.com/).
Lucent/Ascend access products
-----------------------------
Gerald
HP-UX nettl
-----------
nettl is used on HP-UX to trace various streams based subsystems. Wiretap
can read nettl files containing IP frames (NS_LS_IP subsystem) and LAPB
frames (SX25L2 subsystem). It has been tested with files generated on
HP-UX 9.04 and 10.20.
Use the following commands to generate a trace :
# IP capture. 0x30000000 means PDU in and PDU out :
nettl -tn 0x30000000 -e NS_LS_IP -f tracefile
# X25 capture. You must specify an interface :
nettl -tn 0x30000000 -e SX25l2 -d /dev/x25_0 -f tracefile
# stop capture. subsystem is NS_LS_IP or SX25L2 :
nettl -tf -e subsystem
One may be able to specify "-tn pduin pduout" rather than
"-tn 0x30000000"; the nettl man page for HP-UX 10.30 implies that it
should work.
There is also basic support for nettl files containing NS_LS_DRIVER,
NS_LS_TCP, NS_LS_UDP, NS_LS_LOOPBACK, unknown type 0xb9, and NS_LS_ICMP.
However, NS_LS_ICMP will not be decoded since WTAP lacks a raw ICMP
encapsulation type.
Toshiba ISDN Router
-------------------
An under-documented command that the router supports in a telnet session
is "snoop" (not related to the Solaris "snoop" command). If you give
it the "dump" option (either by letting "snoop" query you for its next
argument, or typing "snoop dump" on the command line), you'll get a hex
dump of all packets across the router (except of your own telnet session
-- good thinking Toshiba!). You can select a certain channel to sniff
(LAN, B1, B2, D), but the default is all channels. You save this hex
dump to disk with 'script' or by 'telnet | tee'. Wiretap will read the
ASCII hex dump and convert it to binary data.
ISDN4BSD "i4btrace" utility
---------------------------
Bert Driehuis
Cisco Secure Intrusion Detection System iplogging facility
-----------------------------------------------------------
Mike Hall
pppd logs (pppdump-format files)
--------------------------------
Gilbert
VMS TCPTRACE
------------
Compaq VMS's TCPIPTRACE format is supported. This is the capture program
that comes with TCP/IP or UCX as supplied by Compaq or Digital Equipment
Corporation.
Under UCX 4.x, it is invoked as TCPIPTRACE. Under TCPIP 5.x, it is invoked
as TCPTRACE.
TCPTRACE produces an ascii text based format, that has changed slightly over
time.
DBS Etherwatch (text format)
----------------------------
Text output from DBS Etherwatch is supported. DBS Etherwatch is available
from: https://web.archive.org/web/20070612033348/http://www.users.bigpond.com/dbsneddon/software.htm.
Catapult DCT2000 (.out files)
-----------------------------
DCT2000 test systems produce ascii text-based .out files for ports
that have logging enabled. When being read, the data part of the message is
prefixed with a short header that provides some context (context+port,
direction, original timestamp, etc).
You can choose to suppress the reading of non-standard protocols
(i.e. messages between layers rather than the well-known link-level protocols
usually found on board ports).
Gilbert Ramirez <[email protected]>
Guy Harris <[email protected]>
STANAG 4607
-----------
Initial support for the STANAG 4607 protocol. Documentation at:
https://web.archive.org/web/20130223054955/http://www.nato.int/structur/AC/224/standard/4607/4607.htm |
|
wireshark/wiretap/README.airmagnet | AMC: Wireless Analyzer Captured Data (AirMagnet)
------------------------------------------------
This is just a braindump from looking at some Airmagnet capture files,
in one case having a look at a decoded display in Airmagnet itself.
Lots of things are still unknown.
This is NOT the intention to write a file importer in the foreseeable
future.
Exact timestamp decoding still unknown:
From a different decoded example file:
06 51 49 1b Timestamp (105990427) = 03:30:27.497544
06 51 4a cd Timestamp (105990861) = 03:30:27.497978 (+434)
06 51 4b ce Timestamp (105991118) = 03:30:27.498235 (+257)
Timestamps this file:
Frame1: 15a5 2fb4 363147188 -
Frame2: 15a5 3a8e 363149966 + 1778
Frame3: 15a5 3c50 363150416 + 550
Frame4: 15a5 487d 363153533
Frame5: 15a5 49d9 363153881
Frame6: 15a5 4bcf 363154383
Frame7: 15a5 53da 363156442
Frame8: 15a5 59e4 363157988
Frame9: 15cc 9da8 365731240
FrameA: 15db dd60 366730592
FrameB: 15dc 04f6 366740726
FrameC: 15df 5d29 366959913
Unknown stuff:
Header: 0000 0000 0002 | 3b93 d886
Frame1: da9d | 0000 0000 0002
Frame2: bf9c | 0000 0000 0002
Frame3: d59c | 0000 0000 0002
Frame4: c09c | 0000 0000 0002
Frame5: c09c | 0000 0000 0002
Frame6: d69c | 0000 0000 0002
Frame7: d79c | 0000 0000 0002
Frame8: c09c | 0000 0000 0002
Frame9: d79c | 0000 0000 0002
FrameA: d89c | 0000 0000 0002
FrameB: d69d | 0000 0000 0002
FrameC: d79c | 0000 0000 0002
Headerstructure:
6 Bytes: 'N2GINC'
44 Bytes: unknown (0x0)
6 Bytes: 'N2GINC'
2 Bytes: unknown (0x0)
2 Bytes: #Frames (BE)
2 Bytes: unknown (0x0)
2 Bytes: unknown (0x0002)
4 Bytes: Timestamp
4 Bytes: unknown (Timestamp date part ?)
28 Bytes: unknown (0x0)
==================
Total: 100 Bytes
Recordstructure:
2 Bytes: unknown
2 Bytes: Bytes "on wire" (BE)
2 Bytes: LL Bytes in capturefile (BE)
4 Bytes: unknown (0x0) (02 00 00 00 = Frame displayed in RED)
2 Bytes: unknown (0x0002)
4 Bytes: Timestamp
1 Byte: DataRate ((AND 7f) / 2 in Mbit/s)
1 Byte: Channel (1-13: Chan 1-13, 15: Chan 36, 16: Chan 40, ..., 19: Chan 52, ...)
1 Byte: SignalStrength (%)
1 Byte: NoiseLevel (%)
LL Bytes: Capturedata
6 Bytes: unknown
4 Bytes: 0x33333333
(1 Byte: 0x33) in case LL is an odd number
Filelength: 0x57e Bytes
12 Frames
============== Header ===================
0000000: 4e32 4749 4e43 0000 0000 0000 0000 0000
0000010: 0000 0000 0000 0000 0000 0000 0000 0000
0000020: 0000 0000 0000 0000 0000 0000 0000 0000
0000030: 0000 4e32 4749 4e43 0000 000c 0000 0002
0000040: 1545 5198 3b93 d886 0000 0000 0000 0000
0000050: 0000 0000 0000 0000 0000 0000 0000 0000
0000060: 0000 0000
============== Frame 1 ==================
Length: 0x36
-------- ???
0000064: da9d
0000066: 0026 0026
000006a: 0000 0000 0002
0000070: 15a5 2fb4 Timestamp
0000074: 02 DataRate
0000075: 06 Channel
0000076: 64 SignalStrength (%)
0000077: 01 NoiseLevel (%)
-------- ieee 802.11
0000078: b000 Type/Subtype/Frame Control
000007a: 3a01 Duration
000007c: 0040 9647 71a3 Destaddr
0000082: 0040 9631 d395 Sourceaddr
0000088: 0040 9647 71a3 BSS Id
000008e: b000 Fragment nr/Seq nr
-------- ???
0000090: 0000 0100 0000 3333 3333
============== Frame 2===================
Length: 0x36
-------- ???
000009a: bf9c 0026 0026
00000a0: 0000 0000 0002 15a5 3a8e 1606 3c00
-------- ieee 802.11
00000ae: b000 Type/Subtype/Frame Control
00000b0: 7500
00000b2: 0040 9631 d395
00000b8: 0040 9647 71a3
00000be: 0040 9647 71a3
00000c4: 806d
-------- ???
00000c6: 0000 0200 0000 3333 3333
============== Frame 3 ==================
Length: 0x62
-------- ???
00000d0: d59c 0051 0051
00000d6: 0000 0000 0002 15a5 3c50 0206 6400
-------- ieee 802.11
00000e4: 0000
00000e6: 3a01
00000e8: 0040 9647 71a3
00000ee: 0040 9631 d395
00000f4: 0040 9647 71a3
00000fa: c000
-------- ???
00000fc: 2100 c800
0000100: 0005 616c 616d 6f01 0402 040b 1685 1e00
0000110: 016d 0d1f 00ff 0318 0049 6e73 7472 7563
0000120: 746f 7200 0000 00
-------- ???
0000127: 00 0000 0000 2133 3333 3333
================= Frame 4 ===============
Length: 0x68
-------- ???
0000132: c09c 0058 0058
0000138: 0000 0000 0002 15a5 487d 1606 3e00
-------- ieee 802.11
000014a: 1000
000014c: 7500
000014e: 0040 9631 d395
0000154: 0040 9647 71a3
000015a: 0040 9647 71a3
000015c: 906d
-------- ???
000015e: 2100
0000160: 0000 1d00 0104 8284 8b96 851c 0000 4c0d
0000170: 0000 0000 0100 416c 616d 6f20 436c 6173
0000180: 7372 6f6f 6d20 0002 880c 80f3 0300 8137
-------- ???
0000190: 0300 0000 0000 3333 3333
=================== Frame 5 =============
-------- ???
000019a: c09c 005a 005a
00001a0: 0000 0000 0002 15a5 49d9 1606 3e00
-------- ieee 802.11
00001ae: 0802
00001b0: 7500 0040 9631 d395 0040 9647 71a3 0040
00001c0: 9647 71a3 a06d aaaa 0300 4096 0000 0032
00001d0: 4001 0040 9631 d395 0040 9647 71a3 0100
00001e0: 0000 0000 0000 0000 0000 0000 0000 0000
00001f0: 0000 0000 0000 0000 0000
-------- ???
00001fa: 0000 0000 0000 3333 3333
=================== Frame 6 =============
-------- ???
0000204: d69c 0072 0072
000020a: 0000 0000 0002 15a5 4bcf 0206 6400
-------- ieee 802.11
0000218: 0801 3a01 0040 9647
0000220: 71a3 0040 9631 d395 0040 9647 71a3 d000
0000230: aaaa 0300 4096 0000 004a 4081 0040 9647
0000240: 71a3 0040 9631 d395 016d 0004 1900 0040
0000250: 9647 71a3 0000 0000 0000 0000 0000 0000
0000260: 0000 001e 496e 7374 7275 6374 6f72 0000
0000270: 0000 0000 0000 0000 0000 0000
-------- ???
000027c: 0000 0000 0000 3333 3333
=================== Frame 7 =============
-------- ???
0000286: d79c 0039 0039
000028c: 0000 0000 0002 15a5 53da 0206 6400
-------- ieee 802.11
000029a: 0801 3a01 0040
00002a0: 9647 71a3 0040 9631 d395 0040 9647 71a3
00002b0: e000 aaaa 0300 4096 0000 0011 4001 0040
00002c0: 9647 71a3 00
00002c5: 40 9631 d395 0133 3333 3333
====================== Frame 8 ==========
-------- ???
00002d0: c09c 0072 0072
00002d6: 0000 0000 0002 15a5 59e4 1606 3e00
-------- ieee 802.11
00002e4: 0802 7500 0040 9631 d395 0040
00002f0: 9647 71a3 0040 9647 71a3 b06d aaaa 0300
0000300: 4096 0000 004a 4081 0040 9631 d395 0040
0000310: 9647 71a3 014c 030b 1500 0000 0000 0000
0000320: 0000 0000 0000 0002 0000 0a00 0001 0000
0000330: 416c 616d 6f20 436c 6173 7372 6f6f 6d00
0000340: 0000 0000 0000 0000
0000348: 0000 0000 2200 3333 3333
=================== Frame 9 =============
-------- ???
0000352: d79c 0170 00a0
0000358: 0020 0000 0002 15cc 9da8 1606 6400
-------- ieee 802.11
0000366: 0801 7500 0040 9647 71a3
0000370: 0040 9631 d395 ffff ffff ffff f000 aaaa
0000380: 0300 0000 0800 4500 0148 42a1 0000 8011
0000390: f704 0000 0000 ffff ffff 0044 0043 0134
00003a0: d991 0101 0600 7728 b62a 0000 0000 0000
00003b0: 0000 0000 0000 0000 0000 0000 0000 0040
00003c0: 9631 d395 0000 0000 0000 0000 0000 0000
00003d0: 0000 0000 0000 0000 0000 0000 0000 0000
00003e0: 0000 0000 0000 0000 0000 0000 0000 0000
00003f0: 0000 0000 0000 0000
-------- ???
00003f8: 0000 0000 0000 3333 3333
=================== Frame A =============
-------- ???
0000402: d89c 0182 00a0
0000408: 0020 0000 0002 15db dd60 1606 6400
-------- ieee 802.11
0000416: 0801 7500
000041a: 0040 9647 71a3
0000420: 0040 9631 d395
0000426: ffff ffff ffff
000042c: 0001
-------- LLC
000042e: aaaa 0300 0000 0800
-------- IP
0000436: 4500 015a 42a3 0000 8011 f6f0 0000 0000 ffff ffff
-------- UDP
000044a: 0044 0043 0146 57bc
-------- DHCP
0000452: 0101 0600 7728 b62a 0000 0000 0000
0000460: 0000 0000 0000 0000 0000 0000 0000 0040
0000470: 9631 d395 0000 0000 0000 0000 0000 0000
0000480: 0000 0000 0000 0000 0000 0000 0000 0000
0000490: 0000 0000 0000 0000 0000 0000 0000 0000
00004a0: 0000 0000 0000 0000
-------- ???
00004a8: 0000 0000 0000 3333 3333
=================== Frame B =============
-------- ???
00004b2: d69d 0056 0056
00004b8: 0000 0000 0002 15dc 04f6 1606 6401
-------- ieee 802.11
00004c6: 0801
00004c8: 7500
00004ca: 0040 9647 71a3
00004d0: 0040 9631 d395
00004d6: ffff ffff ffff
0000edc: 1001
-------- LLC
00004de: aaaa 0300 0000 0806
-------- ARP
00004e6: 0001 0800 0604 0001
00004ee: 0040 9631 d395 0a00 0065
00004f8: 0000 0000 0000 0a00 0065
-------- ???
0000502: 7b00 e097 7b00 e097 7b00 e097
000050e: 7b00 e097 7b00 3333 3333
=================== Frame C =============
-------- ???
0000518: d79c 0056 0056
000051e: 0000 0000 0002 15df 5d29 1606 6400
-------- ieee 802.11
000052c: 0801
000052e: 7500
0000530: 0040 9647 71a3
0000536: 0040 9631 d395
000053a: ffff ffff ffff
0000540: 2001
-------- LLC
0000542: aaaa 0300 0000 0806
-------- ARP
000054a: 0001 Hw
000054c: 0800 Protocol
0000550: 06 Hw-Size
0000551: 04 Protocolsize
0000552: 0001 Opcode
0000554: 0040 9631 d395 Sender MAC
000055a: 0a00 0065 Sender IP
000055e: 0000 0000 0000 Destination MAC
0000564: 0a00 0065 Destination IP
-------- ???
0000568: 0000 8602 0000 ffff ffff 0cc3
0000574: 4b82 58a1 1d82 3333 3333
=========================================
000057e: EOF |
|
wireshark/wiretap/README.developer | This is a very quick and very dirty guide to adding support for new
capture file formats. If you see any errors or have any improvements,
submit patches - free software is a community effort....
To add the ability to read a new capture file format, you have to:
write an "open" routine that can read the beginning of the
capture file and figure out if it's in that format or not,
either by looking at a magic number at the beginning or by using
some form of heuristic to determine if it's a file of that type
(if the file format has a magic number, that's what should be
used);
write a "read" routine that can read a packet from the file and
supply the packet length, captured data length, time stamp, and
packet pseudo-header (if any) and data, and have the "open"
routine set the "subtype_read" member of the "wtap" structure
supplied to it to point to that routine;
write a "seek and read" routine that can seek to a specified
location in the file for a packet and supply the packet
pseudo-header (if any) and data, and have the "open" routine set
the "subtype_seek_read" member of the "wtap" structure to point
to that routine;
write a "close" routine, if necessary (if, for example, the
"open" routine allocates any memory), and set the
"subtype_close" member of the "wtap" structure to point to it,
otherwise leave it set to NULL;
add an entry for that file type in the "open_info_base[]" table
in "wiretap/file_access.c", giving a pointer to the "open" routine,
a name to use in file open dialogs, whether the file type uses a
magic number or a heuristic, and if it uses a heuristic, file
extensions for which the heuristic should be tried first. More
discriminating and faster heuristics should be put before less
accurate and slower heuristics;
add a registration routine passing a "file_type_subtype_info" struct
to wtap_register_file_type_subtypes(), giving a descriptive name, a
short name that's convenient to type on a command line (no blanks or
capital letters, please), common file extensions to open and save,
any block types supported, and pointers to the "can_write_encap" and
"dump_open" routines if writing that file is supported (see below),
otherwise just null pointers.
Wiretap applications typically first perform sequential reads through
the capture file and may later do "seek and read" for individual frames.
The "read" routine should set the variable data_offset to the byte
offset within the capture file from which the "seek and read" routine
will read. If the capture records consist of:
capture record header
pseudo-header (e.g., for ATM)
frame data
then data_offset should point to the pseudo-header. The first
sequential read pass will process and store the capture record header
data, but it will not store the pseudo-header. Note that the
seek_and_read routine should work with the "random_fh" file handle
of the passed in wtap struct, instead of the "fh" file handle used
in the normal read routine.
To add the ability to write a new capture file format, you have to:
add a "can_write_encap" routine that returns an indication of
whether a given packet encapsulation format is supported by the
new capture file format;
add a "dump_open" routine that starts writing a file (writing
headers, allocating data structures, etc.);
add a "dump" routine to write a packet to a file, and have the
"dump_open" routine set the "subtype_write" member of the
"wtap_dumper" structure passed to it to point to it;
add a "dump_close" routine, if necessary (if, for example, the
"dump_open" routine allocates any memory, or if some of the file
header can be written only after all the packets have been
written), and have the "dump_open" routine set the
"subtype_close" member of the "wtap_dumper" structure to point
to it;
put pointers to the "can_write_encap" and "dump_open" routines
in the "file_type_subtype_info" struct passed to
wtap_register_file_type_subtypes().
In the wiretap directory, add your source file to CMakelists.txt. |
|
C/C++ | wireshark/wiretap/required_file_handlers.h | /** @file
*
* Functions and variables defined by required file handlers (pcap,
* nanosecond pcap, pcapng).
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __REQUIRED_FILE_HANDLERS_H__
#define __REQUIRED_FILE_HANDLERS_H__
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* These are for use within libwiretap only; they are not exported.
*/
extern void register_pcap(void);
extern void register_pcapng(void);
extern int pcap_file_type_subtype; /* regular pcap */
extern int pcap_nsec_file_type_subtype; /* pcap with nanosecond resolution */
extern int pcapng_file_type_subtype; /* pcapng */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __REQUIRED_FILE_HANDLERS_H__ */ |
C | wireshark/wiretap/rfc7468.c | /* rfc7468.c
*
* Implements loading of files in the format specified by RFC 7468.
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "rfc7468.h"
#include "file_wrappers.h"
#include "wtap-int.h"
#include <wsutil/buffer.h>
#include <glib.h>
#include <string.h>
static int rfc7468_file_type_subtype = -1;
void register_rfc7468(void);
enum line_type {
LINE_TYPE_PREEB,
LINE_TYPE_POSTEB,
LINE_TYPE_OTHER,
};
const char PREEB_BEGIN[] = "-----BEGIN ";
#define PREEB_BEGIN_LEN (sizeof PREEB_BEGIN - 1)
const char POSTEB_BEGIN[] = "-----END ";
#define POSTEB_BEGIN_LEN (sizeof POSTEB_BEGIN - 1)
static gboolean rfc7468_read_line(FILE_T fh, enum line_type *line_type, Buffer *buf,
int* err, gchar** err_info)
{
/* Make the chunk size large enough that most lines can fit in a single chunk.
Strict RFC 7468 syntax only allows up to 64 characters per line, but we provide
some leeway to accommodate nonconformant producers and explanatory text.
The 3 extra bytes are for the trailing CR+LF and NUL terminator. */
char line_chunk[128 + 3];
char *line_chunk_end;
if (!(line_chunk_end = file_getsp(line_chunk, sizeof line_chunk, fh))) {
*err = file_error(fh, err_info);
return FALSE;
}
// First chunk determines the line type.
if (memcmp(line_chunk, PREEB_BEGIN, PREEB_BEGIN_LEN) == 0)
*line_type = LINE_TYPE_PREEB;
else if (memcmp(line_chunk, POSTEB_BEGIN, POSTEB_BEGIN_LEN) == 0)
*line_type = LINE_TYPE_POSTEB;
else
*line_type = LINE_TYPE_OTHER;
for (;;) {
gsize line_chunk_len = line_chunk_end - line_chunk;
if (line_chunk_len > G_MAXINT - ws_buffer_length(buf)) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup_printf(
"File contains an encoding larger than the maximum of %d bytes",
G_MAXINT);
return FALSE;
}
ws_buffer_append(buf, line_chunk, line_chunk_len);
if (line_chunk_end[-1] == '\n' || file_eof(fh))
break;
if (!(line_chunk_end = file_getsp(line_chunk, sizeof line_chunk, fh))) {
*err = file_error(fh, err_info);
return FALSE;
}
}
return TRUE;
}
static gboolean rfc7468_read_impl(FILE_T fh, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info)
{
ws_buffer_clean(buf);
gboolean saw_preeb = FALSE;
for (;;) {
enum line_type line_type;
if (!rfc7468_read_line(fh, &line_type, buf, err, err_info)) {
if (*err != 0 || !saw_preeb) return FALSE;
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("Missing post-encapsulation boundary at end of file");
return FALSE;
}
if (saw_preeb) {
if (line_type == LINE_TYPE_POSTEB) break;
} else {
if (line_type == LINE_TYPE_PREEB) saw_preeb = TRUE;
}
}
rec->rec_type = REC_TYPE_PACKET;
rec->presence_flags = 0;
rec->ts.secs = 0;
rec->ts.nsecs = 0;
rec->rec_header.packet_header.caplen = (guint32)ws_buffer_length(buf);
rec->rec_header.packet_header.len = (guint32)ws_buffer_length(buf);
return TRUE;
}
static gboolean rfc7468_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset)
{
*data_offset = file_tell(wth->fh);
return rfc7468_read_impl(wth->fh, rec, buf, err, err_info);
}
static gboolean rfc7468_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info)
{
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) < 0)
return FALSE;
return rfc7468_read_impl(wth->random_fh, rec, buf, err, err_info);
}
wtap_open_return_val rfc7468_open(wtap *wth, int *err, gchar **err_info)
{
/* To detect whether this file matches our format, we need to find the
first pre-encapsulation boundary, which may be located anywhere in the file,
since it may be preceded by explanatory text. However, we don't want to
read the entire file to find it, since the file may be huge, and detection
needs to be fast. Therefore, we'll assume that if the boundary exists,
it's located within a small initial chunk of the file. The size of
the chunk was chosen arbitrarily. */
char initial_chunk[2048];
int initial_chunk_size = file_read(&initial_chunk, sizeof initial_chunk, wth->fh);
if (initial_chunk_size < 0) {
*err = file_error(wth->fh, err_info);
return WTAP_OPEN_ERROR;
}
char *chunk_end_ptr = initial_chunk + initial_chunk_size;
// Try to find a line that starts with PREEB_BEGIN in the initial chunk.
for (char *line_ptr = initial_chunk; ; ) {
if ((unsigned)(chunk_end_ptr - line_ptr) < PREEB_BEGIN_LEN)
return WTAP_OPEN_NOT_MINE;
if (memcmp(line_ptr, PREEB_BEGIN, PREEB_BEGIN_LEN) == 0)
break;
// Try next line.
char *lf_ptr = memchr(line_ptr, '\n', chunk_end_ptr - line_ptr);
if (!lf_ptr)
return WTAP_OPEN_NOT_MINE;
line_ptr = lf_ptr + 1;
}
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
return WTAP_OPEN_ERROR;
wth->file_type_subtype = rfc7468_file_type_subtype;
wth->file_encap = WTAP_ENCAP_RFC7468;
wth->snapshot_length = 0;
wth->file_tsprec = WTAP_TSPREC_SEC;
wth->subtype_read = rfc7468_read;
wth->subtype_seek_read = rfc7468_seek_read;
return WTAP_OPEN_MINE;
}
static const struct supported_block_type rfc7468_blocks_supported[] = {
/*
* We provide one "packet" for each encoded structure in the file,
* and don't support any options.
*/
{ WTAP_BLOCK_PACKET, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED }
};
static const struct file_type_subtype_info rfc7468_info = {
"RFC 7468 files", "rfc7468", NULL, NULL,
FALSE, BLOCKS_SUPPORTED(rfc7468_blocks_supported),
NULL, NULL, NULL
};
void register_rfc7468(void)
{
rfc7468_file_type_subtype = wtap_register_file_type_subtype(&rfc7468_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("RFC7468",
rfc7468_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/rfc7468.h | /** @file
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __PEM_H__
#define __PEM_H__
#include <glib.h>
#include "wtap.h"
wtap_open_return_val rfc7468_open(wtap *wth, int *err, gchar **err_info);
#endif
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C | wireshark/wiretap/rtpdump.c | /* rtpdump.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* Support for RTPDump file format
* Copyright (c) 2023 by David Perry <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/* The rtpdump file format is the "dump" format as generated by rtpdump from
* <https://github.com/irtlab/rtptools>. It starts with an ASCII text header:
*
* #!rtpplay1.0 source_address/port\n
*
* followed by a binary header:
*
* typedef struct {
* struct timeval start; // start of recording (GMT)
* uint32_t source; // network source (multicast)
* uint16_t port; // UDP port
* } RD_hdr_t;
*
* Note that the SAME source address and port are included twice in
* this header, as seen here:
* <https://github.com/irtlab/rtptools/blob/9356740cb0c/rtpdump.c#L189>
*
* Wireshark can also generate rtpdump files, but it uses DIFFERENT addresses
* and ports in the text vs binary headers. See rtp_write_header() in
* ui/tap-rtp-common.c -- Wireshark puts the destination address and port
* in the text header, but the source address and port in the binary header.
*
* Wireshark may also generate the file with an IPv6 address in the text
* header, whereas rtpdump only supports IPv4 here. The binary header
* can't hold an IPv6 address without fully breaking compatibility with
* the rtptools project, so Wireshark truncates the address.
*
* Either way, each packet which follows is a RTP or RTCP packet of the form
*
* typedef struct {
* uint16_t length; // length of original packet, including header
* uint16_t plen; // actual header+payload length for RTP, 0 for RTCP
* uint32_t offset; // ms since the start of recording
* } RD_packet_t;
*
* followed by length bytes of packet data.
*/
#include "config.h"
#include <wtap-int.h>
#include <file_wrappers.h>
#include <wsutil/exported_pdu_tlvs.h>
#include <wsutil/inet_addr.h>
#include <wsutil/nstime.h>
#include <wsutil/strtoi.h>
#include <wsutil/wslog.h>
#include "rtpdump.h"
#include <string.h>
/* NB. I've included the version string in the magic for stronger identification.
* If we add/change version support, we'll also need to edit:
* - wiretap/mime_file.c
* - resources/freedesktop/org.wireshark.Wireshark-mime.xml
* - epan/dissectors/file-rtpdump.c
*/
#define RTP_MAGIC "#!rtpplay1.0 "
#define RTP_MAGIC_LEN 13
/* Reasonable maximum length for the RTP header (after the magic):
* - WS_INET6_ADDRSTRLEN characters for a IPv6 address
* - 1 for a slash
* - 5 characters for a port number
* - 1 character for a newline
* - 4 bytes for each of start seconds, start useconds, IPv4 address
* - 2 bytes for each of port, padding
* and 2 bytes of fudge factor, just in case.
*/
#define RTP_HEADER_MAX_LEN 25+WS_INET6_ADDRSTRLEN
/* Reasonable buffer size for holding the Exported PDU headers:
* - 8 bytes for the port type header
* - 8 bytes for one port
* - 4+EXP_PDU_TAG_IPV6_LEN for one IPv6 address
* (same space would hold 2 IPv4 addresses with room to spare)
*/
#define RTP_BUFFER_INIT_LEN 20+EXP_PDU_TAG_IPV6_LEN
static gboolean
rtpdump_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info,
gint64 *data_offset);
static gboolean
rtpdump_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info);
static void
rtpdump_close(wtap *wth);
static int rtpdump_file_type_subtype = -1;
typedef union ip_addr_u {
ws_in4_addr ipv4;
ws_in6_addr ipv6;
} ip_addr_t;
typedef struct rtpdump_priv_s {
Buffer epdu_headers;
nstime_t start_time;
} rtpdump_priv_t;
void register_rtpdump(void);
wtap_open_return_val
rtpdump_open(wtap *wth, int *err, char **err_info)
{
guint8 buf_magic[RTP_MAGIC_LEN];
char ch = '\0';
rtpdump_priv_t *priv = NULL;
ip_addr_t txt_addr;
ws_in4_addr bin_addr;
guint16 txt_port = 0;
guint16 bin_port = 0;
GString *header_str = NULL;
gboolean is_ipv6 = FALSE;
gboolean got_ip = FALSE;
nstime_t start_time = NSTIME_INIT_ZERO;
Buffer *buf;
if (!wtap_read_bytes(wth->fh, buf_magic, RTP_MAGIC_LEN, err, err_info)) {
return (*err == WTAP_ERR_SHORT_READ)
? WTAP_OPEN_NOT_MINE
: WTAP_OPEN_ERROR;
}
if (strncmp(buf_magic, RTP_MAGIC, RTP_MAGIC_LEN) != 0) {
return WTAP_OPEN_NOT_MINE;
}
/* Parse the text header for an IP and port. */
header_str = g_string_sized_new(RTP_HEADER_MAX_LEN);
do {
if (!wtap_read_bytes(wth->fh, &ch, 1, err, err_info)) {
g_string_free(header_str, TRUE);
return (*err == WTAP_ERR_SHORT_READ)
? WTAP_OPEN_NOT_MINE
: WTAP_OPEN_ERROR;
}
if (ch == '/') {
/* Everything up to now should be an IP address */
if (ws_inet_pton4(header_str->str, &txt_addr.ipv4)) {
is_ipv6 = FALSE;
}
else if (ws_inet_pton6(header_str->str, &txt_addr.ipv6)) {
is_ipv6 = TRUE;
}
else {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup("rtpdump: bad IP in header text");
g_string_free(header_str, TRUE);
return WTAP_OPEN_ERROR;
}
got_ip = TRUE;
g_string_truncate(header_str, 0);
}
else if (ch == '\n') {
if (!got_ip) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup("rtpdump: no IP in header text");
g_string_free(header_str, TRUE);
return WTAP_OPEN_ERROR;
}
if (!ws_strtou16(header_str->str, NULL, &txt_port)) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup("rtpdump: bad port in header text");
g_string_free(header_str, TRUE);
return WTAP_OPEN_ERROR;
}
break;
}
else if (g_ascii_isprint(ch)) {
g_string_append_c(header_str, ch);
}
else {
g_string_free(header_str, TRUE);
return WTAP_OPEN_NOT_MINE;
}
} while (ch != '\n');
g_string_free(header_str, TRUE);
if (!got_ip || txt_port == 0) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup("rtpdump: bad header text");
return WTAP_OPEN_ERROR;
}
/* Whether generated by rtpdump or Wireshark, the binary header
* has the source IP and port. If the text header has an IPv6 address,
* this address was likely truncated from an IPv6 address as well
* and is likely inaccurate, so we will ignore it.
*/
#define FAIL G_STMT_START { \
return (*err == WTAP_ERR_SHORT_READ) \
? WTAP_OPEN_NOT_MINE \
: WTAP_OPEN_ERROR; \
} G_STMT_END
if (!wtap_read_bytes(wth->fh, &start_time.secs, 4, err, err_info)) FAIL;
start_time.secs = g_ntohl(start_time.secs);
if (!wtap_read_bytes(wth->fh, &start_time.nsecs, 4, err, err_info)) FAIL;
start_time.nsecs = g_ntohl(start_time.nsecs) * 1000;
if (!wtap_read_bytes(wth->fh, &bin_addr, 4, err, err_info)) FAIL;
if (!wtap_read_bytes(wth->fh, &bin_port, 2, err, err_info)) FAIL;
bin_port = g_ntohs(bin_port);
/* Finally, padding */
if (!wtap_read_bytes(wth->fh, NULL, 2, err, err_info)) FAIL;
#undef FAIL
/* If we made it this far, we have all the info we need to generate
* most of the Exported PDU headers for every packet in this stream.
*/
priv = g_new0(rtpdump_priv_t, 1);
priv->start_time = start_time;
buf = &priv->epdu_headers; /* shorthand */
ws_buffer_init(buf, RTP_BUFFER_INIT_LEN);
wtap_buffer_append_epdu_uint(buf, EXP_PDU_TAG_PORT_TYPE, EXP_PDU_PT_UDP);
if (is_ipv6) {
/* File must be generated by Wireshark. Text address is IPv6 destination,
* binary address is invalid and ignored here.
*/
wtap_buffer_append_epdu_tag(buf, EXP_PDU_TAG_IPV6_DST, (const guint8 *)&txt_addr.ipv6, EXP_PDU_TAG_IPV6_LEN);
wtap_buffer_append_epdu_uint(buf, EXP_PDU_TAG_DST_PORT, txt_port);
}
else if (txt_addr.ipv4 == bin_addr && txt_port == bin_port) {
/* File must be generated by rtpdump. Both addresses are IPv4 source. */
wtap_buffer_append_epdu_tag(buf, EXP_PDU_TAG_IPV4_SRC, (const guint8 *)&bin_addr, EXP_PDU_TAG_IPV4_LEN);
wtap_buffer_append_epdu_uint(buf, EXP_PDU_TAG_SRC_PORT, bin_port);
}
else {
/* File must be generated by Wireshark. Text is IPv4 destination,
* binary is IPv4 source.
*/
wtap_buffer_append_epdu_tag(buf, EXP_PDU_TAG_IPV4_DST, (const guint8 *)&txt_addr.ipv4, EXP_PDU_TAG_IPV4_LEN);
wtap_buffer_append_epdu_uint(buf, EXP_PDU_TAG_DST_PORT, txt_port);
wtap_buffer_append_epdu_tag(buf, EXP_PDU_TAG_IPV4_SRC, (const guint8 *)&bin_addr, EXP_PDU_TAG_IPV4_LEN);
wtap_buffer_append_epdu_uint(buf, EXP_PDU_TAG_SRC_PORT, bin_port);
}
wth->priv = (void *)priv;
wth->subtype_close = rtpdump_close;
wth->subtype_read = rtpdump_read;
wth->subtype_seek_read = rtpdump_seek_read;
wth->file_type_subtype = rtpdump_file_type_subtype;
wth->file_encap = WTAP_ENCAP_WIRESHARK_UPPER_PDU;
/* Starting timestamp has microsecond precision, but delta time
* between packets is only milliseconds.
*/
wth->file_tsprec = WTAP_TSPREC_MSEC;
return WTAP_OPEN_MINE;
}
static gboolean
rtpdump_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info)
{
rtpdump_priv_t *priv = (rtpdump_priv_t *)wth->priv;
nstime_t ts = NSTIME_INIT_ZERO;
guint32 epdu_len = 0; /* length of the Exported PDU headers we add */
const guint8 hdr_len = 8; /* Header comprised of the following 3 fields: */
guint16 length; /* length of packet, including this header (may
be smaller than plen if not whole packet recorded) */
guint16 plen; /* actual header+payload length for RTP, 0 for RTCP */
guint32 offset; /* milliseconds since the start of recording */
if (!wtap_read_bytes_or_eof(fh, (void *)&length, 2, err, err_info)) return FALSE;
length = g_ntohs(length);
if (!wtap_read_bytes(fh, (void *)&plen, 2, err, err_info)) return FALSE;
plen = g_ntohs(plen);
if (!wtap_read_bytes(fh, (void *)&offset, 4, err, err_info)) return FALSE;
offset = g_ntohl(offset);
/* Set length to remaining length of packet data */
length -= hdr_len;
ws_buffer_append_buffer(buf, &priv->epdu_headers);
if (plen == 0) {
/* RTCP sample */
plen = length;
wtap_buffer_append_epdu_string(buf, EXP_PDU_TAG_DISSECTOR_NAME, "rtcp");
}
else {
/* RTP sample */
wtap_buffer_append_epdu_string(buf, EXP_PDU_TAG_DISSECTOR_NAME, "rtp");
}
epdu_len = wtap_buffer_append_epdu_end(buf);
/* Offset is milliseconds since the start of recording */
ts.secs = offset / 1000;
ts.nsecs = (offset % 1000) * 1000000;
nstime_sum(&rec->ts, &priv->start_time, &ts);
rec->presence_flags |= WTAP_HAS_TS | WTAP_HAS_CAP_LEN;
rec->rec_header.packet_header.caplen = epdu_len + plen;
rec->rec_header.packet_header.len = epdu_len + length;
rec->rec_type = REC_TYPE_PACKET;
return wtap_read_packet_bytes(fh, buf, length, err, err_info);
}
static gboolean
rtpdump_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err, gchar **err_info,
gint64 *data_offset)
{
*data_offset = file_tell(wth->fh);
return rtpdump_read_packet(wth, wth->fh, rec, buf, err, err_info);
}
static gboolean
rtpdump_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info)
{
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return FALSE;
return rtpdump_read_packet(wth, wth->random_fh, rec, buf, err, err_info);
}
static void
rtpdump_close(wtap *wth)
{
rtpdump_priv_t *priv = (rtpdump_priv_t *)wth->priv;
ws_buffer_free(&priv->epdu_headers);
}
static const struct supported_block_type rtpdump_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 rtpdump_info = {
"RTPDump stream file", "rtpdump", "rtp", "rtpdump",
FALSE, BLOCKS_SUPPORTED(rtpdump_blocks_supported),
NULL, NULL, NULL
};
void register_rtpdump(void)
{
rtpdump_file_type_subtype = wtap_register_file_type_subtype(&rtpdump_info);
} |
C/C++ | wireshark/wiretap/rtpdump.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* Support for RTPDump file format
* Copyright (c) 2023 by David Perry <[email protected]
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef RTPDUMP_H__
#define RTPDUMP_H__
#include <wiretap/wtap.h>
wtap_open_return_val
rtpdump_open(wtap *wth, int *err, char **err_info);
#endif /* RTPDUMP_H__ */ |
C | wireshark/wiretap/ruby_marshal.c | /* ruby_marshal.c
*
* Routines for reading a binary file containing a ruby marshal object
*
* Copyright 2018, Dario Lombardo <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "ruby_marshal.h"
static int ruby_marshal_file_type_subtype = -1;
void register_ruby_marshal(void);
static gboolean is_ruby_marshal(const guint8* filebuf)
{
if (filebuf[0] != RUBY_MARSHAL_MAJOR)
return FALSE;
if (filebuf[1] != RUBY_MARSHAL_MINOR)
return FALSE;
switch (filebuf[2]) {
case '0':
case 'T':
case 'F':
case 'i':
case ':':
case '"':
case 'I':
case '[':
case '{':
case 'f':
case 'c':
case 'm':
case 'S':
case '/':
case 'o':
case 'C':
case 'e':
case ';':
case '@':
return TRUE;
break;
default:
return FALSE;
}
}
wtap_open_return_val ruby_marshal_open(wtap *wth, int *err, gchar **err_info)
{
/* The size of this buffer should match the expectations of is_ruby_marshal */
guint8 filebuf[3];
int bytes_read;
bytes_read = file_read(filebuf, sizeof(filebuf), wth->fh);
if (bytes_read < 0) {
/* Read error. */
*err = file_error(wth->fh, err_info);
return WTAP_OPEN_ERROR;
}
if (bytes_read != sizeof(filebuf) || !is_ruby_marshal(filebuf)) {
return WTAP_OPEN_NOT_MINE;
}
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
return WTAP_OPEN_ERROR;
}
wth->file_type_subtype = ruby_marshal_file_type_subtype;
wth->file_encap = WTAP_ENCAP_RUBY_MARSHAL;
wth->file_tsprec = WTAP_TSPREC_SEC;
wth->subtype_read = wtap_full_file_read;
wth->subtype_seek_read = wtap_full_file_seek_read;
wth->snapshot_length = 0;
return WTAP_OPEN_MINE;
}
static const struct supported_block_type ruby_marshal_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 ruby_marshal_info = {
"Ruby marshal files", "ruby_marshal", NULL, NULL,
FALSE, BLOCKS_SUPPORTED(ruby_marshal_blocks_supported),
NULL, NULL, NULL
};
void register_ruby_marshal(void)
{
ruby_marshal_file_type_subtype = wtap_register_file_type_subtype(&ruby_marshal_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("RUBY_MARSHAL",
ruby_marshal_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/ruby_marshal.h | /** @file
*
* Copyright 2018, Dario Lombardo <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#ifndef __RUBY_MARSHAL_H__
#define __RUBY_MARSHAL_H__
#include <glib.h>
#include "wtap.h"
// Current Ruby Marshal library version
#define RUBY_MARSHAL_MAJOR 4
#define RUBY_MARSHAL_MINOR 8
wtap_open_return_val ruby_marshal_open(wtap *wth, int *err, gchar **err_info);
#endif
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/secrets-types.h | /** @file
*
* Identifiers used by Decryption Secrets Blocks (DSB).
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __SECRETS_TYPES_H__
#define __SECRETS_TYPES_H__
/*
* Type describing the format of the opaque secrets value in a pcapng DSB.
*/
#define SECRETS_TYPE_TLS 0x544c534b /* TLS Key Log */
#define SECRETS_TYPE_SSH 0x5353484b /* SSH Key Log */
#define SECRETS_TYPE_WIREGUARD 0x57474b4c /* WireGuard Key Log */
#define SECRETS_TYPE_ZIGBEE_NWK_KEY 0x5a4e574b /* Zigbee NWK Key */
#define SECRETS_TYPE_ZIGBEE_APS_KEY 0x5a415053 /* Zigbee APS Key */
#endif /* __SECRETS_TYPES_H__ */ |
C | wireshark/wiretap/snoop.c | /* snoop.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "atm.h"
#include "snoop.h"
#include <wsutil/802_11-utils.h>
#include <wsutil/ws_roundup.h>
/* See RFC 1761 for a description of the "snoop" file format. */
typedef struct {
gboolean is_shomiti;
} snoop_t;
/* Magic number in "snoop" files. */
static const char snoop_magic[] = {
's', 'n', 'o', 'o', 'p', '\0', '\0', '\0'
};
/* "snoop" file header (minus magic number). */
struct snoop_hdr {
guint32 version; /* version number (should be 2) */
guint32 network; /* network type */
};
/* "snoop" record header. */
struct snooprec_hdr {
guint32 orig_len; /* actual length of packet */
guint32 incl_len; /* number of octets captured in file */
guint32 rec_len; /* length of record */
guint32 cum_drops; /* cumulative number of dropped packets */
guint32 ts_sec; /* timestamp seconds */
guint32 ts_usec; /* timestamp microseconds */
};
/*
* The link-layer header on ATM packets.
*/
struct snoop_atm_hdr {
guint8 flags; /* destination and traffic type */
guint8 vpi; /* VPI */
guint16 vci; /* VCI */
};
/*
* Extra information stuffed into the padding in Shomiti/Finisar Surveyor
* captures.
*/
struct shomiti_trailer {
guint16 phy_rx_length; /* length on the wire, including FCS? */
guint16 phy_rx_status; /* status flags */
guint32 ts_40_ns_lsb; /* 40 ns time stamp, low-order bytes? */
guint32 ts_40_ns_msb; /* 40 ns time stamp, low-order bytes? */
gint32 frame_id; /* "FrameID"? */
};
/*
* phy_rx_status flags.
*/
#define RX_STATUS_OVERFLOW 0x8000 /* overflow error */
#define RX_STATUS_BAD_CRC 0x4000 /* CRC error */
#define RX_STATUS_DRIBBLE_NIBBLE 0x2000 /* dribble/nibble bits? */
#define RX_STATUS_SHORT_FRAME 0x1000 /* frame < 64 bytes */
#define RX_STATUS_OVERSIZE_FRAME 0x0800 /* frame > 1518 bytes */
#define RX_STATUS_GOOD_FRAME 0x0400 /* frame OK */
#define RX_STATUS_N12_BYTES_RECEIVED 0x0200 /* first 12 bytes of frame received? */
#define RX_STATUS_RXABORT 0x0100 /* RXABORT during reception */
#define RX_STATUS_FIFO_ERROR 0x0080 /* receive FIFO error */
#define RX_STATUS_TRIGGERED 0x0001 /* frame did trigger */
static gboolean snoop_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset);
static gboolean snoop_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
static int snoop_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info);
static gboolean snoop_read_atm_pseudoheader(FILE_T fh,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info);
static gboolean snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info,
int *header_size);
static gboolean snoop_dump(wtap_dumper *wdh, const wtap_rec *rec,
const guint8 *pd, int *err, gchar **err_info);
static int snoop_file_type_subtype = -1;
static int shomiti_file_type_subtype = -1;
void register_snoop(void);
/*
* See
*
* https://pubs.opengroup.org/onlinepubs/9638599/apdxf.htm
*
* for the "dlpi.h" header file specified by The Open Group, which lists
* the DL_ values for various protocols; Solaris 7 uses the same values.
*
* See
*
* https://www.iana.org/assignments/snoop-datalink-types
*
* for the IETF list of snoop datalink types.
*
* The page at
*
* http://mrpink.lerc.nasa.gov/118x/support.html
*
* had links to modified versions of "tcpdump" and "libpcap" for SUNatm
* DLPI support; they suggested that the 3.0 version of SUNatm uses those
* values. The Wayback Machine archived that page, but not the stuff
* to which it linked, unfortunately.
*
* It also has a link to "convert.c", which is a program to convert files
* from the format written by the "atmsnoop" program that comes with the
* SunATM package to regular "snoop" format, claims that "SunATM 2.1 claimed
* to be DL_FDDI (don't ask why). SunATM 3.0 claims to be DL_IPATM, which
* is 0x12".
*
* It also says that "ATM Mac header is 12 bytes long.", and seems to imply
* that in an "atmsnoop" file, the header contains 2 bytes (direction and
* VPI?), 2 bytes of VCI, 6 bytes of something, and 2 bytes of Ethernet
* type; if those 6 bytes are 2 bytes of DSAP, 2 bytes of LSAP, 1 byte
* of LLC control, and 3 bytes of SNAP OUI, that'd mean that an ATM
* pseudo-header in an "atmsnoop" file is probably 1 byte of direction,
* 1 byte of VPI, and 2 bytes of VCI.
*
* The aforementioned page also has a link to some capture files from
* "atmsnoop"; this version of "snoop.c" appears to be able to read them.
*
* Source to an "atmdump" package, which includes a modified version of
* "libpcap" to handle SunATM DLPI and an ATM driver for FreeBSD, and
* also includes "atmdump", which is a modified "tcpdump", was available
* at
*
* ftp://ftp.cs.ndsu.nodak.edu/pub/freebsd/atm/atm-bpf.tgz
*
* (the host name is no longer valid) and that code also indicated that
* DL_IPATM is used, and that an ATM packet handed up from the Sun driver
* for the Sun SBus ATM card on Solaris 2.5.1 has 1 byte of direction,
* 1 byte of VPI, 2 bytes of VCI, and then the ATM PDU, and suggests that
* the direction flag is 0x80 for "transmitted" (presumably meaning
* DTE->DCE) and presumably not 0x80 for "received" (presumably meaning
* DCE->DTE). That code was used as the basis for the SunATM support in
* later versions of libpcap and tcpdump, and it worked at the time the
* development was done with the SunATM code on the system on which the
* development was done.
*
* In fact, the "direction" byte appears to have some other stuff, perhaps
* a traffic type, in the lower 7 bits, with the 8th bit indicating the
* direction. That appears to be the case.
*
* I don't know what the encapsulation of any of the other types is, so I
* leave them all as WTAP_ENCAP_UNKNOWN, except for those for which Brian
* Ginsbach has supplied information about the way UNICOS/mp uses them.
* I also don't know whether "snoop" can handle any of them (it presumably
* can't handle ATM, otherwise Sun wouldn't have supplied "atmsnoop"; even
* if it can't, this may be useful reference information for anybody doing
* code to use DLPI to do raw packet captures on those network types.
*
* https://web.archive.org/web/20010906213807/http://www.shomiti.com/support/TNCapFileFormat.htm
*
* gives information on Shomiti's mutant flavor of snoop. For some unknown
* reason, they decided not to just Go With The DLPI Flow, and instead used
* the types unspecified in RFC 1461 for their own nefarious purposes, such
* as distinguishing 10MB from 100MB from 1000MB Ethernet and distinguishing
* 4MB from 16MB Token Ring, and distinguishing both of them from the
* "Shomiti" versions of same.
*/
wtap_open_return_val snoop_open(wtap *wth, int *err, gchar **err_info)
{
char magic[sizeof snoop_magic];
struct snoop_hdr hdr;
struct snooprec_hdr rec_hdr;
guint padbytes;
gboolean is_shomiti;
static const int snoop_encap[] = {
WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
WTAP_ENCAP_TOKEN_RING,
WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
WTAP_ENCAP_ETHERNET,
WTAP_ENCAP_UNKNOWN, /* HDLC */
WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
WTAP_ENCAP_FDDI_BITSWAPPED,
WTAP_ENCAP_NULL, /* Other */
WTAP_ENCAP_UNKNOWN, /* Frame Relay LAPF */
WTAP_ENCAP_UNKNOWN, /* Multi-protocol over Frame Relay */
WTAP_ENCAP_UNKNOWN, /* Character Async (e.g., SLIP and PPP?) */
WTAP_ENCAP_UNKNOWN, /* X.25 Classical IP */
WTAP_ENCAP_NULL, /* software loopback */
WTAP_ENCAP_UNKNOWN, /* not defined in "dlpi.h" */
WTAP_ENCAP_IP_OVER_FC, /* Fibre Channel */
WTAP_ENCAP_UNKNOWN, /* ATM */
WTAP_ENCAP_ATM_PDUS, /* ATM Classical IP */
WTAP_ENCAP_UNKNOWN, /* X.25 LAPB */
WTAP_ENCAP_UNKNOWN, /* ISDN */
WTAP_ENCAP_UNKNOWN, /* HIPPI */
WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Ethernet */
WTAP_ENCAP_UNKNOWN, /* 100VG-AnyLAN Token Ring */
WTAP_ENCAP_UNKNOWN, /* "ISO 8802/3 and Ethernet" */
WTAP_ENCAP_UNKNOWN, /* 100BaseT (but that's just Ethernet) */
WTAP_ENCAP_IP_OVER_IB_SNOOP, /* Infiniband */
};
#define NUM_SNOOP_ENCAPS (sizeof snoop_encap / sizeof snoop_encap[0])
#define SNOOP_PRIVATE_BIT 0x80000000
static const int snoop_private_encap[] = {
WTAP_ENCAP_UNKNOWN, /* Not Used */
WTAP_ENCAP_UNKNOWN, /* IPv4 Tunnel Link */
WTAP_ENCAP_UNKNOWN, /* IPv6 Tunnel Link */
WTAP_ENCAP_UNKNOWN, /* Virtual network interface */
WTAP_ENCAP_UNKNOWN, /* IEEE 802.11 */
WTAP_ENCAP_IPNET, /* ipnet(7D) link */
WTAP_ENCAP_UNKNOWN, /* IPMP stub interface */
WTAP_ENCAP_UNKNOWN, /* 6to4 Tunnel Link */
};
#define NUM_SNOOP_PRIVATE_ENCAPS (sizeof snoop_private_encap / sizeof snoop_private_encap[0])
static const int shomiti_encap[] = {
WTAP_ENCAP_ETHERNET, /* IEEE 802.3 */
WTAP_ENCAP_UNKNOWN, /* IEEE 802.4 Token Bus */
WTAP_ENCAP_TOKEN_RING,
WTAP_ENCAP_UNKNOWN, /* IEEE 802.6 Metro Net */
WTAP_ENCAP_ETHERNET,
WTAP_ENCAP_UNKNOWN, /* HDLC */
WTAP_ENCAP_UNKNOWN, /* Character Synchronous, e.g. bisync */
WTAP_ENCAP_UNKNOWN, /* IBM Channel-to-Channel */
WTAP_ENCAP_FDDI_BITSWAPPED,
WTAP_ENCAP_UNKNOWN, /* Other */
WTAP_ENCAP_ETHERNET, /* Fast Ethernet */
WTAP_ENCAP_TOKEN_RING, /* 4MB 802.5 token ring */
WTAP_ENCAP_ETHERNET, /* Gigabit Ethernet */
WTAP_ENCAP_TOKEN_RING, /* "IEEE 802.5 Shomiti" */
WTAP_ENCAP_TOKEN_RING, /* "4MB IEEE 802.5 Shomiti" */
WTAP_ENCAP_UNKNOWN, /* Other */
WTAP_ENCAP_UNKNOWN, /* Other */
WTAP_ENCAP_UNKNOWN, /* Other */
WTAP_ENCAP_IEEE_802_11_WITH_RADIO, /* IEEE 802.11 with Radio Header */
WTAP_ENCAP_ETHERNET, /* 10 Gigabit Ethernet */
};
#define NUM_SHOMITI_ENCAPS (sizeof shomiti_encap / sizeof shomiti_encap[0])
int file_encap;
gint64 saved_offset;
snoop_t *snoop;
/* Read in the string that should be at the start of a "snoop" file */
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, snoop_magic, sizeof snoop_magic) != 0) {
return WTAP_OPEN_NOT_MINE;
}
/* Read the rest of the header. */
if (!wtap_read_bytes(wth->fh, &hdr, sizeof hdr, err, err_info))
return WTAP_OPEN_ERROR;
/*
* Make sure it's a version we support.
*/
hdr.version = g_ntohl(hdr.version);
switch (hdr.version) {
case 2: /* Solaris 2.x and later snoop, and Shomiti
Surveyor prior to 3.0, or 3.0 and later
with NDIS card */
case 3: /* Surveyor 3.0 and later, with Shomiti CMM2 hardware */
case 4: /* Surveyor 3.0 and later, with Shomiti GAM hardware */
case 5: /* Surveyor 3.0 and later, with Shomiti THG hardware */
break;
default:
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("snoop: version %u unsupported", hdr.version);
return WTAP_OPEN_ERROR;
}
/*
* Oh, this is lovely.
*
* I suppose Shomiti could give a bunch of lawyerly noise about
* how "well, RFC 1761 said they were unassigned, and that's
* the standard, not the DLPI header file, so it's perfectly OK
* for us to use them, blah blah blah", but it's still irritating
* as hell that they used the unassigned-in-RFC-1761 values for
* their own purposes - especially given that Sun also used
* one of them in atmsnoop.
*
* We can't determine whether it's a Shomiti capture based on
* the version number, as, according to their documentation on
* their capture file format, Shomiti uses a version number of 2
* if the data "was captured using an NDIS card", which presumably
* means "captured with an ordinary boring network card via NDIS"
* as opposed to "captured with our whizzo special capture
* hardware".
*
* The only way I can see to determine that is to check how much
* padding there is in the first packet - if there's enough
* padding for a Shomiti trailer, it's probably a Shomiti
* capture, and otherwise, it's probably from Snoop.
*/
/*
* Start out assuming it's not a Shomiti capture.
*/
is_shomiti = FALSE;
/* Read first record header. */
saved_offset = file_tell(wth->fh);
if (!wtap_read_bytes_or_eof(wth->fh, &rec_hdr, sizeof rec_hdr, err, err_info)) {
if (*err != 0)
return WTAP_OPEN_ERROR;
/*
* The file ends after the record header, which means this
* is a capture with no packets.
*
* We assume it's a snoop file; the actual type of file is
* irrelevant, as there are no records in it, and thus no
* extra information if it's a Shomiti capture, and no
* link-layer headers whose type we have to know, and no
* Ethernet frames that might have an FCS.
*/
} else {
/*
* Compute the number of bytes of padding in the
* record. If it's at least the size of a Shomiti
* trailer record, we assume this is a Shomiti
* capture. (Some atmsnoop captures appear
* to have 4 bytes of padding, and at least one
* snoop capture appears to have 6 bytes of padding;
* the Shomiti header is larger than either of those.)
*/
if (g_ntohl(rec_hdr.rec_len) >
(sizeof rec_hdr + g_ntohl(rec_hdr.incl_len))) {
/*
* Well, we have padding; how much?
*/
padbytes = g_ntohl(rec_hdr.rec_len) -
((guint)sizeof rec_hdr + g_ntohl(rec_hdr.incl_len));
/*
* Is it at least the size of a Shomiti trailer?
*/
is_shomiti =
(padbytes >= sizeof (struct shomiti_trailer));
}
}
/*
* Seek back to the beginning of the first record.
*/
if (file_seek(wth->fh, saved_offset, SEEK_SET, err) == -1)
return WTAP_OPEN_ERROR;
hdr.network = g_ntohl(hdr.network);
if (is_shomiti) {
if (hdr.network >= NUM_SHOMITI_ENCAPS
|| shomiti_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("snoop: Shomiti network type %u unknown or unsupported",
hdr.network);
return WTAP_OPEN_ERROR;
}
file_encap = shomiti_encap[hdr.network];
} else if (hdr.network & SNOOP_PRIVATE_BIT) {
if ((hdr.network^SNOOP_PRIVATE_BIT) >= NUM_SNOOP_PRIVATE_ENCAPS
|| snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT] == WTAP_ENCAP_UNKNOWN) {
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("snoop: private network type %u unknown or unsupported",
hdr.network);
return WTAP_OPEN_ERROR;
}
file_encap = snoop_private_encap[hdr.network^SNOOP_PRIVATE_BIT];
} else {
if (hdr.network >= NUM_SNOOP_ENCAPS
|| snoop_encap[hdr.network] == WTAP_ENCAP_UNKNOWN) {
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("snoop: network type %u unknown or unsupported",
hdr.network);
return WTAP_OPEN_ERROR;
}
file_encap = snoop_encap[hdr.network];
}
/*
* We don't currently use the extra information in Shomiti
* records, so we use the same routines to read snoop and
* Shomiti files.
*/
wth->file_type_subtype = is_shomiti ? shomiti_file_type_subtype : snoop_file_type_subtype;
snoop = g_new0(snoop_t, 1);
wth->priv = (void *)snoop;
wth->subtype_read = snoop_read;
wth->subtype_seek_read = snoop_seek_read;
wth->file_encap = file_encap;
wth->snapshot_length = 0; /* not available in header */
wth->file_tsprec = WTAP_TSPREC_USEC;
snoop->is_shomiti = is_shomiti;
/*
* Add an IDB; we don't know how many interfaces were
* involved, so we just say one interface, about which
* we only know the link-layer type, snapshot length,
* and time stamp resolution.
*/
wtap_add_generated_idb(wth);
return WTAP_OPEN_MINE;
}
/*
* XXX - pad[3] is the length of the header, not including
* the length of the pad field; is it a 1-byte field, a 2-byte
* field with pad[2] usually being 0, a 3-byte field with
* pad[1] and pad[2] usually being 0, or a 4-byte field?
*
* If it's not a 4-byte field, is there anything significant
* in the other bytes?
*
* Can the header length ever be less than 8, so that not
* all the fields following pad are present?
*
* What's in undecrypt? In captures I've seen, undecrypt[0]
* is usually 0x00 but sometimes 0x02 or 0x06, and undecrypt[1]
* is either 0x00 or 0x02.
*
* What's in preamble? In captures I've seen, it's 0x00.
*
* What's in code? In captures I've seen, it's 0x01 or 0x03.
*
* If the header is longer than 8 bytes, what are the other fields?
*/
typedef struct {
guint8 pad[4];
guint8 undecrypt[2];
guint8 rate;
guint8 preamble;
guint8 code;
guint8 signal;
guint8 qual;
guint8 channel;
} shomiti_wireless_header;
/* Read the next packet */
static gboolean snoop_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset)
{
int padbytes;
*data_offset = file_tell(wth->fh);
padbytes = snoop_read_packet(wth, wth->fh, rec, buf, err, err_info);
if (padbytes == -1)
return FALSE;
/*
* Skip over the padding, if any.
*/
if (padbytes != 0) {
if (!wtap_read_bytes(wth->fh, NULL, padbytes, err, err_info))
return FALSE;
}
return TRUE;
}
static gboolean
snoop_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
{
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return FALSE;
if (snoop_read_packet(wth, wth->random_fh, rec, buf, err, err_info) == -1) {
if (*err == 0)
*err = WTAP_ERR_SHORT_READ;
return FALSE;
}
return TRUE;
}
static int
snoop_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info)
{
snoop_t *snoop = (snoop_t *)wth->priv;
struct snooprec_hdr hdr;
guint32 rec_size;
guint32 packet_size;
guint32 orig_size;
int header_size;
/* Read record header. */
if (!wtap_read_bytes_or_eof(fh, &hdr, sizeof hdr, err, err_info))
return -1;
rec_size = g_ntohl(hdr.rec_len);
orig_size = g_ntohl(hdr.orig_len);
packet_size = g_ntohl(hdr.incl_len);
if (orig_size > WTAP_MAX_PACKET_SIZE_STANDARD) {
/*
* Probably a corrupt capture file; don't blow up trying
* to allocate space for an immensely-large packet.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("snoop: File has %u-byte original length, bigger than maximum of %u",
orig_size, WTAP_MAX_PACKET_SIZE_STANDARD);
return -1;
}
if (packet_size > WTAP_MAX_PACKET_SIZE_STANDARD) {
/*
* Probably a corrupt capture file; don't blow up trying
* to allocate space for an immensely-large packet.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("snoop: File has %u-byte packet, bigger than maximum of %u",
packet_size, WTAP_MAX_PACKET_SIZE_STANDARD);
return -1;
}
if (packet_size > rec_size) {
/*
* Probably a corrupt capture file.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("snoop: File has %u-byte packet, bigger than record size %u",
packet_size, rec_size);
return -1;
}
switch (wth->file_encap) {
case WTAP_ENCAP_ATM_PDUS:
/*
* This is an ATM packet, so the first four bytes are
* the direction of the packet (transmit/receive), the
* VPI, and the VCI; read them and generate the
* pseudo-header from them.
*/
if (packet_size < sizeof (struct snoop_atm_hdr)) {
/*
* Uh-oh, the packet isn't big enough to even
* have a pseudo-header.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("snoop: atmsnoop file has a %u-byte packet, too small to have even an ATM pseudo-header",
packet_size);
return -1;
}
if (!snoop_read_atm_pseudoheader(fh, &rec->rec_header.packet_header.pseudo_header,
err, err_info))
return -1; /* Read error */
/*
* Don't count the pseudo-header as part of the packet.
*/
rec_size -= (guint32)sizeof (struct snoop_atm_hdr);
orig_size -= (guint32)sizeof (struct snoop_atm_hdr);
packet_size -= (guint32)sizeof (struct snoop_atm_hdr);
break;
case WTAP_ENCAP_ETHERNET:
/*
* If this is a snoop file, we assume there's no FCS in
* this frame; if this is a Shomit file, we assume there
* is. (XXX - or should we treat it a "maybe"?)
*/
if (snoop->is_shomiti)
rec->rec_header.packet_header.pseudo_header.eth.fcs_len = 4;
else
rec->rec_header.packet_header.pseudo_header.eth.fcs_len = 0;
break;
case WTAP_ENCAP_IEEE_802_11_WITH_RADIO:
if (packet_size < sizeof (shomiti_wireless_header)) {
/*
* Uh-oh, the packet isn't big enough to even
* have a pseudo-header.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("snoop: Shomiti wireless file has a %u-byte packet, too small to have even a wireless pseudo-header",
packet_size);
return -1;
}
if (!snoop_read_shomiti_wireless_pseudoheader(fh,
&rec->rec_header.packet_header.pseudo_header, err, err_info, &header_size))
return -1; /* Read error */
/*
* Don't count the pseudo-header as part of the packet.
*/
rec_size -= header_size;
orig_size -= header_size;
packet_size -= header_size;
break;
}
rec->rec_type = REC_TYPE_PACKET;
rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
rec->ts.secs = g_ntohl(hdr.ts_sec);
rec->ts.nsecs = g_ntohl(hdr.ts_usec) * 1000;
rec->rec_header.packet_header.caplen = packet_size;
rec->rec_header.packet_header.len = orig_size;
if (rec_size < (sizeof hdr + packet_size)) {
/*
* What, *negative* padding? Bogus.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("snoop: File has %u-byte record with packet size of %u",
rec_size, packet_size);
return -1;
}
/*
* Read the packet data.
*/
if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
return -1; /* failed */
/*
* If this is ATM LANE traffic, try to guess what type of LANE
* traffic it is based on the packet contents.
*/
if (wth->file_encap == WTAP_ENCAP_ATM_PDUS &&
rec->rec_header.packet_header.pseudo_header.atm.type == TRAF_LANE) {
atm_guess_lane_type(rec, ws_buffer_start_ptr(buf));
}
return rec_size - ((guint)sizeof hdr + packet_size);
}
static gboolean
snoop_read_atm_pseudoheader(FILE_T fh, union wtap_pseudo_header *pseudo_header,
int *err, gchar **err_info)
{
struct snoop_atm_hdr atm_phdr;
guint8 vpi;
guint16 vci;
if (!wtap_read_bytes(fh, &atm_phdr, sizeof atm_phdr, err, err_info))
return FALSE;
vpi = atm_phdr.vpi;
vci = pntoh16(&atm_phdr.vci);
/*
* The lower 4 bits of the first byte of the header indicate
* the type of traffic, as per the "atmioctl.h" header in
* SunATM.
*/
switch (atm_phdr.flags & 0x0F) {
case 0x01: /* LANE */
pseudo_header->atm.aal = AAL_5;
pseudo_header->atm.type = TRAF_LANE;
break;
case 0x02: /* RFC 1483 LLC multiplexed traffic */
pseudo_header->atm.aal = AAL_5;
pseudo_header->atm.type = TRAF_LLCMX;
break;
case 0x05: /* ILMI */
pseudo_header->atm.aal = AAL_5;
pseudo_header->atm.type = TRAF_ILMI;
break;
case 0x06: /* Signalling AAL */
pseudo_header->atm.aal = AAL_SIGNALLING;
pseudo_header->atm.type = TRAF_UNKNOWN;
break;
case 0x03: /* MARS (RFC 2022) */
pseudo_header->atm.aal = AAL_5;
pseudo_header->atm.type = TRAF_UNKNOWN;
break;
case 0x04: /* IFMP (Ipsilon Flow Management Protocol; see RFC 1954) */
pseudo_header->atm.aal = AAL_5;
pseudo_header->atm.type = TRAF_UNKNOWN; /* XXX - TRAF_IPSILON? */
break;
default:
/*
* Assume it's AAL5, unless it's VPI 0 and VCI 5, in which
* case assume it's AAL_SIGNALLING; we know nothing more
* about it.
*
* XXX - is this necessary? Or are we guaranteed that
* all signalling traffic has a type of 0x06?
*
* XXX - is this guaranteed to be AAL5? Or, if the type is
* 0x00 ("raw"), might it be non-AAL5 traffic?
*/
if (vpi == 0 && vci == 5)
pseudo_header->atm.aal = AAL_SIGNALLING;
else
pseudo_header->atm.aal = AAL_5;
pseudo_header->atm.type = TRAF_UNKNOWN;
break;
}
pseudo_header->atm.subtype = TRAF_ST_UNKNOWN;
pseudo_header->atm.vpi = vpi;
pseudo_header->atm.vci = vci;
pseudo_header->atm.channel = (atm_phdr.flags & 0x80) ? 0 : 1;
/* We don't have this information */
pseudo_header->atm.flags = 0;
pseudo_header->atm.cells = 0;
pseudo_header->atm.aal5t_u2u = 0;
pseudo_header->atm.aal5t_len = 0;
pseudo_header->atm.aal5t_chksum = 0;
return TRUE;
}
static gboolean
snoop_read_shomiti_wireless_pseudoheader(FILE_T fh,
union wtap_pseudo_header *pseudo_header, int *err, gchar **err_info,
int *header_size)
{
shomiti_wireless_header whdr;
int rsize;
if (!wtap_read_bytes(fh, &whdr, sizeof whdr, err, err_info))
return FALSE;
/* the 4th byte of the pad is actually a header length,
* we've already read 8 bytes of it, and it must never
* be less than 8.
*
* XXX - presumably that means that the header length
* doesn't include the length field, as we've read
* 12 bytes total.
*/
if (whdr.pad[3] < 8) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("snoop: Header length in Surveyor record is %u, less than minimum of 8",
whdr.pad[3]);
return FALSE;
}
/* Skip the header. */
rsize = ((int) whdr.pad[3]) - 8;
if (!wtap_read_bytes(fh, NULL, rsize, err, err_info))
return FALSE;
memset(&pseudo_header->ieee_802_11, 0, sizeof(pseudo_header->ieee_802_11));
pseudo_header->ieee_802_11.fcs_len = 4;
pseudo_header->ieee_802_11.decrypted = FALSE;
pseudo_header->ieee_802_11.datapad = FALSE;
pseudo_header->ieee_802_11.phy = PHDR_802_11_PHY_UNKNOWN;
pseudo_header->ieee_802_11.has_channel = TRUE;
pseudo_header->ieee_802_11.channel = whdr.channel;
pseudo_header->ieee_802_11.has_data_rate = TRUE;
pseudo_header->ieee_802_11.data_rate = whdr.rate;
pseudo_header->ieee_802_11.has_signal_percent = TRUE;
pseudo_header->ieee_802_11.signal_percent = whdr.signal;
/*
* We don't know they PHY, but we do have the data rate;
* try to guess the PHY based on the data rate and channel.
*/
if (RATE_IS_DSSS(pseudo_header->ieee_802_11.data_rate)) {
/* 11b */
pseudo_header->ieee_802_11.phy = PHDR_802_11_PHY_11B;
pseudo_header->ieee_802_11.phy_info.info_11b.has_short_preamble = FALSE;
} else if (RATE_IS_OFDM(pseudo_header->ieee_802_11.data_rate)) {
/* 11a or 11g, depending on the band. */
if (CHAN_IS_BG(pseudo_header->ieee_802_11.channel)) {
/* 11g */
pseudo_header->ieee_802_11.phy = PHDR_802_11_PHY_11G;
pseudo_header->ieee_802_11.phy_info.info_11g.has_mode = FALSE;
} else {
/* 11a */
pseudo_header->ieee_802_11.phy = PHDR_802_11_PHY_11A;
pseudo_header->ieee_802_11.phy_info.info_11a.has_channel_type = FALSE;
pseudo_header->ieee_802_11.phy_info.info_11a.has_turbo_type = FALSE;
}
}
/* add back the header and don't forget the pad as well */
*header_size = rsize + 8 + 4;
return TRUE;
}
static const int wtap_encap[] = {
-1, /* WTAP_ENCAP_UNKNOWN -> unsupported */
0x04, /* WTAP_ENCAP_ETHERNET -> DL_ETHER */
0x02, /* WTAP_ENCAP_TOKEN_RING -> DL_TPR */
-1, /* WTAP_ENCAP_SLIP -> unsupported */
-1, /* WTAP_ENCAP_PPP -> unsupported */
0x08, /* WTAP_ENCAP_FDDI -> DL_FDDI */
0x08, /* WTAP_ENCAP_FDDI_BITSWAPPED -> DL_FDDI */
-1, /* WTAP_ENCAP_RAW_IP -> unsupported */
-1, /* WTAP_ENCAP_ARCNET -> unsupported */
-1, /* WTAP_ENCAP_ARCNET_LINUX -> unsupported */
-1, /* WTAP_ENCAP_ATM_RFC1483 -> unsupported */
-1, /* WTAP_ENCAP_LINUX_ATM_CLIP -> unsupported */
-1, /* WTAP_ENCAP_LAPB -> unsupported*/
0x12, /* WTAP_ENCAP_ATM_PDUS -> DL_IPATM */
};
#define NUM_WTAP_ENCAPS (sizeof wtap_encap / sizeof wtap_encap[0])
/* Returns 0 if we could write the specified encapsulation type,
an error indication otherwise. */
static int snoop_dump_can_write_encap(int encap)
{
/* Per-packet encapsulations aren't supported. */
if (encap == WTAP_ENCAP_PER_PACKET)
return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
if (encap < 0 || (unsigned)encap >= NUM_WTAP_ENCAPS || wtap_encap[encap] == -1)
return WTAP_ERR_UNWRITABLE_ENCAP;
return 0;
}
/* Returns TRUE on success, FALSE on failure; sets "*err" to an error code on
failure */
static gboolean snoop_dump_open(wtap_dumper *wdh, int *err, gchar **err_info _U_)
{
struct snoop_hdr file_hdr;
/* This is a snoop file */
wdh->subtype_write = snoop_dump;
/* Write the file header. */
if (!wtap_dump_file_write(wdh, &snoop_magic, sizeof snoop_magic, err))
return FALSE;
/* current "snoop" format is 2 */
file_hdr.version = g_htonl(2);
file_hdr.network = g_htonl(wtap_encap[wdh->file_encap]);
if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
return FALSE;
return TRUE;
}
/* Write a record for a packet to a dump file.
Returns TRUE on success, FALSE on failure. */
static gboolean snoop_dump(wtap_dumper *wdh,
const wtap_rec *rec,
const guint8 *pd, int *err, gchar **err_info _U_)
{
const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
struct snooprec_hdr rec_hdr;
int reclen;
guint padlen;
static const char zeroes[4] = {0};
struct snoop_atm_hdr atm_hdr;
int atm_hdrsize;
/* We can only write packet records. */
if (rec->rec_type != REC_TYPE_PACKET) {
*err = WTAP_ERR_UNWRITABLE_REC_TYPE;
return FALSE;
}
/*
* Make sure this packet doesn't have a link-layer type that
* differs from the one for the file.
*/
if (wdh->file_encap != rec->rec_header.packet_header.pkt_encap) {
*err = WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
return FALSE;
}
if (wdh->file_encap == WTAP_ENCAP_ATM_PDUS)
atm_hdrsize = sizeof (struct snoop_atm_hdr);
else
atm_hdrsize = 0;
/* Record length = header length plus data length... */
reclen = (int)sizeof rec_hdr + rec->rec_header.packet_header.caplen + atm_hdrsize;
/* ... plus enough bytes to pad it to a 4-byte boundary. */
padlen = WS_ROUNDUP_4(reclen) - reclen;
reclen += padlen;
/* Don't write anything we're not willing to read. */
if (rec->rec_header.packet_header.caplen + atm_hdrsize > WTAP_MAX_PACKET_SIZE_STANDARD) {
*err = WTAP_ERR_PACKET_TOO_LARGE;
return FALSE;
}
rec_hdr.orig_len = g_htonl(rec->rec_header.packet_header.len + atm_hdrsize);
rec_hdr.incl_len = g_htonl(rec->rec_header.packet_header.caplen + atm_hdrsize);
rec_hdr.rec_len = g_htonl(reclen);
rec_hdr.cum_drops = 0;
rec_hdr.ts_sec = g_htonl(rec->ts.secs);
rec_hdr.ts_usec = g_htonl(rec->ts.nsecs / 1000);
if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof rec_hdr, err))
return FALSE;
if (wdh->file_encap == WTAP_ENCAP_ATM_PDUS) {
/*
* Write the ATM header.
*/
atm_hdr.flags =
(pseudo_header->atm.channel == 0) ? 0x80 : 0x00;
switch (pseudo_header->atm.aal) {
case AAL_SIGNALLING:
/* Signalling AAL */
atm_hdr.flags |= 0x06;
break;
case AAL_5:
switch (pseudo_header->atm.type) {
case TRAF_LANE:
/* LANE */
atm_hdr.flags |= 0x01;
break;
case TRAF_LLCMX:
/* RFC 1483 LLC multiplexed traffic */
atm_hdr.flags |= 0x02;
break;
case TRAF_ILMI:
/* ILMI */
atm_hdr.flags |= 0x05;
break;
}
break;
}
atm_hdr.vpi = (guint8) pseudo_header->atm.vpi;
atm_hdr.vci = g_htons(pseudo_header->atm.vci);
if (!wtap_dump_file_write(wdh, &atm_hdr, sizeof atm_hdr, err))
return FALSE;
}
if (!wtap_dump_file_write(wdh, pd, rec->rec_header.packet_header.caplen, err))
return FALSE;
/* Now write the padding. */
if (!wtap_dump_file_write(wdh, zeroes, padlen, err))
return FALSE;
return TRUE;
}
static const struct supported_block_type snoop_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 snoop_info = {
"Sun snoop", "snoop", "snoop", "cap",
FALSE, BLOCKS_SUPPORTED(snoop_blocks_supported),
snoop_dump_can_write_encap, snoop_dump_open, NULL
};
static const struct supported_block_type shomiti_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 shomiti_info = {
"Shomiti/Finisar Surveyor", "shomiti", "cap", NULL,
FALSE, BLOCKS_SUPPORTED(shomiti_blocks_supported),
NULL, NULL, NULL
};
void register_snoop(void)
{
snoop_file_type_subtype = wtap_register_file_type_subtype(&snoop_info);
shomiti_file_type_subtype = wtap_register_file_type_subtype(&shomiti_info);
/*
* Register names for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("SNOOP",
snoop_file_type_subtype);
wtap_register_backwards_compatibility_lua_name("SHOMITI",
shomiti_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:
*/ |
C/C++ | wireshark/wiretap/snoop.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __W_SNOOP_H__
#define __W_SNOOP_H__
#include <glib.h>
#include "wtap.h"
#include "ws_symbol_export.h"
wtap_open_return_val snoop_open(wtap *wth, int *err, gchar **err_info);
#endif |
C/C++ | wireshark/wiretap/socketcan.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* Support for Busmaster log file format
* Copyright (c) 2019 by Maksim Salau <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef SOCKETCAN_H__
#define SOCKETCAN_H__
#include <gmodule.h>
#define CAN_MAX_DLEN 8
#define CANFD_MAX_DLEN 64
typedef struct can_frame {
guint32 can_id; /* 32 bit CAN_ID + EFF/RTR/ERR flags */
guint8 can_dlc; /* frame payload length in byte (0 .. CAN_MAX_DLEN) */
guint8 __pad; /* padding */
guint8 __res0; /* reserved / padding */
guint8 __res1; /* reserved / padding */
guint8 data[CAN_MAX_DLEN];
} can_frame_t;
typedef struct canfd_frame {
guint32 can_id; /* 32 bit CAN_ID + EFF flag */
guint8 len; /* frame payload length in byte */
guint8 flags; /* additional flags for CAN FD */
guint8 __res0; /* reserved / padding */
guint8 __res1; /* reserved / padding */
guint8 data[CANFD_MAX_DLEN];
} canfd_frame_t;
#endif /* SOCKETCAN_H__ */ |
C | wireshark/wiretap/stanag4607.c | /* stanag4607.c
*
* STANAG 4607 file reading
*
* http://www.nato.int/structur/AC/224/standard/4607/4607e_JAS_ED3.pdf
* That is now missing from that site, but is available on the Wayback
* Machine:
*
* https://web.archive.org/web/20130223054955/http://www.nato.int/structur/AC/224/standard/4607/4607.htm
*
* https://nso.nato.int/nso/zPublic/ap/aedp-7(2).pdf
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "wtap-int.h"
#include "file_wrappers.h"
#include <wsutil/buffer.h>
#include "stanag4607.h"
typedef struct {
time_t base_secs;
} stanag4607_t;
#define PKT_HDR_SIZE 32 /* size of a packet header */
#define SEG_HDR_SIZE 5 /* size of a segment header */
static int stanag4607_file_type_subtype = -1;
void register_stanag4607(void);
static gboolean is_valid_id(guint16 version_id)
{
#define VERSION_21 0x3231
#define VERSION_30 0x3330
if ((version_id != VERSION_21) &&
(version_id != VERSION_30))
/* Not a stanag4607 file */
return FALSE;
return TRUE;
}
static gboolean stanag4607_read_file(wtap *wth, FILE_T fh, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info)
{
stanag4607_t *stanag4607 = (stanag4607_t *)wth->priv;
guint32 millisecs, secs, nsecs;
gint64 offset = 0;
guint8 stanag_pkt_hdr[PKT_HDR_SIZE+SEG_HDR_SIZE];
guint32 packet_size;
*err = 0;
/* Combined packet header and segment header */
if (!wtap_read_bytes_or_eof(fh, stanag_pkt_hdr, sizeof stanag_pkt_hdr, err, err_info))
return FALSE;
offset += sizeof stanag_pkt_hdr;
if (!is_valid_id(pntoh16(&stanag_pkt_hdr[0]))) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("Bad version number");
return FALSE;
}
rec->rec_type = REC_TYPE_PACKET;
rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
/* The next 4 bytes are the packet length */
packet_size = pntoh32(&stanag_pkt_hdr[2]);
if (packet_size > WTAP_MAX_PACKET_SIZE_STANDARD) {
/*
* Probably a corrupt capture file; don't blow up trying
* to allocate space for an immensely-large packet.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("stanag4607: File has %" PRIu32 "d-byte packet, "
"bigger than maximum of %u", packet_size, WTAP_MAX_PACKET_SIZE_STANDARD);
return FALSE;
}
if (packet_size < PKT_HDR_SIZE+SEG_HDR_SIZE) {
/*
* Probably a corrupt capture file; don't, for example, loop
* infinitely if the size is zero.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("stanag4607: File has %" PRIu32 "d-byte packet, "
"smaller than minimum of %u", packet_size, PKT_HDR_SIZE+SEG_HDR_SIZE);
return FALSE;
}
rec->rec_header.packet_header.caplen = packet_size;
rec->rec_header.packet_header.len = packet_size;
/* Sadly, the header doesn't contain times; but some segments do */
/* So, get the segment header, which is just past the 32-byte header. */
rec->presence_flags = WTAP_HAS_TS;
/* If no time specified, it's the last baseline time */
rec->ts.secs = stanag4607->base_secs;
rec->ts.nsecs = 0;
millisecs = 0;
#define MISSION_SEGMENT 1
#define DWELL_SEGMENT 2
#define JOB_DEFINITION_SEGMENT 5
#define PLATFORM_LOCATION_SEGMENT 13
if (MISSION_SEGMENT == stanag_pkt_hdr[32]) {
guint8 mseg[39];
struct tm tm;
if (!wtap_read_bytes(fh, &mseg, sizeof mseg, err, err_info))
return FALSE;
offset += sizeof mseg;
tm.tm_year = pntoh16(&mseg[35]) - 1900;
tm.tm_mon = mseg[37] - 1;
tm.tm_mday = mseg[38];
tm.tm_hour = 0;
tm.tm_min = 0;
tm.tm_sec = 0;
tm.tm_isdst = -1;
stanag4607->base_secs = mktime(&tm);
rec->ts.secs = stanag4607->base_secs;
}
else if (PLATFORM_LOCATION_SEGMENT == stanag_pkt_hdr[32]) {
if (!wtap_read_bytes(fh, &millisecs, sizeof millisecs, err, err_info))
return FALSE;
offset += sizeof millisecs;
millisecs = g_ntohl(millisecs);
}
else if (DWELL_SEGMENT == stanag_pkt_hdr[32]) {
guint8 dseg[19];
if (!wtap_read_bytes(fh, &dseg, sizeof dseg, err, err_info))
return FALSE;
offset += sizeof dseg;
millisecs = pntoh32(&dseg[15]);
}
if (0 != millisecs) {
secs = millisecs/1000;
nsecs = (millisecs - 1000 * secs) * 1000000;
rec->ts.secs = stanag4607->base_secs + secs;
rec->ts.nsecs = nsecs;
}
/* wind back to the start of the packet ... */
if (file_seek(fh, - offset, SEEK_CUR, err) == -1)
return FALSE;
return wtap_read_packet_bytes(fh, buf, packet_size, err, err_info);
}
static gboolean stanag4607_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset)
{
*data_offset = file_tell(wth->fh);
return stanag4607_read_file(wth, wth->fh, rec, buf, err, err_info);
}
static gboolean stanag4607_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info)
{
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return FALSE;
return stanag4607_read_file(wth, wth->random_fh, rec, buf, err, err_info);
}
wtap_open_return_val stanag4607_open(wtap *wth, int *err, gchar **err_info)
{
guint16 version_id;
stanag4607_t *stanag4607;
if (!wtap_read_bytes(wth->fh, &version_id, sizeof version_id, err, err_info))
return (*err != WTAP_ERR_SHORT_READ) ? WTAP_OPEN_ERROR : WTAP_OPEN_NOT_MINE;
if (!is_valid_id(GUINT16_TO_BE(version_id)))
/* Not a stanag4607 file */
return WTAP_OPEN_NOT_MINE;
/* seek back to the start of the file */
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
return WTAP_OPEN_ERROR;
wth->file_type_subtype = stanag4607_file_type_subtype;
wth->file_encap = WTAP_ENCAP_STANAG_4607;
wth->snapshot_length = 0; /* not known */
stanag4607 = g_new(stanag4607_t, 1);
wth->priv = (void *)stanag4607;
stanag4607->base_secs = 0; /* unknown as of yet */
wth->subtype_read = stanag4607_read;
wth->subtype_seek_read = stanag4607_seek_read;
wth->file_tsprec = WTAP_TSPREC_MSEC;
/*
* Add an IDB; we don't know how many interfaces were
* involved, so we just say one interface, about which
* we only know the link-layer type, snapshot length,
* and time stamp resolution.
*/
wtap_add_generated_idb(wth);
return WTAP_OPEN_MINE;
}
static const struct supported_block_type stanag4607_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 stanag4607_info = {
"STANAG 4607 Format", "stanag4607", NULL, NULL,
FALSE, BLOCKS_SUPPORTED(stanag4607_blocks_supported),
NULL, NULL, NULL
};
void register_stanag4607(void)
{
stanag4607_file_type_subtype = wtap_register_file_type_subtype(&stanag4607_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("STANAG_4607",
stanag4607_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local Variables:
* c-basic-offset: 2
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=2 tabstop=8 expandtab:
* :indentSize=2:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/stanag4607.h | /** @file
*
* STANAG 4607 file reading
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#ifndef __STANAG_4607_H__
#define __STANAG_4607_H__
#include <glib.h>
#include <wiretap/wtap.h>
#include "ws_symbol_export.h"
wtap_open_return_val stanag4607_open(wtap *wth, int *err, gchar **err_info);
#endif |
C | wireshark/wiretap/systemd_journal.c | /* systemd_journal.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include "wtap-int.h"
#include "pcapng_module.h"
#include "file_wrappers.h"
#include "systemd_journal.h"
// To do:
// - Request a pcap encapsulation type.
// - Should we add separate types for binary, plain, and JSON or add a metadata header?
// Systemd journals are stored in the following formats:
// Journal File Format (native binary): https://www.freedesktop.org/wiki/Software/systemd/journal-files/
// Journal Export Format: https://www.freedesktop.org/wiki/Software/systemd/export/
// Journal JSON format: https://www.freedesktop.org/wiki/Software/systemd/json/
// This reads Journal Export Format files but could be extended to support
// the binary and JSON formats.
// Example data:
// __CURSOR=s=1d56bab64d414960b9907ab0cc7f7c62;i=2;b=1497926e8b4b4d3ca6a5805e157fa73c;m=5d0ae5;t=56f2f5b66ce6f;x=20cb01e28bb496a8
// __REALTIME_TIMESTAMP=1529624071163503
// __MONOTONIC_TIMESTAMP=6097637
// _BOOT_ID=1497926e8b4b4d3ca6a5805e157fa73c
// PRIORITY=6
// _MACHINE_ID=62c342838a6e436dacea041aa4b5064b
// _HOSTNAME=example.wireshark.org
// _SOURCE_MONOTONIC_TIMESTAMP=0
// _TRANSPORT=kernel
// SYSLOG_FACILITY=0
// SYSLOG_IDENTIFIER=kernel
// MESSAGE=Initializing cgroup subsys cpuset
static gboolean systemd_journal_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset);
static gboolean systemd_journal_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
static gboolean systemd_journal_read_export_entry(FILE_T fh, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info);
// The Journal Export Format specification doesn't place limits on entry
// lengths or lines per entry. We do.
#define MAX_EXPORT_ENTRY_LENGTH WTAP_MAX_PACKET_SIZE_STANDARD
#define MAX_EXPORT_ENTRY_LINES 100
// Strictly speaking, we only need __REALTIME_TIMESTAMP= since we use
// that to set the packet timestamp. According to
// https://www.freedesktop.org/software/systemd/man/systemd.journal-fields.html
// __CURSOR= and __MONOTONIC_TIMESTAMP= should be present as well, so
// check for them order to improve our heuristics.
#define FLD__CURSOR "__CURSOR="
#define FLD__REALTIME_TIMESTAMP "__REALTIME_TIMESTAMP="
#define FLD__MONOTONIC_TIMESTAMP "__MONOTONIC_TIMESTAMP="
static int systemd_journal_file_type_subtype = -1;
void register_systemd_journal(void);
wtap_open_return_val systemd_journal_open(wtap *wth, int *err _U_, gchar **err_info _U_)
{
gchar *entry_buff = (gchar*) g_malloc(MAX_EXPORT_ENTRY_LENGTH);
gchar *entry_line = NULL;
gboolean got_cursor = FALSE;
gboolean got_rt_ts = FALSE;
gboolean got_mt_ts = FALSE;
int line_num;
errno = 0;
for (line_num = 0; line_num < MAX_EXPORT_ENTRY_LINES; line_num++) {
entry_line = file_gets(entry_buff, MAX_EXPORT_ENTRY_LENGTH, wth->fh);
if (!entry_line) {
break;
}
if (entry_line[0] == '\n') {
break;
} else if (strncmp(entry_line, FLD__CURSOR, strlen(FLD__CURSOR)) == 0) {
got_cursor = TRUE;
} else if (strncmp(entry_line, FLD__REALTIME_TIMESTAMP, strlen(FLD__REALTIME_TIMESTAMP)) == 0) {
got_rt_ts = TRUE;
} else if (strncmp(entry_line, FLD__MONOTONIC_TIMESTAMP, strlen(FLD__MONOTONIC_TIMESTAMP)) == 0) {
got_mt_ts = TRUE;
}
}
g_free(entry_buff);
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1) {
return WTAP_OPEN_ERROR;
}
if (!got_cursor || !got_rt_ts || !got_mt_ts) {
return WTAP_OPEN_NOT_MINE;
}
wth->file_type_subtype = systemd_journal_file_type_subtype;
wth->subtype_read = systemd_journal_read;
wth->subtype_seek_read = systemd_journal_seek_read;
wth->file_encap = WTAP_ENCAP_SYSTEMD_JOURNAL;
wth->file_tsprec = WTAP_TSPREC_USEC;
/*
* Add an IDB; we don't know how many interfaces were
* involved, so we just say one interface, about which
* we only know the link-layer type, snapshot length,
* and time stamp resolution.
*/
wtap_add_generated_idb(wth);
return WTAP_OPEN_MINE;
}
/* Read the next packet */
static gboolean systemd_journal_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset)
{
*data_offset = file_tell(wth->fh);
/* Read record. */
if (!systemd_journal_read_export_entry(wth->fh, rec, buf, err, err_info)) {
/* Read error or EOF */
return FALSE;
}
return TRUE;
}
static gboolean
systemd_journal_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info)
{
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return FALSE;
/* Read record. */
if (!systemd_journal_read_export_entry(wth->random_fh, rec, buf, err, err_info)) {
/* Read error or EOF */
if (*err == 0) {
/* EOF means "short read" in random-access mode */
*err = WTAP_ERR_SHORT_READ;
}
return FALSE;
}
return TRUE;
}
static gboolean
systemd_journal_read_export_entry(FILE_T fh, wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
{
size_t fld_end = 0;
gchar *buf_ptr;
gchar *entry_line = NULL;
gboolean got_cursor = FALSE;
gboolean got_rt_ts = FALSE;
gboolean got_mt_ts = FALSE;
gboolean got_double_newline = FALSE;
int line_num;
size_t rt_ts_len = strlen(FLD__REALTIME_TIMESTAMP);
ws_buffer_assure_space(buf, MAX_EXPORT_ENTRY_LENGTH);
buf_ptr = (gchar *) ws_buffer_start_ptr(buf);
for (line_num = 0; line_num < MAX_EXPORT_ENTRY_LINES; line_num++) {
entry_line = file_gets(buf_ptr + fld_end, MAX_EXPORT_ENTRY_LENGTH - (int) fld_end, fh);
if (!entry_line) {
break;
}
fld_end += strlen(entry_line);
if (entry_line[0] == '\n') {
got_double_newline = TRUE;
break;
} else if (strncmp(entry_line, FLD__CURSOR, strlen(FLD__CURSOR)) == 0) {
got_cursor = TRUE;
} else if (strncmp(entry_line, FLD__REALTIME_TIMESTAMP, rt_ts_len) == 0) {
errno = 0;
unsigned long rt_ts = strtoul(entry_line+rt_ts_len, NULL, 10);
if (!errno) {
rec->ts.secs = (time_t) rt_ts / 1000000;
rec->ts.nsecs = (rt_ts % 1000000) * 1000;
rec->tsprec = WTAP_TSPREC_USEC;
got_rt_ts = TRUE;
}
} else if (strncmp(entry_line, FLD__MONOTONIC_TIMESTAMP, strlen(FLD__MONOTONIC_TIMESTAMP)) == 0) {
got_mt_ts = TRUE;
} else if (!strstr(entry_line, "=")) {
// Start of binary data.
if (fld_end >= MAX_EXPORT_ENTRY_LENGTH - 8) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("systemd: binary length too long");
return FALSE;
}
guint64 data_len, le_data_len;
if (!wtap_read_bytes(fh, &le_data_len, 8, err, err_info)) {
return FALSE;
}
memcpy(buf_ptr + fld_end, &le_data_len, 8);
fld_end += 8;
data_len = pletoh64(&le_data_len);
if (data_len < 1 || data_len - 1 >= MAX_EXPORT_ENTRY_LENGTH - fld_end) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("systemd: binary data too long");
return FALSE;
}
// Data + trailing \n
if (!wtap_read_bytes(fh, buf_ptr + fld_end, (unsigned) data_len + 1, err, err_info)) {
return FALSE;
}
fld_end += (size_t) data_len + 1;
}
if (MAX_EXPORT_ENTRY_LENGTH < fld_end + 2) { // \n\0
break;
}
}
if (!got_cursor || !got_rt_ts || !got_mt_ts) {
return FALSE;
}
if (!got_double_newline && !file_eof(fh)) {
return FALSE;
}
rec->rec_type = REC_TYPE_SYSTEMD_JOURNAL_EXPORT;
rec->block = wtap_block_create(WTAP_BLOCK_SYSTEMD_JOURNAL_EXPORT);
rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
rec->rec_header.systemd_journal_export_header.record_len = (guint32) fld_end;
return TRUE;
}
static const struct supported_block_type systemd_journal_blocks_supported[] = {
/*
* We support systemd journal blocks, with no comments or other options.
*/
{ WTAP_BLOCK_SYSTEMD_JOURNAL_EXPORT, MULTIPLE_BLOCKS_SUPPORTED, NO_OPTIONS_SUPPORTED }
};
static const struct file_type_subtype_info systemd_journal_info = {
"systemd journal export", "systemd_journal", NULL, NULL,
FALSE, BLOCKS_SUPPORTED(systemd_journal_blocks_supported),
NULL, NULL, NULL
};
void register_systemd_journal(void)
{
systemd_journal_file_type_subtype = wtap_register_file_type_subtype(&systemd_journal_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("SYSTEMD_JOURNAL",
systemd_journal_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/systemd_journal.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#ifndef __SYSTEMD_JOURNAL_H__
#define __SYSTEMD_JOURNAL_H__
#include <glib.h>
#include "wtap.h"
#include "ws_symbol_export.h"
wtap_open_return_val systemd_journal_open(wtap *wth, int *err, gchar **err_info);
#endif // __SYSTEMD_JOURNAL_H__ |
C | wireshark/wiretap/tnef.c | /* tnef.c
*
* Transport-Neutral Encapsulation Format (TNEF) file reading
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "wtap-int.h"
#include "file_wrappers.h"
#include <wsutil/buffer.h>
#include "tnef.h"
static int tnef_file_type_subtype = -1;
void register_tnef(void);
wtap_open_return_val tnef_open(wtap *wth, int *err, gchar **err_info)
{
guint32 magic;
if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info))
return (*err != WTAP_ERR_SHORT_READ) ? WTAP_OPEN_ERROR : WTAP_OPEN_NOT_MINE;
if (GUINT32_TO_LE(magic) != TNEF_SIGNATURE)
/* Not a tnef file */
return WTAP_OPEN_NOT_MINE;
/* seek back to the start of the file */
if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
return WTAP_OPEN_ERROR;
wth->file_type_subtype = tnef_file_type_subtype;
wth->file_encap = WTAP_ENCAP_TNEF;
wth->snapshot_length = 0;
wth->subtype_read = wtap_full_file_read;
wth->subtype_seek_read = wtap_full_file_seek_read;
wth->file_tsprec = WTAP_TSPREC_SEC;
return WTAP_OPEN_MINE;
}
static const struct supported_block_type tnef_blocks_supported[] = {
/*
* This is a file format that we dissect, so we provide only one
* "packet" with the file's contents, and don't support any
* options.
*/
{ WTAP_BLOCK_PACKET, ONE_BLOCK_SUPPORTED, NO_OPTIONS_SUPPORTED }
};
static const struct file_type_subtype_info tnef_info = {
"Transport-Neutral Encapsulation Format", "tnef", NULL, NULL,
FALSE, BLOCKS_SUPPORTED(tnef_blocks_supported),
NULL, NULL, NULL
};
void register_tnef(void)
{
tnef_file_type_subtype = wtap_register_file_type_subtype(&tnef_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("TNEF",
tnef_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local Variables:
* c-basic-offset: 2
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=2 tabstop=8 expandtab:
* :indentSize=2:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/tnef.h | /** @file
*
* Transport-Neutral Encapsulation Format (TNEF) file reading
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#ifndef __TNEF_H__
#define __TNEF_H__
#include <glib.h>
#include <wiretap/wtap.h>
#include "ws_symbol_export.h"
#define TNEF_SIGNATURE 0x223E9F78
wtap_open_return_val tnef_open(wtap *wth, int *err, gchar **err_info);
#endif |
C/C++ | wireshark/wiretap/toshiba.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __W_TOSHIBA_H__
#define __W_TOSHIBA_H__
#include <glib.h>
#include "wtap.h"
#include "ws_symbol_export.h"
wtap_open_return_val toshiba_open(wtap *wth, int *err, gchar **err_info);
#endif |
C | wireshark/wiretap/visual.c | /* visual.c
* File read and write routines for Visual Networks cap files.
* Copyright (c) 2001, Tom Nisbet [email protected]
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "visual.h"
/*
* A Visual Networks traffic capture file contains three sections. The
* first is a 192 octet file header. This is followed by the captured
* packet header, and for ATM captures, there is an additional atm packet header.
* The data follows the packet header. The last section is the packet index block.
* The index block contains one 4 octet pointer for each captured packet.
* The first packet index is (4 * num_pkts) octets from the end of the file
* and the last index is in the last four octets of the file.
*
* All integer and time values are stored in little-endian format, except for
* the ATM Packet Header, which is stored in network byte order.
*
* [ File Header ]
*
*
* [ Packet Header 1 ] [(opt) ATM Packet Header] [ Data ]
* ...
* [ Packet Header n ] [(opt) ATM Packet Header] [ Data ]
*
*
* [ Index Block 1 ] ... [ Index Block n ]
*/
/* Capture file header, INCLUDING the magic number, is 192 bytes. */
#define CAPTUREFILE_HEADER_SIZE 192
/* Magic number for Visual Networks traffic capture files. */
static const char visual_magic[] = {
5, 'V', 'N', 'F'
};
/* Visual File Header (minus magic number). */
/* This structure is used to extract information */
struct visual_file_hdr
{
guint32 num_pkts; /* Number of packets in the file */
guint32 start_time; /* Capture start time in PC format */
guint16 media_type; /* IANA ifType of packet source */
guint16 max_length; /* Max allowable stored packet length */
guint16 file_flags; /* File type flags */
/* Bit 0 indicates indexes present */
guint16 file_version; /* Version number of this file format */
guint32 media_speed; /* ifSpeed of packet source in bits/sec. */
guint16 media_param; /* Media-specific extra parameter. */
char RESERVED_[102]; /* MUST BE ALL ZEROS FOR FUTURE COMPATIBILITY */
char description[64]; /* File description (null terminated) */
};
/* Packet status bits */
#define PS_LONG 0x01
#define PS_SHORT 0x02
#define PS_ERRORED 0x04
#define PS_1ST_AFTER_DROP 0x08
#define PS_APPROX_ORDER 0x10
#define PS_SENT 0x40
#define PS_ABORTED 0x80
/* Visual Packet Header */
/* This structure is used to extract information */
struct visual_pkt_hdr
{
guint32 ts_delta; /* Time stamp - msecs since start of capture */
guint16 orig_len; /* Actual length of packet */
guint16 incl_len; /* Number of octets captured in file */
guint32 status; /* Packet status flags (media specific) */
guint8 encap_hint; /* Encapsulation type hint */
guint8 encap_skip; /* Number of bytes to skip before decoding */
char RESERVED_[6]; /* RESERVED - must be zero */
};
/* Optional Visual ATM Packet Header */
/* This structure is used to extract information */
struct visual_atm_hdr
{
guint16 vpi; /* 4 bits of zeros; 12 bits of ATM VPI */
guint16 vci; /* ATM VCI */
guint8 info; /* 4 bits version; 3 bits unused-zero; 1 bit direction */
guint8 category; /* indicates type of traffic. 4 bits of status + 4 bits of type */
guint16 cell_count; /* number of cells that make up this pdu */
guint32 data_length; /* PDU data length for AAL-5 PDUs, all others - cellcount * 48 */
guint32 ts_secs; /* seonds value of sysUpTime when the last cell of this PDU was captured */
guint32 ts_nsec; /* nanoseonds value of sysUpTime when the last cell of this PDU was captured */
};
/* visual_atm_hdr info bit definitions */
#define FROM_NETWORK 0x01
#define ATM_VER_MASK 0xf0 /* Not currently displayed */
/* visual_atm_hdr category definitions */
/* High nibble - not currently displayed */
#define VN_INCOMPLETE 0x40
#define VN_BAD_CRC 0x80
#define VN_CAT_STAT_MASK 0xf0
/* Low nibble */
#define VN_UNKNOWN 0x00
#define VN_AAL1 0x01
#define VN_AAL2 0x02
#define VN_AAL34 0x03
#define VN_O191 0x04
#define VN_AAL5 0x05
#define VN_OAM 0x0a
#define VN_RM 0x0b
#define VN_IDLE 0x0c
#define VN_CAT_TYPE_MASK 0x0f
/* Additional information for reading Visual files */
struct visual_read_info
{
guint32 num_pkts; /* Number of pkts in the file */
guint32 current_pkt; /* Next packet to be read */
time_t start_time; /* Capture start time in seconds */
};
/* Additional information for writing Visual files */
struct visual_write_info
{
guint32 start_time; /* Capture start time in seconds */
int index_table_index; /* Index of the next index entry */
int index_table_size; /* Allocated size of the index table */
guint32 * index_table; /* File offsets for the packets */
guint32 next_offset; /* Offset of next packet */
};
/* Local functions to handle file reads and writes */
static gboolean visual_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset);
static gboolean visual_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
static gboolean visual_read_packet(wtap *wth, FILE_T fh,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
static gboolean visual_dump(wtap_dumper *wdh, const wtap_rec *rec,
const guint8 *pd, int *err, gchar **err_info);
static gboolean visual_dump_finish(wtap_dumper *wdh, int *err,
gchar **err_info);
static void visual_dump_free(wtap_dumper *wdh);
static int visual_file_type_subtype = -1;
void register_visual(void);
/* Open a file for reading */
wtap_open_return_val visual_open(wtap *wth, int *err, gchar **err_info)
{
char magic[sizeof visual_magic];
struct visual_file_hdr vfile_hdr;
struct visual_read_info * visual;
int encap;
/* Check the magic string at the start of the file */
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, visual_magic, sizeof visual_magic) != 0)
{
return WTAP_OPEN_NOT_MINE;
}
/* Read the rest of the file header. */
if (!wtap_read_bytes(wth->fh, &vfile_hdr, sizeof vfile_hdr, err, err_info))
{
return WTAP_OPEN_ERROR;
}
/* Verify the file version is known */
vfile_hdr.file_version = pletoh16(&vfile_hdr.file_version);
if (vfile_hdr.file_version != 1)
{
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("visual: file version %u unsupported", vfile_hdr.file_version);
return WTAP_OPEN_ERROR;
}
/* Translate the encapsulation type; these values are SNMP ifType
values, as found in https://www.iana.org/assignments/smi-numbers.
Note that a file with media type 22 ("propPointToPointSerial") may
contain Cisco HDLC or PPP over HDLC. This will get sorted out after
the first packet is read.
XXX - should we use WTAP_ENCAP_PER_PACKET for that? */
switch (pletoh16(&vfile_hdr.media_type))
{
case 6: /* ethernet-csmacd */
encap = WTAP_ENCAP_ETHERNET;
break;
case 9: /* IEEE802.5 */
encap = WTAP_ENCAP_TOKEN_RING;
break;
case 16: /* lapb */
encap = WTAP_ENCAP_LAPB;
break;
case 22: /* propPointToPointSerial */
case 118: /* HDLC */
encap = WTAP_ENCAP_CHDLC_WITH_PHDR;
break;
case 32: /* frame-relay */
encap = WTAP_ENCAP_FRELAY_WITH_PHDR;
break;
case 37: /* ATM */
encap = WTAP_ENCAP_ATM_PDUS;
break;
default:
*err = WTAP_ERR_UNSUPPORTED;
*err_info = ws_strdup_printf("visual: network type %u unknown or unsupported",
vfile_hdr.media_type);
return WTAP_OPEN_ERROR;
}
/* Fill in the wiretap struct with data from the file header */
wth->file_type_subtype = visual_file_type_subtype;
wth->file_encap = encap;
wth->snapshot_length = pletoh16(&vfile_hdr.max_length);
/* Set up the pointers to the handlers for this file type */
wth->subtype_read = visual_read;
wth->subtype_seek_read = visual_seek_read;
wth->file_tsprec = WTAP_TSPREC_MSEC;
/* Add Visual-specific information to the wiretap struct for later use. */
visual = g_new(struct visual_read_info, 1);
wth->priv = (void *)visual;
visual->num_pkts = pletoh32(&vfile_hdr.num_pkts);
visual->start_time = pletoh32(&vfile_hdr.start_time);
visual->current_pkt = 1;
/*
* Add an IDB; we don't know how many interfaces were
* involved, so we just say one interface, about which
* we only know the link-layer type, snapshot length,
* and time stamp resolution.
*/
wtap_add_generated_idb(wth);
return WTAP_OPEN_MINE;
}
/* Read the next available packet from the file. This is called
in a loop to sequentially read the entire file one time. After
the file has been read once, any Future access to the packets is
done through seek_read. */
static gboolean visual_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset)
{
struct visual_read_info *visual = (struct visual_read_info *)wth->priv;
/* Check for the end of the packet data. Note that a check for file EOF
will not work because there are index values stored after the last
packet's data. */
if (visual->current_pkt > visual->num_pkts)
{
*err = 0; /* it's just an EOF, not an error */
return FALSE;
}
visual->current_pkt++;
*data_offset = file_tell(wth->fh);
return visual_read_packet(wth, wth->fh, rec, buf, err, err_info);
}
/* Read packet header and data for random access. */
static gboolean visual_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
{
/* Seek to the packet header */
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return FALSE;
/* Read the packet. */
if (!visual_read_packet(wth, wth->random_fh, rec, buf, err, err_info)) {
if (*err == 0)
*err = WTAP_ERR_SHORT_READ;
return FALSE;
}
return TRUE;
}
static gboolean
visual_read_packet(wtap *wth, FILE_T fh, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info)
{
struct visual_read_info *visual = (struct visual_read_info *)wth->priv;
struct visual_pkt_hdr vpkt_hdr;
guint32 packet_size;
struct visual_atm_hdr vatm_hdr;
guint32 relmsecs;
guint32 packet_status;
guint8 *pd;
/* Read the packet header. */
if (!wtap_read_bytes_or_eof(fh, &vpkt_hdr, (unsigned int)sizeof vpkt_hdr, err, err_info))
{
return FALSE;
}
/* Get the included length of data. This includes extra headers + payload */
packet_size = pletoh16(&vpkt_hdr.incl_len);
rec->rec_type = REC_TYPE_PACKET;
rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
rec->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
/* Set the packet time and length. */
relmsecs = pletoh32(&vpkt_hdr.ts_delta);
rec->ts.secs = visual->start_time + relmsecs/1000;
rec->ts.nsecs = (relmsecs % 1000)*1000000;
rec->rec_header.packet_header.len = pletoh16(&vpkt_hdr.orig_len);
packet_status = pletoh32(&vpkt_hdr.status);
/* Do encapsulation-specific processing.
Most Visual capture types include the FCS in the original length
value, but don't include the FCS as part of the payload or captured
length. This is different from the model used in most other capture
file formats, including pcap and pcapng in cases where the FCS isn't
captured (which are the typical cases), and causes the RTP audio
payload save to fail since then captured len != orig len.
We adjust the original length to remove the FCS bytes we counted based
on the file encapsualtion type. The only downside to this fix is
throughput calculations will be slightly lower as it won't include
the FCS bytes. However, as noted, that problem also exists with
other capture formats.
We also set status flags. The only status currently supported for
all encapsulations is direction. This either goes in the p2p or the
X.25 pseudo header. It would probably be better to move this up
into the phdr. */
switch (wth->file_encap)
{
case WTAP_ENCAP_ETHERNET:
/* Ethernet has a 4-byte FCS. */
if (rec->rec_header.packet_header.len < 4)
{
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("visual: Ethernet packet has %u-byte original packet, less than the FCS length",
rec->rec_header.packet_header.len);
return FALSE;
}
rec->rec_header.packet_header.len -= 4;
/* XXX - the above implies that there's never an FCS; should this
set the FCS length to 0? */
rec->rec_header.packet_header.pseudo_header.eth.fcs_len = -1;
break;
case WTAP_ENCAP_CHDLC_WITH_PHDR:
/* This has a 2-byte FCS. */
if (rec->rec_header.packet_header.len < 2)
{
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("visual: Cisco HDLC packet has %u-byte original packet, less than the FCS length",
rec->rec_header.packet_header.len);
return FALSE;
}
rec->rec_header.packet_header.len -= 2;
rec->rec_header.packet_header.pseudo_header.p2p.sent = (packet_status & PS_SENT) ? TRUE : FALSE;
break;
case WTAP_ENCAP_PPP_WITH_PHDR:
/* No FCS.
XXX - true? Note that PPP can negotiate no FCS, a 2-byte FCS,
or a 4-byte FCS. */
rec->rec_header.packet_header.pseudo_header.p2p.sent = (packet_status & PS_SENT) ? TRUE : FALSE;
break;
case WTAP_ENCAP_FRELAY_WITH_PHDR:
/* This has a 2-byte FCS. */
if (rec->rec_header.packet_header.len < 2)
{
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("visual: Frame Relay packet has %u-byte original packet, less than the FCS length",
rec->rec_header.packet_header.len);
return FALSE;
}
rec->rec_header.packet_header.len -= 2;
rec->rec_header.packet_header.pseudo_header.dte_dce.flags =
(packet_status & PS_SENT) ? 0x00 : FROM_DCE;
break;
case WTAP_ENCAP_LAPB:
/* This has a 2-byte FCS. */
if (rec->rec_header.packet_header.len < 2)
{
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("visual: Frame Relay packet has %u-byte original packet, less than the FCS length",
rec->rec_header.packet_header.len);
return FALSE;
}
rec->rec_header.packet_header.len -= 2;
rec->rec_header.packet_header.pseudo_header.dte_dce.flags =
(packet_status & PS_SENT) ? 0x00 : FROM_DCE;
break;
case WTAP_ENCAP_ATM_PDUS:
/* ATM original length doesn't include any FCS. Do nothing to
the packet length.
ATM packets have an additional packet header; read and
process it. */
if (!wtap_read_bytes(fh, &vatm_hdr, (unsigned int)sizeof vatm_hdr, err, err_info))
{
return FALSE;
}
/* Remove ATM header from length of included bytes in capture, as
this header was appended by the processor doing the packet
reassembly, and was not transmitted across the wire */
packet_size -= (guint32)sizeof vatm_hdr;
/* Set defaults */
rec->rec_header.packet_header.pseudo_header.atm.type = TRAF_UNKNOWN;
rec->rec_header.packet_header.pseudo_header.atm.subtype = TRAF_ST_UNKNOWN;
rec->rec_header.packet_header.pseudo_header.atm.aal5t_len = 0;
/* Next two items not supported. Defaulting to zero */
rec->rec_header.packet_header.pseudo_header.atm.aal5t_u2u = 0;
rec->rec_header.packet_header.pseudo_header.atm.aal5t_chksum = 0;
/* Flags appear only to convey that packet is a raw cell. Set to 0 */
rec->rec_header.packet_header.pseudo_header.atm.flags = 0;
/* Not supported. Defaulting to zero */
rec->rec_header.packet_header.pseudo_header.atm.aal2_cid = 0;
switch(vatm_hdr.category & VN_CAT_TYPE_MASK )
{
case VN_AAL1:
rec->rec_header.packet_header.pseudo_header.atm.aal = AAL_1;
break;
case VN_AAL2:
rec->rec_header.packet_header.pseudo_header.atm.aal = AAL_2;
break;
case VN_AAL34:
rec->rec_header.packet_header.pseudo_header.atm.aal = AAL_3_4;
break;
case VN_AAL5:
rec->rec_header.packet_header.pseudo_header.atm.aal = AAL_5;
rec->rec_header.packet_header.pseudo_header.atm.type = TRAF_LLCMX;
rec->rec_header.packet_header.pseudo_header.atm.aal5t_len = pntoh32(&vatm_hdr.data_length);
break;
case VN_OAM:
/* Marking next 3 as OAM versus unknown */
case VN_O191:
case VN_IDLE:
case VN_RM:
rec->rec_header.packet_header.pseudo_header.atm.aal = AAL_OAMCELL;
break;
case VN_UNKNOWN:
default:
rec->rec_header.packet_header.pseudo_header.atm.aal = AAL_UNKNOWN;
break;
}
rec->rec_header.packet_header.pseudo_header.atm.vpi = pntoh16(&vatm_hdr.vpi) & 0x0FFF;
rec->rec_header.packet_header.pseudo_header.atm.vci = pntoh16(&vatm_hdr.vci);
rec->rec_header.packet_header.pseudo_header.atm.cells = pntoh16(&vatm_hdr.cell_count);
/* Using bit value of 1 (DCE -> DTE) to indicate From Network */
rec->rec_header.packet_header.pseudo_header.atm.channel = vatm_hdr.info & FROM_NETWORK;
break;
/* Not sure about token ring. Just leaving alone for now. */
case WTAP_ENCAP_TOKEN_RING:
default:
break;
}
rec->rec_header.packet_header.caplen = packet_size;
/* Check for too-large packet. */
if (packet_size > WTAP_MAX_PACKET_SIZE_STANDARD)
{
/* Probably a corrupt capture file; don't blow up trying
to allocate space for an immensely-large packet. */
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("visual: File has %u-byte packet, bigger than maximum of %u",
packet_size, WTAP_MAX_PACKET_SIZE_STANDARD);
return FALSE;
}
/* Read the packet data */
if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
return FALSE;
if (wth->file_encap == WTAP_ENCAP_CHDLC_WITH_PHDR)
{
/* Fill in the encapsulation. Visual files have a media type in the
file header and an encapsulation type in each packet header. Files
with a media type of HDLC can be either Cisco EtherType or PPP.
The encapsulation hint values we've seen are:
2 - seen in an Ethernet capture
13 - seen in a PPP capture; possibly also seen in Cisco HDLC
captures
14 - seen in a PPP capture; probably seen only for PPP.
According to bug 2005, the collection probe can be configured
for PPP, in which case the encapsulation hint is 14, or can
be configured for auto-detect, in which case the encapsulation
hint is 13, and the encapsulation must be guessed from the
packet contents. Auto-detect is the default. */
pd = ws_buffer_start_ptr(buf);
/* If PPP is specified in the encap hint, then use that */
if (vpkt_hdr.encap_hint == 14)
{
/* But first we need to examine the first three octets to
try to determine the proper encapsulation, see RFC 2364. */
if (packet_size >= 3 &&
(0xfe == pd[0]) && (0xfe == pd[1]) && (0x03 == pd[2]))
{
/* It is actually LLC encapsulated PPP */
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_ATM_RFC1483;
}
else
{
/* It is actually PPP */
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_PPP_WITH_PHDR;
}
}
else
{
/* Otherwise, we need to examine the first two octets to
try to determine the encapsulation. */
if (packet_size >= 2 && (0xff == pd[0]) && (0x03 == pd[1]))
{
/* It is actually PPP */
rec->rec_header.packet_header.pkt_encap = WTAP_ENCAP_PPP_WITH_PHDR;
}
}
}
return TRUE;
}
/* Check for media types that may be written in Visual file format.
Returns 0 if the specified encapsulation type is supported,
an error indication otherwise. */
static int visual_dump_can_write_encap(int encap)
{
/* Per-packet encapsulations aren't supported. */
if (encap == WTAP_ENCAP_PER_PACKET)
return WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
/* Check for supported encapsulation types */
switch (encap)
{
case WTAP_ENCAP_ETHERNET:
case WTAP_ENCAP_TOKEN_RING:
case WTAP_ENCAP_LAPB:
case WTAP_ENCAP_CHDLC_WITH_PHDR:
case WTAP_ENCAP_FRELAY_WITH_PHDR:
case WTAP_ENCAP_PPP:
case WTAP_ENCAP_PPP_WITH_PHDR:
return 0;
}
return WTAP_ERR_UNWRITABLE_ENCAP;
}
/* Open a file for writing.
Returns TRUE on success, FALSE on failure; sets "*err" to an
error code on failure */
static gboolean visual_dump_open(wtap_dumper *wdh, int *err, gchar **err_info _U_)
{
struct visual_write_info *visual;
/* Set the write routines for a visual file. */
wdh->subtype_write = visual_dump;
wdh->subtype_finish = visual_dump_finish;
/* Create a struct to hold file information for the duration
of the write */
visual = g_new(struct visual_write_info, 1);
wdh->priv = (void *)visual;
visual->index_table_index = 0;
visual->index_table_size = 1024;
visual->index_table = 0;
visual->next_offset = CAPTUREFILE_HEADER_SIZE;
/* All of the fields in the file header aren't known yet so
just skip over it for now. It will be created after all
of the packets have been written. */
if (wtap_dump_file_seek(wdh, CAPTUREFILE_HEADER_SIZE, SEEK_SET, err) == -1)
return FALSE;
return TRUE;
}
/* Write a packet to a Visual dump file.
Returns TRUE on success, FALSE on failure. */
static gboolean visual_dump(wtap_dumper *wdh, const wtap_rec *rec,
const guint8 *pd, int *err, gchar **err_info _U_)
{
const union wtap_pseudo_header *pseudo_header = &rec->rec_header.packet_header.pseudo_header;
struct visual_write_info * visual = (struct visual_write_info *)wdh->priv;
struct visual_pkt_hdr vpkt_hdr = {0};
size_t hdr_size = sizeof vpkt_hdr;
guint delta_msec;
guint32 packet_status;
/* We can only write packet records. */
if (rec->rec_type != REC_TYPE_PACKET) {
*err = WTAP_ERR_UNWRITABLE_REC_TYPE;
return FALSE;
}
/*
* Make sure this packet doesn't have a link-layer type that
* differs from the one for the file.
*/
if (wdh->file_encap != rec->rec_header.packet_header.pkt_encap) {
*err = WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED;
return FALSE;
}
/* Don't write anything we're not willing to read. */
if (rec->rec_header.packet_header.caplen > WTAP_MAX_PACKET_SIZE_STANDARD) {
*err = WTAP_ERR_PACKET_TOO_LARGE;
return FALSE;
}
/* If the visual structure was never allocated then nothing useful
can be done. */
if (visual == 0)
return FALSE;
/* Visual UpTime capture files have a capture start time in the
file header. Each packet has a capture time (in msec) relative
to the file start time. Use the time of the first packet as the
file start time. */
if (visual->index_table_index == 0)
{
/*
* This is the first packet. Save its start time as the file time.
*
* XXX - is the start time signed, or unsigned? If it's signed,
* in which case we should check against G_MININT32 and G_MAXINT32
* and make start_time a gint32.
*/
if (rec->ts.secs < 0 || rec->ts.secs > WTAP_NSTIME_32BIT_SECS_MAX) {
*err = WTAP_ERR_TIME_STAMP_NOT_SUPPORTED;
return FALSE;
}
visual->start_time = (guint32)rec->ts.secs;
/* Initialize the index table */
visual->index_table = (guint32 *)g_malloc(1024 * sizeof *visual->index_table);
visual->index_table_size = 1024;
}
/* Calculate milliseconds since capture start. */
delta_msec = rec->ts.nsecs / 1000000;
delta_msec += (guint32)((rec->ts.secs - visual->start_time) * 1000);
vpkt_hdr.ts_delta = GUINT32_TO_LE(delta_msec);
/* Fill in the length fields. */
vpkt_hdr.orig_len = GUINT16_TO_LE(rec->rec_header.packet_header.len);
vpkt_hdr.incl_len = GUINT16_TO_LE(rec->rec_header.packet_header.caplen);
/* Fill in the encapsulation hint for the file's media type. */
switch (wdh->file_encap)
{
case WTAP_ENCAP_ETHERNET: /* Ethernet */
vpkt_hdr.encap_hint = 2;
break;
case WTAP_ENCAP_TOKEN_RING: /* Token Ring */
vpkt_hdr.encap_hint = 3;
break;
case WTAP_ENCAP_PPP: /* PPP */
case WTAP_ENCAP_PPP_WITH_PHDR:
vpkt_hdr.encap_hint = 14;
break;
case WTAP_ENCAP_CHDLC_WITH_PHDR: /* HDLC Router */
vpkt_hdr.encap_hint = 13;
break;
case WTAP_ENCAP_FRELAY_WITH_PHDR: /* Frame Relay Auto-detect */
vpkt_hdr.encap_hint = 12;
break;
case WTAP_ENCAP_LAPB: /* Unknown */
default:
vpkt_hdr.encap_hint = 1;
break;
}
/* Set status flags. The only status currently supported for all
encapsulations is direction. This either goes in the p2p or the
X.25 pseudo header. It would probably be better to move this up
into the phdr. */
packet_status = 0;
switch (wdh->file_encap)
{
case WTAP_ENCAP_CHDLC_WITH_PHDR:
packet_status |= (pseudo_header->p2p.sent ? PS_SENT : 0x00);
break;
case WTAP_ENCAP_FRELAY_WITH_PHDR:
case WTAP_ENCAP_LAPB:
packet_status |=
((pseudo_header->dte_dce.flags & FROM_DCE) ? 0x00 : PS_SENT);
break;
}
vpkt_hdr.status = GUINT32_TO_LE(packet_status);
/* Write the packet header. */
if (!wtap_dump_file_write(wdh, &vpkt_hdr, hdr_size, err))
return FALSE;
/* Write the packet data */
if (!wtap_dump_file_write(wdh, pd, rec->rec_header.packet_header.caplen, err))
return FALSE;
/* Store the frame offset in the index table. */
if (visual->index_table_index >= visual->index_table_size)
{
/* End of table reached. Reallocate with a larger size */
visual->index_table_size *= 2;
visual->index_table = (guint32 *)g_realloc(visual->index_table,
visual->index_table_size * sizeof *visual->index_table);
}
visual->index_table[visual->index_table_index] = GUINT32_TO_LE(visual->next_offset);
/* Update the table index and offset for the next frame. */
visual->index_table_index++;
visual->next_offset += (guint32) hdr_size + rec->rec_header.packet_header.caplen;
return TRUE;
}
/* Finish writing to a dump file.
Returns TRUE on success, FALSE on failure. */
static gboolean visual_dump_finish(wtap_dumper *wdh, int *err,
gchar **err_info _U_)
{
struct visual_write_info * visual = (struct visual_write_info *)wdh->priv;
size_t n_to_write;
struct visual_file_hdr vfile_hdr = {0};
const char *magicp;
size_t magic_size;
/* If the visual structure was never allocated then nothing useful
can be done. */
if (visual == 0)
return FALSE;
/* Write out the frame table at the end of the file. */
if (visual->index_table)
{
/* Write the index table to the file. */
n_to_write = visual->index_table_index * sizeof *visual->index_table;
if (!wtap_dump_file_write(wdh, visual->index_table, n_to_write, err))
{
visual_dump_free(wdh);
return FALSE;
}
}
/* Write the magic number at the start of the file. */
if (wtap_dump_file_seek(wdh, 0, SEEK_SET, err) == -1)
return FALSE;
magicp = visual_magic;
magic_size = sizeof visual_magic;
if (!wtap_dump_file_write(wdh, magicp, magic_size, err))
{
visual_dump_free(wdh);
return FALSE;
}
vfile_hdr.num_pkts = GUINT32_TO_LE(visual->index_table_index);
vfile_hdr.start_time = GUINT32_TO_LE(visual->start_time);
vfile_hdr.max_length = GUINT16_TO_LE(65535);
vfile_hdr.file_flags = GUINT16_TO_LE(1); /* indexes are present */
vfile_hdr.file_version = GUINT16_TO_LE(1);
(void) g_strlcpy(vfile_hdr.description, "Wireshark file", 64);
/* Translate the encapsulation type */
switch (wdh->file_encap)
{
case WTAP_ENCAP_ETHERNET:
vfile_hdr.media_type = GUINT16_TO_LE(6);
break;
case WTAP_ENCAP_TOKEN_RING:
vfile_hdr.media_type = GUINT16_TO_LE(9);
break;
case WTAP_ENCAP_LAPB:
vfile_hdr.media_type = GUINT16_TO_LE(16);
break;
case WTAP_ENCAP_PPP: /* PPP is differentiated from CHDLC in PktHdr */
case WTAP_ENCAP_PPP_WITH_PHDR:
case WTAP_ENCAP_CHDLC_WITH_PHDR:
vfile_hdr.media_type = GUINT16_TO_LE(22);
break;
case WTAP_ENCAP_FRELAY_WITH_PHDR:
vfile_hdr.media_type = GUINT16_TO_LE(32);
break;
}
/* Write the file header following the magic bytes. */
if (!wtap_dump_file_write(wdh, &vfile_hdr, sizeof vfile_hdr, err))
{
visual_dump_free(wdh);
return FALSE;
}
/* Deallocate the file write data */
visual_dump_free(wdh);
return TRUE;
}
/* Free the memory allocated by a visual file writer. */
static void visual_dump_free(wtap_dumper *wdh)
{
struct visual_write_info * visual = (struct visual_write_info *)wdh->priv;
if (visual)
{
/* Free the index table memory. */
g_free(visual->index_table);
}
}
static const struct supported_block_type visual_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 visual_info = {
"Visual Networks traffic capture", "visual", NULL, NULL,
TRUE, BLOCKS_SUPPORTED(visual_blocks_supported),
visual_dump_can_write_encap, visual_dump_open, NULL
};
void register_visual(void)
{
visual_file_type_subtype = wtap_register_file_type_subtype(&visual_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("VISUAL_NETWORKS",
visual_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/visual.h | /** @file
*
* File read write routines for Visual Networks .cap files.
* Copyright 2001, Tom Nisbet [email protected]
*
* Based on the code that handles netmon files.
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __VISUAL_H__
#define __VISUAL_H__
#include <glib.h>
#include "wtap.h"
#include "ws_symbol_export.h"
wtap_open_return_val visual_open(wtap *wth, int *err, gchar **err_info);
#endif |
C | wireshark/wiretap/vms.c | /* vms.c
*
* Wiretap Library
* Copyright (c) 2001 by Marc Milgram <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/* Notes:
* TCPIPtrace TCP fragments don't have the header line. So, we are never
* to look for that line for the first line of a packet except the first
* packet. This allows us to read fragmented packets. Define
* TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE to expect the first line to be
* at the start of every packet.
*/
#include "config.h"
#include "wtap-int.h"
#include "vms.h"
#include "file_wrappers.h"
#include <wsutil/strtoi.h>
#include <stdlib.h>
#include <string.h>
/* This module reads the output of the various VMS TCPIP trace utilities
* such as TCPIPTRACE, TCPTRACE and UCX$TRACE
*
* It was initially based on toshiba.c and refined with code from cosine.c
--------------------------------------------------------------------------------
Example TCPIPTRACE TCPTRACE output data:
TCPIPtrace full display RCV packet 8 at 10-JUL-2001 14:54:19.56
IP Version = 4, IHL = 5, TOS = 00, Total Length = 84 = ^x0054
IP Identifier = ^x178F, Flags (0=0,DF=0,MF=0),
Fragment Offset = 0 = ^x0000, Calculated Offset = 0 = ^x0000
IP TTL = 64 = ^x40, Protocol = 17 = ^x11, Header Checksum = ^x4C71
IP Source Address = 10.12.1.80
IP Destination Address = 10.12.1.50
UDP Source Port = 731, UDP Destination Port = 111
UDP Header and Datagram Length = 64 = ^x0040, Checksum = ^xB6C0
50010C0A 714C1140 00008F17 54000045 0000 [email protected]
27E54C3C | C0B64000 6F00DB02 | 32010C0A 0010 ...2...o.@..<L.'
02000000 A0860100 02000000 00000000 0020 ................
00000000 00000000 00000000 03000000 0030 ................
06000000 01000000 A5860100 00000000 0040 ................
00000000 0050 ....
--------------------------------------------------------------------------------
Example UCX$TRACE output data:
UCX INTERnet trace RCV packet seq # = 1 at 14-MAY-2003 11:32:10.93
IP Version = 4, IHL = 5, TOS = 00, Total Length = 583 = ^x0247
IP Identifier = ^x702E, Flags (0=0,DF=0,MF=0),
Fragment Offset = 0 = ^x0000, Calculated Offset = 0 = ^x0000
IP TTL = 128 = ^x80, Protocol = 17 = ^x11, Header Checksum = ^x70EC
IP Source Address = 10.20.4.159
IP Destination Address = 10.20.4.255
UDP Source Port = 138, UDP Destination Port = 138
UDP Header and Datagram Length = 563 = ^x0233, Checksum = ^xB913
9F04140A 70EC1180 0000702E 47020045 0000 E..G.p.....p....
B1B80E11 | B9133302 8A008A00 | FF04140A 0010 .........3......
46484648 45200000 1D028A00 9F04140A 0020 ...........EHFHF
43414341 4341434D 454D4546 45454550 0030 PEEEFEMEMCACACAC
--------------------------------------------------------------------------------
Alternate UCX$TRACE type output data:
TCPIP INTERnet trace RCV packet seq # = 1 at 23-OCT-1998 15:19:33.29
IP Version = 4, IHL = 5, TOS = 00, Total Length = 217 = ^x00D9
IP Identifier = ^x0065, Flags (0=0,DF=0,MF=0),
Fragment Offset = 0 = ^x0000, Calculated Offset = 0 = ^x0000
IP TTL = 32 = ^x20, Protocol = 17 = ^x11, Header Checksum = ^x8F6C
IP Source Address = 16.20.168.93
IP Destination Address = 16.20.255.255
UDP Source Port = 138, UDP Destination Port = 138
UDP Header and Datagram Length = 197 = ^x00C5, Checksum = ^x0E77
5DA81410 8F6C1120 00000065 D9000045 0000 E...awe.....l....]
| 0E77C500 8A008A00 | FFFF1410 0010 ..........w.
--------------------------------------------------------------------------------
The only difference between the utilities is the Packet header line, primarily
the utility identifier and the packet sequence formats.
There appear to be 2 formats for packet sequencing
Format 1:
... packet nn at DD-MMM-YYYY hh:mm:ss.ss
Format 2:
... packet seq # = nn at DD-MMM-YYYY hh:mm:ss.ss
If there are other formats then code will have to be written in parse_vms_packet()
to handle them.
--------------------------------------------------------------------------------
*/
/* Magic text to check for VMS-ness of file using possible utility names
*
*/
#define VMS_HDR_MAGIC_STR1 "TCPIPtrace"
#define VMS_HDR_MAGIC_STR2 "TCPtrace"
#define VMS_HDR_MAGIC_STR3 "INTERnet trace"
/* Magic text for start of packet */
#define VMS_REC_MAGIC_STR1 VMS_HDR_MAGIC_STR1
#define VMS_REC_MAGIC_STR2 VMS_HDR_MAGIC_STR2
#define VMS_REC_MAGIC_STR3 VMS_HDR_MAGIC_STR3
#define VMS_HEADER_LINES_TO_CHECK 200
#define VMS_LINE_LENGTH 240
static gboolean vms_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset);
static gboolean vms_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf,
long byte_offset, int in_off, int remaining_bytes);
static gboolean parse_vms_packet(FILE_T fh, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info);
static int vms_file_type_subtype = -1;
void register_vms(void);
#ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
/* Seeks to the beginning of the next packet, and returns the
byte offset. Returns -1 on failure, and sets "*err" to the error
and sets "*err_info" to null or an additional error string. */
static long vms_seek_next_packet(wtap *wth, int *err, gchar **err_info)
{
long cur_off;
char buf[VMS_LINE_LENGTH];
while (1) {
cur_off = file_tell(wth->fh);
if (cur_off == -1) {
/* Error */
*err = file_error(wth->fh, err_info);
return -1;
}
if (file_gets(buf, sizeof(buf), wth->fh) == NULL) {
/* EOF or error. */
*err = file_error(wth->fh, err_info);
break;
}
if (strstr(buf, VMS_REC_MAGIC_STR1) ||
strstr(buf, VMS_REC_MAGIC_STR2) ||
strstr(buf, VMS_REC_MAGIC_STR2)) {
(void) g_strlcpy(hdr, buf,VMS_LINE_LENGTH);
return cur_off;
}
}
return -1;
}
#endif /* TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE */
/* Look through the first part of a file to see if this is
* a VMS trace file.
*
* Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
* if we get an I/O error, "*err" will be set to a non-zero value and
* "*err_info will be set to null or an additional error string.
*
* Leaves file handle at beginning of line that contains the VMS Magic
* identifier.
*/
static gboolean vms_check_file_type(wtap *wth, int *err, gchar **err_info)
{
char buf[VMS_LINE_LENGTH];
guint reclen, line;
gint64 mpos;
buf[VMS_LINE_LENGTH-1] = '\0';
for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) {
mpos = file_tell(wth->fh);
if (mpos == -1) {
/* Error. */
*err = file_error(wth->fh, err_info);
return FALSE;
}
if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) == NULL) {
/* EOF or error. */
*err = file_error(wth->fh, err_info);
return FALSE;
}
reclen = (guint) strlen(buf);
if (reclen < strlen(VMS_HDR_MAGIC_STR1) ||
reclen < strlen(VMS_HDR_MAGIC_STR2) ||
reclen < strlen(VMS_HDR_MAGIC_STR3)) {
continue;
}
if (strstr(buf, VMS_HDR_MAGIC_STR1) ||
strstr(buf, VMS_HDR_MAGIC_STR2) ||
strstr(buf, VMS_HDR_MAGIC_STR3)) {
/* Go back to the beginning of this line, so we will
* re-read it. */
if (file_seek(wth->fh, mpos, SEEK_SET, err) == -1) {
/* Error. */
return FALSE;
}
return TRUE;
}
}
*err = 0;
return FALSE;
}
wtap_open_return_val vms_open(wtap *wth, int *err, gchar **err_info)
{
/* Look for VMS header */
if (!vms_check_file_type(wth, err, err_info)) {
if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
return WTAP_OPEN_ERROR;
return WTAP_OPEN_NOT_MINE;
}
wth->file_encap = WTAP_ENCAP_RAW_IP;
wth->file_type_subtype = vms_file_type_subtype;
wth->snapshot_length = 0; /* not known */
wth->subtype_read = vms_read;
wth->subtype_seek_read = vms_seek_read;
wth->file_tsprec = WTAP_TSPREC_CSEC;
/*
* Add an IDB; we don't know how many interfaces were
* involved, so we just say one interface, about which
* we only know the link-layer type, snapshot length,
* and time stamp resolution.
*/
wtap_add_generated_idb(wth);
return WTAP_OPEN_MINE;
}
/* Find the next packet and parse it; called from wtap_read(). */
static gboolean vms_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset)
{
gint64 offset = 0;
/* Find the next packet */
#ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
offset = vms_seek_next_packet(wth, err, err_info);
#else
offset = file_tell(wth->fh);
#endif
if (offset < 1) {
*err = file_error(wth->fh, err_info);
return FALSE;
}
*data_offset = offset;
/* Parse the packet */
return parse_vms_packet(wth->fh, rec, buf, err, err_info);
}
/* Used to read packets in random-access fashion */
static gboolean
vms_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info)
{
if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
return FALSE;
if (!parse_vms_packet(wth->random_fh, rec, buf, err, err_info)) {
if (*err == 0)
*err = WTAP_ERR_SHORT_READ;
return FALSE;
}
return TRUE;
}
/* isdumpline assumes that dump lines start with some non-alphanumerics
* followed by 4 hex numbers - each 8 digits long, each hex number followed
* by 3 spaces.
*/
static int
isdumpline( gchar *line )
{
int i, j;
while (*line && !g_ascii_isalnum(*line))
line++;
for (j=0; j<4; j++) {
for (i=0; i<8; i++, line++)
if (! g_ascii_isxdigit(*line))
return FALSE;
for (i=0; i<3; i++, line++)
if (*line != ' ')
return FALSE;
}
return g_ascii_isspace(*line);
}
/* Parses a packet record. */
static gboolean
parse_vms_packet(FILE_T fh, wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
{
char line[VMS_LINE_LENGTH + 1];
int num_items_scanned;
gboolean have_pkt_len = FALSE;
guint32 pkt_len = 0;
int pktnum;
int csec = 101;
struct tm tm;
char mon[4] = {'J', 'A', 'N', 0};
gchar *p;
const gchar *endp;
static const gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
guint32 i;
int offset = 0;
guint8 *pd;
tm.tm_year = 1970;
tm.tm_mon = 0;
tm.tm_mday = 1;
tm.tm_hour = 1;
tm.tm_min = 1;
tm.tm_sec = 1;
/* Skip lines until one starts with a hex number */
do {
if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
*err = file_error(fh, err_info);
if ((*err == 0) && (csec != 101)) {
*err = WTAP_ERR_SHORT_READ;
}
return FALSE;
}
line[VMS_LINE_LENGTH] = '\0';
if ((csec == 101) && (p = strstr(line, "packet ")) != NULL
&& (! strstr(line, "could not save "))) {
/* Find text in line starting with "packet ". */
/* First look for the Format 1 type sequencing */
num_items_scanned = sscanf(p,
"packet %9d at %2d-%3s-%4d %2d:%2d:%2d.%9d",
&pktnum, &tm.tm_mday, mon,
&tm.tm_year, &tm.tm_hour,
&tm.tm_min, &tm.tm_sec, &csec);
/* Next look for the Format 2 type sequencing */
if (num_items_scanned != 8) {
num_items_scanned = sscanf(p,
"packet seq # = %9d at %2d-%3s-%4d %2d:%2d:%2d.%9d",
&pktnum, &tm.tm_mday, mon,
&tm.tm_year, &tm.tm_hour,
&tm.tm_min, &tm.tm_sec, &csec);
}
/* if unknown format then exit with error */
/* We will need to add code to handle new format */
if (num_items_scanned != 8) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("vms: header line not valid");
return FALSE;
}
}
if ( (! have_pkt_len) && (p = strstr(line, "Length "))) {
p += sizeof("Length ");
while (*p && ! g_ascii_isdigit(*p))
p++;
if ( !*p ) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("vms: Length field not valid");
return FALSE;
}
if (!ws_strtou32(p, &endp, &pkt_len) || (*endp != '\0' && !g_ascii_isspace(*endp))) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("vms: Length field '%s' not valid", p);
return FALSE;
}
have_pkt_len = TRUE;
break;
}
} while (! isdumpline(line));
if (! have_pkt_len) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("vms: Length field not found");
return FALSE;
}
if (pkt_len > WTAP_MAX_PACKET_SIZE_STANDARD) {
/*
* Probably a corrupt capture file; return an error,
* so that our caller doesn't blow up trying to allocate
* space for an immensely-large packet.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("vms: File has %u-byte packet, bigger than maximum of %u",
pkt_len, WTAP_MAX_PACKET_SIZE_STANDARD);
return FALSE;
}
p = strstr(months, mon);
if (p)
tm.tm_mon = (int) (p - months) / 3;
tm.tm_year -= 1900;
tm.tm_isdst = -1;
rec->rec_type = REC_TYPE_PACKET;
rec->block = wtap_block_create(WTAP_BLOCK_PACKET);
rec->presence_flags = WTAP_HAS_TS;
rec->ts.secs = mktime(&tm);
rec->ts.nsecs = csec * 10000000;
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(buf, pkt_len);
pd = ws_buffer_start_ptr(buf);
/* Convert the ASCII hex dump to binary data */
for (i = 0; i < pkt_len; i += 16) {
if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
*err = file_error(fh, err_info);
if (*err == 0) {
*err = WTAP_ERR_SHORT_READ;
}
return FALSE;
}
line[VMS_LINE_LENGTH] = '\0';
if (i == 0) {
while (! isdumpline(line)) { /* advance to start of hex data */
if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
*err = file_error(fh, err_info);
if (*err == 0) {
*err = WTAP_ERR_SHORT_READ;
}
return FALSE;
}
line[VMS_LINE_LENGTH] = '\0';
}
while (line[offset] && !g_ascii_isxdigit(line[offset]))
offset++;
}
if (!parse_single_hex_dump_line(line, pd, i,
offset, pkt_len - i)) {
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup("vms: hex dump not valid");
return FALSE;
}
}
/* Avoid TCPIPTRACE-W-BUFFERSFUL, TCPIPtrace could not save n packets.
* errors.
*
* XXX - when we support packet drop report information in the
* Wiretap API, we should parse those lines and return "n" as
* a packet drop count. */
if (!file_gets(line, VMS_LINE_LENGTH, fh)) {
*err = file_error(fh, err_info);
if (*err == 0) {
/* There is no next line, so there's no "TCPIPtrace could not
* save n packets" line; not an error. */
return TRUE;
}
return FALSE;
}
return TRUE;
}
/*
1 2 3 4
0123456789012345678901234567890123456789012345
50010C0A A34C0640 00009017 2C000045 0000 E..,[email protected]
00000000 14945E52 0A00DC02 | 32010C0A 0010 ...2....R^......
0000 | B4050402 00003496 00020260 0020 `....4........
*/
#define START_POS 7
#define HEX_LENGTH ((8 * 4) + 7) /* eight clumps of 4 bytes with 7 inner spaces */
/* Take a string representing one line from a hex dump and converts the
* text to binary data. We check the printed offset with the offset
* we are passed to validate the record. We place the bytes in the buffer
* at the specified offset.
*
* Returns TRUE if good hex dump, FALSE if bad.
*/
static gboolean
parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset,
int in_off, int remaining_bytes) {
int i;
char *s;
int value;
static const int offsets[16] = {39,37,35,33,28,26,24,22,17,15,13,11,6,4,2,0};
char lbuf[3] = {0,0,0};
/* Get the byte_offset directly from the record */
s = rec;
value = (int)strtoul(s + 45 + in_off, NULL, 16); /* XXX - error check? */
if (value != byte_offset) {
return FALSE;
}
if (remaining_bytes > 16)
remaining_bytes = 16;
/* Read the octets right to left, as that is how they are displayed
* in VMS.
*/
for (i = 0; i < remaining_bytes; i++) {
lbuf[0] = rec[offsets[i] + in_off];
lbuf[1] = rec[offsets[i] + 1 + in_off];
buf[byte_offset + i] = (guint8) strtoul(lbuf, NULL, 16);
}
return TRUE;
}
static const struct supported_block_type vms_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 vms_info = {
"TCPIPtrace (VMS)", "tcpiptrace", "txt", NULL,
FALSE, BLOCKS_SUPPORTED(vms_blocks_supported),
NULL, NULL, NULL
};
void register_vms(void)
{
vms_file_type_subtype = wtap_register_file_type_subtype(&vms_info);
/*
* Register name for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("VMS",
vms_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/vms.h | /** @file
*
* Wiretap Library
* Copyright (c) 2001 by Marc Milgram <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#ifndef __W_VMS_H__
#define __W_VMS_H__
#include <glib.h>
#include "wtap.h"
#include "ws_symbol_export.h"
wtap_open_return_val vms_open(wtap *wth, int *err, gchar **err_info);
#endif |
C | wireshark/wiretap/vwr.c | /* vwr.c
* Copyright (c) 2011 by Tom Alexander <[email protected]>
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#include "config.h"
#include <string.h>
#include "wtap-int.h"
#include "file_wrappers.h"
#include "vwr.h"
#include <wsutil/ws_assert.h>
/* platform-specific definitions for portability */
/* unsigned long long constants */
# define NS_IN_US G_GUINT64_CONSTANT(1000) /* nanoseconds-to-microseconds */
# define NS_IN_SEC G_GUINT64_CONSTANT(1000000000) /* nanoseconds-to-seconds */
# define US_IN_SEC G_GUINT64_CONSTANT(1000000) /* microseconds-to-seconds */
# define LL_ZERO G_GUINT64_CONSTANT(0) /* zero in unsigned long long */
/*
* Fetch a 64-bit value in "Corey-endian" form.
*/
#define pcoreytohll(p) ((guint64)*((const guint8 *)(p)+4)<<56| \
(guint64)*((const guint8 *)(p)+5)<<48| \
(guint64)*((const guint8 *)(p)+6)<<40| \
(guint64)*((const guint8 *)(p)+7)<<32| \
(guint64)*((const guint8 *)(p)+0)<<24| \
(guint64)*((const guint8 *)(p)+1)<<16| \
(guint64)*((const guint8 *)(p)+2)<<8| \
(guint64)*((const guint8 *)(p)+3)<<0)
/*
* Fetch a 48-bit value in "Corey-endian" form; it's stored as
* a 64-bit Corey-endian value, with the upper 16 bits ignored.
*/
#define pcorey48tohll(p) ((guint64)*((const guint8 *)(p)+6)<<40| \
(guint64)*((const guint8 *)(p)+7)<<32| \
(guint64)*((const guint8 *)(p)+0)<<24| \
(guint64)*((const guint8 *)(p)+1)<<16| \
(guint64)*((const guint8 *)(p)+2)<<8| \
(guint64)*((const guint8 *)(p)+3)<<0)
/* .vwr log file defines */
#define B_SIZE 32768 /* max var len message = 32 kB */
#define VT_FRAME 0 /* varlen msg is a frame */
#define VT_CPMSG 1 /* varlen msg is a CP<->PP msg */
#define VT_UNKNOWN -1 /* varlen msg is unknown */
#define MAX_TRACKED_CLIENTS 1024 /* track 1024 clients */
#define MAX_TRACKED_FLOWS 65536 /* and 64K flows */
/*
* The file consists of a sequence of records.
* A record begins with a 16-byte header, the first 8 bytes of which
* begin with a byte containing a command plus transmit-receive flags.
*
* Following that are two big-endian 32-bit quantities; for some records
* one or the other of them is the length of the rest of the record.
* Other records contain only the header.
*/
#define VW_RECORD_HEADER_LENGTH 16
/*
* Maximum number of bytes to read looking for a valid frame starting
* with a command byte to determine if this is our file type. Arbitrary.
*/
#define VW_BYTES_TO_CHECK 0x3FFFFFFFU
/* Command byte values */
#define COMMAND_RX 0x21
#define COMMAND_TX 0x31
#define COMMAND_RFN 0x30
#define COMMAND_RF 0x38
#define COMMAND_RFRX 0x39
/*
* The data in packet records begins with a sequence of metadata headers.
*
* For packet records from FPGA versions < 48:
*
* The first header is the IxVeriWave common header, and that's
* followed either by a WLAN metadata header or an Ethernet
* metadata header. The port type field indicates whether it's
* a WLAN packet or an Ethernet packet. Following that may, for
* WLAN, be 1 octet of information from the FPGA and 16 bytes of
* data including the PLCP header. After that comes the WLAN or
* Ethernet frame, beginning with the MAC header.
*
* For packet records from FPGA versions >= 48:
*
* The first header contains only a 1-octet port type value, which
* has a packet type value in the upper 4 bits and zero in the lower
* 4 bits. NOTE: this is indistinguishable from an old FPGA header
* if the packet type value is 0.
*
* If the packet type value isn't 3, the port type value is followed
* by a 1-octet FPGA version number, which is followed by a timestamp
* header.
*
* If the packet type value is 3 or 4, the next item is an RF metadata
* header. For type 3, that immediately follows the port number octet,
* otherwise it immediately follows the timestamp header.
*
* If the packet type isn't 3, the next item is a WLAN metadata header,
* in a format different from the WLAN metadata header for FPGA versions
* < 48. That is followed by a PLCP header, which is followed by a
* header giving additional layer 2 through 4 metadata.
*
* Following those headers is the WLAN or Ethernet frame, beginning with
* the MAC header.
*/
/*
* IxVeriWave common header:
*
* 1 octet - port type
* 1 octet - FPGA version, or 0
* 2 octets - length of the common header
* 2 octets - MSDU length
* 4 octets - flow ID
* 2 octets - VC ID
* 2 octets - flow sequence number
* 4 octets - latency or 0
* 4 octets - lower 32 bits of signature time stamp
* 8 octets - start time
* 8 octets - end time
* 4 octets - delta(?) time
*/
/* Size of the IxVeriWave common header */
#define STATS_COMMON_FIELDS_LEN (1+1+2+2+4+2+2+4+4+8+8+4)
/* Port type */
#define WLAN_PORT 0
#define ETHERNET_PORT 1
/* For VeriWave WLAN and Ethernet metadata headers vw_flags field */
#define VW_FLAGS_TXF 0x01 /* frame was transmitted */
#define VW_FLAGS_FCSERR 0x02 /* FCS error detected */
/*
* VeriWave WLAN metadata header:
*
* 2 octets - header length
* 2 octets - rflags
* 2 octets - channel flags
* 2 octets - PHY rate
* 1 octet - PLCP type
* 1 octet - MCS index
* 1 octet - number of spatial streams
* 1 octet - RSSI
* 1 octet - antenna b signal power, or 100 if missing
* 1 octet - antenna c signal power, or 100 if missing
* 1 octet - antenna d signal power, or 100 if missing
* 1 octet - padding
* 2 octets - VeriWave flags
* 2 octets - HT len
* 2 octets - info
* 2 octets - errors
*/
/* Size of the VeriWave WLAN metadata header */
#define EXT_WLAN_FIELDS_LEN (2+2+2+2+1+1+1+1+1+1+1+1+2+2+2+4)
/* Flags, for rflags field */
#define FLAGS_SHORTPRE 0x0002 /* sent/received with short preamble */
#define FLAGS_WEP 0x0004 /* sent/received with WEP encryption */
#define FLAGS_CHAN_HT 0x0040 /* In HT mode */
#define FLAGS_CHAN_VHT 0x0080 /* VHT Mode */
#define FLAGS_CHAN_SHORTGI 0x0100 /* Short guard interval */
#define FLAGS_CHAN_40MHZ 0x0200 /* 40 Mhz channel bandwidth */
#define FLAGS_CHAN_80MHZ 0x0400 /* 80 Mhz channel bandwidth */
#define FLAGS_CHAN_160MHZ 0x0800 /* 160 Mhz channel bandwidth */
/* Channel flags, for channel flags field */
#define CHAN_CCK 0x0020 /* CCK channel */
#define CHAN_OFDM 0x0040 /* OFDM channel */
/* For VeriWave WLAN metadata header vw_flags field */
#define VW_FLAGS_RETRERR 0x04 /* excess retry error detected */
#define VW_FLAGS_DCRERR 0x10 /* decrypt error detected (WLAN) */
#define VW_FLAGS_ENCMSK 0x60 /* encryption type mask */
/* 0 = none, 1 = WEP, 2 = TKIP, 3 = CCKM */
#define VW_FLAGS_IS_WEP 0x20 /* WEP */
#define VW_FLAGS_IS_TKIP 0x40 /* TKIP */
#define VW_FLAGS_IS_CCMP 0x60 /* CCMP */
/*
* VeriWave Ethernet metadata header:
*
* 2 octets - header length
* 2 octets - VeriWave flags
* 2 octets - info
* 4 octets - errors
* 4 octets - layer 4 ID
* 4 octets - pad
*
* Ethernet frame follows, beginning with the MAC header
*/
/* Size of the VeriWave Ethernet metadata header */
#define EXT_ETHERNET_FIELDS_LEN (2+2+2+4+4+4)
/*
* OCTO timestamp header.
*
* 4 octets - latency or 0
* 4 octets - lower 32 bits of signature time stamp
* 8 octets - start time
* 8 octets - end time
* 4 octets - delta(?) time
*/
/* Size of Timestamp header */
#define OCTO_TIMESTAMP_FIELDS_LEN (4+4+8+8+4+4)
/*
* OCTO layer 1-4 header:
*
* 2 octets - header length
* 1 octet - l1p_1
* 1 octet - number of spatial streams
* 2 octets - PHY rate
* 1 octet - l1p_2
* 1 octet - RSSI
* 1 octet - antenna b signal power, or 100 if missing
* 1 octet - antenna c signal power, or 100 if missing
* 1 octet - antenna d signal power, or 100 if missing
* 1 octet - signal bandwidth mask
* 1 octet - antenna port energy detect and VU_MASK
* 1 octet - L1InfoC or 0
* 2 octets - MSDU length
* 16 octets - PLCP?
* 4 octets - BM, BV, CV, BSSID and ClientID
* 2 octets - FV, QT, HT, L4V, TID and WLAN type
* 1 octets - flow sequence number
* 3 octets - flow ID
* 2 octets - layer 4 ID
* 4 octets - payload decode
* 3 octets - info
* 4 octets - errors
*/
/* Size of Layer-1, PLCP, and Layer-2/4 header in case of OCTO version FPGA */
#define OCTO_LAYER1TO4_LEN (2+14+16+23)
/*
* OCTO modified RF layer:
*
* 1 octet - RF ID
* 3 octets - unused (zero)
* 8 octets - noise for 4 ports
* 8 octets - signal/noise ration for 4 ports
* 8 octets - PFE for 4 ports
* 8 octets - EVM SIG data for 4 ports
* 8 octets - EVM SIG pilot for 4 ports
* 8 octets - EVM Data data for 4 ports
* 8 octets - EVM Data pilot for 4 ports
* 8 octets - EVM worst symbol for 4 ports
* 8 octets - CONTEXT_P for 4 ports
*
* Not supplied:
* 24 octets of additional data
*/
/* Size of RF header, if all fields were supplied */
#define OCTO_RF_MOD_ACTUAL_LEN 100 /* */
/* Size of RF header with the fields we do supply */
#define OCTO_MODIFIED_RF_LEN 76 /* 24 bytes of RF are not displayed*/
/*Offset of different parameters of RF header for port-1*/
#define RF_PORT_1_NOISE_OFF 4
#define RF_PORT_1_SNR_OFF 6
#define RF_PORT_1_PFE_OFF 8
#define RF_PORT_1_CONTEXT_OFF 10
#define RF_PORT_1_EVM_SD_SIG_OFF 12
#define RF_PORT_1_EVM_SP_SIG_OFF 14
#define RF_PORT_1_EVM_SD_DATA_OFF 16
#define RF_PORT_1_EVM_SP_DATA_OFF 18
#define RF_PORT_1_DSYMBOL_IDX_OFF 22
#define RF_INTER_PORT_GAP_OFF 24 /*As size of RF information per port is 24 bytes*/
#define RF_NUMBER_OF_PORTS 4
/* FPGA-generated frame buffer STATS block offsets and definitions */
/* definitions for v2.2 frames, Ethernet format */
#define v22_E_STATS_LEN 44 /* length of stats block trailer */
#define v22_E_VALID_OFF 0 /* bit 6 (0x40) is flow-is-valid flag */
#define v22_E_MTYPE_OFF 1 /* offset of modulation type */
#define v22_E_VCID_OFF 2 /* offset of VC ID */
#define v22_E_FLOWSEQ_OFF 4 /* offset of signature sequence number */
#define v22_E_FLOWID_OFF 5 /* offset of flow ID */
#define v22_E_OCTET_OFF 8 /* offset of octets */
#define v22_E_ERRORS_OFF 10 /* offset of error vector */
#define v22_E_PATN_OFF 12 /* offset of pattern match vector */
#define v22_E_L4ID_OFF 12
#define v22_E_IPLEN_OFF 14
#define v22_E_FRAME_TYPE_OFF 16 /* offset of frame type, 32 bits */
#define v22_E_RSSI_OFF 21 /* RSSI (NOTE: invalid for Ethernet) */
#define v22_E_STARTT_OFF 20 /* offset of start time, 64 bits */
#define v22_E_ENDT_OFF 28 /* offset of end time, 64 bits */
#define v22_E_LATVAL_OFF 36 /* offset of latency, 32 bits */
#define v22_E_INFO_OFF 40 /* NO INFO FIELD IN ETHERNET STATS! */
#define v22_E_DIFFERENTIATOR_OFF 0 /* offset to determine whether */
/* eth/802.11, 8 bits */
/* Media types */
#define v22_E_MT_10_HALF 0 /* 10 Mb/s half-duplex */
#define v22_E_MT_10_FULL 1 /* 10 Mb/s full-duplex */
#define v22_E_MT_100_HALF 2 /* 100 Mb/s half-duplex */
#define v22_E_MT_100_FULL 3 /* 100 Mb/s full-duplex */
#define v22_E_MT_1G_HALF 4 /* 1 Gb/s half-duplex */
#define v22_E_MT_1G_FULL 5 /* 1 Gb/s full-duplex */
/* Error flags */
#define v22_E_FCS_ERROR 0x0002 /* FCS error flag in error vector */
#define v22_E_CRYPTO_ERR 0x1f00 /* RX decrypt error flags (UNUSED) */
#define v22_E_SIG_ERR 0x0004 /* signature magic byte mismatch */
#define v22_E_PAYCHK_ERR 0x0008 /* payload checksum failure */
#define v22_E_RETRY_ERR 0x0400 /* excessive retries on TX fail (UNUSED)*/
/* Masks and defines */
#define v22_E_IS_RX 0x08 /* TX/RX bit in STATS block */
#define v22_E_MT_MASK 0x07 /* modulation type mask (UNUSED) */
#define v22_E_VCID_MASK 0x03ff /* VC ID is only 10 bits */
#define v22_E_FLOW_VALID 0x40 /* flow-is-valid flag (else force to 0) */
#define v22_E_DIFFERENTIATOR_MASK 0x3F /* mask to differentiate ethernet from */
/* Bits in FRAME_TYPE field */
#define v22_E_IS_TCP 0x00000040 /* TCP bit in FRAME_TYPE field */
#define v22_E_IS_UDP 0x00000010 /* UDP bit in FRAME_TYPE field */
#define v22_E_IS_ICMP 0x00000020 /* ICMP bit in FRAME_TYPE field */
#define v22_E_IS_IGMP 0x00000080 /* IGMP bit in FRAME_TYPE field */
/* Bits in MTYPE field (WLAN only) */
#define v22_E_IS_QOS 0x80 /* QoS bit in MTYPE field (WLAN only) */
#define v22_E_IS_VLAN 0x00200000
#define v22_E_RX_DECRYPTS 0x0007 /* RX-frame-was-decrypted (UNUSED) */
#define v22_E_TX_DECRYPTS 0x0007 /* TX-frame-was-decrypted (UNUSED) */
#define v22_E_FC_PROT_BIT 0x40 /* Protected Frame bit in FC1 of frame */
#define v22_E_IS_ETHERNET 0x00700000 /* bits set in frame type if ethernet */
#define v22_E_IS_80211 0x7F000000 /* bits set in frame type if 802.11 */
/* definitions for v2.2 frames, WLAN format for VW510006 FPGA*/
#define v22_W_STATS_LEN 64 /* length of stats block trailer */
#define v22_W_VALID_OFF 0 /* bit 6 (0x40) is flow-is-valid flag */
#define v22_W_MTYPE_OFF 1 /* offset of modulation type */
#define v22_W_VCID_OFF 2 /* offset of VC ID */
#define v22_W_FLOWSEQ_OFF 4 /* offset of signature sequence number */
#define v22_W_FLOWID_OFF 5 /* offset of flow ID */
#define v22_W_OCTET_OFF 8 /* offset of octets */
#define v22_W_ERRORS_OFF 10 /* offset of error vector */
#define v22_W_PATN_OFF 12
#define v22_W_L4ID_OFF 12
#define v22_W_IPLEN_OFF 14
#define v22_W_FRAME_TYPE_OFF 16 /* offset of frame type, 32 bits */
#define v22_W_RSSI_OFF 21 /* RSSI (NOTE: RSSI must be negated!) */
#define v22_W_STARTT_OFF 24 /* offset of start time, 64 bits */
#define v22_W_ENDT_OFF 32 /* offset of end time, 64 bits */
#define v22_W_LATVAL_OFF 40 /* offset of latency, 32 bits */
#define v22_W_INFO_OFF 54 /* offset of INFO field, 16 LSBs */
#define v22_W_DIFFERENTIATOR_OFF 20 /* offset to determine whether */
/* eth/802.11, 32 bits */
#define v22_W_PLCP_LENGTH_OFF 4 /* LENGTH field in the plcp header */
/* Modulation types */
#define v22_W_MT_CCKL 0 /* CCK modulation, long preamble */
#define v22_W_MT_CCKS 1 /* CCK modulation, short preamble */
#define v22_W_MT_OFDM 2 /* OFDM modulation */
/* Bits in FRAME_TYPE field */
#define v22_W_IS_TCP 0x00000040 /* TCP bit in FRAME_TYPE field */
#define v22_W_IS_UDP 0x00000010 /* UDP bit in FRAME_TYPE field */
#define v22_W_IS_ICMP 0x00000020 /* ICMP bit in FRAME_TYPE field */
#define v22_W_IS_IGMP 0x00000080 /* IGMP bit in FRAME_TYPE field */
/* Bits in MTYPE field (WLAN only) */
#define v22_W_IS_QOS 0x80 /* QoS */
/* Error flags */
#define v22_W_FCS_ERROR 0x0002 /* FCS error flag in error vector */
#define v22_W_CRYPTO_ERR 0x1f00 /* RX decrypt error flags */
#define v22_W_SIG_ERR 0x0004 /* signature magic byte mismatch */
#define v22_W_PAYCHK_ERR 0x0008 /* payload checksum failure */
#define v22_W_RETRY_ERR 0x0400 /* excessive retries on TX failure */
/* Masks and defines */
#define v22_W_IS_RX 0x08 /* TX/RX bit in STATS block */
#define v22_W_MT_MASK 0x07 /* modulation type mask */
#define v22_W_VCID_MASK 0x01ff /* VC ID is only 9 bits */
#define v22_W_FLOW_VALID 0x40 /* flow-is-valid flag (else force to 0) */
#define v22_W_DIFFERENTIATOR_MASK 0xf0ff /* mask to differentiate ethernet from */
/* 802.11 capture */
#define v22_W_RX_DECRYPTS 0x0007 /* RX-frame-was-decrypted bits */
#define v22_W_TX_DECRYPTS 0x0007 /* TX-frame-was-decrypted bits */
/* Info bits */
#define v22_W_WEPTYPE 0x0001 /* WEP frame */
#define v22_W_TKIPTYPE 0x0002 /* TKIP frame */
#define v22_W_CCMPTYPE 0x0004 /* CCMP frame */
#define v22_W_MPDU_OF_A_MPDU 0x0400 /* MPDU of A-MPDU */
#define v22_W_FIRST_MPDU_OF_A_MPDU 0x0800 /* first MPDU of A-MPDU */
#define v22_W_LAST_MPDU_OF_A_MPDU 0x1000 /* last MPDU of A-MPDU */
#define v22_W_MSDU_OF_A_MSDU 0x2000 /* MSDU of A-MSDU */
#define v22_W_FIRST_MSDU_OF_A_MSDU 0x4000 /* first MSDU of A-MSDU */
#define v22_W_LAST_MSDU_OF_A_MSDU 0x8000 /* last MSDU of A-MSDU */
/* All aggregation flags */
#define v22_W_AGGREGATE_FLAGS \
(v22_W_MPDU_OF_A_MPDU | \
v22_W_FIRST_MPDU_OF_A_MPDU | \
v22_W_LAST_MPDU_OF_A_MPDU | \
v22_W_MSDU_OF_A_MSDU | \
v22_W_FIRST_MSDU_OF_A_MSDU | \
v22_W_LAST_MSDU_OF_A_MSDU)
#define v22_W_FC_PROT_BIT 0x40 /* Protected Frame bit in FC1 of frame */
#define v22_W_IS_ETHERNET 0x00100000 /* bits set in frame type if ethernet */
#define v22_W_IS_80211 0x7F000000 /* bits set in frame type if 802.11 */
/* definitions for VW510021 FPGA, WLAN format */
/* FORMAT:
16 BYTE header
8 bytes of stat block
plcp stuff (11 bytes plcp + 1 byte pad)
data
remaining 48 bytes of stat block
*/
/* offsets in the stats block */
#define vVW510021_W_STATS_HEADER_LEN 8 /* length of stats block header at beginning of record data */
#define vVW510021_W_STATS_TRAILER_LEN 48 /* length of stats block trailer after the plcp portion*/
#define vVW510021_W_STARTT_OFF 0 /* offset of start time, 64 bits */
#define vVW510021_W_ENDT_OFF 8 /* offset of end time, 64 bits */
#define vVW510021_W_ERRORS_OFF 16 /* offset of error vector */
#define vVW510021_W_VALID_OFF 20 /* 2 Bytes with different validity bits */
#define vVW510021_W_INFO_OFF 22 /* offset of INFO field, 16 LSBs */
#define vVW510021_W_FRAME_TYPE_OFF 24
#define vVW510021_W_L4ID_OFF 28
#define vVW510021_W_IPLEN_OFF 30 /* offset of IP Total Length field */
#define vVW510021_W_FLOWSEQ_OFF 32 /* offset of signature sequence number */
#define vVW510021_W_FLOWID_OFF 33 /* offset of flow ID */
#define vVW510021_W_LATVAL_OFF 36 /* offset of delay/flowtimestamp, 32b */
#define vVW510021_W_DEBUG_OFF 40 /* offset of debug, 16 bits */
#define S2_W_FPGA_VERSION_OFF 44 /* offset of fpga version, 16 bits */
#define vVW510021_W_MATCH_OFF 47 /* offset of pattern match vector */
/* offsets in the header block */
#define vVW510021_W_HEADER_LEN 16 /* length of FRAME header */
#define vVW510021_W_RXTX_OFF 0 /* rxtx offset, cmd byte of header */
#define vVW510021_W_HEADER_VERSION_OFF 9 /* version, 2bytes */
#define vVW510021_MSG_LENGTH_OFF 10 /* MSG LENGTH, 2bytes */
#define vVW510021_W_DEVICE_TYPE_OFF 8 /* version, 2bytes */
/* offsets that occur right after the header */
#define vVW510021_W_AFTERHEADER_LEN 8 /* length of STATs info directly after header */
#define vVW510021_W_L1P_1_OFF 0 /* offset of 1st byte of layer one info */
#define vVW510021_W_L1P_2_OFF 1 /* offset of 2nd byte of layer one info */
#define vVW510021_W_MTYPE_OFF vVW510021_W_L1P_2_OFF
#define vVW510021_W_PREAMBLE_OFF vVW510021_W_L1P_1_OFF
#define vVW510021_W_RSSI_TXPOWER_OFF 2 /* RSSI (NOTE: RSSI must be negated!) */
#define vVW510021_W_MSDU_LENGTH_OFF 3 /* 7:0 of length, next byte 11:8 in top 4 bits */
#define vVW510021_W_BVCV_VALID_OFF 4 /* BV,CV Determine validaity of bssid and txpower */
#define vVW510021_W_VCID_OFF 6 /* offset of VC (client) ID */
#define vVW510021_W_PLCP_LENGTH_OFF 12 /* LENGTH field in the plcp header */
/* Masks and defines */
#define vVW510021_W_IS_BV 0x04 /* BV bit in STATS block */
#define vVW510021_W_IS_CV 0x02 /* BV bit in STATS block */
#define vVW510021_W_FLOW_VALID 0x8000 /* valid_off flow-is-valid flag (else 0) */
#define vVW510021_W_QOS_VALID 0x4000
#define vVW510021_W_HT_VALID 0x2000
#define vVW510021_W_L4ID_VALID 0x1000
#define vVW510021_W_MCS_MASK 0x3f /* mcs index (a/b) type mask */
#define vVW510021_W_MOD_SCHEME_MASK 0x3f /* modulation type mask */
#define vVW510021_W_PLCPC_MASK 0x03 /* PLPCP type mask */
#define vVW510021_W_SEL_MASK 0x80
#define vVW510021_W_WEP_MASK 0x0001
#define vVW510021_W_CBW_MASK 0xC0
#define vVW510024_W_VCID_MASK 0x03ff /* VC ID is only 10 bits */
#define vVW510021_W_MT_SEL_LEGACY 0x00
#define vVW510021_W_IS_WEP 0x0001
/* L1p byte 1 info */
/* Common to Series II and Series III */
#define vVW510021_W_IS_LONGPREAMBLE 0x40 /* short/long preamble bit */
#define vVW510021_W_IS_LONGGI 0x40 /* short/long guard interval bit */
/* Series II */
/*
* Pre-HT - contains rate index.
*/
#define vVW510021_W_S2_RATE_INDEX(l1p_1) ((l1p_1) & 0x3f) /* rate index for pre-HT */
/*
* HT - contains MCS index.
*
* XXX - MCS indices for HT go up to 76, which doesn't fit in 6 bits;
* either the mask is wrong, or the hardware can't receive packets
* with an MCS of 64 through 76, or the hardware can but misreports
* the MCS.
*/
#define vVW510021_W_S2_MCS_INDEX_HT(l1p_1) ((l1p_1) & 0x3f)
/*
* VHT - contains MCS index and number of spatial streams.
* The number of spatial streams from the FPGA is zero-based, so we add
* 1 to it.
*/
#define vVW510021_W_S2_MCS_INDEX_VHT(l1p_1) ((l1p_1) & 0x0f) /* MCS index for VHT */
#define vVW510021_W_S2_NSS_VHT(l1p_1) (((l1p_1) >> 4) + 1) /* NSS */
/* Series III */
/*
* Pre-HT - contains rate index.
*/
#define vVW510021_W_S3_RATE_INDEX(l1p_1) ((l1p_1) & 0x3f)
/*
* HT - contains MCS index.
*
* XXX - MCS indices for HT go up to 76, which doesn't fit in 6 bits;
* either the mask is wrong, or the hardware can't receive packets
* with an MCS of 64 through 76, or the hardware can but misreports
* the MCS.
*/
#define vVW510021_W_S3_MCS_INDEX_HT(l1p_1) ((l1p_1) & 0x3f)
/*
* VHT - contains MCS index and number of spatial streams.
* The number of spatial streams from the FPGA is zero-based, so we add
* 1 to it.
*/
#define vVW510021_W_S3_MCS_INDEX_VHT(l1p_1) ((l1p_1) & 0x0f) /* MCS index */
#define vVW510021_W_S3_NSS_VHT(l1p_1) ((((l1p_1) >> 4) & 0x03) + 1) /* NSS */
/* L1p byte 2 info */
/* Common to Series II and Series III */
#define vVW510021_W_BANDWIDTH_VHT(l1p_2) (((l1p_2) >> 4) & 0xf)
/* 3 = 40 MHz, 4 = 80 MHz; what about 20 and 160 MHz? */
/* Series II */
#define vVW510021_W_S2_PLCP_TYPE(l1p_2) ((l1p_2) & 0x03) /* PLCP type */
/* Series III */
#define vVW510021_W_S3_PLCP_TYPE(l1p_2) ((l1p_2) & 0x0f) /* PLCP type */
/* PLCP types */
#define vVW510021_W_PLCP_LEGACY 0x00 /* pre-HT (11b/a/g) */
#define vVW510021_W_PLCP_MIXED 0x01 /* HT, mixed (11n) */
#define vVW510021_W_PLCP_GREENFIELD 0x02 /* HT, greenfield (11n) */
#define vVW510021_W_PLCP_VHT_MIXED 0x03 /* VHT (11ac) */
/* Bits in FRAME_TYPE field */
#define vVW510021_W_IS_TCP 0x01000000 /* TCP */
#define vVW510021_W_IS_UDP 0x00100000 /* UDP */
#define vVW510021_W_IS_ICMP 0x00001000 /* ICMP */
#define vVW510021_W_IS_IGMP 0x00010000 /* IGMP */
#define vVW510021_W_HEADER_VERSION 0x00
#define vVW510021_W_DEVICE_TYPE 0x15
#define vVW510021_W_11n_DEVICE_TYPE 0x20
#define S2_W_FPGA_VERSION 0x000C
#define vVW510021_W_11n_FPGA_VERSION 0x000D
/* Error flags */
#define vVW510021_W_FCS_ERROR 0x01
#define vVW510021_W_CRYPTO_ERROR 0x50000
#define vVW510021_W_WEPTYPE 0x0001 /* WEP frame */
#define vVW510021_W_TKIPTYPE 0x0002 /* TKIP frame */
#define vVW510021_W_CCMPTYPE 0x0004 /* CCMP frame */
/* definitions for VW510024 FPGA, wired ethernet format */
/* FORMAT:
16 BYTE header
52 bytes of stats block trailer
*/
/* offsets in the stats block */
#define vVW510024_E_STATS_LEN 48 /* length of stats block trailer */
#define vVW510024_E_MSDU_LENGTH_OFF 0 /* MSDU 16 BITS */
#define vVW510024_E_BMCV_VALID_OFF 2 /* BM,CV Determine validITY */
#define vVW510024_E_VCID_OFF 2 /* offset of VC (client) ID 13:8, */
/* 7:0 IN offset 7*/
#define vVW510024_E_STARTT_OFF 4 /* offset of start time, 64 bits */
#define vVW510024_E_ENDT_OFF 12 /* offset of end time, 64 bits */
#define vVW510024_E_ERRORS_OFF 22 /* offset of error vector */
#define vVW510024_E_VALID_OFF 24 /* 2 Bytes with different validity bits */
#define vVW510024_E_INFO_OFF 26 /* offset of INFO field, 16 LSBs */
#define vVW510024_E_FRAME_TYPE_OFF 28
#define vVW510024_E_L4ID_OFF 32
#define vVW510024_E_IPLEN_OFF 34
#define vVW510024_E_FLOWSEQ_OFF 36 /* offset of signature sequence number */
#define vVW510024_E_FLOWID_OFF 37 /* offset of flow ID */
#define vVW510024_E_LATVAL_OFF 40 /* offset of delay/flowtimestamp, 32 bits */
#define vVW510024_E_FPGA_VERSION_OFF 20 /* offset of fpga version, 16 bits */
#define vVW510024_E_MATCH_OFF 51 /* offset of pattern match vector */
/* offsets in the header block */
#define vVW510024_E_HEADER_LEN vVW510021_W_HEADER_LEN /* length of FRAME header */
#define vVW510024_E_RXTX_OFF vVW510021_W_RXTX_OFF /* rxtx offset, cmd byte */
#define vVW510024_E_HEADER_VERSION_OFF 16 /* version, 2bytes */
#define vVW510024_E_MSG_LENGTH_OFF vVW510021_MSG_LENGTH_OFF /* MSG LENGTH, 2bytes */
#define vVW510024_E_DEVICE_TYPE_OFF vVW510021_W_DEVICE_TYPE_OFF /* Device Type, 2bytes */
/* Masks and defines */
#define vVW510024_E_IS_BV 0x80 /* Bm bit in STATS block */
#define vVW510024_E_IS_CV 0x40 /* cV bit in STATS block */
#define vVW510024_E_FLOW_VALID 0x8000 /* valid_off flow-is-valid flag (else force to 0) */
#define vVW510024_E_QOS_VALID 0x0000 /** not valid for ethernet **/
#define vVW510024_E_L4ID_VALID 0x1000
#define vVW510024_E_CBW_MASK 0xC0
#define vVW510024_E_VCID_MASK 0x3FFF /* VCID is only 14 bits */
#define vVW510024_E_IS_TCP 0x01000000 /* TCP bit in FRAME_TYPE field */
#define vVW510024_E_IS_UDP 0x00100000 /* UDP bit in FRAME_TYPE field */
#define vVW510024_E_IS_ICMP 0x00001000 /* ICMP bit in FRAME_TYPE field */
#define vVW510024_E_IS_IGMP 0x00010000
#define vVW510024_E_IS_VLAN 0x00004000
#define vVW510024_E_HEADER_VERSION 0x00
#define vVW510024_E_DEVICE_TYPE 0x18
#define vVW510024_E_FPGA_VERSION 0x0001
#define FPGA_VER_NOT_APPLICABLE 0
#define UNKNOWN_FPGA 0
#define S2_W_FPGA 1
#define S1_W_FPGA 2
#define vVW510012_E_FPGA 3
#define vVW510024_E_FPGA 4
#define S3_W_FPGA 5
/* the flow signature is:
Byte Description
0 Magic Number (0xDD)
1 Chassis Number[7:0]
2 Slot Number[7:0]
3 Port Number[7:0]
4 Flow ID[7:0]
5 Flow ID[15:8]
6 Flow ID[23:16]
7 Flow Sequence Number[7:0]
8 Timestamp[7:0]
9 Timestamp[15:8]
10 Timestamp[23:16]
11 Timestamp[31:24]
12 Timestamp[39:32]
13 Timestamp[47:40]
14 CRC16
15 CRC16
*/
#define SIG_SIZE 16 /* size of signature field, bytes */
#define SIG_FID_OFF 4 /* offset of flow ID in signature */
#define SIG_FSQ_OFF 7 /* offset of flow seqnum in signature */
#define SIG_TS_OFF 8 /* offset of flow seqnum in signature */
/*--------------------------------------------------------------------------------------*/
/* Per-capture file private data structure */
typedef struct {
/* offsets in stats block; these are dependent on the frame type (Ethernet/WLAN) and */
/* version number of .vwr file, and are set up by setup_defaults() */
guint32 STATS_LEN; /* length of stats block trailer */
guint32 STATS_START_OFF; /* STATS OFF AFTER HEADER */
guint32 VALID_OFF; /* bit 6 (0x40) is flow-is-valid flag */
guint32 MTYPE_OFF; /* offset of modulation type */
guint32 VCID_OFF; /* offset of VC ID */
guint32 FLOWSEQ_OFF; /* offset of signature sequence number */
guint32 FLOWID_OFF; /* offset of flow ID */
guint32 OCTET_OFF; /* offset of octets */
guint32 ERRORS_OFF; /* offset of error vector */
guint32 PATN_OFF; /* offset of pattern match vector */
guint32 RSSI_OFF; /* RSSI (NOTE: RSSI must be negated!) */
guint32 STARTT_OFF; /* offset of start time, 64 bits */
guint32 ENDT_OFF; /* offset of end time, 64 bits */
guint32 LATVAL_OFF; /* offset of latency, 32 bits */
guint32 INFO_OFF; /* offset of INFO field, 16 bits */
guint32 L1P_1_OFF; /* offset 1ST Byte of l1params */
guint32 L1P_2_OFF; /* offset 2nd Byte of l1params */
guint32 L4ID_OFF; /* LAYER 4 id offset*/
guint32 IPLEN_OFF; /* */
guint32 PLCP_LENGTH_OFF; /* offset of length field in the PLCP header */
guint32 FPGA_VERSION_OFF; /* offset of fpga version field, 16 bits */
guint32 HEADER_VERSION_OFF; /* offset of header version, 16 bits */
guint32 RXTX_OFF; /* offset of CMD bit, rx or tx */
guint32 FRAME_TYPE_OFF;
/* other information about the file in question */
guint32 MT_10_HALF; /* 10 Mb/s half-duplex */
guint32 MT_10_FULL; /* 10 Mb/s full-duplex */
guint32 MT_100_HALF; /* 100 Mb/s half-duplex */
guint32 MT_100_FULL; /* 100 Mb/s full-duplex */
guint32 MT_1G_HALF; /* 1 Gb/s half-duplex */
guint32 MT_1G_FULL; /* 1 Gb/s full-duplex */
guint32 FCS_ERROR; /* FCS error in frame */
guint32 CRYPTO_ERR; /* RX decrypt error flags */
guint32 PAYCHK_ERR; /* payload checksum failure */
guint32 RETRY_ERR; /* excessive retries on TX failure */
guint8 IS_RX; /* TX/RX bit in STATS block */
guint8 MT_MASK; /* modulation type mask */
guint16 VCID_MASK; /* VC ID might not be a full 16 bits */
guint32 FLOW_VALID; /* flow-is-valid flag (else force to 0) */
guint16 QOS_VALID;
guint32 RX_DECRYPTS; /* RX-frame-was-decrypted bits */
guint32 TX_DECRYPTS; /* TX-frame-was-decrypted bits */
guint32 FC_PROT_BIT; /* Protected Frame bit in FC1 of frame */
guint32 MT_CCKL; /* CCK modulation, long preamble */
guint32 MT_CCKS; /* CCK modulation, short preamble */
guint32 MT_OFDM; /* OFDM modulation */
guint32 MCS_INDEX_MASK; /* mcs index type mask */
guint32 FPGA_VERSION;
guint32 WEPTYPE; /* frame is WEP */
guint32 TKIPTYPE; /* frame is TKIP */
guint32 CCMPTYPE; /* frame is CCMP */
guint32 IS_TCP;
guint32 IS_UDP;
guint32 IS_ICMP;
guint32 IS_IGMP;
guint16 IS_QOS;
guint32 IS_VLAN;
guint32 MPDU_OFF;
guint32 OCTO_VERSION;
} vwr_t;
/*
* NSS for various MCS values.
*/
#define MAX_HT_MCS 76
static guint nss_for_mcs[MAX_HT_MCS+1] = {
1, 1, 1, 1, 1, 1, 1, 1, /* 0-7 */
2, 2, 2, 2, 2, 2, 2, 2, /* 8-15 */
3, 3, 3, 3, 3, 3, 3, 3, /* 16-23 */
4, 4, 4, 4, 4, 4, 4, 4, /* 24-31 */
1, /* 32 */
2, 2, 2, 2, 2, 2, /* 33-38 */
3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, /* 39-52 */
4, 4, 4, 4, 4, 4, /* 53-58 */
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 /* 59-76 */
};
/* internal utility functions */
static int decode_msg(vwr_t *vwr, register guint8 *, int *, int *, int *);
static guint8 get_ofdm_rate(const guint8 *);
static guint8 get_cck_rate(const guint8 *plcp);
static void setup_defaults(vwr_t *, guint16);
static gboolean vwr_read(wtap *, wtap_rec *, Buffer *, int *,
gchar **, gint64 *);
static gboolean vwr_seek_read(wtap *, gint64, wtap_rec *,
Buffer *, int *, gchar **);
static gboolean vwr_read_rec_header(vwr_t *, FILE_T, int *, int *, int *, int *, gchar **);
static gboolean vwr_process_rec_data(FILE_T fh, int rec_size,
wtap_rec *record, Buffer *buf,
vwr_t *vwr, int IS_TX, int log_mode, int *err,
gchar **err_info);
static int vwr_get_fpga_version(wtap *, int *, gchar **);
static gboolean vwr_read_s1_W_rec(vwr_t *, wtap_rec *, Buffer *,
const guint8 *, int, int *, gchar **);
static gboolean vwr_read_s2_W_rec(vwr_t *, wtap_rec *, Buffer *,
const guint8 *, int, int, int *,
gchar **);
/* For FPGA version >= 48 (OCTO Platform), following function will be used */
static gboolean vwr_read_s3_W_rec(vwr_t *, wtap_rec *, Buffer *,
const guint8 *, int, int, int, int *,
gchar **);
static gboolean vwr_read_rec_data_ethernet(vwr_t *, wtap_rec *,
Buffer *, const guint8 *, int,
int, int *, gchar **);
static int find_signature(const guint8 *, int, int, register guint32, register guint8);
static guint64 get_signature_ts(const guint8 *, int, int);
static float get_legacy_rate(guint8);
static float get_ht_rate(guint8, guint16);
static float get_vht_rate(guint8, guint16, guint8);
static int vwr_80211_file_type_subtype = -1;
static int vwr_eth_file_type_subtype = -1;
void register_vwr(void);
/* Open a .vwr file for reading */
/* This does very little, except setting the wiretap header for a VWR file type */
/* and setting the timestamp precision to microseconds. */
wtap_open_return_val vwr_open(wtap *wth, int *err, gchar **err_info)
{
int fpgaVer;
vwr_t *vwr;
*err = 0;
fpgaVer = vwr_get_fpga_version(wth, err, err_info);
if (fpgaVer == -1) {
return WTAP_OPEN_ERROR; /* I/O error */
}
if (fpgaVer == UNKNOWN_FPGA) {
return WTAP_OPEN_NOT_MINE; /* not a VWR file */
}
/* This is a vwr file */
vwr = g_new0(vwr_t, 1);
wth->priv = (void *)vwr;
vwr->FPGA_VERSION = fpgaVer;
/* set the local module options first */
setup_defaults(vwr, fpgaVer);
wth->snapshot_length = 0;
wth->subtype_read = vwr_read;
wth->subtype_seek_read = vwr_seek_read;
wth->file_tsprec = WTAP_TSPREC_USEC;
wth->file_encap = WTAP_ENCAP_IXVERIWAVE;
if (fpgaVer == S2_W_FPGA || fpgaVer == S1_W_FPGA || fpgaVer == S3_W_FPGA)
wth->file_type_subtype = vwr_80211_file_type_subtype;
else if (fpgaVer == vVW510012_E_FPGA || fpgaVer == vVW510024_E_FPGA)
wth->file_type_subtype = vwr_eth_file_type_subtype;
/*
* Add an IDB; we don't know how many interfaces were
* involved, so we just say one interface, about which
* we only know the link-layer type, snapshot length,
* and time stamp resolution.
*/
wtap_add_generated_idb(wth);
return WTAP_OPEN_MINE;
}
/* Read the next packet */
/* Note that the VWR file format consists of a sequence of fixed 16-byte record headers of */
/* different types; some types, including frame record headers, are followed by */
/* variable-length data. */
/* A frame record consists of: the above 16-byte record header, a 1-16384 byte raw PLCP */
/* frame, and a 64-byte statistics block trailer. */
/* The PLCP frame consists of a 4-byte or 6-byte PLCP header, followed by the MAC frame */
static gboolean vwr_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset)
{
vwr_t *vwr = (vwr_t *)wth->priv;
int rec_size = 0, IS_TX = 0, log_mode = 0;
/* read the next frame record header in the capture file; if no more frames, return */
if (!vwr_read_rec_header(vwr, wth->fh, &rec_size, &IS_TX, &log_mode, err, err_info))
return FALSE; /* Read error or EOF */
/*
* We're past the header; return the offset of the header, not of
* the data past the header.
*/
*data_offset = (file_tell(wth->fh) - VW_RECORD_HEADER_LENGTH);
/* got a frame record; read and process it */
if (!vwr_process_rec_data(wth->fh, rec_size, rec, buf, vwr, IS_TX,
log_mode, err, err_info))
return FALSE;
return TRUE;
}
/* read a random record in the middle of a file; the start of the record is @ seek_off */
static gboolean vwr_seek_read(wtap *wth, gint64 seek_off,
wtap_rec *record, Buffer *buf, int *err, gchar **err_info)
{
vwr_t *vwr = (vwr_t *)wth->priv;
int rec_size, IS_TX = 0, log_mode = 0;
/* first seek to the indicated record header */
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return FALSE;
/* read in the record header */
if (!vwr_read_rec_header(vwr, wth->random_fh, &rec_size, &IS_TX, &log_mode, err, err_info))
return FALSE; /* Read error or EOF */
return vwr_process_rec_data(wth->random_fh, rec_size, record, buf,
vwr, IS_TX, log_mode, err, err_info);
}
/* Scan down in the input capture file to find the next frame header. */
/* Decode and skip over all non-frame messages that are in the way. */
/* Return TRUE on success, FALSE on EOF or error. */
/* Also return the frame size in bytes and the "is transmitted frame" flag. */
static gboolean vwr_read_rec_header(vwr_t *vwr, FILE_T fh, int *rec_size, int *IS_TX, int *log_mode, int *err, gchar **err_info)
{
int f_len, v_type;
guint8 header[VW_RECORD_HEADER_LENGTH];
*rec_size = 0;
/* Read out the file data in 16-byte messages, stopping either after we find a frame, */
/* or if we run out of data. */
/* Each 16-byte message is decoded; if we run across a non-frame message followed by a */
/* variable-length item, we read the variable length item out and discard it. */
/* If we find a frame, we return (with the header in the passed buffer). */
while (1) {
if (!wtap_read_bytes_or_eof(fh, header, VW_RECORD_HEADER_LENGTH, err, err_info))
return FALSE;
/* Got a header; invoke decode-message function to parse and process it. */
/* If the function returns a length, then a frame or variable-length message */
/* follows the 16-byte message. */
/* If the variable length message is not a frame, simply skip over it. */
if ((f_len = decode_msg(vwr, header, &v_type, IS_TX, log_mode)) != 0) {
if (f_len > B_SIZE) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("vwr: Invalid message record length %d", f_len);
return FALSE;
}
else if (v_type != VT_FRAME) {
if (!wtap_read_bytes(fh, NULL, f_len, err, err_info))
return FALSE;
}
else {
*rec_size = f_len;
return TRUE;
}
}
}
}
/* Figure out the FPGA version (and also see whether this is a VWR file type. */
/* Return FPGA version if it's a known version, UNKNOWN_FPGA if it's not, */
/* and -1 on an I/O error. */
static int vwr_get_fpga_version(wtap *wth, int *err, gchar **err_info)
{
guint8 *rec; /* local buffer (holds input record) */
guint8 header[VW_RECORD_HEADER_LENGTH];
int rec_size = 0;
guint8 i;
guint8 *s_510006_ptr = NULL;
guint8 *s_510024_ptr = NULL;
guint8 *s_510012_ptr = NULL; /* stats pointers */
gint64 filePos = -1;
guint64 bytes_read = 0;
guint32 frame_type = 0;
int f_len, v_type;
guint16 data_length = 0;
guint16 fpga_version;
gboolean valid_but_empty_file = FALSE;
filePos = file_tell(wth->fh);
if (filePos == -1) {
*err = file_error(wth->fh, err_info);
return -1;
}
fpga_version = 1000;
rec = (guint8*)g_malloc(B_SIZE);
/* Got a frame record; see if it is vwr */
/* If we don't get it all, then declare an error, we can't process the frame. */
/* Read out the file data in 16-byte messages, stopping either after we find a frame, */
/* or if we run out of data. */
/* Each 16-byte message is decoded; if we run across a non-frame message followed by a */
/* variable-length item, we read the variable length item out and discard it. */
/* If we find a frame, we return (with the header in the passed buffer). */
while (wtap_read_bytes(wth->fh, header, VW_RECORD_HEADER_LENGTH, err, err_info)) {
/* Got a header; invoke decode-message function to parse and process it. */
/* If the function returns a length, then a frame or variable-length message */
/* follows the 16-byte message. */
/* If the variable length message is not a frame, simply skip over it. */
if ((f_len = decode_msg(NULL, header, &v_type, NULL, NULL)) != 0) {
if (f_len > B_SIZE) {
g_free(rec);
/* Treat this here as an indication that the file probably */
/* isn't a vwr file. */
return UNKNOWN_FPGA;
}
else if (v_type != VT_FRAME) {
if (!wtap_read_bytes(wth->fh, NULL, f_len, err, err_info)) {
g_free(rec);
if (*err == WTAP_ERR_SHORT_READ)
return UNKNOWN_FPGA; /* short read - not a vwr file */
return -1;
}
else if (v_type == VT_CPMSG)
valid_but_empty_file = TRUE;
}
else {
rec_size = f_len;
/* Got a frame record; read over entire record (frame + trailer) into a local buffer */
/* If we don't get it all, assume this isn't a vwr file */
if (!wtap_read_bytes(wth->fh, rec, rec_size, err, err_info)) {
g_free(rec);
if (*err == WTAP_ERR_SHORT_READ)
return UNKNOWN_FPGA; /* short read - not a vwr file */
return -1;
}
/* I'll grab the bytes where the Ethernet "octets" field should be and the bytes where */
/* the 802.11 "octets" field should be. Then if I do rec_size - octets - */
/* size_of_stats_block and it's 0, I can select the correct type. */
/* octets + stats_len = rec_size only when octets have been incremented to nearest */
/* number divisible by 4. */
/* First check for series I WLAN since the check is more rigorous. */
if (rec_size > v22_W_STATS_LEN) {
s_510006_ptr = &(rec[rec_size - v22_W_STATS_LEN]); /* point to 510006 WLAN */
/* stats block */
data_length = pntoh16(&s_510006_ptr[v22_W_OCTET_OFF]);
i = 0;
while (((data_length + i) % 4) != 0)
i = i + 1;
frame_type = pntoh32(&s_510006_ptr[v22_W_FRAME_TYPE_OFF]);
if (rec_size == (data_length + v22_W_STATS_LEN + i) && (frame_type & v22_W_IS_80211) == 0x1000000) {
fpga_version = S1_W_FPGA;
}
}
/* Next for the series I Ethernet */
if ((rec_size > v22_E_STATS_LEN) && (fpga_version == 1000)) {
s_510012_ptr = &(rec[rec_size - v22_E_STATS_LEN]); /* point to 510012 enet */
/* stats block */
data_length = pntoh16(&s_510012_ptr[v22_E_OCTET_OFF]);
i = 0;
while (((data_length + i) % 4) != 0)
i = i + 1;
if (rec_size == (data_length + v22_E_STATS_LEN + i))
fpga_version = vVW510012_E_FPGA;
}
/* Next the series II WLAN */
if ((rec_size > vVW510021_W_STATS_TRAILER_LEN) && (fpga_version == 1000)) {
/* stats block */
if ((header[8] == 48) || (header[8] == 61) || (header[8] == 68))
fpga_version = S3_W_FPGA;
else {
data_length = (256 * (rec[vVW510021_W_MSDU_LENGTH_OFF + 1] & 0x1f)) + rec[vVW510021_W_MSDU_LENGTH_OFF];
i = 0;
while (((data_length + i) % 4) != 0)
i = i + 1;
/*the 12 is from the 12 bytes of plcp header */
if (rec_size == (data_length + vVW510021_W_STATS_TRAILER_LEN +vVW510021_W_AFTERHEADER_LEN+12+i))
fpga_version = S2_W_FPGA;
}
}
/* Finally the Series II Ethernet */
if ((rec_size > vVW510024_E_STATS_LEN) && (fpga_version == 1000)) {
s_510024_ptr = &(rec[rec_size - vVW510024_E_STATS_LEN]); /* point to 510024 ENET */
data_length = pntoh16(&s_510024_ptr[vVW510024_E_MSDU_LENGTH_OFF]);
i = 0;
while (((data_length + i) % 4) != 0)
i = i + 1;
if (rec_size == (data_length + vVW510024_E_STATS_LEN + i))
fpga_version = vVW510024_E_FPGA;
}
if (fpga_version != 1000)
{
/* reset the file position offset */
if (file_seek (wth->fh, filePos, SEEK_SET, err) == -1) {
g_free(rec);
return (-1);
}
/* We found an FPGA that works */
g_free(rec);
return fpga_version;
}
}
}
bytes_read += VW_RECORD_HEADER_LENGTH;
if (bytes_read > VW_BYTES_TO_CHECK) {
/* no frame found in VW_BYTES_TO_CHECK - not a vwr file */
g_free(rec);
return UNKNOWN_FPGA;
}
}
/* Is this a valid but empty file? If so, claim it's the S3_W_FPGA FPGA. */
if (valid_but_empty_file) {
g_free(rec);
return(S3_W_FPGA);
}
if (*err == WTAP_ERR_SHORT_READ) {
g_free(rec);
return UNKNOWN_FPGA; /* short read - not a vwr file */
}
/*
* Read error.
*/
g_free(rec);
return -1;
}
/* Copy the actual packet data from the capture file into the target data block. */
/* The packet is constructed as a 38-byte VeriWave metadata header plus the raw */
/* MAC octets. */
static gboolean vwr_read_s1_W_rec(vwr_t *vwr, wtap_rec *record,
Buffer *buf, const guint8 *rec, int rec_size,
int *err, gchar **err_info)
{
guint8 *data_ptr;
int bytes_written = 0; /* bytes output to buf so far */
const guint8 *s_ptr, *m_ptr; /* stats pointer */
guint16 msdu_length, actual_octets; /* octets in frame */
guint16 plcp_hdr_len; /* PLCP header length */
guint16 rflags;
guint8 m_type; /* mod type (CCK-L/CCK-S/OFDM), seqnum */
guint flow_seq;
guint64 s_time = LL_ZERO, e_time = LL_ZERO; /* start/end */
/* times, nsec */
guint32 latency;
guint64 start_time, s_sec, s_usec = LL_ZERO; /* start time, sec + usec */
guint64 end_time; /* end time */
guint32 info; /* INFO/ERRORS fields in stats blk */
gint8 rssi; /* RSSI, signed 8-bit number */
int f_tx; /* flag: if set, is a TX frame */
guint8 rate_index; /* pre-HT only */
guint16 vc_id, ht_len=0; /* VC ID, total ip length */
guint flow_id; /* flow ID */
guint32 d_time, errors; /* packet duration & errors */
int sig_off, pay_off; /* MAC+SNAP header len, signature offset */
guint64 sig_ts; /* 32 LSBs of timestamp in signature */
guint16 phyRate;
guint16 vw_flags; /* VeriWave-specific packet flags */
/*
* The record data must be large enough to hold the statistics trailer.
*/
if (rec_size < v22_W_STATS_LEN) {
*err_info = ws_strdup_printf("vwr: Invalid record length %d (must be at least %u)",
rec_size, v22_W_STATS_LEN);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
/* Calculate the start of the statistics block in the buffer */
/* Also get a bunch of fields from the stats block */
s_ptr = &(rec[rec_size - v22_W_STATS_LEN]); /* point to it */
m_type = s_ptr[v22_W_MTYPE_OFF] & v22_E_MT_MASK;
f_tx = !(s_ptr[v22_W_MTYPE_OFF] & v22_E_IS_RX);
actual_octets = pntoh16(&s_ptr[v22_W_OCTET_OFF]);
vc_id = pntoh16(&s_ptr[v22_W_VCID_OFF]) & v22_E_VCID_MASK;
flow_seq = s_ptr[v22_W_FLOWSEQ_OFF];
latency = (guint32)pcorey48tohll(&s_ptr[v22_W_LATVAL_OFF]);
flow_id = pntoh16(&s_ptr[v22_W_FLOWID_OFF+1]); /* only 16 LSBs kept */
errors = pntoh16(&s_ptr[v22_W_ERRORS_OFF]);
info = pntoh16(&s_ptr[v22_W_INFO_OFF]);
rssi = (s_ptr[v22_W_RSSI_OFF] & 0x80) ? (-1 * (s_ptr[v22_W_RSSI_OFF] & 0x7f)) : s_ptr[v22_W_RSSI_OFF];
/*
* Sanity check the octets field to determine if it's greater than
* the packet data available in the record - i.e., the record size
* minus the length of the statistics block.
*
* Report an error if it is.
*/
if (actual_octets > rec_size - v22_W_STATS_LEN) {
*err_info = ws_strdup_printf("vwr: Invalid data length %u (runs past the end of the record)",
actual_octets);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
/* Decode OFDM or CCK PLCP header and determine rate and short preamble flag. */
/* The SIGNAL byte is always the first byte of the PLCP header in the frame. */
if (m_type == vwr->MT_OFDM)
rate_index = get_ofdm_rate(rec);
else if ((m_type == vwr->MT_CCKL) || (m_type == vwr->MT_CCKS))
rate_index = get_cck_rate(rec);
else
rate_index = 1;
rflags = (m_type == vwr->MT_CCKS) ? FLAGS_SHORTPRE : 0;
/* Calculate the MPDU size/ptr stuff; MPDU starts at 4 or 6 depending on OFDM/CCK. */
/* Note that the number of octets in the frame also varies depending on OFDM/CCK, */
/* because the PLCP header is prepended to the actual MPDU. */
plcp_hdr_len = (m_type == vwr->MT_OFDM) ? 4 : 6;
if (actual_octets >= plcp_hdr_len)
actual_octets -= plcp_hdr_len;
else {
*err_info = ws_strdup_printf("vwr: Invalid data length %u (too short to include %u-byte PLCP header)",
actual_octets, plcp_hdr_len);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
m_ptr = &rec[plcp_hdr_len];
msdu_length = actual_octets;
/*
* The MSDU length includes the FCS.
*
* The packet data does *not* include the FCS - it's just 4 bytes
* of junk - so we have to remove it.
*
* We'll be stripping off that junk, so make sure we have at least
* 4 octets worth of packet data.
*
* There seems to be a special case of a length of 0.
*/
if (actual_octets < 4) {
if (actual_octets != 0) {
*err_info = ws_strdup_printf("vwr: Invalid data length %u (too short to include %u-byte PLCP header and 4 bytes of FCS)",
actual_octets, plcp_hdr_len);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
} else {
actual_octets -= 4;
}
/* Calculate start & end times (in sec/usec), converting 64-bit times to usec. */
/* 64-bit times are "Corey-endian" */
s_time = pcoreytohll(&s_ptr[v22_W_STARTT_OFF]);
e_time = pcoreytohll(&s_ptr[v22_W_ENDT_OFF]);
/* find the packet duration (difference between start and end times) */
d_time = (guint32)((e_time - s_time) / NS_IN_US); /* find diff, converting to usec */
/* also convert the packet start time to seconds and microseconds */
start_time = s_time / NS_IN_US; /* convert to microseconds first */
s_sec = (start_time / US_IN_SEC); /* get the number of seconds */
s_usec = start_time - (s_sec * US_IN_SEC); /* get the number of microseconds */
/* also convert the packet end time to seconds and microseconds */
end_time = e_time / NS_IN_US; /* convert to microseconds first */
/* extract the 32 LSBs of the signature timestamp field from the data block*/
pay_off = 42; /* 24 (MAC) + 8 (SNAP) + IP */
sig_off = find_signature(m_ptr, rec_size - 6, pay_off, flow_id, flow_seq);
if (m_ptr[sig_off] == 0xdd)
sig_ts = get_signature_ts(m_ptr, sig_off, rec_size - v22_W_STATS_LEN);
else
sig_ts = 0;
/*
* Fill up the per-packet header.
*
* We include the length of the metadata headers in the packet lengths.
*
* The maximum value of actual_octets is 8191, which, even after
* adding the lengths of the metadata headers, is less than
* WTAP_MAX_PACKET_SIZE_STANDARD will ever be, so we don't need to check it.
*/
record->rec_header.packet_header.len = STATS_COMMON_FIELDS_LEN + EXT_WLAN_FIELDS_LEN + actual_octets;
record->rec_header.packet_header.caplen = STATS_COMMON_FIELDS_LEN + EXT_WLAN_FIELDS_LEN + actual_octets;
record->ts.secs = (time_t)s_sec;
record->ts.nsecs = (int)(s_usec * 1000);
record->rec_header.packet_header.pkt_encap = WTAP_ENCAP_IXVERIWAVE;
record->rec_type = REC_TYPE_PACKET;
record->block = wtap_block_create(WTAP_BLOCK_PACKET);
record->presence_flags = WTAP_HAS_TS;
ws_buffer_assure_space(buf, record->rec_header.packet_header.caplen);
data_ptr = ws_buffer_start_ptr(buf);
/*
* Generate and copy out the common metadata headers,
* set the port type to 0 (WLAN).
*
* All values are copied out in little-endian byte order.
*/
/* 1st octet of record for port_type and command (command is 0, hence RX) */
phtole8(&data_ptr[bytes_written], WLAN_PORT);
bytes_written += 1;
/* 2nd octet of record for fpga version (0, hence pre-OCTO) */
phtole8(&data_ptr[bytes_written], 0);
bytes_written += 1;
phtoles(&data_ptr[bytes_written], STATS_COMMON_FIELDS_LEN); /* it_len */
bytes_written += 2;
phtoles(&data_ptr[bytes_written], msdu_length);
bytes_written += 2;
phtolel(&data_ptr[bytes_written], flow_id);
bytes_written += 4;
phtoles(&data_ptr[bytes_written], vc_id);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], flow_seq);
bytes_written += 2;
if (!f_tx && sig_ts != 0) {
phtolel(&data_ptr[bytes_written], latency);
} else {
phtolel(&data_ptr[bytes_written], 0);
}
bytes_written += 4;
phtolel(&data_ptr[bytes_written], sig_ts); /* 32 LSBs of signature timestamp (nsec) */
bytes_written += 4;
phtolell(&data_ptr[bytes_written], start_time); /* record start & end times of frame */
bytes_written += 8;
phtolell(&data_ptr[bytes_written], end_time);
bytes_written += 8;
phtolel(&data_ptr[bytes_written], d_time);
bytes_written += 4;
/*
* Generate and copy out the WLAN metadata headers.
*
* All values are copied out in little-endian byte order.
*/
phtoles(&data_ptr[bytes_written], EXT_WLAN_FIELDS_LEN);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], rflags);
bytes_written += 2;
if (m_type == vwr->MT_OFDM) {
phtoles(&data_ptr[bytes_written], CHAN_OFDM);
} else {
phtoles(&data_ptr[bytes_written], CHAN_CCK);
}
bytes_written += 2;
phyRate = (guint16)(get_legacy_rate(rate_index) * 10);
phtoles(&data_ptr[bytes_written], phyRate);
bytes_written += 2;
data_ptr[bytes_written] = vVW510021_W_PLCP_LEGACY; /* pre-HT */
bytes_written += 1;
data_ptr[bytes_written] = rate_index;
bytes_written += 1;
data_ptr[bytes_written] = 1; /* pre-VHT, so NSS = 1 */
bytes_written += 1;
data_ptr[bytes_written] = rssi;
bytes_written += 1;
/* antennae b, c, d signal power */
data_ptr[bytes_written] = 100;
bytes_written += 1;
data_ptr[bytes_written] = 100;
bytes_written += 1;
data_ptr[bytes_written] = 100;
bytes_written += 1;
/* padding */
data_ptr[bytes_written] = 0;
bytes_written += 1;
/* fill in the VeriWave flags field */
vw_flags = 0;
if (f_tx)
vw_flags |= VW_FLAGS_TXF;
if (errors & vwr->FCS_ERROR)
vw_flags |= VW_FLAGS_FCSERR;
if (!f_tx && (errors & vwr->CRYPTO_ERR))
vw_flags |= VW_FLAGS_DCRERR;
if (!f_tx && (errors & vwr->RETRY_ERR))
vw_flags |= VW_FLAGS_RETRERR;
if (info & vwr->WEPTYPE)
vw_flags |= VW_FLAGS_IS_WEP;
else if (info & vwr->TKIPTYPE)
vw_flags |= VW_FLAGS_IS_TKIP;
else if (info & vwr->CCMPTYPE)
vw_flags |= VW_FLAGS_IS_CCMP;
phtoles(&data_ptr[bytes_written], vw_flags);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], ht_len);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], info);
bytes_written += 2;
phtolel(&data_ptr[bytes_written], errors);
bytes_written += 4;
/*
* Finally, copy the whole MAC frame to the packet buffer as-is.
* This does not include the PLCP; the MPDU starts at 4 or 6
* depending on OFDM/CCK.
* This also does not include the last 4 bytes, as those don't
* contain an FCS, they just contain junk.
*/
memcpy(&data_ptr[bytes_written], &rec[plcp_hdr_len], actual_octets);
return TRUE;
}
static gboolean vwr_read_s2_W_rec(vwr_t *vwr, wtap_rec *record,
Buffer *buf, const guint8 *rec, int rec_size,
int IS_TX, int *err, gchar **err_info)
{
guint8 *data_ptr;
int bytes_written = 0; /* bytes output to buf so far */
const guint8 *s_start_ptr,*s_trail_ptr, *plcp_ptr, *m_ptr; /* stats & MPDU ptr */
guint32 msdu_length, actual_octets; /* octets in frame */
guint8 l1p_1, l1p_2, plcp_type, rate_mcs_index, nss; /* mod (CCK-L/CCK-S/OFDM) */
guint flow_seq;
guint64 s_time = LL_ZERO, e_time = LL_ZERO; /* start/end */
/* times, nsec */
guint64 latency = LL_ZERO;
guint64 start_time, s_sec, s_usec = LL_ZERO; /* start time, sec + usec */
guint64 end_time; /* end time */
guint16 info; /* INFO/ERRORS fields in stats blk */
guint32 errors;
gint8 rssi[] = {0,0,0,0}; /* RSSI, signed 8-bit number */
int f_tx; /* flag: if set, is a TX frame */
guint16 vc_id, ht_len=0; /* VC ID , total ip length*/
guint32 flow_id, d_time; /* flow ID, packet duration*/
int sig_off, pay_off; /* MAC+SNAP header len, signature offset */
guint64 sig_ts, tsid; /* 32 LSBs of timestamp in signature */
guint16 chanflags = 0; /* channel flags for WLAN metadata header */
guint16 radioflags = 0; /* flags for WLAN metadata header */
guint64 delta_b; /* Used for calculating latency */
float rate;
guint16 phyRate;
guint16 vw_flags; /* VeriWave-specific packet flags */
/*
* The record data must be large enough to hold the statistics header,
* the PLCP, and the statistics trailer.
*/
if ((guint)rec_size < vwr->MPDU_OFF + vVW510021_W_STATS_TRAILER_LEN) {
*err_info = ws_strdup_printf("vwr: Invalid record length %d (must be at least %u)",
rec_size,
vwr->MPDU_OFF + vVW510021_W_STATS_TRAILER_LEN);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
/* Calculate the start of the statistics blocks in the buffer */
/* Also get a bunch of fields from the stats blocks */
s_start_ptr = &(rec[0]); /* point to stats header */
s_trail_ptr = &(rec[rec_size - vVW510021_W_STATS_TRAILER_LEN]); /* point to stats trailer */
l1p_1 = s_start_ptr[vVW510021_W_L1P_1_OFF];
l1p_2 = s_start_ptr[vVW510021_W_L1P_2_OFF];
plcp_type = vVW510021_W_S2_PLCP_TYPE(l1p_2);
/* we do the range checks at the end before copying the values
into the wtap header */
msdu_length = ((s_start_ptr[vVW510021_W_MSDU_LENGTH_OFF+1] & 0x1f) << 8)
+ s_start_ptr[vVW510021_W_MSDU_LENGTH_OFF];
vc_id = pntoh16(&s_start_ptr[vVW510021_W_VCID_OFF]);
if (IS_TX)
{
rssi[0] = (s_start_ptr[vVW510021_W_RSSI_TXPOWER_OFF] & 0x80) ?
-1 * (s_start_ptr[vVW510021_W_RSSI_TXPOWER_OFF] & 0x7f) :
s_start_ptr[vVW510021_W_RSSI_TXPOWER_OFF] & 0x7f;
}
else
{
rssi[0] = (s_start_ptr[vVW510021_W_RSSI_TXPOWER_OFF] & 0x80) ?
(s_start_ptr[vVW510021_W_RSSI_TXPOWER_OFF]- 256) :
s_start_ptr[vVW510021_W_RSSI_TXPOWER_OFF];
}
rssi[1] = 100;
rssi[2] = 100;
rssi[3] = 100;
plcp_ptr = &(rec[8]);
actual_octets = msdu_length;
/*
* Sanity check the octets field to determine if it's greater than
* the packet data available in the record - i.e., the record size
* minus the sum of (length of statistics header + PLCP) and
* (length of statistics trailer).
*
* Report an error if it is.
*/
if (actual_octets > rec_size - (vwr->MPDU_OFF + vVW510021_W_STATS_TRAILER_LEN)) {
*err_info = ws_strdup_printf("vwr: Invalid data length %u (runs past the end of the record)",
actual_octets);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
f_tx = IS_TX;
flow_seq = s_trail_ptr[vVW510021_W_FLOWSEQ_OFF];
latency = 0x00000000; /* clear latency */
flow_id = pntoh24(&s_trail_ptr[vVW510021_W_FLOWID_OFF]); /* all 24 bits valid */
/* For tx latency is duration, for rx latency is timestamp */
/* Get 48-bit latency value */
tsid = pcorey48tohll(&s_trail_ptr[vVW510021_W_LATVAL_OFF]);
errors = pntoh32(&s_trail_ptr[vVW510021_W_ERRORS_OFF]);
info = pntoh16(&s_trail_ptr[vVW510021_W_INFO_OFF]);
if ((info & v22_W_AGGREGATE_FLAGS) != 0)
/* this length includes the Start_Spacing + Delimiter + MPDU + Padding for each piece of the aggregate*/
ht_len = pletoh16(&s_start_ptr[vwr->PLCP_LENGTH_OFF]);
/* decode OFDM or CCK PLCP header and determine rate and short preamble flag */
/* the SIGNAL byte is always the first byte of the PLCP header in the frame */
switch (plcp_type)
{
case vVW510021_W_PLCP_LEGACY:
/*
* From IEEE Std 802.11-2012:
*
* According to section 17.2.2 "PPDU format", the PLCP header
* for the High Rate DSSS PHY (11b) has a SIGNAL field that's
* 8 bits, followed by a SERVICE field that's 8 bits, followed
* by a LENGTH field that's 16 bits, followed by a CRC field
* that's 16 bits. The PSDU follows it. Section 17.2.3 "PPDU
* field definitions" describes those fields.
*
* According to sections 18.3.2 "PLCP frame format" and 18.3.4
* "SIGNAL field", the PLCP for the OFDM PHY (11a) has a SIGNAL
* field that's 24 bits, followed by a service field that's
* 16 bits, followed by the PSDU. Section 18.3.5.2 "SERVICE
* field" describes the SERVICE field.
*
* According to section 19.3.2 "PPDU format", the frames for the
* Extended Rate PHY (11g) either extend the 11b format, using
* additional bits in the SERVICE field, or extend the 11a
* format.
*/
rate_mcs_index = vVW510021_W_S2_RATE_INDEX(l1p_1);
if (rate_mcs_index < 4) {
chanflags |= CHAN_CCK;
}
else {
chanflags |= CHAN_OFDM;
}
rate = get_legacy_rate(rate_mcs_index);
nss = 0;
break;
case vVW510021_W_PLCP_MIXED:
/*
* According to section 20.3.2 "PPDU format", the HT-mixed
* PLCP header has a "Non-HT SIGNAL field" (L-SIG), which
* looks like an 11a SIGNAL field, followed by an HT SIGNAL
* field (HT-SIG) described in section 20.3.9.4.3 "HT-SIG
* definition".
*
* This means that the first octet of HT-SIG is at
* plcp_ptr[3], skipping the 3 octets of the L-SIG field.
*
* 0x80 is the CBW 20/40 bit of HT-SIG.
*/
/* set the appropriate flags to indicate HT mode and CB */
rate_mcs_index = vVW510021_W_S2_MCS_INDEX_HT(l1p_1);
radioflags |= FLAGS_CHAN_HT | ((plcp_ptr[3] & 0x80) ? FLAGS_CHAN_40MHZ : 0) |
((l1p_1 & vVW510021_W_IS_LONGGI) ? 0 : FLAGS_CHAN_SHORTGI);
chanflags |= CHAN_OFDM;
nss = (rate_mcs_index < MAX_HT_MCS) ? nss_for_mcs[rate_mcs_index] : 0;
rate = get_ht_rate(rate_mcs_index, radioflags);
break;
case vVW510021_W_PLCP_GREENFIELD:
/*
* According to section 20.3.2 "PPDU format", the HT-greenfield
* PLCP header just has the HT SIGNAL field (HT-SIG) above, with
* no L-SIG field.
*
* This means that the first octet of HT-SIG is at
* plcp_ptr[0], as there's no L-SIG field to skip.
*
* 0x80 is the CBW 20/40 bit of HT-SIG.
*/
/* set the appropriate flags to indicate HT mode and CB */
rate_mcs_index = vVW510021_W_S2_MCS_INDEX_HT(l1p_1);
radioflags |= FLAGS_CHAN_HT | ((plcp_ptr[0] & 0x80) ? FLAGS_CHAN_40MHZ : 0) |
((l1p_1 & vVW510021_W_IS_LONGGI) ? 0 : FLAGS_CHAN_SHORTGI);
chanflags |= CHAN_OFDM;
nss = (rate_mcs_index < MAX_HT_MCS) ? nss_for_mcs[rate_mcs_index] : 0;
rate = get_ht_rate(rate_mcs_index, radioflags);
break;
case vVW510021_W_PLCP_VHT_MIXED:
/*
* According to section 22.3.2 "VHT PPDU format" of IEEE Std
* 802.11ac-2013, the VHT PLCP header has a "non-HT SIGNAL field"
* (L-SIG), which looks like an 11a SIGNAL field, followed by
* a VHT Signal A field (VHT-SIG-A) described in section
* 22.3.8.3.3 "VHT-SIG-A definition", with training fields
* between it and a VHT Signal B field (VHT-SIG-B) described
* in section 22.3.8.3.6 "VHT-SIG-B definition", followed by
* the PSDU.
*/
{
guint8 SBW = vVW510021_W_BANDWIDTH_VHT(l1p_2);
rate_mcs_index = vVW510021_W_S2_MCS_INDEX_VHT(l1p_1);
radioflags |= FLAGS_CHAN_VHT | ((l1p_1 & vVW510021_W_IS_LONGGI) ? 0 : FLAGS_CHAN_SHORTGI);
chanflags |= CHAN_OFDM;
if (SBW == 3)
radioflags |= FLAGS_CHAN_40MHZ;
else if (SBW == 4)
radioflags |= FLAGS_CHAN_80MHZ;
nss = vVW510021_W_S2_NSS_VHT(l1p_1);
rate = get_vht_rate(rate_mcs_index, radioflags, nss);
}
break;
default:
rate_mcs_index = 0;
nss = 0;
rate = 0.0f;
break;
}
/*
* The MSDU length includes the FCS.
*
* The packet data does *not* include the FCS - it's just 4 bytes
* of junk - so we have to remove it.
*
* We'll be stripping off that junk, so make sure we have at least
* 4 octets worth of packet data.
*
* There seems to be a special case of a length of 0.
*/
if (actual_octets < 4) {
if (actual_octets != 0) {
*err_info = ws_strdup_printf("vwr: Invalid data length %u (too short to include 4 bytes of FCS)",
actual_octets);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
} else {
actual_octets -= 4;
}
/* Calculate start & end times (in sec/usec), converting 64-bit times to usec. */
/* 64-bit times are "Corey-endian" */
s_time = pcoreytohll(&s_trail_ptr[vVW510021_W_STARTT_OFF]);
e_time = pcoreytohll(&s_trail_ptr[vVW510021_W_ENDT_OFF]);
/* find the packet duration (difference between start and end times) */
d_time = (guint32)((e_time - s_time) / NS_IN_US); /* find diff, converting to usec */
/* also convert the packet start time to seconds and microseconds */
start_time = s_time / NS_IN_US; /* convert to microseconds first */
s_sec = (start_time / US_IN_SEC); /* get the number of seconds */
s_usec = start_time - (s_sec * US_IN_SEC); /* get the number of microseconds */
/* also convert the packet end time to seconds and microseconds */
end_time = e_time / NS_IN_US; /* convert to microseconds first */
/* extract the 32 LSBs of the signature timestamp field */
m_ptr = &(rec[8+12]);
pay_off = 42; /* 24 (MAC) + 8 (SNAP) + IP */
sig_off = find_signature(m_ptr, rec_size - 20, pay_off, flow_id, flow_seq);
if (m_ptr[sig_off] == 0xdd)
sig_ts = get_signature_ts(m_ptr, sig_off, rec_size - vVW510021_W_STATS_TRAILER_LEN);
else
sig_ts = 0;
/* Set latency based on rx/tx and signature timestamp */
if (!IS_TX) {
if (tsid < s_time) {
latency = s_time - tsid;
} else {
/* Account for the rollover case. Since we cannot use 0x100000000 - l_time + s_time */
/* we look for a large difference between l_time and s_time. */
delta_b = tsid - s_time;
if (delta_b > 0x10000000)
latency = 0;
else
latency = delta_b;
}
}
/*
* Fill up the per-packet header.
*
* We include the length of the metadata headers in the packet lengths.
*
* The maximum value of actual_octets is 8191, which, even after
* adding the lengths of the metadata headers, is less than
* WTAP_MAX_PACKET_SIZE_STANDARD will ever be, so we don't need to check it.
*/
record->rec_header.packet_header.len = STATS_COMMON_FIELDS_LEN + EXT_WLAN_FIELDS_LEN + actual_octets;
record->rec_header.packet_header.caplen = STATS_COMMON_FIELDS_LEN + EXT_WLAN_FIELDS_LEN + actual_octets;
record->ts.secs = (time_t)s_sec;
record->ts.nsecs = (int)(s_usec * 1000);
record->rec_type = REC_TYPE_PACKET;
record->block = wtap_block_create(WTAP_BLOCK_PACKET);
record->presence_flags = WTAP_HAS_TS;
ws_buffer_assure_space(buf, record->rec_header.packet_header.caplen);
data_ptr = ws_buffer_start_ptr(buf);
/*
* Generate and copy out the common metadata headers,
* set the port type to 0 (WLAN).
*
* All values are copied out in little-endian byte order.
*/
/*** msdu_length = msdu_length + 16; ***/
/* 1st octet of record for port_type and command (command is 0, hence RX) */
phtole8(&data_ptr[bytes_written], WLAN_PORT);
bytes_written += 1;
/* 2nd octet of record for fpga version (0, hence pre-OCTO) */
phtole8(&data_ptr[bytes_written], 0);
bytes_written += 1;
phtoles(&data_ptr[bytes_written], STATS_COMMON_FIELDS_LEN); /* it_len */
bytes_written += 2;
phtoles(&data_ptr[bytes_written], msdu_length);
bytes_written += 2;
phtolel(&data_ptr[bytes_written], flow_id);
bytes_written += 4;
phtoles(&data_ptr[bytes_written], vc_id);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], flow_seq);
bytes_written += 2;
if (!f_tx && sig_ts != 0) {
phtolel(&data_ptr[bytes_written], latency);
} else {
phtolel(&data_ptr[bytes_written], 0);
}
bytes_written += 4;
phtolel(&data_ptr[bytes_written], sig_ts); /* 32 LSBs of signature timestamp (nsec) */
bytes_written += 4;
phtolell(&data_ptr[bytes_written], start_time); /* record start & end times of frame */
bytes_written += 8;
phtolell(&data_ptr[bytes_written], end_time);
bytes_written += 8;
phtolel(&data_ptr[bytes_written], d_time);
bytes_written += 4;
/*
* Generate and copy out the WLAN metadata headers.
*
* All values are copied out in little-endian byte order.
*/
phtoles(&data_ptr[bytes_written], EXT_WLAN_FIELDS_LEN);
bytes_written += 2;
if (info & vVW510021_W_IS_WEP)
radioflags |= FLAGS_WEP;
if (!(l1p_1 & vVW510021_W_IS_LONGPREAMBLE) && (plcp_type == vVW510021_W_PLCP_LEGACY))
radioflags |= FLAGS_SHORTPRE;
phtoles(&data_ptr[bytes_written], radioflags);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], chanflags);
bytes_written += 2;
phyRate = (guint16)(rate * 10);
phtoles(&data_ptr[bytes_written], phyRate);
bytes_written += 2;
data_ptr[bytes_written] = plcp_type;
bytes_written += 1;
data_ptr[bytes_written] = rate_mcs_index;
bytes_written += 1;
data_ptr[bytes_written] = nss;
bytes_written += 1;
data_ptr[bytes_written] = rssi[0];
bytes_written += 1;
data_ptr[bytes_written] = rssi[1];
bytes_written += 1;
data_ptr[bytes_written] = rssi[2];
bytes_written += 1;
data_ptr[bytes_written] = rssi[3];
bytes_written += 1;
/* padding */
data_ptr[bytes_written] = 0;
bytes_written += 1;
/* fill in the VeriWave flags field */
vw_flags = 0;
if (f_tx)
vw_flags |= VW_FLAGS_TXF;
if (errors & 0x1f) /* If any error is flagged, then set the FCS error bit */
vw_flags |= VW_FLAGS_FCSERR;
if (!f_tx && (errors & vwr->CRYPTO_ERR))
vw_flags |= VW_FLAGS_DCRERR;
if (!f_tx && (errors & vwr->RETRY_ERR))
vw_flags |= VW_FLAGS_RETRERR;
if (info & vwr->WEPTYPE)
vw_flags |= VW_FLAGS_IS_WEP;
else if (info & vwr->TKIPTYPE)
vw_flags |= VW_FLAGS_IS_TKIP;
else if (info & vwr->CCMPTYPE)
vw_flags |= VW_FLAGS_IS_CCMP;
phtoles(&data_ptr[bytes_written], vw_flags);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], ht_len);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], info);
bytes_written += 2;
phtolel(&data_ptr[bytes_written], errors);
bytes_written += 4;
/* Finally, copy the whole MAC frame to the packet buffer as-is.
* This does not include the stats header or the PLCP.
* This also does not include the last 4 bytes, as those don't
* contain an FCS, they just contain junk.
*/
memcpy(&data_ptr[bytes_written], &rec[vwr->MPDU_OFF], actual_octets);
return TRUE;
}
static gboolean vwr_read_s3_W_rec(vwr_t *vwr, wtap_rec *record,
Buffer *buf, const guint8 *rec, int rec_size,
int IS_TX, int log_mode, int *err,
gchar **err_info)
{
guint8 *data_ptr;
int bytes_written = 0; /* bytes output to buf so far */
int i;
int stats_offset = 0;
const guint8 *s_start_ptr = NULL,*s_trail_ptr = NULL, *plcp_ptr, *m_ptr; /* stats & MPDU ptr */
guint32 msdu_length = 0, actual_octets = 0; /* octets in frame */
guint8 l1p_1 = 0,l1p_2 = 0, plcp_type, rate_mcs_index, nss; /* mod (CCK-L/CCK-S/OFDM) */
guint64 s_time = LL_ZERO, e_time = LL_ZERO; /* start/end */
/* times, nsec */
guint64 latency = LL_ZERO;
guint64 start_time = 0, s_sec = 0, s_usec = LL_ZERO; /* start time, sec + usec */
guint64 end_time = 0; /* end time */
guint16 info = 0; /* INFO/ERRORS fields in stats blk */
guint32 errors = 0;
gint8 info_2nd = 0,rssi[] = {0,0,0,0}; /* RSSI, signed 8-bit number */
int frame_size;
guint32 d_time = 0, flow_id = 0; /* packet duration, Flow Signature ID*/
int sig_off, pay_off; /* MAC+SNAP header len, signature offset */
guint64 sig_ts = 0, tsid; /* 32 LSBs of timestamp in signature */
guint64 delta_b; /* Used for calculating latency */
guint8 L1InfoC = 0, port_type, ver_fpga = 0;
guint8 flow_seq =0,plcp_hdr_flag = 0,rf_id = 0; /* indicates plcp hdr info */
const guint8 *rf_ptr = NULL;
float rate;
guint16 phyRate;
/*
* The record data must be large enough to hold the statistics header,
* the PLCP, and the statistics trailer.
*/
if (IS_TX == 3) { /*IS_TX =3, i.e., command type is RF Modified*/
if ((guint)rec_size < OCTO_MODIFIED_RF_LEN) {
*err_info = ws_strdup_printf("vwr: Invalid record length %d (must be at least %u)",
rec_size,
OCTO_MODIFIED_RF_LEN);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
rf_ptr = &(rec[0]);
rf_id = rf_ptr[0];
/*
* Fill up the per-packet header.
*
* We include the length of the metadata headers in the packet lengths.
*
* OCTO_MODIFIED_RF_LEN + 1 is less than WTAP_MAX_PACKET_SIZE_STANDARD will
* ever be, so we don't need to check it.
*/
record->rec_header.packet_header.len = OCTO_MODIFIED_RF_LEN + 1; /* 1st octet is reserved for detecting type of frame while displaying in wireshark */
record->rec_header.packet_header.caplen = OCTO_MODIFIED_RF_LEN + 1;
record->ts.secs = (time_t)s_sec;
record->ts.nsecs = (int)(s_usec * 1000);
record->rec_type = REC_TYPE_PACKET;
record->block = wtap_block_create(WTAP_BLOCK_PACKET);
record->presence_flags = WTAP_HAS_TS;
ws_buffer_assure_space(buf, record->rec_header.packet_header.caplen);
data_ptr = ws_buffer_start_ptr(buf);
port_type = IS_TX << 4;
nss = 0;
phyRate = 0;
}
else {
/* Calculate the start of the statistics blocks in the buffer */
/* Also get a bunch of fields from the stats blocks */
/* 'stats_offset' variable is use to locate the exact offset.
* When a RX frame contrains RF,
* the position of Stats, Layer 1-4, PLCP parameters are shifted to
* + OCTO_RF_MOD_ACTUAL_LEN bytes
*/
if (IS_TX == 4) /*IS_TX =4, i.e., command type is RF-RX Modified*/
{
stats_offset = OCTO_RF_MOD_ACTUAL_LEN;
if ((guint)rec_size < stats_offset + vwr->MPDU_OFF + vVW510021_W_STATS_TRAILER_LEN) {
*err_info = ws_strdup_printf("vwr: Invalid record length %d (must be at least %u)",
rec_size,
stats_offset + vwr->MPDU_OFF + vVW510021_W_STATS_TRAILER_LEN);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
rf_ptr = &(rec[0]);
rf_id = rf_ptr[0];
}
else
{
stats_offset = 0;
if ((guint)rec_size < vwr->MPDU_OFF + vVW510021_W_STATS_TRAILER_LEN) {
*err_info = ws_strdup_printf("vwr: Invalid record length %d (must be at least %u)",
rec_size,
vwr->MPDU_OFF + vVW510021_W_STATS_TRAILER_LEN);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
}
s_start_ptr = &(rec[stats_offset]); /* point to stats header */
s_trail_ptr = &(rec[rec_size - vVW510021_W_STATS_TRAILER_LEN] ); /* point to stats trailer */
l1p_1 = s_start_ptr[vVW510021_W_L1P_1_OFF];
l1p_2 = s_start_ptr[vVW510021_W_L1P_2_OFF];
plcp_type = vVW510021_W_S3_PLCP_TYPE(l1p_2);
switch (plcp_type)
{
case vVW510021_W_PLCP_LEGACY:
/* pre-HT */
rate_mcs_index = vVW510021_W_S3_RATE_INDEX(l1p_1);
nss = 0;
break;
case vVW510021_W_PLCP_MIXED:
case vVW510021_W_PLCP_GREENFIELD:
rate_mcs_index = vVW510021_W_S3_MCS_INDEX_HT(l1p_1);
nss = (rate_mcs_index < MAX_HT_MCS) ? nss_for_mcs[rate_mcs_index] : 0;
break;
case vVW510021_W_PLCP_VHT_MIXED:
rate_mcs_index = vVW510021_W_S3_MCS_INDEX_VHT(l1p_1);
nss = vVW510021_W_S3_NSS_VHT(l1p_1);
plcp_hdr_flag = 1;
break;
default:
rate_mcs_index = 0;
nss = 0;
plcp_hdr_flag = 0;
break;
}
for (i = 0; i < 4; i++)
{
if (IS_TX == 1)
{
rssi[i] = (s_start_ptr[4+i] & 0x80) ? -1 * (s_start_ptr[4+i] & 0x7f) : s_start_ptr[4+i] & 0x7f;
}
else
{
rssi[i] = (s_start_ptr[4+i] >= 128) ? (s_start_ptr[4+i] - 256) : s_start_ptr[4+i];
}
}
if (IS_TX == 0 || IS_TX == 4){
L1InfoC = s_start_ptr[8];
}
msdu_length = pntoh24(&s_start_ptr[9]);
/*** 16 bytes of PLCP header + 1 byte of L1P for user position ***/
plcp_ptr = &(rec[stats_offset+16]);
/*** Add the PLCP length for S3_W_FPGA version VHT frames for Beamforming decode ***/
if (log_mode == 3) {
frame_size = rec_size - (stats_offset + vwr->MPDU_OFF + vVW510021_W_STATS_TRAILER_LEN);
if (frame_size > ((int) msdu_length))
actual_octets = msdu_length;
else {
/*
* XXX - does this mean "the packet was cut short during
* capture" or "this is a malformed record"?
*/
actual_octets = frame_size;
}
}
else
{
actual_octets = msdu_length;
}
/*
* Sanity check the octets field to determine if it's greater than
* the packet data available in the record - i.e., the record size
* minus the sum of (length of statistics header + PLCP) and
* (length of statistics trailer).
*
* Report an error if it is.
*/
if (actual_octets > rec_size - (stats_offset + vwr->MPDU_OFF + vVW510021_W_STATS_TRAILER_LEN)) {
*err_info = ws_strdup_printf("vwr: Invalid data length %u (runs past the end of the record)",
actual_octets);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
flow_seq = s_trail_ptr[vVW510021_W_FLOWSEQ_OFF];
latency = 0x00000000; /* clear latency */
flow_id = pntoh24(&s_trail_ptr[vVW510021_W_FLOWID_OFF]); /* all 24 bits valid */
/* For tx latency is duration, for rx latency is timestamp */
/* Get 48-bit latency value */
tsid = pcorey48tohll(&s_trail_ptr[vVW510021_W_LATVAL_OFF]);
errors = pntoh32(&s_trail_ptr[vVW510021_W_ERRORS_OFF]);
info = pntoh16(&s_trail_ptr[vVW510021_W_INFO_OFF]);
if (IS_TX == 0 || IS_TX == 4)
info_2nd = s_trail_ptr[41];
/*** Calculate Data rate based on
* PLCP type, MCS index and number of spatial stream
* radioflags is temporarily calculated, which is used in
* get_ht_rate() and get_vht_rate().
**/
switch (plcp_type)
{
case vVW510021_W_PLCP_LEGACY:
rate = get_legacy_rate(rate_mcs_index);
break;
case vVW510021_W_PLCP_MIXED:
/*
* According to section 20.3.2 "PPDU format", the HT-mixed
* PLCP header has a "Non-HT SIGNAL field" (L-SIG), which
* looks like an 11a SIGNAL field, followed by an HT SIGNAL
* field (HT-SIG) described in section 20.3.9.4.3 "HT-SIG
* definition".
*
* This means that the first octet of HT-SIG is at
* plcp_ptr[3], skipping the 3 octets of the L-SIG field.
*
* 0x80 is the CBW 20/40 bit of HT-SIG.
*/
{
/* set the appropriate flags to indicate HT mode and CB */
guint16 radioflags = FLAGS_CHAN_HT | ((plcp_ptr[3] & 0x80) ? FLAGS_CHAN_40MHZ : 0) |
((l1p_1 & vVW510021_W_IS_LONGGI) ? 0 : FLAGS_CHAN_SHORTGI);
rate = get_ht_rate(rate_mcs_index, radioflags);
}
break;
case vVW510021_W_PLCP_GREENFIELD:
/*
* According to section 20.3.2 "PPDU format", the HT-greenfield
* PLCP header just has the HT SIGNAL field (HT-SIG) above, with
* no L-SIG field.
*
* This means that the first octet of HT-SIG is at
* plcp_ptr[0], as there's no L-SIG field to skip.
*
* 0x80 is the CBW 20/40 bit of HT-SIG.
*/
{
/* set the appropriate flags to indicate HT mode and CB */
guint16 radioflags = FLAGS_CHAN_HT | ((plcp_ptr[0] & 0x80) ? FLAGS_CHAN_40MHZ : 0) |
((l1p_1 & vVW510021_W_IS_LONGGI) ? 0 : FLAGS_CHAN_SHORTGI);
rate = get_ht_rate(rate_mcs_index, radioflags);
}
break;
case vVW510021_W_PLCP_VHT_MIXED:
/*
* According to section 22.3.2 "VHT PPDU format" of IEEE Std
* 802.11ac-2013, the VHT PLCP header has a "non-HT SIGNAL field"
* (L-SIG), which looks like an 11a SIGNAL field, followed by
* a VHT Signal A field (VHT-SIG-A) described in section
* 22.3.8.3.3 "VHT-SIG-A definition", with training fields
* between it and a VHT Signal B field (VHT-SIG-B) described
* in section 22.3.8.3.6 "VHT-SIG-B definition", followed by
* the PSDU.
*/
{
guint8 SBW = vVW510021_W_BANDWIDTH_VHT(l1p_2);
guint16 radioflags = FLAGS_CHAN_VHT | ((l1p_1 & vVW510021_W_IS_LONGGI) ? 0 : FLAGS_CHAN_SHORTGI);
if (SBW == 3)
radioflags |= FLAGS_CHAN_40MHZ;
else if (SBW == 4)
radioflags |= FLAGS_CHAN_80MHZ;
rate = get_vht_rate(rate_mcs_index, radioflags, nss);
}
break;
default:
rate = 0.0f;
break;
}
phyRate = (guint16)(rate * 10);
/* Calculation of Data rate ends*/
/* 'ver_fpga' is the 2nd Octet of each frame.
* msb/lsb nibble indicates log mode/fpga version respectively.
* where log mode = 0 is normal capture and 1 is reduced capture,
* lsb nibble is set to 1 always as this function is applicable for only FPGA version >= 48
*/
if (log_mode == 3) {
if (frame_size >= (int) msdu_length) {
/*
* The MSDU length includes the FCS.
*
* The packet data does *not* include the FCS - it's just 4
* bytes of junk - so we have to remove it.
*
* We'll be stripping off that junk, so make sure we have at
* least 4 octets worth of packet data.
*
* XXX - is the FCS actually present here, as it appears to be
* if log_mode isn't 3?
*
* There seems to be a special case of a length of 0.
*/
if (actual_octets < 4) {
if (actual_octets != 0) {
*err_info = ws_strdup_printf("vwr: Invalid data length %u (too short to include 4 bytes of FCS)",
actual_octets);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
} else {
actual_octets -= 4;
}
}
ver_fpga = 0x11;
} else {
ver_fpga = 0x01;
}
/* Calculate start & end times (in sec/usec), converting 64-bit times to usec. */
/* 64-bit times are "Corey-endian" */
s_time = pcoreytohll(&s_trail_ptr[vVW510021_W_STARTT_OFF]);
e_time = pcoreytohll(&s_trail_ptr[vVW510021_W_ENDT_OFF]);
/* find the packet duration (difference between start and end times) */
d_time = (guint32)((e_time - s_time) / NS_IN_US); /* find diff, converting to usec */
/* also convert the packet start time to seconds and microseconds */
start_time = s_time / NS_IN_US; /* convert to microseconds first */
s_sec = (start_time / US_IN_SEC); /* get the number of seconds */
s_usec = start_time - (s_sec * US_IN_SEC); /* get the number of microseconds */
/* also convert the packet end time to seconds and microseconds */
end_time = e_time / NS_IN_US; /* convert to microseconds first */
/* extract the 32 LSBs of the signature timestamp field */
int m_ptr_offset = stats_offset + 8 + 12;
m_ptr = rec + m_ptr_offset;
pay_off = 42; /* 24 (MAC) + 8 (SNAP) + IP */
sig_off = find_signature(m_ptr, rec_size - m_ptr_offset, pay_off, flow_id, flow_seq);
if (m_ptr[sig_off] == 0xdd)
sig_ts = get_signature_ts(m_ptr, sig_off, rec_size - vVW510021_W_STATS_TRAILER_LEN);
else
sig_ts = 0;
/* Set latency based on rx/tx and signature timestamp */
if (IS_TX == 0 || IS_TX == 4) {
if (tsid < s_time) {
latency = s_time - tsid;
} else {
/* Account for the rollover case. Since we cannot use 0x100000000 - l_time + s_time */
/* we look for a large difference between l_time and s_time. */
delta_b = tsid - s_time;
if (delta_b > 0x10000000)
latency = 0;
else
latency = delta_b;
}
}
port_type = IS_TX << 4;
/*
* Fill up the per-packet header.
*
* We include the length of the metadata headers in the packet lengths.
*/
if (IS_TX == 4) {
record->rec_header.packet_header.len = OCTO_MODIFIED_RF_LEN + OCTO_TIMESTAMP_FIELDS_LEN + OCTO_LAYER1TO4_LEN + actual_octets;
record->rec_header.packet_header.caplen = OCTO_MODIFIED_RF_LEN + OCTO_TIMESTAMP_FIELDS_LEN + OCTO_LAYER1TO4_LEN + actual_octets;
} else {
record->rec_header.packet_header.len = OCTO_TIMESTAMP_FIELDS_LEN + OCTO_LAYER1TO4_LEN + actual_octets;
record->rec_header.packet_header.caplen = OCTO_TIMESTAMP_FIELDS_LEN + OCTO_LAYER1TO4_LEN + actual_octets;
}
if (record->rec_header.packet_header.caplen > WTAP_MAX_PACKET_SIZE_STANDARD) {
/*
* Probably a corrupt capture file; return an error,
* so that our caller doesn't blow up trying to allocate
* space for an immensely-large packet.
*/
*err_info = ws_strdup_printf("vwr: File has %u-byte packet, bigger than maximum of %u",
record->rec_header.packet_header.caplen, WTAP_MAX_PACKET_SIZE_STANDARD);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
record->ts.secs = (time_t)s_sec;
record->ts.nsecs = (int)(s_usec * 1000);
record->rec_type = REC_TYPE_PACKET;
record->block = wtap_block_create(WTAP_BLOCK_PACKET);
record->presence_flags = WTAP_HAS_TS;
ws_buffer_assure_space(buf, record->rec_header.packet_header.caplen);
data_ptr = ws_buffer_start_ptr(buf);
}
/*
* Generate and copy out the common metadata headers,
* set the port type to port_type (XXX).
*
* All values are copied out in little-endian byte order.
*/
/*** msdu_length = msdu_length + 16; ***/
/* 1st octet of record for port_type and other crud */
phtole8(&data_ptr[bytes_written], port_type);
bytes_written += 1;
if (IS_TX != 3) {
phtole8(&data_ptr[bytes_written], ver_fpga); /* 2nd octet of record for FPGA version*/
bytes_written += 1;
phtoles(&data_ptr[bytes_written], OCTO_TIMESTAMP_FIELDS_LEN); /* it_len */
bytes_written += 2;
/*** Time Collapsible header started***/
if (IS_TX == 1 && sig_ts != 0) {
phtolel(&data_ptr[bytes_written], latency);
} else {
phtolel(&data_ptr[bytes_written], 0);
}
bytes_written += 4;
phtolel(&data_ptr[bytes_written], sig_ts); /* 32 LSBs of signature timestamp (nsec) */
bytes_written += 4;
phtolell(&data_ptr[bytes_written], start_time); /* record start & end times of frame */
bytes_written += 8;
phtolell(&data_ptr[bytes_written], end_time);
bytes_written += 8;
phtolel(&data_ptr[bytes_written], d_time);
bytes_written += 4;
/*** Time Collapsible header ends ***/
}
/*** RF Collapsible header starts***/
if (IS_TX == 3 || IS_TX == 4) {
phtole8(&data_ptr[bytes_written], rf_id);
bytes_written += 1;
data_ptr[bytes_written] = 0;
bytes_written += 1;
data_ptr[bytes_written] = 0;
bytes_written += 1;
data_ptr[bytes_written] = 0;
bytes_written += 1;
/*** NOISE for all 4 Ports ***/
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[RF_PORT_1_NOISE_OFF+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_NOISE_OFF+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_NOISE_OFF+1+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
/*** SNR for all 4 Ports ***/
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[RF_PORT_1_SNR_OFF+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_SNR_OFF+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_SNR_OFF+1+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
/*** PFE for all 4 Ports ***/
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[RF_PORT_1_PFE_OFF+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_PFE_OFF+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_PFE_OFF+1+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
/*** EVM SIG Data for all 4 Ports ***/
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[RF_PORT_1_EVM_SD_SIG_OFF+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_EVM_SD_SIG_OFF+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_EVM_SD_SIG_OFF+1+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
/*** EVM SIG PILOT for all 4 Ports ***/
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[RF_PORT_1_EVM_SP_SIG_OFF+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_EVM_SP_SIG_OFF+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_EVM_SP_SIG_OFF+1+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
/*** EVM Data Data for all 4 Ports ***/
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[RF_PORT_1_EVM_SD_DATA_OFF+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_EVM_SD_DATA_OFF+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_EVM_SD_DATA_OFF+1+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
/*** EVM Data PILOT for all 4 Ports ***/
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[RF_PORT_1_EVM_SP_DATA_OFF+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_EVM_SP_DATA_OFF+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_EVM_SP_DATA_OFF+1+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
/*** EVM WORST SYMBOL for all 4 Ports ***/
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[RF_PORT_1_DSYMBOL_IDX_OFF+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_DSYMBOL_IDX_OFF+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_DSYMBOL_IDX_OFF+1+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
/*** CONTEXT_P for all 4 Ports ***/
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[RF_PORT_1_CONTEXT_OFF+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_CONTEXT_OFF+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[RF_PORT_1_CONTEXT_OFF+1+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
/*** FOR rest 24 RF data bytes are commented for future use ***/
/***
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[20+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[20+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[21+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[24+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[24+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[25+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
for (i = 0; i < RF_NUMBER_OF_PORTS; i++)
{
if (pntoh16(&rf_ptr[26+i*RF_INTER_PORT_GAP_OFF]) == 0) {
phtoles(&data_ptr[bytes_written], 0);
bytes_written += 2;
} else {
data_ptr[bytes_written] = rf_ptr[26+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
data_ptr[bytes_written] = rf_ptr[27+i*RF_INTER_PORT_GAP_OFF];
bytes_written += 1;
}
}
***/
}
/*** RF Collapsible header ends***/
if (IS_TX != 3) {
/*
* Generate and copy out the WLAN metadata headers.
*
* All values are copied out in little-endian byte order.
*/
phtoles(&data_ptr[bytes_written], OCTO_LAYER1TO4_LEN);
bytes_written += 2;
/*** Layer-1 Collapsible header started***/
data_ptr[bytes_written] = l1p_1;
bytes_written += 1;
data_ptr[bytes_written] = (nss << 4) | IS_TX;
bytes_written += 1;
phtoles(&data_ptr[bytes_written], phyRate); /* To dosplay Data rate based on the PLCP type & MCS*/
bytes_written += 2;
data_ptr[bytes_written] = l1p_2;
bytes_written += 1;
data_ptr[bytes_written] = rssi[0];
bytes_written += 1;
data_ptr[bytes_written] = rssi[1];
bytes_written += 1;
data_ptr[bytes_written] = rssi[2];
bytes_written += 1;
data_ptr[bytes_written] = rssi[3];
bytes_written += 1;
/* padding may not be required for S3_W*/
data_ptr[bytes_written] = s_start_ptr[2]; /*** For Signal Bandwidth Mask ***/
bytes_written += 1;
data_ptr[bytes_written] = s_start_ptr[3]; /*** For Antenna Port Energy Detect and MU_MASK ***/
bytes_written += 1;
if (plcp_hdr_flag == 1 && (IS_TX == 0 || IS_TX == 4)) {
data_ptr[bytes_written] = L1InfoC; /*** For Other plcp type = VHT ***/
} else {
data_ptr[bytes_written] = 0; /*** For Other plcp type, this offset is set to 0***/
}
bytes_written += 1;
phtoles(&data_ptr[bytes_written], msdu_length);
bytes_written += 2;
/*** Layer-1 Collapsible header Ends ***/
/*** PLCP Collapsible header Starts ***/
memcpy(&data_ptr[bytes_written], &rec[stats_offset+16], 16);
bytes_written += 16;
/*** PLCP Collapsible header Ends ***/
/*** Layer 2-4 Collapsible header Starts ***/
phtolel(&data_ptr[bytes_written], pntoh32(&s_start_ptr[12])); /*** This 4 bytes includes BM,BV,CV,BSSID and ClientID ***/
bytes_written += 4;
phtoles(&data_ptr[bytes_written], pntoh16(&s_trail_ptr[20])); /*** 2 bytes includes FV,QT,HT,L4V,TID and WLAN type ***/
bytes_written += 2;
data_ptr[bytes_written] = flow_seq;
bytes_written += 1;
phtole24(&data_ptr[bytes_written], flow_id);
bytes_written += 3;
phtoles(&data_ptr[bytes_written], pntoh16(&s_trail_ptr[28])); /*** 2 bytes for Layer 4 ID ***/
bytes_written += 2;
phtolel(&data_ptr[bytes_written], pntoh32(&s_trail_ptr[24])); /*** 4 bytes for Payload Decode ***/
bytes_written += 4;
/*** In case of RX, Info has 3 bytes of data, whereas for TX, 2 bytes ***/
if (IS_TX == 0 || IS_TX == 4) {
phtoles(&data_ptr[bytes_written], info);
bytes_written += 2;
data_ptr[bytes_written] = info_2nd;
bytes_written += 1;
}
else {
phtoles(&data_ptr[bytes_written], info);
bytes_written += 2;
data_ptr[bytes_written] = 0;
bytes_written += 1;
}
phtolel(&data_ptr[bytes_written], errors);
bytes_written += 4;
/*** Layer 2-4 Collapsible header Ends ***/
/* Finally, copy the whole MAC frame to the packet buffer as-is.
* This does not include the stats header or the PLCP.
* This also does not include the last 4 bytes, as those don't
* contain an FCS, they just contain junk.
*/
memcpy(&data_ptr[bytes_written], &rec[stats_offset+(vwr->MPDU_OFF)], actual_octets);
}
return TRUE;
}
/* read an Ethernet packet */
/* Copy the actual packet data from the capture file into the target data block. */
/* The packet is constructed as a 38-byte VeriWave-extended Radiotap header plus the raw */
/* MAC octets. */
static gboolean vwr_read_rec_data_ethernet(vwr_t *vwr, wtap_rec *record,
Buffer *buf, const guint8 *rec,
int rec_size, int IS_TX, int *err,
gchar **err_info)
{
guint8 *data_ptr;
int bytes_written = 0; /* bytes output to buf so far */
const guint8 *s_ptr, *m_ptr; /* stats and MPDU pointers */
guint16 msdu_length, actual_octets; /* octets in frame */
guint flow_seq; /* seqnum */
guint64 s_time = LL_ZERO, e_time = LL_ZERO; /* start/end */
/* times, nsec */
guint32 latency = 0;
guint64 start_time, s_sec = LL_ZERO, s_usec = LL_ZERO; /* start time, sec + usec */
guint64 end_time; /* end time */
guint l4id;
guint16 info, validityBits; /* INFO/ERRORS fields in stats */
guint32 errors;
guint16 vc_id; /* VC ID, total (incl of aggregates) */
guint32 flow_id, d_time; /* packet duration */
int f_flow; /* flags: flow valid */
guint32 frame_type; /* frame type field */
int mac_len, sig_off, pay_off; /* MAC header len, signature offset */
/* XXX - the code here fetched tsid, but never used it! */
guint64 sig_ts/*, tsid*/; /* 32 LSBs of timestamp in signature */
guint64 delta_b; /* Used for calculating latency */
guint16 vw_flags; /* VeriWave-specific packet flags */
if ((guint)rec_size < vwr->STATS_LEN) {
*err_info = ws_strdup_printf("vwr: Invalid record length %d (must be at least %u)", rec_size, vwr->STATS_LEN);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
/* Calculate the start of the statistics block in the buffer. */
/* Also get a bunch of fields from the stats block. */
m_ptr = &(rec[0]); /* point to the data block */
s_ptr = &(rec[rec_size - vwr->STATS_LEN]); /* point to the stats block */
msdu_length = pntoh16(&s_ptr[vwr->OCTET_OFF]);
actual_octets = msdu_length;
/*
* Sanity check the octets field to determine if it's greater than
* the packet data available in the record - i.e., the record size
* minus the length of the statistics block.
*
* Report an error if it is.
*/
if (actual_octets > rec_size - vwr->STATS_LEN) {
*err_info = ws_strdup_printf("vwr: Invalid data length %u (runs past the end of the record)",
actual_octets);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
vc_id = pntoh16(&s_ptr[vwr->VCID_OFF]) & vwr->VCID_MASK;
flow_seq = s_ptr[vwr->FLOWSEQ_OFF];
frame_type = pntoh32(&s_ptr[vwr->FRAME_TYPE_OFF]);
if (vwr->FPGA_VERSION == vVW510024_E_FPGA) {
validityBits = pntoh16(&s_ptr[vwr->VALID_OFF]);
f_flow = validityBits & vwr->FLOW_VALID;
mac_len = (validityBits & vwr->IS_VLAN) ? 16 : 14; /* MAC hdr length based on VLAN tag */
errors = pntoh16(&s_ptr[vwr->ERRORS_OFF]);
}
else {
f_flow = s_ptr[vwr->VALID_OFF] & vwr->FLOW_VALID;
mac_len = (frame_type & vwr->IS_VLAN) ? 16 : 14; /* MAC hdr length based on VLAN tag */
/* for older fpga errors is only represented by 16 bits) */
errors = pntoh16(&s_ptr[vwr->ERRORS_OFF]);
}
info = pntoh16(&s_ptr[vwr->INFO_OFF]);
/* 24 LSBs */
flow_id = pntoh24(&s_ptr[vwr->FLOWID_OFF]);
#if 0
/* For tx latency is duration, for rx latency is timestamp. */
/* Get 64-bit latency value. */
tsid = pcorey48tohll(&s_ptr[vwr->LATVAL_OFF]);
#endif
l4id = pntoh16(&s_ptr[vwr->L4ID_OFF]);
/*
* The MSDU length includes the FCS.
*
* The packet data does *not* include the FCS - it's just 4 bytes
* of junk - so we have to remove it.
*
* We'll be stripping off that junk, so make sure we have at least
* 4 octets worth of packet data.
*
* There seems to be a special case of a length of 0.
*/
if (actual_octets < 4) {
if (actual_octets != 0) {
*err_info = ws_strdup_printf("vwr: Invalid data length %u (too short to include 4 bytes of FCS)",
actual_octets);
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
} else {
actual_octets -= 4;
}
/* Calculate start & end times (in sec/usec), converting 64-bit times to usec. */
/* 64-bit times are "Corey-endian" */
s_time = pcoreytohll(&s_ptr[vwr->STARTT_OFF]);
e_time = pcoreytohll(&s_ptr[vwr->ENDT_OFF]);
/* find the packet duration (difference between start and end times) */
d_time = (guint32)((e_time - s_time)); /* find diff, leaving in nsec for Ethernet */
/* also convert the packet start time to seconds and microseconds */
start_time = s_time / NS_IN_US; /* convert to microseconds first */
s_sec = (start_time / US_IN_SEC); /* get the number of seconds */
s_usec = start_time - (s_sec * US_IN_SEC); /* get the number of microseconds */
/* also convert the packet end time to seconds and microseconds */
end_time = e_time / NS_IN_US; /* convert to microseconds first */
if (frame_type & vwr->IS_TCP) /* signature offset for TCP frame */
{
pay_off = mac_len + 40;
}
else if (frame_type & vwr->IS_UDP) /* signature offset for UDP frame */
{
pay_off = mac_len + 28;
}
else if (frame_type & vwr->IS_ICMP) /* signature offset for ICMP frame */
{
pay_off = mac_len + 24;
}
else if (frame_type & vwr->IS_IGMP) /* signature offset for IGMPv2 frame */
{
pay_off = mac_len + 28;
}
else /* signature offset for raw IP frame */
{
pay_off = mac_len + 20;
}
sig_off = find_signature(m_ptr, rec_size, pay_off, flow_id, flow_seq);
if ((m_ptr[sig_off] == 0xdd) && (f_flow != 0))
sig_ts = get_signature_ts(m_ptr, sig_off, msdu_length);
else
sig_ts = 0;
/* Set latency based on rx/tx and signature timestamp */
if (!IS_TX) {
if (sig_ts < s_time) {
latency = (guint32)(s_time - sig_ts);
} else {
/* Account for the rollover case. Since we cannot use 0x100000000 - l_time + s_time */
/* we look for a large difference between l_time and s_time. */
delta_b = sig_ts - s_time;
if (delta_b > 0x10000000) {
latency = 0;
} else
latency = (guint32)delta_b;
}
}
/*
* Fill up the per-packet header.
*
* We include the length of the metadata headers in the packet lengths.
*
* The maximum value of actual_octets is 65535, which, even after
* adding the lengths of the metadata headers, is less than
* WTAP_MAX_PACKET_SIZE_STANDARD will ever be, so we don't need to check it.
*/
record->rec_header.packet_header.len = STATS_COMMON_FIELDS_LEN + EXT_ETHERNET_FIELDS_LEN + actual_octets;
record->rec_header.packet_header.caplen = STATS_COMMON_FIELDS_LEN + EXT_ETHERNET_FIELDS_LEN + actual_octets;
record->ts.secs = (time_t)s_sec;
record->ts.nsecs = (int)(s_usec * 1000);
record->rec_type = REC_TYPE_PACKET;
record->block = wtap_block_create(WTAP_BLOCK_PACKET);
record->presence_flags = WTAP_HAS_TS;
/*etap_hdr.vw_ip_length = (guint16)ip_len;*/
ws_buffer_assure_space(buf, record->rec_header.packet_header.caplen);
data_ptr = ws_buffer_start_ptr(buf);
/*
* Generate and copy out the common metadata headers,
* set the port type to 1 (Ethernet).
*
* All values are copied out in little-endian byte order.
*/
/* 1st octet of record for port_type and command (command is 0, hence RX) */
phtole8(&data_ptr[bytes_written], ETHERNET_PORT);
bytes_written += 1;
/* 2nd octet of record for fpga version (Ethernet, hence non-OCTO) */
phtole8(&data_ptr[bytes_written], 0);
bytes_written += 1;
phtoles(&data_ptr[bytes_written], STATS_COMMON_FIELDS_LEN);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], msdu_length);
bytes_written += 2;
phtolel(&data_ptr[bytes_written], flow_id);
bytes_written += 4;
phtoles(&data_ptr[bytes_written], vc_id);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], flow_seq);
bytes_written += 2;
if (!IS_TX && (sig_ts != 0)) {
phtolel(&data_ptr[bytes_written], latency);
} else {
phtolel(&data_ptr[bytes_written], 0);
}
bytes_written += 4;
phtolel(&data_ptr[bytes_written], sig_ts);
bytes_written += 4;
phtolell(&data_ptr[bytes_written], start_time) /* record start & end times of frame */
bytes_written += 8;
phtolell(&data_ptr[bytes_written], end_time);
bytes_written += 8;
phtolel(&data_ptr[bytes_written], d_time);
bytes_written += 4;
/*
* Generate and copy out the Ethernet metadata headers.
*
* All values are copied out in little-endian byte order.
*/
phtoles(&data_ptr[bytes_written], EXT_ETHERNET_FIELDS_LEN);
bytes_written += 2;
vw_flags = 0;
if (IS_TX)
vw_flags |= VW_FLAGS_TXF;
if (errors & vwr->FCS_ERROR)
vw_flags |= VW_FLAGS_FCSERR;
phtoles(&data_ptr[bytes_written], vw_flags);
bytes_written += 2;
phtoles(&data_ptr[bytes_written], info);
bytes_written += 2;
phtolel(&data_ptr[bytes_written], errors);
bytes_written += 4;
phtolel(&data_ptr[bytes_written], l4id);
bytes_written += 4;
/* Add in pad */
phtolel(&data_ptr[bytes_written], 0);
bytes_written += 4;
/*
* Finally, copy the whole MAC frame to the packet buffer as-is.
* This also does not include the last 4 bytes, as those don't
* contain an FCS, they just contain junk.
*/
memcpy(&data_ptr[bytes_written], m_ptr, actual_octets);
return TRUE;
}
/*--------------------------------------------------------------------------------------*/
/* utility to split up and decode a 16-byte message record */
static int decode_msg(vwr_t *vwr, guint8 *rec, int *v_type, int *IS_TX, int *log_mode)
{
guint8 cmd,fpga_log_mode; /* components of message */
guint32 wd2, wd3;
int v_size; /* size of var-len message */
/* break up the message record into its pieces */
cmd = rec[0];
fpga_log_mode = rec[1];
fpga_log_mode = ((fpga_log_mode & 0x30) >> 4);
wd2 = pntoh32(&rec[8]);
wd3 = pntoh32(&rec[12]);
if (vwr != NULL)
*log_mode = fpga_log_mode; /* Log mode = 3, when MPDU data is reduced */
/* now decode based on the command byte */
switch (cmd) {
case COMMAND_RX:
if (vwr != NULL) {
*IS_TX = 0;
}
v_size = (int)(wd2 & 0xffff);
*v_type = VT_FRAME;
break;
case COMMAND_TX:
if (vwr != NULL) {
*IS_TX = 1;
}
v_size = (int)(wd2 & 0xffff);
*v_type = VT_FRAME;
break;
/*
case COMMAND_RFN:
if (vwr != NULL) {
*IS_TX = 3;
}
v_size = (int)(wd2 & 0xffff);
*v_type = VT_FRAME;
break;
*/
case COMMAND_RF: /* For RF Modified only */
if (vwr != NULL) {
*IS_TX = 3;
}
v_size = (int)(wd2 & 0xffff);
*v_type = VT_FRAME;
break;
case COMMAND_RFRX: /* For RF_RX Modified only */
if (vwr != NULL) {
*IS_TX = 4;
}
v_size = (int)(wd2 & 0xffff);
*v_type = VT_FRAME;
break;
case 0xc1:
case 0x8b:
case 0xbb:
if (vwr != NULL) {
*IS_TX = 2;
}
v_size = (int)(wd2 & 0xffff);
*v_type = VT_CPMSG;
break;
case 0xfe:
if (vwr != NULL) {
*IS_TX = 2;
}
v_size = (int)(wd3 & 0xffff);
*v_type = VT_CPMSG;
break;
default:
if (vwr != NULL) {
*IS_TX = 2;
}
v_size = 0;
*v_type = VT_UNKNOWN;
break;
}
return v_size;
}
/*---------------------------------------------------------------------------------------*/
/* Utilities to extract and decode the PHY bit rate from 802.11 PLCP headers (OFDM/CCK). */
/* They are passed a pointer to 4 or 6 consecutive bytes of PLCP header. */
/* The integer returned by the get_xxx_rate() functions is in units of 0.5 Mb/s. */
/* The string returned by the decode_xxx_rate() functions is 3 characters wide. */
static guint8 get_ofdm_rate(const guint8 *plcp)
{
/* extract the RATE field (LS nibble of first byte) then convert it to the MCS index used by the L1p fields */
switch (plcp[0] & 0x0f) {
case 0x0b: return 4;
case 0x0f: return 5;
case 0x0a: return 6;
case 0x0e: return 7;
case 0x09: return 8;
case 0x0d: return 9;
case 0x08: return 10;
case 0x0c: return 11;
default: return 0;
}
}
static guint8 get_cck_rate(const guint8 *plcp)
{
/* extract rate from the SIGNAL field then convert it to the MCS index used by the L1p fields */
switch (plcp[0]) {
case 0x0a: return 0;
case 0x14: return 1;
case 0x37: return 2;
case 0x6e: return 3;
default: return 0;
}
}
/*--------------------------------------------------------------------------------------*/
/* utility to set up offsets and bitmasks for decoding the stats blocks */
static void setup_defaults(vwr_t *vwr, guint16 fpga)
{
switch (fpga) {
/* WLAN frames */
case S2_W_FPGA:
vwr->STATS_LEN = vVW510021_W_STATS_TRAILER_LEN;
vwr->VALID_OFF = vVW510021_W_VALID_OFF;
vwr->MTYPE_OFF = vVW510021_W_MTYPE_OFF;
vwr->VCID_OFF = vVW510021_W_VCID_OFF;
vwr->FLOWSEQ_OFF = vVW510021_W_FLOWSEQ_OFF;
vwr->FLOWID_OFF = vVW510021_W_FLOWID_OFF;
/*vwr->OCTET_OFF = v22_W_OCTET_OFF;*/
vwr->ERRORS_OFF = vVW510021_W_ERRORS_OFF;
vwr->PATN_OFF = vVW510021_W_MATCH_OFF;
vwr->RSSI_OFF = vVW510021_W_RSSI_TXPOWER_OFF;
vwr->STARTT_OFF = vVW510021_W_STARTT_OFF;
vwr->ENDT_OFF = vVW510021_W_ENDT_OFF;
vwr->LATVAL_OFF = vVW510021_W_LATVAL_OFF;
vwr->INFO_OFF = vVW510021_W_INFO_OFF;
vwr->FPGA_VERSION_OFF = S2_W_FPGA_VERSION_OFF;
vwr->HEADER_VERSION_OFF = vVW510021_W_HEADER_VERSION_OFF;
vwr->OCTET_OFF = vVW510021_W_MSDU_LENGTH_OFF;
vwr->L1P_1_OFF = vVW510021_W_L1P_1_OFF;
vwr->L1P_2_OFF = vVW510021_W_L1P_2_OFF;
vwr->L4ID_OFF = vVW510021_W_L4ID_OFF;
vwr->IPLEN_OFF = vVW510021_W_IPLEN_OFF;
vwr->PLCP_LENGTH_OFF = vVW510021_W_PLCP_LENGTH_OFF;
vwr->MT_MASK = vVW510021_W_SEL_MASK;
vwr->MCS_INDEX_MASK = vVW510021_W_MCS_MASK;
vwr->VCID_MASK = 0xffff;
vwr->FLOW_VALID = vVW510021_W_FLOW_VALID;
vwr->STATS_START_OFF = vVW510021_W_HEADER_LEN;
vwr->FCS_ERROR = vVW510021_W_FCS_ERROR;
vwr->CRYPTO_ERR = v22_W_CRYPTO_ERR;
vwr->RETRY_ERR = v22_W_RETRY_ERR;
/*vwr->STATS_START_OFF = 0;*/
vwr->RXTX_OFF = vVW510021_W_RXTX_OFF;
vwr->MT_10_HALF = 0;
vwr->MT_10_FULL = 0;
vwr->MT_100_HALF = 0;
vwr->MT_100_FULL = 0;
vwr->MT_1G_HALF = 0;
vwr->MT_1G_FULL = 0;
vwr->MT_CCKL = v22_W_MT_CCKL;
vwr->MT_CCKS = v22_W_MT_CCKS;
/*vwr->MT_OFDM = vVW510021_W_MT_OFDM;*/
vwr->WEPTYPE = vVW510021_W_WEPTYPE;
vwr->TKIPTYPE = vVW510021_W_TKIPTYPE;
vwr->CCMPTYPE = vVW510021_W_CCMPTYPE;
vwr->FRAME_TYPE_OFF = vVW510021_W_FRAME_TYPE_OFF;
vwr->IS_TCP = vVW510021_W_IS_TCP;
vwr->IS_UDP = vVW510021_W_IS_UDP;
vwr->IS_ICMP = vVW510021_W_IS_ICMP;
vwr->IS_IGMP = vVW510021_W_IS_IGMP;
vwr->IS_QOS = vVW510021_W_QOS_VALID;
/*
* vVW510021_W_STATS_HEADER_LEN = 8 is:
*
* 2 bytes of l1p_1/l1p_2;
* 1 byte of RSSI;
* 2 bytes of MSDU length + other bits
* 1 byte of XXX;
* 2 bytes of VCID.
*
* The 12 is for 11 bytes of PLCP and 1 byte of pad
* before the data.
*/
vwr->MPDU_OFF = vVW510021_W_STATS_HEADER_LEN + 12;
break;
case S3_W_FPGA:
vwr->STATS_LEN = vVW510021_W_STATS_TRAILER_LEN;
vwr->PLCP_LENGTH_OFF = 16;
/*
* The 16 + 16 is:
*
* 2 bytes of l1p_1/l1p_2;
* 1 byte of signal bandwidth mask;
* 1 byte of antenna port energy;
* 4 bytes of per-antenna RSSI;
* 1 byte of L1InfoC;
* 3 bytes of MSDU length;
* 4 bytes of something;
* 16 bytes of PLCP.
*/
vwr->MPDU_OFF = 16 + 16;
break;
case vVW510012_E_FPGA:
vwr->STATS_LEN = v22_E_STATS_LEN;
vwr->VALID_OFF = v22_E_VALID_OFF;
vwr->MTYPE_OFF = v22_E_MTYPE_OFF;
vwr->VCID_OFF = v22_E_VCID_OFF;
vwr->FLOWSEQ_OFF = v22_E_FLOWSEQ_OFF;
vwr->FLOWID_OFF = v22_E_FLOWID_OFF;
vwr->OCTET_OFF = v22_E_OCTET_OFF;
vwr->ERRORS_OFF = v22_E_ERRORS_OFF;
vwr->PATN_OFF = v22_E_PATN_OFF;
vwr->RSSI_OFF = v22_E_RSSI_OFF;
vwr->STARTT_OFF = v22_E_STARTT_OFF;
vwr->ENDT_OFF = v22_E_ENDT_OFF;
vwr->LATVAL_OFF = v22_E_LATVAL_OFF;
vwr->INFO_OFF = v22_E_INFO_OFF;
vwr->L4ID_OFF = v22_E_L4ID_OFF;
vwr->IS_RX = v22_E_IS_RX;
vwr->MT_MASK = v22_E_MT_MASK;
vwr->VCID_MASK = v22_E_VCID_MASK;
vwr->FLOW_VALID = v22_E_FLOW_VALID;
vwr->FCS_ERROR = v22_E_FCS_ERROR;
vwr->RX_DECRYPTS = v22_E_RX_DECRYPTS;
vwr->TX_DECRYPTS = v22_E_TX_DECRYPTS;
vwr->FC_PROT_BIT = v22_E_FC_PROT_BIT;
vwr->MT_10_HALF = v22_E_MT_10_HALF;
vwr->MT_10_FULL = v22_E_MT_10_FULL;
vwr->MT_100_HALF = v22_E_MT_100_HALF;
vwr->MT_100_FULL = v22_E_MT_100_FULL;
vwr->MT_1G_HALF = v22_E_MT_1G_HALF;
vwr->MT_1G_FULL = v22_E_MT_1G_FULL;
vwr->MT_CCKL = 0;
vwr->MT_CCKS = 0;
vwr->MT_OFDM = 0;
vwr->FRAME_TYPE_OFF = v22_E_FRAME_TYPE_OFF;
vwr->IS_TCP = v22_E_IS_TCP;
vwr->IS_UDP = v22_E_IS_UDP;
vwr->IS_ICMP = v22_E_IS_ICMP;
vwr->IS_IGMP = v22_E_IS_IGMP;
vwr->IS_QOS = v22_E_IS_QOS;
vwr->IS_VLAN = v22_E_IS_VLAN;
break;
/* WLAN frames */
case S1_W_FPGA:
vwr->STATS_LEN = v22_W_STATS_LEN;
vwr->MTYPE_OFF = v22_W_MTYPE_OFF;
vwr->VALID_OFF = v22_W_VALID_OFF;
vwr->VCID_OFF = v22_W_VCID_OFF;
vwr->FLOWSEQ_OFF = v22_W_FLOWSEQ_OFF;
vwr->FLOWID_OFF = v22_W_FLOWID_OFF;
vwr->OCTET_OFF = v22_W_OCTET_OFF;
vwr->ERRORS_OFF = v22_W_ERRORS_OFF;
vwr->PATN_OFF = v22_W_PATN_OFF;
vwr->RSSI_OFF = v22_W_RSSI_OFF;
vwr->STARTT_OFF = v22_W_STARTT_OFF;
vwr->ENDT_OFF = v22_W_ENDT_OFF;
vwr->LATVAL_OFF = v22_W_LATVAL_OFF;
vwr->INFO_OFF = v22_W_INFO_OFF;
vwr->L4ID_OFF = v22_W_L4ID_OFF;
vwr->IPLEN_OFF = v22_W_IPLEN_OFF;
vwr->PLCP_LENGTH_OFF = v22_W_PLCP_LENGTH_OFF;
vwr->FCS_ERROR = v22_W_FCS_ERROR;
vwr->CRYPTO_ERR = v22_W_CRYPTO_ERR;
vwr->PAYCHK_ERR = v22_W_PAYCHK_ERR;
vwr->RETRY_ERR = v22_W_RETRY_ERR;
vwr->IS_RX = v22_W_IS_RX;
vwr->MT_MASK = v22_W_MT_MASK;
vwr->VCID_MASK = v22_W_VCID_MASK;
vwr->FLOW_VALID = v22_W_FLOW_VALID;
vwr->RX_DECRYPTS = v22_W_RX_DECRYPTS;
vwr->TX_DECRYPTS = v22_W_TX_DECRYPTS;
vwr->FC_PROT_BIT = v22_W_FC_PROT_BIT;
vwr->MT_10_HALF = 0;
vwr->MT_10_FULL = 0;
vwr->MT_100_HALF = 0;
vwr->MT_100_FULL = 0;
vwr->MT_1G_HALF = 0;
vwr->MT_1G_FULL = 0;
vwr->MT_CCKL = v22_W_MT_CCKL;
vwr->MT_CCKS = v22_W_MT_CCKS;
vwr->MT_OFDM = v22_W_MT_OFDM;
vwr->WEPTYPE = v22_W_WEPTYPE;
vwr->TKIPTYPE = v22_W_TKIPTYPE;
vwr->CCMPTYPE = v22_W_CCMPTYPE;
vwr->FRAME_TYPE_OFF = v22_W_FRAME_TYPE_OFF;
vwr->IS_TCP = v22_W_IS_TCP;
vwr->IS_UDP = v22_W_IS_UDP;
vwr->IS_ICMP = v22_W_IS_ICMP;
vwr->IS_IGMP = v22_W_IS_IGMP;
vwr->IS_QOS = v22_W_IS_QOS;
break;
/* Ethernet frames */
case vVW510024_E_FPGA:
vwr->STATS_LEN = vVW510024_E_STATS_LEN;
vwr->VALID_OFF = vVW510024_E_VALID_OFF;
vwr->VCID_OFF = vVW510024_E_VCID_OFF;
vwr->FLOWSEQ_OFF = vVW510024_E_FLOWSEQ_OFF;
vwr->FLOWID_OFF = vVW510024_E_FLOWID_OFF;
vwr->OCTET_OFF = vVW510024_E_MSDU_LENGTH_OFF;
vwr->ERRORS_OFF = vVW510024_E_ERRORS_OFF;
vwr->PATN_OFF = vVW510024_E_MATCH_OFF;
vwr->STARTT_OFF = vVW510024_E_STARTT_OFF;
vwr->ENDT_OFF = vVW510024_E_ENDT_OFF;
vwr->LATVAL_OFF = vVW510024_E_LATVAL_OFF;
vwr->INFO_OFF = vVW510024_E_INFO_OFF;
vwr->L4ID_OFF = vVW510024_E_L4ID_OFF;
vwr->IPLEN_OFF = vVW510024_E_IPLEN_OFF;
vwr->FPGA_VERSION_OFF = vVW510024_E_FPGA_VERSION_OFF;
vwr->HEADER_VERSION_OFF = vVW510024_E_HEADER_VERSION_OFF;
vwr->VCID_MASK = vVW510024_E_VCID_MASK;
vwr->FLOW_VALID = vVW510024_E_FLOW_VALID;
vwr->FCS_ERROR = v22_E_FCS_ERROR;
vwr->FRAME_TYPE_OFF = vVW510024_E_FRAME_TYPE_OFF;
vwr->IS_TCP = vVW510024_E_IS_TCP;
vwr->IS_UDP = vVW510024_E_IS_UDP;
vwr->IS_ICMP = vVW510024_E_IS_ICMP;
vwr->IS_IGMP = vVW510024_E_IS_IGMP;
vwr->IS_QOS = vVW510024_E_QOS_VALID;
vwr->IS_VLAN = vVW510024_E_IS_VLAN;
break;
}
}
#define SIG_SCAN_RANGE 64 /* range of signature scanning region */
/* Utility routine: check that signature is at specified location; scan for it if not. */
/* If we can't find a signature at all, then simply return the originally supplied offset. */
int find_signature(const guint8 *m_ptr, int rec_size, int pay_off, guint32 flow_id, guint8 flow_seq)
{
int tgt; /* temps */
guint32 fid;
/* initial check is very simple: look for a '0xdd' at the target location */
if (m_ptr[pay_off] == 0xdd) /* if magic byte is present */
return pay_off; /* got right offset, return it */
/* Hmmm, signature magic byte is not where it is supposed to be; scan from start of */
/* payload until maximum scan range exhausted to see if we can find it. */
/* The scanning process consists of looking for a '0xdd', then checking for the correct */
/* flow ID and sequence number at the appropriate offsets. */
for (tgt = pay_off; tgt < (rec_size); tgt++) {
if (m_ptr[tgt] == 0xdd) { /* found magic byte? check fields */
if ((tgt + 15 < rec_size) && (m_ptr[tgt + 15] == 0xe2)) {
if (m_ptr[tgt + 4] != flow_seq)
continue;
fid = pletoh24(&m_ptr[tgt + 1]);
if (fid != flow_id)
continue;
return (tgt);
}
else if (tgt + SIG_FSQ_OFF < rec_size)
{ /* out which one... */
if (m_ptr[tgt + SIG_FSQ_OFF] != flow_seq) /* check sequence number */
continue; /* if failed, keep scanning */
fid = pletoh24(&m_ptr[tgt + SIG_FID_OFF]); /* assemble flow ID from signature */
if (fid != flow_id) /* check flow ID against expected */
continue; /* if failed, keep scanning */
/* matched magic byte, sequence number, flow ID; found the signature */
return (tgt); /* return offset of signature */
}
}
}
/* failed to find the signature, return the original offset as default */
return pay_off;
}
/* utility routine: harvest the signature time stamp from the data frame */
guint64 get_signature_ts(const guint8 *m_ptr,int sig_off, int sig_max)
{
int ts_offset;
guint64 sig_ts;
if (sig_off + 15 >= sig_max)
return 0;
if (m_ptr[sig_off + 15] == 0xe2)
ts_offset = 5;
else
ts_offset = 8;
sig_ts = pletoh32(&m_ptr[sig_off + ts_offset]);
return (sig_ts & 0xffffffff);
}
static float
get_legacy_rate(guint8 rate_index)
{
/* Rate conversion data */
static const float canonical_rate_legacy[] = {1.0f, 2.0f, 5.5f, 11.0f, 6.0f, 9.0f, 12.0f, 18.0f, 24.0f, 36.0f, 48.0f, 54.0f};
float bitrate = 0.0f;
if (rate_index < G_N_ELEMENTS(canonical_rate_legacy))
bitrate = canonical_rate_legacy[rate_index];
return bitrate;
}
static float
get_ht_rate(guint8 mcs_index, guint16 rflags)
{
/* Rate conversion data */
static const int canonical_ndbps_20_ht[8] = {26, 52, 78, 104, 156, 208, 234, 260};
static const int canonical_ndbps_40_ht[8] = {54, 108, 162, 216, 324, 432, 486, 540};
float symbol_tx_time, bitrate;
int ndbps;
if (rflags & FLAGS_CHAN_SHORTGI)
symbol_tx_time = 3.6f;
else
symbol_tx_time = 4.0f;
if (rflags & FLAGS_CHAN_40MHZ)
ndbps = canonical_ndbps_40_ht[mcs_index - 8*(int)(mcs_index/8)];
else
ndbps = canonical_ndbps_20_ht[mcs_index - 8*(int)(mcs_index/8)];
bitrate = (ndbps * (((int)(mcs_index >> 3) + 1))) / symbol_tx_time;
return bitrate;
}
static float
get_vht_rate(guint8 mcs_index, guint16 rflags, guint8 nss)
{
/* Rate conversion data */
static const int canonical_ndbps_20_vht[9] = {26, 52, 78, 104, 156, 208, 234, 260, 312};
static const int canonical_ndbps_40_vht[10] = {54, 108, 162, 216, 324, 432, 486, 540, 648, 720};
static const int canonical_ndbps_80_vht[10] = {117, 234, 351, 468, 702, 936, 1053, 1170, 1404, 1560};
float symbol_tx_time, bitrate;
if (rflags & FLAGS_CHAN_SHORTGI)
symbol_tx_time = 3.6f;
else
symbol_tx_time = 4.0f;
/*
* Check for the out of range mcs_index.
* Should never happen, but if mcs index is greater than 9 just
* return 0.
*/
if (mcs_index > 9)
return 0.0f;
if (rflags & FLAGS_CHAN_40MHZ)
bitrate = (canonical_ndbps_40_vht[mcs_index] * nss) / symbol_tx_time;
else if (rflags & FLAGS_CHAN_80MHZ)
bitrate = (canonical_ndbps_80_vht[mcs_index] * nss) / symbol_tx_time;
else
{
if (mcs_index == 9)
{
/* This is a special case for 20 MHz. */
if (nss == 3)
bitrate = 1040 / symbol_tx_time;
else if (nss == 6)
bitrate = 2080 / symbol_tx_time;
else
bitrate = 0.0f;
}
else
bitrate = (canonical_ndbps_20_vht[mcs_index] * nss) / symbol_tx_time;
}
return bitrate;
}
static gboolean
vwr_process_rec_data(FILE_T fh, int rec_size,
wtap_rec *record, Buffer *buf, vwr_t *vwr,
int IS_TX, int log_mode, int *err, gchar **err_info)
{
guint8* rec; /* local buffer (holds input record) */
gboolean ret = FALSE;
rec = (guint8*)g_malloc(B_SIZE);
/* Read over the entire record (frame + trailer) into a local buffer. */
/* If we don't get it all, then declare an error, we can't process the frame. */
if (!wtap_read_bytes(fh, rec, rec_size, err, err_info))
{
g_free(rec);
return FALSE;
}
/* now format up the frame data */
switch (vwr->FPGA_VERSION)
{
case S1_W_FPGA:
ret = vwr_read_s1_W_rec(vwr, record, buf, rec, rec_size, err, err_info);
break;
case S2_W_FPGA:
ret = vwr_read_s2_W_rec(vwr, record, buf, rec, rec_size, IS_TX, err, err_info);
break;
case S3_W_FPGA:
ret = vwr_read_s3_W_rec(vwr, record, buf, rec, rec_size, IS_TX, log_mode, err, err_info);
break;
case vVW510012_E_FPGA:
case vVW510024_E_FPGA:
ret = vwr_read_rec_data_ethernet(vwr, record, buf, rec, rec_size, IS_TX, err, err_info);
break;
default:
g_free(rec);
ws_assert_not_reached();
return ret;
}
g_free(rec);
return ret;
}
static const struct supported_block_type vwr_80211_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 vwr_80211_info = {
"Ixia IxVeriWave .vwr Raw 802.11 Capture", "vwr80211", "vwr", NULL,
FALSE, BLOCKS_SUPPORTED(vwr_80211_blocks_supported),
NULL, NULL, NULL
};
static const struct supported_block_type vwr_eth_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 vwr_eth_info = {
"Ixia IxVeriWave .vwr Raw Ethernet Capture", "vwreth", "vwr", NULL,
FALSE, BLOCKS_SUPPORTED(vwr_eth_blocks_supported),
NULL, NULL, NULL
};
void register_vwr(void)
{
vwr_80211_file_type_subtype = wtap_register_file_type_subtype(&vwr_80211_info);
vwr_eth_file_type_subtype = wtap_register_file_type_subtype(&vwr_eth_info);
/*
* Register names for backwards compatibility with the
* wtap_filetypes table in Lua.
*/
wtap_register_backwards_compatibility_lua_name("VWR_80211",
vwr_80211_file_type_subtype);
wtap_register_backwards_compatibility_lua_name("VWR_ETH",
vwr_eth_file_type_subtype);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/vwr.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998-2010 by Tom Alexander <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*/
#ifndef __VWR_H__
#define __VWR_H__
#include "ws_symbol_export.h"
wtap_open_return_val vwr_open(wtap *wth, int *err, gchar **err_info);
#endif |
C/C++ | wireshark/wiretap/wtap-int.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WTAP_INT_H__
#define __WTAP_INT_H__
#include "wtap.h"
#include <time.h>
#ifdef _WIN32
#include <winsock2.h>
#endif
#include <wsutil/file_util.h>
#include "wtap_opttypes.h"
void wtap_init_file_type_subtypes(void);
WS_DLL_PUBLIC
int wtap_fstat(wtap *wth, ws_statb64 *statb, int *err);
typedef gboolean (*subtype_read_func)(struct wtap*, wtap_rec *,
Buffer *, int *, char **, gint64 *);
typedef gboolean (*subtype_seek_read_func)(struct wtap*, gint64, wtap_rec *,
Buffer *, int *, char **);
/**
* Struct holding data of the currently read file.
*/
struct wtap {
FILE_T fh;
FILE_T random_fh; /**< Secondary FILE_T for random access */
gboolean ispipe; /**< TRUE if the file is a pipe */
int file_type_subtype;
guint snapshot_length;
GArray *shb_hdrs;
GArray *interface_data; /**< An array holding the interface data from pcapng IDB:s or equivalent(?)*/
guint next_interface_data; /**< Next interface data that wtap_get_next_interface_description() will show */
GArray *nrbs; /**< holds the Name Res Blocks, or NULL */
GArray *dsbs; /**< An array of DSBs (of type wtap_block_t), or NULL if not supported. */
char *pathname; /**< File pathname; might just be "-" */
void *priv; /* this one holds per-file state and is free'd automatically by wtap_close() */
void *wslua_data; /* this one holds wslua state info and is not free'd */
subtype_read_func subtype_read;
subtype_seek_read_func subtype_seek_read;
void (*subtype_sequential_close)(struct wtap*);
void (*subtype_close)(struct wtap*);
int file_encap; /* per-file, for those
* file formats that have
* per-file encapsulation
* types rather than per-packet
* encapsulation types
*/
int file_tsprec; /* per-file timestamp precision
* of the fractional part of
* the time stamp, for those
* file formats that have
* per-file timestamp
* precision rather than
* per-packet timestamp
* precision
* e.g. WTAP_TSPREC_USEC
*/
wtap_new_ipv4_callback_t add_new_ipv4;
wtap_new_ipv6_callback_t add_new_ipv6;
wtap_new_secrets_callback_t add_new_secrets;
GPtrArray *fast_seek;
};
struct wtap_dumper;
/*
* This could either be a FILE * or a gzFile.
*/
typedef void *WFILE_T;
typedef gboolean (*subtype_add_idb_func)(struct wtap_dumper*, wtap_block_t,
int *, gchar **);
typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
const wtap_rec *rec,
const guint8*, int*, gchar**);
typedef gboolean (*subtype_finish_func)(struct wtap_dumper*, int*, gchar**);
struct wtap_dumper {
WFILE_T fh;
int file_type_subtype;
int snaplen;
int file_encap; /* per-file, for those
* file formats that have
* per-file encapsulation
* types rather than per-packet
* encapsulation types
*/
wtap_compression_type compression_type;
gboolean needs_reload; /* TRUE if the file requires re-loading after saving with wtap */
gint64 bytes_dumped;
void *priv; /* this one holds per-file state and is free'd automatically by wtap_dump_close() */
void *wslua_data; /* this one holds wslua state info and is not free'd */
subtype_add_idb_func subtype_add_idb; /* add an IDB, writing it as necessary */
subtype_write_func subtype_write; /* write out a record */
subtype_finish_func subtype_finish; /* write out information to finish writing file */
addrinfo_lists_t *addrinfo_lists; /**< Struct containing lists of resolved addresses */
GArray *shb_hdrs;
GArray *interface_data; /**< An array holding the interface data from pcapng IDB:s or equivalent(?) NULL if not present.*/
GArray *dsbs_initial; /**< An array of initial DSBs (of type wtap_block_t) */
/*
* Additional blocks that might grow as data is being collected.
* Subtypes should write these blocks before writing new packet blocks.
*/
const GArray *nrbs_growing; /**< A reference to an array of NRBs (of type wtap_block_t) */
const GArray *dsbs_growing; /**< A reference to an array of DSBs (of type wtap_block_t) */
guint nrbs_growing_written; /**< Number of already processed NRBs in nrbs_growing. */
guint dsbs_growing_written; /**< Number of already processed DSBs in dsbs_growing. */
};
WS_DLL_PUBLIC gboolean wtap_dump_file_write(wtap_dumper *wdh, const void *buf,
size_t bufsize, int *err);
WS_DLL_PUBLIC gint64 wtap_dump_file_seek(wtap_dumper *wdh, gint64 offset, int whence, int *err);
WS_DLL_PUBLIC gint64 wtap_dump_file_tell(wtap_dumper *wdh, int *err);
extern gint wtap_num_file_types;
#include <wsutil/pint.h>
/* Macros to byte-swap possibly-unaligned 64-bit, 32-bit and 16-bit quantities;
* they take a pointer to the quantity, and byte-swap it in place.
*/
#define PBSWAP64(p) \
{ \
guint8 tmp; \
tmp = (p)[7]; \
(p)[7] = (p)[0]; \
(p)[0] = tmp; \
tmp = (p)[6]; \
(p)[6] = (p)[1]; \
(p)[1] = tmp; \
tmp = (p)[5]; \
(p)[5] = (p)[2]; \
(p)[2] = tmp; \
tmp = (p)[4]; \
(p)[4] = (p)[3]; \
(p)[3] = tmp; \
}
#define PBSWAP32(p) \
{ \
guint8 tmp; \
tmp = (p)[3]; \
(p)[3] = (p)[0]; \
(p)[0] = tmp; \
tmp = (p)[2]; \
(p)[2] = (p)[1]; \
(p)[1] = tmp; \
}
#define PBSWAP16(p) \
{ \
guint8 tmp; \
tmp = (p)[1]; \
(p)[1] = (p)[0]; \
(p)[0] = tmp; \
}
/* Pointer routines to put items out in a particular byte order.
* These will work regardless of the byte alignment of the pointer.
*/
#ifndef phtons
#define phtons(p, v) \
{ \
(p)[0] = (guint8)((v) >> 8); \
(p)[1] = (guint8)((v) >> 0); \
}
#endif
#ifndef phton24
#define phton24(p, v) \
{ \
(p)[0] = (guint8)((v) >> 16); \
(p)[1] = (guint8)((v) >> 8); \
(p)[2] = (guint8)((v) >> 0); \
}
#endif
#ifndef phtonl
#define phtonl(p, v) \
{ \
(p)[0] = (guint8)((v) >> 24); \
(p)[1] = (guint8)((v) >> 16); \
(p)[2] = (guint8)((v) >> 8); \
(p)[3] = (guint8)((v) >> 0); \
}
#endif
#ifndef phtonll
#define phtonll(p, v) \
{ \
(p)[0] = (guint8)((v) >> 56); \
(p)[1] = (guint8)((v) >> 48); \
(p)[2] = (guint8)((v) >> 40); \
(p)[3] = (guint8)((v) >> 32); \
(p)[4] = (guint8)((v) >> 24); \
(p)[5] = (guint8)((v) >> 16); \
(p)[6] = (guint8)((v) >> 8); \
(p)[7] = (guint8)((v) >> 0); \
}
#endif
#ifndef phtole8
#define phtole8(p, v) \
{ \
(p)[0] = (guint8)((v) >> 0); \
}
#endif
#ifndef phtoles
#define phtoles(p, v) \
{ \
(p)[0] = (guint8)((v) >> 0); \
(p)[1] = (guint8)((v) >> 8); \
}
#endif
#ifndef phtole24
#define phtole24(p, v) \
{ \
(p)[0] = (guint8)((v) >> 0); \
(p)[1] = (guint8)((v) >> 8); \
(p)[2] = (guint8)((v) >> 16); \
}
#endif
#ifndef phtolel
#define phtolel(p, v) \
{ \
(p)[0] = (guint8)((v) >> 0); \
(p)[1] = (guint8)((v) >> 8); \
(p)[2] = (guint8)((v) >> 16); \
(p)[3] = (guint8)((v) >> 24); \
}
#endif
#ifndef phtolell
#define phtolell(p, v) \
{ \
(p)[0] = (guint8)((v) >> 0); \
(p)[1] = (guint8)((v) >> 8); \
(p)[2] = (guint8)((v) >> 16); \
(p)[3] = (guint8)((v) >> 24); \
(p)[4] = (guint8)((v) >> 32); \
(p)[5] = (guint8)((v) >> 40); \
(p)[6] = (guint8)((v) >> 48); \
(p)[7] = (guint8)((v) >> 56); \
}
#endif
/* glib doesn't have g_ptr_array_len of all things!*/
#ifndef g_ptr_array_len
#define g_ptr_array_len(a) ((a)->len)
#endif
/*
* Read a given number of bytes from a file into a buffer or, if
* buf is NULL, just discard them.
*
* If we succeed, return TRUE.
*
* If we get an EOF, return FALSE with *err set to 0, reporting this
* as an EOF.
*
* If we get fewer bytes than the specified number, return FALSE with
* *err set to WTAP_ERR_SHORT_READ, reporting this as a short read
* error.
*
* If we get a read error, return FALSE with *err and *err_info set
* appropriately.
*/
WS_DLL_PUBLIC
gboolean
wtap_read_bytes_or_eof(FILE_T fh, void *buf, unsigned int count, int *err,
gchar **err_info);
/*
* Read a given number of bytes from a file into a buffer or, if
* buf is NULL, just discard them.
*
* If we succeed, return TRUE.
*
* If we get fewer bytes than the specified number, including getting
* an EOF, return FALSE with *err set to WTAP_ERR_SHORT_READ, reporting
* this as a short read error.
*
* If we get a read error, return FALSE with *err and *err_info set
* appropriately.
*/
WS_DLL_PUBLIC
gboolean
wtap_read_bytes(FILE_T fh, void *buf, unsigned int count, int *err,
gchar **err_info);
/*
* Read packet data into a Buffer, growing the buffer as necessary.
*
* This returns an error on a short read, even if the short read hit
* the EOF immediately. (The assumption is that each packet has a
* header followed by raw packet data, and that we've already read the
* header, so if we get an EOF trying to read the packet data, the file
* has been cut short, even if the read didn't read any data at all.)
*/
WS_DLL_PUBLIC
gboolean
wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
gchar **err_info);
/*
* Implementation of wth->subtype_read that reads the full file contents
* as a single packet.
*/
gboolean
wtap_full_file_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset);
/*
* Implementation of wth->subtype_seek_read that reads the full file contents
* as a single packet.
*/
gboolean
wtap_full_file_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
/**
* Add an IDB to the interface data for a file.
*/
void
wtap_add_idb(wtap *wth, wtap_block_t idb);
/**
* Invokes the callback with the given name resolution block.
*/
void
wtapng_process_nrb(wtap *wth, wtap_block_t nrb);
/**
* Invokes the callback with the given decryption secrets block.
*/
void
wtapng_process_dsb(wtap *wth, wtap_block_t dsb);
void
wtap_register_compatibility_file_subtype_name(const char *old_name,
const char *new_name);
void
wtap_register_backwards_compatibility_lua_name(const char *name, int ft);
struct backwards_compatibiliity_lua_name {
const char *name;
int ft;
};
WS_DLL_PUBLIC
const GArray *get_backwards_compatibility_lua_table(void);
/**
* @brief Gets new section header block for new file, based on existing info.
* @details Creates a new wtap_block_t section header block and only
* copies appropriate members of the SHB for a new file. In
* particular, the comment string is copied, and any custom options
* which should be copied are copied. The os, hardware, and
* application strings are *not* copied.
*
* @note Use wtap_free_shb() to free the returned section header.
*
* @param wth The wiretap session.
* @return The new section header, which must be wtap_free_shb'd.
*/
GArray* wtap_file_get_shb_for_new_file(wtap *wth);
/**
* @brief Generate an IDB, given a wiretap handle for the file,
* using the file's encapsulation type, snapshot length,
* and time stamp resolution, and add it to the interface
* data for a file.
* @note This requires that the encapsulation type and time stamp
* resolution not be per-packet; it will terminate the process
* if either of them are.
*
* @param wth The wiretap handle for the file.
*/
WS_DLL_PUBLIC
void wtap_add_generated_idb(wtap *wth);
/**
* @brief Generate an IDB, given a set of dump parameters, using the
* parameters' encapsulation type, snapshot length, and time stamp
* resolution. For use when a dump file has a given encapsulation type,
* and the source is not passing IDBs.
* @note This requires that the encapsulation type and time stamp
* resolution not be per-packet; it will terminate the process
* if either of them are.
*
* @param params The wtap dump parameters.
*/
wtap_block_t wtap_dump_params_generate_idb(const wtap_dump_params *params);
/**
* @brief Generate an IDB, given a packet record, using the records's
* encapsulation type and time stamp resolution, and the default
* snap length for the encapsulation type. For use when a file has
* per-packet encapsulation, and the source is not passing along IDBs.
* @note This requires that the record type be REC_TYPE_PACKET, and the
* encapsulation type and time stamp resolution not be per-packet;
* it will terminate the process if any of them are.
*
* @param rec The packet record.
*/
wtap_block_t wtap_rec_generate_idb(const wtap_rec *rec);
/**
* @brief Gets new name resolution info for new file, based on existing info.
* @details Creates a new wtap_block_t of name resolution info and only
* copies appropriate members for a new file.
*
* @note Use wtap_free_nrb() to free the returned pointer.
*
* @param wth The wiretap session.
* @return The new name resolution info, which must be freed.
*/
GArray* wtap_file_get_nrb_for_new_file(wtap *wth);
#endif /* __WTAP_INT_H__ */
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C | wireshark/wiretap/wtap.c | /* wtap.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config.h>
#include <string.h>
#include <sys/types.h>
#include "wtap-int.h"
#include "wtap_opttypes.h"
#include "file_wrappers.h"
#include <wsutil/file_util.h>
#include <wsutil/buffer.h>
#include <wsutil/ws_assert.h>
#include <wsutil/wslog.h>
#include <wsutil/exported_pdu_tlvs.h>
#ifdef HAVE_PLUGINS
#include <wsutil/plugins.h>
#endif
#ifdef HAVE_PLUGINS
static plugins_t *libwiretap_plugins = NULL;
#endif
#define PADDING4(x) ((((x + 3) >> 2) << 2) - x)
static GSList *wtap_plugins = NULL;
#ifdef HAVE_PLUGINS
void
wtap_register_plugin(const wtap_plugin *plug)
{
wtap_plugins = g_slist_prepend(wtap_plugins, (wtap_plugin *)plug);
}
#else /* HAVE_PLUGINS */
void
wtap_register_plugin(const wtap_plugin *plug _U_)
{
ws_warning("wtap_register_plugin: built without support for binary plugins");
}
#endif /* HAVE_PLUGINS */
int
wtap_plugins_supported(void)
{
#ifdef HAVE_PLUGINS
return plugins_supported() ? 0 : 1;
#else
return -1;
#endif
}
static void
call_plugin_register_wtap_module(gpointer data, gpointer user_data _U_)
{
wtap_plugin *plug = (wtap_plugin *)data;
if (plug->register_wtap_module) {
plug->register_wtap_module();
}
}
/*
* Return the size of the file, as reported by the OS.
* (gint64, in case that's 64 bits.)
*/
gint64
wtap_file_size(wtap *wth, int *err)
{
ws_statb64 statb;
if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
&statb, err) == -1)
return -1;
return statb.st_size;
}
/*
* Do an fstat on the file.
*/
int
wtap_fstat(wtap *wth, ws_statb64 *statb, int *err)
{
if (file_fstat((wth->fh == NULL) ? wth->random_fh : wth->fh,
statb, err) == -1)
return -1;
return 0;
}
int
wtap_file_type_subtype(wtap *wth)
{
return wth->file_type_subtype;
}
guint
wtap_snapshot_length(wtap *wth)
{
return wth->snapshot_length;
}
int
wtap_file_encap(wtap *wth)
{
return wth->file_encap;
}
int
wtap_file_tsprec(wtap *wth)
{
return wth->file_tsprec;
}
guint
wtap_file_get_num_shbs(wtap *wth)
{
return wth->shb_hdrs->len;
}
wtap_block_t
wtap_file_get_shb(wtap *wth, guint shb_num)
{
if ((wth == NULL) || (wth->shb_hdrs == NULL) || (shb_num >= wth->shb_hdrs->len))
return NULL;
return g_array_index(wth->shb_hdrs, wtap_block_t, shb_num);
}
GArray*
wtap_file_get_shb_for_new_file(wtap *wth)
{
guint shb_count;
wtap_block_t shb_hdr_src, shb_hdr_dest;
GArray* shb_hdrs;
if ((wth == NULL) || (wth->shb_hdrs == NULL) || (wth->shb_hdrs->len == 0))
return NULL;
shb_hdrs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
for (shb_count = 0; shb_count < wth->shb_hdrs->len; shb_count++) {
shb_hdr_src = g_array_index(wth->shb_hdrs, wtap_block_t, shb_count);
shb_hdr_dest = wtap_block_make_copy(shb_hdr_src);
g_array_append_val(shb_hdrs, shb_hdr_dest);
}
return shb_hdrs;
}
/*
* XXX - replace with APIs that let us handle multiple comments.
*/
void
wtap_write_shb_comment(wtap *wth, gchar *comment)
{
if ((wth != NULL) && (wth->shb_hdrs != NULL) && (wth->shb_hdrs->len > 0)) {
wtap_block_set_nth_string_option_value(g_array_index(wth->shb_hdrs, wtap_block_t, 0), OPT_COMMENT, 0, comment, (gsize)(comment ? strlen(comment) : 0));
}
}
wtapng_iface_descriptions_t *
wtap_file_get_idb_info(wtap *wth)
{
wtapng_iface_descriptions_t *idb_info;
idb_info = g_new(wtapng_iface_descriptions_t,1);
idb_info->interface_data = wth->interface_data;
return idb_info;
}
wtap_block_t
wtap_get_next_interface_description(wtap *wth)
{
if (wth->next_interface_data < wth->interface_data->len) {
/*
* We have an IDB to return. Advance to the next
* IDB, and return this one.
*/
wtap_block_t idb;
idb = g_array_index(wth->interface_data, wtap_block_t,
wth->next_interface_data);
wth->next_interface_data++;
return idb;
}
/*
* We've returned all the interface descriptions we currently
* have. (There may be more in the future, if we read more.)
*/
return NULL;
}
void
wtap_file_add_decryption_secrets(wtap *wth, const wtap_block_t dsb)
{
if (!wth->dsbs) {
wth->dsbs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
}
g_array_append_val(wth->dsbs, dsb);
}
gboolean
wtap_file_discard_decryption_secrets(wtap *wth)
{
if (!wth->dsbs || wth->dsbs->len == 0)
return FALSE;
wtap_block_array_free(wth->dsbs);
wth->dsbs = NULL;
return TRUE;
}
void
wtap_add_idb(wtap *wth, wtap_block_t idb)
{
g_array_append_val(wth->interface_data, idb);
}
static wtap_block_t
wtap_generate_idb(int encap, int tsprec, int snaplen)
{
wtap_block_t idb;
wtapng_if_descr_mandatory_t *if_descr_mand;
ws_assert(encap != WTAP_ENCAP_UNKNOWN &&
encap != WTAP_ENCAP_PER_PACKET &&
encap != WTAP_ENCAP_NONE);
idb = wtap_block_create(WTAP_BLOCK_IF_ID_AND_INFO);
if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(idb);
if_descr_mand->wtap_encap = encap;
if_descr_mand->tsprecision = tsprec;
switch (tsprec) {
case WTAP_TSPREC_SEC:
if_descr_mand->time_units_per_second = 1;
wtap_block_add_uint8_option(idb, OPT_IDB_TSRESOL, 0);
break;
case WTAP_TSPREC_DSEC:
if_descr_mand->time_units_per_second = 10;
wtap_block_add_uint8_option(idb, OPT_IDB_TSRESOL, 1);
break;
case WTAP_TSPREC_CSEC:
if_descr_mand->time_units_per_second = 100;
wtap_block_add_uint8_option(idb, OPT_IDB_TSRESOL, 2);
break;
case WTAP_TSPREC_MSEC:
if_descr_mand->time_units_per_second = 1000;
wtap_block_add_uint8_option(idb, OPT_IDB_TSRESOL, 3);
break;
case WTAP_TSPREC_USEC:
if_descr_mand->time_units_per_second = 1000000;
/* This is the default, so no need to add an option */
break;
case WTAP_TSPREC_NSEC:
if_descr_mand->time_units_per_second = 1000000000;
wtap_block_add_uint8_option(idb, OPT_IDB_TSRESOL, 9);
break;
case WTAP_TSPREC_PER_PACKET:
case WTAP_TSPREC_UNKNOWN:
default:
/*
* No timestamp precision.
*/
if_descr_mand->time_units_per_second = 1000000; /* default microsecond resolution */
break;
}
if (snaplen == 0) {
/*
* No snapshot length was specified. Pick an
* appropriate snapshot length for this
* link-layer type.
*
* We use WTAP_MAX_PACKET_SIZE_STANDARD for everything except
* D-Bus, which has a maximum packet size of 128MB,
* and EBHSCR, which has a maximum packet size of 8MB,
* which is more than we want to put into files
* with other link-layer header types, as that
* might cause some software reading those files
* to allocate an unnecessarily huge chunk of
* memory for a packet buffer.
*/
if (encap == WTAP_ENCAP_DBUS)
snaplen = 128*1024*1024;
else if (encap == WTAP_ENCAP_EBHSCR)
snaplen = 8*1024*1024;
else
snaplen = WTAP_MAX_PACKET_SIZE_STANDARD;
}
if_descr_mand->snap_len = snaplen;
if_descr_mand->num_stat_entries = 0; /* Number of ISBs */
if_descr_mand->interface_statistics = NULL;
return idb;
}
void
wtap_add_generated_idb(wtap *wth)
{
wtap_block_t idb;
idb = wtap_generate_idb(wth->file_encap, wth->file_tsprec, wth->snapshot_length);
/*
* Add this IDB.
*/
wtap_add_idb(wth, idb);
}
void
wtap_free_idb_info(wtapng_iface_descriptions_t *idb_info)
{
if (idb_info == NULL)
return;
wtap_block_array_free(idb_info->interface_data);
g_free(idb_info);
}
gchar *
wtap_get_debug_if_descr(const wtap_block_t if_descr,
const int indent,
const char* line_end)
{
char* tmp_content;
wtapng_if_descr_mandatory_t* if_descr_mand;
GString *info = g_string_new("");
guint64 tmp64;
gint8 itmp8;
guint8 tmp8;
if_filter_opt_t if_filter;
ws_assert(if_descr);
if_descr_mand = (wtapng_if_descr_mandatory_t*)wtap_block_get_mandatory_data(if_descr);
if (wtap_block_get_string_option_value(if_descr, OPT_IDB_NAME, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
g_string_printf(info,
"%*cName = %s%s", indent, ' ',
tmp_content ? tmp_content : "UNKNOWN",
line_end);
}
if (wtap_block_get_string_option_value(if_descr, OPT_IDB_DESCRIPTION, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
g_string_append_printf(info,
"%*cDescription = %s%s", indent, ' ',
tmp_content ? tmp_content : "NONE",
line_end);
}
g_string_append_printf(info,
"%*cEncapsulation = %s (%d - %s)%s", indent, ' ',
wtap_encap_description(if_descr_mand->wtap_encap),
if_descr_mand->wtap_encap,
wtap_encap_name(if_descr_mand->wtap_encap),
line_end);
if (wtap_block_get_string_option_value(if_descr, OPT_IDB_HARDWARE, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
g_string_append_printf(info,
"%*cHardware = %s%s", indent, ' ',
tmp_content ? tmp_content : "NONE",
line_end);
}
if (wtap_block_get_uint64_option_value(if_descr, OPT_IDB_SPEED, &tmp64) == WTAP_OPTTYPE_SUCCESS) {
g_string_append_printf(info,
"%*cSpeed = %" PRIu64 "%s", indent, ' ',
tmp64,
line_end);
}
g_string_append_printf(info,
"%*cCapture length = %u%s", indent, ' ',
if_descr_mand->snap_len,
line_end);
if (wtap_block_get_uint8_option_value(if_descr, OPT_IDB_FCSLEN, &itmp8) == WTAP_OPTTYPE_SUCCESS) {
g_string_append_printf(info,
"%*cFCS length = %d%s", indent, ' ',
itmp8,
line_end);
}
g_string_append_printf(info,
"%*cTime precision = %s (%d)%s", indent, ' ',
wtap_tsprec_string(if_descr_mand->tsprecision),
if_descr_mand->tsprecision,
line_end);
g_string_append_printf(info,
"%*cTime ticks per second = %" PRIu64 "%s", indent, ' ',
if_descr_mand->time_units_per_second,
line_end);
if (wtap_block_get_uint8_option_value(if_descr, OPT_IDB_TSRESOL, &tmp8) == WTAP_OPTTYPE_SUCCESS) {
g_string_append_printf(info,
"%*cTime resolution = 0x%.2x%s", indent, ' ',
tmp8,
line_end);
}
if (wtap_block_get_if_filter_option_value(if_descr, OPT_IDB_FILTER, &if_filter) == WTAP_OPTTYPE_SUCCESS) {
switch (if_filter.type) {
case if_filter_pcap:
g_string_append_printf(info,
"%*cFilter string = %s%s", indent, ' ',
if_filter.data.filter_str,
line_end);
break;
case if_filter_bpf:
g_string_append_printf(info,
"%*cBPF filter length = %u%s", indent, ' ',
if_filter.data.bpf_prog.bpf_prog_len,
line_end);
break;
default:
g_string_append_printf(info,
"%*cUnknown filter type %u%s", indent, ' ',
if_filter.type,
line_end);
break;
}
}
if (wtap_block_get_string_option_value(if_descr, OPT_IDB_OS, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
g_string_append_printf(info,
"%*cOperating system = %s%s", indent, ' ',
tmp_content ? tmp_content : "UNKNOWN",
line_end);
}
/*
* XXX - support multiple comments.
*/
if (wtap_block_get_nth_string_option_value(if_descr, OPT_COMMENT, 0, &tmp_content) == WTAP_OPTTYPE_SUCCESS) {
g_string_append_printf(info,
"%*cComment = %s%s", indent, ' ',
tmp_content ? tmp_content : "NONE",
line_end);
}
g_string_append_printf(info,
"%*cNumber of stat entries = %u%s", indent, ' ',
if_descr_mand->num_stat_entries,
line_end);
return g_string_free(info, FALSE);
}
wtap_block_t
wtap_file_get_nrb(wtap *wth)
{
if ((wth == NULL) || (wth->nrbs == NULL) || (wth->nrbs->len == 0))
return NULL;
return g_array_index(wth->nrbs, wtap_block_t, 0);
}
GArray*
wtap_file_get_nrb_for_new_file(wtap *wth)
{
guint nrb_count;
wtap_block_t nrb_src, nrb_dest;
GArray* nrbs;
if ((wth == NULL || wth->nrbs == NULL) || (wth->nrbs->len == 0))
return NULL;
nrbs = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
for (nrb_count = 0; nrb_count < wth->nrbs->len; nrb_count++) {
nrb_src = g_array_index(wth->nrbs, wtap_block_t, nrb_count);
nrb_dest = wtap_block_make_copy(nrb_src);
g_array_append_val(nrbs, nrb_dest);
}
return nrbs;
}
void
wtap_dump_params_init(wtap_dump_params *params, wtap *wth)
{
memset(params, 0, sizeof(*params));
if (wth == NULL)
return;
params->encap = wtap_file_encap(wth);
params->snaplen = wtap_snapshot_length(wth);
params->tsprec = wtap_file_tsprec(wth);
params->shb_hdrs = wtap_file_get_shb_for_new_file(wth);
params->idb_inf = wtap_file_get_idb_info(wth);
/* Assume that the input handle remains open until the dumper is closed.
* Refer to the DSBs from the input file, wtap_dump will then copy DSBs
* as they become available. */
params->nrbs_growing = wth->nrbs;
params->dsbs_growing = wth->dsbs;
params->dont_copy_idbs = FALSE;
}
/*
* XXX - eventually, we should make this wtap_dump_params_init(),
* and have everything copy IDBs as they're read.
*/
void
wtap_dump_params_init_no_idbs(wtap_dump_params *params, wtap *wth)
{
memset(params, 0, sizeof(*params));
if (wth == NULL)
return;
params->encap = wtap_file_encap(wth);
params->snaplen = wtap_snapshot_length(wth);
params->tsprec = wtap_file_tsprec(wth);
params->shb_hdrs = wtap_file_get_shb_for_new_file(wth);
params->idb_inf = wtap_file_get_idb_info(wth);
/* Assume that the input handle remains open until the dumper is closed.
* Refer to the DSBs from the input file, wtap_dump will then copy DSBs
* as they become available. */
params->nrbs_growing = wth->nrbs;
params->dsbs_growing = wth->dsbs;
params->dont_copy_idbs = TRUE;
}
void
wtap_dump_params_discard_name_resolution(wtap_dump_params *params)
{
params->nrbs_growing = NULL;
}
void
wtap_dump_params_discard_decryption_secrets(wtap_dump_params *params)
{
params->dsbs_initial = NULL;
params->dsbs_growing = NULL;
}
void
wtap_dump_params_cleanup(wtap_dump_params *params)
{
wtap_block_array_free(params->shb_hdrs);
/* params->idb_inf is currently expected to be freed by the caller. */
memset(params, 0, sizeof(*params));
}
wtap_block_t
wtap_dump_params_generate_idb(const wtap_dump_params *params)
{
return wtap_generate_idb(params->encap, params->tsprec, params->snaplen);
}
/* Table of the encapsulation types we know about. */
struct encap_type_info {
const char *name;
const char *description;
};
static struct encap_type_info encap_table_base[] = {
/* WTAP_ENCAP_UNKNOWN */
{ "unknown", "Unknown" },
/* WTAP_ENCAP_ETHERNET */
{ "ether", "Ethernet" },
/* WTAP_ENCAP_TOKEN_RING */
{ "tr", "Token Ring" },
/* WTAP_ENCAP_SLIP */
{ "slip", "SLIP" },
/* WTAP_ENCAP_PPP */
{ "ppp", "PPP" },
/* WTAP_ENCAP_FDDI */
{ "fddi", "FDDI" },
/* WTAP_ENCAP_FDDI_BITSWAPPED */
{ "fddi-swapped", "FDDI with bit-swapped MAC addresses" },
/* WTAP_ENCAP_RAW_IP */
{ "rawip", "Raw IP" },
/* WTAP_ENCAP_ARCNET */
{ "arcnet", "ARCNET" },
/* WTAP_ENCAP_ARCNET_LINUX */
{ "arcnet_linux", "Linux ARCNET" },
/* WTAP_ENCAP_ATM_RFC1483 */
{ "atm-rfc1483", "RFC 1483 ATM" },
/* WTAP_ENCAP_LINUX_ATM_CLIP */
{ "linux-atm-clip", "Linux ATM CLIP" },
/* WTAP_ENCAP_LAPB */
{ "lapb", "LAPB" },
/* WTAP_ENCAP_ATM_PDUS */
{ "atm-pdus", "ATM PDUs" },
/* WTAP_ENCAP_ATM_PDUS_UNTRUNCATED */
{ "atm-pdus-untruncated", "ATM PDUs - untruncated" },
/* WTAP_ENCAP_NULL */
{ "null", "NULL/Loopback" },
/* WTAP_ENCAP_ASCEND */
{ "ascend", "Lucent/Ascend access equipment" },
/* WTAP_ENCAP_ISDN */
{ "isdn", "ISDN" },
/* WTAP_ENCAP_IP_OVER_FC */
{ "ip-over-fc", "RFC 2625 IP-over-Fibre Channel" },
/* WTAP_ENCAP_PPP_WITH_PHDR */
{ "ppp-with-direction", "PPP with Directional Info" },
/* WTAP_ENCAP_IEEE_802_11 */
{ "ieee-802-11", "IEEE 802.11 Wireless LAN" },
/* WTAP_ENCAP_IEEE_802_11_PRISM */
{ "ieee-802-11-prism", "IEEE 802.11 plus Prism II monitor mode radio header" },
/* WTAP_ENCAP_IEEE_802_11_WITH_RADIO */
{ "ieee-802-11-radio", "IEEE 802.11 Wireless LAN with radio information" },
/* WTAP_ENCAP_IEEE_802_11_RADIOTAP */
{ "ieee-802-11-radiotap", "IEEE 802.11 plus radiotap radio header" },
/* WTAP_ENCAP_IEEE_802_11_AVS */
{ "ieee-802-11-avs", "IEEE 802.11 plus AVS radio header" },
/* WTAP_ENCAP_SLL */
{ "linux-sll", "Linux cooked-mode capture v1" },
/* WTAP_ENCAP_FRELAY */
{ "frelay", "Frame Relay" },
/* WTAP_ENCAP_FRELAY_WITH_PHDR */
{ "frelay-with-direction", "Frame Relay with Directional Info" },
/* WTAP_ENCAP_CHDLC */
{ "chdlc", "Cisco HDLC" },
/* WTAP_ENCAP_CISCO_IOS */
{ "ios", "Cisco IOS internal" },
/* WTAP_ENCAP_LOCALTALK */
{ "ltalk", "Localtalk" },
/* WTAP_ENCAP_OLD_PFLOG */
{ "pflog-old", "OpenBSD PF Firewall logs, pre-3.4" },
/* WTAP_ENCAP_HHDLC */
{ "hhdlc", "HiPath HDLC" },
/* WTAP_ENCAP_DOCSIS */
{ "docsis", "Data Over Cable Service Interface Specification" },
/* WTAP_ENCAP_COSINE */
{ "cosine", "CoSine L2 debug log" },
/* WTAP_ENCAP_WFLEET_HDLC */
{ "whdlc", "Wellfleet HDLC" },
/* WTAP_ENCAP_SDLC */
{ "sdlc", "SDLC" },
/* WTAP_ENCAP_TZSP */
{ "tzsp", "Tazmen sniffer protocol" },
/* WTAP_ENCAP_ENC */
{ "enc", "OpenBSD enc(4) encapsulating interface" },
/* WTAP_ENCAP_PFLOG */
{ "pflog", "OpenBSD PF Firewall logs" },
/* WTAP_ENCAP_CHDLC_WITH_PHDR */
{ "chdlc-with-direction", "Cisco HDLC with Directional Info" },
/* WTAP_ENCAP_BLUETOOTH_H4 */
{ "bluetooth-h4", "Bluetooth H4" },
/* WTAP_ENCAP_MTP2 */
{ "mtp2", "SS7 MTP2" },
/* WTAP_ENCAP_MTP3 */
{ "mtp3", "SS7 MTP3" },
/* WTAP_ENCAP_IRDA */
{ "irda", "IrDA" },
/* WTAP_ENCAP_USER0 */
{ "user0", "USER 0" },
/* WTAP_ENCAP_USER1 */
{ "user1", "USER 1" },
/* WTAP_ENCAP_USER2 */
{ "user2", "USER 2" },
/* WTAP_ENCAP_USER3 */
{ "user3", "USER 3" },
/* WTAP_ENCAP_USER4 */
{ "user4", "USER 4" },
/* WTAP_ENCAP_USER5 */
{ "user5", "USER 5" },
/* WTAP_ENCAP_USER6 */
{ "user6", "USER 6" },
/* WTAP_ENCAP_USER7 */
{ "user7", "USER 7" },
/* WTAP_ENCAP_USER8 */
{ "user8", "USER 8" },
/* WTAP_ENCAP_USER9 */
{ "user9", "USER 9" },
/* WTAP_ENCAP_USER10 */
{ "user10", "USER 10" },
/* WTAP_ENCAP_USER11 */
{ "user11", "USER 11" },
/* WTAP_ENCAP_USER12 */
{ "user12", "USER 12" },
/* WTAP_ENCAP_USER13 */
{ "user13", "USER 13" },
/* WTAP_ENCAP_USER14 */
{ "user14", "USER 14" },
/* WTAP_ENCAP_USER15 */
{ "user15", "USER 15" },
/* WTAP_ENCAP_SYMANTEC */
{ "symantec", "Symantec Enterprise Firewall" },
/* WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 */
{ "ap1394", "Apple IP-over-IEEE 1394" },
/* WTAP_ENCAP_BACNET_MS_TP */
{ "bacnet-ms-tp", "BACnet MS/TP" },
/* WTAP_ENCAP_NETTL_RAW_ICMP */
{ "raw-icmp-nettl", "Raw ICMP with nettl headers" },
/* WTAP_ENCAP_NETTL_RAW_ICMPV6 */
{ "raw-icmpv6-nettl", "Raw ICMPv6 with nettl headers" },
/* WTAP_ENCAP_GPRS_LLC */
{ "gprs-llc", "GPRS LLC" },
/* WTAP_ENCAP_JUNIPER_ATM1 */
{ "juniper-atm1", "Juniper ATM1" },
/* WTAP_ENCAP_JUNIPER_ATM2 */
{ "juniper-atm2", "Juniper ATM2" },
/* WTAP_ENCAP_REDBACK */
{ "redback", "Redback SmartEdge" },
/* WTAP_ENCAP_NETTL_RAW_IP */
{ "rawip-nettl", "Raw IP with nettl headers" },
/* WTAP_ENCAP_NETTL_ETHERNET */
{ "ether-nettl", "Ethernet with nettl headers" },
/* WTAP_ENCAP_NETTL_TOKEN_RING */
{ "tr-nettl", "Token Ring with nettl headers" },
/* WTAP_ENCAP_NETTL_FDDI */
{ "fddi-nettl", "FDDI with nettl headers" },
/* WTAP_ENCAP_NETTL_UNKNOWN */
{ "unknown-nettl", "Unknown link-layer type with nettl headers" },
/* WTAP_ENCAP_MTP2_WITH_PHDR */
{ "mtp2-with-phdr", "MTP2 with pseudoheader" },
/* WTAP_ENCAP_JUNIPER_PPPOE */
{ "juniper-pppoe", "Juniper PPPoE" },
/* WTAP_ENCAP_GCOM_TIE1 */
{ "gcom-tie1", "GCOM TIE1" },
/* WTAP_ENCAP_GCOM_SERIAL */
{ "gcom-serial", "GCOM Serial" },
/* WTAP_ENCAP_NETTL_X25 */
{ "x25-nettl", "X.25 with nettl headers" },
/* WTAP_ENCAP_K12 */
{ "k12", "K12 protocol analyzer" },
/* WTAP_ENCAP_JUNIPER_MLPPP */
{ "juniper-mlppp", "Juniper MLPPP" },
/* WTAP_ENCAP_JUNIPER_MLFR */
{ "juniper-mlfr", "Juniper MLFR" },
/* WTAP_ENCAP_JUNIPER_ETHER */
{ "juniper-ether", "Juniper Ethernet" },
/* WTAP_ENCAP_JUNIPER_PPP */
{ "juniper-ppp", "Juniper PPP" },
/* WTAP_ENCAP_JUNIPER_FRELAY */
{ "juniper-frelay", "Juniper Frame-Relay" },
/* WTAP_ENCAP_JUNIPER_CHDLC */
{ "juniper-chdlc", "Juniper C-HDLC" },
/* WTAP_ENCAP_JUNIPER_GGSN */
{ "juniper-ggsn", "Juniper GGSN" },
/* WTAP_ENCAP_LINUX_LAPD */
{ "linux-lapd", "LAPD with Linux pseudo-header" },
/* WTAP_ENCAP_CATAPULT_DCT2000 */
{ "dct2000", "Catapult DCT2000" },
/* WTAP_ENCAP_BER */
{ "ber", "ASN.1 Basic Encoding Rules" },
/* WTAP_ENCAP_JUNIPER_VP */
{ "juniper-vp", "Juniper Voice PIC" },
/* WTAP_ENCAP_USB_FREEBSD */
{ "usb-freebsd", "USB packets with FreeBSD header" },
/* WTAP_ENCAP_IEEE802_16_MAC_CPS */
{ "ieee-802-16-mac-cps", "IEEE 802.16 MAC Common Part Sublayer" },
/* WTAP_ENCAP_NETTL_RAW_TELNET */
{ "raw-telnet-nettl", "Raw telnet with nettl headers" },
/* WTAP_ENCAP_USB_LINUX */
{ "usb-linux", "USB packets with Linux header" },
/* WTAP_ENCAP_MPEG */
{ "mpeg", "MPEG" },
/* WTAP_ENCAP_PPI */
{ "ppi", "Per-Packet Information header" },
/* WTAP_ENCAP_ERF */
{ "erf", "Extensible Record Format" },
/* WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR */
{ "bluetooth-h4-linux", "Bluetooth H4 with linux header" },
/* WTAP_ENCAP_SITA */
{ "sita-wan", "SITA WAN packets" },
/* WTAP_ENCAP_SCCP */
{ "sccp", "SS7 SCCP" },
/* WTAP_ENCAP_BLUETOOTH_HCI */
{ "bluetooth-hci", "Bluetooth without transport layer" },
/* WTAP_ENCAP_IPMB_KONTRON */
{ "ipmb-kontron", "Intelligent Platform Management Bus with Kontron pseudo-header" },
/* WTAP_ENCAP_IEEE802_15_4 */
{ "wpan", "IEEE 802.15.4 Wireless PAN" },
/* WTAP_ENCAP_X2E_XORAYA */
{ "x2e-xoraya", "X2E Xoraya" },
/* WTAP_ENCAP_FLEXRAY */
{ "flexray", "FlexRay" },
/* WTAP_ENCAP_LIN */
{ "lin", "Local Interconnect Network" },
/* WTAP_ENCAP_MOST */
{ "most", "Media Oriented Systems Transport" },
/* WTAP_ENCAP_CAN20B */
{ "can20b", "Controller Area Network 2.0B" },
/* WTAP_ENCAP_LAYER1_EVENT */
{ "layer1-event", "EyeSDN Layer 1 event" },
/* WTAP_ENCAP_X2E_SERIAL */
{ "x2e-serial", "X2E serial line capture" },
/* WTAP_ENCAP_I2C_LINUX */
{ "i2c-linux", "I2C with Linux-specific pseudo-header" },
/* WTAP_ENCAP_IEEE802_15_4_NONASK_PHY */
{ "wpan-nonask-phy", "IEEE 802.15.4 Wireless PAN non-ASK PHY" },
/* WTAP_ENCAP_TNEF */
{ "tnef", "Transport-Neutral Encapsulation Format" },
/* WTAP_ENCAP_USB_LINUX_MMAPPED */
{ "usb-linux-mmap", "USB packets with Linux header and padding" },
/* WTAP_ENCAP_GSM_UM */
{ "gsm_um", "GSM Um Interface" },
/* WTAP_ENCAP_DPNSS */
{ "dpnss_link", "Digital Private Signalling System No 1 Link Layer" },
/* WTAP_ENCAP_PACKETLOGGER */
{ "packetlogger", "Apple Bluetooth PacketLogger" },
/* WTAP_ENCAP_NSTRACE_1_0 */
{ "nstrace10", "NetScaler Encapsulation 1.0 of Ethernet" },
/* WTAP_ENCAP_NSTRACE_2_0 */
{ "nstrace20", "NetScaler Encapsulation 2.0 of Ethernet" },
/* WTAP_ENCAP_FIBRE_CHANNEL_FC2 */
{ "fc2", "Fibre Channel FC-2" },
/* WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS */
{ "fc2sof", "Fibre Channel FC-2 With Frame Delimiter" },
/* WTAP_ENCAP_JPEG_JFIF */
{ "jfif", "JPEG/JFIF" },
/* WTAP_ENCAP_IPNET */
{ "ipnet", "Solaris IPNET" },
/* WTAP_ENCAP_SOCKETCAN */
{ "socketcan", "SocketCAN" },
/* WTAP_ENCAP_IEEE_802_11_NETMON */
{ "ieee-802-11-netmon", "IEEE 802.11 plus Network Monitor radio header" },
/* WTAP_ENCAP_IEEE802_15_4_NOFCS */
{ "wpan-nofcs", "IEEE 802.15.4 Wireless PAN with FCS not present" },
/* WTAP_ENCAP_RAW_IPFIX */
{ "ipfix", "RFC 5655/RFC 5101 IPFIX" },
/* WTAP_ENCAP_RAW_IP4 */
{ "rawip4", "Raw IPv4" },
/* WTAP_ENCAP_RAW_IP6 */
{ "rawip6", "Raw IPv6" },
/* WTAP_ENCAP_LAPD */
{ "lapd", "LAPD" },
/* WTAP_ENCAP_DVBCI */
{ "dvbci", "DVB-CI (Common Interface)" },
/* WTAP_ENCAP_MUX27010 */
{ "mux27010", "MUX27010" },
/* WTAP_ENCAP_MIME */
{ "mime", "MIME" },
/* WTAP_ENCAP_NETANALYZER */
{ "netanalyzer", "Hilscher netANALYZER" },
/* WTAP_ENCAP_NETANALYZER_TRANSPARENT */
{ "netanalyzer-transparent", "Hilscher netANALYZER-Transparent" },
/* WTAP_ENCAP_IP_OVER_IB */
{ "ip-over-ib", "IP over InfiniBand" },
/* WTAP_ENCAP_MPEG_2_TS */
{ "mp2ts", "ISO/IEC 13818-1 MPEG2-TS" },
/* WTAP_ENCAP_PPP_ETHER */
{ "pppoes", "PPP-over-Ethernet session" },
/* WTAP_ENCAP_NFC_LLCP */
{ "nfc-llcp", "NFC LLCP" },
/* WTAP_ENCAP_NFLOG */
{ "nflog", "NFLOG" },
/* WTAP_ENCAP_V5_EF */
{ "v5-ef", "V5 Envelope Function" },
/* WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR */
{ "bacnet-ms-tp-with-direction", "BACnet MS/TP with Directional Info" },
/* WTAP_ENCAP_IXVERIWAVE */
{ "ixveriwave", "IxVeriWave header and stats block" },
/* WTAP_ENCAP_SDH */
{ "sdh", "SDH" },
/* WTAP_ENCAP_DBUS */
{ "dbus", "D-Bus" },
/* WTAP_ENCAP_AX25_KISS */
{ "ax25-kiss", "AX.25 with KISS header" },
/* WTAP_ENCAP_AX25 */
{ "ax25", "Amateur Radio AX.25" },
/* WTAP_ENCAP_SCTP */
{ "sctp", "SCTP" },
/* WTAP_ENCAP_INFINIBAND */
{ "infiniband", "InfiniBand" },
/* WTAP_ENCAP_JUNIPER_SVCS */
{ "juniper-svcs", "Juniper Services" },
/* WTAP_ENCAP_USBPCAP */
{ "usb-usbpcap", "USB packets with USBPcap header" },
/* WTAP_ENCAP_RTAC_SERIAL */
{ "rtac-serial", "RTAC serial-line" },
/* WTAP_ENCAP_BLUETOOTH_LE_LL */
{ "bluetooth-le-ll", "Bluetooth Low Energy Link Layer" },
/* WTAP_ENCAP_WIRESHARK_UPPER_PDU */
{ "wireshark-upper-pdu", "Wireshark Upper PDU export" },
/* WTAP_ENCAP_STANAG_4607 */
{ "s4607", "STANAG 4607" },
/* WTAP_ENCAP_STANAG_5066_D_PDU */
{ "s5066-dpdu", "STANAG 5066 Data Transfer Sublayer PDUs(D_PDU)" },
/* WTAP_ENCAP_NETLINK */
{ "netlink", "Linux Netlink" },
/* WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR */
{ "bluetooth-linux-monitor", "Bluetooth Linux Monitor" },
/* WTAP_ENCAP_BLUETOOTH_BREDR_BB */
{ "bluetooth-bredr-bb-rf", "Bluetooth BR/EDR Baseband RF" },
/* WTAP_ENCAP_BLUETOOTH_LE_LL_WITH_PHDR */
{ "bluetooth-le-ll-rf", "Bluetooth Low Energy Link Layer RF" },
/* WTAP_ENCAP_NSTRACE_3_0 */
{ "nstrace30", "NetScaler Encapsulation 3.0 of Ethernet" },
/* WTAP_ENCAP_LOGCAT */
{ "logcat", "Android Logcat Binary format" },
/* WTAP_ENCAP_LOGCAT_BRIEF */
{ "logcat_brief", "Android Logcat Brief text format" },
/* WTAP_ENCAP_LOGCAT_PROCESS */
{ "logcat_process", "Android Logcat Process text format" },
/* WTAP_ENCAP_LOGCAT_TAG */
{ "logcat_tag", "Android Logcat Tag text format" },
/* WTAP_ENCAP_LOGCAT_THREAD */
{ "logcat_thread", "Android Logcat Thread text format" },
/* WTAP_ENCAP_LOGCAT_TIME */
{ "logcat_time", "Android Logcat Time text format" },
/* WTAP_ENCAP_LOGCAT_THREADTIME */
{ "logcat_threadtime", "Android Logcat Threadtime text format" },
/* WTAP_ENCAP_LOGCAT_LONG */
{ "logcat_long", "Android Logcat Long text format" },
/* WTAP_ENCAP_PKTAP */
{ "pktap", "Apple PKTAP" },
/* WTAP_ENCAP_EPON */
{ "epon", "Ethernet Passive Optical Network" },
/* WTAP_ENCAP_IPMI_TRACE */
{ "ipmi-trace", "IPMI Trace Data Collection" },
/* WTAP_ENCAP_LOOP */
{ "loop", "OpenBSD loopback" },
/* WTAP_ENCAP_JSON */
{ "json", "JavaScript Object Notation" },
/* WTAP_ENCAP_NSTRACE_3_5 */
{ "nstrace35", "NetScaler Encapsulation 3.5 of Ethernet" },
/* WTAP_ENCAP_ISO14443 */
{ "iso14443", "ISO 14443 contactless smartcard standards" },
/* WTAP_ENCAP_GFP_T */
{ "gfp-t", "ITU-T G.7041/Y.1303 Generic Framing Procedure Transparent mode" },
/* WTAP_ENCAP_GFP_F */
{ "gfp-f", "ITU-T G.7041/Y.1303 Generic Framing Procedure Frame-mapped mode" },
/* WTAP_ENCAP_IP_OVER_IB_PCAP */
{ "ip-ib", "IP over IB" },
/* WTAP_ENCAP_JUNIPER_VN */
{ "juniper-vn", "Juniper VN" },
/* WTAP_ENCAP_USB_DARWIN */
{ "usb-darwin", "USB packets with Darwin (macOS, etc.) headers" },
/* WTAP_ENCAP_LORATAP */
{ "loratap", "LoRaTap" },
/* WTAP_ENCAP_3MB_ETHERNET */
{ "xeth", "Xerox 3MB Ethernet" },
/* WTAP_ENCAP_VSOCK */
{ "vsock", "Linux vsock" },
/* WTAP_ENCAP_NORDIC_BLE */
{ "nordic_ble", "nRF Sniffer for Bluetooth LE" },
/* WTAP_ENCAP_NETMON_NET_NETEVENT */
{ "netmon_event", "Network Monitor Network Event" },
/* WTAP_ENCAP_NETMON_HEADER */
{ "netmon_header", "Network Monitor Header" },
/* WTAP_ENCAP_NETMON_NET_FILTER */
{ "netmon_filter", "Network Monitor Filter" },
/* WTAP_ENCAP_NETMON_NETWORK_INFO_EX */
{ "netmon_network_info", "Network Monitor Network Info" },
/* WTAP_ENCAP_MA_WFP_CAPTURE_V4 */
{ "message_analyzer_wfp_capture_v4", "Message Analyzer WFP Capture v4" },
/* WTAP_ENCAP_MA_WFP_CAPTURE_V6 */
{ "message_analyzer_wfp_capture_v6", "Message Analyzer WFP Capture v6" },
/* WTAP_ENCAP_MA_WFP_CAPTURE_2V4 */
{ "message_analyzer_wfp_capture2_v4", "Message Analyzer WFP Capture2 v4" },
/* WTAP_ENCAP_MA_WFP_CAPTURE_2V6 */
{ "message_analyzer_wfp_capture2_v6", "Message Analyzer WFP Capture2 v6" },
/* WTAP_ENCAP_MA_WFP_CAPTURE_AUTH_V4 */
{ "message_analyzer_wfp_capture_auth_v4", "Message Analyzer WFP Capture Auth v4" },
/* WTAP_ENCAP_MA_WFP_CAPTURE_AUTH_V6 */
{ "message_analyzer_wfp_capture_auth_v6", "Message Analyzer WFP Capture Auth v6" },
/* WTAP_ENCAP_JUNIPER_ST */
{ "juniper-st", "Juniper Secure Tunnel Information" },
/* WTAP_ENCAP_ETHERNET_MPACKET */
{ "ether-mpacket", "IEEE 802.3br mPackets" },
/* WTAP_ENCAP_DOCSIS31_XRA31 */
{ "docsis31_xra31", "DOCSIS with Excentis XRA pseudo-header" },
/* WTAP_ENCAP_DPAUXMON */
{ "dpauxmon", "DisplayPort AUX channel with Unigraf pseudo-header" },
/* WTAP_ENCAP_RUBY_MARSHAL */
{ "ruby_marshal", "Ruby marshal object" },
/* WTAP_ENCAP_RFC7468 */
{ "rfc7468", "RFC 7468 file" },
/* WTAP_ENCAP_SYSTEMD_JOURNAL */
{ "sdjournal", "systemd journal" },
/* WTAP_ENCAP_EBHSCR */
{ "ebhscr", "Elektrobit High Speed Capture and Replay" },
/* WTAP_ENCAP_VPP */
{ "vpp", "Vector Packet Processing graph dispatch trace" },
/* WTAP_ENCAP_IEEE802_15_4_TAP */
{ "wpan-tap", "IEEE 802.15.4 Wireless with TAP pseudo-header" },
/* WTAP_ENCAP_LOG_3GPP */
{ "log_3GPP", "3GPP Phone Log" },
/* WTAP_ENCAP_USB_2_0 */
{ "usb-20", "USB 2.0/1.1/1.0 packets" },
/* WTAP_ENCAP_MP4 */
{ "mp4", "MP4 files" },
/* WTAP_ENCAP_SLL2 */
{ "linux-sll2", "Linux cooked-mode capture v2" },
/* WTAP_ENCAP_ZWAVE_SERIAL */
{ "zwave-serial", "Z-Wave Serial API packets" },
/* WTAP_ENCAP_ETW */
{ "etw", "Event Tracing for Windows messages" },
/* WTAP_ENCAP_ERI_ENB_LOG */
{ "eri_enb_log", "Ericsson eNode-B raw log" },
/* WTAP_ENCAP_ZBNCP */
{ "zbncp", "ZBOSS NCP" },
/* WTAP_ENCAP_USB_2_0_LOW_SPEED */
{ "usb-20-low", "Low-Speed USB 2.0/1.1/1.0 packets" },
/* WTAP_ENCAP_USB_2_0_FULL_SPEED */
{ "usb-20-full", "Full-Speed USB 2.0/1.1/1.0 packets" },
/* WTAP_ENCAP_USB_2_0_HIGH_SPEED */
{ "usb-20-high", "High-Speed USB 2.0 packets" },
/* WTAP_ENCAP_AUTOSAR_DLT */
{ "autosardlt", "AUTOSAR DLT" },
/* WTAP_ENCAP_AUERSWALD_LOG */
{ "auerlog", "Auerswald Log" },
/* WTAP_ENCAP_ATSC_ALP */
{ "alp", "ATSC Link-Layer Protocol (A/330) packets" },
/* WTAP_ENCAP_FIRA_UCI */
{ "fira-uci", "FiRa UWB Controller Interface (UCI) protocol." },
/* WTAP_ENCAP_SILABS_DEBUG_CHANNEL */
{ "silabs-dch", "Silabs Debug Channel"},
};
WS_DLL_LOCAL
gint wtap_num_encap_types = sizeof(encap_table_base) / sizeof(struct encap_type_info);
static GArray* encap_table_arr = NULL;
#define encap_table_entry(encap) \
g_array_index(encap_table_arr, struct encap_type_info, encap)
static void wtap_init_encap_types(void) {
if (encap_table_arr) return;
encap_table_arr = g_array_new(FALSE,TRUE,sizeof(struct encap_type_info));
g_array_append_vals(encap_table_arr,encap_table_base,wtap_num_encap_types);
}
static void wtap_cleanup_encap_types(void) {
if (encap_table_arr) {
g_array_free(encap_table_arr, TRUE);
encap_table_arr = NULL;
}
}
int wtap_get_num_encap_types(void) {
return wtap_num_encap_types;
}
int
wtap_register_encap_type(const char *description, const char *name)
{
struct encap_type_info e;
e.name = g_strdup(name);
e.description = g_strdup(description);
g_array_append_val(encap_table_arr,e);
return wtap_num_encap_types++;
}
/* Name to use in, say, a command-line flag specifying the type. */
const char *
wtap_encap_name(int encap)
{
if (encap < WTAP_ENCAP_NONE || encap >= WTAP_NUM_ENCAP_TYPES)
return "illegal";
else if (encap == WTAP_ENCAP_NONE)
return "none";
else if (encap == WTAP_ENCAP_PER_PACKET)
return "per-packet";
else
return encap_table_entry(encap).name;
}
/* Description to show to users. */
const char *
wtap_encap_description(int encap)
{
if (encap < WTAP_ENCAP_NONE || encap >= WTAP_NUM_ENCAP_TYPES)
return "Illegal";
else if (encap == WTAP_ENCAP_NONE)
return "None";
else if (encap == WTAP_ENCAP_PER_PACKET)
return "Per packet";
else
return encap_table_entry(encap).description;
}
/* Translate a name to a capture file type. */
int
wtap_name_to_encap(const char *name)
{
int encap;
for (encap = 0; encap < WTAP_NUM_ENCAP_TYPES; encap++) {
if (encap_table_entry(encap).name != NULL &&
strcmp(name, encap_table_entry(encap).name) == 0)
return encap;
}
return -1; /* no such encapsulation type */
}
const char*
wtap_tsprec_string(int tsprec)
{
const char* s;
switch (tsprec) {
case WTAP_TSPREC_PER_PACKET:
s = "per-packet";
break;
case WTAP_TSPREC_SEC:
s = "seconds";
break;
case WTAP_TSPREC_DSEC:
s = "deciseconds";
break;
case WTAP_TSPREC_CSEC:
s = "centiseconds";
break;
case WTAP_TSPREC_MSEC:
s = "milliseconds";
break;
case WTAP_TSPREC_USEC:
s = "microseconds";
break;
case WTAP_TSPREC_NSEC:
s = "nanoseconds";
break;
case WTAP_TSPREC_UNKNOWN:
default:
s = "UNKNOWN";
break;
}
return s;
}
static const char *wtap_errlist[] = {
/* WTAP_ERR_NOT_REGULAR_FILE */
"The file isn't a plain file or pipe",
/* WTAP_ERR_RANDOM_OPEN_PIPE */
"The file is being opened for random access but is a pipe",
/* WTAP_ERR_FILE_UNKNOWN_FORMAT */
"The file isn't a capture file in a known format",
/* WTAP_ERR_UNSUPPORTED */
"File contains record data we don't support",
/* WTAP_ERR_CANT_WRITE_TO_PIPE */
"That file format cannot be written to a pipe",
/* WTAP_ERR_CANT_OPEN */
NULL,
/* WTAP_ERR_UNWRITABLE_FILE_TYPE */
"Files can't be saved in that format",
/* WTAP_ERR_UNWRITABLE_ENCAP */
"Packets with that network type can't be saved in that format",
/* WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED */
"That file format doesn't support per-packet encapsulations",
/* WTAP_ERR_CANT_WRITE */
"A write failed for some unknown reason",
/* WTAP_ERR_CANT_CLOSE */
NULL,
/* WTAP_ERR_SHORT_READ */
"Less data was read than was expected",
/* WTAP_ERR_BAD_FILE */
"The file appears to be damaged or corrupt",
/* WTAP_ERR_SHORT_WRITE */
"Less data was written than was requested",
/* WTAP_ERR_UNC_OVERFLOW */
"Uncompression error: data would overflow buffer",
/* WTAP_ERR_RANDOM_OPEN_STDIN */
"The standard input cannot be opened for random access",
/* WTAP_ERR_COMPRESSION_NOT_SUPPORTED */
"That file format doesn't support compression",
/* WTAP_ERR_CANT_SEEK */
NULL,
/* WTAP_ERR_CANT_SEEK_COMPRESSED */
NULL,
/* WTAP_ERR_DECOMPRESS */
"Uncompression error",
/* WTAP_ERR_INTERNAL */
"Internal error",
/* WTAP_ERR_PACKET_TOO_LARGE */
"The packet being written is too large for that format",
/* WTAP_ERR_CHECK_WSLUA */
NULL,
/* WTAP_ERR_UNWRITABLE_REC_TYPE */
"That record type cannot be written in that format",
/* WTAP_ERR_UNWRITABLE_REC_DATA */
"That record can't be written in that format",
/* WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED */
"We don't support decompressing that type of compressed file",
/* WTAP_ERR_TIME_STAMP_NOT_SUPPORTED */
"We don't support writing that record's time stamp to that file type",
};
#define WTAP_ERRLIST_SIZE (sizeof wtap_errlist / sizeof wtap_errlist[0])
const char *
wtap_strerror(int err)
{
static char errbuf[128];
unsigned int wtap_errlist_index;
if (err < 0) {
wtap_errlist_index = -1 - err;
if (wtap_errlist_index >= WTAP_ERRLIST_SIZE) {
snprintf(errbuf, 128, "Error %d", err);
return errbuf;
}
if (wtap_errlist[wtap_errlist_index] == NULL)
return "Unknown reason";
return wtap_errlist[wtap_errlist_index];
} else
return g_strerror(err);
}
/* Close only the sequential side, freeing up memory it uses.
Note that we do *not* want to call the subtype's close function,
as it would free any per-subtype data, and that data may be
needed by the random-access side.
Instead, if the subtype has a "sequential close" function, we call it,
to free up stuff used only by the sequential side. */
void
wtap_sequential_close(wtap *wth)
{
if (wth->subtype_sequential_close != NULL)
(*wth->subtype_sequential_close)(wth);
if (wth->fh != NULL) {
file_close(wth->fh);
wth->fh = NULL;
}
}
static void
g_fast_seek_item_free(gpointer data, gpointer user_data _U_)
{
g_free(data);
}
/*
* Close the file descriptors for the sequential and random streams, but
* don't discard any information about those streams. Used on Windows if
* we need to rename a file that we have open or if we need to rename on
* top of a file we have open.
*/
void
wtap_fdclose(wtap *wth)
{
if (wth->fh != NULL)
file_fdclose(wth->fh);
if (wth->random_fh != NULL)
file_fdclose(wth->random_fh);
}
void
wtap_close(wtap *wth)
{
wtap_sequential_close(wth);
if (wth->subtype_close != NULL)
(*wth->subtype_close)(wth);
if (wth->random_fh != NULL)
file_close(wth->random_fh);
g_free(wth->priv);
g_free(wth->pathname);
if (wth->fast_seek != NULL) {
g_ptr_array_foreach(wth->fast_seek, g_fast_seek_item_free, NULL);
g_ptr_array_free(wth->fast_seek, TRUE);
}
wtap_block_array_free(wth->shb_hdrs);
wtap_block_array_free(wth->nrbs);
wtap_block_array_free(wth->interface_data);
wtap_block_array_free(wth->dsbs);
g_free(wth);
}
void
wtap_cleareof(wtap *wth) {
/* Reset EOF */
file_clearerr(wth->fh);
}
static inline void
wtapng_process_nrb_ipv4(wtap *wth, wtap_block_t nrb)
{
const wtapng_nrb_mandatory_t *nrb_mand = (wtapng_nrb_mandatory_t*)wtap_block_get_mandatory_data(nrb);
if (wth->add_new_ipv4) {
for (GList *elem = nrb_mand->ipv4_addr_list; elem != NULL; elem = elem->next) {
hashipv4_t *tp = elem->data;
wth->add_new_ipv4(tp->addr, tp->name, FALSE);
}
}
}
static inline void
wtapng_process_nrb_ipv6(wtap *wth, wtap_block_t nrb)
{
const wtapng_nrb_mandatory_t *nrb_mand = (wtapng_nrb_mandatory_t*)wtap_block_get_mandatory_data(nrb);
if (wth->add_new_ipv6) {
for (GList *elem = nrb_mand->ipv6_addr_list; elem != NULL; elem = elem->next) {
hashipv6_t *tp = elem->data;
wth->add_new_ipv6(tp->addr, tp->name, FALSE);
}
}
}
void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4) {
if (!wth)
return;
wth->add_new_ipv4 = add_new_ipv4;
/* Are there any existing NRBs? */
if (!wth->nrbs)
return;
/*
* Send all NRBs that were read so far to the new callback. file.c
* relies on this to support redissection (during redissection, the
* previous name resolutions are lost and has to be resupplied).
*/
for (guint i = 0; i < wth->nrbs->len; i++) {
wtap_block_t nrb = g_array_index(wth->nrbs, wtap_block_t, i);
wtapng_process_nrb_ipv4(wth, nrb);
}
}
void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6) {
if (!wth)
return;
wth->add_new_ipv6 = add_new_ipv6;
/* Are there any existing NRBs? */
if (!wth->nrbs)
return;
/*
* Send all NRBs that were read so far to the new callback. file.c
* relies on this to support redissection (during redissection, the
* previous name resolutions are lost and has to be resupplied).
*/
for (guint i = 0; i < wth->nrbs->len; i++) {
wtap_block_t nrb = g_array_index(wth->nrbs, wtap_block_t, i);
wtapng_process_nrb_ipv6(wth, nrb);
}
}
void
wtapng_process_nrb(wtap *wth, wtap_block_t nrb)
{
wtapng_process_nrb_ipv4(wth, nrb);
wtapng_process_nrb_ipv6(wth, nrb);
}
void wtap_set_cb_new_secrets(wtap *wth, wtap_new_secrets_callback_t add_new_secrets) {
/* Is a valid wth given that supports DSBs? */
if (!wth || !wth->dsbs)
return;
wth->add_new_secrets = add_new_secrets;
/*
* Send all DSBs that were read so far to the new callback. file.c
* relies on this to support redissection (during redissection, the
* previous secrets are lost and has to be resupplied).
*/
for (guint i = 0; i < wth->dsbs->len; i++) {
wtap_block_t dsb = g_array_index(wth->dsbs, wtap_block_t, i);
wtapng_process_dsb(wth, dsb);
}
}
void
wtapng_process_dsb(wtap *wth, wtap_block_t dsb)
{
const wtapng_dsb_mandatory_t *dsb_mand = (wtapng_dsb_mandatory_t*)wtap_block_get_mandatory_data(dsb);
if (wth->add_new_secrets)
wth->add_new_secrets(dsb_mand->secrets_type, dsb_mand->secrets_data, dsb_mand->secrets_len);
}
/* Perform per-packet initialization */
static void
wtap_init_rec(wtap *wth, wtap_rec *rec)
{
/*
* Set the packet encapsulation to the file's encapsulation
* value; if that's not WTAP_ENCAP_PER_PACKET, it's the
* right answer (and means that the read routine for this
* capture file type doesn't have to set it), and if it
* *is* WTAP_ENCAP_PER_PACKET, the caller needs to set it
* anyway.
*
* Do the same for the packet time stamp resolution.
*/
rec->rec_header.packet_header.pkt_encap = wth->file_encap;
rec->tsprec = wth->file_tsprec;
rec->block = NULL;
rec->block_was_modified = FALSE;
/*
* Assume the file has only one section; the module for the
* file type needs to indicate the section number if there's
* more than one section.
*/
rec->section_number = 0;
}
gboolean
wtap_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err,
gchar **err_info, gint64 *offset)
{
/*
* Initialize the record to default values.
*/
wtap_init_rec(wth, rec);
ws_buffer_clean(buf);
*err = 0;
*err_info = NULL;
if (!wth->subtype_read(wth, rec, buf, err, err_info, offset)) {
/*
* If we didn't get an error indication, we read
* the last packet. See if there's any deferred
* error, as might, for example, occur if we're
* reading a compressed file, and we got an error
* reading compressed data from the file, but
* got enough compressed data to decompress the
* last packet of the file.
*/
if (*err == 0)
*err = file_error(wth->fh, err_info);
if (rec->block != NULL) {
/*
* Unreference any block created for this record.
*/
wtap_block_unref(rec->block);
rec->block = NULL;
}
return FALSE; /* failure */
}
/*
* Is this a packet record?
*/
if (rec->rec_type == REC_TYPE_PACKET) {
/*
* Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
* probably means the file has that encapsulation type
* but the read routine didn't set this packet's
* encapsulation type.
*/
ws_assert(rec->rec_header.packet_header.pkt_encap != WTAP_ENCAP_PER_PACKET);
ws_assert(rec->rec_header.packet_header.pkt_encap != WTAP_ENCAP_NONE);
}
return TRUE; /* success */
}
/*
* Read a given number of bytes from a file into a buffer or, if
* buf is NULL, just discard them.
*
* If we succeed, return TRUE.
*
* If we get an EOF, return FALSE with *err set to 0, reporting this
* as an EOF.
*
* If we get fewer bytes than the specified number, return FALSE with
* *err set to WTAP_ERR_SHORT_READ, reporting this as a short read
* error.
*
* If we get a read error, return FALSE with *err and *err_info set
* appropriately.
*/
gboolean
wtap_read_bytes_or_eof(FILE_T fh, void *buf, unsigned int count, int *err,
gchar **err_info)
{
int bytes_read;
bytes_read = file_read(buf, count, fh);
if (bytes_read < 0 || (guint)bytes_read != count) {
*err = file_error(fh, err_info);
if (*err == 0 && bytes_read > 0)
*err = WTAP_ERR_SHORT_READ;
return FALSE;
}
return TRUE;
}
/*
* Read a given number of bytes from a file into a buffer or, if
* buf is NULL, just discard them.
*
* If we succeed, return TRUE.
*
* If we get fewer bytes than the specified number, including getting
* an EOF, return FALSE with *err set to WTAP_ERR_SHORT_READ, reporting
* this as a short read error.
*
* If we get a read error, return FALSE with *err and *err_info set
* appropriately.
*/
gboolean
wtap_read_bytes(FILE_T fh, void *buf, unsigned int count, int *err,
gchar **err_info)
{
int bytes_read;
bytes_read = file_read(buf, count, fh);
if (bytes_read < 0 || (guint)bytes_read != count) {
*err = file_error(fh, err_info);
if (*err == 0)
*err = WTAP_ERR_SHORT_READ;
return FALSE;
}
return TRUE;
}
/*
* Read packet data into a Buffer, growing the buffer as necessary.
*
* This returns an error on a short read, even if the short read hit
* the EOF immediately. (The assumption is that each packet has a
* header followed by raw packet data, and that we've already read the
* header, so if we get an EOF trying to read the packet data, the file
* has been cut short, even if the read didn't read any data at all.)
*/
gboolean
wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
gchar **err_info)
{
gboolean rv;
ws_buffer_assure_space(buf, length);
rv = wtap_read_bytes(fh, ws_buffer_end_ptr(buf), length, err,
err_info);
if (rv) {
ws_buffer_increase_length(buf, length);
}
return rv;
}
/*
* Return an approximation of the amount of data we've read sequentially
* from the file so far. (gint64, in case that's 64 bits.)
*/
gint64
wtap_read_so_far(wtap *wth)
{
return file_tell_raw(wth->fh);
}
/* Perform global/initial initialization */
void
wtap_rec_init(wtap_rec *rec)
{
memset(rec, 0, sizeof *rec);
ws_buffer_init(&rec->options_buf, 0);
/* In the future, see if we can create rec->block here once
* and have it be reused like the rest of rec.
* Currently it's recreated for each packet.
*/
}
/* re-initialize record */
void
wtap_rec_reset(wtap_rec *rec)
{
wtap_block_unref(rec->block);
rec->block = NULL;
rec->block_was_modified = FALSE;
}
/* clean up record metadata */
void
wtap_rec_cleanup(wtap_rec *rec)
{
wtap_rec_reset(rec);
ws_buffer_free(&rec->options_buf);
}
wtap_block_t
wtap_rec_generate_idb(const wtap_rec *rec)
{
int tsprec;
ws_assert(rec->rec_type == REC_TYPE_PACKET);
if (rec->presence_flags & WTAP_HAS_TS) {
tsprec = rec->tsprec;
} else {
tsprec = WTAP_TSPREC_USEC;
/* The default */
}
return wtap_generate_idb(rec->rec_header.packet_header.pkt_encap, tsprec, 0);
}
gboolean
wtap_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info)
{
/*
* Initialize the record to default values.
*/
wtap_init_rec(wth, rec);
ws_buffer_clean(buf);
*err = 0;
*err_info = NULL;
if (!wth->subtype_seek_read(wth, seek_off, rec, buf, err, err_info)) {
if (rec->block != NULL) {
/*
* Unreference any block created for this record.
*/
wtap_block_unref(rec->block);
rec->block = NULL;
}
return FALSE;
}
/*
* Is this a packet record?
*/
if (rec->rec_type == REC_TYPE_PACKET) {
/*
* Make sure that it's not WTAP_ENCAP_PER_PACKET, as that
* probably means the file has that encapsulation type
* but the read routine didn't set this packet's
* encapsulation type.
*/
ws_assert(rec->rec_header.packet_header.pkt_encap != WTAP_ENCAP_PER_PACKET);
ws_assert(rec->rec_header.packet_header.pkt_encap != WTAP_ENCAP_NONE);
}
return TRUE;
}
static gboolean
wtap_full_file_read_file(wtap *wth, FILE_T fh, wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
{
gint64 file_size;
int packet_size = 0;
const int block_size = 1024 * 1024;
if ((file_size = wtap_file_size(wth, err)) == -1)
return FALSE;
if (file_size > G_MAXINT) {
/*
* Avoid allocating space for an immensely-large file.
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("%s: File has %" PRId64 "-byte packet, bigger than maximum of %u",
wtap_encap_name(wth->file_encap), file_size, G_MAXINT);
return FALSE;
}
/*
* Compressed files might expand to a larger size than the actual file
* size. Try to read the full size and then read in smaller increments
* to avoid frequent memory reallocations.
*/
int buffer_size = block_size * (1 + (int)file_size / block_size);
for (;;) {
if (buffer_size <= 0) {
*err = WTAP_ERR_BAD_FILE;
*err_info = ws_strdup_printf("%s: Uncompressed file is bigger than maximum of %u",
wtap_encap_name(wth->file_encap), G_MAXINT);
return FALSE;
}
ws_buffer_assure_space(buf, buffer_size);
int nread = file_read(ws_buffer_start_ptr(buf) + packet_size, buffer_size - packet_size, fh);
if (nread < 0) {
*err = file_error(fh, err_info);
if (*err == 0)
*err = WTAP_ERR_BAD_FILE;
return FALSE;
}
packet_size += nread;
if (packet_size != buffer_size) {
/* EOF */
break;
}
buffer_size += block_size;
}
rec->rec_type = REC_TYPE_PACKET;
rec->presence_flags = 0; /* yes, we have no bananas^Wtime stamp */
rec->ts.secs = 0;
rec->ts.nsecs = 0;
rec->rec_header.packet_header.caplen = packet_size;
rec->rec_header.packet_header.len = packet_size;
return TRUE;
}
gboolean
wtap_full_file_read(wtap *wth, wtap_rec *rec, Buffer *buf,
int *err, gchar **err_info, gint64 *data_offset)
{
gint64 offset = file_tell(wth->fh);
/* There is only one packet with the full file contents. */
if (offset != 0) {
*err = 0;
return FALSE;
}
*data_offset = offset;
return wtap_full_file_read_file(wth, wth->fh, rec, buf, err, err_info);
}
gboolean
wtap_full_file_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
{
/* There is only one packet with the full file contents. */
if (seek_off > 0) {
*err = 0;
return FALSE;
}
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return FALSE;
return wtap_full_file_read_file(wth, wth->random_fh, rec, buf, err, err_info);
}
void
wtap_buffer_append_epdu_tag(Buffer *buf, guint16 epdu_tag, const guint8 *data, guint16 data_len)
{
guint8 pad_len = 0;
guint space_needed = 4; /* 2 for tag field, 2 for length field */
guint8 *buf_data;
if (epdu_tag != 0 && data != NULL && data_len != 0) {
pad_len += PADDING4(data_len);
space_needed += data_len + pad_len;
}
else {
data_len = 0;
}
ws_buffer_assure_space(buf, space_needed);
buf_data = ws_buffer_end_ptr(buf);
memset(buf_data, 0, space_needed);
phton16(buf_data + 0, epdu_tag);
/* It seems as though the convention for exported_pdu is to specify
* the fully-padded length of the tag value, not just its useful length.
* e.g. the string value 'a' would be given a length of 4.
*/
phton16(buf_data + 2, data_len + pad_len);
if (data_len > 0) {
/* Still only copy as many bytes as we actually have */
memcpy(buf_data + 4, data, data_len);
}
ws_buffer_increase_length(buf, space_needed);
}
void
wtap_buffer_append_epdu_uint(Buffer *buf, guint16 epdu_tag, guint32 val)
{
const guint space_needed = 8; /* 2 for tag field, 2 for length field, 4 for value */
guint8 *buf_data;
ws_assert(epdu_tag != 0);
ws_buffer_assure_space(buf, space_needed);
buf_data = ws_buffer_end_ptr(buf);
memset(buf_data, 0, space_needed);
phton16(buf_data + 0, epdu_tag);
phton16(buf_data + 2, 4);
phton32(buf_data + 4, val);
ws_buffer_increase_length(buf, space_needed);
}
void
wtap_buffer_append_epdu_string(Buffer *buf, guint16 epdu_tag, const char *val)
{
size_t string_len;
string_len = strlen(val);
/*
* Cut off string length at G_MAXUINT16.
*
* XXX - make sure we don't leave an incomplete UTF-8
* sequence at the end.
*/
if (string_len > G_MAXUINT16)
string_len = G_MAXUINT16;
wtap_buffer_append_epdu_tag(buf, epdu_tag, val, (guint16) string_len);
}
gint
wtap_buffer_append_epdu_end(Buffer *buf)
{
const guint space_needed = 4; /* 2 for tag (=0000), 2 for length field (=0) */
guint8 *buf_data;
ws_buffer_assure_space(buf, space_needed);
buf_data = ws_buffer_end_ptr(buf);
memset(buf_data, 0, space_needed);
ws_buffer_increase_length(buf, space_needed);
return (gint)ws_buffer_length(buf);
}
/*
* Initialize the library.
*/
void
wtap_init(gboolean load_wiretap_plugins)
{
init_open_routines();
wtap_opttypes_initialize();
wtap_init_encap_types();
wtap_init_file_type_subtypes();
if (load_wiretap_plugins) {
#ifdef HAVE_PLUGINS
libwiretap_plugins = plugins_init(WS_PLUGIN_WIRETAP);
#endif
g_slist_foreach(wtap_plugins, call_plugin_register_wtap_module, NULL);
}
}
/*
* Cleanup the library
*/
void
wtap_cleanup(void)
{
wtap_cleanup_encap_types();
wtap_opttypes_cleanup();
ws_buffer_cleanup();
cleanup_open_routines();
g_slist_free(wtap_plugins);
wtap_plugins = NULL;
#ifdef HAVE_PLUGINS
plugins_cleanup(libwiretap_plugins);
libwiretap_plugins = NULL;
#endif
}
/*
* 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:
*/ |
C/C++ | wireshark/wiretap/wtap.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WTAP_H__
#define __WTAP_H__
#include <wireshark.h>
#include <time.h>
#include <wsutil/buffer.h>
#include <wsutil/nstime.h>
#include <wsutil/inet_addr.h>
#include "wtap_opttypes.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* Encapsulation types. Choose names that truly reflect
* what is contained in the packet trace file.
*
* WTAP_ENCAP_PER_PACKET is a value passed to "wtap_dump_open()" or
* "wtap_dump_fdopen()" to indicate that there is no single encapsulation
* type for all packets in the file; this may cause those routines to
* fail if the capture file format being written can't support that.
* It's also returned by "wtap_file_encap()" for capture files that
* don't have a single encapsulation type for all packets in the file.
*
* WTAP_ENCAP_UNKNOWN is returned by "wtap_pcap_encap_to_wtap_encap()"
* if it's handed an unknown encapsulation. It is also used by file
* types for encapsulations which are unsupported by libwiretap.
*
* WTAP_ENCAP_NONE is an initial value used by file types like pcapng
* that do not have a single file level encapsulation type. If and when
* something that indicate encapsulation is read, the encapsulation will
* change (possibly to WTAP_ENCAP_PER_PACKET) and appropriate IDBs will
* be generated. If a file type uses this value, it MUST provide IDBs
* (possibly fake) when the encapsulation changes; otherwise, it should
* return WTAP_ENCAP_UNKNOWN so that attempts to write an output file
* without reading the entire input file first fail gracefully.
*
* WTAP_ENCAP_FDDI_BITSWAPPED is for FDDI captures on systems where the
* MAC addresses you get from the hardware are bit-swapped. Ideally,
* the driver would tell us that, but I know of none that do, so, for
* now, we base it on the machine on which we're *reading* the
* capture, rather than on the machine on which the capture was taken
* (they're probably likely to be the same). We assume that they're
* bit-swapped on everything except for systems running Ultrix, Alpha
* systems, and BSD/OS systems (that's what "tcpdump" does; I guess
* Digital decided to bit-swap addresses in the hardware or in the
* driver, and I guess BSDI bit-swapped them in the driver, given that
* BSD/OS generally runs on Boring Old PC's). If we create a wiretap
* save file format, we'd use the WTAP_ENCAP values to flag the
* encapsulation of a packet, so there we'd at least be able to base
* it on the machine on which the capture was taken.
*
* WTAP_ENCAP_LINUX_ATM_CLIP is the encapsulation you get with the
* ATM on Linux code from <http://linux-atm.sourceforge.net/>;
* that code adds a DLT_ATM_CLIP DLT_ code of 19, and that
* encapsulation isn't the same as the DLT_ATM_RFC1483 encapsulation
* presumably used on some BSD systems, which we turn into
* WTAP_ENCAP_ATM_RFC1483.
*
* WTAP_ENCAP_NULL corresponds to DLT_NULL from "libpcap". This
* corresponds to
*
* 1) PPP-over-HDLC encapsulation, at least with some versions
* of ISDN4BSD (but not the current ones, it appears, unless
* I've missed something);
*
* 2) a 4-byte header containing the AF_ address family, in
* the byte order of the machine that saved the capture,
* for the packet, as used on many BSD systems for the
* loopback device and some other devices, or a 4-byte header
* containing the AF_ address family in network byte order,
* as used on recent OpenBSD systems for the loopback device;
*
* 3) a 4-byte header containing 2 octets of 0 and an Ethernet
* type in the byte order from an Ethernet header, that being
* what older versions of "libpcap" on Linux turn the Ethernet
* header for loopback interfaces into (0.6.0 and later versions
* leave the Ethernet header alone and make it DLT_EN10MB). */
#define WTAP_ENCAP_NONE -2
#define WTAP_ENCAP_PER_PACKET -1
#define WTAP_ENCAP_UNKNOWN 0
#define WTAP_ENCAP_ETHERNET 1
#define WTAP_ENCAP_TOKEN_RING 2
#define WTAP_ENCAP_SLIP 3
#define WTAP_ENCAP_PPP 4
#define WTAP_ENCAP_FDDI 5
#define WTAP_ENCAP_FDDI_BITSWAPPED 6
#define WTAP_ENCAP_RAW_IP 7
#define WTAP_ENCAP_ARCNET 8
#define WTAP_ENCAP_ARCNET_LINUX 9
#define WTAP_ENCAP_ATM_RFC1483 10
#define WTAP_ENCAP_LINUX_ATM_CLIP 11
#define WTAP_ENCAP_LAPB 12
#define WTAP_ENCAP_ATM_PDUS 13
#define WTAP_ENCAP_ATM_PDUS_UNTRUNCATED 14
#define WTAP_ENCAP_NULL 15
#define WTAP_ENCAP_ASCEND 16
#define WTAP_ENCAP_ISDN 17
#define WTAP_ENCAP_IP_OVER_FC 18
#define WTAP_ENCAP_PPP_WITH_PHDR 19
#define WTAP_ENCAP_IEEE_802_11 20
#define WTAP_ENCAP_IEEE_802_11_PRISM 21
#define WTAP_ENCAP_IEEE_802_11_WITH_RADIO 22
#define WTAP_ENCAP_IEEE_802_11_RADIOTAP 23
#define WTAP_ENCAP_IEEE_802_11_AVS 24
#define WTAP_ENCAP_SLL 25
#define WTAP_ENCAP_FRELAY 26
#define WTAP_ENCAP_FRELAY_WITH_PHDR 27
#define WTAP_ENCAP_CHDLC 28
#define WTAP_ENCAP_CISCO_IOS 29
#define WTAP_ENCAP_LOCALTALK 30
#define WTAP_ENCAP_OLD_PFLOG 31
#define WTAP_ENCAP_HHDLC 32
#define WTAP_ENCAP_DOCSIS 33
#define WTAP_ENCAP_COSINE 34
#define WTAP_ENCAP_WFLEET_HDLC 35
#define WTAP_ENCAP_SDLC 36
#define WTAP_ENCAP_TZSP 37
#define WTAP_ENCAP_ENC 38
#define WTAP_ENCAP_PFLOG 39
#define WTAP_ENCAP_CHDLC_WITH_PHDR 40
#define WTAP_ENCAP_BLUETOOTH_H4 41
#define WTAP_ENCAP_MTP2 42
#define WTAP_ENCAP_MTP3 43
#define WTAP_ENCAP_IRDA 44
#define WTAP_ENCAP_USER0 45
#define WTAP_ENCAP_USER1 46
#define WTAP_ENCAP_USER2 47
#define WTAP_ENCAP_USER3 48
#define WTAP_ENCAP_USER4 49
#define WTAP_ENCAP_USER5 50
#define WTAP_ENCAP_USER6 51
#define WTAP_ENCAP_USER7 52
#define WTAP_ENCAP_USER8 53
#define WTAP_ENCAP_USER9 54
#define WTAP_ENCAP_USER10 55
#define WTAP_ENCAP_USER11 56
#define WTAP_ENCAP_USER12 57
#define WTAP_ENCAP_USER13 58
#define WTAP_ENCAP_USER14 59
#define WTAP_ENCAP_USER15 60
#define WTAP_ENCAP_SYMANTEC 61
#define WTAP_ENCAP_APPLE_IP_OVER_IEEE1394 62
#define WTAP_ENCAP_BACNET_MS_TP 63
#define WTAP_ENCAP_NETTL_RAW_ICMP 64
#define WTAP_ENCAP_NETTL_RAW_ICMPV6 65
#define WTAP_ENCAP_GPRS_LLC 66
#define WTAP_ENCAP_JUNIPER_ATM1 67
#define WTAP_ENCAP_JUNIPER_ATM2 68
#define WTAP_ENCAP_REDBACK 69
#define WTAP_ENCAP_NETTL_RAW_IP 70
#define WTAP_ENCAP_NETTL_ETHERNET 71
#define WTAP_ENCAP_NETTL_TOKEN_RING 72
#define WTAP_ENCAP_NETTL_FDDI 73
#define WTAP_ENCAP_NETTL_UNKNOWN 74
#define WTAP_ENCAP_MTP2_WITH_PHDR 75
#define WTAP_ENCAP_JUNIPER_PPPOE 76
#define WTAP_ENCAP_GCOM_TIE1 77
#define WTAP_ENCAP_GCOM_SERIAL 78
#define WTAP_ENCAP_NETTL_X25 79
#define WTAP_ENCAP_K12 80
#define WTAP_ENCAP_JUNIPER_MLPPP 81
#define WTAP_ENCAP_JUNIPER_MLFR 82
#define WTAP_ENCAP_JUNIPER_ETHER 83
#define WTAP_ENCAP_JUNIPER_PPP 84
#define WTAP_ENCAP_JUNIPER_FRELAY 85
#define WTAP_ENCAP_JUNIPER_CHDLC 86
#define WTAP_ENCAP_JUNIPER_GGSN 87
#define WTAP_ENCAP_LINUX_LAPD 88
#define WTAP_ENCAP_CATAPULT_DCT2000 89
#define WTAP_ENCAP_BER 90
#define WTAP_ENCAP_JUNIPER_VP 91
#define WTAP_ENCAP_USB_FREEBSD 92
#define WTAP_ENCAP_IEEE802_16_MAC_CPS 93
#define WTAP_ENCAP_NETTL_RAW_TELNET 94
#define WTAP_ENCAP_USB_LINUX 95
#define WTAP_ENCAP_MPEG 96
#define WTAP_ENCAP_PPI 97
#define WTAP_ENCAP_ERF 98
#define WTAP_ENCAP_BLUETOOTH_H4_WITH_PHDR 99
#define WTAP_ENCAP_SITA 100
#define WTAP_ENCAP_SCCP 101
#define WTAP_ENCAP_BLUETOOTH_HCI 102 /*raw packets without a transport layer header e.g. H4*/
#define WTAP_ENCAP_IPMB_KONTRON 103
#define WTAP_ENCAP_IEEE802_15_4 104
#define WTAP_ENCAP_X2E_XORAYA 105
#define WTAP_ENCAP_FLEXRAY 106
#define WTAP_ENCAP_LIN 107
#define WTAP_ENCAP_MOST 108
#define WTAP_ENCAP_CAN20B 109
#define WTAP_ENCAP_LAYER1_EVENT 110
#define WTAP_ENCAP_X2E_SERIAL 111
#define WTAP_ENCAP_I2C_LINUX 112
#define WTAP_ENCAP_IEEE802_15_4_NONASK_PHY 113
#define WTAP_ENCAP_TNEF 114
#define WTAP_ENCAP_USB_LINUX_MMAPPED 115
#define WTAP_ENCAP_GSM_UM 116
#define WTAP_ENCAP_DPNSS 117
#define WTAP_ENCAP_PACKETLOGGER 118
#define WTAP_ENCAP_NSTRACE_1_0 119
#define WTAP_ENCAP_NSTRACE_2_0 120
#define WTAP_ENCAP_FIBRE_CHANNEL_FC2 121
#define WTAP_ENCAP_FIBRE_CHANNEL_FC2_WITH_FRAME_DELIMS 122
#define WTAP_ENCAP_JPEG_JFIF 123 /* obsoleted by WTAP_ENCAP_MIME*/
#define WTAP_ENCAP_IPNET 124
#define WTAP_ENCAP_SOCKETCAN 125
#define WTAP_ENCAP_IEEE_802_11_NETMON 126
#define WTAP_ENCAP_IEEE802_15_4_NOFCS 127
#define WTAP_ENCAP_RAW_IPFIX 128
#define WTAP_ENCAP_RAW_IP4 129
#define WTAP_ENCAP_RAW_IP6 130
#define WTAP_ENCAP_LAPD 131
#define WTAP_ENCAP_DVBCI 132
#define WTAP_ENCAP_MUX27010 133
#define WTAP_ENCAP_MIME 134
#define WTAP_ENCAP_NETANALYZER 135
#define WTAP_ENCAP_NETANALYZER_TRANSPARENT 136
#define WTAP_ENCAP_IP_OVER_IB_SNOOP 137
#define WTAP_ENCAP_MPEG_2_TS 138
#define WTAP_ENCAP_PPP_ETHER 139
#define WTAP_ENCAP_NFC_LLCP 140
#define WTAP_ENCAP_NFLOG 141
#define WTAP_ENCAP_V5_EF 142
#define WTAP_ENCAP_BACNET_MS_TP_WITH_PHDR 143
#define WTAP_ENCAP_IXVERIWAVE 144
#define WTAP_ENCAP_SDH 145
#define WTAP_ENCAP_DBUS 146
#define WTAP_ENCAP_AX25_KISS 147
#define WTAP_ENCAP_AX25 148
#define WTAP_ENCAP_SCTP 149
#define WTAP_ENCAP_INFINIBAND 150
#define WTAP_ENCAP_JUNIPER_SVCS 151
#define WTAP_ENCAP_USBPCAP 152
#define WTAP_ENCAP_RTAC_SERIAL 153
#define WTAP_ENCAP_BLUETOOTH_LE_LL 154
#define WTAP_ENCAP_WIRESHARK_UPPER_PDU 155
#define WTAP_ENCAP_STANAG_4607 156
#define WTAP_ENCAP_STANAG_5066_D_PDU 157
#define WTAP_ENCAP_NETLINK 158
#define WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR 159
#define WTAP_ENCAP_BLUETOOTH_BREDR_BB 160
#define WTAP_ENCAP_BLUETOOTH_LE_LL_WITH_PHDR 161
#define WTAP_ENCAP_NSTRACE_3_0 162
#define WTAP_ENCAP_LOGCAT 163
#define WTAP_ENCAP_LOGCAT_BRIEF 164
#define WTAP_ENCAP_LOGCAT_PROCESS 165
#define WTAP_ENCAP_LOGCAT_TAG 166
#define WTAP_ENCAP_LOGCAT_THREAD 167
#define WTAP_ENCAP_LOGCAT_TIME 168
#define WTAP_ENCAP_LOGCAT_THREADTIME 169
#define WTAP_ENCAP_LOGCAT_LONG 170
#define WTAP_ENCAP_PKTAP 171
#define WTAP_ENCAP_EPON 172
#define WTAP_ENCAP_IPMI_TRACE 173
#define WTAP_ENCAP_LOOP 174
#define WTAP_ENCAP_JSON 175
#define WTAP_ENCAP_NSTRACE_3_5 176
#define WTAP_ENCAP_ISO14443 177
#define WTAP_ENCAP_GFP_T 178
#define WTAP_ENCAP_GFP_F 179
#define WTAP_ENCAP_IP_OVER_IB_PCAP 180
#define WTAP_ENCAP_JUNIPER_VN 181
#define WTAP_ENCAP_USB_DARWIN 182
#define WTAP_ENCAP_LORATAP 183
#define WTAP_ENCAP_3MB_ETHERNET 184
#define WTAP_ENCAP_VSOCK 185
#define WTAP_ENCAP_NORDIC_BLE 186
#define WTAP_ENCAP_NETMON_NET_NETEVENT 187
#define WTAP_ENCAP_NETMON_HEADER 188
#define WTAP_ENCAP_NETMON_NET_FILTER 189
#define WTAP_ENCAP_NETMON_NETWORK_INFO_EX 190
#define WTAP_ENCAP_MA_WFP_CAPTURE_V4 191
#define WTAP_ENCAP_MA_WFP_CAPTURE_V6 192
#define WTAP_ENCAP_MA_WFP_CAPTURE_2V4 193
#define WTAP_ENCAP_MA_WFP_CAPTURE_2V6 194
#define WTAP_ENCAP_MA_WFP_CAPTURE_AUTH_V4 195
#define WTAP_ENCAP_MA_WFP_CAPTURE_AUTH_V6 196
#define WTAP_ENCAP_JUNIPER_ST 197
#define WTAP_ENCAP_ETHERNET_MPACKET 198
#define WTAP_ENCAP_DOCSIS31_XRA31 199
#define WTAP_ENCAP_DPAUXMON 200
#define WTAP_ENCAP_RUBY_MARSHAL 201
#define WTAP_ENCAP_RFC7468 202
#define WTAP_ENCAP_SYSTEMD_JOURNAL 203 /* Event, not a packet */
#define WTAP_ENCAP_EBHSCR 204
#define WTAP_ENCAP_VPP 205
#define WTAP_ENCAP_IEEE802_15_4_TAP 206
#define WTAP_ENCAP_LOG_3GPP 207
#define WTAP_ENCAP_USB_2_0 208
#define WTAP_ENCAP_MP4 209
#define WTAP_ENCAP_SLL2 210
#define WTAP_ENCAP_ZWAVE_SERIAL 211
#define WTAP_ENCAP_ETW 212
#define WTAP_ENCAP_ERI_ENB_LOG 213
#define WTAP_ENCAP_ZBNCP 214
#define WTAP_ENCAP_USB_2_0_LOW_SPEED 215
#define WTAP_ENCAP_USB_2_0_FULL_SPEED 216
#define WTAP_ENCAP_USB_2_0_HIGH_SPEED 217
#define WTAP_ENCAP_AUTOSAR_DLT 218
#define WTAP_ENCAP_AUERSWALD_LOG 219
#define WTAP_ENCAP_ATSC_ALP 220
#define WTAP_ENCAP_FIRA_UCI 221
#define WTAP_ENCAP_SILABS_DEBUG_CHANNEL 222
/* After adding new item here, please also add new item to encap_table_base array */
#define WTAP_NUM_ENCAP_TYPES wtap_get_num_encap_types()
/* Value to be used as a file type/subtype value if the type is unknown */
#define WTAP_FILE_TYPE_SUBTYPE_UNKNOWN -1
/* timestamp precision (currently only these values are supported) */
#define WTAP_TSPREC_UNKNOWN -2
#define WTAP_TSPREC_PER_PACKET -1 /* as a per-file value, means per-packet */
#define WTAP_TSPREC_SEC WS_TSPREC_SEC
#define WTAP_TSPREC_DSEC WS_TSPREC_100_MSEC
#define WTAP_TSPREC_CSEC WS_TSPREC_10_MSEC
#define WTAP_TSPREC_MSEC WS_TSPREC_MSEC
#define WTAP_TSPREC_USEC WS_TSPREC_USEC
#define WTAP_TSPREC_NSEC WS_TSPREC_NSEC
/* if you add to the above, update wtap_tsprec_string() */
/*
* Maximum packet sizes.
*
* For most link-layer types, we use 262144, which is currently
* libpcap's MAXIMUM_SNAPLEN.
*
* For WTAP_ENCAP_DBUS, the maximum is 128MiB, as per
*
* https://dbus.freedesktop.org/doc/dbus-specification.html#message-protocol-messages
*
* For WTAP_ENCAP_EBHSCR, the maximum is 8MiB, as per
*
* https://www.elektrobit.com/ebhscr
*
* For WTAP_ENCAP_USBPCAP, the maximum is 128MiB, as per
*
* https://gitlab.com/wireshark/wireshark/-/issues/15985
*
* We don't want to write out files that specify a maximum packet size
* greater than 262144 if we don't have to, as software reading those
* files might allocate a buffer much larger than necessary, wasting memory.
*/
#define WTAP_MAX_PACKET_SIZE_STANDARD 262144U
#define WTAP_MAX_PACKET_SIZE_USBPCAP (128U*1024U*1024U)
#define WTAP_MAX_PACKET_SIZE_EBHSCR (32U*1024U*1024U)
#define WTAP_MAX_PACKET_SIZE_DBUS (128U*1024U*1024U)
/*
* "Pseudo-headers" are used to supply to the clients of wiretap
* per-packet information that's not part of the packet payload
* proper.
*
* NOTE: do not use pseudo-header structures to hold information
* used by the code to read a particular capture file type; to
* keep that sort of state information, add a new structure for
* that private information to "wtap-int.h", add a pointer to that
* type of structure to the "capture" member of the "struct wtap"
* structure, and allocate one of those structures and set that member
* in the "open" routine for that capture file type if the open
* succeeds. See various other capture file type handlers for examples
* of that.
*/
/* Packet "pseudo-header" information for Ethernet capture files. */
struct eth_phdr {
gint fcs_len; /* Number of bytes of FCS - -1 means "unknown" */
};
/* Packet "pseudo-header" information for capture files for traffic
between DTE and DCE. */
#define FROM_DCE 0x80
struct dte_dce_phdr {
guint8 flags; /* ENCAP_LAPB, ENCAP_V120, ENCAP_FRELAY: 1st bit means From DCE */
};
/* Packet "pseudo-header" information for ISDN capture files. */
/* Direction */
struct isdn_phdr {
gboolean uton;
guint8 channel; /* 0 = D-channel; n = B-channel n */
};
/* Packet "pseudo-header" for ATM capture files.
Not all of this information is supplied by all capture types.
These originally came from the Network General (DOS-based)
ATM Sniffer file format, but we've added some additional
items. */
/*
* Status bits.
*/
#define ATM_RAW_CELL 0x01 /* TRUE if the packet is a single cell */
#define ATM_NO_HEC 0x02 /* TRUE if the cell has HEC stripped out */
#define ATM_AAL2_NOPHDR 0x04 /* TRUE if the AAL2 PDU has no pseudo-header */
#define ATM_REASSEMBLY_ERROR 0x08 /* TRUE if this is an incompletely-reassembled PDU */
/*
* AAL types.
*/
#define AAL_UNKNOWN 0 /* AAL unknown */
#define AAL_1 1 /* AAL1 */
#define AAL_2 2 /* AAL2 */
#define AAL_3_4 3 /* AAL3/4 */
#define AAL_5 4 /* AAL5 */
#define AAL_USER 5 /* User AAL */
#define AAL_SIGNALLING 6 /* Signaling AAL */
#define AAL_OAMCELL 7 /* OAM cell */
/*
* Traffic types.
*/
#define TRAF_UNKNOWN 0 /* Unknown */
#define TRAF_LLCMX 1 /* LLC multiplexed (RFC 1483) */
#define TRAF_VCMX 2 /* VC multiplexed (RFC 1483) */
#define TRAF_LANE 3 /* LAN Emulation */
#define TRAF_ILMI 4 /* ILMI */
#define TRAF_FR 5 /* Frame Relay */
#define TRAF_SPANS 6 /* FORE SPANS */
#define TRAF_IPSILON 7 /* Ipsilon */
#define TRAF_UMTS_FP 8 /* UMTS Frame Protocol */
#define TRAF_GPRS_NS 9 /* GPRS Network Services */
#define TRAF_SSCOP 10 /* SSCOP */
/*
* Traffic subtypes.
*/
#define TRAF_ST_UNKNOWN 0 /* Unknown */
/*
* For TRAF_VCMX:
*/
#define TRAF_ST_VCMX_802_3_FCS 1 /* 802.3 with an FCS */
#define TRAF_ST_VCMX_802_4_FCS 2 /* 802.4 with an FCS */
#define TRAF_ST_VCMX_802_5_FCS 3 /* 802.5 with an FCS */
#define TRAF_ST_VCMX_FDDI_FCS 4 /* FDDI with an FCS */
#define TRAF_ST_VCMX_802_6_FCS 5 /* 802.6 with an FCS */
#define TRAF_ST_VCMX_802_3 7 /* 802.3 without an FCS */
#define TRAF_ST_VCMX_802_4 8 /* 802.4 without an FCS */
#define TRAF_ST_VCMX_802_5 9 /* 802.5 without an FCS */
#define TRAF_ST_VCMX_FDDI 10 /* FDDI without an FCS */
#define TRAF_ST_VCMX_802_6 11 /* 802.6 without an FCS */
#define TRAF_ST_VCMX_FRAGMENTS 12 /* Fragments */
#define TRAF_ST_VCMX_BPDU 13 /* BPDU */
/*
* For TRAF_LANE:
*/
#define TRAF_ST_LANE_LE_CTRL 1 /* LANE: LE Ctrl */
#define TRAF_ST_LANE_802_3 2 /* LANE: 802.3 */
#define TRAF_ST_LANE_802_5 3 /* LANE: 802.5 */
#define TRAF_ST_LANE_802_3_MC 4 /* LANE: 802.3 multicast */
#define TRAF_ST_LANE_802_5_MC 5 /* LANE: 802.5 multicast */
/*
* For TRAF_IPSILON:
*/
#define TRAF_ST_IPSILON_FT0 1 /* Ipsilon: Flow Type 0 */
#define TRAF_ST_IPSILON_FT1 2 /* Ipsilon: Flow Type 1 */
#define TRAF_ST_IPSILON_FT2 3 /* Ipsilon: Flow Type 2 */
struct atm_phdr {
guint32 flags; /* status flags */
guint8 aal; /* AAL of the traffic */
guint8 type; /* traffic type */
guint8 subtype; /* traffic subtype */
guint16 vpi; /* virtual path identifier */
guint16 vci; /* virtual circuit identifier */
guint8 aal2_cid; /* channel id */
guint16 channel; /* link: 0 for DTE->DCE, 1 for DCE->DTE */
guint16 cells; /* number of cells */
guint16 aal5t_u2u; /* user-to-user indicator */
guint16 aal5t_len; /* length of the packet */
guint32 aal5t_chksum; /* checksum for AAL5 packet */
};
/* Packet "pseudo-header" for the output from "wandsession", "wannext",
"wandisplay", and similar commands on Lucent/Ascend access equipment. */
#define ASCEND_MAX_STR_LEN 64
#define ASCEND_PFX_WDS_X 1
#define ASCEND_PFX_WDS_R 2
#define ASCEND_PFX_WDD 3
#define ASCEND_PFX_ISDN_X 4
#define ASCEND_PFX_ISDN_R 5
#define ASCEND_PFX_ETHER 6
struct ascend_phdr {
guint16 type; /* ASCEND_PFX_*, as defined above */
char user[ASCEND_MAX_STR_LEN]; /* Username, from wandsession header */
guint32 sess; /* Session number, from wandsession header */
char call_num[ASCEND_MAX_STR_LEN]; /* Called number, from WDD header */
guint32 chunk; /* Chunk number, from WDD header */
guint32 task; /* Task number */
};
/* Packet "pseudo-header" for point-to-point links with direction flags. */
struct p2p_phdr {
gboolean sent;
};
/*
* Packet "pseudo-header" information for 802.11.
* Radio information is only present in this form for
* WTAP_ENCAP_IEEE_802_11_WITH_RADIO. This is used for file formats in
* which the radio information isn't provided as a pseudo-header in the
* packet data. It is also used by the dissectors for the pseudo-headers
* in the packet data to supply radio information, in a form independent
* of the file format and pseudo-header format, to the "802.11 radio"
* dissector.
*
* Signal strength, etc. information:
*
* Raw signal strength can be measured in milliwatts.
* It can also be represented as dBm, which is 10 times the log base 10
* of the signal strength in mW.
*
* The Receive Signal Strength Indicator is an integer in the range 0 to 255.
* The actual RSSI value for a given signal strength is dependent on the
* vendor (and perhaps on the adapter). The maximum possible RSSI value
* is also dependent on the vendor and perhaps the adapter.
*
* The signal strength can be represented as a percentage, which is 100
* times the ratio of the RSSI and the maximum RSSI.
*/
/*
* PHY types.
*/
#define PHDR_802_11_PHY_UNKNOWN 0 /* PHY not known */
#define PHDR_802_11_PHY_11_FHSS 1 /* 802.11 FHSS */
#define PHDR_802_11_PHY_11_IR 2 /* 802.11 IR */
#define PHDR_802_11_PHY_11_DSSS 3 /* 802.11 DSSS */
#define PHDR_802_11_PHY_11B 4 /* 802.11b */
#define PHDR_802_11_PHY_11A 5 /* 802.11a */
#define PHDR_802_11_PHY_11G 6 /* 802.11g */
#define PHDR_802_11_PHY_11N 7 /* 802.11n */
#define PHDR_802_11_PHY_11AC 8 /* 802.11ac */
#define PHDR_802_11_PHY_11AD 9 /* 802.11ad */
#define PHDR_802_11_PHY_11AH 10 /* 802.11ah */
#define PHDR_802_11_PHY_11AX 11 /* 802.11ax */
#define PHDR_802_11_PHY_11BE 12 /* 802.11be - EHT */
/*
* PHY-specific information.
*/
/*
* 802.11 legacy FHSS.
*/
struct ieee_802_11_fhss {
guint has_hop_set:1;
guint has_hop_pattern:1;
guint has_hop_index:1;
guint8 hop_set; /* Hop set */
guint8 hop_pattern; /* Hop pattern */
guint8 hop_index; /* Hop index */
};
/*
* 802.11b.
*/
struct ieee_802_11b {
/* Which of this information is present? */
guint has_short_preamble:1;
gboolean short_preamble; /* Short preamble */
};
/*
* 802.11a.
*/
struct ieee_802_11a {
/* Which of this information is present? */
guint has_channel_type:1;
guint has_turbo_type:1;
guint channel_type:2;
guint turbo_type:2;
};
/*
* Channel type values.
*/
#define PHDR_802_11A_CHANNEL_TYPE_NORMAL 0
#define PHDR_802_11A_CHANNEL_TYPE_HALF_CLOCKED 1
#define PHDR_802_11A_CHANNEL_TYPE_QUARTER_CLOCKED 2
/*
* "Turbo" is an Atheros proprietary extension with 40 MHz-wide channels.
* It can be dynamic or static.
*
* See
*
* http://wifi-insider.com/atheros/turbo.htm
*/
#define PHDR_802_11A_TURBO_TYPE_NORMAL 0
#define PHDR_802_11A_TURBO_TYPE_TURBO 1 /* If we don't know whether it's static or dynamic */
#define PHDR_802_11A_TURBO_TYPE_DYNAMIC_TURBO 2
#define PHDR_802_11A_TURBO_TYPE_STATIC_TURBO 3
/*
* 802.11g.
*
* This should only be used for packets sent using OFDM; packets
* sent on an 11g network using DSSS should have the PHY set to
* 11b.
*/
struct ieee_802_11g {
/* Which of this information is present? */
guint has_mode:1;
guint32 mode; /* Various proprietary extensions */
};
/*
* Mode values.
*/
#define PHDR_802_11G_MODE_NORMAL 0
#define PHDR_802_11G_MODE_SUPER_G 1 /* Atheros Super G */
/*
* 802.11n.
*/
struct ieee_802_11n {
/* Which of this information is present? */
guint has_mcs_index:1;
guint has_bandwidth:1;
guint has_short_gi:1;
guint has_greenfield:1;
guint has_fec:1;
guint has_stbc_streams:1;
guint has_ness:1;
guint16 mcs_index; /* MCS index */
guint bandwidth; /* Bandwidth = 20 MHz, 40 MHz, etc. */
guint short_gi:1; /* True for short guard interval */
guint greenfield:1; /* True for greenfield, short for mixed */
guint fec:1; /* FEC: 0 = BCC, 1 = LDPC */
guint stbc_streams:2; /* Number of STBC streams */
guint ness; /* Number of extension spatial streams */
};
/*
* Bandwidth values; used for both 11n and 11ac.
*/
#define PHDR_802_11_BANDWIDTH_20_MHZ 0 /* 20 MHz */
#define PHDR_802_11_BANDWIDTH_40_MHZ 1 /* 40 MHz */
#define PHDR_802_11_BANDWIDTH_20_20L 2 /* 20 + 20L, 40 MHz */
#define PHDR_802_11_BANDWIDTH_20_20U 3 /* 20 + 20U, 40 MHz */
#define PHDR_802_11_BANDWIDTH_80_MHZ 4 /* 80 MHz */
#define PHDR_802_11_BANDWIDTH_40_40L 5 /* 40 + 40L MHz, 80 MHz */
#define PHDR_802_11_BANDWIDTH_40_40U 6 /* 40 + 40U MHz, 80 MHz */
#define PHDR_802_11_BANDWIDTH_20LL 7 /* ???, 80 MHz */
#define PHDR_802_11_BANDWIDTH_20LU 8 /* ???, 80 MHz */
#define PHDR_802_11_BANDWIDTH_20UL 9 /* ???, 80 MHz */
#define PHDR_802_11_BANDWIDTH_20UU 10 /* ???, 80 MHz */
#define PHDR_802_11_BANDWIDTH_160_MHZ 11 /* 160 MHz */
#define PHDR_802_11_BANDWIDTH_80_80L 12 /* 80 + 80L, 160 MHz */
#define PHDR_802_11_BANDWIDTH_80_80U 13 /* 80 + 80U, 160 MHz */
#define PHDR_802_11_BANDWIDTH_40LL 14 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_40LU 15 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_40UL 16 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_40UU 17 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_20LLL 18 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_20LLU 19 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_20LUL 20 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_20LUU 21 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_20ULL 22 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_20ULU 23 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_20UUL 24 /* ???, 160 MHz */
#define PHDR_802_11_BANDWIDTH_20UUU 25 /* ???, 160 MHz */
/*
* 802.11ac.
*/
struct ieee_802_11ac {
/* Which of this information is present? */
guint has_stbc:1;
guint has_txop_ps_not_allowed:1;
guint has_short_gi:1;
guint has_short_gi_nsym_disambig:1;
guint has_ldpc_extra_ofdm_symbol:1;
guint has_beamformed:1;
guint has_bandwidth:1;
guint has_fec:1;
guint has_group_id:1;
guint has_partial_aid:1;
guint stbc:1; /* 1 if all spatial streams have STBC */
guint txop_ps_not_allowed:1;
guint short_gi:1; /* True for short guard interval */
guint short_gi_nsym_disambig:1;
guint ldpc_extra_ofdm_symbol:1;
guint beamformed:1;
guint8 bandwidth; /* Bandwidth = 20 MHz, 40 MHz, etc. */
guint8 mcs[4]; /* MCS index per user */
guint8 nss[4]; /* NSS per user */
guint8 fec; /* Bit array of FEC per user: 0 = BCC, 1 = LDPC */
guint8 group_id;
guint16 partial_aid;
};
/*
* 802.11ad.
*/
/*
* Min and Max frequencies for 802.11ad and a macro for checking for 802.11ad.
*/
#define PHDR_802_11AD_MIN_FREQUENCY 57000
#define PHDR_802_11AD_MAX_FREQUENCY 71000
#define IS_80211AD(frequency) (((frequency) >= PHDR_802_11AD_MIN_FREQUENCY) &&\
((frequency) <= PHDR_802_11AD_MAX_FREQUENCY))
struct ieee_802_11ad {
/* Which of this information is present? */
guint has_mcs_index:1;
guint8 mcs; /* MCS index */
};
/*
* 802.11ax (HE).
*/
struct ieee_802_11ax {
/* Which of this information is present? */
guint has_mcs_index:1;
guint has_bwru:1;
guint has_gi:1;
guint8 nsts:4; /* Number of Space-time Streams */
guint8 mcs:4; /* MCS index */
guint8 bwru:4; /* Bandwidth/RU allocation */
guint8 gi:2; /* Guard Interval */
};
union ieee_802_11_phy_info {
struct ieee_802_11_fhss info_11_fhss;
struct ieee_802_11b info_11b;
struct ieee_802_11a info_11a;
struct ieee_802_11g info_11g;
struct ieee_802_11n info_11n;
struct ieee_802_11ac info_11ac;
struct ieee_802_11ad info_11ad;
struct ieee_802_11ax info_11ax;
};
struct ieee_802_11_phdr {
gint fcs_len; /* Number of bytes of FCS - -1 means "unknown" */
guint decrypted:1; /* TRUE if frame is decrypted even if "protected" bit is set */
guint datapad:1; /* TRUE if frame has padding between 802.11 header and payload */
guint no_a_msdus:1; /* TRUE if we should ignore the A-MSDU bit */
guint phy; /* PHY type */
union ieee_802_11_phy_info phy_info;
/* Which of this information is present? */
guint has_channel:1;
guint has_frequency:1;
guint has_data_rate:1;
guint has_signal_percent:1;
guint has_noise_percent:1;
guint has_signal_dbm:1;
guint has_noise_dbm:1;
guint has_signal_db:1;
guint has_noise_db:1;
guint has_tsf_timestamp:1;
guint has_aggregate_info:1; /* aggregate flags and ID */
guint has_zero_length_psdu_type:1; /* zero-length PSDU type */
guint16 channel; /* Channel number */
guint32 frequency; /* Channel center frequency */
guint16 data_rate; /* Data rate, in .5 Mb/s units */
guint8 signal_percent; /* Signal level, as a percentage */
guint8 noise_percent; /* Noise level, as a percentage */
gint8 signal_dbm; /* Signal level, in dBm */
gint8 noise_dbm; /* Noise level, in dBm */
guint8 signal_db; /* Signal level, in dB from an arbitrary point */
guint8 noise_db; /* Noise level, in dB from an arbitrary point */
guint64 tsf_timestamp;
guint32 aggregate_flags; /* A-MPDU flags */
guint32 aggregate_id; /* ID for A-MPDU reassembly */
guint8 zero_length_psdu_type; /* type of zero-length PSDU */
};
/*
* A-MPDU flags.
*/
#define PHDR_802_11_LAST_PART_OF_A_MPDU 0x00000001 /* this is the last part of an A-MPDU */
#define PHDR_802_11_A_MPDU_DELIM_CRC_ERROR 0x00000002 /* delimiter CRC error after this part */
/*
* Zero-length PSDU types.
*/
#define PHDR_802_11_SOUNDING_PSDU 0 /* sounding PPDU */
#define PHDR_802_11_DATA_NOT_CAPTURED 1 /* data not captured, (e.g. multi-user PPDU) */
#define PHDR_802_11_0_LENGTH_PSDU_S1G_NDP 2 /* S1G NDP CMAC */
#define PHDR_802_11_0_LENGTH_PSDU_VENDOR_SPECIFIC 0xff
/* Packet "pseudo-header" for the output from CoSine L2 debug output. */
#define COSINE_MAX_IF_NAME_LEN 128
#define COSINE_ENCAP_TEST 1
#define COSINE_ENCAP_PPoATM 2
#define COSINE_ENCAP_PPoFR 3
#define COSINE_ENCAP_ATM 4
#define COSINE_ENCAP_FR 5
#define COSINE_ENCAP_HDLC 6
#define COSINE_ENCAP_PPP 7
#define COSINE_ENCAP_ETH 8
#define COSINE_ENCAP_UNKNOWN 99
#define COSINE_DIR_TX 1
#define COSINE_DIR_RX 2
struct cosine_phdr {
guint8 encap; /* COSINE_ENCAP_* as defined above */
guint8 direction; /* COSINE_DIR_*, as defined above */
char if_name[COSINE_MAX_IF_NAME_LEN]; /* Encap & Logical I/F name */
guint16 pro; /* Protocol */
guint16 off; /* Offset */
guint16 pri; /* Priority */
guint16 rm; /* Rate Marking */
guint16 err; /* Error Code */
};
/* Packet "pseudo-header" for IrDA capture files. */
/*
* Direction of the packet
*/
#define IRDA_INCOMING 0x0000
#define IRDA_OUTGOING 0x0004
/*
* "Inline" log messages produced by IrCOMM2k on Windows
*/
#define IRDA_LOG_MESSAGE 0x0100 /* log message */
#define IRDA_MISSED_MSG 0x0101 /* missed log entry or frame */
/*
* Differentiate between frames and log messages
*/
#define IRDA_CLASS_FRAME 0x0000
#define IRDA_CLASS_LOG 0x0100
#define IRDA_CLASS_MASK 0xFF00
struct irda_phdr {
guint16 pkttype; /* packet type */
};
/* Packet "pseudo-header" for nettl (HP-UX) capture files. */
struct nettl_phdr {
guint16 subsys;
guint32 devid;
guint32 kind;
gint32 pid;
guint32 uid;
};
/* Packet "pseudo-header" for MTP2 files. */
#define MTP2_ANNEX_A_NOT_USED 0
#define MTP2_ANNEX_A_USED 1
#define MTP2_ANNEX_A_USED_UNKNOWN 2
struct mtp2_phdr {
guint8 sent;
guint8 annex_a_used;
guint16 link_number;
};
/* Packet "pseudo-header" for K12 files. */
typedef union {
struct {
guint16 vp;
guint16 vc;
guint16 cid;
} atm;
guint32 ds0mask;
} k12_input_info_t;
struct k12_phdr {
guint32 input;
const gchar *input_name;
const gchar *stack_file;
guint32 input_type;
k12_input_info_t input_info;
guint8 *extra_info;
guint32 extra_length;
void* stuff;
};
#define K12_PORT_DS0S 0x00010008
#define K12_PORT_DS1 0x00100008
#define K12_PORT_ATMPVC 0x01020000
struct lapd_phdr {
guint16 pkttype; /* packet type */
guint8 we_network;
};
struct wtap;
struct catapult_dct2000_phdr
{
union
{
struct isdn_phdr isdn;
struct atm_phdr atm;
struct p2p_phdr p2p;
} inner_pseudo_header;
gint64 seek_off;
struct wtap *wth;
};
/*
* Endace Record Format pseudo header
*/
struct erf_phdr {
guint64 ts; /* Time stamp */
guint8 type;
guint8 flags;
guint16 rlen;
guint16 lctr;
guint16 wlen;
};
struct erf_ehdr {
guint64 ehdr;
};
/*
* ERF pseudo header with optional subheader
* (Multichannel or Ethernet)
*/
#define MAX_ERF_EHDR 16
struct wtap_erf_eth_hdr {
guint8 offset;
guint8 pad;
};
struct erf_mc_phdr {
struct erf_phdr phdr;
struct erf_ehdr ehdr_list[MAX_ERF_EHDR];
union
{
struct wtap_erf_eth_hdr eth_hdr;
guint32 mc_hdr;
guint32 aal2_hdr;
} subhdr;
};
#define SITA_FRAME_DIR_TXED (0x00) /* values of sita_phdr.flags */
#define SITA_FRAME_DIR_RXED (0x01)
#define SITA_FRAME_DIR (0x01) /* mask */
#define SITA_ERROR_NO_BUFFER (0x80)
#define SITA_SIG_DSR (0x01) /* values of sita_phdr.signals */
#define SITA_SIG_DTR (0x02)
#define SITA_SIG_CTS (0x04)
#define SITA_SIG_RTS (0x08)
#define SITA_SIG_DCD (0x10)
#define SITA_SIG_UNDEF1 (0x20)
#define SITA_SIG_UNDEF2 (0x40)
#define SITA_SIG_UNDEF3 (0x80)
#define SITA_ERROR_TX_UNDERRUN (0x01) /* values of sita_phdr.errors2 (if SITA_FRAME_DIR_TXED) */
#define SITA_ERROR_TX_CTS_LOST (0x02)
#define SITA_ERROR_TX_UART_ERROR (0x04)
#define SITA_ERROR_TX_RETX_LIMIT (0x08)
#define SITA_ERROR_TX_UNDEF1 (0x10)
#define SITA_ERROR_TX_UNDEF2 (0x20)
#define SITA_ERROR_TX_UNDEF3 (0x40)
#define SITA_ERROR_TX_UNDEF4 (0x80)
#define SITA_ERROR_RX_FRAMING (0x01) /* values of sita_phdr.errors1 (if SITA_FRAME_DIR_RXED) */
#define SITA_ERROR_RX_PARITY (0x02)
#define SITA_ERROR_RX_COLLISION (0x04)
#define SITA_ERROR_RX_FRAME_LONG (0x08)
#define SITA_ERROR_RX_FRAME_SHORT (0x10)
#define SITA_ERROR_RX_UNDEF1 (0x20)
#define SITA_ERROR_RX_UNDEF2 (0x40)
#define SITA_ERROR_RX_UNDEF3 (0x80)
#define SITA_ERROR_RX_NONOCTET_ALIGNED (0x01) /* values of sita_phdr.errors2 (if SITA_FRAME_DIR_RXED) */
#define SITA_ERROR_RX_ABORT (0x02)
#define SITA_ERROR_RX_CD_LOST (0x04)
#define SITA_ERROR_RX_DPLL (0x08)
#define SITA_ERROR_RX_OVERRUN (0x10)
#define SITA_ERROR_RX_FRAME_LEN_VIOL (0x20)
#define SITA_ERROR_RX_CRC (0x40)
#define SITA_ERROR_RX_BREAK (0x80)
#define SITA_PROTO_UNUSED (0x00) /* values of sita_phdr.proto */
#define SITA_PROTO_BOP_LAPB (0x01)
#define SITA_PROTO_ETHERNET (0x02)
#define SITA_PROTO_ASYNC_INTIO (0x03)
#define SITA_PROTO_ASYNC_BLKIO (0x04)
#define SITA_PROTO_ALC (0x05)
#define SITA_PROTO_UTS (0x06)
#define SITA_PROTO_PPP_HDLC (0x07)
#define SITA_PROTO_SDLC (0x08)
#define SITA_PROTO_TOKENRING (0x09)
#define SITA_PROTO_I2C (0x10)
#define SITA_PROTO_DPM_LINK (0x11)
#define SITA_PROTO_BOP_FRL (0x12)
struct sita_phdr {
guint8 sita_flags;
guint8 sita_signals;
guint8 sita_errors1;
guint8 sita_errors2;
guint8 sita_proto;
};
/*pseudo header for Bluetooth HCI*/
struct bthci_phdr {
gboolean sent;
guint32 channel;
};
#define BTHCI_CHANNEL_COMMAND 1
#define BTHCI_CHANNEL_ACL 2
#define BTHCI_CHANNEL_SCO 3
#define BTHCI_CHANNEL_EVENT 4
#define BTHCI_CHANNEL_ISO 5
/* pseudo header for WTAP_ENCAP_BLUETOOTH_LINUX_MONITOR */
struct btmon_phdr {
guint16 adapter_id;
guint16 opcode;
};
/* pseudo header for WTAP_ENCAP_LAYER1_EVENT */
struct l1event_phdr {
gboolean uton;
};
/* * I2C pseudo header */
struct i2c_phdr {
guint8 is_event;
guint8 bus;
guint32 flags;
};
/* pseudo header for WTAP_ENCAP_GSM_UM */
struct gsm_um_phdr {
gboolean uplink;
guint8 channel;
/* The following are only populated for downlink */
guint8 bsic;
guint16 arfcn;
guint32 tdma_frame;
guint8 error;
guint16 timeshift;
};
#define GSM_UM_CHANNEL_UNKNOWN 0
#define GSM_UM_CHANNEL_BCCH 1
#define GSM_UM_CHANNEL_SDCCH 2
#define GSM_UM_CHANNEL_SACCH 3
#define GSM_UM_CHANNEL_FACCH 4
#define GSM_UM_CHANNEL_CCCH 5
#define GSM_UM_CHANNEL_RACH 6
#define GSM_UM_CHANNEL_AGCH 7
#define GSM_UM_CHANNEL_PCH 8
/* Pseudo-header for nstrace packets */
struct nstr_phdr {
gint64 rec_offset;
gint32 rec_len;
guint8 nicno_offset;
guint8 nicno_len;
guint8 dir_offset;
guint8 dir_len;
guint16 eth_offset;
guint8 pcb_offset;
guint8 l_pcb_offset;
guint8 rec_type;
guint8 vlantag_offset;
guint8 coreid_offset;
guint8 srcnodeid_offset;
guint8 destnodeid_offset;
guint8 clflags_offset;
guint8 src_vmname_len_offset;
guint8 dst_vmname_len_offset;
guint8 ns_activity_offset;
guint8 data_offset;
};
/* Packet "pseudo-header" for Nokia output */
struct nokia_phdr {
struct eth_phdr eth;
guint8 stuff[4]; /* mysterious stuff */
};
#define LLCP_PHDR_FLAG_SENT 0
struct llcp_phdr {
guint8 adapter;
guint8 flags;
};
/* pseudo header for WTAP_ENCAP_LOGCAT */
struct logcat_phdr {
gint version;
};
/* Packet "pseudo-header" information for header data from NetMon files. */
struct netmon_phdr {
guint8* title; /* Comment title, as a null-terminated UTF-8 string */
guint32 descLength; /* Number of bytes in the comment description */
guint8* description; /* Comment description, in ASCII RTF */
guint sub_encap; /* "Real" encap value for the record that will be used once pseudo header data is display */
union sub_wtap_pseudo_header {
struct eth_phdr eth;
struct atm_phdr atm;
struct ieee_802_11_phdr ieee_802_11;
} subheader;
};
/* File "pseudo-header" for BER data files. */
struct ber_phdr {
const char *pathname; /* Path name of file. */
};
union wtap_pseudo_header {
struct eth_phdr eth;
struct dte_dce_phdr dte_dce;
struct isdn_phdr isdn;
struct atm_phdr atm;
struct ascend_phdr ascend;
struct p2p_phdr p2p;
struct ieee_802_11_phdr ieee_802_11;
struct cosine_phdr cosine;
struct irda_phdr irda;
struct nettl_phdr nettl;
struct mtp2_phdr mtp2;
struct k12_phdr k12;
struct lapd_phdr lapd;
struct catapult_dct2000_phdr dct2000;
struct erf_mc_phdr erf;
struct sita_phdr sita;
struct bthci_phdr bthci;
struct btmon_phdr btmon;
struct l1event_phdr l1event;
struct i2c_phdr i2c;
struct gsm_um_phdr gsm_um;
struct nstr_phdr nstr;
struct nokia_phdr nokia;
struct llcp_phdr llcp;
struct logcat_phdr logcat;
struct netmon_phdr netmon;
struct ber_phdr ber;
};
/*
* Record type values.
*
* This list will expand over time, so don't assume everything will
* forever be one of the types listed below.
*
* For file-type-specific records, the "ftsrec" field of the pseudo-header
* contains a file-type-specific subtype value, such as a block type for
* a pcapng file.
*
* An "event" is an indication that something happened during the capture
* process, such as a status transition of some sort on the network.
* These should, ideally, have a time stamp and, if they're relevant to
* a particular interface on a multi-interface capture, should also have
* an interface ID. The data for the event is file-type-specific and
* subtype-specific. These should be dissected and displayed just as
* packets are.
*
* A "report" supplies information not corresponding to an event;
* for example, a pcapng Interface Statistics Block would be a report,
* as it doesn't correspond to something happening on the network.
* They may have a time stamp, and should be dissected and displayed
* just as packets are.
*
* We distinguish between "events" and "reports" so that, for example,
* the packet display can show the delta between a packet and an event
* but not show the delta between a packet and a report, as the time
* stamp of a report may not correspond to anything interesting on
* the network but the time stamp of an event would.
*
* XXX - are there any file-type-specific records that *shouldn't* be
* dissected and displayed? If so, they should be parsed and the
* information in them stored somewhere, and used somewhere, whether
* it's just used when saving the file in its native format or also
* used to parse *other* file-type-specific records.
*
* These would be similar to, for example, pcapng Interface Description
* Blocks, for which the position within the file is significant only
* in that an IDB for an interface must appear before any packets from
* the interface; the fact that an IDB appears at some point doesn't
* necessarily mean something happened in the capture at that point.
* Name Resolution Blocks are another example of such a record.
*
* (XXX - if you want to have a record that says "this interface first
* showed up at this time", that needs to be a separate record type
* from the IDB. We *could* add a "New Interface Description Block",
* with a time stamp, for that purpose, but we'd *still* have to
* provide IDBs for those interfaces, for compatibility with programs
* that don't know about the NIDB. An ISB with only an isb_starttime
* option would suffice for this purpose, so nothing needs to be
* added to pcapng for this.)
*/
#define REC_TYPE_PACKET 0 /**< packet */
#define REC_TYPE_FT_SPECIFIC_EVENT 1 /**< file-type-specific event */
#define REC_TYPE_FT_SPECIFIC_REPORT 2 /**< file-type-specific report */
#define REC_TYPE_SYSCALL 3 /**< system call */
#define REC_TYPE_SYSTEMD_JOURNAL_EXPORT 4 /**< systemd journal entry */
#define REC_TYPE_CUSTOM_BLOCK 5 /**< pcapng custom block */
typedef struct {
guint32 caplen; /* data length in the file */
guint32 len; /* data length on the wire */
int pkt_encap; /* WTAP_ENCAP_ value for this packet */
/* pcapng variables */
guint32 interface_id; /* identifier of the interface. */
/* options */
union wtap_pseudo_header pseudo_header;
} wtap_packet_header;
/*
* The pcapng specification says "The word is encoded as an unsigned
* 32-bit integer, using the endianness of the Section Header Block
* scope it is in. In the following table, the bits are numbered with
* 0 being the most-significant bit and 31 being the least-significant
* bit of the 32-bit unsigned integer."
*
* From that, the direction, in bits 0 and 1, is at the *top* of the word.
*
* However, several implementations, such as:
*
* the Wireshark pcapng file reading code;
*
* macOS libpcap and tcpdump;
*
* text2pcap;
*
* and probably the software that generated the capture in bug 11665;
*
* treat 0 as the *least*-significant bit and bit 31 being the *most*-
* significant bit of the flags word, and put the direction at the
* *bottom* of the word.
*
* For now, we go with the known implementations.
*/
/* Direction field of the packet flags */
#define PACK_FLAGS_DIRECTION_MASK 0x00000003 /* unshifted */
#define PACK_FLAGS_DIRECTION_SHIFT 0
#define PACK_FLAGS_DIRECTION(pack_flags) (((pack_flags) & PACK_FLAGS_DIRECTION_MASK) >> PACK_FLAGS_DIRECTION_SHIFT)
#define PACK_FLAGS_DIRECTION_UNKNOWN 0
#define PACK_FLAGS_DIRECTION_INBOUND 1
#define PACK_FLAGS_DIRECTION_OUTBOUND 2
/* Reception type field of the packet flags */
#define PACK_FLAGS_RECEPTION_TYPE_MASK 0x0000001C /* unshifted */
#define PACK_FLAGS_RECEPTION_TYPE_SHIFT 2
#define PACK_FLAGS_RECEPTION_TYPE(pack_flags) (((pack_flags) & PACK_FLAGS_RECEPTION_TYPE_MASK) >> PACK_FLAGS_RECEPTION_TYPE_SHIFT)
#define PACK_FLAGS_RECEPTION_TYPE_UNSPECIFIED 0
#define PACK_FLAGS_RECEPTION_TYPE_UNICAST 1
#define PACK_FLAGS_RECEPTION_TYPE_MULTICAST 2
#define PACK_FLAGS_RECEPTION_TYPE_BROADCAST 3
#define PACK_FLAGS_RECEPTION_TYPE_PROMISCUOUS 4
/* FCS length field of the packet flags */
#define PACK_FLAGS_FCS_LENGTH_MASK 0x000001E0 /* unshifted */
#define PACK_FLAGS_FCS_LENGTH_SHIFT 5
#define PACK_FLAGS_FCS_LENGTH(pack_flags) (((pack_flags) & PACK_FLAGS_FCS_LENGTH_MASK) >> PACK_FLAGS_FCS_LENGTH_SHIFT)
/* Reserved bits of the packet flags */
#define PACK_FLAGS_RESERVED_MASK 0x0000FE00
/* Link-layer-dependent errors of the packet flags */
/* For Ethernet and possibly some other network types */
#define PACK_FLAGS_CRC_ERROR 0x01000000
#define PACK_FLAGS_PACKET_TOO_LONG 0x02000000
#define PACK_FLAGS_PACKET_TOO_SHORT 0x04000000
#define PACK_FLAGS_WRONG_INTER_FRAME_GAP 0x08000000
#define PACK_FLAGS_UNALIGNED_FRAME 0x10000000
#define PACK_FLAGS_START_FRAME_DELIMITER_ERROR 0x20000000
#define PACK_FLAGS_PREAMBLE_ERROR 0x40000000
#define PACK_FLAGS_SYMBOL_ERROR 0x80000000
/* Construct a pack_flags value from its subfield values */
#define PACK_FLAGS_VALUE(direction, reception_type, fcs_length, ll_dependent_errors) \
(((direction) << 30) | \
((reception_type) << 27) | \
((fcs_length) << 23) | \
(ll_dependent_errors))
typedef struct {
guint record_type; /* the type of record this is - file type-specific value */
guint32 record_len; /* length of the record */
} wtap_ft_specific_header;
typedef struct {
guint record_type; /* XXX match ft_specific_record_phdr so that we chain off of packet-pcapng_block for now. */
int byte_order;
/* guint32 sentinel; */
guint64 timestamp; /* ns since epoch - XXX dup of ts */
guint64 thread_id;
guint32 event_len; /* length of the event */
guint32 event_filelen; /* event data length in the file */
guint16 event_type;
guint32 nparams; /* number of parameters of the event */
guint16 cpu_id;
/* ... Event ... */
} wtap_syscall_header;
typedef struct {
guint32 record_len; /* length of the record */
} wtap_systemd_journal_export_header;
typedef struct {
guint32 length; /* length of the record */
guint32 pen; /* private enterprise number */
gboolean copy_allowed; /* CB can be written */
union {
struct nflx {
guint32 type; /* block type */
guint32 skipped; /* Used if type == BBLOG_TYPE_SKIPPED_BLOCK */
} nflx_custom_data_header;
} custom_data_header;
} wtap_custom_block_header;
#define BBLOG_TYPE_EVENT_BLOCK 1
#define BBLOG_TYPE_SKIPPED_BLOCK 2
/*
* The largest nstime.secs value that can be put into an unsigned
* 32-bit quantity.
*
* We assume that time_t is signed; it is signed on Windows/MSVC and
* on many UN*Xes.
*
* So, if time_t is 32-bit, we define this as G_MAXINT32, as that's
* the largest value a time_t can have, and it fits in an unsigned
* 32-bit quantity. If it's 64-bit or larger, we define this as
* G_MAXUINT32, as, even if it's signed, it can be as large as
* G_MAXUINT32, and that's the largest value that can fit in
* a 32-bit unsigned quantity.
*
* Comparing against this, rather than against G_MAXINT2, when checking
* whether a time stamp will fit in a 32-bit unsigned integer seconds
* field in a capture file being written avoids signed vs. unsigned
* warnings if time_t is a signed 32-bit type.
*
* XXX - what if time_t is unsigned? Are there any platforms where
* it is?
*/
#define WTAP_NSTIME_32BIT_SECS_MAX ((time_t)(sizeof(time_t) > sizeof(gint32) ? G_MAXUINT32 : G_MAXINT32))
typedef struct wtap_rec {
guint rec_type; /* what type of record is this? */
guint32 presence_flags; /* what stuff do we have? */
guint section_number; /* section, within file, containing this record */
nstime_t ts; /* time stamp */
int tsprec; /* WTAP_TSPREC_ value for this record */
nstime_t ts_rel_cap; /* time stamp relative from capture start */
gboolean ts_rel_cap_valid; /* is ts_rel_cap valid and can be used? */
union {
wtap_packet_header packet_header;
wtap_ft_specific_header ft_specific_header;
wtap_syscall_header syscall_header;
wtap_systemd_journal_export_header systemd_journal_export_header;
wtap_custom_block_header custom_block_header;
} rec_header;
wtap_block_t block ; /* packet block; holds comments and verdicts in its options */
gboolean block_was_modified; /* TRUE if ANY aspect of the block has been modified */
/*
* We use a Buffer so that we don't have to allocate and free
* a buffer for the options for each record.
*/
Buffer options_buf; /* file-type specific data */
} wtap_rec;
/*
* Bits in presence_flags, indicating which of the fields we have.
*
* For the time stamp, we may need some more flags to indicate
* whether the time stamp is an absolute date-and-time stamp, an
* absolute time-only stamp (which can make relative time
* calculations tricky, as you could in theory have two time
* stamps separated by an unknown number of days), or a time stamp
* relative to some unspecified time in the past (see mpeg.c).
*
* There is no presence flag for len - there has to be *some* length
* value for the packet. (The "captured length" can be missing if
* the file format doesn't report a captured length distinct from
* the on-the-network length because the application(s) producing those
* files don't support slicing packets.)
*
* There could be a presence flag for the packet encapsulation - if it's
* absent, use the file encapsulation - but it's not clear that's useful;
* we currently do that in the module for the file format.
*
* Only WTAP_HAS_TS and WTAP_HAS_SECTION_NUMBER apply to all record types.
*/
#define WTAP_HAS_TS 0x00000001 /**< time stamp */
#define WTAP_HAS_CAP_LEN 0x00000002 /**< captured length separate from on-the-network length */
#define WTAP_HAS_INTERFACE_ID 0x00000004 /**< interface ID */
#define WTAP_HAS_SECTION_NUMBER 0x00000008 /**< section number */
#ifndef MAXNAMELEN
#define MAXNAMELEN 64 /* max name length (hostname and port name) */
#endif
typedef struct hashipv4 {
guint addr;
guint8 flags; /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */
gchar ip[WS_INET_ADDRSTRLEN];
gchar name[MAXNAMELEN];
} hashipv4_t;
typedef struct hashipv6 {
guint8 addr[16];
guint8 flags; /* B0 dummy_entry, B1 resolve, B2 If the address is used in the trace */
gchar ip6[WS_INET6_ADDRSTRLEN];
gchar name[MAXNAMELEN];
} hashipv6_t;
/** A struct with lists of resolved addresses.
* Used when writing name resolutions blocks (NRB)
*/
typedef struct addrinfo_lists {
GList *ipv4_addr_list; /**< A list of resolved hashipv4_t*/
GList *ipv6_addr_list; /**< A list of resolved hashipv6_t*/
} addrinfo_lists_t;
/**
* Parameters for various wtap_dump_* functions, specifying per-file
* information. The structure itself is no longer used after returning
* from wtap_dump_*, but its pointer fields must remain valid until
* wtap_dump_close is called.
*
* @note The shb_hdr and idb_inf arguments will be used until
* wtap_dump_close() is called, but will not be free'd by the dumper. If
* you created them, you must free them yourself after wtap_dump_close().
* dsbs_initial will be freed by wtap_dump_close(),
* dsbs_growing typically refers to another wth->dsbs.
* nrbs_growing typically refers to another wth->nrbs.
*
* @see wtap_dump_params_init, wtap_dump_params_cleanup.
*/
typedef struct wtap_dump_params {
int encap; /**< Per-file packet encapsulation, or WTAP_ENCAP_PER_PACKET */
int snaplen; /**< Per-file snapshot length (what if it's per-interface?) */
int tsprec; /**< Per-file time stamp precision */
GArray *shb_hdrs; /**< The section header block(s) information, or NULL. */
wtapng_iface_descriptions_t *idb_inf; /**< The interface description information, or NULL. */
const GArray *nrbs_growing; /**< NRBs that will be written while writing packets, or NULL.
This array may grow since the dumper was opened and will subsequently
be written before newer packets are written in wtap_dump. */
GArray *dsbs_initial; /**< The initial Decryption Secrets Block(s) to be written, or NULL. */
const GArray *dsbs_growing; /**< DSBs that will be written while writing packets, or NULL.
This array may grow since the dumper was opened and will subsequently
be written before newer packets are written in wtap_dump. */
gboolean dont_copy_idbs; /**< XXX - don't copy IDBs; this should eventually always be the case. */
} wtap_dump_params;
/* Zero-initializer for wtap_dump_params. */
#define WTAP_DUMP_PARAMS_INIT {.snaplen=0}
struct wtap_dumper;
typedef struct wtap wtap;
typedef struct wtap_dumper wtap_dumper;
typedef struct wtap_reader *FILE_T;
/* Similar to the wtap_open_routine_info for open routines, the following
* wtap_wslua_file_info struct is used by wslua code for Lua-based file writers.
*
* This concept is necessary because when wslua goes to invoke the
* registered dump/write_open routine callback in Lua, it needs the ref number representing
* the hooked function inside Lua. This will be stored in the thing pointed to
* by the void* data here. This 'data' pointer will be copied into the
* wtap_dumper struct's 'void* data' member when calling the dump_open function,
* which is how wslua finally retrieves it. Unlike wtap_dumper's 'priv' member, its
* 'data' member is not free'd in wtap_dump_close().
*/
typedef struct wtap_wslua_file_info {
int (*wslua_can_write_encap)(int, void*); /* a can_write_encap func for wslua uses */
void* wslua_data; /* holds the wslua data */
} wtap_wslua_file_info_t;
/*
* For registering extensions used for file formats.
*
* These items are used in dialogs for opening files, so that
* the user can ask to see all capture files (as identified
* by file extension) or particular types of capture files.
*
* Each file type has a description, a flag indicating whether it's
* a capture file or just some file whose contents we can dissect,
* and a list of extensions the file might have.
*
* Some file types aren't real file types, they're just generic types,
* such as "text file" or "XML file", that can be used for, among other
* things, captures we can read, or for extensions such as ".cap" that
* were unimaginatively chosen by several different sniffers for their
* file formats.
*/
struct file_extension_info {
/* the file type name */
const char *name;
/* TRUE if this is a capture file type */
gboolean is_capture_file;
/* a semicolon-separated list of file extensions used for this type */
const char *extensions;
};
/*
* For registering file types that we can open.
*
* Each file type has an open routine and an optional list of extensions
* the file might have.
*
* The open routine should return:
*
* WTAP_OPEN_ERROR on an I/O error;
*
* WTAP_OPEN_MINE if the file it's reading is one of the types
* it handles;
*
* WTAP_OPEN_NOT_MINE if the file it's reading isn't one of the
* types it handles.
*
* If the routine handles this type of file, it should set the "file_type"
* field in the "struct wtap" to the type of the file.
*
* Note that the routine does not have to free the private data pointer on
* error. The caller takes care of that by calling wtap_close on error.
* (See https://gitlab.com/wireshark/wireshark/-/issues/8518)
*
* However, the caller does have to free the private data pointer when
* returning WTAP_OPEN_NOT_MINE, since the next file type will be called
* and will likely just overwrite the pointer.
*/
typedef enum {
WTAP_OPEN_NOT_MINE = 0,
WTAP_OPEN_MINE = 1,
WTAP_OPEN_ERROR = -1
} wtap_open_return_val;
typedef wtap_open_return_val (*wtap_open_routine_t)(struct wtap*, int *,
char **);
/*
* Some file formats have defined magic numbers at fixed offsets from
* the beginning of the file; those routines should return 1 if and
* only if the file has the magic number at that offset. (pcapng
* is a bit of a special case, as it has both the Section Header Block
* type field and its byte-order magic field; it checks for both.)
* Those file formats do not require a file name extension in order
* to recognize them or to avoid recognizing other file types as that
* type, and have no extensions specified for them.
*
* Other file formats don't have defined magic numbers at fixed offsets,
* so a heuristic is required. If that file format has any file name
* extensions used for it, a list of those extensions should be
* specified, so that, if the name of the file being opened has an
* extension, the file formats that use that extension are tried before
* the ones that don't, to handle the case where a file of one type
* might be recognized by the heuristics for a different file type.
*/
typedef enum {
OPEN_INFO_MAGIC = 0,
OPEN_INFO_HEURISTIC = 1
} wtap_open_type;
WS_DLL_PUBLIC void init_open_routines(void);
void cleanup_open_routines(void);
struct open_info {
const char *name;
wtap_open_type type;
wtap_open_routine_t open_routine;
const char *extensions;
gchar **extensions_set; /* populated using extensions member during initialization */
void* wslua_data; /* should be NULL for C-code file readers */
};
WS_DLL_PUBLIC struct open_info *open_routines;
/*
* Types of comments.
*/
#define WTAP_COMMENT_PER_SECTION 0x00000001 /* per-file/per-file-section */
#define WTAP_COMMENT_PER_INTERFACE 0x00000002 /* per-interface */
#define WTAP_COMMENT_PER_PACKET 0x00000004 /* per-packet */
/*
* For a given option type in a certain block type, does a file format
* not support it, support only one such option, or support multiple
* such options?
*/
typedef enum {
OPTION_NOT_SUPPORTED,
ONE_OPTION_SUPPORTED,
MULTIPLE_OPTIONS_SUPPORTED
} option_support_t;
/*
* Entry in a table of supported option types.
*/
struct supported_option_type {
guint opt;
option_support_t support; /* OPTION_NOT_SUPPORTED allowed, equivalent to absence */
};
#define OPTION_TYPES_SUPPORTED(option_type_array) \
sizeof option_type_array / sizeof option_type_array[0], option_type_array
#define NO_OPTIONS_SUPPORTED \
0, NULL
/*
* For a given block type, does a file format not support it, support
* only one such block, or support multiple such blocks?
*/
typedef enum {
BLOCK_NOT_SUPPORTED,
ONE_BLOCK_SUPPORTED,
MULTIPLE_BLOCKS_SUPPORTED
} block_support_t;
/*
* Entry in a table of supported block types.
*/
struct supported_block_type {
wtap_block_type_t type;
block_support_t support; /* BLOCK_NOT_SUPPORTED allowed, equivalent to absence */
size_t num_supported_options;
const struct supported_option_type *supported_options;
};
#define BLOCKS_SUPPORTED(block_type_array) \
sizeof block_type_array / sizeof block_type_array[0], block_type_array
struct file_type_subtype_info {
/**
* The file type description.
*/
const char *description;
/**
* The file type name, used to look up file types by name, e.g.
* looking up a file type specified as a command-line argument.
*/
const char *name;
/**
* The default file extension, used to save this type.
* Should be NULL if no default extension is known.
*/
const char *default_file_extension;
/**
* A semicolon-separated list of additional file extensions
* used for this type.
* Should be NULL if no extensions, or no extensions other
* than the default extension, are known.
*/
const char *additional_file_extensions;
/**
* When writing this file format, is seeking required?
*/
gboolean writing_must_seek;
/**
* Number of block types supported.
*/
size_t num_supported_blocks;
/**
* Table of block types supported.
*/
const struct supported_block_type *supported_blocks;
/**
* Can this type write this encapsulation format?
* Should be NULL is this file type doesn't have write support.
*/
int (*can_write_encap)(int);
/**
* The function to open the capture file for writing.
* Should be NULL if this file type doesn't have write support.
*/
int (*dump_open)(wtap_dumper *, int *, gchar **);
/**
* If can_write_encap returned WTAP_ERR_CHECK_WSLUA, then this is used instead.
* This should be NULL for everyone except Lua-based file writers.
*/
wtap_wslua_file_info_t *wslua_info;
};
#define WTAP_TYPE_AUTO 0
/**
* @brief Initialize the Wiretap library.
*
* @param load_wiretap_plugins Load Wiretap plugins when initializing library.
*/
WS_DLL_PUBLIC
void wtap_init(gboolean load_wiretap_plugins);
/** On failure, "wtap_open_offline()" returns NULL, and puts into the
* "int" pointed to by its second argument:
*
* @param filename Name of the file to open
* @param type WTAP_TYPE_AUTO for automatic recognize file format or explicit choose format type
* @param[out] err a positive "errno" value if the capture file can't be opened;
* a negative number, indicating the type of error, on other failures.
* @param[out] err_info for some errors, a string giving more details of
* the error
* @param do_random TRUE if random access to the file will be done,
* FALSE if not
*/
WS_DLL_PUBLIC
struct wtap* wtap_open_offline(const char *filename, unsigned int type, int *err,
gchar **err_info, gboolean do_random);
/**
* If we were compiled with zlib and we're at EOF, unset EOF so that
* wtap_read/gzread has a chance to succeed. This is necessary if
* we're tailing a file.
*/
WS_DLL_PUBLIC
void wtap_cleareof(wtap *wth);
/**
* Set callback functions to add new hostnames. Currently pcapng-only.
* MUST match add_ipv4_name and add_ipv6_name in addr_resolv.c.
*/
typedef void (*wtap_new_ipv4_callback_t) (const guint addr, const gchar *name, const gboolean static_entry);
WS_DLL_PUBLIC
void wtap_set_cb_new_ipv4(wtap *wth, wtap_new_ipv4_callback_t add_new_ipv4);
typedef void (*wtap_new_ipv6_callback_t) (const void *addrp, const gchar *name, const gboolean static_entry);
WS_DLL_PUBLIC
void wtap_set_cb_new_ipv6(wtap *wth, wtap_new_ipv6_callback_t add_new_ipv6);
/**
* Set callback function to receive new decryption secrets for a particular
* secrets type (as defined in secrets-types.h). Currently pcapng-only.
*/
typedef void (*wtap_new_secrets_callback_t)(guint32 secrets_type, const void *secrets, guint size);
WS_DLL_PUBLIC
void wtap_set_cb_new_secrets(wtap *wth, wtap_new_secrets_callback_t add_new_secrets);
/** Read the next record in the file, filling in *phdr and *buf.
*
* @wth a wtap * returned by a call that opened a file for reading.
* @rec a pointer to a wtap_rec, filled in with information about the
* record.
* @buf a pointer to a Buffer, filled in with data from the record.
* @param err a positive "errno" value, or a negative number indicating
* the type of error, if the read failed.
* @param err_info for some errors, a string giving more details of
* the error
* @param offset a pointer to a gint64, set to the offset in the file
* that should be used on calls to wtap_seek_read() to reread that record,
* if the read succeeded.
* @return TRUE on success, FALSE on failure.
*/
WS_DLL_PUBLIC
gboolean wtap_read(wtap *wth, wtap_rec *rec, Buffer *buf, int *err,
gchar **err_info, gint64 *offset);
/** Read the record at a specified offset in a capture file, filling in
* *phdr and *buf.
*
* @wth a wtap * returned by a call that opened a file for random-access
* reading.
* @seek_off a gint64 giving an offset value returned by a previous
* wtap_read() call.
* @rec a pointer to a struct wtap_rec, filled in with information
* about the record.
* @buf a pointer to a Buffer, filled in with data from the record.
* @param err a positive "errno" value, or a negative number indicating
* the type of error, if the read failed.
* @param err_info for some errors, a string giving more details of
* the error
* @return TRUE on success, FALSE on failure.
*/
WS_DLL_PUBLIC
gboolean wtap_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec,
Buffer *buf, int *err, gchar **err_info);
/*** initialize a wtap_rec structure ***/
WS_DLL_PUBLIC
void wtap_rec_init(wtap_rec *rec);
/*** Re-initialize a wtap_rec structure ***/
WS_DLL_PUBLIC
void wtap_rec_reset(wtap_rec *rec);
/*** clean up a wtap_rec structure, freeing what wtap_rec_init() allocated */
WS_DLL_PUBLIC
void wtap_rec_cleanup(wtap_rec *rec);
/*
* Types of compression for a file, including "none".
*/
typedef enum {
WTAP_UNCOMPRESSED,
WTAP_GZIP_COMPRESSED,
WTAP_ZSTD_COMPRESSED,
WTAP_LZ4_COMPRESSED
} wtap_compression_type;
WS_DLL_PUBLIC
wtap_compression_type wtap_get_compression_type(wtap *wth);
WS_DLL_PUBLIC
const char *wtap_compression_type_description(wtap_compression_type compression_type);
WS_DLL_PUBLIC
const char *wtap_compression_type_extension(wtap_compression_type compression_type);
WS_DLL_PUBLIC
GSList *wtap_get_all_compression_type_extensions_list(void);
/*** get various information snippets about the current file ***/
/** Return an approximation of the amount of data we've read sequentially
* from the file so far. */
WS_DLL_PUBLIC
gint64 wtap_read_so_far(wtap *wth);
WS_DLL_PUBLIC
gint64 wtap_file_size(wtap *wth, int *err);
WS_DLL_PUBLIC
guint wtap_snapshot_length(wtap *wth); /* per file */
WS_DLL_PUBLIC
int wtap_file_type_subtype(wtap *wth);
WS_DLL_PUBLIC
int wtap_file_encap(wtap *wth);
WS_DLL_PUBLIC
int wtap_file_tsprec(wtap *wth);
/**
* @brief Gets number of section header blocks.
* @details Returns the number of existing SHBs.
*
* @param wth The wiretap session.
* @return The number of existing section headers.
*/
WS_DLL_PUBLIC
guint wtap_file_get_num_shbs(wtap *wth);
/**
* @brief Gets existing section header block, not for new file.
* @details Returns the pointer to an existing SHB, without creating a
* new one. This should only be used for accessing info, not
* for creating a new file based on existing SHB info. Use
* wtap_file_get_shb_for_new_file() for that.
*
* @param wth The wiretap session.
* @param shb_num The ordinal number (0-based) of the section header
* in the file
* @return The specified existing section header, which must NOT be g_free'd.
*/
WS_DLL_PUBLIC
wtap_block_t wtap_file_get_shb(wtap *wth, guint shb_num);
/**
* @brief Sets or replaces the section header comment.
* @details The passed-in comment string is set to be the comment
* for the section header block. The passed-in string's
* ownership will be owned by the block, so it should be
* duplicated before passing into this function.
*
* @param wth The wiretap session.
* @param comment The comment string.
*/
WS_DLL_PUBLIC
void wtap_write_shb_comment(wtap *wth, gchar *comment);
/**
* @brief Gets existing interface descriptions.
* @details Returns a new struct containing a pointer to the existing
* description, without creating new descriptions internally.
* @note The returned pointer must be g_free'd, but its internal
* interface_data must not.
*
* @param wth The wiretap session.
* @return A new struct of the existing section descriptions, which must be g_free'd.
*/
WS_DLL_PUBLIC
wtapng_iface_descriptions_t *wtap_file_get_idb_info(wtap *wth);
/**
* @brief Gets next interface description.
*
* @details This returns the first unfetched wtap_block_t from the set
* of interface descriptions. Returns NULL if there are no more
* unfetched interface descriptions; a subsequent call after
* wtap_read() returns, either with a new record or an EOF, may return
* another interface description.
*/
WS_DLL_PUBLIC
wtap_block_t wtap_get_next_interface_description(wtap *wth);
/**
* @brief Free's a interface description block and all of its members.
*
* @details This free's all of the interface descriptions inside the passed-in
* struct, including their members (e.g., comments); and then free's the
* passed-in struct as well.
*
* @warning Do not use this for the struct returned by
* wtap_file_get_idb_info(), as that one did not create the internal
* interface descriptions; for that case you can simply g_free() the new
* struct.
*/
WS_DLL_PUBLIC
void wtap_free_idb_info(wtapng_iface_descriptions_t *idb_info);
/**
* @brief Gets a debug string of an interface description.
* @details Returns a newly allocated string of debug information about
* the given interface descrption, useful for debugging.
* @note The returned pointer must be g_free'd.
*
* @param if_descr The interface description.
* @param indent Number of spaces to indent each line by.
* @param line_end A string to append to each line (e.g., "\n" or ", ").
* @return A newly allocated gcahr array string, which must be g_free'd.
*/
WS_DLL_PUBLIC
gchar *wtap_get_debug_if_descr(const wtap_block_t if_descr,
const int indent,
const char* line_end);
/**
* @brief Gets existing name resolution block, not for new file.
* @details Returns the pointer to the existing NRB, without creating a
* new one. This should only be used for accessing info, not
* for creating a new file based on existing NRB info. Use
* wtap_file_get_nrb_for_new_file() for that.
*
* @param wth The wiretap session.
* @return The existing section header, which must NOT be g_free'd.
*
* XXX - need to be updated to handle multiple NRBs.
*/
WS_DLL_PUBLIC
wtap_block_t wtap_file_get_nrb(wtap *wth);
/**
* @brief Adds a Decryption Secrets Block to the open wiretap session.
* @details The passed-in DSB is added to the DSBs for the current
* session.
*
* @param wth The wiretap session.
* @param dsb The Decryption Secrets Block to add
*/
WS_DLL_PUBLIC
void wtap_file_add_decryption_secrets(wtap *wth, const wtap_block_t dsb);
/**
* Remove any decryption secret information from the per-file information;
* used if we're stripping decryption secrets while the file is open
*
* @param wth The wiretap session from which to remove the
* decryption secrets.
* @return TRUE if any DSBs were removed
*/
WS_DLL_PUBLIC
gboolean wtap_file_discard_decryption_secrets(wtap *wth);
/*** close the file descriptors for the current file ***/
WS_DLL_PUBLIC
void wtap_fdclose(wtap *wth);
/*** reopen the random file descriptor for the current file ***/
WS_DLL_PUBLIC
gboolean wtap_fdreopen(wtap *wth, const char *filename, int *err);
/** Close only the sequential side, freeing up memory it uses. */
WS_DLL_PUBLIC
void wtap_sequential_close(wtap *wth);
/** Closes any open file handles and frees the memory associated with wth. */
WS_DLL_PUBLIC
void wtap_close(wtap *wth);
/*** dump packets into a capture file ***/
WS_DLL_PUBLIC
gboolean wtap_dump_can_open(int filetype);
/**
* Given a GArray of WTAP_ENCAP_ types, return the per-file encapsulation
* type that would be needed to write out a file with those types.
*/
WS_DLL_PUBLIC
int wtap_dump_required_file_encap_type(const GArray *file_encaps);
/**
* Return TRUE if we can write this encapsulation type in this
* capture file type/subtype, FALSE if not.
*/
WS_DLL_PUBLIC
gboolean wtap_dump_can_write_encap(int file_type_subtype, int encap);
/**
* Return TRUE if we can write this capture file type/subtype out in
* compressed form, FALSE if not.
*/
WS_DLL_PUBLIC
gboolean wtap_dump_can_compress(int file_type_subtype);
/**
* Initialize the per-file information based on an existing file. Its
* contents must be freed according to the requirements of wtap_dump_params.
* If wth does not remain valid for the duration of the session, dsbs_growing
* MUST be cleared after this function.
*
* @param params The parameters for wtap_dump_* to initialize.
* @param wth The wiretap session.
*/
WS_DLL_PUBLIC
void wtap_dump_params_init(wtap_dump_params *params, wtap *wth);
/**
* Initialize the per-file information based on an existing file, but
* don't copy over the interface information. Its contents must be freed
* according to the requirements of wtap_dump_params.
* If wth does not remain valid for the duration of the session, dsbs_growing
* MUST be cleared after this function.
*
* XXX - this should eventually become wtap_dump_params_init(), with all
* programs writing capture files copying IDBs over by hand, so that they
* handle IDBs in the middle of the file.
*
* @param params The parameters for wtap_dump_* to initialize.
* @param wth The wiretap session.
*/
WS_DLL_PUBLIC
void wtap_dump_params_init_no_idbs(wtap_dump_params *params, wtap *wth);
/**
* Remove any name resolution information from the per-file information;
* used if we're stripping name resolution as we write the file.
*
* @param params The parameters for wtap_dump_* from which to remove the
* name resolution..
*/
WS_DLL_PUBLIC
void wtap_dump_params_discard_name_resolution(wtap_dump_params *params);
/**
* Remove any decryption secret information from the per-file information;
* used if we're stripping decryption secrets as we write the file.
*
* @param params The parameters for wtap_dump_* from which to remove the
* decryption secrets..
*/
WS_DLL_PUBLIC
void wtap_dump_params_discard_decryption_secrets(wtap_dump_params *params);
/**
* Free memory associated with the wtap_dump_params when it is no longer in
* use by wtap_dumper.
*
* @param params The parameters as initialized by wtap_dump_params_init.
*/
WS_DLL_PUBLIC
void wtap_dump_params_cleanup(wtap_dump_params *params);
/**
* @brief Opens a new capture file for writing.
*
* @param filename The new file's name.
* @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type.
* @param compression_type Type of compression to use when writing, if any
* @param params The per-file information for this file.
* @param[out] err Will be set to an error code on failure.
* @param[out] err_info for some errors, a string giving more details of
* the error
* @return The newly created dumper object, or NULL on failure.
*/
WS_DLL_PUBLIC
wtap_dumper* wtap_dump_open(const char *filename, int file_type_subtype,
wtap_compression_type compression_type, const wtap_dump_params *params,
int *err, gchar **err_info);
/**
* @brief Creates a dumper for a temporary file.
*
* @param tmpdir Directory in which to create the temporary file.
* @param filenamep Points to a pointer that's set to point to the
* pathname of the temporary file; it's allocated with g_malloc()
* @param pfx A string to be used as the prefix for the temporary file name
* @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type.
* @param compression_type Type of compression to use when writing, if any
* @param params The per-file information for this file.
* @param[out] err Will be set to an error code on failure.
* @param[out] err_info for some errors, a string giving more details of
* the error
* @return The newly created dumper object, or NULL on failure.
*/
WS_DLL_PUBLIC
wtap_dumper* wtap_dump_open_tempfile(const char *tmpdir, char **filenamep,
const char *pfx,
int file_type_subtype, wtap_compression_type compression_type,
const wtap_dump_params *params, int *err, gchar **err_info);
/**
* @brief Creates a dumper for an existing file descriptor.
*
* @param fd The file descriptor for which the dumper should be created.
* @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type.
* @param compression_type Type of compression to use when writing, if any
* @param params The per-file information for this file.
* @param[out] err Will be set to an error code on failure.
* @param[out] err_info for some errors, a string giving more details of
* the error
* @return The newly created dumper object, or NULL on failure.
*/
WS_DLL_PUBLIC
wtap_dumper* wtap_dump_fdopen(int fd, int file_type_subtype,
wtap_compression_type compression_type, const wtap_dump_params *params,
int *err, gchar **err_info);
/**
* @brief Creates a dumper for the standard output.
*
* @param file_type_subtype The WTAP_FILE_TYPE_SUBTYPE_XXX file type.
* @param compression_type Type of compression to use when writing, if any
* @param params The per-file information for this file.
* @param[out] err Will be set to an error code on failure.
* @param[out] err_info for some errors, a string giving more details of
* the error
* @return The newly created dumper object, or NULL on failure.
*/
WS_DLL_PUBLIC
wtap_dumper* wtap_dump_open_stdout(int file_type_subtype,
wtap_compression_type compression_type, const wtap_dump_params *params,
int *err, gchar **err_info);
/*
* Add an IDB to the list of IDBs for a file we're writing.
* Makes a copy of the IDB, so it can be freed after this call is made.
*
* @param wdh handle for the file we're writing.
* @param idb the IDB to add
* @param[out] err Will be set to an error code on failure.
* @param[out] err_info for some errors, a string giving more details of
* the error.
* @return TRUE on success, FALSE on failure.
*/
WS_DLL_PUBLIC
gboolean wtap_dump_add_idb(wtap_dumper *wdh, wtap_block_t idb, int *err,
gchar **err_info);
WS_DLL_PUBLIC
gboolean wtap_dump(wtap_dumper *, const wtap_rec *, const guint8 *,
int *err, gchar **err_info);
WS_DLL_PUBLIC
gboolean wtap_dump_flush(wtap_dumper *, int *);
WS_DLL_PUBLIC
int wtap_dump_file_type_subtype(wtap_dumper *wdh);
WS_DLL_PUBLIC
gint64 wtap_get_bytes_dumped(wtap_dumper *);
WS_DLL_PUBLIC
void wtap_set_bytes_dumped(wtap_dumper *wdh, gint64 bytes_dumped);
struct addrinfo;
WS_DLL_PUBLIC
gboolean wtap_addrinfo_list_empty(addrinfo_lists_t *addrinfo_lists);
WS_DLL_PUBLIC
gboolean wtap_dump_set_addrinfo_list(wtap_dumper *wdh, addrinfo_lists_t *addrinfo_lists);
WS_DLL_PUBLIC
void wtap_dump_discard_name_resolution(wtap_dumper *wdh);
WS_DLL_PUBLIC
void wtap_dump_discard_decryption_secrets(wtap_dumper *wdh);
/**
* Closes open file handles and frees memory associated with wdh. Note that
* shb_hdr and idb_inf are not freed by this routine.
*
* @param wdh handle for the file we're closing.
* @param[out] needs_reload if not null, points to a gboolean that will
* be set to TRUE if a full reload of the file would be required if
* this was done as part of a "Save" or "Save As" operation, FALSE
* if no full reload would be required.
* @param[out] err points to an int that will be set to an error code
* on failure.
* @param[out] err_info for some errors, points to a gchar * that will
* be set to a string giving more details of the error.
*
* @return TRUE on success, FALSE on failure.
*/
WS_DLL_PUBLIC
gboolean wtap_dump_close(wtap_dumper *wdh, gboolean *needs_reload,
int *err, gchar **err_info);
/**
* Return TRUE if we can write a file out with the given GArray of file
* encapsulations and the given bitmask of comment types.
*/
WS_DLL_PUBLIC
gboolean wtap_dump_can_write(const GArray *file_encaps, guint32 required_comment_types);
/**
* Generates arbitrary packet data in "exported PDU" format
* and appends it to buf.
* For filetype readers to transform non-packetized data.
* Calls ws_buffer_asssure_space() for you and handles padding
* to 4-byte boundary.
*
* @param[in,out] buf Buffer into which to write field
* @param epdu_tag tag ID of field to create
* @param data data to be written
* @param data_len length of data
*/
WS_DLL_PUBLIC
void wtap_buffer_append_epdu_tag(Buffer *buf, guint16 epdu_tag, const guint8 *data, guint16 data_len);
/**
* Generates packet data for an unsigned integer in "exported PDU" format.
* For filetype readers to transform non-packetized data.
*
* @param[in,out] buf Buffer into which to write field
* @param epdu_tag tag ID of field to create
* @param val integer value to write to buf
*/
WS_DLL_PUBLIC
void wtap_buffer_append_epdu_uint(Buffer *buf, guint16 epdu_tag, guint32 val);
/**
* Generates packet data for a string in "exported PDU" format.
* For filetype readers to transform non-packetized data.
*
* @param[in,out] buf Buffer into which to write field
* @param epdu_tag tag ID of field to create
* @param val string value to write to buf
*/
WS_DLL_PUBLIC
void wtap_buffer_append_epdu_string(Buffer *buf, guint16 epdu_tag, const char *val);
/**
* Close off a set of "exported PDUs" added to the buffer.
* For filetype readers to transform non-packetized data.
*
* @param[in,out] buf Buffer into which to write field
*
* @return Total length of buf populated to date
*/
WS_DLL_PUBLIC
gint wtap_buffer_append_epdu_end(Buffer *buf);
/*
* Sort the file types by name or by description?
*/
typedef enum {
FT_SORT_BY_NAME,
FT_SORT_BY_DESCRIPTION
} ft_sort_order;
/**
* Get a GArray of file type/subtype values for file types/subtypes
* that can be used to save a file of a given type with a given GArray of
* WTAP_ENCAP_ types and the given bitmask of comment types.
*/
WS_DLL_PUBLIC
GArray *wtap_get_savable_file_types_subtypes_for_file(int file_type,
const GArray *file_encaps, guint32 required_comment_types,
ft_sort_order sort_order);
/**
* Get a GArray of all writable file type/subtype values.
*/
WS_DLL_PUBLIC
GArray *wtap_get_writable_file_types_subtypes(ft_sort_order sort_order);
/*** various file type/subtype functions ***/
WS_DLL_PUBLIC
const char *wtap_file_type_subtype_description(int file_type_subtype);
WS_DLL_PUBLIC
const char *wtap_file_type_subtype_name(int file_type_subtype);
WS_DLL_PUBLIC
int wtap_name_to_file_type_subtype(const char *name);
WS_DLL_PUBLIC
int wtap_pcap_file_type_subtype(void);
WS_DLL_PUBLIC
int wtap_pcap_nsec_file_type_subtype(void);
WS_DLL_PUBLIC
int wtap_pcapng_file_type_subtype(void);
/**
* Return an indication of whether this capture file format supports
* the block in question.
*/
WS_DLL_PUBLIC
block_support_t wtap_file_type_subtype_supports_block(int filetype,
wtap_block_type_t type);
/**
* Return an indication of whether this capture file format supports
* the option in queston for the block in question.
*/
WS_DLL_PUBLIC
option_support_t wtap_file_type_subtype_supports_option(int filetype,
wtap_block_type_t type, guint opttype);
/*** various file extension functions ***/
WS_DLL_PUBLIC
GSList *wtap_get_all_capture_file_extensions_list(void);
WS_DLL_PUBLIC
const char *wtap_default_file_extension(int filetype);
WS_DLL_PUBLIC
GSList *wtap_get_file_extensions_list(int filetype, gboolean include_compressed);
WS_DLL_PUBLIC
GSList *wtap_get_all_file_extensions_list(void);
WS_DLL_PUBLIC
void wtap_free_extensions_list(GSList *extensions);
WS_DLL_PUBLIC
const char *wtap_encap_name(int encap);
WS_DLL_PUBLIC
const char *wtap_encap_description(int encap);
WS_DLL_PUBLIC
int wtap_name_to_encap(const char *short_name);
WS_DLL_PUBLIC
const char* wtap_tsprec_string(int tsprec);
WS_DLL_PUBLIC
const char *wtap_strerror(int err);
/*** get available number of file types and encapsulations ***/
WS_DLL_PUBLIC
int wtap_get_num_file_type_extensions(void);
WS_DLL_PUBLIC
int wtap_get_num_encap_types(void);
/*** get information for file type extension ***/
WS_DLL_PUBLIC
const char *wtap_get_file_extension_type_name(int extension_type);
WS_DLL_PUBLIC
GSList *wtap_get_file_extension_type_extensions(guint extension_type);
/*** dynamically register new file types and encapsulations ***/
WS_DLL_PUBLIC
void wtap_register_file_type_extension(const struct file_extension_info *ei);
typedef struct {
void (*register_wtap_module)(void); /* routine to call to register a wiretap module */
} wtap_plugin;
WS_DLL_PUBLIC
void wtap_register_plugin(const wtap_plugin *plug);
/** Returns_
* 0 if plugins can be loaded for libwiretap (file type).
* 1 if plugins are not supported by the platform.
* -1 if plugins were disabled in the build configuration.
*/
WS_DLL_PUBLIC
int wtap_plugins_supported(void);
WS_DLL_PUBLIC
void wtap_register_open_info(struct open_info *oi, const gboolean first_routine);
WS_DLL_PUBLIC
gboolean wtap_has_open_info(const gchar *name);
WS_DLL_PUBLIC
gboolean wtap_uses_lua_filehandler(const wtap* wth);
WS_DLL_PUBLIC
void wtap_deregister_open_info(const gchar *name);
WS_DLL_PUBLIC
unsigned int open_info_name_to_type(const char *name);
WS_DLL_PUBLIC
int wtap_register_file_type_subtype(const struct file_type_subtype_info* fi);
WS_DLL_PUBLIC
void wtap_deregister_file_type_subtype(const int file_type_subtype);
WS_DLL_PUBLIC
int wtap_register_encap_type(const char *description, const char *name);
/*** Cleanup the internal library structures */
WS_DLL_PUBLIC
void wtap_cleanup(void);
/**
* Wiretap error codes.
*/
#define WTAP_ERR_NOT_REGULAR_FILE -1
/**< The file being opened for reading isn't a plain file (or pipe) */
#define WTAP_ERR_RANDOM_OPEN_PIPE -2
/**< The file is being opened for random access and it's a pipe */
#define WTAP_ERR_FILE_UNKNOWN_FORMAT -3
/**< The file being opened is not a capture file in a known format */
#define WTAP_ERR_UNSUPPORTED -4
/**< Supported file type, but there's something in the file we're
reading that we can't support */
#define WTAP_ERR_CANT_WRITE_TO_PIPE -5
/**< Wiretap can't save to a pipe in the specified format */
#define WTAP_ERR_CANT_OPEN -6
/**< The file couldn't be opened, reason unknown */
#define WTAP_ERR_UNWRITABLE_FILE_TYPE -7
/**< Wiretap can't save files in the specified format */
#define WTAP_ERR_UNWRITABLE_ENCAP -8
/**< Wiretap can't read or save files in the specified format with the
specified encapsulation */
#define WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED -9
/**< The specified format doesn't support per-packet encapsulations */
#define WTAP_ERR_CANT_WRITE -10
/**< An attempt to read failed, reason unknown */
#define WTAP_ERR_CANT_CLOSE -11
/**< The file couldn't be closed, reason unknown */
#define WTAP_ERR_SHORT_READ -12
/**< An attempt to read less data than it should have */
#define WTAP_ERR_BAD_FILE -13
/**< The file appears to be damaged or corrupted or otherwise bogus */
#define WTAP_ERR_SHORT_WRITE -14
/**< An attempt to write wrote less data than it should have */
#define WTAP_ERR_UNC_OVERFLOW -15
/**< Uncompressing Sniffer data would overflow buffer */
#define WTAP_ERR_RANDOM_OPEN_STDIN -16
/**< We're trying to open the standard input for random access */
#define WTAP_ERR_COMPRESSION_NOT_SUPPORTED -17
/**< The filetype doesn't support output compression */
#define WTAP_ERR_CANT_SEEK -18
/**< An attempt to seek failed, reason unknown */
#define WTAP_ERR_CANT_SEEK_COMPRESSED -19
/**< An attempt to seek on a compressed stream */
#define WTAP_ERR_DECOMPRESS -20
/**< Error decompressing */
#define WTAP_ERR_INTERNAL -21
/**< "Shouldn't happen" internal errors */
#define WTAP_ERR_PACKET_TOO_LARGE -22
/**< Packet being written is larger than we support; do not use when
reading, use WTAP_ERR_BAD_FILE instead */
#define WTAP_ERR_CHECK_WSLUA -23
/**< Not really an error: the file type being checked is from a Lua
plugin, so that the code will call wslua_can_write_encap() instead if it gets this */
#define WTAP_ERR_UNWRITABLE_REC_TYPE -24
/**< Specified record type can't be written to that file type */
#define WTAP_ERR_UNWRITABLE_REC_DATA -25
/**< Something in the record data can't be written to that file type */
#define WTAP_ERR_DECOMPRESSION_NOT_SUPPORTED -26
/**< We don't support decompressing that type of compressed file */
#define WTAP_ERR_TIME_STAMP_NOT_SUPPORTED -27
/**< We don't support writing that record's time stamp to that
file type */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __WTAP_H__ */
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wiretap/wtap_modules.h | /** @file
*
* Definitions for wiretap module registration
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WTAP_MODULES_H__
#define __WTAP_MODULES_H__
#include <glib.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* Entry in the table of built-in wiretap modules to register.
*/
typedef struct _wtap_module_reg {
const char *cb_name;
void (*cb_func)(void);
} wtap_module_reg_t;
extern wtap_module_reg_t wtap_module_reg[];
extern const guint wtap_module_count;
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __WTAP_MODULES_H__ */
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C | wireshark/wiretap/wtap_opttypes.c | /* wtap_opttypes.c
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2001 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <glib.h>
#include <string.h>
#include "wtap.h"
#include "wtap_opttypes.h"
#include "wtap-int.h"
#include "pcapng_module.h"
#include <wsutil/ws_assert.h>
#include <wsutil/glib-compat.h>
#include <wsutil/inet_ipv6.h>
#include <wsutil/unicode-utils.h>
#if 0
#define wtap_debug(...) ws_warning(__VA_ARGS__)
#define DEBUG_COUNT_REFS
#else
#define wtap_debug(...)
#endif
#define ROUND_TO_4BYTE(len) (((len) + 3) & ~3)
/*
* Structure describing a type of block.
*/
typedef struct {
wtap_block_type_t block_type; /**< internal type code for block */
const char *name; /**< name of block */
const char *description; /**< human-readable description of block */
wtap_block_create_func create;
wtap_mand_free_func free_mand;
wtap_mand_copy_func copy_mand;
GHashTable *options; /**< hash table of known options */
} wtap_blocktype_t;
#define GET_OPTION_TYPE(options, option_id) \
(const wtap_opttype_t *)g_hash_table_lookup((options), GUINT_TO_POINTER(option_id))
/*
* Structure describing a type of option.
*/
typedef struct {
const char *name; /**< name of option */
const char *description; /**< human-readable description of option */
wtap_opttype_e data_type; /**< data type of that option */
guint flags; /**< flags for the option */
} wtap_opttype_t;
/* Flags */
#define WTAP_OPTTYPE_FLAG_MULTIPLE_ALLOWED 0x00000001 /* multiple instances allowed */
/* Debugging reference counting */
#ifdef DEBUG_COUNT_REFS
static guint block_count = 0;
static guint8 blocks_active[sizeof(guint)/8];
static void rc_set(guint refnum)
{
guint cellno = refnum / 8;
guint bitno = refnum % 8;
blocks_active[cellno] |= (guint8)(1 << bitno);
}
static void rc_clear(guint refnum)
{
guint cellno = refnum / 8;
guint bitno = refnum % 8;
blocks_active[cellno] &= (guint8)~(1 << bitno);
}
#endif /* DEBUG_COUNT_REFS */
struct wtap_block
{
wtap_blocktype_t* info;
void* mandatory_data;
GArray* options;
gint ref_count;
#ifdef DEBUG_COUNT_REFS
guint id;
#endif
};
/* Keep track of wtap_blocktype_t's via their id number */
static wtap_blocktype_t* blocktype_list[MAX_WTAP_BLOCK_TYPE_VALUE];
static if_filter_opt_t if_filter_dup(if_filter_opt_t* filter_src)
{
if_filter_opt_t filter_dest;
memset(&filter_dest, 0, sizeof(filter_dest));
/* Deep copy. */
filter_dest.type = filter_src->type;
switch (filter_src->type) {
case if_filter_pcap:
/* pcap filter string */
filter_dest.data.filter_str =
g_strdup(filter_src->data.filter_str);
break;
case if_filter_bpf:
/* BPF program */
filter_dest.data.bpf_prog.bpf_prog_len =
filter_src->data.bpf_prog.bpf_prog_len;
filter_dest.data.bpf_prog.bpf_prog =
(wtap_bpf_insn_t *)g_memdup2(filter_src->data.bpf_prog.bpf_prog,
filter_src->data.bpf_prog.bpf_prog_len * sizeof (wtap_bpf_insn_t));
break;
default:
break;
}
return filter_dest;
}
static void if_filter_free(if_filter_opt_t* filter_src)
{
switch (filter_src->type) {
case if_filter_pcap:
/* pcap filter string */
g_free(filter_src->data.filter_str);
break;
case if_filter_bpf:
/* BPF program */
g_free(filter_src->data.bpf_prog.bpf_prog);
break;
default:
break;
}
}
static packet_verdict_opt_t
packet_verdict_dup(packet_verdict_opt_t* verdict_src)
{
packet_verdict_opt_t verdict_dest;
memset(&verdict_dest, 0, sizeof(verdict_dest));
/* Deep copy. */
verdict_dest.type = verdict_src->type;
switch (verdict_src->type) {
case packet_verdict_hardware:
/* array of octets */
verdict_dest.data.verdict_bytes =
g_byte_array_new_take((guint8 *)g_memdup2(verdict_src->data.verdict_bytes->data,
verdict_src->data.verdict_bytes->len),
verdict_src->data.verdict_bytes->len);
break;
case packet_verdict_linux_ebpf_tc:
/* eBPF TC_ACT_ value */
verdict_dest.data.verdict_linux_ebpf_tc =
verdict_src->data.verdict_linux_ebpf_tc;
break;
case packet_verdict_linux_ebpf_xdp:
/* xdp_action value */
verdict_dest.data.verdict_linux_ebpf_xdp =
verdict_src->data.verdict_linux_ebpf_xdp;
break;
default:
break;
}
return verdict_dest;
}
void wtap_packet_verdict_free(packet_verdict_opt_t* verdict)
{
switch (verdict->type) {
case packet_verdict_hardware:
/* array of bytes */
g_byte_array_free(verdict->data.verdict_bytes, TRUE);
break;
default:
break;
}
}
static packet_hash_opt_t
packet_hash_dup(packet_hash_opt_t* hash_src)
{
packet_hash_opt_t hash_dest;
memset(&hash_dest, 0, sizeof(hash_dest));
/* Deep copy. */
hash_dest.type = hash_src->type;
/* array of octets */
hash_dest.hash_bytes =
g_byte_array_new_take((guint8 *)g_memdup2(hash_src->hash_bytes->data,
hash_src->hash_bytes->len),
hash_src->hash_bytes->len);
return hash_dest;
}
void wtap_packet_hash_free(packet_hash_opt_t* hash)
{
/* array of bytes */
g_byte_array_free(hash->hash_bytes, TRUE);
}
static void wtap_opttype_block_register(wtap_blocktype_t *blocktype)
{
wtap_block_type_t block_type;
static const wtap_opttype_t opt_comment = {
"opt_comment",
"Comment",
WTAP_OPTTYPE_STRING,
WTAP_OPTTYPE_FLAG_MULTIPLE_ALLOWED
};
static const wtap_opttype_t opt_custom = {
"opt_custom",
"Custom Option",
WTAP_OPTTYPE_CUSTOM,
WTAP_OPTTYPE_FLAG_MULTIPLE_ALLOWED
};
block_type = blocktype->block_type;
/* Check input */
ws_assert(block_type < MAX_WTAP_BLOCK_TYPE_VALUE);
/* Don't re-register. */
ws_assert(blocktype_list[block_type] == NULL);
/* Sanity check */
ws_assert(blocktype->name);
ws_assert(blocktype->description);
ws_assert(blocktype->create);
/*
* Initialize the set of supported options.
* All blocks that support options at all support
* OPT_COMMENT and OPT_CUSTOM.
*
* XXX - there's no "g_uint_hash()" or "g_uint_equal()",
* so we use "g_direct_hash()" and "g_direct_equal()".
*/
blocktype->options = g_hash_table_new(g_direct_hash, g_direct_equal);
g_hash_table_insert(blocktype->options, GUINT_TO_POINTER(OPT_COMMENT),
(gpointer)&opt_comment);
g_hash_table_insert(blocktype->options, GUINT_TO_POINTER(OPT_CUSTOM_STR_COPY),
(gpointer)&opt_custom);
g_hash_table_insert(blocktype->options, GUINT_TO_POINTER(OPT_CUSTOM_BIN_COPY),
(gpointer)&opt_custom);
g_hash_table_insert(blocktype->options, GUINT_TO_POINTER(OPT_CUSTOM_STR_NO_COPY),
(gpointer)&opt_custom);
g_hash_table_insert(blocktype->options, GUINT_TO_POINTER(OPT_CUSTOM_BIN_NO_COPY),
(gpointer)&opt_custom);
blocktype_list[block_type] = blocktype;
}
static void wtap_opttype_option_register(wtap_blocktype_t *blocktype, guint opttype, const wtap_opttype_t *option)
{
g_hash_table_insert(blocktype->options, GUINT_TO_POINTER(opttype),
(gpointer) option);
}
wtap_block_type_t wtap_block_get_type(wtap_block_t block)
{
return block->info->block_type;
}
void* wtap_block_get_mandatory_data(wtap_block_t block)
{
return block->mandatory_data;
}
static wtap_optval_t *
wtap_block_get_option(wtap_block_t block, guint option_id)
{
guint i;
wtap_option_t *opt;
if (block == NULL) {
return NULL;
}
for (i = 0; i < block->options->len; i++) {
opt = &g_array_index(block->options, wtap_option_t, i);
if (opt->option_id == option_id)
return &opt->value;
}
return NULL;
}
static wtap_optval_t *
wtap_block_get_nth_option(wtap_block_t block, guint option_id, guint idx)
{
guint i;
wtap_option_t *opt;
guint opt_idx;
if (block == NULL) {
return NULL;
}
opt_idx = 0;
for (i = 0; i < block->options->len; i++) {
opt = &g_array_index(block->options, wtap_option_t, i);
if (opt->option_id == option_id) {
if (opt_idx == idx)
return &opt->value;
opt_idx++;
}
}
return NULL;
}
wtap_block_t wtap_block_create(wtap_block_type_t block_type)
{
wtap_block_t block;
if (block_type >= MAX_WTAP_BLOCK_TYPE_VALUE)
return NULL;
block = g_new(struct wtap_block, 1);
block->info = blocktype_list[block_type];
block->options = g_array_new(FALSE, FALSE, sizeof(wtap_option_t));
block->info->create(block);
block->ref_count = 1;
#ifdef DEBUG_COUNT_REFS
block->id = block_count++;
rc_set(block->id);
wtap_debug("Created #%d %s", block->id, block->info->name);
#endif /* DEBUG_COUNT_REFS */
return block;
}
static void wtap_block_free_option(wtap_block_t block, wtap_option_t *opt)
{
const wtap_opttype_t *opttype;
if (block == NULL) {
return;
}
opttype = GET_OPTION_TYPE(block->info->options, opt->option_id);
switch (opttype->data_type) {
case WTAP_OPTTYPE_STRING:
g_free(opt->value.stringval);
break;
case WTAP_OPTTYPE_BYTES:
g_bytes_unref(opt->value.byteval);
break;
case WTAP_OPTTYPE_CUSTOM:
switch (opt->value.custom_opt.pen) {
case PEN_NFLX:
g_free(opt->value.custom_opt.data.nflx_data.custom_data);
break;
default:
g_free(opt->value.custom_opt.data.generic_data.custom_data);
break;
}
break;
case WTAP_OPTTYPE_IF_FILTER:
if_filter_free(&opt->value.if_filterval);
break;
case WTAP_OPTTYPE_PACKET_VERDICT:
wtap_packet_verdict_free(&opt->value.packet_verdictval);
break;
case WTAP_OPTTYPE_PACKET_HASH:
wtap_packet_hash_free(&opt->value.packet_hash);
break;
default:
break;
}
}
static void wtap_block_free_options(wtap_block_t block)
{
guint i;
wtap_option_t *opt;
if (block == NULL || block->options == NULL) {
return;
}
for (i = 0; i < block->options->len; i++) {
opt = &g_array_index(block->options, wtap_option_t, i);
wtap_block_free_option(block, opt);
}
g_array_remove_range(block->options, 0, block->options->len);
}
wtap_block_t wtap_block_ref(wtap_block_t block)
{
if (block == NULL) {
return NULL;
}
g_atomic_int_inc(&block->ref_count);
#ifdef DEBUG_COUNT_REFS
wtap_debug("Ref #%d %s", block->id, block->info->name);
#endif /* DEBUG_COUNT_REFS */
return block;
}
void wtap_block_unref(wtap_block_t block)
{
if (block != NULL)
{
if (g_atomic_int_dec_and_test(&block->ref_count)) {
#ifdef DEBUG_COUNT_REFS
wtap_debug("Destroy #%d %s", block->id, block->info->name);
rc_clear(block->id);
#endif /* DEBUG_COUNT_REFS */
if (block->info->free_mand != NULL)
block->info->free_mand(block);
g_free(block->mandatory_data);
wtap_block_free_options(block);
g_array_free(block->options, TRUE);
g_free(block);
}
#ifdef DEBUG_COUNT_REFS
else {
wtap_debug("Unref #%d %s", block->id, block->info->name);
}
#endif /* DEBUG_COUNT_REFS */
}
}
void wtap_block_array_free(GArray* block_array)
{
guint block;
if (block_array == NULL)
return;
for (block = 0; block < block_array->len; block++) {
wtap_block_unref(g_array_index(block_array, wtap_block_t, block));
}
g_array_free(block_array, TRUE);
}
/*
* Make a copy of a block.
*/
void
wtap_block_copy(wtap_block_t dest_block, wtap_block_t src_block)
{
guint i;
wtap_option_t *src_opt;
const wtap_opttype_t *opttype;
/*
* Copy the mandatory data.
*/
if (dest_block->info->copy_mand != NULL)
dest_block->info->copy_mand(dest_block, src_block);
/* Copy the options. For now, don't remove any options that are in destination
* but not source.
*/
for (i = 0; i < src_block->options->len; i++)
{
src_opt = &g_array_index(src_block->options, wtap_option_t, i);
opttype = GET_OPTION_TYPE(src_block->info->options, src_opt->option_id);
switch(opttype->data_type) {
case WTAP_OPTTYPE_UINT8:
wtap_block_add_uint8_option(dest_block, src_opt->option_id, src_opt->value.uint8val);
break;
case WTAP_OPTTYPE_UINT32:
wtap_block_add_uint32_option(dest_block, src_opt->option_id, src_opt->value.uint32val);
break;
case WTAP_OPTTYPE_UINT64:
wtap_block_add_uint64_option(dest_block, src_opt->option_id, src_opt->value.uint64val);
break;
case WTAP_OPTTYPE_IPv4:
wtap_block_add_ipv4_option(dest_block, src_opt->option_id, src_opt->value.ipv4val);
break;
case WTAP_OPTTYPE_IPv6:
wtap_block_add_ipv6_option(dest_block, src_opt->option_id, &src_opt->value.ipv6val);
break;
case WTAP_OPTTYPE_STRING:
wtap_block_add_string_option(dest_block, src_opt->option_id, src_opt->value.stringval, strlen(src_opt->value.stringval));
break;
case WTAP_OPTTYPE_BYTES:
wtap_block_add_bytes_option_borrow(dest_block, src_opt->option_id, src_opt->value.byteval);
break;
case WTAP_OPTTYPE_CUSTOM:
switch (src_opt->value.custom_opt.pen) {
case PEN_NFLX:
wtap_block_add_nflx_custom_option(dest_block, src_opt->value.custom_opt.data.nflx_data.type, src_opt->value.custom_opt.data.nflx_data.custom_data, src_opt->value.custom_opt.data.nflx_data.custom_data_len);
break;
default:
wtap_block_add_custom_option(dest_block, src_opt->option_id, src_opt->value.custom_opt.pen, src_opt->value.custom_opt.data.generic_data.custom_data, src_opt->value.custom_opt.data.generic_data.custom_data_len);
break;
}
break;
case WTAP_OPTTYPE_IF_FILTER:
wtap_block_add_if_filter_option(dest_block, src_opt->option_id, &src_opt->value.if_filterval);
break;
case WTAP_OPTTYPE_PACKET_VERDICT:
wtap_block_add_packet_verdict_option(dest_block, src_opt->option_id, &src_opt->value.packet_verdictval);
break;
case WTAP_OPTTYPE_PACKET_HASH:
wtap_block_add_packet_hash_option(dest_block, src_opt->option_id, &src_opt->value.packet_hash);
break;
}
}
}
wtap_block_t wtap_block_make_copy(wtap_block_t block)
{
wtap_block_t block_copy;
block_copy = wtap_block_create(block->info->block_type);
wtap_block_copy(block_copy, block);
return block_copy;
}
guint
wtap_block_count_option(wtap_block_t block, guint option_id)
{
guint i;
guint ret_val = 0;
wtap_option_t *opt;
if (block == NULL) {
return 0;
}
for (i = 0; i < block->options->len; i++) {
opt = &g_array_index(block->options, wtap_option_t, i);
if (opt->option_id == option_id)
ret_val++;
}
return ret_val;
}
gboolean wtap_block_foreach_option(wtap_block_t block, wtap_block_foreach_func func, void* user_data)
{
guint i;
wtap_option_t *opt;
const wtap_opttype_t *opttype;
if (block == NULL) {
return TRUE;
}
for (i = 0; i < block->options->len; i++) {
opt = &g_array_index(block->options, wtap_option_t, i);
opttype = GET_OPTION_TYPE(block->info->options, opt->option_id);
if (!func(block, opt->option_id, opttype->data_type, &opt->value, user_data))
return FALSE;
}
return TRUE;
}
static wtap_opttype_return_val
wtap_block_add_option_common(wtap_block_t block, guint option_id, wtap_opttype_e type, wtap_option_t **optp)
{
wtap_option_t *opt;
const wtap_opttype_t *opttype;
guint i;
if (block == NULL) {
return WTAP_OPTTYPE_BAD_BLOCK;
}
opttype = GET_OPTION_TYPE(block->info->options, option_id);
if (opttype == NULL) {
/* There's no option for this block with that option ID */
return WTAP_OPTTYPE_NO_SUCH_OPTION;
}
/*
* Is this an option of the specified data type?
*/
if (opttype->data_type != type) {
/*
* No.
*/
return WTAP_OPTTYPE_TYPE_MISMATCH;
}
/*
* Can there be more than one instance of this option?
*/
if (!(opttype->flags & WTAP_OPTTYPE_FLAG_MULTIPLE_ALLOWED)) {
/*
* No. Is there already an instance of this option?
*/
if (wtap_block_get_option(block, option_id) != NULL) {
/*
* Yes. Fail.
*/
return WTAP_OPTTYPE_ALREADY_EXISTS;
}
}
/*
* Add an instance.
*/
i = block->options->len;
g_array_set_size(block->options, i + 1);
opt = &g_array_index(block->options, wtap_option_t, i);
opt->option_id = option_id;
*optp = opt;
return WTAP_OPTTYPE_SUCCESS;
}
static wtap_opttype_return_val
wtap_block_get_option_common(wtap_block_t block, guint option_id, wtap_opttype_e type, wtap_optval_t **optvalp)
{
const wtap_opttype_t *opttype;
wtap_optval_t *optval;
if (block == NULL) {
return WTAP_OPTTYPE_BAD_BLOCK;
}
opttype = GET_OPTION_TYPE(block->info->options, option_id);
if (opttype == NULL) {
/* There's no option for this block with that option ID */
return WTAP_OPTTYPE_NO_SUCH_OPTION;
}
/*
* Is this an option of the specified data type?
*/
if (opttype->data_type != type) {
/*
* No.
*/
return WTAP_OPTTYPE_TYPE_MISMATCH;
}
/*
* Can there be more than one instance of this option?
*/
if (opttype->flags & WTAP_OPTTYPE_FLAG_MULTIPLE_ALLOWED) {
/*
* Yes. You can't ask for "the" value.
*/
return WTAP_OPTTYPE_NUMBER_MISMATCH;
}
optval = wtap_block_get_option(block, option_id);
if (optval == NULL) {
/* Didn't find the option */
return WTAP_OPTTYPE_NOT_FOUND;
}
*optvalp = optval;
return WTAP_OPTTYPE_SUCCESS;
}
static wtap_opttype_return_val
wtap_block_get_nth_option_common(wtap_block_t block, guint option_id, wtap_opttype_e type, guint idx, wtap_optval_t **optvalp)
{
const wtap_opttype_t *opttype;
wtap_optval_t *optval;
if (block == NULL) {
return WTAP_OPTTYPE_BAD_BLOCK;
}
opttype = GET_OPTION_TYPE(block->info->options, option_id);
if (opttype == NULL) {
/* There's no option for this block with that option ID */
return WTAP_OPTTYPE_NO_SUCH_OPTION;
}
/*
* Is this an option of the specified data type?
*/
if (opttype->data_type != type) {
/*
* No.
*/
return WTAP_OPTTYPE_TYPE_MISMATCH;
}
/*
* Can there be more than one instance of this option?
*/
if (!(opttype->flags & WTAP_OPTTYPE_FLAG_MULTIPLE_ALLOWED)) {
/*
* No.
*/
return WTAP_OPTTYPE_NUMBER_MISMATCH;
}
optval = wtap_block_get_nth_option(block, option_id, idx);
if (optval == NULL) {
/* Didn't find the option */
return WTAP_OPTTYPE_NOT_FOUND;
}
*optvalp = optval;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_uint8_option(wtap_block_t block, guint option_id, guint8 value)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_UINT8, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.uint8val = value;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_uint8_option_value(wtap_block_t block, guint option_id, guint8 value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_UINT8, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
optval->uint8val = value;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_uint8_option_value(wtap_block_t block, guint option_id, guint8* value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_UINT8, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->uint8val;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_uint32_option(wtap_block_t block, guint option_id, guint32 value)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_UINT32, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.uint32val = value;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_uint32_option_value(wtap_block_t block, guint option_id, guint32 value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_UINT32, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
optval->uint32val = value;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_uint32_option_value(wtap_block_t block, guint option_id, guint32* value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_UINT32, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->uint32val;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_uint64_option(wtap_block_t block, guint option_id, guint64 value)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_UINT64, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.uint64val = value;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_uint64_option_value(wtap_block_t block, guint option_id, guint64 value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_UINT64, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
optval->uint64val = value;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_uint64_option_value(wtap_block_t block, guint option_id, guint64 *value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_UINT64, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->uint64val;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_ipv4_option(wtap_block_t block, guint option_id, guint32 value)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_IPv4, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.ipv4val = value;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_ipv4_option_value(wtap_block_t block, guint option_id, guint32 value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_IPv4, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
optval->ipv4val = value;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_ipv4_option_value(wtap_block_t block, guint option_id, guint32* value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_IPv4, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->ipv4val;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_ipv6_option(wtap_block_t block, guint option_id, ws_in6_addr *value)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_IPv6, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.ipv6val = *value;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_ipv6_option_value(wtap_block_t block, guint option_id, ws_in6_addr *value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_IPv6, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
optval->ipv6val = *value;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_ipv6_option_value(wtap_block_t block, guint option_id, ws_in6_addr* value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_IPv6, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->ipv6val;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_string_option(wtap_block_t block, guint option_id, const char *value, gsize value_length)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_STRING, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.stringval = g_strndup(value, value_length);
WS_UTF_8_CHECK(opt->value.stringval, -1);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_string_option_owned(wtap_block_t block, guint option_id, char *value)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_STRING, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.stringval = value;
WS_UTF_8_CHECK(opt->value.stringval, -1);
return WTAP_OPTTYPE_SUCCESS;
}
static wtap_opttype_return_val
wtap_block_add_string_option_vformat(wtap_block_t block, guint option_id, const char *format, va_list va)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_STRING, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.stringval = ws_strdup_vprintf(format, va);
WS_UTF_8_CHECK(opt->value.stringval, -1);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_string_option_format(wtap_block_t block, guint option_id, const char *format, ...)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
va_list va;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_STRING, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
va_start(va, format);
opt->value.stringval = ws_strdup_vprintf(format, va);
va_end(va);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_string_option_value(wtap_block_t block, guint option_id, const char *value, size_t value_length)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_STRING, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS) {
if (ret == WTAP_OPTTYPE_NOT_FOUND) {
/*
* There's no instance to set, so just try to create a new one
* with the value.
*/
return wtap_block_add_string_option(block, option_id, value, value_length);
}
/* Otherwise fail. */
return ret;
}
g_free(optval->stringval);
optval->stringval = g_strndup(value, value_length);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_nth_string_option_value(wtap_block_t block, guint option_id, guint idx, const char *value, size_t value_length)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_nth_option_common(block, option_id, WTAP_OPTTYPE_STRING, idx, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
g_free(optval->stringval);
optval->stringval = g_strndup(value, value_length);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_string_option_value_format(wtap_block_t block, guint option_id, const char *format, ...)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
va_list va;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_STRING, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS) {
if (ret == WTAP_OPTTYPE_NOT_FOUND) {
/*
* There's no instance to set, so just try to create a new one
* with the formatted string.
*/
va_start(va, format);
ret = wtap_block_add_string_option_vformat(block, option_id, format, va);
va_end(va);
return ret;
}
/* Otherwise fail. */
return ret;
}
g_free(optval->stringval);
va_start(va, format);
optval->stringval = ws_strdup_vprintf(format, va);
va_end(va);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_nth_string_option_value_format(wtap_block_t block, guint option_id, guint idx, const char *format, ...)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
va_list va;
ret = wtap_block_get_nth_option_common(block, option_id, WTAP_OPTTYPE_STRING, idx, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
g_free(optval->stringval);
va_start(va, format);
optval->stringval = ws_strdup_vprintf(format, va);
va_end(va);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_string_option_value(wtap_block_t block, guint option_id, char** value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_STRING, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->stringval;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_nth_string_option_value(wtap_block_t block, guint option_id, guint idx, char** value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_nth_option_common(block, option_id, WTAP_OPTTYPE_STRING, idx, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->stringval;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_bytes_option(wtap_block_t block, guint option_id, const guint8 *value, gsize value_length)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_BYTES, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.byteval = g_bytes_new(value, value_length);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_bytes_option_borrow(wtap_block_t block, guint option_id, GBytes *value)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_BYTES, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.byteval = g_bytes_ref(value);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_bytes_option_value(wtap_block_t block, guint option_id, const guint8 *value, size_t value_length)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_BYTES, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS) {
if (ret == WTAP_OPTTYPE_NOT_FOUND) {
/*
* There's no instance to set, so just try to create a new one
* with the value.
*/
return wtap_block_add_bytes_option(block, option_id, value, value_length);
}
/* Otherwise fail. */
return ret;
}
g_bytes_unref(optval->byteval);
optval->byteval = g_bytes_new(value, value_length);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_nth_bytes_option_value(wtap_block_t block, guint option_id, guint idx, GBytes *value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_nth_option_common(block, option_id, WTAP_OPTTYPE_BYTES, idx, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
g_bytes_unref(optval->byteval);
optval->byteval = g_bytes_ref(value);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_bytes_option_value(wtap_block_t block, guint option_id, GBytes** value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_BYTES, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->byteval;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_nth_bytes_option_value(wtap_block_t block, guint option_id, guint idx, GBytes** value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_nth_option_common(block, option_id, WTAP_OPTTYPE_BYTES, idx, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->byteval;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_nflx_custom_option(wtap_block_t block, guint32 type, const char *custom_data, gsize custom_data_len)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, OPT_CUSTOM_BIN_COPY, WTAP_OPTTYPE_CUSTOM, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.custom_opt.pen = PEN_NFLX;
opt->value.custom_opt.data.nflx_data.type = type;
opt->value.custom_opt.data.nflx_data.custom_data_len = custom_data_len;
opt->value.custom_opt.data.nflx_data.custom_data = g_memdup2(custom_data, custom_data_len);
opt->value.custom_opt.data.nflx_data.use_little_endian = (block->info->block_type == WTAP_BLOCK_CUSTOM);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_nflx_custom_option(wtap_block_t block, guint32 nflx_type, char *nflx_custom_data _U_, gsize nflx_custom_data_len)
{
const wtap_opttype_t *opttype;
wtap_option_t *opt;
guint i;
if (block == NULL) {
return WTAP_OPTTYPE_BAD_BLOCK;
}
opttype = GET_OPTION_TYPE(block->info->options, OPT_CUSTOM_BIN_COPY);
if (opttype == NULL) {
return WTAP_OPTTYPE_NO_SUCH_OPTION;
}
if (opttype->data_type != WTAP_OPTTYPE_CUSTOM) {
return WTAP_OPTTYPE_TYPE_MISMATCH;
}
for (i = 0; i < block->options->len; i++) {
opt = &g_array_index(block->options, wtap_option_t, i);
if ((opt->option_id == OPT_CUSTOM_BIN_COPY) &&
(opt->value.custom_opt.pen == PEN_NFLX) &&
(opt->value.custom_opt.data.nflx_data.type == nflx_type)) {
break;
}
}
if (i == block->options->len) {
return WTAP_OPTTYPE_NOT_FOUND;
}
if (nflx_custom_data_len < opt->value.custom_opt.data.nflx_data.custom_data_len) {
return WTAP_OPTTYPE_TYPE_MISMATCH;
}
switch (nflx_type) {
case NFLX_OPT_TYPE_VERSION: {
guint32 *src, *dst;
ws_assert(nflx_custom_data_len == sizeof(guint32));
src = (guint32 *)opt->value.custom_opt.data.nflx_data.custom_data;
dst = (guint32 *)nflx_custom_data;
*dst = GUINT32_FROM_LE(*src);
break;
}
case NFLX_OPT_TYPE_TCPINFO: {
struct nflx_tcpinfo *src, *dst;
ws_assert(nflx_custom_data_len == sizeof(struct nflx_tcpinfo));
src = (struct nflx_tcpinfo *)opt->value.custom_opt.data.nflx_data.custom_data;
dst = (struct nflx_tcpinfo *)nflx_custom_data;
dst->tlb_tv_sec = GUINT64_FROM_LE(src->tlb_tv_sec);
dst->tlb_tv_usec = GUINT64_FROM_LE(src->tlb_tv_usec);
dst->tlb_ticks = GUINT32_FROM_LE(src->tlb_ticks);
dst->tlb_sn = GUINT32_FROM_LE(src->tlb_sn);
dst->tlb_stackid = src->tlb_stackid;
dst->tlb_eventid = src->tlb_eventid;
dst->tlb_eventflags = GUINT16_FROM_LE(src->tlb_eventflags);
dst->tlb_errno = GINT32_FROM_LE(src->tlb_errno);
dst->tlb_rxbuf_tls_sb_acc = GUINT32_FROM_LE(src->tlb_rxbuf_tls_sb_acc);
dst->tlb_rxbuf_tls_sb_ccc = GUINT32_FROM_LE(src->tlb_rxbuf_tls_sb_ccc);
dst->tlb_rxbuf_tls_sb_spare = GUINT32_FROM_LE(src->tlb_rxbuf_tls_sb_spare);
dst->tlb_txbuf_tls_sb_acc = GUINT32_FROM_LE(src->tlb_txbuf_tls_sb_acc);
dst->tlb_txbuf_tls_sb_ccc = GUINT32_FROM_LE(src->tlb_txbuf_tls_sb_ccc);
dst->tlb_txbuf_tls_sb_spare = GUINT32_FROM_LE(src->tlb_txbuf_tls_sb_spare);
dst->tlb_state = GINT32_FROM_LE(src->tlb_state);
dst->tlb_starttime = GUINT32_FROM_LE(src->tlb_starttime);
dst->tlb_iss = GUINT32_FROM_LE(src->tlb_iss);
dst->tlb_flags = GUINT32_FROM_LE(src->tlb_flags);
dst->tlb_snd_una = GUINT32_FROM_LE(src->tlb_snd_una);
dst->tlb_snd_max = GUINT32_FROM_LE(src->tlb_snd_max);
dst->tlb_snd_cwnd = GUINT32_FROM_LE(src->tlb_snd_cwnd);
dst->tlb_snd_nxt = GUINT32_FROM_LE(src->tlb_snd_nxt);
dst->tlb_snd_recover = GUINT32_FROM_LE(src->tlb_snd_recover);
dst->tlb_snd_wnd = GUINT32_FROM_LE(src->tlb_snd_wnd);
dst->tlb_snd_ssthresh = GUINT32_FROM_LE(src->tlb_snd_ssthresh);
dst->tlb_srtt = GUINT32_FROM_LE(src->tlb_srtt);
dst->tlb_rttvar = GUINT32_FROM_LE(src->tlb_rttvar);
dst->tlb_rcv_up = GUINT32_FROM_LE(src->tlb_rcv_up);
dst->tlb_rcv_adv = GUINT32_FROM_LE(src->tlb_rcv_adv);
dst->tlb_flags2 = GUINT32_FROM_LE(src->tlb_flags2);
dst->tlb_rcv_nxt = GUINT32_FROM_LE(src->tlb_rcv_nxt);
dst->tlb_rcv_wnd = GUINT32_FROM_LE(src->tlb_rcv_wnd);
dst->tlb_dupacks = GUINT32_FROM_LE(src->tlb_dupacks);
dst->tlb_segqlen = GINT32_FROM_LE(src->tlb_segqlen);
dst->tlb_snd_numholes = GINT32_FROM_LE(src->tlb_snd_numholes);
dst->tlb_flex1 = GUINT32_FROM_LE(src->tlb_flex1);
dst->tlb_flex2 = GUINT32_FROM_LE(src->tlb_flex2);
dst->tlb_fbyte_in = GUINT32_FROM_LE(src->tlb_fbyte_in);
dst->tlb_fbyte_out = GUINT32_FROM_LE(src->tlb_fbyte_out);
dst->tlb_snd_scale = src->tlb_snd_scale;
dst->tlb_rcv_scale = src->tlb_rcv_scale;
for (i = 0; i < 3; i++) {
dst->_pad[i] = src->_pad[i];
}
dst->tlb_stackinfo_bbr_cur_del_rate = GUINT64_FROM_LE(src->tlb_stackinfo_bbr_cur_del_rate);
dst->tlb_stackinfo_bbr_delRate = GUINT64_FROM_LE(src->tlb_stackinfo_bbr_delRate);
dst->tlb_stackinfo_bbr_rttProp = GUINT64_FROM_LE(src->tlb_stackinfo_bbr_rttProp);
dst->tlb_stackinfo_bbr_bw_inuse = GUINT64_FROM_LE(src->tlb_stackinfo_bbr_bw_inuse);
dst->tlb_stackinfo_bbr_inflight = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_inflight);
dst->tlb_stackinfo_bbr_applimited = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_applimited);
dst->tlb_stackinfo_bbr_delivered = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_delivered);
dst->tlb_stackinfo_bbr_timeStamp = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_timeStamp);
dst->tlb_stackinfo_bbr_epoch = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_epoch);
dst->tlb_stackinfo_bbr_lt_epoch = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_lt_epoch);
dst->tlb_stackinfo_bbr_pkts_out = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_pkts_out);
dst->tlb_stackinfo_bbr_flex1 = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_flex1);
dst->tlb_stackinfo_bbr_flex2 = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_flex2);
dst->tlb_stackinfo_bbr_flex3 = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_flex3);
dst->tlb_stackinfo_bbr_flex4 = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_flex4);
dst->tlb_stackinfo_bbr_flex5 = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_flex5);
dst->tlb_stackinfo_bbr_flex6 = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_flex6);
dst->tlb_stackinfo_bbr_lost = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_lost);
dst->tlb_stackinfo_bbr_pacing_gain = GUINT16_FROM_LE(src->tlb_stackinfo_bbr_lost);
dst->tlb_stackinfo_bbr_cwnd_gain = GUINT16_FROM_LE(src->tlb_stackinfo_bbr_lost);
dst->tlb_stackinfo_bbr_flex7 = GUINT16_FROM_LE(src->tlb_stackinfo_bbr_flex7);
dst->tlb_stackinfo_bbr_bbr_state = src->tlb_stackinfo_bbr_bbr_state;
dst->tlb_stackinfo_bbr_bbr_substate = src->tlb_stackinfo_bbr_bbr_substate;
dst->tlb_stackinfo_bbr_inhpts = src->tlb_stackinfo_bbr_inhpts;
dst->tlb_stackinfo_bbr_ininput = src->tlb_stackinfo_bbr_ininput;
dst->tlb_stackinfo_bbr_use_lt_bw = src->tlb_stackinfo_bbr_use_lt_bw;
dst->tlb_stackinfo_bbr_flex8 = src->tlb_stackinfo_bbr_flex8;
dst->tlb_stackinfo_bbr_pkt_epoch = GUINT32_FROM_LE(src->tlb_stackinfo_bbr_pkt_epoch);
dst->tlb_len = GUINT32_FROM_LE(src->tlb_len);
break;
}
case NFLX_OPT_TYPE_DUMPINFO: {
struct nflx_dumpinfo *src, *dst;
ws_assert(nflx_custom_data_len == sizeof(struct nflx_dumpinfo));
src = (struct nflx_dumpinfo *)opt->value.custom_opt.data.nflx_data.custom_data;
dst = (struct nflx_dumpinfo *)nflx_custom_data;
dst->tlh_version = GUINT32_FROM_LE(src->tlh_version);
dst->tlh_type = GUINT32_FROM_LE(src->tlh_type);
dst->tlh_length = GUINT64_FROM_LE(src->tlh_length);
dst->tlh_ie_fport = src->tlh_ie_fport;
dst->tlh_ie_lport = src->tlh_ie_lport;
for (i = 0; i < 4; i++) {
dst->tlh_ie_faddr_addr32[i] = src->tlh_ie_faddr_addr32[i];
dst->tlh_ie_laddr_addr32[i] = src->tlh_ie_laddr_addr32[i];
}
dst->tlh_ie_zoneid = src->tlh_ie_zoneid;
dst->tlh_offset_tv_sec = GUINT64_FROM_LE(src->tlh_offset_tv_sec);
dst->tlh_offset_tv_usec = GUINT64_FROM_LE(src->tlh_offset_tv_usec);
memcpy(dst->tlh_id, src->tlh_id, 64);
memcpy(dst->tlh_reason, src->tlh_reason, 32);
memcpy(dst->tlh_tag, src->tlh_tag, 32);
dst->tlh_af = src->tlh_af;
memcpy(dst->_pad, src->_pad, 7);
break;
}
case NFLX_OPT_TYPE_DUMPTIME: {
guint64 *src, *dst;
ws_assert(nflx_custom_data_len == sizeof(guint64));
src = (guint64 *)opt->value.custom_opt.data.nflx_data.custom_data;
dst = (guint64 *)nflx_custom_data;
*dst = GUINT64_FROM_LE(*src);
break;
}
case NFLX_OPT_TYPE_STACKNAME:
ws_assert(nflx_custom_data_len >= 2);
memcpy(nflx_custom_data, opt->value.custom_opt.data.nflx_data.custom_data, nflx_custom_data_len);
break;
default:
return WTAP_OPTTYPE_NOT_FOUND;
}
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_custom_option(wtap_block_t block, guint option_id, guint32 pen, const char *custom_data, gsize custom_data_len)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_CUSTOM, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.custom_opt.pen = pen;
opt->value.custom_opt.data.generic_data.custom_data_len = custom_data_len;
opt->value.custom_opt.data.generic_data.custom_data = g_memdup2(custom_data, custom_data_len);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_if_filter_option(wtap_block_t block, guint option_id, if_filter_opt_t* value)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_IF_FILTER, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.if_filterval = if_filter_dup(value);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_if_filter_option_value(wtap_block_t block, guint option_id, if_filter_opt_t* value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
if_filter_opt_t prev_value;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_IF_FILTER, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
prev_value = optval->if_filterval;
optval->if_filterval = if_filter_dup(value);
/* Free after memory is duplicated in case structure was manipulated with a "get then set" */
if_filter_free(&prev_value);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_if_filter_option_value(wtap_block_t block, guint option_id, if_filter_opt_t* value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_option_common(block, option_id, WTAP_OPTTYPE_IF_FILTER, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->if_filterval;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_packet_verdict_option(wtap_block_t block, guint option_id, packet_verdict_opt_t* value)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_PACKET_VERDICT, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.packet_verdictval = packet_verdict_dup(value);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_set_nth_packet_verdict_option_value(wtap_block_t block, guint option_id, guint idx, packet_verdict_opt_t* value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
packet_verdict_opt_t prev_value;
ret = wtap_block_get_nth_option_common(block, option_id, WTAP_OPTTYPE_PACKET_VERDICT, idx, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
prev_value = optval->packet_verdictval;
optval->packet_verdictval = packet_verdict_dup(value);
/* Free after memory is duplicated in case structure was manipulated with a "get then set" */
wtap_packet_verdict_free(&prev_value);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_get_nth_packet_verdict_option_value(wtap_block_t block, guint option_id, guint idx, packet_verdict_opt_t* value)
{
wtap_opttype_return_val ret;
wtap_optval_t *optval;
ret = wtap_block_get_nth_option_common(block, option_id, WTAP_OPTTYPE_STRING, idx, &optval);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
*value = optval->packet_verdictval;
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_add_packet_hash_option(wtap_block_t block, guint option_id, packet_hash_opt_t* value)
{
wtap_opttype_return_val ret;
wtap_option_t *opt;
ret = wtap_block_add_option_common(block, option_id, WTAP_OPTTYPE_PACKET_HASH, &opt);
if (ret != WTAP_OPTTYPE_SUCCESS)
return ret;
opt->value.packet_hash = packet_hash_dup(value);
return WTAP_OPTTYPE_SUCCESS;
}
wtap_opttype_return_val
wtap_block_remove_option(wtap_block_t block, guint option_id)
{
const wtap_opttype_t *opttype;
guint i;
wtap_option_t *opt;
if (block == NULL) {
return WTAP_OPTTYPE_BAD_BLOCK;
}
opttype = GET_OPTION_TYPE(block->info->options, option_id);
if (opttype == NULL) {
/* There's no option for this block with that option ID */
return WTAP_OPTTYPE_NO_SUCH_OPTION;
}
/*
* Can there be more than one instance of this option?
*/
if (opttype->flags & WTAP_OPTTYPE_FLAG_MULTIPLE_ALLOWED) {
/*
* Yes. You can't remove "the" value.
*/
return WTAP_OPTTYPE_NUMBER_MISMATCH;
}
for (i = 0; i < block->options->len; i++) {
opt = &g_array_index(block->options, wtap_option_t, i);
if (opt->option_id == option_id) {
/* Found it - free up the value */
wtap_block_free_option(block, opt);
/* Remove the option from the array of options */
g_array_remove_index(block->options, i);
return WTAP_OPTTYPE_SUCCESS;
}
}
/* Didn't find the option */
return WTAP_OPTTYPE_NOT_FOUND;
}
wtap_opttype_return_val
wtap_block_remove_nth_option_instance(wtap_block_t block, guint option_id,
guint idx)
{
const wtap_opttype_t *opttype;
guint i;
wtap_option_t *opt;
guint opt_idx;
if (block == NULL) {
return WTAP_OPTTYPE_BAD_BLOCK;
}
opttype = GET_OPTION_TYPE(block->info->options, option_id);
if (opttype == NULL) {
/* There's no option for this block with that option ID */
return WTAP_OPTTYPE_NO_SUCH_OPTION;
}
/*
* Can there be more than one instance of this option?
*/
if (!(opttype->flags & WTAP_OPTTYPE_FLAG_MULTIPLE_ALLOWED)) {
/*
* No.
*/
return WTAP_OPTTYPE_NUMBER_MISMATCH;
}
opt_idx = 0;
for (i = 0; i < block->options->len; i++) {
opt = &g_array_index(block->options, wtap_option_t, i);
if (opt->option_id == option_id) {
if (opt_idx == idx) {
/* Found it - free up the value */
wtap_block_free_option(block, opt);
/* Remove the option from the array of options */
g_array_remove_index(block->options, i);
return WTAP_OPTTYPE_SUCCESS;
}
opt_idx++;
}
}
/* Didn't find the option */
return WTAP_OPTTYPE_NOT_FOUND;
}
static void shb_create(wtap_block_t block)
{
wtapng_section_mandatory_t* section_mand = g_new(wtapng_section_mandatory_t, 1);
section_mand->section_length = -1;
block->mandatory_data = section_mand;
}
static void shb_copy_mand(wtap_block_t dest_block, wtap_block_t src_block)
{
memcpy(dest_block->mandatory_data, src_block->mandatory_data, sizeof(wtapng_section_mandatory_t));
}
static void nrb_create(wtap_block_t block)
{
block->mandatory_data = g_new0(wtapng_nrb_mandatory_t, 1);
}
static void nrb_free_mand(wtap_block_t block)
{
wtapng_nrb_mandatory_t *mand = (wtapng_nrb_mandatory_t *)block->mandatory_data;
g_list_free_full(mand->ipv4_addr_list, g_free);
g_list_free_full(mand->ipv6_addr_list, g_free);
}
#if 0
static gpointer copy_hashipv4(gconstpointer src, gpointer user_data _U_
{
hashipv4_t *src_ipv4 = (hashipv4_t*)src;
hashipv4_t *dst = g_new0(hashipv4_t, 1);
dst->addr = src_ipv4->addr;
(void) g_strlcpy(dst->name, src_ipv4->name, MAXNAMELEN);
return dst;
}
static gpointer copy_hashipv4(gconstpointer src, gpointer user_data _U_
{
hashipv6_t *src_ipv6 = (hashipv6_t*)src;
hashipv6_t *dst = g_new0(hashipv6_t, 1);
dst->addr = src_ipv4->addr;
(void) g_strlcpy(dst->name, src_ipv4->name, MAXNAMELEN);
return dst;
}
static void nrb_copy_mand(wtap_block_t dest_block, wtap_block_t src_block)
{
wtapng_nrb_mandatory_t *src = (wtapng_nrb_mandatory_t *)src_block->mandatory_data;
wtapng_nrb_mandatory_t *dst = (wtapng_nrb_mandatory_t *)dest_block->mandatory_data;
g_list_free_full(dst->ipv4_addr_list, g_free);
g_list_free_full(dst->ipv6_addr_list, g_free);
dst->ipv4_addr_list = g_list_copy_deep(src->ipv4_addr_list, copy_hashipv4, NULL);
dst->ipv6_addr_list = g_list_copy_deep(src->ipv6_addr_list, copy_hashipv6, NULL);
}
#endif
static void isb_create(wtap_block_t block)
{
block->mandatory_data = g_new0(wtapng_if_stats_mandatory_t, 1);
}
static void isb_copy_mand(wtap_block_t dest_block, wtap_block_t src_block)
{
memcpy(dest_block->mandatory_data, src_block->mandatory_data, sizeof(wtapng_if_stats_mandatory_t));
}
static void idb_create(wtap_block_t block)
{
block->mandatory_data = g_new0(wtapng_if_descr_mandatory_t, 1);
}
static void idb_free_mand(wtap_block_t block)
{
guint j;
wtap_block_t if_stats;
wtapng_if_descr_mandatory_t* mand = (wtapng_if_descr_mandatory_t*)block->mandatory_data;
for(j = 0; j < mand->num_stat_entries; j++) {
if_stats = g_array_index(mand->interface_statistics, wtap_block_t, j);
wtap_block_unref(if_stats);
}
if (mand->interface_statistics)
g_array_free(mand->interface_statistics, TRUE);
}
static void idb_copy_mand(wtap_block_t dest_block, wtap_block_t src_block)
{
guint j;
wtap_block_t src_if_stats, dest_if_stats;
wtapng_if_descr_mandatory_t *src_mand = (wtapng_if_descr_mandatory_t*)src_block->mandatory_data,
*dest_mand = (wtapng_if_descr_mandatory_t*)dest_block->mandatory_data;
/* Need special consideration for copying of the interface_statistics member */
if (dest_mand->num_stat_entries != 0)
g_array_free(dest_mand->interface_statistics, TRUE);
memcpy(dest_mand, src_mand, sizeof(wtapng_if_descr_mandatory_t));
if (src_mand->num_stat_entries != 0)
{
dest_mand->interface_statistics = g_array_new(FALSE, FALSE, sizeof(wtap_block_t));
for (j = 0; j < src_mand->num_stat_entries; j++)
{
src_if_stats = g_array_index(src_mand->interface_statistics, wtap_block_t, j);
dest_if_stats = wtap_block_make_copy(src_if_stats);
dest_mand->interface_statistics = g_array_append_val(dest_mand->interface_statistics, dest_if_stats);
}
}
}
static void dsb_create(wtap_block_t block)
{
block->mandatory_data = g_new0(wtapng_dsb_mandatory_t, 1);
}
static void dsb_free_mand(wtap_block_t block)
{
wtapng_dsb_mandatory_t *mand = (wtapng_dsb_mandatory_t *)block->mandatory_data;
g_free(mand->secrets_data);
}
static void dsb_copy_mand(wtap_block_t dest_block, wtap_block_t src_block)
{
wtapng_dsb_mandatory_t *src = (wtapng_dsb_mandatory_t *)src_block->mandatory_data;
wtapng_dsb_mandatory_t *dst = (wtapng_dsb_mandatory_t *)dest_block->mandatory_data;
dst->secrets_type = src->secrets_type;
dst->secrets_len = src->secrets_len;
g_free(dst->secrets_data);
dst->secrets_data = (guint8 *)g_memdup2(src->secrets_data, src->secrets_len);
}
static void pkt_create(wtap_block_t block)
{
/* Commented out for now, there's no mandatory data that isn't handled by
* Wireshark in other ways.
*/
//block->mandatory_data = g_new0(wtapng_packet_mandatory_t, 1);
/* Ensure this is null, so when g_free is called on it, it simply returns */
block->mandatory_data = NULL;
}
static void sjeb_create(wtap_block_t block)
{
/* Ensure this is null, so when g_free is called on it, it simply returns */
block->mandatory_data = NULL;
}
static void cb_create(wtap_block_t block)
{
/* Ensure this is null, so when g_free is called on it, it simply returns */
block->mandatory_data = NULL;
}
void wtap_opttypes_initialize(void)
{
static wtap_blocktype_t shb_block = {
WTAP_BLOCK_SECTION, /* block_type */
"SHB", /* name */
"Section Header Block", /* description */
shb_create, /* create */
NULL, /* free_mand */
shb_copy_mand, /* copy_mand */
NULL /* options */
};
static const wtap_opttype_t shb_hardware = {
"hardware",
"SHB Hardware",
WTAP_OPTTYPE_STRING,
0
};
static const wtap_opttype_t shb_os = {
"os",
"SHB Operating System",
WTAP_OPTTYPE_STRING,
0
};
static const wtap_opttype_t shb_userappl = {
"user_appl",
"SHB User Application",
WTAP_OPTTYPE_STRING,
0
};
static wtap_blocktype_t idb_block = {
WTAP_BLOCK_IF_ID_AND_INFO, /* block_type */
"IDB", /* name */
"Interface Description Block", /* description */
idb_create, /* create */
idb_free_mand, /* free_mand */
idb_copy_mand, /* copy_mand */
NULL /* options */
};
static const wtap_opttype_t if_name = {
"name",
"IDB Name",
WTAP_OPTTYPE_STRING,
0
};
static const wtap_opttype_t if_description = {
"description",
"IDB Description",
WTAP_OPTTYPE_STRING,
0
};
static const wtap_opttype_t if_speed = {
"speed",
"IDB Speed",
WTAP_OPTTYPE_UINT64,
0
};
static const wtap_opttype_t if_tsresol = {
"tsresol",
"IDB Time Stamp Resolution",
WTAP_OPTTYPE_UINT8, /* XXX - signed? */
0
};
static const wtap_opttype_t if_filter = {
"filter",
"IDB Filter",
WTAP_OPTTYPE_IF_FILTER,
0
};
static const wtap_opttype_t if_os = {
"os",
"IDB Operating System",
WTAP_OPTTYPE_STRING,
0
};
static const wtap_opttype_t if_fcslen = {
"fcslen",
"IDB FCS Length",
WTAP_OPTTYPE_UINT8,
0
};
static const wtap_opttype_t if_hardware = {
"hardware",
"IDB Hardware",
WTAP_OPTTYPE_STRING,
0
};
static wtap_blocktype_t dsb_block = {
WTAP_BLOCK_DECRYPTION_SECRETS,
"DSB",
"Decryption Secrets Block",
dsb_create,
dsb_free_mand,
dsb_copy_mand,
NULL
};
static wtap_blocktype_t nrb_block = {
WTAP_BLOCK_NAME_RESOLUTION, /* block_type */
"NRB", /* name */
"Name Resolution Block", /* description */
nrb_create, /* create */
nrb_free_mand, /* free_mand */
/* We eventually want to copy these, when dumper actually
* writes them out. If we're actually processing packets,
* as opposed to just reading and writing a file without
* printing (e.g., editcap), do we still want to copy all
* the pre-existing NRBs, or do we want to limit it to
* the actually used addresses, as currently?
*/
#if 0
nrb_copy_mand, /* copy_mand */
#endif
NULL,
NULL /* options */
};
static const wtap_opttype_t ns_dnsname = {
"dnsname",
"NRB DNS server name",
WTAP_OPTTYPE_STRING,
0
};
static const wtap_opttype_t ns_dnsIP4addr = {
"dnsIP4addr",
"NRB DNS server IPv4 address",
WTAP_OPTTYPE_IPv4,
0
};
static const wtap_opttype_t ns_dnsIP6addr = {
"dnsIP6addr",
"NRB DNS server IPv6 address",
WTAP_OPTTYPE_IPv6,
0
};
static wtap_blocktype_t isb_block = {
WTAP_BLOCK_IF_STATISTICS, /* block_type */
"ISB", /* name */
"Interface Statistics Block", /* description */
isb_create, /* create */
NULL, /* free_mand */
isb_copy_mand, /* copy_mand */
NULL /* options */
};
static const wtap_opttype_t isb_starttime = {
"starttime",
"ISB Start Time",
WTAP_OPTTYPE_UINT64,
0
};
static const wtap_opttype_t isb_endtime = {
"endtime",
"ISB End Time",
WTAP_OPTTYPE_UINT64,
0
};
static const wtap_opttype_t isb_ifrecv = {
"ifrecv",
"ISB Received Packets",
WTAP_OPTTYPE_UINT64,
0
};
static const wtap_opttype_t isb_ifdrop = {
"ifdrop",
"ISB Dropped Packets",
WTAP_OPTTYPE_UINT64,
0
};
static const wtap_opttype_t isb_filteraccept = {
"filteraccept",
"ISB Packets Accepted By Filter",
WTAP_OPTTYPE_UINT64,
0
};
static const wtap_opttype_t isb_osdrop = {
"osdrop",
"ISB Packets Dropped By The OS",
WTAP_OPTTYPE_UINT64,
0
};
static const wtap_opttype_t isb_usrdeliv = {
"usrdeliv",
"ISB Packets Delivered To The User",
WTAP_OPTTYPE_UINT64,
0
};
static wtap_blocktype_t pkt_block = {
WTAP_BLOCK_PACKET, /* block_type */
"EPB/SPB/PB", /* name */
"Packet Block", /* description */
pkt_create, /* create */
NULL, /* free_mand */
NULL, /* copy_mand */
NULL /* options */
};
static const wtap_opttype_t pkt_flags = {
"flags",
"Link-layer flags",
WTAP_OPTTYPE_UINT32,
0
};
static const wtap_opttype_t pkt_dropcount = {
"dropcount",
"Packets Dropped since last packet",
WTAP_OPTTYPE_UINT64,
0
};
static const wtap_opttype_t pkt_id = {
"packetid",
"Unique Packet Identifier",
WTAP_OPTTYPE_UINT64,
0
};
static const wtap_opttype_t pkt_queue = {
"queue",
"Queue ID in which packet was received",
WTAP_OPTTYPE_UINT32,
0
};
static const wtap_opttype_t pkt_hash = {
"hash",
"Hash of packet data",
WTAP_OPTTYPE_PACKET_HASH,
WTAP_OPTTYPE_FLAG_MULTIPLE_ALLOWED
};
static const wtap_opttype_t pkt_verdict = {
"verdict",
"Packet Verdict",
WTAP_OPTTYPE_PACKET_VERDICT,
WTAP_OPTTYPE_FLAG_MULTIPLE_ALLOWED
};
static wtap_blocktype_t journal_block = {
WTAP_BLOCK_SYSTEMD_JOURNAL_EXPORT, /* block_type */
"SJEB", /* name */
"systemd Journal Export Block", /* description */
sjeb_create, /* create */
NULL, /* free_mand */
NULL, /* copy_mand */
NULL /* options */
};
static wtap_blocktype_t cb_block = {
WTAP_BLOCK_CUSTOM, /* block_type */
"CB", /* name */
"Custom Block", /* description */
cb_create, /* create */
NULL, /* free_mand */
NULL, /* copy_mand */
NULL /* options */
};
/*
* Register the SHB and the options that can appear in it.
*/
wtap_opttype_block_register(&shb_block);
wtap_opttype_option_register(&shb_block, OPT_SHB_HARDWARE, &shb_hardware);
wtap_opttype_option_register(&shb_block, OPT_SHB_OS, &shb_os);
wtap_opttype_option_register(&shb_block, OPT_SHB_USERAPPL, &shb_userappl);
/*
* Register the IDB and the options that can appear in it.
*/
wtap_opttype_block_register(&idb_block);
wtap_opttype_option_register(&idb_block, OPT_IDB_NAME, &if_name);
wtap_opttype_option_register(&idb_block, OPT_IDB_DESCRIPTION, &if_description);
wtap_opttype_option_register(&idb_block, OPT_IDB_SPEED, &if_speed);
wtap_opttype_option_register(&idb_block, OPT_IDB_TSRESOL, &if_tsresol);
wtap_opttype_option_register(&idb_block, OPT_IDB_FILTER, &if_filter);
wtap_opttype_option_register(&idb_block, OPT_IDB_OS, &if_os);
wtap_opttype_option_register(&idb_block, OPT_IDB_FCSLEN, &if_fcslen);
wtap_opttype_option_register(&idb_block, OPT_IDB_HARDWARE, &if_hardware);
/*
* Register the NRB and the options that can appear in it.
*/
wtap_opttype_block_register(&nrb_block);
wtap_opttype_option_register(&nrb_block, OPT_NS_DNSNAME, &ns_dnsname);
wtap_opttype_option_register(&nrb_block, OPT_NS_DNSIP4ADDR, &ns_dnsIP4addr);
wtap_opttype_option_register(&nrb_block, OPT_NS_DNSIP6ADDR, &ns_dnsIP6addr);
/*
* Register the ISB and the options that can appear in it.
*/
wtap_opttype_block_register(&isb_block);
wtap_opttype_option_register(&isb_block, OPT_ISB_STARTTIME, &isb_starttime);
wtap_opttype_option_register(&isb_block, OPT_ISB_ENDTIME, &isb_endtime);
wtap_opttype_option_register(&isb_block, OPT_ISB_IFRECV, &isb_ifrecv);
wtap_opttype_option_register(&isb_block, OPT_ISB_IFDROP, &isb_ifdrop);
wtap_opttype_option_register(&isb_block, OPT_ISB_FILTERACCEPT, &isb_filteraccept);
wtap_opttype_option_register(&isb_block, OPT_ISB_OSDROP, &isb_osdrop);
wtap_opttype_option_register(&isb_block, OPT_ISB_USRDELIV, &isb_usrdeliv);
/*
* Register the DSB, currently no options are defined.
*/
wtap_opttype_block_register(&dsb_block);
/*
* Register EPB/SPB/PB and the options that can appear in it/them.
* NB: Simple Packet Blocks have no options.
* NB: obsolete Packet Blocks have dropcount as a mandatory member instead
* of an option.
*/
wtap_opttype_block_register(&pkt_block);
wtap_opttype_option_register(&pkt_block, OPT_PKT_FLAGS, &pkt_flags);
wtap_opttype_option_register(&pkt_block, OPT_PKT_DROPCOUNT, &pkt_dropcount);
wtap_opttype_option_register(&pkt_block, OPT_PKT_PACKETID, &pkt_id);
wtap_opttype_option_register(&pkt_block, OPT_PKT_QUEUE, &pkt_queue);
wtap_opttype_option_register(&pkt_block, OPT_PKT_HASH, &pkt_hash);
wtap_opttype_option_register(&pkt_block, OPT_PKT_VERDICT, &pkt_verdict);
/*
* Register the SJEB and the (no) options that can appear in it.
*/
wtap_opttype_block_register(&journal_block);
/*
* Register the CB and the options that can appear in it.
*/
wtap_opttype_block_register(&cb_block);
#ifdef DEBUG_COUNT_REFS
memset(blocks_active, 0, sizeof(blocks_active));
#endif
}
void wtap_opttypes_cleanup(void)
{
guint block_type;
#ifdef DEBUG_COUNT_REFS
guint i;
guint cellno;
guint bitno;
guint8 mask;
#endif /* DEBUG_COUNT_REFS */
for (block_type = (guint)WTAP_BLOCK_SECTION;
block_type < (guint)MAX_WTAP_BLOCK_TYPE_VALUE; block_type++) {
if (blocktype_list[block_type]) {
if (blocktype_list[block_type]->options)
g_hash_table_destroy(blocktype_list[block_type]->options);
blocktype_list[block_type] = NULL;
}
}
#ifdef DEBUG_COUNT_REFS
for (i = 0 ; i < block_count; i++) {
cellno = i / 8;
bitno = i % 8;
mask = 1 << bitno;
if ((blocks_active[cellno] & mask) == mask) {
wtap_debug("wtap_opttypes_cleanup: orphaned block #%d", i);
}
}
#endif /* DEBUG_COUNT_REFS */
} |
C/C++ | wireshark/wiretap/wtap_opttypes.h | /** @file
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2001 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef WTAP_OPT_TYPES_H
#define WTAP_OPT_TYPES_H
#include "ws_symbol_export.h"
#include <wsutil/inet_ipv4.h>
#include <wsutil/inet_ipv6.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* We use the pcapng option codes for option type values.
*/
/* Options for all blocks */
#define OPT_EOFOPT 0 /**< Appears in pcapng files, but not in blocks. */
#define OPT_COMMENT 1 /**< A UTF-8 string containing a human-readable comment. */
#define OPT_CUSTOM_STR_COPY 2988 /**< A custom option containing a UTF-8 string, copying allowed. */
#define OPT_CUSTOM_BIN_COPY 2989 /**< A custom option containing binary data, copying allowed. */
#define OPT_CUSTOM_STR_NO_COPY 19372 /**< A custom option containing a UTF-8 string, copying not allowed. */
#define OPT_CUSTOM_BIN_NO_COPY 19373 /**< A custom option containing binary data, copying not allowed. */
/* Section Header block (SHB) */
#define OPT_SHB_HARDWARE 2 /**< A UTF-8 string containing the description of the
* hardware used to create this section.
*/
#define OPT_SHB_OS 3 /**< A UTF-8 string containing the
* name of the operating system used to create this section.
*/
#define OPT_SHB_USERAPPL 4 /**< A UTF-8 string containing the
* name of the application used to create this section.
*/
/* Interface Description block (IDB) */
#define OPT_IDB_NAME 2 /**< A UTF-8 string containing the name
* of the device used to capture data.
* "eth0" / "\Device\NPF_{AD1CE675-96D0-47C5-ADD0-2504B9126B68}"
*/
#define OPT_IDB_DESCRIPTION 3 /**< A UTF-8 string containing the description
* of the device used to capture data.
* "Wi-Fi" / "Local Area Connection" /
* "Wireless Network Connection" /
* "First Ethernet Interface"
*/
#define OPT_IDB_IP4ADDR 4 /**< XXX: if_IPv4addr Interface network address and netmask.
* This option can be repeated multiple times within the same Interface Description Block
* when multiple IPv4 addresses are assigned to the interface.
* 192 168 1 1 255 255 255 0
*/
#define OPT_IDB_IP6ADDR 5 /**< XXX: if_IPv6addr Interface network address and prefix length (stored in the last byte).
* This option can be repeated multiple times within the same Interface
* Description Block when multiple IPv6 addresses are assigned to the interface.
* 2001:0db8:85a3:08d3:1319:8a2e:0370:7344/64 is written (in hex) as
* "20 01 0d b8 85 a3 08 d3 13 19 8a 2e 03 70 73 44 40"
*/
#define OPT_IDB_MACADDR 6 /**< XXX: if_MACaddr Interface Hardware MAC address (48 bits). */
#define OPT_IDB_EUIADDR 7 /**< XXX: if_EUIaddr Interface Hardware EUI address (64 bits) */
#define OPT_IDB_SPEED 8 /**< Interface speed (in bps). 100000000 for 100Mbps
*/
#define OPT_IDB_TSRESOL 9 /**< Resolution of timestamps. If the Most Significant Bit is equal to zero,
* the remaining bits indicates the resolution of the timestamp as a
* negative power of 10 (e.g. 6 means microsecond resolution, timestamps
* are the number of microseconds since 1/1/1970). If the Most Significant Bit
* is equal to one, the remaining bits indicates the resolution has a
* negative power of 2 (e.g. 10 means 1/1024 of second).
* If this option is not present, a resolution of 10^-6 is assumed
* (i.e. timestamps have the same resolution of the standard 'libpcap' timestamps).
*/
#define OPT_IDB_TZONE 10 /**< XXX: if_tzone Time zone for GMT support (TODO: specify better). */
#define OPT_IDB_FILTER 11 /**< The filter (e.g. "capture only TCP traffic") used to capture traffic.
* The first byte of the Option Data keeps a code of the filter used
* (e.g. if this is a libpcap string, or BPF bytecode, and more).
* More details about this format will be presented in Appendix XXX (TODO).
* (TODO: better use different options for different fields?
* e.g. if_filter_pcap, if_filter_bpf, ...) 00 "tcp port 23 and host 10.0.0.5"
*/
#define OPT_IDB_OS 12 /**< A UTF-8 string containing the name of the operating system of the
* machine in which this interface is installed.
* This can be different from the same information that can be
* contained by the Section Header Block
* (Section 3.1 (Section Header Block (mandatory))) because
* the capture can have been done on a remote machine.
* "Windows XP SP2" / "openSUSE 10.2"
*/
#define OPT_IDB_FCSLEN 13 /**< An integer value that specified the length of the
* Frame Check Sequence (in bits) for this interface.
* For link layers whose FCS length can change during time,
* the Packet Block Flags Word can be used (see Appendix A (Packet Block Flags Word))
*/
#define OPT_IDB_TSOFFSET 14 /**< XXX: A 64 bits integer value that specifies an offset (in seconds)
* that must be added to the timestamp of each packet to obtain
* the absolute timestamp of a packet. If the option is missing,
* the timestamps stored in the packet must be considered absolute
* timestamps. The time zone of the offset can be specified with the
* option if_tzone. TODO: won't a if_tsoffset_low for fractional
* second offsets be useful for highly synchronized capture systems?
*/
#define OPT_IDB_HARDWARE 15 /**< A UTF-8 string containing the description
* of the hardware of the device used
* to capture data.
* "Broadcom NetXtreme" /
* "Intel(R) PRO/1000 MT Network Connection" /
* "NETGEAR WNA1000Mv2 N150 Wireless USB Micro Adapter"
*/
/*
* These are the flags for an EPB, but we use them for all WTAP_BLOCK_PACKET
*/
#define OPT_PKT_FLAGS 2
#define OPT_PKT_HASH 3
#define OPT_PKT_DROPCOUNT 4
#define OPT_PKT_PACKETID 5
#define OPT_PKT_QUEUE 6
#define OPT_PKT_VERDICT 7
/* Name Resolution Block (NRB) */
#define OPT_NS_DNSNAME 2
#define OPT_NS_DNSIP4ADDR 3
#define OPT_NS_DNSIP6ADDR 4
/* Interface Statistics Block (ISB) */
#define OPT_ISB_STARTTIME 2
#define OPT_ISB_ENDTIME 3
#define OPT_ISB_IFRECV 4
#define OPT_ISB_IFDROP 5
#define OPT_ISB_FILTERACCEPT 6
#define OPT_ISB_OSDROP 7
#define OPT_ISB_USRDELIV 8
struct wtap_block;
typedef struct wtap_block *wtap_block_t;
/*
* Currently supported blocks; these are not the pcapng block type values
* for them, they're identifiers used internally, and more than one
* pcapng block type may use a given block type.
*
* Note that, in a given file format, this information won't necessarily
* appear in the form of blocks in the file, even though they're presented
* to the caller of libwiretap as blocks when reading and are presented
* by the caller of libwiretap as blocks when writing. See, for example,
* the iptrace file format, in which the interface name is given as part
* of the packet record header; we synthesize those blocks when reading
* (we don't currently support writing that format, but if we did, we'd
* get the interface name from the block and put it in the packet record
* header).
*
* WTAP_BLOCK_IF_ID_AND_INFO is a block that not only gives
* descriptive information about an interface but *also* assigns an
* ID to the interface, so that every packet has either an explicit
* or implicit interface ID indicating on which the packet arrived.
*
* It does *not* refer to information about interfaces that does not
* allow identification of the interface on which a packet arrives
* (I'm looking at *you*, Microsoft Network Monitor...). Do *not*
* indicate support for that block if your capture format merely
* gives a list of interface information without having every packet
* explicitly or implicitly (as in, for example, the pcapng Simple
* Packet Block) indicate on which of those interfaces the packet
* arrived.
*
* WTAP_BLOCK_PACKET (which corresponds to the Enhanced Packet Block,
* the Simple Packet Block, and the deprecated Packet Block) is not
* currently used; it's reserved for future use. The same applies
* to WTAP_BLOCK_SYSTEMD_JOURNAL_EXPORT.
*/
typedef enum {
WTAP_BLOCK_SECTION = 0,
WTAP_BLOCK_IF_ID_AND_INFO,
WTAP_BLOCK_NAME_RESOLUTION,
WTAP_BLOCK_IF_STATISTICS,
WTAP_BLOCK_DECRYPTION_SECRETS,
WTAP_BLOCK_PACKET,
WTAP_BLOCK_FT_SPECIFIC_REPORT,
WTAP_BLOCK_FT_SPECIFIC_EVENT,
WTAP_BLOCK_SYSDIG_EVENT,
WTAP_BLOCK_SYSTEMD_JOURNAL_EXPORT,
WTAP_BLOCK_CUSTOM,
MAX_WTAP_BLOCK_TYPE_VALUE
} wtap_block_type_t;
/**
* Holds the required data from a WTAP_BLOCK_SECTION.
*/
typedef struct wtapng_section_mandatory_s {
guint64 section_length; /**< 64-bit value specifying the length in bytes of the
* following section.
* Section Length equal -1 (0xFFFFFFFFFFFFFFFF) means
* that the size of the section is not specified
* Note: if writing to a new file, this length will
* be invalid if anything changes, such as the other
* members of this struct, or the packets written.
*/
} wtapng_section_mandatory_t;
/** struct holding the information to build a WTAP_BLOCK_IF_ID_AND_INFO.
* the interface_data array holds an array of wtap_block_t
* representing interfacs, one per interface.
*/
typedef struct wtapng_iface_descriptions_s {
GArray *interface_data;
} wtapng_iface_descriptions_t;
/**
* Holds the required data from a WTAP_BLOCK_IF_ID_AND_INFO.
*/
typedef struct wtapng_if_descr_mandatory_s {
int wtap_encap; /**< link_type translated to wtap_encap */
guint64 time_units_per_second;
int tsprecision; /**< WTAP_TSPREC_ value for this interface */
guint32 snap_len;
guint8 num_stat_entries;
GArray *interface_statistics; /**< An array holding the interface statistics from
* pcapng ISB:s or equivalent(?)*/
} wtapng_if_descr_mandatory_t;
/**
* Holds the required data from a WTAP_BLOCK_NAME_RESOLUTION.
*/
typedef struct wtapng_nrb_mandatory_s {
GList *ipv4_addr_list;
GList *ipv6_addr_list;
} wtapng_nrb_mandatory_t;
/**
* Holds the required data from a WTAP_BLOCK_IF_STATISTICS.
*/
typedef struct wtapng_if_stats_mandatory_s {
guint32 interface_id;
guint32 ts_high;
guint32 ts_low;
} wtapng_if_stats_mandatory_t;
/**
* Holds the required data from a WTAP_BLOCK_DECRYPTION_SECRETS.
*/
typedef struct wtapng_dsb_mandatory_s {
guint32 secrets_type; /** Type of secrets stored in data (see secrets-types.h) */
guint32 secrets_len; /** Length of the secrets data in bytes */
guint8 *secrets_data; /** Buffer of secrets (not NUL-terminated) */
} wtapng_dsb_mandatory_t;
/**
* Holds the required data from a WTAP_BLOCK_PACKET.
* This includes Enhanced Packet Block, Simple Packet Block, and the deprecated Packet Block.
* NB. I'm not including the packet data here since Wireshark handles it in other ways.
* If we were to add it we'd need to implement copy and free routines in wtap_opttypes.c
*/
#if 0
/* Commented out for now, there's no mandatory data that isn't handled by
* Wireshark in other ways.
*/
typedef struct wtapng_packet_mandatory_s {
guint32 interface_id;
guint32 ts_high;
guint32 ts_low;
guint32 captured_len;
guint32 orig_len;
} wtapng_packet_mandatory_t;
#endif
/**
* Holds the required data from a WTAP_BLOCK_FT_SPECIFIC_REPORT.
*/
typedef struct wtapng_ft_specific_mandatory_s {
guint record_type; /* the type of record this is - file type-specific value */
} wtapng_ft_specific_mandatory_t;
/* Currently supported option types */
typedef enum {
WTAP_OPTTYPE_UINT8,
WTAP_OPTTYPE_UINT32,
WTAP_OPTTYPE_UINT64,
WTAP_OPTTYPE_STRING,
WTAP_OPTTYPE_BYTES,
WTAP_OPTTYPE_IPv4,
WTAP_OPTTYPE_IPv6,
WTAP_OPTTYPE_CUSTOM,
WTAP_OPTTYPE_IF_FILTER,
WTAP_OPTTYPE_PACKET_VERDICT,
WTAP_OPTTYPE_PACKET_HASH,
} wtap_opttype_e;
typedef enum {
WTAP_OPTTYPE_SUCCESS = 0,
WTAP_OPTTYPE_NO_SUCH_OPTION = -1,
WTAP_OPTTYPE_NOT_FOUND = -2,
WTAP_OPTTYPE_TYPE_MISMATCH = -3,
WTAP_OPTTYPE_NUMBER_MISMATCH = -4,
WTAP_OPTTYPE_ALREADY_EXISTS = -5,
WTAP_OPTTYPE_BAD_BLOCK = -6,
} wtap_opttype_return_val;
/* https://www.iana.org/assignments/enterprise-numbers/enterprise-numbers */
#define PEN_NFLX 10949
#define PEN_VCTR 46254
/*
* Structure describing a custom option.
*/
typedef struct custom_opt_s {
guint32 pen;
union {
struct generic_custom_opt_data {
gsize custom_data_len;
gchar *custom_data;
} generic_data;
struct nflx_custom_opt_data {
guint32 type;
gsize custom_data_len;
gchar *custom_data;
gboolean use_little_endian;
} nflx_data;
} data;
} custom_opt_t;
/*
* Structure describing a NFLX custom option.
*/
typedef struct nflx_custom_opt_s {
gboolean nflx_use_little_endian;
guint32 nflx_type;
gsize nflx_custom_data_len;
gchar *nflx_custom_data;
} nflx_custom_opt_t;
/* Interface description data - if_filter option structure */
/* BPF instruction */
typedef struct wtap_bpf_insn_s {
guint16 code;
guint8 jt;
guint8 jf;
guint32 k;
} wtap_bpf_insn_t;
/*
* Type of filter.
*/
typedef enum {
if_filter_pcap = 0, /* pcap filter string */
if_filter_bpf = 1 /* BPF program */
} if_filter_type_e;
typedef struct if_filter_opt_s {
if_filter_type_e type;
union {
gchar *filter_str; /**< pcap filter string */
struct wtap_bpf_insns {
guint bpf_prog_len; /**< number of BPF instructions */
wtap_bpf_insn_t *bpf_prog; /**< BPF instructions */
} bpf_prog; /**< BPF program */
} data;
} if_filter_opt_t;
/* Packet - packet_verdict option structure */
/*
* Type of verdict.
*/
typedef enum {
packet_verdict_hardware = 0, /* array of octets */
packet_verdict_linux_ebpf_tc = 1, /* 64-bit unsigned integer TC_ACT_ value */
packet_verdict_linux_ebpf_xdp = 2 /* 64-bit unsigned integer xdp_action value */
} packet_verdict_type_e;
typedef struct packet_verdict_opt_s {
packet_verdict_type_e type;
union {
GByteArray *verdict_bytes;
guint64 verdict_linux_ebpf_tc;
guint64 verdict_linux_ebpf_xdp;
} data;
} packet_verdict_opt_t;
typedef struct packet_hash_opt_s {
guint8 type;
GByteArray *hash_bytes;
} packet_hash_opt_t;
/*
* Structure describing a value of an option.
*/
typedef union {
guint8 uint8val;
guint32 uint32val;
guint64 uint64val;
ws_in4_addr ipv4val; /* network byte order */
ws_in6_addr ipv6val;
char *stringval;
GBytes *byteval;
custom_opt_t custom_opt;
if_filter_opt_t if_filterval;
packet_verdict_opt_t packet_verdictval;
packet_hash_opt_t packet_hash;
} wtap_optval_t;
/*
* Structure describing an option in a block.
*/
typedef struct {
guint option_id; /**< option code for the option */
wtap_optval_t value; /**< value */
} wtap_option_t;
#define NFLX_OPT_TYPE_VERSION 1
#define NFLX_OPT_TYPE_TCPINFO 2
#define NFLX_OPT_TYPE_DUMPINFO 4
#define NFLX_OPT_TYPE_DUMPTIME 5
#define NFLX_OPT_TYPE_STACKNAME 6
struct nflx_dumpinfo {
guint32 tlh_version;
guint32 tlh_type;
guint64 tlh_length;
guint16 tlh_ie_fport;
guint16 tlh_ie_lport;
guint32 tlh_ie_faddr_addr32[4];
guint32 tlh_ie_laddr_addr32[4];
guint32 tlh_ie_zoneid;
guint64 tlh_offset_tv_sec;
guint64 tlh_offset_tv_usec;
char tlh_id[64];
char tlh_reason[32];
char tlh_tag[32];
guint8 tlh_af;
guint8 _pad[7];
};
/* Flags used in tlb_eventflags */
#define NFLX_TLB_FLAG_RXBUF 0x0001 /* Includes receive buffer info */
#define NFLX_TLB_FLAG_TXBUF 0x0002 /* Includes send buffer info */
#define NFLX_TLB_FLAG_HDR 0x0004 /* Includes a TCP header */
#define NFLX_TLB_FLAG_VERBOSE 0x0008 /* Includes function/line numbers */
#define NFLX_TLB_FLAG_STACKINFO 0x0010 /* Includes stack-specific info */
/* Flags used in tlb_flags */
#define NFLX_TLB_TF_REQ_SCALE 0x00000020 /* Sent WS option */
#define NFLX_TLB_TF_RCVD_SCALE 0x00000040 /* Received WS option */
/* Values of tlb_state */
#define NFLX_TLB_TCPS_ESTABLISHED 4
#define NFLX_TLB_IS_SYNCHRONIZED(state) (state >= NFLX_TLB_TCPS_ESTABLISHED)
struct nflx_tcpinfo {
guint64 tlb_tv_sec;
guint64 tlb_tv_usec;
guint32 tlb_ticks;
guint32 tlb_sn;
guint8 tlb_stackid;
guint8 tlb_eventid;
guint16 tlb_eventflags;
gint32 tlb_errno;
guint32 tlb_rxbuf_tls_sb_acc;
guint32 tlb_rxbuf_tls_sb_ccc;
guint32 tlb_rxbuf_tls_sb_spare;
guint32 tlb_txbuf_tls_sb_acc;
guint32 tlb_txbuf_tls_sb_ccc;
guint32 tlb_txbuf_tls_sb_spare;
gint32 tlb_state;
guint32 tlb_starttime;
guint32 tlb_iss;
guint32 tlb_flags;
guint32 tlb_snd_una;
guint32 tlb_snd_max;
guint32 tlb_snd_cwnd;
guint32 tlb_snd_nxt;
guint32 tlb_snd_recover;
guint32 tlb_snd_wnd;
guint32 tlb_snd_ssthresh;
guint32 tlb_srtt;
guint32 tlb_rttvar;
guint32 tlb_rcv_up;
guint32 tlb_rcv_adv;
guint32 tlb_flags2;
guint32 tlb_rcv_nxt;
guint32 tlb_rcv_wnd;
guint32 tlb_dupacks;
gint32 tlb_segqlen;
gint32 tlb_snd_numholes;
guint32 tlb_flex1;
guint32 tlb_flex2;
guint32 tlb_fbyte_in;
guint32 tlb_fbyte_out;
guint8 tlb_snd_scale:4,
tlb_rcv_scale:4;
guint8 _pad[3];
/* The following fields might become part of a union */
guint64 tlb_stackinfo_bbr_cur_del_rate;
guint64 tlb_stackinfo_bbr_delRate;
guint64 tlb_stackinfo_bbr_rttProp;
guint64 tlb_stackinfo_bbr_bw_inuse;
guint32 tlb_stackinfo_bbr_inflight;
guint32 tlb_stackinfo_bbr_applimited;
guint32 tlb_stackinfo_bbr_delivered;
guint32 tlb_stackinfo_bbr_timeStamp;
guint32 tlb_stackinfo_bbr_epoch;
guint32 tlb_stackinfo_bbr_lt_epoch;
guint32 tlb_stackinfo_bbr_pkts_out;
guint32 tlb_stackinfo_bbr_flex1;
guint32 tlb_stackinfo_bbr_flex2;
guint32 tlb_stackinfo_bbr_flex3;
guint32 tlb_stackinfo_bbr_flex4;
guint32 tlb_stackinfo_bbr_flex5;
guint32 tlb_stackinfo_bbr_flex6;
guint32 tlb_stackinfo_bbr_lost;
guint16 tlb_stackinfo_bbr_pacing_gain;
guint16 tlb_stackinfo_bbr_cwnd_gain;
guint16 tlb_stackinfo_bbr_flex7;
guint8 tlb_stackinfo_bbr_bbr_state;
guint8 tlb_stackinfo_bbr_bbr_substate;
guint8 tlb_stackinfo_bbr_inhpts;
guint8 tlb_stackinfo_bbr_ininput;
guint8 tlb_stackinfo_bbr_use_lt_bw;
guint8 tlb_stackinfo_bbr_flex8;
guint32 tlb_stackinfo_bbr_pkt_epoch;
guint32 tlb_len;
};
struct wtap_dumper;
typedef void (*wtap_block_create_func)(wtap_block_t block);
typedef void (*wtap_mand_free_func)(wtap_block_t block);
typedef void (*wtap_mand_copy_func)(wtap_block_t dest_block, wtap_block_t src_block);
/** Initialize block types.
*
* This is currently just a placeholder as nothing needs to be
* initialized yet. Should handle "registration" when code is
* refactored to do so.
*/
WS_DLL_PUBLIC void
wtap_opttypes_initialize(void);
/** Create a block by type
*
* Return a newly allocated block with default options provided
*
* @param[in] block_type Block type to be created
* @return Newly allocated block
*/
WS_DLL_PUBLIC wtap_block_t
wtap_block_create(wtap_block_type_t block_type);
/** Increase reference count of a block
*
* Call when taking a copy of a block
*
* @param[in] block Block add ref to
* @return The block
*/
WS_DLL_PUBLIC wtap_block_t
wtap_block_ref(wtap_block_t block);
/** Decrease reference count of a block
*
* Needs to be called on any block once you're done with it
*
* @param[in] block Block to be deref'd
*/
WS_DLL_PUBLIC void
wtap_block_unref(wtap_block_t block);
/** Free an array of blocks
*
* Needs to be called to clean up blocks allocated
* through GArray (for multiple blocks of same type)
* Includes freeing the GArray
*
* @param[in] block_array Array of blocks to be freed
*/
WS_DLL_PUBLIC void
wtap_block_array_free(GArray* block_array);
/** Provide type of a block
*
* @param[in] block Block from which to retrieve mandatory data
* @return Block type.
*/
WS_DLL_PUBLIC wtap_block_type_t
wtap_block_get_type(wtap_block_t block);
/** Provide mandatory data of a block
*
* @param[in] block Block from which to retrieve mandatory data
* @return Block mandatory data. Structure varies based on block type
*/
WS_DLL_PUBLIC void*
wtap_block_get_mandatory_data(wtap_block_t block);
/** Count the number of times the given option appears in the block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @return guint - the number of times the option was found
*/
WS_DLL_PUBLIC guint
wtap_block_count_option(wtap_block_t block, guint option_id);
/** Add UINT8 option value to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_uint8_option(wtap_block_t block, guint option_id, guint8 value);
/** Set UINT8 option value in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] value New value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_uint8_option_value(wtap_block_t block, guint option_id, guint8 value);
/** Get UINT8 option value from a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[out] value Returned value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_uint8_option_value(wtap_block_t block, guint option_id, guint8* value) G_GNUC_WARN_UNUSED_RESULT;
/** Add UINT32 option value to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_uint32_option(wtap_block_t block, guint option_id, guint32 value);
/** Set UINT32 option value in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] value New value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_uint32_option_value(wtap_block_t block, guint option_id, guint32 value);
/** Get UINT32 option value from a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[out] value Returned value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_uint32_option_value(wtap_block_t block, guint option_id, guint32* value) G_GNUC_WARN_UNUSED_RESULT;
/** Add UINT64 option value to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_uint64_option(wtap_block_t block, guint option_id, guint64 value);
/** Set UINT64 option value in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] value New value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_uint64_option_value(wtap_block_t block, guint option_id, guint64 value);
/** Get UINT64 option value from a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[out] value Returned value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_uint64_option_value(wtap_block_t block, guint option_id, guint64* value) G_GNUC_WARN_UNUSED_RESULT;
/** Add IPv4 address option value to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_ipv4_option(wtap_block_t block, guint option_id, guint32 value);
/** Set IPv4 option value in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] value New value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_ipv4_option_value(wtap_block_t block, guint option_id, guint32 value);
/** Get IPv4 option value from a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[out] value Returned value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_ipv4_option_value(wtap_block_t block, guint option_id, guint32* value) G_GNUC_WARN_UNUSED_RESULT;
/** Add IPv6 address option value to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_ipv6_option(wtap_block_t block, guint option_id, ws_in6_addr *value);
/** Set IPv6 option value in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] value New value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_ipv6_option_value(wtap_block_t block, guint option_id, ws_in6_addr *value);
/** Get IPv6 option value from a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[out] value Returned value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_ipv6_option_value(wtap_block_t block, guint option_id, ws_in6_addr* value) G_GNUC_WARN_UNUSED_RESULT;
/** Add a string option to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option
* @param[in] value_length Maximum length of string to copy.
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_string_option(wtap_block_t block, guint option_id, const char *value, gsize value_length);
/** Add a string option to a block taking ownership of the null-terminated string.
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_string_option_owned(wtap_block_t block, guint option_id, char *value);
/** Add a string option to a block with a printf-formatted string as its value
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] format printf-like format string
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_string_option_format(wtap_block_t block, guint option_id, const char *format, ...)
G_GNUC_PRINTF(3,4);
/** Set string option value in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] value New value of option
* @param[in] value_length Maximum length of string to copy.
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_string_option_value(wtap_block_t block, guint option_id, const char* value, gsize value_length);
/** Set string option value for the nth instance of a particular option
* in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] idx Instance number of option with that ID
* @param[in] value New value of option
* @param[in] value_length Maximum length of string to copy.
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_nth_string_option_value(wtap_block_t block, guint option_id, guint idx, const char* value, gsize value_length);
/** Set string option value in a block to a printf-formatted string
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] format printf-like format string
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_string_option_value_format(wtap_block_t block, guint option_id, const char *format, ...)
G_GNUC_PRINTF(3,4);
/** Set string option value for the nth instance of a particular option
* in a block to a printf-formatted string
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] idx Instance number of option with that ID
* @param[in] format printf-like format string
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_nth_string_option_value_format(wtap_block_t block, guint option_id, guint idx, const char *format, ...)
G_GNUC_PRINTF(4,5);
/** Get string option value from a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[out] value Returned value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_string_option_value(wtap_block_t block, guint option_id, char** value) G_GNUC_WARN_UNUSED_RESULT;
/** Get string option value for the nth instance of a particular option
* in a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[in] idx Instance number of option with that ID
* @param[out] value Returned value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_nth_string_option_value(wtap_block_t block, guint option_id, guint idx, char** value) G_GNUC_WARN_UNUSED_RESULT;
/** Add a bytes option to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option to copy
* @param[in] value_length Number of bytes to copy
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_bytes_option(wtap_block_t block, guint option_id, const guint8 *value, gsize value_length);
/** Add a bytes option to a block, borrowing the value from a GBytes
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option as a GBytes
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_bytes_option_borrow(wtap_block_t block, guint option_id, GBytes *value);
/** Set bytes option value in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] value New value of option
* @param[in] value_length Number of bytes to copy.
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_bytes_option_value(wtap_block_t block, guint option_id, const guint8* value, gsize value_length);
/** Set bytes option value for nth instance of a particular option in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] idx Instance number of option with that ID
* @param[in] value New value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_nth_bytes_option_value(wtap_block_t block, guint option_id, guint idx, GBytes* value);
/** Get bytes option value from a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[out] value Returned value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
* @note You should call g_bytes_ref() on value if you plan to keep it around
* (and then g_bytes_unref() when you're done with it).
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_bytes_option_value(wtap_block_t block, guint option_id, GBytes** value) G_GNUC_WARN_UNUSED_RESULT;
/** Get bytes option value for nth instance of a particular option in a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[in] idx Instance number of option with that ID
* @param[out] value Returned value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
* @note You should call g_bytes_ref() on value if you plan to keep it around
* (and then g_bytes_unref() when you're done with it).
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_nth_bytes_option_value(wtap_block_t block, guint option_id, guint idx, GBytes** value) G_GNUC_WARN_UNUSED_RESULT;
/** Add an NFLX custom option to a block
*
* @param[in] block Block to which to add the option
* @param[in] nflx_type NFLX option type
* @param[in] nflx_custom_data pointer to the data
* @param[in] nflx_custom_data_len length of custom_data
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_nflx_custom_option(wtap_block_t block, guint32 nflx_type, const char *nflx_custom_data, gsize nflx_custom_data_len);
/** Get an NFLX custom option value from a block
*
* @param[in] block Block from which to get the option value
* @param[in] nflx_type type of the option
* @param[out] nflx_custom_data Returned value of NFLX custom option value
* @param[in] nflx_custom_data_len size of buffer provided in nflx_custom_data
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_nflx_custom_option(wtap_block_t block, guint32 nflx_type, char *nflx_custom_data, gsize nflx_custom_data_len);
/** Add a custom option to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] pen PEN
* @param[in] custom_data pointer to the data
* @param[in] custom_data_len length of custom_data
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_custom_option(wtap_block_t block, guint option_id, guint32 pen, const char *custom_data, gsize custom_data_len);
/** Add an if_filter option value to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_if_filter_option(wtap_block_t block, guint option_id, if_filter_opt_t* value);
/** Set an if_filter option value in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] value New value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_if_filter_option_value(wtap_block_t block, guint option_id, if_filter_opt_t* value);
/** Get an if_filter option value from a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[out] value Returned value of option value
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_if_filter_option_value(wtap_block_t block, guint option_id, if_filter_opt_t* value) G_GNUC_WARN_UNUSED_RESULT;
/** Add a packet_verdict option value to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_packet_verdict_option(wtap_block_t block, guint option_id, packet_verdict_opt_t* value);
/** Set packet_verdict option value for the nth instsance of a particular
* option in a block
*
* @param[in] block Block in which to set the option value
* @param[in] option_id Identifier value for option
* @param[in] idx Instance number of option with that ID
* @param[in] value New value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_set_nth_packet_verdict_option_value(wtap_block_t block, guint option_id, guint idx, packet_verdict_opt_t* value);
/** Get packet_verdict option value for the nth instance of a particular
* option in a block
*
* @param[in] block Block from which to get the option value
* @param[in] option_id Identifier value for option
* @param[in] idx Instance number of option with that ID
* @param[out] value Returned value of option value
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_get_nth_packet_verdict_option_value(wtap_block_t block, guint option_id, guint idx, packet_verdict_opt_t* value) G_GNUC_WARN_UNUSED_RESULT;
WS_DLL_PUBLIC void
wtap_packet_verdict_free(packet_verdict_opt_t* verdict);
/** Add a packet_hash option value to a block
*
* @param[in] block Block to which to add the option
* @param[in] option_id Identifier value for option
* @param[in] value Value of option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_add_packet_hash_option(wtap_block_t block, guint option_id, packet_hash_opt_t* value);
WS_DLL_PUBLIC void
wtap_packet_hash_free(packet_hash_opt_t* hash);
/** Remove an option from a block
*
* @param[in] block Block from which to remove the option
* @param[in] option_id Identifier value for option
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_remove_option(wtap_block_t block, guint option_id);
/** Remove the nth instance of an option from a block
*
* @param[in] block Block from which to remove the option instance
* @param[in] option_id Identifier value for option
* @param[in] idx Instance number of option with that ID
* @return wtap_opttype_return_val - WTAP_OPTTYPE_SUCCESS if successful,
* error code otherwise
*/
WS_DLL_PUBLIC wtap_opttype_return_val
wtap_block_remove_nth_option_instance(wtap_block_t block, guint option_id, guint idx);
/** Copy a block to another.
*
* Any options that are in the destination but not the source are not removed.
* Options that are just in source will be added to destination
*
* @param[in] dest_block Block to be copied to
* @param[in] src_block Block to be copied from
*/
WS_DLL_PUBLIC void
wtap_block_copy(wtap_block_t dest_block, wtap_block_t src_block);
/** Make a copy of a block.
*
* @param[in] block Block to be copied from
* @return Newly allocated copy of that block
*/
WS_DLL_PUBLIC wtap_block_t
wtap_block_make_copy(wtap_block_t block);
typedef gboolean (*wtap_block_foreach_func)(wtap_block_t block, guint option_id, wtap_opttype_e option_type, wtap_optval_t *option, void *user_data);
WS_DLL_PUBLIC gboolean
wtap_block_foreach_option(wtap_block_t block, wtap_block_foreach_func func, void* user_data);
/** Cleanup the internal structures
*/
WS_DLL_PUBLIC void
wtap_opttypes_cleanup(void);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* WTAP_OPT_TYPES_H */ |
wireshark/writecap/.editorconfig | #
# Editor configuration
#
# https://editorconfig.org/
#
[capture-pcap-util-unix.[ch]]
indent_style = tab
indent_size = tab
[capture-pcap-util.[ch]]
indent_style = tab
indent_size = tab
[capture-wpcap.[ch]]
indent_style = tab
indent_size = tab
[ws80211_utils.[ch]]
indent_style = tab
indent_size = tab |
|
Text | wireshark/writecap/CMakeLists.txt | # CMakeLists.txt
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <[email protected]>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
set(WRITECAP_SRC
pcapio.c
)
set_source_files_properties(
${WRITECAP_SRC}
PROPERTIES
COMPILE_FLAGS "${WERROR_COMMON_FLAGS}"
)
add_library(writecap STATIC
${WRITECAP_SRC}
)
set_target_properties(writecap PROPERTIES
LINK_FLAGS "${WS_LINK_FLAGS}"
FOLDER "Libs"
)
#
# 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:
# |
C | wireshark/writecap/pcapio.c | /* pcapio.c
* Our own private code for writing libpcap files when capturing.
*
* We have these because we want a way to open a stream for output given
* only a file descriptor. libpcap 0.9[.x] has "pcap_dump_fopen()", which
* provides that, but
*
* 1) earlier versions of libpcap doesn't have it
*
* and
*
* 2) WinPcap/Npcap don't have it, because a file descriptor opened
* by code built for one version of the MSVC++ C library
* can't be used by library routines built for another version
* (e.g., threaded vs. unthreaded).
*
* Libpcap's pcap_dump() also doesn't return any error indications.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* Derived from code in the Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <config.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef _WIN32
#include <windows.h>
#endif
#include <glib.h>
#include <wsutil/epochs.h>
#include "pcapio.h"
/* Magic numbers in "libpcap" files.
"libpcap" file records are written in the byte order of the host that
writes them, and the reader is expected to fix this up.
PCAP_MAGIC is the magic number, in host byte order; PCAP_SWAPPED_MAGIC
is a byte-swapped version of that.
PCAP_NSEC_MAGIC is for Ulf Lamping's modified "libpcap" format,
which uses the same common file format as PCAP_MAGIC, but the
timestamps are saved in nanosecond resolution instead of microseconds.
PCAP_SWAPPED_NSEC_MAGIC is a byte-swapped version of that. */
#define PCAP_MAGIC 0xa1b2c3d4
#define PCAP_SWAPPED_MAGIC 0xd4c3b2a1
#define PCAP_NSEC_MAGIC 0xa1b23c4d
#define PCAP_SWAPPED_NSEC_MAGIC 0x4d3cb2a1
/* "libpcap" file header. */
struct pcap_hdr {
uint32_t magic; /* magic number */
uint16_t version_major; /* major version number */
uint16_t version_minor; /* minor version number */
int32_t thiszone; /* GMT to local correction */
uint32_t sigfigs; /* accuracy of timestamps */
uint32_t snaplen; /* max length of captured packets, in octets */
uint32_t network; /* data link type */
};
/* "libpcap" record header. */
struct pcaprec_hdr {
uint32_t ts_sec; /* timestamp seconds */
uint32_t ts_usec; /* timestamp microseconds (nsecs for PCAP_NSEC_MAGIC) */
uint32_t incl_len; /* number of octets of packet saved in file */
uint32_t orig_len; /* actual length of packet */
};
/* Magic numbers in ".pcapng" files.
*
* .pcapng file records are written in the byte order of the host that
* writes them, and the reader is expected to fix this up.
* PCAPNG_MAGIC is the magic number, in host byte order;
* PCAPNG_SWAPPED_MAGIC is a byte-swapped version of that.
*/
#define PCAPNG_MAGIC 0x1A2B3C4D
#define PCAPNG_SWAPPED_MAGIC 0x4D3C2B1A
/* Currently we are only supporting the initial version of
the file format. */
#define PCAPNG_MAJOR_VERSION 1
#define PCAPNG_MINOR_VERSION 0
/* Section Header Block without options and trailing Block Total Length */
struct shb {
uint32_t block_type;
uint32_t block_total_length;
uint32_t byte_order_magic;
uint16_t major_version;
uint16_t minor_version;
uint64_t section_length;
};
#define SECTION_HEADER_BLOCK_TYPE 0x0A0D0D0A
/* Interface Description Block without options and trailing Block Total Length */
struct idb {
uint32_t block_type;
uint32_t block_total_length;
uint16_t link_type;
uint16_t reserved;
uint32_t snap_len;
};
#define INTERFACE_DESCRIPTION_BLOCK_TYPE 0x00000001
/* Interface Statistics Block without actual packet, options, and trailing
Block Total Length */
struct isb {
uint32_t block_type;
uint32_t block_total_length;
uint32_t interface_id;
uint32_t timestamp_high;
uint32_t timestamp_low;
};
#define INTERFACE_STATISTICS_BLOCK_TYPE 0x00000005
/* Enhanced Packet Block without actual packet, options, and trailing
Block Total Length */
struct epb {
uint32_t block_type;
uint32_t block_total_length;
uint32_t interface_id;
uint32_t timestamp_high;
uint32_t timestamp_low;
uint32_t captured_len;
uint32_t packet_len;
};
#define ENHANCED_PACKET_BLOCK_TYPE 0x00000006
struct ws_option {
uint16_t type;
uint16_t value_length;
};
#define OPT_ENDOFOPT 0
#define OPT_COMMENT 1
#define EPB_FLAGS 2
#define SHB_HARDWARE 2 /* currently not used */
#define SHB_OS 3
#define SHB_USERAPPL 4
#define IDB_NAME 2
#define IDB_DESCRIPTION 3
#define IDB_IF_SPEED 8
#define IDB_TSRESOL 9
#define IDB_FILTER 11
#define IDB_OS 12
#define IDB_HARDWARE 15
#define ISB_STARTTIME 2
#define ISB_ENDTIME 3
#define ISB_IFRECV 4
#define ISB_IFDROP 5
#define ISB_FILTERACCEPT 6
#define ISB_OSDROP 7
#define ISB_USRDELIV 8
#define ADD_PADDING(x) ((((x) + 3) >> 2) << 2)
/* Write to capture file */
static bool
write_to_file(FILE* pfile, const uint8_t* data, size_t data_length,
uint64_t *bytes_written, int *err)
{
size_t nwritten;
nwritten = fwrite(data, data_length, 1, pfile);
if (nwritten != 1) {
if (ferror(pfile)) {
*err = errno;
} else {
*err = 0;
}
return false;
}
(*bytes_written) += data_length;
return true;
}
/* Writing pcap files */
/* Write the file header to a dump file.
Returns true on success, false on failure.
Sets "*err" to an error code, or 0 for a short write, on failure*/
bool
libpcap_write_file_header(FILE* pfile, int linktype, int snaplen, bool ts_nsecs, uint64_t *bytes_written, int *err)
{
struct pcap_hdr file_hdr;
file_hdr.magic = ts_nsecs ? PCAP_NSEC_MAGIC : PCAP_MAGIC;
/* current "libpcap" format is 2.4 */
file_hdr.version_major = 2;
file_hdr.version_minor = 4;
file_hdr.thiszone = 0; /* XXX - current offset? */
file_hdr.sigfigs = 0; /* unknown, but also apparently unused */
file_hdr.snaplen = snaplen;
file_hdr.network = linktype;
return write_to_file(pfile, (const uint8_t*)&file_hdr, sizeof(file_hdr), bytes_written, err);
}
/* Write a record for a packet to a dump file.
Returns true on success, false on failure. */
bool
libpcap_write_packet(FILE* pfile,
time_t sec, uint32_t usec,
uint32_t caplen, uint32_t len,
const uint8_t *pd,
uint64_t *bytes_written, int *err)
{
struct pcaprec_hdr rec_hdr;
rec_hdr.ts_sec = (uint32_t)sec; /* Y2.038K issue in pcap format.... */
rec_hdr.ts_usec = usec;
rec_hdr.incl_len = caplen;
rec_hdr.orig_len = len;
if (!write_to_file(pfile, (const uint8_t*)&rec_hdr, sizeof(rec_hdr), bytes_written, err))
return false;
return write_to_file(pfile, pd, caplen, bytes_written, err);
}
/* Writing pcapng files */
static uint32_t
pcapng_count_string_option(const char *option_value)
{
if ((option_value != NULL) && (strlen(option_value) > 0) && (strlen(option_value) < UINT16_MAX)) {
/* There's a value to write; get its length */
return (uint32_t)(sizeof(struct ws_option) +
(uint16_t)ADD_PADDING(strlen(option_value)));
}
return 0; /* nothing to write */
}
static bool
pcapng_write_string_option(FILE* pfile,
uint16_t option_type, const char *option_value,
uint64_t *bytes_written, int *err)
{
size_t option_value_length;
struct ws_option option;
const uint32_t padding = 0;
if (option_value == NULL)
return true; /* nothing to write */
option_value_length = strlen(option_value);
if ((option_value_length > 0) && (option_value_length < UINT16_MAX)) {
/* something to write */
option.type = option_type;
option.value_length = (uint16_t)option_value_length;
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)option_value, (int) option_value_length, bytes_written, err))
return false;
if (option_value_length % 4) {
if (!write_to_file(pfile, (const uint8_t*)&padding, 4 - option_value_length % 4, bytes_written, err))
return false;
}
}
return true;
}
/* Write a pre-formatted pcapng block directly to the output file */
bool
pcapng_write_block(FILE* pfile,
const uint8_t *data,
uint32_t length,
uint64_t *bytes_written,
int *err)
{
uint32_t block_length, end_length;
/* Check
* - length and data are aligned to 4 bytes
* - block_total_length field is the same at the start and end of the block
*
* The block_total_length is not checked against the provided length but
* getting the trailing block_total_length from the length argument gives
* us an implicit check of correctness without needing to do an endian swap
*/
if (((length & 3) != 0) || (((gintptr)data & 3) != 0)) {
*err = EINVAL;
return false;
}
block_length = *(const uint32_t *) (data+sizeof(uint32_t));
end_length = *(const uint32_t *) (data+length-sizeof(uint32_t));
if (block_length != end_length) {
*err = EBADMSG;
return false;
}
return write_to_file(pfile, data, length, bytes_written, err);
}
bool
pcapng_write_section_header_block(FILE* pfile,
GPtrArray *comments,
const char *hw,
const char *os,
const char *appname,
uint64_t section_length,
uint64_t *bytes_written,
int *err)
{
struct shb shb;
struct ws_option option;
uint32_t block_total_length;
uint32_t options_length;
/* Size of base header */
block_total_length = sizeof(struct shb) +
sizeof(uint32_t);
options_length = 0;
if (comments != NULL) {
for (unsigned i = 0; i < comments->len; i++) {
options_length += pcapng_count_string_option((char *)g_ptr_array_index(comments, i));
}
}
options_length += pcapng_count_string_option(hw);
options_length += pcapng_count_string_option(os);
options_length += pcapng_count_string_option(appname);
/* If we have options add size of end-of-options */
if (options_length != 0) {
options_length += (uint32_t)sizeof(struct ws_option);
}
block_total_length += options_length;
/* write shb header */
shb.block_type = SECTION_HEADER_BLOCK_TYPE;
shb.block_total_length = block_total_length;
shb.byte_order_magic = PCAPNG_MAGIC;
shb.major_version = PCAPNG_MAJOR_VERSION;
shb.minor_version = PCAPNG_MINOR_VERSION;
shb.section_length = section_length;
if (!write_to_file(pfile, (const uint8_t*)&shb, sizeof(struct shb), bytes_written, err))
return false;
if (comments != NULL) {
for (unsigned i = 0; i < comments->len; i++) {
if (!pcapng_write_string_option(pfile, OPT_COMMENT,
(char *)g_ptr_array_index(comments, i),
bytes_written, err))
return false;
}
}
if (!pcapng_write_string_option(pfile, SHB_HARDWARE, hw,
bytes_written, err))
return false;
if (!pcapng_write_string_option(pfile, SHB_OS, os,
bytes_written, err))
return false;
if (!pcapng_write_string_option(pfile, SHB_USERAPPL, appname,
bytes_written, err))
return false;
if (options_length != 0) {
/* write end of options */
option.type = OPT_ENDOFOPT;
option.value_length = 0;
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
}
/* write the trailing block total length */
return write_to_file(pfile, (const uint8_t*)&block_total_length, sizeof(uint32_t), bytes_written, err);
}
bool
pcapng_write_interface_description_block(FILE* pfile,
const char *comment, /* OPT_COMMENT 1 */
const char *name, /* IDB_NAME 2 */
const char *descr, /* IDB_DESCRIPTION 3 */
const char *filter, /* IDB_FILTER 11 */
const char *os, /* IDB_OS 12 */
const char *hardware, /* IDB_HARDWARE 15 */
int link_type,
int snap_len,
uint64_t *bytes_written,
uint64_t if_speed, /* IDB_IF_SPEED 8 */
uint8_t tsresol, /* IDB_TSRESOL 9 */
int *err)
{
struct idb idb;
struct ws_option option;
uint32_t block_total_length;
uint32_t options_length;
const uint32_t padding = 0;
block_total_length = (uint32_t)(sizeof(struct idb) + sizeof(uint32_t));
options_length = 0;
/* 01 - OPT_COMMENT */
options_length += pcapng_count_string_option(comment);
/* 02 - IDB_NAME */
options_length += pcapng_count_string_option(name);
/* 03 - IDB_DESCRIPTION */
options_length += pcapng_count_string_option(descr);
/* 08 - IDB_IF_SPEED */
if (if_speed != 0) {
options_length += (uint32_t)(sizeof(struct ws_option) +
sizeof(uint64_t));
}
/* 09 - IDB_TSRESOL */
if (tsresol != 0) {
options_length += (uint32_t)(sizeof(struct ws_option) +
sizeof(struct ws_option));
}
/* 11 - IDB_FILTER */
if ((filter != NULL) && (strlen(filter) > 0) && (strlen(filter) < UINT16_MAX)) {
/* No, this isn't a string, it has an extra type byte */
options_length += (uint32_t)(sizeof(struct ws_option) +
(uint16_t)(ADD_PADDING(strlen(filter)+ 1)));
}
/* 12 - IDB_OS */
options_length += pcapng_count_string_option(os);
/* 15 - IDB_HARDWARE */
options_length += pcapng_count_string_option(hardware);
/* If we have options add size of end-of-options */
if (options_length != 0) {
options_length += (uint32_t)sizeof(struct ws_option);
}
block_total_length += options_length;
/* write block header */
idb.block_type = INTERFACE_DESCRIPTION_BLOCK_TYPE;
idb.block_total_length = block_total_length;
idb.link_type = link_type;
idb.reserved = 0;
idb.snap_len = snap_len;
if (!write_to_file(pfile, (const uint8_t*)&idb, sizeof(struct idb), bytes_written, err))
return false;
/* 01 - OPT_COMMENT - write comment string if applicable */
if (!pcapng_write_string_option(pfile, OPT_COMMENT, comment,
bytes_written, err))
return false;
/* 02 - IDB_NAME - write interface name string if applicable */
if (!pcapng_write_string_option(pfile, IDB_NAME, name,
bytes_written, err))
return false;
/* 03 - IDB_DESCRIPTION */
/* write interface description string if applicable */
if (!pcapng_write_string_option(pfile, IDB_DESCRIPTION, descr,
bytes_written, err))
return false;
/* 08 - IDB_IF_SPEED */
if (if_speed != 0) {
option.type = IDB_IF_SPEED;
option.value_length = sizeof(uint64_t);
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)&if_speed, sizeof(uint64_t), bytes_written, err))
return false;
}
/* 09 - IDB_TSRESOL */
if (tsresol != 0) {
option.type = IDB_TSRESOL;
option.value_length = sizeof(uint8_t);
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)&tsresol, sizeof(uint8_t), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)&padding, 3, bytes_written, err))
return false;
}
/* 11 - IDB_FILTER - write filter string if applicable
* We only write version 1 of the filter, pcapng string
*/
if ((filter != NULL) && (strlen(filter) > 0) && (strlen(filter) < UINT16_MAX - 1)) {
option.type = IDB_FILTER;
option.value_length = (uint16_t)(strlen(filter) + 1 );
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
/* The first byte of the Option Data keeps a code of the filter used, 0 = lipbpcap filter string */
if (!write_to_file(pfile, (const uint8_t*)&padding, 1, bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)filter, (int) strlen(filter), bytes_written, err))
return false;
if ((strlen(filter) + 1) % 4) {
if (!write_to_file(pfile, (const uint8_t*)&padding, 4 - (strlen(filter) + 1) % 4, bytes_written, err))
return false;
}
}
/* 12 - IDB_OS - write os string if applicable */
if (!pcapng_write_string_option(pfile, IDB_OS, os,
bytes_written, err))
return false;
/* 15 - IDB_HARDWARE - write hardware string if applicable */
if (!pcapng_write_string_option(pfile, IDB_HARDWARE, hardware,
bytes_written, err))
return false;
if (options_length != 0) {
/* write end of options */
option.type = OPT_ENDOFOPT;
option.value_length = 0;
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
}
/* write the trailing Block Total Length */
return write_to_file(pfile, (const uint8_t*)&block_total_length, sizeof(uint32_t), bytes_written, err);
}
/* Write a record for a packet to a dump file.
Returns true on success, false on failure. */
bool
pcapng_write_enhanced_packet_block(FILE* pfile,
const char *comment,
time_t sec, uint32_t usec,
uint32_t caplen, uint32_t len,
uint32_t interface_id,
unsigned ts_mul,
const uint8_t *pd,
uint32_t flags,
uint64_t *bytes_written,
int *err)
{
struct epb epb;
struct ws_option option;
uint32_t block_total_length;
uint64_t timestamp;
uint32_t options_length;
const uint32_t padding = 0;
uint8_t buff[8];
uint8_t i;
uint8_t pad_len = 0;
block_total_length = (uint32_t)(sizeof(struct epb) +
ADD_PADDING(caplen) +
sizeof(uint32_t));
options_length = 0;
options_length += pcapng_count_string_option(comment);
if (flags != 0) {
options_length += (uint32_t)(sizeof(struct ws_option) +
sizeof(uint32_t));
}
/* If we have options add size of end-of-options */
if (options_length != 0) {
options_length += (uint32_t)sizeof(struct ws_option);
}
block_total_length += options_length;
timestamp = (uint64_t)sec * ts_mul + (uint64_t)usec;
epb.block_type = ENHANCED_PACKET_BLOCK_TYPE;
epb.block_total_length = block_total_length;
epb.interface_id = interface_id;
epb.timestamp_high = (uint32_t)((timestamp>>32) & 0xffffffff);
epb.timestamp_low = (uint32_t)(timestamp & 0xffffffff);
epb.captured_len = caplen;
epb.packet_len = len;
if (!write_to_file(pfile, (const uint8_t*)&epb, sizeof(struct epb), bytes_written, err))
return false;
if (!write_to_file(pfile, pd, caplen, bytes_written, err))
return false;
/* Use more efficient write in case of no "extras" */
if(caplen % 4) {
pad_len = 4 - (caplen % 4);
}
/*
* If we have no options to write, just write out the padding and
* the block total length with one fwrite() call.
*/
if(!comment && flags == 0 && options_length==0){
/* Put padding in the buffer */
for (i = 0; i < pad_len; i++) {
buff[i] = 0;
}
/* Write the total length */
memcpy(&buff[i], &block_total_length, sizeof(uint32_t));
i += sizeof(uint32_t);
return write_to_file(pfile, (const uint8_t*)&buff, i, bytes_written, err);
}
if (pad_len) {
if (!write_to_file(pfile, (const uint8_t*)&padding, pad_len, bytes_written, err))
return false;
}
if (!pcapng_write_string_option(pfile, OPT_COMMENT, comment,
bytes_written, err))
return false;
if (flags != 0) {
option.type = EPB_FLAGS;
option.value_length = sizeof(uint32_t);
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)&flags, sizeof(uint32_t), bytes_written, err))
return false;
}
if (options_length != 0) {
/* write end of options */
option.type = OPT_ENDOFOPT;
option.value_length = 0;
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
}
return write_to_file(pfile, (const uint8_t*)&block_total_length, sizeof(uint32_t), bytes_written, err);
}
bool
pcapng_write_interface_statistics_block(FILE* pfile,
uint32_t interface_id,
uint64_t *bytes_written,
const char *comment, /* OPT_COMMENT 1 */
uint64_t isb_starttime, /* ISB_STARTTIME 2 */
uint64_t isb_endtime, /* ISB_ENDTIME 3 */
uint64_t isb_ifrecv, /* ISB_IFRECV 4 */
uint64_t isb_ifdrop, /* ISB_IFDROP 5 */
int *err)
{
struct isb isb;
#ifdef _WIN32
FILETIME now;
#else
struct timeval now;
#endif
struct ws_option option;
uint32_t block_total_length;
uint32_t options_length;
uint64_t timestamp;
#ifdef _WIN32
/*
* Current time, represented as 100-nanosecond intervals since
* January 1, 1601, 00:00:00 UTC.
*
* I think DWORD might be signed, so cast both parts of "now"
* to uint32_t so that the sign bit doesn't get treated specially.
*
* Windows 8 provides GetSystemTimePreciseAsFileTime which we
* might want to use instead.
*/
GetSystemTimeAsFileTime(&now);
timestamp = (((uint64_t)(uint32_t)now.dwHighDateTime) << 32) +
(uint32_t)now.dwLowDateTime;
/*
* Convert to same thing but as 1-microsecond, i.e. 1000-nanosecond,
* intervals.
*/
timestamp /= 10;
/*
* Subtract difference, in microseconds, between January 1, 1601
* 00:00:00 UTC and January 1, 1970, 00:00:00 UTC.
*/
timestamp -= EPOCH_DELTA_1601_01_01_00_00_00_UTC*1000000;
#else
/*
* Current time, represented as seconds and microseconds since
* January 1, 1970, 00:00:00 UTC.
*/
gettimeofday(&now, NULL);
/*
* Convert to delta in microseconds.
*/
timestamp = (uint64_t)(now.tv_sec) * 1000000 +
(uint64_t)(now.tv_usec);
#endif
block_total_length = (uint32_t)(sizeof(struct isb) + sizeof(uint32_t));
options_length = 0;
if (isb_ifrecv != UINT64_MAX) {
options_length += (uint32_t)(sizeof(struct ws_option) +
sizeof(uint64_t));
}
if (isb_ifdrop != UINT64_MAX) {
options_length += (uint32_t)(sizeof(struct ws_option) +
sizeof(uint64_t));
}
/* OPT_COMMENT */
options_length += pcapng_count_string_option(comment);
if (isb_starttime !=0) {
options_length += (uint32_t)(sizeof(struct ws_option) +
sizeof(uint64_t)); /* ISB_STARTTIME */
}
if (isb_endtime !=0) {
options_length += (uint32_t)(sizeof(struct ws_option) +
sizeof(uint64_t)); /* ISB_ENDTIME */
}
/* If we have options add size of end-of-options */
if (options_length != 0) {
options_length += (uint32_t)sizeof(struct ws_option);
}
block_total_length += options_length;
isb.block_type = INTERFACE_STATISTICS_BLOCK_TYPE;
isb.block_total_length = block_total_length;
isb.interface_id = interface_id;
isb.timestamp_high = (uint32_t)((timestamp>>32) & 0xffffffff);
isb.timestamp_low = (uint32_t)(timestamp & 0xffffffff);
if (!write_to_file(pfile, (const uint8_t*)&isb, sizeof(struct isb), bytes_written, err))
return false;
/* write comment string if applicable */
if (!pcapng_write_string_option(pfile, OPT_COMMENT, comment,
bytes_written, err))
return false;
if (isb_starttime !=0) {
uint32_t high, low;
option.type = ISB_STARTTIME;
option.value_length = sizeof(uint64_t);
high = (uint32_t)((isb_starttime>>32) & 0xffffffff);
low = (uint32_t)(isb_starttime & 0xffffffff);
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)&high, sizeof(uint32_t), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)&low, sizeof(uint32_t), bytes_written, err))
return false;
}
if (isb_endtime !=0) {
uint32_t high, low;
option.type = ISB_ENDTIME;
option.value_length = sizeof(uint64_t);
high = (uint32_t)((isb_endtime>>32) & 0xffffffff);
low = (uint32_t)(isb_endtime & 0xffffffff);
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)&high, sizeof(uint32_t), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)&low, sizeof(uint32_t), bytes_written, err))
return false;
}
if (isb_ifrecv != UINT64_MAX) {
option.type = ISB_IFRECV;
option.value_length = sizeof(uint64_t);
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)&isb_ifrecv, sizeof(uint64_t), bytes_written, err))
return false;
}
if (isb_ifdrop != UINT64_MAX) {
option.type = ISB_IFDROP;
option.value_length = sizeof(uint64_t);
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
if (!write_to_file(pfile, (const uint8_t*)&isb_ifdrop, sizeof(uint64_t), bytes_written, err))
return false;
}
if (options_length != 0) {
/* write end of options */
option.type = OPT_ENDOFOPT;
option.value_length = 0;
if (!write_to_file(pfile, (const uint8_t*)&option, sizeof(struct ws_option), bytes_written, err))
return false;
}
return write_to_file(pfile, (const uint8_t*)&block_total_length, sizeof(uint32_t), bytes_written, err);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 8
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=8 tabstop=8 expandtab:
* :indentSize=8:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/writecap/pcapio.h | /** @file
*
* Declarations of our own routines for writing pcap and pcapng files.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* Derived from code in the Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/* Writing pcap files */
/** Write the file header to a dump file.
Returns true on success, false on failure.
Sets "*err" to an error code, or 0 for a short write, on failure*/
extern bool
libpcap_write_file_header(FILE* pfile, int linktype, int snaplen,
bool ts_nsecs, uint64_t *bytes_written, int *err);
/** Write a record for a packet to a dump file.
Returns true on success, false on failure. */
extern bool
libpcap_write_packet(FILE* pfile,
time_t sec, uint32_t usec,
uint32_t caplen, uint32_t len,
const uint8_t *pd,
uint64_t *bytes_written, int *err);
/* Writing pcapng files */
/* Write a pre-formatted pcapng block */
extern bool
pcapng_write_block(FILE* pfile,
const uint8_t *data,
uint32_t block_total_length,
uint64_t *bytes_written,
int *err);
/** Write a section header block (SHB)
*
*/
extern bool
pcapng_write_section_header_block(FILE* pfile, /**< Write information */
GPtrArray *comments, /**< Comments on the section, Optinon 1 opt_comment
* UTF-8 strings containing comments that areassociated to the current block.
*/
const char *hw, /**< HW, Optinon 2 shb_hardware
* An UTF-8 string containing the description of the hardware used to create this section.
*/
const char *os, /**< Operating system name, Optinon 3 shb_os
* An UTF-8 string containing the name of the operating system used to create this section.
*/
const char *appname, /**< Application name, Optinon 4 shb_userappl
* An UTF-8 string containing the name of the application used to create this section.
*/
uint64_t section_length, /**< Length of section */
uint64_t *bytes_written, /**< Number of written bytes */
int *err /**< Error type */
);
extern bool
pcapng_write_interface_description_block(FILE* pfile,
const char *comment, /* OPT_COMMENT 1 */
const char *name, /* IDB_NAME 2 */
const char *descr, /* IDB_DESCRIPTION 3 */
const char *filter, /* IDB_FILTER 11 */
const char *os, /* IDB_OS 12 */
const char *hardware, /* IDB_HARDWARE 15 */
int link_type,
int snap_len,
uint64_t *bytes_written,
uint64_t if_speed, /* IDB_IF_SPEED 8 */
uint8_t tsresol, /* IDB_TSRESOL 9 */
int *err);
extern bool
pcapng_write_interface_statistics_block(FILE* pfile,
uint32_t interface_id,
uint64_t *bytes_written,
const char *comment, /* OPT_COMMENT 1 */
uint64_t isb_starttime, /* ISB_STARTTIME 2 */
uint64_t isb_endtime, /* ISB_ENDTIME 3 */
uint64_t isb_ifrecv, /* ISB_IFRECV 4 */
uint64_t isb_ifdrop, /* ISB_IFDROP 5 */
int *err);
extern bool
pcapng_write_enhanced_packet_block(FILE* pfile,
const char *comment,
time_t sec, uint32_t usec,
uint32_t caplen, uint32_t len,
uint32_t interface_id,
unsigned ts_mul,
const uint8_t *pd,
uint32_t flags,
uint64_t *bytes_written,
int *err);
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
wireshark/wsutil/.editorconfig | #
# Editor configuration
#
# https://editorconfig.org/
#
[adler32.[ch]]
indent_size = 2
[aes.[ch]]
indent_style = tab
indent_size = tab
[dot11decrypt_wep.[ch]]
indent_style = tab
indent_size = tab
[base64.[ch]]
indent_style = tab
indent_size = tab
[bitswap.[ch]]
indent_size = 2
[buffer.[ch]]
indent_style = tab
indent_size = tab
[cfutils.[ch]]
indent_style = tab
indent_size = tab
[clopts_common.[ch]]
indent_size = 2
[crash_info.[ch]]
indent_style = tab
indent_size = tab
[crc10.[ch]]
indent_style = tab
indent_size = tab
[crc32.[ch]]
indent_style = tab
indent_size = tab
[des.[ch]]
indent_style = tab
indent_size = tab
[g711.[ch]]
indent_style = tab
indent_size = tab
[interface.[ch]]
indent_style = tab
indent_size = tab
[jsmn.[ch]]
indent_size = 8
[md4.[ch]]
indent_style = tab
indent_size = tab
[mpeg-audio.[ch]]
indent_style = tab
indent_size = tab
[os_version_info.[ch]]
indent_style = tab
indent_size = tab
[privileges.[ch]]
indent_style = tab
indent_size = tab
[rc4.[ch]]
indent_size = 2
[report_err.[ch]]
indent_style = tab
indent_size = tab
[strptime.[ch]]
indent_size = 2
[tempfile.[ch]]
indent_size = 2
[time_util.[ch]]
indent_style = tab
indent_size = tab
[to_str.[ch]]
indent_style = tab
indent_size = tab
[type_util.[ch]]
indent_size = 2
[u3.[ch]]
indent_size = 2
[ws_getopt.[ch]]
indent_style = tab
indent_size = tab
[ws_mempbrk.[ch]]
indent_style = tab
indent_size = tab
[ws_mempbrk_sse42.[ch]]
indent_size = 2
[wsgetopt.[ch]]
indent_size = 2 |
|
C | wireshark/wsutil/802_11-utils.c | /* 802_11-utils.c
* 802.11 utility definitions
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2007 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "802_11-utils.h"
typedef struct freq_cvt_s {
unsigned fmin; /* Minimum frequency in MHz */
unsigned fmax; /* Maximum frequency in MHz */
int cmin; /* Minimum/base channel */
bool is_bg; /* B/G channel? */
} freq_cvt_t;
#define FREQ_STEP 5 /* MHz. This seems to be consistent, thankfully */
/*
* XXX - Japanese channels 182 through 196 actually have center
* frequencies that are off by 2.5 MHz from these values, according
* to the IEEE standard, although the table in ARIB STD T-71 version 5.2:
*
* http://www.arib.or.jp/english/html/overview/doc/1-STD-T71v5_2.pdf
*
* section 5.3.8.3.3 doesn't show that.
*
* XXX - what about the U.S. public safety 4.9 GHz band?
*
* XXX - what about 802.11ad?
*/
static freq_cvt_t freq_cvt[] = {
{ 2412, 2472, 1, true }, /* IEEE Std 802.11-2020: Section 15.4.4.3 and Annex E */
{ 2484, 2484, 14, true }, /* IEEE Std 802.11-2020: Section 15.4.4.3 and Annex E */
{ 5000, 5925, 0, false }, /* IEEE Std 802.11-2020: Annex E */
{ 5950, 7125, 0, false }, /* IEEE Std 802.11ax-2021: Annex E */
{ 4910, 4980, 182, false },
};
#define NUM_FREQ_CVT (sizeof(freq_cvt) / sizeof(freq_cvt_t))
#define MAX_CHANNEL(fc) ( (int) ((fc.fmax - fc.fmin) / FREQ_STEP) + fc.cmin )
/*
* Get channel number given a Frequency
*/
int
ieee80211_mhz_to_chan(unsigned freq) {
unsigned i;
for (i = 0; i < NUM_FREQ_CVT; i++) {
if (freq >= freq_cvt[i].fmin && freq <= freq_cvt[i].fmax) {
return ((freq - freq_cvt[i].fmin) / FREQ_STEP) + freq_cvt[i].cmin;
}
}
return -1;
}
/*
* Get Frequency given a Channel number
*
* XXX - Because channel numbering schemes for 2.4 and 5 overlap with 6 GHz,
* this function may not return the correct channel. For example, the frequency
* for channel 1 in 2.4 GHz band is 2412 MHz, while the frequency for channel 1
* in the 6 GHz band is 5955 MHz. To resolve this problem, this function needs
* to take a starting frequency to convert channel to frequencies correctly.
* Unfortunately, this is not possible in some cases, so for now, the order on
* which frequency ranges are defined will favor 2.4 and 5 GHz over 6 GHz.
*/
unsigned
ieee80211_chan_to_mhz(int chan, bool is_bg) {
unsigned i;
for (i = 0; i < NUM_FREQ_CVT; i++) {
if (is_bg == freq_cvt[i].is_bg &&
chan >= freq_cvt[i].cmin && chan <= MAX_CHANNEL(freq_cvt[i])) {
return ((chan - freq_cvt[i].cmin) * FREQ_STEP) + freq_cvt[i].fmin;
}
}
return 0;
}
/*
* Get channel representation string given a Frequency
*/
char*
ieee80211_mhz_to_str(unsigned freq){
int chan = ieee80211_mhz_to_chan(freq);
bool is_bg = FREQ_IS_BG(freq);
if (chan < 0) {
return ws_strdup_printf("%u", freq);
} else {
return ws_strdup_printf("%u [%s %u]", freq, is_bg ? "BG" : "A",
chan);
}
}
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/802_11-utils.h | /* 802_11-utils.h
* 802.11 utility definitions
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2007 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __802_11_UTILS_H__
#define __802_11_UTILS_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/** @file
* 802.11 utilities.
*/
/**
* Given a center frequency in MHz, return a channel number.
* @param freq Frequency in MHz.
* @return The equivalent channel or -1 if no match is found.
*/
WS_DLL_PUBLIC
int
ieee80211_mhz_to_chan(unsigned freq);
/**
* Given an 802.11 channel number and a band type, return a center frequency.
* @param chan Channel number
* @param is_bg true if the channel is a b/g channel, false otherwise.
* @return The equivalent frequency or 0 if no match is found.
*/
WS_DLL_PUBLIC
unsigned
ieee80211_chan_to_mhz(int chan, bool is_bg);
/**
* Given an 802.11 channel center frequency in MHz, return a string
* representation.
* @param freq Frequench in MHz.
* @return A string showing the frequency, channel number, and type.
* The string must be freed with g_free() after use.
*/
WS_DLL_PUBLIC
char*
ieee80211_mhz_to_str(unsigned freq);
/* Should this be "(freq < 4920)", or something else? */
#define FREQ_IS_BG(freq) ((freq) <= 2484)
#define CHAN_IS_BG(chan) ((chan) <= 14)
/*
* Test whether a data rate is an {HR}/DSSS (legacy DSSS/11b) data rate
* and whether it's an OFDM (11a/11g OFDM mode) data rate.
*
* rate is in units of 500 Kb/s.
*
* The 22 and 33 Mb/s rates for DSSS use Packet Binary Convolutional
* Coding (PBCC). That was provided by Texas Instruments as 11b+,
* and was in section 19.6 "ERP-PBCC operation specifications" of
* IEEE Std 802.11g-2003, and sections 18.4.6.6 "DSSS/PBCC data modulation
* and modulation rate (optional)" and 19.6 "ERP-PBCC operation
* specifications" of IEEE Std 802.11-2007, and sections 17.4.6.7 "DSSS/PBCC
* data modulation and modulation rate (optional)" and 19.6 "ERP-PBCC
* operation specifications" of IEEE Std 802.11-2012, marked as optional
* in both cases, but is not present in IEEE Std 802.11-2016.
*
* (Note: not to be confused with "peanut butter and chocolate chips":
*
* https://www.bigoven.com/recipe/peanut-butter-chocolate-chip-cookies-pbcc-cookies/186266
*
* :-))
*/
#define RATE_IS_DSSS(rate) \
((rate) == 2 /* 1 Mb/s */ || \
(rate) == 4 /* 2 Mb/s */ || \
(rate) == 11 /* 5.5 Mb/s */ || \
(rate) == 22 /* 11 Mb/s */ || \
(rate) == 44 /* 22 Mb/s */ || \
(rate) == 66 /* 33 Mb/s */)
#define RATE_IS_OFDM(rate) \
((rate) == 12 /* 6 Mb/s */ || \
(rate) == 18 /* 9 Mb/s */ || \
(rate) == 24 /* 12 Mb/s */ || \
(rate) == 36 /* 18 Mb/s */ || \
(rate) == 48 /* 24 Mb/s */ || \
(rate) == 72 /* 36 Mb/s */ || \
(rate) == 96 /* 48 Mb/s */ || \
(rate) == 108 /* 54 Mb/s */)
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __802_11_UTILS_H__ */
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C | wireshark/wsutil/adler32.c | /* adler32.c
* Compute the Adler32 checksum (RFC 1950)
* 2003 Tomas Kukosa
* Based on code from RFC 1950 (Chapter 9. Appendix: Sample code)
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <wsutil/adler32.h>
#include <string.h>
#define BASE 65521 /* largest prime smaller than 65536 */
/*--- update_adler32 --------------------------------------------------------*/
guint32 update_adler32(guint32 adler, const guint8 *buf, size_t len)
{
guint32 s1 = adler & 0xffff;
guint32 s2 = (adler >> 16) & 0xffff;
size_t n;
for (n = 0; n < len; n++) {
s1 = (s1 + buf[n]) % BASE;
s2 = (s2 + s1) % BASE;
}
return (s2 << 16) + s1;
}
/*--- adler32 ---------------------------------------------------------------*/
guint32 adler32_bytes(const guint8 *buf, size_t len)
{
return update_adler32(1, buf, len);
}
/*--- adler32_str -----------------------------------------------------------*/
guint32 adler32_str(const char *buf)
{
return update_adler32(1, (const guint8*)buf, strlen(buf));
}
/*---------------------------------------------------------------------------*/
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local Variables:
* c-basic-offset: 2
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=2 tabstop=8 expandtab:
* :indentSize=2:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/adler32.h | /** @file
* Compute the Adler32 checksum (RFC 1950)
* 2003 Tomas Kukosa
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef ADLER32_H
#define ADLER32_H
#include <wireshark.h>
#ifdef __cplusplus
extern "C"{
#endif
WS_DLL_PUBLIC guint32 update_adler32(guint32 adler, const guint8 *buf, size_t len);
WS_DLL_PUBLIC guint32 adler32_bytes(const guint8 *buf, size_t len);
WS_DLL_PUBLIC guint32 adler32_str(const char *buf);
#ifdef __cplusplus
}
#endif
#endif /* ADLER32_H */ |
C | wireshark/wsutil/base32.c | /* base32.c
* Base-32 conversion
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "base32.h"
#include <string.h>
/*
* Cjdns style base32 encoding
*/
/** Returned by ws_base32_encode() if the input is not valid base32. */
#define Base32_BAD_INPUT -1
/** Returned by ws_base32_encode() if the output buffer is too small. */
#define Base32_TOO_BIG -2
int ws_base32_decode(guint8* output, const guint32 outputLength,
const guint8* in, const guint32 inputLength)
{
guint32 outIndex = 0;
guint32 inIndex = 0;
guint32 work = 0;
guint32 bits = 0;
static const guint8* kChars = (guint8*) "0123456789bcdfghjklmnpqrstuvwxyz";
while (inIndex < inputLength) {
work |= ((unsigned) in[inIndex++]) << bits;
bits += 8;
while (bits >= 5) {
if (outIndex >= outputLength) {
return Base32_TOO_BIG;
}
output[outIndex++] = kChars[work & 31];
bits -= 5;
work >>= 5;
}
}
if (bits) {
if (outIndex >= outputLength) {
return Base32_TOO_BIG;
}
output[outIndex++] = kChars[work & 31];
}
if (outIndex < outputLength) {
output[outIndex] = '\0';
}
return outIndex;
}
/*
* 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:
*/ |
C/C++ | wireshark/wsutil/base32.h | /** @file
* Base-32 conversion
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __BASE32_H__
#define __BASE32_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/** Returned by base32_decode() if the input is not valid base32. */
#define Base32_BAD_INPUT -1
/** Returned by base32_decode() if the output buffer is too small. */
#define Base32_TOO_BIG -2
/* Encoding of a base32 byte array */
WS_DLL_PUBLIC
int ws_base32_decode(guint8* output, const guint32 outputLength,
const guint8* in, const guint32 inputLength);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __BASE32_H__ */
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C | wireshark/wsutil/bitswap.c | /* bitswap.c
* Table of bit-swapped values of bytes
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "bitswap.h"
/* "swaptab[i]" is the value of "i" with the bits reversed. */
static const guint8 swaptab[256] =
{
0x00, 0x80, 0x40, 0xc0, 0x20, 0xa0, 0x60, 0xe0,
0x10, 0x90, 0x50, 0xd0, 0x30, 0xb0, 0x70, 0xf0,
0x08, 0x88, 0x48, 0xc8, 0x28, 0xa8, 0x68, 0xe8,
0x18, 0x98, 0x58, 0xd8, 0x38, 0xb8, 0x78, 0xf8,
0x04, 0x84, 0x44, 0xc4, 0x24, 0xa4, 0x64, 0xe4,
0x14, 0x94, 0x54, 0xd4, 0x34, 0xb4, 0x74, 0xf4,
0x0c, 0x8c, 0x4c, 0xcc, 0x2c, 0xac, 0x6c, 0xec,
0x1c, 0x9c, 0x5c, 0xdc, 0x3c, 0xbc, 0x7c, 0xfc,
0x02, 0x82, 0x42, 0xc2, 0x22, 0xa2, 0x62, 0xe2,
0x12, 0x92, 0x52, 0xd2, 0x32, 0xb2, 0x72, 0xf2,
0x0a, 0x8a, 0x4a, 0xca, 0x2a, 0xaa, 0x6a, 0xea,
0x1a, 0x9a, 0x5a, 0xda, 0x3a, 0xba, 0x7a, 0xfa,
0x06, 0x86, 0x46, 0xc6, 0x26, 0xa6, 0x66, 0xe6,
0x16, 0x96, 0x56, 0xd6, 0x36, 0xb6, 0x76, 0xf6,
0x0e, 0x8e, 0x4e, 0xce, 0x2e, 0xae, 0x6e, 0xee,
0x1e, 0x9e, 0x5e, 0xde, 0x3e, 0xbe, 0x7e, 0xfe,
0x01, 0x81, 0x41, 0xc1, 0x21, 0xa1, 0x61, 0xe1,
0x11, 0x91, 0x51, 0xd1, 0x31, 0xb1, 0x71, 0xf1,
0x09, 0x89, 0x49, 0xc9, 0x29, 0xa9, 0x69, 0xe9,
0x19, 0x99, 0x59, 0xd9, 0x39, 0xb9, 0x79, 0xf9,
0x05, 0x85, 0x45, 0xc5, 0x25, 0xa5, 0x65, 0xe5,
0x15, 0x95, 0x55, 0xd5, 0x35, 0xb5, 0x75, 0xf5,
0x0d, 0x8d, 0x4d, 0xcd, 0x2d, 0xad, 0x6d, 0xed,
0x1d, 0x9d, 0x5d, 0xdd, 0x3d, 0xbd, 0x7d, 0xfd,
0x03, 0x83, 0x43, 0xc3, 0x23, 0xa3, 0x63, 0xe3,
0x13, 0x93, 0x53, 0xd3, 0x33, 0xb3, 0x73, 0xf3,
0x0b, 0x8b, 0x4b, 0xcb, 0x2b, 0xab, 0x6b, 0xeb,
0x1b, 0x9b, 0x5b, 0xdb, 0x3b, 0xbb, 0x7b, 0xfb,
0x07, 0x87, 0x47, 0xc7, 0x27, 0xa7, 0x67, 0xe7,
0x17, 0x97, 0x57, 0xd7, 0x37, 0xb7, 0x77, 0xf7,
0x0f, 0x8f, 0x4f, 0xcf, 0x2f, 0xaf, 0x6f, 0xef,
0x1f, 0x9f, 0x5f, 0xdf, 0x3f, 0xbf, 0x7f, 0xff,
};
void bitswap_buf_inplace(guint8 *buf, size_t len)
{
size_t i;
for (i = 0; i < len; i++)
buf[i] = swaptab[buf[i]];
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local Variables:
* c-basic-offset: 2
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=2 tabstop=8 expandtab:
* :indentSize=2:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/bitswap.h | /** @file
* Macro to bitswap a byte by looking it up in a table
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __BITSWAP_H__
#define __BITSWAP_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
WS_DLL_PUBLIC void bitswap_buf_inplace(guint8 *buf, size_t len);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* bitswap.h */ |
C/C++ | wireshark/wsutil/bits_count_ones.h | /** @file
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WSUTIL_BITS_COUNT_ONES_H__
#define __WSUTIL_BITS_COUNT_ONES_H__
#include <glib.h>
/*
* The variable-precision SWAR algorithm is an interesting way to count
* the number of bits set in an integer:
*
* https://www.playingwithpointers.com/blog/swar.html
*
* See
*
* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=36041
* https://danluu.com/assembly-intrinsics/
*
* for discussions of various forms of population-counting code on x86.
*
* See
*
* https://docs.microsoft.com/en-us/cpp/intrinsics/popcnt16-popcnt-popcnt64
*
* for MSVC's population count intrinsics.
*
* Note that not all x86 processors support the POPCOUNT instruction.
*
* Other CPUs may have population count instructions as well.
*/
static inline int
ws_count_ones(const guint64 x)
{
guint64 bits = x;
bits = bits - ((bits >> 1) & G_GUINT64_CONSTANT(0x5555555555555555));
bits = (bits & G_GUINT64_CONSTANT(0x3333333333333333)) + ((bits >> 2) & G_GUINT64_CONSTANT(0x3333333333333333));
bits = (bits + (bits >> 4)) & G_GUINT64_CONSTANT(0x0F0F0F0F0F0F0F0F);
return (int)((bits * G_GUINT64_CONSTANT(0x0101010101010101)) >> 56);
}
#endif /* __WSUTIL_BITS_COUNT_ONES_H__ */ |
C/C++ | wireshark/wsutil/bits_ctz.h | /** @file
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WSUTIL_BITS_CTZ_H__
#define __WSUTIL_BITS_CTZ_H__
#include <glib.h>
/* ws_ctz == trailing zeros == position of lowest set bit [0..63] */
/* ws_ilog2 == position of highest set bit == 63 - leading zeros [0..63] */
/* The return value of both ws_ctz and ws_ilog2 is undefined for x == 0 */
#if defined(__GNUC__) && ((__GNUC__ > 3) || (__GNUC__ == 3 && __GNUC_MINOR__ >= 4))
static inline int
ws_ctz(guint64 x)
{
return __builtin_ctzll(x);
}
static inline int
ws_ilog2(guint64 x)
{
return 63 - __builtin_clzll(x);
}
#else
static inline int
__ws_ctz32(guint32 x)
{
/* From http://graphics.stanford.edu/~seander/bithacks.html#ZerosOnRightMultLookup */
static const guint8 table[32] = {
0, 1, 28, 2, 29, 14, 24, 3, 30, 22, 20, 15, 25, 17, 4, 8,
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
};
return table[((guint32)((x & -(gint32)x) * 0x077CB531U)) >> 27];
}
static inline int
ws_ctz(guint64 x)
{
guint32 hi = x >> 32;
guint32 lo = (guint32) x;
if (lo == 0)
return 32 + __ws_ctz32(hi);
else
return __ws_ctz32(lo);
}
static inline int
__ws_ilog2_32(guint32 x)
{
/* From http://graphics.stanford.edu/~seander/bithacks.html#IntegerLogDeBruijn */
static const guint8 table[32] = {
0, 9, 1, 10, 13, 21, 2, 29, 11, 14, 16, 18, 22, 25, 3, 30,
8, 12, 20, 28, 15, 17, 24, 7, 19, 27, 23, 6, 26, 5, 4, 31
};
x |= x >> 1;
x |= x >> 2;
x |= x >> 4;
x |= x >> 8;
x |= x >> 16;
return table[((guint32)(x * 0x07C4ACDDU)) >> 27];
}
static inline int
ws_ilog2(guint64 x)
{
guint32 hi = x >> 32;
guint32 lo = (guint32) x;
if (hi == 0)
return __ws_ilog2_32(lo);
else
return 32 + __ws_ilog2_32(hi);
}
#endif
#endif /* __WSUTIL_BITS_CTZ_H__ */ |
C | wireshark/wsutil/buffer.c | /* buffer.c
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#define WS_LOG_DOMAIN LOG_DOMAIN_WSUTIL
#include "buffer.h"
#include <stdlib.h>
#include <string.h>
#include <wsutil/ws_assert.h>
#include <wsutil/wslog.h>
#define SMALL_BUFFER_SIZE (2 * 1024) /* Everyone still uses 1500 byte frames, right? */
static GPtrArray *small_buffers = NULL; /* Guaranteed to be at least SMALL_BUFFER_SIZE */
/* XXX - Add medium and large buffers? */
/* Initializes a buffer with a certain amount of allocated space */
void
ws_buffer_init(Buffer* buffer, gsize space)
{
ws_assert(buffer);
if (G_UNLIKELY(!small_buffers)) small_buffers = g_ptr_array_sized_new(1024);
if (space <= SMALL_BUFFER_SIZE) {
if (small_buffers->len > 0) {
buffer->data = (guint8*) g_ptr_array_remove_index(small_buffers, small_buffers->len - 1);
ws_assert(buffer->data);
} else {
buffer->data = (guint8*)g_malloc(SMALL_BUFFER_SIZE);
}
buffer->allocated = SMALL_BUFFER_SIZE;
} else {
buffer->data = (guint8*)g_malloc(space);
buffer->allocated = space;
}
buffer->start = 0;
buffer->first_free = 0;
}
/* Frees the memory used by a buffer */
void
ws_buffer_free(Buffer* buffer)
{
ws_assert(buffer);
if (buffer->allocated == SMALL_BUFFER_SIZE) {
ws_assert(buffer->data);
g_ptr_array_add(small_buffers, buffer->data);
} else {
g_free(buffer->data);
}
buffer->allocated = 0;
buffer->data = NULL;
}
/* Assures that there are 'space' bytes at the end of the used space
so that another routine can copy directly into the buffer space. After
doing that, the routine will also want to run
ws_buffer_increase_length(). */
void
ws_buffer_assure_space(Buffer* buffer, gsize space)
{
ws_assert(buffer);
gsize available_at_end = buffer->allocated - buffer->first_free;
gsize space_used;
gboolean space_at_beginning;
/* If we've got the space already, good! */
if (space <= available_at_end) {
return;
}
/* Maybe we don't have the space available at the end, but we would
if we moved the used space back to the beginning of the
allocation. The buffer could have become fragmented through lots
of calls to ws_buffer_remove_start(). I'm using buffer->start as the
same as 'available_at_start' in this comparison. */
/* or maybe there's just no more room. */
space_at_beginning = buffer->start >= space;
if (space_at_beginning || buffer->start > 0) {
space_used = buffer->first_free - buffer->start;
/* this memory copy better be safe for overlapping memory regions! */
memmove(buffer->data, buffer->data + buffer->start, space_used);
buffer->start = 0;
buffer->first_free = space_used;
}
/*if (buffer->start >= space) {*/
if (space_at_beginning) {
return;
}
/* We'll allocate more space */
buffer->allocated += space + 1024;
buffer->data = (guint8*)g_realloc(buffer->data, buffer->allocated);
}
void
ws_buffer_append(Buffer* buffer, guint8 *from, gsize bytes)
{
ws_assert(buffer);
ws_buffer_assure_space(buffer, bytes);
memcpy(buffer->data + buffer->first_free, from, bytes);
buffer->first_free += bytes;
}
void
ws_buffer_remove_start(Buffer* buffer, gsize bytes)
{
ws_assert(buffer);
if (buffer->start + bytes > buffer->first_free) {
ws_error("ws_buffer_remove_start trying to remove %" PRIu64 " bytes. s=%" PRIu64 " ff=%" PRIu64 "!\n",
(guint64)bytes, (guint64)buffer->start,
(guint64)buffer->first_free);
/** ws_error() does an abort() and thus never returns **/
}
buffer->start += bytes;
if (buffer->start == buffer->first_free) {
buffer->start = 0;
buffer->first_free = 0;
}
}
#ifndef SOME_FUNCTIONS_ARE_DEFINES
void
ws_buffer_clean(Buffer* buffer)
{
ws_assert(buffer);
ws_buffer_remove_start(buffer, ws_buffer_length(buffer));
}
#endif
#ifndef SOME_FUNCTIONS_ARE_DEFINES
void
ws_buffer_increase_length(Buffer* buffer, gsize bytes)
{
ws_assert(buffer);
buffer->first_free += bytes;
}
#endif
#ifndef SOME_FUNCTIONS_ARE_DEFINES
gsize
ws_buffer_length(Buffer* buffer)
{
ws_assert(buffer);
return buffer->first_free - buffer->start;
}
#endif
#ifndef SOME_FUNCTIONS_ARE_DEFINES
guint8 *
ws_buffer_start_ptr(Buffer* buffer)
{
ws_assert(buffer);
return buffer->data + buffer->start;
}
#endif
#ifndef SOME_FUNCTIONS_ARE_DEFINES
guint8 *
ws_buffer_end_ptr(Buffer* buffer)
{
ws_assert(buffer);
return buffer->data + buffer->first_free;
}
#endif
#ifndef SOME_FUNCTIONS_ARE_DEFINES
void
ws_buffer_append_buffer(Buffer* buffer, Buffer* src_buffer)
{
ws_assert(buffer);
ws_buffer_append(buffer, ws_buffer_start_ptr(src_buffer), ws_buffer_length(src_buffer));
}
#endif
void
ws_buffer_cleanup(void)
{
if (small_buffers) {
g_ptr_array_set_free_func(small_buffers, g_free);
g_ptr_array_free(small_buffers, TRUE);
small_buffers = NULL;
}
}
/*
* 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:
*/ |
C/C++ | wireshark/wsutil/buffer.h | /** @file
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __W_BUFFER_H__
#define __W_BUFFER_H__
#include <glib.h>
#include "ws_symbol_export.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define SOME_FUNCTIONS_ARE_DEFINES
typedef struct Buffer {
guint8 *data;
gsize allocated;
gsize start;
gsize first_free;
} Buffer;
WS_DLL_PUBLIC
void ws_buffer_init(Buffer* buffer, gsize space);
WS_DLL_PUBLIC
void ws_buffer_free(Buffer* buffer);
WS_DLL_PUBLIC
void ws_buffer_assure_space(Buffer* buffer, gsize space);
WS_DLL_PUBLIC
void ws_buffer_append(Buffer* buffer, guint8 *from, gsize bytes);
WS_DLL_PUBLIC
void ws_buffer_remove_start(Buffer* buffer, gsize bytes);
WS_DLL_PUBLIC
void ws_buffer_cleanup(void);
#ifdef SOME_FUNCTIONS_ARE_DEFINES
# define ws_buffer_clean(buffer) ws_buffer_remove_start((buffer), ws_buffer_length(buffer))
# define ws_buffer_increase_length(buffer,bytes) (buffer)->first_free += (bytes)
# define ws_buffer_length(buffer) ((buffer)->first_free - (buffer)->start)
# define ws_buffer_start_ptr(buffer) ((buffer)->data + (buffer)->start)
# define ws_buffer_end_ptr(buffer) ((buffer)->data + (buffer)->first_free)
# define ws_buffer_append_buffer(buffer,src_buffer) ws_buffer_append((buffer), ws_buffer_start_ptr(src_buffer), ws_buffer_length(src_buffer))
#else
WS_DLL_PUBLIC
void ws_buffer_clean(Buffer* buffer);
WS_DLL_PUBLIC
void ws_buffer_increase_length(Buffer* buffer, gsize bytes);
WS_DLL_PUBLIC
gsize ws_buffer_length(Buffer* buffer);
WS_DLL_PUBLIC
guint8* ws_buffer_start_ptr(Buffer* buffer);
WS_DLL_PUBLIC
guint8* ws_buffer_end_ptr(Buffer* buffer);
WS_DLL_PUBLIC
void ws_buffer_append_buffer(Buffer* buffer, Buffer* src_buffer);
#endif
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif |
C | wireshark/wsutil/cfutils.c | /* cfutils.c
* Routines to work around deficiencies in Core Foundation, such as the
* lack of a routine to convert a CFString to a C string of arbitrary
* size.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2001 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <glib.h>
#include <CoreFoundation/CoreFoundation.h>
#include <wsutil/cfutils.h>
/*
* Convert a CFString to a UTF-8-encoded C string; the resulting string
* is allocated with g_malloc(). Returns NULL if the conversion fails.
*/
char *
CFString_to_C_string(CFStringRef cfstring)
{
CFIndex string_len;
char *string;
string_len = CFStringGetMaximumSizeForEncoding(CFStringGetLength(cfstring),
kCFStringEncodingUTF8);
string = (char *)g_malloc(string_len + 1);
if (!CFStringGetCString(cfstring, string, string_len + 1,
kCFStringEncodingUTF8)) {
g_free(string);
return NULL;
}
return string;
}
/*
* 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:
*/ |
C/C++ | wireshark/wsutil/cfutils.h | /** @file
* Declarations of routines to work around deficiencies in Core Foundation,
* such as the lack of a routine to convert a CFString to a C string of
* arbitrary size.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2001 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WSUTIL_CFUTILS_H__
#define __WSUTIL_CFUTILS_H__
#include "ws_symbol_export.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* Convert a CFString to a g_malloc()ated C string.
*/
WS_DLL_PUBLIC char *CFString_to_C_string(CFStringRef cfstring);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __WSUTIL_CFUTILS_H__ */ |
C | wireshark/wsutil/clopts_common.c | /* clopts_common.c
* Handle command-line arguments common to various programs
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "clopts_common.h"
#include <stdlib.h>
#include <errno.h>
#include <wsutil/strtoi.h>
#include <wsutil/cmdarg_err.h>
int
get_natural_int(const char *string, const char *name)
{
gint32 number;
if (!ws_strtoi32(string, NULL, &number)) {
if (errno == EINVAL) {
cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string);
exit(1);
}
if (number < 0) {
cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
exit(1);
}
cmdarg_err("The specified %s \"%s\" is too large (greater than %d)",
name, string, number);
exit(1);
}
if (number < 0) {
cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
exit(1);
}
return (int)number;
}
int
get_positive_int(const char *string, const char *name)
{
int number;
number = get_natural_int(string, name);
if (number == 0) {
cmdarg_err("The specified %s is zero", name);
exit(1);
}
return number;
}
guint32
get_guint32(const char *string, const char *name)
{
guint32 number;
if (!ws_strtou32(string, NULL, &number)) {
if (errno == EINVAL) {
cmdarg_err("The specified %s \"%s\" isn't a decimal number", name, string);
exit(1);
}
cmdarg_err("The specified %s \"%s\" is too large (greater than %d)",
name, string, number);
exit(1);
}
return number;
}
guint32
get_nonzero_guint32(const char *string, const char *name)
{
guint32 number;
number = get_guint32(string, name);
if (number == 0) {
cmdarg_err("The specified %s is zero", name);
exit(1);
}
return number;
}
double
get_positive_double(const char *string, const char *name)
{
double number = g_ascii_strtod(string, NULL);
if (errno == EINVAL) {
cmdarg_err("The specified %s \"%s\" isn't a floating point number", name, string);
exit(1);
}
if (number < 0.0) {
cmdarg_err("The specified %s \"%s\" is a negative number", name, string);
exit(1);
}
return number;
} |
C/C++ | wireshark/wsutil/clopts_common.h | /** @file
*
* Handle command-line arguments common to various programs
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CLOPTS_COMMON_H__
#define __CLOPTS_COMMON_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* Long options.
* For long options with no corresponding short options, we define values
* outside the range of ASCII graphic characters, make that the last
* component of the entry for the long option, and have a case for that
* option in the switch statement.
*/
// Base value for capture related long options
#define LONGOPT_BASE_CAPTURE 1000
// Base value for dissector related long options
#define LONGOPT_BASE_DISSECTOR 2000
// Base value for application specific long options
#define LONGOPT_BASE_APPLICATION 3000
// Base value for GUI specific long options
#define LONGOPT_BASE_GUI 4000
WS_DLL_PUBLIC int
get_natural_int(const char *string, const char *name);
WS_DLL_PUBLIC int
get_positive_int(const char *string, const char *name);
WS_DLL_PUBLIC guint32
get_guint32(const char *string, const char *name);
WS_DLL_PUBLIC guint32
get_nonzero_guint32(const char *string, const char *name);
WS_DLL_PUBLIC double
get_positive_double(const char *string, const char *name);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __CLOPTS_COMMON_H__ */ |
Text | wireshark/wsutil/CMakeLists.txt | # CMakeLists.txt
#
# Wireshark - Network traffic analyzer
# By Gerald Combs <[email protected]>
# Copyright 1998 Gerald Combs
#
# SPDX-License-Identifier: GPL-2.0-or-later
#
file(TO_NATIVE_PATH "${CMAKE_INSTALL_PREFIX}" PATH_INSTALL_PREFIX)
string(REPLACE "\\" "\\\\" PATH_INSTALL_PREFIX "${PATH_INSTALL_PREFIX}")
file(TO_NATIVE_PATH "${CMAKE_INSTALL_DATADIR}" PATH_DATA_DIR)
string(REPLACE "\\" "\\\\" PATH_DATA_DIR "${PATH_DATA_DIR}")
file(TO_NATIVE_PATH "${CMAKE_INSTALL_DOCDIR}" PATH_DOC_DIR)
string(REPLACE "\\" "\\\\" PATH_DOC_DIR "${PATH_DOC_DIR}")
file(TO_NATIVE_PATH "${PLUGIN_INSTALL_LIBDIR}" PATH_PLUGIN_DIR)
string(REPLACE "\\" "\\\\" PATH_PLUGIN_DIR "${PATH_PLUGIN_DIR}")
file(TO_NATIVE_PATH "${EXTCAP_INSTALL_LIBDIR}" PATH_EXTCAP_DIR)
string(REPLACE "\\" "\\\\" PATH_EXTCAP_DIR "${PATH_EXTCAP_DIR}")
configure_file(${CMAKE_CURRENT_SOURCE_DIR}/path_config.h.in ${CMAKE_CURRENT_BINARY_DIR}/path_config.h)
include_directories(${CMAKE_CURRENT_BINARY_DIR})
set(WMEM_PUBLIC_HEADERS
wmem/wmem.h
wmem/wmem_array.h
wmem/wmem_core.h
wmem/wmem_list.h
wmem/wmem_map.h
wmem/wmem_miscutl.h
wmem/wmem_multimap.h
wmem/wmem_queue.h
wmem/wmem_stack.h
wmem/wmem_strbuf.h
wmem/wmem_strutl.h
wmem/wmem_tree.h
wmem/wmem_interval_tree.h
wmem/wmem_user_cb.h
)
set(WMEM_HEADER_FILES
${WMEM_PUBLIC_HEADERS}
wmem/wmem_allocator.h
wmem/wmem_allocator_block.h
wmem/wmem_allocator_block_fast.h
wmem/wmem_allocator_simple.h
wmem/wmem_allocator_strict.h
wmem/wmem_interval_tree.h
wmem/wmem_map_int.h
wmem/wmem_tree-int.h
wmem/wmem_user_cb_int.h
)
set(WMEM_FILES
wmem/wmem_array.c
wmem/wmem_core.c
wmem/wmem_allocator_block.c
wmem/wmem_allocator_block_fast.c
wmem/wmem_allocator_simple.c
wmem/wmem_allocator_strict.c
wmem/wmem_interval_tree.c
wmem/wmem_list.c
wmem/wmem_map.c
wmem/wmem_miscutl.c
wmem/wmem_multimap.c
wmem/wmem_stack.c
wmem/wmem_strbuf.c
wmem/wmem_strutl.c
wmem/wmem_tree.c
wmem/wmem_user_cb.c
)
set(WSUTIL_PUBLIC_HEADERS
802_11-utils.h
adler32.h
base32.h
bits_count_ones.h
bits_ctz.h
bitswap.h
buffer.h
clopts_common.h
cmdarg_err.h
codecs.h
color.h
cpu_info.h
crash_info.h
crc5.h
crc6.h
crc7.h
crc8.h
crc10.h
crc11.h
crc16.h
crc16-plain.h
crc32.h
curve25519.h
eax.h
epochs.h
exported_pdu_tlvs.h
feature_list.h
filesystem.h
g711.h
inet_addr.h
inet_ipv4.h
inet_ipv6.h
interface.h
introspection.h
jsmn.h
json_dumper.h
mpeg-audio.h
nstime.h
os_version_info.h
pint.h
please_report_bug.h
pow2.h
privileges.h
processes.h
regex.h
report_message.h
sign_ext.h
sober128.h
socket.h
str_util.h
strnatcmp.h
strtoi.h
tempfile.h
time_util.h
to_str.h
type_util.h
unicode-utils.h
utf8_entities.h
version_info.h
ws_assert.h
ws_cpuid.h
glib-compat.h
ws_getopt.h
ws_mempbrk.h
ws_mempbrk_int.h
ws_pipe.h
ws_roundup.h
ws_return.h
wsgcrypt.h
wsjson.h
wslog.h
xtea.h
)
set(WSUTIL_COMMON_FILES
802_11-utils.c
adler32.c
base32.c
bitswap.c
buffer.c
clopts_common.c
cmdarg_err.c
codecs.c
crash_info.c
crc10.c
crc16.c
crc16-plain.c
crc32.c
crc5.c
crc6.c
crc7.c
crc8.c
crc11.c
curve25519.c
dot11decrypt_wep.c
eax.c
feature_list.c
filesystem.c
filter_files.c
g711.c
inet_addr.c
interface.c
introspection.c
jsmn.c
json_dumper.c
mpeg-audio.c
nstime.c
cpu_info.c
os_version_info.c
please_report_bug.c
privileges.c
regex.c
rsa.c
sober128.c
socket.c
strnatcmp.c
str_util.c
strtoi.c
report_message.c
tempfile.c
time_util.c
to_str.c
type_util.c
unicode-utils.c
version_info.c
ws_getopt.c
ws_mempbrk.c
ws_pipe.c
wsgcrypt.c
wsjson.c
wslog.c
xtea.c
)
if(WIN32)
list(APPEND WSUTIL_COMMON_FILES
console_win32.c
)
endif()
if(ENABLE_PLUGINS)
list(APPEND WSUTIL_COMMON_FILES
plugins.c
)
endif()
set(WSUTIL_FILES
${WMEM_FILES}
${WSUTIL_COMMON_FILES}
)
if(WIN32)
list(APPEND WSUTIL_FILES
file_util.c
win32-utils.c
)
endif(WIN32)
if(HAVE_MACOS_FRAMEWORKS)
list(APPEND WSUTIL_FILES cfutils.c)
endif()
#
# XXX - we're assuming MSVC doesn't require a flag to enable SSE 4.2
# support, and that, if the compiler supports a flag for SSE 4.2
# support, the intrinsics are supported iff we can include the
# <nmmintrin.h> flag.
#
# We only check for the GCC-style -msse4.2 flag and the Sun C
# -xarch=sse4_2 flag.
#
if(CMAKE_C_COMPILER_ID MATCHES "MSVC")
set(COMPILER_CAN_HANDLE_SSE4_2 TRUE)
set(SSE4_2_FLAG "")
elseif(CMAKE_SYSTEM_PROCESSOR MATCHES "i686|x86|x86_64|AMD64")
check_c_compiler_flag(-msse4.2 COMPILER_CAN_HANDLE_SSE4_2)
if(COMPILER_CAN_HANDLE_SSE4_2)
set(SSE4_2_FLAG "-msse4.2")
else()
check_c_compiler_flag(-xarch=sse4_2 COMPILER_CAN_HANDLE_SSE4_2)
if(COMPILER_CAN_HANDLE_SSE4_2)
set(SSE4_2_FLAG "-xarch=sse4_2")
endif()
endif()
else()
set(COMPILE_CAN_HANDLE_SSE4_2 FALSE)
set(SSE4_2_FLAG "")
endif()
if(SSE4_2_FLAG)
message(STATUS "SSE4.2 compiler flag: ${SSE4_2_FLAG}")
else()
message(STATUS "No SSE4.2 compiler flag enabled")
endif()
if(COMPILER_CAN_HANDLE_SSE4_2)
#
# Make sure we have the necessary headers for the SSE4.2 intrinsics
# and that we can use them.
#
# First, check whether we have emmintrin.h and can use it
# *without* the SSE 4.2 flag.
#
check_include_file("emmintrin.h" EMMINTRIN_H_WORKS)
#
# OK, if that works, see whether we have nmmintrin.h and
# can use it *with* the SSE 4.2 flag.
#
if(EMMINTRIN_H_WORKS)
#
# Does this add the SSE4.2 flags to the beginning of
# CFLAGS?
#
# Note that if there's a mix of "enable SSE 4.2" and
# "disable SSE 4.2" flags, this may not indicate that
# we can use the header. That's not a bug, that's a
# feature; the other flags may have been forced by
# the build process, e.g. in Gentoo Linux, and we want
# to check this with whatever flags will actually be
# used when building (see bug 10792).
#
cmake_push_check_state()
set(CMAKE_REQUIRED_FLAGS "${SSE4_2_FLAG}")
check_include_file("nmmintrin.h" HAVE_SSE4_2)
cmake_pop_check_state()
endif()
endif()
if(HAVE_SSE4_2)
list(APPEND WSUTIL_FILES ws_mempbrk_sse42.c)
endif()
if(NOT HAVE_STRPTIME)
list(APPEND WSUTIL_FILES strptime.c)
endif()
if(APPLE)
#
# We assume that APPLE means macOS so that we have the macOS
# frameworks.
#
FIND_LIBRARY (APPLE_CORE_FOUNDATION_LIBRARY CoreFoundation)
endif()
set_source_files_properties(
${WSUTIL_FILES}
PROPERTIES
COMPILE_FLAGS "${WERROR_COMMON_FLAGS}"
)
if (HAVE_SSE4_2)
# TODO with CMake 2.8.12, we could use COMPILE_OPTIONS and just append
# instead of this COMPILE_FLAGS duplication...
set_source_files_properties(
ws_mempbrk_sse42.c
PROPERTIES
COMPILE_FLAGS "${WERROR_COMMON_FLAGS} ${SSE4_2_FLAG}"
)
endif()
if (ENABLE_APPLICATION_BUNDLE)
set_source_files_properties(
filesystem.c
PROPERTIES
COMPILE_FLAGS "${WERROR_COMMON_FLAGS} -DENABLE_APPLICATION_BUNDLE"
)
endif()
add_library(wsutil
${WSUTIL_FILES}
${CMAKE_BINARY_DIR}/resources/libwsutil.rc
)
if(NOT VCSVERSION_OVERRIDE)
add_dependencies(wsutil vcs_version)
endif()
target_compile_definitions(wsutil PRIVATE
WS_BUILD_DLL
BUILD_WSUTIL
)
set_target_properties(wsutil PROPERTIES
PREFIX "lib"
LINK_FLAGS "${WS_LINK_FLAGS}"
VERSION "0.0.0" SOVERSION 0
FOLDER "DLLs"
INSTALL_RPATH "${LIBRARY_INSTALL_RPATH}"
)
if(MSVC)
set_target_properties(wsutil PROPERTIES LINK_FLAGS_DEBUG "${WS_MSVC_DEBUG_LINK_FLAGS}")
endif()
target_link_libraries(wsutil
PUBLIC
${GLIB2_LIBRARIES}
PRIVATE
${GMODULE2_LIBRARIES}
${APPLE_CORE_FOUNDATION_LIBRARY}
${CMAKE_DL_LIBS}
${GCRYPT_LIBRARIES}
${GNUTLS_LIBRARIES}
${ZLIB_LIBRARIES}
${PCRE2_LIBRARIES}
${WIN_IPHLPAPI_LIBRARY}
${WIN_WS2_32_LIBRARY}
)
target_include_directories(wsutil SYSTEM
PUBLIC
${GLIB2_INCLUDE_DIRS}
${GCRYPT_INCLUDE_DIRS}
${GNUTLS_INCLUDE_DIRS}
PRIVATE
${GMODULE2_INCLUDE_DIRS}
${ZLIB_INCLUDE_DIRS}
${PCRE2_INCLUDE_DIRS}
)
install(TARGETS wsutil
EXPORT WiresharkTargets
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
)
install(FILES ${WMEM_PUBLIC_HEADERS}
DESTINATION "${PROJECT_INSTALL_INCLUDEDIR}/wsutil/wmem"
COMPONENT "Development"
EXCLUDE_FROM_ALL
)
install(FILES ${WSUTIL_PUBLIC_HEADERS}
DESTINATION "${PROJECT_INSTALL_INCLUDEDIR}/wsutil"
COMPONENT "Development"
EXCLUDE_FROM_ALL
)
add_library(wsutil_static STATIC
${WSUTIL_FILES}
)
target_compile_definitions(wsutil_static PRIVATE
ENABLE_STATIC
BUILD_WSUTIL
)
target_link_libraries(wsutil_static
PUBLIC
${GLIB2_LIBRARIES}
PRIVATE
${GMODULE2_LIBRARIES}
${APPLE_CORE_FOUNDATION_LIBRARY}
${CMAKE_DL_LIBS}
${GCRYPT_LIBRARIES}
${GNUTLS_LIBRARIES}
${ZLIB_LIBRARIES}
${PCRE2_LIBRARIES}
${WIN_IPHLPAPI_LIBRARY}
${WIN_WS2_32_LIBRARY}
)
target_include_directories(wsutil_static SYSTEM
PUBLIC
${GLIB2_INCLUDE_DIRS}
${GCRYPT_INCLUDE_DIRS}
${GNUTLS_INCLUDE_DIRS}
PRIVATE
${GMODULE2_INCLUDE_DIRS}
${ZLIB_INCLUDE_DIRS}
${PCRE2_INCLUDE_DIRS}
)
if(NOT VCSVERSION_OVERRIDE)
add_dependencies(wsutil_static vcs_version)
endif()
add_executable(wmem_test EXCLUDE_FROM_ALL wmem/wmem_test.c ${WMEM_FILES})
target_link_libraries(wmem_test wsutil)
set_target_properties(wmem_test PROPERTIES
FOLDER "Tests"
EXCLUDE_FROM_DEFAULT_BUILD True
COMPILE_DEFINITIONS "WS_BUILD_DLL"
COMPILE_FLAGS "${WERROR_COMMON_FLAGS}"
)
add_executable(test_wsutil EXCLUDE_FROM_ALL
test_wsutil.c
)
target_link_libraries(test_wsutil ${GLIB2_LIBRARIES} wsutil)
set_target_properties(test_wsutil PROPERTIES
FOLDER "Tests"
EXCLUDE_FROM_DEFAULT_BUILD True
COMPILE_FLAGS "${WERROR_COMMON_FLAGS}"
)
CHECKAPI(
NAME
wsutil
SWITCHES
SOURCES
${WMEM_FILES}
${WSUTIL_COMMON_FILES}
)
set_source_files_properties(jsmn.c PROPERTIES COMPILE_DEFINITIONS "JSMN_STRICT")
#
# 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:
# |
C | wireshark/wsutil/cmdarg_err.c | /* cmdarg_err.c
* Routines to report command-line argument errors.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "cmdarg_err.h"
static void (*print_err)(const char *, va_list ap);
static void (*print_err_cont)(const char *, va_list ap);
/*
* Set the reporting functions for error messages.
*/
void
cmdarg_err_init(void (*err)(const char *, va_list),
void (*err_cont)(const char *, va_list))
{
print_err = err;
print_err_cont = err_cont;
}
/*
* Report an error in command-line arguments.
*/
void
vcmdarg_err(const char *fmt, va_list ap)
{
print_err(fmt, ap);
}
void
cmdarg_err(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
print_err(fmt, ap);
va_end(ap);
}
/*
* Report additional information for an error in command-line arguments.
*/
void
cmdarg_err_cont(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
print_err_cont(fmt, ap);
va_end(ap);
} |
C/C++ | wireshark/wsutil/cmdarg_err.h | /** @file
*
* Declarations of routines to report command-line argument errors.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CMDARG_ERR_H__
#define __CMDARG_ERR_H__
#include <wireshark.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* Set the reporting functions for error messages.
*/
WS_DLL_PUBLIC void
cmdarg_err_init(void (*err)(const char *, va_list),
void (*err_cont)(const char *, va_list));
/*
* Report an error in command-line arguments.
*/
WS_DLL_PUBLIC void
vcmdarg_err(const char *fmt, va_list ap)
G_GNUC_PRINTF(1, 0);
WS_DLL_PUBLIC void
cmdarg_err(const char *fmt, ...)
G_GNUC_PRINTF(1, 2);
/*
* Report additional information for an error in command-line arguments.
*/
WS_DLL_PUBLIC void
cmdarg_err_cont(const char *fmt, ...)
G_GNUC_PRINTF(1, 2);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __CMDARG_ERR_H__ */ |
C | wireshark/wsutil/codecs.c | /* codecs.c
* codecs interface 2007 Tomas Kukosa
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "codecs.h"
#include <wsutil/wslog.h>
#ifdef HAVE_PLUGINS
#include <wsutil/plugins.h>
#endif
#ifdef HAVE_PLUGINS
static plugins_t *libwscodecs_plugins = NULL;
#endif
static GSList *codecs_plugins = NULL;
#ifdef HAVE_PLUGINS
void
codecs_register_plugin(const codecs_plugin *plug)
{
codecs_plugins = g_slist_prepend(codecs_plugins, (codecs_plugin *)plug);
}
#else /* HAVE_PLUGINS */
void
codecs_register_plugin(const codecs_plugin *plug _U_)
{
ws_warning("codecs_register_plugin: built without support for binary plugins");
}
#endif /* HAVE_PLUGINS */
static void
call_plugin_register_codec_module(gpointer data, gpointer user_data _U_)
{
codecs_plugin *plug = (codecs_plugin *)data;
if (plug->register_codec_module) {
plug->register_codec_module();
}
}
/*
* For all codec plugins, call their register routines.
*/
void
codecs_init(void)
{
#ifdef HAVE_PLUGINS
libwscodecs_plugins = plugins_init(WS_PLUGIN_CODEC);
#endif
g_slist_foreach(codecs_plugins, call_plugin_register_codec_module, NULL);
}
void
codecs_cleanup(void)
{
g_slist_free(codecs_plugins);
codecs_plugins = NULL;
#ifdef HAVE_PLUGINS
plugins_cleanup(libwscodecs_plugins);
libwscodecs_plugins = NULL;
#endif
}
struct codec_handle {
const char *name;
codec_init_fn init_fn;
codec_release_fn release_fn;
codec_get_channels_fn channels_fn;
codec_get_frequency_fn frequency_fn;
codec_decode_fn decode_fn;
};
/*
* List of registered codecs.
*/
static GHashTable *registered_codecs = NULL;
/* Find a registered codec by name. */
codec_handle_t
find_codec(const char *name)
{
codec_handle_t ret;
char *key = g_ascii_strup(name, -1);
ret = (registered_codecs) ? (codec_handle_t)g_hash_table_lookup(registered_codecs, key) : NULL;
g_free(key);
return ret;
}
/* Register a codec by name. */
gboolean
register_codec(const char *name, codec_init_fn init_fn, codec_release_fn release_fn,
codec_get_channels_fn channels_fn, codec_get_frequency_fn frequency_fn,
codec_decode_fn decode_fn)
{
struct codec_handle *handle;
char *key;
/* Create our hash table if it doesn't already exist */
if (registered_codecs == NULL)
registered_codecs = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
/* RFC 4855 3. Mapping to SDP Parameters "Note that the payload format
* (encoding) names... are commonly shown in upper case. These names
* are case-insensitive in both places."
*/
key = g_ascii_strup(name, -1);
/* Make sure the registration is unique */
if (g_hash_table_lookup(registered_codecs, key) != NULL) {
g_free(key);
return FALSE; /* report an error, or have our caller do it? */
}
handle = g_new(struct codec_handle, 1);
handle->name = name;
handle->init_fn = init_fn;
handle->release_fn = release_fn;
handle->channels_fn = channels_fn;
handle->frequency_fn = frequency_fn;
handle->decode_fn = decode_fn;
g_hash_table_insert(registered_codecs, (gpointer)key, (gpointer) handle);
return TRUE;
}
/* Deregister a codec by name. */
gboolean
deregister_codec(const char *name)
{
bool ret = FALSE;
if (registered_codecs) {
char *key = g_ascii_strup(name, -1);
ret = g_hash_table_remove(registered_codecs, key);
g_free(key);
}
return ret;
}
void *codec_init(codec_handle_t codec, codec_context_t *context)
{
if (!codec) return NULL;
return (codec->init_fn)(context);
}
void codec_release(codec_handle_t codec, codec_context_t *context)
{
if (!codec) return;
(codec->release_fn)(context);
}
unsigned codec_get_channels(codec_handle_t codec, codec_context_t *context)
{
if (!codec) return 0;
return (codec->channels_fn)(context);
}
unsigned codec_get_frequency(codec_handle_t codec, codec_context_t *context)
{
if (!codec) return 0;
return (codec->frequency_fn)(context);
}
size_t codec_decode(codec_handle_t codec, codec_context_t *context, const void *input, size_t inputSizeBytes, void *output, size_t *outputSizeBytes)
{
if (!codec) return 0;
return (codec->decode_fn)(context, input, inputSizeBytes, output, outputSizeBytes);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/codecs.h | /** @file
* codecs interface 2007 Tomas Kukosa
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef _CODECS_H_
#define _CODECS_H_
#include "ws_symbol_export.h"
#include "ws_attributes.h"
#include <glib.h>
#include "wsutil/wmem/wmem_map.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
typedef struct {
void (*register_codec_module)(void); /* routine to call to register a codec */
} codecs_plugin;
WS_DLL_PUBLIC void codecs_register_plugin(const codecs_plugin *plug);
/**
* For all built-in codecs and codec plugins, call their register routines.
*/
WS_DLL_PUBLIC void codecs_init(void);
WS_DLL_PUBLIC void codecs_cleanup(void);
/**
* Get compile-time information for libraries used by libwscodecs.
*/
WS_DLL_PUBLIC void codec_get_compiled_version_info(GString *str);
struct codec_handle;
typedef struct codec_handle *codec_handle_t;
typedef struct _codec_context_t {
unsigned sample_rate;
unsigned channels;
wmem_map_t *fmtp_map;
void *priv; /* Private state set by the decoder */
} codec_context_t;
/*****************************************************************************/
/* Interface which must be implemented by a codec */
/* Codec decodes bytes to samples. Sample is 2 bytes! Codec writer must
* be careful when API refers bytes and when samples and its counts.
*/
/*****************************************************************************/
/** Initialize context of codec.
* Context can contain any information required by codec to pass between calls
* Note: There is just one codec context in runtime therefore no RTP stream
* related information should be stored in the context!
*
* @return Pointer to codec context
*/
typedef void *(*codec_init_fn)(codec_context_t *context);
/** Destroy context of codec
*
* @param context Pointer to codec context
*/
typedef void (*codec_release_fn)(codec_context_t *context);
/** Get count of channels provided by the codec
*
* @param context Pointer to codec context
*
* @return Count of channels (e.g. 1)
*/
typedef unsigned (*codec_get_channels_fn)(codec_context_t *context);
/** Get frequency/rate provided by the codec
*
* @param context Pointer to codec context
*
* @return Frequency (e.g. 8000)
*/
typedef unsigned (*codec_get_frequency_fn)(codec_context_t *context);
/** Decode one frame of payload
* Function is called twice, with different values of parameters:
* (1) To query size of required buffer in bytes for decoded samples
* pointed by inputBytes:
* outputSamples or outputSamplesSize must be set NULL
* (2) To decode samples:
* outputSamples points to allocated memory, outputSamplesSize is set to
* value returned in step (1)
*
* @param context Pointer to codec context
* @param inputBytes Pointer to input frame
* @param inputBytesSize Length of input frame in bytes
* (count of bytes to decode)
* @param outputSamples Pointer to output buffer with samples
* @param outputSamplesSize Length of output buffer in bytes (not samples!)
* Function can override this value. All codecs set it to same value as it returns in (2) when (2) is called.
*
* @return Count of reqired bytes (!not samples) to allocate in (1) or
* Count of decoded bytes (!not samples) in (2)
*/
typedef size_t (*codec_decode_fn)(codec_context_t *context,
const void *inputBytes, size_t inputBytesSize,
void *outputSamples, size_t *outputSamplesSize);
/*****************************************************************************/
/* Codec registering interface */
/*****************************************************************************/
WS_DLL_PUBLIC gboolean register_codec(const char *name, codec_init_fn init_fn,
codec_release_fn release_fn, codec_get_channels_fn channels_fn,
codec_get_frequency_fn frequency_fn, codec_decode_fn decode_fn);
WS_DLL_PUBLIC gboolean deregister_codec(const char *name);
WS_DLL_PUBLIC codec_handle_t find_codec(const char *name);
WS_DLL_PUBLIC void *codec_init(codec_handle_t codec, codec_context_t *context);
WS_DLL_PUBLIC void codec_release(codec_handle_t codec, codec_context_t *context);
WS_DLL_PUBLIC unsigned codec_get_channels(codec_handle_t codec, codec_context_t *context);
WS_DLL_PUBLIC unsigned codec_get_frequency(codec_handle_t codec, codec_context_t *context);
WS_DLL_PUBLIC size_t codec_decode(codec_handle_t codec, codec_context_t *context,
const void *inputBytes, size_t inputBytesSize,
void *outputSamples, size_t *outputSamplesSize);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* _CODECS_H_ */
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/color.h | /** @file
*
* Definitions for colors
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __COLOR_H__
#define __COLOR_H__
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* Data structure holding RGB value for a color, 16 bits per channel.
*/
typedef struct {
guint16 red;
guint16 green;
guint16 blue;
} color_t;
/*
* Convert a color_t to a 24-bit RGB value, reducing each channel to
* 8 bits and combining them.
*/
inline static unsigned int
color_t_to_rgb(const color_t *color) {
return (((color->red >> 8) << 16)
| ((color->green >> 8) << 8)
| (color->blue >> 8));
}
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C | wireshark/wsutil/console_win32.c | /* console_win32.c
* Console support for MSWindows
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2002, Jeffrey C. Foster <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifdef _WIN32
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <glib.h>
#include <wsutil/file_util.h>
#include "console_win32.h"
#include <fcntl.h>
#include <conio.h>
#include <windows.h>
#include <tchar.h>
static gboolean has_console; /* TRUE if app has console */
static gboolean console_wait; /* "Press any key..." */
static gboolean stdin_capture = FALSE; /* Don't grab stdin & stdout if TRUE */
/*
* Check whether a given standard handle needs to be redirected.
*
* If you run a Windows-subsystem program from cmd.exe on Windows XP,
* and you haven't redirected the handle in question, GetStdHandle()
* succeeds (so it doesn't return INVALID_HANDLE_VALUE or NULL), but
* GetFile_type fails on the results with ERROR_INVALID_HANDLE.
* In that case, redirection to a console is necessary.
*
* If you run it from the shell prompt in "mintty" in at least some
* versions of Cygwin on Windows XP, and you haven't redirected the
* handle in question, GetStdHandle() succeeds and returns a handle
* that's a pipe or socket; it appears mintty reads from it and outputs
* what it reads to the console.
*/
static gboolean
needs_redirection(int std_handle)
{
HANDLE fd;
DWORD handle_type;
DWORD error;
fd = GetStdHandle(std_handle);
if (fd == NULL) {
/*
* No standard handle. According to Microsoft's
* documentation for GetStdHandle(), one reason for
* this would be that the process is "a service on
* an interactive desktop"; I'm not sure whether
* such a process should be popping up a console.
*
* However, it also appears to be the case for
* the standard input and standard error, but
* *not* the standard output, for something run
* with a double-click in Windows Explorer,
* sow we'll say it needs redirection.
*/
return TRUE;
}
if (fd == INVALID_HANDLE_VALUE) {
/*
* OK, I'm not when this would happen; return
* "no redirection" for now.
*/
return FALSE;
}
handle_type = GetFileType(fd);
if (handle_type == FILE_TYPE_UNKNOWN) {
error = GetLastError();
if (error == ERROR_INVALID_HANDLE) {
/*
* OK, this appears to be the case where we're
* running something in a mode that needs a
* console.
*/
return TRUE;
}
}
/*
* Assume no redirection is needed for all other cases.
*/
return FALSE;
}
/*
* If this application has no console window to which its standard output
* would go, create one.
*/
void
create_console(void)
{
gboolean must_redirect_stdin;
gboolean must_redirect_stdout;
gboolean must_redirect_stderr;
if (stdin_capture) {
/* We've been handed "-i -". Don't mess with stdio. */
return;
}
if (has_console) {
return;
}
/* Are the standard input, output, and error invalid handles? */
must_redirect_stdin = needs_redirection(STD_INPUT_HANDLE);
must_redirect_stdout = needs_redirection(STD_OUTPUT_HANDLE);
must_redirect_stderr = needs_redirection(STD_ERROR_HANDLE);
/* If none of them are invalid, we don't need to do anything. */
if (!must_redirect_stdin && !must_redirect_stdout && !must_redirect_stderr)
return;
/* OK, at least one of them needs to be redirected to a console;
try to attach to the parent process's console and, if that fails,
try to create one. */
/*
* See if we have an existing console (i.e. we were run from a
* command prompt).
*/
if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
/* Probably not, as we couldn't attach to the parent process's
console.
Try to create a console.
According to a comment on
http://msdn.microsoft.com/en-us/library/windows/desktop/ms681952(v=vs.85).aspx
(which now redirects to a docs.microsoft.com page that is
devoid of comments, and which is not available on the
Wayback Machine)
and according to
http://connect.microsoft.com/VisualStudio/feedback/details/689696/installing-security-update-kb2507938-prevents-console-allocation
(which has disappeared, and isn't available on the Wayback
Machine)
and
https://answers.microsoft.com/en-us/windows/forum/windows_xp-windows_update/kb2567680-andor-kb2507938-breaks-attachconsole-api/e8191280-2d49-4be4-9918-18486fba0afa
even a failed attempt to attach to another process's console
will cause subsequent AllocConsole() calls to fail, possibly due
to bugs introduced by a security patch. To work around this, we
do a FreeConsole() first. */
FreeConsole();
if (AllocConsole()) {
/* That succeeded. */
console_wait = TRUE;
SetConsoleTitle(_T("Wireshark Debug Console"));
} else {
/* On Windows XP, this still fails; FreeConsole() apparently
doesn't clear the state, as it does on Windows 7. */
return; /* couldn't create console */
}
}
if (must_redirect_stdin)
ws_freopen("CONIN$", "r", stdin);
if (must_redirect_stdout) {
ws_freopen("CONOUT$", "w", stdout);
fprintf(stdout, "\n");
}
if (must_redirect_stderr) {
ws_freopen("CONOUT$", "w", stderr);
fprintf(stderr, "\n");
}
/* Now register "destroy_console()" as a routine to be called just
before the application exits, so that we can destroy the console
after the user has typed a key (so that the console doesn't just
disappear out from under them, giving the user no chance to see
the message(s) we put in there). */
atexit(destroy_console);
/* Well, we have a console now. */
has_console = TRUE;
}
void
restore_pipes(void)
{
gboolean must_redirect_stdin;
gboolean must_redirect_stdout;
gboolean must_redirect_stderr;
HANDLE fd;
if (stdin_capture) {
/* We've been handed "-i -". Don't mess with stdio. */
return;
}
if (has_console) {
return;
}
/* Are the standard input, output, and error invalid handles? */
must_redirect_stdin = needs_redirection(STD_INPUT_HANDLE);
must_redirect_stdout = needs_redirection(STD_OUTPUT_HANDLE);
must_redirect_stderr = needs_redirection(STD_ERROR_HANDLE);
/* If none of them are invalid, we don't need to do anything. */
if (!must_redirect_stdin && !must_redirect_stdout && !must_redirect_stderr)
return;
if (!must_redirect_stdin)
fd = GetStdHandle(STD_INPUT_HANDLE);
/* OK, at least one of them needs to be redirected to a console;
try to attach to the parent process's console and, if that fails,
cleanup and return. */
/*
* See if we have an existing console (i.e. we were run from a
* command prompt).
*/
if (!AttachConsole(ATTACH_PARENT_PROCESS)) {
FreeConsole();
return; /* No parent - cleanup and exit */
}
if (must_redirect_stdin) {
ws_freopen("CONIN$", "r", stdin);
} else {
SetStdHandle(STD_INPUT_HANDLE, fd);
}
if (must_redirect_stdout) {
ws_freopen("CONOUT$", "w", stdout);
fprintf(stdout, "\n");
}
if (must_redirect_stderr) {
ws_freopen("CONOUT$", "w", stderr);
fprintf(stderr, "\n");
}
/* Now register "destroy_console()" as a routine to be called just
before the application exits, so that we can destroy the console
after the user has typed a key (so that the console doesn't just
disappear out from under them, giving the user no chance to see
the message(s) we put in there). */
atexit(destroy_console);
/* Well, we have a console now. */
has_console = TRUE;
}
void
destroy_console(void)
{
if (console_wait) {
printf("\n\nPress any key to exit\n");
_getch();
}
FreeConsole();
}
void
set_console_wait(gboolean set_console_wait)
{
console_wait = set_console_wait;
}
gboolean
get_console_wait(void)
{
return console_wait;
}
void
set_stdin_capture(gboolean set_stdin_capture)
{
stdin_capture = set_stdin_capture;
}
gboolean
get_stdin_capture(void)
{
return stdin_capture;
}
#endif /* _WIN32 */ |
C/C++ | wireshark/wsutil/console_win32.h | /** @file
*
* Console support for MSWindows
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2002, Jeffrey C. Foster <[email protected]>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CONSOLE_WIN32_H__
#define __CONSOLE_WIN32_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#ifdef _WIN32
/** @file
* Win32 specific console.
*/
/** Create Windows console.
*
*/
WS_DLL_PUBLIC
void create_console(void);
/** Connect to stdio if available.
*
*/
WS_DLL_PUBLIC
void restore_pipes(void);
/** Destroy Windows console.
*
*/
WS_DLL_PUBLIC
void destroy_console(void);
/** Set console wait. GTK+ only.
* @param console_wait set/no set console wait
*/
WS_DLL_PUBLIC
void set_console_wait(gboolean console_wait);
/** get console wait
* @return set/no set console wait
*/
WS_DLL_PUBLIC
gboolean get_console_wait(void);
/** Set stdin capture.
* @param console_wait set/no stdin_capture
*/
WS_DLL_PUBLIC
void set_stdin_capture(gboolean set_stdin_capture);
/** get stdin caputre
* @return set/no set stdin_capture
*/
WS_DLL_PUBLIC
gboolean get_stdin_capture(void);
#endif/* _WIN32 */
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __CONSOLE_WIN32_H__ */ |
C | wireshark/wsutil/cpu_info.c | /* cpu_info.c
* Routines to report CPU information
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <wsutil/cpu_info.h>
#include <string.h>
#include <wsutil/ws_cpuid.h>
#include <wsutil/file_util.h>
#if defined(__FreeBSD__) || defined(__NetBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) || defined(__APPLE__)
#define HAVE_SYSCTL
#elif defined(sun) || defined(__sun)
#define HAVE_SYSINFO
#endif
#if defined(_WIN32)
#include <windows.h>
#elif defined(HAVE_SYSCTL)
#include <sys/types.h>
#include <sys/sysctl.h>
#elif defined(HAVE_SYSINFO)
#include <sys/systeminfo.h>
#endif
/*
* Functions used for the GTree we use to keep a list of *unique*
* model strings.
*/
static gint
compare_model_names(gconstpointer a, gconstpointer b, gpointer user_data _U_)
{
return strcmp((const char *)a, (const char *)b);
}
struct string_info {
GString *str;
const char *sep;
};
static gboolean
add_model_name_to_string(gpointer key, gpointer value _U_,
gpointer data)
{
struct string_info *info = (struct string_info *)data;
/* Separate this from the previous entry, if necessary. */
if (info->sep != NULL)
g_string_append(info->str, info->sep);
/* Now add the model name. */
g_string_append(info->str, g_strstrip((char *)key));
/*
* There will *definitely* need to be a separator for any subsequent
* model string.
*/
info->sep = ", ";
/* Keep going. */
return FALSE;
}
/*
* Get the CPU info, and append it to the GString
*
* On at least some OSes, there's a call that will return this information
* for all CPU types for which the OS determines that information, not just
* x86 processors with CPUID and the brand string. On those OSes, we use
* that.
*
* On other OSes, we use ws_cpuid(), which will fail unconditionally on
* non-x86 CPUs.
*/
void
get_cpu_info(GString *str)
{
GTree *model_names = g_tree_new_full(compare_model_names, NULL, g_free, NULL);
#if defined(__linux__)
/*
* We scan /proc/cpuinfo looking for lines that begins with
* "model name\t: ", and extract what comes after that prefix.
*
* /proc/cpuinfo can report information about multiple "CPU"s.
* A "CPU" appears to be a CPU core, so this treats a multi-core
* chip as multiple CPUs (which is arguably should), but doesn't
* appear to treat a multi-threaded core as multiple CPUs.
*
* So we accumulate a table of *multiple* CPU strings, saving
* one copy of each unique string, and glue them together at
* the end. We use a GTree for this.
*
* We test for Linux first, so that, even if you're on a Linux
* that supports sysctl(), we don't use it, we scan /proc/cpuinfo,
* as that's the right way to do this.
*/
FILE *proc_cpuinfo;
proc_cpuinfo = ws_fopen("/proc/cpuinfo", "r");
if (proc_cpuinfo == NULL) {
/* Just give up. */
g_tree_destroy(model_names);
return;
}
char *line = NULL;
size_t linecap = 0;
static const char prefix[] = "model name\t: ";
#define PREFIX_STRLEN (sizeof prefix - 1)
ssize_t linelen;
/*
* Read lines from /proc/cpuinfo; stop when we either hit an EOF
* or get an error.
*/
for (;;) {
linelen = getline(&line, &linecap, proc_cpuinfo);
if (linelen == -1) {
/* EOF or error; just stop. */
break;
}
/* Remove trailing newline. */
if (linelen != 0)
line[linelen - 1] = '\0';
if (strncmp(line, prefix, PREFIX_STRLEN) == 0) {
/* OK, we have a model name. */
char *model_name;
/* Get everything after the prefix. */
model_name = g_strdup(line + PREFIX_STRLEN);
/*
* Add an entry to the tree with the model name as key and
* a null value. There will only be one such entry in the
* tree; if there's already such an entry, it will be left
* alone, and model_name will be freed, otherwise a new
* node will be created using model_name as the key.
*
* Thus, we don't free model_name; either it will be freed
* for us, or it will be used in the tree and freed when we
* free the tree.
*/
g_tree_insert(model_names, model_name, NULL);
}
}
fclose(proc_cpuinfo);
#define xx_free free /* hack so checkAPIs doesn't complain */
xx_free(line); /* yes, free(), as getline() mallocates it */
#elif defined(_WIN32)
/*
* They're in the Registry. (Isn't everything?)
*/
HKEY processors_key;
if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,
L"HARDWARE\\DESCRIPTION\\System\\CentralProcessor",
0, KEY_READ, &processors_key) != ERROR_SUCCESS) {
/* Just give up. */
g_tree_destroy(model_names);
return;
}
/*
* The processors appear under that key. Enumerate all the keys
* under it.
*/
DWORD num_subkeys;
DWORD max_subkey_len;
wchar_t *subkey_buf;
/*
* How many subkeys are there, and what's the biggest subkey size?
*
* I assume that when the documentation says that some number is
* in units of "Unicode characters" they mean "units of elements
* of UTF-16 characters", i.e. "units of 2-octet items".
*/
if (RegQueryInfoKeyW(processors_key, NULL, NULL, NULL, &num_subkeys,
&max_subkey_len, NULL, NULL, NULL, NULL, NULL,
NULL) != ERROR_SUCCESS) {
/* Just give up. */
g_tree_destroy(model_names);
return;
}
/*
* max_subkey_len does not count the trailing '\0'. Add it.
*/
max_subkey_len++;
/*
* Allocate a buffer for the subkey.
*/
subkey_buf = (wchar_t *)g_malloc(max_subkey_len * sizeof (wchar_t));
if (subkey_buf == NULL) {
/* Just give up. */
g_tree_destroy(model_names);
return;
}
for (DWORD processor_index = 0; processor_index < num_subkeys;
processor_index++) {
/*
* The documentation says that this is "in characters"; I'm
* assuming, for now, that they mean "Unicode characters",
* meaning "2-octet items".
*/
DWORD subkey_bufsize = max_subkey_len;
if (RegEnumKeyExW(processors_key, processor_index, subkey_buf,
&subkey_bufsize, NULL, NULL, NULL,
NULL) != ERROR_SUCCESS) {
/* Just exit the loop. */
break;
}
/*
* Get the length of processor name string for this processor.
*
* That's the "ProcessorNameString" value for the subkey of
* processors_key with the name in subkey_buf.
*
* It's a string, so only allow REG_SZ values.
*/
DWORD model_name_bufsize;
model_name_bufsize = 0;
if (RegGetValueW(processors_key, subkey_buf, L"ProcessorNameString",
RRF_RT_REG_SZ, NULL, NULL,
&model_name_bufsize) != ERROR_SUCCESS) {
/* Just exit the loop. */
break;
}
/*
* Allocate a buffer for the string, as UTF-16.
* The retrieved length includes the terminating '\0'.
*/
wchar_t *model_name_wchar = g_malloc(model_name_bufsize);
if (RegGetValueW(processors_key, subkey_buf, L"ProcessorNameString",
RRF_RT_REG_SZ, NULL, model_name_wchar,
&model_name_bufsize) != ERROR_SUCCESS) {
/* Just exit the loop. */
g_free(model_name_wchar);
break;
}
/* Convert it to UTF-8. */
char *model_name = g_utf16_to_utf8(model_name_wchar, -1, NULL, NULL, NULL);
g_free(model_name_wchar);
/*
* Add an entry to the tree with the model name as key and
* a null value. There will only be one such entry in the
* tree; if there's already such an entry, it will be left
* alone, and model_name will be freed, otherwise a new
* node will be created using model_name as the key.
*
* Thus, we don't free model_name; either it will be freed
* for us, or it will be used in the tree and freed when we
* free the tree.
*/
g_tree_insert(model_names, model_name, NULL);
}
g_free(subkey_buf);
/*
* Close the registry key.
*/
RegCloseKey(processors_key);
#elif defined(HAVE_SYSCTL)
/*
* Fetch the string using the appropriate sysctl.
*/
size_t model_name_len;
char *model_name;
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__)
/*
* Thanks, OpenBSD guys, for not having APIs to map MIB names to
* MIB values! Just consruct the MIB entry directly.
*
* We also do that for FreeBSD and DragonFly BSD, because we can.
*
* FreeBSD appears to support this for x86, PowerPC/Power ISA, and
* Arm. OpenBSD appears to support this for a number of
* architectures. DragonFly BSD appears to support it only for
* x86, but I think they only run on x86-64 now, and may never
* have run on anything non-x86.
*/
int mib[2] = { CTL_HW, HW_MODEL };
size_t miblen = 2;
#else
/* These require a lookup, as they don't have #defines. */
#if defined(__APPLE__) /* Darwin */
/*
* The code seems to support this on both x86 and ARM.
*/
#define BRAND_STRING_SYSCTL "machdep.cpu.brand_string"
#define MIB_DEPTH 3
#elif defined(__NetBSD__)
/*
* XXX - the "highly portable Unix-like Open Source operating
* system" that "is available for a wide range of platforms"
* doesn't seem to support this except on x86, and doesn't
* seem to support any other MIB for, for example, ARM64.
*
* Maybe someday, so use it anyway.
*/
#define BRAND_STRING_SYSCTL "machdep.cpu_brand"
#define MIB_DEPTH 2
#endif
int mib[MIB_DEPTH];
size_t miblen = MIB_DEPTH;
/* Look up the sysctl name and get the MIB. */
if (sysctlnametomib(BRAND_STRING_SYSCTL, mib, &miblen) == -1) {
/*
* Either there's no such string or something else went wrong.
* Just give up.
*/
g_tree_destroy(model_names);
return;
}
#endif
if (sysctl(mib, (u_int)miblen, NULL, &model_name_len, NULL, 0) == -1) {
/*
* Either there's no such string or something else went wrong.
* Just give up.
*/
g_tree_destroy(model_names);
return;
}
model_name = g_malloc(model_name_len);
if (sysctl(mib, (u_int)miblen, model_name, &model_name_len, NULL, 0) == -1) {
/*
* Either there's no such string or something else went wrong.
* Just give up.
*/
g_free(model_name);
g_tree_destroy(model_names);
return;
}
g_tree_insert(model_names, model_name, NULL);
#elif defined(HAVE_SYSINFO) && defined(SI_CPUBRAND)
/*
* Solaris. Use sysinfo() with SI_CPUBRAND; the documentation
* indicates that it works on SPARC as well as x86.
*
* Unfortunately, SI_CPUBRAND seems to be a recent addition, so
* older versions of Solaris - dating back to some versions of
* 11.3 - don't have it.
*/
int model_name_len;
char *model_name;
/* How big is the model name? */
model_name_len = sysinfo(SI_CPUBRAND, NULL, 0);
if (model_name_len == -1) {
g_tree_destroy(model_names);
return;
}
model_name = g_malloc(model_name_len);
if (sysinfo(SI_CPUBRAND, model_name, model_name_len) == -1) {
g_tree_destroy(model_names);
return;
}
g_tree_insert(model_names, model_name, NULL);
#else
/*
* OS for which we don't support the "get the CPU type" call; we
* use ws_cpuid(), which uses CPUID on x86 and doesn't get any
* information for other instruction sets.
*/
guint32 CPUInfo[4];
char CPUBrandString[0x40];
unsigned nExIds;
/*
* Calling ws_cpuid with 0x80000000 as the selector argument, i.e.
* executing a cpuid instruction with EAX equal to 0x80000000 and
* ECX equal to 0, gets the number of valid extended IDs.
*/
if (!ws_cpuid(CPUInfo, 0x80000000)) {
g_tree_destroy(model_names);
return;
}
nExIds = CPUInfo[0];
if (nExIds<0x80000005) {
g_tree_destroy(model_names);
return;
}
memset(CPUBrandString, 0, sizeof(CPUBrandString));
/* Interpret CPU brand string */
ws_cpuid(CPUInfo, 0x80000002);
memcpy(CPUBrandString, CPUInfo, sizeof(CPUInfo));
ws_cpuid(CPUInfo, 0x80000003);
memcpy(CPUBrandString + 16, CPUInfo, sizeof(CPUInfo));
ws_cpuid(CPUInfo, 0x80000004);
memcpy(CPUBrandString + 32, CPUInfo, sizeof(CPUInfo));
model_name = g_strdup(g_strstrip(CPUBrandString));
g_tree_insert(model_names, model_name, NULL);
#endif
gint num_model_names = g_tree_nnodes(model_names);
if (num_model_names > 0) {
/*
* We have at least one model name, so add the name(s) to
* the string.
*
* If the string is not empty, separate the name(s) from
* what precedes it.
*/
if (str->len > 0)
g_string_append(str, ", with ");
if (num_model_names > 1) {
/*
* There's more than one, so put the list inside curly
* brackets.
*/
g_string_append(str, "{ ");
}
/* Iterate over the tree, adding model names to the string. */
struct string_info info;
info.str = str;
info.sep = NULL;
g_tree_foreach(model_names, add_model_name_to_string, &info);
if (num_model_names > 1) {
/*
* There's more than one, so put the list inside curly
* brackets.
*/
g_string_append(str, " }");
}
}
/* We're done; get rid of the tree. */
g_tree_destroy(model_names);
/*
* We do this on all OSes and instruction sets, so that we don't
* have to figure out how to dredge the "do we have SSE 4.2?"
* information from whatever source provides it in the OS on
* x86 processors. We already have ws_cpuid_sse42() (which we
* use to determine whether to use SSE 4.2 code to scan buffers
* for strings), so use that; it always returns "false" on non-x86
* processors.
*
* If you have multiple CPUs, some of which support it and some
* of which don't, I'm not sure we can guarantee that buffer
* scanning will work if, for example, the scanning code gets
* preempted while running on an SSE-4.2-capable CPU and, when
* it gets rescheduled, gets rescheduled on a non-SSE-4.2-capable
* CPU and tries to continue the SSE 4.2-based scan. So we don't
* worry about that case; constructing a CPU string is the *least*
* of our worries in that case.
*/
if (ws_cpuid_sse42())
g_string_append(str, " (with SSE4.2)");
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/cpu_info.h | /** @file
* Declarations of routines to report CPU information
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WSUTIL_CPU_INFO_H__
#define __WSUTIL_CPU_INFO_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
WS_DLL_PUBLIC void get_cpu_info(GString *str);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __WSUTIL_CPU_INFO_H__ */ |
C | wireshark/wsutil/crash_info.c | /* crash_info.c
* Routines to try to provide more useful information in crash dumps.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2006 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "crash_info.h"
#ifdef __APPLE__
/*
* Copyright 2005-2012 Apple Inc. All rights reserved.
*
* IMPORTANT: This Apple software is supplied to you by Apple Computer,
* Inc. ("Apple") in consideration of your agreement to the following
* terms, and your use, installation, modification or redistribution of
* this Apple software constitutes acceptance of these terms. If you do
* not agree with these terms, please do not use, install, modify or
* redistribute this Apple software.
*
* In consideration of your agreement to abide by the following terms, and
* subject to these terms, Apple grants you a personal, non-exclusive
* license, under Apple's copyrights in this original Apple software (the
* "Apple Software"), to use, reproduce, modify and redistribute the Apple
* Software, with or without modifications, in source and/or binary forms;
* provided that if you redistribute the Apple Software in its entirety and
* without modifications, you must retain this notice and the following
* text and disclaimers in all such redistributions of the Apple Software.
* Neither the name, trademarks, service marks or logos of Apple Computer,
* Inc. may be used to endorse or promote products derived from the Apple
* Software without specific prior written permission from Apple. Except
* as expressly stated in this notice, no other rights or licenses, express
* or implied, are granted by Apple herein, including but not limited to
* any patent rights that may be infringed by your derivative works or by
* other works in which the Apple Software may be incorporated.
*
* The Apple Software is provided by Apple on an "AS IS" basis. APPLE
* MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
* THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
* OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
*
* IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
* OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
* MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
* AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
* STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
/*
* This used to be the way to add an application-specific string to
* crash dumps; see
*
* http://www.allocinit.net/blog/2008/01/04/application-specific-information-in-leopard-crash-reports/
*
* It still appears to work as of OS X 10.8 (Mountain Lion).
*/
__private_extern__ char *__crashreporter_info__ = NULL;
#if 0
/*
* And this appears to be the new way to do it, as of Lion.
* However, if we do both, we get the message twice, so we're
* not doing this one, for now.
*
* This code was lifted from SVN trunk CUPS.
*/
#define _crc_make_getter(attr, type) (type)(gCRAnnotations.attr)
#define _crc_make_setter(attr, arg) (gCRAnnotations.attr = (uint64_t)(unsigned long)(arg))
#define CRASH_REPORTER_CLIENT_HIDDEN __attribute__((visibility("hidden")))
#define CRASHREPORTER_ANNOTATIONS_VERSION 4
#define CRASHREPORTER_ANNOTATIONS_SECTION "__crash_info"
/*
* Yes, these are all 64-bit, even on 32-bit platforms.
*
* version is presumably the version of this structure.
*
* message and message2 are reported, one after the other,
* under "Application Specific Information".
*
* signature_string is reported under "Application Specific
* Signatures".
*
* backtrace is reported under "Application Specific Backtrace".
*
* Dunno which versions are supported by which versions of macOS.
*/
struct crashreporter_annotations_t {
uint64_t version; /* unsigned long */
uint64_t message; /* char * */
uint64_t signature_string; /* char * */
uint64_t backtrace; /* char * */
uint64_t message2; /* char * */
uint64_t thread; /* uint64_t */
uint64_t dialog_mode; /* unsigned int */
};
CRASH_REPORTER_CLIENT_HIDDEN
struct crashreporter_annotations_t gCRAnnotations
__attribute__((section("__DATA," CRASHREPORTER_ANNOTATIONS_SECTION))) = {
CRASHREPORTER_ANNOTATIONS_VERSION, /* version */
0, /* message */
0, /* signature_string */
0, /* backtrace */
0, /* message2 */
0, /* thread */
0 /* dialog_mode */
};
#define CRSetCrashLogMessage(m) _crc_make_setter(message, m)
#endif /* 0 */
void
ws_vadd_crash_info(const char *fmt, va_list ap)
{
char *m, *old_info, *new_info;
m = ws_strdup_vprintf(fmt, ap);
if (__crashreporter_info__ == NULL)
__crashreporter_info__ = m;
else {
old_info = __crashreporter_info__;
new_info = ws_strdup_printf("%s\n%s", old_info, m);
g_free(m);
__crashreporter_info__ = new_info;
g_free(old_info);
}
}
#else /* __APPLE__ */
/*
* Perhaps Google Breakpad (http://code.google.com/p/google-breakpad/) or
* other options listed at
* http://stackoverflow.com/questions/7631908/library-for-logging-call-stack-at-runtime-windows-linux
* ?
*/
void
ws_vadd_crash_info(const char *fmt _U_, va_list ap _U_)
{
}
#endif /* __APPLE__ */
void
ws_add_crash_info(const char *fmt, ...)
{
va_list ap;
va_start(ap, fmt);
ws_vadd_crash_info(fmt, ap);
va_end(ap);
}
/*
* 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:
*/ |
C/C++ | wireshark/wsutil/crash_info.h | /** @file
* Routines to try to provide more useful information in crash dumps.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2006 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CRASH_INFO_H__
#define __CRASH_INFO_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif
WS_DLL_PUBLIC void ws_vadd_crash_info(const char *fmt, va_list ap);
WS_DLL_PUBLIC void ws_add_crash_info(const char *fmt, ...)
G_GNUC_PRINTF(1,2);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __CRASH_INFO_H__ */ |
C | wireshark/wsutil/crc10.c | /*
* crc10.c
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "crc10.h"
/*
* Charles Michael Heard's CRC-10 code, from
*
* http://web.archive.org/web/20061005231950/http://cell-relay.indiana.edu/cell-relay/publications/software/CRC/crc10.html
*
* with the CRC table initialized with values computed by
* his "gen_byte_crc10_table()" routine, rather than by calling that
* routine at run time, and with various data type cleanups.
*/
static const uint16_t byte_crc10_table[256] = {
0x0000, 0x0233, 0x0255, 0x0066, 0x0299, 0x00aa, 0x00cc, 0x02ff,
0x0301, 0x0132, 0x0154, 0x0367, 0x0198, 0x03ab, 0x03cd, 0x01fe,
0x0031, 0x0202, 0x0264, 0x0057, 0x02a8, 0x009b, 0x00fd, 0x02ce,
0x0330, 0x0103, 0x0165, 0x0356, 0x01a9, 0x039a, 0x03fc, 0x01cf,
0x0062, 0x0251, 0x0237, 0x0004, 0x02fb, 0x00c8, 0x00ae, 0x029d,
0x0363, 0x0150, 0x0136, 0x0305, 0x01fa, 0x03c9, 0x03af, 0x019c,
0x0053, 0x0260, 0x0206, 0x0035, 0x02ca, 0x00f9, 0x009f, 0x02ac,
0x0352, 0x0161, 0x0107, 0x0334, 0x01cb, 0x03f8, 0x039e, 0x01ad,
0x00c4, 0x02f7, 0x0291, 0x00a2, 0x025d, 0x006e, 0x0008, 0x023b,
0x03c5, 0x01f6, 0x0190, 0x03a3, 0x015c, 0x036f, 0x0309, 0x013a,
0x00f5, 0x02c6, 0x02a0, 0x0093, 0x026c, 0x005f, 0x0039, 0x020a,
0x03f4, 0x01c7, 0x01a1, 0x0392, 0x016d, 0x035e, 0x0338, 0x010b,
0x00a6, 0x0295, 0x02f3, 0x00c0, 0x023f, 0x000c, 0x006a, 0x0259,
0x03a7, 0x0194, 0x01f2, 0x03c1, 0x013e, 0x030d, 0x036b, 0x0158,
0x0097, 0x02a4, 0x02c2, 0x00f1, 0x020e, 0x003d, 0x005b, 0x0268,
0x0396, 0x01a5, 0x01c3, 0x03f0, 0x010f, 0x033c, 0x035a, 0x0169,
0x0188, 0x03bb, 0x03dd, 0x01ee, 0x0311, 0x0122, 0x0144, 0x0377,
0x0289, 0x00ba, 0x00dc, 0x02ef, 0x0010, 0x0223, 0x0245, 0x0076,
0x01b9, 0x038a, 0x03ec, 0x01df, 0x0320, 0x0113, 0x0175, 0x0346,
0x02b8, 0x008b, 0x00ed, 0x02de, 0x0021, 0x0212, 0x0274, 0x0047,
0x01ea, 0x03d9, 0x03bf, 0x018c, 0x0373, 0x0140, 0x0126, 0x0315,
0x02eb, 0x00d8, 0x00be, 0x028d, 0x0072, 0x0241, 0x0227, 0x0014,
0x01db, 0x03e8, 0x038e, 0x01bd, 0x0342, 0x0171, 0x0117, 0x0324,
0x02da, 0x00e9, 0x008f, 0x02bc, 0x0043, 0x0270, 0x0216, 0x0025,
0x014c, 0x037f, 0x0319, 0x012a, 0x03d5, 0x01e6, 0x0180, 0x03b3,
0x024d, 0x007e, 0x0018, 0x022b, 0x00d4, 0x02e7, 0x0281, 0x00b2,
0x017d, 0x034e, 0x0328, 0x011b, 0x03e4, 0x01d7, 0x01b1, 0x0382,
0x027c, 0x004f, 0x0029, 0x021a, 0x00e5, 0x02d6, 0x02b0, 0x0083,
0x012e, 0x031d, 0x037b, 0x0148, 0x03b7, 0x0184, 0x01e2, 0x03d1,
0x022f, 0x001c, 0x007a, 0x0249, 0x00b6, 0x0285, 0x02e3, 0x00d0,
0x011f, 0x032c, 0x034a, 0x0179, 0x0386, 0x01b5, 0x01d3, 0x03e0,
0x021e, 0x002d, 0x004b, 0x0278, 0x0087, 0x02b4, 0x02d2, 0x00e1
};
/* Update the data block's CRC-10 remainder one byte at a time */
uint16_t
update_crc10_by_bytes(uint16_t crc10_accum, const uint8_t *data_blk_ptr,
int data_blk_size)
{
register int i;
for (i = 0; i < data_blk_size; i++) {
crc10_accum = ((crc10_accum << 8) & 0x3ff)
^ byte_crc10_table[( crc10_accum >> 2) & 0xff]
^ *data_blk_ptr++;
}
return crc10_accum;
}
/*
* 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:
*/ |
C/C++ | wireshark/wsutil/crc10.h | /** @file
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CRC10_H__
#define __CRC10_H__
#include <wireshark.h>
/* Update the data block's CRC-10 remainder one byte at a time */
WS_DLL_PUBLIC uint16_t update_crc10_by_bytes(uint16_t crc10, const uint8_t *data_blk_ptr, int data_blk_size);
#endif /* __CRC10_H__ */ |
C | wireshark/wsutil/crc11.c | /*
* crc11.c
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include <wsutil/crc11.h>
/**
* Functions and types for CRC checks.
*
* Generated on Tue Aug 7 15:45:57 2012,
* by pycrc v0.7.10, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 11
* Poly = 0x307
* XorIn = 0x000
* ReflectIn = False
* XorOut = 0x000
* ReflectOut = False
* Algorithm = table-driven
*****************************************************************************/
/**
* Static table used for the table_driven implementation.
*****************************************************************************/
static const uint16_t crc11_table_307_noreflect_noxor[256] = {
0x000, 0x307, 0x60e, 0x509, 0x71b, 0x41c, 0x115, 0x212, 0x531, 0x636, 0x33f, 0x038, 0x22a, 0x12d, 0x424, 0x723,
0x165, 0x262, 0x76b, 0x46c, 0x67e, 0x579, 0x070, 0x377, 0x454, 0x753, 0x25a, 0x15d, 0x34f, 0x048, 0x541, 0x646,
0x2ca, 0x1cd, 0x4c4, 0x7c3, 0x5d1, 0x6d6, 0x3df, 0x0d8, 0x7fb, 0x4fc, 0x1f5, 0x2f2, 0x0e0, 0x3e7, 0x6ee, 0x5e9,
0x3af, 0x0a8, 0x5a1, 0x6a6, 0x4b4, 0x7b3, 0x2ba, 0x1bd, 0x69e, 0x599, 0x090, 0x397, 0x185, 0x282, 0x78b, 0x48c,
0x594, 0x693, 0x39a, 0x09d, 0x28f, 0x188, 0x481, 0x786, 0x0a5, 0x3a2, 0x6ab, 0x5ac, 0x7be, 0x4b9, 0x1b0, 0x2b7,
0x4f1, 0x7f6, 0x2ff, 0x1f8, 0x3ea, 0x0ed, 0x5e4, 0x6e3, 0x1c0, 0x2c7, 0x7ce, 0x4c9, 0x6db, 0x5dc, 0x0d5, 0x3d2,
0x75e, 0x459, 0x150, 0x257, 0x045, 0x342, 0x64b, 0x54c, 0x26f, 0x168, 0x461, 0x766, 0x574, 0x673, 0x37a, 0x07d,
0x63b, 0x53c, 0x035, 0x332, 0x120, 0x227, 0x72e, 0x429, 0x30a, 0x00d, 0x504, 0x603, 0x411, 0x716, 0x21f, 0x118,
0x02f, 0x328, 0x621, 0x526, 0x734, 0x433, 0x13a, 0x23d, 0x51e, 0x619, 0x310, 0x017, 0x205, 0x102, 0x40b, 0x70c,
0x14a, 0x24d, 0x744, 0x443, 0x651, 0x556, 0x05f, 0x358, 0x47b, 0x77c, 0x275, 0x172, 0x360, 0x067, 0x56e, 0x669,
0x2e5, 0x1e2, 0x4eb, 0x7ec, 0x5fe, 0x6f9, 0x3f0, 0x0f7, 0x7d4, 0x4d3, 0x1da, 0x2dd, 0x0cf, 0x3c8, 0x6c1, 0x5c6,
0x380, 0x087, 0x58e, 0x689, 0x49b, 0x79c, 0x295, 0x192, 0x6b1, 0x5b6, 0x0bf, 0x3b8, 0x1aa, 0x2ad, 0x7a4, 0x4a3,
0x5bb, 0x6bc, 0x3b5, 0x0b2, 0x2a0, 0x1a7, 0x4ae, 0x7a9, 0x08a, 0x38d, 0x684, 0x583, 0x791, 0x496, 0x19f, 0x298,
0x4de, 0x7d9, 0x2d0, 0x1d7, 0x3c5, 0x0c2, 0x5cb, 0x6cc, 0x1ef, 0x2e8, 0x7e1, 0x4e6, 0x6f4, 0x5f3, 0x0fa, 0x3fd,
0x771, 0x476, 0x17f, 0x278, 0x06a, 0x36d, 0x664, 0x563, 0x240, 0x147, 0x44e, 0x749, 0x55b, 0x65c, 0x355, 0x052,
0x614, 0x513, 0x01a, 0x31d, 0x10f, 0x208, 0x701, 0x406, 0x325, 0x022, 0x52b, 0x62c, 0x43e, 0x739, 0x230, 0x137
};
/**
* Update the crc value with new data.
*
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The updated crc value.
*****************************************************************************/
uint16_t crc11_307_noreflect_noxor(const uint8_t *data, uint64_t data_len)
{
uint16_t crc = 0;
unsigned tbl_idx;
while (data_len--) {
tbl_idx = ((crc >> 3) ^ *data) & 0xff;
crc = (crc11_table_307_noreflect_noxor[tbl_idx] ^ (crc << 8)) & 0x7ff;
data++;
}
return crc & 0x7ff;
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/crc11.h | /** @file
* http://www.tty1.net/pycrc/faq_en.html#code-ownership
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CRC11_____H__
#include <stdint.h>
#include "ws_symbol_export.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* Functions and types for CRC checks.
*
* Generated on Tue Aug 7 15:45:57 2012,
* by pycrc v0.7.10, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 11
* Poly = 0x307
* XorIn = 0x000
* ReflectIn = False
* XorOut = 0x000
* ReflectOut = False
* Algorithm = table-driven
*****************************************************************************/
WS_DLL_PUBLIC
uint16_t crc11_307_noreflect_noxor(const uint8_t *data, uint64_t data_len);
#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif
#endif /*__CRC11_____H__*/ |
C | wireshark/wsutil/crc16-plain.c | /*
* crc16-plain.c
* http://www.tty1.net/pycrc/faq_en.html#code-ownership
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/**
* \file crc16-plain.c
* Functions and types for CRC checks.
*
* Generated on Wed Mar 18 14:12:09 2009,
* by pycrc v0.7, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 16
* Poly = 0x8005
* XorIn = 0x0000
* ReflectIn = True
* XorOut = 0x0000
* ReflectOut = True
* Algorithm = table-driven
* Direct = True
*
* Modified 2009-03-16 not to include <stdint.h> as our Win32 environment
* appears not to have it; we're using GLib types, instead.
* Modified 2023-06-28 to use C99 types.
*****************************************************************************/
#include "wsutil/crc16-plain.h"
/**
* Static table used for the table_driven implementation.
*****************************************************************************/
static const crc16_plain_t crc_table_reflected[256] = {
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
};
/**
* Reflect all bits of a \a data word of \a data_len bytes.
*
* \param data The data word to be reflected.
* \param data_len The width of \a data expressed in number of bits.
* \return The reflected data.
*****************************************************************************/
long crc16_plain_reflect(long data, size_t data_len)
{
unsigned int i;
long ret;
ret = data & 0x01;
for (i = 1; i < data_len; i++)
{
data >>= 1;
ret = (ret << 1) | (data & 0x01);
}
return ret;
}
/**
* Update the crc value with new data.
*
* \param crc The current crc value.
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The updated crc value.
*****************************************************************************/
crc16_plain_t crc16_plain_update(crc16_plain_t crc, const unsigned char *data, size_t data_len)
{
unsigned int tbl_idx;
while (data_len--) {
tbl_idx = (crc ^ *data) & 0xff;
crc = (crc_table_reflected[tbl_idx] ^ (crc >> 8)) & 0xffff;
data++;
}
return crc & 0xffff;
}
/* Generated on Tue Jul 24 09:08:46 2012,
* by pycrc v0.7.10, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 16
* Poly = 0x8005
* XorIn = 0x0000
* ReflectIn = False
* XorOut = 0x0000
* ReflectOut = False
* Algorithm = table-driven
*****************************************************************************/
static const uint16_t crc16_table_8005_noreflect_noxor[256] = {
0x0000, 0x8005, 0x800f, 0x000a, 0x801b, 0x001e, 0x0014, 0x8011,
0x8033, 0x0036, 0x003c, 0x8039, 0x0028, 0x802d, 0x8027, 0x0022,
0x8063, 0x0066, 0x006c, 0x8069, 0x0078, 0x807d, 0x8077, 0x0072,
0x0050, 0x8055, 0x805f, 0x005a, 0x804b, 0x004e, 0x0044, 0x8041,
0x80c3, 0x00c6, 0x00cc, 0x80c9, 0x00d8, 0x80dd, 0x80d7, 0x00d2,
0x00f0, 0x80f5, 0x80ff, 0x00fa, 0x80eb, 0x00ee, 0x00e4, 0x80e1,
0x00a0, 0x80a5, 0x80af, 0x00aa, 0x80bb, 0x00be, 0x00b4, 0x80b1,
0x8093, 0x0096, 0x009c, 0x8099, 0x0088, 0x808d, 0x8087, 0x0082,
0x8183, 0x0186, 0x018c, 0x8189, 0x0198, 0x819d, 0x8197, 0x0192,
0x01b0, 0x81b5, 0x81bf, 0x01ba, 0x81ab, 0x01ae, 0x01a4, 0x81a1,
0x01e0, 0x81e5, 0x81ef, 0x01ea, 0x81fb, 0x01fe, 0x01f4, 0x81f1,
0x81d3, 0x01d6, 0x01dc, 0x81d9, 0x01c8, 0x81cd, 0x81c7, 0x01c2,
0x0140, 0x8145, 0x814f, 0x014a, 0x815b, 0x015e, 0x0154, 0x8151,
0x8173, 0x0176, 0x017c, 0x8179, 0x0168, 0x816d, 0x8167, 0x0162,
0x8123, 0x0126, 0x012c, 0x8129, 0x0138, 0x813d, 0x8137, 0x0132,
0x0110, 0x8115, 0x811f, 0x011a, 0x810b, 0x010e, 0x0104, 0x8101,
0x8303, 0x0306, 0x030c, 0x8309, 0x0318, 0x831d, 0x8317, 0x0312,
0x0330, 0x8335, 0x833f, 0x033a, 0x832b, 0x032e, 0x0324, 0x8321,
0x0360, 0x8365, 0x836f, 0x036a, 0x837b, 0x037e, 0x0374, 0x8371,
0x8353, 0x0356, 0x035c, 0x8359, 0x0348, 0x834d, 0x8347, 0x0342,
0x03c0, 0x83c5, 0x83cf, 0x03ca, 0x83db, 0x03de, 0x03d4, 0x83d1,
0x83f3, 0x03f6, 0x03fc, 0x83f9, 0x03e8, 0x83ed, 0x83e7, 0x03e2,
0x83a3, 0x03a6, 0x03ac, 0x83a9, 0x03b8, 0x83bd, 0x83b7, 0x03b2,
0x0390, 0x8395, 0x839f, 0x039a, 0x838b, 0x038e, 0x0384, 0x8381,
0x0280, 0x8285, 0x828f, 0x028a, 0x829b, 0x029e, 0x0294, 0x8291,
0x82b3, 0x02b6, 0x02bc, 0x82b9, 0x02a8, 0x82ad, 0x82a7, 0x02a2,
0x82e3, 0x02e6, 0x02ec, 0x82e9, 0x02f8, 0x82fd, 0x82f7, 0x02f2,
0x02d0, 0x82d5, 0x82df, 0x02da, 0x82cb, 0x02ce, 0x02c4, 0x82c1,
0x8243, 0x0246, 0x024c, 0x8249, 0x0258, 0x825d, 0x8257, 0x0252,
0x0270, 0x8275, 0x827f, 0x027a, 0x826b, 0x026e, 0x0264, 0x8261,
0x0220, 0x8225, 0x822f, 0x022a, 0x823b, 0x023e, 0x0234, 0x8231,
0x8213, 0x0216, 0x021c, 0x8219, 0x0208, 0x820d, 0x8207, 0x0202
};
/**
* Calculate the crc-16 (x^16 + x^15 + x^2 + 1) value for data. Note that this
* CRC is not equal to crc16_plain.
*
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The crc value.
*****************************************************************************/
uint16_t crc16_8005_noreflect_noxor(const uint8_t *data, uint64_t data_len)
{
unsigned tbl_idx;
uint16_t crc = 0;
while (data_len--) {
tbl_idx = ((crc >> 8) ^ *data) & 0xff;
crc = (crc16_table_8005_noreflect_noxor[tbl_idx] ^ (crc << 8)) & 0xffff;
data++;
}
return crc & 0xffff;
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/crc16-plain.h | /** @file
* http://www.tty1.net/pycrc/faq_en.html#code-ownership
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/**
* \file crc16-plain.h
* Functions and types for CRC checks.
*
* Generated on Wed Mar 18 14:12:15 2009,
* by pycrc v0.7, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 16
* Poly = 0x8005
* XorIn = 0x0000
* ReflectIn = True
* XorOut = 0x0000
* ReflectOut = True
* Algorithm = table-driven
* Direct = True
*
* Modified 2009-03-16 not to include <stdint.h> as our Win32 environment
* appears not to have it; we're using GLib types, instead.
*****************************************************************************/
#ifndef __CRC____PLAIN_H__
#define __CRC____PLAIN_H__
#include <stddef.h>
#include <stdint.h>
#include "ws_symbol_export.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* The definition of the used algorithm.
*****************************************************************************/
#define CRC_ALGO_TABLE_DRIVEN 1
/**
* The type of the CRC values.
*
* This type must be big enough to contain at least 16 bits.
*****************************************************************************/
typedef uint16_t crc16_plain_t;
/**
* Reflect all bits of a \a data word of \a data_len bytes.
*
* \param data The data word to be reflected.
* \param data_len The width of \a data expressed in number of bits.
* \return The reflected data.
*****************************************************************************/
long crc16_plain_reflect(long data, size_t data_len);
/**
* Calculate the initial crc value.
*
* \return The initial crc value.
*****************************************************************************/
static inline crc16_plain_t crc16_plain_init(void)
{
return 0x0000;
}
/**
* Update the crc value with new data.
*
* \param crc The current crc value.
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The updated crc value.
*****************************************************************************/
WS_DLL_PUBLIC
crc16_plain_t crc16_plain_update(crc16_plain_t crc, const unsigned char *data, size_t data_len);
/**
* Calculate the final crc value.
*
* \param crc The current crc value.
* \return The final crc value.
*****************************************************************************/
static inline crc16_plain_t crc16_plain_finalize(crc16_plain_t crc)
{
return crc ^ 0x0000;
}
/* Generated on Tue Jul 24 09:08:46 2012,
* by pycrc v0.7.10, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 16
* Poly = 0x8005
* XorIn = 0x0000
* ReflectIn = False
* XorOut = 0x0000
* ReflectOut = False
* Algorithm = table-driven
*
* Calculate the crc-16 (x^16 + x^15 + x^2 + 1) value for data. Note that this
* CRC is not equal to crc16_plain.
*
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The crc value.
*****************************************************************************/
WS_DLL_PUBLIC
uint16_t crc16_8005_noreflect_noxor(const uint8_t *data, uint64_t data_len);
#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif
#endif /* __CRC____PLAIN_H__ */ |
C | wireshark/wsutil/crc16.c | /* crc16.c
* CRC-16 routine
*
* 2004 Richard van der Hoff <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* References:
* "A Painless Guide to CRC Error Detection Algorithms", Ross Williams
* http://www.repairfaq.org/filipg/LINK/F_crc_v3.html
*
* ITU-T Recommendation V.42 (2002), "Error-Correcting Procedures for
* DCEs using asynchronous-to-synchronous conversion", Para. 8.1.1.6.1
*/
#include "config.h"
#include <wsutil/crc16.h>
/*****************************************************************/
/*
* Table for the CCITT/ITU/CRC-16 16-bit CRC
*
* Polynomial is
*
* x^16 + x^12 + x^5 + 1
*/
/* */
/* CRC LOOKUP TABLE */
/* ================ */
/* The following CRC lookup table was generated automagically */
/* by the Rocksoft^tm Model CRC Algorithm Table Generation */
/* Program V1.0 using the following model parameters: */
/* */
/* Width : 2 bytes. */
/* Poly : 0x1021 */
/* Reverse : TRUE. */
/* */
/* For more information on the Rocksoft^tm Model CRC Algorithm, */
/* see the document titled "A Painless Guide to CRC Error */
/* Detection Algorithms" by Ross Williams. See */
/* */
/* http://www.ross.net/crc/crcpaper.html */
/* */
/* which links to a text version and an HTML-but-not-all-on-one- */
/* page version, or various HTML-all-on-one-page versions such */
/* as the one at */
/* */
/* http://www.geocities.com/SiliconValley/Pines/8659/crc.htm */
/* */
/* (search for the title to find others). */
/* */
/*****************************************************************/
static const unsigned crc16_ccitt_table_reverse[256] =
{
0x0000, 0x1189, 0x2312, 0x329B, 0x4624, 0x57AD, 0x6536, 0x74BF,
0x8C48, 0x9DC1, 0xAF5A, 0xBED3, 0xCA6C, 0xDBE5, 0xE97E, 0xF8F7,
0x1081, 0x0108, 0x3393, 0x221A, 0x56A5, 0x472C, 0x75B7, 0x643E,
0x9CC9, 0x8D40, 0xBFDB, 0xAE52, 0xDAED, 0xCB64, 0xF9FF, 0xE876,
0x2102, 0x308B, 0x0210, 0x1399, 0x6726, 0x76AF, 0x4434, 0x55BD,
0xAD4A, 0xBCC3, 0x8E58, 0x9FD1, 0xEB6E, 0xFAE7, 0xC87C, 0xD9F5,
0x3183, 0x200A, 0x1291, 0x0318, 0x77A7, 0x662E, 0x54B5, 0x453C,
0xBDCB, 0xAC42, 0x9ED9, 0x8F50, 0xFBEF, 0xEA66, 0xD8FD, 0xC974,
0x4204, 0x538D, 0x6116, 0x709F, 0x0420, 0x15A9, 0x2732, 0x36BB,
0xCE4C, 0xDFC5, 0xED5E, 0xFCD7, 0x8868, 0x99E1, 0xAB7A, 0xBAF3,
0x5285, 0x430C, 0x7197, 0x601E, 0x14A1, 0x0528, 0x37B3, 0x263A,
0xDECD, 0xCF44, 0xFDDF, 0xEC56, 0x98E9, 0x8960, 0xBBFB, 0xAA72,
0x6306, 0x728F, 0x4014, 0x519D, 0x2522, 0x34AB, 0x0630, 0x17B9,
0xEF4E, 0xFEC7, 0xCC5C, 0xDDD5, 0xA96A, 0xB8E3, 0x8A78, 0x9BF1,
0x7387, 0x620E, 0x5095, 0x411C, 0x35A3, 0x242A, 0x16B1, 0x0738,
0xFFCF, 0xEE46, 0xDCDD, 0xCD54, 0xB9EB, 0xA862, 0x9AF9, 0x8B70,
0x8408, 0x9581, 0xA71A, 0xB693, 0xC22C, 0xD3A5, 0xE13E, 0xF0B7,
0x0840, 0x19C9, 0x2B52, 0x3ADB, 0x4E64, 0x5FED, 0x6D76, 0x7CFF,
0x9489, 0x8500, 0xB79B, 0xA612, 0xD2AD, 0xC324, 0xF1BF, 0xE036,
0x18C1, 0x0948, 0x3BD3, 0x2A5A, 0x5EE5, 0x4F6C, 0x7DF7, 0x6C7E,
0xA50A, 0xB483, 0x8618, 0x9791, 0xE32E, 0xF2A7, 0xC03C, 0xD1B5,
0x2942, 0x38CB, 0x0A50, 0x1BD9, 0x6F66, 0x7EEF, 0x4C74, 0x5DFD,
0xB58B, 0xA402, 0x9699, 0x8710, 0xF3AF, 0xE226, 0xD0BD, 0xC134,
0x39C3, 0x284A, 0x1AD1, 0x0B58, 0x7FE7, 0x6E6E, 0x5CF5, 0x4D7C,
0xC60C, 0xD785, 0xE51E, 0xF497, 0x8028, 0x91A1, 0xA33A, 0xB2B3,
0x4A44, 0x5BCD, 0x6956, 0x78DF, 0x0C60, 0x1DE9, 0x2F72, 0x3EFB,
0xD68D, 0xC704, 0xF59F, 0xE416, 0x90A9, 0x8120, 0xB3BB, 0xA232,
0x5AC5, 0x4B4C, 0x79D7, 0x685E, 0x1CE1, 0x0D68, 0x3FF3, 0x2E7A,
0xE70E, 0xF687, 0xC41C, 0xD595, 0xA12A, 0xB0A3, 0x8238, 0x93B1,
0x6B46, 0x7ACF, 0x4854, 0x59DD, 0x2D62, 0x3CEB, 0x0E70, 0x1FF9,
0xF78F, 0xE606, 0xD49D, 0xC514, 0xB1AB, 0xA022, 0x92B9, 0x8330,
0x7BC7, 0x6A4E, 0x58D5, 0x495C, 0x3DE3, 0x2C6A, 0x1EF1, 0x0F78
};
/* Same as above, only without reverse (Reverse=false) */
static const unsigned crc16_ccitt_table[256] =
{
0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
0x1231, 0x0210, 0x3273, 0x2252, 0x52B5, 0x4294, 0x72F7, 0x62D6,
0x9339, 0x8318, 0xB37B, 0xA35A, 0xD3BD, 0xC39C, 0xF3FF, 0xE3DE,
0x2462, 0x3443, 0x0420, 0x1401, 0x64E6, 0x74C7, 0x44A4, 0x5485,
0xA56A, 0xB54B, 0x8528, 0x9509, 0xE5EE, 0xF5CF, 0xC5AC, 0xD58D,
0x3653, 0x2672, 0x1611, 0x0630, 0x76D7, 0x66F6, 0x5695, 0x46B4,
0xB75B, 0xA77A, 0x9719, 0x8738, 0xF7DF, 0xE7FE, 0xD79D, 0xC7BC,
0x48C4, 0x58E5, 0x6886, 0x78A7, 0x0840, 0x1861, 0x2802, 0x3823,
0xC9CC, 0xD9ED, 0xE98E, 0xF9AF, 0x8948, 0x9969, 0xA90A, 0xB92B,
0x5AF5, 0x4AD4, 0x7AB7, 0x6A96, 0x1A71, 0x0A50, 0x3A33, 0x2A12,
0xDBFD, 0xCBDC, 0xFBBF, 0xEB9E, 0x9B79, 0x8B58, 0xBB3B, 0xAB1A,
0x6CA6, 0x7C87, 0x4CE4, 0x5CC5, 0x2C22, 0x3C03, 0x0C60, 0x1C41,
0xEDAE, 0xFD8F, 0xCDEC, 0xDDCD, 0xAD2A, 0xBD0B, 0x8D68, 0x9D49,
0x7E97, 0x6EB6, 0x5ED5, 0x4EF4, 0x3E13, 0x2E32, 0x1E51, 0x0E70,
0xFF9F, 0xEFBE, 0xDFDD, 0xCFFC, 0xBF1B, 0xAF3A, 0x9F59, 0x8F78,
0x9188, 0x81A9, 0xB1CA, 0xA1EB, 0xD10C, 0xC12D, 0xF14E, 0xE16F,
0x1080, 0x00A1, 0x30C2, 0x20E3, 0x5004, 0x4025, 0x7046, 0x6067,
0x83B9, 0x9398, 0xA3FB, 0xB3DA, 0xC33D, 0xD31C, 0xE37F, 0xF35E,
0x02B1, 0x1290, 0x22F3, 0x32D2, 0x4235, 0x5214, 0x6277, 0x7256,
0xB5EA, 0xA5CB, 0x95A8, 0x8589, 0xF56E, 0xE54F, 0xD52C, 0xC50D,
0x34E2, 0x24C3, 0x14A0, 0x0481, 0x7466, 0x6447, 0x5424, 0x4405,
0xA7DB, 0xB7FA, 0x8799, 0x97B8, 0xE75F, 0xF77E, 0xC71D, 0xD73C,
0x26D3, 0x36F2, 0x0691, 0x16B0, 0x6657, 0x7676, 0x4615, 0x5634,
0xD94C, 0xC96D, 0xF90E, 0xE92F, 0x99C8, 0x89E9, 0xB98A, 0xA9AB,
0x5844, 0x4865, 0x7806, 0x6827, 0x18C0, 0x08E1, 0x3882, 0x28A3,
0xCB7D, 0xDB5C, 0xEB3F, 0xFB1E, 0x8BF9, 0x9BD8, 0xABBB, 0xBB9A,
0x4A75, 0x5A54, 0x6A37, 0x7A16, 0x0AF1, 0x1AD0, 0x2AB3, 0x3A92,
0xFD2E, 0xED0F, 0xDD6C, 0xCD4D, 0xBDAA, 0xAD8B, 0x9DE8, 0x8DC9,
0x7C26, 0x6C07, 0x5C64, 0x4C45, 0x3CA2, 0x2C83, 0x1CE0, 0x0CC1,
0xEF1F, 0xFF3E, 0xCF5D, 0xDF7C, 0xAF9B, 0xBFBA, 0x8FD9, 0x9FF8,
0x6E17, 0x7E36, 0x4E55, 0x5E74, 0x2E93, 0x3EB2, 0x0ED1, 0x1EF0
};
/* This table was compiled using the polynom 0x5935 */
static const unsigned crc16_precompiled_5935[256] =
{
0x0000, 0x5935, 0xB26A, 0xEB5F, 0x3DE1, 0x64D4, 0x8F8B, 0xD6BE,
0x7BC2, 0x22F7, 0xC9A8, 0x909D, 0x4623, 0x1F16, 0xF449, 0xAD7C,
0xF784, 0xAEB1, 0x45EE, 0x1CDB, 0xCA65, 0x9350, 0x780F, 0x213A,
0x8C46, 0xD573, 0x3E2C, 0x6719, 0xB1A7, 0xE892, 0x03CD, 0x5AF8,
0xB63D, 0xEF08, 0x0457, 0x5D62, 0x8BDC, 0xD2E9, 0x39B6, 0x6083,
0xCDFF, 0x94CA, 0x7F95, 0x26A0, 0xF01E, 0xA92B, 0x4274, 0x1B41,
0x41B9, 0x188C, 0xF3D3, 0xAAE6, 0x7C58, 0x256D, 0xCE32, 0x9707,
0x3A7B, 0x634E, 0x8811, 0xD124, 0x079A, 0x5EAF, 0xB5F0, 0xECC5,
0x354F, 0x6C7A, 0x8725, 0xDE10, 0x08AE, 0x519B, 0xBAC4, 0xE3F1,
0x4E8D, 0x17B8, 0xFCE7, 0xA5D2, 0x736C, 0x2A59, 0xC106, 0x9833,
0xC2CB, 0x9BFE, 0x70A1, 0x2994, 0xFF2A, 0xA61F, 0x4D40, 0x1475,
0xB909, 0xE03C, 0x0B63, 0x5256, 0x84E8, 0xDDDD, 0x3682, 0x6FB7,
0x8372, 0xDA47, 0x3118, 0x682D, 0xBE93, 0xE7A6, 0x0CF9, 0x55CC,
0xF8B0, 0xA185, 0x4ADA, 0x13EF, 0xC551, 0x9C64, 0x773B, 0x2E0E,
0x74F6, 0x2DC3, 0xC69C, 0x9FA9, 0x4917, 0x1022, 0xFB7D, 0xA248,
0x0F34, 0x5601, 0xBD5E, 0xE46B, 0x32D5, 0x6BE0, 0x80BF, 0xD98A,
0x6A9E, 0x33AB, 0xD8F4, 0x81C1, 0x577F, 0x0E4A, 0xE515, 0xBC20,
0x115C, 0x4869, 0xA336, 0xFA03, 0x2CBD, 0x7588, 0x9ED7, 0xC7E2,
0x9D1A, 0xC42F, 0x2F70, 0x7645, 0xA0FB, 0xF9CE, 0x1291, 0x4BA4,
0xE6D8, 0xBFED, 0x54B2, 0x0D87, 0xDB39, 0x820C, 0x6953, 0x3066,
0xDCA3, 0x8596, 0x6EC9, 0x37FC, 0xE142, 0xB877, 0x5328, 0x0A1D,
0xA761, 0xFE54, 0x150B, 0x4C3E, 0x9A80, 0xC3B5, 0x28EA, 0x71DF,
0x2B27, 0x7212, 0x994D, 0xC078, 0x16C6, 0x4FF3, 0xA4AC, 0xFD99,
0x50E5, 0x09D0, 0xE28F, 0xBBBA, 0x6D04, 0x3431, 0xDF6E, 0x865B,
0x5FD1, 0x06E4, 0xEDBB, 0xB48E, 0x6230, 0x3B05, 0xD05A, 0x896F,
0x2413, 0x7D26, 0x9679, 0xCF4C, 0x19F2, 0x40C7, 0xAB98, 0xF2AD,
0xA855, 0xF160, 0x1A3F, 0x430A, 0x95B4, 0xCC81, 0x27DE, 0x7EEB,
0xD397, 0x8AA2, 0x61FD, 0x38C8, 0xEE76, 0xB743, 0x5C1C, 0x0529,
0xE9EC, 0xB0D9, 0x5B86, 0x02B3, 0xD40D, 0x8D38, 0x6667, 0x3F52,
0x922E, 0xCB1B, 0x2044, 0x7971, 0xAFCF, 0xF6FA, 0x1DA5, 0x4490,
0x1E68, 0x475D, 0xAC02, 0xF537, 0x2389, 0x7ABC, 0x91E3, 0xC8D6,
0x65AA, 0x3C9F, 0xD7C0, 0x8EF5, 0x584B, 0x017E, 0xEA21, 0xB314
};
/* This table was compiled using the polynom 0x755B */
static const unsigned crc16_precompiled_755B[] =
{
0x0000, 0x755b, 0xeab6, 0x9fed, 0xa037, 0xd56c, 0x4a81, 0x3fda, /* 0x00 */
0x3535, 0x406e, 0xdf83, 0xaad8, 0x9502, 0xe059, 0x7fb4, 0x0aef, /* 0x08 */
0x6a6a, 0x1f31, 0x80dc, 0xf587, 0xca5d, 0xbf06, 0x20eb, 0x55b0, /* 0x10 */
0x5f5f, 0x2a04, 0xb5e9, 0xc0b2, 0xff68, 0x8a33, 0x15de, 0x6085, /* 0x18 */
0xd4d4, 0xa18f, 0x3e62, 0x4b39, 0x74e3, 0x01b8, 0x9e55, 0xeb0e, /* 0x20 */
0xe1e1, 0x94ba, 0x0b57, 0x7e0c, 0x41d6, 0x348d, 0xab60, 0xde3b, /* 0x28 */
0xbebe, 0xcbe5, 0x5408, 0x2153, 0x1e89, 0x6bd2, 0xf43f, 0x8164, /* 0x30 */
0x8b8b, 0xfed0, 0x613d, 0x1466, 0x2bbc, 0x5ee7, 0xc10a, 0xb451, /* 0x38 */
0xdcf3, 0xa9a8, 0x3645, 0x431e, 0x7cc4, 0x099f, 0x9672, 0xe329, /* 0x40 */
0xe9c6, 0x9c9d, 0x0370, 0x762b, 0x49f1, 0x3caa, 0xa347, 0xd61c, /* 0x48 */
0xb699, 0xc3c2, 0x5c2f, 0x2974, 0x16ae, 0x63f5, 0xfc18, 0x8943, /* 0x50 */
0x83ac, 0xf6f7, 0x691a, 0x1c41, 0x239b, 0x56c0, 0xc92d, 0xbc76, /* 0x58 */
0x0827, 0x7d7c, 0xe291, 0x97ca, 0xa810, 0xdd4b, 0x42a6, 0x37fd, /* 0x60 */
0x3d12, 0x4849, 0xd7a4, 0xa2ff, 0x9d25, 0xe87e, 0x7793, 0x02c8, /* 0x68 */
0x624d, 0x1716, 0x88fb, 0xfda0, 0xc27a, 0xb721, 0x28cc, 0x5d97, /* 0x70 */
0x5778, 0x2223, 0xbdce, 0xc895, 0xf74f, 0x8214, 0x1df9, 0x68a2, /* 0x78 */
0xccbd, 0xb9e6, 0x260b, 0x5350, 0x6c8a, 0x19d1, 0x863c, 0xf367, /* 0x80 */
0xf988, 0x8cd3, 0x133e, 0x6665, 0x59bf, 0x2ce4, 0xb309, 0xc652, /* 0x88 */
0xa6d7, 0xd38c, 0x4c61, 0x393a, 0x06e0, 0x73bb, 0xec56, 0x990d, /* 0x90 */
0x93e2, 0xe6b9, 0x7954, 0x0c0f, 0x33d5, 0x468e, 0xd963, 0xac38, /* 0x98 */
0x1869, 0x6d32, 0xf2df, 0x8784, 0xb85e, 0xcd05, 0x52e8, 0x27b3, /* 0xA0 */
0x2d5c, 0x5807, 0xc7ea, 0xb2b1, 0x8d6b, 0xf830, 0x67dd, 0x1286, /* 0xA8 */
0x7203, 0x0758, 0x98b5, 0xedee, 0xd234, 0xa76f, 0x3882, 0x4dd9, /* 0xB0 */
0x4736, 0x326d, 0xad80, 0xd8db, 0xe701, 0x925a, 0x0db7, 0x78ec, /* 0xB8 */
0x104e, 0x6515, 0xfaf8, 0x8fa3, 0xb079, 0xc522, 0x5acf, 0x2f94, /* 0xC0 */
0x257b, 0x5020, 0xcfcd, 0xba96, 0x854c, 0xf017, 0x6ffa, 0x1aa1, /* 0xC8 */
0x7a24, 0x0f7f, 0x9092, 0xe5c9, 0xda13, 0xaf48, 0x30a5, 0x45fe, /* 0xD0 */
0x4f11, 0x3a4a, 0xa5a7, 0xd0fc, 0xef26, 0x9a7d, 0x0590, 0x70cb, /* 0xD8 */
0xc49a, 0xb1c1, 0x2e2c, 0x5b77, 0x64ad, 0x11f6, 0x8e1b, 0xfb40, /* 0xE0 */
0xf1af, 0x84f4, 0x1b19, 0x6e42, 0x5198, 0x24c3, 0xbb2e, 0xce75, /* 0xE8 */
0xaef0, 0xdbab, 0x4446, 0x311d, 0x0ec7, 0x7b9c, 0xe471, 0x912a, /* 0xF0 */
0x9bc5, 0xee9e, 0x7173, 0x0428, 0x3bf2, 0x4ea9, 0xd144, 0xa41f /* 0xF8 */
};
/* This table was compiled using the polynom: 0x9949 */
static const unsigned crc16_precompiled_9949_reverse[] =
{
0x0000, 0x0ED2, 0x1DA4, 0x1376, 0x3B48, 0x359A, 0x26EC, 0x283E,
0x7690, 0x7842, 0x6B34, 0x65E6, 0x4DD8, 0x430A, 0x507C, 0x5EAE,
0xED20, 0xE3F2, 0xF084, 0xFE56, 0xD668, 0xD8BA, 0xCBCC, 0xC51E,
0x9BB0, 0x9562, 0x8614, 0x88C6, 0xA0F8, 0xAE2A, 0xBD5C, 0xB38E,
0xFF73, 0xF1A1, 0xE2D7, 0xEC05, 0xC43B, 0xCAE9, 0xD99F, 0xD74D,
0x89E3, 0x8731, 0x9447, 0x9A95, 0xB2AB, 0xBC79, 0xAF0F, 0xA1DD,
0x1253, 0x1C81, 0x0FF7, 0x0125, 0x291B, 0x27C9, 0x34BF, 0x3A6D,
0x64C3, 0x6A11, 0x7967, 0x77B5, 0x5F8B, 0x5159, 0x422F, 0x4CFD,
0xDBD5, 0xD507, 0xC671, 0xC8A3, 0xE09D, 0xEE4F, 0xFD39, 0xF3EB,
0xAD45, 0xA397, 0xB0E1, 0xBE33, 0x960D, 0x98DF, 0x8BA9, 0x857B,
0x36F5, 0x3827, 0x2B51, 0x2583, 0x0DBD, 0x036F, 0x1019, 0x1ECB,
0x4065, 0x4EB7, 0x5DC1, 0x5313, 0x7B2D, 0x75FF, 0x6689, 0x685B,
0x24A6, 0x2A74, 0x3902, 0x37D0, 0x1FEE, 0x113C, 0x024A, 0x0C98,
0x5236, 0x5CE4, 0x4F92, 0x4140, 0x697E, 0x67AC, 0x74DA, 0x7A08,
0xC986, 0xC754, 0xD422, 0xDAF0, 0xF2CE, 0xFC1C, 0xEF6A, 0xE1B8,
0xBF16, 0xB1C4, 0xA2B2, 0xAC60, 0x845E, 0x8A8C, 0x99FA, 0x9728,
0x9299, 0x9C4B, 0x8F3D, 0x81EF, 0xA9D1, 0xA703, 0xB475, 0xBAA7,
0xE409, 0xEADB, 0xF9AD, 0xF77F, 0xDF41, 0xD193, 0xC2E5, 0xCC37,
0x7FB9, 0x716B, 0x621D, 0x6CCF, 0x44F1, 0x4A23, 0x5955, 0x5787,
0x0929, 0x07FB, 0x148D, 0x1A5F, 0x3261, 0x3CB3, 0x2FC5, 0x2117,
0x6DEA, 0x6338, 0x704E, 0x7E9C, 0x56A2, 0x5870, 0x4B06, 0x45D4,
0x1B7A, 0x15A8, 0x06DE, 0x080C, 0x2032, 0x2EE0, 0x3D96, 0x3344,
0x80CA, 0x8E18, 0x9D6E, 0x93BC, 0xBB82, 0xB550, 0xA626, 0xA8F4,
0xF65A, 0xF888, 0xEBFE, 0xE52C, 0xCD12, 0xC3C0, 0xD0B6, 0xDE64,
0x494C, 0x479E, 0x54E8, 0x5A3A, 0x7204, 0x7CD6, 0x6FA0, 0x6172,
0x3FDC, 0x310E, 0x2278, 0x2CAA, 0x0494, 0x0A46, 0x1930, 0x17E2,
0xA46C, 0xAABE, 0xB9C8, 0xB71A, 0x9F24, 0x91F6, 0x8280, 0x8C52,
0xD2FC, 0xDC2E, 0xCF58, 0xC18A, 0xE9B4, 0xE766, 0xF410, 0xFAC2,
0xB63F, 0xB8ED, 0xAB9B, 0xA549, 0x8D77, 0x83A5, 0x90D3, 0x9E01,
0xC0AF, 0xCE7D, 0xDD0B, 0xD3D9, 0xFBE7, 0xF535, 0xE643, 0xE891,
0x5B1F, 0x55CD, 0x46BB, 0x4869, 0x6057, 0x6E85, 0x7DF3, 0x7321,
0x2D8F, 0x235D, 0x302B, 0x3EF9, 0x16C7, 0x1815, 0x0B63, 0x05B1
};
/* This table was compiled using the polynom: 0x3D65 */
static const unsigned crc16_precompiled_3D65_reverse[] =
{
0x0000, 0x365E, 0x6CBC, 0x5AE2, 0xD978, 0xEF26, 0xB5C4, 0x839A,
0xFF89, 0xC9D7, 0x9335, 0xA56B, 0x26F1, 0x10AF, 0x4A4D, 0x7C13,
0xB26B, 0x8435, 0xDED7, 0xE889, 0x6B13, 0x5D4D, 0x07AF, 0x31F1,
0x4DE2, 0x7BBC, 0x215E, 0x1700, 0x949A, 0xA2C4, 0xF826, 0xCE78,
0x29AF, 0x1FF1, 0x4513, 0x734D, 0xF0D7, 0xC689, 0x9C6B, 0xAA35,
0xD626, 0xE078, 0xBA9A, 0x8CC4, 0x0F5E, 0x3900, 0x63E2, 0x55BC,
0x9BC4, 0xAD9A, 0xF778, 0xC126, 0x42BC, 0x74E2, 0x2E00, 0x185E,
0x644D, 0x5213, 0x08F1, 0x3EAF, 0xBD35, 0x8B6B, 0xD189, 0xE7D7,
0x535E, 0x6500, 0x3FE2, 0x09BC, 0x8A26, 0xBC78, 0xE69A, 0xD0C4,
0xACD7, 0x9A89, 0xC06B, 0xF635, 0x75AF, 0x43F1, 0x1913, 0x2F4D,
0xE135, 0xD76B, 0x8D89, 0xBBD7, 0x384D, 0x0E13, 0x54F1, 0x62AF,
0x1EBC, 0x28E2, 0x7200, 0x445E, 0xC7C4, 0xF19A, 0xAB78, 0x9D26,
0x7AF1, 0x4CAF, 0x164D, 0x2013, 0xA389, 0x95D7, 0xCF35, 0xF96B,
0x8578, 0xB326, 0xE9C4, 0xDF9A, 0x5C00, 0x6A5E, 0x30BC, 0x06E2,
0xC89A, 0xFEC4, 0xA426, 0x9278, 0x11E2, 0x27BC, 0x7D5E, 0x4B00,
0x3713, 0x014D, 0x5BAF, 0x6DF1, 0xEE6B, 0xD835, 0x82D7, 0xB489,
0xA6BC, 0x90E2, 0xCA00, 0xFC5E, 0x7FC4, 0x499A, 0x1378, 0x2526,
0x5935, 0x6F6B, 0x3589, 0x03D7, 0x804D, 0xB613, 0xECF1, 0xDAAF,
0x14D7, 0x2289, 0x786B, 0x4E35, 0xCDAF, 0xFBF1, 0xA113, 0x974D,
0xEB5E, 0xDD00, 0x87E2, 0xB1BC, 0x3226, 0x0478, 0x5E9A, 0x68C4,
0x8F13, 0xB94D, 0xE3AF, 0xD5F1, 0x566B, 0x6035, 0x3AD7, 0x0C89,
0x709A, 0x46C4, 0x1C26, 0x2A78, 0xA9E2, 0x9FBC, 0xC55E, 0xF300,
0x3D78, 0x0B26, 0x51C4, 0x679A, 0xE400, 0xD25E, 0x88BC, 0xBEE2,
0xC2F1, 0xF4AF, 0xAE4D, 0x9813, 0x1B89, 0x2DD7, 0x7735, 0x416B,
0xF5E2, 0xC3BC, 0x995E, 0xAF00, 0x2C9A, 0x1AC4, 0x4026, 0x7678,
0x0A6B, 0x3C35, 0x66D7, 0x5089, 0xD313, 0xE54D, 0xBFAF, 0x89F1,
0x4789, 0x71D7, 0x2B35, 0x1D6B, 0x9EF1, 0xA8AF, 0xF24D, 0xC413,
0xB800, 0x8E5E, 0xD4BC, 0xE2E2, 0x6178, 0x5726, 0x0DC4, 0x3B9A,
0xDC4D, 0xEA13, 0xB0F1, 0x86AF, 0x0535, 0x336B, 0x6989, 0x5FD7,
0x23C4, 0x159A, 0x4F78, 0x7926, 0xFABC, 0xCCE2, 0x9600, 0xA05E,
0x6E26, 0x5878, 0x029A, 0x34C4, 0xB75E, 0x8100, 0xDBE2, 0xEDBC,
0x91AF, 0xA7F1, 0xFD13, 0xCB4D, 0x48D7, 0x7E89, 0x246B, 0x1235
};
/* This table was compiled using the polynom: 0x080F */
static const unsigned crc16_precompiled_080F[] =
{
0x0000, 0x080F, 0x101E, 0x1811, 0x203C, 0x2833, 0x3022, 0x382D,
0x4078, 0x4877, 0x5066, 0x5869, 0x6044, 0x684B, 0x705A, 0x7855,
0x80F0, 0x88FF, 0x90EE, 0x98E1, 0xA0CC, 0xA8C3, 0xB0D2, 0xB8DD,
0xC088, 0xC887, 0xD096, 0xD899, 0xE0B4, 0xE8BB, 0xF0AA, 0xF8A5,
0x09EF, 0x01E0, 0x19F1, 0x11FE, 0x29D3, 0x21DC, 0x39CD, 0x31C2,
0x4997, 0x4198, 0x5989, 0x5186, 0x69AB, 0x61A4, 0x79B5, 0x71BA,
0x891F, 0x8110, 0x9901, 0x910E, 0xA923, 0xA12C, 0xB93D, 0xB132,
0xC967, 0xC168, 0xD979, 0xD176, 0xE95B, 0xE154, 0xF945, 0xF14A,
0x13DE, 0x1BD1, 0x03C0, 0x0BCF, 0x33E2, 0x3BED, 0x23FC, 0x2BF3,
0x53A6, 0x5BA9, 0x43B8, 0x4BB7, 0x739A, 0x7B95, 0x6384, 0x6B8B,
0x932E, 0x9B21, 0x8330, 0x8B3F, 0xB312, 0xBB1D, 0xA30C, 0xAB03,
0xD356, 0xDB59, 0xC348, 0xCB47, 0xF36A, 0xFB65, 0xE374, 0xEB7B,
0x1A31, 0x123E, 0x0A2F, 0x0220, 0x3A0D, 0x3202, 0x2A13, 0x221C,
0x5A49, 0x5246, 0x4A57, 0x4258, 0x7A75, 0x727A, 0x6A6B, 0x6264,
0x9AC1, 0x92CE, 0x8ADF, 0x82D0, 0xBAFD, 0xB2F2, 0xAAE3, 0xA2EC,
0xDAB9, 0xD2B6, 0xCAA7, 0xC2A8, 0xFA85, 0xF28A, 0xEA9B, 0xE294,
0x27BC, 0x2FB3, 0x37A2, 0x3FAD, 0x0780, 0x0F8F, 0x179E, 0x1F91,
0x67C4, 0x6FCB, 0x77DA, 0x7FD5, 0x47F8, 0x4FF7, 0x57E6, 0x5FE9,
0xA74C, 0xAF43, 0xB752, 0xBF5D, 0x8770, 0x8F7F, 0x976E, 0x9F61,
0xE734, 0xEF3B, 0xF72A, 0xFF25, 0xC708, 0xCF07, 0xD716, 0xDF19,
0x2E53, 0x265C, 0x3E4D, 0x3642, 0x0E6F, 0x0660, 0x1E71, 0x167E,
0x6E2B, 0x6624, 0x7E35, 0x763A, 0x4E17, 0x4618, 0x5E09, 0x5606,
0xAEA3, 0xA6AC, 0xBEBD, 0xB6B2, 0x8E9F, 0x8690, 0x9E81, 0x968E,
0xEEDB, 0xE6D4, 0xFEC5, 0xF6CA, 0xCEE7, 0xC6E8, 0xDEF9, 0xD6F6,
0x3462, 0x3C6D, 0x247C, 0x2C73, 0x145E, 0x1C51, 0x0440, 0x0C4F,
0x741A, 0x7C15, 0x6404, 0x6C0B, 0x5426, 0x5C29, 0x4438, 0x4C37,
0xB492, 0xBC9D, 0xA48C, 0xAC83, 0x94AE, 0x9CA1, 0x84B0, 0x8CBF,
0xF4EA, 0xFCE5, 0xE4F4, 0xECFB, 0xD4D6, 0xDCD9, 0xC4C8, 0xCCC7,
0x3D8D, 0x3582, 0x2D93, 0x259C, 0x1DB1, 0x15BE, 0x0DAF, 0x05A0,
0x7DF5, 0x75FA, 0x6DEB, 0x65E4, 0x5DC9, 0x55C6, 0x4DD7, 0x45D8,
0xBD7D, 0xB572, 0xAD63, 0xA56C, 0x9D41, 0x954E, 0x8D5F, 0x8550,
0xFD05, 0xF50A, 0xED1B, 0xE514, 0xDD39, 0xD536, 0xCD27, 0xC528
};
/**
* Generated on Fri Jul 19 17:16:42 2019
* by pycrc v0.9.2, https://pycrc.org
* using the configuration:
* - Width = 16
* - Poly = 0x8005
* - XorIn = 0xffff
* - ReflectIn = True
* - XorOut = 0xffff
* - ReflectOut = True
* - Algorithm = table-driven
*/
static const unsigned crc16_usb_table[] = {
0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040
};
static const uint16_t crc16_ccitt_start = 0xFFFF;
static const uint16_t crc16_ccitt_xorout = 0xFFFF;
static const uint16_t crc16_usb_start = 0xFFFF;
static const uint16_t crc16_usb_xorout = 0xFFFF;
/* two types of crcs are possible: unreflected (bits shift left) and
* reflected (bits shift right).
*/
static uint16_t crc16_unreflected(const uint8_t *buf, unsigned len,
uint16_t crc_in, const unsigned table[])
{
/* we use guints, rather than guint16s, as they are likely to be
faster. We just ignore the top 16 bits and let them do what they want.
*/
unsigned crc16 = (unsigned)crc_in;
while( len-- != 0 )
crc16 = table[((crc16 >> 8) ^ *buf++) & 0xff] ^ (crc16 << 8);
return (uint16_t)crc16;
}
static uint16_t crc16_reflected(const uint8_t *buf, unsigned len,
uint16_t crc_in, const unsigned table[])
{
/* we use guints, rather than guint16s, as they are likely to be
faster. We just ignore the top 16 bits and let them do what they want.
XXX - does any time saved not zero-extending uint16_t's to 32 bits
into a register outweigh any increased cache footprint from the
larger CRC table? */
unsigned crc16 = (unsigned)crc_in;
while( len-- != 0 )
crc16 = table[(crc16 ^ *buf++) & 0xff] ^ (crc16 >> 8);
return (uint16_t)crc16;
}
uint16_t crc16_ccitt(const uint8_t *buf, unsigned len)
{
return crc16_reflected(buf,len,crc16_ccitt_start,crc16_ccitt_table_reverse)
^ crc16_ccitt_xorout;
}
uint16_t crc16_x25_ccitt_seed(const uint8_t *buf, unsigned len, uint16_t seed)
{
return crc16_unreflected(buf,len,seed,crc16_ccitt_table);
}
uint16_t crc16_ccitt_seed(const uint8_t *buf, unsigned len, uint16_t seed)
{
return crc16_reflected(buf,len,seed,crc16_ccitt_table_reverse)
^ crc16_ccitt_xorout;
}
/* ISO14443-3, section 6.2.4: For ISO14443-A, the polynomial 0x1021 is
used, the initial register value shall be 0x6363, the final register
value is not XORed with anything. */
uint16_t crc16_iso14443a(const uint8_t *buf, unsigned len)
{
return crc16_reflected(buf,len, 0x6363 ,crc16_ccitt_table_reverse);
}
uint16_t crc16_usb(const uint8_t *buf, unsigned len)
{
return crc16_reflected(buf, len, crc16_usb_start, crc16_usb_table)
^ crc16_usb_xorout;
}
uint16_t crc16_0x5935(const uint8_t *buf, uint32_t len, uint16_t seed)
{
return crc16_unreflected(buf, len, seed, crc16_precompiled_5935);
}
uint16_t crc16_0x755B(const uint8_t *buf, uint32_t len, uint16_t seed)
{
return crc16_unreflected(buf, len, seed, crc16_precompiled_755B);
}
uint16_t crc16_0x9949_seed(const uint8_t *buf, unsigned len, uint16_t seed)
{
return crc16_reflected(buf, len, seed, crc16_precompiled_9949_reverse);
}
uint16_t crc16_0x3D65_seed(const uint8_t *buf, unsigned len, uint16_t seed)
{
return crc16_reflected(buf, len, seed, crc16_precompiled_3D65_reverse);
}
uint16_t crc16_0x080F_seed(const uint8_t *buf, unsigned len, uint16_t seed)
{
uint16_t crc = seed;
if (len > 0)
{
while (len-- > 0)
{
uint8_t data = *buf++;
crc = crc16_precompiled_080F[((crc >> 8) ^ data)] ^ (crc << 8);
}
}
return crc;
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/crc16.h | /** @file
* Declaration of CRC-16 routines and table
*
* 2004 Richard van der Hoff <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CRC16_H__
#define __CRC16_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/* Calculate the CCITT/ITU/CRC-16 16-bit CRC
(parameters for this CRC are:
Polynomial: x^16 + x^12 + x^5 + 1 (0x1021);
Start value 0xFFFF;
XOR result with 0xFFFF;
First bit is LSB)
*/
/** Compute CRC16 CCITT checksum of a buffer of data.
@param buf The buffer containing the data.
@param len The number of bytes to include in the computation.
@return The CRC16 CCITT checksum. */
WS_DLL_PUBLIC uint16_t crc16_ccitt(const uint8_t *buf, unsigned len);
/** Compute CRC16 X.25 CCITT checksum of a buffer of data.
@param buf The buffer containing the data.
@param len The number of bytes to include in the computation.
@return The CRC16 X.25 CCITT checksum. */
WS_DLL_PUBLIC uint16_t crc16_x25_ccitt_seed(const uint8_t *buf, unsigned len, uint16_t seed);
/** Compute CRC16 CCITT checksum of a buffer of data. If computing the
* checksum over multiple buffers and you want to feed the partial CRC16
* back in, remember to take the 1's complement of the partial CRC16 first.
@param buf The buffer containing the data.
@param len The number of bytes to include in the computation.
@param seed The seed to use.
@return The CRC16 CCITT checksum (using the given seed). */
WS_DLL_PUBLIC uint16_t crc16_ccitt_seed(const uint8_t *buf, unsigned len, uint16_t seed);
/** Compute the 16bit CRC_A value of a buffer as defined in ISO14443-3.
@param buf The buffer containing the data.
@param len The number of bytes to include in the computation.
@return the CRC16 checksum for the buffer */
WS_DLL_PUBLIC uint16_t crc16_iso14443a(const uint8_t *buf, unsigned len);
/** Compute the 16bit CRC value of a buffer as defined in USB Specification.
@param buf The buffer containing the data.
@param len The number of bytes to include in the computation.
@return the CRC16 checksum for the buffer */
WS_DLL_PUBLIC uint16_t crc16_usb(const uint8_t *buf, unsigned len);
/** Calculates a CRC16 checksum for the given buffer with the polynom
* 0x5935 using a precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC16 checksum for the buffer
*/
WS_DLL_PUBLIC uint16_t crc16_0x5935(const uint8_t *buf, uint32_t len, uint16_t seed);
/** Calculates a CRC16 checksum for the given buffer with the polynom
* 0x755B using a precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC16 checksum for the buffer
*/
WS_DLL_PUBLIC uint16_t crc16_0x755B(const uint8_t *buf, uint32_t len, uint16_t seed);
/** Computes CRC16 checksum for the given data with the polynom 0x9949 using
* precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC16 checksum for the buffer
*/
WS_DLL_PUBLIC uint16_t crc16_0x9949_seed(const uint8_t *buf, unsigned len, uint16_t seed);
/** Computes CRC16 checksum for the given data with the polynom 0x3D65 using
* precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC16 checksum for the buffer
*/
WS_DLL_PUBLIC uint16_t crc16_0x3D65_seed(const uint8_t *buf, unsigned len, uint16_t seed);
/** Computes CRC16 checksum for the given data with the polynom 0x080F using
* precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC16 checksum for the buffer
*/
WS_DLL_PUBLIC uint16_t crc16_0x080F_seed(const uint8_t *buf, unsigned len, uint16_t seed);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* crc16.h */ |
C | wireshark/wsutil/crc32.c | /* crc32.c
* CRC-32 routine
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Credits:
*
* Table from Solomon Peachy
* Routine from Chris Waters
*/
#include "config.h"
#include <wsutil/crc32.h>
#define CRC32_ACCUMULATE(c,d,table) (c=(c>>8)^(table)[(c^(d))&0xFF])
/*****************************************************************/
/* */
/* CRC32C LOOKUP TABLE */
/* +++================ */
/* The following CRC lookup table was generated automagically */
/* by the Rocksoft^tm Model CRC Algorithm Table Generation */
/* Program V1.0 using the following model parameters: */
/* */
/* Width : 4 bytes. */
/* Poly : 0x1EDC6F41L */
/* Reverse : TRUE. */
/* */
/* For more information on the Rocksoft^tm Model CRC Algorithm, */
/* see the document titled "A Painless Guide to CRC Error */
/* Detection Algorithms" by Ross Williams */
/* ([email protected].). This document is likely to be */
/* in the FTP archive "ftp.adelaide.edu.au/pub/rocksoft". */
/* */
/*****************************************************************/
#define CRC32C(c,d) CRC32_ACCUMULATE(c,d,crc32c_table)
static const uint32_t crc32c_table[256] = {
0x00000000U, 0xF26B8303U, 0xE13B70F7U, 0x1350F3F4U, 0xC79A971FU,
0x35F1141CU, 0x26A1E7E8U, 0xD4CA64EBU, 0x8AD958CFU, 0x78B2DBCCU,
0x6BE22838U, 0x9989AB3BU, 0x4D43CFD0U, 0xBF284CD3U, 0xAC78BF27U,
0x5E133C24U, 0x105EC76FU, 0xE235446CU, 0xF165B798U, 0x030E349BU,
0xD7C45070U, 0x25AFD373U, 0x36FF2087U, 0xC494A384U, 0x9A879FA0U,
0x68EC1CA3U, 0x7BBCEF57U, 0x89D76C54U, 0x5D1D08BFU, 0xAF768BBCU,
0xBC267848U, 0x4E4DFB4BU, 0x20BD8EDEU, 0xD2D60DDDU, 0xC186FE29U,
0x33ED7D2AU, 0xE72719C1U, 0x154C9AC2U, 0x061C6936U, 0xF477EA35U,
0xAA64D611U, 0x580F5512U, 0x4B5FA6E6U, 0xB93425E5U, 0x6DFE410EU,
0x9F95C20DU, 0x8CC531F9U, 0x7EAEB2FAU, 0x30E349B1U, 0xC288CAB2U,
0xD1D83946U, 0x23B3BA45U, 0xF779DEAEU, 0x05125DADU, 0x1642AE59U,
0xE4292D5AU, 0xBA3A117EU, 0x4851927DU, 0x5B016189U, 0xA96AE28AU,
0x7DA08661U, 0x8FCB0562U, 0x9C9BF696U, 0x6EF07595U, 0x417B1DBCU,
0xB3109EBFU, 0xA0406D4BU, 0x522BEE48U, 0x86E18AA3U, 0x748A09A0U,
0x67DAFA54U, 0x95B17957U, 0xCBA24573U, 0x39C9C670U, 0x2A993584U,
0xD8F2B687U, 0x0C38D26CU, 0xFE53516FU, 0xED03A29BU, 0x1F682198U,
0x5125DAD3U, 0xA34E59D0U, 0xB01EAA24U, 0x42752927U, 0x96BF4DCCU,
0x64D4CECFU, 0x77843D3BU, 0x85EFBE38U, 0xDBFC821CU, 0x2997011FU,
0x3AC7F2EBU, 0xC8AC71E8U, 0x1C661503U, 0xEE0D9600U, 0xFD5D65F4U,
0x0F36E6F7U, 0x61C69362U, 0x93AD1061U, 0x80FDE395U, 0x72966096U,
0xA65C047DU, 0x5437877EU, 0x4767748AU, 0xB50CF789U, 0xEB1FCBADU,
0x197448AEU, 0x0A24BB5AU, 0xF84F3859U, 0x2C855CB2U, 0xDEEEDFB1U,
0xCDBE2C45U, 0x3FD5AF46U, 0x7198540DU, 0x83F3D70EU, 0x90A324FAU,
0x62C8A7F9U, 0xB602C312U, 0x44694011U, 0x5739B3E5U, 0xA55230E6U,
0xFB410CC2U, 0x092A8FC1U, 0x1A7A7C35U, 0xE811FF36U, 0x3CDB9BDDU,
0xCEB018DEU, 0xDDE0EB2AU, 0x2F8B6829U, 0x82F63B78U, 0x709DB87BU,
0x63CD4B8FU, 0x91A6C88CU, 0x456CAC67U, 0xB7072F64U, 0xA457DC90U,
0x563C5F93U, 0x082F63B7U, 0xFA44E0B4U, 0xE9141340U, 0x1B7F9043U,
0xCFB5F4A8U, 0x3DDE77ABU, 0x2E8E845FU, 0xDCE5075CU, 0x92A8FC17U,
0x60C37F14U, 0x73938CE0U, 0x81F80FE3U, 0x55326B08U, 0xA759E80BU,
0xB4091BFFU, 0x466298FCU, 0x1871A4D8U, 0xEA1A27DBU, 0xF94AD42FU,
0x0B21572CU, 0xDFEB33C7U, 0x2D80B0C4U, 0x3ED04330U, 0xCCBBC033U,
0xA24BB5A6U, 0x502036A5U, 0x4370C551U, 0xB11B4652U, 0x65D122B9U,
0x97BAA1BAU, 0x84EA524EU, 0x7681D14DU, 0x2892ED69U, 0xDAF96E6AU,
0xC9A99D9EU, 0x3BC21E9DU, 0xEF087A76U, 0x1D63F975U, 0x0E330A81U,
0xFC588982U, 0xB21572C9U, 0x407EF1CAU, 0x532E023EU, 0xA145813DU,
0x758FE5D6U, 0x87E466D5U, 0x94B49521U, 0x66DF1622U, 0x38CC2A06U,
0xCAA7A905U, 0xD9F75AF1U, 0x2B9CD9F2U, 0xFF56BD19U, 0x0D3D3E1AU,
0x1E6DCDEEU, 0xEC064EEDU, 0xC38D26C4U, 0x31E6A5C7U, 0x22B65633U,
0xD0DDD530U, 0x0417B1DBU, 0xF67C32D8U, 0xE52CC12CU, 0x1747422FU,
0x49547E0BU, 0xBB3FFD08U, 0xA86F0EFCU, 0x5A048DFFU, 0x8ECEE914U,
0x7CA56A17U, 0x6FF599E3U, 0x9D9E1AE0U, 0xD3D3E1ABU, 0x21B862A8U,
0x32E8915CU, 0xC083125FU, 0x144976B4U, 0xE622F5B7U, 0xF5720643U,
0x07198540U, 0x590AB964U, 0xAB613A67U, 0xB831C993U, 0x4A5A4A90U,
0x9E902E7BU, 0x6CFBAD78U, 0x7FAB5E8CU, 0x8DC0DD8FU, 0xE330A81AU,
0x115B2B19U, 0x020BD8EDU, 0xF0605BEEU, 0x24AA3F05U, 0xD6C1BC06U,
0xC5914FF2U, 0x37FACCF1U, 0x69E9F0D5U, 0x9B8273D6U, 0x88D28022U,
0x7AB90321U, 0xAE7367CAU, 0x5C18E4C9U, 0x4F48173DU, 0xBD23943EU,
0xF36E6F75U, 0x0105EC76U, 0x12551F82U, 0xE03E9C81U, 0x34F4F86AU,
0xC69F7B69U, 0xD5CF889DU, 0x27A40B9EU, 0x79B737BAU, 0x8BDCB4B9U,
0x988C474DU, 0x6AE7C44EU, 0xBE2DA0A5U, 0x4C4623A6U, 0x5F16D052U,
0xAD7D5351U };
/*
* Table for the AUTODIN/HDLC/802.x CRC.
*
* Polynomial is
*
* x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 +
* x^7 + x^5 + x^4 + x^2 + x + 1
*/
static const uint32_t crc32_ccitt_table[256] = {
0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
0x2d02ef8d
};
/*
* Table for the MPEG-2 CRC.
*
* Polynomial is
*
* x^32 + x^26 + x^23 + x^22 + x^16 + x^12 + x^11 + x^10 + x^8 +
* x^7 + x^5 + x^4 + x^2 + x + 1
*
* (which is the same polynomial as the one above us).
*
* NOTE: this is also used for ATM AAL5.
*/
static const uint32_t crc32_mpeg2_table[256] = {
0x00000000, 0x04c11db7, 0x09823b6e, 0x0d4326d9, 0x130476dc, 0x17c56b6b,
0x1a864db2, 0x1e475005, 0x2608edb8, 0x22c9f00f, 0x2f8ad6d6, 0x2b4bcb61,
0x350c9b64, 0x31cd86d3, 0x3c8ea00a, 0x384fbdbd, 0x4c11db70, 0x48d0c6c7,
0x4593e01e, 0x4152fda9, 0x5f15adac, 0x5bd4b01b, 0x569796c2, 0x52568b75,
0x6a1936c8, 0x6ed82b7f, 0x639b0da6, 0x675a1011, 0x791d4014, 0x7ddc5da3,
0x709f7b7a, 0x745e66cd, 0x9823b6e0, 0x9ce2ab57, 0x91a18d8e, 0x95609039,
0x8b27c03c, 0x8fe6dd8b, 0x82a5fb52, 0x8664e6e5, 0xbe2b5b58, 0xbaea46ef,
0xb7a96036, 0xb3687d81, 0xad2f2d84, 0xa9ee3033, 0xa4ad16ea, 0xa06c0b5d,
0xd4326d90, 0xd0f37027, 0xddb056fe, 0xd9714b49, 0xc7361b4c, 0xc3f706fb,
0xceb42022, 0xca753d95, 0xf23a8028, 0xf6fb9d9f, 0xfbb8bb46, 0xff79a6f1,
0xe13ef6f4, 0xe5ffeb43, 0xe8bccd9a, 0xec7dd02d, 0x34867077, 0x30476dc0,
0x3d044b19, 0x39c556ae, 0x278206ab, 0x23431b1c, 0x2e003dc5, 0x2ac12072,
0x128e9dcf, 0x164f8078, 0x1b0ca6a1, 0x1fcdbb16, 0x018aeb13, 0x054bf6a4,
0x0808d07d, 0x0cc9cdca, 0x7897ab07, 0x7c56b6b0, 0x71159069, 0x75d48dde,
0x6b93dddb, 0x6f52c06c, 0x6211e6b5, 0x66d0fb02, 0x5e9f46bf, 0x5a5e5b08,
0x571d7dd1, 0x53dc6066, 0x4d9b3063, 0x495a2dd4, 0x44190b0d, 0x40d816ba,
0xaca5c697, 0xa864db20, 0xa527fdf9, 0xa1e6e04e, 0xbfa1b04b, 0xbb60adfc,
0xb6238b25, 0xb2e29692, 0x8aad2b2f, 0x8e6c3698, 0x832f1041, 0x87ee0df6,
0x99a95df3, 0x9d684044, 0x902b669d, 0x94ea7b2a, 0xe0b41de7, 0xe4750050,
0xe9362689, 0xedf73b3e, 0xf3b06b3b, 0xf771768c, 0xfa325055, 0xfef34de2,
0xc6bcf05f, 0xc27dede8, 0xcf3ecb31, 0xcbffd686, 0xd5b88683, 0xd1799b34,
0xdc3abded, 0xd8fba05a, 0x690ce0ee, 0x6dcdfd59, 0x608edb80, 0x644fc637,
0x7a089632, 0x7ec98b85, 0x738aad5c, 0x774bb0eb, 0x4f040d56, 0x4bc510e1,
0x46863638, 0x42472b8f, 0x5c007b8a, 0x58c1663d, 0x558240e4, 0x51435d53,
0x251d3b9e, 0x21dc2629, 0x2c9f00f0, 0x285e1d47, 0x36194d42, 0x32d850f5,
0x3f9b762c, 0x3b5a6b9b, 0x0315d626, 0x07d4cb91, 0x0a97ed48, 0x0e56f0ff,
0x1011a0fa, 0x14d0bd4d, 0x19939b94, 0x1d528623, 0xf12f560e, 0xf5ee4bb9,
0xf8ad6d60, 0xfc6c70d7, 0xe22b20d2, 0xe6ea3d65, 0xeba91bbc, 0xef68060b,
0xd727bbb6, 0xd3e6a601, 0xdea580d8, 0xda649d6f, 0xc423cd6a, 0xc0e2d0dd,
0xcda1f604, 0xc960ebb3, 0xbd3e8d7e, 0xb9ff90c9, 0xb4bcb610, 0xb07daba7,
0xae3afba2, 0xaafbe615, 0xa7b8c0cc, 0xa379dd7b, 0x9b3660c6, 0x9ff77d71,
0x92b45ba8, 0x9675461f, 0x8832161a, 0x8cf30bad, 0x81b02d74, 0x857130c3,
0x5d8a9099, 0x594b8d2e, 0x5408abf7, 0x50c9b640, 0x4e8ee645, 0x4a4ffbf2,
0x470cdd2b, 0x43cdc09c, 0x7b827d21, 0x7f436096, 0x7200464f, 0x76c15bf8,
0x68860bfd, 0x6c47164a, 0x61043093, 0x65c52d24, 0x119b4be9, 0x155a565e,
0x18197087, 0x1cd86d30, 0x029f3d35, 0x065e2082, 0x0b1d065b, 0x0fdc1bec,
0x3793a651, 0x3352bbe6, 0x3e119d3f, 0x3ad08088, 0x2497d08d, 0x2056cd3a,
0x2d15ebe3, 0x29d4f654, 0xc5a92679, 0xc1683bce, 0xcc2b1d17, 0xc8ea00a0,
0xd6ad50a5, 0xd26c4d12, 0xdf2f6bcb, 0xdbee767c, 0xe3a1cbc1, 0xe760d676,
0xea23f0af, 0xeee2ed18, 0xf0a5bd1d, 0xf464a0aa, 0xf9278673, 0xfde69bc4,
0x89b8fd09, 0x8d79e0be, 0x803ac667, 0x84fbdbd0, 0x9abc8bd5, 0x9e7d9662,
0x933eb0bb, 0x97ffad0c, 0xafb010b1, 0xab710d06, 0xa6322bdf, 0xa2f33668,
0xbcb4666d, 0xb8757bda, 0xb5365d03, 0xb1f740b4
};
/* This table was compiled using the polynom: 0x0AA725CF*/
static const uint32_t crc32_0AA725CF_reverse[] = {
0x00000000U, 0xCEAA95CEU, 0x7A1CE13DU, 0xB4B674F3U,
0xF439C27AU, 0x3A9357B4U, 0x8E252347U, 0x408FB689U,
0x0F3A4E55U, 0xC190DB9BU, 0x7526AF68U, 0xBB8C3AA6U,
0xFB038C2FU, 0x35A919E1U, 0x811F6D12U, 0x4FB5F8DCU,
0x1E749CAAU, 0xD0DE0964U, 0x64687D97U, 0xAAC2E859U,
0xEA4D5ED0U, 0x24E7CB1EU, 0x9051BFEDU, 0x5EFB2A23U,
0x114ED2FFU, 0xDFE44731U, 0x6B5233C2U, 0xA5F8A60CU,
0xE5771085U, 0x2BDD854BU, 0x9F6BF1B8U, 0x51C16476U,
0x3CE93954U, 0xF243AC9AU, 0x46F5D869U, 0x885F4DA7U,
0xC8D0FB2EU, 0x067A6EE0U, 0xB2CC1A13U, 0x7C668FDDU,
0x33D37701U, 0xFD79E2CFU, 0x49CF963CU, 0x876503F2U,
0xC7EAB57BU, 0x094020B5U, 0xBDF65446U, 0x735CC188U,
0x229DA5FEU, 0xEC373030U, 0x588144C3U, 0x962BD10DU,
0xD6A46784U, 0x180EF24AU, 0xACB886B9U, 0x62121377U,
0x2DA7EBABU, 0xE30D7E65U, 0x57BB0A96U, 0x99119F58U,
0xD99E29D1U, 0x1734BC1FU, 0xA382C8ECU, 0x6D285D22U,
0x79D272A8U, 0xB778E766U, 0x03CE9395U, 0xCD64065BU,
0x8DEBB0D2U, 0x4341251CU, 0xF7F751EFU, 0x395DC421U,
0x76E83CFDU, 0xB842A933U, 0x0CF4DDC0U, 0xC25E480EU,
0x82D1FE87U, 0x4C7B6B49U, 0xF8CD1FBAU, 0x36678A74U,
0x67A6EE02U, 0xA90C7BCCU, 0x1DBA0F3FU, 0xD3109AF1U,
0x939F2C78U, 0x5D35B9B6U, 0xE983CD45U, 0x2729588BU,
0x689CA057U, 0xA6363599U, 0x1280416AU, 0xDC2AD4A4U,
0x9CA5622DU, 0x520FF7E3U, 0xE6B98310U, 0x281316DEU,
0x453B4BFCU, 0x8B91DE32U, 0x3F27AAC1U, 0xF18D3F0FU,
0xB1028986U, 0x7FA81C48U, 0xCB1E68BBU, 0x05B4FD75U,
0x4A0105A9U, 0x84AB9067U, 0x301DE494U, 0xFEB7715AU,
0xBE38C7D3U, 0x7092521DU, 0xC42426EEU, 0x0A8EB320U,
0x5B4FD756U, 0x95E54298U, 0x2153366BU, 0xEFF9A3A5U,
0xAF76152CU, 0x61DC80E2U, 0xD56AF411U, 0x1BC061DFU,
0x54759903U, 0x9ADF0CCDU, 0x2E69783EU, 0xE0C3EDF0U,
0xA04C5B79U, 0x6EE6CEB7U, 0xDA50BA44U, 0x14FA2F8AU,
0xF3A4E550U, 0x3D0E709EU, 0x89B8046DU, 0x471291A3U,
0x079D272AU, 0xC937B2E4U, 0x7D81C617U, 0xB32B53D9U,
0xFC9EAB05U, 0x32343ECBU, 0x86824A38U, 0x4828DFF6U,
0x08A7697FU, 0xC60DFCB1U, 0x72BB8842U, 0xBC111D8CU,
0xEDD079FAU, 0x237AEC34U, 0x97CC98C7U, 0x59660D09U,
0x19E9BB80U, 0xD7432E4EU, 0x63F55ABDU, 0xAD5FCF73U,
0xE2EA37AFU, 0x2C40A261U, 0x98F6D692U, 0x565C435CU,
0x16D3F5D5U, 0xD879601BU, 0x6CCF14E8U, 0xA2658126U,
0xCF4DDC04U, 0x01E749CAU, 0xB5513D39U, 0x7BFBA8F7U,
0x3B741E7EU, 0xF5DE8BB0U, 0x4168FF43U, 0x8FC26A8DU,
0xC0779251U, 0x0EDD079FU, 0xBA6B736CU, 0x74C1E6A2U,
0x344E502BU, 0xFAE4C5E5U, 0x4E52B116U, 0x80F824D8U,
0xD13940AEU, 0x1F93D560U, 0xAB25A193U, 0x658F345DU,
0x250082D4U, 0xEBAA171AU, 0x5F1C63E9U, 0x91B6F627U,
0xDE030EFBU, 0x10A99B35U, 0xA41FEFC6U, 0x6AB57A08U,
0x2A3ACC81U, 0xE490594FU, 0x50262DBCU, 0x9E8CB872U,
0x8A7697F8U, 0x44DC0236U, 0xF06A76C5U, 0x3EC0E30BU,
0x7E4F5582U, 0xB0E5C04CU, 0x0453B4BFU, 0xCAF92171U,
0x854CD9ADU, 0x4BE64C63U, 0xFF503890U, 0x31FAAD5EU,
0x71751BD7U, 0xBFDF8E19U, 0x0B69FAEAU, 0xC5C36F24U,
0x94020B52U, 0x5AA89E9CU, 0xEE1EEA6FU, 0x20B47FA1U,
0x603BC928U, 0xAE915CE6U, 0x1A272815U, 0xD48DBDDBU,
0x9B384507U, 0x5592D0C9U, 0xE124A43AU, 0x2F8E31F4U,
0x6F01877DU, 0xA1AB12B3U, 0x151D6640U, 0xDBB7F38EU,
0xB69FAEACU, 0x78353B62U, 0xCC834F91U, 0x0229DA5FU,
0x42A66CD6U, 0x8C0CF918U, 0x38BA8DEBU, 0xF6101825U,
0xB9A5E0F9U, 0x770F7537U, 0xC3B901C4U, 0x0D13940AU,
0x4D9C2283U, 0x8336B74DU, 0x3780C3BEU, 0xF92A5670U,
0xA8EB3206U, 0x6641A7C8U, 0xD2F7D33BU, 0x1C5D46F5U,
0x5CD2F07CU, 0x927865B2U, 0x26CE1141U, 0xE864848FU,
0xA7D17C53U, 0x697BE99DU, 0xDDCD9D6EU, 0x136708A0U,
0x53E8BE29U, 0x9D422BE7U, 0x29F45F14U, 0xE75ECADAU
};
/* This table was compiled using the polynom: 0x5D6DCB */
static const uint32_t crc32_5D6DCB[] =
{
0x00000000, 0x005d6dcb, 0x00badb96, 0x00e7b65d,
0x0028dae7, 0x0075b72c, 0x00920171, 0x00cf6cba,
0x0051b5ce, 0x000cd805, 0x00eb6e58, 0x00b60393,
0x00796f29, 0x002402e2, 0x00c3b4bf, 0x009ed974,
0x00a36b9c, 0x00fe0657, 0x0019b00a, 0x0044ddc1,
0x008bb17b, 0x00d6dcb0, 0x00316aed, 0x006c0726,
0x00f2de52, 0x00afb399, 0x004805c4, 0x0015680f,
0x00da04b5, 0x0087697e, 0x0060df23, 0x003db2e8,
0x001bbaf3, 0x0046d738, 0x00a16165, 0x00fc0cae,
0x00336014, 0x006e0ddf, 0x0089bb82, 0x00d4d649,
0x004a0f3d, 0x001762f6, 0x00f0d4ab, 0x00adb960,
0x0062d5da, 0x003fb811, 0x00d80e4c, 0x00856387,
0x00b8d16f, 0x00e5bca4, 0x00020af9, 0x005f6732,
0x00900b88, 0x00cd6643, 0x002ad01e, 0x0077bdd5,
0x00e964a1, 0x00b4096a, 0x0053bf37, 0x000ed2fc,
0x00c1be46, 0x009cd38d, 0x007b65d0, 0x0026081b,
0x003775e6, 0x006a182d, 0x008dae70, 0x00d0c3bb,
0x001faf01, 0x0042c2ca, 0x00a57497, 0x00f8195c,
0x0066c028, 0x003bade3, 0x00dc1bbe, 0x00817675,
0x004e1acf, 0x00137704, 0x00f4c159, 0x00a9ac92,
0x00941e7a, 0x00c973b1, 0x002ec5ec, 0x0073a827,
0x00bcc49d, 0x00e1a956, 0x00061f0b, 0x005b72c0,
0x00c5abb4, 0x0098c67f, 0x007f7022, 0x00221de9,
0x00ed7153, 0x00b01c98, 0x0057aac5, 0x000ac70e,
0x002ccf15, 0x0071a2de, 0x00961483, 0x00cb7948,
0x000415f2, 0x00597839, 0x00bece64, 0x00e3a3af,
0x007d7adb, 0x00201710, 0x00c7a14d, 0x009acc86,
0x0055a03c, 0x0008cdf7, 0x00ef7baa, 0x00b21661,
0x008fa489, 0x00d2c942, 0x00357f1f, 0x006812d4,
0x00a77e6e, 0x00fa13a5, 0x001da5f8, 0x0040c833,
0x00de1147, 0x00837c8c, 0x0064cad1, 0x0039a71a,
0x00f6cba0, 0x00aba66b, 0x004c1036, 0x00117dfd,
0x006eebcc, 0x00338607, 0x00d4305a, 0x00895d91,
0x0046312b, 0x001b5ce0, 0x00fceabd, 0x00a18776,
0x003f5e02, 0x006233c9, 0x00858594, 0x00d8e85f,
0x001784e5, 0x004ae92e, 0x00ad5f73, 0x00f032b8,
0x00cd8050, 0x0090ed9b, 0x00775bc6, 0x002a360d,
0x00e55ab7, 0x00b8377c, 0x005f8121, 0x0002ecea,
0x009c359e, 0x00c15855, 0x0026ee08, 0x007b83c3,
0x00b4ef79, 0x00e982b2, 0x000e34ef, 0x00535924,
0x0075513f, 0x00283cf4, 0x00cf8aa9, 0x0092e762,
0x005d8bd8, 0x0000e613, 0x00e7504e, 0x00ba3d85,
0x0024e4f1, 0x0079893a, 0x009e3f67, 0x00c352ac,
0x000c3e16, 0x005153dd, 0x00b6e580, 0x00eb884b,
0x00d63aa3, 0x008b5768, 0x006ce135, 0x00318cfe,
0x00fee044, 0x00a38d8f, 0x00443bd2, 0x00195619,
0x00878f6d, 0x00dae2a6, 0x003d54fb, 0x00603930,
0x00af558a, 0x00f23841, 0x00158e1c, 0x0048e3d7,
0x00599e2a, 0x0004f3e1, 0x00e345bc, 0x00be2877,
0x007144cd, 0x002c2906, 0x00cb9f5b, 0x0096f290,
0x00082be4, 0x0055462f, 0x00b2f072, 0x00ef9db9,
0x0020f103, 0x007d9cc8, 0x009a2a95, 0x00c7475e,
0x00faf5b6, 0x00a7987d, 0x00402e20, 0x001d43eb,
0x00d22f51, 0x008f429a, 0x0068f4c7, 0x0035990c,
0x00ab4078, 0x00f62db3, 0x00119bee, 0x004cf625,
0x00839a9f, 0x00def754, 0x00394109, 0x00642cc2,
0x004224d9, 0x001f4912, 0x00f8ff4f, 0x00a59284,
0x006afe3e, 0x003793f5, 0x00d025a8, 0x008d4863,
0x00139117, 0x004efcdc, 0x00a94a81, 0x00f4274a,
0x003b4bf0, 0x0066263b, 0x00819066, 0x00dcfdad,
0x00e14f45, 0x00bc228e, 0x005b94d3, 0x0006f918,
0x00c995a2, 0x0094f869, 0x00734e34, 0x002e23ff,
0x00b0fa8b, 0x00ed9740, 0x000a211d, 0x00574cd6,
0x0098206c, 0x00c54da7, 0x0022fbfa, 0x007f9631
};
uint32_t
crc32c_table_lookup (unsigned char pos)
{
return crc32c_table[pos];
}
uint32_t
crc32_ccitt_table_lookup (unsigned char pos)
{
return crc32_ccitt_table[pos];
}
uint32_t
crc32c_calculate(const void *buf, int len, uint32_t crc)
{
const uint8_t *p = (const uint8_t *)buf;
crc = CRC32C_SWAP(crc);
while (len-- > 0) {
CRC32C(crc, *p++);
}
return CRC32C_SWAP(crc);
}
uint32_t
crc32c_calculate_no_swap(const void *buf, int len, uint32_t crc)
{
const uint8_t *p = (const uint8_t *)buf;
while (len-- > 0) {
CRC32C(crc, *p++);
}
return crc;
}
uint32_t
crc32_ccitt(const uint8_t *buf, unsigned len)
{
return (crc32_ccitt_seed(buf, len, CRC32_CCITT_SEED));
}
uint32_t
crc32_ccitt_seed(const uint8_t *buf, unsigned len, uint32_t seed)
{
unsigned i;
uint32_t crc32 = seed;
for (i = 0; i < len; i++)
CRC32_ACCUMULATE(crc32, buf[i], crc32_ccitt_table);
return ( ~crc32 );
}
uint32_t
crc32_mpeg2_seed(const uint8_t *buf, unsigned len, uint32_t seed)
{
unsigned i;
uint32_t crc32;
crc32 = seed;
for (i = 0; i < len; i++)
crc32 = (crc32 << 8) ^ crc32_mpeg2_table[((crc32 >> 24) ^ buf[i]) & 0xff];
return ( crc32 );
}
uint32_t
crc32_0x0AA725CF_seed(const uint8_t *buf, unsigned len, uint32_t seed)
{
unsigned crc32;
crc32 = (unsigned)seed;
while( len-- != 0 )
CRC32_ACCUMULATE(crc32, *buf++, crc32_0AA725CF_reverse);
return (uint32_t)crc32;
}
uint32_t
crc32_0x5D6DCB_seed(const uint8_t *buf, unsigned len, uint32_t seed)
{
uint32_t crc = seed;
if (len > 0)
{
while (len-- > 0)
{
uint8_t data = *buf++;
/* XOR data with CRC2, look up result, then XOR that with CRC; */
crc = crc32_5D6DCB[((crc >> 16) ^ data) & 0xff] ^ (crc << 8);
}
}
return (crc & 0x00ffffff);
}
/*
* 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:
*/ |
C/C++ | wireshark/wsutil/crc32.h | /** @file
* Declaration of CRC-32 routine and table
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CRC32_H__
#define __CRC32_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
#define CRC32_CCITT_SEED 0xFFFFFFFF
#define CRC32C_PRELOAD 0xffffffff
#define CRC32_MPEG2_SEED 0xFFFFFFFF
/*
* Byte swap fix contributed by Dave Wysochanski <[email protected]>.
*/
#define CRC32C_SWAP(crc32c_value) \
(((crc32c_value & 0xff000000) >> 24) | \
((crc32c_value & 0x00ff0000) >> 8) | \
((crc32c_value & 0x0000ff00) << 8) | \
((crc32c_value & 0x000000ff) << 24))
/** Lookup the crc value in the crc32_ccitt_table
@param pos Position in the table. */
WS_DLL_PUBLIC uint32_t crc32_ccitt_table_lookup (unsigned char pos);
/** Lookup the crc value in the crc32c_table
@param pos Position in the table. */
WS_DLL_PUBLIC uint32_t crc32c_table_lookup (unsigned char pos);
/** Compute CRC32C checksum of a buffer of data.
@param buf The buffer containing the data.
@param len The number of bytes to include in the computation.
@param crc The preload value for the CRC32C computation.
@return The CRC32C checksum. */
WS_DLL_PUBLIC uint32_t crc32c_calculate(const void *buf, int len, uint32_t crc);
/** Compute CRC32C checksum of a buffer of data without swapping seed crc
or completed checksum
@param buf The buffer containing the data.
@param len The number of bytes to include in the computation.
@param crc The preload value for the CRC32C computation.
@return The CRC32C checksum. */
WS_DLL_PUBLIC uint32_t crc32c_calculate_no_swap(const void *buf, int len, uint32_t crc);
/** Compute CRC32 CCITT checksum of a buffer of data.
@param buf The buffer containing the data.
@param len The number of bytes to include in the computation.
@return The CRC32 CCITT checksum. */
WS_DLL_PUBLIC uint32_t crc32_ccitt(const uint8_t *buf, unsigned len);
/** Compute CRC32 CCITT checksum of a buffer of data. If computing the
* checksum over multiple buffers and you want to feed the partial CRC32
* back in, remember to take the 1's complement of the partial CRC32 first.
@param buf The buffer containing the data.
@param len The number of bytes to include in the computation.
@param seed The seed to use.
@return The CRC32 CCITT checksum (using the given seed). */
WS_DLL_PUBLIC uint32_t crc32_ccitt_seed(const uint8_t *buf, unsigned len, uint32_t seed);
/** Compute MPEG-2 CRC32 checksum of a buffer of data.
@param buf The buffer containing the data.
@param len The number of bytes to include in the computation.
@param seed The seed to use.
@return The CRC32 MPEG-2 checksum (using the given seed). */
WS_DLL_PUBLIC uint32_t crc32_mpeg2_seed(const uint8_t *buf, unsigned len, uint32_t seed);
/** Computes CRC32 checksum for the given data with the polynom 0x0AA725CF using
* precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC32 checksum for the buffer
*/
WS_DLL_PUBLIC uint32_t crc32_0x0AA725CF_seed(const uint8_t *buf, unsigned len, uint32_t seed);
/** Computes CRC32 checksum for the given data with the polynom 0x5D6DCB using
* precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC32 checksum for the buffer
*/
WS_DLL_PUBLIC uint32_t crc32_0x5D6DCB_seed(const uint8_t *buf, unsigned len, uint32_t seed);
WS_DLL_PUBLIC int Dot11DecryptWepDecrypt(
const unsigned char *seed,
const size_t seed_len,
unsigned char *cypher_text,
const size_t data_len);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* crc32.h */ |
C | wireshark/wsutil/crc5.c | /* crc5.c
* CRC-5 routine
*
* 2019 Tomasz Mon <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <wsutil/crc5.h>
static uint8_t crc5_usb_bits(uint32_t v, int vl, uint8_t ival)
{
/* This function is based on code posted by John Sullivan to Wireshark-dev
* mailing list on Jul 21, 2019.
*
* "One of the properties of LFSRs is that a 1 bit in the input toggles a
* completely predictable set of register bits *at any point in the
* future*. This isn't often useful for most CRC caculations on variable
* sized input, as the cost of working out which those bits are vastly
* outweighs most other methods."
*
* In USB 2.0, the CRC5 is calculated on either 11 or 19 bits inputs,
* and thus this approach is viable.
*/
uint8_t rv = ival;
static const uint8_t bvals[19] = {
0x1e, 0x15, 0x03, 0x06, 0x0c, 0x18, 0x19, 0x1b,
0x1f, 0x17, 0x07, 0x0e, 0x1c, 0x11, 0x0b, 0x16,
0x05, 0x0a, 0x14
};
for (int i = 0; i < vl; i++) {
if (v & (1 << i)) {
rv ^= bvals[19 - vl + i];
}
}
return rv;
}
uint8_t crc5_usb_11bit_input(uint16_t input)
{
return crc5_usb_bits(input, 11, 0x02);
}
uint8_t crc5_usb_19bit_input(uint32_t input)
{
return crc5_usb_bits(input, 19, 0x1d);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/crc5.h | /** @file
* Declaration of CRC-5 routines and table
*
* 2019 Tomasz Mon <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CRC5_H__
#define __CRC5_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/** Compute the 5-bit CRC value of a input value matching the CRC-5
defined in USB 2.0 Specification. This function calculates the CRC
on low 11 bits of the input value. High bits are ignored.
@param input Source data for which the CRC-5 should be calculated.
@return the CRC5 checksum for input value */
WS_DLL_PUBLIC uint8_t crc5_usb_11bit_input(uint16_t input);
/** Compute the 5-bit CRC value of a input value matching the CRC-5
defined in USB 2.0 Specification. This function calculates the CRC
on low 19 bits of the input value. High bits are ignored.
@param input Source data for which the CRC-5 should be calculated.
@return the CRC5 checksum for input value */
WS_DLL_PUBLIC uint8_t crc5_usb_19bit_input(uint32_t input);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __CRC5_H__ */ |
C | wireshark/wsutil/crc6.c | /*
* crc6.c
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
/*
Patch by Ross Jacobs <[email protected]>:
Fixed CRC6 0x6F lookup table + function per Wireshark bug 14875
*/
#include "config.h"
#include "crc6.h"
/**
* Functions and types for CRC checks.
*
* Generated on Wed Jan 2 2019,
* by pycrc v0.9.1, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 6
* Poly = 0x6f
* XorIn = 0
* ReflectIn = False
* XorOut = 0
* ReflectOut = False
*/
static const uint8_t crc6_table[256] = {
0x00, 0x2f, 0x31, 0x1e, 0x0d, 0x22, 0x3c, 0x13, 0x1a, 0x35, 0x2b, 0x04, 0x17, 0x38, 0x26, 0x09,
0x34, 0x1b, 0x05, 0x2a, 0x39, 0x16, 0x08, 0x27, 0x2e, 0x01, 0x1f, 0x30, 0x23, 0x0c, 0x12, 0x3d,
0x07, 0x28, 0x36, 0x19, 0x0a, 0x25, 0x3b, 0x14, 0x1d, 0x32, 0x2c, 0x03, 0x10, 0x3f, 0x21, 0x0e,
0x33, 0x1c, 0x02, 0x2d, 0x3e, 0x11, 0x0f, 0x20, 0x29, 0x06, 0x18, 0x37, 0x24, 0x0b, 0x15, 0x3a,
0x0e, 0x21, 0x3f, 0x10, 0x03, 0x2c, 0x32, 0x1d, 0x14, 0x3b, 0x25, 0x0a, 0x19, 0x36, 0x28, 0x07,
0x3a, 0x15, 0x0b, 0x24, 0x37, 0x18, 0x06, 0x29, 0x20, 0x0f, 0x11, 0x3e, 0x2d, 0x02, 0x1c, 0x33,
0x09, 0x26, 0x38, 0x17, 0x04, 0x2b, 0x35, 0x1a, 0x13, 0x3c, 0x22, 0x0d, 0x1e, 0x31, 0x2f, 0x00,
0x3d, 0x12, 0x0c, 0x23, 0x30, 0x1f, 0x01, 0x2e, 0x27, 0x08, 0x16, 0x39, 0x2a, 0x05, 0x1b, 0x34,
0x1c, 0x33, 0x2d, 0x02, 0x11, 0x3e, 0x20, 0x0f, 0x06, 0x29, 0x37, 0x18, 0x0b, 0x24, 0x3a, 0x15,
0x28, 0x07, 0x19, 0x36, 0x25, 0x0a, 0x14, 0x3b, 0x32, 0x1d, 0x03, 0x2c, 0x3f, 0x10, 0x0e, 0x21,
0x1b, 0x34, 0x2a, 0x05, 0x16, 0x39, 0x27, 0x08, 0x01, 0x2e, 0x30, 0x1f, 0x0c, 0x23, 0x3d, 0x12,
0x2f, 0x00, 0x1e, 0x31, 0x22, 0x0d, 0x13, 0x3c, 0x35, 0x1a, 0x04, 0x2b, 0x38, 0x17, 0x09, 0x26,
0x12, 0x3d, 0x23, 0x0c, 0x1f, 0x30, 0x2e, 0x01, 0x08, 0x27, 0x39, 0x16, 0x05, 0x2a, 0x34, 0x1b,
0x26, 0x09, 0x17, 0x38, 0x2b, 0x04, 0x1a, 0x35, 0x3c, 0x13, 0x0d, 0x22, 0x31, 0x1e, 0x00, 0x2f,
0x15, 0x3a, 0x24, 0x0b, 0x18, 0x37, 0x29, 0x06, 0x0f, 0x20, 0x3e, 0x11, 0x02, 0x2d, 0x33, 0x1c,
0x21, 0x0e, 0x10, 0x3f, 0x2c, 0x03, 0x1d, 0x32, 0x3b, 0x14, 0x0a, 0x25, 0x36, 0x19, 0x07, 0x28
};
/**
* CRC6 is used by 3GPP (TS 25.415, TS 25.446) for header CRCs
* Poly: D^6 + D^5 + D^3 + D^2 + D^1 + 1
*
* TS 25.415 docs: https://www.etsi.org/deliver/etsi_ts/125400_125499/125415/04.06.00_60/ts_125415v040600p.pdf
* TS 25.446 docs: https://www.etsi.org/deliver/etsi_ts/125400_125499/125446/10.01.00_60/ts_125446v100100p.pdf
*/
uint16_t crc6_0X6F(uint16_t crc, const uint8_t *data, int data_len)
{
uint8_t tbl_idx;
while (data_len--) {
tbl_idx = (crc << 2) ^ *data;
crc = crc6_table[tbl_idx] & 0x3f;
data++;
}
return crc & 0x3f;
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/crc6.h | /** @file
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CRC6_H__
#define __CRC6_H__
#include <wireshark.h>
WS_DLL_PUBLIC uint16_t crc6_0X6F(uint16_t crc6, const uint8_t *data_blk_ptr, int data_blk_size);
#endif /* __CRC6_H__ */ |
C | wireshark/wsutil/crc7.c | /**
* crc7.c
*
* Functions and types for CRC checks.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
*
* Generated on Wed Jul 11 17:24:30 2012,
* by pycrc v0.7.10, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 7
* Poly = 0x45
* XorIn = 0x00
* ReflectIn = False
* XorOut = 0x00
* ReflectOut = False
* Algorithm = table-driven
*****************************************************************************/
#include "config.h"
#include "crc7.h" /* include the header file generated with pycrc */
/**
* Static table used for the table_driven implementation.
*****************************************************************************/
static const uint8_t crc_table[256] = {
0x00, 0x8a, 0x9e, 0x14, 0xb6, 0x3c, 0x28, 0xa2, 0xe6, 0x6c, 0x78, 0xf2, 0x50, 0xda, 0xce, 0x44,
0x46, 0xcc, 0xd8, 0x52, 0xf0, 0x7a, 0x6e, 0xe4, 0xa0, 0x2a, 0x3e, 0xb4, 0x16, 0x9c, 0x88, 0x02,
0x8c, 0x06, 0x12, 0x98, 0x3a, 0xb0, 0xa4, 0x2e, 0x6a, 0xe0, 0xf4, 0x7e, 0xdc, 0x56, 0x42, 0xc8,
0xca, 0x40, 0x54, 0xde, 0x7c, 0xf6, 0xe2, 0x68, 0x2c, 0xa6, 0xb2, 0x38, 0x9a, 0x10, 0x04, 0x8e,
0x92, 0x18, 0x0c, 0x86, 0x24, 0xae, 0xba, 0x30, 0x74, 0xfe, 0xea, 0x60, 0xc2, 0x48, 0x5c, 0xd6,
0xd4, 0x5e, 0x4a, 0xc0, 0x62, 0xe8, 0xfc, 0x76, 0x32, 0xb8, 0xac, 0x26, 0x84, 0x0e, 0x1a, 0x90,
0x1e, 0x94, 0x80, 0x0a, 0xa8, 0x22, 0x36, 0xbc, 0xf8, 0x72, 0x66, 0xec, 0x4e, 0xc4, 0xd0, 0x5a,
0x58, 0xd2, 0xc6, 0x4c, 0xee, 0x64, 0x70, 0xfa, 0xbe, 0x34, 0x20, 0xaa, 0x08, 0x82, 0x96, 0x1c,
0xae, 0x24, 0x30, 0xba, 0x18, 0x92, 0x86, 0x0c, 0x48, 0xc2, 0xd6, 0x5c, 0xfe, 0x74, 0x60, 0xea,
0xe8, 0x62, 0x76, 0xfc, 0x5e, 0xd4, 0xc0, 0x4a, 0x0e, 0x84, 0x90, 0x1a, 0xb8, 0x32, 0x26, 0xac,
0x22, 0xa8, 0xbc, 0x36, 0x94, 0x1e, 0x0a, 0x80, 0xc4, 0x4e, 0x5a, 0xd0, 0x72, 0xf8, 0xec, 0x66,
0x64, 0xee, 0xfa, 0x70, 0xd2, 0x58, 0x4c, 0xc6, 0x82, 0x08, 0x1c, 0x96, 0x34, 0xbe, 0xaa, 0x20,
0x3c, 0xb6, 0xa2, 0x28, 0x8a, 0x00, 0x14, 0x9e, 0xda, 0x50, 0x44, 0xce, 0x6c, 0xe6, 0xf2, 0x78,
0x7a, 0xf0, 0xe4, 0x6e, 0xcc, 0x46, 0x52, 0xd8, 0x9c, 0x16, 0x02, 0x88, 0x2a, 0xa0, 0xb4, 0x3e,
0xb0, 0x3a, 0x2e, 0xa4, 0x06, 0x8c, 0x98, 0x12, 0x56, 0xdc, 0xc8, 0x42, 0xe0, 0x6a, 0x7e, 0xf4,
0xf6, 0x7c, 0x68, 0xe2, 0x40, 0xca, 0xde, 0x54, 0x10, 0x9a, 0x8e, 0x04, 0xa6, 0x2c, 0x38, 0xb2
};
/**
* Update the crc value with new data.
*
* \param crc The current crc value.
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The updated crc value.
*****************************************************************************/
uint8_t crc7update(uint8_t crc, const unsigned char *data, int data_len)
{
unsigned int tbl_idx;
while (data_len--) {
tbl_idx = ((crc >> 0) ^ *data) & 0xff;
crc = (crc_table[tbl_idx] ^ (crc << (8 - 1))) & (0x7f << 1);
data++;
}
return crc & (0x7f << 1);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/crc7.h | /** @file
*
* Functions and types for CRC checks.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* Generated on Wed Jul 11 17:24:57 2012,
* by pycrc v0.7.10, http://www.tty1.net/pycrc/
* using the configuration:
* Width = 7
* Poly = 0x45
* XorIn = 0x00
* ReflectIn = False
* XorOut = 0x00
* ReflectOut = False
* Algorithm = table-driven
****************************************************************************
*/
#ifndef __CRC7__H__
#define __CRC7__H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif
/**
* The definition of the used algorithm.
*****************************************************************************/
#define CRC_ALGO_TABLE_DRIVEN 1
/**
* Calculate the initial crc value.
*
* \return The initial crc value.
*****************************************************************************/
static inline uint8_t crc7init(void)
{
return 0x00 << 1;
}
/**
* Update the crc value with new data.
*
* \param crc The current crc value.
* \param data Pointer to a buffer of \a data_len bytes.
* \param data_len Number of bytes in the \a data buffer.
* \return The updated crc value.
*****************************************************************************/
WS_DLL_PUBLIC uint8_t crc7update(uint8_t crc, const unsigned char *data, int data_len);
/**
* Calculate the final crc value.
*
* \param crc The current crc value.
* \return The final crc value.
*****************************************************************************/
static inline uint8_t crc7finalize(uint8_t crc)
{
return (crc >> 1) ^ 0x00;
}
#ifdef __cplusplus
} /* closing brace for extern "C" */
#endif
#endif /* __CRC7__H__ */ |
C | wireshark/wsutil/crc8.c | /* crc8.c
* Implementation CRC-8 declarations and routines
*
* 2011 Roland Knall <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <wsutil/crc8.h>
/* @brief Precompiled table for CRC8 values for the polynom 0x2F */
static const uint8_t crc8_precompiled_2F[256] =
{
0x00, 0x2F, 0x5E, 0x71, 0xBC, 0x93, 0xE2, 0xCD,
0x57, 0x78, 0x09, 0x26, 0xEB, 0xC4, 0xB5, 0x9A,
0xAE, 0x81, 0xF0, 0xDF, 0x12, 0x3D, 0x4C, 0x63,
0xF9, 0xD6, 0xA7, 0x88, 0x45, 0x6A, 0x1B, 0x34,
0x73, 0x5C, 0x2D, 0x02, 0xCF, 0xE0, 0x91, 0xBE,
0x24, 0x0B, 0x7A, 0x55, 0x98, 0xB7, 0xC6, 0xE9,
0xDD, 0xF2, 0x83, 0xAC, 0x61, 0x4E, 0x3F, 0x10,
0x8A, 0xA5, 0xD4, 0xFB, 0x36, 0x19, 0x68, 0x47,
0xE6, 0xC9, 0xB8, 0x97, 0x5A, 0x75, 0x04, 0x2B,
0xB1, 0x9E, 0xEF, 0xC0, 0x0D, 0x22, 0x53, 0x7C,
0x48, 0x67, 0x16, 0x39, 0xF4, 0xDB, 0xAA, 0x85,
0x1F, 0x30, 0x41, 0x6E, 0xA3, 0x8C, 0xFD, 0xD2,
0x95, 0xBA, 0xCB, 0xE4, 0x29, 0x06, 0x77, 0x58,
0xC2, 0xED, 0x9C, 0xB3, 0x7E, 0x51, 0x20, 0x0F,
0x3B, 0x14, 0x65, 0x4A, 0x87, 0xA8, 0xD9, 0xF6,
0x6C, 0x43, 0x32, 0x1D, 0xD0, 0xFF, 0x8E, 0xA1,
0xE3, 0xCC, 0xBD, 0x92, 0x5F, 0x70, 0x01, 0x2E,
0xB4, 0x9B, 0xEA, 0xC5, 0x08, 0x27, 0x56, 0x79,
0x4D, 0x62, 0x13, 0x3C, 0xF1, 0xDE, 0xAF, 0x80,
0x1A, 0x35, 0x44, 0x6B, 0xA6, 0x89, 0xF8, 0xD7,
0x90, 0xBF, 0xCE, 0xE1, 0x2C, 0x03, 0x72, 0x5D,
0xC7, 0xE8, 0x99, 0xB6, 0x7B, 0x54, 0x25, 0x0A,
0x3E, 0x11, 0x60, 0x4F, 0x82, 0xAD, 0xDC, 0xF3,
0x69, 0x46, 0x37, 0x18, 0xD5, 0xFA, 0x8B, 0xA4,
0x05, 0x2A, 0x5B, 0x74, 0xB9, 0x96, 0xE7, 0xC8,
0x52, 0x7D, 0x0C, 0x23, 0xEE, 0xC1, 0xB0, 0x9F,
0xAB, 0x84, 0xF5, 0xDA, 0x17, 0x38, 0x49, 0x66,
0xFC, 0xD3, 0xA2, 0x8D, 0x40, 0x6F, 0x1E, 0x31,
0x76, 0x59, 0x28, 0x07, 0xCA, 0xE5, 0x94, 0xBB,
0x21, 0x0E, 0x7F, 0x50, 0x9D, 0xB2, 0xC3, 0xEC,
0xD8, 0xF7, 0x86, 0xA9, 0x64, 0x4B, 0x3A, 0x15,
0x8F, 0xA0, 0xD1, 0xFE, 0x33, 0x1C, 0x6D, 0x42
};
/* @brief Precompiled table for CRC8 values for the polynom 0x37 */
static const unsigned char crc8_precompiled_37[256] =
{
0x00, 0x37, 0x6e, 0x59, 0xdc, 0xeb, 0xb2, 0x85,
0x8f, 0xb8, 0xe1, 0xd6, 0x53, 0x64, 0x3d, 0x0a,
0x29, 0x1e, 0x47, 0x70, 0xf5, 0xc2, 0x9b, 0xac,
0xa6, 0x91, 0xc8, 0xff, 0x7a, 0x4d, 0x14, 0x23,
0x52, 0x65, 0x3c, 0x0b, 0x8e, 0xb9, 0xe0, 0xd7,
0xdd, 0xea, 0xb3, 0x84, 0x01, 0x36, 0x6f, 0x58,
0x7b, 0x4c, 0x15, 0x22, 0xa7, 0x90, 0xc9, 0xfe,
0xf4, 0xc3, 0x9a, 0xad, 0x28, 0x1f, 0x46, 0x71,
0xa4, 0x93, 0xca, 0xfd, 0x78, 0x4f, 0x16, 0x21,
0x2b, 0x1c, 0x45, 0x72, 0xf7, 0xc0, 0x99, 0xae,
0x8d, 0xba, 0xe3, 0xd4, 0x51, 0x66, 0x3f, 0x08,
0x02, 0x35, 0x6c, 0x5b, 0xde, 0xe9, 0xb0, 0x87,
0xf6, 0xc1, 0x98, 0xaf, 0x2a, 0x1d, 0x44, 0x73,
0x79, 0x4e, 0x17, 0x20, 0xa5, 0x92, 0xcb, 0xfc,
0xdf, 0xe8, 0xb1, 0x86, 0x03, 0x34, 0x6d, 0x5a,
0x50, 0x67, 0x3e, 0x09, 0x8c, 0xbb, 0xe2, 0xd5,
0x7f, 0x48, 0x11, 0x26, 0xa3, 0x94, 0xcd, 0xfa,
0xf0, 0xc7, 0x9e, 0xa9, 0x2c, 0x1b, 0x42, 0x75,
0x56, 0x61, 0x38, 0x0f, 0x8a, 0xbd, 0xe4, 0xd3,
0xd9, 0xee, 0xb7, 0x80, 0x05, 0x32, 0x6b, 0x5c,
0x2d, 0x1a, 0x43, 0x74, 0xf1, 0xc6, 0x9f, 0xa8,
0xa2, 0x95, 0xcc, 0xfb, 0x7e, 0x49, 0x10, 0x27,
0x04, 0x33, 0x6a, 0x5d, 0xd8, 0xef, 0xb6, 0x81,
0x8b, 0xbc, 0xe5, 0xd2, 0x57, 0x60, 0x39, 0x0e,
0xdb, 0xec, 0xb5, 0x82, 0x07, 0x30, 0x69, 0x5e,
0x54, 0x63, 0x3a, 0x0d, 0x88, 0xbf, 0xe6, 0xd1,
0xf2, 0xc5, 0x9c, 0xab, 0x2e, 0x19, 0x40, 0x77,
0x7d, 0x4a, 0x13, 0x24, 0xa1, 0x96, 0xcf, 0xf8,
0x89, 0xbe, 0xe7, 0xd0, 0x55, 0x62, 0x3b, 0x0c,
0x06, 0x31, 0x68, 0x5f, 0xda, 0xed, 0xb4, 0x83,
0xa0, 0x97, 0xce, 0xf9, 0x7c, 0x4b, 0x12, 0x25,
0x2f, 0x18, 0x41, 0x76, 0xf3, 0xc4, 0x9d, 0xaa,
};
/* @brief Precompiled table for CRC8 values for the polynom 0x3B */
static const unsigned char crc8_precompiled_3b[256] =
{
0x00, 0x3b, 0x76, 0x4d, 0xec, 0xd7, 0x9a, 0xa1,
0xe3, 0xd8, 0x95, 0xae, 0x0f, 0x34, 0x79, 0x42,
0xfd, 0xc6, 0x8b, 0xb0, 0x11, 0x2a, 0x67, 0x5c,
0x1e, 0x25, 0x68, 0x53, 0xf2, 0xc9, 0x84, 0xbf,
0xc1, 0xfa, 0xb7, 0x8c, 0x2d, 0x16, 0x5b, 0x60,
0x22, 0x19, 0x54, 0x6f, 0xce, 0xf5, 0xb8, 0x83,
0x3c, 0x07, 0x4a, 0x71, 0xd0, 0xeb, 0xa6, 0x9d,
0xdf, 0xe4, 0xa9, 0x92, 0x33, 0x08, 0x45, 0x7e,
0xb9, 0x82, 0xcf, 0xf4, 0x55, 0x6e, 0x23, 0x18,
0x5a, 0x61, 0x2c, 0x17, 0xb6, 0x8d, 0xc0, 0xfb,
0x44, 0x7f, 0x32, 0x09, 0xa8, 0x93, 0xde, 0xe5,
0xa7, 0x9c, 0xd1, 0xea, 0x4b, 0x70, 0x3d, 0x06,
0x78, 0x43, 0x0e, 0x35, 0x94, 0xaf, 0xe2, 0xd9,
0x9b, 0xa0, 0xed, 0xd6, 0x77, 0x4c, 0x01, 0x3a,
0x85, 0xbe, 0xf3, 0xc8, 0x69, 0x52, 0x1f, 0x24,
0x66, 0x5d, 0x10, 0x2b, 0x8a, 0xb1, 0xfc, 0xc7,
0x49, 0x72, 0x3f, 0x04, 0xa5, 0x9e, 0xd3, 0xe8,
0xaa, 0x91, 0xdc, 0xe7, 0x46, 0x7d, 0x30, 0x0b,
0xb4, 0x8f, 0xc2, 0xf9, 0x58, 0x63, 0x2e, 0x15,
0x57, 0x6c, 0x21, 0x1a, 0xbb, 0x80, 0xcd, 0xf6,
0x88, 0xb3, 0xfe, 0xc5, 0x64, 0x5f, 0x12, 0x29,
0x6b, 0x50, 0x1d, 0x26, 0x87, 0xbc, 0xf1, 0xca,
0x75, 0x4e, 0x03, 0x38, 0x99, 0xa2, 0xef, 0xd4,
0x96, 0xad, 0xe0, 0xdb, 0x7a, 0x41, 0x0c, 0x37,
0xf0, 0xcb, 0x86, 0xbd, 0x1c, 0x27, 0x6a, 0x51,
0x13, 0x28, 0x65, 0x5e, 0xff, 0xc4, 0x89, 0xb2,
0x0d, 0x36, 0x7b, 0x40, 0xe1, 0xda, 0x97, 0xac,
0xee, 0xd5, 0x98, 0xa3, 0x02, 0x39, 0x74, 0x4f,
0x31, 0x0a, 0x47, 0x7c, 0xdd, 0xe6, 0xab, 0x90,
0xd2, 0xe9, 0xa4, 0x9f, 0x3e, 0x05, 0x48, 0x73,
0xcc, 0xf7, 0xba, 0x81, 0x20, 0x1b, 0x56, 0x6d,
0x2f, 0x14, 0x59, 0x62, 0xc3, 0xf8, 0xb5, 0x8e,
};
/** Calculates a CRC8 checksum for the given buffer with the polynom
* stored in the crc_table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @param crc_table a table storing 256 entries for crc8 checksums
* @return the CRC8 checksum for the buffer
*/
static uint8_t crc8_precompiled(const uint8_t *buf, uint32_t len, uint8_t seed, const uint8_t crc_table[])
{
uint8_t crc;
crc = seed;
while(len-- > 0)
crc = crc_table[(uint8_t)(*buf++) ^ crc];
return crc;
}
/** Calculates a CRC8 checksum for the given buffer with the polynom
* 0x2F using the precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC8 checksum for the buffer
*/
uint8_t crc8_0x2F(const uint8_t *buf, uint32_t len, uint8_t seed)
{
return crc8_precompiled(buf, len, seed, crc8_precompiled_2F);
}
/** Calculates a CRC8 checksum for the given buffer with the polynom
* 0x37 using the precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC8 checksum for the buffer
*/
uint8_t crc8_0x37(const uint8_t *buf, uint32_t len, uint8_t seed)
{
uint8_t crc = seed;
while (len-- > 0)
{
crc = crc8_precompiled_37[(crc ^ *buf++)];
}
return (crc);
}
/** Calculates a CRC8 checksum for the given buffer with the polynom
* 0x3B using the precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC8 checksum for the buffer
*/
uint8_t crc8_0x3B(const uint8_t *buf, uint32_t len, uint8_t seed)
{
uint8_t crc = seed;
while (len-- > 0)
{
crc = crc8_precompiled_3b[(crc ^ *buf++)];
}
return (crc);
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/crc8.h | /** @file
* Declaration of CRC-8 routine and tables
*
* 2011 Roland Knall <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CRC8_H__
#define __CRC8_H__
#include <wireshark.h>
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/** Calculates a CRC8 checksum for the given buffer with the polynom
* 0x2F using the precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC8 checksum for the buffer
*/
WS_DLL_PUBLIC uint8_t crc8_0x2F(const uint8_t *buf, uint32_t len, uint8_t seed);
/** Calculates a CRC8 checksum for the given buffer with the polynom
* 0x37 using the precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC8 checksum for the buffer
*/
WS_DLL_PUBLIC uint8_t crc8_0x37(const uint8_t *buf, uint32_t len, uint8_t seed);
/** Calculates a CRC8 checksum for the given buffer with the polynom
* 0x3B using the precompiled CRC table
* @param buf a pointer to a buffer of the given length
* @param len the length of the given buffer
* @param seed The seed to use.
* @return the CRC8 checksum for the buffer
*/
WS_DLL_PUBLIC uint8_t crc8_0x3B(const uint8_t *buf, uint32_t len, uint8_t seed);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* crc8.h */ |
C | wireshark/wsutil/curve25519.c | /* curve25519.c
* NaCl/Sodium-compatible API for Curve25519 cryptography.
*
* Copyright (c) 2018, Peter Wu <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "curve25519.h"
#include <gcrypt.h>
static inline void
copy_and_reverse(unsigned char *dest, const unsigned char *src, size_t n)
{
for (size_t i = 0; i < n; i++) {
dest[n - 1 - i] = src[i];
}
}
static int
x25519_mpi(unsigned char *q, const unsigned char *n, gcry_mpi_t mpi_p)
{
unsigned char priv_be[32];
unsigned char result_be[32];
size_t result_len = 0;
gcry_mpi_t mpi = NULL;
gcry_ctx_t ctx = NULL;
gcry_mpi_point_t P = NULL;
gcry_mpi_point_t Q = NULL;
int r = -1;
/* Default to infinity (all zeroes). */
memset(q, 0, 32);
/* Keys are in little-endian, but gcry_mpi_scan expects big endian. Convert
* keys and ensure that the result is a valid Curve25519 secret scalar. */
copy_and_reverse(priv_be, n, 32);
priv_be[0] &= 127;
priv_be[0] |= 64;
priv_be[31] &= 248;
gcry_mpi_scan(&mpi, GCRYMPI_FMT_USG, priv_be, 32, NULL);
if (gcry_mpi_ec_new(&ctx, NULL, "Curve25519")) {
/* Should not happen, possibly out-of-memory. */
goto leave;
}
/* Compute Q = nP */
Q = gcry_mpi_point_new(0);
P = gcry_mpi_point_set(NULL, mpi_p, NULL, GCRYMPI_CONST_ONE);
gcry_mpi_ec_mul(Q, mpi, P, ctx);
/* Note: mpi is reused to store the result. */
if (gcry_mpi_ec_get_affine(mpi, NULL, Q, ctx)) {
/* Infinity. */
goto leave;
}
if (gcry_mpi_print(GCRYMPI_FMT_USG, result_be, 32, &result_len, mpi)) {
/* Should not happen, possibly out-of-memory. */
goto leave;
}
copy_and_reverse(q, result_be, result_len);
r = 0;
leave:
gcry_mpi_point_release(P);
gcry_mpi_point_release(Q);
gcry_ctx_release(ctx);
gcry_mpi_release(mpi);
/* XXX erase priv_be and result_be */
return r;
}
int
crypto_scalarmult_curve25519(unsigned char *q, const unsigned char *n,
const unsigned char *p)
{
unsigned char p_be[32];
gcry_mpi_t mpi_p = NULL;
copy_and_reverse(p_be, p, 32);
/* Clear unused bit. */
p_be[0] &= 0x7f;
gcry_mpi_scan(&mpi_p, GCRYMPI_FMT_USG, p_be, 32, NULL);
int r = x25519_mpi(q, n, mpi_p);
gcry_mpi_release(mpi_p);
return r;
}
int
crypto_scalarmult_curve25519_base(unsigned char *q, const unsigned char *n)
{
gcry_mpi_t mpi_basepoint_x = gcry_mpi_set_ui(NULL, 9);
int r = x25519_mpi(q, n, mpi_basepoint_x);
gcry_mpi_release(mpi_basepoint_x);
return r;
} |
C/C++ | wireshark/wsutil/curve25519.h | /** @file
* NaCl/Sodium-compatible API for Curve25519 cryptography.
*
* Copyright (c) 2018, Peter Wu <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __CURVE25519_H__
#define __CURVE25519_H__
#include <wireshark.h>
/*
* Computes Q = X25519(n, P). In other words, given the secret key n, the public
* key P, compute the shared secret Q. Each key is 32 bytes long.
* Returns 0 on success or -1 on failure.
*/
WS_DLL_PUBLIC
int crypto_scalarmult_curve25519(unsigned char *q, const unsigned char *n,
const unsigned char *p);
/*
* Computes the Curve25519 32-byte public key Q from the 32-byte secret key n.
* Returns 0 on success or -1 on failure.
*/
WS_DLL_PUBLIC
int crypto_scalarmult_curve25519_base(unsigned char *q, const unsigned char *n);
#endif /* __CURVE25519_H__ */ |
C | wireshark/wsutil/dot11decrypt_wep.c | /* dot11decrypt_wep.c
*
* Copyright (c) 2002-2005 Sam Leffler, Errno Consulting
* Copyright (c) 2006 CACE Technologies, Davis (California)
* All rights reserved.
*
* SPDX-License-Identifier: BSD-3-Clause
*/
#include "config.h"
/************************************************************************/
/* File includes */
#include "crc32.h"
/************************************************************************/
/* Note: copied from net80211/ieee80211_airpdcap_tkip.c */
#define S_SWAP(a,b) { guint8 t = S[a]; S[a] = S[b]; S[b] = t; }
/* Note: copied from FreeBSD source code, RELENG 6, */
/* sys/net80211/ieee80211_crypto_wep.c, 391 */
int Dot11DecryptWepDecrypt(
const guchar *seed,
const size_t seed_len,
guchar *cypher_text,
const size_t data_len)
{
guint32 i, j, k, crc;
guint8 S[256];
guint8 icv[4];
size_t buflen;
/* Generate key stream (RC4 Pseudo-Random Number Generator) */
for (i = 0; i < 256; i++)
S[i] = (guint8)i;
for (j = i = 0; i < 256; i++) {
j = (j + S[i] + seed[i % seed_len]) & 0xff;
S_SWAP(i, j);
}
/* Apply RC4 to data and compute CRC32 over decrypted data */
crc = ~(guint32)0;
buflen = data_len;
for (i = j = k = 0; k < buflen; k++) {
i = (i + 1) & 0xff;
j = (j + S[i]) & 0xff;
S_SWAP(i, j);
*cypher_text ^= S[(S[i] + S[j]) & 0xff];
crc = crc32_ccitt_table_lookup((crc ^ *cypher_text) & 0xff) ^ (crc >> 8);
cypher_text++;
}
crc = ~crc;
/* Encrypt little-endian CRC32 and verify that it matches with the received ICV */
icv[0] = (guint8)crc;
icv[1] = (guint8)(crc >> 8);
icv[2] = (guint8)(crc >> 16);
icv[3] = (guint8)(crc >> 24);
for (k = 0; k < 4; k++) {
i = (i + 1) & 0xff;
j = (j + S[i]) & 0xff;
S_SWAP(i, j);
if ((icv[k] ^ S[(S[i] + S[j]) & 0xff]) != *cypher_text++) {
/* ICV mismatch - drop frame */
return 1/*DOT11DECRYPT_RET_UNSUCCESS*/;
}
}
return 0/*DOT11DECRYPT_RET_SUCCESS*/;
}
/*
* 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:
*/ |
C | wireshark/wsutil/eax.c | /* eax.c
* Encryption and decryption routines implementing the EAX' encryption mode
* Copyright 2010, Edward J. Beroset, [email protected]
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "eax.h"
#include <stdlib.h>
#include <string.h>
/* Use libgcrypt for cipher libraries. */
#include <gcrypt.h>
typedef struct {
guint8 L[EAX_SIZEOF_KEY];
guint8 D[EAX_SIZEOF_KEY];
guint8 Q[EAX_SIZEOF_KEY];
} eax_s;
static eax_s instance;
/* these are defined as macros so they'll be easy to redo in assembly if desired */
#define BLK_CPY(dst, src) { memcpy(dst, src, EAX_SIZEOF_KEY); }
#define BLK_XOR(dst, src) { int z; for (z=0; z < EAX_SIZEOF_KEY; z++) dst[z] ^= src[z]; }
static void Dbl(guint8 *out, const guint8 *in);
static void CTR(const guint8 *ws, guint8 *pK, guint8 *pN, guint16 SizeN);
static void CMAC(guint8 *pK, guint8 *ws, const guint8 *pN, guint16 SizeN);
static void dCMAC(guint8 *pK, guint8 *ws, const guint8 *pN, guint16 SizeN, const guint8 *pC, guint16 SizeC);
void AesEncrypt(unsigned char msg[EAX_SIZEOF_KEY], unsigned char key[EAX_SIZEOF_KEY]);
/*!
Decrypts cleartext data using EAX' mode (see ANSI Standard C12.22-2008).
@param[in] pN pointer to cleartext (canonified form)
@param[in] pK pointer to secret key
@param[in,out] pC pointer to ciphertext
@param[in] SizeN byte length of cleartext (pN) buffer
@param[in] SizeK byte length of secret key (pK)
@param[in] SizeC byte length of ciphertext (pC) buffer
@param[in] pMac four-byte Message Authentication Code
@param[in] Mode EAX_MODE_CLEARTEXT_AUTH or EAX_MODE_CIPHERTEXT_AUTH
@return TRUE if message has been authenticated; FALSE if not
authenticated, invalid Mode or error
*/
gboolean Eax_Decrypt(guint8 *pN, guint8 *pK, guint8 *pC,
guint32 SizeN, guint32 SizeK, guint32 SizeC, MAC_T *pMac,
guint8 Mode)
{
guint8 wsn[EAX_SIZEOF_KEY];
guint8 wsc[EAX_SIZEOF_KEY];
int i;
/* key size must match this implementation */
if (SizeK != EAX_SIZEOF_KEY)
return FALSE;
/* the key is new */
for (i = 0; i < EAX_SIZEOF_KEY; i++)
instance.L[i] = 0;
AesEncrypt(instance.L, pK);
Dbl(instance.D, instance.L);
Dbl(instance.Q, instance.D);
/* the key is set up */
/* first copy the nonce into our working space */
BLK_CPY(wsn, instance.D);
if (Mode == EAX_MODE_CLEARTEXT_AUTH) {
dCMAC(pK, wsn, pN, SizeN, pC, SizeC);
} else {
CMAC(pK, wsn, pN, SizeN);
}
/*
* In authentication mode the inputs are: pN, pK (and associated sizes),
* the result is the 4 byte MAC.
*/
if (Mode == EAX_MODE_CLEARTEXT_AUTH)
{
return (memcmp(pMac, &wsn[EAX_SIZEOF_KEY-sizeof(*pMac)], sizeof(*pMac)) ? FALSE : TRUE);
}
/*
* In cipher mode the inputs are: pN, pK, pP (and associated sizes),
* the results are pC (and its size) along with the 4 byte MAC.
*/
else if (Mode == EAX_MODE_CIPHERTEXT_AUTH)
{
if (SizeC == 0)
return (memcmp(pMac, &wsn[EAX_SIZEOF_KEY-sizeof(*pMac)], sizeof(*pMac)) ? FALSE : TRUE);
{
/* first copy the nonce into our working space */
BLK_CPY(wsc, instance.Q);
CMAC(pK, wsc, pC, SizeC);
BLK_XOR(wsc, wsn);
}
if (memcmp(pMac, &wsc[EAX_SIZEOF_KEY-sizeof(*pMac)], sizeof(*pMac)) == 0)
{
CTR(wsn, pK, pC, SizeC);
return TRUE;
}
}
return FALSE;
}
/* set up D or Q from L */
static void Dbl(guint8 *out, const guint8 *in)
{
int i;
guint8 carry = 0;
/* this might be a lot more efficient in assembly language */
for (i=0; i < EAX_SIZEOF_KEY; i++)
{
out[i] = ( in[i] << 1 ) | carry;
carry = (in[i] & 0x80) ? 1 : 0;
}
if (carry)
out[0] ^= 0x87;
}
static void CMAC(guint8 *pK, guint8 *ws, const guint8 *pN, guint16 SizeN)
{
dCMAC(pK, ws, pN, SizeN, NULL, 0);
}
static void dCMAC(guint8 *pK, guint8 *ws, const guint8 *pN, guint16 SizeN, const guint8 *pC, guint16 SizeC)
{
gcry_cipher_hd_t cipher_hd;
guint8 *work;
guint8 *ptr;
guint16 SizeT = SizeN + SizeC;
guint16 worksize = SizeT;
/* worksize must be an integral multiple of 16 */
if (SizeT & 0xf) {
worksize += 0x10 - (worksize & 0xf);
}
work = (guint8 *)g_malloc(worksize);
if (work == NULL) {
return;
}
memcpy(work, pN, SizeN);
if (pC != NULL) {
memcpy(&work[SizeN], pC, SizeC);
}
/*
* pad the data if necessary, and XOR Q or D, depending on
* whether data was padded or not
*/
if (worksize != SizeT) {
work[SizeT] = 0x80;
for (ptr = &work[SizeT+1]; ptr < &work[worksize]; ptr++)
*ptr = 0;
ptr= &work[worksize-0x10];
BLK_XOR(ptr, instance.Q);
} else {
ptr = &work[worksize-0x10];
BLK_XOR(ptr, instance.D);
}
/* open the cipher */
if (gcry_cipher_open(&cipher_hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CBC,0)){/* GCRY_CIPHER_CBC_MAC)) { */
g_free(work);
return;
}
if (gcry_cipher_setkey(cipher_hd, pK, EAX_SIZEOF_KEY)) {
g_free(work);
gcry_cipher_close(cipher_hd);
return;
}
if (gcry_cipher_setiv(cipher_hd, ws, EAX_SIZEOF_KEY)) {
g_free(work);
gcry_cipher_close(cipher_hd);
return;
}
if (gcry_cipher_encrypt(cipher_hd, work, worksize, work, worksize)) {
g_free(work);
gcry_cipher_close(cipher_hd);
return;
}
memcpy(ws, ptr, EAX_SIZEOF_KEY);
g_free(work);
gcry_cipher_close(cipher_hd);
return;
}
static void CTR(const guint8 *ws, guint8 *pK, guint8 *pN, guint16 SizeN)
{
gcry_cipher_hd_t cipher_hd;
guint8 ctr[EAX_SIZEOF_KEY];
BLK_CPY(ctr, ws);
ctr[12] &= 0x7f;
ctr[14] &= 0x7f;
/* open the cipher */
if (gcry_cipher_open(&cipher_hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_CTR, 0)) {
return;
}
if (gcry_cipher_setkey(cipher_hd, pK, EAX_SIZEOF_KEY)) {
gcry_cipher_close(cipher_hd);
return;
}
if (gcry_cipher_setctr(cipher_hd, ctr, EAX_SIZEOF_KEY)) {
gcry_cipher_close(cipher_hd);
return;
}
if (gcry_cipher_encrypt(cipher_hd, pN, SizeN, pN, SizeN)) {
gcry_cipher_close(cipher_hd);
return;
}
gcry_cipher_close(cipher_hd);
return;
}
void AesEncrypt(unsigned char msg[EAX_SIZEOF_KEY], unsigned char key[EAX_SIZEOF_KEY])
{
gcry_cipher_hd_t cipher_hd;
/* open the cipher */
if (gcry_cipher_open(&cipher_hd, GCRY_CIPHER_AES128, GCRY_CIPHER_MODE_ECB, 0)) {
return;
}
if (gcry_cipher_setkey(cipher_hd, key, EAX_SIZEOF_KEY)) {
gcry_cipher_close(cipher_hd);
return;
}
if (gcry_cipher_encrypt(cipher_hd, msg, EAX_SIZEOF_KEY, msg, EAX_SIZEOF_KEY)) {
gcry_cipher_close(cipher_hd);
return;
}
gcry_cipher_close(cipher_hd);
return;
}
/*
* Editor modelines - https://www.wireshark.org/tools/modelines.html
*
* Local variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* vi: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
C/C++ | wireshark/wsutil/eax.h | /** @file
* Encryption and decryption routines implementing the EAX' encryption mode
* Copyright 2010, Edward J. Beroset, [email protected]
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef _EAX_H
#define _EAX_H
#include <wireshark.h>
typedef struct tagMAC_T
{
guint8 Mac[4];
} MAC_T;
#define EAX_MODE_CLEARTEXT_AUTH 1
#define EAX_MODE_CIPHERTEXT_AUTH 2
#define EAX_SIZEOF_KEY 16
/*!
Decrypts cleartext data using EAX' mode (see ANSI Standard C12.22-2008).
@param[in] pN pointer to cleartext (canonified form)
@param[in] pK pointer to secret key
@param[in,out] pC pointer to ciphertext
@param[in] SizeN byte length of cleartext (pN) buffer
@param[in] SizeK byte length of secret key (pK)
@param[in] SizeC byte length of ciphertext (pC) buffer
@param[in] pMac four-byte Message Authentication Code
@param[in] Mode EAX_MODE_CLEARTEXT_AUTH or EAX_MODE_CIPHERTEXT_AUTH
@return TRUE if message has been authenticated; FALSE if not
authenticated, invalid Mode or error
*/
WS_DLL_PUBLIC
gboolean Eax_Decrypt(guint8 *pN, guint8 *pK, guint8 *pC,
guint32 SizeN, guint32 SizeK, guint32 SizeC, MAC_T *pMac,
guint8 Mode);
#endif |
C/C++ | wireshark/wsutil/epochs.h | /** @file
*
* Definitions of epoch values for various absolute time types.
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 2006 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __EPOCHS_H__
#define __EPOCHS_H__
#include <glib.h>
/*
* Deltas between the epochs for various non-UN*X time stamp formats and
* the January 1, 1970, 00:00:00 (proleptic?) UTC epoch for the UN*X time
* stamp format.
*/
/*
* 1900-01-01 00:00:00 (proleptic?) UTC.
* Used by a number of time formats.
*/
#define EPOCH_DELTA_1900_01_01_00_00_00_UTC 2208988800U
/*
* 1904-01-01 00:00:00 (proleptic?) UTC.
* Used in the classic Mac OS, and by formats, such as MPEG-4 Part 14 (MP4),
* which is based on Apple's QuickTime format.
*/
#define EPOCH_DELTA_1904_01_01_00_00_00_UTC 2082844800U
/*
* 1601-01-01 (proleptic Gregorian) 00:00:00 (proleptic?) UTC.
* The Windows NT epoch, used in a number of places, as it is
* the start of a 400 year Gregorian cycle.
*
* This is
*
* 369*365.25*24*60*60-(3*24*60*60+6*60*60)
*
* or equivalently,
*
* (89*4*365.25+(3*4+1)*365)*24*60*60
*
* 1970-1601 is 369; 365.25 is the average length of a year in days,
* including leap years.
*
* 369 = 4*92 + 1, so there are 92 groups of 4 consecutive years plus
* one leftover year, 1969, with 365 days.
*
* All but three of the groups of 4 consecutive years average 365.25 days
* per year, as they have one leap year in the group. However, 1700, 1800,
* and 1900 were not leap years, as, while they're all evenly divisible by 4,
* they're also evenly divisible by 100, but not evenly divisible by 400.
*
* So we have 89 groups of 4 consecutive years that average 365.25
* days per year, 3 groups of 4 consecutive years that average 365 days
* (as they lack a leap year), and one leftover year, 1969, that is
* 365 days long.
*/
#define EPOCH_DELTA_1601_01_01_00_00_00_UTC G_GUINT64_CONSTANT(11644473600)
#endif /* __EPOCHS_H__ */ |
C/C++ | wireshark/wsutil/exported_pdu_tlvs.h | /** @file
*
* Definitions for exported_pdu TLVs
* Copyright 2013, Anders Broman <[email protected]>
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef EXPORTED_PDU_TLVS_H
#define EXPORTED_PDU_TLVS_H
/**
* This is the format of the link-layer header of packets of type
* LINKTYPE_WIRESHARK_UPPER_PDU in pcap and pcapng files.
*
* It is a sequence of TLVs; at least one TLV MUST indicate what protocol is
* in the PDU following the TLVs.
*
* Each TLV contains, in order:
*
* a 2-byte big-endian type field;
* a 2-byte big-endian length field;
* a value, the length of which is indicated by the value of
* the length field (that value does not include the length
* of the type or length fields themselves).
*
* TLVs are not guaranteed to be aligned to any particular number
* of bytes.
*
* The list of TLVs may begin with a TLV of type EXP_PDU_TAG_OPTIONS_LENGTH;
* its value is a 4-byte integer value, giving the length of all TLVs
* following that TLV (i.e., the length does not include the length of
* the EXP_PDU_TAG_OPTIONS_LENGTH TLV). This tag is deprecated; it is
* not guaranteed to be present, and code reading packets should not
* require it to be present.
*
* The last TLV is of type EXP_PDU_TAG_END_OF_OPT; it has a length
* of 0, and the value is zero-length.
*
* For string values, a string may have zero, one, or more null bytes
* at the end; code that reads the string value must not assume that
* there are, or are not, null bytes at the end. Null bytes are included
* in the length field, but are not part of the string value.
*
* For integral values, the values are in big-endian format.
*/
/* Tag values
*
* Do NOT add new values to this list without asking
* wireshark-dev[AT]wireshark.org for a value. Otherwise, you run the risk of
* using a value that's already being used for some other purpose, and of
* having tools that read exported_pdu captures not being able to handle
* captures with your new tag value, with no hope that they will ever be
* changed to do so (as that would destroy their ability to read captures
* using that value for that other purpose).
*/
#define EXP_PDU_TAG_END_OF_OPT 0 /**< End-of-options Tag. */
/* 1 - 9 reserved */
#define EXP_PDU_TAG_OPTIONS_LENGTH 10 /**< Total length of the options excluding this TLV
* Deprecated - do not use
*/
#define EXP_PDU_TAG_LINKTYPE 11 /**< Deprecated - do not use */
#define EXP_PDU_TAG_DISSECTOR_NAME 12 /**< The value part should be an ASCII non NULL terminated string
* of the registered dissector used by Wireshark e.g "sip"
* Will be used to call the next dissector.
* NOTE: this is NOT a protocol name;
* a given protocol may have multiple
* dissectors, if, for example, the
* protocol headers depend on the
* protocol being used to transport
* the protocol in question.
*/
#define EXP_PDU_TAG_HEUR_DISSECTOR_NAME 13 /**< The value part should be an ASCII non NULL terminated string
* containing the heuristic dissector unique short name given
* during registration, e.g "sip_udp"
* Will be used to call the next dissector.
*/
#define EXP_PDU_TAG_DISSECTOR_TABLE_NAME 14 /**< The value part should be an ASCII non NULL terminated string
* containing the dissector table name given
* during registration, e.g "gsm_map.v3.arg.opcode"
* Will be used to call the next dissector.
*/
/* For backwards source compatibility */
#define EXP_PDU_TAG_PROTO_NAME EXP_PDU_TAG_DISSECTOR_NAME
#define EXP_PDU_TAG_HEUR_PROTO_NAME EXP_PDU_TAG_HEUR_DISSECTOR_NAME
/* Add protocol type related tags here.
* NOTE Only one protocol type tag may be present in a packet, the first one
* found will be used*/
/* 13 - 19 reserved */
#define EXP_PDU_TAG_IPV4_SRC 20 /**< IPv4 source address - 4 bytes */
#define EXP_PDU_TAG_IPV4_DST 21 /**< IPv4 destination address - 4 bytes */
#define EXP_PDU_TAG_IPV6_SRC 22 /**< IPv6 source address - 16 bytes */
#define EXP_PDU_TAG_IPV6_DST 23 /**< IPv6 destination address - 16 bytes */
/* Port type values for EXP_PDU_TAG_PORT_TYPE; these do not necessarily
* correspond to port type values inside libwireshark. */
#define EXP_PDU_PT_NONE 0
#define EXP_PDU_PT_SCTP 1
#define EXP_PDU_PT_TCP 2
#define EXP_PDU_PT_UDP 3
#define EXP_PDU_PT_DCCP 4
#define EXP_PDU_PT_IPX 5
#define EXP_PDU_PT_NCP 6
#define EXP_PDU_PT_EXCHG 7
#define EXP_PDU_PT_DDP 8
#define EXP_PDU_PT_SBCCS 9
#define EXP_PDU_PT_IDP 10
#define EXP_PDU_PT_TIPC 11
#define EXP_PDU_PT_USB 12
#define EXP_PDU_PT_I2C 13
#define EXP_PDU_PT_IBQP 14
#define EXP_PDU_PT_BLUETOOTH 15
#define EXP_PDU_PT_TDMOP 16
#define EXP_PDU_PT_IWARP_MPA 17
#define EXP_PDU_PT_MCTP 18
#define EXP_PDU_TAG_PORT_TYPE 24 /**< part type - 4 bytes, EXP_PDU_PT value */
#define EXP_PDU_TAG_SRC_PORT 25 /**< source port - 4 bytes (even for protocols with 2-byte ports) */
#define EXP_PDU_TAG_DST_PORT 26 /**< destination port - 4 bytes (even for protocols with 2-byte ports) */
#define EXP_PDU_TAG_SS7_OPC 28
#define EXP_PDU_TAG_SS7_DPC 29
#define EXP_PDU_TAG_ORIG_FNO 30
#define EXP_PDU_TAG_DVBCI_EVT 31
#define EXP_PDU_TAG_DISSECTOR_TABLE_NAME_NUM_VAL 32 /**< value part is the numeric value to be used calling the dissector table
* given with tag EXP_PDU_TAG_DISSECTOR_TABLE_NAME, must follow immediately after the table tag.
*/
#define EXP_PDU_TAG_COL_PROT_TEXT 33 /**< UTF-8 text string to put in COL_PROTOCOL, one use case is in conjunction with dissector tables where
* COL_PROTOCOL might not be filled in.
*/
/**< value part is structure passed into TCP subdissectors. The field
begins with a 2-byte version number; if the version number value is
1, the value part is in the form:
version 2 bytes - xport PDU version of structure (for backwards/forwards compatibility)
seq 4 bytes - Sequence number of first byte in the data
nxtseq 4 bytes - Sequence number of first byte after data
lastackseq 4 bytes - Sequence number of last ack
is_reassembled 1 byte - Non-zero if this is reassembled data
flags 2 bytes - TCP flags
urgent_pointer 2 bytes - Urgent pointer value for the current packet
All multi-byte values are in big-endian format. There is no alignment
padding between values, so seq. nxtseq, and lastackseq are not aligned
on 4-byte boundaries, andflags and urgent_pointer are not aligned on
2-byte boundaries.
*/
#define EXP_PDU_TAG_TCP_INFO_DATA 34
#define EXP_PDU_TAG_P2P_DIRECTION 35 /**< The packet direction (P2P_DIR_SENT, P2P_DIR_RECV). */
#define EXP_PDU_TAG_COL_INFO_TEXT 36 /**< UTF-8 text string to put in COL_INFO, useful when puting meta data into the packet list.
*/
#define EXP_PDU_TAG_IPV4_LEN 4
#define EXP_PDU_TAG_IPV6_LEN 16
#define EXP_PDU_TAG_PORT_TYPE_LEN 4
#define EXP_PDU_TAG_PORT_LEN 4
#define EXP_PDU_TAG_SS7_OPC_LEN 8 /* 4 bytes PC, 2 bytes standard type, 1 byte NI, 1 byte padding */
#define EXP_PDU_TAG_SS7_DPC_LEN 8 /* 4 bytes PC, 2 bytes standard type, 1 byte NI, 1 byte padding */
#define EXP_PDU_TAG_ORIG_FNO_LEN 4
#define EXP_PDU_TAG_DVBCI_EVT_LEN 1
#define EXP_PDU_TAG_DISSECTOR_TABLE_NUM_VAL_LEN 4
#endif /* EXPORTED_PDU_TLVS_H */ |
C | wireshark/wsutil/feature_list.c | /* feature_list.c
* Routines for gathering and handling lists of present/absent features
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include <wsutil/feature_list.h>
void
with_feature(feature_list l, const char *fmt, ...)
{
va_list arg;
GString *msg = g_string_new("+");
va_start(arg, fmt);
g_string_append_vprintf(msg, fmt, arg);
va_end(arg);
*l = g_list_prepend(*l, g_string_free(msg, FALSE));
}
void
without_feature(feature_list l, const char *fmt, ...)
{
va_list arg;
GString *msg = g_string_new("-");
va_start(arg, fmt);
g_string_append_vprintf(msg, fmt, arg);
va_end(arg);
*l = g_list_prepend(*l, g_string_free(msg, FALSE));
}
static gint
feature_sort_alpha(gconstpointer a, gconstpointer b)
{
return g_ascii_strcasecmp((gchar *)a + 1, (gchar *)b + 1);
}
void
sort_features(feature_list l)
{
*l = g_list_sort(*l, feature_sort_alpha);
}
void
free_features(feature_list l)
{
g_list_free_full(*l, g_free);
*l = NULL;
} |
C/C++ | wireshark/wsutil/feature_list.h | /** @file
* Declarations of routines for gathering and handling lists of
* present/absent features (usually actually dependencies)
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#ifndef __WSUTIL_FEATURE_LIST_H__
#define __WSUTIL_FEATURE_LIST_H__
#include <glib.h>
#include "ws_symbol_export.h"
#ifdef __cplusplus
extern "C" {
#endif /* __cplusplus */
/*
* Handle to a list of features/dependencies.
* Semi-opaque. Functions which gather the list of features
* will be passed one of these to use with
* `with_feature()`/`without_feature()` (below).
*/
typedef GList **feature_list;
/*
* The format of entries in a feature_list is a gchar* starting with a
* '+' or '-' character indicating if the feature is respectively
* present or absent, followed by the unchanged feature description.
* This allows the insert order of features to be preserved,
* while still preserving the present/absent status in a simple way.
*/
/*
* Pointer to a function which gathers a list of features.
*/
typedef void(*gather_feature_func)(feature_list l);
/*
* Add an indicator to the given feature_list that the named
* feature is present.
*/
WS_DLL_PUBLIC
void with_feature(feature_list l, const char *fmt, ...) G_GNUC_PRINTF(2,3);
/*
* Add an indicator to the given feature_list that the named
* feature is absent.
*/
WS_DLL_PUBLIC
void without_feature(feature_list l, const char *fmt, ...) G_GNUC_PRINTF(2,3);
/*
* Sort the given feature list, alphabetically by feature name.
* (The leading '+' or '-' is not factored into the sort.)
* Currently unused.
*/
WS_DLL_PUBLIC
void sort_features(feature_list l);
/*
* Free the memory used by the feature list,
* and reset its pointer to NULL.
*/
WS_DLL_PUBLIC
void free_features(feature_list l);
#ifdef __cplusplus
}
#endif /* __cplusplus */
#endif /* __WSUTIL_FEATURE_LIST_H__ */ |
C | wireshark/wsutil/filesystem.c | /* filesystem.c
* Filesystem utility routines
*
* Wireshark - Network traffic analyzer
* By Gerald Combs <[email protected]>
* Copyright 1998 Gerald Combs
*
* SPDX-License-Identifier: GPL-2.0-or-later
*/
#include "config.h"
#include "filesystem.h"
#define WS_LOG_DOMAIN LOG_DOMAIN_WSUTIL
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#ifdef _WIN32
#include <windows.h>
#include <tchar.h>
#include <shlobj.h>
#include <wsutil/unicode-utils.h>
#else /* _WIN32 */
#ifdef ENABLE_APPLICATION_BUNDLE
#include <mach-o/dyld.h>
#endif
#ifdef __linux__
#include <sys/utsname.h>
#endif
#ifdef __FreeBSD__
#include <sys/types.h>
#include <sys/sysctl.h>
#endif
#ifdef HAVE_DLGET
#include <dlfcn.h>
#endif
#include <pwd.h>
#endif /* _WIN32 */
#include <wsutil/report_message.h>
#include <wsutil/privileges.h>
#include <wsutil/file_util.h>
#include <wsutil/utf8_entities.h>
#include <wiretap/wtap.h> /* for WTAP_ERR_SHORT_WRITE */
#include "path_config.h"
#define PROFILES_DIR "profiles"
#define PLUGINS_DIR_NAME "plugins"
#define EXTCAP_DIR_NAME "extcap"
#define PROFILES_INFO_NAME "profile_files.txt"
#define _S G_DIR_SEPARATOR_S
/*
* Application configuration namespace. Used to construct configuration
* paths and environment variables.
* XXX We might want to use the term "application flavor" instead, with
* "packet" and "log" flavors.
*/
enum configuration_namespace_e {
CONFIGURATION_NAMESPACE_UNINITIALIZED,
CONFIGURATION_NAMESPACE_WIRESHARK,
CONFIGURATION_NAMESPACE_LOGRAY
};
enum configuration_namespace_e configuration_namespace = CONFIGURATION_NAMESPACE_UNINITIALIZED;
#define CONFIGURATION_NAMESPACE_PROPER (configuration_namespace == CONFIGURATION_NAMESPACE_WIRESHARK ? "Wireshark" : "Logray")
#define CONFIGURATION_NAMESPACE_LOWER (configuration_namespace == CONFIGURATION_NAMESPACE_WIRESHARK ? "wireshark" : "logray")
#define CONFIGURATION_ENVIRONMENT_VARIABLE(suffix) (configuration_namespace == CONFIGURATION_NAMESPACE_WIRESHARK ? "WIRESHARK_" suffix : "LOGRAY_" suffix)
char *persconffile_dir = NULL;
char *datafile_dir = NULL;
char *persdatafile_dir = NULL;
char *persconfprofile = NULL;
char *doc_dir = NULL;
/* Directory from which the executable came. */
static char *progfile_dir = NULL;
static char *install_prefix = NULL;
static gboolean do_store_persconffiles = FALSE;
static GHashTable *profile_files = NULL;
/*
* Given a pathname, return a pointer to the last pathname separator
* character in the pathname, or NULL if the pathname contains no
* separators.
*/
char *
find_last_pathname_separator(const char *path)
{
char *separator;
#ifdef _WIN32
char c;
/*
* We have to scan for '\' or '/'.
* Get to the end of the string.
*/
separator = strchr(path, '\0'); /* points to ending '\0' */
while (separator > path) {
c = *--separator;
if (c == '\\' || c == '/')
return separator; /* found it */
}
/*
* OK, we didn't find any, so no directories - but there might
* be a drive letter....
*/
return strchr(path, ':');
#else
separator = strrchr(path, '/');
return separator;
#endif
}
/*
* Given a pathname, return the last component.
*/
const char *
get_basename(const char *path)
{
const char *filename;
ws_assert(path != NULL);
filename = find_last_pathname_separator(path);
if (filename == NULL) {
/*
* There're no directories, drive letters, etc. in the
* name; the pathname *is* the file name.
*/
filename = path;
} else {
/*
* Skip past the pathname or drive letter separator.
*/
filename++;
}
return filename;
}
/*
* Given a pathname, return a string containing everything but the
* last component. NOTE: this overwrites the pathname handed into
* it....
*/
char *
get_dirname(char *path)
{
char *separator;
ws_assert(path != NULL);
separator = find_last_pathname_separator(path);
if (separator == NULL) {
/*
* There're no directories, drive letters, etc. in the
* name; there is no directory path to return.
*/
return NULL;
}
/*
* Get rid of the last pathname separator and the final file
* name following it.
*/
*separator = '\0';
/*
* "path" now contains the pathname of the directory containing
* the file/directory to which it referred.
*/
return path;
}
/*
* Given a pathname, return:
*
* the errno, if an attempt to "stat()" the file fails;
*
* EISDIR, if the attempt succeeded and the file turned out
* to be a directory;
*
* 0, if the attempt succeeded and the file turned out not
* to be a directory.
*/
int
test_for_directory(const char *path)
{
ws_statb64 statb;
if (ws_stat64(path, &statb) < 0)
return errno;
if (S_ISDIR(statb.st_mode))
return EISDIR;
else
return 0;
}
int
test_for_fifo(const char *path)
{
ws_statb64 statb;
if (ws_stat64(path, &statb) < 0)
return errno;
if (S_ISFIFO(statb.st_mode))
return ESPIPE;
else
return 0;
}
#ifdef ENABLE_APPLICATION_BUNDLE
/*
* Directory of the application bundle in which we're contained,
* if we're contained in an application bundle. Otherwise, NULL.
*
* Note: Table 2-5 "Subdirectories of the Contents directory" of
*
* https://developer.apple.com/library/mac/documentation/CoreFoundation/Conceptual/CFBundles/BundleTypes/BundleTypes.html#//apple_ref/doc/uid/10000123i-CH101-SW1
*
* says that the "Frameworks" directory
*
* Contains any private shared libraries and frameworks used by the
* executable. The frameworks in this directory are revision-locked
* to the application and cannot be superseded by any other, even
* newer, versions that may be available to the operating system. In
* other words, the frameworks included in this directory take precedence
* over any other similarly named frameworks found in other parts of
* the operating system. For information on how to add private
* frameworks to your application bundle, see Framework Programming Guide.
*
* so if we were to ship with any frameworks (e.g. Qt) we should
* perhaps put them in a Frameworks directory rather than under
* Resources.
*
* It also says that the "PlugIns" directory
*
* Contains loadable bundles that extend the basic features of your
* application. You use this directory to include code modules that
* must be loaded into your applicationbs process space in order to
* be used. You would not use this directory to store standalone
* executables.
*
* Our plugins are just raw .so/.dylib files; I don't know whether by
* "bundles" they mean application bundles (i.e., directory hierarchies)
* or just "bundles" in the Mach-O sense (which are an image type that
* can be loaded with dlopen() but not linked as libraries; our plugins
* are, I think, built as dylibs and can be loaded either way).
*
* And it says that the "SharedSupport" directory
*
* Contains additional non-critical resources that do not impact the
* ability of the application to run. You might use this directory to
* include things like document templates, clip art, and tutorials
* that your application expects to be present but that do not affect
* the ability of your application to run.
*
* I don't think I'd put the files that currently go under Resources/share
* into that category; they're not, for example, sample Lua scripts that
* don't actually get run by Wireshark, they're configuration/data files
* for Wireshark whose absence might not prevent Wireshark from running
* but that would affect how it behaves when run.
*/
static char *appbundle_dir;
#endif
/*
* TRUE if we're running from the build directory and we aren't running
* with special privileges.
*/
static gboolean running_in_build_directory_flag = FALSE;
/*
* Set our configuration namespace. This will be used for top-level
* configuration directory names and environment variable prefixes.
*/
static void
set_configuration_namespace(const char *namespace_name)
{
if (configuration_namespace != CONFIGURATION_NAMESPACE_UNINITIALIZED) {
return;
}
if (!namespace_name || g_ascii_strcasecmp(namespace_name, "wireshark") == 0)
{
configuration_namespace = CONFIGURATION_NAMESPACE_WIRESHARK;
}
else if (g_ascii_strcasecmp(namespace_name, "logray") == 0)
{
configuration_namespace = CONFIGURATION_NAMESPACE_LOGRAY;
}
else
{
ws_error("Unknown configuration namespace %s", namespace_name);
}
ws_debug("Using configuration namespace %s.", CONFIGURATION_NAMESPACE_PROPER);
}
const char *
get_configuration_namespace(void)
{
return CONFIGURATION_NAMESPACE_PROPER;
}
bool is_packet_configuration_namespace(void)
{
return configuration_namespace != CONFIGURATION_NAMESPACE_LOGRAY;
}
#ifndef _WIN32
/*
* Get the pathname of the executable using various platform-
* dependent mechanisms for various UN*Xes.
*
* These calls all should return something independent of the argv[0]
* passed to the program, so it shouldn't be fooled by an argv[0]
* that doesn't match the executable path.
*
* We don't use dladdr() because:
*
* not all UN*Xes necessarily have dladdr();
*
* those that do have it don't necessarily have dladdr(main)
* return information about the executable image;
*
* those that do have a dladdr() where dladdr(main) returns
* information about the executable image don't necessarily
* have a mechanism by which the executable image can get
* its own path from the kernel (either by a call or by it
* being handed to it along with argv[] and the environment),
* so they just fall back on getting it from argv[0], which we
* already have code to do;
*
* those that do have such a mechanism don't necessarily use
* it in dladdr(), and, instead, just fall back on getting it
* from argv[0];
*
* so the only places where it's worth bothering to use dladdr()
* are platforms where dladdr(main) return information about the
* executable image by getting it from the kernel rather than
* by looking at argv[0], and where we can't get at that information
* ourselves, and we haven't seen any indication that there are any
* such platforms.
*
* In particular, some dynamic linkers supply a dladdr() such that
* dladdr(main) just returns something derived from argv[0], so
* just using dladdr(main) is the wrong thing to do if there's
* another mechanism that can get you a more reliable version of
* the executable path.
*
* So, on platforms where we know of a mechanism to get that path
* (where getting that path doesn't involve argv[0], which is not
* guaranteed to reflect the path to the binary), this routine
* attempsts to use that platform's mechanism. On other platforms,
* it just returns NULL.
*
* This is not guaranteed to return an absolute path; if it doesn't,
* our caller must prepend the current directory if it's a path.
*
* This is not guaranteed to return the "real path"; it might return
* something with symbolic links in the path. Our caller must
* use realpath() if they want the real thing, but that's also true of
* something obtained by looking at argv[0].
*/
#define xx_free free /* hack so checkAPIs doesn't complain */
static const char *
get_current_executable_path(void)
{
#if defined(ENABLE_APPLICATION_BUNDLE)
static char *executable_path;
uint32_t path_buf_size;
if (executable_path) {
return executable_path;
}
path_buf_size = PATH_MAX;
executable_path = (char *)g_malloc(path_buf_size);
if (_NSGetExecutablePath(executable_path, &path_buf_size) == -1) {
executable_path = (char *)g_realloc(executable_path, path_buf_size);
if (_NSGetExecutablePath(executable_path, &path_buf_size) == -1)
return NULL;
}
/*
* Resolve our path so that it's possible to symlink the executables
* in our application bundle.
*/
char *rp_execpath = realpath(executable_path, NULL);
if (rp_execpath) {
g_free(executable_path);
executable_path = g_strdup(rp_execpath);
xx_free(rp_execpath);
}
return executable_path;
#elif defined(__linux__)
/*
* In older versions of GNU libc's dynamic linker, as used on Linux,
* dladdr(main) supplies a path based on argv[0], so we use
* /proc/self/exe instead; there are Linux distributions with
* kernels that support /proc/self/exe and those older versions
* of the dynamic linker, and this will get a better answer on
* those versions.
*
* It only works on Linux 2.2 or later, so we just give up on
* earlier versions.
*
* XXX - are there OS versions that support "exe" but not "self"?
*/
struct utsname name;
static char executable_path[PATH_MAX + 1];
ssize_t r;
if (uname(&name) == -1)
return NULL;
if (strncmp(name.release, "1.", 2) == 0)
return NULL; /* Linux 1.x */
if (strcmp(name.release, "2.0") == 0 ||
strncmp(name.release, "2.0.", 4) == 0 ||
strcmp(name.release, "2.1") == 0 ||
strncmp(name.release, "2.1.", 4) == 0)
return NULL; /* Linux 2.0.x or 2.1.x */
if ((r = readlink("/proc/self/exe", executable_path, PATH_MAX)) == -1)
return NULL;
executable_path[r] = '\0';
return executable_path;
#elif defined(__FreeBSD__) && defined(KERN_PROC_PATHNAME)
/*
* In older versions of FreeBSD's dynamic linker, dladdr(main)
* supplies a path based on argv[0], so we use the KERN_PROC_PATHNAME
* sysctl instead; there are, I think, versions of FreeBSD
* that support the sysctl that have and those older versions
* of the dynamic linker, and this will get a better answer on
* those versions.
*/
int mib[4];
char *executable_path;
size_t path_buf_size;
mib[0] = CTL_KERN;
mib[1] = KERN_PROC;
mib[2] = KERN_PROC_PATHNAME;
mib[3] = -1;
path_buf_size = PATH_MAX;
executable_path = (char *)g_malloc(path_buf_size);
if (sysctl(mib, 4, executable_path, &path_buf_size, NULL, 0) == -1) {
if (errno != ENOMEM)
return NULL;
executable_path = (char *)g_realloc(executable_path, path_buf_size);
if (sysctl(mib, 4, executable_path, &path_buf_size, NULL, 0) == -1)
return NULL;
}
return executable_path;
#elif defined(__NetBSD__)
/*
* In all versions of NetBSD's dynamic linker as of 2013-08-12,
* dladdr(main) supplies a path based on argv[0], so we use
* /proc/curproc/exe instead.
*
* XXX - are there OS versions that support "exe" but not "curproc"
* or "self"? Are there any that support "self" but not "curproc"?
*/
static char executable_path[PATH_MAX + 1];
ssize_t r;
if ((r = readlink("/proc/curproc/exe", executable_path, PATH_MAX)) == -1)
return NULL;
executable_path[r] = '\0';
return executable_path;
#elif defined(__DragonFly__)
/*
* In older versions of DragonFly BSD's dynamic linker, dladdr(main)
* supplies a path based on argv[0], so we use /proc/curproc/file
* instead; it appears to be supported by all versions of DragonFly
* BSD.
*/
static char executable_path[PATH_MAX + 1];
ssize_t r;
if ((r = readlink("/proc/curproc/file", executable_path, PATH_MAX)) == -1)
return NULL;
executable_path[r] = '\0';
return executable_path;
#elif defined(HAVE_GETEXECNAME)
/*
* Solaris, with getexecname().
* It appears that getexecname() dates back to at least Solaris 8,
* but /proc/{pid}/path is first documented in the Solaris 10 documentation,
* so we use getexecname() if available, rather than /proc/self/path/a.out
* (which isn't documented, but appears to be a symlink to the
* executable image file).
*/
return getexecname();
#elif defined(HAVE_DLGET)
/*
* HP-UX 11, with dlget(); use dlget() and dlgetname().
* See
*
* https://web.archive.org/web/20081025174755/http://h21007.www2.hp.com/portal/site/dspp/menuitem.863c3e4cbcdc3f3515b49c108973a801?ciid=88086d6e1de021106d6e1de02110275d6e10RCRD#two
*/
struct load_module_desc desc;
if (dlget(-2, &desc, sizeof(desc)) != NULL)
return dlgetname(&desc, sizeof(desc), NULL, NULL, NULL);
else
return NULL;
#else
/* Fill in your favorite UN*X's code here, if there is something */
return NULL;
#endif
}
#endif /* _WIN32 */
static void trim_progfile_dir(void)
{
char *progfile_last_dir = find_last_pathname_separator(progfile_dir);
if (! (progfile_last_dir && strncmp(progfile_last_dir + 1, "extcap", sizeof("extcap")) == 0)) {
return;
}
*progfile_last_dir = '\0';
char *extcap_progfile_dir = progfile_dir;
progfile_dir = g_strdup(extcap_progfile_dir);
g_free(extcap_progfile_dir);
}
#if !defined(_WIN32) || defined(HAVE_MSYSTEM)
static char *
trim_last_dir_from_path(const char *_path)
{
char *path = ws_strdup(_path);
char *last_dir = find_last_pathname_separator(path);
if (last_dir) {
*last_dir = '\0';
}
return path;
}
#endif
/*
* Construct the path name of a non-extcap Wireshark executable file,
* given the program name. The executable name doesn't include ".exe";
* append it on Windows, so that callers don't have to worry about that.
*
* This presumes that all non-extcap executables are in the same directory.
*
* The returned file name was g_malloc()'d so it must be g_free()d when the
* caller is done with it.
*/
char *
get_executable_path(const char *program_name)
{
/*
* Fail if we don't know what directory contains the executables.
*/
if (progfile_dir == NULL)
return NULL;
#ifdef _WIN32
return ws_strdup_printf("%s\\%s.exe", progfile_dir, program_name);
#else
return ws_strdup_printf("%s/%s", progfile_dir, program_name);
#endif
}
/*
* Get the pathname of the directory from which the executable came,
* and save it for future use. Returns NULL on success, and a
* g_mallocated string containing an error on failure.
*/
#ifdef _WIN32
char *
configuration_init_w32(const char* arg0 _U_)
{
TCHAR prog_pathname_w[_MAX_PATH+2];
char *prog_pathname;
DWORD error;
TCHAR *msg_w;
guchar *msg;
size_t msglen;
/*
* Attempt to get the full pathname of the currently running
* program.
*/
if (GetModuleFileName(NULL, prog_pathname_w, G_N_ELEMENTS(prog_pathname_w)) != 0 && GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
/*
* XXX - Should we use g_utf16_to_utf8()?
*/
prog_pathname = utf_16to8(prog_pathname_w);
/*
* We got it; strip off the last component, which would be
* the file name of the executable, giving us the pathname
* of the directory where the executable resides.
*/
progfile_dir = g_path_get_dirname(prog_pathname);
if (progfile_dir != NULL) {
trim_progfile_dir();
/* we succeeded */
} else {
/*
* OK, no. What do we do now?
*/
return ws_strdup_printf("No \\ in executable pathname \"%s\"",
prog_pathname);
}
} else {
/*
* Oh, well. Return an indication of the error.
*/
error = GetLastError();
if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM|FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error, 0, (LPTSTR) &msg_w, 0, NULL) == 0) {
/*
* Gak. We can't format the message.
*/
return ws_strdup_printf("GetModuleFileName failed: %lu (FormatMessage failed: %lu)",
error, GetLastError());
}
msg = utf_16to8(msg_w);
LocalFree(msg_w);
/*
* "FormatMessage()" "helpfully" sticks CR/LF at the
* end of the message. Get rid of it.
*/
msglen = strlen(msg);
if (msglen >= 2) {
msg[msglen - 1] = '\0';
msg[msglen - 2] = '\0';
}
return ws_strdup_printf("GetModuleFileName failed: %s (%lu)",
msg, error);
}
#ifdef HAVE_MSYSTEM
/*
* We already have the program_dir. Find the installation prefix.
* This is one level up from the bin_dir. If the program_dir does
* not end with "bin" then assume we are running in the build directory
* and the "installation prefix" (staging directory) is the same as
* the program_dir.
*/
if (g_str_has_suffix(progfile_dir, _S"bin")) {
install_prefix = trim_last_dir_from_path(progfile_dir);
}
else {
install_prefix = g_strdup(progfile_dir);
running_in_build_directory_flag = TRUE;
}
#endif /* HAVE_MSYSTEM */
return NULL;
}
#else /* !_WIN32 */
char *
configuration_init_posix(const char* arg0)
{
const char *execname;
char *prog_pathname;
char *curdir;
long path_max;
const char *pathstr;
const char *path_start, *path_end;
size_t path_component_len, path_len;
char *retstr;
char *path;
char *dir_end;
/* Hard-coded value used if we cannot obtain the path of the running executable. */
install_prefix = g_strdup(INSTALL_PREFIX);
/*
* Check whether XXX_RUN_FROM_BUILD_DIRECTORY is set in the
* environment; if so, set running_in_build_directory_flag if we
* weren't started with special privileges. (If we were started
* with special privileges, it's not safe to allow the user to point
* us to some other directory; running_in_build_directory_flag, when
* set, causes us to look for plugins and the like in the build
* directory.)
*/
const char *run_from_envar = CONFIGURATION_ENVIRONMENT_VARIABLE("RUN_FROM_BUILD_DIRECTORY");
if (g_getenv(run_from_envar) != NULL
&& !started_with_special_privs()) {
running_in_build_directory_flag = TRUE;
}
execname = get_current_executable_path();
if (execname == NULL) {
/*
* OK, guess based on argv[0].
*/
execname = arg0;
}
/*
* Try to figure out the directory in which the currently running
* program resides, given something purporting to be the executable
* name (from an OS mechanism or from the argv[0] it was started with).
* That might be the absolute path of the program, or a path relative
* to the current directory of the process that started it, or
* just a name for the program if it was started from the command
* line and was searched for in $PATH. It's not guaranteed to be
* any of those, however, so there are no guarantees....
*/
if (execname[0] == '/') {
/*
* It's an absolute path.
*/
prog_pathname = g_strdup(execname);
} else if (strchr(execname, '/') != NULL) {
/*
* It's a relative path, with a directory in it.
* Get the current directory, and combine it
* with that directory.
*/
path_max = pathconf(".", _PC_PATH_MAX);
if (path_max == -1) {
/*
* We have no idea how big a buffer to
* allocate for the current directory.
*/
return ws_strdup_printf("pathconf failed: %s\n",
g_strerror(errno));
}
curdir = (char *)g_malloc(path_max);
if (getcwd(curdir, path_max) == NULL) {
/*
* It failed - give up, and just stick
* with DATA_DIR.
*/
g_free(curdir);
return ws_strdup_printf("getcwd failed: %s\n",
g_strerror(errno));
}
path = ws_strdup_printf("%s/%s", curdir, execname);
g_free(curdir);
prog_pathname = path;
} else {
/*
* It's just a file name.
* Search the path for a file with that name
* that's executable.
*/
prog_pathname = NULL; /* haven't found it yet */
pathstr = g_getenv("PATH");
path_start = pathstr;
if (path_start != NULL) {
while (*path_start != '\0') {
path_end = strchr(path_start, ':');
if (path_end == NULL)
path_end = path_start + strlen(path_start);
path_component_len = path_end - path_start;
path_len = path_component_len + 1
+ strlen(execname) + 1;
path = (char *)g_malloc(path_len);
memcpy(path, path_start, path_component_len);
path[path_component_len] = '\0';
(void) g_strlcat(path, "/", path_len);
(void) g_strlcat(path, execname, path_len);
if (access(path, X_OK) == 0) {
/*
* Found it!
*/
prog_pathname = path;
break;
}
/*
* That's not it. If there are more
* path components to test, try them.
*/
if (*path_end == ':')
path_end++;
path_start = path_end;
g_free(path);
}
if (prog_pathname == NULL) {
/*
* Program not found in path.
*/
return ws_strdup_printf("\"%s\" not found in \"%s\"",
execname, pathstr);
}
} else {
/*
* PATH isn't set.
* XXX - should we pick a default?
*/
return g_strdup("PATH isn't set");
}
}
/*
* OK, we have what we think is the pathname
* of the program.
*
* First, find the last "/" in the directory,
* as that marks the end of the directory pathname.
*/
dir_end = strrchr(prog_pathname, '/');
if (dir_end != NULL) {
/*
* Found it. Strip off the last component,
* as that's the path of the program.
*/
*dir_end = '\0';
/*
* Is there a "/run" at the end?
*/
dir_end = strrchr(prog_pathname, '/');
if (dir_end != NULL) {
if (!started_with_special_privs()) {
/*
* Check for the CMake output directory. As people may name
* their directories "run" (really?), also check for the
* CMakeCache.txt file before assuming a CMake output dir.
*/
if (strcmp(dir_end, "/run") == 0) {
gchar *cmake_file;
cmake_file = ws_strdup_printf("%.*s/CMakeCache.txt",
(int)(dir_end - prog_pathname),
prog_pathname);
if (file_exists(cmake_file))
running_in_build_directory_flag = TRUE;
g_free(cmake_file);
}
#ifdef ENABLE_APPLICATION_BUNDLE
{
/*
* Scan up the path looking for a component
* named "Contents". If we find it, we assume
* we're in a bundle, and that the top-level
* directory of the bundle is the one containing
* "Contents".
*
* Not all executables are in the Contents/MacOS
* directory, so we can't just check for those
* in the path and strip them off.
*
* XXX - should we assume that it's either
* Contents/MacOS or Resources/bin?
*/
char *component_end, *p;
component_end = strchr(prog_pathname, '\0');
p = component_end;
for (;;) {
while (p >= prog_pathname && *p != '/')
p--;
if (p == prog_pathname) {
/*
* We're looking at the first component of
* the pathname now, so we're definitely
* not in a bundle, even if we're in
* "/Contents".
*/
break;
}
if (strncmp(p, "/Contents", component_end - p) == 0) {
/* Found it. */
appbundle_dir = (char *)g_malloc(p - prog_pathname + 1);
memcpy(appbundle_dir, prog_pathname, p - prog_pathname);
appbundle_dir[p - prog_pathname] = '\0';
break;
}
component_end = p;
p--;
}
}
#endif
}
}
/*
* OK, we have the path we want.
*/
progfile_dir = prog_pathname;
trim_progfile_dir();
} else {
/*
* This "shouldn't happen"; we apparently
* have no "/" in the pathname.
* Just free up prog_pathname.
*/
retstr = ws_strdup_printf("No / found in \"%s\"", prog_pathname);
g_free(prog_pathname);
return retstr;
}
/*
* We already have the program_dir. Find the installation prefix.
* This is one level up from the bin_dir. If the program_dir does
* not end with "bin" then assume we are running in the build directory
* and the "installation prefix" (staging directory) is the same as
* the program_dir.
*/
g_free(install_prefix);
if (g_str_has_suffix(progfile_dir, _S"bin")) {
install_prefix = trim_last_dir_from_path(progfile_dir);
}
else {
install_prefix = g_strdup(progfile_dir);
running_in_build_directory_flag = TRUE;
}
return NULL;
}
#endif /* ?_WIN32 */
char *
configuration_init(const char* arg0, const char *namespace_name)
{
set_configuration_namespace(namespace_name);
#ifdef _WIN32
return configuration_init_w32(arg0);
#else
return configuration_init_posix(arg0);
#endif
}
/*
* Get the directory in which the program resides.
*/
const char *
get_progfile_dir(void)
{
return progfile_dir;
}
/*
* Get the directory in which the global configuration and data files are
* stored.
*
* On Windows, we use the directory in which the executable for this
* process resides.
*
* On macOS (when executed from an app bundle), use a directory within
* that app bundle.
*
* Otherwise, if the program was executed from the build directory, use the
* directory in which the executable for this process resides. In all other
* cases, use the DATA_DIR value that was set at compile time.
*
* XXX - if we ever make libwireshark a real library, used by multiple
* applications (more than just TShark and versions of Wireshark with
* various UIs), should the configuration files belong to the library
* (and be shared by all those applications) or to the applications?
*
* If they belong to the library, that could be done on UNIX by the
* configure script, but it's trickier on Windows, as you can't just
* use the pathname of the executable.
*
* If they belong to the application, that could be done on Windows
* by using the pathname of the executable, but we'd have to have it
* passed in as an argument, in some call, on UNIX.
*
* Note that some of those configuration files might be used by code in
* libwireshark, some of them might be used by dissectors (would they
* belong to libwireshark, the application, or a separate library?),
* and some of them might be used by other code (the Wireshark preferences
* file includes resolver preferences that control the behavior of code
* in libwireshark, dissector preferences, and UI preferences, for
* example).
*/
const char *
get_datafile_dir(void)
{
if (datafile_dir != NULL)
return datafile_dir;
const char *data_dir_envar = CONFIGURATION_ENVIRONMENT_VARIABLE("DATA_DIR");
if (g_getenv(data_dir_envar) && !started_with_special_privs()) {
/*
* The user specified a different directory for data files
* and we aren't running with special privileges.
* Let {WIRESHARK,LOGRAY}_DATA_DIR take precedence.
* XXX - We might be able to dispense with the priv check
*/
datafile_dir = g_strdup(g_getenv(data_dir_envar));
return datafile_dir;
}
#if defined(HAVE_MSYSTEM)
if (running_in_build_directory_flag) {
datafile_dir = g_strdup(install_prefix);
} else {
datafile_dir = g_build_filename(install_prefix, DATA_DIR, (gchar *)NULL);
}
#elif defined(_WIN32)
/*
* Do we have the pathname of the program? If so, assume we're
* running an installed version of the program. If we fail,
* we don't change "datafile_dir", and thus end up using the
* default.
*
* XXX - does NSIS put the installation directory into
* "\HKEY_LOCAL_MACHINE\SOFTWARE\Wireshark\InstallDir"?
* If so, perhaps we should read that from the registry,
* instead.
*/
if (progfile_dir != NULL) {
/*
* Yes, we do; use that.
*/
datafile_dir = g_strdup(progfile_dir);
} else {
/*
* No, we don't.
* Fall back on the default installation directory.
*/
datafile_dir = g_strdup("C:\\Program Files\\Wireshark\\");
}
#else
#ifdef ENABLE_APPLICATION_BUNDLE
/*
* If we're running from an app bundle and weren't started
* with special privileges, use the Contents/Resources/share/wireshark
* subdirectory of the app bundle.
*
* (appbundle_dir is not set to a non-null value if we're
* started with special privileges, so we need only check
* it; we don't need to call started_with_special_privs().)
*/
else if (appbundle_dir != NULL) {
datafile_dir = ws_strdup_printf("%s/Contents/Resources/share/%s",
appbundle_dir, CONFIGURATION_NAMESPACE_LOWER);
}
#endif
else if (running_in_build_directory_flag && progfile_dir != NULL) {
/*
* We're (probably) being run from the build directory and
* weren't started with special privileges.
*
* (running_in_build_directory_flag is never set to TRUE
* if we're started with special privileges, so we need
* only check it; we don't need to call started_with_special_privs().)
*
* Data files (dtds/, radius/, etc.) are copied to the build
* directory during the build which also contains executables. A special
* exception is macOS (when built with an app bundle).
*/
datafile_dir = g_strdup(progfile_dir);
} else {
datafile_dir = g_build_filename(install_prefix, DATA_DIR, (char *)NULL);
}
#endif
return datafile_dir;
}
const char *
get_doc_dir(void)
{
if (doc_dir != NULL)
return doc_dir;
/* No environment variable for this. */
if (false) {
;
}
#if defined(HAVE_MSYSTEM)
if (running_in_build_directory_flag) {
doc_dir = g_strdup(install_prefix);
} else {
doc_dir = g_build_filename(install_prefix, DOC_DIR, (gchar *)NULL);
}
#elif defined(_WIN32)
if (progfile_dir != NULL) {
doc_dir = g_strdup(progfile_dir);
} else {
/* Fall back on the default installation directory. */
doc_dir = g_strdup("C:\\Program Files\\Wireshark\\");
}
#else
#ifdef ENABLE_APPLICATION_BUNDLE
/*
* If we're running from an app bundle and weren't started
* with special privileges, use the Contents/Resources/share/wireshark
* subdirectory of the app bundle.
*
* (appbundle_dir is not set to a non-null value if we're
* started with special privileges, so we need only check
* it; we don't need to call started_with_special_privs().)
*/
else if (appbundle_dir != NULL) {
doc_dir = ws_strdup_printf("%s/Contents/Resources/%s",
appbundle_dir, DOC_DIR);
}
#endif
else if (running_in_build_directory_flag && progfile_dir != NULL) {
/*
* We're (probably) being run from the build directory and
* weren't started with special privileges.
*/
doc_dir = g_strdup(progfile_dir);
} else {
doc_dir = g_build_filename(install_prefix, DOC_DIR, (char *)NULL);
}
#endif
return doc_dir;
}
/*
* Find the directory where the plugins are stored.
*
* On Windows, we use the plugin\{VERSION} subdirectory of the datafile
* directory, where {VERSION} is the version number of this version of
* Wireshark.
*
* On UN*X:
*
* if we appear to be run from the build directory, we use the
* "plugin" subdirectory of the datafile directory;
*
* otherwise, if the WIRESHARK_PLUGIN_DIR environment variable is
* set and we aren't running with special privileges, we use the
* value of that environment variable;
*
* otherwise, if we're running from an app bundle in macOS, we
* use the Contents/PlugIns/wireshark subdirectory of the app bundle;
*
* otherwise, we use the PLUGIN_DIR value supplied by the
* configure script.
*/
static char *plugin_dir = NULL;
static char *plugin_dir_with_version = NULL;
static char *plugin_pers_dir = NULL;
static char *plugin_pers_dir_with_version = NULL;
static char *extcap_pers_dir = NULL;
static void
init_plugin_dir(void)
{
const char *plugin_dir_envar = CONFIGURATION_ENVIRONMENT_VARIABLE("PLUGIN_DIR");
if (g_getenv(plugin_dir_envar) && !started_with_special_privs()) {
/*
* The user specified a different directory for plugins
* and we aren't running with special privileges.
* Let {WIRESHARK,LOGRAY}_PLUGIN_DIR take precedence.
*/
plugin_dir = g_strdup(g_getenv(plugin_dir_envar));
return;
}
#if defined(HAVE_PLUGINS) || defined(HAVE_LUA)
#if defined(HAVE_MSYSTEM)
if (running_in_build_directory_flag) {
plugin_dir = g_build_filename(install_prefix, "plugins", (gchar *)NULL);
} else {
plugin_dir = g_build_filename(install_prefix, PLUGIN_DIR, (gchar *)NULL);
}
#elif defined(_WIN32)
/*
* On Windows, the data file directory is the installation
* directory; the plugins are stored under it.
*
* Assume we're running the installed version of Wireshark;
* on Windows, the data file directory is the directory
* in which the Wireshark binary resides.
*/
plugin_dir = g_build_filename(get_datafile_dir(), "plugins", (gchar *)NULL);
/*
* Make sure that pathname refers to a directory.
*/
if (test_for_directory(plugin_dir) != EISDIR) {
/*
* Either it doesn't refer to a directory or it
* refers to something that doesn't exist.
*
* Assume that means we're running a version of
* Wireshark we've built in a build directory,
* in which case {datafile dir}\plugins is the
* top-level plugins source directory, and use
* that directory and set the "we're running in
* a build directory" flag, so the plugin
* scanner will check all subdirectories of that
* directory for plugins.
*/
g_free(plugin_dir);
plugin_dir = g_build_filename(get_datafile_dir(), "plugins", (gchar *)NULL);
running_in_build_directory_flag = TRUE;
}
#else
#ifdef ENABLE_APPLICATION_BUNDLE
/*
* If we're running from an app bundle and weren't started
* with special privileges, use the Contents/PlugIns/wireshark
* subdirectory of the app bundle.
*
* (appbundle_dir is not set to a non-null value if we're
* started with special privileges, so we need only check
* it; we don't need to call started_with_special_privs().)
*/
else if (appbundle_dir != NULL) {
plugin_dir = g_build_filename(appbundle_dir, "Contents/PlugIns",
CONFIGURATION_NAMESPACE_LOWER, (gchar *)NULL);
}
#endif
else if (running_in_build_directory_flag) {
/*
* We're (probably) being run from the build directory and
* weren't started with special privileges, so we'll use
* the "plugins" subdirectory of the directory where the program
* we're running is (that's the build directory).
*/
plugin_dir = g_build_filename(get_progfile_dir(), "plugins", (gchar *)NULL);
} else {
plugin_dir = g_build_filename(install_prefix, PLUGIN_DIR, (char *)NULL);
}
#endif
#endif /* defined(HAVE_PLUGINS) || defined(HAVE_LUA) */
}
static void
init_plugin_pers_dir(void)
{
#if defined(HAVE_PLUGINS) || defined(HAVE_LUA)
#ifdef _WIN32
plugin_pers_dir = get_persconffile_path(PLUGINS_DIR_NAME, FALSE);
#else
plugin_pers_dir = g_build_filename(g_get_home_dir(), ".local/lib",
CONFIGURATION_NAMESPACE_LOWER, PLUGINS_DIR_NAME, (gchar *)NULL);
#endif
#endif /* defined(HAVE_PLUGINS) || defined(HAVE_LUA) */
}
/*
* Get the directory in which the plugins are stored.
*/
const char *
get_plugins_dir(void)
{
if (!plugin_dir)
init_plugin_dir();
return plugin_dir;
}
const char *
get_plugins_dir_with_version(void)
{
if (!plugin_dir)
init_plugin_dir();
if (plugin_dir && !plugin_dir_with_version)
plugin_dir_with_version = g_build_filename(plugin_dir, PLUGIN_PATH_ID, (gchar *)NULL);
return plugin_dir_with_version;
}
/* Get the personal plugin dir */
const char *
get_plugins_pers_dir(void)
{
if (!plugin_pers_dir)
init_plugin_pers_dir();
return plugin_pers_dir;
}
const char *
get_plugins_pers_dir_with_version(void)
{
if (!plugin_pers_dir)
init_plugin_pers_dir();
if (plugin_pers_dir && !plugin_pers_dir_with_version)
plugin_pers_dir_with_version = g_build_filename(plugin_pers_dir, PLUGIN_PATH_ID, (gchar *)NULL);
return plugin_pers_dir_with_version;
}
/*
* Find the directory where the extcap hooks are stored.
*
* If the WIRESHARK_EXTCAP_DIR environment variable is set and we are not
* running with special privileges, use that. Otherwise:
*
* On Windows, we use the "extcap" subdirectory of the datafile directory.
*
* On UN*X:
*
* if we appear to be run from the build directory, we use the
* "extcap" subdirectory of the build directory.
*
* otherwise, if we're running from an app bundle in macOS, we
* use the Contents/MacOS/extcap subdirectory of the app bundle;
*
* otherwise, we use the EXTCAP_DIR value supplied by CMake.
*/
static char *extcap_dir = NULL;
static void
init_extcap_dir(void)
{
const char *extcap_dir_envar = CONFIGURATION_ENVIRONMENT_VARIABLE("EXTCAP_DIR");
if (g_getenv(extcap_dir_envar) && !started_with_special_privs()) {
/*
* The user specified a different directory for extcap hooks
* and we aren't running with special privileges.
*/
extcap_dir = g_strdup(g_getenv(extcap_dir_envar));
}
#if defined(HAVE_MSYSTEM)
else if (running_in_build_directory_flag) {
extcap_dir = g_build_filename(install_prefix, "extcap", (gchar *)NULL);
} else {
extcap_dir = g_build_filename(install_prefix, EXTCAP_DIR, (gchar *)NULL);
}
#elif defined(_WIN32)
else {
/*
* On Windows, the data file directory is the installation
* directory; the extcap hooks are stored under it.
*
* Assume we're running the installed version of Wireshark;
* on Windows, the data file directory is the directory
* in which the Wireshark binary resides.
*/
extcap_dir = g_build_filename(get_datafile_dir(), "extcap", (gchar *)NULL);
}
#else
else if (running_in_build_directory_flag) {
/*
* We're (probably) being run from the build directory and
* weren't started with special privileges, so we'll use
* the "extcap hooks" subdirectory of the directory where the program
* we're running is (that's the build directory).
*/
extcap_dir = g_build_filename(get_progfile_dir(), "extcap", (gchar *)NULL);
}
#ifdef ENABLE_APPLICATION_BUNDLE
else if (appbundle_dir != NULL) {
/*
* If we're running from an app bundle and weren't started
* with special privileges, use the Contents/MacOS/extcap
* subdirectory of the app bundle.
*
* (appbundle_dir is not set to a non-null value if we're
* started with special privileges, so we need only check
* it; we don't need to call started_with_special_privs().)
*/
extcap_dir = g_build_filename(appbundle_dir, "Contents/MacOS/extcap", (gchar *)NULL);
}
#endif
else {
extcap_dir = g_build_filename(install_prefix, EXTCAP_DIR, (char *)NULL);
}
#endif
}
static void
init_extcap_pers_dir(void)
{
#ifdef _WIN32
extcap_pers_dir = get_persconffile_path(EXTCAP_DIR_NAME, FALSE);
#else
extcap_pers_dir = g_build_filename(g_get_home_dir(), ".local/lib",
CONFIGURATION_NAMESPACE_LOWER, EXTCAP_DIR_NAME, (gchar *)NULL);
#endif
}
/*
* Get the directory in which the extcap hooks are stored.
*
*/
const char *
get_extcap_dir(void)
{
if (!extcap_dir)
init_extcap_dir();
return extcap_dir;
}
/* Get the personal plugin dir */
const char *
get_extcap_pers_dir(void)
{
if (!extcap_pers_dir)
init_extcap_pers_dir();
return extcap_pers_dir;
}
/*
* Get the flag indicating whether we're running from a build
* directory.
*/
gboolean
running_in_build_directory(void)
{
return running_in_build_directory_flag;
}
/*
* Get the directory in which files that, at least on UNIX, are
* system files (such as "/etc/ethers") are stored; on Windows,
* there's no "/etc" directory, so we get them from the global
* configuration and data file directory.
*/
const char *
get_systemfile_dir(void)
{
#ifdef _WIN32
return get_datafile_dir();
#else
return "/etc";
#endif
}
void
set_profile_name(const gchar *profilename)
{
g_free (persconfprofile);
if (profilename && strlen(profilename) > 0 &&
strcmp(profilename, DEFAULT_PROFILE) != 0) {
persconfprofile = g_strdup (profilename);
} else {
/* Default Profile */
persconfprofile = NULL;
}
}
const char *
get_profile_name(void)
{
if (persconfprofile) {
return persconfprofile;
} else {
return DEFAULT_PROFILE;
}
}
gboolean
is_default_profile(void)
{
return (!persconfprofile || strcmp(persconfprofile, DEFAULT_PROFILE) == 0) ? TRUE : FALSE;
}
gboolean
has_global_profiles(void)
{
WS_DIR *dir;
WS_DIRENT *file;
gchar *global_dir = get_global_profiles_dir();
gchar *filename;
gboolean has_global = FALSE;
if ((test_for_directory(global_dir) == EISDIR) &&
((dir = ws_dir_open(global_dir, 0, NULL)) != NULL))
{
while ((file = ws_dir_read_name(dir)) != NULL) {
filename = ws_strdup_printf ("%s%s%s", global_dir, G_DIR_SEPARATOR_S,
ws_dir_get_name(file));
if (test_for_directory(filename) == EISDIR) {
has_global = TRUE;
g_free (filename);
break;
}
g_free (filename);
}
ws_dir_close(dir);
}
g_free(global_dir);
return has_global;
}
void
profile_store_persconffiles(gboolean store)
{
if (store) {
profile_files = g_hash_table_new (g_str_hash, g_str_equal);
}
do_store_persconffiles = store;
}
void
profile_register_persconffile(const char *filename)
{
if (do_store_persconffiles && !g_hash_table_lookup (profile_files, filename)) {
/* Store filenames so we know which filenames belongs to a configuration profile */
g_hash_table_insert (profile_files, g_strdup(filename), g_strdup(filename));
}
}
/*
* Get the directory in which personal configuration files reside.
*
* On Windows, it's "Wireshark", under %APPDATA% or, if %APPDATA% isn't set,
* it's "%USERPROFILE%\Application Data" (which is what %APPDATA% normally
* is on Windows 2000).
*
* On UNIX-compatible systems, we first look in XDG_CONFIG_HOME/wireshark
* and, if that doesn't exist, ~/.wireshark, for backwards compatibility.
* If neither exists, we use XDG_CONFIG_HOME/wireshark, so that the directory
* is initially created as XDG_CONFIG_HOME/wireshark. We use that regardless
* of whether the user is running under an XDG desktop or not, so that
* if the user's home directory is on a server and shared between
* different desktop environments on different machines, they can all
* share the same configuration file directory.
*
* XXX - what about stuff that shouldn't be shared between machines,
* such as plugins in the form of shared loadable images?
*/
static const char *
get_persconffile_dir_no_profile(void)
{
const char *env;
/* Return the cached value, if available */
if (persconffile_dir != NULL)
return persconffile_dir;
/*
* See if the user has selected an alternate environment.
*/
const char *config_dir_envar = CONFIGURATION_ENVIRONMENT_VARIABLE("CONFIG_DIR");
env = g_getenv(config_dir_envar);
#ifdef _WIN32
if (env == NULL) {
/* for backward compatibility */
env = g_getenv("WIRESHARK_APPDATA");
}
#endif
if (env != NULL) {
persconffile_dir = g_strdup(env);
return persconffile_dir;
}
#ifdef _WIN32
/*
* Use %APPDATA% or %USERPROFILE%, so that configuration
* files are stored in the user profile, rather than in
* the home directory. The Windows convention is to store
* configuration information in the user profile, and doing
* so means you can use Wireshark even if the home directory
* is an inaccessible network drive.
*/
env = g_getenv("APPDATA");
const char *persconf_namespace = CONFIGURATION_NAMESPACE_PROPER;
if (env != NULL) {
/*
* Concatenate %APPDATA% with "\Wireshark" or "\Logray".
*/
persconffile_dir = g_build_filename(env, persconf_namespace, NULL);
return persconffile_dir;
}
/*
* OK, %APPDATA% wasn't set, so use %USERPROFILE%\Application Data.
*/
env = g_getenv("USERPROFILE");
if (env != NULL) {
persconffile_dir = g_build_filename(env, "Application Data", persconf_namespace, NULL);
return persconffile_dir;
}
/*
* Give up and use "C:".
*/
persconffile_dir = g_build_filename("C:", persconf_namespace, NULL);
return persconffile_dir;
#else
char *xdg_path, *path;
struct passwd *pwd;
const char *homedir;
/*
* Check if XDG_CONFIG_HOME/wireshark exists and is a directory.
*/
xdg_path = g_build_filename(g_get_user_config_dir(),
CONFIGURATION_NAMESPACE_LOWER, NULL);
if (g_file_test(xdg_path, G_FILE_TEST_IS_DIR)) {
persconffile_dir = xdg_path;
return persconffile_dir;
}
/*
* It doesn't exist, or it does but isn't a directory, so try
* ~/.wireshark.
*
* If $HOME is set, use that for ~.
*
* (Note: before GLib 2.36, g_get_home_dir() didn't look at $HOME,
* but we always want to do so, so we don't use g_get_home_dir().)
*/
homedir = g_getenv("HOME");
if (homedir == NULL) {
/*
* It's not set.
*
* Get their home directory from the password file.
* If we can't even find a password file entry for them,
* use "/tmp".
*/
pwd = getpwuid(getuid());
if (pwd != NULL) {
homedir = pwd->pw_dir;
} else {
homedir = "/tmp";
}
}
path = g_build_filename(homedir,
configuration_namespace == CONFIGURATION_NAMESPACE_WIRESHARK ? ".wireshark" : ".logray",
NULL);
if (g_file_test(path, G_FILE_TEST_IS_DIR)) {
g_free(xdg_path);
persconffile_dir = path;
return persconffile_dir;
}
/*
* Neither are directories that exist; use the XDG path, so we'll
* create that as necessary.
*/
g_free(path);
persconffile_dir = xdg_path;
return persconffile_dir;
#endif
}
void
set_persconffile_dir(const char *p)
{
g_free(persconffile_dir);
persconffile_dir = g_strdup(p);
}
char *
get_profiles_dir(void)
{
return ws_strdup_printf ("%s%s%s", get_persconffile_dir_no_profile (),
G_DIR_SEPARATOR_S, PROFILES_DIR);
}
int
create_profiles_dir(char **pf_dir_path_return)
{
char *pf_dir_path;
ws_statb64 s_buf;
/*
* Create the "Default" personal configuration files directory, if necessary.
*/
if (create_persconffile_profile (NULL, pf_dir_path_return) == -1) {
return -1;
}
/*
* Check if profiles directory exists.
* If not then create it.
*/
pf_dir_path = get_profiles_dir ();
if (ws_stat64(pf_dir_path, &s_buf) != 0) {
if (errno != ENOENT) {
/* Some other problem; give up now. */
*pf_dir_path_return = pf_dir_path;
return -1;
}
/*
* It doesn't exist; try to create it.
*/
int ret = ws_mkdir(pf_dir_path, 0755);
if (ret == -1) {
*pf_dir_path_return = pf_dir_path;
return ret;
}
}
g_free(pf_dir_path);
return 0;
}
char *
get_global_profiles_dir(void)
{
return ws_strdup_printf ("%s%s%s", get_datafile_dir(),
G_DIR_SEPARATOR_S, PROFILES_DIR);
}
static char *
get_persconffile_dir(const gchar *profilename)
{
char *persconffile_profile_dir = NULL, *profile_dir;
if (profilename && strlen(profilename) > 0 &&
strcmp(profilename, DEFAULT_PROFILE) != 0) {
profile_dir = get_profiles_dir();
persconffile_profile_dir = ws_strdup_printf ("%s%s%s", profile_dir,
G_DIR_SEPARATOR_S, profilename);
g_free(profile_dir);
} else {
persconffile_profile_dir = g_strdup (get_persconffile_dir_no_profile ());
}
return persconffile_profile_dir;
}
char *
get_profile_dir(const char *profilename, gboolean is_global)
{
gchar *profile_dir;
if (is_global) {
if (profilename && strlen(profilename) > 0 &&
strcmp(profilename, DEFAULT_PROFILE) != 0)
{
gchar *global_path = get_global_profiles_dir();
profile_dir = g_build_filename(global_path, profilename, NULL);
g_free(global_path);
} else {
profile_dir = g_strdup(get_datafile_dir());
}
} else {
/*
* If we didn't supply a profile name, i.e. if profilename is
* null, get_persconffile_dir() returns the default profile.
*/
profile_dir = get_persconffile_dir(profilename);
}
return profile_dir;
}
gboolean
profile_exists(const gchar *profilename, gboolean global)
{
gchar *path = NULL;
gboolean exists;
/*
* If we're looking up a global profile, we must have a
* profile name.
*/
if (global && !profilename)
return FALSE;
path = get_profile_dir(profilename, global);
exists = (test_for_directory(path) == EISDIR) ? TRUE : FALSE;
g_free(path);
return exists;
}
static int
delete_directory (const char *directory, char **pf_dir_path_return)
{
WS_DIR *dir;
WS_DIRENT *file;
gchar *filename;
int ret = 0;
if ((dir = ws_dir_open(directory, 0, NULL)) != NULL) {
while ((file = ws_dir_read_name(dir)) != NULL) {
filename = ws_strdup_printf ("%s%s%s", directory, G_DIR_SEPARATOR_S,
ws_dir_get_name(file));
if (test_for_directory(filename) != EISDIR) {
ret = ws_remove(filename);
#if 0
} else {
/* The user has manually created a directory in the profile directory */
/* I do not want to delete the directory recursively yet */
ret = delete_directory (filename, pf_dir_path_return);
#endif
}
if (ret != 0) {
*pf_dir_path_return = filename;
break;
}
g_free (filename);
}
ws_dir_close(dir);
}
if (ret == 0 && (ret = ws_remove(directory)) != 0) {
*pf_dir_path_return = g_strdup (directory);
}
return ret;
}
/* Copy files from one directory to another. Does not recursively copy directories */
static int
copy_directory(const char *from_dir, const char *to_dir, char **pf_filename_return)
{
int ret = 0;
gchar *from_file, *to_file;
const char *filename;
WS_DIR *dir;
WS_DIRENT *file;
if ((dir = ws_dir_open(from_dir, 0, NULL)) != NULL) {
while ((file = ws_dir_read_name(dir)) != NULL) {
filename = ws_dir_get_name(file);
from_file = ws_strdup_printf ("%s%s%s", from_dir, G_DIR_SEPARATOR_S, filename);
if (test_for_directory(from_file) != EISDIR) {
to_file = ws_strdup_printf ("%s%s%s", to_dir, G_DIR_SEPARATOR_S, filename);
if (!copy_file_binary_mode(from_file, to_file)) {
*pf_filename_return = g_strdup(filename);
g_free (from_file);
g_free (to_file);
ret = -1;
break;
}
g_free (to_file);
#if 0
} else {
/* The user has manually created a directory in the profile
* directory. Do not copy the directory recursively (yet?)
*/
#endif
}
g_free (from_file);
}
ws_dir_close(dir);
}
return ret;
}
static int
reset_default_profile(char **pf_dir_path_return)
{
char *profile_dir = get_persconffile_dir(NULL);
gchar *filename, *del_file;
GList *files, *file;
int ret = 0;
files = g_hash_table_get_keys(profile_files);
file = g_list_first(files);
while (file) {
filename = (gchar *)file->data;
del_file = ws_strdup_printf("%s%s%s", profile_dir, G_DIR_SEPARATOR_S, filename);
if (file_exists(del_file)) {
ret = ws_remove(del_file);
if (ret != 0) {
*pf_dir_path_return = profile_dir;
g_free(del_file);
break;
}
}
g_free(del_file);
file = g_list_next(file);
}
g_list_free(files);
g_free(profile_dir);
return ret;
}
int
delete_persconffile_profile(const char *profilename, char **pf_dir_path_return)
{
if (strcmp(profilename, DEFAULT_PROFILE) == 0) {
return reset_default_profile(pf_dir_path_return);
}
char *profile_dir = get_persconffile_dir(profilename);
int ret = 0;
if (test_for_directory (profile_dir) == EISDIR) {
ret = delete_directory (profile_dir, pf_dir_path_return);
}
g_free(profile_dir);
return ret;
}
int
rename_persconffile_profile(const char *fromname, const char *toname,
char **pf_from_dir_path_return, char **pf_to_dir_path_return)
{
char *from_dir = get_persconffile_dir(fromname);
char *to_dir = get_persconffile_dir(toname);
int ret = 0;
ret = ws_rename (from_dir, to_dir);
if (ret != 0) {
*pf_from_dir_path_return = from_dir;
*pf_to_dir_path_return = to_dir;
return ret;
}
g_free (from_dir);
g_free (to_dir);
return 0;
}
/*
* Create the directory that holds personal configuration files, if
* necessary. If we attempted to create it, and failed, return -1 and
* set "*pf_dir_path_return" to the pathname of the directory we failed
* to create (it's g_mallocated, so our caller should free it); otherwise,
* return 0.
*/
int
create_persconffile_profile(const char *profilename, char **pf_dir_path_return)
{
char *pf_dir_path;
#ifdef _WIN32
char *pf_dir_path_copy, *pf_dir_parent_path;
size_t pf_dir_parent_path_len;
int save_errno;
#endif
ws_statb64 s_buf;
int ret;
if (profilename) {
/*
* Create the personal profiles directory, if necessary.
*/
if (create_profiles_dir(pf_dir_path_return) == -1) {
return -1;
}
}
pf_dir_path = get_persconffile_dir(profilename);
if (ws_stat64(pf_dir_path, &s_buf) != 0) {
if (errno != ENOENT) {
/* Some other problem; give up now. */
*pf_dir_path_return = pf_dir_path;
return -1;
}
#ifdef _WIN32
/*
* Does the parent directory of that directory
* exist? %APPDATA% may not exist even though
* %USERPROFILE% does.
*
* We check for the existence of the directory
* by first checking whether the parent directory
* is just a drive letter and, if it's not, by
* doing a "stat()" on it. If it's a drive letter,
* or if the "stat()" succeeds, we assume it exists.
*/
pf_dir_path_copy = g_strdup(pf_dir_path);
pf_dir_parent_path = get_dirname(pf_dir_path_copy);
pf_dir_parent_path_len = strlen(pf_dir_parent_path);
if (pf_dir_parent_path_len > 0
&& pf_dir_parent_path[pf_dir_parent_path_len - 1] != ':'
&& ws_stat64(pf_dir_parent_path, &s_buf) != 0) {
/*
* Not a drive letter and the stat() failed.
*/
if (errno != ENOENT) {
/* Some other problem; give up now. */
*pf_dir_path_return = pf_dir_path;
save_errno = errno;
g_free(pf_dir_path_copy);
errno = save_errno;
return -1;
}
/*
* No, it doesn't exist - make it first.
*/
ret = ws_mkdir(pf_dir_parent_path, 0755);
if (ret == -1) {
*pf_dir_path_return = pf_dir_parent_path;
save_errno = errno;
g_free(pf_dir_path);
errno = save_errno;
return -1;
}
}
g_free(pf_dir_path_copy);
ret = ws_mkdir(pf_dir_path, 0755);
#else
ret = g_mkdir_with_parents(pf_dir_path, 0755);
#endif
} else {
/*
* Something with that pathname exists; if it's not
* a directory, we'll get an error if we try to put
* something in it, so we don't fail here, we wait
* for that attempt to fail.
*/
ret = 0;
}
if (ret == -1)
*pf_dir_path_return = pf_dir_path;
else
g_free(pf_dir_path);
return ret;
}
const GHashTable *
allowed_profile_filenames(void)
{
return profile_files;
}
int
create_persconffile_dir(char **pf_dir_path_return)
{
return create_persconffile_profile(persconfprofile, pf_dir_path_return);
}
int
copy_persconffile_profile(const char *toname, const char *fromname, gboolean from_global,
char **pf_filename_return, char **pf_to_dir_path_return, char **pf_from_dir_path_return)
{
int ret = 0;
gchar *from_dir;
gchar *to_dir = get_persconffile_dir(toname);
gchar *from_file, *to_file;
const char *filename;
GHashTableIter files;
gpointer file;
from_dir = get_profile_dir(fromname, from_global);
if (!profile_files || do_store_persconffiles) {
/* Either the profile_files hashtable does not exist yet
* (this is very early in startup) or we are still adding
* files to it. Just copy all the non-directories.
*/
ret = copy_directory(from_dir, to_dir, pf_filename_return);
} else {
g_hash_table_iter_init(&files, profile_files);
while (g_hash_table_iter_next(&files, &file, NULL)) {
filename = (const char *)file;
from_file = ws_strdup_printf ("%s%s%s", from_dir, G_DIR_SEPARATOR_S, filename);
to_file = ws_strdup_printf ("%s%s%s", to_dir, G_DIR_SEPARATOR_S, filename);
if (file_exists(from_file) && !copy_file_binary_mode(from_file, to_file)) {
*pf_filename_return = g_strdup(filename);
g_free (from_file);
g_free (to_file);
ret = -1;
break;
}
g_free (to_file);
g_free (from_file);
}
}
if (ret != 0) {
*pf_to_dir_path_return = to_dir;
*pf_from_dir_path_return = from_dir;
} else {
g_free (to_dir);
g_free (from_dir);
}
return ret;
}
/*
* Get the (default) directory in which personal data is stored.
*
* On Win32, this is the "My Documents" folder in the personal profile.
* On UNIX this is simply the current directory.
*/
/* XXX - should this and the get_home_dir() be merged? */
extern const char *
get_persdatafile_dir(void)
{
#ifdef _WIN32
TCHAR tszPath[MAX_PATH];
/* Return the cached value, if available */
if (persdatafile_dir != NULL)
return persdatafile_dir;
/*
* Hint: SHGetFolderPath is not available on MSVC 6 - without
* Platform SDK
*/
if (SHGetSpecialFolderPath(NULL, tszPath, CSIDL_PERSONAL, FALSE)) {
persdatafile_dir = g_utf16_to_utf8(tszPath, -1, NULL, NULL, NULL);
return persdatafile_dir;
} else {
return "";
}
#else
return "";
#endif
}
void
set_persdatafile_dir(const char *p)
{
g_free(persdatafile_dir);
persdatafile_dir = g_strdup(p);
}
/*
* Construct the path name of a personal configuration file, given the
* file name.
*
* On Win32, if "for_writing" is FALSE, we check whether the file exists
* and, if not, construct a path name relative to the ".wireshark"
* subdirectory of the user's home directory, and check whether that
* exists; if it does, we return that, so that configuration files
* from earlier versions can be read.
*
* The returned file name was g_malloc()'d so it must be g_free()d when the
* caller is done with it.
*/
char *
get_persconffile_path(const char *filename, gboolean from_profile)
{
char *path, *dir = NULL;
if (from_profile) {
/* Store filenames so we know which filenames belongs to a configuration profile */
profile_register_persconffile(filename);
dir = get_persconffile_dir(persconfprofile);
} else {
dir = get_persconffile_dir(NULL);
}
path = g_build_filename(dir, filename, NULL);
g_free(dir);
return path;
}
/*
* Construct the path name of a global configuration file, given the
* file name.
*
* The returned file name was g_malloc()'d so it must be g_free()d when the
* caller is done with it.
*/
char *
get_datafile_path(const char *filename)
{
if (running_in_build_directory_flag && !strcmp(filename, "hosts")) {
/* We're running in the build directory and the requested file is a
* generated (or a test) file. Return the file name in the build
* directory (not in the source/data directory).
* (Oh the things we do to keep the source directory pristine...)
*/
return g_build_filename(get_progfile_dir(), filename, (char *)NULL);
} else {
return g_build_filename(get_datafile_dir(), filename, (char *)NULL);
}
}
/*
* Construct the path name of a global documentation file, given the
* file name.
*
* The returned file name was g_malloc()'d so it must be g_free()d when the
* caller is done with it.
*/
char *
get_docfile_path(const char *filename)
{
if (running_in_build_directory_flag) {
/* We're running in the build directory and the requested file is a
* generated (or a test) file. Return the file name in the build
* directory (not in the source/data directory).
* (Oh the things we do to keep the source directory pristine...)
*/
return g_build_filename(get_progfile_dir(), filename, (char *)NULL);
} else {
return g_build_filename(get_doc_dir(), filename, (char *)NULL);
}
}
/*
* Return an error message for UNIX-style errno indications on open or
* create operations.
*/
const char *
file_open_error_message(int err, gboolean for_writing)
{
const char *errmsg;
static char errmsg_errno[1024+1];
switch (err) {
case ENOENT:
if (for_writing)
errmsg = "The path to the file \"%s\" doesn't exist.";
else
errmsg = "The file \"%s\" doesn't exist.";
break;
case EACCES:
if (for_writing)
errmsg = "You don't have permission to create or write to the file \"%s\".";
else
errmsg = "You don't have permission to read the file \"%s\".";
break;
case EISDIR:
errmsg = "\"%s\" is a directory (folder), not a file.";
break;
case ENOSPC:
errmsg = "The file \"%s\" could not be created because there is no space left on the file system.";
break;
#ifdef EDQUOT
case EDQUOT:
errmsg = "The file \"%s\" could not be created because you are too close to, or over, your disk quota.";
break;
#endif
case EINVAL:
errmsg = "The file \"%s\" could not be created because an invalid filename was specified.";
break;
#ifdef ENAMETOOLONG
case ENAMETOOLONG:
/* XXX Make sure we truncate on a character boundary. */
errmsg = "The file name \"%.80s" UTF8_HORIZONTAL_ELLIPSIS "\" is too long.";
break;
#endif
case ENOMEM:
/*
* The problem probably has nothing to do with how much RAM the
* user has on their machine, so don't confuse them by saying
* "memory". The problem is probably either virtual address
* space or swap space.
*/
#if GLIB_SIZEOF_VOID_P == 4
/*
* ILP32; we probably ran out of virtual address space.
*/
#define ENOMEM_REASON "it can't be handled by a 32-bit application"
#else
/*
* LP64 or LLP64; we probably ran out of swap space.
*/
#if defined(_WIN32)
/*
* You need to make the pagefile bigger.
*/
#define ENOMEM_REASON "the pagefile is too small"
#elif defined(ENABLE_APPLICATION_BUNDLE)
/*
* dynamic_pager couldn't, or wouldn't, create more swap files.
*/
#define ENOMEM_REASON "your system ran out of swap file space"
#else
/*
* Either you have a fixed swap partition or a fixed swap file,
* and it needs to be made bigger.
*
* This is UN*X, but it's not macOS, so we assume the user is
* *somewhat* nerdy.
*/
#define ENOMEM_REASON "your system is out of swap space"
#endif
#endif /* GLIB_SIZEOF_VOID_P == 4 */
if (for_writing)
errmsg = "The file \"%s\" could not be created because " ENOMEM_REASON ".";
else
errmsg = "The file \"%s\" could not be opened because " ENOMEM_REASON ".";
break;
default:
snprintf(errmsg_errno, sizeof(errmsg_errno),
"The file \"%%s\" could not be %s: %s.",
for_writing ? "created" : "opened",
g_strerror(err));
errmsg = errmsg_errno;
break;
}
return errmsg;
}
/*
* Return an error message for UNIX-style errno indications on write
* operations.
*/
const char *
file_write_error_message(int err)
{
const char *errmsg;
static char errmsg_errno[1024+1];
switch (err) {
case ENOSPC:
errmsg = "The file \"%s\" could not be saved because there is no space left on the file system.";
break;
#ifdef EDQUOT
case EDQUOT:
errmsg = "The file \"%s\" could not be saved because you are too close to, or over, your disk quota.";
break;
#endif
default:
snprintf(errmsg_errno, sizeof(errmsg_errno),
"An error occurred while writing to the file \"%%s\": %s.",
g_strerror(err));
errmsg = errmsg_errno;
break;
}
return errmsg;
}
gboolean
file_exists(const char *fname)
{
ws_statb64 file_stat;
if (!fname) {
return FALSE;
}
if (ws_stat64(fname, &file_stat) != 0 && errno == ENOENT) {
return FALSE;
} else {
return TRUE;
}
}
gboolean config_file_exists_with_entries(const char *fname, char comment_char)
{
gboolean start_of_line = TRUE;
gboolean has_entries = FALSE;
FILE *file;
int c;
if (!fname) {
return FALSE;
}
if ((file = ws_fopen(fname, "r")) == NULL) {
return FALSE;
}
do {
c = ws_getc_unlocked(file);
if (start_of_line && c != comment_char && !g_ascii_isspace(c) && g_ascii_isprint(c)) {
has_entries = TRUE;
break;
}
if (c == '\n' || !g_ascii_isspace(c)) {
start_of_line = (c == '\n');
}
} while (c != EOF);
fclose(file);
return has_entries;
}
/*
* Check that the from file is not the same as to file
* We do it here so we catch all cases ...
* Unfortunately, the file requester gives us an absolute file
* name and the read file name may be relative (if supplied on
* the command line), so we can't just compare paths. From Joerg Mayer.
*/
gboolean
files_identical(const char *fname1, const char *fname2)
{
/* Two different implementations, because:
*
* - _fullpath is not available on UN*X, so we can't get full
* paths and compare them (which wouldn't work with hard links
* in any case);
*
* - st_ino isn't filled in with a meaningful value on Windows.
*/
#ifdef _WIN32
char full1[MAX_PATH], full2[MAX_PATH];
/*
* Get the absolute full paths of the file and compare them.
* That won't work if you have hard links, but those aren't
* much used on Windows, even though NTFS supports them.
*
* XXX - will _fullpath work with UNC?
*/
if( _fullpath( full1, fname1, MAX_PATH ) == NULL ) {
return FALSE;
}
if( _fullpath( full2, fname2, MAX_PATH ) == NULL ) {
return FALSE;
}
if(strcmp(full1, full2) == 0) {
return TRUE;
} else {
return FALSE;
}
#else
ws_statb64 filestat1, filestat2;
/*
* Compare st_dev and st_ino.
*/
if (ws_stat64(fname1, &filestat1) == -1)
return FALSE; /* can't get info about the first file */
if (ws_stat64(fname2, &filestat2) == -1)
return FALSE; /* can't get info about the second file */
return (filestat1.st_dev == filestat2.st_dev &&
filestat1.st_ino == filestat2.st_ino);
#endif
}
gboolean
file_needs_reopen(int fd, const char* filename)
{
#ifdef _WIN32
/* Windows handles st_dev in a way unsuitable here:
* * _fstat() simply casts the file descriptor (ws_fileno(fp)) to unsigned
* and assigns this value to st_dev and st_rdev
* * _wstat() converts drive letter (eg. C) to number (A=0, B=1, C=2, ...)
* and assigns such number to st_dev and st_rdev
*
* The st_ino parameter is simply zero as there is no specific assignment
* to it in the Universal CRT source code.
*
* Thus instead of using fstat(), use Windows specific API.
*/
HANDLE open_handle = (HANDLE)_get_osfhandle(fd);
HANDLE current_handle = CreateFile(utf_8to16(filename), FILE_READ_ATTRIBUTES,
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE,
NULL, OPEN_EXISTING, 0, NULL);
BY_HANDLE_FILE_INFORMATION open_info, current_info;
if (current_handle == INVALID_HANDLE_VALUE) {
return TRUE;
}
#if (_WIN32_WINNT >= _WIN32_WINNT_WIN8)
FILE_ID_INFO open_id, current_id;
if (GetFileInformationByHandleEx(open_handle, FileIdInfo, &open_id, sizeof(open_id)) &&
GetFileInformationByHandleEx(current_handle, FileIdInfo, ¤t_id, sizeof(current_id))) {
/* 128-bit identifier is available, use it */
CloseHandle(current_handle);
return open_id.VolumeSerialNumber != current_id.VolumeSerialNumber ||
memcmp(&open_id.FileId, ¤t_id.FileId, sizeof(open_id.FileId)) != 0;
}
#endif /* _WIN32_WINNT >= _WIN32_WINNT_WIN8 */
if (GetFileInformationByHandle(open_handle, &open_info) &&
GetFileInformationByHandle(current_handle, ¤t_info)) {
/* Fallback to 64-bit identifier */
CloseHandle(current_handle);
guint64 open_size = (((guint64)open_info.nFileSizeHigh) << 32) | open_info.nFileSizeLow;
guint64 current_size = (((guint64)current_info.nFileSizeHigh) << 32) | current_info.nFileSizeLow;
return open_info.dwVolumeSerialNumber != current_info.dwVolumeSerialNumber ||
open_info.nFileIndexHigh != current_info.nFileIndexHigh ||
open_info.nFileIndexLow != current_info.nFileIndexLow ||
open_size > current_size;
}
CloseHandle(current_handle);
return TRUE;
#else
ws_statb64 open_stat, current_stat;
/* consider a file deleted when stat fails for either file,
* or when the residing device / inode has changed. */
if (0 != ws_fstat64(fd, &open_stat))
return TRUE;
if (0 != ws_stat64(filename, ¤t_stat))
return TRUE;
return open_stat.st_dev != current_stat.st_dev ||
open_stat.st_ino != current_stat.st_ino ||
open_stat.st_size > current_stat.st_size;
#endif
}
gboolean
write_file_binary_mode(const char *filename, const void *content, size_t content_len)
{
int fd;
size_t bytes_left;
unsigned int bytes_to_write;
ssize_t bytes_written;
const guint8 *ptr;
int err;
fd = ws_open(filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
if (fd == -1) {
report_open_failure(filename, errno, TRUE);
return FALSE;
}
/*
* The third argument to _write() on Windows is an unsigned int,
* so, on Windows, that's the size of the third argument to
* ws_write().
*
* The third argument to write() on UN*X is a size_t, although
* the return value is an ssize_t, so one probably shouldn't
* write more than the max value of an ssize_t.
*
* In either case, there's no guarantee that a size_t such as
* content_len can be passed to ws_write(), so we write in
* chunks of at most 2^31 bytes.
*/
ptr = (const guint8 *)content;
bytes_left = content_len;
while (bytes_left != 0) {
if (bytes_left > 0x40000000) {
bytes_to_write = 0x40000000;
} else {
bytes_to_write = (unsigned int)bytes_left;
}
bytes_written = ws_write(fd, ptr, bytes_to_write);
if (bytes_written <= 0) {
if (bytes_written < 0) {
err = errno;
} else {
err = WTAP_ERR_SHORT_WRITE;
}
report_write_failure(filename, err);
ws_close(fd);
return FALSE;
}
bytes_left -= bytes_written;
ptr += bytes_written;
}
ws_close(fd);
return TRUE;
}
/*
* Copy a file in binary mode, for those operating systems that care about
* such things. This should be OK for all files, even text files, as
* we'll copy the raw bytes, and we don't look at the bytes as we copy
* them.
*
* Returns TRUE on success, FALSE on failure. If a failure, it also
* displays a simple dialog window with the error message.
*/
gboolean
copy_file_binary_mode(const char *from_filename, const char *to_filename)
{
int from_fd, to_fd, err;
ws_file_ssize_t nread, nwritten;
guint8 *pd = NULL;
/* Copy the raw bytes of the file. */
from_fd = ws_open(from_filename, O_RDONLY | O_BINARY, 0000 /* no creation so don't matter */);
if (from_fd < 0) {
report_open_failure(from_filename, errno, FALSE);
goto done;
}
/* Use open() instead of creat() so that we can pass the O_BINARY
flag, which is relevant on Win32; it appears that "creat()"
may open the file in text mode, not binary mode, but we want
to copy the raw bytes of the file, so we need the output file
to be open in binary mode. */
to_fd = ws_open(to_filename, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 0644);
if (to_fd < 0) {
report_open_failure(to_filename, errno, TRUE);
ws_close(from_fd);
goto done;
}
#define FS_READ_SIZE 65536
pd = (guint8 *)g_malloc(FS_READ_SIZE);
while ((nread = ws_read(from_fd, pd, FS_READ_SIZE)) > 0) {
nwritten = ws_write(to_fd, pd, nread);
if (nwritten < nread) {
if (nwritten < 0)
err = errno;
else
err = WTAP_ERR_SHORT_WRITE;
report_write_failure(to_filename, err);
ws_close(from_fd);
ws_close(to_fd);
goto done;
}
}
if (nread < 0) {
err = errno;
report_read_failure(from_filename, err);
ws_close(from_fd);
ws_close(to_fd);
goto done;
}
ws_close(from_fd);
if (ws_close(to_fd) < 0) {
report_write_failure(to_filename, errno);
goto done;
}
g_free(pd);
pd = NULL;
return TRUE;
done:
g_free(pd);
return FALSE;
}
gchar *
data_file_url(const gchar *filename)
{
gchar *file_path;
gchar *uri;
/* Absolute path? */
if(g_path_is_absolute(filename)) {
file_path = g_strdup(filename);
} else {
file_path = ws_strdup_printf("%s/%s", get_datafile_dir(), filename);
}
/* XXX - check, if the file is really existing, otherwise display a simple_dialog about the problem */
/* convert filename to uri */
uri = g_filename_to_uri(file_path, NULL, NULL);
g_free(file_path);
return uri;
}
gchar *
doc_file_url(const gchar *filename)
{
gchar *file_path;
gchar *uri;
/* Absolute path? */
if(g_path_is_absolute(filename)) {
file_path = g_strdup(filename);
} else {
file_path = ws_strdup_printf("%s/%s", get_doc_dir(), filename);
}
/* XXX - check, if the file is really existing, otherwise display a simple_dialog about the problem */
/* convert filename to uri */
uri = g_filename_to_uri(file_path, NULL, NULL);
g_free(file_path);
return uri;
}
void
free_progdirs(void)
{
g_free(persconffile_dir);
persconffile_dir = NULL;
g_free(datafile_dir);
datafile_dir = NULL;
g_free(persdatafile_dir);
persdatafile_dir = NULL;
g_free(persconfprofile);
persconfprofile = NULL;
g_free(progfile_dir);
progfile_dir = NULL;
g_free(doc_dir);
doc_dir = NULL;
g_free(install_prefix);
install_prefix = NULL;
#if defined(HAVE_PLUGINS) || defined(HAVE_LUA)
g_free(plugin_dir);
plugin_dir = NULL;
g_free(plugin_dir_with_version);
plugin_dir_with_version = NULL;
g_free(plugin_pers_dir);
plugin_pers_dir = NULL;
g_free(plugin_pers_dir_with_version);
plugin_pers_dir_with_version = NULL;
#endif
g_free(extcap_dir);
extcap_dir = NULL;
g_free(extcap_pers_dir);
extcap_pers_dir = NULL;
}
/*
* Editor modelines
*
* Local Variables:
* c-basic-offset: 4
* tab-width: 8
* indent-tabs-mode: nil
* End:
*
* ex: set shiftwidth=4 tabstop=8 expandtab:
* :indentSize=4:tabSize=8:noTabs=true:
*/ |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.