svc/conntrack.in: Introduce a function for parsing address strings.
authorMark Wooding <mdw@distorted.org.uk>
Thu, 28 Sep 2017 18:16:33 +0000 (19:16 +0100)
committerMark Wooding <mdw@distorted.org.uk>
Thu, 28 Jun 2018 23:29:23 +0000 (00:29 +0100)
It still returns a raw integer -- for now.  But it's a start.

svc/conntrack.in

index a55e4c3..061d6d7 100644 (file)
@@ -94,6 +94,9 @@ def toposort(cmp, things):
     if done:
       break
 
+def parse_address(addrstr):
+  return unpack('>L', S.inet_aton(addrstr))[0]
+
 ###--------------------------------------------------------------------------
 ### Parse the configuration file.
 
@@ -170,12 +173,12 @@ class Config (object):
         ## and MASK is either a dotted-quad or a single integer N indicating
         ## a mask with N leading ones followed by trailing zeroes.
         slash = net.index('/')
-        addr, = unpack('>L', S.inet_aton(net[:slash]))
+        addr = parse_address(net[:slash])
         if net[slash + 1:].isdigit():
           n = int(net[slash + 1:], 10)
           mask = (1 << 32) - (1 << 32 - n)
         else:
-          mask, = unpack('>L', S.inet_aton(net[slash + 1:]))
+          mask = parse_address(net[slash + 1:])
         pats.append((tag, peer, addr & mask, mask))
 
       ## Annoyingly, RawConfigParser doesn't preserve the order of options.
@@ -229,7 +232,7 @@ def localaddr(peer):
     try:
       sk.connect((peer, 1))
       addr, _ = sk.getsockname()
-      addr, = unpack('>L', S.inet_aton(addr))
+      addr = parse_address(addr)
       return addr
     except S.error:
       return None