Fix some JSON names, regularize use of IP/port info in service code.

This commit is contained in:
Adam Ierymenko 2020-07-31 15:32:09 -07:00
parent 7c929099b3
commit 9da0b43d2d
No known key found for this signature in database
GPG key ID: C8877CF2D7A5D7F3
8 changed files with 90 additions and 38 deletions

View file

@ -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
}

26
cmd/zerotier/cli/peer.go Normal file
View file

@ -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
}

View file

@ -139,9 +139,11 @@ func main() {
case "peers", "listpeers", "lspeers": case "peers", "listpeers", "lspeers":
exitCode = cli.Peers(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs, *jflag, false) exitCode = cli.Peers(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs, *jflag, false)
case "peer": case "peer":
exitCode = cli.Peer(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs, *jflag)
case "roots": case "roots":
exitCode = cli.Peers(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs, *jflag, true) exitCode = cli.Peers(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs, *jflag, true)
case "controller": case "controller":
exitCode = cli.Controller(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs, *jflag)
case "set": case "set":
exitCode = cli.Set(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs) exitCode = cli.Set(basePath, authToken(basePath, *tflag, *tTflag), cmdArgs)
case "identity": case "identity":

View file

@ -98,10 +98,9 @@ func randomUInt() uint {
func TimeMs() int64 { return int64(time.Now().UnixNano()) / int64(1000000) } 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 // 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) copy(((*[16]byte)(unsafe.Pointer(&k[0])))[:], ipn.IP)
ones, bits := ipn.Mask.Size() k[2] = uint64(ipn.Port)
k[2] = (uint64(ones) << 32) | uint64(bits)
return return
} }

View file

@ -64,37 +64,35 @@ func (t *nativeTap) Enabled() bool {
} }
// AddIP adds an IP address (with netmask) to this tap // AddIP adds an IP address (with netmask) to this tap
func (t *nativeTap) AddIP(ip *net.IPNet) error { func (t *nativeTap) AddIP(ip *InetAddress) error {
bits, _ := ip.Mask.Size()
if len(ip.IP) == 16 { if len(ip.IP) == 16 {
if bits > 128 || bits < 0 { if ip.Port > 128 || ip.Port < 0 {
return ErrInvalidParameter 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 { } else if len(ip.IP) == 4 {
if bits > 32 || bits < 0 { if ip.Port > 32 || ip.Port < 0 {
return ErrInvalidParameter 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 return ErrInvalidParameter
} }
// RemoveIP removes this IP address (with netmask) from this tap // RemoveIP removes this IP address (with netmask) from this tap
func (t *nativeTap) RemoveIP(ip *net.IPNet) error { func (t *nativeTap) RemoveIP(ip *InetAddress) error {
bits, _ := ip.Mask.Size()
if len(ip.IP) == 16 { if len(ip.IP) == 16 {
if bits > 128 || bits < 0 { if ip.Port > 128 || ip.Port < 0 {
return ErrInvalidParameter 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 return nil
} }
if len(ip.IP) == 4 { if len(ip.IP) == 4 {
if bits > 32 || bits < 0 { if ip.Port > 32 || ip.Port < 0 {
return ErrInvalidParameter 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 nil
} }
return ErrInvalidParameter return ErrInvalidParameter

View file

@ -72,63 +72,62 @@ func (n *NetworkID) UnmarshalJSON(j []byte) error {
if err != nil { if err != nil {
return err return err
} }
tmp, err := NewNetworkIDFromString(s) *n, err = NewNetworkIDFromString(s)
*n = tmp
return err return err
} }
// NetworkConfig represents the network's current configuration as distributed by its network controller. // NetworkConfig represents the network's current configuration as distributed by its network controller.
type NetworkConfig struct { type NetworkConfig struct {
// ID is this network's 64-bit globally unique identifier // 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 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 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 is a status code indicating this network's authorization status
Status int Status int `json:"status"`
// Type is this network's type // Type is this network's type
Type int Type int `json:"type"`
// MTU is the Ethernet MTU for this network // 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 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 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 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 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 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 // NetworkLocalSettings is settings for this network that can be changed locally
type NetworkLocalSettings struct { type NetworkLocalSettings struct {
// AllowManagedIPs determines whether managed IP assignment is allowed // 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 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 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 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 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 // 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 // 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 // longer wanted. IPs assigned to the tap externally (e.g. by an
// "ifconfig" command) are left alone. // "ifconfig" command) are left alone.
haveAssignedIPs := make(map[[3]uint64]*net.IPNet) haveAssignedIPs := make(map[[3]uint64]*InetAddress)
wantAssignedIPs := make(map[[3]uint64]bool) wantAssignedIPs := make(map[[3]uint64]bool)
if n.settings.AllowManagedIPs { if n.settings.AllowManagedIPs {
for _, ip := range n.config.AssignedAddresses { for _, ip := range n.config.AssignedAddresses {

View file

@ -416,11 +416,12 @@ func (n *Node) Leave(nwid NetworkID) error {
nw := n.networks[nwid] nw := n.networks[nwid]
delete(n.networks, nwid) delete(n.networks, nwid)
n.networksLock.Unlock() n.networksLock.Unlock()
if nw != nil { if nw != nil {
n.infoLog.Printf("leaving network %.16x", nwid) n.infoLog.Printf("leaving network %.16x", nwid)
nw.leaving() nw.leaving()
C.ZT_GoNode_leave(n.gn, C.uint64_t(nwid))
} }
C.ZT_GoNode_leave(n.gn, C.uint64_t(nwid))
return nil 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++ { for i := 0; i < int(ncc.assignedAddressCount); i++ {
a := sockaddrStorageToIPNet(&ncc.assignedAddresses[i]) a := sockaddrStorageToIPNet(&ncc.assignedAddresses[i])
if a != nil { 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++ { for i := 0; i < int(ncc.routeCount); i++ {

View file

@ -33,10 +33,10 @@ type Tap interface {
Enabled() bool Enabled() bool
// AddIP assigns an IP address to this tap device // 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 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 returns an array of all IPs currently assigned to this tap including those not assigned by ZeroTier
IPs() ([]net.IPNet, error) IPs() ([]net.IPNet, error)