Remove some unneeded includes of <sys/time.h>.
[metze/wireshark/wip.git] / wiretap / wtap-int.h
1 /* wtap-int.h
2  *
3  * Wiretap Library
4  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * as published by the Free Software Foundation; either version 2
9  * of the License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #ifndef __WTAP_INT_H__
22 #define __WTAP_INT_H__
23
24 #include <glib.h>
25 #include <stdio.h>
26 #include <time.h>
27
28 #ifdef HAVE_WINSOCK2_H
29 #include <winsock2.h>
30 #endif
31
32 #include <wsutil/file_util.h>
33
34 #include "wtap.h"
35
36 WS_DLL_PUBLIC
37 int wtap_fstat(wtap *wth, ws_statb64 *statb, int *err);
38
39 typedef gboolean (*subtype_read_func)(struct wtap*, int*, char**, gint64*);
40 typedef gboolean (*subtype_seek_read_func)(struct wtap*, gint64,
41                                            struct wtap_pkthdr *, Buffer *buf,
42                                            int *, char **);
43 /**
44  * Struct holding data of the currently read file.
45  */
46 struct wtap {
47     FILE_T                      fh;
48     FILE_T                      random_fh;              /**< Secondary FILE_T for random access */
49     int                         file_type_subtype;
50     guint                       snapshot_length;
51     struct Buffer               *frame_buffer;
52     struct wtap_pkthdr          phdr;
53     struct wtapng_section_s     shb_hdr;
54     guint                       number_of_interfaces;   /**< The number of interfaces a capture was made on, number of IDB:s in a pcapng file or equivalent(?)*/
55     GArray                      *interface_data;        /**< An array holding the interface data from pcapng IDB:s or equivalent(?)*/
56
57     void                        *priv;          /* this one holds per-file state and is free'd automatically by wtap_close() */
58     void                        *wslua_data;    /* this one holds wslua state info and is not free'd */
59
60     subtype_read_func           subtype_read;
61     subtype_seek_read_func      subtype_seek_read;
62     void                        (*subtype_sequential_close)(struct wtap*);
63     void                        (*subtype_close)(struct wtap*);
64     int                         file_encap;    /* per-file, for those
65                                                 * file formats that have
66                                                 * per-file encapsulation
67                                                 * types rather than per-packet
68                                                 * encapsulation types
69                                                 */
70     int                         file_tsprec;   /* per-file timestamp precision
71                                                 * of the fractional part of
72                                                 * the time stamp, for those
73                                                 * file formats that have
74                                                 * per-file timestamp
75                                                 * precision rather than
76                                                 * per-packet timestamp
77                                                 * precision
78                                                 * e.g. WTAP_TSPREC_USEC
79                                                 */
80     wtap_new_ipv4_callback_t    add_new_ipv4;
81     wtap_new_ipv6_callback_t    add_new_ipv6;
82     GPtrArray                   *fast_seek;
83 };
84
85 struct wtap_dumper;
86
87 /*
88  * This could either be a FILE * or a gzFile.
89  */
90 typedef void *WFILE_T;
91
92 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
93                                        const struct wtap_pkthdr*,
94                                        const guint8*, int*, gchar**);
95 typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
96
97 struct wtap_dumper {
98     WFILE_T                 fh;
99     int                     file_type_subtype;
100     int                     snaplen;
101     int                     encap;
102     gboolean                compressed;
103     gint64                  bytes_dumped;
104
105     void                    *priv;       /* this one holds per-file state and is free'd automatically by wtap_dump_close() */
106     void                    *wslua_data; /* this one holds wslua state info and is not free'd */
107
108     subtype_write_func      subtype_write;
109     subtype_close_func      subtype_close;
110
111     int                     tsprecision;    /**< timestamp precision of the lower 32bits
112                                              * e.g. WTAP_TSPREC_USEC
113                                              */
114     addrinfo_lists_t        *addrinfo_lists;        /**< Struct containing lists of resolved addresses */
115     struct wtapng_section_s *shb_hdr;
116     GArray                  *interface_data;        /**< An array holding the interface data from pcapng IDB:s or equivalent(?) NULL if not present.*/
117 };
118
119 WS_DLL_PUBLIC gboolean wtap_dump_file_write(wtap_dumper *wdh, const void *buf,
120     size_t bufsize, int *err);
121 WS_DLL_PUBLIC gint64 wtap_dump_file_seek(wtap_dumper *wdh, gint64 offset, int whence, int *err);
122 WS_DLL_PUBLIC gint64 wtap_dump_file_tell(wtap_dumper *wdh, int *err);
123
124
125 extern gint wtap_num_file_types;
126
127 #include <wsutil/pint.h>
128
129 /* Macros to byte-swap possibly-unaligned 64-bit, 32-bit and 16-bit quantities;
130  * they take a pointer to the quantity, and byte-swap it in place.
131  */
132 #define PBSWAP64(p) \
133     {            \
134         guint8 tmp;        \
135         tmp = (p)[7];      \
136         (p)[7] = (p)[0];   \
137         (p)[0] = tmp;      \
138         tmp = (p)[6];      \
139         (p)[6] = (p)[1];   \
140         (p)[1] = tmp;      \
141         tmp = (p)[5];      \
142         (p)[5] = (p)[2];   \
143         (p)[2] = tmp;      \
144         tmp = (p)[4];      \
145         (p)[4] = (p)[3];   \
146         (p)[3] = tmp;      \
147     }
148 #define PBSWAP32(p) \
149     {            \
150         guint8 tmp;         \
151         tmp = (p)[3];       \
152         (p)[3] = (p)[0];    \
153         (p)[0] = tmp;       \
154         tmp = (p)[2];       \
155         (p)[2] = (p)[1];    \
156         (p)[1] = tmp;       \
157     }
158 #define PBSWAP16(p) \
159     {            \
160         guint8 tmp;        \
161         tmp = (p)[1];      \
162         (p)[1] = (p)[0];   \
163         (p)[0] = tmp;      \
164     }
165
166
167 /* Pointer routines to put items out in a particular byte order.
168  * These will work regardless of the byte alignment of the pointer.
169  */
170
171 #ifndef phtons
172 #define phtons(p, v) \
173     {                 \
174         (p)[0] = (guint8)((v) >> 8);    \
175         (p)[1] = (guint8)((v) >> 0);    \
176     }
177 #endif
178
179 #ifndef phton24
180 #define phton24(p, v) \
181     {                 \
182         (p)[0] = (guint8)((v) >> 16);    \
183         (p)[1] = (guint8)((v) >> 8);     \
184         (p)[2] = (guint8)((v) >> 0);     \
185     }
186 #endif
187
188 #ifndef phtonl
189 #define phtonl(p, v) \
190     {                 \
191         (p)[0] = (guint8)((v) >> 24);    \
192         (p)[1] = (guint8)((v) >> 16);    \
193         (p)[2] = (guint8)((v) >> 8);     \
194         (p)[3] = (guint8)((v) >> 0);     \
195     }
196 #endif
197
198 #ifndef phtonll
199 #define phtonll(p, v) \
200     {                 \
201         (p)[0] = (guint8)((v) >> 56);    \
202         (p)[1] = (guint8)((v) >> 48);    \
203         (p)[2] = (guint8)((v) >> 40);    \
204         (p)[3] = (guint8)((v) >> 32);    \
205         (p)[4] = (guint8)((v) >> 24);    \
206         (p)[5] = (guint8)((v) >> 16);    \
207         (p)[6] = (guint8)((v) >> 8);     \
208         (p)[7] = (guint8)((v) >> 0);     \
209     }
210 #endif
211
212 #ifndef phtoles
213 #define phtoles(p, v) \
214     {                 \
215         (p)[0] = (guint8)((v) >> 0);    \
216         (p)[1] = (guint8)((v) >> 8);    \
217     }
218 #endif
219
220 #ifndef phtolel
221 #define phtolel(p, v) \
222     {                 \
223         (p)[0] = (guint8)((v) >> 0);     \
224         (p)[1] = (guint8)((v) >> 8);     \
225         (p)[2] = (guint8)((v) >> 16);    \
226         (p)[3] = (guint8)((v) >> 24);    \
227     }
228 #endif
229
230 #ifndef phtolell
231 #define phtolell(p, v) \
232     {                 \
233         (p)[0] = (guint8)((v) >> 0);     \
234         (p)[1] = (guint8)((v) >> 8);     \
235         (p)[2] = (guint8)((v) >> 16);    \
236         (p)[3] = (guint8)((v) >> 24);    \
237         (p)[4] = (guint8)((v) >> 32);    \
238         (p)[5] = (guint8)((v) >> 40);    \
239         (p)[6] = (guint8)((v) >> 48);    \
240         (p)[7] = (guint8)((v) >> 56);    \
241     }
242 #endif
243
244 /* glib doesn't have g_ptr_array_len of all things!*/
245 #ifndef g_ptr_array_len
246 #define g_ptr_array_len(a)      ((a)->len)
247 #endif
248
249 /*** get GSList of all compressed file extensions ***/
250 GSList *wtap_get_compressed_file_extensions(void);
251
252 /*
253  * Read a given number of bytes from a file.
254  *
255  * If we succeed, return TRUE.
256  *
257  * If we get an EOF, return FALSE with *err set to 0, reporting this
258  * as an EOF.
259  *
260  * If we get fewer bytes than the specified number, return FALSE with
261  * *err set to WTAP_ERR_SHORT_READ, reporting this as a short read
262  * error.
263  *
264  * If we get a read error, return FALSE with *err and *err_info set
265  * appropriately.
266  */
267 WS_DLL_PUBLIC
268 gboolean
269 wtap_read_bytes_or_eof(FILE_T fh, void *buf, unsigned int count, int *err,
270     gchar **err_info);
271
272 /*
273  * Read a given number of bytes from a file.
274  *
275  * If we succeed, return TRUE.
276  *
277  * If we get fewer bytes than the specified number, including getting
278  * an EOF, return FALSE with *err set to WTAP_ERR_SHORT_READ, reporting
279  * this as a short read error.
280  *
281  * If we get a read error, return FALSE with *err and *err_info set
282  * appropriately.
283  */
284 WS_DLL_PUBLIC
285 gboolean
286 wtap_read_bytes(FILE_T fh, void *buf, unsigned int count, int *err,
287     gchar **err_info);
288
289 /*
290  * Read packet data into a Buffer, growing the buffer as necessary.
291  *
292  * This returns an error on a short read, even if the short read hit
293  * the EOF immediately.  (The assumption is that each packet has a
294  * header followed by raw packet data, and that we've already read the
295  * header, so if we get an EOF trying to read the packet data, the file
296  * has been cut short, even if the read didn't read any data at all.)
297  */
298 WS_DLL_PUBLIC
299 gboolean
300 wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
301     gchar **err_info);
302
303 #endif /* __WTAP_INT_H__ */
304
305 /*
306  * Editor modelines
307  *
308  * Local Variables:
309  * c-basic-offset: 8
310  * tab-width: 8
311  * indent-tabs-mode: t
312  * End:
313  *
314  * ex: set shiftwidth=8 tabstop=8 noexpandtab:
315  * :indentSize=8:tabSize=8:noTabs=false:
316  */