Constants.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2011-2014 ZeroTier Networks LLC
  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. //
  30. // This include file also auto-detects and canonicalizes some environment
  31. // information defines:
  32. //
  33. // __LINUX__
  34. // __APPLE__
  35. // __BSD__ (OSX also defines this)
  36. // __UNIX_LIKE__ (Linux, BSD, etc.)
  37. // __WINDOWS__
  38. //
  39. // Also makes sure __BYTE_ORDER is defined reasonably.
  40. //
  41. // Hack: make sure __GCC__ is defined on old GCC compilers
  42. #ifndef __GCC__
  43. #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)
  44. #define __GCC__
  45. #endif
  46. #endif
  47. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  48. #ifndef __LINUX__
  49. #define __LINUX__
  50. #endif
  51. #ifndef __UNIX_LIKE__
  52. #define __UNIX_LIKE__
  53. #endif
  54. #include <endian.h>
  55. #endif
  56. // TODO: Android is what? Linux technically, but does it define it?
  57. #ifdef __APPLE__
  58. #include <TargetConditionals.h>
  59. #ifndef __UNIX_LIKE__
  60. #define __UNIX_LIKE__
  61. #endif
  62. #ifndef __BSD__
  63. #define __BSD__
  64. #endif
  65. #endif
  66. #if defined(_WIN32) || defined(_WIN64)
  67. #ifndef __WINDOWS__
  68. #define __WINDOWS__
  69. #endif
  70. #define NOMINMAX
  71. #pragma warning(disable : 4290)
  72. #pragma warning(disable : 4996)
  73. #pragma warning(disable : 4101)
  74. #undef __UNIX_LIKE__
  75. #undef __BSD__
  76. #define ZT_PATH_SEPARATOR '\\'
  77. #define ZT_PATH_SEPARATOR_S "\\"
  78. #define ZT_EOL_S "\r\n"
  79. #include <WinSock2.h>
  80. #include <Windows.h>
  81. #endif
  82. // Assume these are little-endian. PPC is not supported for OSX, and ARM
  83. // runs in little-endian mode for these OS families.
  84. #if defined(__APPLE__) || defined(__WINDOWS__)
  85. #undef __BYTE_ORDER
  86. #undef __LITTLE_ENDIAN
  87. #undef __BIG_ENDIAN
  88. #define __BIG_ENDIAN 4321
  89. #define __LITTLE_ENDIAN 1234
  90. #define __BYTE_ORDER 1234
  91. #endif
  92. #ifdef __UNIX_LIKE__
  93. #define ZT_PATH_SEPARATOR '/'
  94. #define ZT_PATH_SEPARATOR_S "/"
  95. #define ZT_EOL_S "\n"
  96. #endif
  97. #ifndef __BYTE_ORDER
  98. #include <endian.h>
  99. #endif
  100. /**
  101. * Length of a ZeroTier address in bytes
  102. */
  103. #define ZT_ADDRESS_LENGTH 5
  104. /**
  105. * Length of a hexadecimal ZeroTier address
  106. */
  107. #define ZT_ADDRESS_LENGTH_HEX 10
  108. /**
  109. * Addresses beginning with this byte are reserved for the joy of in-band signaling
  110. */
  111. #define ZT_ADDRESS_RESERVED_PREFIX 0xff
  112. /**
  113. * Default local port for ZeroTier UDP traffic
  114. */
  115. #define ZT_DEFAULT_UDP_PORT 9993
  116. /**
  117. * Default payload MTU for UDP packets
  118. *
  119. * In the future we might support UDP path MTU discovery, but for now we
  120. * set a maximum that is equal to 1500 minus 8 (for PPPoE overhead, common
  121. * in some markets) minus 48 (IPv6 UDP overhead).
  122. */
  123. #define ZT_UDP_DEFAULT_PAYLOAD_MTU 1444
  124. /**
  125. * Default MTU used for Ethernet tap device
  126. *
  127. * This is pretty much an unchangeable global constant. To make it change
  128. * across nodes would require logic to send ICMP packet too big messages,
  129. * which would complicate things. 1500 has been good enough on most LANs
  130. * for ages, so a larger MTU should be fine for the forseeable future. This
  131. * typically results in two UDP packets per single large frame. Experimental
  132. * results seem to show that this is good. Larger MTUs resulting in more
  133. * fragments seemed too brittle on slow/crummy links for no benefit.
  134. *
  135. * If this does change, also change it in tap.h in the tuntaposx code under
  136. * mac-tap.
  137. *
  138. * Overhead for a normal frame split into two packets:
  139. *
  140. * 1414 = 1444 (typical UDP MTU) - 28 (packet header) - 2 (ethertype)
  141. * 1428 = 1444 (typical UDP MTU) - 16 (fragment header)
  142. * SUM: 2842
  143. *
  144. * We use 2800, which leaves some room for other payload in other types of
  145. * messages such as multicast propagation or future support for bridging.
  146. */
  147. #define ZT_IF_MTU 2800
  148. /**
  149. * Default interface metric for ZeroTier taps -- should be higher than physical ports
  150. */
  151. #define ZT_DEFAULT_IF_METRIC 32768
  152. /**
  153. * Maximum number of packet fragments we'll support
  154. *
  155. * The actual spec allows 16, but this is the most we'll support right
  156. * now. Packets with more than this many fragments are dropped.
  157. */
  158. #define ZT_MAX_PACKET_FRAGMENTS 4
  159. /**
  160. * Timeout for receipt of fragmented packets in ms
  161. *
  162. * Since there's no retransmits, this is just a really bad case scenario for
  163. * transit time. It's short enough that a DOS attack from exhausing buffers is
  164. * very unlikely, as the transfer rate would have to be fast enough to fill
  165. * system memory in this time.
  166. */
  167. #define ZT_FRAGMENTED_PACKET_RECEIVE_TIMEOUT 1000
  168. /**
  169. * Length of secret key in bytes -- 256-bit for Salsa20
  170. */
  171. #define ZT_PEER_SECRET_KEY_LENGTH 32
  172. /**
  173. * How often Topology::clean() and Network::clean() and similar are called, in ms
  174. */
  175. #define ZT_DB_CLEAN_PERIOD 120000
  176. /**
  177. * How long to remember peer records in RAM if they haven't been used
  178. */
  179. #define ZT_PEER_IN_MEMORY_EXPIRATION 600000
  180. /**
  181. * Delay between WHOIS retries in ms
  182. */
  183. #define ZT_WHOIS_RETRY_DELAY 500
  184. /**
  185. * Maximum identity WHOIS retries (each attempt tries consulting a different peer)
  186. */
  187. #define ZT_MAX_WHOIS_RETRIES 3
  188. /**
  189. * Transmit queue entry timeout
  190. */
  191. #define ZT_TRANSMIT_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  192. /**
  193. * Receive queue entry timeout
  194. */
  195. #define ZT_RECEIVE_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  196. /**
  197. * Maximum number of ZT hops allowed (this is not IP hops/TTL)
  198. *
  199. * The protocol allows up to 7, but we limit it to something smaller.
  200. */
  201. #define ZT_RELAY_MAX_HOPS 3
  202. /**
  203. * Expire time for multicast 'likes' and indirect multicast memberships in ms
  204. */
  205. #define ZT_MULTICAST_LIKE_EXPIRE 600000
  206. /**
  207. * Time between polls of local tap devices for multicast membership changes
  208. */
  209. #define ZT_MULTICAST_LOCAL_POLL_PERIOD 10000
  210. /**
  211. * Delay between explicit MULTICAST_GATHER requests for a given multicast channel
  212. */
  213. #define ZT_MULTICAST_EXPLICIT_GATHER_DELAY (ZT_MULTICAST_LIKE_EXPIRE / 10)
  214. /**
  215. * Minimum delay between implicit gathers via MULTICAST_FRAME
  216. */
  217. #define ZT_MULTICAST_IMPLICIT_GATHER_DELAY 30000
  218. /**
  219. * Timeout for outgoing multicasts
  220. *
  221. * Attempts will be made to gather recipients and send until we reach
  222. * the limit or sending times out.
  223. */
  224. #define ZT_MULTICAST_TRANSMIT_TIMEOUT 5000
  225. /**
  226. * Default maximum number of peers to address with a single multicast (if unspecified in network)
  227. */
  228. #define ZT_MULTICAST_DEFAULT_LIMIT 128
  229. /**
  230. * Delay between scans of the topology active peer DB for peers that need ping
  231. */
  232. #define ZT_PING_CHECK_DELAY 10000
  233. /**
  234. * Delay between checks of network configuration fingerprint
  235. */
  236. #define ZT_NETWORK_FINGERPRINT_CHECK_DELAY 5000
  237. /**
  238. * Delay between ordinary case pings of direct links
  239. */
  240. #define ZT_PEER_DIRECT_PING_DELAY 120000
  241. /**
  242. * Delay between requests for updated network autoconf information
  243. */
  244. #define ZT_NETWORK_AUTOCONF_DELAY 60000
  245. /**
  246. * Delay in core loop between checks of network autoconf newness
  247. */
  248. #define ZT_NETWORK_AUTOCONF_CHECK_DELAY 10000
  249. /**
  250. * Time since a ping was sent to be considered unanswered
  251. */
  252. #define ZT_PING_UNANSWERED_AFTER 1500
  253. /**
  254. * Try to ping supernodes this often until we get something from them
  255. */
  256. #define ZT_STARTUP_AGGRO (ZT_PING_UNANSWERED_AFTER * 2)
  257. /**
  258. * Maximum delay between runs of the main loop in Node.cpp
  259. */
  260. #define ZT_MAX_SERVICE_LOOP_INTERVAL ZT_STARTUP_AGGRO
  261. /**
  262. * Try TCP tunnels if nothing received for this long
  263. */
  264. #define ZT_TCP_TUNNEL_FAILOVER_TIMEOUT (ZT_STARTUP_AGGRO * 5)
  265. /**
  266. * Timeout for overall peer activity (measured from last receive)
  267. */
  268. #define ZT_PEER_ACTIVITY_TIMEOUT ((ZT_PEER_DIRECT_PING_DELAY * 2) + ZT_PING_CHECK_DELAY)
  269. /**
  270. * Path activity timeout (for non-fixed paths)
  271. */
  272. #define ZT_PEER_PATH_ACTIVITY_TIMEOUT ZT_PEER_ACTIVITY_TIMEOUT
  273. /**
  274. * Close TCP sockets if unused for this long (SocketManager)
  275. */
  276. #define ZT_TCP_TUNNEL_ACTIVITY_TIMEOUT ZT_PEER_ACTIVITY_TIMEOUT
  277. /**
  278. * Stop relaying via peers that have not responded to direct sends
  279. *
  280. * When we send something (including frames), we generally expect a response.
  281. * Switching relays if no response in a short period of time causes more
  282. * rapid failover if a supernode goes down or becomes unreachable. In the
  283. * mistaken case, little harm is done as it'll pick the next-fastest
  284. * supernode and will switch back eventually.
  285. */
  286. #define ZT_PEER_RELAY_CONVERSATION_LATENCY_THRESHOLD 10000
  287. /**
  288. * Delay sleep overshoot for detection of a probable sleep/wake event
  289. */
  290. #define ZT_SLEEP_WAKE_DETECTION_THRESHOLD 5000
  291. /**
  292. * Time to pause main service loop after sleep/wake detect
  293. */
  294. #define ZT_SLEEP_WAKE_SETTLE_TIME 5000
  295. /**
  296. * Minimum interval between attempts by relays to unite peers
  297. *
  298. * When a relay gets a packet destined for another peer, it sends both peers
  299. * a RENDEZVOUS message no more than this often. This instructs the peers
  300. * to attempt NAT-t and gives each the other's corresponding IP:port pair.
  301. */
  302. #define ZT_MIN_UNITE_INTERVAL 30000
  303. /**
  304. * Delay between initial direct NAT-t packet and more aggressive techniques
  305. */
  306. #define ZT_NAT_T_TACTICAL_ESCALATION_DELAY 2000
  307. /**
  308. * Size of anti-recursion history (see AntiRecursion.hpp)
  309. */
  310. #define ZT_ANTIRECURSION_HISTORY_SIZE 16
  311. /**
  312. * TTL for certificates of membership on private networks
  313. *
  314. * This is the max delta for the timestamp field of a COM, so it's a window
  315. * plus or minus the certificate's timestamp. In milliseconds.
  316. */
  317. #define ZT_NETWORK_CERTIFICATE_TTL_WINDOW (ZT_NETWORK_AUTOCONF_DELAY * 4)
  318. /**
  319. * How often to broadcast beacons over physical local LANs
  320. */
  321. #define ZT_BEACON_INTERVAL 30000
  322. /**
  323. * Do not respond to any beacon more often than this
  324. */
  325. #define ZT_MIN_BEACON_RESPONSE_INTERVAL (ZT_BEACON_INTERVAL / 32)
  326. /**
  327. * Minimum interval between attempts to do a software update
  328. */
  329. #define ZT_UPDATE_MIN_INTERVAL 120000
  330. /**
  331. * Maximum interval between checks for new versions
  332. */
  333. #define ZT_UPDATE_MAX_INTERVAL 7200000
  334. /**
  335. * Software update HTTP timeout in seconds
  336. */
  337. #define ZT_UPDATE_HTTP_TIMEOUT 120
  338. /**
  339. * Delay between fetches of the root topology update URL
  340. *
  341. * 86400000 = check once every 24 hours (this doesn't change often)
  342. */
  343. #define ZT_UPDATE_ROOT_TOPOLOGY_CHECK_INTERVAL 86400000
  344. /**
  345. * Sanity limit on maximum bridge routes
  346. *
  347. * If the number of bridge routes exceeds this, we cull routes from the
  348. * bridges with the most MACs behind them until it doesn't. This is a
  349. * sanity limit to prevent memory-filling DOS attacks, nothing more. No
  350. * physical LAN has anywhere even close to this many nodes. Note that this
  351. * does not limit the size of ZT virtual LANs, only bridge routing.
  352. */
  353. #define ZT_MAX_BRIDGE_ROUTES 67108864
  354. /**
  355. * If there is no known route, spam to up to this many active bridges
  356. */
  357. #define ZT_MAX_BRIDGE_SPAM 16
  358. /**
  359. * Timeout for IPC connections (e.g. unix domain sockets) in seconds
  360. */
  361. #define ZT_IPC_TIMEOUT 600
  362. /**
  363. * A test pseudo-network-ID that can be joined
  364. *
  365. * Joining this network ID will result in a network with no IP addressing
  366. * and default parameters. No network configuration master will be consulted
  367. * and instead a static config will be used. This is used in built-in testnet
  368. * scenarios and can also be used for external testing.
  369. *
  370. * This is an impossible real network ID since 0xff is a reserved address
  371. * prefix.
  372. */
  373. #define ZT_TEST_NETWORK_ID 0xffffffffffffffffULL
  374. #endif