Constants.hpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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 ZT1_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. * Since there's no retransmits, this is just a really bad case scenario for
  158. * transit time. It's short enough that a DOS attack from exhausing buffers is
  159. * very unlikely, as the transfer rate would have to be fast enough to fill
  160. * system memory in this time.
  161. */
  162. #define ZT_FRAGMENTED_PACKET_RECEIVE_TIMEOUT 1000
  163. /**
  164. * Length of secret key in bytes -- 256-bit for Salsa20
  165. */
  166. #define ZT_PEER_SECRET_KEY_LENGTH 32
  167. /**
  168. * How often Topology::clean() and Network::clean() and similar are called, in ms
  169. */
  170. #define ZT_HOUSEKEEPING_PERIOD 120000
  171. /**
  172. * Overriding granularity for timer tasks to prevent CPU-intensive thrashing on every packet
  173. */
  174. #define ZT_CORE_TIMER_TASK_GRANULARITY 1000
  175. /**
  176. * How long to remember peer records in RAM if they haven't been used
  177. */
  178. #define ZT_PEER_IN_MEMORY_EXPIRATION 600000
  179. /**
  180. * Delay between WHOIS retries in ms
  181. */
  182. #define ZT_WHOIS_RETRY_DELAY 500
  183. /**
  184. * Maximum identity WHOIS retries (each attempt tries consulting a different peer)
  185. */
  186. #define ZT_MAX_WHOIS_RETRIES 3
  187. /**
  188. * Transmit queue entry timeout
  189. */
  190. #define ZT_TRANSMIT_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  191. /**
  192. * Receive queue entry timeout
  193. */
  194. #define ZT_RECEIVE_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  195. /**
  196. * Maximum number of ZT hops allowed (this is not IP hops/TTL)
  197. *
  198. * The protocol allows up to 7, but we limit it to something smaller.
  199. */
  200. #define ZT_RELAY_MAX_HOPS 3
  201. /**
  202. * Expire time for multicast 'likes' and indirect multicast memberships in ms
  203. */
  204. #define ZT_MULTICAST_LIKE_EXPIRE 600000
  205. /**
  206. * Delay between explicit MULTICAST_GATHER requests for a given multicast channel
  207. */
  208. #define ZT_MULTICAST_EXPLICIT_GATHER_DELAY (ZT_MULTICAST_LIKE_EXPIRE / 10)
  209. /**
  210. * Timeout for outgoing multicasts
  211. *
  212. * This is how long we wait for explicit or implicit gather results.
  213. */
  214. #define ZT_MULTICAST_TRANSMIT_TIMEOUT 5000
  215. /**
  216. * Default maximum number of peers to address with a single multicast (if unspecified in network config)
  217. */
  218. #define ZT_MULTICAST_DEFAULT_LIMIT 32
  219. /**
  220. * How frequently to send a zero-byte UDP keepalive packet
  221. *
  222. * There are NATs with timeouts as short as 20 seconds, so this turns out
  223. * to be needed.
  224. */
  225. #define ZT_NAT_KEEPALIVE_DELAY 19000
  226. /**
  227. * Delay between scans of the topology active peer DB for peers that need ping
  228. *
  229. * This is also how often pings will be retried to upstream peers (relays, roots)
  230. * constantly until something is heard.
  231. */
  232. #define ZT_PING_CHECK_INVERVAL 6250
  233. /**
  234. * Delay between ordinary case pings of direct links
  235. */
  236. #define ZT_PEER_DIRECT_PING_DELAY 120000
  237. /**
  238. * Delay between requests for updated network autoconf information
  239. */
  240. #define ZT_NETWORK_AUTOCONF_DELAY 60000
  241. /**
  242. * Timeout for overall peer activity (measured from last receive)
  243. */
  244. #define ZT_PEER_ACTIVITY_TIMEOUT (ZT_PEER_DIRECT_PING_DELAY + (ZT_PING_CHECK_INVERVAL * 3))
  245. /**
  246. * Stop relaying via peers that have not responded to direct sends
  247. *
  248. * When we send something (including frames), we generally expect a response.
  249. * Switching relays if no response in a short period of time causes more
  250. * rapid failover if a root server goes down or becomes unreachable. In the
  251. * mistaken case, little harm is done as it'll pick the next-fastest
  252. * root server and will switch back eventually.
  253. */
  254. #define ZT_PEER_RELAY_CONVERSATION_LATENCY_THRESHOLD 10000
  255. /**
  256. * Minimum interval between attempts by relays to unite peers
  257. *
  258. * When a relay gets a packet destined for another peer, it sends both peers
  259. * a RENDEZVOUS message no more than this often. This instructs the peers
  260. * to attempt NAT-t and gives each the other's corresponding IP:port pair.
  261. */
  262. #define ZT_MIN_UNITE_INTERVAL 60000
  263. /**
  264. * Delay between initial direct NAT-t packet and more aggressive techniques
  265. *
  266. * This may also be a delay before sending the first packet if we determine
  267. * that we should wait for the remote to initiate rendezvous first.
  268. */
  269. #define ZT_NAT_T_TACTICAL_ESCALATION_DELAY 1000
  270. /**
  271. * Size of anti-recursion history (see AntiRecursion.hpp)
  272. */
  273. #define ZT_ANTIRECURSION_HISTORY_SIZE 16
  274. /**
  275. * Minimum delay between attempts to confirm new paths to peers (to avoid HELLO flooding)
  276. */
  277. #define ZT_MIN_PATH_CONFIRMATION_INTERVAL 5000
  278. /**
  279. * Interval between direct path pushes in milliseconds
  280. */
  281. #define ZT_DIRECT_PATH_PUSH_INTERVAL 300000
  282. /**
  283. * Sanity limit on maximum bridge routes
  284. *
  285. * If the number of bridge routes exceeds this, we cull routes from the
  286. * bridges with the most MACs behind them until it doesn't. This is a
  287. * sanity limit to prevent memory-filling DOS attacks, nothing more. No
  288. * physical LAN has anywhere even close to this many nodes. Note that this
  289. * does not limit the size of ZT virtual LANs, only bridge routing.
  290. */
  291. #define ZT_MAX_BRIDGE_ROUTES 67108864
  292. /**
  293. * If there is no known route, spam to up to this many active bridges
  294. */
  295. #define ZT_MAX_BRIDGE_SPAM 16
  296. /**
  297. * A test pseudo-network-ID that can be joined
  298. *
  299. * Joining this network ID will result in a network with no IP addressing
  300. * and default parameters. No network configuration master will be consulted
  301. * and instead a static config will be used. This is used in built-in testnet
  302. * scenarios and can also be used for external testing.
  303. *
  304. * This is an impossible real network ID since 0xff is a reserved address
  305. * prefix.
  306. */
  307. #define ZT_TEST_NETWORK_ID 0xffffffffffffffffULL
  308. #endif