Use InetAddress.getLoopbackAddress() rather than hardcoding '127.0.0.1'.

This commit is contained in:
Andreas Schildbach
2018-03-14 11:49:59 +01:00
parent 7e9c45ad55
commit afe1b69f31
8 changed files with 15 additions and 16 deletions

View File

@@ -18,7 +18,6 @@
package org.bitcoinj.core;
import com.google.common.base.Objects;
import com.google.common.net.InetAddresses;
import java.io.IOException;
import java.io.OutputStream;
@@ -113,7 +112,7 @@ public class PeerAddress extends ChildMessage {
}
public static PeerAddress localhost(NetworkParameters params) {
return new PeerAddress(params, InetAddresses.forString("127.0.0.1"), params.getPort());
return new PeerAddress(params, InetAddress.getLoopbackAddress(), params.getPort());
}
@Override

View File

@@ -20,7 +20,6 @@ package org.bitcoinj.core;
import com.google.common.annotations.*;
import com.google.common.base.*;
import com.google.common.collect.*;
import com.google.common.net.*;
import com.google.common.primitives.*;
import com.google.common.util.concurrent.*;
import net.jcip.annotations.*;
@@ -1020,7 +1019,7 @@ public class PeerGroup implements TransactionBroadcaster {
Socket socket = null;
try {
socket = new Socket();
socket.connect(new InetSocketAddress(InetAddresses.forString("127.0.0.1"), params.getPort()), vConnectTimeoutMillis);
socket.connect(new InetSocketAddress(InetAddress.getLoopbackAddress(), params.getPort()), vConnectTimeoutMillis);
localhostCheckState = LocalhostCheckState.FOUND;
return true;
} catch (IOException e) {

View File

@@ -202,7 +202,7 @@ public class BitcoindComparisonTool {
bitcoindChainHead = PARAMS.getGenesisBlock().getHash();
// bitcoind MUST be on localhost or we will get banned as a DoSer
new NioClient(new InetSocketAddress(InetAddress.getByName("127.0.0.1"), args.length > 2 ? Integer.parseInt(args[2]) : PARAMS.getPort()), bitcoind, 1000);
new NioClient(new InetSocketAddress(InetAddress.getLoopbackAddress(), args.length > 2 ? Integer.parseInt(args[2]) : PARAMS.getPort()), bitcoind, 1000);
connectedFuture.get();

View File

@@ -19,7 +19,6 @@ package org.bitcoinj.core;
import com.google.common.base.Stopwatch;
import com.google.common.collect.*;
import com.google.common.net.*;
import com.google.common.util.concurrent.*;
import org.bitcoinj.core.listeners.*;
import org.bitcoinj.net.discovery.*;
@@ -749,7 +748,7 @@ public class PeerGroupTest extends TestWithPeerGroup {
// Check that if we have a localhost port 8333 or 18333 then it's used instead of the p2p network.
ServerSocket local = null;
try {
local = new ServerSocket(UNITTEST.getPort(), 100, InetAddresses.forString("127.0.0.1"));
local = new ServerSocket(UNITTEST.getPort(), 100, InetAddress.getLoopbackAddress());
}
catch(BindException e) { // Port already in use, skipping this test.
return;

View File

@@ -39,6 +39,7 @@ import javax.annotation.*;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketException;
import java.nio.channels.CancelledKeyException;
@@ -81,7 +82,7 @@ public class PeerTest extends TestWithNetworkConnections {
public void setUp() throws Exception {
super.setUp();
VersionMessage ver = new VersionMessage(UNITTEST, 100);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 4000);
InetSocketAddress address = new InetSocketAddress(InetAddress.getLoopbackAddress(), 4000);
peer = new Peer(UNITTEST, ver, new PeerAddress(UNITTEST, address), blockChain);
peer.addWallet(wallet);
}
@@ -284,7 +285,7 @@ public class PeerTest extends TestWithNetworkConnections {
public void invDownloadTxMultiPeer() throws Exception {
// Check co-ordination of which peer to download via the memory pool.
VersionMessage ver = new VersionMessage(UNITTEST, 100);
InetSocketAddress address = new InetSocketAddress("127.0.0.1", 4242);
InetSocketAddress address = new InetSocketAddress(InetAddress.getLoopbackAddress(), 4242);
Peer peer2 = new Peer(UNITTEST, ver, new PeerAddress(UNITTEST, address), blockChain);
peer2.addWallet(wallet);
VersionMessage peerVersion = new VersionMessage(UNITTEST, OTHER_PEER_CHAIN_HEIGHT);

View File

@@ -19,6 +19,7 @@ package org.bitcoinj.testing;
import org.bitcoinj.core.*;
import com.google.common.util.concurrent.SettableFuture;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.HashMap;
import java.util.Map;
@@ -36,7 +37,7 @@ public abstract class InboundMessageQueuer extends PeerSocketHandler {
public BloomFilter lastReceivedFilter;
protected InboundMessageQueuer(NetworkParameters params) {
super(params, new InetSocketAddress("127.0.0.1", 2000));
super(params, new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000));
}
public Message nextMessage() {

View File

@@ -100,7 +100,7 @@ public class TestWithNetworkConnections {
channels.awaitRunning();
}
socketAddress = new InetSocketAddress("127.0.0.1", 1111);
socketAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 1111);
}
protected void startPeerServers() throws IOException {
@@ -125,7 +125,7 @@ public class TestWithNetworkConnections {
}
};
}
}, new InetSocketAddress("127.0.0.1", 2000 + i));
}, new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000 + i));
peerServers[i].startAsync();
peerServers[i].awaitRunning();
}
@@ -158,11 +158,11 @@ public class TestWithNetworkConnections {
}
});
if (clientType == ClientType.NIO_CLIENT_MANAGER || clientType == ClientType.BLOCKING_CLIENT_MANAGER)
channels.openConnection(new InetSocketAddress("127.0.0.1", 2000), peer);
channels.openConnection(new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000), peer);
else if (clientType == ClientType.NIO_CLIENT)
new NioClient(new InetSocketAddress("127.0.0.1", 2000), peer, 100);
new NioClient(new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000), peer, 100);
else if (clientType == ClientType.BLOCKING_CLIENT)
new BlockingClient(new InetSocketAddress("127.0.0.1", 2000), peer, 100, SocketFactory.getDefault(), null);
new BlockingClient(new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000), peer, 100, SocketFactory.getDefault(), null);
else
throw new RuntimeException();
// Claim we are connected to a different IP that what we really are, so tx confidence broadcastBy sets work

View File

@@ -113,7 +113,7 @@ public class TestWithPeerGroup extends TestWithNetworkConnections {
protected InboundMessageQueuer connectPeerWithoutVersionExchange(int id) throws Exception {
Preconditions.checkArgument(id < PEER_SERVERS);
InetSocketAddress remoteAddress = new InetSocketAddress("127.0.0.1", 2000 + id);
InetSocketAddress remoteAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000 + id);
Peer peer = peerGroup.connectTo(remoteAddress).getConnectionOpenFuture().get();
InboundMessageQueuer writeTarget = newPeerWriteTargetQueue.take();
writeTarget.peer = peer;