misc.go 6.7 KB

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