[hipl-dev] [Branch ~hipl-core/hipl/trunk] Rev 6367: Refactor hosts.Hosts.str_is_ipv6() -> hosts.valid_ipv6() and add tests.

  • From: noreply@xxxxxxxxxxxxx
  • To: HIPL core team <hipl-dev@xxxxxxxxxxxxx>
  • Date: Tue, 24 Apr 2012 15:49:11 -0000

------------------------------------------------------------
revno: 6367
committer: Paul Tötterman <paul.totterman@xxxxxx>
branch nick: methods-to-functions
timestamp: Tue 2012-04-03 21:34:55 +0300
message:
  Refactor hosts.Hosts.str_is_ipv6() -> hosts.valid_ipv6() and add tests.
modified:
  tools/hipdnsproxy/hosts.py


--
lp:hipl
https://code.launchpad.net/~hipl-core/hipl/trunk

Your team HIPL core team is subscribed to branch lp:hipl.
To unsubscribe from this branch go to 
https://code.launchpad.net/~hipl-core/hipl/trunk/+edit-subscription
=== modified file 'tools/hipdnsproxy/hosts.py'
--- tools/hipdnsproxy/hosts.py  2012-04-03 18:30:02 +0000
+++ tools/hipdnsproxy/hosts.py  2012-04-03 18:34:55 +0000
@@ -25,12 +25,26 @@
 
 """Hosts file handling for hipdnsproxy."""
 
-import binascii
 import os
 import pyip6
 import time
 
 
+def valid_ipv6(addr):
+    """Is the string a valid IPv6 address?
+
+    TODO(ptman): Improve the test
+
+    >>> valid_ipv6('::1')
+    True
+    >>> valid_ipv6('127.0.0.1')
+    False
+    """
+    if ':' in addr:
+        return True
+    return False
+
+
 def normalize(name):
     """Normalize FQDN.
 
@@ -101,15 +115,6 @@
             if keyword == 'search':
                 self.suffixes = tuple([part.lower() for part in parts])
 
-    def str_is_ipv6(self, addr_str):
-        """Is the string a valid IPv6 address?
-
-        TODO(ptman): Improve the test
-        """
-        if addr_str.find(':') == -1:
-            return False
-        return True
-
     def str_is_hit(self, addr_str):
         """Is the string a valid Host Identity Tag?"""
         if addr_str.startswith('2001:001') or (addr_str.startswith('2001:1')
@@ -199,7 +204,7 @@
 
                 if self.str_is_hit(addr):
                     name_hit[name] = (addr, 0)
-                elif self.str_is_ipv6(addr):
+                elif valid_ipv6(addr):
                     name_aaaa[name] = (addr, 0)
                 elif not self.str_is_lsi(addr):
                     name_a[name] = (addr, 0)
@@ -211,7 +216,7 @@
     def getaddr_from_list(self, addr_str, names):
         """Find hostname matching address from names."""
         for name in names:
-            if self.str_is_ipv6(names[name][0]):
+            if valid_ipv6(names[name][0]):
                 # canonicalize IPv6 address
                 binaddr = pyip6.inet_pton(names[name][0])
                 caddr = pyip6.inet_ntop(binaddr)
@@ -224,7 +229,7 @@
         """Find hostname matching address."""
         if addr is None:
             return None
-        if self.str_is_ipv6(addr):
+        if valid_ipv6(addr):
             # canonicalize IPv6 address
             binaddr = pyip6.inet_pton(addr)
             addr_str = pyip6.inet_ntop(binaddr)
@@ -266,7 +271,7 @@
         valid_to = int(time.time()) + ttl
         if self.str_is_hit(addr):
             self.name_hit[hostname] = (addr, valid_to)
-        elif self.str_is_ipv6(addr):
+        elif valid_ipv6(addr):
             self.name_aaaa[hostname] = (addr, valid_to)
         else:
             self.name_a[hostname] = (addr, valid_to)

Other related posts:

  • » [hipl-dev] [Branch ~hipl-core/hipl/trunk] Rev 6367: Refactor hosts.Hosts.str_is_ipv6() -> hosts.valid_ipv6() and add tests. - noreply