From c7bca5d21286ef98741ddb8d42c37190f267c216 Mon Sep 17 00:00:00 2001 From: vadym Date: Sun, 30 Oct 2022 23:03:28 +0200 Subject: [PATCH] 1. preparing for NetworkDomain load from config --- src/core/address.go | 28 ++++++++++++++-------------- src/core/core.go | 3 ++- src/core/options.go | 3 +++ src/ipv6rwc/ipv6rwc.go | 8 ++++---- src/tun/tun.go | 3 ++- 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/src/core/address.go b/src/core/address.go index b9440a47..a472cc7f 100644 --- a/src/core/address.go +++ b/src/core/address.go @@ -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) } diff --git a/src/core/core.go b/src/core/core.go index 3d5ec2e8..bdbe8ee5 100644 --- a/src/core/core.go +++ b/src/core/core.go @@ -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 } } diff --git a/src/core/options.go b/src/core/options.go index 66aa16ce..7ad793e9 100644 --- a/src/core/options.go +++ b/src/core/options.go @@ -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() {} diff --git a/src/ipv6rwc/ipv6rwc.go b/src/ipv6rwc/ipv6rwc.go index 646bd74d..d121a48f 100644 --- a/src/ipv6rwc/ipv6rwc.go +++ b/src/ipv6rwc/ipv6rwc.go @@ -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") diff --git a/src/tun/tun.go b/src/tun/tun.go index d4579095..f01e90ec 100644 --- a/src/tun/tun.go +++ b/src/tun/tun.go @@ -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