Skip to content

Commit

Permalink
addrmgr: Declutter package API.
Browse files Browse the repository at this point in the history
Hide functions which are only used internally.
  • Loading branch information
markusrichter authored and davecgh committed Mar 5, 2018
1 parent eb9f01f commit 33ee347
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 84 deletions.
28 changes: 14 additions & 14 deletions addrmgr/addrmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -723,7 +723,7 @@ func (a *AddrManager) HostToNetAddress(host string, port uint16, services wire.S
// ip is in the range used for Tor addresses then it will be transformed into
// the relevant .onion address.
func ipString(na *wire.NetAddress) string {
if IsOnionCatTor(na) {
if isOnionCatTor(na) {
// We know now that na.IP is long enogh.
base32 := base32.StdEncoding.EncodeToString(na.IP[6:])
return strings.ToLower(base32) + ".onion"
Expand Down Expand Up @@ -989,57 +989,57 @@ func getReachabilityFrom(localAddr, remoteAddr *wire.NetAddress) int {
return Unreachable
}

if IsOnionCatTor(remoteAddr) {
if IsOnionCatTor(localAddr) {
if isOnionCatTor(remoteAddr) {
if isOnionCatTor(localAddr) {
return Private
}

if IsRoutable(localAddr) && IsIPv4(localAddr) {
if IsRoutable(localAddr) && isIPv4(localAddr) {
return Ipv4
}

return Default
}

if IsRFC4380(remoteAddr) {
if isRFC4380(remoteAddr) {
if !IsRoutable(localAddr) {
return Default
}

if IsRFC4380(localAddr) {
if isRFC4380(localAddr) {
return Teredo
}

if IsIPv4(localAddr) {
if isIPv4(localAddr) {
return Ipv4
}

return Ipv6Weak
}

if IsIPv4(remoteAddr) {
if IsRoutable(localAddr) && IsIPv4(localAddr) {
if isIPv4(remoteAddr) {
if IsRoutable(localAddr) && isIPv4(localAddr) {
return Ipv4
}
return Unreachable
}

/* ipv6 */
var tunnelled bool
// Is our v6 is tunnelled?
if IsRFC3964(localAddr) || IsRFC6052(localAddr) || IsRFC6145(localAddr) {
// Is our v6 tunnelled?
if isRFC3964(localAddr) || isRFC6052(localAddr) || isRFC6145(localAddr) {
tunnelled = true
}

if !IsRoutable(localAddr) {
return Default
}

if IsRFC4380(localAddr) {
if isRFC4380(localAddr) {
return Teredo
}

if IsIPv4(localAddr) {
if isIPv4(localAddr) {
return Ipv4
}

Expand Down Expand Up @@ -1078,7 +1078,7 @@ func (a *AddrManager) GetBestLocalAddress(remoteAddr *wire.NetAddress) *wire.Net

// Send something unroutable if nothing suitable.
var ip net.IP
if !IsIPv4(remoteAddr) && !IsOnionCatTor(remoteAddr) {
if !isIPv4(remoteAddr) && !isOnionCatTor(remoteAddr) {
ip = net.IPv6zero
} else {
ip = net.IPv4zero
Expand Down
88 changes: 44 additions & 44 deletions addrmgr/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,28 +100,28 @@ func ipNet(ip string, ones, bits int) net.IPNet {
return net.IPNet{IP: net.ParseIP(ip), Mask: net.CIDRMask(ones, bits)}
}

// IsIPv4 returns whether or not the given address is an IPv4 address.
func IsIPv4(na *wire.NetAddress) bool {
// isIPv4 returns whether or not the given address is an IPv4 address.
func isIPv4(na *wire.NetAddress) bool {
return na.IP.To4() != nil
}

// IsLocal returns whether or not the given address is a local address.
func IsLocal(na *wire.NetAddress) bool {
// isLocal returns whether or not the given address is a local address.
func isLocal(na *wire.NetAddress) bool {
return na.IP.IsLoopback() || zero4Net.Contains(na.IP)
}

// IsOnionCatTor returns whether or not the passed address is in the IPv6 range
// isOnionCatTor returns whether or not the passed address is in the IPv6 range
// used by bitcoin to support Tor (fd87:d87e:eb43::/48). Note that this range
// is the same range used by OnionCat, which is part of the RFC4193 unique local
// IPv6 range.
func IsOnionCatTor(na *wire.NetAddress) bool {
func isOnionCatTor(na *wire.NetAddress) bool {
return onionCatNet.Contains(na.IP)
}

// IsRFC1918 returns whether or not the passed address is part of the IPv4
// isRFC1918 returns whether or not the passed address is part of the IPv4
// private network address space as defined by RFC1918 (10.0.0.0/8,
// 172.16.0.0/12, or 192.168.0.0/16).
func IsRFC1918(na *wire.NetAddress) bool {
func isRFC1918(na *wire.NetAddress) bool {
for _, rfc := range rfc1918Nets {
if rfc.Contains(na.IP) {
return true
Expand All @@ -130,58 +130,58 @@ func IsRFC1918(na *wire.NetAddress) bool {
return false
}

// IsRFC2544 returns whether or not the passed address is part of the IPv4
// isRFC2544 returns whether or not the passed address is part of the IPv4
// address space as defined by RFC2544 (198.18.0.0/15)
func IsRFC2544(na *wire.NetAddress) bool {
func isRFC2544(na *wire.NetAddress) bool {
return rfc2544Net.Contains(na.IP)
}

// IsRFC3849 returns whether or not the passed address is part of the IPv6
// isRFC3849 returns whether or not the passed address is part of the IPv6
// documentation range as defined by RFC3849 (2001:DB8::/32).
func IsRFC3849(na *wire.NetAddress) bool {
func isRFC3849(na *wire.NetAddress) bool {
return rfc3849Net.Contains(na.IP)
}

// IsRFC3927 returns whether or not the passed address is part of the IPv4
// isRFC3927 returns whether or not the passed address is part of the IPv4
// autoconfiguration range as defined by RFC3927 (169.254.0.0/16).
func IsRFC3927(na *wire.NetAddress) bool {
func isRFC3927(na *wire.NetAddress) bool {
return rfc3927Net.Contains(na.IP)
}

// IsRFC3964 returns whether or not the passed address is part of the IPv6 to
// isRFC3964 returns whether or not the passed address is part of the IPv6 to
// IPv4 encapsulation range as defined by RFC3964 (2002::/16).
func IsRFC3964(na *wire.NetAddress) bool {
func isRFC3964(na *wire.NetAddress) bool {
return rfc3964Net.Contains(na.IP)
}

// IsRFC4193 returns whether or not the passed address is part of the IPv6
// isRFC4193 returns whether or not the passed address is part of the IPv6
// unique local range as defined by RFC4193 (FC00::/7).
func IsRFC4193(na *wire.NetAddress) bool {
func isRFC4193(na *wire.NetAddress) bool {
return rfc4193Net.Contains(na.IP)
}

// IsRFC4380 returns whether or not the passed address is part of the IPv6
// isRFC4380 returns whether or not the passed address is part of the IPv6
// teredo tunneling over UDP range as defined by RFC4380 (2001::/32).
func IsRFC4380(na *wire.NetAddress) bool {
func isRFC4380(na *wire.NetAddress) bool {
return rfc4380Net.Contains(na.IP)
}

// IsRFC4843 returns whether or not the passed address is part of the IPv6
// isRFC4843 returns whether or not the passed address is part of the IPv6
// ORCHID range as defined by RFC4843 (2001:10::/28).
func IsRFC4843(na *wire.NetAddress) bool {
func isRFC4843(na *wire.NetAddress) bool {
return rfc4843Net.Contains(na.IP)
}

// IsRFC4862 returns whether or not the passed address is part of the IPv6
// isRFC4862 returns whether or not the passed address is part of the IPv6
// stateless address autoconfiguration range as defined by RFC4862 (FE80::/64).
func IsRFC4862(na *wire.NetAddress) bool {
func isRFC4862(na *wire.NetAddress) bool {
return rfc4862Net.Contains(na.IP)
}

// IsRFC5737 returns whether or not the passed address is part of the IPv4
// isRFC5737 returns whether or not the passed address is part of the IPv4
// documentation address space as defined by RFC5737 (192.0.2.0/24,
// 198.51.100.0/24, 203.0.113.0/24)
func IsRFC5737(na *wire.NetAddress) bool {
func isRFC5737(na *wire.NetAddress) bool {
for _, rfc := range rfc5737Net {
if rfc.Contains(na.IP) {
return true
Expand All @@ -191,29 +191,29 @@ func IsRFC5737(na *wire.NetAddress) bool {
return false
}

// IsRFC6052 returns whether or not the passed address is part of the IPv6
// isRFC6052 returns whether or not the passed address is part of the IPv6
// well-known prefix range as defined by RFC6052 (64:FF9B::/96).
func IsRFC6052(na *wire.NetAddress) bool {
func isRFC6052(na *wire.NetAddress) bool {
return rfc6052Net.Contains(na.IP)
}

// IsRFC6145 returns whether or not the passed address is part of the IPv6 to
// isRFC6145 returns whether or not the passed address is part of the IPv6 to
// IPv4 translated address range as defined by RFC6145 (::FFFF:0:0:0/96).
func IsRFC6145(na *wire.NetAddress) bool {
func isRFC6145(na *wire.NetAddress) bool {
return rfc6145Net.Contains(na.IP)
}

// IsRFC6598 returns whether or not the passed address is part of the IPv4
// isRFC6598 returns whether or not the passed address is part of the IPv4
// shared address space specified by RFC6598 (100.64.0.0/10)
func IsRFC6598(na *wire.NetAddress) bool {
func isRFC6598(na *wire.NetAddress) bool {
return rfc6598Net.Contains(na.IP)
}

// IsValid returns whether or not the passed address is valid. The address is
// isValid returns whether or not the passed address is valid. The address is
// considered invalid under the following circumstances:
// IPv4: It is either a zero or all bits set address.
// IPv6: It is either a zero or RFC3849 documentation address.
func IsValid(na *wire.NetAddress) bool {
func isValid(na *wire.NetAddress) bool {
// IsUnspecified returns if address is 0, so only all bits set, and
// RFC3849 need to be explicitly checked.
return na.IP != nil && !(na.IP.IsUnspecified() ||
Expand All @@ -224,10 +224,10 @@ func IsValid(na *wire.NetAddress) bool {
// the public internet. This is true as long as the address is valid and is not
// in any reserved ranges.
func IsRoutable(na *wire.NetAddress) bool {
return IsValid(na) && !(IsRFC1918(na) || IsRFC2544(na) ||
IsRFC3927(na) || IsRFC4862(na) || IsRFC3849(na) ||
IsRFC4843(na) || IsRFC5737(na) || IsRFC6598(na) ||
IsLocal(na) || (IsRFC4193(na) && !IsOnionCatTor(na)))
return isValid(na) && !(isRFC1918(na) || isRFC2544(na) ||
isRFC3927(na) || isRFC4862(na) || isRFC3849(na) ||
isRFC4843(na) || isRFC5737(na) || isRFC6598(na) ||
isLocal(na) || (isRFC4193(na) && !isOnionCatTor(na)))
}

// GroupKey returns a string representing the network group an address is part
Expand All @@ -236,27 +236,27 @@ func IsRoutable(na *wire.NetAddress) bool {
// onion address for Tor address, and the string "unroutable" for an unroutable
// address.
func GroupKey(na *wire.NetAddress) string {
if IsLocal(na) {
if isLocal(na) {
return "local"
}
if !IsRoutable(na) {
return "unroutable"
}
if IsIPv4(na) {
if isIPv4(na) {
return na.IP.Mask(net.CIDRMask(16, 32)).String()
}
if IsRFC6145(na) || IsRFC6052(na) {
if isRFC6145(na) || isRFC6052(na) {
// last four bytes are the ip address
ip := na.IP[12:16]
return ip.Mask(net.CIDRMask(16, 32)).String()
}

if IsRFC3964(na) {
if isRFC3964(na) {
ip := na.IP[2:6]
return ip.Mask(net.CIDRMask(16, 32)).String()

}
if IsRFC4380(na) {
if isRFC4380(na) {
// teredo tunnels have the last 4 bytes as the v4 address XOR
// 0xff.
ip := net.IP(make([]byte, 4))
Expand All @@ -265,7 +265,7 @@ func GroupKey(na *wire.NetAddress) string {
}
return ip.Mask(net.CIDRMask(16, 32)).String()
}
if IsOnionCatTor(na) {
if isOnionCatTor(na) {
// group is keyed off the first 4 bits of the actual onion key.
return fmt.Sprintf("tor:%d", na.IP[6]&((1<<4)-1))
}
Expand Down
51 changes: 25 additions & 26 deletions addrmgr/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,12 @@
// Use of this source code is governed by an ISC
// license that can be found in the LICENSE file.

package addrmgr_test
package addrmgr

import (
"net"
"testing"

"github.com/decred/dcrd/addrmgr"
"github.com/decred/dcrd/wire"
)

Expand Down Expand Up @@ -89,55 +88,55 @@ func TestIPTypes(t *testing.T) {

t.Logf("Running %d tests", len(tests))
for _, test := range tests {
if rv := addrmgr.IsRFC1918(&test.in); rv != test.rfc1918 {
t.Errorf("IsRFC1918 %s\n got: %v want: %v", test.in.IP, rv, test.rfc1918)
if rv := isRFC1918(&test.in); rv != test.rfc1918 {
t.Errorf("isRFC1918 %s\n got: %v want: %v", test.in.IP, rv, test.rfc1918)
}

if rv := addrmgr.IsRFC3849(&test.in); rv != test.rfc3849 {
t.Errorf("IsRFC3849 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3849)
if rv := isRFC3849(&test.in); rv != test.rfc3849 {
t.Errorf("isRFC3849 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3849)
}

if rv := addrmgr.IsRFC3927(&test.in); rv != test.rfc3927 {
t.Errorf("IsRFC3927 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3927)
if rv := isRFC3927(&test.in); rv != test.rfc3927 {
t.Errorf("isRFC3927 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3927)
}

if rv := addrmgr.IsRFC3964(&test.in); rv != test.rfc3964 {
t.Errorf("IsRFC3964 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3964)
if rv := isRFC3964(&test.in); rv != test.rfc3964 {
t.Errorf("isRFC3964 %s\n got: %v want: %v", test.in.IP, rv, test.rfc3964)
}

if rv := addrmgr.IsRFC4193(&test.in); rv != test.rfc4193 {
t.Errorf("IsRFC4193 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4193)
if rv := isRFC4193(&test.in); rv != test.rfc4193 {
t.Errorf("isRFC4193 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4193)
}

if rv := addrmgr.IsRFC4380(&test.in); rv != test.rfc4380 {
t.Errorf("IsRFC4380 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4380)
if rv := isRFC4380(&test.in); rv != test.rfc4380 {
t.Errorf("isRFC4380 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4380)
}

if rv := addrmgr.IsRFC4843(&test.in); rv != test.rfc4843 {
t.Errorf("IsRFC4843 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4843)
if rv := isRFC4843(&test.in); rv != test.rfc4843 {
t.Errorf("isRFC4843 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4843)
}

if rv := addrmgr.IsRFC4862(&test.in); rv != test.rfc4862 {
t.Errorf("IsRFC4862 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4862)
if rv := isRFC4862(&test.in); rv != test.rfc4862 {
t.Errorf("isRFC4862 %s\n got: %v want: %v", test.in.IP, rv, test.rfc4862)
}

if rv := addrmgr.IsRFC6052(&test.in); rv != test.rfc6052 {
if rv := isRFC6052(&test.in); rv != test.rfc6052 {
t.Errorf("isRFC6052 %s\n got: %v want: %v", test.in.IP, rv, test.rfc6052)
}

if rv := addrmgr.IsRFC6145(&test.in); rv != test.rfc6145 {
t.Errorf("IsRFC1918 %s\n got: %v want: %v", test.in.IP, rv, test.rfc6145)
if rv := isRFC6145(&test.in); rv != test.rfc6145 {
t.Errorf("isRFC1918 %s\n got: %v want: %v", test.in.IP, rv, test.rfc6145)
}

if rv := addrmgr.IsLocal(&test.in); rv != test.local {
t.Errorf("IsLocal %s\n got: %v want: %v", test.in.IP, rv, test.local)
if rv := isLocal(&test.in); rv != test.local {
t.Errorf("isLocal %s\n got: %v want: %v", test.in.IP, rv, test.local)
}

if rv := addrmgr.IsValid(&test.in); rv != test.valid {
if rv := isValid(&test.in); rv != test.valid {
t.Errorf("IsValid %s\n got: %v want: %v", test.in.IP, rv, test.valid)
}

if rv := addrmgr.IsRoutable(&test.in); rv != test.routable {
if rv := IsRoutable(&test.in); rv != test.routable {
t.Errorf("IsRoutable %s\n got: %v want: %v", test.in.IP, rv, test.routable)
}
}
Expand Down Expand Up @@ -194,7 +193,7 @@ func TestGroupKey(t *testing.T) {
for i, test := range tests {
nip := net.ParseIP(test.ip)
na := *wire.NewNetAddressIPPort(nip, 8333, wire.SFNodeNetwork)
if key := addrmgr.GroupKey(&na); key != test.expected {
if key := GroupKey(&na); key != test.expected {
t.Errorf("TestGroupKey #%d (%s): unexpected group key "+
"- got '%s', want '%s'", i, test.name,
key, test.expected)
Expand Down

0 comments on commit 33ee347

Please sign in to comment.