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