github.com/unicornultrafoundation/go-u2u@v1.0.0-rc1.0.20240205080301-e74a83d3fadc/p2p/discover/common.go (about)

     1  // Copyright 2019 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 discover
    18  
    19  import (
    20  	"crypto/ecdsa"
    21  	"net"
    22  
    23  	"github.com/unicornultrafoundation/go-u2u/common/mclock"
    24  	"github.com/unicornultrafoundation/go-u2u/log"
    25  	"github.com/unicornultrafoundation/go-u2u/p2p/enode"
    26  	"github.com/unicornultrafoundation/go-u2u/p2p/enr"
    27  	"github.com/unicornultrafoundation/go-u2u/p2p/netutil"
    28  )
    29  
    30  // UDPConn is a network connection on which discovery can operate.
    31  type UDPConn interface {
    32  	ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
    33  	WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)
    34  	Close() error
    35  	LocalAddr() net.Addr
    36  }
    37  
    38  // Config holds settings for the discovery listener.
    39  type Config struct {
    40  	// These settings are required and configure the UDP listener:
    41  	PrivateKey *ecdsa.PrivateKey
    42  
    43  	// These settings are optional:
    44  	NetRestrict  *netutil.Netlist   // list of allowed IP networks
    45  	IPRestrict   []string           // list of allowed IP addresses
    46  	PrivateNodes []*enode.Node      // list of private enodes
    47  	Bootnodes    []*enode.Node      // list of bootstrap nodes
    48  	Unhandled    chan<- ReadPacket  // unhandled packets are sent on this channel
    49  	Log          log.Logger         // if set, log messages go here
    50  	ValidSchemes enr.IdentityScheme // allowed identity schemes
    51  	Clock        mclock.Clock
    52  }
    53  
    54  func (cfg Config) withDefaults() Config {
    55  	if cfg.Log == nil {
    56  		cfg.Log = log.Root()
    57  	}
    58  	if cfg.ValidSchemes == nil {
    59  		cfg.ValidSchemes = enode.ValidSchemes
    60  	}
    61  	if cfg.Clock == nil {
    62  		cfg.Clock = mclock.System{}
    63  	}
    64  	return cfg
    65  }
    66  
    67  // ListenUDP starts listening for discovery packets on the given UDP socket.
    68  func ListenUDP(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) {
    69  	return ListenV4(c, ln, cfg)
    70  }
    71  
    72  // ReadPacket is a packet that couldn't be handled. Those packets are sent to the unhandled
    73  // channel if configured.
    74  type ReadPacket struct {
    75  	Data []byte
    76  	Addr *net.UDPAddr
    77  }
    78  
    79  func min(x, y int) int {
    80  	if x > y {
    81  		return y
    82  	}
    83  	return x
    84  }
    85  
    86  // contains checks if a string is present in a slice
    87  func has(s []string, str string) bool {
    88  	for _, v := range s {
    89  		if v == str {
    90  			return true
    91  		}
    92  	}
    93  
    94  	return false
    95  }
    96  
    97  func containsEnode(nodes []*enode.Node, node *enode.Node) bool {
    98  	for _, n := range nodes {
    99  		if n.IP().Equal(node.IP()) || n.ID() == node.ID() {
   100  			return true
   101  		}
   102  	}
   103  
   104  	return false
   105  }