Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ipv6 bootnodes #138

Merged
merged 2 commits into from Jul 14, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 2 additions & 9 deletions rskj-core/src/main/java/co/rsk/net/discovery/PeerExplorer.java
Expand Up @@ -22,6 +22,7 @@
import co.rsk.net.discovery.table.NodeDistanceTable;
import co.rsk.net.discovery.table.OperationResult;
import co.rsk.net.discovery.table.PeerDiscoveryRequestBuilder;
import co.rsk.util.IpUtils;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.jetty.util.ConcurrentHashSet;
Expand Down Expand Up @@ -288,15 +289,7 @@ private void addConnection(PongPeerMessage message, InetSocketAddress incommingA

private void loadInitialBootNodes(List<String> nodes) {
if (CollectionUtils.isNotEmpty(nodes)) {
for (String node : nodes) {
String[] addressData = StringUtils.split(node, ":");
if (addressData != null && addressData.length == 2) {
List<String> dataList = Arrays.asList(addressData);
bootNodes.add(new InetSocketAddress(dataList.get(0), Integer.parseInt(dataList.get(1))));
} else {
logger.debug("Invalid bootNode address: {}", node);
}
}
bootNodes.addAll(IpUtils.parseAddresses(nodes));
}
}

Expand Down
54 changes: 54 additions & 0 deletions rskj-core/src/main/java/co/rsk/util/IpUtils.java
@@ -0,0 +1,54 @@
package co.rsk.util;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Created by mario on 13/07/17.
*/
public class IpUtils {
private static final Logger logger = LoggerFactory.getLogger(IpUtils.class);

private static final String IPV6_INPUT_FORMAT = "^\\[(.*)\\]:([0-9]{1,})";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • Square brackets are not mandatory for IPv6 addresses if they are not part of an URL or if a port number is not specified. But if there won't be a default port for nodes then it is correct.
  • Second group of regex for port number could limit to 5 digits and allow IPs without port

private static final String IPV4_INPUT_FORMAT = "^(.*):([0-9]{1,})";
private static final Pattern ipv6Pattern = Pattern.compile(IPV6_INPUT_FORMAT);
private static final Pattern ipv4Pattern = Pattern.compile(IPV4_INPUT_FORMAT);


public static InetSocketAddress parseAddress(String address) {
Matcher matcher = ipv6Pattern.matcher(address);
if(matcher.matches())
return parseMatch(matcher);

matcher = ipv4Pattern.matcher(address);
if(StringUtils.countMatches(address, ":") == 1 && matcher.matches())
return parseMatch(matcher);

logger.debug("Invalid address: {}. For ipv6 use de convention [address]:port. For ipv4 address:port", address);
return null;
}

public static List<InetSocketAddress> parseAddresses(List<String> addresses) {
List<InetSocketAddress> result = new ArrayList<>();
if(CollectionUtils.isNotEmpty(addresses)) {
for(String a : addresses) {
InetSocketAddress res = parseAddress(a);
if (res != null)
result.add(res);
}
}
return result;
}

private static InetSocketAddress parseMatch(Matcher matcher) {
return new InetSocketAddress(matcher.group(1), Integer.valueOf(matcher.group(2)));
}
}
64 changes: 64 additions & 0 deletions rskj-core/src/test/java/co/rsk/util/IpUtilsTest.java
@@ -0,0 +1,64 @@
package co.rsk.util;

import org.apache.commons.collections4.CollectionUtils;
import org.junit.Assert;
import org.junit.Test;

import java.net.InetSocketAddress;
import java.util.ArrayList;
import java.util.List;

/**
* Created by mario on 13/07/17.
*/
public class IpUtilsTest {

private static final String IPV6_WITH_PORT = "[2001:db8:85a3:8d3:1319:8a2e:370:7348]:443";
private static final String IPV6_NO_PORT = "[2001:db8:85a3:8d3:1319:8a2e:370:7348]";
private static final String IPV6_INVALID = "2001:db8:85a3:8d3:1319:8a2e:370:7348";
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This IPv6 address is valid if square brackets are not mandatory.

private static final String IPV4_WITH_PORT = "172.217.28.228:80";
private static final String IPV4_NO_PORT = "172.217.28.228";

@Test
public void parseIPv6() {
InetSocketAddress result = IpUtils.parseAddress(IPV6_WITH_PORT);
Assert.assertNotNull(result);
}

@Test
public void parseIPv6NoPort() {
InetSocketAddress result = IpUtils.parseAddress(IPV6_NO_PORT);
Assert.assertNull(result);
}

@Test
public void parseIPv6InvalidFormat() {
InetSocketAddress result = IpUtils.parseAddress(IPV6_INVALID);
Assert.assertNull(result);
}

@Test
public void parseIPv4() {
InetSocketAddress result = IpUtils.parseAddress(IPV4_WITH_PORT);
Assert.assertNotNull(result);
}

@Test
public void parseIPv4NoPort() {
InetSocketAddress result = IpUtils.parseAddress(IPV4_NO_PORT);
Assert.assertNull(result);
}

@Test
public void parseAddresses() {
List<String> addresses = new ArrayList<>();
addresses.add(IPV6_WITH_PORT);
addresses.add(IPV6_NO_PORT);
addresses.add(IPV6_INVALID);
addresses.add(IPV4_WITH_PORT);
addresses.add(IPV4_NO_PORT);
List<InetSocketAddress> result = IpUtils.parseAddresses(addresses);
Assert.assertTrue(CollectionUtils.isNotEmpty(result));
Assert.assertEquals(2, CollectionUtils.size(result));
}
}