source: lib/libtrace_int.h @ 94af885

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

Add new API for dealing with Big endian or Little Endian numbers on the
wrong endian machine.

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