Stop loading a pcapng file with multiple section header blocks.
[metze/wireshark/wip.git] / wiretap / wtap-int.h
1 /* wtap-int.h
2  *
3  * $Id$
4  *
5  * Wiretap Library
6  * Copyright (c) 1998 by Gilbert Ramirez <gram@alumni.rice.edu>
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License
10  * as published by the Free Software Foundation; either version 2
11  * of the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
21  */
22
23 #ifndef __WTAP_INT_H__
24 #define __WTAP_INT_H__
25
26 #ifdef HAVE_SYS_TIME_H
27 #include <sys/time.h>
28 #endif
29
30 #include <glib.h>
31 #include <stdio.h>
32 #include <time.h>
33
34 #ifdef HAVE_WINSOCK2_H
35 #include <winsock2.h>
36 #endif
37
38 #ifdef HAVE_LIBZ
39 #include <zlib.h>
40 #define FILE_T  gzFile
41 #else /* No zLib */
42 #define FILE_T  FILE *
43 #endif /* HAVE_LIBZ */
44
45 #include "wtap.h"
46
47 /* Information for a compressed Sniffer data stream. */
48 typedef struct {
49         unsigned char *buf;     /* buffer into which we uncompress data */
50         size_t  nbytes;         /* number of bytes of data in that buffer */
51         int     nextout;        /* offset in that buffer of stream's current position */
52         gint64  comp_offset;    /* current offset in compressed data stream */
53         gint64  uncomp_offset;  /* current offset in uncompressed data stream */
54 } ngsniffer_comp_stream_t;
55
56 typedef struct {
57   char *sdate;            /* Packet start date        */
58   gboolean tcp_formatted; /* TCP/IP data formated Y/N */
59   int format;             /* Trace format type        */
60 } iseries_t;
61
62 typedef struct {
63         guint   maj_vers;
64         guint   min_vers;
65         guint32 timeunit;
66         time_t  start;
67         guint   network;                /* network type */
68         ngsniffer_comp_stream_t seq;    /* sequential access */
69         ngsniffer_comp_stream_t rand;   /* random access */
70         GList   *first_blob;            /* list element for first blob */
71         GList   *last_blob;             /* list element for last blob */
72         GList   *current_blob;          /* list element for current blob */
73 } ngsniffer_t;
74
75 typedef struct {
76         gboolean byte_swapped;
77 } i4btrace_t;
78
79 typedef struct {
80         gboolean is_hpux_11;
81 } nettl_t;
82
83 typedef struct {
84         time_t  start;
85 } lanalyzer_t;
86
87 typedef enum {
88         NOT_SWAPPED,
89         SWAPPED,
90         MAYBE_SWAPPED
91 } swapped_type_t;
92
93 typedef struct {
94         gboolean byte_swapped;
95         swapped_type_t lengths_swapped;
96         guint16 version_major;
97         guint16 version_minor;
98 } libpcap_t;
99
100 typedef struct {
101         gboolean byte_swapped;
102         guint16 version_major;
103         guint16 version_minor;
104         gint8 if_fcslen;
105         GArray *interface_data;
106         guint number_of_interfaces;
107 } pcapng_t;
108
109 typedef struct {
110         time_t  start_secs;
111         guint32 start_usecs;
112         guint8  version_major;
113         guint32 *frame_table;
114         guint32 frame_table_size;
115         guint   current_frame;
116 } netmon_t;
117
118 typedef struct {
119         time_t          start_time;
120         double          ticks_per_sec;
121         double          start_timestamp;
122         gboolean        wrapped;
123         guint32         nframes;
124         gint64          start_offset;
125         gint64          end_offset;
126         int             version_major;
127         gboolean        fcs_valid;      /* if packets have valid FCS at the end */
128         guint           isdn_type;      /* 1 = E1 PRI, 2 = T1 PRI, 3 = BRI */
129 } netxray_t;
130
131 typedef struct {
132         time_t inittime;
133         int adjusted;
134         gint64 next_packet_seek_start;
135 } ascend_t;
136
137 typedef struct {
138         gboolean byteswapped;
139 } csids_t;
140
141 typedef struct {
142         struct timeval reference_time;
143 } etherpeek_t;
144
145 typedef struct {
146         gboolean        has_fcs;
147 } airopeek9_t;
148
149 typedef struct _k12_t k12_t;
150
151 typedef struct {
152         time_t  start_secs;
153         guint32 start_usecs;
154 } catapult_dct2000_t;
155
156 typedef struct {
157         struct wtap_nstime now;
158         time_t t0;
159 } mpeg_t;
160
161 typedef struct {
162         gchar *pnstrace_buf;
163         gint32 nstrace_buf_offset;
164         gint32 nstrace_buflen;
165         /* Performance Monitor Time variables */
166         guint32 nspm_curtime;           /* current time since 1970 */
167         guint64 nspm_curtimemsec;       /* current time in mili second */
168         guint64 nspm_curtimelastmsec;   /* nspm_curtime last update time in milisec */
169         guint64 nsg_creltime;
170         guint64 file_size;
171 } nstrace_t;
172
173
174 typedef gboolean (*subtype_read_func)(struct wtap*, int*, char**, gint64*);
175 typedef gboolean (*subtype_seek_read_func)(struct wtap*, gint64, union wtap_pseudo_header*,
176                                         guint8*, int, int *, char **);
177 struct wtap {
178         FILE_T                  fh;
179         int                     fd;           /* File descriptor for cap file */
180         FILE_T                  random_fh;    /* Secondary FILE_T for random access */
181         int                     file_type;
182         int                     snapshot_length;
183         struct Buffer           *frame_buffer;
184         struct wtap_pkthdr      phdr;
185         union wtap_pseudo_header pseudo_header;
186
187         gint64                  data_offset;
188
189         union {
190                 libpcap_t               *pcap;
191                 lanalyzer_t             *lanalyzer;
192                 ngsniffer_t             *ngsniffer;
193                 iseries_t               *iseries;
194                 i4btrace_t              *i4btrace;
195                 nettl_t                 *nettl;
196                 netmon_t                *netmon;
197                 netxray_t               *netxray;
198                 ascend_t                *ascend;
199                 csids_t                 *csids;
200                 etherpeek_t             *etherpeek;
201                 airopeek9_t             *airopeek9;
202                 k12_t                   *k12;
203                 catapult_dct2000_t      *catapult_dct2000;
204                 mpeg_t                  *mpeg;
205                 nstrace_t               *nstrace;
206                 void                    *generic;
207                 pcapng_t                *pcapng;
208         } capture;
209
210         subtype_read_func       subtype_read;
211         subtype_seek_read_func  subtype_seek_read;
212         void                    (*subtype_sequential_close)(struct wtap*);
213         void                    (*subtype_close)(struct wtap*);
214         int                     file_encap;     /* per-file, for those
215                                                    file formats that have
216                                                    per-file encapsulation
217                                                    types */
218         int                     tsprecision;    /* timestamp precision of the lower 32bits
219                                                  * e.g. WTAP_FILE_TSPREC_USEC */
220 };
221
222 struct wtap_dumper;
223
224 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
225                 const struct wtap_pkthdr*, const union wtap_pseudo_header*,
226                 const guchar*, int*);
227 typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
228
229 typedef struct {
230         gboolean first_frame;
231         time_t start;
232 } ngsniffer_dump_t;
233
234 typedef struct {
235         gboolean first_frame;
236         struct wtap_nstime start;
237         guint32 nframes;
238 } netxray_dump_t;
239
240 typedef struct {
241         gboolean got_first_record_time;
242         struct wtap_nstime first_record_time;
243         guint32 frame_table_offset;
244         guint32 *frame_table;
245         guint   frame_table_index;
246         guint   frame_table_size;
247 } netmon_dump_t;
248
249 typedef struct {
250         guint16 page_offset;
251         guint16 page_len;
252         guint32 absrec_time;
253 } nstrace_dump_t;
254
255 typedef struct {
256         guint32 nframes;
257 } _5views_dump_t;
258
259 typedef struct {
260         guint64 packet_count;
261         guint8  network_type;
262 } niobserver_dump_t;
263
264 typedef struct {
265         guint32 file_len;
266         guint32 num_of_records;
267         guint32 file_offset;
268 } k12_dump_t;
269
270 typedef struct {
271     gboolean           first_packet_written;
272     struct wtap_nstime start_time;
273 } dct2000_dump_t;
274
275 typedef struct {
276         GArray *interface_data;
277         guint number_of_interfaces;
278 } pcapng_dump_t;
279
280 struct wtap_dumper {
281         FILE*                   fh;
282         int                     file_type;
283         int                     snaplen;
284         int                     encap;
285         gboolean        compressed;
286         gint64          bytes_dumped;
287
288         union {
289                 void                    *opaque;
290                 ngsniffer_dump_t        *ngsniffer;
291                 netmon_dump_t           *netmon;
292                 netxray_dump_t          *netxray;
293                 _5views_dump_t          *_5views;
294                 niobserver_dump_t       *niobserver;
295                 k12_dump_t              *k12;
296                 dct2000_dump_t          *dct2000;
297                 nstrace_dump_t          *nstr;
298                 pcapng_dump_t           *pcapng;
299         } dump;
300
301         subtype_write_func      subtype_write;
302         subtype_close_func      subtype_close;
303
304         int                     tsprecision;    /* timestamp precision of the lower 32bits
305                                                          * e.g. WTAP_FILE_TSPREC_USEC */
306 };
307
308 extern gboolean wtap_dump_file_write_all(wtap_dumper *wdh, const void *buf, unsigned bufsize, int *err);
309 extern size_t wtap_dump_file_write(wtap_dumper *wdh, const void *buf, size_t bufsize);
310 extern int wtap_dump_file_ferror(wtap_dumper *wdh);
311
312 extern gint wtap_num_file_types;
313
314 /* Macros to byte-swap 64-bit, 32-bit and 16-bit quantities. */
315 #define BSWAP64(x) \
316         ((((x)&G_GINT64_CONSTANT(0xFF00000000000000U))>>56) |   \
317          (((x)&G_GINT64_CONSTANT(0x00FF000000000000U))>>40) |   \
318          (((x)&G_GINT64_CONSTANT(0x0000FF0000000000U))>>24) |   \
319          (((x)&G_GINT64_CONSTANT(0x000000FF00000000U))>>8) |    \
320          (((x)&G_GINT64_CONSTANT(0x00000000FF000000U))<<8) |    \
321          (((x)&G_GINT64_CONSTANT(0x0000000000FF0000U))<<24) |   \
322          (((x)&G_GINT64_CONSTANT(0x000000000000FF00U))<<40) |   \
323          (((x)&G_GINT64_CONSTANT(0x00000000000000FFU))<<56))
324 #define BSWAP32(x) \
325         ((((x)&0xFF000000)>>24) | \
326          (((x)&0x00FF0000)>>8) | \
327          (((x)&0x0000FF00)<<8) | \
328          (((x)&0x000000FF)<<24))
329 #define BSWAP16(x) \
330          ((((x)&0xFF00)>>8) | \
331           (((x)&0x00FF)<<8))
332
333 /* Macros to byte-swap possibly-unaligned 32-bit and 16-bit quantities;
334  * they take a pointer to the quantity, and byte-swap it in place.
335  */
336 #define PBSWAP32(p) \
337         {                       \
338         guint8 tmp;             \
339         tmp = (p)[3];           \
340         (p)[3] = (p)[0];        \
341         (p)[0] = tmp;           \
342         tmp = (p)[2];           \
343         (p)[2] = (p)[1];        \
344         (p)[1] = tmp;           \
345         }
346 #define PBSWAP16(p) \
347         {                       \
348         guint8 tmp;             \
349         tmp = (p)[1];           \
350         (p)[1] = (p)[0];        \
351         (p)[0] = tmp;           \
352         }
353
354 /* Turn host-byte-order values into little-endian values. */
355 #define htoles(s) GUINT16_TO_LE(s)
356 #define htolel(l) GUINT32_TO_LE(l)
357 #define htolell(ll) GUINT64_TO_LE(ll)
358
359 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
360  * byte array, returns the value of the two or four bytes at the pointer.
361  * The pletoh[sl] versions return the little-endian representation.
362  * We also provide pntohll and pletohll, which extract 64-bit integral
363  * quantities.
364  *
365  * These will work regardless of the byte alignment of the pointer.
366  */
367
368 #ifndef pntohs
369 #define pntohs(p)  ((guint16)                       \
370                     ((guint16)*((const guint8 *)(p)+0)<<8|  \
371                      (guint16)*((const guint8 *)(p)+1)<<0))
372 #endif
373
374 #ifndef pntoh24
375 #define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16| \
376                      (guint32)*((const guint8 *)(p)+1)<<8|  \
377                      (guint32)*((const guint8 *)(p)+2)<<0)
378 #endif
379
380 #ifndef pntohl
381 #define pntohl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
382                     (guint32)*((const guint8 *)(p)+1)<<16|  \
383                     (guint32)*((const guint8 *)(p)+2)<<8|   \
384                     (guint32)*((const guint8 *)(p)+3)<<0)
385 #endif
386
387 #ifndef pntohll
388 #define pntohll(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
389                      (guint64)*((const guint8 *)(p)+1)<<48|  \
390                      (guint64)*((const guint8 *)(p)+2)<<40|  \
391                      (guint64)*((const guint8 *)(p)+3)<<32|  \
392                      (guint64)*((const guint8 *)(p)+4)<<24|  \
393                      (guint64)*((const guint8 *)(p)+5)<<16|  \
394                      (guint64)*((const guint8 *)(p)+6)<<8|   \
395                      (guint64)*((const guint8 *)(p)+7)<<0)
396 #endif
397
398
399 #ifndef pletohs
400 #define pletohs(p) ((guint16)                       \
401                     ((guint16)*((const guint8 *)(p)+1)<<8|  \
402                      (guint16)*((const guint8 *)(p)+0)<<0))
403 #endif
404
405 #ifndef pletoh24
406 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
407                      (guint32)*((const guint8 *)(p)+1)<<8|  \
408                      (guint32)*((const guint8 *)(p)+0)<<0)
409 #endif
410
411
412 #ifndef pletohl
413 #define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24|  \
414                     (guint32)*((const guint8 *)(p)+2)<<16|  \
415                     (guint32)*((const guint8 *)(p)+1)<<8|   \
416                     (guint32)*((const guint8 *)(p)+0)<<0)
417 #endif
418
419
420 #ifndef pletohll
421 #define pletohll(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
422                      (guint64)*((const guint8 *)(p)+6)<<48|  \
423                      (guint64)*((const guint8 *)(p)+5)<<40|  \
424                      (guint64)*((const guint8 *)(p)+4)<<32|  \
425                      (guint64)*((const guint8 *)(p)+3)<<24|  \
426                      (guint64)*((const guint8 *)(p)+2)<<16|  \
427                      (guint64)*((const guint8 *)(p)+1)<<8|   \
428                      (guint64)*((const guint8 *)(p)+0)<<0)
429 #endif
430
431 /* Pointer routines to put items out in a particular byte order.
432  * These will work regardless of the byte alignment of the pointer.
433  */
434
435 #ifndef phtons
436 #define phtons(p, v) \
437         {                               \
438         (p)[0] = (guint8)((v) >> 8);    \
439         (p)[1] = (guint8)((v) >> 0);    \
440         }
441 #endif
442
443 #ifndef phtonl
444 #define phtonl(p, v) \
445         {                               \
446         (p)[0] = (guint8)((v) >> 24);   \
447         (p)[1] = (guint8)((v) >> 16);   \
448         (p)[2] = (guint8)((v) >> 8);    \
449         (p)[3] = (guint8)((v) >> 0);    \
450         }
451 #endif
452
453 #ifndef phtonll
454 #define phtonll(p, v) \
455         {                               \
456         (p)[0] = (guint8)((v) >> 56);   \
457         (p)[1] = (guint8)((v) >> 48);   \
458         (p)[2] = (guint8)((v) >> 40);   \
459         (p)[3] = (guint8)((v) >> 32);   \
460         (p)[4] = (guint8)((v) >> 24);   \
461         (p)[5] = (guint8)((v) >> 16);   \
462         (p)[6] = (guint8)((v) >> 8);    \
463         (p)[7] = (guint8)((v) >> 0);    \
464         }
465 #endif
466
467 #ifndef pletonll
468 #define pletonll(p, v) \
469         {                               \
470         (p)[0] = (guint8)((v) >> 0);    \
471         (p)[1] = (guint8)((v) >> 8);    \
472         (p)[2] = (guint8)((v) >> 16);   \
473         (p)[3] = (guint8)((v) >> 24);   \
474         (p)[4] = (guint8)((v) >> 32);   \
475         (p)[5] = (guint8)((v) >> 40);   \
476         (p)[6] = (guint8)((v) >> 48);   \
477         (p)[7] = (guint8)((v) >> 56);   \
478         }
479 #endif
480
481 #define wtap_file_read_unknown_bytes(target, num_bytes, fh, err) \
482         G_STMT_START \
483         { \
484                 int _bytes_read; \
485                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
486                 if (_bytes_read != (int) (num_bytes)) { \
487                         *(err) = file_error((fh)); \
488                         return FALSE; \
489                 } \
490         } \
491         G_STMT_END
492
493 #define wtap_file_read_expected_bytes(target, num_bytes, fh, err) \
494         G_STMT_START \
495         { \
496                 int _bytes_read; \
497                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
498                 if (_bytes_read != (int) (num_bytes)) { \
499                         *(err) = file_error((fh)); \
500                         if (*(err) == 0 && _bytes_read > 0) { \
501                                 *(err) = WTAP_ERR_SHORT_READ; \
502                         } \
503                         return FALSE; \
504                 } \
505         } \
506         G_STMT_END
507
508 /* glib doesn't have g_ptr_array_len of all things!*/
509 #ifndef g_ptr_array_len
510 #define g_ptr_array_len(a)      ((a)->len)
511 #endif
512
513 #endif /* __WTAP_INT_H__ */