defines.h.in 10 KB

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