change all file offsets from long to gint64 so we can - theoretically - handle files...
[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_LIBZ
35 #ifdef HAVE_WINSOCK2_H
36 #include <winsock2.h>
37 #endif
38 #include <zlib.h>
39 #define FILE_T  gzFile
40 #else /* No zLib */
41 #define FILE_T  FILE *
42 #endif /* HAVE_LIBZ */
43
44 #include "wtap.h"
45
46 /* Information for a compressed Sniffer data stream. */
47 typedef struct {
48         unsigned char *buf;     /* buffer into which we uncompress data */
49         size_t  nbytes;         /* number of bytes of data in that buffer */
50         int     nextout;        /* offset in that buffer of stream's current position */
51         gint64  comp_offset;    /* current offset in compressed data stream */
52         gint64  uncomp_offset;  /* current offset in uncompressed data stream */
53 } ngsniffer_comp_stream_t;
54
55 typedef struct {
56   guint8 *sdate;          /* Packet start date        */        
57   gboolean tcp_formatted; /* TCP/IP data formated Y/N */
58   int format;             /* Trace format type        */
59 } iseries_t;
60
61 typedef struct {
62         guint   maj_vers;
63         guint   min_vers;
64         double  timeunit;
65         time_t  start;
66         guint   network;                /* network type */
67         ngsniffer_comp_stream_t seq;    /* sequential access */
68         ngsniffer_comp_stream_t rand;   /* random access */
69         GList   *first_blob;            /* list element for first blob */
70         GList   *last_blob;             /* list element for last blob */
71         GList   *current_blob;          /* list element for current blob */
72 } ngsniffer_t;
73
74 typedef struct {
75         gboolean byte_swapped;
76 } i4btrace_t;
77
78 typedef struct {
79         gboolean is_hpux_11;
80 } nettl_t;
81
82 typedef struct {
83         time_t  start;
84 } lanalyzer_t;
85
86 typedef enum {
87         NOT_SWAPPED,
88         SWAPPED,
89         MAYBE_SWAPPED
90 } swapped_type_t;
91
92 typedef struct {
93         gboolean byte_swapped;
94         swapped_type_t lengths_swapped;
95         guint16 version_major;
96         guint16 version_minor;
97 } libpcap_t;
98
99 typedef struct {
100         time_t  start_secs;
101         guint32 start_usecs;
102         guint8  version_major;
103         guint32 *frame_table;
104         guint32 frame_table_size;
105         guint   current_frame;
106 } netmon_t;
107
108 typedef struct {
109         time_t          start_time;
110         double          timeunit;
111         double          start_timestamp;
112         gboolean        wrapped;
113         guint32         nframes;
114         gint64          start_offset;
115         gint64          end_offset;
116         int             version_major;
117         gboolean        fcs_valid;      /* if packets have valid FCS at the end */
118         guint           isdn_type;      /* 1 = E1 PRI, 2 = T1 PRI, 3 = BRI */
119 } netxray_t;
120
121 typedef struct {
122         time_t inittime;
123         int adjusted;
124         gint64 next_packet_seek_start;
125 } ascend_t;
126
127 typedef struct {
128         gboolean byteswapped;
129 } csids_t;
130
131 typedef struct {
132         struct timeval reference_time;
133 } etherpeek_t;
134
135 typedef struct {
136         gboolean        has_fcs;
137 } airopeek9_t;
138
139 typedef struct {
140         guint32         atm_encap;
141         gboolean        is_rawatm;
142         gboolean        is_ppp;
143 } erf_t;
144
145 typedef struct _k12_t k12_t;
146
147 typedef struct {
148         time_t  start_secs;
149         guint32 start_usecs;
150 } catapult_dct2000_t;
151
152 typedef gboolean (*subtype_read_func)(struct wtap*, int*, char**, gint64*);
153 typedef gboolean (*subtype_seek_read_func)(struct wtap*, gint64, union wtap_pseudo_header*,
154                                         guint8*, int, int *, char **);
155 struct wtap {
156         FILE_T                  fh;
157         int                     fd;           /* File descriptor for cap file */
158         FILE_T                  random_fh;    /* Secondary FILE_T for random access */
159         int                     file_type;
160         int                     snapshot_length;
161         struct Buffer           *frame_buffer;
162         struct wtap_pkthdr      phdr;
163         union wtap_pseudo_header pseudo_header;
164
165         gint64                  data_offset;
166
167         union {
168                 libpcap_t               *pcap;
169                 lanalyzer_t             *lanalyzer;
170                 ngsniffer_t             *ngsniffer;
171                 iseries_t               *iseries;
172                 i4btrace_t              *i4btrace;
173                 nettl_t                 *nettl;
174                 netmon_t                *netmon;
175                 netxray_t               *netxray;
176                 ascend_t                *ascend;
177                 csids_t                 *csids;
178                 etherpeek_t             *etherpeek;
179                 airopeek9_t             *airopeek9;
180                 erf_t                   *erf;
181                 k12_t                   *k12;
182                 catapult_dct2000_t*     catapult_dct2000;
183                 void                    *generic;
184         } capture;
185
186         subtype_read_func       subtype_read;
187         subtype_seek_read_func  subtype_seek_read;
188         void                    (*subtype_sequential_close)(struct wtap*);
189         void                    (*subtype_close)(struct wtap*);
190         int                     file_encap;     /* per-file, for those
191                                                    file formats that have
192                                                    per-file encapsulation
193                                                    types */
194         int                     tsprecision;    /* timestamp precision of the lower 32bits
195                                                          * e.g. WTAP_FILE_TSPREC_USEC */
196 };
197
198 struct wtap_dumper;
199
200 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
201                 const struct wtap_pkthdr*, const union wtap_pseudo_header*,
202                 const guchar*, int*);
203 typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
204
205 typedef struct {
206         gboolean first_frame;
207         time_t start;
208 } ngsniffer_dump_t;
209
210 typedef struct {
211         gboolean first_frame;
212         struct wtap_nstime start;
213         guint32 nframes;
214 } netxray_dump_t;
215
216 typedef struct {
217         gboolean got_first_record_time;
218         struct wtap_nstime first_record_time;
219         guint32 frame_table_offset;
220         guint32 *frame_table;
221         guint   frame_table_index;
222         guint   frame_table_size;
223 } netmon_dump_t;
224
225 typedef struct {
226         guint32 nframes;
227 } _5views_dump_t;
228
229 typedef struct {
230         guint64 packet_count;
231         guint8  network_type;
232 } niobserver_dump_t;
233
234 typedef struct {
235         guint32 file_len;
236         guint32 num_of_records;
237         guint32 file_offset;
238 } k12_dump_t;
239
240 typedef struct {
241     gboolean           first_packet_written;
242     struct wtap_nstime start_time;
243 } dct2000_dump_t;
244
245 struct wtap_dumper {
246         FILE*                   fh;
247         int                     file_type;
248         int                     snaplen;
249         int                     encap;
250         gboolean        compressed;
251         gint64          bytes_dumped;
252
253         union {
254                 void                    *opaque;
255                 ngsniffer_dump_t        *ngsniffer;
256                 netmon_dump_t           *netmon;
257                 netxray_dump_t          *netxray;
258                 _5views_dump_t          *_5views;
259                 niobserver_dump_t       *niobserver;
260                 k12_dump_t              *k12;
261                 dct2000_dump_t          *dct2000;
262         } dump;
263
264         subtype_write_func      subtype_write;
265         subtype_close_func      subtype_close;
266
267         int                     tsprecision;    /* timestamp precision of the lower 32bits
268                                                          * e.g. WTAP_FILE_TSPREC_USEC */
269 };
270
271 extern size_t wtap_dump_file_write(wtap_dumper *wdh, const void *buf, unsigned bufsize);
272 extern int wtap_dump_file_ferror(wtap_dumper *wdh);
273
274 /* Macros to byte-swap 32-bit and 16-bit quantities. */
275 #define BSWAP32(x) \
276         ((((x)&0xFF000000)>>24) | \
277          (((x)&0x00FF0000)>>8) | \
278          (((x)&0x0000FF00)<<8) | \
279          (((x)&0x000000FF)<<24))
280 #define BSWAP16(x) \
281          ((((x)&0xFF00)>>8) | \
282           (((x)&0x00FF)<<8))
283
284 /* Turn host-byte-order values into little-endian values. */
285 #define htoles(s) GUINT16_TO_LE(s)
286 #define htolel(l) GUINT32_TO_LE(l)
287 #define htolell(ll) GUINT64_TO_LE(ll)
288
289 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
290  * byte array, returns the value of the two or four bytes at the pointer.
291  * The pletoh[sl] versions return the little-endian representation.
292  * We also provide pntohll and phtolell, which extract 64-bit integral
293  * quantities.
294  */
295
296 #ifndef pntohs
297 #define pntohs(p)  ((guint16)                       \
298                     ((guint16)*((const guint8 *)(p)+0)<<8|  \
299                      (guint16)*((const guint8 *)(p)+1)<<0))
300 #endif
301
302 #ifndef pntoh24
303 #define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16| \
304                      (guint32)*((const guint8 *)(p)+1)<<8|  \
305                      (guint32)*((const guint8 *)(p)+2)<<0)
306 #endif
307
308 #ifndef pntohl
309 #define pntohl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
310                     (guint32)*((const guint8 *)(p)+1)<<16|  \
311                     (guint32)*((const guint8 *)(p)+2)<<8|   \
312                     (guint32)*((const guint8 *)(p)+3)<<0)
313 #endif
314
315 #ifndef pntohll
316 #define pntohll(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
317                      (guint64)*((const guint8 *)(p)+1)<<48|  \
318                      (guint64)*((const guint8 *)(p)+2)<<40|  \
319                      (guint64)*((const guint8 *)(p)+3)<<32|  \
320                      (guint64)*((const guint8 *)(p)+4)<<24|  \
321                      (guint64)*((const guint8 *)(p)+5)<<16|  \
322                      (guint64)*((const guint8 *)(p)+6)<<8|   \
323                      (guint64)*((const guint8 *)(p)+7)<<0)
324 #endif
325
326
327 #ifndef phtons
328 #define phtons(p)  ((guint16)                       \
329                     ((guint16)*((const guint8 *)(p)+0)<<8|  \
330                      (guint16)*((const guint8 *)(p)+1)<<0))
331 #endif
332
333 #ifndef phtonl
334 #define phtonl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
335                     (guint32)*((const guint8 *)(p)+1)<<16|  \
336                     (guint32)*((const guint8 *)(p)+2)<<8|   \
337                     (guint32)*((const guint8 *)(p)+3)<<0)
338 #endif
339
340 #ifndef pletohs
341 #define pletohs(p) ((guint16)                       \
342                     ((guint16)*((const guint8 *)(p)+1)<<8|  \
343                      (guint16)*((const guint8 *)(p)+0)<<0))
344 #endif
345
346 #ifndef pletoh24
347 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
348                      (guint32)*((const guint8 *)(p)+1)<<8|  \
349                      (guint32)*((const guint8 *)(p)+0)<<0)
350 #endif
351
352
353 #ifndef pletohl
354 #define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24|  \
355                     (guint32)*((const guint8 *)(p)+2)<<16|  \
356                     (guint32)*((const guint8 *)(p)+1)<<8|   \
357                     (guint32)*((const guint8 *)(p)+0)<<0)
358 #endif
359
360
361 #ifndef pletohll
362 #define pletohll(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
363                      (guint64)*((const guint8 *)(p)+6)<<48|  \
364                      (guint64)*((const guint8 *)(p)+5)<<40|  \
365                      (guint64)*((const guint8 *)(p)+4)<<32|  \
366                      (guint64)*((const guint8 *)(p)+3)<<24|  \
367                      (guint64)*((const guint8 *)(p)+2)<<16|  \
368                      (guint64)*((const guint8 *)(p)+1)<<8|   \
369                      (guint64)*((const guint8 *)(p)+0)<<0)
370 #endif
371
372 #define wtap_file_read_unknown_bytes(target, num_bytes, fh, err) \
373         G_STMT_START \
374         { \
375                 int _bytes_read; \
376                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
377                 if (_bytes_read != (int) (num_bytes)) { \
378                         *(err) = file_error((fh)); \
379                         return FALSE; \
380                 } \
381         } \
382         G_STMT_END
383
384 #define wtap_file_read_expected_bytes(target, num_bytes, fh, err) \
385         G_STMT_START \
386         { \
387                 int _bytes_read; \
388                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
389                 if (_bytes_read != (int) (num_bytes)) { \
390                         *(err) = file_error((fh)); \
391                         if (*(err) == 0 && _bytes_read > 0) { \
392                                 *(err) = WTAP_ERR_SHORT_READ; \
393                         } \
394                         return FALSE; \
395                 } \
396         } \
397         G_STMT_END
398
399 /* glib doesn't have g_ptr_array_len of all things!*/
400 #ifndef g_ptr_array_len
401 #define g_ptr_array_len(a)      ((a)->len)
402 #endif
403
404 #endif /* __WTAP_INT_H__ */