3 * $Id: ngsniffer.c,v 1.2 1998/11/12 06:01:24 gram Exp $
6 * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 #include "ngsniffer.h"
26 /* Returns WTAP_FILE_NGSNIFFER on success, WTAP_FILE_UNKNOWN on failure */
27 int ngsniffer_open(wtap *wth)
32 fseek(wth->fh, 0, SEEK_SET);
33 bytes_read = fread(magic, 1, 32, wth->fh);
35 if (bytes_read != 32) {
36 return WTAP_FILE_UNKNOWN;
41 if (strcmp(magic, "TRSNIFF data ")) {
42 return WTAP_FILE_UNKNOWN;
45 /* This is a ngsniffer file */
46 wth->frame_number = 0;
47 wth->file_byte_offset = 0x10b;
48 wth->subtype_read = ngsniffer_read;
50 /* I think this is link type */
51 if (magic[30] == 0x25) {
52 wth->encapsulation = WTAP_ENCAP_ETHERNET;
54 else if (magic[30] == 0x24) {
55 wth->encapsulation = WTAP_ENCAP_TR;
58 g_error("The magic byte that I think tells DLT is 0x%02X\n", magic[30]);
62 if (fseek(wth->fh, 0x10b, SEEK_SET) < 0) {
63 return WTAP_FILE_UNKNOWN; /* I should exit(-1) here */
65 return WTAP_FILE_NGSNIFFER;
68 /* Read the next packet */
69 int ngsniffer_read(wtap *wth)
71 struct ngsniffer_hdr frame_hdr;
72 int bytes_read, packet_size;
74 bytes_read = fread(&frame_hdr, 1, sizeof(struct ngsniffer_hdr), wth->fh);
76 if (bytes_read == sizeof(struct ngsniffer_hdr)) {
78 packet_size = frame_hdr.bytes;
79 buffer_assure_space(&wth->frame_buffer, packet_size);
81 bytes_read = fread(buffer_start_ptr(&wth->frame_buffer), 1,
82 frame_hdr.bytes, wth->fh);
84 if (bytes_read != packet_size) {
85 g_error("ngsniffer_read: fread for data: %d bytes out of %d read\n",
86 bytes_read, packet_size);
90 wth->file_byte_offset += sizeof(struct ngsniffer_hdr) + packet_size;
92 wth->phdr.ts.tv_sec = 0;
93 wth->phdr.ts.tv_usec = 0;
94 wth->phdr.caplen = packet_size;
95 wth->phdr.len = packet_size;