00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00050 #ifndef LIBTRACE_INT_H
00051 #define LIBTRACE_INT_H
00052
00053 #ifdef __cplusplus
00054 extern "C" {
00055 #endif
00056
00057 #include "config.h"
00058 #include "common.h"
00059 #include "libtrace.h"
00060 #include "wandio.h"
00061
00062 #ifdef _MSC_VER
00063
00064 #pragma warning(disable:4996)
00065
00066 #pragma warning(disable:4142)
00067 #endif
00068
00069 #ifdef HAVE_INTTYPES_H
00070 # include <inttypes.h>
00071 #else
00072 # include "lt_inttypes.h"
00073 #endif
00074
00075 #ifdef HAVE_STDDEF_H
00076 # include <stddef.h>
00077 #else
00078 #ifndef WIN32
00079 # error "Can't find stddev.h -- do you define ptrdiff_t elsewhere?"
00080 #endif
00081 #endif
00082
00083
00084 #include "rt_protocol.h"
00085
00086
00087 #ifdef HAVE_NET_BPF_H
00088 # include <net/bpf.h>
00089 # define HAVE_BPF 1
00090 #else
00091 #ifdef HAVE_PCAP_BPF_H
00092 # include <pcap-bpf.h>
00093 # define HAVE_BPF 1
00094 #endif
00095 #endif
00096
00097 #ifdef HAVE_PCAP_H
00098 # include <pcap.h>
00099 # ifdef HAVE_PCAP_INT_H
00100 # include <pcap-int.h>
00101 # endif
00102 #endif
00103
00104 #ifdef HAVE_ZLIB_H
00105 # include <zlib.h>
00106 #endif
00107
00108 #ifndef HAVE_STRNDUP
00109 char *strndup(const char *s, size_t size);
00110 #endif
00111
00112 #ifndef HAVE_STRNCASECMP
00113 # ifndef HAVE__STRNICMP
00114
00115 int strncasecmp(const char *str1, const char *str2, size_t n);
00116 # else
00117 # define strncasecmp _strnicmp
00118 # endif
00119 #endif
00120
00121 #ifndef HAVE_SNPRINTF
00122 # ifndef HAVE_SPRINTF_S
00123
00124 int snprintf(char *str, size_t size, const char *format, ...);
00125 # else
00126 # define snprintf sprintf_s
00127 # endif
00128 #endif
00129
00130 #include "daglegacy.h"
00131
00132 #ifdef HAVE_DAG_API
00133 # include "dagnew.h"
00134 # include "dagapi.h"
00135 # if DAG_VERSION == 24
00136 # include <erftypes.h>
00137 # else
00138 # include <daginf.h>
00139 # endif
00140 # include "erftypes.h"
00141 #else
00142 # include "dagformat.h"
00143 #endif
00144
00145 #ifdef HAVE_LLVM
00146 #include "bpf-jit/bpf-jit.h"
00147 #endif
00148
00149
00150
00152 struct libtrace_event_status_t {
00154 libtrace_packet_t *packet;
00157 double tdelta;
00159 double trace_last_ts;
00161 int psize;
00164 bool waiting;
00165 };
00166
00170 struct libtrace_t {
00172 struct libtrace_format_t *format;
00174 struct libtrace_event_status_t event;
00176 void *format_data;
00179 struct libtrace_filter_t *filter;
00182 size_t snaplen;
00184 uint64_t accepted_packets;
00186 uint64_t filtered_packets;
00188 char *uridata;
00190 io_t *io;
00192 libtrace_err_t err;
00194 bool started;
00195 };
00196
00200 struct libtrace_out_t {
00202 struct libtrace_format_t *format;
00204 void *format_data;
00206 char *uridata;
00208 libtrace_err_t err;
00210 bool started;
00211 };
00212
00219 void trace_set_err(libtrace_t *trace, int errcode,const char *msg,...)
00220
00221 PRINTF(3,4);
00228 void trace_set_err_out(libtrace_out_t *trace, int errcode, const char *msg,...)
00229 PRINTF(3,4);
00230
00235 void trace_clear_cache(libtrace_packet_t *packet);
00236
00265 int trace_prepare_packet(libtrace_t *trace, libtrace_packet_t *packet,
00266 void *buffer, libtrace_rt_types_t rt_type, uint32_t flags);
00267
00269 enum {
00272 TRACE_PREP_OWN_BUFFER =1,
00273
00276 TRACE_PREP_DO_NOT_OWN_BUFFER =0
00277 };
00278
00280 typedef struct libtrace_sll_header_t {
00281 uint16_t pkttype;
00282 uint16_t hatype;
00283 uint16_t halen;
00284 unsigned char addr[8];
00285 uint16_t protocol;
00286 } libtrace_sll_header_t;
00287
00288
00289
00290
00292 #define TRACE_SLL_HOST 0
00293
00294 #define TRACE_SLL_BROADCAST 1
00295
00296 #define TRACE_SLL_MULTICAST 2
00297
00299 #define TRACE_SLL_OTHERHOST 3
00300
00301 #define TRACE_SLL_OUTGOING 4
00302
00303 #ifndef PF_RULESET_NAME_SIZE
00304 #define PF_RULESET_NAME_SIZE 16
00305 #endif
00306
00307 #ifndef IFNAMSIZ
00308 #define IFNAMSIZ 16
00309 #endif
00310
00311
00313 typedef struct libtrace_pflog_header_t {
00314 uint8_t length;
00315 sa_family_t af;
00316 uint8_t action;
00317 uint8_t reason;
00318 char ifname[IFNAMSIZ];
00319 char ruleset[PF_RULESET_NAME_SIZE];
00320 uint32_t rulenr;
00321 uint32_t subrulenr;
00322 uint8_t dir;
00323 uint8_t pad[3];
00324 } PACKED libtrace_pflog_header_t;
00325
00326
00327
00329
00330 struct libtrace_format_t {
00333 const char *name;
00335 const char *version;
00337 enum base_format_t type;
00338
00339
00347 int (*probe_filename)(const char *fname);
00348
00356 int (*probe_magic)(io_t *io);
00357
00363 int (*init_input)(libtrace_t *libtrace);
00364
00374 int (*config_input)(libtrace_t *libtrace,trace_option_t option,void *value);
00380 int (*start_input)(libtrace_t *libtrace);
00381
00388 int (*pause_input)(libtrace_t *libtrace);
00389
00395 int (*init_output)(libtrace_out_t *libtrace);
00396
00406 int (*config_output)(libtrace_out_t *libtrace, trace_option_output_t option, void *value);
00407
00417 int (*start_output)(libtrace_out_t *libtrace);
00418
00427 int (*fin_input)(libtrace_t *libtrace);
00428
00434 int (*fin_output)(libtrace_out_t *libtrace);
00435
00449 int (*read_packet)(libtrace_t *libtrace, libtrace_packet_t *packet);
00450
00470 int (*prepare_packet)(libtrace_t *libtrace, libtrace_packet_t *packet,
00471 void *buffer, libtrace_rt_types_t rt_type,
00472 uint32_t flags);
00473
00479 void (*fin_packet)(libtrace_packet_t *packet);
00480
00487 int (*write_packet)(libtrace_out_t *libtrace, libtrace_packet_t *packet);
00493 libtrace_linktype_t (*get_link_type)(const libtrace_packet_t *packet);
00494
00501 libtrace_direction_t (*get_direction)(const libtrace_packet_t *packet);
00502
00513 libtrace_direction_t (*set_direction)(libtrace_packet_t *packet, libtrace_direction_t direction);
00514
00529 uint64_t (*get_erf_timestamp)(const libtrace_packet_t *packet);
00530
00544 struct timeval (*get_timeval)(const libtrace_packet_t *packet);
00545
00559 struct timespec (*get_timespec)(const libtrace_packet_t *packet);
00560
00575 double (*get_seconds)(const libtrace_packet_t *packet);
00576
00597 int (*seek_erf)(libtrace_t *trace, uint64_t timestamp);
00618 int (*seek_timeval)(libtrace_t *trace, struct timeval tv);
00619
00641 int (*seek_seconds)(libtrace_t *trace, double seconds);
00642
00653 int (*get_capture_length)(const libtrace_packet_t *packet);
00654
00665 int (*get_wire_length)(const libtrace_packet_t *packet);
00666
00677 int (*get_framing_length)(const libtrace_packet_t *packet);
00678
00690 size_t (*set_capture_length)(struct libtrace_packet_t *packet,size_t size);
00699 uint64_t (*get_received_packets)(libtrace_t *trace);
00700
00708 uint64_t (*get_filtered_packets)(libtrace_t *trace);
00709
00717 uint64_t (*get_dropped_packets)(libtrace_t *trace);
00718
00730 uint64_t (*get_captured_packets)(libtrace_t *trace);
00731
00738 int (*get_fd)(const libtrace_t *trace);
00739
00753 struct libtrace_eventobj_t (*trace_event)(libtrace_t *trace, libtrace_packet_t *packet);
00754
00756 void (*help)(void);
00757
00760 struct libtrace_format_t *next;
00761 };
00762
00764 extern struct libtrace_format_t *form;
00765
00770 void register_format(struct libtrace_format_t *format);
00771
00778 libtrace_linktype_t pcap_linktype_to_libtrace(libtrace_dlt_t linktype);
00779
00785 libtrace_rt_types_t pcap_linktype_to_rt(libtrace_dlt_t linktype);
00786
00793 libtrace_dlt_t libtrace_to_pcap_linktype(libtrace_linktype_t type);
00794
00801 libtrace_dlt_t libtrace_to_pcap_dlt(libtrace_linktype_t type);
00802
00808 libtrace_dlt_t rt_to_pcap_linktype(libtrace_rt_types_t rt_type);
00809
00816 libtrace_linktype_t erf_type_to_libtrace(uint8_t erf);
00817
00824 uint8_t libtrace_to_erf_type(libtrace_linktype_t linktype);
00825
00832 libtrace_linktype_t arphrd_type_to_libtrace(unsigned int arphrd);
00833
00840 unsigned int libtrace_to_arphrd_type(libtrace_linktype_t type);
00841
00851 void promote_packet(libtrace_packet_t *packet);
00852
00862 bool demote_packet(libtrace_packet_t *packet);
00863
00885 void *trace_get_payload_from_linux_sll(const void *link,
00886 uint16_t *arphrd_type,
00887 uint16_t *next_header,
00888 uint32_t *remaining);
00889
00910 DLLEXPORT void *trace_get_payload_from_atm(void *link, uint8_t *type,
00911 uint32_t *remaining);
00912
00919 uint64_t byteswap64(uint64_t num);
00920
00927 uint32_t byteswap32(uint32_t num);
00928
00935 uint16_t byteswap16(uint16_t num);
00936
00942 #if BYTE_ORDER == BIG_ENDIAN
00943 #define bswap_host_to_be64(num) ((uint64_t)(num))
00944 #define bswap_host_to_le64(num) byteswap64(num)
00945 #define bswap_host_to_be32(num) ((uint32_t)(num))
00946 #define bswap_host_to_le32(num) byteswap32(num)
00947 #define bswap_host_to_be16(num) ((uint16_t)(num))
00948 #define bswap_host_to_le16(num) byteswap16(num)
00949
00950 #define bswap_be_to_host64(num) ((uint64_t)(num))
00951 #define bswap_le_to_host64(num) byteswap64(num)
00952 #define bswap_be_to_host32(num) ((uint32_t)(num))
00953 #define bswap_le_to_host32(num) byteswap32(num)
00954 #define bswap_be_to_host16(num) ((uint16_t)(num))
00955 #define bswap_le_to_host16(num) byteswap16(num)
00956
00957
00958
00959
00960 #elif BYTE_ORDER == LITTLE_ENDIAN
00961 #define bswap_host_to_be64(num) (byteswap64(num))
00962 #define bswap_host_to_le64(num) ((uint64_t)(num))
00963 #define bswap_host_to_be32(num) (htonl(num))
00964 #define bswap_host_to_le32(num) ((uint32_t)(num))
00965 #define bswap_host_to_be16(num) (htons(num))
00966 #define bswap_host_to_le16(num) ((uint16_t)(num))
00967
00968 #define bswap_be_to_host64(num) (byteswap64(num))
00969 #define bswap_le_to_host64(num) ((uint64_t)(num))
00970 #define bswap_be_to_host32(num) (ntohl(num))
00971 #define bswap_le_to_host32(num) ((uint32_t)(num))
00972 #define bswap_be_to_host16(num) (ntohs(num))
00973 #define bswap_le_to_host16(num) ((uint16_t)(num))
00974
00975 #else
00976 #error "Unknown byte order"
00977 #endif
00978
00980 #ifdef HAVE_BPF
00981
00982
00983
00984
00985
00986
00988 struct libtrace_filter_t {
00989 struct bpf_program filter;
00990 char * filterstring;
00991 int flag;
00992 struct bpf_jit_t *jitfilter;
00993 };
00994 #else
00995
00997 struct libtrace_filter_t {};
00998 #endif
00999
01001 typedef struct libtrace_pcapfile_pkt_hdr_t {
01002 uint32_t ts_sec;
01003 uint32_t ts_usec;
01004 uint32_t caplen;
01005 uint32_t wirelen;
01006 } libtrace_pcapfile_pkt_hdr_t;
01007
01008 #ifdef HAVE_DAG
01009
01010 void dag_constructor(void);
01011 #endif
01012
01013 void erf_constructor(void);
01015 void tsh_constructor(void);
01017 void legacy_constructor(void);
01019 void linuxnative_constructor(void);
01021 void pcap_constructor(void);
01023 void pcapfile_constructor(void);
01025 void rt_constructor(void);
01027 void duck_constructor(void);
01029 void atmhdr_constructor(void);
01030 #ifdef HAVE_BPF
01031
01032 void bpf_constructor(void);
01033 #endif
01034
01046 bool trace_get_wireless_flags(void *link, libtrace_linktype_t linktype, uint8_t *flags);
01047 #define TRACE_RADIOTAP_F_FCS 0x10
01048
01049 #ifdef __cplusplus
01050 }
01051 #endif
01052
01053 #endif