forked from ethereum/go-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
p2p: move rlpx into separate package (ethereum#21464)
This change moves the RLPx protocol implementation into a separate package, p2p/rlpx. The new package can be used to establish RLPx connections for protocol testing purposes. Co-authored-by: Felix Lange <[email protected]>
- Loading branch information
1 parent
483e6a3
commit 20f39de
Showing
10 changed files
with
901 additions
and
742 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -63,6 +63,7 @@ func init() { | |
discv5Command, | ||
dnsCommand, | ||
nodesetCommand, | ||
rlpxCommand, | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
// Copyright 2020 The go-ethereum Authors | ||
// This file is part of go-ethereum. | ||
// | ||
// go-ethereum is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
// | ||
// go-ethereum is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
// | ||
// You should have received a copy of the GNU General Public License | ||
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/ethereum/go-ethereum/common/hexutil" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/ethereum/go-ethereum/p2p" | ||
"github.com/ethereum/go-ethereum/p2p/rlpx" | ||
"github.com/ethereum/go-ethereum/rlp" | ||
"gopkg.in/urfave/cli.v1" | ||
) | ||
|
||
var ( | ||
rlpxCommand = cli.Command{ | ||
Name: "rlpx", | ||
Usage: "RLPx Commands", | ||
Subcommands: []cli.Command{ | ||
rlpxPingCommand, | ||
}, | ||
} | ||
rlpxPingCommand = cli.Command{ | ||
Name: "ping", | ||
Usage: "Perform a RLPx handshake", | ||
ArgsUsage: "<node>", | ||
Action: rlpxPing, | ||
} | ||
) | ||
|
||
func rlpxPing(ctx *cli.Context) error { | ||
n := getNodeArg(ctx) | ||
|
||
fd, err := net.Dial("tcp", fmt.Sprintf("%v:%d", n.IP(), n.TCP())) | ||
if err != nil { | ||
return err | ||
} | ||
conn := rlpx.NewConn(fd, n.Pubkey()) | ||
|
||
ourKey, _ := crypto.GenerateKey() | ||
_, err = conn.Handshake(ourKey) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
code, data, _, err := conn.Read() | ||
if err != nil { | ||
return err | ||
} | ||
switch code { | ||
case 0: | ||
var h devp2pHandshake | ||
if err := rlp.DecodeBytes(data, &h); err != nil { | ||
return fmt.Errorf("invalid handshake: %v", err) | ||
} | ||
fmt.Printf("%+v\n", h) | ||
case 1: | ||
var msg []p2p.DiscReason | ||
if rlp.DecodeBytes(data, &msg); len(msg) == 0 { | ||
return fmt.Errorf("invalid disconnect message") | ||
} | ||
return fmt.Errorf("received disconnect message: %v", msg[0]) | ||
default: | ||
return fmt.Errorf("invalid message code %d, expected handshake (code zero)", code) | ||
} | ||
return nil | ||
} | ||
|
||
// devp2pHandshake is the RLP structure of the devp2p protocol handshake. | ||
type devp2pHandshake struct { | ||
Version uint64 | ||
Name string | ||
Caps []p2p.Cap | ||
ListenPort uint64 | ||
ID hexutil.Bytes // secp256k1 public key | ||
// Ignore additional fields (for forward compatibility). | ||
Rest []rlp.RawValue `rlp:"tail"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.