Skip to content

Commit

Permalink
Fixed bug with IP address identification
Browse files Browse the repository at this point in the history
  • Loading branch information
offbynull committed Dec 6, 2015
1 parent 20c8579 commit 174900e
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target/
/target/
/nbproject/
31 changes: 29 additions & 2 deletions src/main/java/com/offbynull/portmapper/common/RegexUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,20 @@
*/
public final class RegexUtils {
/**
* IPV4 Regex. Taken from http://stackoverflow.com/questions/106179/regular-expression-to-match-hostname-or-ip-address.
* Semi-IPV4 Regex. Groups 1 to 4 are the individual address components. Further checking needs to be done to
* ensure that each component is between 0 to 255 and that there are no trailing zeros.
*
* This isn't being done in the regex due to complexity.
*/
private static final Pattern IPV4_PATTERN = Pattern.compile(
"(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9‌​]{2}|2[0-4][0-9]|25[0-5])");
"([0-9]{1,3})\\."
+ "([0-9]{1,3})\\."
+ "([0-9]{1,3})\\."
+ "([0-9]{1,3})");

private static final int IPV4_COMPONENT_COUNT = 4;

private static final int IPV4_COMPONENT_MAX = 255;

private RegexUtils() {
// do nothing
Expand All @@ -49,7 +59,24 @@ public static List<String> findAllIpv4Addresses(String text) {
List<String> ret = new LinkedList<>();

Matcher matcher = IPV4_PATTERN.matcher(text);
top:
while (matcher.find()) {
for (int i = 1; i <= IPV4_COMPONENT_COUNT; i++) {
String componentStr = matcher.group(i);
int component = Integer.parseInt(componentStr);

// Fail if component has trailing zeros
if (!String.valueOf(component).equals(componentStr)) {
continue top;
}

// Fail if component is greater than 255. The regex ensures that it's never below 0.
if (component > IPV4_COMPONENT_MAX) {
continue top;
}
}

// IP is valid, add it to the return list
ret.add(matcher.group(0));
}

Expand Down
46 changes: 46 additions & 0 deletions src/test/java/com/offbynull/portmapper/common/RegexUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package com.offbynull.portmapper.common;

import java.util.List;
import org.junit.Assert;
import org.junit.Test;

public class RegexUtilsTest {

@Test
public void mustPassOnProperAddressesWithFullComponents() {
List<String> addresses = RegexUtils.findAllIpv4Addresses(
"sf sdf sd 212.123.222.199 fs\n\n \r\tfsf123.124.125.126sdfsfsdfs");

Assert.assertEquals(2, addresses.size());
Assert.assertEquals("212.123.222.199", addresses.get(0));
Assert.assertEquals("123.124.125.126", addresses.get(1));
}

@Test
public void mustPassOnProperAddressesWithNonFullComponents() {
List<String> addresses = RegexUtils.findAllIpv4Addresses(
"sf sdf sd 10.6.0.254 fs\n\n \r\tfsf10.6.0.2sdfsfsdfs");

Assert.assertEquals(2, addresses.size());
Assert.assertEquals("10.6.0.254", addresses.get(0));
Assert.assertEquals("10.6.0.2", addresses.get(1));
}

@Test
public void mustRejectComponentsWithTrailingZeros() {
List<String> addresses = RegexUtils.findAllIpv4Addresses(
"sf sdf sd 010.6.0.254 fs\n\n \r\tfsf10.6.0.2sdfsfsdfs");

Assert.assertEquals(1, addresses.size());
Assert.assertEquals("10.6.0.2", addresses.get(0));
}

@Test
public void mustRejectComponentsThatExceed255() {
List<String> addresses = RegexUtils.findAllIpv4Addresses(
"sf sdf sd 10.6.0.256 fs\n\n \r\tfsf10.6.0.2sdfsfsdfs");

Assert.assertEquals(1, addresses.size());
Assert.assertEquals("10.6.0.2", addresses.get(0));
}
}

0 comments on commit 174900e

Please sign in to comment.