[tor-commits] [metrics-lib/master] Accept exit policy lines with IPv4 masks in dotted-quad format.

karsten at torproject.org karsten at torproject.org
Sat Feb 25 12:03:45 UTC 2012


commit e952d082b036138d6e5a9319e5ee70acdd1545eb
Author: Karsten Loesing <karsten.loesing at gmx.net>
Date:   Fri Feb 17 12:39:39 2012 +0100

    Accept exit policy lines with IPv4 masks in dotted-quad format.
---
 .../torproject/descriptor/impl/ParseHelper.java    |   24 +++++++++++++------
 .../descriptor/impl/ServerDescriptorImplTest.java  |   10 ++++++++
 2 files changed, 26 insertions(+), 8 deletions(-)

diff --git a/src/org/torproject/descriptor/impl/ParseHelper.java b/src/org/torproject/descriptor/impl/ParseHelper.java
index 2779d5a..8db09e2 100644
--- a/src/org/torproject/descriptor/impl/ParseHelper.java
+++ b/src/org/torproject/descriptor/impl/ParseHelper.java
@@ -87,18 +87,26 @@ public class ParseHelper {
     } else if (addressPart.contains("/")) {
       String[] addressParts = addressPart.split("/");
       String address = addressParts[0];
+      String mask = addressParts[1];
       ParseHelper.parseIpv4Address(line, address);
-      int maskValue = -1;
-      try {
-        maskValue = Integer.parseInt(addressPart.substring(
-            addressPart.indexOf("/") + 1));
-      } catch (NumberFormatException e) {
-        /* Handle below. */
-      }
-      if (addressParts.length != 2 || maskValue < 0 || maskValue > 32) {
+      if (addressParts.length != 2) {
         throw new DescriptorParseException("'" + addressPart + "' in "
             + "line '" + line + "' is not a valid address part.");
       }
+      if (mask.contains(".")) {
+        ParseHelper.parseIpv4Address(line, mask);
+      } else {
+        int maskValue = -1;
+        try {
+          maskValue = Integer.parseInt(mask);
+        } catch (NumberFormatException e) {
+          /* Handle below. */
+        }
+        if (maskValue < 0 || maskValue > 32) {
+          throw new DescriptorParseException("'" + mask + "' in line '"
+              + line + "' is not a valid IPv4 mask.");
+        }
+      }
     } else {
       ParseHelper.parseIpv4Address(line, addressPart);
     }
diff --git a/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java b/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java
index 598c3ab..72bec62 100644
--- a/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java
+++ b/test/org/torproject/descriptor/impl/ServerDescriptorImplTest.java
@@ -763,6 +763,16 @@ public class ServerDescriptorImplTest {
   }
 
   @Test()
+  public void testExitPolicyMaskTypes() throws DescriptorParseException {
+    ServerDescriptor descriptor = DescriptorBuilder.
+        createWithExitPolicyLines("reject 192.168.0.0/16:*\n"
+        + "reject 94.134.192.243/255.255.255.0:*");
+    assertEquals(Arrays.asList(new String[] { "reject 192.168.0.0/16:*",
+        "reject 94.134.192.243/255.255.255.0:*"}),
+        descriptor.getExitPolicyLines());
+  }
+
+  @Test()
   public void testRouterSignatureOpt()
       throws DescriptorParseException {
     DescriptorBuilder.createWithRouterSignatureLines("opt "





More information about the tor-commits mailing list