2001-12-04 22:28:19 +00:00
|
|
|
/* visual.c
|
|
|
|
* File read and write routines for Visual Networks cap files.
|
|
|
|
* Copyright (c) 2001, Tom Nisbet tnisbet@visualnetworks.com
|
|
|
|
*
|
|
|
|
* Wiretap Library
|
|
|
|
* Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
|
2002-08-28 20:30:45 +00:00
|
|
|
*
|
2001-12-04 22:28:19 +00:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2002-08-28 20:30:45 +00:00
|
|
|
*
|
2001-12-04 22:28:19 +00:00
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
2002-08-28 20:30:45 +00:00
|
|
|
*
|
2001-12-04 22:28:19 +00:00
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
2012-06-28 22:56:06 +00:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2001-12-04 22:28:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
|
|
|
#include <errno.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "wtap-int.h"
|
|
|
|
#include "file_wrappers.h"
|
2014-07-15 23:40:46 +00:00
|
|
|
#include <wsutil/buffer.h>
|
2001-12-04 22:28:19 +00:00
|
|
|
#include "visual.h"
|
|
|
|
|
|
|
|
/*
|
2007-08-23 22:11:39 +00:00
|
|
|
* A Visual Networks traffic capture file contains three sections. The
|
2001-12-04 22:28:19 +00:00
|
|
|
* first is a 192 octet file header. This is followed by the captured
|
2007-08-23 22:11:39 +00:00
|
|
|
* 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.
|
2001-12-04 22:28:19 +00:00
|
|
|
* 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.
|
|
|
|
*
|
2013-11-08 17:17:57 +00:00
|
|
|
* All integer and time values are stored in little-endian format, except for
|
2007-08-23 22:11:39 +00:00
|
|
|
* the ATM Packet Header, which is stored in network byte order.
|
|
|
|
*
|
2013-11-08 17:17:57 +00:00
|
|
|
* [ File Header ]
|
|
|
|
*
|
2007-08-23 22:11:39 +00:00
|
|
|
*
|
|
|
|
* [ Packet Header 1 ] [(opt) ATM Packet Header] [ Data ]
|
|
|
|
* ...
|
|
|
|
* [ Packet Header n ] [(opt) ATM Packet Header] [ Data ]
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* [ Index Block 1 ] ... [ Index Block n ]
|
2001-12-04 22:28:19 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* 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'
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-08-23 22:11:39 +00:00
|
|
|
/* Visual File Header (minus magic number). */
|
|
|
|
/* This structure is used to extract information */
|
2001-12-04 22:28:19 +00:00
|
|
|
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 COMPATABILITY */
|
|
|
|
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
|
|
|
|
|
2007-08-23 22:11:39 +00:00
|
|
|
/* Visual Packet Header */
|
|
|
|
/* This structure is used to extract information */
|
2001-12-04 22:28:19 +00:00
|
|
|
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 */
|
|
|
|
};
|
|
|
|
|
2007-08-23 22:11:39 +00:00
|
|
|
/* Optional Visual ATM Packet Header */
|
|
|
|
/* This structure is used to extract information */
|
|
|
|
struct visual_atm_hdr
|
|
|
|
{
|
2013-11-08 17:17:57 +00:00
|
|
|
guint16 vpi; /* 4 bits of zeros; 12 bits of ATM VPI */
|
2007-08-23 22:11:39 +00:00
|
|
|
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
|
|
|
|
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
/* 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 */
|
|
|
|
double start_time; /* Capture start time in microseconds */
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/* Additional information for writing Visual files */
|
|
|
|
struct visual_write_info
|
|
|
|
{
|
2013-01-04 05:22:43 +00:00
|
|
|
guint start_time; /* Capture start time in seconds */
|
2001-12-04 22:28:19 +00:00
|
|
|
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 */
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean visual_read(wtap *wth, int *err, gchar **err_info,
|
2006-11-05 22:46:44 +00:00
|
|
|
gint64 *data_offset);
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean visual_seek_read(wtap *wth, gint64 seek_off,
|
2014-01-02 20:47:21 +00:00
|
|
|
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
|
2014-05-09 05:18:49 +00:00
|
|
|
static gboolean visual_read_packet(wtap *wth, FILE_T fh,
|
|
|
|
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info);
|
|
|
|
static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
|
2012-10-16 21:50:57 +00:00
|
|
|
const guint8 *pd, int *err);
|
2014-05-09 05:18:49 +00:00
|
|
|
static gboolean visual_dump_close(wtap_dumper *wdh, int *err);
|
|
|
|
static void visual_dump_free(wtap_dumper *wdh);
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
/* Open a file for reading */
|
2014-10-09 23:44:15 +00:00
|
|
|
wtap_open_return_val visual_open(wtap *wth, int *err, gchar **err_info)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
|
|
|
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 */
|
Add some higher-level file-read APIs and use them.
Add wtap_read_bytes(), which takes a FILE_T, a pointer, a byte count, an
error number pointer, and an error string pointer as arguments, and that
treats a short read of any sort, including a read that returns 0 bytes,
as a WTAP_ERR_SHORT_READ error, and that returns the error number and
string through its last two arguments.
Add wtap_read_bytes_or_eof(), which is similar, but that treats a read
that returns 0 bytes as an EOF, supplying an error number of 0 as an EOF
indication.
Use those in file readers; that simplifies the code and makes it less
likely that somebody will fail to supply the error number and error
string on a file read error.
Change-Id: Ia5dba2a6f81151e87b614461349d611cffc16210
Reviewed-on: https://code.wireshark.org/review/4512
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2014-10-07 01:00:57 +00:00
|
|
|
if (!wtap_read_bytes(wth->fh, magic, sizeof magic, err, err_info))
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
Add some higher-level file-read APIs and use them.
Add wtap_read_bytes(), which takes a FILE_T, a pointer, a byte count, an
error number pointer, and an error string pointer as arguments, and that
treats a short read of any sort, including a read that returns 0 bytes,
as a WTAP_ERR_SHORT_READ error, and that returns the error number and
string through its last two arguments.
Add wtap_read_bytes_or_eof(), which is similar, but that treats a read
that returns 0 bytes as an EOF, supplying an error number of 0 as an EOF
indication.
Use those in file readers; that simplifies the code and makes it less
likely that somebody will fail to supply the error number and error
string on a file read error.
Change-Id: Ia5dba2a6f81151e87b614461349d611cffc16210
Reviewed-on: https://code.wireshark.org/review/4512
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2014-10-07 01:00:57 +00:00
|
|
|
if (*err != WTAP_ERR_SHORT_READ)
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
|
|
|
return WTAP_OPEN_NOT_MINE;
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
|
|
|
if (memcmp(magic, visual_magic, sizeof visual_magic) != 0)
|
|
|
|
{
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_NOT_MINE;
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Read the rest of the file header. */
|
Add some higher-level file-read APIs and use them.
Add wtap_read_bytes(), which takes a FILE_T, a pointer, a byte count, an
error number pointer, and an error string pointer as arguments, and that
treats a short read of any sort, including a read that returns 0 bytes,
as a WTAP_ERR_SHORT_READ error, and that returns the error number and
string through its last two arguments.
Add wtap_read_bytes_or_eof(), which is similar, but that treats a read
that returns 0 bytes as an EOF, supplying an error number of 0 as an EOF
indication.
Use those in file readers; that simplifies the code and makes it less
likely that somebody will fail to supply the error number and error
string on a file read error.
Change-Id: Ia5dba2a6f81151e87b614461349d611cffc16210
Reviewed-on: https://code.wireshark.org/review/4512
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2014-10-07 01:00:57 +00:00
|
|
|
if (!wtap_read_bytes(wth->fh, &vfile_hdr, sizeof vfile_hdr, err, err_info))
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Verify the file version is known */
|
2013-12-03 20:35:50 +00:00
|
|
|
vfile_hdr.file_version = pletoh16(&vfile_hdr.file_version);
|
2001-12-04 22:28:19 +00:00
|
|
|
if (vfile_hdr.file_version != 1)
|
|
|
|
{
|
|
|
|
*err = WTAP_ERR_UNSUPPORTED;
|
Have the Wiretap open, read, and seek-and-read routines return, in
addition to an error code, an error info string, for
WTAP_ERR_UNSUPPORTED, WTAP_ERR_UNSUPPORTED_ENCAP, and
WTAP_ERR_BAD_RECORD errors. Replace the error messages logged with
"g_message()" for those errors with g_strdup()ed or g_strdup_printf()ed
strings returned as the error info string, and change the callers of
those routines to, for those errors, put the info string into the
printed message or alert box for the error.
Add messages for cases where those errors were returned without printing
an additional message.
Nobody uses the error code from "cf_read()" - "cf_read()" puts up the
alert box itself for failures; get rid of the error code, so it just
returns a success/failure indication.
Rename "file_read_error_message()" to "cf_read_error_message()", as it
handles read errors from Wiretap, and have it take an error info string
as an argument. (That handles a lot of the work of putting the info
string into the error message.)
Make some variables in "ascend-grammar.y" static.
Check the return value of "erf_read_header()" in "erf_seek_read()".
Get rid of an unused #define in "i4btrace.c".
svn path=/trunk/; revision=9852
2004-01-25 21:55:17 +00:00
|
|
|
*err_info = g_strdup_printf("visual: file version %u unsupported", vfile_hdr.file_version);
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
|
|
|
|
2007-11-25 00:23:37 +00:00
|
|
|
/* Translate the encapsulation type; these values are SNMP ifType
|
|
|
|
values, as found in http://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? */
|
2013-12-03 20:35:50 +00:00
|
|
|
switch (pletoh16(&vfile_hdr.media_type))
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
2007-11-25 00:23:37 +00:00
|
|
|
case 6: /* ethernet-csmacd */
|
2003-01-31 01:02:14 +00:00
|
|
|
encap = WTAP_ENCAP_ETHERNET;
|
|
|
|
break;
|
|
|
|
|
2007-11-25 00:23:37 +00:00
|
|
|
case 9: /* IEEE802.5 */
|
2003-01-31 01:02:14 +00:00
|
|
|
encap = WTAP_ENCAP_TOKEN_RING;
|
|
|
|
break;
|
|
|
|
|
2007-11-25 00:23:37 +00:00
|
|
|
case 16: /* lapb */
|
2003-01-31 01:02:14 +00:00
|
|
|
encap = WTAP_ENCAP_LAPB;
|
|
|
|
break;
|
|
|
|
|
2007-11-25 00:23:37 +00:00
|
|
|
case 22: /* propPointToPointSerial */
|
|
|
|
case 118: /* HDLC */
|
2003-10-25 07:17:28 +00:00
|
|
|
encap = WTAP_ENCAP_CHDLC_WITH_PHDR;
|
2003-01-31 01:02:14 +00:00
|
|
|
break;
|
|
|
|
|
2007-11-25 00:23:37 +00:00
|
|
|
case 32: /* frame-relay */
|
2003-01-31 01:02:14 +00:00
|
|
|
encap = WTAP_ENCAP_FRELAY_WITH_PHDR;
|
|
|
|
break;
|
|
|
|
|
2007-11-25 00:23:37 +00:00
|
|
|
case 37: /* ATM */
|
2007-08-23 22:11:39 +00:00
|
|
|
encap = WTAP_ENCAP_ATM_PDUS;
|
|
|
|
break;
|
|
|
|
|
2001-12-04 22:28:19 +00:00
|
|
|
default:
|
|
|
|
*err = WTAP_ERR_UNSUPPORTED_ENCAP;
|
Have the Wiretap open, read, and seek-and-read routines return, in
addition to an error code, an error info string, for
WTAP_ERR_UNSUPPORTED, WTAP_ERR_UNSUPPORTED_ENCAP, and
WTAP_ERR_BAD_RECORD errors. Replace the error messages logged with
"g_message()" for those errors with g_strdup()ed or g_strdup_printf()ed
strings returned as the error info string, and change the callers of
those routines to, for those errors, put the info string into the
printed message or alert box for the error.
Add messages for cases where those errors were returned without printing
an additional message.
Nobody uses the error code from "cf_read()" - "cf_read()" puts up the
alert box itself for failures; get rid of the error code, so it just
returns a success/failure indication.
Rename "file_read_error_message()" to "cf_read_error_message()", as it
handles read errors from Wiretap, and have it take an error info string
as an argument. (That handles a lot of the work of putting the info
string into the error message.)
Make some variables in "ascend-grammar.y" static.
Check the return value of "erf_read_header()" in "erf_seek_read()".
Get rid of an unused #define in "i4btrace.c".
svn path=/trunk/; revision=9852
2004-01-25 21:55:17 +00:00
|
|
|
*err_info = g_strdup_printf("visual: network type %u unknown or unsupported",
|
|
|
|
vfile_hdr.media_type);
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_ERROR;
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Fill in the wiretap struct with data from the file header */
|
2014-05-09 05:18:49 +00:00
|
|
|
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_VISUAL_NETWORKS;
|
|
|
|
wth->file_encap = encap;
|
|
|
|
wth->snapshot_length = pletoh16(&vfile_hdr.max_length);
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
/* Set up the pointers to the handlers for this file type */
|
2014-05-09 05:18:49 +00:00
|
|
|
wth->subtype_read = visual_read;
|
|
|
|
wth->subtype_seek_read = visual_seek_read;
|
2014-09-28 18:37:06 +00:00
|
|
|
wth->file_tsprec = WTAP_TSPREC_USEC;
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
/* Add Visual-specific information to the wiretap struct for later use. */
|
2010-02-26 07:59:54 +00:00
|
|
|
visual = (struct visual_read_info *)g_malloc(sizeof(struct visual_read_info));
|
2014-05-09 05:18:49 +00:00
|
|
|
wth->priv = (void *)visual;
|
2013-12-03 20:35:50 +00:00
|
|
|
visual->num_pkts = pletoh32(&vfile_hdr.num_pkts);
|
|
|
|
visual->start_time = ((double) pletoh32(&vfile_hdr.start_time)) * 1000000;
|
2001-12-04 22:28:19 +00:00
|
|
|
visual->current_pkt = 1;
|
|
|
|
|
2014-10-09 23:44:15 +00:00
|
|
|
return WTAP_OPEN_MINE;
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* 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. */
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean visual_read(wtap *wth, int *err, gchar **err_info,
|
2006-11-05 22:46:44 +00:00
|
|
|
gint64 *data_offset)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
2014-05-09 05:18:49 +00:00
|
|
|
struct visual_read_info *visual = (struct visual_read_info *)wth->priv;
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
/* 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. */
|
2002-08-28 20:30:45 +00:00
|
|
|
if (visual->current_pkt > visual->num_pkts)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
|
|
|
*err = 0; /* it's just an EOF, not an error */
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
|
|
|
visual->current_pkt++;
|
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
*data_offset = file_tell(wth->fh);
|
2007-08-23 22:11:39 +00:00
|
|
|
|
2014-05-23 10:50:02 +00:00
|
|
|
return visual_read_packet(wth, wth->fh, &wth->phdr, wth->frame_buffer,
|
|
|
|
err, err_info);
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 18:57:22 +00:00
|
|
|
/* Read packet header and data for random access. */
|
2014-05-23 10:50:02 +00:00
|
|
|
static gboolean visual_seek_read(wtap *wth, gint64 seek_off,
|
2014-05-09 05:18:49 +00:00
|
|
|
struct wtap_pkthdr *phdr, Buffer *buf, int *err, gchar **err_info)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
|
|
|
/* Seek to the packet header */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2001-12-04 22:28:19 +00:00
|
|
|
|
2013-06-17 21:18:47 +00:00
|
|
|
/* Read the packet. */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!visual_read_packet(wth, wth->random_fh, phdr, buf, err, err_info)) {
|
2013-06-17 21:18:47 +00:00
|
|
|
if (*err == 0)
|
|
|
|
*err = WTAP_ERR_SHORT_READ;
|
2014-05-23 10:50:02 +00:00
|
|
|
return FALSE;
|
2013-06-06 18:57:22 +00:00
|
|
|
}
|
2014-05-23 10:50:02 +00:00
|
|
|
return TRUE;
|
2003-10-01 07:11:49 +00:00
|
|
|
}
|
|
|
|
|
2013-06-06 18:57:22 +00:00
|
|
|
static gboolean
|
2014-05-09 05:18:49 +00:00
|
|
|
visual_read_packet(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
|
2013-06-17 21:18:47 +00:00
|
|
|
Buffer *buf, int *err, gchar **err_info)
|
2003-10-01 07:11:49 +00:00
|
|
|
{
|
2014-05-09 05:18:49 +00:00
|
|
|
struct visual_read_info *visual = (struct visual_read_info *)wth->priv;
|
2013-06-17 21:18:47 +00:00
|
|
|
struct visual_pkt_hdr vpkt_hdr;
|
2013-06-06 18:57:22 +00:00
|
|
|
guint32 packet_size;
|
|
|
|
struct visual_atm_hdr vatm_hdr;
|
|
|
|
double t;
|
|
|
|
time_t secs;
|
|
|
|
guint32 usecs;
|
2003-10-01 07:11:49 +00:00
|
|
|
guint32 packet_status;
|
2013-06-17 21:18:47 +00:00
|
|
|
guint8 *pd;
|
2003-10-01 07:11:49 +00:00
|
|
|
|
2013-06-06 18:57:22 +00:00
|
|
|
/* Read the packet header. */
|
Add some higher-level file-read APIs and use them.
Add wtap_read_bytes(), which takes a FILE_T, a pointer, a byte count, an
error number pointer, and an error string pointer as arguments, and that
treats a short read of any sort, including a read that returns 0 bytes,
as a WTAP_ERR_SHORT_READ error, and that returns the error number and
string through its last two arguments.
Add wtap_read_bytes_or_eof(), which is similar, but that treats a read
that returns 0 bytes as an EOF, supplying an error number of 0 as an EOF
indication.
Use those in file readers; that simplifies the code and makes it less
likely that somebody will fail to supply the error number and error
string on a file read error.
Change-Id: Ia5dba2a6f81151e87b614461349d611cffc16210
Reviewed-on: https://code.wireshark.org/review/4512
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2014-10-07 01:00:57 +00:00
|
|
|
if (!wtap_read_bytes_or_eof(fh, &vpkt_hdr, (unsigned int)sizeof vpkt_hdr, err, err_info))
|
2013-06-06 18:57:22 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Get the included length of data. This includes extra headers + payload */
|
2013-12-03 20:35:50 +00:00
|
|
|
packet_size = pletoh16(&vpkt_hdr.incl_len);
|
2013-06-06 18:57:22 +00:00
|
|
|
|
2014-05-24 18:28:30 +00:00
|
|
|
phdr->rec_type = REC_TYPE_PACKET;
|
2013-06-06 18:57:22 +00:00
|
|
|
phdr->presence_flags = WTAP_HAS_TS|WTAP_HAS_CAP_LEN;
|
|
|
|
|
|
|
|
/* Set the packet time and length. */
|
|
|
|
t = visual->start_time;
|
2013-12-03 20:35:50 +00:00
|
|
|
t += ((double)pletoh32(&vpkt_hdr.ts_delta))*1000;
|
2013-06-06 18:57:22 +00:00
|
|
|
secs = (time_t)(t/1000000);
|
|
|
|
usecs = (guint32)(t - secs*1000000);
|
|
|
|
phdr->ts.secs = secs;
|
|
|
|
phdr->ts.nsecs = usecs * 1000;
|
2013-11-08 17:17:57 +00:00
|
|
|
|
2013-12-03 20:35:50 +00:00
|
|
|
phdr->len = pletoh16(&vpkt_hdr.orig_len);
|
2013-06-06 18:57:22 +00:00
|
|
|
|
2013-12-03 20:35:50 +00:00
|
|
|
packet_status = pletoh32(&vpkt_hdr.status);
|
2013-06-06 18:57:22 +00:00
|
|
|
|
|
|
|
/* 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 pcap-ng 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
|
2001-12-04 22:28:19 +00:00
|
|
|
X.25 pseudo header. It would probably be better to move this up
|
|
|
|
into the phdr. */
|
2014-05-09 05:18:49 +00:00
|
|
|
switch (wth->file_encap)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
2003-10-01 07:11:49 +00:00
|
|
|
case WTAP_ENCAP_ETHERNET:
|
2013-06-06 18:57:22 +00:00
|
|
|
/* Ethernet has a 4-byte FCS. */
|
|
|
|
if (phdr->len < 4)
|
|
|
|
{
|
|
|
|
*err = WTAP_ERR_BAD_FILE;
|
|
|
|
*err_info = g_strdup_printf("visual: Ethernet packet has %u-byte original packet, less than the FCS length",
|
|
|
|
phdr->len);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
phdr->len -= 4;
|
2003-10-01 07:11:49 +00:00
|
|
|
|
2013-06-06 18:57:22 +00:00
|
|
|
/* XXX - the above implies that there's never an FCS; should this
|
|
|
|
set the FCS length to 0? */
|
|
|
|
phdr->pseudo_header.eth.fcs_len = -1;
|
|
|
|
break;
|
2013-11-08 17:17:57 +00:00
|
|
|
|
2003-10-25 07:17:28 +00:00
|
|
|
case WTAP_ENCAP_CHDLC_WITH_PHDR:
|
2013-06-06 18:57:22 +00:00
|
|
|
/* This has a 2-byte FCS. */
|
|
|
|
if (phdr->len < 2)
|
|
|
|
{
|
|
|
|
*err = WTAP_ERR_BAD_FILE;
|
|
|
|
*err_info = g_strdup_printf("visual: Cisco HDLC packet has %u-byte original packet, less than the FCS length",
|
|
|
|
phdr->len);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
phdr->len -= 2;
|
|
|
|
|
|
|
|
phdr->pseudo_header.p2p.sent = (packet_status & PS_SENT) ? TRUE : FALSE;
|
|
|
|
break;
|
|
|
|
|
2001-12-04 22:28:19 +00:00
|
|
|
case WTAP_ENCAP_PPP_WITH_PHDR:
|
2013-06-06 18:57:22 +00:00
|
|
|
/* No FCS.
|
|
|
|
XXX - true? Note that PPP can negotiate no FCS, a 2-byte FCS,
|
|
|
|
or a 4-byte FCS. */
|
|
|
|
phdr->pseudo_header.p2p.sent = (packet_status & PS_SENT) ? TRUE : FALSE;
|
2001-12-04 22:28:19 +00:00
|
|
|
break;
|
|
|
|
|
2003-01-31 01:02:14 +00:00
|
|
|
case WTAP_ENCAP_FRELAY_WITH_PHDR:
|
2013-06-06 18:57:22 +00:00
|
|
|
/* This has a 2-byte FCS. */
|
|
|
|
if (phdr->len < 2)
|
|
|
|
{
|
|
|
|
*err = WTAP_ERR_BAD_FILE;
|
|
|
|
*err_info = g_strdup_printf("visual: Frame Relay packet has %u-byte original packet, less than the FCS length",
|
|
|
|
phdr->len);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
phdr->len -= 2;
|
|
|
|
|
|
|
|
phdr->pseudo_header.x25.flags =
|
|
|
|
(packet_status & PS_SENT) ? 0x00 : FROM_DCE;
|
|
|
|
break;
|
|
|
|
|
2001-12-04 22:28:19 +00:00
|
|
|
case WTAP_ENCAP_LAPB:
|
2013-06-06 18:57:22 +00:00
|
|
|
/* This has a 2-byte FCS. */
|
|
|
|
if (phdr->len < 2)
|
|
|
|
{
|
|
|
|
*err = WTAP_ERR_BAD_FILE;
|
|
|
|
*err_info = g_strdup_printf("visual: Frame Relay packet has %u-byte original packet, less than the FCS length",
|
|
|
|
phdr->len);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
phdr->len -= 2;
|
|
|
|
|
|
|
|
phdr->pseudo_header.x25.flags =
|
2003-10-01 07:11:49 +00:00
|
|
|
(packet_status & PS_SENT) ? 0x00 : FROM_DCE;
|
2001-12-04 22:28:19 +00:00
|
|
|
break;
|
2007-08-23 22:11:39 +00:00
|
|
|
|
|
|
|
case WTAP_ENCAP_ATM_PDUS:
|
2013-06-06 18:57:22 +00:00
|
|
|
/* 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. */
|
Add some higher-level file-read APIs and use them.
Add wtap_read_bytes(), which takes a FILE_T, a pointer, a byte count, an
error number pointer, and an error string pointer as arguments, and that
treats a short read of any sort, including a read that returns 0 bytes,
as a WTAP_ERR_SHORT_READ error, and that returns the error number and
string through its last two arguments.
Add wtap_read_bytes_or_eof(), which is similar, but that treats a read
that returns 0 bytes as an EOF, supplying an error number of 0 as an EOF
indication.
Use those in file readers; that simplifies the code and makes it less
likely that somebody will fail to supply the error number and error
string on a file read error.
Change-Id: Ia5dba2a6f81151e87b614461349d611cffc16210
Reviewed-on: https://code.wireshark.org/review/4512
Reviewed-by: Guy Harris <guy@alum.mit.edu>
2014-10-07 01:00:57 +00:00
|
|
|
if (!wtap_read_bytes(fh, &vatm_hdr, (unsigned int)sizeof vatm_hdr, err, err_info))
|
2013-06-06 18:57:22 +00:00
|
|
|
{
|
|
|
|
return FALSE;
|
|
|
|
}
|
2013-11-08 17:17:57 +00:00
|
|
|
|
|
|
|
/* Remove ATM header from length of included bytes in capture, as
|
2013-06-06 18:57:22 +00:00
|
|
|
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 */
|
|
|
|
phdr->pseudo_header.atm.type = TRAF_UNKNOWN;
|
|
|
|
phdr->pseudo_header.atm.subtype = TRAF_ST_UNKNOWN;
|
|
|
|
phdr->pseudo_header.atm.aal5t_len = 0;
|
|
|
|
|
|
|
|
/* Next two items not supported. Defaulting to zero */
|
|
|
|
phdr->pseudo_header.atm.aal5t_u2u = 0;
|
|
|
|
phdr->pseudo_header.atm.aal5t_chksum = 0;
|
2013-11-08 17:17:57 +00:00
|
|
|
|
2013-06-06 18:57:22 +00:00
|
|
|
/* Flags appear only to convey that packet is a raw cell. Set to 0 */
|
2013-11-08 17:17:57 +00:00
|
|
|
phdr->pseudo_header.atm.flags = 0;
|
|
|
|
|
2013-06-06 18:57:22 +00:00
|
|
|
/* Not supported. Defaulting to zero */
|
|
|
|
phdr->pseudo_header.atm.aal2_cid = 0;
|
|
|
|
|
|
|
|
switch(vatm_hdr.category & VN_CAT_TYPE_MASK )
|
|
|
|
{
|
|
|
|
case VN_AAL1:
|
|
|
|
phdr->pseudo_header.atm.aal = AAL_1;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VN_AAL2:
|
|
|
|
phdr->pseudo_header.atm.aal = AAL_2;
|
|
|
|
break;
|
2013-11-08 17:17:57 +00:00
|
|
|
|
2013-06-06 18:57:22 +00:00
|
|
|
case VN_AAL34:
|
|
|
|
phdr->pseudo_header.atm.aal = AAL_3_4;
|
|
|
|
break;
|
2013-11-08 17:17:57 +00:00
|
|
|
|
2013-06-06 18:57:22 +00:00
|
|
|
case VN_AAL5:
|
|
|
|
phdr->pseudo_header.atm.aal = AAL_5;
|
|
|
|
phdr->pseudo_header.atm.type = TRAF_LLCMX;
|
2013-12-03 20:35:50 +00:00
|
|
|
phdr->pseudo_header.atm.aal5t_len = pntoh32(&vatm_hdr.data_length);
|
2013-06-06 18:57:22 +00:00
|
|
|
break;
|
2013-11-08 17:17:57 +00:00
|
|
|
|
2013-06-06 18:57:22 +00:00
|
|
|
case VN_OAM:
|
|
|
|
/* Marking next 3 as OAM versus unknown */
|
|
|
|
case VN_O191:
|
|
|
|
case VN_IDLE:
|
|
|
|
case VN_RM:
|
|
|
|
phdr->pseudo_header.atm.aal = AAL_OAMCELL;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case VN_UNKNOWN:
|
|
|
|
default:
|
|
|
|
phdr->pseudo_header.atm.aal = AAL_UNKNOWN;
|
|
|
|
break;
|
|
|
|
}
|
2013-12-03 20:35:50 +00:00
|
|
|
phdr->pseudo_header.atm.vpi = pntoh16(&vatm_hdr.vpi) & 0x0FFF;
|
|
|
|
phdr->pseudo_header.atm.vci = pntoh16(&vatm_hdr.vci);
|
|
|
|
phdr->pseudo_header.atm.cells = pntoh16(&vatm_hdr.cell_count);
|
2013-11-08 17:17:57 +00:00
|
|
|
|
2013-06-06 18:57:22 +00:00
|
|
|
/* Using bit value of 1 (DCE -> DTE) to indicate From Network */
|
|
|
|
phdr->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;
|
|
|
|
}
|
|
|
|
|
|
|
|
phdr->caplen = packet_size;
|
|
|
|
|
|
|
|
/* Check for too-large packet. */
|
|
|
|
if (packet_size > WTAP_MAX_PACKET_SIZE)
|
|
|
|
{
|
|
|
|
/* 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 = g_strdup_printf("visual: File has %u-byte packet, bigger than maximum of %u",
|
|
|
|
packet_size, WTAP_MAX_PACKET_SIZE);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2013-06-17 21:18:47 +00:00
|
|
|
/* Read the packet data */
|
|
|
|
if (!wtap_read_packet_bytes(fh, buf, packet_size, err, err_info))
|
|
|
|
return FALSE;
|
2013-06-16 00:20:00 +00:00
|
|
|
|
2014-05-09 05:18:49 +00:00
|
|
|
if (wth->file_encap == WTAP_ENCAP_CHDLC_WITH_PHDR)
|
2013-06-06 18:57:22 +00:00
|
|
|
{
|
2013-06-17 21:18:47 +00:00
|
|
|
/* 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. */
|
2014-08-02 11:00:48 +00:00
|
|
|
pd = ws_buffer_start_ptr(buf);
|
2013-06-17 21:18:47 +00:00
|
|
|
|
|
|
|
/* If PPP is specified in the encap hint, then use that */
|
|
|
|
if (vpkt_hdr.encap_hint == 14)
|
2013-06-06 18:57:22 +00:00
|
|
|
{
|
2013-06-17 21:18:47 +00:00
|
|
|
/* 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 */
|
|
|
|
phdr->pkt_encap = WTAP_ENCAP_ATM_RFC1483;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* It is actually PPP */
|
|
|
|
phdr->pkt_encap = WTAP_ENCAP_PPP_WITH_PHDR;
|
|
|
|
}
|
2013-06-06 18:57:22 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-17 21:18:47 +00:00
|
|
|
/* 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 */
|
|
|
|
phdr->pkt_encap = WTAP_ENCAP_PPP_WITH_PHDR;
|
|
|
|
}
|
2013-06-06 18:57:22 +00:00
|
|
|
}
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
2013-06-17 21:18:47 +00:00
|
|
|
|
|
|
|
return TRUE;
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
|
|
|
|
2002-08-28 20:30:45 +00:00
|
|
|
/* Check for media types that may be written in Visual file format.
|
2001-12-04 22:28:19 +00:00
|
|
|
Returns 0 if the specified encapsulation type is supported,
|
|
|
|
an error indication otherwise. */
|
2002-02-27 08:57:25 +00:00
|
|
|
int visual_dump_can_write_encap(int encap)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
|
|
|
/* 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:
|
2003-10-25 07:17:28 +00:00
|
|
|
case WTAP_ENCAP_CHDLC_WITH_PHDR:
|
2003-01-31 01:02:14 +00:00
|
|
|
case WTAP_ENCAP_FRELAY_WITH_PHDR:
|
2001-12-04 22:28:19 +00:00
|
|
|
case WTAP_ENCAP_PPP:
|
|
|
|
case WTAP_ENCAP_PPP_WITH_PHDR:
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return WTAP_ERR_UNSUPPORTED_ENCAP;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Open a file for writing.
|
|
|
|
Returns TRUE on success, FALSE on failure; sets "*err" to an
|
|
|
|
error code on failure */
|
2014-05-09 05:18:49 +00:00
|
|
|
gboolean visual_dump_open(wtap_dumper *wdh, int *err)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
|
|
|
struct visual_write_info *visual;
|
|
|
|
|
|
|
|
/* Set the write routines for a visual file. */
|
|
|
|
wdh->subtype_write = visual_dump;
|
|
|
|
wdh->subtype_close = visual_dump_close;
|
|
|
|
|
|
|
|
/* Create a struct to hold file information for the duration
|
|
|
|
of the write */
|
2010-02-26 07:59:54 +00:00
|
|
|
visual = (struct visual_write_info *)g_malloc(sizeof(struct visual_write_info));
|
|
|
|
wdh->priv = (void *)visual;
|
2001-12-04 22:28:19 +00:00
|
|
|
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. */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (wtap_dump_file_seek(wdh, CAPTUREFILE_HEADER_SIZE, SEEK_SET, err) == -1)
|
2002-03-04 00:25:35 +00:00
|
|
|
return FALSE;
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Write a packet to a Visual dump file.
|
|
|
|
Returns TRUE on success, FALSE on failure. */
|
2014-05-09 05:18:49 +00:00
|
|
|
static gboolean visual_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
|
2012-10-16 21:50:57 +00:00
|
|
|
const guint8 *pd, int *err)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
2012-10-16 21:50:57 +00:00
|
|
|
const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
|
2012-06-02 15:32:34 +00:00
|
|
|
struct visual_write_info * visual = (struct visual_write_info *)wdh->priv;
|
2001-12-04 22:28:19 +00:00
|
|
|
struct visual_pkt_hdr vpkt_hdr;
|
|
|
|
size_t hdr_size = sizeof vpkt_hdr;
|
2013-01-04 05:22:43 +00:00
|
|
|
guint delta_msec;
|
2001-12-04 22:28:19 +00:00
|
|
|
guint32 packet_status;
|
|
|
|
|
2014-05-24 18:28:30 +00:00
|
|
|
/* We can only write packet records. */
|
|
|
|
if (phdr->rec_type != REC_TYPE_PACKET) {
|
|
|
|
*err = WTAP_ERR_REC_TYPE_UNSUPPORTED;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2014-01-22 00:26:36 +00:00
|
|
|
/* Don't write anything we're not willing to read. */
|
|
|
|
if (phdr->caplen > WTAP_MAX_PACKET_SIZE) {
|
|
|
|
*err = WTAP_ERR_PACKET_TOO_LARGE;
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
2001-12-04 22:28:19 +00:00
|
|
|
/* If the visual structure was never allocated then nothing useful
|
|
|
|
can be done. */
|
|
|
|
if (visual == 0)
|
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Zero out unused and reserved fields in the packet header. */
|
|
|
|
memset(&vpkt_hdr, 0, hdr_size);
|
|
|
|
|
|
|
|
/* 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. */
|
2002-08-28 20:30:45 +00:00
|
|
|
if (visual->index_table_index == 0)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
|
|
|
/* This is the first packet. Save its start time as the file time. */
|
2007-03-21 01:32:50 +00:00
|
|
|
visual->start_time = (guint32) phdr->ts.secs;
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
/* Initialize the index table */
|
2012-06-02 15:32:34 +00:00
|
|
|
visual->index_table = (guint32 *)g_malloc(1024 * sizeof *visual->index_table);
|
2001-12-04 22:28:19 +00:00
|
|
|
visual->index_table_size = 1024;
|
|
|
|
}
|
2002-08-28 20:30:45 +00:00
|
|
|
|
2001-12-04 22:28:19 +00:00
|
|
|
/* Calculate milliseconds since capture start. */
|
2005-08-24 21:31:56 +00:00
|
|
|
delta_msec = phdr->ts.nsecs / 1000000;
|
2007-03-21 01:32:50 +00:00
|
|
|
delta_msec += ( (guint32) phdr->ts.secs - visual->start_time) * 1000;
|
2013-11-29 18:44:00 +00:00
|
|
|
vpkt_hdr.ts_delta = GUINT32_TO_LE(delta_msec);
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
/* Fill in the length fields. */
|
2013-11-29 18:44:00 +00:00
|
|
|
vpkt_hdr.orig_len = GUINT16_TO_LE(phdr->len);
|
|
|
|
vpkt_hdr.incl_len = GUINT16_TO_LE(phdr->caplen);
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
/* Fill in the encapsulation hint for the file's media type. */
|
|
|
|
switch (wdh->encap)
|
|
|
|
{
|
|
|
|
case WTAP_ENCAP_ETHERNET: /* Ethernet */
|
2002-08-28 20:30:45 +00:00
|
|
|
vpkt_hdr.encap_hint = 2;
|
2001-12-04 22:28:19 +00:00
|
|
|
break;
|
|
|
|
case WTAP_ENCAP_TOKEN_RING: /* Token Ring */
|
2002-08-28 20:30:45 +00:00
|
|
|
vpkt_hdr.encap_hint = 3;
|
2001-12-04 22:28:19 +00:00
|
|
|
break;
|
|
|
|
case WTAP_ENCAP_PPP: /* PPP */
|
|
|
|
case WTAP_ENCAP_PPP_WITH_PHDR:
|
2002-08-28 20:30:45 +00:00
|
|
|
vpkt_hdr.encap_hint = 14;
|
2001-12-04 22:28:19 +00:00
|
|
|
break;
|
2003-10-25 07:17:28 +00:00
|
|
|
case WTAP_ENCAP_CHDLC_WITH_PHDR: /* HDLC Router */
|
2002-08-28 20:30:45 +00:00
|
|
|
vpkt_hdr.encap_hint = 13;
|
2001-12-04 22:28:19 +00:00
|
|
|
break;
|
2003-01-31 01:02:14 +00:00
|
|
|
case WTAP_ENCAP_FRELAY_WITH_PHDR: /* Frame Relay Auto-detect */
|
2002-08-28 20:30:45 +00:00
|
|
|
vpkt_hdr.encap_hint = 12;
|
2001-12-04 22:28:19 +00:00
|
|
|
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->encap)
|
|
|
|
{
|
2003-10-25 07:17:28 +00:00
|
|
|
case WTAP_ENCAP_CHDLC_WITH_PHDR:
|
2001-12-04 22:28:19 +00:00
|
|
|
packet_status |= (pseudo_header->p2p.sent ? PS_SENT : 0x00);
|
|
|
|
break;
|
|
|
|
|
2003-01-31 01:02:14 +00:00
|
|
|
case WTAP_ENCAP_FRELAY_WITH_PHDR:
|
2001-12-04 22:28:19 +00:00
|
|
|
case WTAP_ENCAP_LAPB:
|
2002-04-09 08:15:04 +00:00
|
|
|
packet_status |=
|
|
|
|
((pseudo_header->x25.flags & FROM_DCE) ? 0x00 : PS_SENT);
|
2001-12-04 22:28:19 +00:00
|
|
|
break;
|
|
|
|
}
|
2013-11-29 18:44:00 +00:00
|
|
|
vpkt_hdr.status = GUINT32_TO_LE(packet_status);
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
/* Write the packet header. */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, &vpkt_hdr, hdr_size, err))
|
2001-12-04 22:28:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Write the packet data */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
|
2001-12-04 22:28:19 +00:00
|
|
|
return FALSE;
|
|
|
|
|
|
|
|
/* Store the frame offset in the index table. */
|
2002-08-28 20:30:45 +00:00
|
|
|
if (visual->index_table_index >= visual->index_table_size)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
|
|
|
/* End of table reached. Reallocate with a larger size */
|
|
|
|
visual->index_table_size *= 2;
|
2012-06-02 15:32:34 +00:00
|
|
|
visual->index_table = (guint32 *)g_realloc(visual->index_table,
|
2001-12-04 22:28:19 +00:00
|
|
|
visual->index_table_size * sizeof *visual->index_table);
|
|
|
|
}
|
2013-11-29 18:44:00 +00:00
|
|
|
visual->index_table[visual->index_table_index] = GUINT32_TO_LE(visual->next_offset);
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
/* Update the table index and offset for the next frame. */
|
|
|
|
visual->index_table_index++;
|
2009-03-09 22:37:55 +00:00
|
|
|
visual->next_offset += (guint32) hdr_size + phdr->caplen;
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/* Finish writing to a dump file.
|
|
|
|
Returns TRUE on success, FALSE on failure. */
|
2014-05-09 05:18:49 +00:00
|
|
|
static gboolean visual_dump_close(wtap_dumper *wdh, int *err)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
2012-06-02 15:32:34 +00:00
|
|
|
struct visual_write_info * visual = (struct visual_write_info *)wdh->priv;
|
2001-12-04 22:28:19 +00:00
|
|
|
size_t n_to_write;
|
|
|
|
struct visual_file_hdr vfile_hdr;
|
|
|
|
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;
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, visual->index_table, n_to_write, err))
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
|
|
|
visual_dump_free(wdh);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the magic number at the start of the file. */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (wtap_dump_file_seek(wdh, 0, SEEK_SET, err) == -1)
|
|
|
|
return FALSE;
|
2001-12-04 22:28:19 +00:00
|
|
|
magicp = visual_magic;
|
|
|
|
magic_size = sizeof visual_magic;
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, magicp, magic_size, err))
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
|
|
|
visual_dump_free(wdh);
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize the file header with zeroes for the reserved fields. */
|
|
|
|
memset(&vfile_hdr, '\0', sizeof vfile_hdr);
|
2013-11-29 18:44:00 +00:00
|
|
|
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);
|
2008-05-08 05:16:33 +00:00
|
|
|
g_strlcpy(vfile_hdr.description, "Wireshark file", 64);
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
/* Translate the encapsulation type */
|
|
|
|
switch (wdh->encap)
|
|
|
|
{
|
2003-01-31 01:02:14 +00:00
|
|
|
case WTAP_ENCAP_ETHERNET:
|
2013-11-29 18:44:00 +00:00
|
|
|
vfile_hdr.media_type = GUINT16_TO_LE(6);
|
2003-01-31 01:02:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WTAP_ENCAP_TOKEN_RING:
|
2013-11-29 18:44:00 +00:00
|
|
|
vfile_hdr.media_type = GUINT16_TO_LE(9);
|
2003-01-31 01:02:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WTAP_ENCAP_LAPB:
|
2013-11-29 18:44:00 +00:00
|
|
|
vfile_hdr.media_type = GUINT16_TO_LE(16);
|
2003-01-31 01:02:14 +00:00
|
|
|
break;
|
|
|
|
|
2001-12-04 22:28:19 +00:00
|
|
|
case WTAP_ENCAP_PPP: /* PPP is differentiated from CHDLC in PktHdr */
|
|
|
|
case WTAP_ENCAP_PPP_WITH_PHDR:
|
2003-10-25 07:17:28 +00:00
|
|
|
case WTAP_ENCAP_CHDLC_WITH_PHDR:
|
2013-11-29 18:44:00 +00:00
|
|
|
vfile_hdr.media_type = GUINT16_TO_LE(22);
|
2003-01-31 01:02:14 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case WTAP_ENCAP_FRELAY_WITH_PHDR:
|
2013-11-29 18:44:00 +00:00
|
|
|
vfile_hdr.media_type = GUINT16_TO_LE(32);
|
2003-01-31 01:02:14 +00:00
|
|
|
break;
|
2001-12-04 22:28:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Write the file header following the magic bytes. */
|
2014-05-09 05:18:49 +00:00
|
|
|
if (!wtap_dump_file_write(wdh, &vfile_hdr, sizeof vfile_hdr, err))
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
|
|
|
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. */
|
2014-05-09 05:18:49 +00:00
|
|
|
static void visual_dump_free(wtap_dumper *wdh)
|
2001-12-04 22:28:19 +00:00
|
|
|
{
|
2012-06-02 15:32:34 +00:00
|
|
|
struct visual_write_info * visual = (struct visual_write_info *)wdh->priv;
|
2001-12-04 22:28:19 +00:00
|
|
|
|
|
|
|
if (visual)
|
|
|
|
{
|
|
|
|
/* Free the index table memory. */
|
|
|
|
if (visual->index_table)
|
|
|
|
g_free(visual->index_table);
|
|
|
|
}
|
|
|
|
}
|