fbbe89ef12fee08baec2d9d7269c875cec597b8a
[obnox/wireshark/wip.git] / wiretap / wtap-int.h
1 /* wtap-int.h
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.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 #ifndef __WTAP_INT_H__
24 #define __WTAP_INT_H__
25
26 #ifdef HAVE_SYS_TIME_H
27 #include <sys/time.h>
28 #endif
29
30 #include <glib.h>
31 #include <stdio.h>
32 #include <time.h>
33
34 #ifdef HAVE_LIBZ
35 #ifdef HAVE_WINSOCK2_H
36 #include <winsock2.h>
37 #endif
38 #include <zlib.h>
39 #define FILE_T  gzFile
40 #else /* No zLib */
41 #define FILE_T  FILE *
42 #endif /* HAVE_LIBZ */
43
44 #include "wtap.h"
45
46 /* Information for a compressed Sniffer data stream. */
47 typedef struct {
48         unsigned char *buf;     /* buffer into which we uncompress data */
49         size_t  nbytes;         /* number of bytes of data in that buffer */
50         int     nextout;        /* offset in that buffer of stream's current position */
51         long    comp_offset;    /* current offset in compressed data stream */
52         long    uncomp_offset;  /* current offset in uncompressed data stream */
53 } ngsniffer_comp_stream_t;
54
55 typedef struct {
56         guint   maj_vers;
57         guint   min_vers;
58         double  timeunit;
59         time_t  start;
60         gboolean is_atm;
61         ngsniffer_comp_stream_t seq;    /* sequential access */
62         ngsniffer_comp_stream_t rand;   /* random access */
63         GList   *first_blob;            /* list element for first blob */
64         GList   *last_blob;             /* list element for last blob */
65         GList   *current_blob;          /* list element for current blob */
66 } ngsniffer_t;
67
68 typedef struct {
69         gboolean byte_swapped;
70 } i4btrace_t;
71
72 typedef struct {
73         gboolean is_hpux_11;
74 } nettl_t;
75
76 typedef struct {
77         time_t  start;
78 } lanalyzer_t;
79
80 typedef enum {
81         NOT_SWAPPED,
82         SWAPPED,
83         MAYBE_SWAPPED
84 } swapped_type_t;
85
86 typedef struct {
87         gboolean byte_swapped;
88         swapped_type_t lengths_swapped;
89         guint16 version_major;
90         guint16 version_minor;
91 } libpcap_t;
92
93 typedef struct {
94         time_t  start_secs;
95         guint32 start_usecs;
96         guint8  version_major;
97         guint32 *frame_table;
98         guint32 frame_table_size;
99         guint   current_frame;
100 } netmon_t;
101
102 typedef struct {
103         time_t          start_time;
104         double          timeunit;
105         double          start_timestamp;
106         gboolean        wrapped;
107         int             end_offset;
108         int             version_major;
109         gboolean        fcs_valid;      /* if packets have valid FCS at the end */
110         guint           isdn_type;      /* 1 = E1 PRI, 2 = T1 PRI, 3 = BRI */
111 } netxray_t;
112
113 typedef struct {
114         time_t inittime;
115         int adjusted;
116         long next_packet_seek_start;
117 } ascend_t;
118
119 typedef struct {
120         gboolean byteswapped;
121 } csids_t;
122
123 typedef struct {
124         struct timeval reference_time;
125 } etherpeek_t;
126
127 typedef struct {
128         gboolean        has_fcs;
129 } airopeek9_t;
130
131 typedef struct {
132         guint32         atm_encap;
133         gboolean        is_rawatm;
134         gboolean        is_ppp;
135 } erf_t;
136
137 typedef struct _k12_t k12_t;
138
139 typedef gboolean (*subtype_read_func)(struct wtap*, int*, char**, long*);
140 typedef gboolean (*subtype_seek_read_func)(struct wtap*, long, union wtap_pseudo_header*,
141                                         guint8*, int, int *, char **);
142 struct wtap {
143         FILE_T                  fh;
144         int                     fd;           /* File descriptor for cap file */
145         FILE_T                  random_fh;    /* Secondary FILE_T for random access */
146         int                     file_type;
147         int                     snapshot_length;
148         struct Buffer           *frame_buffer;
149         struct wtap_pkthdr      phdr;
150         union wtap_pseudo_header pseudo_header;
151
152         long                    data_offset;
153
154         union {
155                 libpcap_t               *pcap;
156                 lanalyzer_t             *lanalyzer;
157                 ngsniffer_t             *ngsniffer;
158                 i4btrace_t              *i4btrace;
159                 nettl_t                 *nettl;
160                 netmon_t                *netmon;
161                 netxray_t               *netxray;
162                 ascend_t                *ascend;
163                 csids_t                 *csids;
164                 etherpeek_t             *etherpeek;
165                 airopeek9_t             *airopeek9;
166                 erf_t                   *erf;
167                 k12_t                   *k12;
168                 void                    *generic;
169         } capture;
170
171         subtype_read_func       subtype_read;
172         subtype_seek_read_func  subtype_seek_read;
173         void                    (*subtype_sequential_close)(struct wtap*);
174         void                    (*subtype_close)(struct wtap*);
175         int                     file_encap;     /* per-file, for those
176                                                    file formats that have
177                                                    per-file encapsulation
178                                                    types */
179 };
180
181 struct wtap_dumper;
182
183 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
184                 const struct wtap_pkthdr*, const union wtap_pseudo_header*,
185                 const guchar*, int*);
186 typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
187
188 typedef struct {
189         gboolean first_frame;
190         time_t start;
191 } ngsniffer_dump_t;
192
193 typedef struct {
194         gboolean first_frame;
195         struct timeval start;
196         guint32 nframes;
197 } netxray_dump_t;
198
199 typedef struct {
200         gboolean got_first_record_time;
201         struct timeval first_record_time;
202         guint32 frame_table_offset;
203         guint32 *frame_table;
204         guint   frame_table_index;
205         guint   frame_table_size;
206 } netmon_dump_t;
207
208 typedef struct {
209         guint32 nframes;
210 } _5views_dump_t;
211
212 typedef struct {
213         guint64 packet_count;
214         guint8  network_type;
215 } niobserver_dump_t;
216
217 typedef struct {
218         guint32 file_len;
219         guint32 num_of_records;
220         guint32 file_offset;
221 } k12_dump_t;
222
223
224 struct wtap_dumper {
225         FILE*                   fh;
226         int                     file_type;
227         int                     snaplen;
228         int                     encap;
229         long                    bytes_dumped;
230
231         union {
232                 void                    *opaque;
233                 ngsniffer_dump_t        *ngsniffer;
234                 netmon_dump_t           *netmon;
235                 netxray_dump_t          *netxray;
236                 _5views_dump_t          *_5views;
237                 niobserver_dump_t       *niobserver;
238                 k12_dump_t              *k12;
239         } dump;
240
241         subtype_write_func      subtype_write;
242         subtype_close_func      subtype_close;
243 };
244
245
246 /* Macros to byte-swap 32-bit and 16-bit quantities. */
247 #define BSWAP32(x) \
248         ((((x)&0xFF000000)>>24) | \
249          (((x)&0x00FF0000)>>8) | \
250          (((x)&0x0000FF00)<<8) | \
251          (((x)&0x000000FF)<<24))
252 #define BSWAP16(x) \
253          ((((x)&0xFF00)>>8) | \
254           (((x)&0x00FF)<<8))
255
256 /* Turn host-byte-order values into little-endian values. */
257 #define htoles(s) GUINT16_TO_LE(s)
258 #define htolel(l) GUINT32_TO_LE(l)
259 #ifdef G_HAVE_GINT64
260 #define htolell(ll) GUINT64_TO_LE(ll)
261 #endif /* G_HAVE_GINT64 */
262
263 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
264  * byte array, returns the value of the two or four bytes at the pointer.
265  * The pletoh[sl] versions return the little-endian representation.
266  *
267  * If G_HAVE_GINT64 is defined, so we can use "gint64" and "guint64" to
268  * refer to 64-bit integral quantities, we also provide pntohll and
269  * phtolell, which extract 64-bit integral quantities.
270  */
271
272 #ifndef pntohs
273 #define pntohs(p)  ((guint16)                       \
274                     ((guint16)*((const guint8 *)(p)+0)<<8|  \
275                      (guint16)*((const guint8 *)(p)+1)<<0))
276 #endif
277
278 #ifndef pntoh24
279 #define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16| \
280                      (guint32)*((const guint8 *)(p)+1)<<8|  \
281                      (guint32)*((const guint8 *)(p)+2)<<0)
282 #endif
283
284 #ifndef pntohl
285 #define pntohl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
286                     (guint32)*((const guint8 *)(p)+1)<<16|  \
287                     (guint32)*((const guint8 *)(p)+2)<<8|   \
288                     (guint32)*((const guint8 *)(p)+3)<<0)
289 #endif
290
291 #ifdef G_HAVE_GINT64
292 #ifndef pntohll
293 #define pntohll(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
294                      (guint64)*((const guint8 *)(p)+1)<<48|  \
295                      (guint64)*((const guint8 *)(p)+2)<<40|  \
296                      (guint64)*((const guint8 *)(p)+3)<<32|  \
297                      (guint64)*((const guint8 *)(p)+4)<<24|  \
298                      (guint64)*((const guint8 *)(p)+5)<<16|  \
299                      (guint64)*((const guint8 *)(p)+6)<<8|   \
300                      (guint64)*((const guint8 *)(p)+7)<<0)
301 #endif
302 #endif
303
304
305 #ifndef phtons
306 #define phtons(p)  ((guint16)                       \
307                     ((guint16)*((const guint8 *)(p)+0)<<8|  \
308                      (guint16)*((const guint8 *)(p)+1)<<0))
309 #endif
310
311 #ifndef phtonl
312 #define phtonl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
313                     (guint32)*((const guint8 *)(p)+1)<<16|  \
314                     (guint32)*((const guint8 *)(p)+2)<<8|   \
315                     (guint32)*((const guint8 *)(p)+3)<<0)
316 #endif
317
318 #ifndef pletohs
319 #define pletohs(p) ((guint16)                       \
320                     ((guint16)*((const guint8 *)(p)+1)<<8|  \
321                      (guint16)*((const guint8 *)(p)+0)<<0))
322 #endif
323
324 #ifndef pletoh24
325 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
326                      (guint32)*((const guint8 *)(p)+1)<<8|  \
327                      (guint32)*((const guint8 *)(p)+0)<<0)
328 #endif
329
330
331 #ifndef pletohl
332 #define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24|  \
333                     (guint32)*((const guint8 *)(p)+2)<<16|  \
334                     (guint32)*((const guint8 *)(p)+1)<<8|   \
335                     (guint32)*((const guint8 *)(p)+0)<<0)
336 #endif
337
338
339 #ifdef G_HAVE_GINT64
340 #ifndef pletohll
341 #define pletohll(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
342                      (guint64)*((const guint8 *)(p)+6)<<48|  \
343                      (guint64)*((const guint8 *)(p)+5)<<40|  \
344                      (guint64)*((const guint8 *)(p)+4)<<32|  \
345                      (guint64)*((const guint8 *)(p)+3)<<24|  \
346                      (guint64)*((const guint8 *)(p)+2)<<16|  \
347                      (guint64)*((const guint8 *)(p)+1)<<8|   \
348                      (guint64)*((const guint8 *)(p)+0)<<0)
349 #endif
350 #endif
351
352 #define wtap_file_read_unknown_bytes(target, num_bytes, fh, err) \
353         G_STMT_START \
354         { \
355                 int _bytes_read; \
356                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
357                 if (_bytes_read != (int) (num_bytes)) { \
358                         *(err) = file_error((fh)); \
359                         return FALSE; \
360                 } \
361         } \
362         G_STMT_END
363
364 #define wtap_file_read_expected_bytes(target, num_bytes, fh, err) \
365         G_STMT_START \
366         { \
367                 int _bytes_read; \
368                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
369                 if (_bytes_read != (int) (num_bytes)) { \
370                         *(err) = file_error((fh)); \
371                         if (*(err) == 0 && _bytes_read > 0) { \
372                                 *(err) = WTAP_ERR_SHORT_READ; \
373                         } \
374                         return FALSE; \
375                 } \
376         } \
377         G_STMT_END
378
379 /* glib doesn't have g_ptr_array_len of all things!*/
380 #ifndef g_ptr_array_len
381 #define g_ptr_array_len(a)      ((a)->len)
382 #endif
383
384 #endif /* __WTAP_INT_H__ */