From 9da0b43d2d51a48482094b7fa9dbdc0afb013cf1 Mon Sep 17 00:00:00 2001 From: Adam Ierymenko Date: Fri, 31 Jul 2020 15:32:09 -0700 Subject: [PATCH] Fix some JSON names, regularize use of IP/port info in service code. --- cmd/zerotier/cli/controller.go | 26 ++++++++++++++++++++++++ cmd/zerotier/cli/peer.go | 26 ++++++++++++++++++++++++ cmd/zerotier/zerotier.go | 2 ++ pkg/zerotier/misc.go | 5 ++--- pkg/zerotier/nativetap.go | 22 +++++++++----------- pkg/zerotier/network.go | 37 +++++++++++++++++----------------- pkg/zerotier/node.go | 6 ++++-- pkg/zerotier/tap.go | 4 ++-- 8 files changed, 90 insertions(+), 38 deletions(-) create mode 100644 cmd/zerotier/cli/controller.go create mode 100644 cmd/zerotier/cli/peer.go diff --git a/cmd/zerotier/cli/controller.go b/cmd/zerotier/cli/controller.go new file mode 100644 index 000000000..60093b150 --- /dev/null +++ b/cmd/zerotier/cli/controller.go @@ -0,0 +1,26 @@ +/* + * Copyright (c)2013-2020 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2025-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +package cli + +func Controller(basePath, authToken string, args []string, jsonOutput bool) int { + if len(args) < 1 { + Help() + return 1 + } + + switch args[0] { + } + + return 0 +} diff --git a/cmd/zerotier/cli/peer.go b/cmd/zerotier/cli/peer.go new file mode 100644 index 000000000..78fe2d7ff --- /dev/null +++ b/cmd/zerotier/cli/peer.go @@ -0,0 +1,26 @@ +/* + * Copyright (c)2013-2020 ZeroTier, Inc. + * + * Use of this software is governed by the Business Source License included + * in the LICENSE.TXT file in the project's root directory. + * + * Change Date: 2025-01-01 + * + * On the date above, in accordance with the Business Source License, use + * of this software will be governed by version 2.0 of the Apache License. + */ +/****/ + +package cli + +func Peer(basePath, authToken string, args []string, jsonOutput bool) int { + if len(args) < 1 { + Help() + return 1 + } + + switch args[0] { + } + + return 0 +} diff --git a/cmd/zerotier/zerotier.go b/cmd/zerotier/zerotier.go index 8c64fd6cc..298e020cb 100644 --- a/cmd/zerotier/zerotier.go +++ b/cmd/zerotier/zerotier.go @@ -139,9 +139,11 @@ func main() { case "peers", "listpeers", "lspeers": exitCode = cli.Peers(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs, *jflag, false) case "peer": + exitCode = cli.Peer(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs, *jflag) case "roots": exitCode = cli.Peers(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs, *jflag, true) case "controller": + exitCode = cli.Controller(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs, *jflag) case "set": exitCode = cli.Set(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs) case "identity": diff --git a/pkg/zerotier/misc.go b/pkg/zerotier/misc.go index a3f9bc22f..700b947d4 100644 --- a/pkg/zerotier/misc.go +++ b/pkg/zerotier/misc.go @@ -98,10 +98,9 @@ func randomUInt() uint { func TimeMs() int64 { return int64(time.Now().UnixNano()) / int64(1000000) } // ipNetToKey creates a key that can be used in a map[] from a net.IPNet -func ipNetToKey(ipn *net.IPNet) (k [3]uint64) { +func ipNetToKey(ipn *InetAddress) (k [3]uint64) { copy(((*[16]byte)(unsafe.Pointer(&k[0])))[:], ipn.IP) - ones, bits := ipn.Mask.Size() - k[2] = (uint64(ones) << 32) | uint64(bits) + k[2] = uint64(ipn.Port) return } diff --git a/pkg/zerotier/nativetap.go b/pkg/zerotier/nativetap.go index fbec75ab6..f6818b296 100644 --- a/pkg/zerotier/nativetap.go +++ b/pkg/zerotier/nativetap.go @@ -64,37 +64,35 @@ func (t *nativeTap) Enabled() bool { } // AddIP adds an IP address (with netmask) to this tap -func (t *nativeTap) AddIP(ip *net.IPNet) error { - bits, _ := ip.Mask.Size() +func (t *nativeTap) AddIP(ip *InetAddress) error { if len(ip.IP) == 16 { - if bits > 128 || bits < 0 { + if ip.Port > 128 || ip.Port < 0 { return ErrInvalidParameter } - C.ZT_GoTap_addIp(t.tap, C.int(syscall.AF_INET6), unsafe.Pointer(&ip.IP[0]), C.int(bits)) + C.ZT_GoTap_addIp(t.tap, C.int(syscall.AF_INET6), unsafe.Pointer(&ip.IP[0]), C.int(ip.Port)) } else if len(ip.IP) == 4 { - if bits > 32 || bits < 0 { + if ip.Port > 32 || ip.Port < 0 { return ErrInvalidParameter } - C.ZT_GoTap_addIp(t.tap, C.int(syscall.AF_INET), unsafe.Pointer(&ip.IP[0]), C.int(bits)) + C.ZT_GoTap_addIp(t.tap, C.int(syscall.AF_INET), unsafe.Pointer(&ip.IP[0]), C.int(ip.Port)) } return ErrInvalidParameter } // RemoveIP removes this IP address (with netmask) from this tap -func (t *nativeTap) RemoveIP(ip *net.IPNet) error { - bits, _ := ip.Mask.Size() +func (t *nativeTap) RemoveIP(ip *InetAddress) error { if len(ip.IP) == 16 { - if bits > 128 || bits < 0 { + if ip.Port > 128 || ip.Port < 0 { return ErrInvalidParameter } - C.ZT_GoTap_removeIp(t.tap, C.int(syscall.AF_INET6), unsafe.Pointer(&ip.IP[0]), C.int(bits)) + C.ZT_GoTap_removeIp(t.tap, C.int(syscall.AF_INET6), unsafe.Pointer(&ip.IP[0]), C.int(ip.Port)) return nil } if len(ip.IP) == 4 { - if bits > 32 || bits < 0 { + if ip.Port > 32 || ip.Port < 0 { return ErrInvalidParameter } - C.ZT_GoTap_removeIp(t.tap, C.int(syscall.AF_INET), unsafe.Pointer(&ip.IP[0]), C.int(bits)) + C.ZT_GoTap_removeIp(t.tap, C.int(syscall.AF_INET), unsafe.Pointer(&ip.IP[0]), C.int(ip.Port)) return nil } return ErrInvalidParameter diff --git a/pkg/zerotier/network.go b/pkg/zerotier/network.go index 9c3c7e2f2..f43b6009a 100644 --- a/pkg/zerotier/network.go +++ b/pkg/zerotier/network.go @@ -72,63 +72,62 @@ func (n *NetworkID) UnmarshalJSON(j []byte) error { if err != nil { return err } - tmp, err := NewNetworkIDFromString(s) - *n = tmp + *n, err = NewNetworkIDFromString(s) return err } // NetworkConfig represents the network's current configuration as distributed by its network controller. type NetworkConfig struct { // ID is this network's 64-bit globally unique identifier - ID NetworkID + ID NetworkID `json:"id"` // MAC is the Ethernet MAC address of this device on this network - MAC MAC + MAC MAC `json:"mac"` // Name is a short human-readable name set by the controller - Name string + Name string `json:"name"` // Status is a status code indicating this network's authorization status - Status int + Status int `json:"status"` // Type is this network's type - Type int + Type int `json:"type"` // MTU is the Ethernet MTU for this network - MTU int + MTU int `json:"mtu"` // Bridge is true if this network is allowed to bridge in other devices with different Ethernet addresses - Bridge bool + Bridge bool `json:"bridge"` // BroadcastEnabled is true if the broadcast (ff:ff:ff:ff:ff:ff) address works (excluding IPv4 ARP which is handled via a special path) - BroadcastEnabled bool + BroadcastEnabled bool `json:"broadcastEnabled"` // NetconfRevision is the revision number reported by the controller - NetconfRevision uint64 + NetconfRevision uint64 `json:"netconfRevision"` // AssignedAddresses are static IPs assigned by the network controller to this device - AssignedAddresses []net.IPNet + AssignedAddresses []InetAddress `json:"assignedAddresses,omitempty"` // Routes are static routes assigned by the network controller to this device - Routes []Route + Routes []Route `json:"routes,omitempty"` } // NetworkLocalSettings is settings for this network that can be changed locally type NetworkLocalSettings struct { // AllowManagedIPs determines whether managed IP assignment is allowed - AllowManagedIPs bool + AllowManagedIPs bool `json:"allowManagedIPs"` // AllowGlobalIPs determines if managed IPs that overlap with public Internet addresses are allowed - AllowGlobalIPs bool + AllowGlobalIPs bool `json:"allowGlobalIPs"` // AllowManagedRoutes determines whether managed routes can be set - AllowManagedRoutes bool + AllowManagedRoutes bool `json:"allowManagedRoutes"` // AllowGlobalRoutes determines if managed routes can overlap with public Internet addresses - AllowGlobalRoutes bool + AllowGlobalRoutes bool `json:"allowGlobalRoutes"` // AllowDefaultRouteOverride determines if the default (0.0.0.0 or ::0) route on the system can be overridden ("full tunnel" mode) - AllowDefaultRouteOverride bool + AllowDefaultRouteOverride bool `json:"allowDefaultRouteOverride"` } // Network is a currently joined network @@ -301,7 +300,7 @@ func (n *Network) updateConfig(nc *NetworkConfig, ls *NetworkLocalSettings) { // and remove any IPs from the tap that were assigned that are no // longer wanted. IPs assigned to the tap externally (e.g. by an // "ifconfig" command) are left alone. - haveAssignedIPs := make(map[[3]uint64]*net.IPNet) + haveAssignedIPs := make(map[[3]uint64]*InetAddress) wantAssignedIPs := make(map[[3]uint64]bool) if n.settings.AllowManagedIPs { for _, ip := range n.config.AssignedAddresses { diff --git a/pkg/zerotier/node.go b/pkg/zerotier/node.go index fc4fc40f7..a9b5d1911 100644 --- a/pkg/zerotier/node.go +++ b/pkg/zerotier/node.go @@ -416,11 +416,12 @@ func (n *Node) Leave(nwid NetworkID) error { nw := n.networks[nwid] delete(n.networks, nwid) n.networksLock.Unlock() + if nw != nil { n.infoLog.Printf("leaving network %.16x", nwid) nw.leaving() + C.ZT_GoNode_leave(n.gn, C.uint64_t(nwid)) } - C.ZT_GoNode_leave(n.gn, C.uint64_t(nwid)) return nil } @@ -891,7 +892,8 @@ func goVirtualNetworkConfigFunc(gn, _ unsafe.Pointer, nwid C.uint64_t, op C.int, for i := 0; i < int(ncc.assignedAddressCount); i++ { a := sockaddrStorageToIPNet(&ncc.assignedAddresses[i]) if a != nil { - nc.AssignedAddresses = append(nc.AssignedAddresses, *a) + _, bits := a.Mask.Size() + nc.AssignedAddresses = append(nc.AssignedAddresses, InetAddress{IP: a.IP, Port: bits}) } } for i := 0; i < int(ncc.routeCount); i++ { diff --git a/pkg/zerotier/tap.go b/pkg/zerotier/tap.go index a1e19e352..dfc37e9e9 100644 --- a/pkg/zerotier/tap.go +++ b/pkg/zerotier/tap.go @@ -33,10 +33,10 @@ type Tap interface { Enabled() bool // AddIP assigns an IP address to this tap device - AddIP(ip *net.IPNet) error + AddIP(ip *InetAddress) error // RemoveIP removes an IP address from this tap - RemoveIP(ip *net.IPNet) error + RemoveIP(ip *InetAddress) error // IPs returns an array of all IPs currently assigned to this tap including those not assigned by ZeroTier IPs() ([]net.IPNet, error)