Constants.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. /*
  2. * ZeroTier One - Global Peer to Peer Ethernet
  3. * Copyright (C) 2012-2013 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. // __UNIX_LIKE__ - any "unix like" OS (BSD, posix, etc.)
  36. // __WINDOWS__
  37. //
  38. // Also makes sure __BYTE_ORDER is defined reasonably.
  39. //
  40. // Canonicalize Linux... is this necessary? Do it anyway to be defensive.
  41. #if defined(__linux__) || defined(linux) || defined(__LINUX__) || defined(__linux)
  42. #ifndef __LINUX__
  43. #define __LINUX__
  44. #ifndef __UNIX_LIKE__
  45. #define __UNIX_LIKE__
  46. #endif
  47. #endif
  48. #endif
  49. // TODO: Android is what? Linux technically, but does it define it?
  50. // OSX and iOS are unix-like OSes far as we're concerned
  51. #ifdef __APPLE__
  52. #ifndef __UNIX_LIKE__
  53. #define __UNIX_LIKE__
  54. #endif
  55. #endif
  56. // Linux has endian.h
  57. #ifdef __LINUX__
  58. #include <endian.h>
  59. #endif
  60. #if defined(_WIN32) || defined(_WIN64)
  61. #ifndef __WINDOWS__
  62. #define __WINDOWS__
  63. #endif
  64. #define NOMINMAX
  65. #pragma warning(disable : 4290)
  66. #pragma warning(disable : 4996)
  67. #undef __UNIX_LIKE__
  68. #define ZT_PATH_SEPARATOR '\\'
  69. #define ZT_PATH_SEPARATOR_S "\\"
  70. #define ZT_EOL_S "\r\n"
  71. #endif
  72. // Assume these are little-endian. PPC is not supported for OSX, and ARM
  73. // runs in little-endian mode for these OS families.
  74. #if defined(__APPLE__) || defined(__WINDOWS__)
  75. #undef __BYTE_ORDER
  76. #undef __LITTLE_ENDIAN
  77. #undef __BIG_ENDIAN
  78. #define __BIG_ENDIAN 4321
  79. #define __LITTLE_ENDIAN 1234
  80. #define __BYTE_ORDER 1234
  81. #endif
  82. #ifdef __UNIX_LIKE__
  83. #define ZT_PATH_SEPARATOR '/'
  84. #define ZT_PATH_SEPARATOR_S "/"
  85. #define ZT_EOL_S "\n"
  86. #endif
  87. // Error out if required symbols are missing
  88. #ifndef __BYTE_ORDER
  89. error_no_byte_order_defined;
  90. #endif
  91. #ifndef ZT_OSNAME
  92. #ifdef __WINDOWS__
  93. #define ZT_OSNAME "windows"
  94. #else
  95. no ZT_OSNAME defined;
  96. #endif
  97. #endif
  98. #ifndef ZT_ARCH
  99. #ifdef __WINDOWS__
  100. #ifdef _WIN64
  101. #define ZT_ARCH "x64"
  102. #else
  103. #define ZT_ARCH "x86"
  104. #endif
  105. #else
  106. error_no_ZT_ARCH_defined;
  107. #endif
  108. #endif
  109. /**
  110. * Length of a ZeroTier address in bytes
  111. */
  112. #define ZT_ADDRESS_LENGTH 5
  113. /**
  114. * Addresses beginning with this byte are reserved for the joy of in-band signaling
  115. */
  116. #define ZT_ADDRESS_RESERVED_PREFIX 0xff
  117. /**
  118. * Default local UDP port
  119. */
  120. #define ZT_DEFAULT_UDP_PORT 8993
  121. /**
  122. * Local control port, also used for multiple invocation check
  123. */
  124. #define ZT_CONTROL_UDP_PORT 39393
  125. /**
  126. * Default payload MTU for UDP packets
  127. *
  128. * In the future we might support UDP path MTU discovery, but for now we
  129. * set a maximum that is equal to 1500 minus 8 (for PPPoE overhead, common
  130. * in some markets) minus 48 (IPv6 UDP overhead).
  131. */
  132. #define ZT_UDP_DEFAULT_PAYLOAD_MTU 1444
  133. /**
  134. * MTU used for Ethernet tap device
  135. *
  136. * This is pretty much an unchangeable global constant. To make it change
  137. * across nodes would require logic to send ICMP packet too big messages,
  138. * which would complicate things. 1500 has been good enough on most LANs
  139. * for ages, so a larger MTU should be fine for the forseeable future. This
  140. * typically results in two UDP packets per single large frame. Experimental
  141. * results seem to show that this is good. Larger MTUs resulting in more
  142. * fragments seemed too brittle on slow/crummy links for no benefit.
  143. *
  144. * If this does change, also change it in tap.h in the tuntaposx code under
  145. * mac-tap.
  146. *
  147. * Overhead for a normal frame split into two packets:
  148. *
  149. * 1414 = 1444 (typical UDP MTU) - 28 (packet header) - 2 (ethertype)
  150. * 1428 = 1444 (typical UDP MTU) - 16 (fragment header)
  151. * SUM: 2842
  152. *
  153. * We use 2800, which leaves some room for other payload in other types of
  154. * messages such as multicast propagation or future support for bridging.
  155. */
  156. #define ZT_IF_MTU 2800
  157. /**
  158. * Maximum number of packet fragments we'll support
  159. *
  160. * The actual spec allows 16, but this is the most we'll support right
  161. * now. Packets with more than this many fragments are dropped.
  162. */
  163. #define ZT_MAX_PACKET_FRAGMENTS 3
  164. /**
  165. * Timeout for receipt of fragmented packets in ms
  166. *
  167. * Since there's no retransmits, this is just a really bad case scenario for
  168. * transit time. It's short enough that a DOS attack from exhausing buffers is
  169. * very unlikely, as the transfer rate would have to be fast enough to fill
  170. * system memory in this time.
  171. */
  172. #define ZT_FRAGMENTED_PACKET_RECEIVE_TIMEOUT 1500
  173. /**
  174. * First byte of MAC addresses derived from ZeroTier addresses
  175. *
  176. * This has the 0x02 bit set, which indicates a locally administrered
  177. * MAC address rather than one with a known HW ID.
  178. */
  179. #define ZT_MAC_FIRST_OCTET 0x32
  180. /**
  181. * How often Topology::clean() and Network::clean() are called in ms
  182. */
  183. #define ZT_DB_CLEAN_PERIOD 300000
  184. /**
  185. * Delay between WHOIS retries in ms
  186. */
  187. #define ZT_WHOIS_RETRY_DELAY 500
  188. /**
  189. * Maximum identity WHOIS retries
  190. */
  191. #define ZT_MAX_WHOIS_RETRIES 3
  192. /**
  193. * Transmit queue entry timeout
  194. */
  195. #define ZT_TRANSMIT_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  196. /**
  197. * Receive queue entry timeout
  198. */
  199. #define ZT_RECEIVE_QUEUE_TIMEOUT (ZT_WHOIS_RETRY_DELAY * (ZT_MAX_WHOIS_RETRIES + 1))
  200. /**
  201. * Maximum number of ZT hops allowed
  202. *
  203. * The protocol allows up to 7, but we limit it to something smaller.
  204. */
  205. #define ZT_RELAY_MAX_HOPS 3
  206. /**
  207. * Breadth of tree for rumor mill multicast propagation
  208. */
  209. #define ZT_MULTICAST_PROPAGATION_BREADTH 4
  210. /**
  211. * Depth of tree for rumor mill multicast propagation
  212. *
  213. * The maximum number of peers who can receive a multicast is equal to
  214. * the sum of BREADTH^i where I is from 1 to DEPTH. This ignores the effect
  215. * of the rate limiting algorithm or bloom filter collisions.
  216. *
  217. * 5 results in a max of 1364 recipients for a given multicast. With a limit
  218. * of 50 bytes/sec (average) for multicast, this results in a worst case of
  219. * around 68kb/sec of multicast traffic. FYI the average multicast traffic
  220. * from a Mac seems to be about ~25bytes/sec. Windows measurements are TBD.
  221. * Linux is quieter than Mac.
  222. *
  223. * This are eventually going to become per-network tunable parameters, along
  224. * with per-network peer multicast rate limits.
  225. */
  226. #define ZT_MULTICAST_PROPAGATION_DEPTH 5
  227. /**
  228. * Length of ring buffer history of recent multicast packets
  229. */
  230. #define ZT_MULTICAST_DEDUP_HISTORY_LENGTH 1024
  231. /**
  232. * Expiration time in ms for multicast deduplication history items
  233. */
  234. #define ZT_MULTICAST_DEDUP_HISTORY_EXPIRE 4000
  235. /**
  236. * Period between announcements of all multicast 'likes' in ms
  237. *
  238. * Announcement occurs when a multicast group is locally joined, but all
  239. * memberships are periodically re-broadcast. If they're not they will
  240. * expire.
  241. */
  242. #define ZT_MULTICAST_LIKE_ANNOUNCE_ALL_PERIOD 120000
  243. /**
  244. * Expire time for multicast 'likes' in ms
  245. */
  246. #define ZT_MULTICAST_LIKE_EXPIRE ((ZT_MULTICAST_LIKE_ANNOUNCE_ALL_PERIOD * 2) + 1000)
  247. /**
  248. * Time between polls of local taps for multicast membership changes
  249. */
  250. #define ZT_MULTICAST_LOCAL_POLL_PERIOD 10000
  251. /**
  252. * Default bytes per second limit for multicasts per peer on a network
  253. */
  254. #define ZT_MULTICAST_DEFAULT_BYTES_PER_SECOND 100.0
  255. /**
  256. * Default balance preload for multicast rate limiters on a network
  257. */
  258. #define ZT_MULTICAST_DEFAULT_RATE_PRELOAD 25000.0
  259. /**
  260. * Default maximum balance for multicast rate limiters
  261. */
  262. #define ZT_MULTICAST_DEFAULT_RATE_MAX_BALANCE 25000.0
  263. /**
  264. * Default minimum balance for multicast rate limiters (max debt)
  265. */
  266. #define ZT_MULTICAST_DEFAULT_RATE_MIN_BALANCE -5000.0
  267. /**
  268. * Delay between scans of the topology active peer DB for peers that need ping
  269. */
  270. #define ZT_PING_CHECK_DELAY 7000
  271. /**
  272. * Delay between checks of network configuration fingerprint
  273. */
  274. #define ZT_NETWORK_FINGERPRINT_CHECK_DELAY 5000
  275. /**
  276. * Delay between pings (actually HELLOs) to direct links
  277. */
  278. #define ZT_PEER_DIRECT_PING_DELAY 120000
  279. /**
  280. * Delay in ms between firewall opener packets to direct links
  281. *
  282. * This should be lower than the UDP conversation entry timeout in most
  283. * stateful firewalls.
  284. */
  285. #define ZT_FIREWALL_OPENER_DELAY 50000
  286. /**
  287. * Delay between requests for updated network autoconf information
  288. */
  289. #define ZT_NETWORK_AUTOCONF_DELAY 120000
  290. /**
  291. * Delay in core loop between checks of network autoconf newness
  292. */
  293. #define ZT_NETWORK_AUTOCONF_CHECK_DELAY 7000
  294. /**
  295. * Minimum delay in Node service loop
  296. *
  297. * This is the shortest of the check delays/periods.
  298. */
  299. #define ZT_MIN_SERVICE_LOOP_INTERVAL ZT_NETWORK_FINGERPRINT_CHECK_DELAY
  300. /**
  301. * Activity timeout for links
  302. *
  303. * A link that hasn't spoken in this long is simply considered inactive.
  304. */
  305. #define ZT_PEER_LINK_ACTIVITY_TIMEOUT ((ZT_PEER_DIRECT_PING_DELAY * 2) + 1000)
  306. /**
  307. * IP hops (a.k.a. TTL) to set for firewall opener packets
  308. *
  309. * 2 should permit traversal of double-NAT configurations, such as from inside
  310. * a VM running behind local NAT on a host that is itself behind NAT.
  311. */
  312. #define ZT_FIREWALL_OPENER_HOPS 2
  313. /**
  314. * Delay sleep overshoot for detection of a probable sleep/wake event
  315. */
  316. #define ZT_SLEEP_WAKE_DETECTION_THRESHOLD 2000
  317. /**
  318. * Time to pause main service loop after sleep/wake detect
  319. */
  320. #define ZT_SLEEP_WAKE_SETTLE_TIME 5000
  321. /**
  322. * Minimum interval between attempts by relays to unite peers
  323. */
  324. #define ZT_MIN_UNITE_INTERVAL 30000
  325. /**
  326. * Delay in milliseconds between firewall opener and real packet for NAT-t
  327. */
  328. #define ZT_RENDEZVOUS_NAT_T_DELAY 500
  329. #endif