[hipl-dev] [Branch ~hipl-core/hipl/trunk] Rev 6369: Refactor hosts.Hosts.str_is_lsi() -> hosts.valid_lsi() and add tests.

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

------------------------------------------------------------
revno: 6369
committer: Paul Tötterman <paul.totterman@xxxxxx>
branch nick: methods-to-functions
timestamp: Tue 2012-04-03 22:40:35 +0300
message:
  Refactor hosts.Hosts.str_is_lsi() -> hosts.valid_lsi() and add tests.
modified:
  tools/hipdnsproxy/hipdnsproxy.in
  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/hipdnsproxy.in'
--- tools/hipdnsproxy/hipdnsproxy.in    2012-04-03 19:31:12 +0000
+++ tools/hipdnsproxy/hipdnsproxy.in    2012-04-03 19:40:35 +0000
@@ -472,11 +472,6 @@
             if result:
                 return result
 
-    def str_is_lsi(self, lsi_str):
-        """Is the string a valid Local Scope Identifier?"""
-        for hostsdb in self.hosts:
-            return hostsdb.str_is_lsi(lsi_str)
-
     def cache_name(self, name, addr, ttl):
         """Cache the name-address mapping with ttl in all hosts files."""
         for hostsdb in self.hosts:
@@ -647,7 +642,7 @@
             lr_ptr = None
             addr_str = self.ptr_str_to_addr_str(qname)
             if (not self.disable_lsi and addr_str is not None and
-                self.str_is_lsi(addr_str)):
+                hosts.valid_lsi(addr_str)):
                 addr_str = self.lsi_to_hit(addr_str)
             lr_ptr = self.getaddr(addr_str)
             lr_aaaa_hit = None
@@ -896,7 +891,7 @@
                             qname = packet['questions'][0][0]
                             addr_str = self.ptr_str_to_addr_str(qname)
                             if (addr_str is not None and
-                                self.str_is_lsi(addr_str)):
+                                hosts.valid_lsi(addr_str)):
                                 query = (packet, from_a[0], from_a[1], qname)
                                 hit_str = self.lsi_to_hit(addr_str)
                                 if hit_str is not None:

=== modified file 'tools/hipdnsproxy/hosts.py'
--- tools/hipdnsproxy/hosts.py  2012-04-03 19:31:12 +0000
+++ tools/hipdnsproxy/hosts.py  2012-04-03 19:40:35 +0000
@@ -65,6 +65,35 @@
     return False
 
 
+def valid_lsi(addr):
+    """Is the string a valid Local Scope Identifier?
+
+    >>> valid_lsi('1.0.0.1')
+    True
+    >>> valid_lsi('127.0.0.1')
+    False
+    >>> valid_lsi('1.0.1')
+    False
+    >>> valid_lsi('1.0.0.365')
+    False
+    >>> valid_lsi('1.foobar')
+    False
+    """
+    parts = addr.split('.')
+
+    if not len(parts) == 4:
+        return False
+
+    if not int(parts[0]) == 1:
+        return False
+
+    in_range = all([0 <= int(x) < 256 for x in parts])
+    if not in_range:
+        return False
+
+    return True
+
+
 def normalize(name):
     """Normalize FQDN.
 
@@ -147,12 +176,6 @@
             if keyword == 'search':
                 self.suffixes = tuple([part.lower() for part in parts])
 
-    def str_is_lsi(self, addr_str):
-        """Is the string a valid Local Scope Identifier?"""
-        if addr_str.startswith('1.'):
-            return True
-        return False
-
     def ptr4_str_to_addr_str(self, ptr_str):
         """Convert IPv4 PTR to IPv4 address."""
         in4 = ''
@@ -231,7 +254,7 @@
                     name_hit[name] = (addr, 0)
                 elif valid_ipv6(addr):
                     name_aaaa[name] = (addr, 0)
-                elif not self.str_is_lsi(addr):
+                elif valid_lsi(addr):
                     name_a[name] = (addr, 0)
 
         self.name_a = name_a

Other related posts:

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