mirror of
https://github.com/bitcoinj/bitcoinj.git
synced 2025-02-24 06:47:54 +01:00
DnsDiscovery: Implement service bit filtering.
This commit is contained in:
parent
7d9f109ee1
commit
70559a3140
2 changed files with 33 additions and 17 deletions
|
@ -19,6 +19,8 @@ package org.bitcoinj.net.discovery;
|
|||
|
||||
import org.bitcoinj.core.*;
|
||||
import org.bitcoinj.utils.*;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import java.net.*;
|
||||
import java.util.*;
|
||||
|
@ -36,6 +38,8 @@ import java.util.concurrent.*;
|
|||
* to connect to, you need to discover them via other means (like addr broadcasts).</p>
|
||||
*/
|
||||
public class DnsDiscovery extends MultiplexingDiscovery {
|
||||
private static final Logger log = LoggerFactory.getLogger(DnsDiscovery.class);
|
||||
|
||||
/**
|
||||
* Supports finding peers through DNS A records. Community run DNS entry points will be used.
|
||||
*
|
||||
|
@ -85,17 +89,32 @@ public class DnsDiscovery extends MultiplexingDiscovery {
|
|||
|
||||
@Override
|
||||
public List<InetSocketAddress> getPeers(long services, long timeoutValue, TimeUnit timeoutUnit) throws PeerDiscoveryException {
|
||||
if (services != 0)
|
||||
throw new PeerDiscoveryException("DNS seeds cannot filter by services: " + services);
|
||||
try {
|
||||
InetAddress[] response = InetAddress.getAllByName(hostname);
|
||||
List<InetSocketAddress> result = new ArrayList<>(response.length);
|
||||
for (InetAddress r : response)
|
||||
result.add(new InetSocketAddress(r, params.getPort()));
|
||||
return result;
|
||||
} catch (UnknownHostException e) {
|
||||
throw new PeerDiscoveryException(e);
|
||||
InetAddress[] response = null;
|
||||
if (services != 0) {
|
||||
String hostnameWithServices = "x" + Long.toHexString(services) + "." + hostname;
|
||||
log.info("Requesting {} peers from {}", VersionMessage.toStringServices(services),
|
||||
hostnameWithServices);
|
||||
try {
|
||||
response = InetAddress.getAllByName(hostnameWithServices);
|
||||
log.info("Got {} peers from {}", response.length, hostnameWithServices);
|
||||
} catch (UnknownHostException e) {
|
||||
log.info("Seed {} doesn't appear to support service bit filtering: {}", hostname, e.getMessage());
|
||||
}
|
||||
}
|
||||
if (response == null || response.length == 0) {
|
||||
log.info("Requesting all peers from {}", hostname);
|
||||
try {
|
||||
response = InetAddress.getAllByName(hostname);
|
||||
log.info("Got {} peers from {}", response.length, hostname);
|
||||
} catch (UnknownHostException e) {
|
||||
throw new PeerDiscoveryException(e);
|
||||
}
|
||||
}
|
||||
|
||||
List<InetSocketAddress> result = new ArrayList<>(response.length);
|
||||
for (InetAddress r : response)
|
||||
result.add(new InetSocketAddress(r, params.getPort()));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -76,13 +76,10 @@ public class MultiplexingDiscovery implements PeerDiscovery {
|
|||
for (HttpDiscovery.Details httpSeed : httpSeeds)
|
||||
discoveries.add(new HttpDiscovery(params, httpSeed, httpClient));
|
||||
}
|
||||
// Also use DNS seeds if there is no specific service requirement
|
||||
if (services == 0) {
|
||||
String[] dnsSeeds = params.getDnsSeeds();
|
||||
if (dnsSeeds != null)
|
||||
for (String dnsSeed : dnsSeeds)
|
||||
discoveries.add(new DnsSeedDiscovery(params, dnsSeed));
|
||||
}
|
||||
String[] dnsSeeds = params.getDnsSeeds();
|
||||
if (dnsSeeds != null)
|
||||
for (String dnsSeed : dnsSeeds)
|
||||
discoveries.add(new DnsSeedDiscovery(params, dnsSeed));
|
||||
return new MultiplexingDiscovery(params, discoveries, parallelQueries, shufflePeers);
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue