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