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