mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-08-19 18:32:23 +00:00
p2p/netutil: change AddrIP -> AddrAddr
This commit is contained in:
parent
cfa4db6b09
commit
ae3966eb01
3 changed files with 47 additions and 42 deletions
|
|
@ -16,18 +16,52 @@
|
||||||
|
|
||||||
package netutil
|
package netutil
|
||||||
|
|
||||||
import "net"
|
import (
|
||||||
|
"fmt"
|
||||||
|
"math/rand"
|
||||||
|
"net"
|
||||||
|
"net/netip"
|
||||||
|
)
|
||||||
|
|
||||||
// AddrIP gets the IP address contained in addr. It returns nil if no address is present.
|
// AddrAddr gets the IP address contained in addr. It returns nil if no address is present.
|
||||||
func AddrIP(addr net.Addr) net.IP {
|
func AddrAddr(addr net.Addr) netip.Addr {
|
||||||
switch a := addr.(type) {
|
switch a := addr.(type) {
|
||||||
case *net.IPAddr:
|
case *net.IPAddr:
|
||||||
return a.IP
|
return IPToAddr(a.IP)
|
||||||
case *net.TCPAddr:
|
case *net.TCPAddr:
|
||||||
return a.IP
|
return IPToAddr(a.IP)
|
||||||
case *net.UDPAddr:
|
case *net.UDPAddr:
|
||||||
return a.IP
|
return IPToAddr(a.IP)
|
||||||
default:
|
default:
|
||||||
return nil
|
return netip.Addr{}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IPToAddr converts net.IP to netip.Addr. Note that unlike netip.AddrFromSlice, this
|
||||||
|
// function will always ensure that the resulting Addr is IPv4 when the input is.
|
||||||
|
func IPToAddr(ip net.IP) netip.Addr {
|
||||||
|
if ip4 := ip.To4(); ip4 != nil {
|
||||||
|
addr, _ := netip.AddrFromSlice(ip4)
|
||||||
|
return addr
|
||||||
|
} else if ip6 := ip.To16(); ip6 != nil {
|
||||||
|
addr, _ := netip.AddrFromSlice(ip6)
|
||||||
|
return addr
|
||||||
|
}
|
||||||
|
return netip.Addr{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// RandomAddr creates a random IP address.
|
||||||
|
func RandomAddr(rng *rand.Rand, ipv4 bool) netip.Addr {
|
||||||
|
var bytes []byte
|
||||||
|
if ipv4 || rng.Intn(2) == 0 {
|
||||||
|
bytes = make([]byte, 4)
|
||||||
|
} else {
|
||||||
|
bytes = make([]byte, 16)
|
||||||
|
}
|
||||||
|
rng.Read(bytes)
|
||||||
|
addr, ok := netip.AddrFromSlice(bytes)
|
||||||
|
if !ok {
|
||||||
|
panic(fmt.Errorf("BUG! invalid IP %v", bytes))
|
||||||
|
}
|
||||||
|
return addr
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,7 +21,6 @@ import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"math/rand"
|
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"slices"
|
"slices"
|
||||||
|
|
@ -339,32 +338,3 @@ func (s DistinctNetSet) String() string {
|
||||||
buf.WriteString("}")
|
buf.WriteString("}")
|
||||||
return buf.String()
|
return buf.String()
|
||||||
}
|
}
|
||||||
|
|
||||||
// IPToAddr converts net.IP to netip.Addr. Note that unlike netip.AddrFromSlice, this
|
|
||||||
// function will always ensure that the resulting Addr is IPv4 when the input is.
|
|
||||||
func IPToAddr(ip net.IP) netip.Addr {
|
|
||||||
if ip4 := ip.To4(); ip4 != nil {
|
|
||||||
addr, _ := netip.AddrFromSlice(ip4)
|
|
||||||
return addr
|
|
||||||
} else if ip6 := ip.To16(); ip6 != nil {
|
|
||||||
addr, _ := netip.AddrFromSlice(ip6)
|
|
||||||
return addr
|
|
||||||
}
|
|
||||||
return netip.Addr{}
|
|
||||||
}
|
|
||||||
|
|
||||||
// RandomAddr creates a random IP address.
|
|
||||||
func RandomAddr(rng *rand.Rand, ipv4 bool) netip.Addr {
|
|
||||||
var bytes []byte
|
|
||||||
if ipv4 || rng.Intn(2) == 0 {
|
|
||||||
bytes = make([]byte, 4)
|
|
||||||
} else {
|
|
||||||
bytes = make([]byte, 16)
|
|
||||||
}
|
|
||||||
rng.Read(bytes)
|
|
||||||
addr, ok := netip.AddrFromSlice(bytes)
|
|
||||||
if !ok {
|
|
||||||
panic(fmt.Errorf("BUG! invalid IP %v", bytes))
|
|
||||||
}
|
|
||||||
return addr
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -905,7 +905,7 @@ func (srv *Server) listenLoop() {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
remoteIP := netutil.AddrIP(fd.RemoteAddr())
|
remoteIP := netutil.AddrAddr(fd.RemoteAddr())
|
||||||
if err := srv.checkInboundConn(remoteIP); err != nil {
|
if err := srv.checkInboundConn(remoteIP); err != nil {
|
||||||
srv.log.Debug("Rejected inbound connection", "addr", fd.RemoteAddr(), "err", err)
|
srv.log.Debug("Rejected inbound connection", "addr", fd.RemoteAddr(), "err", err)
|
||||||
fd.Close()
|
fd.Close()
|
||||||
|
|
@ -924,18 +924,19 @@ func (srv *Server) listenLoop() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (srv *Server) checkInboundConn(remoteIP net.IP) error {
|
func (srv *Server) checkInboundConn(remoteIP netip.Addr) error {
|
||||||
if remoteIP == nil {
|
if !remoteIP.IsValid() {
|
||||||
|
// This case happens for internal test connections without remote address.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
// Reject connections that do not match NetRestrict.
|
// Reject connections that do not match NetRestrict.
|
||||||
if srv.NetRestrict != nil && !srv.NetRestrict.Contains(remoteIP) {
|
if srv.NetRestrict != nil && !srv.NetRestrict.ContainsAddr(remoteIP) {
|
||||||
return errors.New("not in netrestrict list")
|
return errors.New("not in netrestrict list")
|
||||||
}
|
}
|
||||||
// Reject Internet peers that try too often.
|
// Reject Internet peers that try too often.
|
||||||
now := srv.clock.Now()
|
now := srv.clock.Now()
|
||||||
srv.inboundHistory.expire(now, nil)
|
srv.inboundHistory.expire(now, nil)
|
||||||
if !netutil.IsLAN(remoteIP) && srv.inboundHistory.contains(remoteIP.String()) {
|
if !netutil.AddrIsLAN(remoteIP) && srv.inboundHistory.contains(remoteIP.String()) {
|
||||||
return errors.New("too many attempts")
|
return errors.New("too many attempts")
|
||||||
}
|
}
|
||||||
srv.inboundHistory.add(remoteIP.String(), now.Add(inboundThrottleTime))
|
srv.inboundHistory.add(remoteIP.String(), now.Add(inboundThrottleTime))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue