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