source: lib/libtrace_int.h @ d5a27e8

4.0.1-hotfixescachetimestampsdevelopdpdk-ndagetsilivegetfragoffhelplibtrace4ndag_formatpfringrc-4.0.1rc-4.0.2rc-4.0.3rc-4.0.4ringdecrementfixringperformanceringtimestampfixes
Last change on this file since d5a27e8 was d5a27e8, checked in by Perry Lorier <perry@…>, 14 years ago

Update the copyright information
Fix libpcap issue
Remove all swig/python builds

  • Property mode set to 100644
File size: 14.9 KB
Line 
1/*
2 * This file is part of libtrace
3 *
4 * Copyright (c) 2007 The University of Waikato, Hamilton, New Zealand.
5 * Authors: Daniel Lawson
6 *          Perry Lorier
7 *         
8 * All rights reserved.
9 *
10 * This code has been developed by the University of Waikato WAND
11 * research group. For further information please see http://www.wand.net.nz/
12 *
13 * libtrace is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * libtrace is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with libtrace; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
26 *
27 * $Id$
28 *
29 */
30/** @file */
31
32#ifndef LIBTRACE_INT_H
33#define LIBTRACE_INT_H
34
35#ifdef __cplusplus
36extern "C" {
37#endif
38
39#include "common.h"
40#include "config.h"
41#include "libtrace.h"
42
43#ifdef _MSC_VER
44// warning: deprecated function
45#pragma warning(disable:4996)
46// warning: benign redefinitions of types
47#pragma warning(disable:4142)
48#endif
49
50#ifdef HAVE_INTTYPES_H
51# include <inttypes.h>
52#else
53# include "lt_inttypes.h"
54#endif
55
56#ifdef HAVE_STDDEF_H
57# include <stddef.h>
58#else
59#ifndef WIN32
60# error "Can't find stddev.h -- do you define ptrdiff_t elsewhere?"
61#endif
62#endif
63
64
65#include "rt_protocol.h"
66       
67#ifdef HAVE_PCAP_BPF_H
68#  include <pcap-bpf.h>
69#  define HAVE_BPF 1
70#else
71#  ifdef HAVE_NET_BPF_H
72#    include <net/bpf.h>
73#    define HAVE_BPF 1
74#  endif
75#endif
76
77#ifdef HAVE_PCAP_H
78#  include <pcap.h>
79#  ifdef HAVE_PCAP_INT_H
80#    include <pcap-int.h>
81#  endif
82#endif
83
84#ifdef HAVE_ZLIB_H
85#  include <zlib.h>
86#endif
87
88#ifndef HAVE_STRNCASECMP
89# ifndef HAVE__STRNICMP
90int strncasecmp(const char *str1, const char *str2, size_t n);
91# else
92# define strncasecmp _strnicmp
93# endif
94#endif
95
96#ifndef HAVE_SNPRINTF
97# ifndef HAVE_SPRINTF_S
98int snprintf(char *str, size_t size, const char *format, ...);
99# else
100# define snprintf sprintf_s
101# endif
102#endif
103
104#include "wag.h"
105#include "daglegacy.h"
106       
107#ifdef HAVE_DAG_API
108#  include "dagnew.h"
109#  include "dagapi.h"
110#       ifndef DAG_VERSION_2_4
111#               include <daginf.h>
112#       endif
113#else
114#  include "dagformat.h"
115#endif
116
117#define RP_BUFSIZE 65536U
118
119struct libtrace_event_status_t {
120        libtrace_packet_t *packet;
121        int psize;
122        double tdelta;
123        double trace_last_ts;
124};
125
126/** The information about traces that are open
127 * @internal
128 */
129struct libtrace_t {
130        struct libtrace_format_t *format; /**< format driver pointer */
131        void *format_data; /**<format data pointer */
132        bool started;                   /**< if this trace has started */
133        libtrace_err_t err;             /**< error information */
134        struct libtrace_event_status_t event;   /**< the next event */
135        char *uridata;                  /**< the uri of this trace */
136        struct libtrace_filter_t *filter; /**< used by libtrace if the module
137                                            * doesn't support filters natively
138                                            */
139        size_t snaplen;         /**< used by libtrace if the module
140                                          * doesn't support snapping natively
141                                          */
142};
143
144/** Information about output traces
145 * @internal
146 */
147struct libtrace_out_t {
148        struct libtrace_format_t *format;       /**< format driver */
149        void *format_data;              /**< format data */
150        bool started;                   /**< trace started */
151        libtrace_err_t err;             /**< Associated error */
152        char *uridata;                  /**< URI associated with this trace */
153};
154
155void trace_set_err(libtrace_t *trace, int errcode,const char *msg,...) 
156                                                                PRINTF(3,4);
157void trace_set_err_out(libtrace_out_t *trace, int errcode, const char *msg,...)
158                                                                PRINTF(3,4);
159
160typedef struct libtrace_sll_header_t {
161        uint16_t pkttype;               /* packet type */
162        uint16_t hatype;                /* link-layer address type */
163        uint16_t halen;                 /* link-layer address length */
164        char addr[8];                   /* link-layer address */
165        uint16_t protocol;              /* protocol */
166} libtrace_sll_header_t;
167
168#define TRACE_SLL_HOST          0
169#define TRACE_SLL_BROADCAST     1
170#define TRACE_SLL_MULTICAST     2
171#define TRACE_SLL_OTHERHOST     3
172#define TRACE_SLL_OUTGOING      4
173
174#ifndef PF_RULESET_NAME_SIZE
175#define PF_RULESET_NAME_SIZE 16
176#endif
177
178#ifndef IFNAMSIZ
179#define IFNAMSIZ 16
180#endif
181
182typedef struct libtrace_pflog_header_t {
183        uint8_t    length;
184        sa_family_t   af;
185        uint8_t    action;
186        uint8_t    reason;
187        char       ifname[IFNAMSIZ];
188        char       ruleset[PF_RULESET_NAME_SIZE];
189        uint32_t   rulenr;
190        uint32_t   subrulenr;
191        uint8_t    dir;
192        uint8_t    pad[3];
193} libtrace_pflog_header_t;
194
195
196
197/** Module definition structure */
198/* all of these should return -1, or NULL on failure */
199struct libtrace_format_t {
200        /** the uri name of this module */
201        const char *name;
202        /** the version of this module */
203        const char *version;
204        /** the RT protocol type of this module */
205        enum base_format_t type;
206        /** stuff that deals with input @{ */
207        /** initialise an trace (or NULL if input is not supported) */
208        int (*init_input)(libtrace_t *libtrace);
209        /** configure an trace (or NULL if input is not supported) */
210        int (*config_input)(libtrace_t *libtrace,trace_option_t option,void *value);
211        /** start/unpause an trace (or NULL if input not supported) */
212        int (*start_input)(libtrace_t *libtrace);
213        /** pause an trace (or NULL if input not supported) */
214        int (*pause_input)(libtrace_t *libtrace);
215        /** @} */
216        /** stuff that deals with output @{ */
217        /** initialise output traces (or NULL if output not supported) */
218        int (*init_output)(libtrace_out_t *libtrace);
219        /** configure output traces (or NULL if output not supported) */
220        int (*config_output)(libtrace_out_t *libtrace, trace_option_output_t option, void *);
221        /** start output traces (or NULL if output not supported)
222         * There is no pause for output traces, as packets are not arriving
223         * asyncronously
224         */
225        int (*start_output)(libtrace_out_t *libtrace);
226        /** @} */
227        /** finish an input trace, cleanup (or NULL if input not supported)
228         * if the trace is not paused, libtrace will pause the trace before
229         * calling this function.
230         */
231        int (*fin_input)(libtrace_t *libtrace);
232        /** finish an output trace, cleanup (or NULL if output not supported) */
233        int (*fin_output)(libtrace_out_t *libtrace);
234        /** read a packet from a trace into the provided packet structure
235         * @returns -1 on error, or get_framing_length()+get_capture_length() \
236         * on success.
237         * if this function is not supported, this field may be NULL.
238         */
239        int (*read_packet)(libtrace_t *libtrace, libtrace_packet_t *packet);
240        /** finalise a packet
241         * cleanup any resources used by a packet that can't be reused for
242         * the next packet.
243         */
244        void (*fin_packet)(libtrace_packet_t *packet);
245        /** write a packet to a trace from the provided packet
246         * (or NULL if output not supported)
247         */
248        int (*write_packet)(libtrace_out_t *libtrace, libtrace_packet_t *packet);
249        /** return the libtrace link type for this packet
250         * @return the libtrace link type, or -1 if this link type is unknown
251         */ 
252        libtrace_linktype_t (*get_link_type)(const libtrace_packet_t *packet);
253        /** return the direction of this packet
254         * @note This callback may be NULL if not supported.
255         */ 
256        libtrace_direction_t (*get_direction)(const libtrace_packet_t *packet);
257        /** set the direction of this packet
258         * @note This callback may be NULL if not supported.
259         */ 
260        libtrace_direction_t (*set_direction)(libtrace_packet_t *packet, libtrace_direction_t direction);
261        /** return the erf timestamp of the packet.
262         * @return the 64bit erf timestamp
263         * This field may be NULL in the structure, and libtrace will
264         * synthesise the result from get_timeval or get_seconds if they
265         * exist.  AT least one of get_erf_timestamp, get_timeval or
266         * get_seconds must be implemented.
267         */
268        uint64_t (*get_erf_timestamp)(const libtrace_packet_t *packet);
269        /** return the timeval of this packet.
270         * @return the timeval
271         * This field may be NULL in the structure, and libtrace will
272         * synthesise the result from get_erf_timestamp or get_seconds if they
273         * exist.  AT least one of get_erf_timestamp, get_timeval or
274         * get_seconds must be implemented.
275         */
276        struct timeval (*get_timeval)(const libtrace_packet_t *packet);
277        /** return the timestamp of this packet.
278         * @return the floating point seconds since 1970-01-01 00:00:00
279         * This field may be NULL in the structure, and libtrace will
280         * synthesise the result from get_timeval or get_erf_timestamp if they
281         * exist.  AT least one of get_erf_timestamp, get_timeval or
282         * get_seconds must be implemented.
283         */
284        double (*get_seconds)(const libtrace_packet_t *packet);
285        /** move the pointer within the trace.
286         * @return 0 on success, -1 on failure.
287         * The next packet returned by read_packet be the first
288         * packet in the trace to have a timestamp equal or greater than
289         * timestamp.
290         * @note this function may be NULL if the format does not support
291         * this feature.  If the format implements seek_timeval and/or
292         * seek_seconds then libtrace will call those functions instead.
293         */
294        int (*seek_erf)(libtrace_t *trace, uint64_t timestamp);
295        /** move the pointer within the trace.
296         * @return 0 on success, -1 on failure.
297         * The next packet returned by read_packet be the first
298         * packet in the trace to have a timestamp equal or greater than
299         * timestamp.
300         * @note this function may be NULL if the format does not support
301         * this feature.  If the format implements seek_erf and/or
302         * seek_seconds then libtrace will call those functions instead.
303         */
304        int (*seek_timeval)(libtrace_t *trace, struct timeval tv);
305        /** move the pointer within the trace.
306         * @return 0 on success, -1 on failure.
307         * The next packet returned by read_packet be the first
308         * packet in the trace to have a timestamp equal or greater than
309         * tv.
310         * @note this function may be NULL if the format does not support
311         * this feature.  If the format implements seek_erf and/or
312         * seek_timeval then libtrace will call those functions instead.
313         */
314        int (*seek_seconds)(libtrace_t *trace, double seconds);
315        /** return the captured payload length
316         * @return the amount of data captured in a trace.
317         * This is the number of bytes actually in the trace.  This does not
318         * include the trace framing length.  This is usually shorter or
319         * equal to the wire length.
320         */
321        int (*get_capture_length)(const libtrace_packet_t *packet);
322        /** return the original length of the packet on the wire.
323         * @return the length of the packet on the wire before truncation.
324         * This is the number of bytes actually in the trace.  This does not
325         * include the trace framing length.  This is usually shorter or
326         * equal to the wire length.
327         */
328        int (*get_wire_length)(const libtrace_packet_t *packet);
329        /** return the length of the trace framing header
330         * @return the length of the framing header
331         * The framing header is the extra metadata a trace stores about
332         * a packet.  This does not include the wire or capture length
333         * of the packet.  Usually get_framing_length()+get_capture_length()
334         * is the size returned by read_packet
335         */
336        int (*get_framing_length)(const libtrace_packet_t *packet);
337        /** truncate (snap) the packet
338         * @returns the new size
339         * @note This callback may be NULL if not supported.
340         */
341        size_t (*set_capture_length)(struct libtrace_packet_t *packet,size_t size);
342        /** return the filedescriptor associated with this interface.
343         * @note This callback may be NULL if not supported.
344         * This function is only needed if you use trace_event_interface
345         * as the pointer for trace_event
346         */
347        int (*get_fd)(const libtrace_t *trace);
348        /** return the next event from this source
349         * @note may be NULL if not supported.
350         */
351        struct libtrace_eventobj_t (*trace_event)(libtrace_t *trace, libtrace_packet_t *packet);       
352        /** return information about this trace format to standard out */
353        void (*help)(void);
354        /** next pointer, should be NULL */
355        struct libtrace_format_t *next;
356};
357
358extern struct libtrace_format_t *form;
359
360void register_format(struct libtrace_format_t *format);
361
362libtrace_linktype_t pcap_dlt_to_libtrace(libtrace_dlt_t dlt);
363libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type);
364libtrace_rt_types_t pcap_dlt_to_rt(libtrace_dlt_t dlt);
365libtrace_dlt_t rt_to_pcap_dlt(libtrace_rt_types_t rt_type);
366libtrace_linktype_t erf_type_to_libtrace(uint8_t erf);
367uint8_t libtrace_to_erf_type(libtrace_linktype_t linktype);
368libtrace_linktype_t arphrd_type_to_libtrace(unsigned int);
369unsigned int libtrace_to_arphrd_type(libtrace_linktype_t);
370
371void promote_packet(libtrace_packet_t *packet);
372bool demote_packet(libtrace_packet_t *packet);
373
374void *trace_get_payload_from_linux_sll(void *, uint16_t *, uint32_t *);
375void *trace_get_payload_from_pos(void *, uint16_t *, uint32_t *);
376DLLEXPORT void *trace_get_payload_from_atm(void *, uint8_t *, uint32_t *);
377
378uint64_t byteswap64(uint64_t num);
379uint32_t byteswap32(uint32_t num);
380uint16_t byteswap16(uint16_t num);
381
382/* Because some traces/protocols are defined as
383 * being "big endian" or "little endian" we have
384 * this series of macros.
385 */
386#if BYTE_ORDER == BIG_ENDIAN
387#define bswap_host_to_be64(num) ((uint64_t)(num))
388#define bswap_host_to_le64(num) byteswap64(num)
389#define bswap_host_to_be32(num) ((uint32_t)(num))
390#define bswap_host_to_le32(num) byteswap32(num)
391#define bswap_host_to_be16(num) ((uint16_t)(num))
392#define bswap_host_to_le16(num) byteswap16(num)
393
394#define bswap_be_to_host64(num) ((uint64_t)(num))
395#define bswap_le_to_host64(num) byteswap64(num)
396#define bswap_be_to_host32(num) ((uint32_t)(num))
397#define bswap_le_to_host32(num) byteswap32(num)
398#define bswap_be_to_host16(num) ((uint16_t)(num))
399#define bswap_le_to_host16(num) byteswap16(num)
400
401/* We use ntoh*() here, because the compiler may
402 * attempt to optimise it
403 */
404#elif BYTE_ORDER == LITTLE_ENDIAN
405#define bswap_host_to_be64(num) (byteswap64(num))
406#define bswap_host_to_le64(num) ((uint64_t)(num))
407#define bswap_host_to_be32(num) (htonl(num))
408#define bswap_host_to_le32(num) ((uint32_t)(num))
409#define bswap_host_to_be16(num) (htons(num))
410#define bswap_host_to_le16(num) ((uint16_t)(num))
411
412#define bswap_be_to_host64(num) (byteswap64(num))
413#define bswap_le_to_host64(num) ((uint64_t)(num))
414#define bswap_be_to_host32(num) (ntohl(num))
415#define bswap_le_to_host32(num) ((uint32_t)(num))
416#define bswap_be_to_host16(num) (ntohs(num))
417#define bswap_le_to_host16(num) ((uint16_t)(num))
418
419#else
420#error "Unknown byte order"
421#endif
422
423#ifdef HAVE_BPF
424/* A type encapsulating a bpf filter
425 * This type covers the compiled bpf filter, as well as the original filter
426 * string
427 *
428 */
429struct libtrace_filter_t {
430        struct bpf_program filter;
431        int flag;
432        char * filterstring;
433};
434#else
435struct libtrace_filter_t {};
436#endif
437
438/** libtrace packet
439 */
440typedef struct libtrace_pcapfile_pkt_hdr_t {
441        uint32_t ts_sec;
442        uint32_t ts_usec;
443        uint32_t caplen;
444        uint32_t wirelen;
445} libtrace_pcapfile_pkt_hdr_t;
446
447#ifdef HAVE_DAG
448void dag_constructor(void);
449#endif
450void erf_constructor(void);
451void legacy_constructor(void);
452void linuxnative_constructor(void);
453void pcap_constructor(void);
454void pcapfile_constructor(void);
455void rt_constructor(void);
456void wag_constructor(void);
457void duck_constructor(void);
458
459
460       
461#ifdef __cplusplus
462}
463#endif
464
465#endif /* LIBTRACE_INT_H */
Note: See TracBrowser for help on using the repository browser.