Constants.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. #if defined(__FreeBSD__) || defined(__OpenBSD__)
  58. #ifndef __UNIX_LIKE__
  59. #define __UNIX_LIKE__
  60. #endif
  61. #ifndef __BSD__
  62. #define __BSD__
  63. #endif
  64. #include <machine/endian.h>
  65. #ifndef __BYTE_ORDER
  66. #define __BYTE_ORDER _BYTE_ORDER
  67. #define __LITTLE_ENDIAN _LITTLE_ENDIAN
  68. #define __BIG_ENDIAN _BIG_ENDIAN
  69. #endif
  70. #endif
  71. // TODO: Android is what? Linux technically, but does it define it?
  72. #ifdef __APPLE__
  73. #include <TargetConditionals.h>
  74. #ifndef __UNIX_LIKE__
  75. #define __UNIX_LIKE__
  76. #endif
  77. #ifndef __BSD__
  78. #define __BSD__
  79. #endif
  80. #endif
  81. #if defined(_WIN32) || defined(_WIN64)
  82. #ifndef __WINDOWS__
  83. #define __WINDOWS__
  84. #endif
  85. #ifndef NOMINMAX
  86. #define NOMINMAX
  87. #endif
  88. #pragma warning(disable : 4290)
  89. #pragma warning(disable : 4996)
  90. #pragma warning(disable : 4101)
  91. #undef __UNIX_LIKE__
  92. #undef __BSD__
  93. #define ZT_PATH_SEPARATOR '\\'
  94. #define ZT_PATH_SEPARATOR_S "\\"
  95. #define ZT_EOL_S "\r\n"
  96. #include <WinSock2.h>
  97. #include <Windows.h>
  98. #endif
  99. // Assume these are little-endian. PPC is not supported for OSX, and ARM
  100. // runs in little-endian mode for these OS families.
  101. #if defined(__APPLE__) || defined(__WINDOWS__)
  102. #undef __BYTE_ORDER
  103. #undef __LITTLE_ENDIAN
  104. #undef __BIG_ENDIAN
  105. #define __BIG_ENDIAN 4321
  106. #define __LITTLE_ENDIAN 1234
  107. #define __BYTE_ORDER 1234
  108. #endif
  109. #ifdef __UNIX_LIKE__
  110. #define ZT_PATH_SEPARATOR '/'
  111. #define ZT_PATH_SEPARATOR_S "/"
  112. #define ZT_EOL_S "\n"
  113. #endif
  114. #ifndef __BYTE_ORDER
  115. #include <endian.h>
  116. #endif
  117. /**
  118. * Length of a ZeroTier address in bytes
  119. */
  120. #define ZT_ADDRESS_LENGTH 5
  121. /**
  122. * Length of a hexadecimal ZeroTier address
  123. */
  124. #define ZT_ADDRESS_LENGTH_HEX 10
  125. /**
  126. * Addresses beginning with this byte are reserved for the joy of in-band signaling
  127. */
  128. #define ZT_ADDRESS_RESERVED_PREFIX 0xff
  129. /**
  130. * Default payload MTU for UDP packets
  131. *
  132. * In the future we might support UDP path MTU discovery, but for now we
  133. * set a maximum that is equal to 1500 minus 8 (for PPPoE overhead, common
  134. * in some markets) minus 48 (IPv6 UDP overhead).
  135. */
  136. #define ZT_UDP_DEFAULT_PAYLOAD_MTU 1444
  137. /**
  138. * Default MTU used for Ethernet tap device
  139. */
  140. #define ZT_IF_MTU ZT1_MAX_MTU
  141. /**
  142. * Maximum number of packet fragments we'll support
  143. *
  144. * The actual spec allows 16, but this is the most we'll support right
  145. * now. Packets with more than this many fragments are dropped.
  146. */
  147. #define ZT_MAX_PACKET_FRAGMENTS 4
  148. /**
  149. * Timeout for receipt of fragmented packets in ms
  150. *
  151. * Since there's no retransmits, this is just a really bad case scenario for
  152. * transit time. It's short enough that a DOS attack from exhausing buffers is
  153. * very unlikely, as the transfer rate would have to be fast enough to fill
  154. * system memory in this time.
  155. */
  156. #define ZT_FRAGMENTED_PACKET_RECEIVE_TIMEOUT 1000
  157. /**
  158. * Length of secret key in bytes -- 256-bit for Salsa20
  159. */
  160. #define ZT_PEER_SECRET_KEY_LENGTH 32
  161. /**
  162. * How often Topology::clean() and Network::clean() and similar are called, in ms
  163. */
  164. #define ZT_HOUSEKEEPING_PERIOD 120000
  165. /**
  166. * Overriding granularity for timer tasks to prevent CPU-intensive thrashing on every packet
  167. */
  168. #define ZT_CORE_TIMER_TASK_GRANULARITY 1000
  169. /**
  170. * How long to remember peer records in RAM if they haven't been used
  171. */
  172. #define ZT_PEER_IN_MEMORY_EXPIRATION 600000
  173. /**
  174. * Delay between WHOIS retries in ms
  175. */
  176. #define ZT_WHOIS_RETRY_DELAY 500
  177. /**
  178. * Maximum identity WHOIS retries (each attempt tries consulting a different peer)
  179. */
  180. #define ZT_MAX_WHOIS_RETRIES 3
  181. /**
  182. * Transmit queue entry timeout
  183. */
  184. #define ZT_TRANSMIT_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  185. /**
  186. * Receive queue entry timeout
  187. */
  188. #define ZT_RECEIVE_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  189. /**
  190. * Maximum number of ZT hops allowed (this is not IP hops/TTL)
  191. *
  192. * The protocol allows up to 7, but we limit it to something smaller.
  193. */
  194. #define ZT_RELAY_MAX_HOPS 3
  195. /**
  196. * Expire time for multicast 'likes' and indirect multicast memberships in ms
  197. */
  198. #define ZT_MULTICAST_LIKE_EXPIRE 600000
  199. /**
  200. * Delay between explicit MULTICAST_GATHER requests for a given multicast channel
  201. */
  202. #define ZT_MULTICAST_EXPLICIT_GATHER_DELAY (ZT_MULTICAST_LIKE_EXPIRE / 10)
  203. /**
  204. * Timeout for outgoing multicasts
  205. *
  206. * This is how long we wait for explicit or implicit gather results.
  207. */
  208. #define ZT_MULTICAST_TRANSMIT_TIMEOUT 5000
  209. /**
  210. * Default maximum number of peers to address with a single multicast (if unspecified in network config)
  211. */
  212. #define ZT_MULTICAST_DEFAULT_LIMIT 32
  213. /**
  214. * How frequently to send a zero-byte UDP keepalive packet
  215. *
  216. * There are NATs with timeouts as short as 30 seconds, so this turns out
  217. * to be needed.
  218. */
  219. #define ZT_NAT_KEEPALIVE_DELAY 25000
  220. /**
  221. * Delay between scans of the topology active peer DB for peers that need ping
  222. *
  223. * This is also how often pings will be retried to upstream peers (rootservers)
  224. * constantly until something is heard.
  225. */
  226. #define ZT_PING_CHECK_INVERVAL 6250
  227. /**
  228. * Delay between ordinary case pings of direct links
  229. */
  230. #define ZT_PEER_DIRECT_PING_DELAY 120000
  231. /**
  232. * Delay between requests for updated network autoconf information
  233. */
  234. #define ZT_NETWORK_AUTOCONF_DELAY 60000
  235. /**
  236. * Timeout for overall peer activity (measured from last receive)
  237. */
  238. #define ZT_PEER_ACTIVITY_TIMEOUT (ZT_PEER_DIRECT_PING_DELAY + (ZT_PING_CHECK_INVERVAL * 3))
  239. /**
  240. * Stop relaying via peers that have not responded to direct sends
  241. *
  242. * When we send something (including frames), we generally expect a response.
  243. * Switching relays if no response in a short period of time causes more
  244. * rapid failover if a rootserver goes down or becomes unreachable. In the
  245. * mistaken case, little harm is done as it'll pick the next-fastest
  246. * rootserver and will switch back eventually.
  247. */
  248. #define ZT_PEER_RELAY_CONVERSATION_LATENCY_THRESHOLD 10000
  249. /**
  250. * Minimum interval between attempts by relays to unite peers
  251. *
  252. * When a relay gets a packet destined for another peer, it sends both peers
  253. * a RENDEZVOUS message no more than this often. This instructs the peers
  254. * to attempt NAT-t and gives each the other's corresponding IP:port pair.
  255. */
  256. #define ZT_MIN_UNITE_INTERVAL 60000
  257. /**
  258. * Delay between initial direct NAT-t packet and more aggressive techniques
  259. */
  260. #define ZT_NAT_T_TACTICAL_ESCALATION_DELAY 1000
  261. /**
  262. * Size of anti-recursion history (see AntiRecursion.hpp)
  263. */
  264. #define ZT_ANTIRECURSION_HISTORY_SIZE 16
  265. /**
  266. * How often to send LAN beacons
  267. */
  268. #define ZT_BEACON_INTERVAL 30000
  269. /**
  270. * Do not respond to any beacon more often than this
  271. */
  272. #define ZT_MIN_BEACON_RESPONSE_INTERVAL 2500
  273. /**
  274. * Minimum delay between attempts to confirm new paths to peers (to avoid HELLO flooding)
  275. */
  276. #define ZT_MIN_PATH_CONFIRMATION_INTERVAL 5000
  277. /**
  278. * Sanity limit on maximum bridge routes
  279. *
  280. * If the number of bridge routes exceeds this, we cull routes from the
  281. * bridges with the most MACs behind them until it doesn't. This is a
  282. * sanity limit to prevent memory-filling DOS attacks, nothing more. No
  283. * physical LAN has anywhere even close to this many nodes. Note that this
  284. * does not limit the size of ZT virtual LANs, only bridge routing.
  285. */
  286. #define ZT_MAX_BRIDGE_ROUTES 67108864
  287. /**
  288. * If there is no known route, spam to up to this many active bridges
  289. */
  290. #define ZT_MAX_BRIDGE_SPAM 16
  291. /**
  292. * A test pseudo-network-ID that can be joined
  293. *
  294. * Joining this network ID will result in a network with no IP addressing
  295. * and default parameters. No network configuration master will be consulted
  296. * and instead a static config will be used. This is used in built-in testnet
  297. * scenarios and can also be used for external testing.
  298. *
  299. * This is an impossible real network ID since 0xff is a reserved address
  300. * prefix.
  301. */
  302. #define ZT_TEST_NETWORK_ID 0xffffffffffffffffULL
  303. #endif