defines.h 9.4 KB

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