node.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. /*
  2. * Copyright (c)2019 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: 2023-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. "net"
  16. "runtime"
  17. "sync"
  18. "sync/atomic"
  19. "unsafe"
  20. )
  21. //#cgo CFLAGS: -O3
  22. //#cgo LDFLAGS: ${SRCDIR}/../../../build/node/libzt_core.a ${SRCDIR}/../../../build/go/native/libzt_go_native.a -lc++
  23. //#define ZT_CGO 1
  24. //#include <stdint.h>
  25. //#include "../../native/GoGlue.h"
  26. //#if __has_include("../../../version.h")
  27. //#include "../../../version.h"
  28. //#else
  29. //#define ZEROTIER_ONE_VERSION_MAJOR 255
  30. //#define ZEROTIER_ONE_VERSION_MINOR 255
  31. //#define ZEROTIER_ONE_VERSION_REVISION 255
  32. //#define ZEROTIER_ONE_VERSION_BUILD 255
  33. //#endif
  34. import "C"
  35. const (
  36. // CoreVersionMajor is the major version of the ZeroTier core
  37. CoreVersionMajor int = C.ZEROTIER_ONE_VERSION_MAJOR
  38. // CoreVersionMinor is the minor version of the ZeroTier core
  39. CoreVersionMinor int = C.ZEROTIER_ONE_VERSION_MINOR
  40. // CoreVersionRevision is the revision of the ZeroTier core
  41. CoreVersionRevision int = C.ZEROTIER_ONE_VERSION_REVISION
  42. // CoreVersionBuild is the build version of the ZeroTier core
  43. CoreVersionBuild int = C.ZEROTIER_ONE_VERSION_BUILD
  44. )
  45. // Node is an instance of a ZeroTier node
  46. type Node struct {
  47. gn *C.ZT_GoNode
  48. zn *C.ZT_Node
  49. networks map[uint64]*Network
  50. networksLock sync.RWMutex
  51. online uint32
  52. running uint32
  53. }
  54. // NewNode creates and initializes a new instance of the ZeroTier node service
  55. func NewNode() *Node {
  56. n := new(Node)
  57. n.networks = make(map[uint64]*Network)
  58. gnRawAddr := uintptr(unsafe.Pointer(n.gn))
  59. nodesByUserPtrLock.Lock()
  60. nodesByUserPtr[gnRawAddr] = n
  61. nodesByUserPtrLock.Unlock()
  62. runtime.SetFinalizer(n, func(obj interface{}) { // make sure this always happens
  63. nodesByUserPtrLock.Lock()
  64. delete(nodesByUserPtr, gnRawAddr)
  65. nodesByUserPtrLock.Unlock()
  66. })
  67. n.running = 1
  68. return n
  69. }
  70. // Close closes this Node and frees its underlying C++ Node structures
  71. func (n *Node) Close() {
  72. if atomic.SwapUint32(&n.running, 0) != 0 {
  73. C.ZT_GoNode_delete(n.gn)
  74. nodesByUserPtrLock.Lock()
  75. delete(nodesByUserPtr, uintptr(unsafe.Pointer(n.gn)))
  76. nodesByUserPtrLock.Unlock()
  77. }
  78. }
  79. func (n *Node) pathCheck(ztAddress uint64, af int, ip net.IP, port int) bool {
  80. return true
  81. }
  82. func (n *Node) pathLookup(ztAddress uint64) (net.IP, int) {
  83. return nil, 0
  84. }
  85. func (n *Node) stateObjectPut(objType int, id [2]uint64, data []byte) {
  86. }
  87. func (n *Node) stateObjectDelete(objType int, id [2]uint64) {
  88. }
  89. func (n *Node) stateObjectGet(objType int, id [2]uint64) ([]byte, bool) {
  90. return nil, false
  91. }
  92. func (n *Node) handleTrace(traceMessage string) {
  93. }
  94. func (n *Node) handleUserMessage(originAddress, messageTypeID uint64, data []byte) {
  95. }
  96. func (n *Node) handleRemoteTrace(originAddress uint64, dictData []byte) {
  97. }
  98. func (n *Node) handleNetworkConfigUpdate(op int, config *C.ZT_VirtualNetworkConfig) int {
  99. return 0
  100. }