github.com/c2s/go-ethereum@v1.9.7/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/ethereum/go-ethereum/log"
    24  	"github.com/ethereum/go-ethereum/p2p/enode"
    25  	"github.com/ethereum/go-ethereum/p2p/netutil"
    26  )
    27  
    28  // UDPConn is a network connection on which discovery can operate.
    29  type UDPConn interface {
    30  	ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
    31  	WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)
    32  	Close() error
    33  	LocalAddr() net.Addr
    34  }
    35  
    36  // Config holds settings for the discovery listener.
    37  type Config struct {
    38  	// These settings are required and configure the UDP listener:
    39  	PrivateKey *ecdsa.PrivateKey
    40  
    41  	// These settings are optional:
    42  	NetRestrict *netutil.Netlist  // network whitelist
    43  	Bootnodes   []*enode.Node     // list of bootstrap nodes
    44  	Unhandled   chan<- ReadPacket // unhandled packets are sent on this channel
    45  	Log         log.Logger        // if set, log messages go here
    46  }
    47  
    48  // ListenUDP starts listening for discovery packets on the given UDP socket.
    49  func ListenUDP(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) {
    50  	return ListenV4(c, ln, cfg)
    51  }
    52  
    53  // ReadPacket is a packet that couldn't be handled. Those packets are sent to the unhandled
    54  // channel if configured. This is exported for internal use, do not use this type.
    55  type ReadPacket struct {
    56  	Data []byte
    57  	Addr *net.UDPAddr
    58  }