misc.go 7.7 KB

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