2014-01-09 06:44:08 +01:00
|
|
|
// Copyright (c) 2013-2014 Conformal Systems LLC.
|
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.
|
|
|
|
|
|
|
|
package btcwire
|
|
|
|
|
|
|
|
import (
|
2014-03-30 22:56:52 +02:00
|
|
|
"bytes"
|
2013-05-08 21:31:00 +02:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"net"
|
2014-04-19 10:57:53 +02:00
|
|
|
"strings"
|
2013-05-08 21:31:00 +02:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MaxUserAgentLen is the maximum allowed length for the user agent field in a
|
|
|
|
// version message (MsgVersion).
|
|
|
|
const MaxUserAgentLen = 2000
|
|
|
|
|
2014-04-19 10:57:53 +02:00
|
|
|
// DefaultUserAgent for btcwire in the stack
|
|
|
|
const DefaultUserAgent = "/btcwire:0.1.4/"
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
// MsgVersion implements the Message interface and represents a bitcoin version
|
|
|
|
// message. It is used for a peer to advertise itself as soon as an outbound
|
|
|
|
// connection is made. The remote peer then uses this information along with
|
|
|
|
// its own to negotiate. The remote peer must then respond with a version
|
|
|
|
// message of its own containing the negotiated values followed by a verack
|
|
|
|
// message (MsgVerAck). This exchange must take place before any further
|
|
|
|
// communication is allowed to proceed.
|
|
|
|
type MsgVersion struct {
|
|
|
|
// Version of the protocol the node is using.
|
|
|
|
ProtocolVersion int32
|
|
|
|
|
|
|
|
// Bitfield which identifies the enabled services.
|
|
|
|
Services ServiceFlag
|
|
|
|
|
|
|
|
// Time the message was generated. This is encoded as an int64 on the wire.
|
|
|
|
Timestamp time.Time
|
|
|
|
|
|
|
|
// Address of the remote peer.
|
|
|
|
AddrYou NetAddress
|
|
|
|
|
|
|
|
// Address of the local peer.
|
|
|
|
AddrMe NetAddress
|
|
|
|
|
|
|
|
// Unique value associated with message that is used to detect self
|
|
|
|
// connections.
|
|
|
|
Nonce uint64
|
|
|
|
|
|
|
|
// The user agent that generated messsage. This is a encoded as a varString
|
|
|
|
// on the wire. This has a max length of MaxUserAgentLen.
|
|
|
|
UserAgent string
|
|
|
|
|
|
|
|
// Last block seen by the generator of the version message.
|
|
|
|
LastBlock int32
|
2014-03-27 20:19:03 +01:00
|
|
|
|
2014-03-30 18:00:23 +02:00
|
|
|
// Don't announce transactions to peer.
|
|
|
|
DisableRelayTx bool
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// HasService returns whether the specified service is supported by the peer
|
|
|
|
// that generated the message.
|
|
|
|
func (msg *MsgVersion) HasService(service ServiceFlag) bool {
|
|
|
|
if msg.Services&service == service {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddService adds service as a supported service by the peer generating the
|
|
|
|
// message.
|
|
|
|
func (msg *MsgVersion) AddService(service ServiceFlag) {
|
|
|
|
msg.Services |= service
|
|
|
|
}
|
|
|
|
|
|
|
|
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
2014-03-30 22:56:52 +02:00
|
|
|
// The version message is special in that the protocol version hasn't been
|
|
|
|
// negotiated yet. As a result, the pver field is ignored and any fields which
|
|
|
|
// are added in new versions are optional. This also mean that r must be a
|
|
|
|
// *bytes.Buffer so the number of remaining bytes can be ascertained.
|
|
|
|
//
|
2013-05-08 21:31:00 +02:00
|
|
|
// This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgVersion) BtcDecode(r io.Reader, pver uint32) error {
|
2014-03-30 22:56:52 +02:00
|
|
|
buf, ok := r.(*bytes.Buffer)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf("MsgVersion.BtcDecode reader is not a " +
|
|
|
|
"*bytes.Buffer")
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:56:52 +02:00
|
|
|
var sec int64
|
|
|
|
err := readElements(buf, &msg.ProtocolVersion, &msg.Services, &sec)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-03-30 22:56:52 +02:00
|
|
|
msg.Timestamp = time.Unix(sec, 0)
|
2013-05-08 21:31:00 +02:00
|
|
|
|
2014-03-30 22:56:52 +02:00
|
|
|
err = readNetAddress(buf, pver, &msg.AddrYou, false)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-30 22:56:52 +02:00
|
|
|
// Protocol versions >= 106 added a from address, nonce, and user agent
|
|
|
|
// field and they are only considered present if there are bytes
|
|
|
|
// remaining in the message.
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
err = readNetAddress(buf, pver, &msg.AddrMe, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
2014-03-30 22:56:52 +02:00
|
|
|
if buf.Len() > 0 {
|
|
|
|
err = readElement(buf, &msg.Nonce)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
2014-03-30 22:56:52 +02:00
|
|
|
if buf.Len() > 0 {
|
|
|
|
userAgent, err := readVarString(buf, pver)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-04-19 10:57:53 +02:00
|
|
|
err = validateUserAgent(userAgent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-03-30 22:56:52 +02:00
|
|
|
}
|
|
|
|
msg.UserAgent = userAgent
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2014-03-30 22:56:52 +02:00
|
|
|
// Protocol versions >= 209 added a last known block field. It is only
|
|
|
|
// considered present if there are bytes remaining in the message.
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
err = readElement(buf, &msg.LastBlock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2014-03-30 18:00:23 +02:00
|
|
|
// There was no relay transactions field before BIP0037Version, but
|
|
|
|
// the default behavior prior to the addition of the field was to always
|
|
|
|
// relay transactions.
|
|
|
|
if buf.Len() > 0 {
|
|
|
|
// It's safe to ignore the error here since the buffer has at
|
|
|
|
// least one byte and that byte will result in a boolean value
|
|
|
|
// regardless of its value. Also, the wire encoding for the
|
|
|
|
// field is true when transactions should be relayed, so reverse
|
|
|
|
// it for the DisableRelayTx field.
|
|
|
|
var relayTx bool
|
|
|
|
readElement(r, &relayTx)
|
|
|
|
msg.DisableRelayTx = !relayTx
|
2014-03-27 20:19:03 +01:00
|
|
|
}
|
|
|
|
|
2013-05-08 21:31:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
|
|
|
|
// This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgVersion) BtcEncode(w io.Writer, pver uint32) error {
|
2014-04-19 10:57:53 +02:00
|
|
|
err := validateUserAgent(msg.UserAgent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2014-04-19 10:57:53 +02:00
|
|
|
err = writeElements(w, msg.ProtocolVersion, msg.Services,
|
2013-05-08 21:31:00 +02:00
|
|
|
msg.Timestamp.Unix())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writeNetAddress(w, pver, &msg.AddrYou, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writeNetAddress(w, pver, &msg.AddrMe, false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writeElement(w, msg.Nonce)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writeVarString(w, pver, msg.UserAgent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = writeElement(w, msg.LastBlock)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-03-30 18:00:23 +02:00
|
|
|
// There was no relay transactions field before BIP0037Version. Also,
|
|
|
|
// the wire encoding for the field is true when transactions should be
|
|
|
|
// relayed, so reverse it from the DisableRelayTx field.
|
2014-03-27 20:19:03 +01:00
|
|
|
if pver >= BIP0037Version {
|
2014-03-30 18:00:23 +02:00
|
|
|
err = writeElement(w, !msg.DisableRelayTx)
|
2014-03-27 20:19:03 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2013-05-08 21:31:00 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns the protocol command string for the message. This is part
|
|
|
|
// of the Message interface implementation.
|
|
|
|
func (msg *MsgVersion) Command() string {
|
|
|
|
return cmdVersion
|
|
|
|
}
|
|
|
|
|
|
|
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
|
|
// receiver. This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgVersion) MaxPayloadLength(pver uint32) uint32 {
|
|
|
|
// XXX: <= 106 different
|
|
|
|
|
2014-03-30 18:00:23 +02:00
|
|
|
// Protocol version 4 bytes + services 8 bytes + timestamp 8 bytes +
|
|
|
|
// remote and local net addresses + nonce 8 bytes + length of user
|
|
|
|
// agent (varInt) + max allowed useragent length + last block 4 bytes +
|
|
|
|
// relay transactions flag 1 byte.
|
|
|
|
return 33 + (maxNetAddressPayload(pver) * 2) + MaxVarIntPayload +
|
|
|
|
MaxUserAgentLen
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMsgVersion returns a new bitcoin version message that conforms to the
|
|
|
|
// Message interface using the passed parameters and defaults for the remaining
|
|
|
|
// fields.
|
|
|
|
func NewMsgVersion(me *NetAddress, you *NetAddress, nonce uint64,
|
2014-04-19 10:57:53 +02:00
|
|
|
lastBlock int32) *MsgVersion {
|
2013-05-08 21:31:00 +02:00
|
|
|
|
2014-02-24 16:53:31 +01:00
|
|
|
// Limit the timestamp to one second precision since the protocol
|
2013-05-08 21:31:00 +02:00
|
|
|
// doesn't support better.
|
|
|
|
return &MsgVersion{
|
|
|
|
ProtocolVersion: int32(ProtocolVersion),
|
|
|
|
Services: 0,
|
|
|
|
Timestamp: time.Unix(time.Now().Unix(), 0),
|
|
|
|
AddrYou: *you,
|
|
|
|
AddrMe: *me,
|
|
|
|
Nonce: nonce,
|
2014-04-19 10:57:53 +02:00
|
|
|
UserAgent: DefaultUserAgent,
|
2013-05-08 21:31:00 +02:00
|
|
|
LastBlock: lastBlock,
|
2014-03-30 18:00:23 +02:00
|
|
|
DisableRelayTx: false,
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMsgVersionFromConn is a convenience function that extracts the remote
|
|
|
|
// and local address from conn and returns a new bitcoin version message that
|
|
|
|
// conforms to the Message interface. See NewMsgVersion.
|
2014-04-19 10:57:53 +02:00
|
|
|
func NewMsgVersionFromConn(conn net.Conn, nonce uint64,
|
2013-05-08 21:31:00 +02:00
|
|
|
lastBlock int32) (*MsgVersion, error) {
|
|
|
|
|
|
|
|
// Don't assume any services until we know otherwise.
|
|
|
|
lna, err := NewNetAddress(conn.LocalAddr(), 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't assume any services until we know otherwise.
|
|
|
|
rna, err := NewNetAddress(conn.RemoteAddr(), 0)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-04-19 10:57:53 +02:00
|
|
|
return NewMsgVersion(lna, rna, nonce, lastBlock), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validateUserAgent checks userAgent length against MaxUserAgentLen
|
|
|
|
func validateUserAgent(userAgent string) error {
|
|
|
|
if len(userAgent) > MaxUserAgentLen {
|
|
|
|
str := fmt.Sprintf("user agent too long [len %v, max %v]",
|
|
|
|
len(userAgent), MaxUserAgentLen)
|
|
|
|
return messageError("MsgVersion", str)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2014-04-20 22:01:39 +02:00
|
|
|
// AddUserAgent adds a user agent to the user agent string for the version
|
|
|
|
// message. The version string is not defined to any strict format, although
|
|
|
|
// it is recommended to use the form "major.minor.revision" e.g. "2.6.41".
|
2014-04-19 10:57:53 +02:00
|
|
|
func (msg *MsgVersion) AddUserAgent(name string, version string,
|
|
|
|
comments ...string) error {
|
|
|
|
|
|
|
|
newUserAgent := fmt.Sprintf("%s:%s", name, version)
|
|
|
|
if len(comments) != 0 {
|
|
|
|
newUserAgent = fmt.Sprintf("%s(%s)", newUserAgent,
|
|
|
|
strings.Join(comments, "; "))
|
|
|
|
}
|
|
|
|
newUserAgent = fmt.Sprintf("%s%s/", msg.UserAgent, newUserAgent)
|
|
|
|
err := validateUserAgent(newUserAgent)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
msg.UserAgent = newUserAgent
|
|
|
|
return nil
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|