Wireshark  4.3.0
The Wireshark network protocol analyzer
wtap-int.h
Go to the documentation of this file.
1 
9 #ifndef __WTAP_INT_H__
10 #define __WTAP_INT_H__
11 
12 #include "wtap.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_opttypes.h"
22 
23 void wtap_init_file_type_subtypes(void);
24 
25 WS_DLL_PUBLIC
26 int wtap_fstat(wtap *wth, ws_statb64 *statb, int *err);
27 
28 typedef bool (*subtype_read_func)(struct wtap*, wtap_rec *,
29  Buffer *, int *, char **, int64_t *);
30 typedef bool (*subtype_seek_read_func)(struct wtap*, int64_t, wtap_rec *,
31  Buffer *, int *, char **);
32 
36 struct wtap {
37  FILE_T fh;
39  bool ispipe;
40  int file_type_subtype;
41  unsigned snapshot_length;
42  GArray *shb_hdrs;
44  GArray *interface_data;
46  GArray *nrbs;
47  GArray *dsbs;
48  GArray *meta_events;
50  char *pathname;
52  void *priv; /* this one holds per-file state and is free'd automatically by wtap_close() */
53  void *wslua_data; /* this one holds wslua state info and is not free'd */
54 
55  subtype_read_func subtype_read;
56  subtype_seek_read_func subtype_seek_read;
57  void (*subtype_sequential_close)(struct wtap*);
58  void (*subtype_close)(struct wtap*);
59  int file_encap; /* per-file, for those
60  * file formats that have
61  * per-file encapsulation
62  * types rather than per-packet
63  * encapsulation types
64  */
65  int file_tsprec; /* per-file timestamp precision
66  * of the fractional part of
67  * the time stamp, for those
68  * file formats that have
69  * per-file timestamp
70  * precision rather than
71  * per-packet timestamp
72  * precision
73  * e.g. WTAP_TSPREC_USEC
74  */
75  wtap_new_ipv4_callback_t add_new_ipv4;
76  wtap_new_ipv6_callback_t add_new_ipv6;
77  wtap_new_secrets_callback_t add_new_secrets;
78  GPtrArray *fast_seek;
79 };
80 
81 struct wtap_dumper;
82 
83 /*
84  * This could either be a FILE * or a gzFile.
85  */
86 typedef void *WFILE_T;
87 
88 typedef bool (*subtype_add_idb_func)(struct wtap_dumper*, wtap_block_t,
89  int *, char **);
90 
91 typedef bool (*subtype_write_func)(struct wtap_dumper*,
92  const wtap_rec *rec,
93  const uint8_t*, int*, char**);
94 typedef bool (*subtype_finish_func)(struct wtap_dumper*, int*, char**);
95 
96 struct wtap_dumper {
97  WFILE_T fh;
98  int file_type_subtype;
99  int snaplen;
100  int file_encap; /* per-file, for those
101  * file formats that have
102  * per-file encapsulation
103  * types rather than per-packet
104  * encapsulation types
105  */
106  wtap_compression_type compression_type;
107  bool needs_reload; /* true if the file requires re-loading after saving with wtap */
108  int64_t bytes_dumped;
109 
110  void *priv; /* this one holds per-file state and is free'd automatically by wtap_dump_close() */
111  void *wslua_data; /* this one holds wslua state info and is not free'd */
112 
113  subtype_add_idb_func subtype_add_idb; /* add an IDB, writing it as necessary */
114  subtype_write_func subtype_write; /* write out a record */
115  subtype_finish_func subtype_finish; /* write out information to finish writing file */
116 
118  GArray *shb_hdrs;
119  const GArray *shb_iface_to_global;
120  GArray *interface_data;
121  GArray *dsbs_initial;
123  /*
124  * Additional blocks that might grow as data is being collected.
125  * Subtypes should write these blocks before writing new packet blocks.
126  */
127  const GArray *nrbs_growing;
128  const GArray *dsbs_growing;
129  const GArray *mevs_growing;
133 };
134 
135 WS_DLL_PUBLIC bool wtap_dump_file_write(wtap_dumper *wdh, const void *buf,
136  size_t bufsize, int *err);
137 WS_DLL_PUBLIC int64_t wtap_dump_file_seek(wtap_dumper *wdh, int64_t offset, int whence, int *err);
138 WS_DLL_PUBLIC int64_t wtap_dump_file_tell(wtap_dumper *wdh, int *err);
139 
140 extern int wtap_num_file_types;
141 
142 #include <wsutil/pint.h>
143 
144 /* Macros to byte-swap possibly-unaligned 64-bit, 32-bit and 16-bit quantities;
145  * they take a pointer to the quantity, and byte-swap it in place.
146  */
147 #define PBSWAP64(p) \
148  { \
149  uint8_t tmp; \
150  tmp = (p)[7]; \
151  (p)[7] = (p)[0]; \
152  (p)[0] = tmp; \
153  tmp = (p)[6]; \
154  (p)[6] = (p)[1]; \
155  (p)[1] = tmp; \
156  tmp = (p)[5]; \
157  (p)[5] = (p)[2]; \
158  (p)[2] = tmp; \
159  tmp = (p)[4]; \
160  (p)[4] = (p)[3]; \
161  (p)[3] = tmp; \
162  }
163 #define PBSWAP32(p) \
164  { \
165  uint8_t tmp; \
166  tmp = (p)[3]; \
167  (p)[3] = (p)[0]; \
168  (p)[0] = tmp; \
169  tmp = (p)[2]; \
170  (p)[2] = (p)[1]; \
171  (p)[1] = tmp; \
172  }
173 #define PBSWAP16(p) \
174  { \
175  uint8_t tmp; \
176  tmp = (p)[1]; \
177  (p)[1] = (p)[0]; \
178  (p)[0] = tmp; \
179  }
180 
181 
182 /* Pointer routines to put items out in a particular byte order.
183  * These will work regardless of the byte alignment of the pointer.
184  */
185 
186 #ifndef phtons
187 #define phtons(p, v) \
188  { \
189  (p)[0] = (uint8_t)((v) >> 8); \
190  (p)[1] = (uint8_t)((v) >> 0); \
191  }
192 #endif
193 
194 #ifndef phton24
195 #define phton24(p, v) \
196  { \
197  (p)[0] = (uint8_t)((v) >> 16); \
198  (p)[1] = (uint8_t)((v) >> 8); \
199  (p)[2] = (uint8_t)((v) >> 0); \
200  }
201 #endif
202 
203 #ifndef phtonl
204 #define phtonl(p, v) \
205  { \
206  (p)[0] = (uint8_t)((v) >> 24); \
207  (p)[1] = (uint8_t)((v) >> 16); \
208  (p)[2] = (uint8_t)((v) >> 8); \
209  (p)[3] = (uint8_t)((v) >> 0); \
210  }
211 #endif
212 
213 #ifndef phtonll
214 #define phtonll(p, v) \
215  { \
216  (p)[0] = (uint8_t)((v) >> 56); \
217  (p)[1] = (uint8_t)((v) >> 48); \
218  (p)[2] = (uint8_t)((v) >> 40); \
219  (p)[3] = (uint8_t)((v) >> 32); \
220  (p)[4] = (uint8_t)((v) >> 24); \
221  (p)[5] = (uint8_t)((v) >> 16); \
222  (p)[6] = (uint8_t)((v) >> 8); \
223  (p)[7] = (uint8_t)((v) >> 0); \
224  }
225 #endif
226 
227 #ifndef phtole8
228 #define phtole8(p, v) \
229  { \
230  (p)[0] = (uint8_t)((v) >> 0); \
231  }
232 #endif
233 
234 #ifndef phtoles
235 #define phtoles(p, v) \
236  { \
237  (p)[0] = (uint8_t)((v) >> 0); \
238  (p)[1] = (uint8_t)((v) >> 8); \
239  }
240 #endif
241 
242 #ifndef phtole24
243 #define phtole24(p, v) \
244  { \
245  (p)[0] = (uint8_t)((v) >> 0); \
246  (p)[1] = (uint8_t)((v) >> 8); \
247  (p)[2] = (uint8_t)((v) >> 16); \
248  }
249 #endif
250 
251 #ifndef phtolel
252 #define phtolel(p, v) \
253  { \
254  (p)[0] = (uint8_t)((v) >> 0); \
255  (p)[1] = (uint8_t)((v) >> 8); \
256  (p)[2] = (uint8_t)((v) >> 16); \
257  (p)[3] = (uint8_t)((v) >> 24); \
258  }
259 #endif
260 
261 #ifndef phtolell
262 #define phtolell(p, v) \
263  { \
264  (p)[0] = (uint8_t)((v) >> 0); \
265  (p)[1] = (uint8_t)((v) >> 8); \
266  (p)[2] = (uint8_t)((v) >> 16); \
267  (p)[3] = (uint8_t)((v) >> 24); \
268  (p)[4] = (uint8_t)((v) >> 32); \
269  (p)[5] = (uint8_t)((v) >> 40); \
270  (p)[6] = (uint8_t)((v) >> 48); \
271  (p)[7] = (uint8_t)((v) >> 56); \
272  }
273 #endif
274 
275 /* glib doesn't have g_ptr_array_len of all things!*/
276 #ifndef g_ptr_array_len
277 #define g_ptr_array_len(a) ((a)->len)
278 #endif
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 an EOF, return false with *err set to 0, reporting this
287  * as an EOF.
288  *
289  * If we get fewer bytes than the specified number, return false with
290  * *err set to WTAP_ERR_SHORT_READ, reporting this as a short read
291  * error.
292  *
293  * If we get a read error, return false with *err and *err_info set
294  * appropriately.
295  */
296 WS_DLL_PUBLIC
297 bool
298 wtap_read_bytes_or_eof(FILE_T fh, void *buf, unsigned int count, int *err,
299  char **err_info);
300 
301 /*
302  * Read a given number of bytes from a file into a buffer or, if
303  * buf is NULL, just discard them.
304  *
305  * If we succeed, return true.
306  *
307  * If we get fewer bytes than the specified number, including getting
308  * an EOF, return false with *err set to WTAP_ERR_SHORT_READ, reporting
309  * this as a short read error.
310  *
311  * If we get a read error, return false with *err and *err_info set
312  * appropriately.
313  */
314 WS_DLL_PUBLIC
315 bool
316 wtap_read_bytes(FILE_T fh, void *buf, unsigned int count, int *err,
317  char **err_info);
318 
319 /*
320  * Read packet data into a Buffer, growing the buffer as necessary.
321  *
322  * This returns an error on a short read, even if the short read hit
323  * the EOF immediately. (The assumption is that each packet has a
324  * header followed by raw packet data, and that we've already read the
325  * header, so if we get an EOF trying to read the packet data, the file
326  * has been cut short, even if the read didn't read any data at all.)
327  */
328 WS_DLL_PUBLIC
329 bool
330 wtap_read_packet_bytes(FILE_T fh, Buffer *buf, unsigned length, int *err,
331  char **err_info);
332 
333 /*
334  * Implementation of wth->subtype_read that reads the full file contents
335  * as a single packet.
336  */
337 bool
338 wtap_full_file_read(wtap *wth, wtap_rec *rec, Buffer *buf,
339  int *err, char **err_info, int64_t *data_offset);
340 
341 /*
342  * Implementation of wth->subtype_seek_read that reads the full file contents
343  * as a single packet.
344  */
345 bool
346 wtap_full_file_seek_read(wtap *wth, int64_t seek_off, wtap_rec *rec, Buffer *buf, int *err, char **err_info);
347 
351 void
353 
357 void
359 
363 void
365 
366 void
367 wtap_register_compatibility_file_subtype_name(const char *old_name,
368  const char *new_name);
369 
370 void
371 wtap_register_backwards_compatibility_lua_name(const char *name, int ft);
372 
374  const char *name;
375  int ft;
376 };
377 
378 WS_DLL_PUBLIC
379 const GArray *get_backwards_compatibility_lua_table(void);
380 
395 
407 WS_DLL_PUBLIC
408 void wtap_add_generated_idb(wtap *wth);
409 
423 
436 
448 
449 #endif /* __WTAP_INT_H__ */
450 
451 /*
452  * Editor modelines - https://www.wireshark.org/tools/modelines.html
453  *
454  * Local variables:
455  * c-basic-offset: 4
456  * tab-width: 8
457  * indent-tabs-mode: nil
458  * End:
459  *
460  * vi: set shiftwidth=4 tabstop=8 expandtab:
461  * :indentSize=4:tabSize=8:noTabs=true:
462  */
Definition: buffer.h:22
Definition: wtap.h:1469
Definition: wtap-int.h:373
Definition: pcapio.c:113
Definition: wtap_opttypes.c:85
Definition: wtap.h:1489
Definition: wtap-int.h:96
unsigned nrbs_growing_written
Definition: wtap-int.h:130
GArray * dsbs_initial
Definition: wtap-int.h:121
GArray * interface_data
Definition: wtap-int.h:120
const GArray * dsbs_growing
Definition: wtap-int.h:128
const GArray * mevs_growing
Definition: wtap-int.h:129
unsigned mevs_growing_written
Definition: wtap-int.h:132
const GArray * shb_iface_to_global
Definition: wtap-int.h:119
unsigned dsbs_growing_written
Definition: wtap-int.h:131
addrinfo_lists_t * addrinfo_lists
Definition: wtap-int.h:117
const GArray * nrbs_growing
Definition: wtap-int.h:127
Definition: file_wrappers.c:168
Definition: wtap.h:1395
Definition: wtap-int.h:36
GArray * interface_data
Definition: wtap-int.h:44
bool ispipe
Definition: wtap-int.h:39
GArray * shb_iface_to_global
Definition: wtap-int.h:43
FILE_T random_fh
Definition: wtap-int.h:38
unsigned next_interface_data
Definition: wtap-int.h:45
GArray * nrbs
Definition: wtap-int.h:46
char * pathname
Definition: wtap-int.h:50
GArray * meta_events
Definition: wtap-int.h:48
GArray * dsbs
Definition: wtap-int.h:47
void wtapng_process_nrb(wtap *wth, wtap_block_t nrb)
Definition: wtap.c:1668
GArray * wtap_file_get_shb_for_new_file(wtap *wth)
Gets new section header block for new file, based on existing info.
Definition: wtap.c:149
void wtapng_process_dsb(wtap *wth, wtap_block_t dsb)
Definition: wtap.c:1692
GArray * wtap_file_get_nrb_for_new_file(wtap *wth)
Gets new name resolution info for new file, based on existing info.
Definition: wtap.c:520
wtap_block_t wtap_rec_generate_idb(const wtap_rec *rec)
Generate an IDB, given a packet record, using the records's encapsulation type and time stamp resolut...
Definition: wtap.c:1902
wtap_block_t wtap_dump_params_generate_idb(const wtap_dump_params *params)
Generate an IDB, given a set of dump parameters, using the parameters' encapsulation type,...
Definition: wtap.c:616
WS_DLL_PUBLIC void wtap_add_generated_idb(wtap *wth)
Generate an IDB, given a wiretap handle for the file, using the file's encapsulation type,...
Definition: wtap.c:350
void wtap_add_idb(wtap *wth, wtap_block_t idb)
Definition: wtap.c:274
void(* wtap_new_secrets_callback_t)(uint32_t secrets_type, const void *secrets, unsigned size)
Definition: wtap.h:1853
void(* wtap_new_ipv4_callback_t)(const unsigned addr, const char *name, const bool static_entry)
Definition: wtap.h:1841