ZeroTierOne/go/pkg/zerotier/network.go

132 lines
3.4 KiB
Go
Raw Normal View History

2019-09-21 01:48:48 +00:00
/*
* Copyright (c)2019 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: 2023-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 zerotier
import (
2019-09-21 02:51:57 +00:00
"encoding/json"
"fmt"
2019-09-21 01:48:48 +00:00
"net"
2019-09-21 02:51:57 +00:00
"strconv"
"sync"
2019-09-21 01:48:48 +00:00
"time"
)
2019-09-21 02:51:57 +00:00
// NetworkID is a network's 64-bit unique ID
type NetworkID uint64
// NewNetworkIDFromString parses a network ID in string form
func NewNetworkIDFromString(s string) (NetworkID, error) {
if len(s) != 16 {
return NetworkID(0), ErrInvalidZeroTierAddress
}
n, err := strconv.ParseUint(s, 16, 64)
return NetworkID(n), err
}
// String returns this network ID's 16-digit hex identifier
func (n NetworkID) String() string {
return fmt.Sprintf("%.16x", uint64(n))
}
// MarshalJSON marshals this NetworkID as a string
func (n NetworkID) MarshalJSON() ([]byte, error) {
return []byte(n.String()), nil
}
// UnmarshalJSON unmarshals this NetworkID from a string
func (n *NetworkID) UnmarshalJSON(j []byte) error {
var s string
err := json.Unmarshal(j, &s)
if err != nil {
return err
}
tmp, err := NewNetworkIDFromString(s)
*n = tmp
return err
}
2019-09-21 01:48:48 +00:00
// NetworkConfig represents the network's current state
type NetworkConfig struct {
// ID is this network's 64-bit globally unique identifier
2019-09-21 02:51:57 +00:00
ID NetworkID
2019-09-21 01:48:48 +00:00
// MAC is the Ethernet MAC address of this device on this network
MAC MAC
// Name is a short human-readable name set by the controller
Name string
// Status is a status code indicating this network's authorization status
Status int
// LastUpdated is the time this network's configuration was last updated from the controller
LastUpdated time.Time
// Type is this network's type
Type int
// MTU is the Ethernet MTU for this network
MTU int
// CanBridge is true if this network is allowed to bridge in other devices with different Ethernet addresses
CanBridge bool
// AllowsBroadcast is true if the broadcast (ff:ff:ff:ff:ff:ff) address works (excluding IPv4 ARP which is handled via a special path)
AllowsBroadcast bool
// IPs are static IPs assigned by the network controller to this device
IPs []net.IPNet
// Routes are static routes assigned by the network controller to this device
Routes []Route
// MulticastSubscriptions are this device's current multicast subscriptions
MulticastSubscriptions []MulticastGroup
2019-09-21 02:51:57 +00:00
// PortDeviceType is a human-readable description of this port's implementation type or name
PortDeviceType string
2019-09-21 01:48:48 +00:00
// PortDeviceName is the OS-specific device name (e.g. tun0 or feth1856) for this network's virtual port
PortDeviceName string
// PortErrorCode is an OS-specific error code returned by the virtual NIC driver
PortErrorCode int
}
// Network is a currently joined network
type Network struct {
2019-09-21 03:34:31 +00:00
id NetworkID
2019-09-21 02:51:57 +00:00
config NetworkConfig
configLock sync.RWMutex
2019-09-21 03:34:31 +00:00
tap Tap
2019-09-21 02:51:57 +00:00
tapLock sync.Mutex
}
2019-09-21 03:34:31 +00:00
// ID gets this network's unique ID
func (n *Network) ID() NetworkID { return n.id }
2019-09-21 02:51:57 +00:00
// Config returns a copy of this network's current configuration
func (n *Network) Config() NetworkConfig {
n.configLock.RLock()
defer n.configLock.RUnlock()
return n.config
2019-09-21 01:48:48 +00:00
}
2019-09-21 03:34:31 +00:00
// Tap gets this network's tap device
func (n *Network) Tap() Tap {
n.tapLock.Lock()
defer n.tapLock.Unlock()
return n.tap
}