1#ifndef __DEFINES_H__
2#define __DEFINES_H__
3
4#include "config.h"
5
6#ifdef HAVE_STDDEF_H
7#include <stddef.h>
8#endif
9
10#ifdef HAVE_SYS_TYPES_H
11#include <sys/types.h>
12#endif
13
14#ifdef WIN32
15#include "msvc_inttypes.h"
16#include "msvc_stdint.h"
17#else
18
19#ifdef HAVE_INTTYPES_H
20#include <inttypes.h>
21#endif
22
23#ifdef HAVE_STDINT_H
24#include <stdint.h>
25#endif
26
27#endif /* WIN32 */
28
29#ifdef HAVE_SYS_SOCKET
30#include <sys/socket.h>
31#endif
32
33#ifdef HAVE_NETINET_IN_H
34#include <netinet/in.h>
35#endif
36
37#ifdef HAVE_NETINET_IN_SYSTM_H
38#include <netinet/in_systm.h>
39#endif
40
41#ifdef HAVE_SCHED_H
42#include <sched.h>
43#endif
44
45#include "tcpr.h"
46
47#ifdef HAVE_BPF
48#include <net/bpf.h>
49#define PCAP_DONT_INCLUDE_PCAP_BPF_H 1
50#endif
51
52#if defined INCLUDE_PCAP_BPF_H_FILE && !defined PCAP_DONT_INCLUDE_PCAP_BPF_H
53#include "@INCLUDE_PCAP_BPF_HEADER@"
54#define PCAP_DONT_INCLUDE_PCAP_BPF_H 1 /* don't re-include it in pcap.h */
55#endif
56
57#include "@LPCAPINC@"
58
59/* include our own strlcat/strlcpy? */
60#ifndef HAVE_STRLCPY
61#include "lib/strlcpy.h"
62#endif
63
64/*
65 * net/bpf.h doesn't include DLT types, but pcap-bpf.h does.
66 * Unfortunately, pcap-bpf.h also includes things in net/bpf.h
67 * while also missing some key things (wow, that sucks)
68 * The result is that I stole the DLT types from pcap-bpf.h and
69 * put them in here.
70 */
71#include "common/dlt_names.h"
72
73#ifdef HAVE_BOOL_H
74#include <bool.h>
75#endif
76
77#ifdef HAVE_STDBOOL_H
78#include <stdbool.h>
79#endif
80
81
82/* should packet counters be 32 or 64 bit? --enable-64bit */
83#ifdef ENABLE_64BITS
84#define COUNTER unsigned long long
85#define COUNTER_SPEC "%llu"
86#else
87#define COUNTER unsigned long
88#define COUNTER_SPEC "%lu"
89#endif
90#define COUNTER_OVERFLOW_RISK (((COUNTER)~0) >> 20)
91
92#include "common/list.h"
93#include "common/cidr.h"
94
95
96typedef struct tcpr_ipv4_hdr ipv4_hdr_t;
97typedef struct tcpr_ipv6_hdr ipv6_hdr_t;
98typedef struct tcpr_tcp_hdr tcp_hdr_t;
99typedef struct tcpr_udp_hdr udp_hdr_t;
100typedef struct tcpr_icmpv4_hdr icmpv4_hdr_t;
101typedef struct tcpr_icmpv6_hdr icmpv6_hdr_t;
102typedef struct tcpr_ethernet_hdr eth_hdr_t;
103typedef struct tcpr_802_1q_tag vlan_hdr_t;
104typedef struct tcpr_802_1q_hdr vlan_pkt_hdr_t;
105typedef struct sll_header sll_hdr_t;
106typedef struct tcpr_arp_hdr arp_hdr_t;
107typedef struct tcpr_dnsv4_hdr dnsv4_hdr_t;
108
109/* our custom typdefs/structs */
110typedef u_char tcpr_macaddr_t[TCPR_ETH_H];
111
112typedef struct tcpr_bpf_s {
113    char *filter;
114    int optimize;
115    struct bpf_program program;
116} tcpr_bpf_t;
117
118typedef struct tcpr_xX_s {
119#define xX_MODE_INCLUDE 'x'
120#define xX_MODE_EXCLUDE 'X'
121    int mode;
122    tcpr_list_t *list;
123    tcpr_cidr_t *cidr;
124#define xX_TYPE_LIST 1
125#define xX_TYPE_CIDR 2
126    int type;
127} tcpr_xX_t;
128
129/* number of ports 0-65535 */
130#define NUM_PORTS 65536
131typedef struct tcpr_services_s {
132    char tcp[NUM_PORTS];
133    char udp[NUM_PORTS];
134} tcpr_services_t;
135
136typedef struct tcpr_speed_s {
137    /* speed modifiers */
138    int mode;
139#define SPEED_MULTIPLIER 1
140#define SPEED_MBPSRATE   2
141#define SPEED_PACKETRATE 3
142#define SPEED_TOPSPEED   4
143#define SPEED_ONEATATIME 5
144    COUNTER speed;
145    float multiplier;
146    int pps_multi;
147} tcpr_speed_t;
148
149#define MAX_FILES   1024        /* Max number of files we can pass to tcpreplay */
150
151#define DEFAULT_MTU 1500        /* Max Transmission Unit of standard ethernet
152                                 * don't forget *frames* are MTU + L2 header!
153                                 */
154
155#define MAX_SNAPLEN 262144      /* tell libpcap to capture the entire packet
156                                 * this is the maximum size supported by libpcap
157                                 * (https://github.com/the-tcpdump-group/libpcap/blob/master/pcap-int.h#L99-L125)
158                                 */
159
160#define MAXPACKET (MAX_SNAPLEN + 22)    /* snap length plus some room for adding a
161                                         * couple VLAN headers or a L2 header
162                                         */
163
164#define PACKET_HEADROOM 512     /* additional headroom allocated for packets to accommodate editing */
165
166#define DNS_RESOLVE 1
167#define DNS_DONT_RESOLVE 0
168
169#define RESOLVE 0               /* disable dns lookups */
170#define BPF_OPTIMIZE 1          /* default is to optimize bpf program */
171#define PCAP_TIMEOUT 100        /* 100ms pcap_open_live timeout */
172
173#define DEFAULT_FUZZ_FACTOR 8
174
175/* HP-UX already defines TRUE/FALSE */
176#ifndef TRUE
177typedef enum bool_e {
178    FALSE = 0,
179    TRUE
180} bool_t;
181#endif
182
183#define EBUF_SIZE 1024           /* size of our error buffers */
184#define MAC_SIZE  7             /* size of the mac[] buffer */
185
186typedef enum pad_e {
187    PAD_PACKET,
188    TRUNC_PACKET
189} pad_t;
190
191#define DNS_QUERY_FLAG 0x8000
192
193typedef enum direction_e {
194    DIR_UNKNOWN = -1,
195    DIR_CLIENT  = 0,
196    DIR_SERVER  = 1,
197    DIR_ANY     = 2
198} direction_t;
199
200typedef enum tcpprep_mode_e {
201    ERROR_MODE,     /* Some kind of error has occurred */
202    CIDR_MODE,      /* single pass, CIDR netblock */
203    REGEX_MODE,     /* single pass, regex */
204    PORT_MODE,      /* single pass, use src/dst ports to split */
205    MAC_MODE,       /* single pass, use src mac to split */
206    FIRST_MODE,     /* single pass, use first seen to split */
207    AUTO_MODE,      /* first pass through in auto mode */
208    ROUTER_MODE,    /* second pass through in router mode */
209    BRIDGE_MODE,    /* second pass through in bridge mode */
210    SERVER_MODE,    /* second pass through in server (router) mode */
211    CLIENT_MODE     /* second pass through in client (router) mode */
212} tcpprep_mode_t;
213
214#define BROADCAST_MAC "\xFF\xFF\xFF\xFF\xFF\xFF"
215
216/* MAC macros for printf */
217#define MAC_FORMAT "%02X:%02X:%02X:%02X:%02X:%02X"
218#define MAC_STR(x) x[0], x[1], x[2], x[3], x[4], x[5]
219
220/* struct timeval print structs */
221#ifdef HAVE_DARWIN
222/* Darwin defines usec as an __int32_t, not unsigned long. */
223#define TIMEVAL_FORMAT "%lus %dusec"
224#else
225#define TIMEVAL_FORMAT "%lus %luusec"
226#endif
227#define TIMESPEC_FORMAT "%lus %lunsec"
228
229/* force a word or half-word swap on both Big and Little endian systems */
230#ifndef SWAPLONG
231#define SWAPLONG(y) \
232((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
233#endif
234
235#ifndef SWAPSHORT
236#define SWAPSHORT(y) \
237( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
238#endif
239
240/* converts a 64bit int to network byte order */
241#if !defined ntohll && !defined htonll
242#ifndef HAVE_NTOHLL
243#ifdef WORDS_BIGENDIAN
244#define ntohll(x) (x)
245#define htonll(x) (x)
246#else
247/* stolen from http://www.codeproject.com/cpp/endianness.asp */
248#define ntohll(x) (((u_int64_t)(ntohl((int)((x << 32) >> 32))) << 32) | \
249                     (unsigned int)ntohl(((int)(x >> 32))))
250#define htonll(x) ntohll(x)
251#endif /* WORDS_BIGENDIAN */
252#endif /* HAVE_NTOHLL */
253#endif /* !ntohll && !htonll */
254
255#define DEBUG_INFO   1          /* informational only, lessthan 1 line per packet */
256#define DEBUG_BASIC  2          /* limited debugging, one line per packet */
257#define DEBUG_DETAIL 3          /* more detailed, a few lines per packet */
258#define DEBUG_MORE   4          /* even more detail */
259#define DEBUG_CODE   5          /* examines code & values, many lines per packet */
260
261#if defined HAVE_IOPERM && defined __i386_
262    #define HAVE_IOPORT_SLEEP
263#endif
264
265/* Win32 doesn't know about PF_INET6 */
266#ifndef PF_INET6
267#ifdef AF_INET6
268#define PF_INET6 AF_INET6
269#else
270#define PF_INET6 30 /* stolen from OS/X */
271#endif
272#endif
273
274/* convert IPv6 Extension Header Len value to bytes */
275#define IPV6_EXTLEN_TO_BYTES(x) ((x * 4) + 8)
276
277#ifndef HAVE_UINT8_T
278typedef u_int8_t uint8_t
279typedef u_int16_t uint16_t
280typedef u_int32_t uint32_t
281#endif
282
283/* Support for flexible arrays. */
284#undef __flexarr
285#if defined(__GNUC__) && ((__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ >= 97))
286/* GCC 2.97 supports C99 flexible array members.  */
287# define __flexarr      []
288#else
289# ifdef __GNUC__
290#  define __flexarr     [0]
291# else
292#  if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
293#   define __flexarr    []
294#  elif defined(_WIN32)
295/* MS VC++ */
296#   define __flexarr    []
297#  else
298/* Some other non-C99 compiler. Approximate with [1]. */
299#   define __flexarr    [1]
300#  endif
301# endif
302#endif
303
304
305/* Time converters */
306#define SEC_TO_MILLISEC(x) (x * 1000)
307#define SEC_TO_MICROSEC(x) (x * 1000000)
308#define SEC_TO_NANOSEC(x)  ((u_int64_t)x * 1000000000)
309
310#define MILLISEC_TO_SEC(x) (x / 1000)
311#define MICROSEC_TO_SEC(x) (x / 1000000)
312#define NANOSEC_TO_SEC(x)  ((u_int64_t)x / 1000000000)
313
314#define TIMEVAL_TO_MILLISEC(x)  (((x)->tv_sec * 1000) + ((x)->tv_usec / 1000))
315#define TIMEVAL_TO_MICROSEC(x)  (((x)->tv_sec * 1000000) + (x)->tv_usec)
316#define TIMEVAL_TO_NANOSEC(x)   ((u_int64_t)((x)->tv_sec * 1000000000) + ((u_int64_t)(x)->tv_usec * 1000))
317#define TIMSTAMP_TO_MICROSEC(x) (TIMEVAL_TO_MICROSEC(x))
318
319#define MILLISEC_TO_TIMEVAL(x, tv)          \
320    do {                                    \
321        (tv)->tv_sec = (x) / 1000;          \
322        (tv)->tv_usec = (x * 1000) - ((tv)->tv_sec * 1000000);   \
323    } while(0)
324
325#define MICROSEC_TO_TIMEVAL(x, tv)          \
326    do {                                    \
327        (tv)->tv_sec = (x) / 1000000;       \
328        (tv)->tv_usec = (x) - ((tv)->tv_sec * 1000000);   \
329    } while(0)
330
331#define NANOSEC_TO_TIMEVAL(x, tv)           \
332    do {                                    \
333        (tv)->tv_sec =  (x) / 1000000000;   \
334        (tv)->tv_usec = ((x) % 1000000000) / 1000;   \
335    } while(0)
336
337#define NANOSEC_TO_TIMESPEC(x, ts)          \
338    do {                                    \
339        (ts)->tv_sec = (x) / 1000000000;    \
340        (ts)->tv_nsec = (x) % 1000000000;   \
341    } while(0)
342
343#define TIMESPEC_TO_MILLISEC(x)  (((x)->tv_sec * 1000) + ((x)->tv_nsec / 1000000))
344#define TIMESPEC_TO_MICROSEC(x)  (((x)->tv_sec * 1000000) + (x)->tv_nsec / 1000)
345#define TIMESPEC_TO_NANOSEC(x) ((u_int64_t)((x)->tv_sec * 1000000000) + ((u_int64_t)(x)->tv_nsec))
346
347#define TIMEVAL_SET(a, b)                  \
348    do {                                    \
349        (a)->tv_sec = (b)->tv_sec;          \
350        (a)->tv_usec = (b)->tv_usec;        \
351    } while(0)
352
353#define TIMESPEC_SET(a, b)                 \
354    do {                                    \
355        (a)->tv_sec = (b)->tv_sec;          \
356        (a)->tv_nsec = (b)->tv_nsec;        \
357    } while(0)
358
359/*
360 * Help suppress some compiler warnings
361 * No problem if variable is actually used
362*/
363#ifdef UNUSED
364#elif defined(__GNUC__)
365# define UNUSED(x) x __attribute__((unused))
366#elif defined(__LCLINT__)
367# define UNUSED(x) /*@unused@*/ x
368#else
369# define UNUSED(x) x
370#endif
371
372#ifndef max
373#  define max(a,b) \
374       ({ __typeof__ (a) _a = (a); \
375           __typeof__ (b) _b = (b); \
376       _a > _b ? _a : _b; })
377#endif
378
379#ifndef min
380#  define min(a,b) \
381       ({ __typeof__ (a) _a = (a); \
382           __typeof__ (b) _b = (b); \
383       _a > _b ? _b : _a; })
384#endif
385
386#endif /* DEFINES */
387