misc.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c)2013-2020 ZeroTier, Inc.
  3. *
  4. * Use of this software is governed by the Business Source License included
  5. * in the LICENSE.TXT file in the project's root directory.
  6. *
  7. * Change Date: 2024-01-01
  8. *
  9. * On the date above, in accordance with the Business Source License, use
  10. * of this software will be governed by version 2.0 of the Apache License.
  11. */
  12. /****/
  13. package zerotier
  14. import (
  15. "encoding/base32"
  16. "encoding/binary"
  17. "math/rand"
  18. "net"
  19. "sync"
  20. "time"
  21. "unsafe"
  22. )
  23. // LogoChar is the unicode character that is ZeroTier's logo
  24. const LogoChar = "⏁"
  25. // Base32StdLowerCase is a base32 encoder/decoder using a lower-case standard alphabet and no padding.
  26. var Base32StdLowerCase = base32.NewEncoding("abcdefghijklmnopqrstuvwxyz234567").WithPadding(base32.NoPadding)
  27. // unassignedPrivilegedPorts are ports below 1024 that do not appear to be assigned by IANA.
  28. // The new 2.0+ ZeroTier default is 793, which we will eventually seek to have assigned. These
  29. // are searched as backups if this port is already in use on a system.
  30. var unassignedPrivilegedPorts = []int{
  31. 4,
  32. 6,
  33. 8,
  34. 10,
  35. 12,
  36. 14,
  37. 15,
  38. 16,
  39. 26,
  40. 28,
  41. 30,
  42. 32,
  43. 34,
  44. 36,
  45. 40,
  46. 60,
  47. 269, 270, 271, 272, 273, 274, 275, 276, 277, 278, 279,
  48. 285,
  49. 288, 289, 290, 291, 292, 293, 294, 295, 296, 297, 298, 299, 300, 301, 302, 303, 304, 305, 306, 307,
  50. 323, 324, 325, 326, 327, 328, 329, 330, 331, 332,
  51. 334, 335, 336, 337, 338, 339, 340, 341, 342, 343,
  52. 703,
  53. 708,
  54. 713, 714, 715, 716, 717, 718, 719, 720, 721, 722, 723, 724, 725, 726, 727, 728,
  55. 732, 733, 734, 735, 736, 737, 738, 739, 740,
  56. 743,
  57. 745, 746,
  58. 755, 756,
  59. 766,
  60. 768,
  61. 778, 779,
  62. 781, 782, 783, 784, 785, 786, 787, 788, 789, 790, 791, 792, 793, 794, 795, 796, 797, 798, 799,
  63. 802, 803, 804, 805, 806, 807, 808, 809,
  64. 811, 812, 813, 814, 815, 816, 817, 818, 819, 820, 821, 822, 823, 824, 825, 826, 827,
  65. 834, 835, 836, 837, 838, 839, 840, 841, 842, 843, 844, 845, 846,
  66. 849, 850, 851, 852, 853, 854, 855, 856, 857, 858, 859,
  67. 862, 863, 864, 865, 866, 867, 868, 869, 870, 871, 872,
  68. 874, 875, 876, 877, 878, 879, 880, 881, 882, 883, 884, 885,
  69. 889, 890, 891, 892, 893, 894, 895, 896, 897, 898, 899,
  70. 904, 905, 906, 907, 908, 909, 910, 911,
  71. 914, 915, 916, 917, 918, 919, 920, 921, 922, 923, 924, 925, 926, 927, 928, 929, 930, 931, 932, 933, 934, 935, 936, 937, 938, 939, 940, 941, 942, 943, 944, 945, 946, 947, 948, 949, 950, 951, 952, 953, 954, 955, 956, 957, 958, 959, 960, 961, 962, 963, 964, 965, 966, 967, 968, 969, 970, 971, 972, 973, 974, 975, 976, 977, 978, 979, 980, 981, 982, 983, 984, 985, 986, 987, 988,
  72. 1001, 1002, 1003, 1004, 1005, 1006, 1007, 1008, 1009,
  73. 1023,
  74. }
  75. var prng = rand.NewSource(time.Now().UnixNano())
  76. var prngLock sync.Mutex
  77. func randomUInt() uint {
  78. prngLock.Lock()
  79. i := prng.Int63()
  80. prngLock.Unlock()
  81. return uint(i)
  82. }
  83. // TimeMs returns the time in milliseconds since epoch.
  84. func TimeMs() int64 { return int64(time.Now().UnixNano()) / int64(1000000) }
  85. // ipNetToKey creates a key that can be used in a map[] from a net.IPNet
  86. func ipNetToKey(ipn *net.IPNet) (k [3]uint64) {
  87. copy(((*[16]byte)(unsafe.Pointer(&k[0])))[:], ipn.IP)
  88. ones, bits := ipn.Mask.Size()
  89. k[2] = (uint64(ones) << 32) | uint64(bits)
  90. return
  91. }
  92. func allZero(b []byte) bool {
  93. for _, bb := range b {
  94. if bb != 0 {
  95. return false
  96. }
  97. }
  98. return true
  99. }
  100. // checkPort does trial binding to a port using both UDP and TCP and returns false if any bindings fail.
  101. func checkPort(port int) bool {
  102. var ua net.UDPAddr
  103. ua.IP = net.IPv6zero
  104. ua.Port = port
  105. uc, err := net.ListenUDP("udp6", &ua)
  106. if uc != nil {
  107. _ = uc.Close()
  108. }
  109. if err != nil {
  110. return false
  111. }
  112. ua.IP = net.IPv4zero
  113. uc, err = net.ListenUDP("udp4", &ua)
  114. if uc != nil {
  115. _ = uc.Close()
  116. }
  117. if err != nil {
  118. return false
  119. }
  120. var ta net.TCPAddr
  121. ta.IP = net.IPv6zero
  122. ta.Port = port
  123. tc, err := net.ListenTCP("tcp6", &ta)
  124. if tc != nil {
  125. _ = tc.Close()
  126. }
  127. if err != nil {
  128. return false
  129. }
  130. ta.IP = net.IPv4zero
  131. tc, err = net.ListenTCP("tcp4", &ta)
  132. if tc != nil {
  133. _ = tc.Close()
  134. }
  135. if err != nil {
  136. return false
  137. }
  138. return true
  139. }
  140. // The ipClassify code below is based on and should produce identical results to
  141. // InetAddress::ipScope() in the C++ code.
  142. const (
  143. ipClassificationNone = -1
  144. ipClassificationLoopback = 0
  145. ipClassificationPseudoprivate = 1
  146. ipClassificationPrivate = 2
  147. ipClassificationLinkLocal = 3
  148. ipClassificationMulticast = 4
  149. ipClassificationGlobal = 5
  150. )
  151. var ipv4PseudoprivatePrefixes = []byte{
  152. 0x06, // 6.0.0.0/8 (US Army)
  153. 0x0b, // 11.0.0.0/8 (US DoD)
  154. 0x15, // 21.0.0.0/8 (US DDN-RVN)
  155. 0x16, // 22.0.0.0/8 (US DISA)
  156. 0x19, // 25.0.0.0/8 (UK Ministry of Defense)
  157. 0x1a, // 26.0.0.0/8 (US DISA)
  158. 0x1c, // 28.0.0.0/8 (US DSI-North)
  159. 0x1d, // 29.0.0.0/8 (US DISA)
  160. 0x1e, // 30.0.0.0/8 (US DISA)
  161. 0x33, // 51.0.0.0/8 (UK Department of Social Security)
  162. 0x37, // 55.0.0.0/8 (US DoD)
  163. 0x38, // 56.0.0.0/8 (US Postal Service)
  164. }
  165. // ipClassify determines the official or in a few cases unofficial role of an IP address
  166. func ipClassify(ip net.IP) int {
  167. if len(ip) == 16 {
  168. ip4 := ip.To4()
  169. if len(ip4) == 4 {
  170. ip = ip4
  171. }
  172. }
  173. if len(ip) == 4 {
  174. ip4FirstByte := ip[0]
  175. for _, b := range ipv4PseudoprivatePrefixes {
  176. if ip4FirstByte == b {
  177. return ipClassificationPseudoprivate
  178. }
  179. }
  180. ip4 := binary.BigEndian.Uint32(ip)
  181. switch ip4FirstByte {
  182. case 0x0a: // 10.0.0.0/8
  183. return ipClassificationPrivate
  184. case 0x64: // 100.64.0.0/10
  185. if (ip4 & 0xffc00000) == 0x64400000 {
  186. return ipClassificationPrivate
  187. }
  188. case 0x7f: // 127.0.0.1/8
  189. return ipClassificationLoopback
  190. case 0xa9: // 169.254.0.0/16
  191. if (ip4 & 0xffff0000) == 0xa9fe0000 {
  192. return ipClassificationLinkLocal
  193. }
  194. case 0xac: // 172.16.0.0/12
  195. if (ip4 & 0xfff00000) == 0xac100000 {
  196. return ipClassificationPrivate
  197. }
  198. case 0xc0: // 192.168.0.0/16
  199. if (ip4 & 0xffff0000) == 0xc0a80000 {
  200. return ipClassificationPrivate
  201. }
  202. }
  203. switch ip4 >> 28 {
  204. case 0xe: // 224.0.0.0/4
  205. return ipClassificationMulticast
  206. case 0xf: // 240.0.0.0/4 ("reserved," usually unusable)
  207. return ipClassificationNone
  208. }
  209. return ipClassificationGlobal
  210. }
  211. if len(ip) == 16 {
  212. if (ip[0] & 0xf0) == 0xf0 {
  213. if ip[0] == 0xff { // ff00::/8
  214. return ipClassificationMulticast
  215. }
  216. if ip[0] == 0xfe && (ip[1]&0xc0) == 0x80 {
  217. if allZero(ip[2:15]) {
  218. if ip[15] == 0x01 { // fe80::1/128
  219. return ipClassificationLoopback
  220. }
  221. return ipClassificationLinkLocal
  222. }
  223. }
  224. if (ip[0] & 0xfe) == 0xfc { // fc00::/7
  225. return ipClassificationPrivate
  226. }
  227. }
  228. if allZero(ip[0:15]) {
  229. if ip[15] == 0x01 { // ::1/128
  230. return ipClassificationLoopback
  231. }
  232. if ip[15] == 0x00 { // ::/128
  233. return ipClassificationNone
  234. }
  235. }
  236. return ipClassificationGlobal
  237. }
  238. return ipClassificationNone
  239. }