Skip to content

Commit dba1750

Browse files
committed
p2p/discover: split out discv4 code
This change restructures the internals of p2p/discover to make room for the discv5 code which will soon be added to this package. - packet type names now have a "V4" suffix. - ListenUDP returns *UDPv4 instead of *Table. This technically breaks the API but the only caller in go-ethereum is package p2p, which uses a compatible interface and doesn't need changes. - The internal transport interface is changed to make Table reusable for v5. - The 'lookup' code moves from table to transport. This required updating the lookup unit test to use udpTest instead of a custom transport.
1 parent a43ec8b commit dba1750

7 files changed

Lines changed: 700 additions & 700 deletions

File tree

‎p2p/discover/common.go‎

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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/ethereum/go-ethereum/log"
24+
"github.com/ethereum/go-ethereum/p2p/enode"
25+
"github.com/ethereum/go-ethereum/p2p/netutil"
26+
)
27+
28+
type UDPConn interface {
29+
ReadFromUDP(b []byte) (n int, addr *net.UDPAddr, err error)
30+
WriteToUDP(b []byte, addr *net.UDPAddr) (n int, err error)
31+
Close() error
32+
LocalAddr() net.Addr
33+
}
34+
35+
// Config holds Table-related settings.
36+
type Config struct {
37+
// These settings are required and configure the UDP listener:
38+
PrivateKey *ecdsa.PrivateKey
39+
40+
// These settings are optional:
41+
NetRestrict *netutil.Netlist // network whitelist
42+
Bootnodes []*enode.Node // list of bootstrap nodes
43+
Unhandled chan<- ReadPacket // unhandled packets are sent on this channel
44+
Log log.Logger // if set, log messages go here
45+
}
46+
47+
// ListenUDP starts listening for discovery packets on the given UDP socket.
48+
func ListenUDP(c UDPConn, ln *enode.LocalNode, cfg Config) (*UDPv4, error) {
49+
return ListenV4(c, ln, cfg)
50+
}
51+
52+
// ReadPacket is a packet that couldn't be handled. Those packets are sent to the unhandled
53+
// channel if configured.
54+
type ReadPacket struct {
55+
Data []byte
56+
Addr *net.UDPAddr
57+
}

0 commit comments

Comments
 (0)