Support for iptrace 1.0 traces (AIX 3). Thanks to Florian Lohoff
[metze/wireshark/wip.git] / wiretap / wtap.h
1 /* wtap.h
2  *
3  * $Id: wtap.h,v 1.50 1999/11/26 17:57:14 gram Exp $
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@verdict.uthscsa.edu>
7  * 
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  * 
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  * 
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  *
22  */
23
24 #ifndef __WTAP_H__
25 #define __WTAP_H__
26
27 /* Encapsulation types. Choose names that truly reflect
28  * what is contained in the packet trace file.
29  *
30  * WTAP_ENCAP_PER_PACKET is a value passed to "wtap_dump_open()" or
31  * "wtap_dump_fd_open()" to indicate that there is no single encapsulation
32  * type for all packets in the file; this may cause those routines to
33  * fail if the capture file format being written can't support that.
34  * It's also returned by "wtap_file_encap()" for capture files that
35  * don't have a single encapsulation type for all packets in the file.
36  *
37  * WTAP_ENCAP_UNKNOWN is returned by "wtap_pcap_encap_to_wtap_encap()"
38  * if it's handed an unknown encapsulation.
39  *
40  * WTAP_ENCAP_FDDI_BITSWAPPED is for FDDI captures on systems where the
41  * MAC addresses you get from the hardware are bit-swapped.  Ideally,
42  * the driver would tell us that, but I know of none that do, so, for
43  * now, we base it on the machine on which we're *reading* the
44  * capture, rather than on the machine on which the capture was taken
45  * (they're probably likely to be the same).  We assume that they're
46  * bit-swapped on everything except for systems running Ultrix, Alpha
47  * systems, and BSD/OS systems (that's what "tcpdump" does; I guess
48  * Digital decided to bit-swap addresses in the hardware or in the
49  * driver, and I guess BSDI bit-swapped them in the driver, given that
50  * BSD/OS generally runs on Boring Old PC's).  If we create a wiretap
51  * save file format, we'd use the WTAP_ENCAP values to flag the
52  * encapsulation of a packet, so there we'd at least be able to base
53  * it on the machine on which the capture was taken.
54  *
55  * WTAP_ENCAP_LINUX_ATM_CLIP is the encapsulation you get with the
56  * ATM on Linux code from <http://lrcwww.epfl.ch/linux-atm/>;
57  * that code adds a DLT_ATM_CLIP DLT_ code of 19, and that
58  * encapsulation isn't the same as the DLT_ATM_RFC1483 encapsulation
59  * presumably used on some BSD systems, which we turn into
60  * WTAP_ENCAP_ATM_RFC1483.
61  *
62  * WTAP_ENCAP_NULL corresponds to DLT_NULL from "libpcap".  This
63  * corresponds to
64  *
65  *      1) PPP-over-HDLC encapsulation, at least with some versions
66  *         of ISDN4BSD (but not the current ones, it appears, unless
67  *         I've missed something);
68  *
69  *      2) a 4-byte header containing the AF_ address family, in
70  *         the byte order of the machine that saved the capture,
71  *         for the packet, as used on many BSD systems for the
72  *         loopback device and some other devices;
73  *
74  *      3) a 4-byte header containing 2 octets of 0 and an Ethernet
75  *         type in the byte order from an Ethernet header, that being
76  *         what "libpcap" on Linux turns the Ethernet header for
77  *         loopback interfaces into. */
78 #define WTAP_ENCAP_PER_PACKET                   -1
79 #define WTAP_ENCAP_UNKNOWN                      0
80 #define WTAP_ENCAP_ETHERNET                     1
81 #define WTAP_ENCAP_TR                           2
82 #define WTAP_ENCAP_SLIP                         3
83 #define WTAP_ENCAP_PPP                          4
84 #define WTAP_ENCAP_FDDI                         5
85 #define WTAP_ENCAP_FDDI_BITSWAPPED              6
86 #define WTAP_ENCAP_RAW_IP                       7
87 #define WTAP_ENCAP_ARCNET                       8
88 #define WTAP_ENCAP_ATM_RFC1483                  9
89 #define WTAP_ENCAP_LINUX_ATM_CLIP               10
90 #define WTAP_ENCAP_LAPB                         11
91 #define WTAP_ENCAP_ATM_SNIFFER                  12
92 #define WTAP_ENCAP_NULL                         13
93 #define WTAP_ENCAP_ASCEND                       14
94 #define WTAP_ENCAP_LAPD                         15
95
96 /* last WTAP_ENCAP_ value + 1 */
97 #define WTAP_NUM_ENCAP_TYPES                    16
98
99 /* File types that can be read by wiretap.
100    We may eventually support writing some or all of these file types,
101    too, so we distinguish between different versions of them. */
102 #define WTAP_FILE_UNKNOWN                       0
103 #define WTAP_FILE_WTAP                          1
104 #define WTAP_FILE_PCAP                          2
105 #define WTAP_FILE_PCAP_MODIFIED                 3
106 #define WTAP_FILE_LANALYZER                     4
107 #define WTAP_FILE_NGSNIFFER                     5
108 #define WTAP_FILE_SNOOP                         6
109 #define WTAP_FILE_IPTRACE_1_0                   7
110 #define WTAP_FILE_IPTRACE_2_0                   8
111 #define WTAP_FILE_NETMON_1_x                    9
112 #define WTAP_FILE_NETMON_2_x                    10
113 #define WTAP_FILE_NETXRAY_1_0                   11
114 #define WTAP_FILE_NETXRAY_1_1                   12
115 #define WTAP_FILE_NETXRAY_2_001                 13
116 #define WTAP_FILE_RADCOM                        14
117 #define WTAP_FILE_ASCEND                        15
118 #define WTAP_FILE_NETTL                         16
119 #define WTAP_FILE_TOSHIBA                       17
120
121 /*
122  * Maximum packet size we'll support.
123  */
124 #define WTAP_MAX_PACKET_SIZE                    65535
125
126 #include <sys/types.h>
127
128 #ifdef HAVE_SYS_TIME_H
129 #include <sys/time.h>
130 #endif
131
132 #ifdef HAVE_WINSOCK_H
133 #include <winsock.h>
134 #endif
135
136 #include <glib.h>
137 #include <stdio.h>
138
139 #ifdef HAVE_LIBZ
140 #include "zlib.h"
141 #define FILE_T  gzFile
142 #else /* No zLib */
143 #define FILE_T  FILE *
144 #endif /* HAVE_LIBZ */
145
146 typedef struct {
147         double  timeunit;
148         time_t  start;
149         int     is_atm;
150 } ngsniffer_t;
151
152 typedef struct {
153         time_t  start;
154 } radcom_t;
155
156 typedef struct {
157         time_t  start;
158 } nettl_t;
159
160 typedef struct {
161         time_t  start;
162 } lanalyzer_t;
163
164 typedef struct {
165         gboolean byte_swapped;
166         gboolean modified;
167         guint16 version_major;
168         guint16 version_minor;
169 } libpcap_t;
170
171 typedef struct {
172         time_t  start_secs;
173         guint32 start_usecs;
174         guint8  version_major;
175         int     end_offset;
176 } netmon_t;
177
178 typedef struct {
179         time_t  start_time;
180         double  timeunit;
181         double  start_timestamp;
182         int     wrapped;
183         int     end_offset;
184         int     version_major;
185 } netxray_t;
186
187 typedef struct {
188         time_t inittime;
189         int adjusted;
190         int seek_add;
191 } ascend_t;
192
193 /* Packet "pseudo-header" information for X.25 capture files. */
194 struct x25_phdr {
195         guint8  flags; /* ENCAP_LAPB : 1st bit means From DCE */
196 };
197
198 /* Packet "pseudo-header" for ATM Sniffer capture files. */
199 struct ngsniffer_atm_phdr {
200         guint8  AppTrafType;    /* traffic type */
201         guint8  AppHLType;      /* protocol type */
202         guint16 Vpi;            /* virtual path identifier */
203         guint16 Vci;            /* virtual circuit identifier */
204         guint16 channel;        /* link: 0 for DCE, 1 for DTE */
205         guint16 cells;          /* number of cells */
206         guint16 aal5t_u2u;      /* user-to-user indicator */
207         guint16 aal5t_len;      /* length of the packet */
208         guint32 aal5t_chksum;   /* checksum for AAL5 packet */
209 };
210
211 /* Packet "pseudo-header" for the output from "wandsession", "wannext",
212    "wandisplay", and similar commands on Lucent/Ascend access equipment. */
213
214 #define ASCEND_MAX_STR_LEN 64
215
216 #define ASCEND_PFX_WDS_X 1
217 #define ASCEND_PFX_WDS_R 2
218 #define ASCEND_PFX_WDD   3
219
220 struct ascend_phdr {
221         guint16 type;                   /* ASCEND_PFX_*, as defined above */
222         char    user[ASCEND_MAX_STR_LEN];   /* Username, from wandsession header */
223         guint32 sess;                   /* Session number, from wandsession header */
224         char    call_num[ASCEND_MAX_STR_LEN];   /* Called number, from WDD header */
225         guint32 chunk;                  /* Chunk number, from WDD header */
226         guint32 task;                   /* Task number */
227 };
228
229 /* Packet "pseudo-header" information for LAPD capture files. */
230 struct lapd_phdr {
231         gboolean        from_network_to_user;
232 };
233
234 /*
235  * Bits in AppTrafType.
236  *
237  * For AAL types other than AAL5, the packet data is presumably for a
238  * single cell, not a reassembled frame, as the ATM Sniffer manual says
239  * it dosn't reassemble cells other than AAL5 cells.
240  */
241 #define ATT_AALTYPE             0x0F    /* AAL type: */
242 #define ATT_AAL_UNKNOWN         0x00    /* Unknown AAL */
243 #define ATT_AAL1                0x01    /* AAL1 */
244 #define ATT_AAL3_4              0x02    /* AAL3/4 */
245 #define ATT_AAL5                0x03    /* AAL5 */
246 #define ATT_AAL_USER            0x04    /* User AAL */
247 #define ATT_AAL_SIGNALLING      0x05    /* Signaling AAL */
248 #define ATT_OAMCELL             0x06    /* OAM cell */
249
250 #define ATT_HLTYPE              0xF0    /* Higher-layer type: */
251 #define ATT_HL_UNKNOWN          0x00    /* unknown */
252 #define ATT_HL_LLCMX            0x10    /* LLC multiplexed (probably RFC 1483) */
253 #define ATT_HL_VCMX             0x20    /* VC multiplexed (probably RFC 1483) */
254 #define ATT_HL_LANE             0x30    /* LAN Emulation */
255 #define ATT_HL_ILMI             0x40    /* ILMI */
256 #define ATT_HL_FRMR             0x50    /* Frame Relay */
257 #define ATT_HL_SPANS            0x60    /* FORE SPANS */
258 #define ATT_HL_IPSILON          0x70    /* Ipsilon */
259
260 /*
261  * Values for AppHLType; the interpretation depends on the ATT_HLTYPE
262  * bits in AppTrafType.
263  */
264 #define AHLT_UNKNOWN            0x0
265 #define AHLT_VCMX_802_3_FCS     0x1     /* VCMX: 802.3 FCS */
266 #define AHLT_LANE_LE_CTRL       0x1     /* LANE: LE Ctrl */
267 #define AHLT_IPSILON_FT0        0x1     /* Ipsilon: Flow Type 0 */
268 #define AHLT_VCMX_802_4_FCS     0x2     /* VCMX: 802.4 FCS */
269 #define AHLT_LANE_802_3         0x2     /* LANE: 802.3 */
270 #define AHLT_IPSILON_FT1        0x2     /* Ipsilon: Flow Type 1 */
271 #define AHLT_VCMX_802_5_FCS     0x3     /* VCMX: 802.5 FCS */
272 #define AHLT_LANE_802_5         0x3     /* LANE: 802.5 */
273 #define AHLT_IPSILON_FT2        0x3     /* Ipsilon: Flow Type 2 */
274 #define AHLT_VCMX_FDDI_FCS      0x4     /* VCMX: FDDI FCS */
275 #define AHLT_LANE_802_3_MC      0x4     /* LANE: 802.3 multicast */
276 #define AHLT_VCMX_802_6_FCS     0x5     /* VCMX: 802.6 FCS */
277 #define AHLT_LANE_802_5_MC      0x5     /* LANE: 802.5 multicast */
278 #define AHLT_VCMX_802_3         0x7     /* VCMX: 802.3 */
279 #define AHLT_VCMX_802_4         0x8     /* VCMX: 802.4 */
280 #define AHLT_VCMX_802_5         0x9     /* VCMX: 802.5 */
281 #define AHLT_VCMX_FDDI          0xa     /* VCMX: FDDI */
282 #define AHLT_VCMX_802_6         0xb     /* VCMX: 802.6 */
283 #define AHLT_VCMX_FRAGMENTS     0xc     /* VCMX: Fragments */
284 #define AHLT_VCMX_BPDU          0xe     /* VCMX: BPDU */
285
286 union pseudo_header {
287         struct x25_phdr                 x25;
288         struct ngsniffer_atm_phdr       ngsniffer_atm;
289         struct ascend_phdr              ascend;
290         struct lapd_phdr                lapd;
291 };
292
293 struct wtap_pkthdr {
294         struct timeval ts;
295         guint32 caplen;
296         guint32 len;
297         int pkt_encap;
298         union pseudo_header pseudo_header;
299 };
300
301 typedef void (*wtap_handler)(u_char*, const struct wtap_pkthdr*,
302                 int, const u_char *);
303
304 struct wtap;
305 struct Buffer;
306
307 typedef int (*subtype_read_func)(struct wtap*, int*);
308 typedef struct wtap {
309         FILE_T                  fh;
310         int                     fd;           /* File descriptor for cap file */
311         int                     file_type;
312         int                     snapshot_length;
313         struct Buffer           *frame_buffer;
314         struct wtap_pkthdr      phdr;
315
316         long                    data_offset;
317
318         union {
319                 libpcap_t               *pcap;
320                 lanalyzer_t             *lanalyzer;
321                 ngsniffer_t             *ngsniffer;
322                 radcom_t                *radcom;
323                 nettl_t                 *nettl;
324                 netmon_t                *netmon;
325                 netxray_t               *netxray;
326                 ascend_t                *ascend;
327         } capture;
328
329         subtype_read_func       subtype_read;
330         int                     file_encap;     /* per-file, for those
331                                                    file formats that have
332                                                    per-file encapsulation
333                                                    types */
334 } wtap;
335
336 struct wtap_dumper;
337
338 typedef int (*subtype_write_func)(struct wtap_dumper*,
339                 const struct wtap_pkthdr*, const u_char*, int*);
340 typedef int (*subtype_close_func)(struct wtap_dumper*, int*);
341 typedef struct wtap_dumper {
342         FILE*                   fh;
343         int                     file_type;
344         int                     snaplen;
345         int                     encap;
346
347         subtype_write_func      subtype_write;
348         subtype_close_func      subtype_close;
349 } wtap_dumper;
350
351 /*
352  * On failure, "wtap_open_offline()" returns NULL, and puts into the
353  * "int" pointed to by its second argument:
354  *
355  * a positive "errno" value if the capture file can't be opened;
356  *
357  * a negative number, indicating the type of error, on other failures.
358  */
359 wtap* wtap_open_offline(const char *filename, int *err);
360 int wtap_loop(wtap *wth, int, wtap_handler, u_char*, int*);
361
362 FILE* wtap_file(wtap *wth);
363 int wtap_fd(wtap *wth);
364 int wtap_snapshot_length(wtap *wth); /* per file */
365 int wtap_file_type(wtap *wth);
366 int wtap_file_encap(wtap *wth);
367 const char *wtap_file_type_string(wtap *wth);
368 const char *wtap_strerror(int err);
369 void wtap_close(wtap *wth);
370 int wtap_seek_read (int file_type, FILE *fh, int seek_off, guint8 *pd, int len);
371 int wtap_def_seek_read (FILE *fh, int seek_off, guint8 *pd, int len);
372
373 wtap_dumper* wtap_dump_open(const char *filename, int filetype, int encap,
374         int snaplen, int *err);
375 wtap_dumper* wtap_dump_fdopen(int fd, int filetype, int encap, int snaplen,
376         int *err);
377 int wtap_dump(wtap_dumper *, const struct wtap_pkthdr *, const u_char *,
378         int *err);
379 FILE* wtap_dump_file(wtap_dumper *);
380 int wtap_dump_close(wtap_dumper *, int *);
381
382 /* XXX - needed until "wiretap" can do live packet captures */
383 int wtap_pcap_encap_to_wtap_encap(int encap);
384
385 /*
386  * Wiretap error codes.
387  */
388 #define WTAP_ERR_NOT_REGULAR_FILE               -1
389         /* The file being opened for reading isn't a plain file */
390 #define WTAP_ERR_FILE_UNKNOWN_FORMAT            -2
391         /* The file being opened is not a capture file in a known format */
392 #define WTAP_ERR_UNSUPPORTED                    -3
393         /* Supported file type, but there's something in the file we
394            can't support */
395 #define WTAP_ERR_CANT_OPEN                      -4
396         /* The file couldn't be opened, reason unknown */
397 #define WTAP_ERR_UNSUPPORTED_FILE_TYPE          -5
398         /* Wiretap can't save files in the specified format */
399 #define WTAP_ERR_UNSUPPORTED_ENCAP              -6
400         /* Wiretap can't save files in the specified format with the
401            specified encapsulation */
402 #define WTAP_ERR_ENCAP_PER_PACKET_UNSUPPORTED   -7
403         /* The specified format doesn't support per-packet encapsulations */
404 #define WTAP_ERR_CANT_CLOSE                     -8
405         /* The file couldn't be closed, reason unknown */
406 #define WTAP_ERR_CANT_READ                      -9
407         /* An attempt to read failed, reason unknown */
408 #define WTAP_ERR_SHORT_READ                     -10
409         /* An attempt to read read less data than it should have */
410 #define WTAP_ERR_BAD_RECORD                     -11
411         /* We read an invalid record */
412 #define WTAP_ERR_SHORT_WRITE                    -12
413         /* An attempt to write wrote less data than it should have */
414
415 /* Errors from zlib; zlib error Z_xxx turns into Wiretap error
416    WTAP_ERR_ZLIB + Z_xxx.
417
418    WTAP_ERR_ZLIB_MIN and WTAP_ERR_ZLIB_MAX bound the range of zlib
419    errors; we leave room for 100 positive and 100 negative error
420    codes. */
421
422 #define WTAP_ERR_ZLIB                           -200
423 #define WTAP_ERR_ZLIB_MAX                       -100
424 #define WTAP_ERR_ZLIB_MIN                       -300
425
426 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
427  * byte array, returns the value of the two or four bytes at the pointer.
428  * The pletoh[sl] versions return the little-endian representation.
429  */
430
431 #ifndef pntohs
432 #define pntohs(p)  ((guint16)                       \
433                     ((guint16)*((guint8 *)p+0)<<8|  \
434                      (guint16)*((guint8 *)p+1)<<0))
435 #endif
436
437 #ifndef pntohl
438 #define pntohl(p)  ((guint32)*((guint8 *)p+0)<<24|  \
439                     (guint32)*((guint8 *)p+1)<<16|  \
440                     (guint32)*((guint8 *)p+2)<<8|   \
441                     (guint32)*((guint8 *)p+3)<<0)
442 #endif
443
444 #ifndef phtons
445 #define phtons(p)  ((guint16)                       \
446                     ((guint16)*((guint8 *)p+0)<<8|  \
447                      (guint16)*((guint8 *)p+1)<<0))
448 #endif
449
450 #ifndef phtonl
451 #define phtonl(p)  ((guint32)*((guint8 *)p+0)<<24|  \
452                     (guint32)*((guint8 *)p+1)<<16|  \
453                     (guint32)*((guint8 *)p+2)<<8|   \
454                     (guint32)*((guint8 *)p+3)<<0)
455 #endif
456
457 #ifndef pletohs
458 #define pletohs(p) ((guint16)                       \
459                     ((guint16)*((guint8 *)p+1)<<8|  \
460                      (guint16)*((guint8 *)p+0)<<0))
461 #endif
462
463 #ifndef plethol
464 #define pletohl(p) ((guint32)*((guint8 *)p+3)<<24|  \
465                     (guint32)*((guint8 *)p+2)<<16|  \
466                     (guint32)*((guint8 *)p+1)<<8|   \
467                     (guint32)*((guint8 *)p+0)<<0)
468 #endif
469
470 #endif /* __WTAP_H__ */