I added the LANalzyer file format to wiretap. I cleaned up some code in the
[obnox/wireshark/wip.git] / wiretap / ngsniffer.c
1 /* ngsniffer.c
2  *
3  * $Id: ngsniffer.c,v 1.2 1998/11/12 06:01:24 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
7  * 
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.
12  * 
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.
17  * 
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.
21  *
22  */
23 #include "wtap.h"
24 #include "ngsniffer.h"
25
26 /* Returns WTAP_FILE_NGSNIFFER on success, WTAP_FILE_UNKNOWN on failure */
27 int ngsniffer_open(wtap *wth)
28 {
29         int bytes_read;
30         char magic[33];
31
32         fseek(wth->fh, 0, SEEK_SET);
33         bytes_read = fread(magic, 1, 32, wth->fh);
34
35         if (bytes_read != 32) {
36                 return WTAP_FILE_UNKNOWN;
37         }
38
39         magic[16] = 0;
40
41         if (strcmp(magic, "TRSNIFF data    ")) {
42                 return WTAP_FILE_UNKNOWN;
43         }
44
45         /* This is a ngsniffer file */
46         wth->frame_number = 0;
47         wth->file_byte_offset = 0x10b;
48         wth->subtype_read = ngsniffer_read;
49
50         /* I think this is link type */
51         if (magic[30] == 0x25) {
52                 wth->encapsulation = WTAP_ENCAP_ETHERNET;
53         }
54         else if (magic[30] == 0x24) {
55                 wth->encapsulation = WTAP_ENCAP_TR;
56         }
57         else {
58                 g_error("The magic byte that I think tells DLT is 0x%02X\n", magic[30]);
59                 exit(-1);
60         }
61
62         if (fseek(wth->fh, 0x10b, SEEK_SET) < 0) {
63                 return WTAP_FILE_UNKNOWN; /* I should exit(-1) here */
64         }
65         return WTAP_FILE_NGSNIFFER;
66 }
67
68 /* Read the next packet */
69 int ngsniffer_read(wtap *wth)
70 {
71         struct ngsniffer_hdr frame_hdr;
72         int     bytes_read, packet_size;
73
74         bytes_read = fread(&frame_hdr, 1, sizeof(struct ngsniffer_hdr), wth->fh);
75
76         if (bytes_read == sizeof(struct ngsniffer_hdr)) {
77                 wth->frame_number++;
78                 packet_size = frame_hdr.bytes;
79                 buffer_assure_space(&wth->frame_buffer, packet_size);
80
81                 bytes_read = fread(buffer_start_ptr(&wth->frame_buffer), 1,
82                                                 frame_hdr.bytes, wth->fh);
83
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);
87                         return 0;
88                 }
89
90                 wth->file_byte_offset += sizeof(struct ngsniffer_hdr) + packet_size;
91
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;
96
97                 return 1;
98         }
99
100         return 0;
101 }