github.com/dominant-strategies/go-quai@v0.28.2/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/dominant-strategies/go-quai/common/mclock" 24 "github.com/dominant-strategies/go-quai/log" 25 "github.com/dominant-strategies/go-quai/p2p/enode" 26 "github.com/dominant-strategies/go-quai/p2p/enr" 27 "github.com/dominant-strategies/go-quai/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 Bootnodes []*enode.Node // list of bootstrap nodes 46 Unhandled chan<- ReadPacket // unhandled packets are sent on this channel 47 Log *log.Logger // if set, log messages go here 48 ValidSchemes enr.IdentityScheme // allowed identity schemes 49 Clock mclock.Clock 50 } 51 52 func (cfg Config) withDefaults() Config { 53 if cfg.Log == nil { 54 cfg.Log = &log.Log 55 } 56 if cfg.ValidSchemes == nil { 57 cfg.ValidSchemes = enode.ValidSchemes 58 } 59 if cfg.Clock == nil { 60 cfg.Clock = mclock.System{} 61 } 62 return cfg 63 } 64 65 // ListenUDP starts listening for discovery packets on the given UDP socket. 66 func ListenUDP(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) { 67 return ListenV4(c, ln, cfg) 68 } 69 70 // ReadPacket is a packet that couldn't be handled. Those packets are sent to the unhandled 71 // channel if configured. 72 type ReadPacket struct { 73 Data []byte 74 Addr *net.UDPAddr 75 } 76 77 func min(x, y int) int { 78 if x > y { 79 return y 80 } 81 return x 82 }