Constants.hpp 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  1. /*
  2. * ZeroTier One - Network Virtualization Everywhere
  3. * Copyright (C) 2011-2015 ZeroTier, Inc.
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. * --
  19. *
  20. * ZeroTier may be used and distributed under the terms of the GPLv3, which
  21. * are available at: http://www.gnu.org/licenses/gpl-3.0.html
  22. *
  23. * If you would like to embed ZeroTier into a commercial application or
  24. * redistribute it in a modified binary form, please contact ZeroTier Networks
  25. * LLC. Start here: http://www.zerotier.com/
  26. */
  27. #ifndef ZT_CONSTANTS_HPP
  28. #define ZT_CONSTANTS_HPP
  29. #include "../include/ZeroTierOne.h"
  30. //
  31. // This include file also auto-detects and canonicalizes some environment
  32. // information defines:
  33. //
  34. // __LINUX__
  35. // __APPLE__
  36. // __BSD__ (OSX also defines this)
  37. // __UNIX_LIKE__ (Linux, BSD, etc.)
  38. // __WINDOWS__
  39. //
  40. // Also makes sure __BYTE_ORDER is defined reasonably.
  41. //
  42. // Hack: make sure __GCC__ is defined on old GCC compilers
  43. #ifndef __GCC__
  44. #if defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_1) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_2) || defined(__GCC_HAVE_SYNC_COMPARE_AND_SWAP_4)
  45. #define __GCC__
  46. #endif
  47. #endif
  48. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  49. #ifndef __LINUX__
  50. #define __LINUX__
  51. #endif
  52. #ifndef __UNIX_LIKE__
  53. #define __UNIX_LIKE__
  54. #endif
  55. #include <endian.h>
  56. #endif
  57. // Disable type punning on ARM architecture -- some ARM chips throw SIGBUS on unaligned access
  58. #if defined(__arm__) || defined(__ARMEL__)
  59. #ifndef ZT_NO_TYPE_PUNNING
  60. #define ZT_NO_TYPE_PUNNING
  61. #endif
  62. #endif
  63. #if defined(__FreeBSD__) || defined(__OpenBSD__)
  64. #ifndef __UNIX_LIKE__
  65. #define __UNIX_LIKE__
  66. #endif
  67. #ifndef __BSD__
  68. #define __BSD__
  69. #endif
  70. #include <machine/endian.h>
  71. #ifndef __BYTE_ORDER
  72. #define __BYTE_ORDER _BYTE_ORDER
  73. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  74. #define __BIG_ENDIAN _BIG_ENDIAN
  75. #endif
  76. #endif
  77. // TODO: Android is what? Linux technically, but does it define it?
  78. #ifdef __APPLE__
  79. #include <TargetConditionals.h>
  80. #ifndef __UNIX_LIKE__
  81. #define __UNIX_LIKE__
  82. #endif
  83. #ifndef __BSD__
  84. #define __BSD__
  85. #endif
  86. #endif
  87. #if defined(_WIN32) || defined(_WIN64)
  88. #ifndef __WINDOWS__
  89. #define __WINDOWS__
  90. #endif
  91. #ifndef NOMINMAX
  92. #define NOMINMAX
  93. #endif
  94. #pragma warning(disable : 4290)
  95. #pragma warning(disable : 4996)
  96. #pragma warning(disable : 4101)
  97. #undef __UNIX_LIKE__
  98. #undef __BSD__
  99. #define ZT_PATH_SEPARATOR '\\'
  100. #define ZT_PATH_SEPARATOR_S "\\"
  101. #define ZT_EOL_S "\r\n"
  102. #include <WinSock2.h>
  103. #include <Windows.h>
  104. #endif
  105. // Assume these are little-endian. PPC is not supported for OSX, and ARM
  106. // runs in little-endian mode for these OS families.
  107. #if defined(__APPLE__) || defined(__WINDOWS__)
  108. #undef __BYTE_ORDER
  109. #undef __LITTLE_ENDIAN
  110. #undef __BIG_ENDIAN
  111. #define __BIG_ENDIAN 4321
  112. #define __LITTLE_ENDIAN 1234
  113. #define __BYTE_ORDER 1234
  114. #endif
  115. #ifdef __UNIX_LIKE__
  116. #define ZT_PATH_SEPARATOR '/'
  117. #define ZT_PATH_SEPARATOR_S "/"
  118. #define ZT_EOL_S "\n"
  119. #endif
  120. #ifndef __BYTE_ORDER
  121. #include <endian.h>
  122. #endif
  123. /**
  124. * Length of a ZeroTier address in bytes
  125. */
  126. #define ZT_ADDRESS_LENGTH 5
  127. /**
  128. * Length of a hexadecimal ZeroTier address
  129. */
  130. #define ZT_ADDRESS_LENGTH_HEX 10
  131. /**
  132. * Addresses beginning with this byte are reserved for the joy of in-band signaling
  133. */
  134. #define ZT_ADDRESS_RESERVED_PREFIX 0xff
  135. /**
  136. * Default payload MTU for UDP packets
  137. *
  138. * In the future we might support UDP path MTU discovery, but for now we
  139. * set a maximum that is equal to 1500 minus 8 (for PPPoE overhead, common
  140. * in some markets) minus 48 (IPv6 UDP overhead).
  141. */
  142. #define ZT_UDP_DEFAULT_PAYLOAD_MTU 1444
  143. /**
  144. * Default MTU used for Ethernet tap device
  145. */
  146. #define ZT_IF_MTU ZT_MAX_MTU
  147. /**
  148. * Maximum number of packet fragments we'll support
  149. *
  150. * The actual spec allows 16, but this is the most we'll support right
  151. * now. Packets with more than this many fragments are dropped.
  152. */
  153. #define ZT_MAX_PACKET_FRAGMENTS 4
  154. /**
  155. * Timeout for receipt of fragmented packets in ms
  156. */
  157. #define ZT_FRAGMENTED_PACKET_RECEIVE_TIMEOUT 500
  158. /**
  159. * Length of secret key in bytes -- 256-bit -- do not change
  160. */
  161. #define ZT_PEER_SECRET_KEY_LENGTH 32
  162. /**
  163. * How often Topology::clean() and Network::clean() and similar are called, in ms
  164. */
  165. #define ZT_HOUSEKEEPING_PERIOD 120000
  166. /**
  167. * Overriding granularity for timer tasks to prevent CPU-intensive thrashing on every packet
  168. */
  169. #define ZT_CORE_TIMER_TASK_GRANULARITY 500
  170. /**
  171. * How long to remember peer records in RAM if they haven't been used
  172. */
  173. #define ZT_PEER_IN_MEMORY_EXPIRATION 600000
  174. /**
  175. * Delay between WHOIS retries in ms
  176. */
  177. #define ZT_WHOIS_RETRY_DELAY 500
  178. /**
  179. * Maximum identity WHOIS retries (each attempt tries consulting a different peer)
  180. */
  181. #define ZT_MAX_WHOIS_RETRIES 3
  182. /**
  183. * Transmit queue entry timeout
  184. */
  185. #define ZT_TRANSMIT_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  186. /**
  187. * Receive queue entry timeout
  188. */
  189. #define ZT_RECEIVE_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  190. /**
  191. * Maximum number of ZT hops allowed (this is not IP hops/TTL)
  192. *
  193. * The protocol allows up to 7, but we limit it to something smaller.
  194. */
  195. #define ZT_RELAY_MAX_HOPS 3
  196. /**
  197. * Expire time for multicast 'likes' and indirect multicast memberships in ms
  198. */
  199. #define ZT_MULTICAST_LIKE_EXPIRE 600000
  200. /**
  201. * Delay between explicit MULTICAST_GATHER requests for a given multicast channel
  202. */
  203. #define ZT_MULTICAST_EXPLICIT_GATHER_DELAY (ZT_MULTICAST_LIKE_EXPIRE / 10)
  204. /**
  205. * Timeout for outgoing multicasts
  206. *
  207. * This is how long we wait for explicit or implicit gather results.
  208. */
  209. #define ZT_MULTICAST_TRANSMIT_TIMEOUT 5000
  210. /**
  211. * Default maximum number of peers to address with a single multicast (if unspecified in network config)
  212. */
  213. #define ZT_MULTICAST_DEFAULT_LIMIT 32
  214. /**
  215. * How frequently to send a zero-byte UDP keepalive packet
  216. *
  217. * There are NATs with timeouts as short as 20 seconds, so this turns out
  218. * to be needed.
  219. */
  220. #define ZT_NAT_KEEPALIVE_DELAY 19000
  221. /**
  222. * Delay between scans of the topology active peer DB for peers that need ping
  223. *
  224. * This is also how often pings will be retried to upstream peers (relays, roots)
  225. * constantly until something is heard.
  226. */
  227. #define ZT_PING_CHECK_INVERVAL 9500
  228. /**
  229. * Delay between ordinary case pings of direct links
  230. */
  231. #define ZT_PEER_DIRECT_PING_DELAY 60000
  232. /**
  233. * Timeout for overall peer activity (measured from last receive)
  234. */
  235. #define ZT_PEER_ACTIVITY_TIMEOUT ((ZT_PEER_DIRECT_PING_DELAY * 4) + ZT_PING_CHECK_INVERVAL)
  236. /**
  237. * Delay between requests for updated network autoconf information
  238. */
  239. #define ZT_NETWORK_AUTOCONF_DELAY 60000
  240. /**
  241. * Minimum interval between attempts by relays to unite peers
  242. *
  243. * When a relay gets a packet destined for another peer, it sends both peers
  244. * a RENDEZVOUS message no more than this often. This instructs the peers
  245. * to attempt NAT-t and gives each the other's corresponding IP:port pair.
  246. */
  247. #define ZT_MIN_UNITE_INTERVAL 30000
  248. /**
  249. * Delay between initial direct NAT-t packet and more aggressive techniques
  250. *
  251. * This may also be a delay before sending the first packet if we determine
  252. * that we should wait for the remote to initiate rendezvous first.
  253. */
  254. #define ZT_NAT_T_TACTICAL_ESCALATION_DELAY 1000
  255. /**
  256. * Minimum delay between attempts to confirm new paths to peers (to avoid HELLO flooding)
  257. */
  258. #define ZT_MIN_PATH_CONFIRMATION_INTERVAL 1000
  259. /**
  260. * How long (max) to remember network certificates of membership?
  261. *
  262. * This only applies to networks we don't belong to.
  263. */
  264. #define ZT_PEER_NETWORK_COM_EXPIRATION 3600000
  265. /**
  266. * Sanity limit on maximum bridge routes
  267. *
  268. * If the number of bridge routes exceeds this, we cull routes from the
  269. * bridges with the most MACs behind them until it doesn't. This is a
  270. * sanity limit to prevent memory-filling DOS attacks, nothing more. No
  271. * physical LAN has anywhere even close to this many nodes. Note that this
  272. * does not limit the size of ZT virtual LANs, only bridge routing.
  273. */
  274. #define ZT_MAX_BRIDGE_ROUTES 67108864
  275. /**
  276. * If there is no known route, spam to up to this many active bridges
  277. */
  278. #define ZT_MAX_BRIDGE_SPAM 16
  279. /**
  280. * Interval between direct path pushes in milliseconds
  281. */
  282. #define ZT_DIRECT_PATH_PUSH_INTERVAL 120000
  283. /**
  284. * Time horizon for push direct paths cutoff
  285. */
  286. #define ZT_PUSH_DIRECT_PATHS_CUTOFF_TIME 60000
  287. /**
  288. * Maximum number of direct path pushes within cutoff time
  289. *
  290. * This limits response to PUSH_DIRECT_PATHS to CUTOFF_LIMIT responses
  291. * per CUTOFF_TIME milliseconds per peer to prevent this from being
  292. * useful for DOS amplification attacks.
  293. */
  294. #define ZT_PUSH_DIRECT_PATHS_CUTOFF_LIMIT 5
  295. /**
  296. * Maximum number of paths per IP scope (e.g. global, link-local) and family (e.g. v4/v6)
  297. */
  298. #define ZT_PUSH_DIRECT_PATHS_MAX_PER_SCOPE_AND_FAMILY 1
  299. /**
  300. * A test pseudo-network-ID that can be joined
  301. *
  302. * Joining this network ID will result in a network with no IP addressing
  303. * and default parameters. No network configuration master will be consulted
  304. * and instead a static config will be used. This is used in built-in testnet
  305. * scenarios and can also be used for external testing.
  306. *
  307. * This is an impossible real network ID since 0xff is a reserved address
  308. * prefix.
  309. */
  310. #define ZT_TEST_NETWORK_ID 0xffffffffffffffffULL
  311. #endif