Add MP3 to the list of magic types
[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 || hdr.caplen == 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
112   if ( iplen == 0 )
113     return(0);
114
115   /* if iplen and hdr.caplen are equal, default to no byteswap. */
116   if( iplen > hdr.caplen ) {
117     /* maybe this is just a byteswapped version. the iplen ipflags */
118     /* and ipid are swapped. We cannot use the normal swaps because */
119     /* we don't know the host */
120     iplen = BSWAP16(iplen);
121     if( iplen <= hdr.caplen ) {
122       /* we know this format */
123       byteswap = TRUE;
124     } else {
125       /* don't know this one */
126       return 0;
127     }
128   } else {
129     byteswap = FALSE;
130   }
131
132   /* no file header. So reset the fh to 0 so we can read the first packet */
133   if (file_seek(wth->fh, 0, SEEK_SET, err) == -1)
134     return -1;
135
136   wth->data_offset = 0;
137   wth->capture.csids = g_malloc(sizeof(csids_t));
138   wth->capture.csids->byteswapped = byteswap;
139   wth->file_encap = WTAP_ENCAP_RAW_IP;
140   wth->file_type = WTAP_FILE_CSIDS;
141   wth->snapshot_length = 0; /* not known */
142   wth->subtype_read = csids_read;
143   wth->subtype_seek_read = csids_seek_read;
144   wth->subtype_close = csids_close;
145   wth->tsprecision = WTAP_FILE_TSPREC_SEC;
146
147   return 1;
148 }
149
150 /* Find the next packet and parse it; called from wtap_read(). */
151 static gboolean csids_read(wtap *wth, int *err, gchar **err_info _U_,
152     gint64 *data_offset)
153 {
154   guint8 *buf;
155   int bytesRead = 0;
156   struct csids_header hdr;
157
158   *data_offset = wth->data_offset;
159
160   bytesRead = file_read( &hdr, 1, sizeof( struct csids_header) , wth->fh );
161   if( bytesRead != sizeof( struct csids_header) ) {
162     *err = file_error( wth->fh );
163     if (*err == 0 && bytesRead != 0)
164       *err = WTAP_ERR_SHORT_READ;
165     return FALSE;
166   }
167   hdr.seconds = pntohl(&hdr.seconds);
168   hdr.caplen = pntohs(&hdr.caplen);
169
170   wth->data_offset += sizeof( struct csids_header );
171
172   /* Make sure we have enough room for the packet */
173   buffer_assure_space(wth->frame_buffer, hdr.caplen);
174   buf = buffer_start_ptr(wth->frame_buffer);
175
176   bytesRead = file_read( buf, 1, hdr.caplen, wth->fh );
177   if( bytesRead != hdr.caplen ) {
178     *err = file_error( wth->fh );
179     if (*err == 0)
180       *err = WTAP_ERR_SHORT_READ;
181     return FALSE;
182   }
183
184   wth->data_offset += hdr.caplen;
185
186   wth->phdr.len = hdr.caplen;
187   wth->phdr.caplen = hdr.caplen;
188   wth->phdr.ts.secs = hdr.seconds;
189   wth->phdr.ts.nsecs = 0;
190
191   if( wth->capture.csids->byteswapped ) {
192     PBSWAP16(buf);   /* the ip len */
193     PBSWAP16(buf+2); /* ip id */
194     PBSWAP16(buf+4); /* 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 ) {
244     PBSWAP16(pd);   /* the ip len */
245     PBSWAP16(pd+2); /* ip id */
246     PBSWAP16(pd+4); /* ip flags and fragoff */
247   }
248
249   return TRUE;
250 }
251
252 static void
253 csids_close(wtap *wth)
254 {
255   g_free(wth->capture.csids);
256 }