Skip to content

Commit

Permalink
Rename methods for keys RIVM-120
Browse files Browse the repository at this point in the history
  • Loading branch information
vikulin committed Aug 15, 2023
1 parent 7f1eab3 commit 166eada
Show file tree
Hide file tree
Showing 10 changed files with 33 additions and 33 deletions.
2 changes: 1 addition & 1 deletion cmd/genkeys/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func main() {
fmt.Println("Pub:", hex.EncodeToString(newKey.pub))
logger := log.New(os.Stdout, "", log.Flags())
core, _ := c.New(newKey.priv, logger, nil)
addr := core.AddrForKey(types.Domain{Key: newKey.pub, Name: newKey.pub})
addr := core.AddrForDomain(types.Domain{Key: newKey.pub, Name: newKey.pub})
fmt.Println("IP:", net.IP(addr[:]).String())
}
}
Expand Down
4 changes: 2 additions & 2 deletions cmd/mesh/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,14 +215,14 @@ func run(args rivArgs, sigCh chan os.Signal) {
switch {
case args.getaddr:
if key := getNodeKey(); !key.Equal(types.Domain{}) {
addr := n.core.AddrForKey(key)
addr := n.core.AddrForDomain(key)
ip := net.IP(addr[:])
fmt.Println(ip.String())
}
return
case args.getsnet:
if key := getNodeKey(); !key.Equal(types.Domain{}) {
snet := n.core.SubnetForKey(key)
snet := n.core.SubnetForDomain(key)
ipnet := net.IPNet{
IP: append(snet[:], 0, 0, 0, 0, 0, 0, 0, 0),
Mask: net.CIDRMask(len(snet)*8, 128),
Expand Down
2 changes: 1 addition & 1 deletion contrib/mobile/mobile.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func (m *Mesh) GetPeersJSON() (result string) {
IP string
}{}
for _, v := range m.core.GetPeers() {
a := m.core.AddrForKey(v.Domain)
a := m.core.AddrForDomain(v.Domain)
ip := net.IP(a[:]).String()
peers = append(peers, struct {
core.PeerInfo
Expand Down
20 changes: 10 additions & 10 deletions src/core/address.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@ func (c *Core) IsValidSubnet(s Subnet) bool {
return s[l-1] == prefix[l-1]|0x01
}

// AddrForKey takes a Domain as an argument and returns an *Address.
// AddrForDomain takes a Domain as an argument and returns an *Address.
// This function returns nil if the key length is not ed25519.PublicKeySize.
// This address begins with the contents of GetPrefix(), with the last bit set to 0 to indicate an address.
// The following 8 bits are set to the number of leading 1 bits in the bitwise inverse of the public key.
// The bitwise inverse of the key, excluding the leading 1 bits and the first leading 0 bit, is truncated to the appropriate length and makes up the remainder of the address.
func (c *Core) AddrForKey(domain iwt.Domain) *Address {
// The bitwise inverse of the Domain name, excluding the leading 1 bits and the first leading 0 bit, is truncated to the appropriate length and makes up the remainder of the address.
func (c *Core) AddrForDomain(domain iwt.Domain) *Address {
// 128 bit address
// Begins with prefix
// Next bit is a 0
Expand Down Expand Up @@ -102,16 +102,16 @@ func (c *Core) AddrForKey(domain iwt.Domain) *Address {
return &addr
}

// SubnetForKey takes a Domain as an argument and returns a *Subnet.
// SubnetForDomain takes a Domain as an argument and returns a *Subnet.
// This function returns nil if the key length is not ed25519.PublicKeySize.
// The subnet begins with the address prefix, with the last bit set to 1 to indicate a prefix.
// The following 8 bits are set to the number of leading 1 bits in the bitwise inverse of the key.
// The bitwise inverse of the key, excluding the leading 1 bits and the first leading 0 bit, is truncated to the appropriate length and makes up the remainder of the subnet.
func (c *Core) SubnetForKey(domain iwt.Domain) *Subnet {
// The bitwise inverse of the Domain name bytes, excluding the leading 1 bits and the first leading 0 bit, is truncated to the appropriate length and makes up the remainder of the subnet.
func (c *Core) SubnetForDomain(domain iwt.Domain) *Subnet {
// Exactly as the address version, with two exceptions:
// 1) The first bit after the fixed prefix is a 1 instead of a 0
// 2) It's truncated to a subnet prefix length instead of 128 bits
addr := c.AddrForKey(domain)
addr := c.AddrForDomain(domain)
if addr == nil {
return nil
}
Expand All @@ -124,7 +124,7 @@ func (c *Core) SubnetForKey(domain iwt.Domain) *Subnet {
// GetKet returns the partial ed25519.PublicKey for the Address.
// This is used for key lookup.

func (c *Core) GetAddressKey(a Address) iwt.Domain {
func (c *Core) GetAddressDomain(a Address) iwt.Domain {
var key [ed25519.PublicKeySize]byte
prefix := c.GetPrefix() // nolint:staticcheck
ones := int(a[len(prefix)])
Expand Down Expand Up @@ -152,8 +152,8 @@ func (c *Core) GetAddressKey(a Address) iwt.Domain {

// GetKet returns the partial ed25519.PublicKey for the Subnet.
// This is used for key lookup.
func (c *Core) GetSubnetKey(s Subnet) iwt.Domain {
func (c *Core) GetSubnetDomain(s Subnet) iwt.Domain {
var addr Address
copy(addr[:], s[:])
return c.GetAddressKey(addr)
return c.GetAddressDomain(addr)
}
8 changes: 4 additions & 4 deletions src/core/address_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (c *Core) TestAddress_AddrForKey(t *testing.T) {
0xfc, 0, 132, 138, 96, 79, 187, 126, 67, 132, 101, 219, 141, 182, 104, 149,
}

if *c.AddrForKey(types.Domain{Key: publicKey, Name: publicKey}) != expectedAddress {
if *c.AddrForDomain(types.Domain{Key: publicKey, Name: publicKey}) != expectedAddress {
t.Fatal("invalid address returned")
}
}
Expand All @@ -78,7 +78,7 @@ func (c *Core) TestAddress_SubnetForKey(t *testing.T) {

expectedSubnet := Subnet{0xfd, 0, 132, 138, 96, 79, 187, 126}

if *c.SubnetForKey(types.Domain{Key: publicKey, Name: publicKey}) != expectedSubnet {
if *c.SubnetForDomain(types.Domain{Key: publicKey, Name: publicKey}) != expectedSubnet {
t.Fatal("invalid subnet returned")
}
}
Expand All @@ -95,7 +95,7 @@ func (c *Core) TestAddress_Address_GetKey(t *testing.T) {
255, 255, 255, 255, 255, 255, 255, 255,
}

if !bytes.Equal(c.GetAddressKey(address).Key, expectedPublicKey) {
if !bytes.Equal(c.GetAddressDomain(address).Key, expectedPublicKey) {
t.Fatal("invalid public key returned")
}
}
Expand All @@ -110,7 +110,7 @@ func (c *Core) TestAddress_Subnet_GetKey(t *testing.T) {
255, 255, 255, 255, 255, 255, 255, 255,
}

if !bytes.Equal(c.GetSubnetKey(subnet).Key, expectedPublicKey) {
if !bytes.Equal(c.GetSubnetDomain(subnet).Key, expectedPublicKey) {
t.Fatal("invalid public key returned")
}
}
4 changes: 2 additions & 2 deletions src/core/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (c *Core) Listen(u *url.URL, sintf string) (*Listener, error) {
// that application also implements either VPN functionality or deals with IP
// packets specifically.
func (c *Core) Address() net.IP {
addr := net.IP(c.AddrForKey(c.public)[:])
addr := net.IP(c.AddrForDomain(c.public)[:])
return addr
}

Expand All @@ -181,7 +181,7 @@ func (c *Core) Address() net.IP {
// that application also implements either VPN functionality or deals with IP
// packets specifically.
func (c *Core) Subnet() net.IPNet {
subnet := c.SubnetForKey(c.public)[:]
subnet := c.SubnetForDomain(c.public)[:]
subnet = append(subnet, 0, 0, 0, 0, 0, 0, 0, 0)
return net.IPNet{IP: subnet, Mask: net.CIDRMask(64, 128)}
}
Expand Down
2 changes: 1 addition & 1 deletion src/core/link.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,7 +193,7 @@ func (intf *link) handler(dial *linkDial) error {
if intf.incoming {
dir = "inbound"
}
remoteAddr := net.IP(intf.links.core.AddrForKey(meta.domain)[:]).String()
remoteAddr := net.IP(intf.links.core.AddrForDomain(meta.domain)[:]).String()
remoteStr := fmt.Sprintf("%s@%s", remoteAddr, intf.info.remote)
localStr := intf.conn.LocalAddr()
intf.links.core.log.Infof("Connected %s %s: %s, source %s",
Expand Down
6 changes: 3 additions & 3 deletions src/core/proto.go
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ func (p *protoHandler) getSelfHandler(in json.RawMessage) (interface{}, error) {
if err := msg.UnmarshalJSON(info); err != nil {
return nil, err
}
ip := net.IP(p.core.AddrForKey(req.Key)[:])
ip := net.IP(p.core.AddrForDomain(req.Key)[:])
res := DebugGetSelfResponse{ip.String(): msg}
return res, nil
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func (p *protoHandler) getPeersHandler(in json.RawMessage) (interface{}, error)
if err := msg.UnmarshalJSON(js); err != nil {
return nil, err
}
ip := net.IP(p.core.AddrForKey(req.Key)[:])
ip := net.IP(p.core.AddrForDomain(req.Key)[:])
res := DebugGetPeersResponse{ip.String(): msg}
return res, nil
}
Expand Down Expand Up @@ -362,7 +362,7 @@ func (p *protoHandler) getDHTHandler(in json.RawMessage) (interface{}, error) {
if err := msg.UnmarshalJSON(js); err != nil {
return nil, err
}
ip := net.IP(p.core.AddrForKey(req.Key)[:])
ip := net.IP(p.core.AddrForDomain(req.Key)[:])
res := DebugGetDHTResponse{ip.String(): msg}
return res, nil
}
Expand Down
14 changes: 7 additions & 7 deletions src/ipv6rwc/ipv6rwc.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ type buffer struct {

func (k *keyStore) init(c *core.Core) {
k.core = c
k.address = *c.AddrForKey(k.core.GetSelf().Domain)
k.subnet = *c.SubnetForKey(k.core.GetSelf().Domain)
k.address = *c.AddrForDomain(k.core.GetSelf().Domain)
k.subnet = *c.SubnetForDomain(k.core.GetSelf().Domain)
if err := k.core.SetOutOfBandHandler(k.oobHandler); err != nil {
err = fmt.Errorf("tun.core.SetOutOfBandHander: %w", err)
panic(err)
Expand Down Expand Up @@ -95,7 +95,7 @@ func (k *keyStore) sendToAddress(addr core.Address, bs []byte) {
}
})
k.mutex.Unlock()
k.sendKeyLookup(k.core.GetAddressKey(addr))
k.sendKeyLookup(k.core.GetAddressDomain(addr))
}
}

Expand Down Expand Up @@ -124,7 +124,7 @@ func (k *keyStore) sendToSubnet(subnet core.Subnet, bs []byte) {
}
})
k.mutex.Unlock()
k.sendKeyLookup(k.core.GetSubnetKey(subnet))
k.sendKeyLookup(k.core.GetSubnetDomain(subnet))
}
}

Expand All @@ -137,8 +137,8 @@ func (k *keyStore) update(key iwt.Domain) *keyInfo {
if info = k.keyToInfo[kArray]; info == nil {
info = new(keyInfo)
info.domain = key
info.address = *k.core.AddrForKey(info.domain)
info.subnet = *k.core.SubnetForKey(info.domain)
info.address = *k.core.AddrForDomain(info.domain)
info.subnet = *k.core.SubnetForDomain(info.domain)
k.keyToInfo[kArray] = info
k.addrToInfo[info.address] = info
k.subnetToInfo[info.subnet] = info
Expand Down Expand Up @@ -187,7 +187,7 @@ func (k *keyStore) oobHandler(fromKey, toKey types.Domain, data []byte) {
sig := data[1:]
switch data[0] {
case typeKeyLookup:
snet := *k.core.SubnetForKey(toKey)
snet := *k.core.SubnetForDomain(toKey)
if snet == k.subnet && ed25519.Verify(fromKey.Key, toKey.Key, sig) {
// This is looking for at least our subnet (possibly our address)
// Send a response
Expand Down
4 changes: 2 additions & 2 deletions src/restapi/rest_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,7 @@ func (a *RestServer) getApiDhtHandler(w http.ResponseWriter, r *http.Request) {
dht := a.Core.GetDHT()
result := make([]map[string]any, 0, len(dht))
for _, d := range dht {
addr := a.Core.AddrForKey(d.Domain)
addr := a.Core.AddrForDomain(d.Domain)
entry := map[string]any{
"address": net.IP(addr[:]).String(),
"key": hex.EncodeToString(d.Domain.Key),
Expand Down Expand Up @@ -555,7 +555,7 @@ func (a *RestServer) prepareGetPeers() []Peer {
peers := a.Core.GetPeers()
response := make([]Peer, 0, len(peers))
for _, p := range peers {
addr := a.Core.AddrForKey(p.Domain)
addr := a.Core.AddrForDomain(p.Domain)
entry := Peer{
net.IP(addr[:]).String(),
hex.EncodeToString(p.Domain.Key),
Expand Down

0 comments on commit 166eada

Please sign in to comment.