Skip to content
This repository was archived by the owner on Aug 23, 2020. It is now read-only.

Fix remove source port check #1156

Merged
merged 7 commits into from
Nov 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 16 additions & 14 deletions src/main/java/com/iota/iri/network/Neighbor.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.iota.iri.network;

import org.apache.commons.lang3.StringUtils;

import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.concurrent.atomic.AtomicInteger;
Expand All @@ -16,35 +19,25 @@ public abstract class Neighbor {
private long numberOfSentTransactions;
private long numberOfStaleTransactions;

private boolean flagged = false;
private final boolean flagged;
public boolean isFlagged() {
return flagged;
}
public void setFlagged(boolean flagged) {
this.flagged = flagged;
}


private final static AtomicInteger numPeers = new AtomicInteger(0);
public static int getNumPeers() {
return numPeers.get();
}
public static void incNumPeers() {
numPeers.incrementAndGet();
}
public static void decNumPeers() {
int v = numPeers.decrementAndGet();
if (v < 0) {
numPeers.set(0);
}
}

private final String hostAddress;

public String getHostAddress() {
return hostAddress;
}


public Neighbor(final InetSocketAddress address, boolean isConfigured) {
this.address = address;
this.hostAddress = address.getAddress().getHostAddress();
Expand All @@ -54,7 +47,6 @@ public Neighbor(final InetSocketAddress address, boolean isConfigured) {
public abstract void send(final DatagramPacket packet);
public abstract int getPort();
public abstract String connectionType();
public abstract boolean matches(SocketAddress address);

@Override
public boolean equals(final Object obj) {
Expand All @@ -69,7 +61,17 @@ public int hashCode() {
public InetSocketAddress getAddress() {
return address;
}


protected boolean matches(SocketAddress address) {
if (address instanceof InetSocketAddress) {
// faster than fallback
InetAddress adr = ((InetSocketAddress) address).getAddress();
return adr != null && StringUtils.equals(adr.getHostAddress(), hostAddress);
} else { // fallback
return address != null && address.toString().contains(hostAddress);
}
}

void incAllTransactions() {
numberOfAllTransactions++;
}
Expand Down
13 changes: 1 addition & 12 deletions src/main/java/com/iota/iri/network/TCPNeighbor.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.net.DatagramPacket;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.nio.ByteBuffer;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -70,7 +69,7 @@ public void setSink(Socket sink) {
this.sink.close();
log.info("Sink {} closed", this.getHostAddress());
} catch (IOException e) {
log.error("Sink {} close failure", this.getHostAddress(), e);
log.error("Sink {} close failure: {}", this.getHostAddress(), e.toString());
}
}
}
Expand Down Expand Up @@ -114,14 +113,4 @@ public ByteBuffer getNextMessage() throws InterruptedException {
return (this.sendQueue.poll(10000, TimeUnit.MILLISECONDS));
}

@Override
public boolean matches(SocketAddress address) {
if (address.toString().contains(this.getHostAddress())) {
int port = this.getSource().getPort();
if (address.toString().contains(Integer.toString(port))) {
return true;
}
}
return false;
}
}
13 changes: 1 addition & 12 deletions src/main/java/com/iota/iri/network/UDPNeighbor.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.net.SocketAddress;

/**
* Created by paul on 4/15/17.
*/
public class UDPNeighbor extends Neighbor {

private static final Logger log = LoggerFactory.getLogger(UDPNeighbor.class);

private final DatagramSocket socket;
Expand Down Expand Up @@ -47,15 +47,4 @@ public String connectionType() {
return "udp";
}

@Override
public boolean matches(SocketAddress address) {
if (this.getAddress().toString().contains(address.toString())) {
int port = this.getAddress().getPort();
if (address.toString().contains(Integer.toString(port))) {
return true;
}
}
return false;
}

}
35 changes: 35 additions & 0 deletions src/test/java/com/iota/iri/network/UDPNeighborTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package com.iota.iri.network;

import org.junit.Test;

import java.net.InetSocketAddress;
import java.net.SocketAddress;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class UDPNeighborTest {

private final UDPNeighbor neighbor = new UDPNeighbor(address("localhost", 42), null, false);

@Test
public void sameIpWhenMatchesThenTrue() {
assertTrue("expected match", neighbor.matches(address("localhost", 42)));
assertTrue("expected match", neighbor.matches(address("localhost", 666)));
assertTrue("expected match", neighbor.matches(address("127.0.0.1", 42)));
assertTrue("expected match", neighbor.matches(address("127.0.0.1", 666)));
}

@Test
public void differentIpWhenMatchesThenFalse() {
assertFalse("expected no match", neighbor.matches(address("foo.bar.com", 42)));
assertFalse("expected no match", neighbor.matches(address("8.8.8.8", 42)));
assertFalse("expected no match", neighbor.matches(null));
assertFalse("expected no match", neighbor.matches(new SocketAddress() {}));
}

private InetSocketAddress address(String hostOrIp, int port) {
return new InetSocketAddress(hostOrIp, port);
}

}