3 * $Id: csids.c,v 1.9 2002/02/08 10:07:40 guy Exp $
5 * Copyright (c) 2000 by Mike Hall <mlh@io.com>
6 * Copyright (c) 2000 by Cisco Systems
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.
29 #include "file_wrappers.h"
36 * This module reads the output from the Cisco Secure Intrustion Detection
37 * System iplogging facility. The term iplogging is misleading since this
38 * logger will only output TCP. There is no link layer information.
39 * Packet format is 4 byte timestamp (seconds since epoch), and a 4 byte size
40 * of data following for that packet.
42 * For a time there was an error in iplogging and the ip length, flags, and id
43 * were byteswapped. We will check for this and handle it before handing to ethereal.
47 static gboolean csids_read(wtap *wth, int *err, long *data_offset);
48 static int csids_seek_read(wtap *wth, long seek_off,
49 union wtap_pseudo_header *pseudo_header, guint8 *pd, int len);
52 guint32 seconds; /* seconds since epoch */
53 guint16 zeropad; /* 2 byte zero'ed pads */
54 guint16 caplen; /* the capture length */
57 /* XXX - return -1 on I/O error and actually do something with 'err'. */
58 int csids_open(wtap *wth, int *err)
60 /* There is no file header. There is only a header for each packet
61 * so we read a packet header and compare the caplen with iplen. They
62 * should always be equal except with the wierd byteswap version.
64 * THIS IS BROKEN-- anytime the caplen is 0x0101 or 0x0202 up to 0x0505
65 * this will byteswap it. I need to fix this. XXX --mlh
68 int tmp,iplen,bytesRead;
70 gboolean byteswap = FALSE;
71 struct csids_header hdr;
74 /* check the file to make sure it is a csids file. */
75 bytesRead = file_read( &hdr, 1, sizeof( struct csids_header), wth->fh );
76 if( bytesRead != sizeof( struct csids_header) ) {
77 *err = file_error( wth->fh );
84 if( hdr.zeropad != 0 ) {
87 hdr.seconds = pntohl( &hdr.seconds );
88 hdr.caplen = pntohs( &hdr.caplen );
89 bytesRead = file_read( &tmp, 1, 2, wth->fh );
90 if( bytesRead != 2 ) {
91 *err = file_error( wth->fh );
98 bytesRead = file_read( &iplen, 1, 2, wth->fh );
99 if( bytesRead != 2 ) {
100 *err = file_error( wth->fh );
107 iplen = pntohs(&iplen);
108 /* if iplen and hdr.caplen are equal, default to no byteswap. */
109 if( iplen > hdr.caplen ) {
110 /* maybe this is just a byteswapped version. the iplen ipflags */
111 /* and ipid are swapped. We cannot use the normal swaps because */
112 /* we don't know the host */
113 iplen = BSWAP16(iplen);
114 if( iplen <= hdr.caplen ) {
115 /* we know this format */
118 /* don't know this one */
125 wth->data_offset = 0;
126 wth->capture.csids = g_malloc(sizeof(csids_t));
127 wth->capture.csids->byteswapped = byteswap;
128 wth->file_encap = WTAP_ENCAP_RAW_IP;
129 wth->file_type = WTAP_FILE_CSIDS;
130 wth->snapshot_length = 0; /* not known */
131 wth->subtype_read = csids_read;
132 wth->subtype_seek_read = csids_seek_read;
134 /* no file header. So reset the fh to 0 so we can read the first packet */
135 file_seek(wth->fh, 0, SEEK_SET);
140 /* Find the next packet and parse it; called from wtap_loop(). */
141 static gboolean csids_read(wtap *wth, int *err, long *data_offset)
145 struct csids_header hdr;
147 *data_offset = wth->data_offset;
149 bytesRead = file_read( &hdr, 1, sizeof( struct csids_header) , wth->fh );
150 if( bytesRead != sizeof( struct csids_header) ) {
151 *err = file_error( wth->fh );
152 if (*err == 0 && bytesRead != 0)
153 *err = WTAP_ERR_SHORT_READ;
156 hdr.seconds = pntohl(&hdr.seconds);
157 hdr.caplen = pntohs(&hdr.caplen);
159 wth->data_offset += sizeof( struct csids_header );
161 /* Make sure we have enough room for the packet */
162 buffer_assure_space(wth->frame_buffer, hdr.caplen);
163 buf = buffer_start_ptr(wth->frame_buffer);
165 bytesRead = file_read( buf, 1, hdr.caplen, wth->fh );
166 if( bytesRead != hdr.caplen ) {
167 *err = file_error( wth->fh );
169 *err = WTAP_ERR_SHORT_READ;
173 wth->data_offset += hdr.caplen;
175 wth->phdr.len = hdr.caplen;
176 wth->phdr.caplen = hdr.caplen;
177 wth->phdr.ts.tv_sec = hdr.seconds;
178 wth->phdr.ts.tv_usec = 0;
179 wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
181 if( wth->capture.csids->byteswapped == TRUE ) {
182 guint16* swap = (guint16*)buf;
184 *(swap) = BSWAP16(*swap); /* the ip len */
186 *(swap) = BSWAP16(*swap); /* ip id */
188 *(swap) = BSWAP16(*swap); /* ip flags and fragoff */
194 /* Used to read packets in random-access fashion */
196 csids_seek_read (wtap *wth,
198 union wtap_pseudo_header *pseudo_header,
204 struct csids_header hdr;
206 file_seek(wth->random_fh, seek_off , SEEK_SET);
208 bytesRead = file_read( &hdr, 1, sizeof( struct csids_header) , wth->random_fh );
209 if( bytesRead != sizeof( struct csids_header) ) {
210 err = file_error( wth->fh );
217 hdr.seconds = pntohl(&hdr.seconds);
218 hdr.caplen = pntohs(&hdr.caplen);
220 if( len != hdr.caplen ) {
224 bytesRead = file_read( pd, 1, hdr.caplen, wth->random_fh );
225 if( bytesRead != hdr.caplen ) {
226 err = file_error( wth->fh );
232 if( wth->capture.csids->byteswapped == TRUE ) {
233 guint16* swap = (guint16*)pd;
235 *(swap) = BSWAP16(*swap); /* the ip len */
237 *(swap) = BSWAP16(*swap); /* ip id */
239 *(swap) = BSWAP16(*swap); /* ip flags and fragoff */