msgaddr.go raw

   1  // Copyright (c) 2013-2015 The btcsuite developers
   2  // Use of this source code is governed by an ISC
   3  // license that can be found in the LICENSE file.
   4  
   5  package wire
   6  
   7  import (
   8  	"fmt"
   9  	"io"
  10  )
  11  
  12  // MaxAddrPerMsg is the maximum number of addresses that can be in a single
  13  // bitcoin addr message (MsgAddr).
  14  const MaxAddrPerMsg = 1000
  15  
  16  // MsgAddr implements the Message interface and represents a bitcoin
  17  // addr message.  It is used to provide a list of known active peers on the
  18  // network.  An active peer is considered one that has transmitted a message
  19  // within the last 3 hours.  Nodes which have not transmitted in that time
  20  // frame should be forgotten.  Each message is limited to a maximum number of
  21  // addresses, which is currently 1000.  As a result, multiple messages must
  22  // be used to relay the full list.
  23  //
  24  // Use the AddAddress function to build up the list of known addresses when
  25  // sending an addr message to another peer.
  26  type MsgAddr struct {
  27  	AddrList []*NetAddress
  28  }
  29  
  30  // AddAddress adds a known active peer to the message.
  31  func (msg *MsgAddr) AddAddress(na *NetAddress) error {
  32  	if len(msg.AddrList)+1 > MaxAddrPerMsg {
  33  		str := fmt.Sprintf("too many addresses in message [max %v]",
  34  			MaxAddrPerMsg)
  35  		return messageError("MsgAddr.AddAddress", str)
  36  	}
  37  
  38  	msg.AddrList = append(msg.AddrList, na)
  39  	return nil
  40  }
  41  
  42  // AddAddresses adds multiple known active peers to the message.
  43  func (msg *MsgAddr) AddAddresses(netAddrs ...*NetAddress) error {
  44  	for _, na := range netAddrs {
  45  		err := msg.AddAddress(na)
  46  		if err != nil {
  47  			return err
  48  		}
  49  	}
  50  	return nil
  51  }
  52  
  53  // ClearAddresses removes all addresses from the message.
  54  func (msg *MsgAddr) ClearAddresses() {
  55  	msg.AddrList = []*NetAddress{}
  56  }
  57  
  58  // BtcDecode decodes r using the bitcoin protocol encoding into the receiver.
  59  // This is part of the Message interface implementation.
  60  func (msg *MsgAddr) BtcDecode(r io.Reader, pver uint32, enc MessageEncoding) error {
  61  	count, err := ReadVarInt(r, pver)
  62  	if err != nil {
  63  		return err
  64  	}
  65  
  66  	// Limit to max addresses per message.
  67  	if count > MaxAddrPerMsg {
  68  		str := fmt.Sprintf("too many addresses for message "+
  69  			"[count %v, max %v]", count, MaxAddrPerMsg)
  70  		return messageError("MsgAddr.BtcDecode", str)
  71  	}
  72  	if err := validateElementCount(
  73  		r, count, minNetAddressPayload,
  74  	); err != nil {
  75  		return err
  76  	}
  77  
  78  	addrList := make([]NetAddress, count)
  79  	msg.AddrList = make([]*NetAddress, 0, count)
  80  	for i := uint64(0); i < count; i++ {
  81  		na := &addrList[i]
  82  		err := readNetAddress(r, pver, na, true)
  83  		if err != nil {
  84  			return err
  85  		}
  86  		msg.AddAddress(na)
  87  	}
  88  	return nil
  89  }
  90  
  91  // BtcEncode encodes the receiver to w using the bitcoin protocol encoding.
  92  // This is part of the Message interface implementation.
  93  func (msg *MsgAddr) BtcEncode(w io.Writer, pver uint32, enc MessageEncoding) error {
  94  	// Protocol versions before MultipleAddressVersion only allowed 1 address
  95  	// per message.
  96  	count := len(msg.AddrList)
  97  	if pver < MultipleAddressVersion && count > 1 {
  98  		str := fmt.Sprintf("too many addresses for message of "+
  99  			"protocol version %v [count %v, max 1]", pver, count)
 100  		return messageError("MsgAddr.BtcEncode", str)
 101  
 102  	}
 103  	if count > MaxAddrPerMsg {
 104  		str := fmt.Sprintf("too many addresses for message "+
 105  			"[count %v, max %v]", count, MaxAddrPerMsg)
 106  		return messageError("MsgAddr.BtcEncode", str)
 107  	}
 108  
 109  	err := WriteVarInt(w, pver, uint64(count))
 110  	if err != nil {
 111  		return err
 112  	}
 113  
 114  	for _, na := range msg.AddrList {
 115  		err = writeNetAddress(w, pver, na, true)
 116  		if err != nil {
 117  			return err
 118  		}
 119  	}
 120  
 121  	return nil
 122  }
 123  
 124  // Command returns the protocol command string for the message.  This is part
 125  // of the Message interface implementation.
 126  func (msg *MsgAddr) Command() string {
 127  	return CmdAddr
 128  }
 129  
 130  // MaxPayloadLength returns the maximum length the payload can be for the
 131  // receiver.  This is part of the Message interface implementation.
 132  func (msg *MsgAddr) MaxPayloadLength(pver uint32) uint32 {
 133  	if pver < MultipleAddressVersion {
 134  		// Num addresses (varInt) + a single net addresses.
 135  		return MaxVarIntPayload + maxNetAddressPayload(pver)
 136  	}
 137  
 138  	// Num addresses (varInt) + max allowed addresses.
 139  	return MaxVarIntPayload + (MaxAddrPerMsg * maxNetAddressPayload(pver))
 140  }
 141  
 142  // NewMsgAddr returns a new bitcoin addr message that conforms to the
 143  // Message interface.  See MsgAddr for details.
 144  func NewMsgAddr() *MsgAddr {
 145  	return &MsgAddr{
 146  		AddrList: make([]*NetAddress, 0, MaxAddrPerMsg),
 147  	}
 148  }
 149