From Mark C. Brown:
[obnox/wireshark/wip.git] / wiretap / nettl.c
1 /* nettl.c
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * Enhancements by Mark C. Brown <mbrown@hp.com>
9  * Copyright (C) 2003, 2005 Hewlett-Packard Development Company, L.P.
10  *
11  * This program is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU General Public License
13  * as published by the Free Software Foundation; either version 2
14  * of the License, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <stdlib.h>
31 #include <errno.h>
32 #include <string.h>
33 #include "wtap-int.h"
34 #include "file_wrappers.h"
35 #include "buffer.h"
36 #include "nettl.h"
37
38 /* HP nettl file header */
39
40 /* Magic number size */
41 #define MAGIC_SIZE      12
42
43 /* HP-UX 9.x */
44 static guint8 nettl_magic_hpux9[MAGIC_SIZE] = {
45     0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xD0, 0x00
46 };
47 /* HP-UX 10.x and 11.x */
48 static guint8 nettl_magic_hpux10[MAGIC_SIZE] = {
49     0x54, 0x52, 0x00, 0x64, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80
50 };
51
52 #define FILE_HDR_SIZE   128
53 #define NETTL_FILENAME_SIZE 56
54
55 struct nettl_file_hdr {
56     guint8      magic[MAGIC_SIZE];
57     gchar       file_name[NETTL_FILENAME_SIZE];
58     gchar       tz[20];
59     gchar       host_name[9];
60     gchar       os_vers[9];
61     guchar      os_v;
62     guint8      xxa[8];
63     gchar       model[11];
64     guint16     unknown;        /* just padding to 128 bytes? */
65 };
66
67 /* HP nettl record header */
68 /* see /usr/include/sys/netdiag1.h for hints */
69 struct nettlrec_hdr {
70     guint16     hdr_len;
71     guint16     subsys;
72     guint32     devid;
73     guint8      xxa[4];
74     guint32     kind;
75     guint8      xxb[16];
76     guint32     caplen;
77     guint32     length;
78     guint32     sec;
79     guint32     usec;
80     guint32     pid;
81     guint8      xxc[10];
82     guint16     uid;
83 };
84
85 /*
86  * This is what we treat as the minimum size of a record header.
87  * It is *not* necessarily the same as sizeof(struct nettlrec_hdr),
88  * because it doesn't include any padding added to the structure.
89  */
90 #define NETTL_REC_HDR_LEN       64
91
92 /* HP nettl record header for the SX25L2 subsystem - The FCS is not included
93    in the file. */
94 struct nettlrec_sx25l2_hdr {
95     guint8      xxa[8];
96     guint8      from_dce;
97     guint8      xxb[55];
98     guint8      caplen[2];
99     guint8      length[2];
100     guint8      xxc[4];
101     guint8      sec[4];
102     guint8      usec[4];
103     guint8      xxd[4];
104 };
105
106 /* NL_LS_DRIVER :
107 The following shows what the header and subheader looks like for NS_LS_DRIVER
108 The capture was taken on HPUX11 and for a 100baseT interface.
109
110 000080 00 44 00 0b 00 00 00 02 00 00 00 00 20 00 00 00
111 000090 00 00 00 00 00 00 04 06 00 00 00 00 00 00 00 00
112 0000a0 00 00 00 74 00 00 00 74 3c e3 76 19 00 06 34 63
113 0000b0 ff ff ff ff 00 00 00 00 00 00 00 00 ff ff ff ff
114 0000c0 00 00 00 00 00 00 01 02 00 5c 00 5c ff ff ff ff
115 0000d0 3c e3 76 19 00 06 34 5a 00 0b 00 14 <here starts the MAC header>
116
117 Each entry starts with 0x0044000b
118
119 The values 0x005c at position 0x0000c8 and 0x0000ca matches the number of
120 bytes in the packet up to the next entry, which starts with 0x00440b again.
121 These are the captured and real and captured length of the packet.
122
123 The values 0x00000074 at positions 0x0000a0 and 0x0000a4 seems to indicate
124 the same number as positions 0x0000c8 and 0x0000ca but added with 24.
125 Perhaps we have here two layers of headers.
126 The first layer is fixed and consists of all the bytes from 0x000084 up to and
127 including 0x0000c3 which is a generic header for all packets captured from any
128 device. This header might be of fixed size 64 bytes (although the first two
129 bytes appear to be the length of that header, in big-endian format) and there
130 might be something in it which indicates the type of the next header which is
131 link type specific. Following this header there is another header for the
132 100baseT interface which in this case is 24 bytes long spanning positions
133 0x0000c4 to 0x0000db.
134
135 In another capture, claimed to be taken on an HP-UX 8 box, but with a
136 file header suggesting it was taken on HP-UX 10.20, the header for
137 NS_LS_DRIVER looks like:
138
139 000080   00 40 00 0b ff ff ff ff 00 00 00 00 00 00 00 00
140 000090   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
141 0000a0   00 00 00 51 00 00 00 51 42 02 5e bf 00 0e ab 7c
142 0000b0   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
143 0000c0   00 02 01 00 00 3b 00 3b ff ff ff ff 42 02 5e bf
144 0000d0   00 0e 8e 44 00 0b <here starts the MAC header>
145
146 When someone reports that the loading of the captures breaks, we can
147 compare this header above with what he/she got to learn how to
148 distinguish between different types of link specific headers.
149
150
151 For now, the subheader for 100baseT seems to be
152         4-5     captured length
153         6-7     actual length
154         8-11    unknown
155         12-15   secs
156         16-19   usecs
157         20-21   unknown
158 */
159 struct nettlrec_ns_ls_drv_eth_hdr {
160     guint8      xxa[4];
161     guint8      caplen[2];
162     guint8      length[2];
163     guint8      xxb[4];
164     guint8      sec[4];
165     guint8      usec[4];
166     guint8      xxc[2];
167 };
168
169 /*
170  * This is the size of an NS_LS_DRV_ETH header; it is *not* necessarily
171  * the same as sizeof(struct nettlrec_ns_ls_drv_eth_hdr), because it
172  * doesn't include any padding added to the structure.
173  */
174 #define NS_LS_DRV_ETH_HDR_LEN   22
175
176 /* header is followed by data and once again the total length (2 bytes) ! */
177
178 static gboolean nettl_read(wtap *wth, int *err, gchar **err_info,
179                 gint64 *data_offset);
180 static gboolean nettl_seek_read(wtap *wth, gint64 seek_off,
181                 union wtap_pseudo_header *pseudo_header, guchar *pd,
182                 int length, int *err, gchar **err_info);
183 static int nettl_read_rec_header(wtap *wth, FILE_T fh,
184                 struct wtap_pkthdr *phdr, union wtap_pseudo_header *pseudo_header,
185                 int *err, gchar **err_info, gboolean *fddihack);
186 static gboolean nettl_read_rec_data(FILE_T fh, guchar *pd, int length,
187                 int *err, gboolean fddihack);
188 static void nettl_close(wtap *wth);
189 static gboolean nettl_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
190     const union wtap_pseudo_header *pseudo_header, const guchar *pd, int *err);
191
192 int nettl_open(wtap *wth, int *err, gchar **err_info _U_)
193 {
194     struct nettl_file_hdr file_hdr;
195     guint16 dummy[2];
196     int subsys;
197     int bytes_read;
198
199     /* Read in the string that should be at the start of a HP file */
200     errno = WTAP_ERR_CANT_READ;
201     bytes_read = file_read(file_hdr.magic, 1, MAGIC_SIZE, wth->fh);
202     if (bytes_read != MAGIC_SIZE) {
203         *err = file_error(wth->fh);
204         if (*err != 0)
205             return -1;
206         return 0;
207     }
208
209     if (memcmp(file_hdr.magic, nettl_magic_hpux9, MAGIC_SIZE) &&
210         memcmp(file_hdr.magic, nettl_magic_hpux10, MAGIC_SIZE)) {
211         return 0;
212     }
213
214     /* Read the rest of the file header */
215     bytes_read = file_read(file_hdr.file_name, 1, FILE_HDR_SIZE - MAGIC_SIZE,
216                            wth->fh);
217     if (bytes_read != FILE_HDR_SIZE - MAGIC_SIZE) {
218         *err = file_error(wth->fh);
219         if (*err != 0)
220             return -1;
221         return 0;
222     }
223
224     /* This is an nettl file */
225     wth->file_type = WTAP_FILE_NETTL;
226     wth->capture.nettl = g_malloc(sizeof(nettl_t));
227     if (file_hdr.os_vers[2] == '1' && file_hdr.os_vers[3] == '1')
228         wth->capture.nettl->is_hpux_11 = TRUE;
229     else
230         wth->capture.nettl->is_hpux_11 = FALSE;
231     wth->subtype_read = nettl_read;
232     wth->subtype_seek_read = nettl_seek_read;
233     wth->subtype_close = nettl_close;
234     wth->snapshot_length = 0;   /* not available */
235
236     /* read the first header to take a guess at the file encap */
237     bytes_read = file_read(dummy, 1, 4, wth->fh);
238     if (bytes_read != 4) {
239         if (*err != 0)
240             return -1;
241         if (bytes_read != 0) {
242             *err = WTAP_ERR_SHORT_READ;
243             g_free(wth->capture.nettl);
244             return -1;
245         }
246         return 0;
247     }
248
249     subsys = g_ntohs(dummy[1]);
250     switch (subsys) {
251         case NETTL_SUBSYS_HPPB_FDDI :
252         case NETTL_SUBSYS_EISA_FDDI :
253         case NETTL_SUBSYS_PCI_FDDI :
254         case NETTL_SUBSYS_HSC_FDDI :
255                 wth->file_encap = WTAP_ENCAP_NETTL_FDDI;
256                 break;
257         case NETTL_SUBSYS_TOKEN :
258         case NETTL_SUBSYS_PCI_TR :
259                 wth->file_encap = WTAP_ENCAP_NETTL_TOKEN_RING;
260                 break;
261         case NETTL_SUBSYS_NS_LS_IP :
262         case NETTL_SUBSYS_NS_LS_LOOPBACK :
263         case NETTL_SUBSYS_NS_LS_TCP :
264         case NETTL_SUBSYS_NS_LS_UDP :
265         case NETTL_SUBSYS_NS_LS_IPV6 :
266                 wth->file_encap = WTAP_ENCAP_NETTL_RAW_IP;
267                 break;
268         case NETTL_SUBSYS_NS_LS_ICMP :
269                 wth->file_encap = WTAP_ENCAP_NETTL_RAW_ICMP;
270                 break;
271         case NETTL_SUBSYS_NS_LS_ICMPV6 :
272                 wth->file_encap = WTAP_ENCAP_NETTL_RAW_ICMPV6;
273                 break;
274         case NETTL_SUBSYS_NS_LS_TELNET :
275                 wth->file_encap = WTAP_ENCAP_NETTL_RAW_TELNET;
276                 break;
277         default:
278                 /* If this assumption is bad, the read will catch it */
279                 wth->file_encap = WTAP_ENCAP_NETTL_ETHERNET;
280     }
281
282     if (file_seek(wth->fh, FILE_HDR_SIZE, SEEK_SET, err) == -1) {
283         g_free(wth->capture.nettl);
284         return -1;
285     }
286     wth->data_offset = FILE_HDR_SIZE;
287     wth->tsprecision = WTAP_FILE_TSPREC_USEC;
288
289     return 1;
290 }
291
292 /* Read the next packet */
293 static gboolean nettl_read(wtap *wth, int *err, gchar **err_info,
294     gint64 *data_offset)
295 {
296     int ret;
297     gboolean fddihack=FALSE;
298
299     /* Read record header. */
300     *data_offset = wth->data_offset;
301     ret = nettl_read_rec_header(wth, wth->fh, &wth->phdr, &wth->pseudo_header,
302         err, err_info, &fddihack);
303     if (ret <= 0) {
304         /* Read error or EOF */
305         return FALSE;
306     }
307     wth->data_offset += ret;
308
309     /*
310      * If the per-file encapsulation isn't known, set it to this
311      * packet's encapsulation.
312      *
313      * If it *is* known, and it isn't this packet's encapsulation,
314      * set it to WTAP_ENCAP_PER_PACKET, as this file doesn't
315      * have a single encapsulation for all packets in the file.
316      */
317     if (wth->file_encap == WTAP_ENCAP_UNKNOWN)
318         wth->file_encap = wth->phdr.pkt_encap;
319     else {
320         if (wth->file_encap != wth->phdr.pkt_encap)
321             wth->file_encap = WTAP_ENCAP_PER_PACKET;
322     }
323
324     /*
325      * Read the packet data.
326      */
327     buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
328     if (!nettl_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
329                 wth->phdr.caplen, err, fddihack))
330         return FALSE;   /* Read error */
331     wth->data_offset += wth->phdr.caplen;
332     return TRUE;
333 }
334
335 static gboolean
336 nettl_seek_read(wtap *wth, gint64 seek_off,
337                 union wtap_pseudo_header *pseudo_header, guchar *pd,
338                 int length, int *err, gchar **err_info)
339 {
340     int ret;
341     struct wtap_pkthdr phdr;
342     gboolean fddihack=FALSE;
343
344     if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
345         return FALSE;
346
347     /* Read record header. */
348     ret = nettl_read_rec_header(wth, wth->random_fh, &phdr, pseudo_header,
349         err, err_info, &fddihack);
350     if (ret <= 0) {
351         /* Read error or EOF */
352         if (ret == 0) {
353             /* EOF means "short read" in random-access mode */
354             *err = WTAP_ERR_SHORT_READ;
355         }
356         return FALSE;
357     }
358
359     /*
360      * Read the packet data.
361      */
362     return nettl_read_rec_data(wth->random_fh, pd, length, err, fddihack);
363 }
364
365 static int
366 nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
367                 union wtap_pseudo_header *pseudo_header, int *err,
368                 gchar **err_info, gboolean *fddihack)
369 {
370     int bytes_read;
371     struct nettlrec_hdr rec_hdr;
372     guint16 hdr_len;
373     struct nettlrec_ns_ls_drv_eth_hdr drv_eth_hdr;
374     guint16 length, caplen;
375     int offset = 0;
376     int subsys;
377     int padlen;
378     guint8 dummyc[16];
379
380     errno = WTAP_ERR_CANT_READ;
381     bytes_read = file_read(&rec_hdr.hdr_len, 1, sizeof rec_hdr.hdr_len, fh);
382     if (bytes_read != sizeof rec_hdr.hdr_len) {
383         *err = file_error(fh);
384         if (*err != 0)
385             return -1;
386         if (bytes_read != 0) {
387             *err = WTAP_ERR_SHORT_READ;
388             return -1;
389         }
390         return 0;
391     }
392     offset += 2;
393     hdr_len = g_ntohs(rec_hdr.hdr_len);
394     if (hdr_len < NETTL_REC_HDR_LEN) {
395         *err = WTAP_ERR_BAD_RECORD;
396         *err_info = g_strdup_printf("nettl: record header length %u too short",
397             hdr_len);
398         return -1;
399     }
400     bytes_read = file_read(&rec_hdr.subsys, 1, NETTL_REC_HDR_LEN - 2, fh);
401     if (bytes_read != NETTL_REC_HDR_LEN - 2) {
402         *err = file_error(fh);
403         if (*err == 0)
404             *err = WTAP_ERR_SHORT_READ;
405         return -1;
406     }
407     offset += NETTL_REC_HDR_LEN - 2;
408     subsys = g_ntohs(rec_hdr.subsys);
409     hdr_len -= NETTL_REC_HDR_LEN;
410     if (file_seek(fh, hdr_len, SEEK_CUR, err) == -1)
411         return -1;
412     offset += hdr_len;
413
414     if ( (pntohl(&rec_hdr.kind) & NETTL_HDR_PDU_MASK) == 0 ) {
415         /* not actually a data packet (PDU) trace record */
416         phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_IP;
417         length = pntohl(&rec_hdr.length);
418         caplen = pntohl(&rec_hdr.caplen);
419         padlen = 0;
420     } else switch (subsys) {
421         case NETTL_SUBSYS_LAN100 :
422         case NETTL_SUBSYS_EISA100BT :
423         case NETTL_SUBSYS_BASE100 :
424         case NETTL_SUBSYS_GSC100BT :
425         case NETTL_SUBSYS_PCI100BT :
426         case NETTL_SUBSYS_SPP100BT :
427         case NETTL_SUBSYS_100VG :
428         case NETTL_SUBSYS_GELAN :
429         case NETTL_SUBSYS_BTLAN :
430         case NETTL_SUBSYS_INTL100 :
431         case NETTL_SUBSYS_IGELAN :
432         case NETTL_SUBSYS_IETHER :
433         case NETTL_SUBSYS_IXGBE :
434         case NETTL_SUBSYS_HSSN :
435         case NETTL_SUBSYS_IGSSN :
436         case NETTL_SUBSYS_ICXGBE :
437         case NETTL_SUBSYS_IEXGBE :
438         case NETTL_SUBSYS_HPPB_FDDI :
439         case NETTL_SUBSYS_EISA_FDDI :
440         case NETTL_SUBSYS_PCI_FDDI :
441         case NETTL_SUBSYS_HSC_FDDI :
442         case NETTL_SUBSYS_TOKEN :
443         case NETTL_SUBSYS_PCI_TR :
444         case NETTL_SUBSYS_NS_LS_IP :
445         case NETTL_SUBSYS_NS_LS_LOOPBACK :
446         case NETTL_SUBSYS_NS_LS_TCP :
447         case NETTL_SUBSYS_NS_LS_UDP :
448         case NETTL_SUBSYS_HP_APAPORT :
449         case NETTL_SUBSYS_HP_APALACP :
450         case NETTL_SUBSYS_NS_LS_IPV6 :
451         case NETTL_SUBSYS_NS_LS_ICMPV6 :
452         case NETTL_SUBSYS_NS_LS_ICMP :
453         case NETTL_SUBSYS_NS_LS_TELNET :
454         case NETTL_SUBSYS_NS_LS_SCTP :
455             if( (subsys == NETTL_SUBSYS_NS_LS_IP)
456              || (subsys == NETTL_SUBSYS_NS_LS_LOOPBACK)
457              || (subsys == NETTL_SUBSYS_NS_LS_UDP)
458              || (subsys == NETTL_SUBSYS_NS_LS_TCP)
459              || (subsys == NETTL_SUBSYS_NS_LS_SCTP)
460              || (subsys == NETTL_SUBSYS_NS_LS_IPV6)) {
461                 phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_IP;
462             } else if (subsys == NETTL_SUBSYS_NS_LS_ICMP) {
463                 phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_ICMP;
464             } else if (subsys == NETTL_SUBSYS_NS_LS_ICMPV6) {
465                 phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_ICMPV6;
466             } else if (subsys == NETTL_SUBSYS_NS_LS_TELNET) {
467                 phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_TELNET;
468             } else if( (subsys == NETTL_SUBSYS_HPPB_FDDI)
469                     || (subsys == NETTL_SUBSYS_EISA_FDDI)
470                     || (subsys == NETTL_SUBSYS_PCI_FDDI)
471                     || (subsys == NETTL_SUBSYS_HSC_FDDI) ) {
472                 phdr->pkt_encap = WTAP_ENCAP_NETTL_FDDI;
473             } else if( (subsys == NETTL_SUBSYS_PCI_TR)
474                     || (subsys == NETTL_SUBSYS_TOKEN) ) {
475                 phdr->pkt_encap = WTAP_ENCAP_NETTL_TOKEN_RING;
476             } else {
477                 phdr->pkt_encap = WTAP_ENCAP_NETTL_ETHERNET;
478             }
479
480             length = pntohl(&rec_hdr.length);
481             caplen = pntohl(&rec_hdr.caplen);
482
483             /* HPPB FDDI has different inbound vs outbound trace records */
484             if (subsys == NETTL_SUBSYS_HPPB_FDDI) {
485                 if (pntohl(&rec_hdr.kind) == NETTL_HDR_PDUIN) {
486                     /* inbound is very strange...
487                        there are an extra 3 bytes after the DSAP and SSAP
488                        for SNAP frames ???
489                     */
490                     *fddihack=TRUE;
491                     padlen = 0;
492                 } else {
493                     /* outbound appears to have variable padding */
494                     bytes_read = file_read(dummyc, 1, 9, fh);
495                     if (bytes_read != 9) {
496                         *err = file_error(fh);
497                         if (*err == 0)
498                             *err = WTAP_ERR_SHORT_READ;
499                         return -1;
500                     }
501                     /* padding is usually either a total 11 or 16 bytes??? */
502                     padlen = (int)dummyc[8];
503                     if (file_seek(fh, padlen, SEEK_CUR, err) == -1)
504                         return -1;
505                     padlen += 9;
506                     offset += padlen;
507                 }
508             } else if ( (subsys == NETTL_SUBSYS_PCI_FDDI)
509                      || (subsys == NETTL_SUBSYS_EISA_FDDI)
510                      || (subsys == NETTL_SUBSYS_HSC_FDDI) ) {
511                 /* other flavor FDDI cards have an extra 3 bytes of padding */
512                 if (file_seek(fh, 3, SEEK_CUR, err) == -1)
513                     return -1;
514                 padlen = 3;
515                 offset += padlen;
516             } else if (subsys == NETTL_SUBSYS_NS_LS_LOOPBACK) {
517                 /* LOOPBACK has an extra 26 bytes of padding */
518                 if (file_seek(fh, 26, SEEK_CUR, err) == -1)
519                     return -1;
520                 padlen = 26;
521                 offset += padlen;
522             } else if (subsys == NETTL_SUBSYS_NS_LS_SCTP) {
523                 /*
524                  * SCTP 8 byte header that we will ignore...
525                  * 32 bit integer defines format
526                  *   1 = Log
527                  *   2 = ASCII
528                  *   3 = Binary (PDUs should be Binary format)
529                  * 32 bit integer defines type
530                  *   1 = Inbound
531                  *   2 = Outbound
532                  */
533                 if (file_seek(fh, 8, SEEK_CUR, err) == -1)
534                     return -1;
535                 padlen = 8;
536                 offset += padlen;   
537             } else {
538                 padlen = 0;
539             }
540             break;
541
542         case NETTL_SUBSYS_NS_LS_DRIVER :
543             /* XXX we dont know how to identify this as ethernet frames, so
544                we assumes everything is. We will crash and burn for anything else */
545             /* for encapsulated 100baseT we do this */
546             phdr->pkt_encap = WTAP_ENCAP_NETTL_ETHERNET;
547             bytes_read = file_read(&drv_eth_hdr, 1, NS_LS_DRV_ETH_HDR_LEN, fh);
548             if (bytes_read != NS_LS_DRV_ETH_HDR_LEN) {
549                 *err = file_error(fh);
550                 if (*err == 0)
551                     *err = WTAP_ERR_SHORT_READ;
552                 return -1;
553             }
554             offset += NS_LS_DRV_ETH_HDR_LEN;
555
556             length = pntohs(&drv_eth_hdr.length); 
557             caplen = pntohs(&drv_eth_hdr.caplen);
558             /*
559              * XXX - is there a length field that would give the length
560              * of this header, so that we don't have to check for
561              * nettl files from HP-UX 11?
562              *
563              * And what are the extra two bytes?
564              */
565             if (wth->capture.nettl->is_hpux_11) {
566                 if (file_seek(fh, 2, SEEK_CUR, err) == -1) return -1;
567                 offset += 2;
568             }
569             padlen = 0;
570             break;
571
572         case NETTL_SUBSYS_SX25L2:
573         case NETTL_SUBSYS_SX25L3:
574             /*
575              * XXX - is the 24-byte padding actually a header with
576              * packet lengths, time stamps, etc., just as is the case
577              * for NETTL_SUBSYS_NS_LS_DRIVER?  It might be
578              *
579              *    guint8        caplen[2];
580              *    guint8        length[2];
581              *    guint8        xxc[4];
582              *    guint8        sec[4];
583              *    guint8        usec[4];
584              *    guint8        xxd[4];
585              *
586              * or something such as that - if it has 4 bytes before that
587              * (making it 24 bytes), it'd be like struct
588              * nettlrec_ns_ls_drv_eth_hdr but with 2 more bytes at the end.
589              *
590              * And is "from_dce" at xxa[0] in the nettlrec_hdr structure?
591              */
592             phdr->pkt_encap = WTAP_ENCAP_NETTL_X25;
593             length = pntohl(&rec_hdr.length);
594             caplen = pntohl(&rec_hdr.caplen);
595             padlen = 24;        /* sizeof (struct nettlrec_sx25l2_hdr) - NETTL_REC_HDR_LEN + 4 */
596             if (file_seek(fh, padlen, SEEK_CUR, err) == -1)
597                 return -1;
598             offset += padlen;
599             break;
600
601         default:
602             /* We're going to assume it's ethernet if we don't recognize the
603                subsystem -- We'll probably spew junks and core if it isn't... */
604             wth->file_encap = WTAP_ENCAP_PER_PACKET;
605             phdr->pkt_encap = WTAP_ENCAP_NETTL_ETHERNET;
606             length = pntohl(&rec_hdr.length);
607             caplen = pntohl(&rec_hdr.caplen);
608             padlen = 0;
609             break;
610     }
611
612     if (length < padlen) {
613         *err = WTAP_ERR_BAD_RECORD;
614         *err_info = g_strdup_printf("nettl: packet length %u in record header too short, less than %u",
615             length, padlen);
616         return -1;
617     }
618     phdr->len = length - padlen;
619     if (caplen < padlen) {
620         *err = WTAP_ERR_BAD_RECORD;
621         *err_info = g_strdup_printf("nettl: captured length %u in record header too short, less than %u",
622             caplen, padlen);
623         return -1;
624     }
625     phdr->caplen = caplen - padlen;
626     phdr->ts.secs = pntohl(&rec_hdr.sec);
627     phdr->ts.nsecs = pntohl(&rec_hdr.usec) * 1000;
628
629     pseudo_header->nettl.subsys   = subsys;
630     pseudo_header->nettl.devid    = pntohl(&rec_hdr.devid);
631     pseudo_header->nettl.kind     = pntohl(&rec_hdr.kind);
632     pseudo_header->nettl.pid      = pntohl(&rec_hdr.pid);
633     pseudo_header->nettl.uid      = pntohs(&rec_hdr.uid);
634
635     return offset;
636 }
637
638 static gboolean
639 nettl_read_rec_data(FILE_T fh, guchar *pd, int length, int *err, gboolean fddihack)
640 {
641     int bytes_read;
642     guchar *p=NULL;
643     guint8 dummy[3];
644
645     if (fddihack == TRUE) {
646        /* read in FC, dest, src, DSAP and SSAP */
647        if (file_read(pd, 1, 15, fh) == 15) {
648           if (pd[13] == 0xAA) {
649              /* it's SNAP, have to eat 3 bytes??? */
650              if (file_read(dummy, 1, 3, fh) == 3) {
651                 p=pd+15;
652                 bytes_read = file_read(p, 1, length-18, fh);
653                 bytes_read += 18;
654              } else {
655                 bytes_read = -1;
656              }
657           } else {
658              /* not SNAP */
659              p=pd+15;
660              bytes_read = file_read(p, 1, length-15, fh);
661              bytes_read += 15;
662           }
663        } else
664           bytes_read = -1;
665     } else
666        bytes_read = file_read(pd, 1, length, fh);
667
668     if (bytes_read != length) {
669         *err = file_error(fh);
670         if (*err == 0)
671             *err = WTAP_ERR_SHORT_READ;
672         return FALSE;
673     }
674     return TRUE;
675 }
676
677 static void nettl_close(wtap *wth)
678 {
679     g_free(wth->capture.nettl);
680 }
681
682
683 /* Returns 0 if we could write the specified encapsulation type,
684    an error indication otherwise.  nettl files are WTAP_ENCAP_UNKNOWN
685    when they are first opened, so we allow that for tshark read/write.
686  */
687
688 int nettl_dump_can_write_encap(int encap)
689 {
690
691         switch (encap) {
692                 case WTAP_ENCAP_ETHERNET:
693                 case WTAP_ENCAP_FDDI_BITSWAPPED:
694                 case WTAP_ENCAP_TOKEN_RING:
695                 case WTAP_ENCAP_NETTL_ETHERNET:
696                 case WTAP_ENCAP_NETTL_FDDI:
697                 case WTAP_ENCAP_NETTL_TOKEN_RING:
698                 case WTAP_ENCAP_NETTL_RAW_IP:
699                 case WTAP_ENCAP_NETTL_RAW_ICMP:
700                 case WTAP_ENCAP_NETTL_RAW_ICMPV6:
701                 case WTAP_ENCAP_NETTL_RAW_TELNET:
702 /*
703                 case WTAP_ENCAP_NETTL_X25:
704 */
705                 case WTAP_ENCAP_PER_PACKET:
706                 case WTAP_ENCAP_UNKNOWN:
707                 case WTAP_ENCAP_NETTL_UNKNOWN:
708                         return 0;
709                 default:
710                         return WTAP_ERR_UNSUPPORTED_ENCAP;
711         }
712 }
713
714
715 /* Returns TRUE on success, FALSE on failure;
716    sets "*err" to an error code on failure */
717 gboolean nettl_dump_open(wtap_dumper *wdh, gboolean cant_seek _U_, int *err)
718 {
719         struct nettl_file_hdr file_hdr;
720         size_t nwritten;
721
722         /* This is a nettl file */
723         wdh->subtype_write = nettl_dump;
724         wdh->subtype_close = NULL;
725
726         /* Write the file header. */
727         memset(&file_hdr,0,sizeof(file_hdr));
728         memcpy(file_hdr.magic,nettl_magic_hpux10,sizeof(file_hdr.magic));
729         g_strlcpy(file_hdr.file_name,"/tmp/wireshark.TRC000",NETTL_FILENAME_SIZE);
730         g_strlcpy(file_hdr.tz,"UTC",20);
731         g_strlcpy(file_hdr.host_name,"",9);
732         g_strlcpy(file_hdr.os_vers,"B.11.11",9);
733         file_hdr.os_v=0x55;
734         g_strlcpy(file_hdr.model,"9000/800",11);
735         file_hdr.unknown=g_htons(0x406);
736         nwritten = fwrite(&file_hdr, 1, sizeof file_hdr, wdh->fh);
737         if (nwritten != sizeof(file_hdr)) {
738                 if (nwritten == 0 && ferror(wdh->fh))
739                         *err = errno;
740                 else
741                         *err = WTAP_ERR_SHORT_WRITE;
742                 return FALSE;
743         }
744         wdh->bytes_dumped += sizeof(file_hdr);
745
746         return TRUE;
747 }
748
749 /* Write a record for a packet to a dump file.
750    Returns TRUE on success, FALSE on failure. */
751 static gboolean nettl_dump(wtap_dumper *wdh,
752         const struct wtap_pkthdr *phdr,
753         const union wtap_pseudo_header *pseudo_header _U_,
754         const guchar *pd, int *err)
755 {
756         struct nettlrec_hdr rec_hdr;
757         size_t nwritten;
758         guint8 dummyc[24];
759
760         memset(&rec_hdr,0,sizeof(rec_hdr));
761         /* HP-UX 11.X header should be 68 bytes */
762         rec_hdr.hdr_len = g_htons(sizeof(rec_hdr) + 4);
763         rec_hdr.kind = g_htonl(NETTL_HDR_PDUIN);
764         rec_hdr.sec = g_htonl(phdr->ts.secs);
765         rec_hdr.usec = g_htonl(phdr->ts.nsecs/1000);
766         rec_hdr.caplen = g_htonl(phdr->caplen);
767         rec_hdr.length = g_htonl(phdr->len);
768         rec_hdr.devid = -1;
769         rec_hdr.pid = -1;
770         rec_hdr.uid = -1;
771
772         switch (phdr->pkt_encap) {
773
774                 case WTAP_ENCAP_NETTL_FDDI:
775                         /* account for pad bytes */
776                         rec_hdr.caplen = g_htonl(phdr->caplen + 3);
777                         rec_hdr.length = g_htonl(phdr->len + 3);
778                         /* fall through and fill the rest of the fields */
779                 case WTAP_ENCAP_NETTL_ETHERNET:
780                 case WTAP_ENCAP_NETTL_TOKEN_RING:
781                 case WTAP_ENCAP_NETTL_RAW_IP:
782                 case WTAP_ENCAP_NETTL_RAW_ICMP:
783                 case WTAP_ENCAP_NETTL_RAW_ICMPV6:
784                 case WTAP_ENCAP_NETTL_RAW_TELNET:
785                 case WTAP_ENCAP_NETTL_UNKNOWN:
786                         rec_hdr.subsys = g_htons(pseudo_header->nettl.subsys);
787                         rec_hdr.devid = g_htonl(pseudo_header->nettl.devid);
788                         rec_hdr.kind = g_htonl(pseudo_header->nettl.kind);
789                         rec_hdr.pid = g_htonl(pseudo_header->nettl.pid);
790                         rec_hdr.uid = g_htons(pseudo_header->nettl.uid);
791                         break;
792
793                 case WTAP_ENCAP_RAW_IP:
794                         rec_hdr.subsys = g_htons(NETTL_SUBSYS_NS_LS_IP);
795                         break;
796
797                 case WTAP_ENCAP_ETHERNET:
798                         rec_hdr.subsys = g_htons(NETTL_SUBSYS_BTLAN);
799                         break;
800
801                 case WTAP_ENCAP_FDDI_BITSWAPPED:
802                         rec_hdr.subsys = g_htons(NETTL_SUBSYS_PCI_FDDI);
803                         /* account for pad bytes */
804                         rec_hdr.caplen = g_htonl(phdr->caplen + 3);
805                         rec_hdr.length = g_htonl(phdr->len + 3);
806                         break;
807
808                 case WTAP_ENCAP_TOKEN_RING:
809                         rec_hdr.subsys = g_htons(NETTL_SUBSYS_PCI_TR);
810                         break;
811 /*      
812                 case WTAP_ENCAP_NETTL_X25:
813                         rec_hdr.caplen = g_htonl(phdr->caplen + 24);
814                         rec_hdr.length = g_htonl(phdr->len + 24);
815                         rec_hdr.subsys = g_htons(pseudo_header->nettl.subsys);
816                         rec_hdr.devid = g_htonl(pseudo_header->nettl.devid);
817                         rec_hdr.kind = g_htonl(pseudo_header->nettl.kind);
818                         rec_hdr.pid = g_htonl(pseudo_header->nettl.pid);
819                         rec_hdr.uid = g_htons(pseudo_header->nettl.uid);
820                         break;
821 */
822                 default:
823                         /* found one we don't support */
824                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
825                         return FALSE;
826         }
827
828         nwritten = fwrite(&rec_hdr, 1, sizeof(rec_hdr), wdh->fh);
829         if (nwritten != sizeof(rec_hdr)) {
830                 if (nwritten == 0 && ferror(wdh->fh))
831                         *err = errno;
832                 else
833                         *err = WTAP_ERR_SHORT_WRITE;
834                 return FALSE;
835         }
836         wdh->bytes_dumped += sizeof(rec_hdr);
837
838         /* Write out 4 extra bytes of unknown stuff for HP-UX11
839          * header format.
840          */
841         memset(dummyc, 0, sizeof dummyc);
842         nwritten = fwrite(dummyc, 1, 4, wdh->fh);
843         if (nwritten != 4) {
844                 if (nwritten == 0 && ferror(wdh->fh))
845                         *err = errno;
846                 else
847                         *err = WTAP_ERR_SHORT_WRITE;
848                 return FALSE;
849         }
850         wdh->bytes_dumped += 4;
851
852         if ((phdr->pkt_encap == WTAP_ENCAP_FDDI_BITSWAPPED) ||
853             (phdr->pkt_encap == WTAP_ENCAP_NETTL_FDDI)) {
854                 /* add those weird 3 bytes of padding */
855                 nwritten = fwrite(dummyc, 1, 3, wdh->fh);
856                 if (nwritten != 3) {
857                         if (nwritten == 0 && ferror(wdh->fh))
858                                 *err = errno;
859                         else
860                                 *err = WTAP_ERR_SHORT_WRITE;
861                         return FALSE;
862                 }
863                 wdh->bytes_dumped += 3;
864         }
865 /*
866         } else if (phdr->pkt_encap == WTAP_ENCAP_NETTL_X25) {
867                 nwritten = fwrite(dummyc, 1, 24, wdh->fh);
868                 if (nwritten != 24) {
869                         if (nwritten == 0 && ferror(wdh->fh))
870                                 *err = errno;
871                         else
872                                 *err = WTAP_ERR_SHORT_WRITE;
873                         return FALSE;
874                 }
875                 wdh->bytes_dumped += 24;
876         }
877 */
878
879         /* write actual PDU data */
880
881         nwritten = fwrite(pd, 1, phdr->caplen, wdh->fh);
882         if (nwritten != phdr->caplen) {
883                 if (nwritten == 0 && ferror(wdh->fh))
884                         *err = errno;
885                 else
886                         *err = WTAP_ERR_SHORT_WRITE;
887                 return FALSE;
888         }
889         wdh->bytes_dumped += phdr->caplen;
890
891         return TRUE;
892 }