defines.h.in 14 KB

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