node.go 3.1 KB

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