common.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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. package discover
  17. import (
  18. "crypto/ecdsa"
  19. "net"
  20. "github.com/ethereum/go-ethereum/common/mclock"
  21. "github.com/ethereum/go-ethereum/log"
  22. "github.com/ethereum/go-ethereum/p2p/enode"
  23. "github.com/ethereum/go-ethereum/p2p/enr"
  24. "github.com/ethereum/go-ethereum/p2p/netutil"
  25. )
  26. // UDPConn is a network connection on which discovery can operate.
  27. type UDPConn interface {
  28. ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
  29. WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)
  30. Close() error
  31. LocalAddr() net.Addr
  32. }
  33. // Config holds settings for the discovery listener.
  34. type Config struct {
  35. // These settings are required and configure the UDP listener:
  36. PrivateKey *ecdsa.PrivateKey
  37. // These settings are optional:
  38. NetRestrict *netutil.Netlist // network whitelist
  39. Bootnodes []*enode.Node // list of bootstrap nodes
  40. Unhandled chan<- ReadPacket // unhandled packets are sent on this channel
  41. Log log.Logger // if set, log messages go here
  42. ValidSchemes enr.IdentityScheme // allowed identity schemes
  43. Clock mclock.Clock
  44. }
  45. func (cfg Config) withDefaults() Config {
  46. if cfg.Log == nil {
  47. cfg.Log = log.Root()
  48. }
  49. if cfg.ValidSchemes == nil {
  50. cfg.ValidSchemes = enode.ValidSchemes
  51. }
  52. if cfg.Clock == nil {
  53. cfg.Clock = mclock.System{}
  54. }
  55. return cfg
  56. }
  57. // ListenUDP starts listening for discovery packets on the given UDP socket.
  58. func ListenUDP(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) {
  59. return ListenV4(c, ln, cfg)
  60. }
  61. // ReadPacket is a packet that couldn't be handled. Those packets are sent to the unhandled
  62. // channel if configured.
  63. type ReadPacket struct {
  64. Data []byte
  65. Addr *net.UDPAddr
  66. }
  67. func min(x, y int) int {
  68. if x > y {
  69. return y
  70. }
  71. return x
  72. }