from sys import stdin, stdout, exit, argv
import re as RX
import os as OS
+import socket as S
from cStringIO import StringIO
###--------------------------------------------------------------------------
class ResolvingHost (object):
"""
A host name which is being looked up by a bulk-resolver instance.
+
+ Most notably, this is where the flag-handling logic lives for the
+ $FLAGS[HOSTNAME] syntax.
"""
def __init__(me, name):
"""Make a new resolving-host object for the host NAME."""
me.name = name
- me.addr = None
+ me.addr = { 'INET': [], 'INET6': [] }
me.failure = None
- def setaddr(me, addr):
- """Add the address ADDR."""
- me.addr = addr
+ def addaddr(me, af, addr):
+ """
+ Add the address ADDR with address family AF.
+
+ The address family may be `INET' or `INET6'.
+ """
+ me.addr[af].append(addr)
def failed(me, msg):
"""
"""
me.failure = msg
- def get(me):
- """Return the resolved address."""
+ def get(me, flags):
+ """Return a list of addresses according to the FLAGS string."""
if me.failure is not None: raise ResolverFailure(me.name, me.failure)
- return me.addr
-
-class BulkResolver (object):
+ aa = []
+ a4 = me.addr['INET']
+ a6 = me.addr['INET6']
+ all, any = False, False
+ for ch in flags:
+ if ch == '*': all = True
+ elif ch == '4': aa += a4; any = True
+ elif ch == '6': aa += a6; any = True
+ else: raise ValueError("unknown address-resolution flag `%s'" % ch)
+ if not any: aa = a4 + a6
+ if not aa: raise ResolverFailure(me.name, 'no matching addresses found')
+ if not all: aa = [aa[0]]
+ return aa
+
+class BaseBulkResolver (object):
"""
Resolve a number of DNS names in parallel.
def __init__(me):
"""Initialize the resolver."""
me._namemap = {}
- me._noutstand = 0
def prepare(me, name):
"""Prime the resolver to resolve the given host NAME."""
if name not in me._namemap:
me._namemap[name] = host = ResolvingHost(name)
- host._resolv = M.SelResolveByName(
- name,
- lambda cname, alias, addr: me._resolved(host, addr[0]),
- lambda: me._resolved(host, None))
- me._noutstand += 1
+ try:
+ ailist = S.getaddrinfo(name, None, S.AF_UNSPEC, S.SOCK_DGRAM, 0,
+ S.AI_NUMERICHOST | S.AI_NUMERICSERV)
+ except S.gaierror:
+ me._prepare(host, name)
+ else:
+ for af, skty, proto, cname, sa in ailist:
+ if af == S.AF_INET: host.addaddr('INET', sa[0])
+ elif af == S.AF_INET6: host.addaddr('INET6', sa[0])
+
+ def lookup(me, name, flags):
+ """Fetch the address corresponding to the host NAME."""
+ return me._namemap[name].get(flags)
+
+class BresBulkResolver (BaseBulkResolver):
+ """
+ A BulkResolver using mLib's `bres' background resolver.
+
+ This is always available (and might use ADNS), but only does IPv4.
+ """
+
+ def __init__(me):
+ super(BresBulkResolver, me).__init__()
+ """Initialize the resolver."""
+ me._noutstand = 0
+
+ def _prepare(me, host, name):
+ """Arrange to resolve a NAME, reporting the results to HOST."""
+ host._resolv = M.SelResolveByName(
+ name,
+ lambda cname, alias, addr: me._resolved(host, cname, addr),
+ lambda: me._resolved(host, None, []))
+ me._noutstand += 1
def run(me):
"""Run the background DNS resolver until it's finished."""
while me._noutstand: M.select()
- def lookup(me, name):
- """Fetch the address corresponding to the host NAME."""
- return me._namemap[name].get()
-
- def _resolved(me, host, addr):
- """Callback function: remember that ADDR is the address for HOST."""
- if addr is None:
+ def _resolved(me, host, cname, addr):
+ """Callback function: remember that ADDRs are the addresses for HOST."""
+ if not addr:
host.failed('(unknown failure)')
else:
- host.setaddr(addr)
+ if cname is not None: host.name = cname
+ for a in addr: host.addaddr('INET', a)
host._resolv = None
me._noutstand -= 1
+## Select a bulk resolver. Currently, there's only one choice.
+BulkResolver = BresBulkResolver
+
###--------------------------------------------------------------------------
### The configuration parser.
## Match a $(VAR) configuration variable reference; group 1 is the VAR.
RX_REF = RX.compile(r'(?x) \$ \( ([^)]+) \)')
-## Match a $[HOST] name resolution reference; group 1 is the HOST.
-RX_RESOLVE = RX.compile(r'(?x) \$ \[ ([^]]+) \]')
+## Match a $FLAGS[HOST] name resolution reference; group 1 are the flags;
+## group 2 is the HOST.
+RX_RESOLVE = RX.compile(r'(?x) \$ ([46*]*) \[ ([^]]+) \]')
class ConfigSyntaxError (ExpectedError):
def __init__(me, fname, lno, msg):
def _expand(me, string, resolvep):
"""
- Expands $(...) and (optionally) $[...] placeholders in STRING.
+ Expands $(...) and (optionally) $FLAGS[...] placeholders in STRING.
RESOLVEP is a boolean switch: do we bother to tax the resolver or not?
This is turned off by MyConfigParser's resolve() method while it's
collecting hostnames to be resolved.
"""
- string = RX_REF.sub \
- (lambda m: me.get(m.group(1), resolvep), string)
+ string = RX_REF.sub(lambda m: me.get(m.group(1), resolvep), string)
if resolvep:
- string = RX_RESOLVE.sub(lambda m: me._cp._resolver.lookup(m.group(1)),
- string)
+ string = RX_RESOLVE.sub(
+ lambda m: ' '.join(me._cp._resolver.lookup(m.group(2), m.group(1))),
+ string)
return string
def _parents(me):
* It recognizes `$(VAR)' references to configuration variables during
expansion and processes them correctly.
- * It recognizes `$[HOST]' name-resolver requests and handles them
- correctly.
+ * It recognizes `$FLAGS[HOST]' name-resolver requests and handles them
+ correctly. FLAGS consists of characters `4' (IPv4 addresses), `6'
+ (IPv6 addresses), and `*' (all, space-separated, rather than just the
+ first).
* Its parsing behaviour is well-defined.
for key in sec.items():
value = sec.get(key, resolvep = False)
for match in RX_RESOLVE.finditer(value):
- me._resolver.prepare(match.group(1))
+ me._resolver.prepare(match.group(2))
me._resolver.run()
###--------------------------------------------------------------------------