Change the Wiretap code so that it doesn't dereference
[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 struct {
153         struct wtap_nstime now;
154         double t0;
155 } mpeg_t;
156
157 typedef gboolean (*subtype_read_func)(struct wtap*, int*, char**, gint64*);
158 typedef gboolean (*subtype_seek_read_func)(struct wtap*, gint64, union wtap_pseudo_header*,
159                                         guint8*, int, int *, char **);
160 struct wtap {
161         FILE_T                  fh;
162         int                     fd;           /* File descriptor for cap file */
163         FILE_T                  random_fh;    /* Secondary FILE_T for random access */
164         int                     file_type;
165         int                     snapshot_length;
166         struct Buffer           *frame_buffer;
167         struct wtap_pkthdr      phdr;
168         union wtap_pseudo_header pseudo_header;
169
170         gint64                  data_offset;
171
172         union {
173                 libpcap_t               *pcap;
174                 lanalyzer_t             *lanalyzer;
175                 ngsniffer_t             *ngsniffer;
176                 iseries_t               *iseries;
177                 i4btrace_t              *i4btrace;
178                 nettl_t                 *nettl;
179                 netmon_t                *netmon;
180                 netxray_t               *netxray;
181                 ascend_t                *ascend;
182                 csids_t                 *csids;
183                 etherpeek_t             *etherpeek;
184                 airopeek9_t             *airopeek9;
185                 erf_t                   *erf;
186                 k12_t                   *k12;
187                 catapult_dct2000_t      *catapult_dct2000;
188                 mpeg_t                  *mpeg;
189                 void                    *generic;
190         } capture;
191
192         subtype_read_func       subtype_read;
193         subtype_seek_read_func  subtype_seek_read;
194         void                    (*subtype_sequential_close)(struct wtap*);
195         void                    (*subtype_close)(struct wtap*);
196         int                     file_encap;     /* per-file, for those
197                                                    file formats that have
198                                                    per-file encapsulation
199                                                    types */
200         int                     tsprecision;    /* timestamp precision of the lower 32bits
201                                                  * e.g. WTAP_FILE_TSPREC_USEC */
202 };
203
204 struct wtap_dumper;
205
206 typedef gboolean (*subtype_write_func)(struct wtap_dumper*,
207                 const struct wtap_pkthdr*, const union wtap_pseudo_header*,
208                 const guchar*, int*);
209 typedef gboolean (*subtype_close_func)(struct wtap_dumper*, int*);
210
211 typedef struct {
212         gboolean first_frame;
213         time_t start;
214 } ngsniffer_dump_t;
215
216 typedef struct {
217         gboolean first_frame;
218         struct wtap_nstime start;
219         guint32 nframes;
220 } netxray_dump_t;
221
222 typedef struct {
223         gboolean got_first_record_time;
224         struct wtap_nstime first_record_time;
225         guint32 frame_table_offset;
226         guint32 *frame_table;
227         guint   frame_table_index;
228         guint   frame_table_size;
229 } netmon_dump_t;
230
231 typedef struct {
232         guint32 nframes;
233 } _5views_dump_t;
234
235 typedef struct {
236         guint64 packet_count;
237         guint8  network_type;
238 } niobserver_dump_t;
239
240 typedef struct {
241         guint32 file_len;
242         guint32 num_of_records;
243         guint32 file_offset;
244 } k12_dump_t;
245
246 typedef struct {
247     gboolean           first_packet_written;
248     struct wtap_nstime start_time;
249 } dct2000_dump_t;
250
251 struct wtap_dumper {
252         FILE*                   fh;
253         int                     file_type;
254         int                     snaplen;
255         int                     encap;
256         gboolean        compressed;
257         gint64          bytes_dumped;
258
259         union {
260                 void                    *opaque;
261                 ngsniffer_dump_t        *ngsniffer;
262                 netmon_dump_t           *netmon;
263                 netxray_dump_t          *netxray;
264                 _5views_dump_t          *_5views;
265                 niobserver_dump_t       *niobserver;
266                 k12_dump_t              *k12;
267                 dct2000_dump_t          *dct2000;
268         } dump;
269
270         subtype_write_func      subtype_write;
271         subtype_close_func      subtype_close;
272
273         int                     tsprecision;    /* timestamp precision of the lower 32bits
274                                                          * e.g. WTAP_FILE_TSPREC_USEC */
275 };
276
277 extern size_t wtap_dump_file_write(wtap_dumper *wdh, const void *buf, unsigned bufsize);
278 extern int wtap_dump_file_ferror(wtap_dumper *wdh);
279
280 extern gint wtap_num_file_types;
281
282 /* Macros to byte-swap 32-bit and 16-bit quantities. */
283 #define BSWAP32(x) \
284         ((((x)&0xFF000000)>>24) | \
285          (((x)&0x00FF0000)>>8) | \
286          (((x)&0x0000FF00)<<8) | \
287          (((x)&0x000000FF)<<24))
288 #define BSWAP16(x) \
289          ((((x)&0xFF00)>>8) | \
290           (((x)&0x00FF)<<8))
291
292 /* Macros to byte-swap possibly-unaligned 32-bit and 16-bit quantities;
293  * they take a pointer to the quantity, and byte-swap it in place.
294  */
295 #define PBSWAP32(p) \
296         {                       \
297         guint8 tmp;             \
298         tmp = (p)[3];           \
299         (p)[3] = (p)[0];        \
300         (p)[0] = tmp;           \
301         tmp = (p)[2];           \
302         (p)[2] = (p)[1];        \
303         (p)[1] = tmp;           \
304         }
305 #define PBSWAP16(p) \
306         {                       \
307         guint8 tmp;             \
308         tmp = (p)[1];           \
309         (p)[1] = (p)[0];        \
310         (p)[0] = tmp;           \
311         }
312
313 /* Turn host-byte-order values into little-endian values. */
314 #define htoles(s) GUINT16_TO_LE(s)
315 #define htolel(l) GUINT32_TO_LE(l)
316 #define htolell(ll) GUINT64_TO_LE(ll)
317
318 /* Pointer versions of ntohs and ntohl.  Given a pointer to a member of a
319  * byte array, returns the value of the two or four bytes at the pointer.
320  * The pletoh[sl] versions return the little-endian representation.
321  * We also provide pntohll and pletohll, which extract 64-bit integral
322  * quantities.
323  *
324  * These will work regardless of the byte alignment of the pointer.
325  */
326
327 #ifndef pntohs
328 #define pntohs(p)  ((guint16)                       \
329                     ((guint16)*((const guint8 *)(p)+0)<<8|  \
330                      (guint16)*((const guint8 *)(p)+1)<<0))
331 #endif
332
333 #ifndef pntoh24
334 #define pntoh24(p)  ((guint32)*((const guint8 *)(p)+0)<<16| \
335                      (guint32)*((const guint8 *)(p)+1)<<8|  \
336                      (guint32)*((const guint8 *)(p)+2)<<0)
337 #endif
338
339 #ifndef pntohl
340 #define pntohl(p)  ((guint32)*((const guint8 *)(p)+0)<<24|  \
341                     (guint32)*((const guint8 *)(p)+1)<<16|  \
342                     (guint32)*((const guint8 *)(p)+2)<<8|   \
343                     (guint32)*((const guint8 *)(p)+3)<<0)
344 #endif
345
346 #ifndef pntohll
347 #define pntohll(p)  ((guint64)*((const guint8 *)(p)+0)<<56|  \
348                      (guint64)*((const guint8 *)(p)+1)<<48|  \
349                      (guint64)*((const guint8 *)(p)+2)<<40|  \
350                      (guint64)*((const guint8 *)(p)+3)<<32|  \
351                      (guint64)*((const guint8 *)(p)+4)<<24|  \
352                      (guint64)*((const guint8 *)(p)+5)<<16|  \
353                      (guint64)*((const guint8 *)(p)+6)<<8|   \
354                      (guint64)*((const guint8 *)(p)+7)<<0)
355 #endif
356
357
358 #ifndef pletohs
359 #define pletohs(p) ((guint16)                       \
360                     ((guint16)*((const guint8 *)(p)+1)<<8|  \
361                      (guint16)*((const guint8 *)(p)+0)<<0))
362 #endif
363
364 #ifndef pletoh24
365 #define pletoh24(p) ((guint32)*((const guint8 *)(p)+2)<<16|  \
366                      (guint32)*((const guint8 *)(p)+1)<<8|  \
367                      (guint32)*((const guint8 *)(p)+0)<<0)
368 #endif
369
370
371 #ifndef pletohl
372 #define pletohl(p) ((guint32)*((const guint8 *)(p)+3)<<24|  \
373                     (guint32)*((const guint8 *)(p)+2)<<16|  \
374                     (guint32)*((const guint8 *)(p)+1)<<8|   \
375                     (guint32)*((const guint8 *)(p)+0)<<0)
376 #endif
377
378
379 #ifndef pletohll
380 #define pletohll(p) ((guint64)*((const guint8 *)(p)+7)<<56|  \
381                      (guint64)*((const guint8 *)(p)+6)<<48|  \
382                      (guint64)*((const guint8 *)(p)+5)<<40|  \
383                      (guint64)*((const guint8 *)(p)+4)<<32|  \
384                      (guint64)*((const guint8 *)(p)+3)<<24|  \
385                      (guint64)*((const guint8 *)(p)+2)<<16|  \
386                      (guint64)*((const guint8 *)(p)+1)<<8|   \
387                      (guint64)*((const guint8 *)(p)+0)<<0)
388 #endif
389
390 /* Pointer routines to put items out in a particular byte order.
391  * These will work regardless of the byte alignment of the pointer.
392  */
393
394 #ifndef phtons
395 #define phtons(p, v) \
396         {                       \
397         (p)[0] = (v) >> 8;      \
398         (p)[1] = (v);           \
399         }
400 #endif
401
402 #ifndef phtonl
403 #define phtonl(p, v) \
404         {                               \
405         (p)[0] = ((v) >> 24);   \
406         (p)[1] = ((v) >> 16);   \
407         (p)[2] = ((v) >> 8);    \
408         (p)[3] = (v);           \
409         }
410 #endif
411
412 #define wtap_file_read_unknown_bytes(target, num_bytes, fh, err) \
413         G_STMT_START \
414         { \
415                 int _bytes_read; \
416                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
417                 if (_bytes_read != (int) (num_bytes)) { \
418                         *(err) = file_error((fh)); \
419                         return FALSE; \
420                 } \
421         } \
422         G_STMT_END
423
424 #define wtap_file_read_expected_bytes(target, num_bytes, fh, err) \
425         G_STMT_START \
426         { \
427                 int _bytes_read; \
428                 _bytes_read = file_read((target), 1, (num_bytes), (fh)); \
429                 if (_bytes_read != (int) (num_bytes)) { \
430                         *(err) = file_error((fh)); \
431                         if (*(err) == 0 && _bytes_read > 0) { \
432                                 *(err) = WTAP_ERR_SHORT_READ; \
433                         } \
434                         return FALSE; \
435                 } \
436         } \
437         G_STMT_END
438
439 /* glib doesn't have g_ptr_array_len of all things!*/
440 #ifndef g_ptr_array_len
441 #define g_ptr_array_len(a)      ((a)->len)
442 #endif
443
444 #endif /* __WTAP_INT_H__ */