Ticket #6124: 6124-twopools.patch

File 6124-twopools.patch, 6.2 KB (added by dcf, 6 years ago)
  • facilitator/facilitator

    From c1f940d5558aa98a67bcf67b2b281fe8a1d4b985 Mon Sep 17 00:00:00 2001
    From: David Fifield <david@bamsoftware.com>
    Date: Wed, 19 Dec 2012 19:16:43 -0800
    Subject: [PATCH 1/3] Make separate reg pools for IPv4 and IPv6.
    
    This is only cosmetic at the moment, as all regs will go into the IPv4
    pool.
    ---
     facilitator/facilitator |   18 ++++++++++++------
     1 file changed, 12 insertions(+), 6 deletions(-)
    
    diff --git a/facilitator/facilitator b/facilitator/facilitator
    index 97155ca..c1cf89a 100755
    a b class Handler(SocketServer.StreamRequestHandler): 
    234234        print >> self.wfile, "ERROR"
    235235
    236236    def do_GET(self, params):
    237         reg = REGS.fetch()
     237        reg = REGS_IPV4.fetch()
    238238        if reg:
    239239            log(u"proxy gets %s, relay %s (now %d)" %
    240                 (safe_str(unicode(reg)), options.relay_spec, len(REGS)))
     240                (safe_str(unicode(reg)), options.relay_spec, num_regs()))
    241241            print >> self.wfile, fac.render_transaction("OK", ("CLIENT", str(reg)), ("RELAY", options.relay_spec))
    242242        else:
    243243            log(u"proxy gets none")
    class Handler(SocketServer.StreamRequestHandler): 
    260260            self.send_error()
    261261            return False
    262262
    263         if REGS.add(reg):
    264             log(u"client %s (now %d)" % (safe_str(unicode(reg)), len(REGS)))
     263        if REGS_IPV4.add(reg):
     264            log(u"client %s (now %d)" % (safe_str(unicode(reg)), num_regs()))
    265265        else:
    266             log(u"client %s (already present, now %d)" % (safe_str(unicode(reg)), len(REGS)))
     266            log(u"client %s (already present, now %d)" % (safe_str(unicode(reg)), num_regs()))
    267267
    268268        self.send_ok()
    269269        return True
    class Handler(SocketServer.StreamRequestHandler): 
    273273class Server(SocketServer.ThreadingMixIn, SocketServer.TCPServer):
    274274    allow_reuse_address = True
    275275
    276 REGS = RegSet()
     276# Separate pools for IPv4 and IPv6 clients.
     277REGS_IPV4 = RegSet()
     278REGS_IPV6 = RegSet()
     279
     280def num_regs():
     281    """Return the total number of registrations."""
     282    return len(REGS_IPV4) + len(REGS_IPV6)
    277283
    278284def main():
    279285    opts, args = getopt.gnu_getopt(sys.argv[1:], "dhl:p:r:",
  • facilitator/facilitator

    -- 
    1.7.10.4
    
    From a3b3d601d66a5c5faf711ade9063e8a0abe077a9 Mon Sep 17 00:00:00 2001
    From: David Fifield <david@bamsoftware.com>
    Date: Wed, 19 Dec 2012 19:20:36 -0800
    Subject: [PATCH 2/3] Add abstractions for using IPv4/IPv6 reg pools.
    
    Still always going to the IPv4 pool.
    ---
     facilitator/facilitator |   45 +++++++++++++++++++++++++++++++++++++++++++--
     1 file changed, 43 insertions(+), 2 deletions(-)
    
    diff --git a/facilitator/facilitator b/facilitator/facilitator
    index c1cf89a..3cef702 100755
    a b class Handler(SocketServer.StreamRequestHandler): 
    234234        print >> self.wfile, "ERROR"
    235235
    236236    def do_GET(self, params):
    237         reg = REGS_IPV4.fetch()
     237        proxy_spec = fac.param_first("FROM", params)
     238        if proxy_spec is None:
     239            log(u"GET missing FROM param")
     240            self.send_error()
     241            return False
     242        try:
     243            proxy_addr = fac.parse_addr_spec(proxy_spec, defport=0)
     244        except ValueError, e:
     245            log(u"syntax error in proxy address %s: %s" % (safe_str(repr(proxy_spec)), safe_str(repr(str(e)))))
     246            self.send_error()
     247            return False
     248
     249        try:
     250            reg = get_reg_for_proxy(proxy_addr)
     251        except Exception, e:
     252            log(u"error getting reg for proxy address %s: %s" % (safe_str(repr(proxy_spec)), safe_str(repr(str(e)))))
     253            self.send_error()
     254            return False
    238255        if reg:
    239256            log(u"proxy gets %s, relay %s (now %d)" %
    240257                (safe_str(unicode(reg)), options.relay_spec, num_regs()))
    class Handler(SocketServer.StreamRequestHandler): 
    260277            self.send_error()
    261278            return False
    262279
    263         if REGS_IPV4.add(reg):
     280        try:
     281            ok = put_reg(reg):
     282        except Exception, e:
     283            log(u"error putting reg %s: %s" % (safe_str(repr(client_spec)), safe_str(repr(str(e)))))
     284            self.send_error()
     285            return False
     286        if ok:
    264287            log(u"client %s (now %d)" % (safe_str(unicode(reg)), num_regs()))
    265288        else:
    266289            log(u"client %s (already present, now %d)" % (safe_str(unicode(reg)), num_regs()))
    def num_regs(): 
    281304    """Return the total number of registrations."""
    282305    return len(REGS_IPV4) + len(REGS_IPV6)
    283306
     307def regs_for_af(af):
     308    """Return the correct regs pool for the given address family."""
     309    if af == socket.AF_INET:
     310        return REGS_IPV4
     311    elif af == socket.AF_INET6:
     312        return REGS_IPV6
     313    else:
     314        raise ValueError("unknown address family %d" % af)
     315
     316def get_reg_for_proxy(proxy_addr):
     317    """Get a client registration appropriate for the given proxy (one of a
     318    matching address family)."""
     319    return REGS_IPV4.fetch()
     320
     321def put_reg(reg):
     322    """Add a registration."""
     323    REGS_IPV4.add(reg)
     324
    284325def main():
    285326    opts, args = getopt.gnu_getopt(sys.argv[1:], "dhl:p:r:",
    286327        ["debug", "help", "log=", "port=", "pidfile=", "relay=", "unsafe-logging"])
  • facilitator/facilitator

    -- 
    1.7.10.4
    
    From 20427b644996c76eba0479603e84686146828d00 Mon Sep 17 00:00:00 2001
    From: David Fifield <david@bamsoftware.com>
    Date: Wed, 19 Dec 2012 19:26:30 -0800
    Subject: [PATCH 3/3] Add a simple function to guess an address family.
    
    ---
     facilitator/facilitator |    6 ++++++
     1 file changed, 6 insertions(+)
    
    diff --git a/facilitator/facilitator b/facilitator/facilitator
    index 3cef702..2eb267d 100755
    a b def regs_for_af(af): 
    313313    else:
    314314        raise ValueError("unknown address family %d" % af)
    315315
     316def addr_af(addr_str):
     317    """Return the address family for an address string. This is a plain string,
     318    not a tuple, and IPv6 addresses are not bracketed."""
     319    addrs = socket.getaddrinfo(addr_str, 0, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST)
     320    return addrs[0][0]
     321
    316322def get_reg_for_proxy(proxy_addr):
    317323    """Get a client registration appropriate for the given proxy (one of a
    318324    matching address family)."""