In the EtherPeek file reader, keep the capture start time in a private
[obnox/wireshark/wip.git] / wiretap / wtap-int.h
1 /* wtap-int.h
2  *
3  * $Id: wtap-int.h,v 1.19 2002/01/18 00:25:50 guy Exp $
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
24 #ifndef __WTAP_INT_H__
25 #define __WTAP_INT_H__
26
27 #ifdef HAVE_SYS_TYPES_H
28 #include <sys/types.h>
29 #endif
30
31 #ifdef HAVE_SYS_TIME_H
32 #include <sys/time.h>
33 #endif
34
35 #ifdef HAVE_WINSOCK_H
36 #include <winsock.h>
37 #endif
38
39 #include <glib.h>
40 #include <stdio.h>
41 #include <time.h>
42
43 #ifdef HAVE_LIBZ
44 #include "zlib.h"
45 #define FILE_T  gzFile
46 #else /* No zLib */
47 #define FILE_T  FILE *
48 #endif /* HAVE_LIBZ */
49
50 #include "wtap.h"
51
52 /* Information for a compressed Sniffer data stream. */
53 typedef struct {
54         unsigned char *buf;     /* buffer into which we uncompress data */
55         size_t  nbytes;         /* number of bytes of data in that buffer */
56         int     nextout;        /* offset in that buffer of stream's current position */
57         long    comp_offset;    /* current offset in compressed data stream */
58         long    uncomp_offset;  /* current offset in uncompressed data stream */
59 } ngsniffer_comp_stream_t;
60
61 typedef struct {
62         double  timeunit;
63         time_t  start;
64         int     is_atm;
65         ngsniffer_comp_stream_t seq;    /* sequential access */
66         ngsniffer_comp_stream_t rand;   /* random access */
67         GList   *first_blob;            /* list element for first blob */
68         GList   *last_blob;             /* list element for last blob */
69         GList   *current_blob;          /* list element for current blob */
70 } ngsniffer_t;
71
72 typedef struct {
73         gboolean byte_swapped;
74         int bchannel_prot[2];   /* For the V.120 heuristic */
75 } i4btrace_t;
76
77 typedef struct {
78         gboolean is_hpux_11;
79 } nettl_t;
80
81 typedef struct {
82         time_t  start;
83 } lanalyzer_t;
84
85 typedef struct {
86         gboolean byte_swapped;
87         guint16 version_major;
88         guint16 version_minor;
89 } libpcap_t;
90
91 typedef struct {
92         time_t  start_secs;
93         guint32 start_usecs;
94         guint8  version_major;
95         guint32 *frame_table;
96         guint32 frame_table_size;
97         guint   current_frame;
98 } netmon_t;
99
100 typedef struct {
101         time_t  start_time;
102         double  timeunit;
103         double  start_timestamp;
104         int     wrapped;
105         int     end_offset;
106         int     version_major;
107 } netxray_t;
108
109 typedef struct {
110         time_t inittime;
111         int adjusted;
112         long next_packet_seek_start;
113 } ascend_t;
114
115 typedef struct {
116         gboolean byteswapped;
117 } csids_t;
118
119 typedef struct {
120         struct timeval start_timestamp;
121 } etherpeek_t;
122
123 typedef int (*subtype_read_func)(struct wtap*, int*, long*);
124 typedef int (*subtype_seek_read_func)(struct wtap*, long, union wtap_pseudo_header*,
125                                         guint8*, int);
126 struct wtap {
127         FILE_T                  fh;
128         int                     fd;           /* File descriptor for cap file */
129         FILE_T                  random_fh;    /* Secondary FILE_T for random access */
130         int                     file_type;
131         int                     snapshot_length;
132         struct Buffer           *frame_buffer;
133         struct wtap_pkthdr      phdr;
134         union wtap_pseudo_header pseudo_header;
135
136         long                    data_offset;
137
138         union {
139                 libpcap_t               *pcap;
140                 lanalyzer_t             *lanalyzer;
141                 ngsniffer_t             *ngsniffer;
142                 i4btrace_t              *i4btrace;
143                 nettl_t                 *nettl;
144                 netmon_t                *netmon;
145                 netxray_t               *netxray;
146                 ascend_t                *ascend;
147                 csids_t                 *csids;
148                 etherpeek_t             *etherpeek;
149                 void                    *generic;
150         } capture;
151
152         subtype_read_func       subtype_read;
153         subtype_seek_read_func  subtype_seek_read;
154         void                    (*subtype_sequential_close)(struct wtap*);
155         void                    (*subtype_close)(struct wtap*);
156         int                     file_encap;     /* per-file, for those
157                                                    file formats that have
158                                                    per-file encapsulation
159                                                    types */
160 };
161
162 struct wtap_dumper;
163
164 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
165                 const struct wtap_pkthdr*, const union wtap_pseudo_header*,
166                 const u_char*, int*);
167 typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
168
169 typedef struct {
170         gboolean first_frame;
171         time_t start;
172 } ngsniffer_dump_t;
173
174 typedef struct {
175         gboolean first_frame;
176         struct timeval start;
177         guint32 nframes;
178 } netxray_dump_t;
179
180 typedef struct {
181         gboolean got_first_record_time;
182         struct timeval first_record_time;
183         guint32 frame_table_offset;
184         guint32 *frame_table;
185         guint   frame_table_index;
186         guint   frame_table_size;
187 } netmon_dump_t;
188
189 struct wtap_dumper {
190         FILE*                   fh;
191         int                     file_type;
192         int                     snaplen;
193         int                     encap;
194         long                    bytes_dumped;
195
196         union {
197                 void                    *opaque;
198                 ngsniffer_dump_t        *ngsniffer;
199                 netmon_dump_t           *netmon;
200                 netxray_dump_t          *netxray;
201         } dump;
202
203         subtype_write_func      subtype_write;
204         subtype_close_func      subtype_close;
205 };
206
207
208 /* Macros to byte-swap 32-bit and 16-bit quantities. */
209 #define BSWAP32(x) \
210         ((((x)&0xFF000000)>>24) | \
211          (((x)&0x00FF0000)>>8) | \
212          (((x)&0x0000FF00)<<8) | \
213          (((x)&0x000000FF)<<24))
214 #define BSWAP16(x) \
215          ((((x)&0xFF00)>>8) | \
216           (((x)&0x00FF)<<8))
217
218 /* Turn host-byte-order values into little-endian values. */
219 #ifdef WORDS_BIGENDIAN
220 #define htoles(s) ((guint16)                       \
221                     ((guint16)((s) & 0x00FF)<<8|  \
222                      (guint16)((s) & 0xFF00)>>8))
223
224 #define htolel(l) ((guint32)((l) & 0x000000FF)<<24|  \
225                    (guint32)((l) & 0x0000FF00)<<8|  \
226                    (guint32)((l) & 0x00FF0000)>>8|   \
227                    (guint32)((l) & 0xFF000000)>>24)
228
229 #ifdef G_HAVE_GINT64
230 #define htolell(ll) GUINT64_TO_LE(ll)
231 #endif /* G_HAVE_GINT64 */
232
233 #else /* WORDS_BIGENDIAN */
234 #define htoles(s)       (s)
235 #define htolel(l)       (l)
236 #define htolell(ll)     (ll)
237 #endif /* WORDS_BIGENDIAN */
238
239 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
240  * byte array, returns the value of the two or four bytes at the pointer.
241  * The pletoh[sl] versions return the little-endian representation.
242  *
243  * If G_HAVE_GINT64 is defined, so we can use "gint64" and "guint64" to
244  * refer to 64-bit integral quantities, we also provide pntohll and
245  * phtolell, which extract 64-bit integral quantities.
246  */
247
248 #ifndef pntohs
249 #define pntohs(p)  ((guint16)                       \
250                     ((guint16)*((guint8 *)(p)+0)<<8|  \
251                      (guint16)*((guint8 *)(p)+1)<<0))
252 #endif
253
254 #ifndef pntoh24
255 #define pntoh24(p)  ((guint32)*((guint8 *)(p)+0)<<16| \
256                      (guint32)*((guint8 *)(p)+1)<<8|  \
257                      (guint32)*((guint8 *)(p)+2)<<0)
258 #endif
259
260 #ifndef pntohl
261 #define pntohl(p)  ((guint32)*((guint8 *)(p)+0)<<24|  \
262                     (guint32)*((guint8 *)(p)+1)<<16|  \
263                     (guint32)*((guint8 *)(p)+2)<<8|   \
264                     (guint32)*((guint8 *)(p)+3)<<0)
265 #endif
266
267 #ifdef G_HAVE_GINT64
268 #ifndef pntohll
269 #define pntohll(p)  ((guint64)*((guint8 *)(p)+0)<<56|  \
270                      (guint64)*((guint8 *)(p)+1)<<48|  \
271                      (guint64)*((guint8 *)(p)+2)<<40|  \
272                      (guint64)*((guint8 *)(p)+3)<<32|  \
273                      (guint64)*((guint8 *)(p)+4)<<24|  \
274                      (guint64)*((guint8 *)(p)+5)<<16|  \
275                      (guint64)*((guint8 *)(p)+6)<<8|   \
276                      (guint64)*((guint8 *)(p)+7)<<0)
277 #endif
278 #endif
279
280
281 #ifndef phtons
282 #define phtons(p)  ((guint16)                       \
283                     ((guint16)*((guint8 *)(p)+0)<<8|  \
284                      (guint16)*((guint8 *)(p)+1)<<0))
285 #endif
286
287 #ifndef phtonl
288 #define phtonl(p)  ((guint32)*((guint8 *)(p)+0)<<24|  \
289                     (guint32)*((guint8 *)(p)+1)<<16|  \
290                     (guint32)*((guint8 *)(p)+2)<<8|   \
291                     (guint32)*((guint8 *)(p)+3)<<0)
292 #endif
293
294 #ifndef pletohs
295 #define pletohs(p) ((guint16)                       \
296                     ((guint16)*((guint8 *)(p)+1)<<8|  \
297                      (guint16)*((guint8 *)(p)+0)<<0))
298 #endif
299
300 #ifndef pletoh24
301 #define pletoh24(p) ((guint32)*((guint8 *)(p)+2)<<16|  \
302                      (guint32)*((guint8 *)(p)+1)<<8|  \
303                      (guint32)*((guint8 *)(p)+0)<<0)
304 #endif
305
306
307 #ifndef pletohl
308 #define pletohl(p) ((guint32)*((guint8 *)(p)+3)<<24|  \
309                     (guint32)*((guint8 *)(p)+2)<<16|  \
310                     (guint32)*((guint8 *)(p)+1)<<8|   \
311                     (guint32)*((guint8 *)(p)+0)<<0)
312 #endif
313
314
315 #ifdef G_HAVE_GINT64
316 #ifndef pletohll
317 #define pletohll(p) ((guint64)*((guint8 *)(p)+7)<<56|  \
318                      (guint64)*((guint8 *)(p)+6)<<48|  \
319                      (guint64)*((guint8 *)(p)+5)<<40|  \
320                      (guint64)*((guint8 *)(p)+4)<<32|  \
321                      (guint64)*((guint8 *)(p)+3)<<24|  \
322                      (guint64)*((guint8 *)(p)+2)<<16|  \
323                      (guint64)*((guint8 *)(p)+1)<<8|   \
324                      (guint64)*((guint8 *)(p)+0)<<0)
325 #endif
326 #endif
327
328 #define wtap_file_read_unknown_bytes(target, num_bytes, fh, err) \
329         G_STMT_START \
330         { \
331                 int _bytes_read; \
332                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
333                 if (_bytes_read != (num_bytes)) { \
334                         *(err) = file_error((fh)); \
335                         return FALSE; \
336                 } \
337         } \
338         G_STMT_END
339
340 #define wtap_file_read_expected_bytes(target, num_bytes, fh, err) \
341         G_STMT_START \
342         { \
343                 int _bytes_read; \
344                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
345                 if (_bytes_read != (num_bytes)) { \
346                         *(err) = file_error((fh)); \
347                         if (*(err) == 0 && _bytes_read > 0) { \
348                                 *(err) = WTAP_ERR_SHORT_READ; \
349                         } \
350                         return FALSE; \
351                 } \
352         } \
353         G_STMT_END
354
355 /* glib doesn't have g_ptr_array_len of all things!*/
356 #ifndef g_ptr_array_len
357 #define g_ptr_array_len(a)      ((a)->len)
358 #endif
359
360 #endif /* __WTAP_INT_H__ */