mirror of
https://github.com/kaspanet/kaspad.git
synced 2025-07-08 13:52:32 +00:00
Change AddressKey from string to port+ipv6 address (#1458)
This commit is contained in:
parent
2075c585da
commit
2823461fe2
@ -5,7 +5,7 @@
|
|||||||
package addressmanager
|
package addressmanager
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/binary"
|
"net"
|
||||||
"sync"
|
"sync"
|
||||||
|
|
||||||
"github.com/kaspanet/kaspad/app/appmessage"
|
"github.com/kaspanet/kaspad/app/appmessage"
|
||||||
@ -18,9 +18,11 @@ type AddressRandomizer interface {
|
|||||||
RandomAddresses(addresses []*appmessage.NetAddress, count int) []*appmessage.NetAddress
|
RandomAddresses(addresses []*appmessage.NetAddress, count int) []*appmessage.NetAddress
|
||||||
}
|
}
|
||||||
|
|
||||||
// AddressKey represents a "string" key of the ip addresses
|
// AddressKey represents a pair of IP and port, the IP is always in V6 representation
|
||||||
// for use as keys in maps.
|
type AddressKey struct {
|
||||||
type AddressKey string
|
port uint16
|
||||||
|
address [net.IPv6len]byte
|
||||||
|
}
|
||||||
|
|
||||||
// ErrAddressNotFound is an error returned from some functions when a
|
// ErrAddressNotFound is an error returned from some functions when a
|
||||||
// given address is not found in the address manager
|
// given address is not found in the address manager
|
||||||
@ -28,13 +30,10 @@ var ErrAddressNotFound = errors.New("address not found")
|
|||||||
|
|
||||||
// NetAddressKey returns a key of the ip address to use it in maps.
|
// NetAddressKey returns a key of the ip address to use it in maps.
|
||||||
func netAddressKey(netAddress *appmessage.NetAddress) AddressKey {
|
func netAddressKey(netAddress *appmessage.NetAddress) AddressKey {
|
||||||
port := make([]byte, 2, 2)
|
key := AddressKey{port: netAddress.Port}
|
||||||
binary.LittleEndian.PutUint16(port, netAddress.Port)
|
// all IPv4 can be represented as IPv6.
|
||||||
|
copy(key.address[:], netAddress.IP.To16())
|
||||||
key := make([]byte, len(netAddress.IP), len(netAddress.IP)+len(port))
|
return key
|
||||||
copy(key, netAddress.IP)
|
|
||||||
|
|
||||||
return AddressKey(append(key, port...))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// netAddressKeys returns a key of the ip address to use it in maps.
|
// netAddressKeys returns a key of the ip address to use it in maps.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user