[svn] commit: r3234 - /branches/trac353/src/lib/python/isc/net/addr.py

BIND 10 source code commits bind10-changes at lists.isc.org
Fri Oct 15 16:01:01 UTC 2010


Author: vorner
Date: Fri Oct 15 16:01:01 2010
New Revision: 3234

Log:
Guess the address type first, then run inet_pton

This avoids call to inet_pton call that fails when trying to translate
v6 address. It should also workaround a bug MacOS version, that
incorrectly accepts 0000.0.0.0 as an IP address.

Modified:
    branches/trac353/src/lib/python/isc/net/addr.py

Modified: branches/trac353/src/lib/python/isc/net/addr.py
==============================================================================
--- branches/trac353/src/lib/python/isc/net/addr.py (original)
+++ branches/trac353/src/lib/python/isc/net/addr.py Fri Oct 15 16:01:01 2010
@@ -15,6 +15,14 @@
 
 """Module where address representations live."""
 import socket
+import re
+
+# These regular expressions are not validating. They are supposed to
+# guess which kind of address it is and throw away just obvious nonsense.
+# It is expected that inet_pton will complain if it isn't an address, so
+# they can have false positives.
+isv4 = re.compile(r'^([0-9]{1,3}\.){3}[0-9]{1,3}$')
+isv6 = re.compile(r'^([0-9a-f]{,4}:){,7}[0-9a-f]{,4}$', re.IGNORECASE)
 
 class InvalidAddress(ValueError):
     """Exception for invalid addresses."""
@@ -31,18 +39,17 @@
         an InvalidAddr exception if the provided string isn't valid address.
         """
         try:
-            a = socket.inet_pton(socket.AF_INET, addr)
-            self.family = socket.AF_INET
-            self.addr = a
-            return
-        except:
-            pass
-
-        try:
-            a = socket.inet_pton(socket.AF_INET6, addr)
-            self.family = socket.AF_INET6
-            self.addr = a
-            return
+            if isv4.match(addr):
+                a = socket.inet_pton(socket.AF_INET, addr)
+                self.family = socket.AF_INET
+                self.addr = a
+            elif isv6.match(addr):
+                a = socket.inet_pton(socket.AF_INET6, addr)
+                self.family = socket.AF_INET6
+                self.addr = a
+            else:
+                raise InvalidAddress(addr +
+                    ' is not a valid IPv4 nor IPv6 address')
         except socket.error as e:
             raise InvalidAddress(str(e))
 




More information about the bind10-changes mailing list