3 * $Id: csids.c,v 1.12 2002/03/05 05:58: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
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, int *err);
50 static void csids_close(wtap *wth);
53 guint32 seconds; /* seconds since epoch */
54 guint16 zeropad; /* 2 byte zero'ed pads */
55 guint16 caplen; /* the capture length */
58 /* XXX - return -1 on I/O error and actually do something with 'err'. */
59 int csids_open(wtap *wth, int *err)
61 /* There is no file header. There is only a header for each packet
62 * so we read a packet header and compare the caplen with iplen. They
63 * should always be equal except with the wierd byteswap version.
65 * THIS IS BROKEN-- anytime the caplen is 0x0101 or 0x0202 up to 0x0505
66 * this will byteswap it. I need to fix this. XXX --mlh
69 int tmp,iplen,bytesRead;
71 gboolean byteswap = FALSE;
72 struct csids_header hdr;
75 /* check the file to make sure it is a csids file. */
76 bytesRead = file_read( &hdr, 1, sizeof( struct csids_header), wth->fh );
77 if( bytesRead != sizeof( struct csids_header) ) {
78 *err = file_error( wth->fh );
85 if( hdr.zeropad != 0 ) {
88 hdr.seconds = pntohl( &hdr.seconds );
89 hdr.caplen = pntohs( &hdr.caplen );
90 bytesRead = file_read( &tmp, 1, 2, wth->fh );
91 if( bytesRead != 2 ) {
92 *err = file_error( wth->fh );
99 bytesRead = file_read( &iplen, 1, 2, wth->fh );
100 if( bytesRead != 2 ) {
101 *err = file_error( wth->fh );
108 iplen = pntohs(&iplen);
109 /* if iplen and hdr.caplen are equal, default to no byteswap. */
110 if( iplen > hdr.caplen ) {
111 /* maybe this is just a byteswapped version. the iplen ipflags */
112 /* and ipid are swapped. We cannot use the normal swaps because */
113 /* we don't know the host */
114 iplen = BSWAP16(iplen);
115 if( iplen <= hdr.caplen ) {
116 /* we know this format */
119 /* don't know this one */
126 /* no file header. So reset the fh to 0 so we can read the first packet */
127 if (file_seek(wth->fh, 0, SEEK_SET) == -1) {
128 *err = file_error( wth->fh );
132 wth->data_offset = 0;
133 wth->capture.csids = g_malloc(sizeof(csids_t));
134 wth->capture.csids->byteswapped = byteswap;
135 wth->file_encap = WTAP_ENCAP_RAW_IP;
136 wth->file_type = WTAP_FILE_CSIDS;
137 wth->snapshot_length = 0; /* not known */
138 wth->subtype_read = csids_read;
139 wth->subtype_seek_read = csids_seek_read;
140 wth->subtype_close = csids_close;
145 /* Find the next packet and parse it; called from wtap_loop(). */
146 static gboolean csids_read(wtap *wth, int *err, long *data_offset)
150 struct csids_header hdr;
152 *data_offset = wth->data_offset;
154 bytesRead = file_read( &hdr, 1, sizeof( struct csids_header) , wth->fh );
155 if( bytesRead != sizeof( struct csids_header) ) {
156 *err = file_error( wth->fh );
157 if (*err == 0 && bytesRead != 0)
158 *err = WTAP_ERR_SHORT_READ;
161 hdr.seconds = pntohl(&hdr.seconds);
162 hdr.caplen = pntohs(&hdr.caplen);
164 wth->data_offset += sizeof( struct csids_header );
166 /* Make sure we have enough room for the packet */
167 buffer_assure_space(wth->frame_buffer, hdr.caplen);
168 buf = buffer_start_ptr(wth->frame_buffer);
170 bytesRead = file_read( buf, 1, hdr.caplen, wth->fh );
171 if( bytesRead != hdr.caplen ) {
172 *err = file_error( wth->fh );
174 *err = WTAP_ERR_SHORT_READ;
178 wth->data_offset += hdr.caplen;
180 wth->phdr.len = hdr.caplen;
181 wth->phdr.caplen = hdr.caplen;
182 wth->phdr.ts.tv_sec = hdr.seconds;
183 wth->phdr.ts.tv_usec = 0;
184 wth->phdr.pkt_encap = WTAP_ENCAP_RAW_IP;
186 if( wth->capture.csids->byteswapped == TRUE ) {
187 guint16* swap = (guint16*)buf;
189 *(swap) = BSWAP16(*swap); /* the ip len */
191 *(swap) = BSWAP16(*swap); /* ip id */
193 *(swap) = BSWAP16(*swap); /* ip flags and fragoff */
199 /* Used to read packets in random-access fashion */
201 csids_seek_read (wtap *wth,
203 union wtap_pseudo_header *pseudo_header _U_,
209 struct csids_header hdr;
211 if( file_seek( wth->random_fh, seek_off, SEEK_SET ) == -1 ) {
212 *err = file_error( wth->random_fh );
216 bytesRead = file_read( &hdr, 1, sizeof( struct csids_header), wth->random_fh );
217 if( bytesRead != sizeof( struct csids_header) ) {
218 *err = file_error( wth->random_fh );
220 *err = WTAP_ERR_SHORT_READ;
224 hdr.seconds = pntohl(&hdr.seconds);
225 hdr.caplen = pntohs(&hdr.caplen);
227 if( len != hdr.caplen ) {
228 *err = WTAP_ERR_BAD_RECORD;
232 bytesRead = file_read( pd, 1, hdr.caplen, wth->random_fh );
233 if( bytesRead != hdr.caplen ) {
234 *err = file_error( wth->random_fh );
236 *err = WTAP_ERR_SHORT_READ;
241 if( wth->capture.csids->byteswapped == TRUE ) {
242 guint16* swap = (guint16*)pd;
244 *(swap) = BSWAP16(*swap); /* the ip len */
246 *(swap) = BSWAP16(*swap); /* ip id */
248 *(swap) = BSWAP16(*swap); /* ip flags and fragoff */
255 csids_close(wtap *wth)
257 g_free(wth->capture.csids);