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 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). // 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. // 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} return [...]byte{0xfc}
} }
// IsValid returns true if an address falls within the range used by nodes in the network. // IsValid returns true if an address falls within the range used by nodes in the network.
func (a *Address) IsValid() bool { func (c *Core) IsValidAddress(a Address) bool {
prefix := GetPrefix() prefix := c.GetPrefix()
for idx := range prefix { for idx := range prefix {
if (*a)[idx] != prefix[idx] { if a[idx] != prefix[idx] {
return false 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. // IsValid returns true if a prefix falls within the range usable by the network.
func (s *Subnet) IsValid() bool { func (c *Core) IsValidSubnet(s Subnet) bool {
prefix := GetPrefix() prefix := c.GetPrefix()
l := len(prefix) l := len(prefix)
for idx := range prefix[:l-1] { for idx := range prefix[:l-1] {
if (*s)[idx] != prefix[idx] { if s[idx] != prefix[idx] {
return false 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. // 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) temp = append(temp, bits)
} }
} }
prefix := GetPrefix() prefix := c.GetPrefix()
copy(addr[:], prefix[:]) copy(addr[:], prefix[:])
addr[len(prefix)] = ones addr[len(prefix)] = ones
copy(addr[len(prefix)+1:], temp) copy(addr[len(prefix)+1:], temp)
@ -108,16 +108,16 @@ func (c *Core) SubnetForKey(publicKey ed25519.PublicKey) *Subnet {
} }
var snet Subnet var snet Subnet
copy(snet[:], addr[:]) copy(snet[:], addr[:])
prefix := GetPrefix() // nolint:staticcheck prefix := c.GetPrefix() // nolint:staticcheck
snet[len(prefix)-1] |= 0x01 snet[len(prefix)-1] |= 0x01
return &snet return &snet
} }
// GetKet returns the partial ed25519.PublicKey for the Address. // GetKet returns the partial ed25519.PublicKey for the Address.
// This is used for key lookup. // 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 var key [ed25519.PublicKeySize]byte
prefix := GetPrefix() // nolint:staticcheck prefix := c.GetPrefix() // nolint:staticcheck
ones := int(a[len(prefix)]) ones := int(a[len(prefix)])
for idx := 0; idx < ones; idx++ { for idx := 0; idx < ones; idx++ {
key[idx/8] |= 0x80 >> byte(idx%8) 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. // GetKet returns the partial ed25519.PublicKey for the Subnet.
// This is used for key lookup. // This is used for key lookup.
func (s *Subnet) GetKey() ed25519.PublicKey { func (c *Core) GetSubnetKey(s Subnet) ed25519.PublicKey {
var addr Address var addr Address
copy(addr[:], s[:]) copy(addr[:], s[:])
return addr.GetKey() return c.GetAddressKey(addr)
} }

View file

@ -35,11 +35,12 @@ type Core struct {
log Logger log Logger
addPeerTimer *time.Timer addPeerTimer *time.Timer
config struct { config struct {
_peers map[Peer]*linkInfo // configurable after startup _peers map[Peer]*linkInfo // configurable after startup
_listeners map[ListenAddress]struct{} // configurable after startup _listeners map[ListenAddress]struct{} // configurable after startup
nodeinfo NodeInfo // immutable after startup nodeinfo NodeInfo // immutable after startup
nodeinfoPrivacy NodeInfoPrivacy // immutable after startup nodeinfoPrivacy NodeInfoPrivacy // immutable after startup
_allowedPublicKeys map[[32]byte]struct{} // configurable 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 NodeInfo map[string]interface{}
type NodeInfoPrivacy bool type NodeInfoPrivacy bool
type NetworkDomain struct {
Prefix [1]byte
}
type AllowedPublicKey ed25519.PublicKey type AllowedPublicKey ed25519.PublicKey
func (a ListenAddress) isSetupOption() {} func (a ListenAddress) isSetupOption() {}

View file

@ -94,7 +94,7 @@ func (k *keyStore) sendToAddress(addr core.Address, bs []byte) {
} }
}) })
k.mutex.Unlock() 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.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()) strErr := fmt.Sprint("incorrect source address: ", net.IP(srcAddr[:]).String())
return 0, errors.New(strErr) return 0, errors.New(strErr)
} }
if dstAddr.IsValid() { if k.core.IsValidAddress(dstAddr) {
k.sendToAddress(dstAddr, bs) k.sendToAddress(dstAddr, bs)
} else if dstSubnet.IsValid() { } else if k.core.IsValidSubnet(dstSubnet) {
k.sendToSubnet(dstSubnet, bs) k.sendToSubnet(dstSubnet, bs)
} else { } else {
return 0, errors.New("invalid destination address") 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 // should pass this object to the mesh.SetRouterAdapter() function before
// calling mesh.Start(). // calling mesh.Start().
type TunAdapter struct { type TunAdapter struct {
core *core.Core
rwc *ipv6rwc.ReadWriteCloser rwc *ipv6rwc.ReadWriteCloser
log core.Logger log core.Logger
addr core.Address addr core.Address
@ -107,7 +108,7 @@ func (tun *TunAdapter) _start() error {
} }
tun.addr = tun.rwc.Address() tun.addr = tun.rwc.Address()
tun.subnet = tun.rwc.Subnet() 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" { if tun.config.name == "none" || tun.config.name == "dummy" {
tun.log.Debugln("Not starting TUN as ifname is none or dummy") tun.log.Debugln("Not starting TUN as ifname is none or dummy")
tun.isEnabled = false tun.isEnabled = false