/* eyesdn.c
- *
- * $Id$
*
* Wiretap Library
* Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
#include "config.h"
#include "wtap-int.h"
-#include "buffer.h"
#include "eyesdn.h"
#include "file_wrappers.h"
-#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
*/
-static int esc_read(guint8 *buf, int len, FILE_T fh)
+static gboolean esc_read(FILE_T fh, guint8 *buf, int len, int *err, gchar **err_info)
{
int i;
int value;
for(i=0; i<len; i++) {
value=file_getc(fh);
- if(value==-1)
- return -2; /* EOF or error */
- if(value==0xff)
- return -1; /* error !!, read into next frame */
+ if(value==-1) {
+ /* EOF or error */
+ *err=file_error(fh, err_info);
+ if(*err==0)
+ *err=WTAP_ERR_SHORT_READ;
+ return FALSE;
+ }
+ if(value==0xff) {
+ /* error !!, read into next frame */
+ *err=WTAP_ERR_BAD_FILE;
+ *err_info=g_strdup("eyesdn: No flag character seen in frame");
+ return FALSE;
+ }
if(value==0xfe) {
/* we need to escape */
value=file_getc(fh);
- if(value==-1)
- return -2;
+ if(value==-1) {
+ /* EOF or error */
+ *err=file_error(fh, err_info);
+ if(*err==0)
+ *err=WTAP_ERR_SHORT_READ;
+ return FALSE;
+ }
value+=2;
}
buf[i]=value;
}
- return i;
+ return TRUE;
}
/* Magic text to check for eyesdn-ness of file */
static const unsigned char eyesdn_hdr_magic[] =
{ 'E', 'y', 'e', 'S', 'D', 'N'};
-#define EYESDN_HDR_MAGIC_SIZE (sizeof(eyesdn_hdr_magic) / sizeof(eyesdn_hdr_magic[0]))
+#define EYESDN_HDR_MAGIC_SIZE sizeof(eyesdn_hdr_magic)
/* Size of a record header */
#define EYESDN_HDR_LENGTH 12
return -1;
}
-int eyesdn_open(wtap *wth, int *err, gchar **err_info)
+wtap_open_return_val eyesdn_open(wtap *wth, int *err, gchar **err_info)
{
- int bytes_read;
char magic[EYESDN_HDR_MAGIC_SIZE];
/* Look for eyesdn header */
- errno = WTAP_ERR_CANT_READ;
- bytes_read = file_read(&magic, sizeof magic, wth->fh);
- if (bytes_read != sizeof magic) {
- *err = file_error(wth->fh, err_info);
- if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
- return -1;
- return 0;
+ if (!wtap_read_bytes(wth->fh, &magic, sizeof magic, err, err_info)) {
+ if (*err != WTAP_ERR_SHORT_READ)
+ return WTAP_OPEN_ERROR;
+ return WTAP_OPEN_NOT_MINE;
}
if (memcmp(magic, eyesdn_hdr_magic, EYESDN_HDR_MAGIC_SIZE) != 0)
- return 0;
+ return WTAP_OPEN_NOT_MINE;
wth->file_encap = WTAP_ENCAP_PER_PACKET;
wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_EYESDN;
wth->snapshot_length = 0; /* not known */
wth->subtype_read = eyesdn_read;
wth->subtype_seek_read = eyesdn_seek_read;
- wth->tsprecision = WTAP_FILE_TSPREC_USEC;
+ wth->file_tsprec = WTAP_TSPREC_USEC;
- return 1;
+ return WTAP_OPEN_MINE;
}
/* Find the next packet and parse it; called from wtap_read(). */
int usecs;
int pkt_len;
guint8 channel, direction;
- int bytes_read;
- guint8 *pd;
+ guint8 *pd;
/* Our file pointer should be at the summary information header
* for a packet. Read in that header and extract the useful
* information.
*/
- if (esc_read(hdr, EYESDN_HDR_LENGTH, fh) != EYESDN_HDR_LENGTH) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!esc_read(fh, hdr, EYESDN_HDR_LENGTH, err, err_info))
return FALSE;
- }
- /* extract information from header */
- usecs = pntoh24(&hdr[0]);
+ /* extract information from header */
+ usecs = pntoh24(&hdr[0]);
#ifdef TV64BITS
- secs = hdr[3];
+ secs = hdr[3];
#else
- secs = 0;
+ secs = 0;
#endif
- secs = (secs << 8) | hdr[4];
- secs = (secs << 8) | hdr[5];
- secs = (secs << 8) | hdr[6];
- secs = (secs << 8) | hdr[7];
+ secs = (secs << 8) | hdr[4];
+ secs = (secs << 8) | hdr[5];
+ secs = (secs << 8) | hdr[6];
+ secs = (secs << 8) | hdr[7];
- channel = hdr[8];
- direction = hdr[9];
- pkt_len = pntoh16(&hdr[10]);
+ channel = hdr[8];
+ direction = hdr[9];
+ pkt_len = pntoh16(&hdr[10]);
switch(direction >> 1) {
}
cur_off = file_tell(fh);
- if (esc_read(cell, CELL_LEN, fh) != CELL_LEN) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
+ if (!esc_read(fh, cell, CELL_LEN, err, err_info))
return FALSE;
- }
if (file_seek(fh, cur_off, SEEK_SET, err) == -1)
return FALSE;
phdr->pkt_encap = WTAP_ENCAP_ATM_PDUS_UNTRUNCATED;
return FALSE;
}
+ phdr->rec_type = REC_TYPE_PACKET;
phdr->presence_flags = WTAP_HAS_TS;
phdr->ts.secs = secs;
phdr->ts.nsecs = usecs * 1000;
phdr->len = pkt_len;
/* Make sure we have enough room for the packet */
- buffer_assure_space(buf, EYESDN_MAX_PACKET_LEN);
-
- errno = WTAP_ERR_CANT_READ;
- pd = buffer_start_ptr(buf);
- bytes_read = esc_read(pd, pkt_len, fh);
- if (bytes_read != pkt_len) {
- if (bytes_read == -2) {
- *err = file_error(fh, err_info);
- if (*err == 0)
- *err = WTAP_ERR_SHORT_READ;
- } else if (bytes_read == -1) {
- *err = WTAP_ERR_BAD_FILE;
- *err_info = g_strdup("eyesdn: No flag character seen in frame");
- } else
- *err = WTAP_ERR_SHORT_READ;
+ ws_buffer_assure_space(buf, EYESDN_MAX_PACKET_LEN);
+
+ pd = ws_buffer_start_ptr(buf);
+ if (!esc_read(fh, pd, pkt_len, err, err_info))
return FALSE;
- }
return TRUE;
}
static gboolean eyesdn_dump(wtap_dumper *wdh,
const struct wtap_pkthdr *phdr,
- const guint8 *pd, int *err);
+ const guint8 *pd, int *err, gchar **err_info);
gboolean eyesdn_dump_open(wtap_dumper *wdh, int *err)
{
wdh->subtype_write=eyesdn_dump;
- wdh->subtype_close=NULL;
if (!wtap_dump_file_write(wdh, eyesdn_hdr_magic,
EYESDN_HDR_MAGIC_SIZE, err))
return 0;
default:
- return WTAP_ERR_UNSUPPORTED_ENCAP;
+ return WTAP_ERR_UNWRITABLE_ENCAP;
}
}
* Returns TRUE on success, FALSE on failure. */
static gboolean eyesdn_dump(wtap_dumper *wdh,
const struct wtap_pkthdr *phdr,
- const guint8 *pd, int *err)
+ const guint8 *pd, int *err, gchar **err_info _U_)
{
static const guint8 start_flag = 0xff;
const union wtap_pseudo_header *pseudo_header = &phdr->pseudo_header;
int protocol;
int size;
+ /* We can only write packet records. */
+ if (phdr->rec_type != REC_TYPE_PACKET) {
+ *err = WTAP_ERR_UNWRITABLE_REC_TYPE;
+ return FALSE;
+ }
+
+ /* Don't write out anything bigger than we can read.
+ * (The length field in packet headers is 16 bits, which
+ * imposes a hard limit.) */
+ if (phdr->caplen > 65535) {
+ *err = WTAP_ERR_PACKET_TOO_LARGE;
+ return FALSE;
+ }
+
usecs=phdr->ts.nsecs/1000;
secs=phdr->ts.secs;
size=phdr->caplen;
break;
default:
- *err=WTAP_ERR_UNSUPPORTED_ENCAP;
+ *err=WTAP_ERR_UNWRITABLE_ENCAP;
return FALSE;
}
return FALSE;
return TRUE;
}
+
+/*
+ * Editor modelines - http://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:
+ */