defines.h 8.0 KB

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