Browse Source

Go! Go! https://www.youtube.com/watch?v=hyGYdqMfrQQ

Adam Ierymenko 5 years ago
parent
commit
fbf74d3baa

+ 12 - 0
go/pkg/zerotier/nativetap.go

@@ -38,6 +38,18 @@ type nativeTap struct {
 	multicastGroupHandlersLock sync.Mutex
 	multicastGroupHandlersLock sync.Mutex
 }
 }
 
 
+var _ Tap = &nativeTap{}
+
+// Type returns a human-readable description of this tap implementation
+func (t *nativeTap) Type() string {
+	return "native"
+}
+
+// Error gets this tap device's error status
+func (t *nativeTap) Error() (int, string) {
+	return 0, ""
+}
+
 // SetEnabled sets this tap's enabled state
 // SetEnabled sets this tap's enabled state
 func (t *nativeTap) SetEnabled(enabled bool) {
 func (t *nativeTap) SetEnabled(enabled bool) {
 	if enabled && atomic.SwapUint32(&t.enabled, 1) == 0 {
 	if enabled && atomic.SwapUint32(&t.enabled, 1) == 0 {

+ 20 - 10
go/pkg/zerotier/network.go

@@ -94,23 +94,29 @@ type NetworkConfig struct {
 	// MulticastSubscriptions are this device's current multicast subscriptions
 	// MulticastSubscriptions are this device's current multicast subscriptions
 	MulticastSubscriptions []MulticastGroup
 	MulticastSubscriptions []MulticastGroup
 
 
-	// PortDeviceType is a human-readable description of this port's implementation type or name
-	PortDeviceType string
+	// Enabled is true if this network's tap device is enabled
+	Enabled bool
 
 
-	// PortDeviceName is the OS-specific device name (e.g. tun0 or feth1856) for this network's virtual port
-	PortDeviceName string
+	// TapDeviceType is a human-readable description of this network's tap device type
+	TapDeviceType string
 
 
-	// PortErrorCode is an OS-specific error code returned by the virtual NIC driver
-	PortErrorCode int
+	// TapDevicePort is the OS-specific virtual device name (if applicable)
+	TapDevicePort string
+
+	// TapErrorCode is an implementation-specific error code from the tap device (0 for no error)
+	TapErrorCode int
+
+	// TapError is a human-readable description of this tap device's error state or an empty string if there is no error
+	TapError string
 }
 }
 
 
 // Network is a currently joined network
 // Network is a currently joined network
 type Network struct {
 type Network struct {
 	id         NetworkID
 	id         NetworkID
 	config     NetworkConfig
 	config     NetworkConfig
-	configLock sync.RWMutex
 	tap        Tap
 	tap        Tap
-	tapLock    sync.Mutex
+	configLock sync.RWMutex
+	tapLock    sync.RWMutex
 }
 }
 
 
 // ID gets this network's unique ID
 // ID gets this network's unique ID
@@ -119,13 +125,17 @@ func (n *Network) ID() NetworkID { return n.id }
 // Config returns a copy of this network's current configuration
 // Config returns a copy of this network's current configuration
 func (n *Network) Config() NetworkConfig {
 func (n *Network) Config() NetworkConfig {
 	n.configLock.RLock()
 	n.configLock.RLock()
+	n.tapLock.RLock()
+	defer n.tapLock.RUnlock()
 	defer n.configLock.RUnlock()
 	defer n.configLock.RUnlock()
+	n.config.Enabled = n.tap.Enabled()
+	n.config.TapErrorCode, n.config.TapError = n.tap.Error()
 	return n.config
 	return n.config
 }
 }
 
 
 // Tap gets this network's tap device
 // Tap gets this network's tap device
 func (n *Network) Tap() Tap {
 func (n *Network) Tap() Tap {
-	n.tapLock.Lock()
-	defer n.tapLock.Unlock()
+	n.tapLock.RLock()
+	defer n.tapLock.RUnlock()
 	return n.tap
 	return n.tap
 }
 }

+ 2 - 0
go/pkg/zerotier/node-callbacks.go

@@ -13,6 +13,8 @@
 
 
 package zerotier
 package zerotier
 
 
+// These are exported callbacks that are called from the C++ code in GoGlue.cpp
+
 //#cgo CFLAGS: -O3
 //#cgo CFLAGS: -O3
 //#define ZT_CGO 1
 //#define ZT_CGO 1
 //#include <stdint.h>
 //#include <stdint.h>

+ 16 - 1
go/pkg/zerotier/node.go

@@ -22,7 +22,7 @@ import (
 )
 )
 
 
 //#cgo CFLAGS: -O3
 //#cgo CFLAGS: -O3
-//#cgo LDFLAGS: ${SRCDIR}/../../../build/node/libzt_core.a ${SRCDIR}/../../../build/go/native/libzt_go_native.a -lc++
+//#cgo LDFLAGS: ${SRCDIR}/../../../build/node/libzt_core.a ${SRCDIR}/../../../build/go/native/libzt_go_native.a -lc++ -lpthread
 //#define ZT_CGO 1
 //#define ZT_CGO 1
 //#include <stdint.h>
 //#include <stdint.h>
 //#include "../../native/GoGlue.h"
 //#include "../../native/GoGlue.h"
@@ -50,6 +50,8 @@ const (
 	CoreVersionBuild int = C.ZEROTIER_ONE_VERSION_BUILD
 	CoreVersionBuild int = C.ZEROTIER_ONE_VERSION_BUILD
 )
 )
 
 
+//////////////////////////////////////////////////////////////////////////////
+
 // Node is an instance of a ZeroTier node
 // Node is an instance of a ZeroTier node
 type Node struct {
 type Node struct {
 	gn *C.ZT_GoNode
 	gn *C.ZT_GoNode
@@ -92,6 +94,19 @@ func (n *Node) Close() {
 	}
 	}
 }
 }
 
 
+// Join joins a network
+// If tap is nil, the default system tap for this OS/platform is used (if available).
+func (n *Node) Join(nwid uint64, tap Tap) (*Network, error) {
+	return nil, nil
+}
+
+// Leave leaves a network
+func (n *Node) Leave(nwid uint64) error {
+	return nil
+}
+
+//////////////////////////////////////////////////////////////////////////////
+
 func (n *Node) pathCheck(ztAddress uint64, af int, ip net.IP, port int) bool {
 func (n *Node) pathCheck(ztAddress uint64, af int, ip net.IP, port int) bool {
 	return true
 	return true
 }
 }

+ 2 - 0
go/pkg/zerotier/tap.go

@@ -17,6 +17,8 @@ import "net"
 
 
 // Tap represents an Ethernet tap connecting a virtual network to a device or something else "real"
 // Tap represents an Ethernet tap connecting a virtual network to a device or something else "real"
 type Tap interface {
 type Tap interface {
+	Type() string
+	Error() (int, string)
 	SetEnabled(enabled bool)
 	SetEnabled(enabled bool)
 	Enabled() bool
 	Enabled() bool
 	AddIP(ip net.IPNet) error
 	AddIP(ip net.IPNet) error