github.com/BlockABC/godash@v0.0.0-20191112120524-f4aa3a32c566/addrmgr/network.go (about)

     1  // Copyright (c) 2013-2014 The btcsuite developers
     2  // Copyright (c) 2016 The Dash developers
     3  // Use of this source code is governed by an ISC
     4  // license that can be found in the LICENSE file.
     5  
     6  package addrmgr
     7  
     8  import (
     9  	"fmt"
    10  	"net"
    11  
    12  	"github.com/BlockABC/godash/wire"
    13  )
    14  
    15  var (
    16  	// rfc1918Nets specifies the IPv4 private address blocks as defined by
    17  	// by RFC1918 (10.0.0.0/8, 172.16.0.0/12, and 192.168.0.0/16).
    18  	rfc1918Nets = []net.IPNet{
    19  		ipNet("10.0.0.0", 8, 32),
    20  		ipNet("172.16.0.0", 12, 32),
    21  		ipNet("192.168.0.0", 16, 32),
    22  	}
    23  
    24  	// rfc2544Net specifies the the IPv4 block as defined by RFC2544
    25  	// (198.18.0.0/15)
    26  	rfc2544Net = ipNet("198.18.0.0", 15, 32)
    27  
    28  	// rfc3849Net specifies the IPv6 documentation address block as defined
    29  	// by RFC3849 (2001:DB8::/32).
    30  	rfc3849Net = ipNet("2001:DB8::", 32, 128)
    31  
    32  	// rfc3927Net specifies the IPv4 auto configuration address block as
    33  	// defined by RFC3927 (169.254.0.0/16).
    34  	rfc3927Net = ipNet("169.254.0.0", 16, 32)
    35  
    36  	// rfc3964Net specifies the IPv6 to IPv4 encapsulation address block as
    37  	// defined by RFC3964 (2002::/16).
    38  	rfc3964Net = ipNet("2002::", 16, 128)
    39  
    40  	// rfc4193Net specifies the IPv6 unique local address block as defined
    41  	// by RFC4193 (FC00::/7).
    42  	rfc4193Net = ipNet("FC00::", 7, 128)
    43  
    44  	// rfc4380Net specifies the IPv6 teredo tunneling over UDP address block
    45  	// as defined by RFC4380 (2001::/32).
    46  	rfc4380Net = ipNet("2001::", 32, 128)
    47  
    48  	// rfc4843Net specifies the IPv6 ORCHID address block as defined by
    49  	// RFC4843 (2001:10::/28).
    50  	rfc4843Net = ipNet("2001:10::", 28, 128)
    51  
    52  	// rfc4862Net specifies the IPv6 stateless address autoconfiguration
    53  	// address block as defined by RFC4862 (FE80::/64).
    54  	rfc4862Net = ipNet("FE80::", 64, 128)
    55  
    56  	// rfc5737Net specifies the IPv4 documentation address blocks as defined
    57  	// by RFC5737 (192.0.2.0/24, 198.51.100.0/24, 203.0.113.0/24)
    58  	rfc5737Net = []net.IPNet{
    59  		ipNet("192.0.2.0", 24, 32),
    60  		ipNet("198.51.100.0", 24, 32),
    61  		ipNet("203.0.113.0", 24, 32),
    62  	}
    63  
    64  	// rfc6052Net specifies the IPv6 well-known prefix address block as
    65  	// defined by RFC6052 (64:FF9B::/96).
    66  	rfc6052Net = ipNet("64:FF9B::", 96, 128)
    67  
    68  	// rfc6145Net specifies the IPv6 to IPv4 translated address range as
    69  	// defined by RFC6145 (::FFFF:0:0:0/96).
    70  	rfc6145Net = ipNet("::FFFF:0:0:0", 96, 128)
    71  
    72  	// rfc6598Net specifies the IPv4 block as defined by RFC6598 (100.64.0.0/10)
    73  	rfc6598Net = ipNet("100.64.0.0", 10, 32)
    74  
    75  	// onionCatNet defines the IPv6 address block used to support Tor.
    76  	// bitcoind encodes a .onion address as a 16 byte number by decoding the
    77  	// address prior to the .onion (i.e. the key hash) base32 into a ten
    78  	// byte number. It then stores the first 6 bytes of the address as
    79  	// 0xfd, 0x87, 0xd8, 0x7e, 0xeb, 0x43.
    80  	//
    81  	// This is the same range used by OnionCat, which is part part of the
    82  	// RFC4193 unique local IPv6 range.
    83  	//
    84  	// In summary the format is:
    85  	// { magic 6 bytes, 10 bytes base32 decode of key hash }
    86  	onionCatNet = ipNet("fd87:d87e:eb43::", 48, 128)
    87  
    88  	// zero4Net defines the IPv4 address block for address staring with 0
    89  	// (0.0.0.0/8).
    90  	zero4Net = ipNet("0.0.0.0", 8, 32)
    91  
    92  	// heNet defines the Hurricane Electric IPv6 address block.
    93  	heNet = ipNet("2001:470::", 32, 128)
    94  )
    95  
    96  // ipNet returns a net.IPNet struct given the passed IP address string, number
    97  // of one bits to include at the start of the mask, and the total number of bits
    98  // for the mask.
    99  func ipNet(ip string, ones, bits int) net.IPNet {
   100  	return net.IPNet{IP: net.ParseIP(ip), Mask: net.CIDRMask(ones, bits)}
   101  }
   102  
   103  // IsIPv4 returns whether or not the given address is an IPv4 address.
   104  func IsIPv4(na *wire.NetAddress) bool {
   105  	return na.IP.To4() != nil
   106  }
   107  
   108  // IsLocal returns whether or not the given address is a local address.
   109  func IsLocal(na *wire.NetAddress) bool {
   110  	return na.IP.IsLoopback() || zero4Net.Contains(na.IP)
   111  }
   112  
   113  // IsOnionCatTor returns whether or not the passed address is in the IPv6 range
   114  // used by bitcoin to support Tor (fd87:d87e:eb43::/48).  Note that this range
   115  // is the same range used by OnionCat, which is part of the RFC4193 unique local
   116  // IPv6 range.
   117  func IsOnionCatTor(na *wire.NetAddress) bool {
   118  	return onionCatNet.Contains(na.IP)
   119  }
   120  
   121  // IsRFC1918 returns whether or not the passed address is part of the IPv4
   122  // private network address space as defined by RFC1918 (10.0.0.0/8,
   123  // 172.16.0.0/12, or 192.168.0.0/16).
   124  func IsRFC1918(na *wire.NetAddress) bool {
   125  	for _, rfc := range rfc1918Nets {
   126  		if rfc.Contains(na.IP) {
   127  			return true
   128  		}
   129  	}
   130  	return false
   131  }
   132  
   133  // IsRFC2544 returns whether or not the passed address is part of the IPv4
   134  // address space as defined by RFC2544 (198.18.0.0/15)
   135  func IsRFC2544(na *wire.NetAddress) bool {
   136  	return rfc2544Net.Contains(na.IP)
   137  }
   138  
   139  // IsRFC3849 returns whether or not the passed address is part of the IPv6
   140  // documentation range as defined by RFC3849 (2001:DB8::/32).
   141  func IsRFC3849(na *wire.NetAddress) bool {
   142  	return rfc3849Net.Contains(na.IP)
   143  }
   144  
   145  // IsRFC3927 returns whether or not the passed address is part of the IPv4
   146  // autoconfiguration range as defined by RFC3927 (169.254.0.0/16).
   147  func IsRFC3927(na *wire.NetAddress) bool {
   148  	return rfc3927Net.Contains(na.IP)
   149  }
   150  
   151  // IsRFC3964 returns whether or not the passed address is part of the IPv6 to
   152  // IPv4 encapsulation range as defined by RFC3964 (2002::/16).
   153  func IsRFC3964(na *wire.NetAddress) bool {
   154  	return rfc3964Net.Contains(na.IP)
   155  }
   156  
   157  // IsRFC4193 returns whether or not the passed address is part of the IPv6
   158  // unique local range as defined by RFC4193 (FC00::/7).
   159  func IsRFC4193(na *wire.NetAddress) bool {
   160  	return rfc4193Net.Contains(na.IP)
   161  }
   162  
   163  // IsRFC4380 returns whether or not the passed address is part of the IPv6
   164  // teredo tunneling over UDP range as defined by RFC4380 (2001::/32).
   165  func IsRFC4380(na *wire.NetAddress) bool {
   166  	return rfc4380Net.Contains(na.IP)
   167  }
   168  
   169  // IsRFC4843 returns whether or not the passed address is part of the IPv6
   170  // ORCHID range as defined by RFC4843 (2001:10::/28).
   171  func IsRFC4843(na *wire.NetAddress) bool {
   172  	return rfc4843Net.Contains(na.IP)
   173  }
   174  
   175  // IsRFC4862 returns whether or not the passed address is part of the IPv6
   176  // stateless address autoconfiguration range as defined by RFC4862 (FE80::/64).
   177  func IsRFC4862(na *wire.NetAddress) bool {
   178  	return rfc4862Net.Contains(na.IP)
   179  }
   180  
   181  // IsRFC5737 returns whether or not the passed address is part of the IPv4
   182  // documentation address space as defined by RFC5737 (192.0.2.0/24,
   183  // 198.51.100.0/24, 203.0.113.0/24)
   184  func IsRFC5737(na *wire.NetAddress) bool {
   185  	for _, rfc := range rfc5737Net {
   186  		if rfc.Contains(na.IP) {
   187  			return true
   188  		}
   189  	}
   190  
   191  	return false
   192  }
   193  
   194  // IsRFC6052 returns whether or not the passed address is part of the IPv6
   195  // well-known prefix range as defined by RFC6052 (64:FF9B::/96).
   196  func IsRFC6052(na *wire.NetAddress) bool {
   197  	return rfc6052Net.Contains(na.IP)
   198  }
   199  
   200  // IsRFC6145 returns whether or not the passed address is part of the IPv6 to
   201  // IPv4 translated address range as defined by RFC6145 (::FFFF:0:0:0/96).
   202  func IsRFC6145(na *wire.NetAddress) bool {
   203  	return rfc6145Net.Contains(na.IP)
   204  }
   205  
   206  // IsRFC6598 returns whether or not the passed address is part of the IPv4
   207  // shared address space specified by RFC6598 (100.64.0.0/10)
   208  func IsRFC6598(na *wire.NetAddress) bool {
   209  	return rfc6598Net.Contains(na.IP)
   210  }
   211  
   212  // IsValid returns whether or not the passed address is valid.  The address is
   213  // considered invalid under the following circumstances:
   214  // IPv4: It is either a zero or all bits set address.
   215  // IPv6: It is either a zero or RFC3849 documentation address.
   216  func IsValid(na *wire.NetAddress) bool {
   217  	// IsUnspecified returns if address is 0, so only all bits set, and
   218  	// RFC3849 need to be explicitly checked.
   219  	return na.IP != nil && !(na.IP.IsUnspecified() ||
   220  		na.IP.Equal(net.IPv4bcast))
   221  }
   222  
   223  // IsRoutable returns whether or not the passed address is routable over
   224  // the public internet.  This is true as long as the address is valid and is not
   225  // in any reserved ranges.
   226  func IsRoutable(na *wire.NetAddress) bool {
   227  	return IsValid(na) && !(IsRFC1918(na) || IsRFC2544(na) ||
   228  		IsRFC3927(na) || IsRFC4862(na) || IsRFC3849(na) ||
   229  		IsRFC4843(na) || IsRFC5737(na) || IsRFC6598(na) ||
   230  		IsLocal(na) || (IsRFC4193(na) && !IsOnionCatTor(na)))
   231  }
   232  
   233  // GroupKey returns a string representing the network group an address is part
   234  // of.  This is the /16 for IPv4, the /32 (/36 for he.net) for IPv6, the string
   235  // "local" for a local address, the string "tor:key" where key is the /4 of the
   236  // onion address for tor address, and the string "unroutable" for an unroutable
   237  // address.
   238  func GroupKey(na *wire.NetAddress) string {
   239  	if IsLocal(na) {
   240  		return "local"
   241  	}
   242  	if !IsRoutable(na) {
   243  		return "unroutable"
   244  	}
   245  	if IsIPv4(na) {
   246  		return na.IP.Mask(net.CIDRMask(16, 32)).String()
   247  	}
   248  	if IsRFC6145(na) || IsRFC6052(na) {
   249  		// last four bytes are the ip address
   250  		ip := net.IP(na.IP[12:16])
   251  		return ip.Mask(net.CIDRMask(16, 32)).String()
   252  	}
   253  
   254  	if IsRFC3964(na) {
   255  		ip := net.IP(na.IP[2:6])
   256  		return ip.Mask(net.CIDRMask(16, 32)).String()
   257  
   258  	}
   259  	if IsRFC4380(na) {
   260  		// teredo tunnels have the last 4 bytes as the v4 address XOR
   261  		// 0xff.
   262  		ip := net.IP(make([]byte, 4))
   263  		for i, byte := range na.IP[12:16] {
   264  			ip[i] = byte ^ 0xff
   265  		}
   266  		return ip.Mask(net.CIDRMask(16, 32)).String()
   267  	}
   268  	if IsOnionCatTor(na) {
   269  		// group is keyed off the first 4 bits of the actual onion key.
   270  		return fmt.Sprintf("tor:%d", na.IP[6]&((1<<4)-1))
   271  	}
   272  
   273  	// OK, so now we know ourselves to be a IPv6 address.
   274  	// bitcoind uses /32 for everything, except for Hurricane Electric's
   275  	// (he.net) IP range, which it uses /36 for.
   276  	bits := 32
   277  	if heNet.Contains(na.IP) {
   278  		bits = 36
   279  	}
   280  
   281  	return na.IP.Mask(net.CIDRMask(bits, 128)).String()
   282  }