AFAIKT there's no reason to #include <wsutil/crc32.c>; Include crc32.h like everyone else...

Also: Do some whitespace and indentation cleanup.

svn path=/trunk/; revision=41281
This commit is contained in:
Bill Meier 2012-03-02 00:31:30 +00:00
parent a146f5a2e2
commit f2c1c108f5
1 changed files with 183 additions and 180 deletions

View File

@ -39,7 +39,7 @@
*
* See
*
* http://www.endace.com/support/EndaceRecordFormat.pdf
* http://www.endace.com/support/EndaceRecordFormat.pdf
*/
#ifdef HAVE_CONFIG_H
@ -49,7 +49,9 @@
#include <stdlib.h>
#include <string.h>
#include <wsutil/crc32.c>
#include <glib.h>
#include <wsutil/crc32.h>
#include "wtap-int.h"
#include "file_wrappers.h"
@ -58,18 +60,18 @@
#include "erf.h"
static int erf_read_header(FILE_T fh,
struct wtap_pkthdr *phdr,
union wtap_pseudo_header *pseudo_header,
erf_header_t *erf_header,
int *err,
gchar **err_info,
guint32 *bytes_read,
guint32 *packet_size);
struct wtap_pkthdr *phdr,
union wtap_pseudo_header *pseudo_header,
erf_header_t *erf_header,
int *err,
gchar **err_info,
guint32 *bytes_read,
guint32 *packet_size);
static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
gint64 *data_offset);
gint64 *data_offset);
static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
union wtap_pseudo_header *pseudo_header, guint8 *pd,
int length, int *err, gchar **err_info);
union wtap_pseudo_header *pseudo_header, guint8 *pd,
int length, int *err, gchar **err_info);
static const struct {
int erf_encap_value;
@ -89,19 +91,19 @@ static const struct {
extern int erf_open(wtap *wth, int *err, gchar **err_info)
{
int i, n, records_for_erf_check = RECORDS_FOR_ERF_CHECK;
int valid_prev = 0;
char *s;
erf_timestamp_t prevts,ts;
erf_header_t header;
guint32 mc_hdr;
guint16 eth_hdr;
guint32 packet_size;
guint16 rlen;
guint64 erf_ext_header;
guint8 type;
size_t r;
gchar * buffer;
int i, n, records_for_erf_check = RECORDS_FOR_ERF_CHECK;
int valid_prev = 0;
char *s;
erf_timestamp_t prevts,ts;
erf_header_t header;
guint32 mc_hdr;
guint16 eth_hdr;
guint32 packet_size;
guint16 rlen;
guint64 erf_ext_header;
guint8 type;
size_t r;
gchar * buffer;
memset(&prevts, 0, sizeof(prevts));
@ -125,28 +127,28 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
if (r == 0 ) break;
if (r != sizeof(header)) {
if ((*err = file_error(wth->fh, err_info)) != 0) {
return -1;
return -1;
} else {
/* ERF header too short accept the file,
only if the very first records have been successfully checked */
if (i < MIN_RECORDS_FOR_ERF_CHECK) {
return 0;
} else {
/* BREAK, the last record is too short, and will be ignored */
break;
}
/* ERF header too short accept the file,
only if the very first records have been successfully checked */
if (i < MIN_RECORDS_FOR_ERF_CHECK) {
return 0;
} else {
/* BREAK, the last record is too short, and will be ignored */
break;
}
}
}
rlen=g_ntohs(header.rlen);
/* fail on invalid record type, invalid rlen, timestamps decreasing, or incrementing too far */
/* Test valid rlen >= 16 */
if (rlen < 16) {
return 0;
}
packet_size = rlen - (guint32)sizeof(header);
if (packet_size > WTAP_MAX_PACKET_SIZE) {
/*
@ -160,7 +162,7 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
/* Skip PAD records, timestamps may not be set */
if ((header.type & 0x7F) == ERF_TYPE_PAD) {
if (file_seek(wth->fh, packet_size, SEEK_CUR, err) == -1) {
return -1;
return -1;
}
continue;
}
@ -170,65 +172,65 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
if ((header.type & 0x7F) == 0 || (header.type & 0x7F) > ERF_TYPE_MAX ) {
return 0;
}
/* The ERF_TYPE_MAX is the PAD record, but the last used type is ERF_TYPE_INFINIBAND_LINK */
if ((header.type & 0x7F) > ERF_TYPE_INFINIBAND_LINK) {
return 0;
}
if ((ts = pletohll(&header.ts)) < prevts) {
/* reassembled AALx records may not be in time order, also records are not in strict time order between physical interfaces, so allow 1 sec fudge */
if ( ((prevts-ts)>>32) > 1 ) {
return 0;
return 0;
}
}
/* Check to see if timestamp increment is > 1 week */
if ( (valid_prev) && (ts > prevts) && (((ts-prevts)>>32) > 3600*24*7) ) {
return 0;
}
memcpy(&prevts, &ts, sizeof(prevts));
/* Read over the extension headers */
type = header.type;
while (type & 0x80){
if (file_read(&erf_ext_header, sizeof(erf_ext_header),wth->fh) != sizeof(erf_ext_header)) {
*err = file_error(wth->fh, err_info);
return -1;
}
packet_size -= (guint32)sizeof(erf_ext_header);
memcpy(&type, &erf_ext_header, sizeof(type));
if (file_read(&erf_ext_header, sizeof(erf_ext_header),wth->fh) != sizeof(erf_ext_header)) {
*err = file_error(wth->fh, err_info);
return -1;
}
packet_size -= (guint32)sizeof(erf_ext_header);
memcpy(&type, &erf_ext_header, sizeof(type));
}
/* Read over MC or ETH subheader */
switch(header.type & 0x7F) {
case ERF_TYPE_MC_HDLC:
case ERF_TYPE_MC_RAW:
case ERF_TYPE_MC_ATM:
case ERF_TYPE_MC_RAW_CHANNEL:
case ERF_TYPE_MC_AAL5:
case ERF_TYPE_MC_AAL2:
case ERF_TYPE_COLOR_MC_HDLC_POS:
case ERF_TYPE_AAL2: /* not an MC type but has a similar 'AAL2 ext' header */
if (file_read(&mc_hdr,sizeof(mc_hdr),wth->fh) != sizeof(mc_hdr)) {
*err = file_error(wth->fh, err_info);
return -1;
}
packet_size -= (guint32)sizeof(mc_hdr);
break;
case ERF_TYPE_ETH:
case ERF_TYPE_COLOR_ETH:
case ERF_TYPE_DSM_COLOR_ETH:
if (file_read(&eth_hdr,sizeof(eth_hdr),wth->fh) != sizeof(eth_hdr)) {
*err = file_error(wth->fh, err_info);
return -1;
}
packet_size -= (guint32)sizeof(eth_hdr);
break;
default:
break;
case ERF_TYPE_MC_HDLC:
case ERF_TYPE_MC_RAW:
case ERF_TYPE_MC_ATM:
case ERF_TYPE_MC_RAW_CHANNEL:
case ERF_TYPE_MC_AAL5:
case ERF_TYPE_MC_AAL2:
case ERF_TYPE_COLOR_MC_HDLC_POS:
case ERF_TYPE_AAL2: /* not an MC type but has a similar 'AAL2 ext' header */
if (file_read(&mc_hdr,sizeof(mc_hdr),wth->fh) != sizeof(mc_hdr)) {
*err = file_error(wth->fh, err_info);
return -1;
}
packet_size -= (guint32)sizeof(mc_hdr);
break;
case ERF_TYPE_ETH:
case ERF_TYPE_COLOR_ETH:
case ERF_TYPE_DSM_COLOR_ETH:
if (file_read(&eth_hdr,sizeof(eth_hdr),wth->fh) != sizeof(eth_hdr)) {
*err = file_error(wth->fh, err_info);
return -1;
}
packet_size -= (guint32)sizeof(eth_hdr);
break;
default:
break;
}
/* The file_seek function do not return an error if the end of file
@ -244,11 +246,11 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
r = file_read(buffer, packet_size, wth->fh);
g_free(buffer);
if (r != packet_size) {
if (r != packet_size) {
/* ERF record too short, accept the file,
only if the very first records have been successfully checked */
only if the very first records have been successfully checked */
if (i < MIN_RECORDS_FOR_ERF_CHECK) {
return 0;
return 0;
}
}
@ -256,7 +258,7 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
} /* records_for_erf_check */
if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1) { /* rewind */
if (file_seek(wth->fh, 0L, SEEK_SET, err) == -1) { /* rewind */
return -1;
}
@ -264,7 +266,7 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
/* This is an ERF file */
wth->file_type = WTAP_FILE_ERF;
wth->snapshot_length = 0; /* not available in header, only in frame */
wth->snapshot_length = 0; /* not available in header, only in frame */
/*
* Use the encapsulation for ERF records.
@ -280,25 +282,25 @@ extern int erf_open(wtap *wth, int *err, gchar **err_info)
/* Read the next packet */
static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
gint64 *data_offset)
gint64 *data_offset)
{
erf_header_t erf_header;
guint32 packet_size, bytes_read;
guint32 packet_size, bytes_read;
*data_offset = wth->data_offset;
do {
if (!erf_read_header(wth->fh,
&wth->phdr, &wth->pseudo_header, &erf_header,
err, err_info, &bytes_read, &packet_size)) {
&wth->phdr, &wth->pseudo_header, &erf_header,
err, err_info, &bytes_read, &packet_size)) {
return FALSE;
}
wth->data_offset += bytes_read;
buffer_assure_space(wth->frame_buffer, packet_size);
wtap_file_read_expected_bytes(buffer_start_ptr(wth->frame_buffer),
(gint32)(packet_size), wth->fh, err, err_info);
(gint32)(packet_size), wth->fh, err, err_info);
wth->data_offset += packet_size;
} while ( erf_header.type == ERF_TYPE_PAD );
@ -307,18 +309,18 @@ static gboolean erf_read(wtap *wth, int *err, gchar **err_info,
}
static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
union wtap_pseudo_header *pseudo_header, guint8 *pd,
int length _U_, int *err, gchar **err_info)
union wtap_pseudo_header *pseudo_header, guint8 *pd,
int length _U_, int *err, gchar **err_info)
{
erf_header_t erf_header;
guint32 packet_size;
guint32 packet_size;
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
return FALSE;
do {
if (!erf_read_header(wth->random_fh, NULL, pseudo_header, &erf_header,
err, err_info, NULL, &packet_size))
err, err_info, NULL, &packet_size))
return FALSE;
} while ( erf_header.type == ERF_TYPE_PAD );
@ -329,21 +331,22 @@ static gboolean erf_seek_read(wtap *wth, gint64 seek_off,
}
static int erf_read_header(FILE_T fh,
struct wtap_pkthdr *phdr,
union wtap_pseudo_header *pseudo_header,
erf_header_t *erf_header,
int *err,
gchar **err_info,
guint32 *bytes_read,
guint32 *packet_size)
struct wtap_pkthdr *phdr,
union wtap_pseudo_header *pseudo_header,
erf_header_t *erf_header,
int *err,
gchar **err_info,
guint32 *bytes_read,
guint32 *packet_size)
{
guint32 mc_hdr;
guint8 erf_exhdr[8];
guint8 erf_exhdr[8];
guint64 erf_exhdr_sw;
guint8 type = 0;
guint8 type = 0;
guint16 eth_hdr;
guint32 skiplen=0;
int i = 0 , max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
guint32 skiplen = 0;
int i = 0;
int max = sizeof(pseudo_header->erf.ehdr_list)/sizeof(struct erf_ehdr);
wtap_file_read_expected_bytes(erf_header, sizeof(*erf_header), fh, err,
err_info);
@ -360,7 +363,7 @@ static int erf_read_header(FILE_T fh,
*/
*err = WTAP_ERR_BAD_FILE;
*err_info = g_strdup_printf("erf: File has %u-byte packet, bigger than maximum of %u",
*packet_size, WTAP_MAX_PACKET_SIZE);
*packet_size, WTAP_MAX_PACKET_SIZE);
return FALSE;
}
@ -379,7 +382,7 @@ static int erf_read_header(FILE_T fh,
phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
phdr->ts.secs = (long) (ts >> 32);
ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
ts = ((ts & 0xffffffff) * 1000 * 1000 * 1000);
ts += (ts & 0x80000000) << 1; /* rounding */
phdr->ts.nsecs = ((int) (ts >> 32));
if (phdr->ts.nsecs >= 1000000000) {
@ -400,84 +403,84 @@ static int erf_read_header(FILE_T fh,
/* Copy the ERF extension header into the pseudo header */
type = erf_header->type;
while (type & 0x80){
wtap_file_read_expected_bytes(&erf_exhdr, sizeof(erf_exhdr), fh, err,
err_info);
if (bytes_read != NULL)
*bytes_read += (guint32)sizeof(erf_exhdr);
*packet_size -= (guint32)sizeof(erf_exhdr);
skiplen += (guint32)sizeof(erf_exhdr);
erf_exhdr_sw = pntohll(erf_exhdr);
if (i < max)
memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
type = erf_exhdr[0];
i++;
wtap_file_read_expected_bytes(&erf_exhdr, sizeof(erf_exhdr), fh, err,
err_info);
if (bytes_read != NULL)
*bytes_read += (guint32)sizeof(erf_exhdr);
*packet_size -= (guint32)sizeof(erf_exhdr);
skiplen += (guint32)sizeof(erf_exhdr);
erf_exhdr_sw = pntohll(erf_exhdr);
if (i < max)
memcpy(&pseudo_header->erf.ehdr_list[i].ehdr, &erf_exhdr_sw, sizeof(erf_exhdr_sw));
type = erf_exhdr[0];
i++;
}
switch (erf_header->type & 0x7F) {
case ERF_TYPE_IPV4:
case ERF_TYPE_IPV6:
case ERF_TYPE_RAW_LINK:
case ERF_TYPE_INFINIBAND:
case ERF_TYPE_INFINIBAND_LINK:
/***
if (phdr != NULL) {
phdr->len = g_htons(erf_header->wlen);
phdr->caplen = g_htons(erf_header->wlen);
}
return TRUE;
***/
break;
case ERF_TYPE_PAD:
case ERF_TYPE_HDLC_POS:
case ERF_TYPE_COLOR_HDLC_POS:
case ERF_TYPE_DSM_COLOR_HDLC_POS:
case ERF_TYPE_ATM:
case ERF_TYPE_AAL5:
break;
case ERF_TYPE_IPV4:
case ERF_TYPE_IPV6:
case ERF_TYPE_RAW_LINK:
case ERF_TYPE_INFINIBAND:
case ERF_TYPE_INFINIBAND_LINK:
#if 0
if (phdr != NULL) {
phdr->len = g_htons(erf_header->wlen);
phdr->caplen = g_htons(erf_header->wlen);
}
return TRUE;
#endif
break;
case ERF_TYPE_PAD:
case ERF_TYPE_HDLC_POS:
case ERF_TYPE_COLOR_HDLC_POS:
case ERF_TYPE_DSM_COLOR_HDLC_POS:
case ERF_TYPE_ATM:
case ERF_TYPE_AAL5:
break;
case ERF_TYPE_ETH:
case ERF_TYPE_COLOR_ETH:
case ERF_TYPE_DSM_COLOR_ETH:
wtap_file_read_expected_bytes(&eth_hdr, sizeof(eth_hdr), fh, err,
err_info);
if (bytes_read != NULL)
*bytes_read += (guint32)sizeof(eth_hdr);
*packet_size -= (guint32)sizeof(eth_hdr);
skiplen += (guint32)sizeof(eth_hdr);
pseudo_header->erf.subhdr.eth_hdr = g_htons(eth_hdr);
break;
case ERF_TYPE_ETH:
case ERF_TYPE_COLOR_ETH:
case ERF_TYPE_DSM_COLOR_ETH:
wtap_file_read_expected_bytes(&eth_hdr, sizeof(eth_hdr), fh, err,
err_info);
if (bytes_read != NULL)
*bytes_read += (guint32)sizeof(eth_hdr);
*packet_size -= (guint32)sizeof(eth_hdr);
skiplen += (guint32)sizeof(eth_hdr);
pseudo_header->erf.subhdr.eth_hdr = g_htons(eth_hdr);
break;
case ERF_TYPE_MC_HDLC:
case ERF_TYPE_MC_RAW:
case ERF_TYPE_MC_ATM:
case ERF_TYPE_MC_RAW_CHANNEL:
case ERF_TYPE_MC_AAL5:
case ERF_TYPE_MC_AAL2:
case ERF_TYPE_COLOR_MC_HDLC_POS:
case ERF_TYPE_AAL2: /* not an MC type but has a similar 'AAL2 ext' header */
wtap_file_read_expected_bytes(&mc_hdr, sizeof(mc_hdr), fh, err,
err_info);
if (bytes_read != NULL)
*bytes_read += (guint32)sizeof(mc_hdr);
*packet_size -= (guint32)sizeof(mc_hdr);
skiplen += (guint32)sizeof(mc_hdr);
pseudo_header->erf.subhdr.mc_hdr = g_htonl(mc_hdr);
break;
case ERF_TYPE_MC_HDLC:
case ERF_TYPE_MC_RAW:
case ERF_TYPE_MC_ATM:
case ERF_TYPE_MC_RAW_CHANNEL:
case ERF_TYPE_MC_AAL5:
case ERF_TYPE_MC_AAL2:
case ERF_TYPE_COLOR_MC_HDLC_POS:
case ERF_TYPE_AAL2: /* not an MC type but has a similar 'AAL2 ext' header */
wtap_file_read_expected_bytes(&mc_hdr, sizeof(mc_hdr), fh, err,
err_info);
if (bytes_read != NULL)
*bytes_read += (guint32)sizeof(mc_hdr);
*packet_size -= (guint32)sizeof(mc_hdr);
skiplen += (guint32)sizeof(mc_hdr);
pseudo_header->erf.subhdr.mc_hdr = g_htonl(mc_hdr);
break;
case ERF_TYPE_IP_COUNTER:
case ERF_TYPE_TCP_FLOW_COUNTER:
/* unsupported, continue with default: */
default:
*err = WTAP_ERR_UNSUPPORTED_ENCAP;
*err_info = g_strdup_printf("erf: unknown record encapsulation %u",
erf_header->type);
return FALSE;
case ERF_TYPE_IP_COUNTER:
case ERF_TYPE_TCP_FLOW_COUNTER:
/* unsupported, continue with default: */
default:
*err = WTAP_ERR_UNSUPPORTED_ENCAP;
*err_info = g_strdup_printf("erf: unknown record encapsulation %u",
erf_header->type);
return FALSE;
}
if (phdr != NULL) {
phdr->len = g_htons(erf_header->wlen);
phdr->caplen = MIN( g_htons(erf_header->wlen),
g_htons(erf_header->rlen) - (guint32)sizeof(*erf_header) - skiplen );
g_htons(erf_header->rlen) - (guint32)sizeof(*erf_header) - skiplen );
}
if (*packet_size > WTAP_MAX_PACKET_SIZE) {
@ -510,9 +513,9 @@ static gboolean erf_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pse
guint8 erf_subhdr[((sizeof(struct erf_mc_hdr) > sizeof(struct erf_eth_hdr))?
sizeof(struct erf_mc_hdr) : sizeof(struct erf_eth_hdr))];
guint8 ehdr[8*MAX_ERF_EHDR];
size_t size = 0;
size_t size = 0;
size_t subhdr_size = 0;
int i = 0;
int i = 0;
switch(encap){
case WTAP_ENCAP_ERF:
@ -574,19 +577,19 @@ static gboolean erf_write_phdr(wtap_dumper *wdh, int encap, const union wtap_pse
}
static gboolean erf_dump(
wtap_dumper *wdh,
const struct wtap_pkthdr *phdr,
wtap_dumper *wdh,
const struct wtap_pkthdr *phdr,
const union wtap_pseudo_header *pseudo_header,
const guint8 *pd,
int *err)
const guint8 *pd,
int *err)
{
union wtap_pseudo_header other_phdr;
int encap;
gint64 alignbytes = 0;
int i;
int round_down = 0;
int encap;
gint64 alignbytes = 0;
int i;
int round_down = 0;
gboolean must_add_crc = FALSE;
guint32 crc32 = 0x00000000;
guint32 crc32 = 0x00000000;
if(wdh->encap == WTAP_ENCAP_PER_PACKET){
encap = phdr->pkt_encap;