1. preparing for NetworkDomain load from config

This commit is contained in:
vadym 2022-10-30 23:03:28 +02:00
parent 09d4564282
commit c7bca5d212
5 changed files with 25 additions and 20 deletions

View file

@ -16,15 +16,15 @@ type Subnet [8]byte
// The current implementation requires this to be a multiple of 8 bits + 7 bits.
// The 8th bit of the last byte is used to signal nodes (0) or /64 prefixes (1).
// Nodes that configure this differently will be unable to communicate with each other using IP packets, though routing and the DHT machinery *should* still work.
func GetPrefix() [1]byte {
func (c *Core) GetPrefix() [1]byte {
return [...]byte{0xfc}
}
// IsValid returns true if an address falls within the range used by nodes in the network.
func (a *Address) IsValid() bool {
prefix := GetPrefix()
func (c *Core) IsValidAddress(a Address) bool {
prefix := c.GetPrefix()
for idx := range prefix {
if (*a)[idx] != prefix[idx] {
if a[idx] != prefix[idx] {
return false
}
}
@ -32,15 +32,15 @@ func (a *Address) IsValid() bool {
}
// IsValid returns true if a prefix falls within the range usable by the network.
func (s *Subnet) IsValid() bool {
prefix := GetPrefix()
func (c *Core) IsValidSubnet(s Subnet) bool {
prefix := c.GetPrefix()
l := len(prefix)
for idx := range prefix[:l-1] {
if (*s)[idx] != prefix[idx] {
if s[idx] != prefix[idx] {
return false
}
}
return (*s)[l-1] == prefix[l-1]|0x01
return s[l-1] == prefix[l-1]|0x01
}
// AddrForKey takes an ed25519.PublicKey as an argument and returns an *Address.
@ -86,7 +86,7 @@ func (c *Core) AddrForKey(publicKey ed25519.PublicKey) *Address {
temp = append(temp, bits)
}
}
prefix := GetPrefix()
prefix := c.GetPrefix()
copy(addr[:], prefix[:])
addr[len(prefix)] = ones
copy(addr[len(prefix)+1:], temp)
@ -108,16 +108,16 @@ func (c *Core) SubnetForKey(publicKey ed25519.PublicKey) *Subnet {
}
var snet Subnet
copy(snet[:], addr[:])
prefix := GetPrefix() // nolint:staticcheck
prefix := c.GetPrefix() // nolint:staticcheck
snet[len(prefix)-1] |= 0x01
return &snet
}
// GetKet returns the partial ed25519.PublicKey for the Address.
// This is used for key lookup.
func (a *Address) GetKey() ed25519.PublicKey {
func (c *Core) GetAddressKey(a Address) ed25519.PublicKey {
var key [ed25519.PublicKeySize]byte
prefix := GetPrefix() // nolint:staticcheck
prefix := c.GetPrefix() // nolint:staticcheck
ones := int(a[len(prefix)])
for idx := 0; idx < ones; idx++ {
key[idx/8] |= 0x80 >> byte(idx%8)
@ -143,8 +143,8 @@ func (a *Address) GetKey() ed25519.PublicKey {
// GetKet returns the partial ed25519.PublicKey for the Subnet.
// This is used for key lookup.
func (s *Subnet) GetKey() ed25519.PublicKey {
func (c *Core) GetSubnetKey(s Subnet) ed25519.PublicKey {
var addr Address
copy(addr[:], s[:])
return addr.GetKey()
return c.GetAddressKey(addr)
}

View file

@ -35,11 +35,12 @@ type Core struct {
log Logger
addPeerTimer *time.Timer
config struct {
_peers map[Peer]*linkInfo // configurable after startup
_peers map[Peer]*linkInfo // configurable after startup
_listeners map[ListenAddress]struct{} // configurable after startup
nodeinfo NodeInfo // immutable after startup
nodeinfoPrivacy NodeInfoPrivacy // immutable after startup
_allowedPublicKeys map[[32]byte]struct{} // configurable after startup
networkdomain NetworkDomain // immutable after startup
}
}

View file

@ -32,6 +32,9 @@ type Peer struct {
}
type NodeInfo map[string]interface{}
type NodeInfoPrivacy bool
type NetworkDomain struct {
Prefix [1]byte
}
type AllowedPublicKey ed25519.PublicKey
func (a ListenAddress) isSetupOption() {}

View file

@ -94,7 +94,7 @@ func (k *keyStore) sendToAddress(addr core.Address, bs []byte) {
}
})
k.mutex.Unlock()
k.sendKeyLookup(addr.GetKey())
k.sendKeyLookup(k.core.GetAddressKey(addr))
}
}
@ -123,7 +123,7 @@ func (k *keyStore) sendToSubnet(subnet core.Subnet, bs []byte) {
}
})
k.mutex.Unlock()
k.sendKeyLookup(subnet.GetKey())
k.sendKeyLookup(k.core.GetSubnetKey(subnet))
}
}
@ -286,9 +286,9 @@ func (k *keyStore) writePC(bs []byte) (int, error) {
strErr := fmt.Sprint("incorrect source address: ", net.IP(srcAddr[:]).String())
return 0, errors.New(strErr)
}
if dstAddr.IsValid() {
if k.core.IsValidAddress(dstAddr) {
k.sendToAddress(dstAddr, bs)
} else if dstSubnet.IsValid() {
} else if k.core.IsValidSubnet(dstSubnet) {
k.sendToSubnet(dstSubnet, bs)
} else {
return 0, errors.New("invalid destination address")

View file

@ -26,6 +26,7 @@ type MTU uint16
// should pass this object to the mesh.SetRouterAdapter() function before
// calling mesh.Start().
type TunAdapter struct {
core *core.Core
rwc *ipv6rwc.ReadWriteCloser
log core.Logger
addr core.Address
@ -107,7 +108,7 @@ func (tun *TunAdapter) _start() error {
}
tun.addr = tun.rwc.Address()
tun.subnet = tun.rwc.Subnet()
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(core.GetPrefix())-1)
addr := fmt.Sprintf("%s/%d", net.IP(tun.addr[:]).String(), 8*len(tun.core.GetPrefix())-1)
if tun.config.name == "none" || tun.config.name == "dummy" {
tun.log.Debugln("Not starting TUN as ifname is none or dummy")
tun.isEnabled = false