2016-01-25 01:56:47 +01:00
|
|
|
// Copyright (c) 2013-2016 The btcsuite developers
|
2013-05-08 21:31:00 +02:00
|
|
|
// Use of this source code is governed by an ISC
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2015-01-31 19:32:55 +01:00
|
|
|
package wire
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"unicode/utf8"
|
2016-08-08 21:04:33 +02:00
|
|
|
|
|
|
|
"github.com/btcsuite/btcd/chaincfg/chainhash"
|
2013-05-08 21:31:00 +02:00
|
|
|
)
|
|
|
|
|
2014-01-19 05:29:44 +01:00
|
|
|
// MessageHeaderSize is the number of bytes in a bitcoin message header.
|
|
|
|
// Bitcoin network (magic) 4 bytes + command 12 bytes + payload length 4 bytes +
|
|
|
|
// checksum 4 bytes.
|
|
|
|
const MessageHeaderSize = 24
|
|
|
|
|
2014-07-13 00:32:21 +02:00
|
|
|
// CommandSize is the fixed size of all commands in the common bitcoin message
|
2013-05-08 21:31:00 +02:00
|
|
|
// header. Shorter commands must be zero padded.
|
2014-07-13 00:32:21 +02:00
|
|
|
const CommandSize = 12
|
2013-05-08 21:31:00 +02:00
|
|
|
|
2014-06-07 05:33:39 +02:00
|
|
|
// MaxMessagePayload is the maximum bytes a message can be regardless of other
|
2013-05-08 21:31:00 +02:00
|
|
|
// individual limits imposed by messages themselves.
|
2014-06-07 05:33:39 +02:00
|
|
|
const MaxMessagePayload = (1024 * 1024 * 32) // 32MB
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
// Commands used in bitcoin message headers which describe the type of message.
|
|
|
|
const (
|
2017-03-09 01:14:39 +01:00
|
|
|
CmdVersion = "version"
|
|
|
|
CmdVerAck = "verack"
|
|
|
|
CmdGetAddr = "getaddr"
|
|
|
|
CmdAddr = "addr"
|
|
|
|
CmdGetBlocks = "getblocks"
|
|
|
|
CmdInv = "inv"
|
|
|
|
CmdGetData = "getdata"
|
|
|
|
CmdNotFound = "notfound"
|
|
|
|
CmdBlock = "block"
|
|
|
|
CmdTx = "tx"
|
|
|
|
CmdGetHeaders = "getheaders"
|
|
|
|
CmdHeaders = "headers"
|
|
|
|
CmdPing = "ping"
|
|
|
|
CmdPong = "pong"
|
|
|
|
CmdAlert = "alert"
|
|
|
|
CmdMemPool = "mempool"
|
|
|
|
CmdFilterAdd = "filteradd"
|
|
|
|
CmdFilterClear = "filterclear"
|
|
|
|
CmdFilterLoad = "filterload"
|
|
|
|
CmdMerkleBlock = "merkleblock"
|
|
|
|
CmdReject = "reject"
|
|
|
|
CmdSendHeaders = "sendheaders"
|
|
|
|
CmdFeeFilter = "feefilter"
|
2018-01-19 04:10:42 +01:00
|
|
|
CmdGetCFilters = "getcfilters"
|
2017-03-09 01:14:39 +01:00
|
|
|
CmdGetCFHeaders = "getcfheaders"
|
2018-01-22 22:56:27 +01:00
|
|
|
CmdGetCFCheckpt = "getcfcheckpt"
|
2017-03-09 01:14:39 +01:00
|
|
|
CmdCFilter = "cfilter"
|
|
|
|
CmdCFHeaders = "cfheaders"
|
2018-01-23 01:51:53 +01:00
|
|
|
CmdCFCheckpt = "cfcheckpt"
|
2020-11-23 14:17:10 +01:00
|
|
|
CmdSendAddrV2 = "sendaddrv2"
|
2013-05-08 21:31:00 +02:00
|
|
|
)
|
|
|
|
|
2016-10-19 01:32:35 +02:00
|
|
|
// MessageEncoding represents the wire message encoding format to be used.
|
2016-10-19 01:21:48 +02:00
|
|
|
type MessageEncoding uint32
|
|
|
|
|
|
|
|
const (
|
|
|
|
// BaseEncoding encodes all messages in the default format specified
|
|
|
|
// for the Bitcoin wire protocol.
|
|
|
|
BaseEncoding MessageEncoding = 1 << iota
|
|
|
|
|
|
|
|
// WitnessEncoding encodes all messages other than transaction messages
|
|
|
|
// using the default Bitcoin wire protocol specification. For transaction
|
|
|
|
// messages, the new encoding format detailed in BIP0144 will be used.
|
|
|
|
WitnessEncoding
|
|
|
|
)
|
|
|
|
|
|
|
|
// LatestEncoding is the most recently specified encoding for the Bitcoin wire
|
|
|
|
// protocol.
|
|
|
|
var LatestEncoding = WitnessEncoding
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
// Message is an interface that describes a bitcoin message. A type that
|
|
|
|
// implements Message has complete control over the representation of its data
|
|
|
|
// and may therefore contain additional or fewer fields than those which
|
|
|
|
// are used directly in the protocol encoded message.
|
|
|
|
type Message interface {
|
2016-10-19 01:21:48 +02:00
|
|
|
BtcDecode(io.Reader, uint32, MessageEncoding) error
|
|
|
|
BtcEncode(io.Writer, uint32, MessageEncoding) error
|
2013-05-08 21:31:00 +02:00
|
|
|
Command() string
|
|
|
|
MaxPayloadLength(uint32) uint32
|
|
|
|
}
|
|
|
|
|
|
|
|
// makeEmptyMessage creates a message of the appropriate concrete type based
|
|
|
|
// on the command.
|
|
|
|
func makeEmptyMessage(command string) (Message, error) {
|
|
|
|
var msg Message
|
|
|
|
switch command {
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdVersion:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgVersion{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdVerAck:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgVerAck{}
|
|
|
|
|
2020-11-23 14:17:10 +01:00
|
|
|
case CmdSendAddrV2:
|
|
|
|
msg = &MsgSendAddrV2{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdGetAddr:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgGetAddr{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdAddr:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgAddr{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdGetBlocks:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgGetBlocks{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdBlock:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgBlock{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdInv:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgInv{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdGetData:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgGetData{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdNotFound:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgNotFound{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdTx:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgTx{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdPing:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgPing{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdPong:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgPong{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdGetHeaders:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgGetHeaders{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdHeaders:
|
2013-05-08 21:31:00 +02:00
|
|
|
msg = &MsgHeaders{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdAlert:
|
2013-05-09 07:01:29 +02:00
|
|
|
msg = &MsgAlert{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdMemPool:
|
2013-05-09 07:01:29 +02:00
|
|
|
msg = &MsgMemPool{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdFilterAdd:
|
2014-04-11 04:29:00 +02:00
|
|
|
msg = &MsgFilterAdd{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdFilterClear:
|
2014-04-11 04:29:00 +02:00
|
|
|
msg = &MsgFilterClear{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdFilterLoad:
|
2014-04-11 04:29:00 +02:00
|
|
|
msg = &MsgFilterLoad{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdMerkleBlock:
|
2014-04-11 04:29:00 +02:00
|
|
|
msg = &MsgMerkleBlock{}
|
|
|
|
|
2014-07-14 17:38:29 +02:00
|
|
|
case CmdReject:
|
2013-11-12 06:12:07 +01:00
|
|
|
msg = &MsgReject{}
|
|
|
|
|
2015-12-02 20:53:24 +01:00
|
|
|
case CmdSendHeaders:
|
|
|
|
msg = &MsgSendHeaders{}
|
|
|
|
|
2016-08-25 21:08:32 +02:00
|
|
|
case CmdFeeFilter:
|
|
|
|
msg = &MsgFeeFilter{}
|
|
|
|
|
2018-01-19 04:10:42 +01:00
|
|
|
case CmdGetCFilters:
|
|
|
|
msg = &MsgGetCFilters{}
|
2016-12-13 12:40:30 +01:00
|
|
|
|
2017-03-09 01:14:39 +01:00
|
|
|
case CmdGetCFHeaders:
|
|
|
|
msg = &MsgGetCFHeaders{}
|
2017-02-02 11:42:44 +01:00
|
|
|
|
2018-01-22 22:56:27 +01:00
|
|
|
case CmdGetCFCheckpt:
|
|
|
|
msg = &MsgGetCFCheckpt{}
|
|
|
|
|
2017-01-20 16:55:09 +01:00
|
|
|
case CmdCFilter:
|
|
|
|
msg = &MsgCFilter{}
|
|
|
|
|
2017-03-09 01:14:39 +01:00
|
|
|
case CmdCFHeaders:
|
|
|
|
msg = &MsgCFHeaders{}
|
2017-02-02 11:42:44 +01:00
|
|
|
|
2018-01-23 01:51:53 +01:00
|
|
|
case CmdCFCheckpt:
|
|
|
|
msg = &MsgCFCheckpt{}
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unhandled command [%s]", command)
|
|
|
|
}
|
|
|
|
return msg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// messageHeader defines the header structure for all bitcoin protocol messages.
|
|
|
|
type messageHeader struct {
|
|
|
|
magic BitcoinNet // 4 bytes
|
|
|
|
command string // 12 bytes
|
|
|
|
length uint32 // 4 bytes
|
|
|
|
checksum [4]byte // 4 bytes
|
|
|
|
}
|
|
|
|
|
2013-05-09 22:23:31 +02:00
|
|
|
// readMessageHeader reads a bitcoin message header from r.
|
2014-01-19 05:29:44 +01:00
|
|
|
func readMessageHeader(r io.Reader) (int, *messageHeader, error) {
|
|
|
|
// Since readElements doesn't return the amount of bytes read, attempt
|
|
|
|
// to read the entire header into a buffer first in case there is a
|
|
|
|
// short read so the proper amount of read bytes are known. This works
|
|
|
|
// since the header is a fixed size.
|
2014-04-08 21:28:02 +02:00
|
|
|
var headerBytes [MessageHeaderSize]byte
|
|
|
|
n, err := io.ReadFull(r, headerBytes[:])
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
2014-01-19 05:29:44 +01:00
|
|
|
return n, nil, err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
2014-06-05 06:39:03 +02:00
|
|
|
hr := bytes.NewReader(headerBytes[:])
|
2014-01-19 05:29:44 +01:00
|
|
|
|
|
|
|
// Create and populate a messageHeader struct from the raw header bytes.
|
|
|
|
hdr := messageHeader{}
|
2014-07-13 00:32:21 +02:00
|
|
|
var command [CommandSize]byte
|
2014-01-19 05:29:44 +01:00
|
|
|
readElements(hr, &hdr.magic, &command, &hdr.length, &hdr.checksum)
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
// Strip trailing zeros from command string.
|
2020-08-28 02:30:27 +02:00
|
|
|
hdr.command = string(bytes.TrimRight(command[:], "\x00"))
|
2013-05-08 21:31:00 +02:00
|
|
|
|
2014-01-19 05:29:44 +01:00
|
|
|
return n, &hdr, nil
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// discardInput reads n bytes from reader r in chunks and discards the read
|
|
|
|
// bytes. This is used to skip payloads when various errors occur and helps
|
|
|
|
// prevent rogue nodes from causing massive memory allocation through forging
|
|
|
|
// header length.
|
|
|
|
func discardInput(r io.Reader, n uint32) {
|
2013-05-09 20:00:00 +02:00
|
|
|
maxSize := uint32(10 * 1024) // 10k at a time
|
2013-05-08 21:31:00 +02:00
|
|
|
numReads := n / maxSize
|
|
|
|
bytesRemaining := n % maxSize
|
|
|
|
if n > 0 {
|
|
|
|
buf := make([]byte, maxSize)
|
|
|
|
for i := uint32(0); i < numReads; i++ {
|
|
|
|
io.ReadFull(r, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if bytesRemaining > 0 {
|
|
|
|
buf := make([]byte, bytesRemaining)
|
|
|
|
io.ReadFull(r, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-19 05:29:44 +01:00
|
|
|
// WriteMessageN writes a bitcoin Message to w including the necessary header
|
|
|
|
// information and returns the number of bytes written. This function is the
|
|
|
|
// same as WriteMessage except it also returns the number of bytes written.
|
|
|
|
func WriteMessageN(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) (int, error) {
|
2016-10-19 01:21:48 +02:00
|
|
|
return WriteMessageWithEncodingN(w, msg, pver, btcnet, BaseEncoding)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMessage writes a bitcoin Message to w including the necessary header
|
|
|
|
// information. This function is the same as WriteMessageN except it doesn't
|
|
|
|
// doesn't return the number of bytes written. This function is mainly provided
|
|
|
|
// for backwards compatibility with the original API, but it's also useful for
|
|
|
|
// callers that don't care about byte counts.
|
|
|
|
func WriteMessage(w io.Writer, msg Message, pver uint32, btcnet BitcoinNet) error {
|
|
|
|
_, err := WriteMessageN(w, msg, pver, btcnet)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteMessageWithEncodingN writes a bitcoin Message to w including the
|
|
|
|
// necessary header information and returns the number of bytes written.
|
|
|
|
// This function is the same as WriteMessageN except it also allows the caller
|
|
|
|
// to specify the message encoding format to be used when serializing wire
|
|
|
|
// messages.
|
|
|
|
func WriteMessageWithEncodingN(w io.Writer, msg Message, pver uint32,
|
|
|
|
btcnet BitcoinNet, encoding MessageEncoding) (int, error) {
|
|
|
|
|
2014-01-19 05:29:44 +01:00
|
|
|
totalBytes := 0
|
2013-05-08 21:31:00 +02:00
|
|
|
|
2013-05-16 16:07:04 +02:00
|
|
|
// Enforce max command size.
|
2014-07-13 00:32:21 +02:00
|
|
|
var command [CommandSize]byte
|
2013-05-08 21:31:00 +02:00
|
|
|
cmd := msg.Command()
|
2014-07-13 00:32:21 +02:00
|
|
|
if len(cmd) > CommandSize {
|
2013-05-11 09:29:11 +02:00
|
|
|
str := fmt.Sprintf("command [%s] is too long [max %v]",
|
2014-07-13 00:32:21 +02:00
|
|
|
cmd, CommandSize)
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, messageError("WriteMessage", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
copy(command[:], []byte(cmd))
|
|
|
|
|
2013-05-16 16:07:04 +02:00
|
|
|
// Encode the message payload.
|
2013-05-08 21:31:00 +02:00
|
|
|
var bw bytes.Buffer
|
2016-10-19 01:21:48 +02:00
|
|
|
err := msg.BtcEncode(&bw, pver, encoding)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
payload := bw.Bytes()
|
|
|
|
lenp := len(payload)
|
|
|
|
|
2013-05-16 16:07:04 +02:00
|
|
|
// Enforce maximum overall message payload.
|
2014-06-07 05:33:39 +02:00
|
|
|
if lenp > MaxMessagePayload {
|
2013-05-10 03:56:03 +02:00
|
|
|
str := fmt.Sprintf("message payload is too large - encoded "+
|
|
|
|
"%d bytes, but maximum message payload is %d bytes",
|
2014-06-07 05:33:39 +02:00
|
|
|
lenp, MaxMessagePayload)
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, messageError("WriteMessage", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2013-05-16 16:07:04 +02:00
|
|
|
// Enforce maximum message payload based on the message type.
|
|
|
|
mpl := msg.MaxPayloadLength(pver)
|
|
|
|
if uint32(lenp) > mpl {
|
|
|
|
str := fmt.Sprintf("message payload is too large - encoded "+
|
|
|
|
"%d bytes, but maximum message payload size for "+
|
|
|
|
"messages of type [%s] is %d.", lenp, cmd, mpl)
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, messageError("WriteMessage", str)
|
2013-05-16 16:07:04 +02:00
|
|
|
}
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
// Create header for the message.
|
|
|
|
hdr := messageHeader{}
|
|
|
|
hdr.magic = btcnet
|
|
|
|
hdr.command = cmd
|
|
|
|
hdr.length = uint32(lenp)
|
2016-08-08 21:04:33 +02:00
|
|
|
copy(hdr.checksum[:], chainhash.DoubleHashB(payload)[0:4])
|
2013-05-08 21:31:00 +02:00
|
|
|
|
2014-01-19 05:29:44 +01:00
|
|
|
// Encode the header for the message. This is done to a buffer
|
|
|
|
// rather than directly to the writer since writeElements doesn't
|
|
|
|
// return the number of bytes written.
|
2014-03-20 18:14:38 +01:00
|
|
|
hw := bytes.NewBuffer(make([]byte, 0, MessageHeaderSize))
|
|
|
|
writeElements(hw, hdr.magic, command, hdr.length, hdr.checksum)
|
2014-01-19 05:29:44 +01:00
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
// Write header.
|
2014-01-19 05:29:44 +01:00
|
|
|
n, err := w.Write(hw.Bytes())
|
2016-01-25 01:56:47 +01:00
|
|
|
totalBytes += n
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2019-08-21 00:26:23 +02:00
|
|
|
// Only write the payload if there is one, e.g., verack messages don't
|
|
|
|
// have one.
|
|
|
|
if len(payload) > 0 {
|
|
|
|
n, err = w.Write(payload)
|
|
|
|
totalBytes += n
|
|
|
|
}
|
|
|
|
|
2016-11-03 05:02:04 +01:00
|
|
|
return totalBytes, err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2016-10-19 01:21:48 +02:00
|
|
|
// ReadMessageWithEncodingN reads, validates, and parses the next bitcoin Message
|
|
|
|
// from r for the provided protocol version and bitcoin network. It returns the
|
|
|
|
// number of bytes read in addition to the parsed Message and raw bytes which
|
|
|
|
// comprise the message. This function is the same as ReadMessageN except it
|
|
|
|
// allows the caller to specify which message encoding is to to consult when
|
|
|
|
// decoding wire messages.
|
|
|
|
func ReadMessageWithEncodingN(r io.Reader, pver uint32, btcnet BitcoinNet,
|
|
|
|
enc MessageEncoding) (int, Message, []byte, error) {
|
2014-01-19 05:29:44 +01:00
|
|
|
|
|
|
|
totalBytes := 0
|
|
|
|
n, hdr, err := readMessageHeader(r)
|
2016-01-25 01:56:47 +01:00
|
|
|
totalBytes += n
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, nil, nil, err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2013-05-10 03:56:03 +02:00
|
|
|
// Enforce maximum message payload.
|
2014-06-07 05:33:39 +02:00
|
|
|
if hdr.length > MaxMessagePayload {
|
2013-05-10 03:56:03 +02:00
|
|
|
str := fmt.Sprintf("message payload is too large - header "+
|
|
|
|
"indicates %d bytes, but max message payload is %d "+
|
2014-06-07 05:33:39 +02:00
|
|
|
"bytes.", hdr.length, MaxMessagePayload)
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
2013-05-10 03:56:03 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for messages from the wrong bitcoin network.
|
2013-05-08 21:31:00 +02:00
|
|
|
if hdr.magic != btcnet {
|
2013-05-10 05:25:21 +02:00
|
|
|
discardInput(r, hdr.length)
|
2013-05-10 03:56:03 +02:00
|
|
|
str := fmt.Sprintf("message from other network [%v]", hdr.magic)
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2013-05-10 03:56:03 +02:00
|
|
|
// Check for malformed commands.
|
2013-05-10 03:15:27 +02:00
|
|
|
command := hdr.command
|
|
|
|
if !utf8.ValidString(command) {
|
|
|
|
discardInput(r, hdr.length)
|
2013-05-10 03:56:03 +02:00
|
|
|
str := fmt.Sprintf("invalid command %v", []byte(command))
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
2013-05-10 03:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Create struct of appropriate message type based on the command.
|
|
|
|
msg, err := makeEmptyMessage(command)
|
|
|
|
if err != nil {
|
|
|
|
discardInput(r, hdr.length)
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, nil, nil, messageError("ReadMessage",
|
|
|
|
err.Error())
|
2013-05-10 03:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check for maximum length based on the message type as a malicious client
|
|
|
|
// could otherwise create a well-formed header and set the length to max
|
|
|
|
// numbers in order to exhaust the machine's memory.
|
|
|
|
mpl := msg.MaxPayloadLength(pver)
|
|
|
|
if hdr.length > mpl {
|
|
|
|
discardInput(r, hdr.length)
|
2013-05-10 03:56:03 +02:00
|
|
|
str := fmt.Sprintf("payload exceeds max length - header "+
|
|
|
|
"indicates %v bytes, but max payload size for "+
|
|
|
|
"messages of type [%v] is %v.", hdr.length, command, mpl)
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
2013-05-10 03:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read payload.
|
|
|
|
payload := make([]byte, hdr.length)
|
2014-01-19 05:29:44 +01:00
|
|
|
n, err = io.ReadFull(r, payload)
|
2016-01-25 01:56:47 +01:00
|
|
|
totalBytes += n
|
2013-05-10 03:15:27 +02:00
|
|
|
if err != nil {
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, nil, nil, err
|
2013-05-10 03:15:27 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test checksum.
|
2016-08-08 21:04:33 +02:00
|
|
|
checksum := chainhash.DoubleHashB(payload)[0:4]
|
2018-09-13 23:35:08 +02:00
|
|
|
if !bytes.Equal(checksum, hdr.checksum[:]) {
|
2013-05-10 03:56:03 +02:00
|
|
|
str := fmt.Sprintf("payload checksum failed - header "+
|
|
|
|
"indicates %v, but actual checksum is %v.",
|
|
|
|
hdr.checksum, checksum)
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, nil, nil, messageError("ReadMessage", str)
|
2013-05-10 03:15:27 +02:00
|
|
|
}
|
|
|
|
|
2014-06-05 06:39:03 +02:00
|
|
|
// Unmarshal message. NOTE: This must be a *bytes.Buffer since the
|
|
|
|
// MsgVersion BtcDecode function requires it.
|
2013-05-10 03:15:27 +02:00
|
|
|
pr := bytes.NewBuffer(payload)
|
2016-10-19 01:21:48 +02:00
|
|
|
err = msg.BtcDecode(pr, pver, enc)
|
2013-05-10 03:15:27 +02:00
|
|
|
if err != nil {
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, nil, nil, err
|
2013-05-10 03:15:27 +02:00
|
|
|
}
|
|
|
|
|
2014-01-19 05:29:44 +01:00
|
|
|
return totalBytes, msg, payload, nil
|
|
|
|
}
|
|
|
|
|
2016-10-19 01:21:48 +02:00
|
|
|
// ReadMessageN reads, validates, and parses the next bitcoin Message from r for
|
|
|
|
// the provided protocol version and bitcoin network. It returns the number of
|
|
|
|
// bytes read in addition to the parsed Message and raw bytes which comprise the
|
|
|
|
// message. This function is the same as ReadMessage except it also returns the
|
|
|
|
// number of bytes read.
|
|
|
|
func ReadMessageN(r io.Reader, pver uint32, btcnet BitcoinNet) (int, Message, []byte, error) {
|
|
|
|
return ReadMessageWithEncodingN(r, pver, btcnet, BaseEncoding)
|
|
|
|
}
|
|
|
|
|
2014-01-19 05:29:44 +01:00
|
|
|
// ReadMessage reads, validates, and parses the next bitcoin Message from r for
|
|
|
|
// the provided protocol version and bitcoin network. It returns the parsed
|
|
|
|
// Message and raw bytes which comprise the message. This function only differs
|
|
|
|
// from ReadMessageN in that it doesn't return the number of bytes read. This
|
|
|
|
// function is mainly provided for backwards compatibility with the original
|
|
|
|
// API, but it's also useful for callers that don't care about byte counts.
|
|
|
|
func ReadMessage(r io.Reader, pver uint32, btcnet BitcoinNet) (Message, []byte, error) {
|
|
|
|
_, msg, buf, err := ReadMessageN(r, pver, btcnet)
|
|
|
|
return msg, buf, err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|