change all file offsets from long to gint64 so we can - theoretically - handle files...
[obnox/wireshark/wip.git] / wiretap / csids.c
1 /* csids.c
2  *
3  * $Id$
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  * wireshark.
45  */
46
47 static gboolean csids_read(wtap *wth, int *err, gchar **err_info,
48         gint64 *data_offset);
49 static gboolean csids_seek_read(wtap *wth, gint64 seek_off,
50         union wtap_pseudo_header *pseudo_header, guint8 *pd, int len,
51         int *err, gchar **err_info);
52 static void csids_close(wtap *wth);
53
54 struct csids_header {
55   guint32 seconds; /* seconds since epoch */
56   guint16 zeropad; /* 2 byte zero'ed pads */
57   guint16 caplen;  /* the capture length  */
58 };
59
60 /* XXX - return -1 on I/O error and actually do something with 'err'. */
61 int csids_open(wtap *wth, int *err, gchar **err_info _U_)
62 {
63   /* There is no file header. There is only a header for each packet
64    * so we read a packet header and compare the caplen with iplen. They
65    * should always be equal except with the wierd byteswap version.
66    *
67    * THIS IS BROKEN-- anytime the caplen is 0x0101 or 0x0202 up to 0x0505
68    * this will byteswap it. I need to fix this. XXX --mlh
69    */
70
71   int tmp,iplen,bytesRead;
72
73   gboolean byteswap = FALSE;
74   struct csids_header hdr;
75   bytesRead=0;
76
77   /* check the file to make sure it is a csids file. */
78   bytesRead = file_read( &hdr, 1, sizeof( struct csids_header), wth->fh );
79   if( bytesRead != sizeof( struct csids_header) ) {
80     *err = file_error( wth->fh );
81     if( *err != 0 ) {
82       return -1;
83     } else {
84       return 0;
85     }
86   }
87   if( hdr.zeropad != 0 ) {
88         return 0;
89   }
90   hdr.seconds = pntohl( &hdr.seconds );
91   hdr.caplen = pntohs( &hdr.caplen );
92   bytesRead = file_read( &tmp, 1, 2, wth->fh );
93   if( bytesRead != 2 ) {
94     *err = file_error( wth->fh );
95     if( *err != 0 ) {
96       return -1;
97     } else {
98       return 0;
99     }
100   }
101   bytesRead = file_read( &iplen, 1, 2, wth->fh );
102   if( bytesRead != 2 ) {
103     *err = file_error( wth->fh );
104     if( *err != 0 ) {
105       return -1;
106     } else {
107       return 0;
108     }
109   }
110   iplen = pntohs(&iplen);
111   /* if iplen and hdr.caplen are equal, default to no byteswap. */
112   if( iplen > hdr.caplen ) {
113     /* maybe this is just a byteswapped version. the iplen ipflags */
114     /* and ipid are swapped. We cannot use the normal swaps because */
115     /* we don't know the host */
116     iplen = BSWAP16(iplen);
117     if( iplen <= hdr.caplen ) {
118       /* we know this format */
119       byteswap = TRUE;
120     } else {
121       /* don't know this one */
122       return 0;
123     }
124   } else {
125     byteswap = FALSE;
126   }
127
128   /* no file header. So reset the fh to 0 so we can read the first packet */
129   if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
130     return -1;
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   wth->tsprecision = WTAP_FILE_TSPREC_SEC;
142
143   return 1;
144 }
145
146 /* Find the next packet and parse it; called from wtap_read(). */
147 static gboolean csids_read(wtap *wth, int *err, gchar **err_info _U_,
148     gint64 *data_offset)
149 {
150   guint8 *buf;
151   int bytesRead = 0;
152   struct csids_header hdr;
153
154   *data_offset = wth->data_offset;
155
156   bytesRead = file_read( &hdr, 1, sizeof( struct csids_header) , wth->fh );
157   if( bytesRead != sizeof( struct csids_header) ) {
158     *err = file_error( wth->fh );
159     if (*err == 0 && bytesRead != 0)
160       *err = WTAP_ERR_SHORT_READ;
161     return FALSE;
162   }
163   hdr.seconds = pntohl(&hdr.seconds);
164   hdr.caplen = pntohs(&hdr.caplen);
165
166   wth->data_offset += sizeof( struct csids_header );
167
168   /* Make sure we have enough room for the packet */
169   buffer_assure_space(wth->frame_buffer, hdr.caplen);
170   buf = buffer_start_ptr(wth->frame_buffer);
171
172   bytesRead = file_read( buf, 1, hdr.caplen, wth->fh );
173   if( bytesRead != hdr.caplen ) {
174     *err = file_error( wth->fh );
175     if (*err == 0)
176       *err = WTAP_ERR_SHORT_READ;
177     return FALSE;
178   }
179
180   wth->data_offset += hdr.caplen;
181
182   wth->phdr.len = hdr.caplen;
183   wth->phdr.caplen = hdr.caplen;
184   wth->phdr.ts.secs = hdr.seconds;
185   wth->phdr.ts.nsecs = 0;
186
187   if( wth->capture.csids->byteswapped == TRUE ) {
188     guint16* swap = (guint16*)buf;
189     swap++;
190     *(swap) = BSWAP16(*swap); /* the ip len */
191     swap++;
192     *(swap) = BSWAP16(*swap); /* ip id */
193     swap++;
194     *(swap) = BSWAP16(*swap); /* ip flags and fragoff */
195   }
196
197   return TRUE;
198 }
199
200 /* Used to read packets in random-access fashion */
201 static gboolean
202 csids_seek_read (wtap *wth,
203                  gint64 seek_off,
204                  union wtap_pseudo_header *pseudo_header _U_,
205                  guint8 *pd,
206                  int len,
207                  int *err,
208                  gchar **err_info)
209 {
210   int bytesRead;
211   struct csids_header hdr;
212
213   if( file_seek( wth->random_fh, seek_off, SEEK_SET, err ) == -1 )
214     return FALSE;
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 FALSE;
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     *err_info = g_strdup_printf("csids: record length %u doesn't match requested length %d",
230                                  hdr.caplen, len);
231     return FALSE;
232   }
233
234   bytesRead = file_read( pd, 1, hdr.caplen, wth->random_fh );
235   if( bytesRead != hdr.caplen ) {
236     *err = file_error( wth->random_fh );
237     if( *err == 0 ) {
238       *err = WTAP_ERR_SHORT_READ;
239     }
240     return FALSE;
241   }
242
243   if( wth->capture.csids->byteswapped == TRUE ) {
244     guint16* swap = (guint16*)pd;
245     swap++;
246     *(swap) = BSWAP16(*swap); /* the ip len */
247     swap++;
248     *(swap) = BSWAP16(*swap); /* ip id */
249     swap++;
250     *(swap) = BSWAP16(*swap); /* ip flags and fragoff */
251   }
252
253   return TRUE;
254 }
255
256 static void
257 csids_close(wtap *wth)
258 {
259   g_free(wth->capture.csids);
260 }