Commit b2bf1ae6 authored by Joanne Hugé's avatar Joanne Hugé Committed by Tom Niget

python2 to python3: Use 2to3 script

parent f2fd7247
...@@ -72,7 +72,7 @@ if args.duration: ...@@ -72,7 +72,7 @@ if args.duration:
signal.signal(signal.SIGALRM, handler) signal.signal(signal.SIGALRM, handler)
signal.alarm(args.duration) signal.alarm(args.duration)
execfile("fixnemu.py") exec(compile(open("fixnemu.py", "rb").read(), "fixnemu.py", 'exec'))
# create nodes # create nodes
for name in """internet=I registry=R for name in """internet=I registry=R
...@@ -89,9 +89,9 @@ for name in """internet=I registry=R ...@@ -89,9 +89,9 @@ for name in """internet=I registry=R
'net.ipv4.icmp_echo_ignore_broadcasts=0')).wait() 'net.ipv4.icmp_echo_ignore_broadcasts=0')).wait()
node._screen = node.Popen((SCREEN, '-DmS', name)) node._screen = node.Popen((SCREEN, '-DmS', name))
node.screen = (lambda name: lambda *cmd: node.screen = (lambda name: lambda *cmd:
subprocess.call([SCREEN, '-r', name, '-X', 'eval'] + map( subprocess.call([SCREEN, '-r', name, '-X', 'eval'] + list(map(
"""screen sh -c 'set %s; "\$@"; echo "\$@"; exec $SHELL'""" """screen sh -c 'set %s; "\$@"; echo "\$@"; exec $SHELL'"""
.__mod__, cmd)))(name) .__mod__, cmd))))(name)
# create switch # create switch
switch1 = nemu.Switch() switch1 = nemu.Switch()
...@@ -208,10 +208,10 @@ for m in machine6, machine7, machine8: ...@@ -208,10 +208,10 @@ for m in machine6, machine7, machine8:
null = file(os.devnull, "r+") null = file(os.devnull, "r+")
for ip in '10.1.1.2', '10.1.1.3', '10.2.1.2', '10.2.1.3': for ip in '10.1.1.2', '10.1.1.3', '10.2.1.2', '10.2.1.3':
if machine1.Popen(('ping', '-c1', ip), stdout=null).wait(): if machine1.Popen(('ping', '-c1', ip), stdout=null).wait():
print 'Failed to ping %s' % ip print('Failed to ping %s' % ip)
break break
else: else:
print "Connectivity IPv4 OK!" print("Connectivity IPv4 OK!")
nodes = [] nodes = []
gateway1.screen('miniupnpd -d -f miniupnpd.conf -P miniupnpd.pid' gateway1.screen('miniupnpd -d -f miniupnpd.conf -P miniupnpd.pid'
...@@ -297,7 +297,7 @@ if args.ping: ...@@ -297,7 +297,7 @@ if args.ping:
'2001:db8:43:1::1' if i == 10 else '2001:db8:43:1::1' if i == 10 else
# Only 1 address for machine2 because prefix_len = 80,+48 = 128 # Only 1 address for machine2 because prefix_len = 80,+48 = 128
'2001:db8:42:%s::1' % i '2001:db8:42:%s::1' % i
for i in xrange(11) for i in range(11)
if i != j] if i != j]
name = machine.name if machine.short[0] == 'R' else 'm' + machine.short name = machine.name if machine.short[0] == 'R' else 'm' + machine.short
machine.screen('python ping.py {} {}'.format(name, ' '.join(ips))) machine.screen('python ping.py {} {}'.format(name, ' '.join(ips)))
...@@ -314,30 +314,30 @@ class testHMAC(Thread): ...@@ -314,30 +314,30 @@ class testHMAC(Thread):
reg1_db.text_factory = reg2_db.text_factory = str reg1_db.text_factory = reg2_db.text_factory = str
m_net1 = 'registry', 'm1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8' m_net1 = 'registry', 'm1', 'm2', 'm3', 'm4', 'm5', 'm6', 'm7', 'm8'
m_net2 = 'registry2', 'm10' m_net2 = 'registry2', 'm10'
print 'Testing HMAC, letting the time to machines to create tunnels...' print('Testing HMAC, letting the time to machines to create tunnels...')
time.sleep(45) time.sleep(45)
print 'Check that the initial HMAC config is deployed on network 1' print('Check that the initial HMAC config is deployed on network 1')
test_hmac.checkHMAC(reg1_db, m_net1) test_hmac.checkHMAC(reg1_db, m_net1)
print 'Test that a HMAC update works with nodes that are up' print('Test that a HMAC update works with nodes that are up')
registry.backticks_raise(updateHMAC) registry.backticks_raise(updateHMAC)
print 'Updated HMAC (config = hmac0 & hmac1), waiting...' print('Updated HMAC (config = hmac0 & hmac1), waiting...')
time.sleep(60) time.sleep(60)
print 'Checking HMAC on machines connected to registry 1...' print('Checking HMAC on machines connected to registry 1...')
test_hmac.checkHMAC(reg1_db, m_net1) test_hmac.checkHMAC(reg1_db, m_net1)
print ('Test that machines can update upon reboot ' + print(('Test that machines can update upon reboot ' +
'when they were off during a HMAC update.') 'when they were off during a HMAC update.'))
test_hmac.killRe6st(machine1) test_hmac.killRe6st(machine1)
print 'Re6st on machine 1 is stopped' print('Re6st on machine 1 is stopped')
time.sleep(5) time.sleep(5)
registry.backticks_raise(updateHMAC) registry.backticks_raise(updateHMAC)
print 'Updated HMAC on registry (config = hmac1 & hmac2), waiting...' print('Updated HMAC on registry (config = hmac1 & hmac2), waiting...')
time.sleep(60) time.sleep(60)
machine1.screen(machine1.re6st_cmdline) machine1.screen(machine1.re6st_cmdline)
print 'Started re6st on machine 1, waiting for it to get new conf' print('Started re6st on machine 1, waiting for it to get new conf')
time.sleep(60) time.sleep(60)
print 'Checking HMAC on machines connected to registry 1...' print('Checking HMAC on machines connected to registry 1...')
test_hmac.checkHMAC(reg1_db, m_net1) test_hmac.checkHMAC(reg1_db, m_net1)
print 'Testing of HMAC done!' print('Testing of HMAC done!')
# TODO: missing last step # TODO: missing last step
reg1_db.close() reg1_db.close()
reg2_db.close() reg2_db.close()
...@@ -399,7 +399,7 @@ def route_svg(ipv4, z = 4, default = type('', (), {'short': None})): ...@@ -399,7 +399,7 @@ def route_svg(ipv4, z = 4, default = type('', (), {'short': None})):
gv.append('%s[pos="%s,%s!"];' gv.append('%s[pos="%s,%s!"];'
% (n.name, z * math.cos(a * i), z * math.sin(a * i))) % (n.name, z * math.cos(a * i), z * math.sin(a * i)))
l = [] l = []
for p, r in graph[n].iteritems(): for p, r in graph[n].items():
j = abs(nodes.index(p[0]) - i) j = abs(nodes.index(p[0]) - i)
l.append((min(j, N - j), p, r)) l.append((min(j, N - j), p, r))
for j, (l, (p, t), r) in enumerate(sorted(l)): for j, (l, (p, t), r) in enumerate(sorted(l)):
...@@ -427,9 +427,9 @@ def route_svg(ipv4, z = 4, default = type('', (), {'short': None})): ...@@ -427,9 +427,9 @@ def route_svg(ipv4, z = 4, default = type('', (), {'short': None})):
).communicate('\n'.join(gv))[0] ).communicate('\n'.join(gv))[0]
if args.port: if args.port:
import SimpleHTTPServer, SocketServer import http.server, socketserver
class Handler(SimpleHTTPServer.SimpleHTTPRequestHandler): class Handler(http.server.SimpleHTTPRequestHandler):
_path_match = re.compile('/(.+)\.(html|svg)$').match _path_match = re.compile('/(.+)\.(html|svg)$').match
pages = 'ipv6', 'ipv4', 'tunnels' pages = 'ipv6', 'ipv4', 'tunnels'
...@@ -439,7 +439,7 @@ if args.port: ...@@ -439,7 +439,7 @@ if args.port:
try: try:
name, ext = self._path_match(self.path).groups() name, ext = self._path_match(self.path).groups()
page = self.pages.index(name) page = self.pages.index(name)
except AttributeError, ValueError: except AttributeError as ValueError:
if self.path == '/': if self.path == '/':
self.send_response(302) self.send_response(302)
self.send_header('Location', self.pages[0] + '.html') self.send_header('Location', self.pages[0] + '.html')
...@@ -513,7 +513,7 @@ if args.port: ...@@ -513,7 +513,7 @@ if args.port:
self.end_headers() self.end_headers()
self.wfile.write(body) self.wfile.write(body)
class TCPServer(SocketServer.TCPServer): class TCPServer(socketserver.TCPServer):
allow_reuse_address = True allow_reuse_address = True
TCPServer(('', args.port), Handler).serve_forever() TCPServer(('', args.port), Handler).serve_forever()
......
...@@ -56,7 +56,7 @@ def _get_all_route_data(): ...@@ -56,7 +56,7 @@ def _get_all_route_data():
metric)) metric))
return ret return ret
get_all_route_data.func_code = _get_all_route_data.func_code get_all_route_data.__code__ = _get_all_route_data.__code__
interface__init__ = interface.__init__ interface__init__ = interface.__init__
def __init__(self, *args, **kw): def __init__(self, *args, **kw):
...@@ -65,12 +65,12 @@ def __init__(self, *args, **kw): ...@@ -65,12 +65,12 @@ def __init__(self, *args, **kw):
self.name = self.name.split('@',1)[0] self.name = self.name.split('@',1)[0]
interface.__init__ = __init__ interface.__init__ = __init__
get_addr_data.orig = function(get_addr_data.func_code, get_addr_data.orig = function(get_addr_data.__code__,
get_addr_data.func_globals) get_addr_data.__globals__)
def _get_addr_data(): def _get_addr_data():
byidx, bynam = get_addr_data.orig() byidx, bynam = get_addr_data.orig()
return byidx, {name.split('@',1)[0]: a for name, a in bynam.iteritems()} return byidx, {name.split('@',1)[0]: a for name, a in bynam.items()}
get_addr_data.func_code = _get_addr_data.func_code get_addr_data.__code__ = _get_addr_data.__code__
@staticmethod @staticmethod
def _gen_if_name(): def _gen_if_name():
......
...@@ -64,7 +64,7 @@ class Ping(Thread): ...@@ -64,7 +64,7 @@ class Ping(Thread):
os.utime(csv_path, (time.time(), time.time())) os.utime(csv_path, (time.time(), time.time()))
for add in no_responses: for add in no_responses:
print('No response from %s with seq no %d' % (add, seq)) print(('No response from %s with seq no %d' % (add, seq)))
parser = argparse.ArgumentParser() parser = argparse.ArgumentParser()
parser.add_argument('n', help = 'my machine name (m1,m2...)') parser.add_argument('n', help = 'my machine name (m1,m2...)')
......
...@@ -30,4 +30,4 @@ def __file__(): ...@@ -30,4 +30,4 @@ def __file__():
return os.path.join(sys.path[0], sys.argv[1]) return os.path.join(sys.path[0], sys.argv[1])
__file__ = __file__() __file__ = __file__()
execfile(__file__) exec(compile(open(__file__, "rb").read(), __file__, 'exec'))
...@@ -34,10 +34,10 @@ def checkHMAC(db, machines): ...@@ -34,10 +34,10 @@ def checkHMAC(db, machines):
else: else:
i = 0 if hmac[0] else 1 i = 0 if hmac[0] else 1
if hmac[i] != sign or hmac[i+1] != accept: if hmac[i] != sign or hmac[i+1] != accept:
print 'HMAC config wrong for in %s' % args print('HMAC config wrong for in %s' % args)
rc = False rc = False
if rc: if rc:
print('All nodes use Babel with the correct HMAC configuration') print('All nodes use Babel with the correct HMAC configuration')
else: else:
print('Expected config: %s' % dict(zip(BABEL_HMAC, hmac))) print(('Expected config: %s' % dict(list(zip(BABEL_HMAC, hmac)))))
return rc return rc
...@@ -93,20 +93,18 @@ class Cache(object): ...@@ -93,20 +93,18 @@ class Cache(object):
self._registry.getNetworkConfig(self._prefix))) self._registry.getNetworkConfig(self._prefix)))
base64 = x.pop('', ()) base64 = x.pop('', ())
config = {} config = {}
for k, v in x.iteritems(): for k, v in x.items():
k = str(k) k = str(k)
if k.startswith('babel_hmac'): if k.startswith('babel_hmac'):
if v: if v:
v = self._decrypt(v.decode('base64')) v = self._decrypt(v.decode('base64'))
elif k in base64: elif k in base64:
v = v.decode('base64') v = v.decode('base64')
elif type(v) is unicode:
v = str(v)
elif isinstance(v, (list, dict)): elif isinstance(v, (list, dict)):
k += ':json' k += ':json'
v = json.dumps(v) v = json.dumps(v)
config[k] = v config[k] = v
except socket.error, e: except socket.error as e:
logging.warning(e) logging.warning(e)
return return
except Exception: except Exception:
...@@ -135,11 +133,11 @@ class Cache(object): ...@@ -135,11 +133,11 @@ class Cache(object):
db.executemany("INSERT OR REPLACE INTO config VALUES(?,?)", db.executemany("INSERT OR REPLACE INTO config VALUES(?,?)",
((k, buffer(v) if k in base64 or ((k, buffer(v) if k in base64 or
k.startswith('babel_hmac') else v) k.startswith('babel_hmac') else v)
for k, v in config.iteritems())) for k, v in config.items()))
self._loadConfig(config.iteritems()) self._loadConfig(iter(config.items()))
return [k[:-5] if k.endswith(':json') else k return [k[:-5] if k.endswith(':json') else k
for k in chain(remove, (k for k in chain(remove, (k
for k, v in config.iteritems() for k, v in config.items()
if k not in old or old[k] != v))] if k not in old or old[k] != v))]
def warnProtocol(self): def warnProtocol(self):
...@@ -240,7 +238,7 @@ class Cache(object): ...@@ -240,7 +238,7 @@ class Cache(object):
try: try:
bootpeer = self._registry.getBootstrapPeer(self._prefix) bootpeer = self._registry.getBootstrapPeer(self._prefix)
prefix, address = self._decrypt(bootpeer).split() prefix, address = self._decrypt(bootpeer).split()
except (socket.error, subprocess.CalledProcessError, ValueError), e: except (socket.error, subprocess.CalledProcessError, ValueError) as e:
logging.warning('Failed to bootstrap (%s)', logging.warning('Failed to bootstrap (%s)',
e if bootpeer else 'no peer returned') e if bootpeer else 'no peer returned')
else: else:
...@@ -276,5 +274,5 @@ class Cache(object): ...@@ -276,5 +274,5 @@ class Cache(object):
def getCountry(self, ip): def getCountry(self, ip):
try: try:
return self._registry.getCountry(self._prefix, ip) return self._registry.getCountry(self._prefix, ip)
except socket.error, e: except socket.error as e:
logging.warning('Failed to get country (%s)', ip) logging.warning('Failed to get country (%s)', ip)
...@@ -101,7 +101,7 @@ def main(): ...@@ -101,7 +101,7 @@ def main():
if config.req: if config.req:
components.update(config.req) components.update(config.req)
subj = req.get_subject() subj = req.get_subject()
for k, v in components.iteritems(): for k, v in list(components.items()):
if k in reserved: if k in reserved:
sys.exit(k + " field is reserved.") sys.exit(k + " field is reserved.")
if v: if v:
...@@ -116,11 +116,11 @@ def main(): ...@@ -116,11 +116,11 @@ def main():
token = '' token = ''
elif not token: elif not token:
if not config.email: if not config.email:
config.email = raw_input('Please enter your email address: ') config.email = eval(input('Please enter your email address: '))
s.requestToken(config.email) s.requestToken(config.email)
token_advice = "Use --token to retry without asking a new token\n" token_advice = "Use --token to retry without asking a new token\n"
while not token: while not token:
token = raw_input('Please enter your token: ') token = eval(input('Please enter your token: '))
try: try:
with open(key_path) as f: with open(key_path) as f:
...@@ -131,7 +131,7 @@ def main(): ...@@ -131,7 +131,7 @@ def main():
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise raise
bits = ca.get_pubkey().bits() bits = ca.get_pubkey().bits()
print "Generating %s-bit key ..." % bits print(("Generating %s-bit key ..." % bits))
pkey = crypto.PKey() pkey = crypto.PKey()
pkey.generate_key(crypto.TYPE_RSA, bits) pkey.generate_key(crypto.TYPE_RSA, bits)
key = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey) key = crypto.dump_privatekey(crypto.FILETYPE_PEM, pkey)
...@@ -164,13 +164,13 @@ def main(): ...@@ -164,13 +164,13 @@ def main():
cert = loadCert(cert) cert = loadCert(cert)
not_after = x509.notAfter(cert) not_after = x509.notAfter(cert)
print("Setup complete. Certificate is valid until %s UTC" print(("Setup complete. Certificate is valid until %s UTC"
" and will be automatically renewed after %s UTC.\n" " and will be automatically renewed after %s UTC.\n"
"Do not forget to backup to your private key (%s) or" "Do not forget to backup to your private key (%s) or"
" you will lose your assigned subnet." % ( " you will lose your assigned subnet." % (
time.asctime(time.gmtime(not_after)), time.asctime(time.gmtime(not_after)),
time.asctime(time.gmtime(not_after - registry.RENEW_PERIOD)), time.asctime(time.gmtime(not_after - registry.RENEW_PERIOD)),
key_path)) key_path)))
if not os.path.lexists(conf_path): if not os.path.lexists(conf_path):
create(conf_path, """\ create(conf_path, """\
...@@ -193,8 +193,8 @@ key %s ...@@ -193,8 +193,8 @@ key %s
cn = x509.subnetFromCert(cert) cn = x509.subnetFromCert(cert)
subnet = network + utils.binFromSubnet(cn) subnet = network + utils.binFromSubnet(cn)
print "Your subnet: %s/%u (CN=%s)" \ print(("Your subnet: %s/%u (CN=%s)" \
% (utils.ipFromBin(subnet), len(subnet), cn) % (utils.ipFromBin(subnet), len(subnet), cn)))
if __name__ == "__main__": if __name__ == "__main__":
main() main()
...@@ -246,7 +246,7 @@ def main(): ...@@ -246,7 +246,7 @@ def main():
try: try:
from re6st.upnpigd import Forwarder from re6st.upnpigd import Forwarder
forwarder = Forwarder('re6stnet openvpn server') forwarder = Forwarder('re6stnet openvpn server')
except Exception, e: except Exception as e:
if ipv4: if ipv4:
raise raise
logging.info("%s: assume we are not NATed", e) logging.info("%s: assume we are not NATed", e)
...@@ -256,10 +256,10 @@ def main(): ...@@ -256,10 +256,10 @@ def main():
forwarder.addRule(port, proto) forwarder.addRule(port, proto)
address.append(forwarder.checkExternalIp()) address.append(forwarder.checkExternalIp())
elif 'any' not in ipv4: elif 'any' not in ipv4:
address += map(ip_changed, ipv4) address += list(map(ip_changed, ipv4))
ipv4_any = () ipv4_any = ()
if ipv6: if ipv6:
address += map(ip_changed, ipv6) address += list(map(ip_changed, ipv6))
ipv6_any = () ipv6_any = ()
else: else:
ip_changed = remote_gateway = None ip_changed = remote_gateway = None
...@@ -299,7 +299,7 @@ def main(): ...@@ -299,7 +299,7 @@ def main():
timeout = 4 * cache.hello timeout = 4 * cache.hello
cleanup = [lambda: cache.cacheMinimize(config.client_count), cleanup = [lambda: cache.cacheMinimize(config.client_count),
lambda: shutil.rmtree(config.run, True)] lambda: shutil.rmtree(config.run, True)]
utils.makedirs(config.run, 0700) utils.makedirs(config.run, 0o700)
control_socket = os.path.join(config.run, 'babeld.sock') control_socket = os.path.join(config.run, 'babeld.sock')
if config.client_count and not config.client: if config.client_count and not config.client:
tunnel_manager = tunnel.TunnelManager(control_socket, tunnel_manager = tunnel.TunnelManager(control_socket,
...@@ -362,7 +362,7 @@ def main(): ...@@ -362,7 +362,7 @@ def main():
if not dh: if not dh:
dh = os.path.join(config.state, "dh.pem") dh = os.path.join(config.state, "dh.pem")
cache.getDh(dh) cache.getDh(dh)
for iface, (port, proto) in server_tunnels.iteritems(): for iface, (port, proto) in server_tunnels.items():
r, x = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM) r, x = socket.socketpair(socket.AF_UNIX, socket.SOCK_DGRAM)
utils.setCloexec(r) utils.setCloexec(r)
cleanup.append(plib.server(iface, config.max_clients, cleanup.append(plib.server(iface, config.max_clients,
...@@ -442,7 +442,7 @@ def main(): ...@@ -442,7 +442,7 @@ def main():
except: except:
pass pass
exit.release() exit.release()
except ReexecException, e: except ReexecException as e:
logging.info(e) logging.info(e)
except Exception: except Exception:
utils.log_exception() utils.log_exception()
...@@ -455,7 +455,7 @@ def main(): ...@@ -455,7 +455,7 @@ def main():
if __name__ == "__main__": if __name__ == "__main__":
try: try:
main() main()
except SystemExit, e: except SystemExit as e:
if type(e.code) is str: if type(e.code) is str:
if hasattr(logging, 'trace'): # utils.setupLog called if hasattr(logging, 'trace'): # utils.setupLog called
logging.critical(e.code) logging.critical(e.code)
......
#!/usr/bin/python2 #!/usr/bin/python2
import httplib, logging, os, socket, sys import http.client, logging, os, socket, sys
from BaseHTTPServer import BaseHTTPRequestHandler from http.server import BaseHTTPRequestHandler
from SocketServer import ThreadingTCPServer from socketserver import ThreadingTCPServer
from urlparse import parse_qsl from urllib.parse import parse_qsl
if 're6st' not in sys.modules: if 're6st' not in sys.modules:
sys.path[0] = os.path.dirname(os.path.dirname(sys.path[0])) sys.path[0] = os.path.dirname(os.path.dirname(sys.path[0]))
from re6st import registry, utils, version from re6st import registry, utils, version
...@@ -36,7 +36,7 @@ class RequestHandler(BaseHTTPRequestHandler): ...@@ -36,7 +36,7 @@ class RequestHandler(BaseHTTPRequestHandler):
return self.server.handle_request(self, path, query) return self.server.handle_request(self, path, query)
except Exception: except Exception:
logging.info(self.requestline, exc_info=1) logging.info(self.requestline, exc_info=1)
self.send_error(httplib.BAD_REQUEST) self.send_error(http.client.BAD_REQUEST)
def log_error(*args): def log_error(*args):
pass pass
......
...@@ -44,7 +44,7 @@ class Array(object): ...@@ -44,7 +44,7 @@ class Array(object):
r = [] r = []
o = offset + 2 o = offset + 2
decode = self._item.decode decode = self._item.decode
for i in xrange(*uint16.unpack_from(buffer, offset)): for i in range(*uint16.unpack_from(buffer, offset)):
o, x = decode(buffer, o) o, x = decode(buffer, o)
r.append(x) r.append(x)
return o, r return o, r
...@@ -206,7 +206,7 @@ class Babel(object): ...@@ -206,7 +206,7 @@ class Babel(object):
def select(*args): def select(*args):
try: try:
s.connect(self.socket_path) s.connect(self.socket_path)
except socket.error, e: except socket.error as e:
logging.debug("Can't connect to %r (%r)", self.socket_path, e) logging.debug("Can't connect to %r (%r)", self.socket_path, e)
return e return e
s.send("\1") s.send("\1")
...@@ -323,7 +323,7 @@ class iterRoutes(object): ...@@ -323,7 +323,7 @@ class iterRoutes(object):
c.select(*args) c.select(*args)
utils.select(*args) utils.select(*args)
return (prefix return (prefix
for neigh_routes in c.neighbours.itervalues() for neigh_routes in c.neighbours.values()
for prefix in neigh_routes[1] for prefix in neigh_routes[1]
if prefix) if prefix)
......
...@@ -37,7 +37,8 @@ class Socket(object): ...@@ -37,7 +37,8 @@ class Socket(object):
try: try:
self._socket.recv(0) self._socket.recv(0)
return True return True
except socket.error, (err, _): except socket.error as e:
(err, _) = e
if err != errno.EAGAIN: if err != errno.EAGAIN:
raise raise
self._socket.setblocking(1) self._socket.setblocking(1)
...@@ -52,7 +53,7 @@ class Console(object): ...@@ -52,7 +53,7 @@ class Console(object):
socket.SOCK_STREAM | socket.SOCK_CLOEXEC) socket.SOCK_STREAM | socket.SOCK_CLOEXEC)
try: try:
self._removeSocket() self._removeSocket()
except OSError, e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise raise
s.bind(path) s.bind(path)
......
...@@ -132,7 +132,7 @@ def router(ip, ip4, rt6, hello_interval, log_path, state_path, pidfile, ...@@ -132,7 +132,7 @@ def router(ip, ip4, rt6, hello_interval, log_path, state_path, pidfile,
# WKRD: babeld fails to start if pidfile already exists # WKRD: babeld fails to start if pidfile already exists
try: try:
os.remove(pidfile) os.remove(pidfile)
except OSError, e: except OSError as e:
if e.errno != errno.ENOENT: if e.errno != errno.ENOENT:
raise raise
logging.info('%r', cmd) logging.info('%r', cmd)
......
...@@ -18,16 +18,16 @@ Authenticated communication: ...@@ -18,16 +18,16 @@ Authenticated communication:
- the last one that was really used by the client (!hello) - the last one that was really used by the client (!hello)
- the one of the last handshake (hello) - the one of the last handshake (hello)
""" """
import base64, hmac, hashlib, httplib, inspect, json, logging import base64, hmac, hashlib, http.client, inspect, json, logging
import mailbox, os, platform, random, select, smtplib, socket, sqlite3 import mailbox, os, platform, random, select, smtplib, socket, sqlite3
import string, sys, threading, time, weakref, zlib import string, sys, threading, time, weakref, zlib
from collections import defaultdict, deque from collections import defaultdict, deque
from datetime import datetime from datetime import datetime
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler from http.server import HTTPServer, BaseHTTPRequestHandler
from email.mime.text import MIMEText from email.mime.text import MIMEText
from operator import itemgetter from operator import itemgetter
from OpenSSL import crypto from OpenSSL import crypto
from urllib import splittype, splithost, unquote, urlencode from urllib.parse import splittype, splithost, unquote, urlencode
from . import ctl, tunnel, utils, version, x509 from . import ctl, tunnel, utils, version, x509
HMAC_HEADER = "Re6stHMAC" HMAC_HEADER = "Re6stHMAC"
...@@ -41,7 +41,7 @@ def rpc(f): ...@@ -41,7 +41,7 @@ def rpc(f):
defaults = () defaults = ()
i = len(args) - len(defaults) i = len(args) - len(defaults)
f.getcallargs = eval("lambda %s: locals()" % ','.join(args[1:i] f.getcallargs = eval("lambda %s: locals()" % ','.join(args[1:i]
+ map("%s=%r".__mod__, zip(args[i:], defaults)))) + list(map("%s=%r".__mod__, list(zip(args[i:], defaults))))))
return f return f
def rpc_private(f): def rpc_private(f):
...@@ -59,7 +59,7 @@ class RegistryServer(object): ...@@ -59,7 +59,7 @@ class RegistryServer(object):
cert_duration = 365 * 86400 cert_duration = 365 * 86400
def _geoiplookup(self, ip): def _geoiplookup(self, ip):
raise HTTPError(httplib.BAD_REQUEST) raise HTTPError(http.client.BAD_REQUEST)
def __init__(self, config): def __init__(self, config):
self.config = config self.config = config
...@@ -76,7 +76,7 @@ class RegistryServer(object): ...@@ -76,7 +76,7 @@ class RegistryServer(object):
if x and not x.startswith('#'): if x and not x.startswith('#'):
x = x.split() x = x.split()
self.community_map[x.pop(0)] = x self.community_map[x.pop(0)] = x
if sum('*' in x for x in self.community_map.itervalues()) != 1: if sum('*' in x for x in self.community_map.values()) != 1:
sys.exit("Invalid community configuration: missing or multiple default location ('*')") sys.exit("Invalid community configuration: missing or multiple default location ('*')")
else: else:
self.community_map[''] = '*' self.community_map[''] = '*'
...@@ -169,8 +169,8 @@ class RegistryServer(object): ...@@ -169,8 +169,8 @@ class RegistryServer(object):
def updateNetworkConfig(self, _it0=itemgetter(0)): def updateNetworkConfig(self, _it0=itemgetter(0)):
kw = { kw = {
'babel_default': 'max-rtt-penalty 5000 rtt-max 500 rtt-decay 125', 'babel_default': 'max-rtt-penalty 5000 rtt-max 500 rtt-decay 125',
'crl': map(_it0, self.db.execute( 'crl': list(map(_it0, self.db.execute(
"SELECT serial FROM crl ORDER BY serial")), "SELECT serial FROM crl ORDER BY serial"))),
'protocol': version.protocol, 'protocol': version.protocol,
'registry_prefix': self.prefix, 'registry_prefix': self.prefix,
} }
...@@ -286,7 +286,7 @@ class RegistryServer(object): ...@@ -286,7 +286,7 @@ class RegistryServer(object):
x_forwarded_for = request.headers.get('X-Forwarded-For') x_forwarded_for = request.headers.get('X-Forwarded-For')
if request.client_address[0] not in authorized_origin or \ if request.client_address[0] not in authorized_origin or \
x_forwarded_for and x_forwarded_for not in authorized_origin: x_forwarded_for and x_forwarded_for not in authorized_origin:
return request.send_error(httplib.FORBIDDEN) return request.send_error(http.client.FORBIDDEN)
key = m.getcallargs(**kw).get('cn') key = m.getcallargs(**kw).get('cn')
if key: if key:
h = base64.b64decode(request.headers[HMAC_HEADER]) h = base64.b64decode(request.headers[HMAC_HEADER])
...@@ -313,16 +313,16 @@ class RegistryServer(object): ...@@ -313,16 +313,16 @@ class RegistryServer(object):
request.headers.get("host")) request.headers.get("host"))
try: try:
result = m(**kw) result = m(**kw)
except HTTPError, e: except HTTPError as e:
return request.send_error(*e.args) return request.send_error(*e.args)
except: except:
logging.warning(request.requestline, exc_info=1) logging.warning(request.requestline, exc_info=1)
return request.send_error(httplib.INTERNAL_SERVER_ERROR) return request.send_error(http.client.INTERNAL_SERVER_ERROR)
if result: if result:
request.send_response(httplib.OK) request.send_response(http.client.OK)
request.send_header("Content-Length", str(len(result))) request.send_header("Content-Length", str(len(result)))
else: else:
request.send_response(httplib.NO_CONTENT) request.send_response(http.client.NO_CONTENT)
if key: if key:
request.send_header(HMAC_HEADER, base64.b64encode( request.send_header(HMAC_HEADER, base64.b64encode(
hmac.HMAC(key, result, hashlib.sha1).digest())) hmac.HMAC(key, result, hashlib.sha1).digest()))
...@@ -367,7 +367,7 @@ class RegistryServer(object): ...@@ -367,7 +367,7 @@ class RegistryServer(object):
def addToken(self, email, token): def addToken(self, email, token):
prefix_len = self.config.prefix_length prefix_len = self.config.prefix_length
if not prefix_len: if not prefix_len:
raise HTTPError(httplib.FORBIDDEN) raise HTTPError(http.client.FORBIDDEN)
request = token is None request = token is None
with self.lock: with self.lock:
while True: while True:
...@@ -381,7 +381,7 @@ class RegistryServer(object): ...@@ -381,7 +381,7 @@ class RegistryServer(object):
break break
except sqlite3.IntegrityError: except sqlite3.IntegrityError:
if not request: if not request:
raise HTTPError(httplib.CONFLICT) raise HTTPError(http.client.CONFLICT)
self.timeout = 1 self.timeout = 1
if request: if request:
return token return token
...@@ -389,7 +389,7 @@ class RegistryServer(object): ...@@ -389,7 +389,7 @@ class RegistryServer(object):
@rpc @rpc
def requestToken(self, email): def requestToken(self, email):
if not self.config.mailhost: if not self.config.mailhost:
raise HTTPError(httplib.FORBIDDEN) raise HTTPError(http.client.FORBIDDEN)
token = self.addToken(email, None) token = self.addToken(email, None)
...@@ -418,11 +418,11 @@ class RegistryServer(object): ...@@ -418,11 +418,11 @@ class RegistryServer(object):
s.quit() s.quit()
def getCommunity(self, country, continent): def getCommunity(self, country, continent):
for prefix, location_list in self.community_map.iteritems(): for prefix, location_list in self.community_map.items():
if country in location_list: if country in location_list:
return prefix return prefix
default = '' default = ''
for prefix, location_list in self.community_map.iteritems(): for prefix, location_list in self.community_map.items():
if continent in location_list: if continent in location_list:
return prefix return prefix
if '*' in location_list: if '*' in location_list:
...@@ -434,9 +434,9 @@ class RegistryServer(object): ...@@ -434,9 +434,9 @@ class RegistryServer(object):
prev_prefix = None prev_prefix = None
max_len = 128, max_len = 128,
while True: while True:
max_len = q("SELECT max(length(prefix)) FROM cert" max_len = next(q("SELECT max(length(prefix)) FROM cert"
" WHERE cert is null AND length(prefix) < ?", " WHERE cert is null AND length(prefix) < ?",
max_len).next() max_len))
if not max_len[0]: if not max_len[0]:
break break
for prefix, in q("SELECT prefix FROM cert" for prefix, in q("SELECT prefix FROM cert"
...@@ -460,25 +460,25 @@ class RegistryServer(object): ...@@ -460,25 +460,25 @@ class RegistryServer(object):
while True: while True:
try: try:
# Find longest free prefix whithin community. # Find longest free prefix whithin community.
prefix, = q( prefix, = next(q(
"SELECT prefix FROM cert" "SELECT prefix FROM cert"
" WHERE prefix LIKE ?" " WHERE prefix LIKE ?"
" AND length(prefix) <= ? AND cert is null" " AND length(prefix) <= ? AND cert is null"
" ORDER BY length(prefix) DESC", " ORDER BY length(prefix) DESC",
(community + '%', prefix_len)).next() (community + '%', prefix_len)))
except StopIteration: except StopIteration:
# Community not yet allocated? # Community not yet allocated?
# There should be exactly 1 row whose # There should be exactly 1 row whose
# prefix is the beginning of community. # prefix is the beginning of community.
prefix, x = q("SELECT prefix, cert FROM cert" prefix, x = next(q("SELECT prefix, cert FROM cert"
" WHERE substr(?,1,length(prefix)) = prefix", " WHERE substr(?,1,length(prefix)) = prefix",
(community,)).next() (community,)))
if x is not None: if x is not None:
logging.error('No more free /%u prefix available', logging.error('No more free /%u prefix available',
prefix_len) prefix_len)
raise raise
# Split the tree until prefix has wanted length. # Split the tree until prefix has wanted length.
for x in xrange(len(prefix), prefix_len): for x in range(len(prefix), prefix_len):
# Prefix starts with community, then we complete with 0. # Prefix starts with community, then we complete with 0.
x = community[x] if x < community_len else '0' x = community[x] if x < community_len else '0'
q("UPDATE cert SET prefix = ? WHERE prefix = ?", q("UPDATE cert SET prefix = ? WHERE prefix = ?",
...@@ -496,11 +496,11 @@ class RegistryServer(object): ...@@ -496,11 +496,11 @@ class RegistryServer(object):
with self.db: with self.db:
if token: if token:
if not self.config.prefix_length: if not self.config.prefix_length:
raise HTTPError(httplib.FORBIDDEN) raise HTTPError(http.client.FORBIDDEN)
try: try:
token, email, prefix_len, _ = self.db.execute( token, email, prefix_len, _ = next(self.db.execute(
"SELECT * FROM token WHERE token = ?", "SELECT * FROM token WHERE token = ?",
(token,)).next() (token,)))
except StopIteration: except StopIteration:
return return
self.db.execute("DELETE FROM token WHERE token = ?", self.db.execute("DELETE FROM token WHERE token = ?",
...@@ -508,7 +508,7 @@ class RegistryServer(object): ...@@ -508,7 +508,7 @@ class RegistryServer(object):
else: else:
prefix_len = self.config.anonymous_prefix_length prefix_len = self.config.anonymous_prefix_length
if not prefix_len: if not prefix_len:
raise HTTPError(httplib.FORBIDDEN) raise HTTPError(http.client.FORBIDDEN)
email = None email = None
country, continent = '*', '*' country, continent = '*', '*'
if self.geoip_db: if self.geoip_db:
...@@ -624,7 +624,7 @@ class RegistryServer(object): ...@@ -624,7 +624,7 @@ class RegistryServer(object):
if age < time.time() or not peers: if age < time.time() or not peers:
self.request_dump() self.request_dump()
peers = [prefix peers = [prefix
for neigh_routes in self.ctl.neighbours.itervalues() for neigh_routes in self.ctl.neighbours.values()
for prefix in neigh_routes[1] for prefix in neigh_routes[1]
if prefix] if prefix]
peers.append(self.prefix) peers.append(self.prefix)
...@@ -706,8 +706,8 @@ class RegistryServer(object): ...@@ -706,8 +706,8 @@ class RegistryServer(object):
def getNodePrefix(self, email): def getNodePrefix(self, email):
with self.lock, self.db: with self.lock, self.db:
try: try:
cert, = self.db.execute("SELECT cert FROM cert WHERE email = ?", cert, = next(self.db.execute("SELECT cert FROM cert WHERE email = ?",
(email,)).next() (email,)))
except StopIteration: except StopIteration:
return return
certificate = crypto.load_certificate(crypto.FILETYPE_PEM, cert) certificate = crypto.load_certificate(crypto.FILETYPE_PEM, cert)
...@@ -728,7 +728,7 @@ class RegistryServer(object): ...@@ -728,7 +728,7 @@ class RegistryServer(object):
peer = utils.binFromSubnet(peer) peer = utils.binFromSubnet(peer)
with self.peers_lock: with self.peers_lock:
self.request_dump() self.request_dump()
for neigh_routes in self.ctl.neighbours.itervalues(): for neigh_routes in self.ctl.neighbours.values():
for prefix in neigh_routes[1]: for prefix in neigh_routes[1]:
if prefix == peer: if prefix == peer:
break break
...@@ -745,7 +745,7 @@ class RegistryServer(object): ...@@ -745,7 +745,7 @@ class RegistryServer(object):
with self.peers_lock: with self.peers_lock:
self.request_dump() self.request_dump()
peers = {prefix peers = {prefix
for neigh_routes in self.ctl.neighbours.itervalues() for neigh_routes in self.ctl.neighbours.values()
for prefix in neigh_routes[1] for prefix in neigh_routes[1]
if prefix} if prefix}
peers.add(self.prefix) peers.add(self.prefix)
...@@ -794,7 +794,7 @@ class RegistryServer(object): ...@@ -794,7 +794,7 @@ class RegistryServer(object):
self.sendto(utils.binFromSubnet(peers.popleft()), 5) self.sendto(utils.binFromSubnet(peers.popleft()), 5)
elif not r: elif not r:
break break
return json.dumps({k: list(v) for k, v in graph.iteritems()}) return json.dumps({k: list(v) for k, v in graph.items()})
class RegistryClient(object): class RegistryClient(object):
...@@ -807,8 +807,8 @@ class RegistryClient(object): ...@@ -807,8 +807,8 @@ class RegistryClient(object):
self.auto_close = auto_close self.auto_close = auto_close
scheme, host = splittype(url) scheme, host = splittype(url)
host, path = splithost(host) host, path = splithost(host)
self._conn = dict(http=httplib.HTTPConnection, self._conn = dict(http=http.client.HTTPConnection,
https=httplib.HTTPSConnection, https=http.client.HTTPSConnection,
)[scheme](unquote(host), timeout=60) )[scheme](unquote(host), timeout=60)
self._path = path.rstrip('/') self._path = path.rstrip('/')
...@@ -818,7 +818,7 @@ class RegistryClient(object): ...@@ -818,7 +818,7 @@ class RegistryClient(object):
kw = getcallargs(*args, **kw) kw = getcallargs(*args, **kw)
query = '/' + name query = '/' + name
if kw: if kw:
if any(type(v) is not str for v in kw.itervalues()): if any(type(v) is not str for v in kw.values()):
raise TypeError raise TypeError
query += '?' + urlencode(kw) query += '?' + urlencode(kw)
url = self._path + query url = self._path + query
...@@ -846,14 +846,14 @@ class RegistryClient(object): ...@@ -846,14 +846,14 @@ class RegistryClient(object):
self._conn.endheaders() self._conn.endheaders()
response = self._conn.getresponse() response = self._conn.getresponse()
body = response.read() body = response.read()
if response.status in (httplib.OK, httplib.NO_CONTENT): if response.status in (http.client.OK, http.client.NO_CONTENT):
if (not client_prefix or if (not client_prefix or
hmac.HMAC(key, body, hashlib.sha1).digest() == hmac.HMAC(key, body, hashlib.sha1).digest() ==
base64.b64decode(response.msg[HMAC_HEADER])): base64.b64decode(response.msg[HMAC_HEADER])):
if self.auto_close and name != 'hello': if self.auto_close and name != 'hello':
self._conn.close() self._conn.close()
return body return body
elif response.status == httplib.FORBIDDEN: elif response.status == http.client.FORBIDDEN:
# XXX: We should improve error handling, while making # XXX: We should improve error handling, while making
# sure re6st nodes don't crash on temporary errors. # sure re6st nodes don't crash on temporary errors.
# This is currently good enough for re6st-conf, to # This is currently good enough for re6st-conf, to
......
...@@ -6,7 +6,7 @@ import os ...@@ -6,7 +6,7 @@ import os
import sys import sys
import unittest import unittest
from shutil import rmtree from shutil import rmtree
from StringIO import StringIO from io import StringIO
from mock import patch from mock import patch
from OpenSSL import crypto from OpenSSL import crypto
......
...@@ -3,7 +3,7 @@ import os ...@@ -3,7 +3,7 @@ import os
import random import random
import string import string
import json import json
import httplib import http.client
import base64 import base64
import unittest import unittest
import hmac import hmac
...@@ -162,7 +162,7 @@ class TestRegistryServer(unittest.TestCase): ...@@ -162,7 +162,7 @@ class TestRegistryServer(unittest.TestCase):
[(hashlib.sha1(key).digest(), protocol)]) [(hashlib.sha1(key).digest(), protocol)])
func.assert_called_once_with(**params) func.assert_called_once_with(**params)
# http response check # http response check
request.send_response.assert_called_once_with(httplib.OK) request.send_response.assert_called_once_with(http.client.OK)
request.send_header.assert_any_call("Content-Length", str(len(result))) request.send_header.assert_any_call("Content-Length", str(len(result)))
request.send_header.assert_any_call( request.send_header.assert_any_call(
registry.HMAC_HEADER, registry.HMAC_HEADER,
...@@ -189,8 +189,8 @@ class TestRegistryServer(unittest.TestCase): ...@@ -189,8 +189,8 @@ class TestRegistryServer(unittest.TestCase):
self.server.handle_request(request_bad, method, params) self.server.handle_request(request_bad, method, params)
func.assert_called_once_with(**params) func.assert_called_once_with(**params)
request_bad.send_error.assert_called_once_with(httplib.FORBIDDEN) request_bad.send_error.assert_called_once_with(http.client.FORBIDDEN)
request_good.send_response.assert_called_once_with(httplib.NO_CONTENT) request_good.send_response.assert_called_once_with(http.client.NO_CONTENT)
# will cause valueError, if a node send hello twice to a registry # will cause valueError, if a node send hello twice to a registry
def test_getPeerProtocol(self): def test_getPeerProtocol(self):
......
...@@ -2,7 +2,7 @@ import sys ...@@ -2,7 +2,7 @@ import sys
import os import os
import unittest import unittest
import hmac import hmac
import httplib import http.client
import base64 import base64
import hashlib import hashlib
from mock import Mock, patch from mock import Mock, patch
...@@ -26,15 +26,15 @@ class TestRegistryClient(unittest.TestCase): ...@@ -26,15 +26,15 @@ class TestRegistryClient(unittest.TestCase):
self.assertEqual(client1._path, "/example") self.assertEqual(client1._path, "/example")
self.assertEqual(client1._conn.host, "localhost") self.assertEqual(client1._conn.host, "localhost")
self.assertIsInstance(client1._conn, httplib.HTTPSConnection) self.assertIsInstance(client1._conn, http.client.HTTPSConnection)
self.assertIsInstance(client2._conn, httplib.HTTPConnection) self.assertIsInstance(client2._conn, http.client.HTTPConnection)
def test_rpc_hello(self): def test_rpc_hello(self):
prefix = "0000000011111111" prefix = "0000000011111111"
protocol = "7" protocol = "7"
body = "a_hmac_key" body = "a_hmac_key"
query = "/hello?client_prefix=0000000011111111&protocol=7" query = "/hello?client_prefix=0000000011111111&protocol=7"
response = fakeResponse(body, httplib.OK) response = fakeResponse(body, http.client.OK)
self.client._conn.getresponse.return_value = response self.client._conn.getresponse.return_value = response
res = self.client.hello(prefix, protocol) res = self.client.hello(prefix, protocol)
...@@ -58,7 +58,7 @@ class TestRegistryClient(unittest.TestCase): ...@@ -58,7 +58,7 @@ class TestRegistryClient(unittest.TestCase):
key = hashlib.sha1(key).digest() key = hashlib.sha1(key).digest()
# response part # response part
body = None body = None
response = fakeResponse(body, httplib.NO_CONTENT) response = fakeResponse(body, http.client.NO_CONTENT)
response.msg = dict(Re6stHMAC=hmac.HMAC(key, body, hashlib.sha1).digest()) response.msg = dict(Re6stHMAC=hmac.HMAC(key, body, hashlib.sha1).digest())
self.client._conn.getresponse.return_value = response self.client._conn.getresponse.return_value = response
......
...@@ -242,14 +242,14 @@ class BaseTunnelManager(object): ...@@ -242,14 +242,14 @@ class BaseTunnelManager(object):
self._country = {} self._country = {}
address_dict = {family: self._updateCountry(address) address_dict = {family: self._updateCountry(address)
for family, address in address_dict.iteritems()} for family, address in address_dict.items()}
elif cache.same_country: elif cache.same_country:
sys.exit("Can not respect 'same_country' network configuration" sys.exit("Can not respect 'same_country' network configuration"
" (GEOIP2_MMDB not set)") " (GEOIP2_MMDB not set)")
self._address = {family: utils.dump_address(address) self._address = {family: utils.dump_address(address)
for family, address in address_dict.iteritems() for family, address in address_dict.items()
if address} if address}
cache.my_address = ';'.join(self._address.itervalues()) cache.my_address = ';'.join(iter(self._address.values()))
self.sock = socket.socket(socket.AF_INET6, self.sock = socket.socket(socket.AF_INET6,
socket.SOCK_DGRAM | socket.SOCK_CLOEXEC) socket.SOCK_DGRAM | socket.SOCK_CLOEXEC)
...@@ -346,7 +346,7 @@ class BaseTunnelManager(object): ...@@ -346,7 +346,7 @@ class BaseTunnelManager(object):
def _sendto(self, to, msg, peer=None): def _sendto(self, to, msg, peer=None):
try: try:
r = self.sock.sendto(peer.encode(msg) if peer else msg, to) r = self.sock.sendto(peer.encode(msg) if peer else msg, to)
except socket.error, e: except socket.error as e:
(logging.info if e.errno == errno.ENETUNREACH else logging.error)( (logging.info if e.errno == errno.ENETUNREACH else logging.error)(
'Failed to send message to %s (%s)', to, e) 'Failed to send message to %s (%s)', to, e)
return return
...@@ -410,7 +410,7 @@ class BaseTunnelManager(object): ...@@ -410,7 +410,7 @@ class BaseTunnelManager(object):
serial = cert.get_serial_number() serial = cert.get_serial_number()
if serial in self.cache.crl: if serial in self.cache.crl:
raise ValueError("revoked") raise ValueError("revoked")
except (x509.VerifyError, ValueError), e: except (x509.VerifyError, ValueError) as e:
if retry: if retry:
return True return True
logging.debug('ignored invalid certificate from %r (%s)', logging.debug('ignored invalid certificate from %r (%s)',
...@@ -467,8 +467,8 @@ class BaseTunnelManager(object): ...@@ -467,8 +467,8 @@ class BaseTunnelManager(object):
# Don't send country to old nodes # Don't send country to old nodes
if self._getPeer(peer).protocol < 7: if self._getPeer(peer).protocol < 7:
return ';'.join(','.join(a.split(',')[:3]) for a in return ';'.join(','.join(a.split(',')[:3]) for a in
';'.join(self._address.itervalues()).split(';')) ';'.join(iter(self._address.values())).split(';'))
return ';'.join(self._address.itervalues()) return ';'.join(iter(self._address.values()))
elif not code: # network version elif not code: # network version
if peer: if peer:
try: try:
...@@ -553,8 +553,8 @@ class BaseTunnelManager(object): ...@@ -553,8 +553,8 @@ class BaseTunnelManager(object):
if (not self.NEED_RESTART.isdisjoint(changed) if (not self.NEED_RESTART.isdisjoint(changed)
or version.protocol < self.cache.min_protocol or version.protocol < self.cache.min_protocol
# TODO: With --management, we could kill clients without restarting. # TODO: With --management, we could kill clients without restarting.
or not all(crl.isdisjoint(serials.itervalues()) or not all(crl.isdisjoint(iter(serials.values()))
for serials in self._served.itervalues())): for serials in self._served.values())):
# Wait at least 1 second to broadcast new version to neighbours. # Wait at least 1 second to broadcast new version to neighbours.
self.selectTimeout(time.time() + 1 + self.cache.delay_restart, self.selectTimeout(time.time() + 1 + self.cache.delay_restart,
self._restart) self._restart)
...@@ -606,7 +606,7 @@ class BaseTunnelManager(object): ...@@ -606,7 +606,7 @@ class BaseTunnelManager(object):
with open('/proc/net/ipv6_route', "r", 4096) as f: with open('/proc/net/ipv6_route', "r", 4096) as f:
try: try:
routing_table = f.read() routing_table = f.read()
except IOError, e: except IOError as e:
# ???: If someone can explain why the kernel sometimes fails # ???: If someone can explain why the kernel sometimes fails
# even when there's a lot of free memory. # even when there's a lot of free memory.
if e.errno != errno.ENOMEM: if e.errno != errno.ENOMEM:
...@@ -683,7 +683,7 @@ class TunnelManager(BaseTunnelManager): ...@@ -683,7 +683,7 @@ class TunnelManager(BaseTunnelManager):
self._client_count = client_count self._client_count = client_count
self.new_iface_list = deque('re6stnet' + str(i) self.new_iface_list = deque('re6stnet' + str(i)
for i in xrange(1, self._client_count + 1)) for i in range(1, self._client_count + 1))
self._free_iface_list = [] self._free_iface_list = []
def close(self): def close(self):
...@@ -752,7 +752,7 @@ class TunnelManager(BaseTunnelManager): ...@@ -752,7 +752,7 @@ class TunnelManager(BaseTunnelManager):
def babel_dump(self): def babel_dump(self):
t = time.time() t = time.time()
if self._killing: if self._killing:
for prefix, tunnel_killer in self._killing.items(): for prefix, tunnel_killer in list(self._killing.items()):
if tunnel_killer.timeout < t: if tunnel_killer.timeout < t:
if tunnel_killer.state != 'unlocking': if tunnel_killer.state != 'unlocking':
logging.info( logging.info(
...@@ -780,7 +780,7 @@ class TunnelManager(BaseTunnelManager): ...@@ -780,7 +780,7 @@ class TunnelManager(BaseTunnelManager):
def _cleanDeads(self): def _cleanDeads(self):
disconnected = False disconnected = False
for prefix in self._connection_dict.keys(): for prefix in list(self._connection_dict.keys()):
status = self._connection_dict[prefix].refresh() status = self._connection_dict[prefix].refresh()
if status: if status:
disconnected |= status > 0 disconnected |= status > 0
...@@ -902,7 +902,7 @@ class TunnelManager(BaseTunnelManager): ...@@ -902,7 +902,7 @@ class TunnelManager(BaseTunnelManager):
neighbours = self.ctl.neighbours neighbours = self.ctl.neighbours
# Collect all nodes known by Babel # Collect all nodes known by Babel
peers = {prefix peers = {prefix
for neigh_routes in neighbours.itervalues() for neigh_routes in neighbours.values()
for prefix in neigh_routes[1] for prefix in neigh_routes[1]
if prefix} if prefix}
# Keep only distant peers. # Keep only distant peers.
...@@ -987,7 +987,7 @@ class TunnelManager(BaseTunnelManager): ...@@ -987,7 +987,7 @@ class TunnelManager(BaseTunnelManager):
break break
def killAll(self): def killAll(self):
for prefix in self._connection_dict.keys(): for prefix in list(self._connection_dict.keys()):
self._kill(prefix) self._kill(prefix)
def handleClientEvent(self): def handleClientEvent(self):
...@@ -999,7 +999,7 @@ class TunnelManager(BaseTunnelManager): ...@@ -999,7 +999,7 @@ class TunnelManager(BaseTunnelManager):
if c and c.time < float(time): if c and c.time < float(time):
try: try:
c.connected(serial) c.connected(serial)
except (KeyError, TypeError), e: except (KeyError, TypeError) as e:
logging.error("%s (route_up %s)", e, common_name) logging.error("%s (route_up %s)", e, common_name)
else: else:
logging.info("ignore route_up notification for %s %r", logging.info("ignore route_up notification for %s %r",
...@@ -1010,10 +1010,10 @@ class TunnelManager(BaseTunnelManager): ...@@ -1010,10 +1010,10 @@ class TunnelManager(BaseTunnelManager):
if self.cache.same_country: if self.cache.same_country:
address = self._updateCountry(address) address = self._updateCountry(address)
self._address[family] = utils.dump_address(address) self._address[family] = utils.dump_address(address)
self.cache.my_address = ';'.join(self._address.itervalues()) self.cache.my_address = ';'.join(iter(self._address.values()))
def broadcastNewVersion(self): def broadcastNewVersion(self):
self._babel_dump_new_version() self._babel_dump_new_version()
for prefix, c in self._connection_dict.items(): for prefix, c in list(self._connection_dict.items()):
if c.serial in self.cache.crl: if c.serial in self.cache.crl:
self._kill(prefix) self._kill(prefix)
...@@ -40,7 +40,7 @@ class Forwarder(object): ...@@ -40,7 +40,7 @@ class Forwarder(object):
def wrapper(*args, **kw): def wrapper(*args, **kw):
try: try:
return wrapped(*args, **kw) return wrapped(*args, **kw)
except Exception, e: except Exception as e:
raise UPnPException(str(e)) raise UPnPException(str(e))
return wraps(wrapped)(wrapper) return wraps(wrapped)(wrapper)
...@@ -68,14 +68,14 @@ class Forwarder(object): ...@@ -68,14 +68,14 @@ class Forwarder(object):
else: else:
try: try:
return self._refresh() return self._refresh()
except UPnPException, e: except UPnPException as e:
logging.debug("UPnP failure", exc_info=1) logging.debug("UPnP failure", exc_info=1)
self.clear() self.clear()
try: try:
self.discover() self.discover()
self.selectigd() self.selectigd()
return self._refresh() return self._refresh()
except UPnPException, e: except UPnPException as e:
self.next_refresh = self._next_retry = time.time() + 60 self.next_refresh = self._next_retry = time.time() + 60
logging.info(str(e)) logging.info(str(e))
self.clear() self.clear()
...@@ -109,7 +109,7 @@ class Forwarder(object): ...@@ -109,7 +109,7 @@ class Forwarder(object):
try: try:
self.addportmapping(port, *args) self.addportmapping(port, *args)
break break
except UPnPException, e: except UPnPException as e:
if str(e) != 'ConflictInMappingEntry': if str(e) != 'ConflictInMappingEntry':
raise raise
port = None port = None
......
...@@ -164,7 +164,7 @@ class Popen(subprocess.Popen): ...@@ -164,7 +164,7 @@ class Popen(subprocess.Popen):
self._args = tuple(args[0] if args else kw['args']) self._args = tuple(args[0] if args else kw['args'])
try: try:
super(Popen, self).__init__(*args, **kw) super(Popen, self).__init__(*args, **kw)
except OSError, e: except OSError as e:
if e.errno != errno.ENOMEM: if e.errno != errno.ENOMEM:
raise raise
self.returncode = -1 self.returncode = -1
...@@ -209,7 +209,7 @@ def select(R, W, T): ...@@ -209,7 +209,7 @@ def select(R, W, T):
def makedirs(*args): def makedirs(*args):
try: try:
os.makedirs(*args) os.makedirs(*args)
except OSError, e: except OSError as e:
if e.errno != errno.EEXIST: if e.errno != errno.EEXIST:
raise raise
...@@ -240,7 +240,7 @@ def parse_address(address_list): ...@@ -240,7 +240,7 @@ def parse_address(address_list):
a = address.split(',') a = address.split(',')
int(a[1]) # Check if port is an int int(a[1]) # Check if port is an int
yield tuple(a[:4]) yield tuple(a[:4])
except ValueError, e: except ValueError as e:
logging.warning("Failed to parse node address %r (%s)", logging.warning("Failed to parse node address %r (%s)",
address, e) address, e)
...@@ -262,7 +262,7 @@ newHmacSecret = newHmacSecret() ...@@ -262,7 +262,7 @@ newHmacSecret = newHmacSecret()
# - the 3 first bits code the number of bytes # - the 3 first bits code the number of bytes
def packInteger(i): def packInteger(i):
for n in xrange(8): for n in range(8):
x = 32 << 8 * n x = 32 << 8 * n
if i < x: if i < x:
return struct.pack("!Q", i + n * x)[7-n:] return struct.pack("!Q", i + n * x)[7-n:]
...@@ -275,7 +275,7 @@ def unpackInteger(x): ...@@ -275,7 +275,7 @@ def unpackInteger(x):
i, = struct.unpack("!Q", '\0' * (7 - n) + x[:n+1]) i, = struct.unpack("!Q", '\0' * (7 - n) + x[:n+1])
except struct.error: except struct.error:
return return
return sum((32 << 8 * i for i in xrange(n)), return sum((32 << 8 * i for i in range(n)),
i - (n * 32 << 8 * n)), n + 1 i - (n * 32 << 8 * n)), n + 1
### ###
......
...@@ -14,7 +14,7 @@ def copy_file(self, infile, outfile, *args, **kw): ...@@ -14,7 +14,7 @@ def copy_file(self, infile, outfile, *args, **kw):
if not self.dry_run: if not self.dry_run:
log.info("generating %s -> %s", infile, outfile) log.info("generating %s -> %s", infile, outfile)
with open(outfile, "wb") as f: with open(outfile, "wb") as f:
for x in sorted(version.iteritems()): for x in sorted(version.items()):
if not x[0].startswith("_"): if not x[0].startswith("_"):
f.write("%s = %r\n" % x) f.write("%s = %r\n" % x)
return outfile, 1 return outfile, 1
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment