common.go 2.1 KB

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