github.com/aigarnetwork/aigar@v0.0.0-20191115204914-d59a6eb70f8e/p2p/discover/common.go (about)

     1  //  Copyright 2018 The go-ethereum Authors
     2  //  Copyright 2019 The go-aigar Authors
     3  //  This file is part of the go-aigar library.
     4  //
     5  //  The go-aigar library is free software: you can redistribute it and/or modify
     6  //  it under the terms of the GNU Lesser General Public License as published by
     7  //  the Free Software Foundation, either version 3 of the License, or
     8  //  (at your option) any later version.
     9  //
    10  //  The go-aigar library is distributed in the hope that it will be useful,
    11  //  but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    13  //  GNU Lesser General Public License for more details.
    14  //
    15  //  You should have received a copy of the GNU Lesser General Public License
    16  //  along with the go-aigar library. If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package discover
    19  
    20  import (
    21  	"crypto/ecdsa"
    22  	"net"
    23  
    24  	"github.com/AigarNetwork/aigar/log"
    25  	"github.com/AigarNetwork/aigar/p2p/enode"
    26  	"github.com/AigarNetwork/aigar/p2p/netutil"
    27  )
    28  
    29  // UDPConn is a network connection on which discovery can operate.
    30  type UDPConn interface {
    31  	ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
    32  	WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)
    33  	Close() error
    34  	LocalAddr() net.Addr
    35  }
    36  
    37  // Config holds settings for the discovery listener.
    38  type Config struct {
    39  	// These settings are required and configure the UDP listener:
    40  	PrivateKey *ecdsa.PrivateKey
    41  
    42  	// These settings are optional:
    43  	NetRestrict *netutil.Netlist  // network whitelist
    44  	Bootnodes   []*enode.Node     // list of bootstrap nodes
    45  	Unhandled   chan<- ReadPacket // unhandled packets are sent on this channel
    46  	Log         log.Logger        // if set, log messages go here
    47  }
    48  
    49  // ListenUDP starts listening for discovery packets on the given UDP socket.
    50  func ListenUDP(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) {
    51  	return ListenV4(c, ln, cfg)
    52  }
    53  
    54  // ReadPacket is a packet that couldn't be handled. Those packets are sent to the unhandled
    55  // channel if configured. This is exported for internal use, do not use this type.
    56  type ReadPacket struct {
    57  	Data []byte
    58  	Addr *net.UDPAddr
    59  }