mirror of
https://github.com/yggdrasil-network/yggdrasil-go.git
synced 2025-04-29 14:45:07 +03:00
1. preparing for NetworkDomain load from config
This commit is contained in:
parent
09d4564282
commit
c7bca5d212
5 changed files with 25 additions and 20 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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() {}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue