2015-05-01 08:28:01 +02:00
|
|
|
// Copyright (c) 2013-2015 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 (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
)
|
|
|
|
|
|
|
|
// MaxAddrPerMsg is the maximum number of addresses that can be in a single
|
|
|
|
// bitcoin addr message (MsgAddr).
|
|
|
|
const MaxAddrPerMsg = 1000
|
|
|
|
|
|
|
|
// MsgAddr implements the Message interface and represents a bitcoin
|
|
|
|
// addr message. It is used to provide a list of known active peers on the
|
|
|
|
// network. An active peer is considered one that has transmitted a message
|
|
|
|
// within the last 3 hours. Nodes which have not transmitted in that time
|
|
|
|
// frame should be forgotten. Each message is limited to a maximum number of
|
|
|
|
// addresses, which is currently 1000. As a result, multiple messages must
|
|
|
|
// be used to relay the full list.
|
|
|
|
//
|
|
|
|
// Use the AddAddress function to build up the list of known addresses when
|
|
|
|
// sending an addr message to another peer.
|
|
|
|
type MsgAddr struct {
|
|
|
|
AddrList []*NetAddress
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAddress adds a known active peer to the message.
|
|
|
|
func (msg *MsgAddr) AddAddress(na *NetAddress) error {
|
|
|
|
if len(msg.AddrList)+1 > MaxAddrPerMsg {
|
2013-05-10 21:54:36 +02:00
|
|
|
str := fmt.Sprintf("too many addresses in message [max %v]",
|
|
|
|
MaxAddrPerMsg)
|
|
|
|
return messageError("MsgAddr.AddAddress", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
msg.AddrList = append(msg.AddrList, na)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddAddresses adds multiple known active peers to the message.
|
|
|
|
func (msg *MsgAddr) AddAddresses(netAddrs ...*NetAddress) error {
|
|
|
|
for _, na := range netAddrs {
|
|
|
|
err := msg.AddAddress(na)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ClearAddresses removes all addresses from the message.
|
|
|
|
func (msg *MsgAddr) ClearAddresses() {
|
|
|
|
msg.AddrList = []*NetAddress{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
|
|
|
|
// This is part of the Message interface implementation.
|
2016-10-19 01:21:48 +02:00
|
|
|
func (msg *MsgAddr) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
|
2016-02-22 17:13:11 +01:00
|
|
|
count, err := ReadVarInt(r, pver)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Limit to max addresses per message.
|
|
|
|
if count > MaxAddrPerMsg {
|
2013-05-10 21:54:36 +02:00
|
|
|
str := fmt.Sprintf("too many addresses for message "+
|
|
|
|
"[count %v, max %v]", count, MaxAddrPerMsg)
|
|
|
|
return messageError("MsgAddr.BtcDecode", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2016-04-21 23:49:38 +02:00
|
|
|
addrList := make([]NetAddress, count)
|
2013-09-25 20:57:01 +02:00
|
|
|
msg.AddrList = make([]*NetAddress, 0, count)
|
2013-05-08 21:31:00 +02:00
|
|
|
for i := uint64(0); i < count; i++ {
|
2016-04-21 23:49:38 +02:00
|
|
|
na := &addrList[i]
|
|
|
|
err := readNetAddress(r, pver, na, true)
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-04-21 23:49:38 +02:00
|
|
|
msg.AddAddress(na)
|
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.
|
2016-10-19 01:21:48 +02:00
|
|
|
func (msg *MsgAddr) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
|
2013-05-08 21:31:00 +02:00
|
|
|
// Protocol versions before MultipleAddressVersion only allowed 1 address
|
|
|
|
// per message.
|
|
|
|
count := len(msg.AddrList)
|
|
|
|
if pver < MultipleAddressVersion && count > 1 {
|
2013-05-10 21:54:36 +02:00
|
|
|
str := fmt.Sprintf("too many addresses for message of "+
|
|
|
|
"protocol version %v [count %v, max 1]", pver, count)
|
|
|
|
return messageError("MsgAddr.BtcEncode", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
if count > MaxAddrPerMsg {
|
2013-05-10 21:54:36 +02:00
|
|
|
str := fmt.Sprintf("too many addresses for message "+
|
|
|
|
"[count %v, max %v]", count, MaxAddrPerMsg)
|
|
|
|
return messageError("MsgAddr.BtcEncode", str)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
2016-02-22 17:13:11 +01:00
|
|
|
err := WriteVarInt(w, pver, uint64(count))
|
2013-05-08 21:31:00 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, na := range msg.AddrList {
|
|
|
|
err = writeNetAddress(w, pver, na, true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Command returns the protocol command string for the message. This is part
|
|
|
|
// of the Message interface implementation.
|
|
|
|
func (msg *MsgAddr) Command() string {
|
2014-07-14 17:38:29 +02:00
|
|
|
return CmdAddr
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// MaxPayloadLength returns the maximum length the payload can be for the
|
|
|
|
// receiver. This is part of the Message interface implementation.
|
|
|
|
func (msg *MsgAddr) MaxPayloadLength(pver uint32) uint32 {
|
|
|
|
if pver < MultipleAddressVersion {
|
|
|
|
// Num addresses (varInt) + a single net addresses.
|
2014-03-12 02:09:55 +01:00
|
|
|
return MaxVarIntPayload + maxNetAddressPayload(pver)
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Num addresses (varInt) + max allowed addresses.
|
2014-03-12 02:09:55 +01:00
|
|
|
return MaxVarIntPayload + (MaxAddrPerMsg * maxNetAddressPayload(pver))
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMsgAddr returns a new bitcoin addr message that conforms to the
|
|
|
|
// Message interface. See MsgAddr for details.
|
|
|
|
func NewMsgAddr() *MsgAddr {
|
2013-09-25 20:57:01 +02:00
|
|
|
return &MsgAddr{
|
|
|
|
AddrList: make([]*NetAddress, 0, MaxAddrPerMsg),
|
|
|
|
}
|
2013-05-08 21:31:00 +02:00
|
|
|
}
|