Don't do pcap heuristics on a pipe.
[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 <time.h>
26
27 #ifdef _WIN32
28 #include <winsock2.h>
29 #endif
30
31 #include <wsutil/file_util.h>
32
33 #include "wtap.h"
34 #include "wtap_opttypes.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 /**
45  * Struct holding data of the currently read file.
46  */
47 struct wtap {
48     FILE_T                      fh;
49     FILE_T                      random_fh;              /**< Secondary FILE_T for random access */
50     gboolean                    ispipe;                 /**< TRUE if the file is a pipe */
51     int                         file_type_subtype;
52     guint                       snapshot_length;
53     struct Buffer               *frame_buffer;
54     struct wtap_pkthdr          phdr;
55     GArray                      *shb_hdrs;
56     GArray                      *interface_data;        /**< An array holding the interface data from pcapng IDB:s or equivalent(?)*/
57     GArray                      *nrb_hdrs;              /**< holds the Name Res Block's comment/custom_opts, or NULL */
58
59     void                        *priv;          /* this one holds per-file state and is free'd automatically by wtap_close() */
60     void                        *wslua_data;    /* this one holds wslua state info and is not free'd */
61
62     subtype_read_func           subtype_read;
63     subtype_seek_read_func      subtype_seek_read;
64     void                        (*subtype_sequential_close)(struct wtap*);
65     void                        (*subtype_close)(struct wtap*);
66     int                         file_encap;    /* per-file, for those
67                                                 * file formats that have
68                                                 * per-file encapsulation
69                                                 * types rather than per-packet
70                                                 * encapsulation types
71                                                 */
72     int                         file_tsprec;   /* per-file timestamp precision
73                                                 * of the fractional part of
74                                                 * the time stamp, for those
75                                                 * file formats that have
76                                                 * per-file timestamp
77                                                 * precision rather than
78                                                 * per-packet timestamp
79                                                 * precision
80                                                 * e.g. WTAP_TSPREC_USEC
81                                                 */
82     wtap_new_ipv4_callback_t    add_new_ipv4;
83     wtap_new_ipv6_callback_t    add_new_ipv6;
84     GPtrArray                   *fast_seek;
85 };
86
87 struct wtap_dumper;
88
89 /*
90  * This could either be a FILE * or a gzFile.
91  */
92 typedef void *WFILE_T;
93
94 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
95                                        const struct wtap_pkthdr*,
96                                        const guint8*, int*, gchar**);
97 typedef gboolean (*subtype_finish_func)(struct wtap_dumper*, int*);
98
99 struct wtap_dumper {
100     WFILE_T                 fh;
101     int                     file_type_subtype;
102     int                     snaplen;
103     int                     encap;
104     gboolean                compressed;
105     gboolean                needs_reload;   /* TRUE if the file requires re-loading after saving with wtap */
106     gint64                  bytes_dumped;
107
108     void                    *priv;          /* this one holds per-file state and is free'd automatically by wtap_dump_close() */
109     void                    *wslua_data;    /* this one holds wslua state info and is not free'd */
110
111     subtype_write_func      subtype_write;  /* write out a record */
112     subtype_finish_func     subtype_finish; /* write out information to finish writing file */
113
114     addrinfo_lists_t        *addrinfo_lists; /**< Struct containing lists of resolved addresses */
115     GArray                  *shb_hdrs;
116     GArray                  *nrb_hdrs;        /**< name resolution comment/custom_opt, or NULL */
117     GArray                  *interface_data; /**< An array holding the interface data from pcapng IDB:s or equivalent(?) NULL if not present.*/
118 };
119
120 WS_DLL_PUBLIC gboolean wtap_dump_file_write(wtap_dumper *wdh, const void *buf,
121     size_t bufsize, int *err);
122 WS_DLL_PUBLIC gint64 wtap_dump_file_seek(wtap_dumper *wdh, gint64 offset, int whence, int *err);
123 WS_DLL_PUBLIC gint64 wtap_dump_file_tell(wtap_dumper *wdh, int *err);
124
125
126 extern gint wtap_num_file_types;
127
128 #include <wsutil/pint.h>
129
130 /* Macros to byte-swap possibly-unaligned 64-bit, 32-bit and 16-bit quantities;
131  * they take a pointer to the quantity, and byte-swap it in place.
132  */
133 #define PBSWAP64(p) \
134     {            \
135         guint8 tmp;        \
136         tmp = (p)[7];      \
137         (p)[7] = (p)[0];   \
138         (p)[0] = tmp;      \
139         tmp = (p)[6];      \
140         (p)[6] = (p)[1];   \
141         (p)[1] = tmp;      \
142         tmp = (p)[5];      \
143         (p)[5] = (p)[2];   \
144         (p)[2] = tmp;      \
145         tmp = (p)[4];      \
146         (p)[4] = (p)[3];   \
147         (p)[3] = tmp;      \
148     }
149 #define PBSWAP32(p) \
150     {            \
151         guint8 tmp;         \
152         tmp = (p)[3];       \
153         (p)[3] = (p)[0];    \
154         (p)[0] = tmp;       \
155         tmp = (p)[2];       \
156         (p)[2] = (p)[1];    \
157         (p)[1] = tmp;       \
158     }
159 #define PBSWAP16(p) \
160     {            \
161         guint8 tmp;        \
162         tmp = (p)[1];      \
163         (p)[1] = (p)[0];   \
164         (p)[0] = tmp;      \
165     }
166
167
168 /* Pointer routines to put items out in a particular byte order.
169  * These will work regardless of the byte alignment of the pointer.
170  */
171
172 #ifndef phtons
173 #define phtons(p, v) \
174     {                 \
175         (p)[0] = (guint8)((v) >> 8);    \
176         (p)[1] = (guint8)((v) >> 0);    \
177     }
178 #endif
179
180 #ifndef phton24
181 #define phton24(p, v) \
182     {                 \
183         (p)[0] = (guint8)((v) >> 16);    \
184         (p)[1] = (guint8)((v) >> 8);     \
185         (p)[2] = (guint8)((v) >> 0);     \
186     }
187 #endif
188
189 #ifndef phtonl
190 #define phtonl(p, v) \
191     {                 \
192         (p)[0] = (guint8)((v) >> 24);    \
193         (p)[1] = (guint8)((v) >> 16);    \
194         (p)[2] = (guint8)((v) >> 8);     \
195         (p)[3] = (guint8)((v) >> 0);     \
196     }
197 #endif
198
199 #ifndef phtonll
200 #define phtonll(p, v) \
201     {                 \
202         (p)[0] = (guint8)((v) >> 56);    \
203         (p)[1] = (guint8)((v) >> 48);    \
204         (p)[2] = (guint8)((v) >> 40);    \
205         (p)[3] = (guint8)((v) >> 32);    \
206         (p)[4] = (guint8)((v) >> 24);    \
207         (p)[5] = (guint8)((v) >> 16);    \
208         (p)[6] = (guint8)((v) >> 8);     \
209         (p)[7] = (guint8)((v) >> 0);     \
210     }
211 #endif
212
213 #ifndef phtole8
214 #define phtole8(p, v) \
215     {                 \
216         (p)[0] = (guint8)((v) >> 0);    \
217     }
218 #endif
219
220 #ifndef phtoles
221 #define phtoles(p, v) \
222     {                 \
223         (p)[0] = (guint8)((v) >> 0);    \
224         (p)[1] = (guint8)((v) >> 8);    \
225     }
226 #endif
227
228 #ifndef phtole24
229 #define phtole24(p, v) \
230     {                 \
231         (p)[0] = (guint8)((v) >> 0);     \
232         (p)[1] = (guint8)((v) >> 8);     \
233         (p)[2] = (guint8)((v) >> 16);    \
234     }
235 #endif
236
237 #ifndef phtolel
238 #define phtolel(p, v) \
239     {                 \
240         (p)[0] = (guint8)((v) >> 0);     \
241         (p)[1] = (guint8)((v) >> 8);     \
242         (p)[2] = (guint8)((v) >> 16);    \
243         (p)[3] = (guint8)((v) >> 24);    \
244     }
245 #endif
246
247 #ifndef phtolell
248 #define phtolell(p, v) \
249     {                 \
250         (p)[0] = (guint8)((v) >> 0);     \
251         (p)[1] = (guint8)((v) >> 8);     \
252         (p)[2] = (guint8)((v) >> 16);    \
253         (p)[3] = (guint8)((v) >> 24);    \
254         (p)[4] = (guint8)((v) >> 32);    \
255         (p)[5] = (guint8)((v) >> 40);    \
256         (p)[6] = (guint8)((v) >> 48);    \
257         (p)[7] = (guint8)((v) >> 56);    \
258     }
259 #endif
260
261 /* glib doesn't have g_ptr_array_len of all things!*/
262 #ifndef g_ptr_array_len
263 #define g_ptr_array_len(a)      ((a)->len)
264 #endif
265
266 /*
267  * Table of extensions for compressed file types we support.
268  * Last pointer in the list is null.
269  */
270 extern const char *compressed_file_extension_table[];
271
272 /*
273  * Read a given number of bytes from a file into a buffer or, if
274  * buf is NULL, just discard them.
275  *
276  * If we succeed, return TRUE.
277  *
278  * If we get an EOF, return FALSE with *err set to 0, reporting this
279  * as an EOF.
280  *
281  * If we get fewer bytes than the specified number, return FALSE with
282  * *err set to WTAP_ERR_SHORT_READ, reporting this as a short read
283  * error.
284  *
285  * If we get a read error, return FALSE with *err and *err_info set
286  * appropriately.
287  */
288 WS_DLL_PUBLIC
289 gboolean
290 wtap_read_bytes_or_eof(FILE_T fh, void *buf, unsigned int count, int *err,
291     gchar **err_info);
292
293 /*
294  * Read a given number of bytes from a file into a buffer or, if
295  * buf is NULL, just discard them.
296  *
297  * If we succeed, return TRUE.
298  *
299  * If we get fewer bytes than the specified number, including getting
300  * an EOF, return FALSE with *err set to WTAP_ERR_SHORT_READ, reporting
301  * this as a short read error.
302  *
303  * If we get a read error, return FALSE with *err and *err_info set
304  * appropriately.
305  */
306 WS_DLL_PUBLIC
307 gboolean
308 wtap_read_bytes(FILE_T fh, void *buf, unsigned int count, int *err,
309     gchar **err_info);
310
311 /*
312  * Read packet data into a Buffer, growing the buffer as necessary.
313  *
314  * This returns an error on a short read, even if the short read hit
315  * the EOF immediately.  (The assumption is that each packet has a
316  * header followed by raw packet data, and that we've already read the
317  * header, so if we get an EOF trying to read the packet data, the file
318  * has been cut short, even if the read didn't read any data at all.)
319  */
320 WS_DLL_PUBLIC
321 gboolean
322 wtap_read_packet_bytes(FILE_T fh, Buffer *buf, guint length, int *err,
323     gchar **err_info);
324
325 #endif /* __WTAP_INT_H__ */
326
327 /*
328  * Editor modelines
329  *
330  * Local Variables:
331  * c-basic-offset: 8
332  * tab-width: 8
333  * indent-tabs-mode: t
334  * End:
335  *
336  * ex: set shiftwidth=8 tabstop=8 noexpandtab:
337  * :indentSize=8:tabSize=8:noTabs=false:
338  */