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