53fe4e59fe570039d79ecc3064ced15076aa6525
[obnox/wireshark/wip.git] / wiretap / wtap-int.h
1 /* wtap-int.h
2  *
3  * $Id: wtap-int.h,v 1.43 2004/01/05 17:33:28 ulfl 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         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         guint32         atm_encap;
129         gboolean        is_rawatm;
130         gboolean        is_ppp;
131 } erf_t;
132
133 typedef gboolean (*subtype_read_func)(struct wtap*, int*, long*);
134 typedef gboolean (*subtype_seek_read_func)(struct wtap*, long, union wtap_pseudo_header*,
135                                         guint8*, int, int *);
136 struct wtap {
137         FILE_T                  fh;
138         int                     fd;           /* File descriptor for cap file */
139         FILE_T                  random_fh;    /* Secondary FILE_T for random access */
140         int                     file_type;
141         int                     snapshot_length;
142         struct Buffer           *frame_buffer;
143         struct wtap_pkthdr      phdr;
144         union wtap_pseudo_header pseudo_header;
145
146         long                    data_offset;
147
148         union {
149                 libpcap_t               *pcap;
150                 lanalyzer_t             *lanalyzer;
151                 ngsniffer_t             *ngsniffer;
152                 i4btrace_t              *i4btrace;
153                 nettl_t                 *nettl;
154                 netmon_t                *netmon;
155                 netxray_t               *netxray;
156                 ascend_t                *ascend;
157                 csids_t                 *csids;
158                 etherpeek_t             *etherpeek;
159                 erf_t                   *erf;
160                 void                    *generic;
161         } capture;
162
163         subtype_read_func       subtype_read;
164         subtype_seek_read_func  subtype_seek_read;
165         void                    (*subtype_sequential_close)(struct wtap*);
166         void                    (*subtype_close)(struct wtap*);
167         int                     file_encap;     /* per-file, for those
168                                                    file formats that have
169                                                    per-file encapsulation
170                                                    types */
171 };
172
173 struct wtap_dumper;
174
175 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
176                 const struct wtap_pkthdr*, const union wtap_pseudo_header*,
177                 const guchar*, int*);
178 typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
179
180 typedef struct {
181         gboolean first_frame;
182         time_t start;
183 } ngsniffer_dump_t;
184
185 typedef struct {
186         gboolean first_frame;
187         struct timeval start;
188         guint32 nframes;
189 } netxray_dump_t;
190
191 typedef struct {
192         gboolean got_first_record_time;
193         struct timeval first_record_time;
194         guint32 frame_table_offset;
195         guint32 *frame_table;
196         guint   frame_table_index;
197         guint   frame_table_size;
198 } netmon_dump_t;
199
200 typedef struct {
201         guint32 nframes;
202 } _5views_dump_t;
203
204 typedef struct {
205         guint64 packet_count;
206         guint8  network_type;
207 } niobserver_dump_t;
208
209 struct wtap_dumper {
210         FILE*                   fh;
211         int                     file_type;
212         int                     snaplen;
213         int                     encap;
214         long                    bytes_dumped;
215
216         union {
217                 void                    *opaque;
218                 ngsniffer_dump_t        *ngsniffer;
219                 netmon_dump_t           *netmon;
220                 netxray_dump_t          *netxray;
221                 _5views_dump_t          *_5views;
222                 niobserver_dump_t       *niobserver;
223         } dump;
224
225         subtype_write_func      subtype_write;
226         subtype_close_func      subtype_close;
227 };
228
229
230 /* Macros to byte-swap 32-bit and 16-bit quantities. */
231 #define BSWAP32(x) \
232         ((((x)&0xFF000000)>>24) | \
233          (((x)&0x00FF0000)>>8) | \
234          (((x)&0x0000FF00)<<8) | \
235          (((x)&0x000000FF)<<24))
236 #define BSWAP16(x) \
237          ((((x)&0xFF00)>>8) | \
238           (((x)&0x00FF)<<8))
239
240 /* Turn host-byte-order values into little-endian values. */
241 #define htoles(s) GUINT16_TO_LE(s)
242 #define htolel(l) GUINT32_TO_LE(l)
243 #ifdef G_HAVE_GINT64
244 #define htolell(ll) GUINT64_TO_LE(ll)
245 #endif /* G_HAVE_GINT64 */
246
247 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
248  * byte array, returns the value of the two or four bytes at the pointer.
249  * The pletoh[sl] versions return the little-endian representation.
250  *
251  * If G_HAVE_GINT64 is defined, so we can use "gint64" and "guint64" to
252  * refer to 64-bit integral quantities, we also provide pntohll and
253  * phtolell, which extract 64-bit integral quantities.
254  */
255
256 #ifndef pntohs
257 #define pntohs(p)  ((guint16)                       \
258                     ((guint16)*((const guint8 *)(p)+0)<<8|  \
259                      (guint16)*((const guint8 *)(p)+1)<<0))
260 #endif
261
262 #ifndef pntoh24
263 #define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16| \
264                      (guint32)*((const guint8 *)(p)+1)<<8|  \
265                      (guint32)*((const guint8 *)(p)+2)<<0)
266 #endif
267
268 #ifndef pntohl
269 #define pntohl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
270                     (guint32)*((const guint8 *)(p)+1)<<16|  \
271                     (guint32)*((const guint8 *)(p)+2)<<8|   \
272                     (guint32)*((const guint8 *)(p)+3)<<0)
273 #endif
274
275 #ifdef G_HAVE_GINT64
276 #ifndef pntohll
277 #define pntohll(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
278                      (guint64)*((const guint8 *)(p)+1)<<48|  \
279                      (guint64)*((const guint8 *)(p)+2)<<40|  \
280                      (guint64)*((const guint8 *)(p)+3)<<32|  \
281                      (guint64)*((const guint8 *)(p)+4)<<24|  \
282                      (guint64)*((const guint8 *)(p)+5)<<16|  \
283                      (guint64)*((const guint8 *)(p)+6)<<8|   \
284                      (guint64)*((const guint8 *)(p)+7)<<0)
285 #endif
286 #endif
287
288
289 #ifndef phtons
290 #define phtons(p)  ((guint16)                       \
291                     ((guint16)*((const guint8 *)(p)+0)<<8|  \
292                      (guint16)*((const guint8 *)(p)+1)<<0))
293 #endif
294
295 #ifndef phtonl
296 #define phtonl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
297                     (guint32)*((const guint8 *)(p)+1)<<16|  \
298                     (guint32)*((const guint8 *)(p)+2)<<8|   \
299                     (guint32)*((const guint8 *)(p)+3)<<0)
300 #endif
301
302 #ifndef pletohs
303 #define pletohs(p) ((guint16)                       \
304                     ((guint16)*((const guint8 *)(p)+1)<<8|  \
305                      (guint16)*((const guint8 *)(p)+0)<<0))
306 #endif
307
308 #ifndef pletoh24
309 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
310                      (guint32)*((const guint8 *)(p)+1)<<8|  \
311                      (guint32)*((const guint8 *)(p)+0)<<0)
312 #endif
313
314
315 #ifndef pletohl
316 #define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24|  \
317                     (guint32)*((const guint8 *)(p)+2)<<16|  \
318                     (guint32)*((const guint8 *)(p)+1)<<8|   \
319                     (guint32)*((const guint8 *)(p)+0)<<0)
320 #endif
321
322
323 #ifdef G_HAVE_GINT64
324 #ifndef pletohll
325 #define pletohll(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
326                      (guint64)*((const guint8 *)(p)+6)<<48|  \
327                      (guint64)*((const guint8 *)(p)+5)<<40|  \
328                      (guint64)*((const guint8 *)(p)+4)<<32|  \
329                      (guint64)*((const guint8 *)(p)+3)<<24|  \
330                      (guint64)*((const guint8 *)(p)+2)<<16|  \
331                      (guint64)*((const guint8 *)(p)+1)<<8|   \
332                      (guint64)*((const guint8 *)(p)+0)<<0)
333 #endif
334 #endif
335
336 #define wtap_file_read_unknown_bytes(target, num_bytes, fh, err) \
337         G_STMT_START \
338         { \
339                 int _bytes_read; \
340                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
341                 if (_bytes_read != (int) (num_bytes)) { \
342                         *(err) = file_error((fh)); \
343                         return FALSE; \
344                 } \
345         } \
346         G_STMT_END
347
348 #define wtap_file_read_expected_bytes(target, num_bytes, fh, err) \
349         G_STMT_START \
350         { \
351                 int _bytes_read; \
352                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
353                 if (_bytes_read != (int) (num_bytes)) { \
354                         *(err) = file_error((fh)); \
355                         if (*(err) == 0 && _bytes_read > 0) { \
356                                 *(err) = WTAP_ERR_SHORT_READ; \
357                         } \
358                         return FALSE; \
359                 } \
360         } \
361         G_STMT_END
362
363 /* glib doesn't have g_ptr_array_len of all things!*/
364 #ifndef g_ptr_array_len
365 #define g_ptr_array_len(a)      ((a)->len)
366 #endif
367
368 #endif /* __WTAP_INT_H__ */