defines.h.in 13 KB

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