Commit 2945cfc2 authored by vyzo's avatar vyzo Committed by Steven Allen

add IsPrivateAddr

parent 49e7bdea
......@@ -61,6 +61,21 @@ func IsPublicAddr(a ma.Multiaddr) bool {
return false
}
// IsPrivateAddr returns true if the IP part of the mutiadr is in a private network
func IsPrivateAddr(a ma.Multiaddr) bool {
ip, err := a.ValueForProtocol(ma.P_IP4)
if err == nil {
return inAddrRange(ip, Private4)
}
ip, err = a.ValueForProtocol(ma.P_IP6)
if err == nil {
return inAddrRange(ip, Private6)
}
return false
}
func inAddrRange(s string, ipnets []*net.IPNet) bool {
ip := net.ParseIP(s)
for _, ipnet := range ipnets {
......
......@@ -16,6 +16,10 @@ func TestIsPublicAddr(t *testing.T) {
t.Fatal("192.168.1.1 is not a public address!")
}
if !IsPrivateAddr(a) {
t.Fatal("192.168.1.1 is a private address!")
}
a, err = ma.NewMultiaddr("/ip4/1.1.1.1/tcp/80")
if err != nil {
t.Fatal(err)
......@@ -24,4 +28,8 @@ func TestIsPublicAddr(t *testing.T) {
if !IsPublicAddr(a) {
t.Fatal("1.1.1.1 is a public address!")
}
if IsPrivateAddr(a) {
t.Fatal("1.1.1.1 is not a private address!")
}
}
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