Skip to content

Commit

Permalink
Use InetAddress.getLoopbackAddress() rather than hardcoding '127.0.0.1'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andreas Schildbach committed Mar 14, 2018
1 parent 7e9c45a commit afe1b69
Show file tree
Hide file tree
Showing 8 changed files with 15 additions and 16 deletions.
3 changes: 1 addition & 2 deletions core/src/main/java/org/bitcoinj/core/PeerAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -113,7 +112,7 @@ public PeerAddress(NetworkParameters params, String hostname, int port) {
}

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
Expand Down
3 changes: 1 addition & 2 deletions core/src/main/java/org/bitcoinj/core/PeerGroup.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
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.*;
Expand Down Expand Up @@ -1020,7 +1019,7 @@ private boolean maybeCheckForLocalhostPeer() {
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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public Message onPreMessageReceived(Peer peer, Message m) {
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();

Expand Down
3 changes: 1 addition & 2 deletions core/src/test/java/org/bitcoinj/core/PeerGroupTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

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.*;
Expand Down Expand Up @@ -749,7 +748,7 @@ public void preferLocalPeer() throws IOException {
// 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;
Expand Down
5 changes: 3 additions & 2 deletions core/src/test/java/org/bitcoinj/core/PeerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
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;
Expand Down Expand Up @@ -81,7 +82,7 @@ public PeerTest(ClientType clientType) {
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);
}
Expand Down Expand Up @@ -284,7 +285,7 @@ public void invDownloadTx() throws Exception {
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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public void setUp(BlockStore blockStore) throws Exception {
channels.awaitRunning();
}

socketAddress = new InetSocketAddress("127.0.0.1", 1111);
socketAddress = new InetSocketAddress(InetAddress.getLoopbackAddress(), 1111);
}

protected void startPeerServers() throws IOException {
Expand All @@ -125,7 +125,7 @@ public void connectionOpened() {
}
};
}
}, new InetSocketAddress("127.0.0.1", 2000 + i));
}, new InetSocketAddress(InetAddress.getLoopbackAddress(), 2000 + i));
peerServers[i].startAsync();
peerServers[i].awaitRunning();
}
Expand Down Expand Up @@ -158,11 +158,11 @@ public void onPeerDisconnected(Peer p, int peerCount) {
}
});
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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public void run() {

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;
Expand Down

0 comments on commit afe1b69

Please sign in to comment.