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