mirror of
https://github.com/logos-messaging/go-multiaddr.git
synced 2026-01-03 21:43:06 +00:00
Make the IP loopback check faster
Decapsulate currently allocates a lot and this appears to have been causing some issues (hard to tell, pprof is misbehaving). Note: I removed the TODO as this function now *only* checks if we're dealing with a loopback address. Not sure what `OverIPLoopback` was supposed to mean. Note 2: Decapsulate actually checked if the IP6 loopback addresses appeared *anywhere* in the multiadder. However, as we weren't doing this for IP4, I decided to simplify this and only check prefixes.
This commit is contained in:
parent
6040dff26d
commit
9d1c543b50
23
ip.go
23
ip.go
@ -51,22 +51,21 @@ func IsThinWaist(m ma.Multiaddr) bool {
|
||||
}
|
||||
}
|
||||
|
||||
var localPrefixes = [][]byte{
|
||||
{ma.P_IP4, 127}, // 127.*
|
||||
IP6LinkLocalLoopback.Bytes(),
|
||||
IP6Loopback.Bytes(),
|
||||
}
|
||||
|
||||
// IsIPLoopback returns whether a Multiaddr is a "Loopback" IP address
|
||||
// This means either /ip4/127.0.0.1 or /ip6/::1
|
||||
// TODO: differentiate IsIPLoopback and OverIPLoopback
|
||||
// This means either /ip4/127.*.*.*, /ip6/::1, or /ip6/fe80::1
|
||||
func IsIPLoopback(m ma.Multiaddr) bool {
|
||||
b := m.Bytes()
|
||||
|
||||
// /ip4/127 prefix (_entire_ /8 is loopback...)
|
||||
if bytes.HasPrefix(b, []byte{ma.P_IP4, 127}) {
|
||||
return true
|
||||
for _, prefix := range localPrefixes {
|
||||
if bytes.HasPrefix(b, prefix) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
// /ip6/::1
|
||||
if !m.Decapsulate(IP6Loopback).Equal(m) || !m.Decapsulate(IP6LinkLocalLoopback).Equal(m) {
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user