-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAddressMessage.java
More file actions
150 lines (145 loc) · 6.24 KB
/
AddressMessage.java
File metadata and controls
150 lines (145 loc) · 6.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/**
* Copyright 2013-2014 Ronald W Hoffman
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package JavaBitcoin;
import java.io.ByteArrayInputStream;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.LinkedList;
import java.util.List;
/**
* <p>An 'addr' message is sent to inform a nodes about peers on the network.</p>
*
* <p>Address Message</p>
* <pre>
* Size Field Description
* ==== ===== ===========
* VarInt Count The number of addresses
* Variable Addresses One or more network addresses
* </pre>
*
* <p>Network Address</p>
* <pre>
* Size Field Description
* ==== ===== ===========
* 4 bytes Time Timestamp in seconds since the epoch
* 8 bytes Services Services provided by the node
* 16 bytes Address IPv6 address (IPv4 addresses are encoded as IPv6 addresses)
* 2 bytes Port Port (network byte order)
* </pre>
*/
public class AddressMessage {
/**
* Build an 'addr' message
*
* We will include all peers that we have seen within the last hour as well as
* our own external address
*
* @param peer The destination peer or null for a broadcast message
* @return Message to be sent to the peer
*/
public static Message buildAddressMessage(Peer peer) {
//
// Create an address list containing peers that we have seen within the past 15 minutes.
// The maximum length of the list is 100 entries. Static addresses are not included
// in the list. We will include our own address with a current timestamp if the
// address is valid.
//
long oldestTime = System.currentTimeMillis()/1000 - (15*60);
List<PeerAddress> addresses = new LinkedList<>();
if (Parameters.listenAddressValid) {
PeerAddress localAddress = new PeerAddress(Parameters.listenAddress, Parameters.listenPort);
localAddress.setServices(Parameters.SUPPORTED_SERVICES);
addresses.add(localAddress);
}
synchronized(Parameters.lock) {
for (PeerAddress address : Parameters.peerAddresses) {
if (addresses.size() == 100)
break;
if (address.getTimeStamp() >= oldestTime && !address.isStatic())
addresses.add(address);
}
}
//
// Build the message payload
//
byte[] varCount = VarInt.encode(addresses.size());
byte[] msgData = new byte[addresses.size()*PeerAddress.PEER_ADDRESS_SIZE+varCount.length];
System.arraycopy(varCount, 0, msgData, 0, varCount.length);
int offset = varCount.length;
for (PeerAddress address : addresses) {
address.getBytes(msgData, offset);
offset += PeerAddress.PEER_ADDRESS_SIZE;
}
//
// Build the message
//
ByteBuffer buffer = MessageHeader.buildMessage("addr", msgData);
return new Message(buffer, peer, MessageHeader.ADDR_CMD);
}
/**
* Process an 'addr' message and add new address to our peer address list
*
* @param msg Message
* @param inStream Message data stream
* @throws EOFException Serialized byte stream is too short
* @throws IOException Error reading from input stream
* @throws VerificationException Message contains more than 1000 entries
*/
public static void processAddressMessage(Message msg, ByteArrayInputStream inStream)
throws EOFException, IOException, VerificationException {
long oldestTime = System.currentTimeMillis()/1000 - (30*60);
//
// Get the address count
//
int addrCount = new VarInt(inStream).toInt();
if (addrCount < 0 || addrCount > 1000)
throw new VerificationException("More than 1000 addresses in 'addr' message");
//
// Process the addresses and keep any addresses that are not too old
//
for (int i=0; i<addrCount; i++) {
PeerAddress peerAddress = new PeerAddress(inStream);
if (peerAddress.getTimeStamp() < oldestTime ||
(peerAddress.getServices()&Parameters.NODE_NETWORK) == 0 ||
peerAddress.getAddress().equals(Parameters.listenAddress))
continue;
synchronized(Parameters.lock) {
PeerAddress mapAddress = Parameters.peerMap.get(peerAddress);
if (mapAddress == null) {
boolean added = false;
long timeStamp = peerAddress.getTimeStamp();
for (int j=0; j<Parameters.peerAddresses.size(); j++) {
PeerAddress chkAddress = Parameters.peerAddresses.get(j);
if (chkAddress.getTimeStamp() < timeStamp) {
Parameters.peerAddresses.add(j, peerAddress);
Parameters.peerMap.put(peerAddress, peerAddress);
added = true;
break;
}
}
if (!added) {
Parameters.peerAddresses.add(peerAddress);
Parameters.peerMap.put(peerAddress, peerAddress);
}
} else {
mapAddress.setTimeStamp(Math.max(mapAddress.getTimeStamp(), peerAddress.getTimeStamp()));
mapAddress.setServices(peerAddress.getServices());
}
}
}
}
}