Have "wtap_seek_read()" return 0 on success and -1 on failure, and take
[metze/wireshark/wip.git] / wiretap / csids.c
1 /* csids.c
2  *
3  * $Id: csids.c,v 1.12 2002/03/05 05:58:40 guy Exp $
4  *
5  * Copyright (c) 2000 by Mike Hall <mlh@io.com>
6  * Copyright (c) 2000 by Cisco Systems
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 #ifdef HAVE_CONFIG_H
24 #include "config.h"
25 #endif
26 #include "wtap-int.h"
27 #include "buffer.h"
28 #include "csids.h"
29 #include "file_wrappers.h"
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 /* 
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.
41  *
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
44  * ethereal.
45  */
46
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);
51
52 struct csids_header {
53   guint32 seconds; /* seconds since epoch */
54   guint16 zeropad; /* 2 byte zero'ed pads */
55   guint16 caplen;  /* the capture length  */
56 };
57
58 /* XXX - return -1 on I/O error and actually do something with 'err'. */
59 int csids_open(wtap *wth, int *err)
60 {
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.
64    * 
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 
67    */
68
69   int tmp,iplen,bytesRead;
70   
71   gboolean byteswap = FALSE;
72   struct csids_header hdr;
73   bytesRead=0;
74
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 );
79     if( *err != 0 ) {
80       return -1;
81     } else {
82       return 0;
83     }
84   }
85   if( hdr.zeropad != 0 ) {
86         return 0;
87   }
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 );
93     if( *err != 0 ) {
94       return -1;
95     } else {
96       return 0;
97     }
98   }
99   bytesRead = file_read( &iplen, 1, 2, wth->fh );
100   if( bytesRead != 2 ) {
101     *err = file_error( wth->fh );
102     if( *err != 0 ) {
103       return -1;
104     } else {
105       return 0;
106     }
107   }
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 */
117       byteswap = TRUE;
118     } else {
119       /* don't know this one */
120       return 0;
121     }
122   } else {
123     byteswap = FALSE;
124   } 
125
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 );
129     return -1;
130   }
131
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;
141
142   return 1;
143 }
144
145 /* Find the next packet and parse it; called from wtap_loop(). */
146 static gboolean csids_read(wtap *wth, int *err, long *data_offset)
147 {
148   guint8 *buf;
149   int bytesRead = 0;
150   struct csids_header hdr;
151
152   *data_offset = wth->data_offset;
153
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;
159     return FALSE;
160   }
161   hdr.seconds = pntohl(&hdr.seconds);
162   hdr.caplen = pntohs(&hdr.caplen);
163
164   wth->data_offset += sizeof( struct csids_header );
165
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);
169   
170   bytesRead = file_read( buf, 1, hdr.caplen, wth->fh );
171   if( bytesRead != hdr.caplen ) {
172     *err = file_error( wth->fh );
173     if (*err == 0)
174       *err = WTAP_ERR_SHORT_READ;
175     return FALSE;
176   }
177   
178   wth->data_offset += hdr.caplen;
179
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;
185
186   if( wth->capture.csids->byteswapped == TRUE ) {
187     guint16* swap = (guint16*)buf;
188     swap++;
189     *(swap) = BSWAP16(*swap); /* the ip len */
190     swap++;
191     *(swap) = BSWAP16(*swap); /* ip id */
192     swap++;
193     *(swap) = BSWAP16(*swap); /* ip flags and fragoff */
194   }
195
196   return TRUE;
197 }
198
199 /* Used to read packets in random-access fashion */
200 static int
201 csids_seek_read (wtap *wth,
202                  long seek_off,
203                  union wtap_pseudo_header *pseudo_header _U_,
204                  guint8 *pd,
205                  int len,
206                  int *err)
207 {
208   int bytesRead = 0;
209   struct csids_header hdr;
210
211   if( file_seek( wth->random_fh, seek_off, SEEK_SET ) == -1 ) {
212     *err = file_error( wth->random_fh );
213     return -1;
214   }
215
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 );
219     if( *err == 0 ) {
220       *err = WTAP_ERR_SHORT_READ;
221     }
222     return -1;
223   }
224   hdr.seconds = pntohl(&hdr.seconds);
225   hdr.caplen = pntohs(&hdr.caplen);
226   
227   if( len != hdr.caplen ) {
228     *err = WTAP_ERR_BAD_RECORD;
229     return -1;
230   }
231
232   bytesRead = file_read( pd, 1, hdr.caplen, wth->random_fh );
233   if( bytesRead != hdr.caplen ) {
234     *err = file_error( wth->random_fh );
235     if( *err == 0 ) {
236       *err = WTAP_ERR_SHORT_READ;
237     }
238     return -1;
239   }
240
241   if( wth->capture.csids->byteswapped == TRUE ) {
242     guint16* swap = (guint16*)pd;
243     swap++;
244     *(swap) = BSWAP16(*swap); /* the ip len */
245     swap++;
246     *(swap) = BSWAP16(*swap); /* ip id */
247     swap++;
248     *(swap) = BSWAP16(*swap); /* ip flags and fragoff */
249   }
250   
251   return 0;
252 }
253
254 static void
255 csids_close(wtap *wth)
256 {
257   g_free(wth->capture.csids);
258 }