Ticket #6124: patch_ticket_6124.patch

File patch_ticket_6124.patch, 5.5 KB (added by jct, 6 years ago)

A patch with the candidate code in order to solve the ticket.

  • facilitator/facilitator

    From 6feb138c118c4cbc9206f1b7431411409f445da1 Mon Sep 17 00:00:00 2001
    From: Jorge Couchet <jorge.couchet@gmail.com>
    Date: Wed, 28 Nov 2012 20:51:24 +0100
    Subject: [PATCH] Patch for the ticket 6124
    
    ---
     facilitator/facilitator         |   42 ++++++++++++++++++++++-
     facilitator/ticket-6124-test.py |   71 +++++++++++++++++++++++++++++++++++++++
     2 files changed, 112 insertions(+), 1 deletion(-)
     create mode 100755 facilitator/ticket-6124-test.py
    
    diff --git a/facilitator/facilitator b/facilitator/facilitator
    index 26e449d..af480cc 100755
    a b import socket 
    88import sys
    99import threading
    1010import time
     11import re
    1112
    1213import fac
    1314
    class RegSet(object): 
    121122        finally:
    122123            self.cv.release()
    123124
     125    # It tries to guess the IP version for a string coding a numeric IP address.
     126    # It returns 4 for a guessed IPv4 address, 6 for a guessed IPv6 address, and -1 otherwise.
     127    def ip_ver(self, host, port = None):
     128        try:
     129            # The flag 'AI_NUMERICHOST' is disabling domain name resolution.
     130            addrs = socket.getaddrinfo(host, port, 0, socket.SOCK_STREAM, socket.IPPROTO_TCP, socket.AI_NUMERICHOST)
     131            af = addrs[0][0]
     132            if af == socket.AF_INET:
     133                return 4
     134            elif af == socket.AF_INET6:
     135                return 6
     136            else:
     137                return -1
     138        except:
     139            return -1
     140
     141    # It returns a set's element that has an IP version equal to the 'version' parameter.
     142    def fetch_check_ver(self, version):
     143        self.cv.acquire()
     144        try:
     145            if not self.set:
     146                return None
     147            for re in self.set:
     148                if self.ip_ver(re.host) == version:
     149                    return re
     150            return None
     151        finally:
     152            self.cv.release()
     153
    124154    def __len__(self):
    125155        self.cv.acquire()
    126156        try:
    class Handler(SocketServer.StreamRequestHandler): 
    224254    def send_error(self):
    225255        print >> self.wfile, "ERROR"
    226256
     257    # It is receiving a string with a numeric IP address.
     258    # A IPv6 address is enclosed between square brackets.
     259    def ip_ver(self, addr_spec):
     260        if re.match(ur'^\[(.+)\]$', addr_spec):
     261            return 6
     262        else:
     263            return 4
     264
    227265    def do_GET(self, params):
    228         reg = REGS.fetch()
     266        proxy_spec = fac.param_first("FROM", params)
     267        if proxy_spec is not None:
     268            reg = REGS.fetch_check_ver(self.ip_ver(proxy_spec))
    229269        if reg:
    230270            log(u"proxy gets %s, relay %s (now %d)" %
    231271                (safe_str(unicode(reg)), options.relay_spec, len(REGS)))
  • new file facilitator/ticket-6124-test.py

    diff --git a/facilitator/ticket-6124-test.py b/facilitator/ticket-6124-test.py
    new file mode 100755
    index 0000000..a9dbc6b
    - +  
     1#!/usr/bin/env python
     2
     3import subprocess
     4import time
     5import unittest
     6
     7import fac
     8
     9
     10FACILITATOR_PORT = 9002
     11FACILITATOR_ADDR = ("127.0.0.1", FACILITATOR_PORT)
     12
     13class FacilitatorTest(unittest.TestCase):
     14
     15    def setUp(self):
     16        self.process = subprocess.Popen(["./facilitator", "-d", "-p", str(FACILITATOR_PORT), "-r", "0.0.1.0:1", "-l", "/dev/null"])
     17        time.sleep(0.1)
     18
     19    def tearDown(self):
     20        self.process.terminate()
     21
     22    # IPv6 client and IPv4 proxy
     23    def test_t1(self):
     24        proxy_addr = ("145.89.56.7", None)
     25        client_spec = "[234:1::1:2]:455"
     26        client_addr = fac.parse_addr_spec(client_spec, defhost=proxy_addr[0])
     27        fac.put_reg(FACILITATOR_ADDR, client_addr, proxy_addr)
     28        reg = fac.get_reg(FACILITATOR_ADDR, proxy_addr)
     29        self.assertEqual(reg['client'], "")
     30
     31    # IPv6 client and IPv6 proxy
     32    def test_t2(self):
     33        proxy_addr = ("[145::2]", None)
     34        client_spec = "[234:1::1:2]:455"
     35        client_addr = fac.parse_addr_spec(client_spec, defhost=proxy_addr[0])
     36        fac.put_reg(FACILITATOR_ADDR, client_addr, proxy_addr)
     37        reg = fac.get_reg(FACILITATOR_ADDR, proxy_addr)
     38        self.assertEqual(reg['client'], "[234:1::1:2]:455")
     39
     40    # IPv4 client and IPv6 proxy
     41    def test_t3(self):
     42        proxy_addr = ("[145::1]", None)
     43        client_spec = "234.12.1.1:455"
     44        client_addr = fac.parse_addr_spec(client_spec, defhost=proxy_addr[0])
     45        fac.put_reg(FACILITATOR_ADDR, client_addr, proxy_addr)
     46        reg = fac.get_reg(FACILITATOR_ADDR, proxy_addr)
     47        self.assertEqual(reg['client'], "")
     48
     49    # IPv4 client and IPv4 proxy
     50    def test_t4(self):
     51        proxy_addr = ("156.45.89.70", None)
     52        client_spec = "234.12.1.1:455"
     53        client_addr = fac.parse_addr_spec(client_spec, defhost=proxy_addr[0])
     54        fac.put_reg(FACILITATOR_ADDR, client_addr, proxy_addr)
     55        reg = fac.get_reg(FACILITATOR_ADDR, proxy_addr)
     56        self.assertEqual(reg['client'], "234.12.1.1:455")
     57
     58    # IPv6 client, IPv4 client and IPv4 proxy
     59    def test_t5(self):
     60        proxy_addr = ("156.45.89.70", None)
     61        client_spec = "[234:1::1:2]:455"
     62        client_addr = fac.parse_addr_spec(client_spec, defhost=proxy_addr[0])
     63        fac.put_reg(FACILITATOR_ADDR, client_addr, proxy_addr)
     64        client_spec = "234.12.1.1:455"
     65        client_addr = fac.parse_addr_spec(client_spec, defhost=proxy_addr[0])
     66        fac.put_reg(FACILITATOR_ADDR, client_addr, proxy_addr)
     67        reg = fac.get_reg(FACILITATOR_ADDR, proxy_addr)
     68        self.assertEqual(reg['client'], "234.12.1.1:455")
     69
     70if __name__ == "__main__":
     71    unittest.main()