From Jason House: include <winsock2.h> early in "wtap-int.h" on Win32
[obnox/wireshark/wip.git] / wiretap / wtap-int.h
1 /* wtap-int.h
2  *
3  * $Id: wtap-int.h,v 1.29 2002/10/15 05:38:01 guy Exp $
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         double  timeunit;
57         time_t  start;
58         int     is_atm;
59         ngsniffer_comp_stream_t seq;    /* sequential access */
60         ngsniffer_comp_stream_t rand;   /* random access */
61         GList   *first_blob;            /* list element for first blob */
62         GList   *last_blob;             /* list element for last blob */
63         GList   *current_blob;          /* list element for current blob */
64 } ngsniffer_t;
65
66 typedef struct {
67         gboolean byte_swapped;
68         int bchannel_prot[2];   /* For the V.120 heuristic */
69 } i4btrace_t;
70
71 typedef struct {
72         gboolean is_hpux_11;
73 } nettl_t;
74
75 typedef struct {
76         time_t  start;
77 } lanalyzer_t;
78
79 typedef struct {
80         gboolean byte_swapped;
81         guint16 version_major;
82         guint16 version_minor;
83 } libpcap_t;
84
85 typedef struct {
86         time_t  start_secs;
87         guint32 start_usecs;
88         guint8  version_major;
89         guint32 *frame_table;
90         guint32 frame_table_size;
91         guint   current_frame;
92 } netmon_t;
93
94 typedef struct {
95         time_t          start_time;
96         double          timeunit;
97         double          start_timestamp;
98         gboolean        wrapped;
99         int             end_offset;
100         int             version_major;
101         guint           padding;        /* end-of-packet padding */
102 } netxray_t;
103
104 typedef struct {
105         time_t inittime;
106         int adjusted;
107         long next_packet_seek_start;
108 } ascend_t;
109
110 typedef struct {
111         gboolean byteswapped;
112 } csids_t;
113
114 typedef struct {
115         struct timeval reference_time;
116 } etherpeek_t;
117
118 typedef gboolean (*subtype_read_func)(struct wtap*, int*, long*);
119 typedef gboolean (*subtype_seek_read_func)(struct wtap*, long, union wtap_pseudo_header*,
120                                         guint8*, int, int *);
121 struct wtap {
122         FILE_T                  fh;
123         int                     fd;           /* File descriptor for cap file */
124         FILE_T                  random_fh;    /* Secondary FILE_T for random access */
125         int                     file_type;
126         int                     snapshot_length;
127         struct Buffer           *frame_buffer;
128         struct wtap_pkthdr      phdr;
129         union wtap_pseudo_header pseudo_header;
130
131         long                    data_offset;
132
133         union {
134                 libpcap_t               *pcap;
135                 lanalyzer_t             *lanalyzer;
136                 ngsniffer_t             *ngsniffer;
137                 i4btrace_t              *i4btrace;
138                 nettl_t                 *nettl;
139                 netmon_t                *netmon;
140                 netxray_t               *netxray;
141                 ascend_t                *ascend;
142                 csids_t                 *csids;
143                 etherpeek_t             *etherpeek;
144                 void                    *generic;
145         } capture;
146
147         subtype_read_func       subtype_read;
148         subtype_seek_read_func  subtype_seek_read;
149         void                    (*subtype_sequential_close)(struct wtap*);
150         void                    (*subtype_close)(struct wtap*);
151         int                     file_encap;     /* per-file, for those
152                                                    file formats that have
153                                                    per-file encapsulation
154                                                    types */
155 };
156
157 struct wtap_dumper;
158
159 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
160                 const struct wtap_pkthdr*, const union wtap_pseudo_header*,
161                 const guchar*, int*);
162 typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
163
164 typedef struct {
165         gboolean first_frame;
166         time_t start;
167 } ngsniffer_dump_t;
168
169 typedef struct {
170         gboolean first_frame;
171         struct timeval start;
172         guint32 nframes;
173 } netxray_dump_t;
174
175 typedef struct {
176         gboolean got_first_record_time;
177         struct timeval first_record_time;
178         guint32 frame_table_offset;
179         guint32 *frame_table;
180         guint   frame_table_index;
181         guint   frame_table_size;
182 } netmon_dump_t;
183
184 struct wtap_dumper {
185         FILE*                   fh;
186         int                     file_type;
187         int                     snaplen;
188         int                     encap;
189         long                    bytes_dumped;
190
191         union {
192                 void                    *opaque;
193                 ngsniffer_dump_t        *ngsniffer;
194                 netmon_dump_t           *netmon;
195                 netxray_dump_t          *netxray;
196         } dump;
197
198         subtype_write_func      subtype_write;
199         subtype_close_func      subtype_close;
200 };
201
202
203 /* Macros to byte-swap 32-bit and 16-bit quantities. */
204 #define BSWAP32(x) \
205         ((((x)&0xFF000000)>>24) | \
206          (((x)&0x00FF0000)>>8) | \
207          (((x)&0x0000FF00)<<8) | \
208          (((x)&0x000000FF)<<24))
209 #define BSWAP16(x) \
210          ((((x)&0xFF00)>>8) | \
211           (((x)&0x00FF)<<8))
212
213 /* Turn host-byte-order values into little-endian values. */
214 #ifdef WORDS_BIGENDIAN
215 #define htoles(s) ((guint16)                       \
216                     ((guint16)((s) & 0x00FF)<<8|  \
217                      (guint16)((s) & 0xFF00)>>8))
218
219 #define htolel(l) ((guint32)((l) & 0x000000FF)<<24|  \
220                    (guint32)((l) & 0x0000FF00)<<8|  \
221                    (guint32)((l) & 0x00FF0000)>>8|   \
222                    (guint32)((l) & 0xFF000000)>>24)
223
224 #ifdef G_HAVE_GINT64
225 #define htolell(ll) GUINT64_TO_LE(ll)
226 #endif /* G_HAVE_GINT64 */
227
228 #else /* WORDS_BIGENDIAN */
229 #define htoles(s)       (s)
230 #define htolel(l)       (l)
231 #define htolell(ll)     (ll)
232 #endif /* WORDS_BIGENDIAN */
233
234 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
235  * byte array, returns the value of the two or four bytes at the pointer.
236  * The pletoh[sl] versions return the little-endian representation.
237  *
238  * If G_HAVE_GINT64 is defined, so we can use "gint64" and "guint64" to
239  * refer to 64-bit integral quantities, we also provide pntohll and
240  * phtolell, which extract 64-bit integral quantities.
241  */
242
243 #ifndef pntohs
244 #define pntohs(p)  ((guint16)                       \
245                     ((guint16)*((guint8 *)(p)+0)<<8|  \
246                      (guint16)*((guint8 *)(p)+1)<<0))
247 #endif
248
249 #ifndef pntoh24
250 #define pntoh24(p)  ((guint32)*((guint8 *)(p)+0)<<16| \
251                      (guint32)*((guint8 *)(p)+1)<<8|  \
252                      (guint32)*((guint8 *)(p)+2)<<0)
253 #endif
254
255 #ifndef pntohl
256 #define pntohl(p)  ((guint32)*((guint8 *)(p)+0)<<24|  \
257                     (guint32)*((guint8 *)(p)+1)<<16|  \
258                     (guint32)*((guint8 *)(p)+2)<<8|   \
259                     (guint32)*((guint8 *)(p)+3)<<0)
260 #endif
261
262 #ifdef G_HAVE_GINT64
263 #ifndef pntohll
264 #define pntohll(p)  ((guint64)*((guint8 *)(p)+0)<<56|  \
265                      (guint64)*((guint8 *)(p)+1)<<48|  \
266                      (guint64)*((guint8 *)(p)+2)<<40|  \
267                      (guint64)*((guint8 *)(p)+3)<<32|  \
268                      (guint64)*((guint8 *)(p)+4)<<24|  \
269                      (guint64)*((guint8 *)(p)+5)<<16|  \
270                      (guint64)*((guint8 *)(p)+6)<<8|   \
271                      (guint64)*((guint8 *)(p)+7)<<0)
272 #endif
273 #endif
274
275
276 #ifndef phtons
277 #define phtons(p)  ((guint16)                       \
278                     ((guint16)*((guint8 *)(p)+0)<<8|  \
279                      (guint16)*((guint8 *)(p)+1)<<0))
280 #endif
281
282 #ifndef phtonl
283 #define phtonl(p)  ((guint32)*((guint8 *)(p)+0)<<24|  \
284                     (guint32)*((guint8 *)(p)+1)<<16|  \
285                     (guint32)*((guint8 *)(p)+2)<<8|   \
286                     (guint32)*((guint8 *)(p)+3)<<0)
287 #endif
288
289 #ifndef pletohs
290 #define pletohs(p) ((guint16)                       \
291                     ((guint16)*((guint8 *)(p)+1)<<8|  \
292                      (guint16)*((guint8 *)(p)+0)<<0))
293 #endif
294
295 #ifndef pletoh24
296 #define pletoh24(p) ((guint32)*((guint8 *)(p)+2)<<16|  \
297                      (guint32)*((guint8 *)(p)+1)<<8|  \
298                      (guint32)*((guint8 *)(p)+0)<<0)
299 #endif
300
301
302 #ifndef pletohl
303 #define pletohl(p) ((guint32)*((guint8 *)(p)+3)<<24|  \
304                     (guint32)*((guint8 *)(p)+2)<<16|  \
305                     (guint32)*((guint8 *)(p)+1)<<8|   \
306                     (guint32)*((guint8 *)(p)+0)<<0)
307 #endif
308
309
310 #ifdef G_HAVE_GINT64
311 #ifndef pletohll
312 #define pletohll(p) ((guint64)*((guint8 *)(p)+7)<<56|  \
313                      (guint64)*((guint8 *)(p)+6)<<48|  \
314                      (guint64)*((guint8 *)(p)+5)<<40|  \
315                      (guint64)*((guint8 *)(p)+4)<<32|  \
316                      (guint64)*((guint8 *)(p)+3)<<24|  \
317                      (guint64)*((guint8 *)(p)+2)<<16|  \
318                      (guint64)*((guint8 *)(p)+1)<<8|   \
319                      (guint64)*((guint8 *)(p)+0)<<0)
320 #endif
321 #endif
322
323 #define wtap_file_read_unknown_bytes(target, num_bytes, fh, err) \
324         G_STMT_START \
325         { \
326                 int _bytes_read; \
327                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
328                 if (_bytes_read != (num_bytes)) { \
329                         *(err) = file_error((fh)); \
330                         return FALSE; \
331                 } \
332         } \
333         G_STMT_END
334
335 #define wtap_file_read_expected_bytes(target, num_bytes, fh, err) \
336         G_STMT_START \
337         { \
338                 int _bytes_read; \
339                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
340                 if (_bytes_read != (num_bytes)) { \
341                         *(err) = file_error((fh)); \
342                         if (*(err) == 0 && _bytes_read > 0) { \
343                                 *(err) = WTAP_ERR_SHORT_READ; \
344                         } \
345                         return FALSE; \
346                 } \
347         } \
348         G_STMT_END
349
350 /* glib doesn't have g_ptr_array_len of all things!*/
351 #ifndef g_ptr_array_len
352 #define g_ptr_array_len(a)      ((a)->len)
353 #endif
354
355 #endif /* __WTAP_INT_H__ */