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