Support for reading compressed Sniffer files, from Tim Farley, Joerg
[obnox/wireshark/wip.git] / wiretap / nettl.c
1 /* nettl.c
2  *
3  * $Id: nettl.c,v 1.13 2000/05/19 23:06:57 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@xiexie.org>
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
27 #include <stdlib.h>
28 #include <errno.h>
29 #include <time.h>
30 #include "wtap-int.h"
31 #include "file_wrappers.h"
32 #include "buffer.h"
33 #include "nettl.h"
34
35 static char nettl_magic_hpux9[12] = {
36     0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xD0, 0x00
37 };
38 static char nettl_magic_hpux10[12] = {
39     0x54, 0x52, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80
40 };
41
42 /* HP nettl record header for the SX25L2 subsystem - The FCS is not included in the file. */
43 struct nettlrec_sx25l2_hdr {
44     guint8      xxa[8];
45     guint8      from_dce;
46     guint8      xxb[55];
47     guint8      length[2];
48     guint8      length2[2];    /* don't know which one is captured length / real length */
49     guint8      xxc[4];
50     guint8      sec[4];
51     guint8      usec[4];
52     guint8      xxd[4];
53 };
54
55 /* HP nettl record header for the NS_LS_IP subsystem */
56 struct nettlrec_ns_ls_ip_hdr {
57     guint8      xxa[28];
58     guint8      length[4];
59     guint8      length2[4];    /* don't know which one is captured length / real length */
60     guint8      sec[4];
61     guint8      usec[4];
62     guint8      xxb[16];
63 };
64
65 /* header is followed by data and once again the total length (2 bytes) ! */
66
67 static int nettl_read(wtap *wth, int *err);
68 static int nettl_seek_read(wtap *wth, int seek_off,
69                 union wtap_pseudo_header *pseudo_header, u_char *pd, int length);
70 static int nettl_read_rec_header(wtap *wth, FILE_T fh,
71                 struct wtap_pkthdr *phdr, union wtap_pseudo_header *pseudo_header,
72                 int *err);
73 static int nettl_read_rec_data(FILE_T fh, char *pd, int length, int *err);
74 static void nettl_close(wtap *wth);
75
76 int nettl_open(wtap *wth, int *err)
77 {
78     char magic[12], os_vers[2];
79     int bytes_read;
80
81     /* Read in the string that should be at the start of a HP file */
82     file_seek(wth->fh, 0, SEEK_SET);
83     errno = WTAP_ERR_CANT_READ;
84     bytes_read = file_read(magic, 1, 12, wth->fh);
85     if (bytes_read != 12) {
86         *err = file_error(wth->fh);
87         if (*err != 0)
88             return -1;
89         return 0;
90     }
91
92     if (memcmp(magic, nettl_magic_hpux9, 12) &&
93         memcmp(magic, nettl_magic_hpux10, 12)) {
94         return 0;
95     }
96
97     file_seek(wth->fh, 0x63, SEEK_SET);
98     wth->data_offset = 0x63;
99     bytes_read = file_read(os_vers, 1, 2, wth->fh);
100     if (bytes_read != 2) {
101         *err = file_error(wth->fh);
102         if (*err != 0)
103             return -1;
104         return 0;
105     }
106
107     file_seek(wth->fh, 0x80, SEEK_SET);
108     wth->data_offset = 0x80;
109
110     /* This is an nettl file */
111     wth->file_type = WTAP_FILE_NETTL;
112     wth->capture.nettl = g_malloc(sizeof(nettl_t));
113     if (os_vers[0] == '1' && os_vers[1] == '1')
114         wth->capture.nettl->is_hpux_11 = TRUE;
115     else
116         wth->capture.nettl->is_hpux_11 = FALSE;
117     wth->subtype_read = nettl_read;
118     wth->subtype_seek_read = nettl_seek_read;
119     wth->subtype_close = nettl_close;
120     wth->snapshot_length = 16384;       /* not available in header, only in frame */
121
122     return 1;
123 }
124
125 /* Read the next packet */
126 static int nettl_read(wtap *wth, int *err)
127 {
128     int record_offset;
129     int ret;
130
131     /* Read record header. */
132     record_offset = wth->data_offset;
133     ret = nettl_read_rec_header(wth, wth->fh, &wth->phdr, &wth->pseudo_header,
134         err);
135     if (ret <= 0) {
136         /* Read error or EOF */
137         return ret;
138     }
139     wth->data_offset += ret;
140
141     /*
142      * Read the packet data.
143      */
144     buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
145     if (nettl_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
146                 wth->phdr.caplen, err) < 0)
147         return -1;      /* Read error */
148     wth->data_offset += wth->phdr.caplen;
149     return record_offset;
150 }
151
152 static int
153 nettl_seek_read(wtap *wth, int seek_off,
154                 union wtap_pseudo_header *pseudo_header, u_char *pd, int length)
155 {
156     int ret;
157     int err;            /* XXX - return this */
158     struct wtap_pkthdr phdr;
159
160     file_seek(wth->random_fh, seek_off, SEEK_SET);
161
162     /* Read record header. */
163     ret = nettl_read_rec_header(wth, wth->random_fh, &phdr, pseudo_header,
164         &err);
165     if (ret <= 0) {
166         /* Read error or EOF */
167         return ret;
168     }
169
170     /*
171      * Read the packet data.
172      */
173     return nettl_read_rec_data(wth->random_fh, pd, length, &err);
174 }
175
176 static int
177 nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
178                 union wtap_pseudo_header *pseudo_header, int *err)
179 {
180     int bytes_read;
181     struct nettlrec_sx25l2_hdr lapb_hdr;
182     struct nettlrec_ns_ls_ip_hdr ip_hdr;
183     guint16 length;
184     int offset = 0;
185     guint8 encap[4];
186     guint8 dummy[4];
187
188     errno = WTAP_ERR_CANT_READ;
189     bytes_read = file_read(encap, 1, 4, fh);
190     if (bytes_read != 4) {
191         *err = file_error(fh);
192         if (*err != 0)
193             return -1;
194         if (bytes_read != 0) {
195             *err = WTAP_ERR_SHORT_READ;
196             return -1;
197         }
198         return 0;
199     }
200     offset += 4;
201
202     switch (encap[3]) {
203     case NETTL_SUBSYS_NS_LS_IP :
204         phdr->pkt_encap = WTAP_ENCAP_RAW_IP;
205         bytes_read = file_read(&ip_hdr, 1, sizeof ip_hdr, fh);
206         if (bytes_read != sizeof ip_hdr) {
207             *err = file_error(fh);
208             if (*err != 0)
209                 return -1;
210             if (bytes_read != 0) {
211                 *err = WTAP_ERR_SHORT_READ;
212                 return -1;
213             }
214             return 0;
215         }
216         offset += sizeof ip_hdr;
217
218         /* The packet header in HP-UX 11 nettl traces is 4 octets longer than
219          * HP-UX 9 and 10 */
220         if (wth->capture.nettl->is_hpux_11) {
221             bytes_read = file_read(dummy, 1, 4, fh);
222             if (bytes_read != 4) {
223                 *err = file_error(fh);
224                 if (*err != 0)
225                     return -1;
226                 if (bytes_read != 0) {
227                     *err = WTAP_ERR_SHORT_READ;
228                     return -1;
229                 }
230                 return 0;
231             }
232             offset += 4;
233         }
234
235         length = pntohl(&ip_hdr.length);
236         if (length <= 0) return 0;
237         phdr->len = length;
238         phdr->caplen = length;
239
240         phdr->ts.tv_sec = pntohl(&ip_hdr.sec);
241         phdr->ts.tv_usec = pntohl(&ip_hdr.usec);
242         break;
243     case NETTL_SUBSYS_SX25L2 :
244         phdr->pkt_encap = WTAP_ENCAP_LAPB;
245         bytes_read = file_read(&lapb_hdr, 1, sizeof lapb_hdr, fh);
246         if (bytes_read != sizeof lapb_hdr) {
247             *err = file_error(fh);
248             if (*err != 0)
249                 return -1;
250             if (bytes_read != 0) {
251                 *err = WTAP_ERR_SHORT_READ;
252                 return -1;
253             }
254             return 0;
255         }
256         offset += sizeof lapb_hdr;
257
258         if (wth->capture.nettl->is_hpux_11) {
259             bytes_read = file_read(dummy, 1, 4, fh);
260             if (bytes_read != 4) {
261                 *err = file_error(fh);
262                 if (*err != 0)
263                     return -1;
264                 if (bytes_read != 0) {
265                     *err = WTAP_ERR_SHORT_READ;
266                     return -1;
267                 }
268                 return 0;
269             }
270             offset += 4;
271         }
272
273         length = pntohs(&lapb_hdr.length);
274         if (length <= 0) return 0;
275         phdr->len = length;
276         phdr->caplen = length;
277
278         phdr->ts.tv_sec = pntohl(&lapb_hdr.sec);
279         phdr->ts.tv_usec = pntohl(&lapb_hdr.usec);
280         pseudo_header->x25.flags = (lapb_hdr.from_dce & 0x20 ? 0x80 : 0x00);
281         break;
282     default:
283         g_message("nettl: network type %u unknown or unsupported",
284                     encap[3]);
285         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
286         return -1;
287     }
288     return offset;
289 }
290
291 static int
292 nettl_read_rec_data(FILE_T fh, char *pd, int length, int *err)
293 {
294     int bytes_read;
295
296     bytes_read = file_read(pd, 1, length, fh);
297
298     if (bytes_read != length) {
299         *err = file_error(fh);
300         if (*err == 0)
301             *err = WTAP_ERR_SHORT_READ;
302         return -1;
303     }
304     return 0;
305 }
306
307 static void nettl_close(wtap *wth)
308 {
309     g_free(wth->capture.nettl);
310 }