github.com/theQRL/go-zond@v0.1.1/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 "time" 23 24 "github.com/theQRL/go-zond/common/mclock" 25 "github.com/theQRL/go-zond/log" 26 "github.com/theQRL/go-zond/p2p/enode" 27 "github.com/theQRL/go-zond/p2p/enr" 28 "github.com/theQRL/go-zond/p2p/netutil" 29 ) 30 31 // UDPConn is a network connection on which discovery can operate. 32 type UDPConn interface { 33 ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error) 34 WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error) 35 Close() error 36 LocalAddr() net.Addr 37 } 38 39 // Config holds settings for the discovery listener. 40 type Config struct { 41 // These settings are required and configure the UDP listener: 42 PrivateKey *ecdsa.PrivateKey 43 44 // All remaining settings are optional. 45 46 // Packet handling configuration: 47 NetRestrict *netutil.Netlist // list of allowed IP networks 48 Unhandled chan<- ReadPacket // unhandled packets are sent on this channel 49 50 // Node table configuration: 51 Bootnodes []*enode.Node // list of bootstrap nodes 52 PingInterval time.Duration // speed of node liveness check 53 RefreshInterval time.Duration // used in bucket refresh 54 55 // The options below are useful in very specific cases, like in unit tests. 56 V5ProtocolID *[6]byte 57 Log log.Logger // if set, log messages go here 58 ValidSchemes enr.IdentityScheme // allowed identity schemes 59 Clock mclock.Clock 60 } 61 62 func (cfg Config) withDefaults() Config { 63 // Node table configuration: 64 if cfg.PingInterval == 0 { 65 cfg.PingInterval = 10 * time.Second 66 } 67 if cfg.RefreshInterval == 0 { 68 cfg.RefreshInterval = 30 * time.Minute 69 } 70 71 // Debug/test settings: 72 if cfg.Log == nil { 73 cfg.Log = log.Root() 74 } 75 if cfg.ValidSchemes == nil { 76 cfg.ValidSchemes = enode.ValidSchemes 77 } 78 if cfg.Clock == nil { 79 cfg.Clock = mclock.System{} 80 } 81 return cfg 82 } 83 84 // ListenUDP starts listening for discovery packets on the given UDP socket. 85 func ListenUDP(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) { 86 return ListenV4(c, ln, cfg) 87 } 88 89 // ReadPacket is a packet that couldn't be handled. Those packets are sent to the unhandled 90 // channel if configured. 91 type ReadPacket struct { 92 Data []byte 93 Addr *net.UDPAddr 94 } 95 96 func min(x, y int) int { 97 if x > y { 98 return y 99 } 100 return x 101 }