mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
p2p/enr: add netip.Addr based IP entries
This commit is contained in:
parent
7ed52c949e
commit
acda6d4886
1 changed files with 55 additions and 0 deletions
|
|
@ -21,6 +21,7 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/rlp"
|
"github.com/ethereum/go-ethereum/rlp"
|
||||||
)
|
)
|
||||||
|
|
@ -167,6 +168,60 @@ func (v *IPv6) DecodeRLP(s *rlp.Stream) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IPv4Addr is the "ip" key, which holds the IP address of the node.
|
||||||
|
type IPv4Addr netip.Addr
|
||||||
|
|
||||||
|
func (v IPv4Addr) ENRKey() string { return "ip" }
|
||||||
|
|
||||||
|
// EncodeRLP implements rlp.Encoder.
|
||||||
|
func (v IPv4Addr) EncodeRLP(w io.Writer) error {
|
||||||
|
addr := netip.Addr(v)
|
||||||
|
if !addr.Is4() {
|
||||||
|
return fmt.Errorf("address is not IPv4")
|
||||||
|
}
|
||||||
|
enc := rlp.NewEncoderBuffer(w)
|
||||||
|
bytes := addr.As4()
|
||||||
|
enc.WriteBytes(bytes[:])
|
||||||
|
return enc.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeRLP implements rlp.Decoder.
|
||||||
|
func (v *IPv4Addr) DecodeRLP(s *rlp.Stream) error {
|
||||||
|
var bytes [4]byte
|
||||||
|
if err := s.ReadBytes(bytes[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = IPv4Addr(netip.AddrFrom4(bytes))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// IPv6Addr is the "ip6" key, which holds the IP address of the node.
|
||||||
|
type IPv6Addr netip.Addr
|
||||||
|
|
||||||
|
func (v IPv6Addr) ENRKey() string { return "ip6" }
|
||||||
|
|
||||||
|
// EncodeRLP implements rlp.Encoder.
|
||||||
|
func (v IPv6Addr) EncodeRLP(w io.Writer) error {
|
||||||
|
addr := netip.Addr(v)
|
||||||
|
if !addr.Is6() {
|
||||||
|
return fmt.Errorf("address is not IPv6")
|
||||||
|
}
|
||||||
|
enc := rlp.NewEncoderBuffer(w)
|
||||||
|
bytes := addr.As16()
|
||||||
|
enc.WriteBytes(bytes[:])
|
||||||
|
return enc.Flush()
|
||||||
|
}
|
||||||
|
|
||||||
|
// DecodeRLP implements rlp.Decoder.
|
||||||
|
func (v *IPv6Addr) DecodeRLP(s *rlp.Stream) error {
|
||||||
|
var bytes [16]byte
|
||||||
|
if err := s.ReadBytes(bytes[:]); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
*v = IPv6Addr(netip.AddrFrom16(bytes))
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
// KeyError is an error related to a key.
|
// KeyError is an error related to a key.
|
||||||
type KeyError struct {
|
type KeyError struct {
|
||||||
Key string
|
Key string
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue