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.
14 // maxNetAddressPayload returns the max payload size for a bitcoin NetAddress
15 // based on the protocol version.
16 func maxNetAddressPayload(pver uint32) uint32 {
17 // Services 8 bytes + ip 16 bytes + port 2 bytes.
20 // NetAddressTimeVersion added a timestamp field.
21 if pver >= NetAddressTimeVersion {
29 // NetAddress defines information about a peer on the network including the time
30 // it was last seen, the services it supports, its IP address, and port.
31 type NetAddress struct {
32 // Last time the address was seen. This is, unfortunately, encoded as a
33 // uint32 on the wire and therefore is limited to 2106. This field is
34 // not present in the bitcoin version message (MsgVersion) nor was it
35 // added until protocol version >= NetAddressTimeVersion.
38 // Bitfield which identifies the services supported by the address.
41 // IP address of the peer.
44 // Port the peer is using. This is encoded in big endian on the wire
45 // which differs from most everything else.
49 // HasService returns whether the specified service is supported by the address.
50 func (na *NetAddress) HasService(service ServiceFlag) bool {
51 return na.Services&service == service
54 // AddService adds service as a supported service by the peer generating the
56 func (na *NetAddress) AddService(service ServiceFlag) {
57 na.Services |= service
60 // NewNetAddressIPPort returns a new NetAddress using the provided IP, port, and
61 // supported services with defaults for the remaining fields.
62 func NewNetAddressIPPort(ip net.IP, port uint16, services ServiceFlag) *NetAddress {
63 return NewNetAddressTimestamp(time.Now(), services, ip, port)
66 // NewNetAddressTimestamp returns a new NetAddress using the provided
67 // timestamp, IP, port, and supported services. The timestamp is rounded to
68 // single second precision.
69 func NewNetAddressTimestamp(
70 timestamp time.Time, services ServiceFlag, ip net.IP, port uint16) *NetAddress {
71 // Limit the timestamp to one second precision since the protocol
72 // doesn't support better.
74 Timestamp: time.Unix(timestamp.Unix(), 0),
82 // NewNetAddress returns a new NetAddress using the provided TCP address and
83 // supported services with defaults for the remaining fields.
84 func NewNetAddress(addr *net.TCPAddr, services ServiceFlag) *NetAddress {
85 return NewNetAddressIPPort(addr.IP, uint16(addr.Port), services)
88 // readNetAddress reads an encoded NetAddress from r depending on the protocol
89 // version and whether or not the timestamp is included per ts. Some messages
90 // like version do not include the timestamp.
91 func readNetAddress(r io.Reader, pver uint32, na *NetAddress, ts bool) error {
94 // NOTE: The bitcoin protocol uses a uint32 for the timestamp so it will
95 // stop working somewhere around 2106. Also timestamp wasn't added until
96 // protocol version >= NetAddressTimeVersion
97 if ts && pver >= NetAddressTimeVersion {
98 err := readElement(r, (*uint32Time)(&na.Timestamp))
104 err := readElements(r, &na.Services, &ip)
108 // Sigh. Bitcoin protocol mixes little and big endian.
109 port, err := binarySerializer.Uint16(r, bigEndian)
115 Timestamp: na.Timestamp,
116 Services: na.Services,
123 // writeNetAddress serializes a NetAddress to w depending on the protocol
124 // version and whether or not the timestamp is included per ts. Some messages
125 // like version do not include the timestamp.
126 func writeNetAddress(w io.Writer, pver uint32, na *NetAddress, ts bool) error {
127 // NOTE: The bitcoin protocol uses a uint32 for the timestamp so it will
128 // stop working somewhere around 2106. Also timestamp wasn't added until
129 // until protocol version >= NetAddressTimeVersion.
130 if ts && pver >= NetAddressTimeVersion {
131 err := writeElement(w, uint32(na.Timestamp.Unix()))
137 // Ensure to always write 16 bytes even if the ip is nil.
140 copy(ip[:], na.IP.To16())
142 err := writeElements(w, na.Services, ip)
147 // Sigh. Bitcoin protocol mixes little and big endian.
148 return binary.Write(w, bigEndian, na.Port)