-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathPeerAddress.java
More file actions
406 lines (364 loc) · 11.5 KB
/
PeerAddress.java
File metadata and controls
406 lines (364 loc) · 11.5 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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
/**
* Copyright 2011 Google Inc.
* 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.EOFException;
import java.io.InputStream;
import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.UnknownHostException;
import java.util.Arrays;
/**
* A PeerAddress holds an IP address and port number representing the network location of
* a peer in the Bitcoin Peer-to-Peer network.
*/
public class PeerAddress {
/** Length of an encoded peer address */
public static final int PEER_ADDRESS_SIZE = 30;
/** IPv6-encoded IPv4 address prefix */
public static final byte[] IPV6_PREFIX = new byte[] {
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00,
(byte)0x00, (byte)0x00, (byte)0x00, (byte)0x00, (byte)0xff, (byte)0xff
};
/** The IP address */
private InetAddress address;
/** The IP port */
private int port;
/** Time seen */
private long timeSeen;
/** Peer services */
private long services;
/** Peer connected */
private boolean connected;
/** Time peer connected */
private long timeConnected;
/** Outbound connection */
private boolean outboundConnection;
/** Static address */
private boolean staticAddress;
/**
* Constructs a peer address from the given IP address and port
*
* @param address IP address
* @param port IP port
*/
public PeerAddress(InetAddress address, int port) {
this.address = address;
this.port = port;
timeSeen = System.currentTimeMillis()/1000;
}
/**
* Constructs a peer address from the given IP address and port
*
* @param address IP address
* @param port IP port
* @param timeSeen Latest time peer was seen
*/
public PeerAddress(InetAddress address, int port, long timeSeen) {
this.address = address;
this.port = port;
this.timeSeen = timeSeen;
}
/**
* Constructs a peer address from a network socket
*
* @param socket Network socket
*/
public PeerAddress(InetSocketAddress socket) {
this(socket.getAddress(), socket.getPort());
}
/**
* Constructs a peer address from a string in the format "[address]:port" where
* address can be "nnn.nnn.nnn.nnn" for IPv4 or "xxxx:xxxx:xxxx;xxxx:xxxx:xxxx:xxxx:xxxx"
* for IPv6.
*
* @param peerString Address string
* @throws UnknownHostException Incorrect address format
*/
public PeerAddress(String peerString) throws UnknownHostException {
//
// Separate the address and the port
//
int addrSep = peerString.lastIndexOf(']');
int portSep = peerString.lastIndexOf(':');
if (peerString.charAt(0) != '[' || addrSep < 0 ||
portSep < addrSep || portSep == peerString.length()-1)
throw new UnknownHostException("Incorrect [address]:port format");
String addrString = peerString.substring(1, addrSep);
String portString = peerString.substring(portSep+1);
//
// Decode the address
//
byte[] addrBytes;
if (addrString.indexOf('.') >= 0) {
String[] addrParts = addrString.split("\\D");
if (addrParts.length != 4)
throw new UnknownHostException("Incorrect IPv4 address format");
addrBytes = new byte[4];
for (int j=0; j<4; j++)
addrBytes[j] = (byte)Integer.parseInt(addrParts[j]);
} else if (addrString.indexOf(':') >= 0) {
String[] addrParts = addrString.split(":");
if (addrParts.length != 8)
throw new UnknownHostException("Incorrect IPv6 address format");
addrBytes = new byte[16];
int offset = 0;
for (int j=0; j<8; j++) {
if (addrParts[j].length() == 0) {
offset += 2;
} else {
int nibble = Integer.parseInt(addrParts[j], 16);
addrBytes[offset++] = (byte)(nibble>>8);
addrBytes[offset++] = (byte)nibble;
}
}
} else {
throw new UnknownHostException("Incorrect [address]:port format");
}
//
// Create the address and port values
//
address = InetAddress.getByAddress(addrBytes);
port = Integer.parseInt(portString);
timeSeen = System.currentTimeMillis()/1000;
}
/**
* Constructs a peer address from the serialized data
*
* @param inStream Input stream
* @throws EOFException End-of-data while processing serialized data
* @throws IOException Unable to read input stream
*/
public PeerAddress(InputStream inStream) throws EOFException, IOException {
byte[] bytes = new byte[PEER_ADDRESS_SIZE];
int count = inStream.read(bytes);
if (count < PEER_ADDRESS_SIZE)
throw new EOFException("End-of-data processing serialized address");
timeSeen = Utils.readUint32LE(bytes, 0);
services = Utils.readUint64LE(bytes, 4);
boolean ipv4 = true;
for (int j=0; j<12; j++) {
if (bytes[j+12] != IPV6_PREFIX[j]) {
ipv4 = false;
break;
}
}
if (ipv4)
address = InetAddress.getByAddress(Arrays.copyOfRange(bytes, 24, 28));
else
address = InetAddress.getByAddress(Arrays.copyOfRange(bytes, 12, 28));
port = (((int)bytes[28]&0xff)<<8) | ((int)bytes[29]&0xff);
}
/**
* Returns the serialized address
*
* @return Serialized address
*/
public byte[] getBytes() {
byte[] bytes = new byte[PEER_ADDRESS_SIZE];
getBytes(bytes, 0);
return bytes;
}
/**
* Returns the serialized address
*
* @param bytes Address buffer
* @param offset Buffer offset
*/
public void getBytes(byte[] bytes, int offset) {
Utils.uint32ToByteArrayLE(timeSeen, bytes, offset);
Utils.uint64ToByteArrayLE(services, bytes, offset+4);
byte[] addrBytes = address.getAddress();
if (addrBytes.length == 16) {
System.arraycopy(addrBytes, 0, bytes, offset+12, 16);
} else {
System.arraycopy(IPV6_PREFIX, 0, bytes, offset+12, 12);
System.arraycopy(addrBytes, 0, bytes, offset+24, 4);
}
bytes[offset+28] = (byte)(port>>8);
bytes[offset+29] = (byte)port;
}
/**
* Returns the IP address
*
* @return IP address
*/
public InetAddress getAddress() {
return address;
}
/**
* Sets the IP address
*
* @param address IP address
*/
public void setAddress(InetAddress address) {
this.address = address;
}
/**
* Returns the IP port
*
* @return IP port
*/
public int getPort() {
return port;
}
/**
* Sets the IP port
*
* @param port IP port
*/
public void setPort(int port) {
this.port = port;
}
/**
* Returns the timestamp for this peer
*
* @return Timestamp in seconds since the epoch
*/
public long getTimeStamp() {
return timeSeen;
}
/**
* Sets the timestamp for this peer
*
* @param timeSeen Time peer was seen in seconds since the epoch
*/
public void setTimeStamp(long timeSeen) {
this.timeSeen = timeSeen;
}
/**
* Sets the peer services
*
* @param services Peer services
*/
public void setServices(long services) {
this.services = services;
}
/**
* Returns the peer services
*
* @return Peer services
*/
public long getServices() {
return services;
}
/**
* Checks if this peer is connected
*
* @return TRUE if the peer is connected
*/
public boolean isConnected() {
return connected;
}
/**
* Sets the peer connection status
*
* @param isConnected TRUE if the peer is connected
*/
public void setConnected(boolean isConnected) {
connected = isConnected;
}
/**
* Returns the time the peer connected to us
*
* @return Time connected
*/
public long getTimeConnected() {
return timeConnected;
}
/**
* Sets the time the peer connected to us
*
* @param timeConnected Time the peer connected (seconds since the epoch)
*/
public void setTimeConnected(long timeConnected) {
this.timeConnected = timeConnected;
}
/**
* Checks if this is an outbound connection
*
* @return TRUE if this is an outbound connection
*/
public boolean isOutbound() {
return outboundConnection;
}
/**
* Set the peer connection type
*
* @param isOutbound TRUE if this is an outbound connection
*/
public void setOutbound(boolean isOutbound) {
outboundConnection = isOutbound;
}
/**
* Check if this is a static address
*
* @return TRUE if this is a static address
*/
public boolean isStatic() {
return staticAddress;
}
/**
* Set the address type
*
* @param isStatic TRUE if this is a static address
*/
public void setStatic(boolean isStatic) {
staticAddress = isStatic;
}
/**
* Return a socket address for our IP address and port
*
* @return Socket address
*/
public InetSocketAddress toSocketAddress() {
return new InetSocketAddress(address, port);
}
/**
* Returns a string representation of the IP address and port
*
* @return String representation
*/
@Override
public String toString() {
return String.format("[%s]:%d", address.getHostAddress(), port);
}
/**
* Checks if the supplied address is equal to this address
*
* @param obj Address to check
* @return TRUE if the addresses are equal
*/
@Override
public boolean equals(Object obj) {
boolean areEqual = false;
if (obj != null && (obj instanceof PeerAddress)) {
PeerAddress other = (PeerAddress)obj;
areEqual = (address.equals(other.address) && port == other.port);
}
return areEqual;
}
/**
* Returns the hash code for this object
*
* @return The hash code
*/
@Override
public int hashCode() {
return (address.hashCode()^port);
}
}