Declare opt_cont_buf_len
[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_WINSOCK2_H
35 #include <winsock2.h>
36 #endif
37
38 #include <wsutil/file_util.h>
39
40 #include "wtap.h"
41
42 int wtap_fstat(wtap *wth, ws_statb64 *statb, int *err);
43
44 typedef gboolean (*subtype_read_func)(struct wtap*, int*, char**, gint64*);
45 typedef gboolean (*subtype_seek_read_func)(struct wtap*, gint64, union wtap_pseudo_header*,
46                                         guint8*, int, int *, char **);
47 /**
48  * Struct holding data of the currently read file.
49  */
50 struct wtap {
51         FILE_T                                          fh;
52         FILE_T                                          random_fh;                              /**< Secondary FILE_T for random access */
53         int                                                     file_type;
54         guint                                           snapshot_length;
55         struct Buffer                           *frame_buffer;
56         struct wtap_pkthdr                      phdr;
57         struct wtapng_section_s         shb_hdr;
58         guint                                           number_of_interfaces;   /**< The number of interfaces a capture was made on, number of IDB:s in a pcapng file or equivalent(?)*/
59         GArray                                          *interface_data;                /**< An array holding the interface data from pcapng IDB:s or equivalent(?)*/
60         union wtap_pseudo_header        pseudo_header;
61
62         gint64                                          data_offset;
63
64         void                                            *priv;
65
66         subtype_read_func                       subtype_read;
67         subtype_seek_read_func          subtype_seek_read;
68         void                                            (*subtype_sequential_close)(struct wtap*);
69         void                                            (*subtype_close)(struct wtap*);
70         int                                                     file_encap;     /* per-file, for those
71                                                                                          * file formats that have
72                                                                                          * per-file encapsulation
73                                                                                          * types
74                                                                                          */
75         int                                                     tsprecision;    /* timestamp precision of the lower 32bits
76                                                                                                  * e.g. WTAP_FILE_TSPREC_USEC
77                                                                                                  */
78         wtap_new_ipv4_callback_t        add_new_ipv4;
79         wtap_new_ipv6_callback_t        add_new_ipv6;
80         GPtrArray                                       *fast_seek;
81 };
82
83 struct wtap_dumper;
84
85 /*
86  * This could either be a FILE * or a gzFile.
87  */
88 typedef void *WFILE_T;
89
90 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
91                 const struct wtap_pkthdr*, const union wtap_pseudo_header*,
92                 const guint8*, int*);
93 typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
94
95 struct wtap_dumper {
96         WFILE_T                 fh;
97         int                             file_type;
98         int                             snaplen;
99         int                             encap;
100         gboolean                compressed;
101         gint64                  bytes_dumped;
102
103         void                    *priv;
104
105         subtype_write_func      subtype_write;
106         subtype_close_func      subtype_close;
107
108         int                                                     tsprecision;    /**< timestamp precision of the lower 32bits
109                                                                                                  * e.g. WTAP_FILE_TSPREC_USEC
110                                                                                                  */
111         struct addrinfo                         *addrinfo_list;
112         struct wtapng_section_s         *shb_hdr;
113         guint                                           number_of_interfaces;   /**< The number of interfaces a capture was made on, number of IDB:s in a pcapng file or equivalent(?)*/
114         GArray                                          *interface_data;                /**< An array holding the interface data from pcapng IDB:s or equivalent(?) NULL if not present.*/
115 };
116
117 extern gboolean wtap_dump_file_write(wtap_dumper *wdh, const void *buf,
118     size_t bufsize, int *err);
119 extern gint64 wtap_dump_file_seek(wtap_dumper *wdh, gint64 offset, int whence, int *err);
120 extern gint64 wtap_dump_file_tell(wtap_dumper *wdh);
121
122
123 extern gint wtap_num_file_types;
124
125 /* Macros to byte-swap 64-bit, 32-bit and 16-bit quantities. */
126 #define BSWAP64(x) \
127         ((((x)&G_GINT64_CONSTANT(0xFF00000000000000U))>>56) |   \
128          (((x)&G_GINT64_CONSTANT(0x00FF000000000000U))>>40) |   \
129          (((x)&G_GINT64_CONSTANT(0x0000FF0000000000U))>>24) |   \
130          (((x)&G_GINT64_CONSTANT(0x000000FF00000000U))>>8) |    \
131          (((x)&G_GINT64_CONSTANT(0x00000000FF000000U))<<8) |    \
132          (((x)&G_GINT64_CONSTANT(0x0000000000FF0000U))<<24) |   \
133          (((x)&G_GINT64_CONSTANT(0x000000000000FF00U))<<40) |   \
134          (((x)&G_GINT64_CONSTANT(0x00000000000000FFU))<<56))
135 #define BSWAP32(x) \
136         ((((x)&0xFF000000)>>24) | \
137          (((x)&0x00FF0000)>>8) | \
138          (((x)&0x0000FF00)<<8) | \
139          (((x)&0x000000FF)<<24))
140 #define BSWAP16(x) \
141          ((((x)&0xFF00)>>8) | \
142           (((x)&0x00FF)<<8))
143
144 /* Macros to byte-swap possibly-unaligned 64-bit, 32-bit and 16-bit quantities;
145  * they take a pointer to the quantity, and byte-swap it in place.
146  */
147 #define PBSWAP64(p) \
148         {                       \
149         guint8 tmp;             \
150         tmp = (p)[7];           \
151         (p)[7] = (p)[0];        \
152         (p)[0] = tmp;           \
153         tmp = (p)[6];           \
154         (p)[6] = (p)[1];        \
155         (p)[1] = tmp;           \
156         tmp = (p)[5];           \
157         (p)[5] = (p)[2];        \
158         (p)[2] = tmp;           \
159         tmp = (p)[4];           \
160         (p)[4] = (p)[3];        \
161         (p)[3] = tmp;           \
162         }
163 #define PBSWAP32(p) \
164         {                       \
165         guint8 tmp;             \
166         tmp = (p)[3];           \
167         (p)[3] = (p)[0];        \
168         (p)[0] = tmp;           \
169         tmp = (p)[2];           \
170         (p)[2] = (p)[1];        \
171         (p)[1] = tmp;           \
172         }
173 #define PBSWAP16(p) \
174         {                       \
175         guint8 tmp;             \
176         tmp = (p)[1];           \
177         (p)[1] = (p)[0];        \
178         (p)[0] = tmp;           \
179         }
180
181 /* Turn host-byte-order values into little-endian values. */
182 #define htoles(s) GUINT16_TO_LE(s)
183 #define htolel(l) GUINT32_TO_LE(l)
184 #define htolell(ll) GUINT64_TO_LE(ll)
185
186 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
187  * byte array, returns the value of the two or four bytes at the pointer.
188  * The pletoh[sl] versions return the little-endian representation.
189  * We also provide pntohll and pletohll, which extract 64-bit integral
190  * quantities.
191  *
192  * These will work regardless of the byte alignment of the pointer.
193  */
194
195 #ifndef pntohs
196 #define pntohs(p)  ((guint16)                       \
197                     ((guint16)*((const guint8 *)(p)+0)<<8|  \
198                      (guint16)*((const guint8 *)(p)+1)<<0))
199 #endif
200
201 #ifndef pntoh24
202 #define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16| \
203                      (guint32)*((const guint8 *)(p)+1)<<8|  \
204                      (guint32)*((const guint8 *)(p)+2)<<0)
205 #endif
206
207 #ifndef pntohl
208 #define pntohl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
209                     (guint32)*((const guint8 *)(p)+1)<<16|  \
210                     (guint32)*((const guint8 *)(p)+2)<<8|   \
211                     (guint32)*((const guint8 *)(p)+3)<<0)
212 #endif
213
214 #ifndef pntohll
215 #define pntohll(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
216                      (guint64)*((const guint8 *)(p)+1)<<48|  \
217                      (guint64)*((const guint8 *)(p)+2)<<40|  \
218                      (guint64)*((const guint8 *)(p)+3)<<32|  \
219                      (guint64)*((const guint8 *)(p)+4)<<24|  \
220                      (guint64)*((const guint8 *)(p)+5)<<16|  \
221                      (guint64)*((const guint8 *)(p)+6)<<8|   \
222                      (guint64)*((const guint8 *)(p)+7)<<0)
223 #endif
224
225
226 #ifndef pletohs
227 #define pletohs(p) ((guint16)                       \
228                     ((guint16)*((const guint8 *)(p)+1)<<8|  \
229                      (guint16)*((const guint8 *)(p)+0)<<0))
230 #endif
231
232 #ifndef pletoh24
233 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
234                      (guint32)*((const guint8 *)(p)+1)<<8|  \
235                      (guint32)*((const guint8 *)(p)+0)<<0)
236 #endif
237
238
239 #ifndef pletohl
240 #define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24|  \
241                     (guint32)*((const guint8 *)(p)+2)<<16|  \
242                     (guint32)*((const guint8 *)(p)+1)<<8|   \
243                     (guint32)*((const guint8 *)(p)+0)<<0)
244 #endif
245
246
247 #ifndef pletohll
248 #define pletohll(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
249                      (guint64)*((const guint8 *)(p)+6)<<48|  \
250                      (guint64)*((const guint8 *)(p)+5)<<40|  \
251                      (guint64)*((const guint8 *)(p)+4)<<32|  \
252                      (guint64)*((const guint8 *)(p)+3)<<24|  \
253                      (guint64)*((const guint8 *)(p)+2)<<16|  \
254                      (guint64)*((const guint8 *)(p)+1)<<8|   \
255                      (guint64)*((const guint8 *)(p)+0)<<0)
256 #endif
257
258 /* Pointer routines to put items out in a particular byte order.
259  * These will work regardless of the byte alignment of the pointer.
260  */
261
262 #ifndef phtons
263 #define phtons(p, v) \
264         {                               \
265         (p)[0] = (guint8)((v) >> 8);    \
266         (p)[1] = (guint8)((v) >> 0);    \
267         }
268 #endif
269
270 #ifndef phton24
271 #define phton24(p, v) \
272         {                               \
273         (p)[0] = (guint8)((v) >> 16);   \
274         (p)[1] = (guint8)((v) >> 8);    \
275         (p)[2] = (guint8)((v) >> 0);    \
276         }
277 #endif
278
279 #ifndef phtonl
280 #define phtonl(p, v) \
281         {                               \
282         (p)[0] = (guint8)((v) >> 24);   \
283         (p)[1] = (guint8)((v) >> 16);   \
284         (p)[2] = (guint8)((v) >> 8);    \
285         (p)[3] = (guint8)((v) >> 0);    \
286         }
287 #endif
288
289 #ifndef phtonll
290 #define phtonll(p, v) \
291         {                               \
292         (p)[0] = (guint8)((v) >> 56);   \
293         (p)[1] = (guint8)((v) >> 48);   \
294         (p)[2] = (guint8)((v) >> 40);   \
295         (p)[3] = (guint8)((v) >> 32);   \
296         (p)[4] = (guint8)((v) >> 24);   \
297         (p)[5] = (guint8)((v) >> 16);   \
298         (p)[6] = (guint8)((v) >> 8);    \
299         (p)[7] = (guint8)((v) >> 0);    \
300         }
301 #endif
302
303 #ifndef phtoles
304 #define phtoles(p, v) \
305         {                               \
306         (p)[0] = (guint8)((v) >> 0);    \
307         (p)[1] = (guint8)((v) >> 8);    \
308         }
309 #endif
310
311 #ifndef phtolell
312 #define phtolell(p, v) \
313         {                               \
314         (p)[0] = (guint8)((v) >> 0);    \
315         (p)[1] = (guint8)((v) >> 8);    \
316         (p)[2] = (guint8)((v) >> 16);   \
317         (p)[3] = (guint8)((v) >> 24);   \
318         (p)[4] = (guint8)((v) >> 32);   \
319         (p)[5] = (guint8)((v) >> 40);   \
320         (p)[6] = (guint8)((v) >> 48);   \
321         (p)[7] = (guint8)((v) >> 56);   \
322         }
323 #endif
324
325 #define wtap_file_read_unknown_bytes(target, num_bytes, fh, err, err_info) \
326         G_STMT_START \
327         { \
328                 int _bytes_read; \
329                 _bytes_read = file_read((target), (num_bytes), (fh)); \
330                 if (_bytes_read != (int) (num_bytes)) { \
331                         *(err) = file_error((fh), (err_info)); \
332                         return FALSE; \
333                 } \
334         } \
335         G_STMT_END
336
337 #define wtap_file_read_expected_bytes(target, num_bytes, fh, err, err_info) \
338         G_STMT_START \
339         { \
340                 int _bytes_read; \
341                 _bytes_read = file_read((target), (num_bytes), (fh)); \
342                 if (_bytes_read != (int) (num_bytes)) { \
343                         *(err) = file_error((fh), (err_info)); \
344                         if (*(err) == 0 && _bytes_read > 0) { \
345                                 *(err) = WTAP_ERR_SHORT_READ; \
346                         } \
347                         return FALSE; \
348                 } \
349         } \
350         G_STMT_END
351
352 /* glib doesn't have g_ptr_array_len of all things!*/
353 #ifndef g_ptr_array_len
354 #define g_ptr_array_len(a)      ((a)->len)
355 #endif
356
357 /*** get GSList of all compressed file extensions ***/
358 GSList *wtap_get_compressed_file_extensions(void);
359
360 #endif /* __WTAP_INT_H__ */
361
362 /*
363  * Editor modelines
364  *
365  * Local Variables:
366  * c-basic-offset: 8
367  * tab-width: 8
368  * indent-tabs-mode: t
369  * End:
370  *
371  * ex: set shiftwidth=8 tabstop=8 noexpandtab:
372  * :indentSize=8:tabSize=8:noTabs=false:
373  */