defines.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 "/usr/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 "/usr/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 ""
  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. };
  83. typedef struct tcpr_speed_s tcpr_speed_t;
  84. #define MAX_FILES 1024 /* Max number of files we can pass to tcpreplay */
  85. #define DEFAULT_MTU 1500 /* Max Transmission Unit of standard ethernet
  86. * don't forget *frames* are MTU + L2 header! */
  87. #define MAXPACKET 65535 /* was 16436 linux loopback, but maybe something is bigger then
  88. linux loopback */
  89. #define MAX_SNAPLEN 65535 /* tell libpcap to capture the entire packet */
  90. #define DNS_RESOLVE 1
  91. #define DNS_DONT_RESOLVE 0
  92. #define RESOLVE 0 /* disable dns lookups */
  93. #define BPF_OPTIMIZE 1 /* default is to optimize bpf program */
  94. #define PCAP_TIMEOUT 100 /* 100ms pcap_open_live timeout */
  95. /* HP-UX already defines TRUE/FALSE */
  96. #ifndef TRUE
  97. enum bool_t {
  98. FALSE = 0,
  99. TRUE
  100. };
  101. #endif
  102. #define EBUF_SIZE 1024 /* size of our error buffers */
  103. #define MAC_SIZE 7 /* size of the mac[] buffer */
  104. enum pad_t {
  105. PAD_PACKET,
  106. TRUNC_PACKET
  107. };
  108. #define DNS_QUERY_FLAG 0x8000
  109. enum direction_t {
  110. DIR_UNKNOWN = -1,
  111. DIR_CLIENT = 0,
  112. DIR_SERVER = 1,
  113. DIR_ANY = 2
  114. };
  115. enum tcpprep_mode {
  116. ERROR_MODE, /* Some kind of error has occurred */
  117. CIDR_MODE, /* single pass, CIDR netblock */
  118. REGEX_MODE, /* single pass, regex */
  119. PORT_MODE, /* single pass, use src/dst ports to split */
  120. MAC_MODE, /* single pass, use src mac to split */
  121. FIRST_MODE, /* single pass, use first seen to split */
  122. AUTO_MODE, /* first pass through in auto mode */
  123. ROUTER_MODE, /* second pass through in router mode */
  124. BRIDGE_MODE, /* second pass through in bridge mode */
  125. SERVER_MODE, /* second pass through in server (router) mode */
  126. CLIENT_MODE /* second pass through in client (router) mode */
  127. };
  128. #define BROADCAST_MAC "\xFF\xFF\xFF\xFF\xFF\xFF"
  129. /* MAC macros for printf */
  130. #define MAC_FORMAT "%02X:%02X:%02X:%02X:%02X:%02X"
  131. #define MAC_STR(x) x[0], x[1], x[2], x[3], x[4], x[5]
  132. /* struct timeval print structs */
  133. #define TIMEVAL_FORMAT "%lus %luusec"
  134. #define TIMESPEC_FORMAT "%lus %lunsec"
  135. /* force a word or half-word swap on both Big and Little endian systems */
  136. #ifndef SWAPLONG
  137. #define SWAPLONG(y) \
  138. ((((y)&0xff)<<24) | (((y)&0xff00)<<8) | (((y)&0xff0000)>>8) | (((y)>>24)&0xff))
  139. #endif
  140. #ifndef SWAPSHORT
  141. #define SWAPSHORT(y) \
  142. ( (((y)&0xff)<<8) | ((u_short)((y)&0xff00)>>8) )
  143. #endif
  144. /* converts a 64bit int to network byte order */
  145. #ifndef HAVE_NTOHLL
  146. #ifdef WORDS_BIGENDIAN
  147. #define ntohll(x) (x)
  148. #define htonll(x) (x)
  149. #else
  150. /* stolen from http://www.codeproject.com/cpp/endianness.asp */
  151. #define ntohll(x) (((u_int64_t)(ntohl((int)((x << 32) >> 32))) << 32) | \
  152. (unsigned int)ntohl(((int)(x >> 32))))
  153. #define htonll(x) ntohll(x)
  154. #endif /* WORDS_BIGENDIAN */
  155. #endif
  156. #define DEBUG_INFO 1 /* informational only, lessthan 1 line per packet */
  157. #define DEBUG_BASIC 2 /* limited debugging, one line per packet */
  158. #define DEBUG_DETAIL 3 /* more detailed, a few lines per packet */
  159. #define DEBUG_MORE 4 /* even more detail */
  160. #define DEBUG_CODE 5 /* examines code & values, many lines per packet */
  161. /* Win32 doesn't know about PF_INET6 */
  162. #ifndef PF_INET6
  163. #ifdef AF_INET6
  164. #define PF_INET6 AF_INET6
  165. #else
  166. #define PF_INET6 30 /* stolen from OS/X */
  167. #endif
  168. #endif
  169. #ifndef HAVE_UINT8_T
  170. typedef u_int8_t uint8_t
  171. typedef u_int16_t uint16_t
  172. typedef u_int32_t uint32_t
  173. #endif
  174. /* Support for flexible arrays. */
  175. #undef __flexarr
  176. #if defined(__GNUC__) && ((__GNUC__ > 2) || (__GNUC__ == 2 && __GNUC_MINOR__ >= 97))
  177. /* GCC 2.97 supports C99 flexible array members. */
  178. # define __flexarr []
  179. #else
  180. # ifdef __GNUC__
  181. # define __flexarr [0]
  182. # else
  183. # if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
  184. # define __flexarr []
  185. # elif defined(_WIN32)
  186. /* MS VC++ */
  187. # define __flexarr []
  188. # else
  189. /* Some other non-C99 compiler. Approximate with [1]. */
  190. # define __flexarr [1]
  191. # endif
  192. # endif
  193. #endif
  194. /* Time converters */
  195. #define SEC_TO_MILLISEC(x) (x * 1000)
  196. #define SEC_TO_MICROSEC(x) (x * 1000000)
  197. #define SEC_TO_NANOSEC(x) ((u_int64_t)x * 1000000000)
  198. #define MILLISEC_TO_SEC(x) (x / 1000)
  199. #define MICROSEC_TO_SEC(x) (x / 1000000)
  200. #define NANOSEC_TO_SEC(x) ((u_int64_t)x / 1000000000)
  201. #define TIMEVAL_TO_MILLISEC(x) (((x)->tv_sec * 1000) + ((x)->tv_usec / 1000))
  202. #define TIMEVAL_TO_MICROSEC(x) (((x)->tv_sec * 1000000) + (x)->tv_usec)
  203. #define TIMEVAL_TO_NANOSEC(x) ((u_int64_t)((x)->tv_sec * 1000000000) + ((u_int64_t)(x)->tv_usec * 1000))
  204. #define MILLISEC_TO_TIMEVAL(x, tv) \
  205. do { \
  206. (tv)->tv_sec = (x) / 1000; \
  207. (tv)->tv_usec = (x * 1000) - ((tv)->tv_sec * 1000000); \
  208. } while(0)
  209. #define MICROSEC_TO_TIMEVAL(x, tv) \
  210. do { \
  211. (tv)->tv_sec = (x) / 1000000; \
  212. (tv)->tv_usec = (x) - ((tv)->tv_sec * 1000000); \
  213. } while(0)
  214. #define NANOSEC_TO_TIMEVAL(x, tv) \
  215. do { \
  216. (tv)->tv_sec = (x) / 1000000000; \
  217. (tv)->tv_usec = ((x) % 1000000000) / 1000); \
  218. } while(0)
  219. #define NANOSEC_TO_TIMESPEC(x, ts) \
  220. do { \
  221. (ts)->tv_sec = (x) / 1000000000; \
  222. (ts)->tv_nsec = (x) % 1000000000; \
  223. } while(0)
  224. #define TIMESPEC_TO_MILLISEC(x) (((x)->tv_sec * 1000) + ((x)->tv_nsec / 1000000))
  225. #define TIMESPEC_TO_MICROSEC(x) (((x)->tv_sec * 1000000) + (x)->tv_nsec / 1000)
  226. #define TIMESPEC_TO_NANOSEC(x) ((u_int64_t)((x)->tv_sec * 1000000000) + ((u_int64_t)(x)->tv_nsec))
  227. #endif /* DEFINES */