Commit 9d1c543b authored by Steven Allen's avatar Steven Allen

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.
parent 6040dff2
......@@ -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}) {
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
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment