From Harald Welte:
[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 const 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 const 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     guint8      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 typedef struct {
179         gboolean is_hpux_11;
180 } nettl_t;
181
182 static gboolean nettl_read(wtap *wth, int *err, gchar **err_info,
183                 gint64 *data_offset);
184 static gboolean nettl_seek_read(wtap *wth, gint64 seek_off,
185                 union wtap_pseudo_header *pseudo_header, guint8 *pd,
186                 int length, int *err, gchar **err_info);
187 static int nettl_read_rec_header(wtap *wth, FILE_T fh,
188                 struct wtap_pkthdr *phdr, union wtap_pseudo_header *pseudo_header,
189                 int *err, gchar **err_info, gboolean *fddihack);
190 static gboolean nettl_read_rec_data(FILE_T fh, guint8 *pd, int length,
191                 int *err, gchar **err_info, gboolean fddihack);
192 static gboolean nettl_dump(wtap_dumper *wdh, const struct wtap_pkthdr *phdr,
193     const union wtap_pseudo_header *pseudo_header, const guint8 *pd, int *err);
194
195 int nettl_open(wtap *wth, int *err, gchar **err_info)
196 {
197     struct nettl_file_hdr file_hdr;
198     guint16 dummy[2];
199     int subsys;
200     int bytes_read;
201     nettl_t *nettl;
202
203     memset(&file_hdr, 0, sizeof(file_hdr));
204
205     /* Read in the string that should be at the start of a HP file */
206     errno = WTAP_ERR_CANT_READ;
207     bytes_read = file_read(file_hdr.magic, MAGIC_SIZE, wth->fh);
208     if (bytes_read != MAGIC_SIZE) {
209         *err = file_error(wth->fh, err_info);
210         if (*err != 0)
211             return -1;
212         return 0;
213     }
214
215     if (memcmp(file_hdr.magic, nettl_magic_hpux9, MAGIC_SIZE) &&
216         memcmp(file_hdr.magic, nettl_magic_hpux10, MAGIC_SIZE)) {
217         return 0;
218     }
219
220     /* Read the rest of the file header */
221     bytes_read = file_read(file_hdr.file_name, FILE_HDR_SIZE - MAGIC_SIZE,
222                            wth->fh);
223     if (bytes_read != FILE_HDR_SIZE - MAGIC_SIZE) {
224         *err = file_error(wth->fh, err_info);
225         if (*err != 0)
226             return -1;
227         return 0;
228     }
229
230     /* This is an nettl file */
231     wth->file_type = WTAP_FILE_NETTL;
232     nettl = g_malloc(sizeof(nettl_t));
233     wth->priv = (void *)nettl;
234     if (file_hdr.os_vers[2] == '1' && file_hdr.os_vers[3] == '1')
235         nettl->is_hpux_11 = TRUE;
236     else
237         nettl->is_hpux_11 = FALSE;
238     wth->subtype_read = nettl_read;
239     wth->subtype_seek_read = nettl_seek_read;
240     wth->snapshot_length = 0;   /* not available */
241
242     /* read the first header to take a guess at the file encap */
243     bytes_read = file_read(dummy, 4, wth->fh);
244     if (bytes_read != 4) {
245         if (*err != 0) {
246             wth->priv = NULL;
247             g_free(nettl);
248             return -1;
249         }
250         if (bytes_read != 0) {
251             *err = WTAP_ERR_SHORT_READ;
252             wth->priv = NULL;
253             g_free(nettl);
254             return -1;
255         }
256         wth->priv = NULL;
257         g_free(nettl);
258         return 0;
259     }
260
261     subsys = g_ntohs(dummy[1]);
262     switch (subsys) {
263         case NETTL_SUBSYS_HPPB_FDDI :
264         case NETTL_SUBSYS_EISA_FDDI :
265         case NETTL_SUBSYS_PCI_FDDI :
266         case NETTL_SUBSYS_HSC_FDDI :
267                 wth->file_encap = WTAP_ENCAP_NETTL_FDDI;
268                 break;
269         case NETTL_SUBSYS_TOKEN :
270         case NETTL_SUBSYS_PCI_TR :
271                 wth->file_encap = WTAP_ENCAP_NETTL_TOKEN_RING;
272                 break;
273         case NETTL_SUBSYS_NS_LS_IP :
274         case NETTL_SUBSYS_NS_LS_LOOPBACK :
275         case NETTL_SUBSYS_NS_LS_TCP :
276         case NETTL_SUBSYS_NS_LS_UDP :
277         case NETTL_SUBSYS_NS_LS_IPV6 :
278                 wth->file_encap = WTAP_ENCAP_NETTL_RAW_IP;
279                 break;
280         case NETTL_SUBSYS_NS_LS_ICMP :
281                 wth->file_encap = WTAP_ENCAP_NETTL_RAW_ICMP;
282                 break;
283         case NETTL_SUBSYS_NS_LS_ICMPV6 :
284                 wth->file_encap = WTAP_ENCAP_NETTL_RAW_ICMPV6;
285                 break;
286         case NETTL_SUBSYS_NS_LS_TELNET :
287                 wth->file_encap = WTAP_ENCAP_NETTL_RAW_TELNET;
288                 break;
289         default:
290                 /* If this assumption is bad, the read will catch it */
291                 wth->file_encap = WTAP_ENCAP_NETTL_ETHERNET;
292     }
293
294     if (file_seek(wth->fh, FILE_HDR_SIZE, SEEK_SET, err) == -1) {
295         g_free(nettl);
296         return -1;
297     }
298     wth->data_offset = FILE_HDR_SIZE;
299     wth->tsprecision = WTAP_FILE_TSPREC_USEC;
300
301     return 1;
302 }
303
304 /* Read the next packet */
305 static gboolean nettl_read(wtap *wth, int *err, gchar **err_info,
306     gint64 *data_offset)
307 {
308     int ret;
309     gboolean fddihack=FALSE;
310
311     /* Read record header. */
312     *data_offset = wth->data_offset;
313     ret = nettl_read_rec_header(wth, wth->fh, &wth->phdr, &wth->pseudo_header,
314         err, err_info, &fddihack);
315     if (ret <= 0) {
316         /* Read error or EOF */
317         return FALSE;
318     }
319     wth->data_offset += ret;
320
321     if (wth->phdr.caplen > WTAP_MAX_PACKET_SIZE) {
322         /*
323          * Probably a corrupt capture file; don't blow up trying
324          * to allocate space for an immensely-large packet.
325          */
326         *err = WTAP_ERR_BAD_FILE;
327         *err_info = g_strdup_printf("nettl: File has %u-byte packet, bigger than maximum of %u",
328             wth->phdr.caplen, WTAP_MAX_PACKET_SIZE);
329         return FALSE;
330     }
331
332     /*
333      * If the per-file encapsulation isn't known, set it to this
334      * packet's encapsulation.
335      *
336      * If it *is* known, and it isn't this packet's encapsulation,
337      * set it to WTAP_ENCAP_PER_PACKET, as this file doesn't
338      * have a single encapsulation for all packets in the file.
339      */
340     if (wth->file_encap == WTAP_ENCAP_UNKNOWN)
341         wth->file_encap = wth->phdr.pkt_encap;
342     else {
343         if (wth->file_encap != wth->phdr.pkt_encap)
344             wth->file_encap = WTAP_ENCAP_PER_PACKET;
345     }
346
347     /*
348      * Read the packet data.
349      */
350     buffer_assure_space(wth->frame_buffer, wth->phdr.caplen);
351     if (!nettl_read_rec_data(wth->fh, buffer_start_ptr(wth->frame_buffer),
352                 wth->phdr.caplen, err, err_info, fddihack))
353         return FALSE;   /* Read error */
354     wth->data_offset += wth->phdr.caplen;
355     return TRUE;
356 }
357
358 static gboolean
359 nettl_seek_read(wtap *wth, gint64 seek_off,
360                 union wtap_pseudo_header *pseudo_header, guint8 *pd,
361                 int length, int *err, gchar **err_info)
362 {
363     int ret;
364     struct wtap_pkthdr phdr;
365     gboolean fddihack=FALSE;
366
367     if (file_seek(wth->random_fh, seek_off, SEEK_SET, err) == -1)
368         return FALSE;
369
370     /* Read record header. */
371     ret = nettl_read_rec_header(wth, wth->random_fh, &phdr, pseudo_header,
372         err, err_info, &fddihack);
373     if (ret <= 0) {
374         /* Read error or EOF */
375         if (ret == 0) {
376             /* EOF means "short read" in random-access mode */
377             *err = WTAP_ERR_SHORT_READ;
378         }
379         return FALSE;
380     }
381
382     /*
383      * Read the packet data.
384      */
385     return nettl_read_rec_data(wth->random_fh, pd, length, err, err_info,
386         fddihack);
387 }
388
389 static int
390 nettl_read_rec_header(wtap *wth, FILE_T fh, struct wtap_pkthdr *phdr,
391                 union wtap_pseudo_header *pseudo_header, int *err,
392                 gchar **err_info, gboolean *fddihack)
393 {
394     nettl_t *nettl = (nettl_t *)wth->priv;
395     int bytes_read;
396     struct nettlrec_hdr rec_hdr;
397     guint16 hdr_len;
398     struct nettlrec_ns_ls_drv_eth_hdr drv_eth_hdr;
399     guint16 length, caplen;
400     int offset = 0;
401     int subsys;
402     int padlen;
403     guint8 dummyc[16];
404
405     errno = WTAP_ERR_CANT_READ;
406     bytes_read = file_read(&rec_hdr.hdr_len, sizeof rec_hdr.hdr_len, fh);
407     if (bytes_read != sizeof rec_hdr.hdr_len) {
408         *err = file_error(fh, err_info);
409         if (*err != 0)
410             return -1;
411         if (bytes_read != 0) {
412             *err = WTAP_ERR_SHORT_READ;
413             return -1;
414         }
415         return 0;
416     }
417     offset += 2;
418     hdr_len = g_ntohs(rec_hdr.hdr_len);
419     if (hdr_len < NETTL_REC_HDR_LEN) {
420         *err = WTAP_ERR_BAD_FILE;
421         *err_info = g_strdup_printf("nettl: record header length %u too short",
422             hdr_len);
423         return -1;
424     }
425     bytes_read = file_read(&rec_hdr.subsys, NETTL_REC_HDR_LEN - 2, fh);
426     if (bytes_read != NETTL_REC_HDR_LEN - 2) {
427         *err = file_error(fh, err_info);
428         if (*err == 0)
429             *err = WTAP_ERR_SHORT_READ;
430         return -1;
431     }
432     offset += NETTL_REC_HDR_LEN - 2;
433     subsys = g_ntohs(rec_hdr.subsys);
434     hdr_len -= NETTL_REC_HDR_LEN;
435     if (file_seek(fh, hdr_len, SEEK_CUR, err) == -1)
436         return -1;
437     offset += hdr_len;
438
439     if ( (pntohl(&rec_hdr.kind) & NETTL_HDR_PDU_MASK) == 0 ) {
440         /* not actually a data packet (PDU) trace record */
441         phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_IP;
442         length = pntohl(&rec_hdr.length);
443         caplen = pntohl(&rec_hdr.caplen);
444         padlen = 0;
445     } else switch (subsys) {
446         case NETTL_SUBSYS_LAN100 :
447         case NETTL_SUBSYS_EISA100BT :
448         case NETTL_SUBSYS_BASE100 :
449         case NETTL_SUBSYS_GSC100BT :
450         case NETTL_SUBSYS_PCI100BT :
451         case NETTL_SUBSYS_SPP100BT :
452         case NETTL_SUBSYS_100VG :
453         case NETTL_SUBSYS_GELAN :
454         case NETTL_SUBSYS_BTLAN :
455         case NETTL_SUBSYS_INTL100 :
456         case NETTL_SUBSYS_IGELAN :
457         case NETTL_SUBSYS_IETHER :
458         case NETTL_SUBSYS_IXGBE :
459         case NETTL_SUBSYS_HSSN :
460         case NETTL_SUBSYS_IGSSN :
461         case NETTL_SUBSYS_ICXGBE :
462         case NETTL_SUBSYS_IEXGBE :
463         case NETTL_SUBSYS_HPPB_FDDI :
464         case NETTL_SUBSYS_EISA_FDDI :
465         case NETTL_SUBSYS_PCI_FDDI :
466         case NETTL_SUBSYS_HSC_FDDI :
467         case NETTL_SUBSYS_TOKEN :
468         case NETTL_SUBSYS_PCI_TR :
469         case NETTL_SUBSYS_NS_LS_IP :
470         case NETTL_SUBSYS_NS_LS_LOOPBACK :
471         case NETTL_SUBSYS_NS_LS_TCP :
472         case NETTL_SUBSYS_NS_LS_UDP :
473         case NETTL_SUBSYS_HP_APAPORT :
474         case NETTL_SUBSYS_HP_APALACP :
475         case NETTL_SUBSYS_NS_LS_IPV6 :
476         case NETTL_SUBSYS_NS_LS_ICMPV6 :
477         case NETTL_SUBSYS_NS_LS_ICMP :
478         case NETTL_SUBSYS_NS_LS_TELNET :
479         case NETTL_SUBSYS_NS_LS_SCTP :
480             if( (subsys == NETTL_SUBSYS_NS_LS_IP)
481              || (subsys == NETTL_SUBSYS_NS_LS_LOOPBACK)
482              || (subsys == NETTL_SUBSYS_NS_LS_UDP)
483              || (subsys == NETTL_SUBSYS_NS_LS_TCP)
484              || (subsys == NETTL_SUBSYS_NS_LS_SCTP)
485              || (subsys == NETTL_SUBSYS_NS_LS_IPV6)) {
486                 phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_IP;
487             } else if (subsys == NETTL_SUBSYS_NS_LS_ICMP) {
488                 phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_ICMP;
489             } else if (subsys == NETTL_SUBSYS_NS_LS_ICMPV6) {
490                 phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_ICMPV6;
491             } else if (subsys == NETTL_SUBSYS_NS_LS_TELNET) {
492                 phdr->pkt_encap = WTAP_ENCAP_NETTL_RAW_TELNET;
493             } else if( (subsys == NETTL_SUBSYS_HPPB_FDDI)
494                     || (subsys == NETTL_SUBSYS_EISA_FDDI)
495                     || (subsys == NETTL_SUBSYS_PCI_FDDI)
496                     || (subsys == NETTL_SUBSYS_HSC_FDDI) ) {
497                 phdr->pkt_encap = WTAP_ENCAP_NETTL_FDDI;
498             } else if( (subsys == NETTL_SUBSYS_PCI_TR)
499                     || (subsys == NETTL_SUBSYS_TOKEN) ) {
500                 phdr->pkt_encap = WTAP_ENCAP_NETTL_TOKEN_RING;
501             } else {
502                 phdr->pkt_encap = WTAP_ENCAP_NETTL_ETHERNET;
503             }
504
505             length = pntohl(&rec_hdr.length);
506             caplen = pntohl(&rec_hdr.caplen);
507
508             /* HPPB FDDI has different inbound vs outbound trace records */
509             if (subsys == NETTL_SUBSYS_HPPB_FDDI) {
510                 if (pntohl(&rec_hdr.kind) == NETTL_HDR_PDUIN) {
511                     /* inbound is very strange...
512                        there are an extra 3 bytes after the DSAP and SSAP
513                        for SNAP frames ???
514                     */
515                     *fddihack=TRUE;
516                     padlen = 0;
517                 } else {
518                     /* outbound appears to have variable padding */
519                     bytes_read = file_read(dummyc, 9, fh);
520                     if (bytes_read != 9) {
521                         *err = file_error(fh, err_info);
522                         if (*err == 0)
523                             *err = WTAP_ERR_SHORT_READ;
524                         return -1;
525                     }
526                     /* padding is usually either a total 11 or 16 bytes??? */
527                     padlen = (int)dummyc[8];
528                     if (file_seek(fh, padlen, SEEK_CUR, err) == -1)
529                         return -1;
530                     padlen += 9;
531                     offset += padlen;
532                 }
533             } else if ( (subsys == NETTL_SUBSYS_PCI_FDDI)
534                      || (subsys == NETTL_SUBSYS_EISA_FDDI)
535                      || (subsys == NETTL_SUBSYS_HSC_FDDI) ) {
536                 /* other flavor FDDI cards have an extra 3 bytes of padding */
537                 if (file_seek(fh, 3, SEEK_CUR, err) == -1)
538                     return -1;
539                 padlen = 3;
540                 offset += padlen;
541             } else if (subsys == NETTL_SUBSYS_NS_LS_LOOPBACK) {
542                 /* LOOPBACK has an extra 26 bytes of padding */
543                 if (file_seek(fh, 26, SEEK_CUR, err) == -1)
544                     return -1;
545                 padlen = 26;
546                 offset += padlen;
547             } else if (subsys == NETTL_SUBSYS_NS_LS_SCTP) {
548                 /*
549                  * SCTP 8 byte header that we will ignore...
550                  * 32 bit integer defines format
551                  *   1 = Log
552                  *   2 = ASCII
553                  *   3 = Binary (PDUs should be Binary format)
554                  * 32 bit integer defines type
555                  *   1 = Inbound
556                  *   2 = Outbound
557                  */
558                 if (file_seek(fh, 8, SEEK_CUR, err) == -1)
559                     return -1;
560                 padlen = 8;
561                 offset += padlen;   
562             } else {
563                 padlen = 0;
564             }
565             break;
566
567         case NETTL_SUBSYS_NS_LS_DRIVER :
568             /* XXX we dont know how to identify this as ethernet frames, so
569                we assumes everything is. We will crash and burn for anything else */
570             /* for encapsulated 100baseT we do this */
571             phdr->pkt_encap = WTAP_ENCAP_NETTL_ETHERNET;
572             bytes_read = file_read(&drv_eth_hdr, NS_LS_DRV_ETH_HDR_LEN, fh);
573             if (bytes_read != NS_LS_DRV_ETH_HDR_LEN) {
574                 *err = file_error(fh, err_info);
575                 if (*err == 0)
576                     *err = WTAP_ERR_SHORT_READ;
577                 return -1;
578             }
579             offset += NS_LS_DRV_ETH_HDR_LEN;
580
581             length = pntohs(&drv_eth_hdr.length); 
582             caplen = pntohs(&drv_eth_hdr.caplen);
583             /*
584              * XXX - is there a length field that would give the length
585              * of this header, so that we don't have to check for
586              * nettl files from HP-UX 11?
587              *
588              * And what are the extra two bytes?
589              */
590             if (nettl->is_hpux_11) {
591                 if (file_seek(fh, 2, SEEK_CUR, err) == -1) return -1;
592                 offset += 2;
593             }
594             padlen = 0;
595             break;
596
597         case NETTL_SUBSYS_SX25L2:
598         case NETTL_SUBSYS_SX25L3:
599             /*
600              * XXX - is the 24-byte padding actually a header with
601              * packet lengths, time stamps, etc., just as is the case
602              * for NETTL_SUBSYS_NS_LS_DRIVER?  It might be
603              *
604              *    guint8        caplen[2];
605              *    guint8        length[2];
606              *    guint8        xxc[4];
607              *    guint8        sec[4];
608              *    guint8        usec[4];
609              *    guint8        xxd[4];
610              *
611              * or something such as that - if it has 4 bytes before that
612              * (making it 24 bytes), it'd be like struct
613              * nettlrec_ns_ls_drv_eth_hdr but with 2 more bytes at the end.
614              *
615              * And is "from_dce" at xxa[0] in the nettlrec_hdr structure?
616              */
617             phdr->pkt_encap = WTAP_ENCAP_NETTL_X25;
618             length = pntohl(&rec_hdr.length);
619             caplen = pntohl(&rec_hdr.caplen);
620             padlen = 24;        /* sizeof (struct nettlrec_sx25l2_hdr) - NETTL_REC_HDR_LEN + 4 */
621             if (file_seek(fh, padlen, SEEK_CUR, err) == -1)
622                 return -1;
623             offset += padlen;
624             break;
625
626         default:
627             /* We're going to assume it's ethernet if we don't recognize the
628                subsystem -- We'll probably spew junks and core if it isn't... */
629             wth->file_encap = WTAP_ENCAP_PER_PACKET;
630             phdr->pkt_encap = WTAP_ENCAP_NETTL_ETHERNET;
631             length = pntohl(&rec_hdr.length);
632             caplen = pntohl(&rec_hdr.caplen);
633             padlen = 0;
634             break;
635     }
636
637     if (length < padlen) {
638         *err = WTAP_ERR_BAD_FILE;
639         *err_info = g_strdup_printf("nettl: packet length %u in record header too short, less than %u",
640             length, padlen);
641         return -1;
642     }
643     phdr->len = length - padlen;
644     if (caplen < padlen) {
645         *err = WTAP_ERR_BAD_FILE;
646         *err_info = g_strdup_printf("nettl: captured length %u in record header too short, less than %u",
647             caplen, padlen);
648         return -1;
649     }
650     phdr->caplen = caplen - padlen;
651     phdr->ts.secs = pntohl(&rec_hdr.sec);
652     phdr->ts.nsecs = pntohl(&rec_hdr.usec) * 1000;
653
654     pseudo_header->nettl.subsys   = subsys;
655     pseudo_header->nettl.devid    = pntohl(&rec_hdr.devid);
656     pseudo_header->nettl.kind     = pntohl(&rec_hdr.kind);
657     pseudo_header->nettl.pid      = pntohl(&rec_hdr.pid);
658     pseudo_header->nettl.uid      = pntohs(&rec_hdr.uid);
659
660     return offset;
661 }
662
663 static gboolean
664 nettl_read_rec_data(FILE_T fh, guint8 *pd, int length, int *err,
665         gchar **err_info, gboolean fddihack)
666 {
667     int bytes_to_read, bytes_read;
668     guint8 dummy[3];
669
670     if (fddihack) {
671         /* read in FC, dest, src, DSAP and SSAP */
672         bytes_to_read = 15;
673         if (bytes_to_read > length)
674             bytes_to_read = length;
675         bytes_read = file_read(pd, bytes_to_read, fh);
676         if (bytes_read != bytes_to_read) {
677             if (*err == 0)
678                 *err = WTAP_ERR_SHORT_READ;
679             return FALSE;
680         }
681         length -= bytes_read;
682         if (length == 0) {
683                 /* There's nothing past the FC, dest, src, DSAP and SSAP */
684                 return TRUE;
685         }
686         if (pd[13] == 0xAA) {
687             /* it's SNAP, have to eat 3 bytes??? */
688             bytes_to_read = 3;
689             if (bytes_to_read > length)
690                 bytes_to_read = length;
691             bytes_read = file_read(dummy, bytes_to_read, fh);
692             if (bytes_read != bytes_to_read) {
693                 if (*err == 0)
694                     *err = WTAP_ERR_SHORT_READ;
695                 return FALSE;
696             }
697             length -= bytes_read;
698             if (length == 0) {
699                 /* There's nothing past the FC, dest, src, DSAP, SSAP, and 3 bytes to eat */
700                 return TRUE;
701             }
702         }
703         bytes_read = file_read(pd + 15, length, fh);
704     } else
705         bytes_read = file_read(pd, length, fh);
706
707     if (bytes_read != length) {
708         *err = file_error(fh, err_info);
709         if (*err == 0)
710             *err = WTAP_ERR_SHORT_READ;
711         return FALSE;
712     }
713     return TRUE;
714 }
715
716
717 /* Returns 0 if we could write the specified encapsulation type,
718    an error indication otherwise.  nettl files are WTAP_ENCAP_UNKNOWN
719    when they are first opened, so we allow that for tshark read/write.
720  */
721
722 int nettl_dump_can_write_encap(int encap)
723 {
724
725         switch (encap) {
726                 case WTAP_ENCAP_ETHERNET:
727                 case WTAP_ENCAP_FDDI_BITSWAPPED:
728                 case WTAP_ENCAP_TOKEN_RING:
729                 case WTAP_ENCAP_NETTL_ETHERNET:
730                 case WTAP_ENCAP_NETTL_FDDI:
731                 case WTAP_ENCAP_NETTL_TOKEN_RING:
732                 case WTAP_ENCAP_NETTL_RAW_IP:
733                 case WTAP_ENCAP_NETTL_RAW_ICMP:
734                 case WTAP_ENCAP_NETTL_RAW_ICMPV6:
735                 case WTAP_ENCAP_NETTL_RAW_TELNET:
736 /*
737                 case WTAP_ENCAP_NETTL_X25:
738 */
739                 case WTAP_ENCAP_PER_PACKET:
740                 case WTAP_ENCAP_UNKNOWN:
741                 case WTAP_ENCAP_NETTL_UNKNOWN:
742                         return 0;
743                 default:
744                         return WTAP_ERR_UNSUPPORTED_ENCAP;
745         }
746 }
747
748
749 /* Returns TRUE on success, FALSE on failure;
750    sets "*err" to an error code on failure */
751 gboolean nettl_dump_open(wtap_dumper *wdh, int *err)
752 {
753         struct nettl_file_hdr file_hdr;
754
755         /* This is a nettl file */
756         wdh->subtype_write = nettl_dump;
757         wdh->subtype_close = NULL;
758
759         /* Write the file header. */
760         memset(&file_hdr,0,sizeof(file_hdr));
761         memcpy(file_hdr.magic,nettl_magic_hpux10,sizeof(file_hdr.magic));
762         g_strlcpy(file_hdr.file_name,"/tmp/wireshark.TRC000",NETTL_FILENAME_SIZE);
763         g_strlcpy(file_hdr.tz,"UTC",20);
764         g_strlcpy(file_hdr.host_name,"",9);
765         g_strlcpy(file_hdr.os_vers,"B.11.11",9);
766         file_hdr.os_v=0x55;
767         g_strlcpy(file_hdr.model,"9000/800",11);
768         file_hdr.unknown=g_htons(0x406);
769         if (!wtap_dump_file_write(wdh, &file_hdr, sizeof file_hdr, err))
770                 return FALSE;
771         wdh->bytes_dumped += sizeof(file_hdr);
772
773         return TRUE;
774 }
775
776 /* Write a record for a packet to a dump file.
777    Returns TRUE on success, FALSE on failure. */
778 static gboolean nettl_dump(wtap_dumper *wdh,
779         const struct wtap_pkthdr *phdr,
780         const union wtap_pseudo_header *pseudo_header _U_,
781         const guint8 *pd, int *err)
782 {
783         struct nettlrec_hdr rec_hdr;
784         guint8 dummyc[24];
785
786         memset(&rec_hdr,0,sizeof(rec_hdr));
787         /* HP-UX 11.X header should be 68 bytes */
788         rec_hdr.hdr_len = g_htons(sizeof(rec_hdr) + 4);
789         rec_hdr.kind = g_htonl(NETTL_HDR_PDUIN);
790         rec_hdr.sec = g_htonl(phdr->ts.secs);
791         rec_hdr.usec = g_htonl(phdr->ts.nsecs/1000);
792         rec_hdr.caplen = g_htonl(phdr->caplen);
793         rec_hdr.length = g_htonl(phdr->len);
794         rec_hdr.devid = -1;
795         rec_hdr.pid = -1;
796         rec_hdr.uid = -1;
797
798         switch (phdr->pkt_encap) {
799
800                 case WTAP_ENCAP_NETTL_FDDI:
801                         /* account for pad bytes */
802                         rec_hdr.caplen = g_htonl(phdr->caplen + 3);
803                         rec_hdr.length = g_htonl(phdr->len + 3);
804                         /* fall through and fill the rest of the fields */
805                 case WTAP_ENCAP_NETTL_ETHERNET:
806                 case WTAP_ENCAP_NETTL_TOKEN_RING:
807                 case WTAP_ENCAP_NETTL_RAW_IP:
808                 case WTAP_ENCAP_NETTL_RAW_ICMP:
809                 case WTAP_ENCAP_NETTL_RAW_ICMPV6:
810                 case WTAP_ENCAP_NETTL_RAW_TELNET:
811                 case WTAP_ENCAP_NETTL_UNKNOWN:
812                         rec_hdr.subsys = g_htons(pseudo_header->nettl.subsys);
813                         rec_hdr.devid = g_htonl(pseudo_header->nettl.devid);
814                         rec_hdr.kind = g_htonl(pseudo_header->nettl.kind);
815                         rec_hdr.pid = g_htonl(pseudo_header->nettl.pid);
816                         rec_hdr.uid = g_htons(pseudo_header->nettl.uid);
817                         break;
818
819                 case WTAP_ENCAP_RAW_IP:
820                         rec_hdr.subsys = g_htons(NETTL_SUBSYS_NS_LS_IP);
821                         break;
822
823                 case WTAP_ENCAP_ETHERNET:
824                         rec_hdr.subsys = g_htons(NETTL_SUBSYS_BTLAN);
825                         break;
826
827                 case WTAP_ENCAP_FDDI_BITSWAPPED:
828                         rec_hdr.subsys = g_htons(NETTL_SUBSYS_PCI_FDDI);
829                         /* account for pad bytes */
830                         rec_hdr.caplen = g_htonl(phdr->caplen + 3);
831                         rec_hdr.length = g_htonl(phdr->len + 3);
832                         break;
833
834                 case WTAP_ENCAP_TOKEN_RING:
835                         rec_hdr.subsys = g_htons(NETTL_SUBSYS_PCI_TR);
836                         break;
837 /*      
838                 case WTAP_ENCAP_NETTL_X25:
839                         rec_hdr.caplen = g_htonl(phdr->caplen + 24);
840                         rec_hdr.length = g_htonl(phdr->len + 24);
841                         rec_hdr.subsys = g_htons(pseudo_header->nettl.subsys);
842                         rec_hdr.devid = g_htonl(pseudo_header->nettl.devid);
843                         rec_hdr.kind = g_htonl(pseudo_header->nettl.kind);
844                         rec_hdr.pid = g_htonl(pseudo_header->nettl.pid);
845                         rec_hdr.uid = g_htons(pseudo_header->nettl.uid);
846                         break;
847 */
848                 default:
849                         /* found one we don't support */
850                         *err = WTAP_ERR_UNSUPPORTED_ENCAP;
851                         return FALSE;
852         }
853
854         if (!wtap_dump_file_write(wdh, &rec_hdr, sizeof(rec_hdr), err))
855                 return FALSE;
856         wdh->bytes_dumped += sizeof(rec_hdr);
857
858         /* Write out 4 extra bytes of unknown stuff for HP-UX11
859          * header format.
860          */
861         memset(dummyc, 0, sizeof dummyc);
862         if (!wtap_dump_file_write(wdh, dummyc, 4, err))
863                 return FALSE;
864         wdh->bytes_dumped += 4;
865
866         if ((phdr->pkt_encap == WTAP_ENCAP_FDDI_BITSWAPPED) ||
867             (phdr->pkt_encap == WTAP_ENCAP_NETTL_FDDI)) {
868                 /* add those weird 3 bytes of padding */
869                 if (!wtap_dump_file_write(wdh, dummyc, 3, err))
870                         return FALSE;
871                 wdh->bytes_dumped += 3;
872         }
873 /*
874         } else if (phdr->pkt_encap == WTAP_ENCAP_NETTL_X25) {
875                 if (!wtap_dump_file_write(wdh, dummyc, 24, err))
876                         return FALSE;
877                 wdh->bytes_dumped += 24;
878         }
879 */
880
881         /* write actual PDU data */
882
883         if (!wtap_dump_file_write(wdh, pd, phdr->caplen, err))
884                 return FALSE;
885         wdh->bytes_dumped += phdr->caplen;
886
887         return TRUE;
888 }