github.com/reapchain/go-reapchain@v0.2.15-0.20210609012950-9735c110c705/p2p/netutil/net.go (about)

     1  // Copyright 2016 The go-ethereum Authors
     2  // This file is part of the go-ethereum library.
     3  //
     4  // The go-ethereum library is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU Lesser General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // The go-ethereum library is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU Lesser General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU Lesser General Public License
    15  // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  // Package netutil contains extensions to the net package.
    18  package netutil
    19  
    20  import (
    21  	"errors"
    22  	"net"
    23  	"strings"
    24  )
    25  
    26  var lan4, lan6, special4, special6 Netlist
    27  
    28  func init() {
    29  	// Lists from RFC 5735, RFC 5156,
    30  	// https://www.iana.org/assignments/iana-ipv4-special-registry/
    31  	lan4.Add("0.0.0.0/8")              // "This" network
    32  	lan4.Add("10.0.0.0/8")             // Private Use
    33  	lan4.Add("172.16.0.0/12")          // Private Use
    34  	// lan4.Add("192.168.0.0/16")           // Private Use, disable temporarily
    35  	lan4.Add("192.168.0.0/24")         // Private Use for a office
    36  	lan6.Add("fe80::/10")              // Link-Local
    37  	lan6.Add("fc00::/7")               // Unique-Local
    38  	special4.Add("192.0.0.0/29")       // IPv4 Service Continuity
    39  	special4.Add("192.0.0.9/32")       // PCP Anycast
    40  	special4.Add("192.0.0.170/32")     // NAT64/DNS64 Discovery
    41  	special4.Add("192.0.0.171/32")     // NAT64/DNS64 Discovery
    42  	special4.Add("192.0.2.0/24")       // TEST-NET-1
    43  	special4.Add("192.31.196.0/24")    // AS112
    44  	special4.Add("192.52.193.0/24")    // AMT
    45  	special4.Add("192.88.99.0/24")     // 6to4 Relay Anycast
    46  	special4.Add("192.175.48.0/24")    // AS112
    47  	special4.Add("198.18.0.0/15")      // Device Benchmark Testing
    48  	special4.Add("198.51.100.0/24")    // TEST-NET-2
    49  	special4.Add("203.0.113.0/24")     // TEST-NET-3
    50  	special4.Add("255.255.255.255/32") // Limited Broadcast
    51  
    52  	// http://www.iana.org/assignments/iana-ipv6-special-registry/
    53  	special6.Add("100::/64")
    54  	special6.Add("2001::/32")
    55  	special6.Add("2001:1::1/128")
    56  	special6.Add("2001:2::/48")
    57  	special6.Add("2001:3::/32")
    58  	special6.Add("2001:4:112::/48")
    59  	special6.Add("2001:5::/32")
    60  	special6.Add("2001:10::/28")
    61  	special6.Add("2001:20::/28")
    62  	special6.Add("2001:db8::/32")
    63  	special6.Add("2002::/16")
    64  }
    65  
    66  // Netlist is a list of IP networks.
    67  type Netlist []net.IPNet
    68  
    69  // ParseNetlist parses a comma-separated list of CIDR masks.
    70  // Whitespace and extra commas are ignored.
    71  func ParseNetlist(s string) (*Netlist, error) {
    72  	ws := strings.NewReplacer(" ", "", "\n", "", "\t", "")
    73  	masks := strings.Split(ws.Replace(s), ",")
    74  	l := make(Netlist, 0)
    75  	for _, mask := range masks {
    76  		if mask == "" {
    77  			continue
    78  		}
    79  		_, n, err := net.ParseCIDR(mask)
    80  		if err != nil {
    81  			return nil, err
    82  		}
    83  		l = append(l, *n)
    84  	}
    85  	return &l, nil
    86  }
    87  
    88  // MarshalTOML implements toml.MarshalerRec.
    89  func (l Netlist) MarshalTOML() interface{} {
    90  	list := make([]string, 0, len(l))
    91  	for _, net := range l {
    92  		list = append(list, net.String())
    93  	}
    94  	return list
    95  }
    96  
    97  // UnmarshalTOML implements toml.UnmarshalerRec.
    98  func (l *Netlist) UnmarshalTOML(fn func(interface{}) error) error {
    99  	var masks []string
   100  	if err := fn(&masks); err != nil {
   101  		return err
   102  	}
   103  	for _, mask := range masks {
   104  		_, n, err := net.ParseCIDR(mask)
   105  		if err != nil {
   106  			return err
   107  		}
   108  		*l = append(*l, *n)
   109  	}
   110  	return nil
   111  }
   112  
   113  // Add parses a CIDR mask and appends it to the list. It panics for invalid masks and is
   114  // intended to be used for setting up static lists.
   115  func (l *Netlist) Add(cidr string) {
   116  	_, n, err := net.ParseCIDR(cidr)
   117  	if err != nil {
   118  		panic(err)
   119  	}
   120  	*l = append(*l, *n)
   121  }
   122  
   123  // Contains reports whether the given IP is contained in the list.
   124  func (l *Netlist) Contains(ip net.IP) bool {
   125  	if l == nil {
   126  		return false
   127  	}
   128  	for _, net := range *l {
   129  		if net.Contains(ip) {
   130  			return true
   131  		}
   132  	}
   133  	return false
   134  }
   135  
   136  // IsLAN reports whether an IP is a local network address.
   137  func IsLAN(ip net.IP) bool {
   138  	if ip.IsLoopback() {
   139  		return true
   140  	}
   141  	if v4 := ip.To4(); v4 != nil {
   142  		return lan4.Contains(v4)
   143  	}
   144  	return lan6.Contains(ip)
   145  }
   146  
   147  // IsSpecialNetwork reports whether an IP is located in a special-use network range
   148  // This includes broadcast, multicast and documentation addresses.
   149  func IsSpecialNetwork(ip net.IP) bool {
   150  	if ip.IsMulticast() {
   151  		return true
   152  	}
   153  	if v4 := ip.To4(); v4 != nil {
   154  		return special4.Contains(v4)
   155  	}
   156  	return special6.Contains(ip)
   157  }
   158  
   159  var (
   160  	errInvalid     = errors.New("invalid IP")
   161  	errUnspecified = errors.New("zero address")
   162  	errSpecial     = errors.New("special network")
   163  	errLoopback    = errors.New("loopback address from non-loopback host")
   164  	errLAN         = errors.New("LAN address from WAN host")
   165  )
   166  
   167  // CheckRelayIP reports whether an IP relayed from the given sender IP
   168  // is a valid connection target.
   169  //
   170  // There are four rules:
   171  //   - Special network addresses are never valid.
   172  //   - Loopback addresses are OK if relayed by a loopback host.
   173  //   - LAN addresses are OK if relayed by a LAN host.
   174  //   - All other addresses are always acceptable.
   175  func CheckRelayIP(sender, addr net.IP) error {
   176  	if len(addr) != net.IPv4len && len(addr) != net.IPv6len {
   177  		return errInvalid
   178  	}
   179  	if addr.IsUnspecified() {
   180  		return errUnspecified
   181  	}
   182  	if IsSpecialNetwork(addr) {
   183  		return errSpecial
   184  	}
   185  	if addr.IsLoopback() && !sender.IsLoopback() {
   186  		return errLoopback
   187  	}
   188  	if IsLAN(addr) && !IsLAN(sender) {
   189  		return errLAN
   190  	}
   191  	return nil
   192  }