a first very rough step towards python3
[nepi.git] / src / nepi / resources / linux / scripts / tunchannel.py
index 170bf9d..11c68af 100644 (file)
@@ -18,7 +18,7 @@
 #         Claudio Freire <claudio-daniel.freire@inria.fr>
 #
 
-from __future__ import print_function
+
 
 import select
 import sys
@@ -37,7 +37,7 @@ import ctypes
 import time
 
 def ipfmt(ip):
-    ipbytes = map(ord,ip.decode("hex"))
+    ipbytes = list(map(ord,ip.decode("hex")))
     return '.'.join(map(str,ipbytes))
 
 tagtype = {
@@ -182,7 +182,7 @@ def piWrap(buf, ether_mode, etherProto=etherProto):
         buf,
     ))
 
-_padmap = [ chr(padding) * padding for padding in xrange(127) ]
+_padmap = [ chr(padding) * padding for padding in range(127) ]
 del padding
 
 def encrypt(packet, crypter, len=len, padmap=_padmap):
@@ -202,7 +202,7 @@ def decrypt(packet, crypter, ord=ord):
         padding = ord(packet[-1])
         if not (0 < padding <= crypter.block_size):
             # wrong padding
-            raise RuntimeError, "Truncated packet %s"
+            raise RuntimeError("Truncated packet %s")
         packet = packet[:-padding]
     
     return packet
@@ -404,7 +404,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
         
         try:
             rdrdy, wrdy, errs = select(rset,wset,eset,1)
-        except selecterror, e:
+        except selecterror as e:
             if e.args[0] == errno.EINTR:
                 # just retry
                 continue
@@ -442,7 +442,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
             sent = 0
             try:
                 try:
-                    for x in xrange(maxbatch):
+                    for x in range(maxbatch):
                         packet = pullPacket(fwbuf)
 
                         if crypto_mode:
@@ -460,7 +460,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                         
                         if not rnonblock or not fpacketReady(fwbuf):
                             break
-                except OSError,e:
+                except OSError as e:
                     # This except handles the entire While block on PURPOSE
                     # as an optimization (setting a try/except block is expensive)
                     # The only operation that can raise this exception is rwrite
@@ -485,7 +485,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                 bwfree -= sent
         if tun in wrdy:
             try:
-                for x in xrange(maxtbatch):
+                for x in range(maxtbatch):
                     packet = pullPacket(bkbuf)
                     twrite(tunfd, packet)
                     #wt += 1
@@ -500,7 +500,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                     if slowlocal:
                         # Give some time for the kernel to process the packets
                         time.sleep(0)
-            except OSError,e:
+            except OSError as e:
                 # This except handles the entire While block on PURPOSE
                 # as an optimization (setting a try/except block is expensive)
                 # The only operation that can raise this exception is os_write
@@ -513,7 +513,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
         # check incoming data packets
         if tun in rdrdy:
             try:
-                for x in xrange(maxbatch):
+                for x in range(maxbatch):
                     packet = tread(tunfd,2000) # tun.read blocks until it gets 2k!
                     if not packet:
                         continue
@@ -522,7 +522,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                     
                     if not tnonblock or len(fwbuf) >= maxfwbuf:
                         break
-            except OSError,e:
+            except OSError as e:
                 # This except handles the entire While block on PURPOSE
                 # as an optimization (setting a try/except block is expensive)
                 # The only operation that can raise this exception is os_read
@@ -531,7 +531,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
         if remote in rdrdy:
             try:
                 try:
-                    for x in xrange(maxbatch):
+                    for x in range(maxbatch):
                         packet = rread(remote,2000)
                         
                         #rr += 1
@@ -543,7 +543,7 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                         elif not packet:
                             if not udp and packet == "":
                                 # Connection broken, try to reconnect (or just die)
-                                raise RuntimeError, "Connection broken"
+                                raise RuntimeError("Connection broken")
                             else:
                                 continue
 
@@ -551,13 +551,13 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
                         
                         if not rnonblock or len(bkbuf) >= maxbkbuf:
                             break
-                except OSError,e:
+                except OSError as e:
                     # This except handles the entire While block on PURPOSE
                     # as an optimization (setting a try/except block is expensive)
                     # The only operation that can raise this exception is rread
                     if e.errno not in retrycodes:
                         raise
-            except Exception, e:
+            except Exception as e:
                 if reconnect is not None:
                     # in UDP mode, sometimes connected sockets can return a connection refused
                     # on read. Give the caller a chance to reconnect
@@ -583,11 +583,11 @@ def tun_fwd(tun, remote, with_pi, ether_mode, cipher_key, udp, TERMINATE, SUSPEN
 def udp_connect(TERMINATE, local_addr, local_port, peer_addr, peer_port):
     rsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
     retrydelay = 1.0
-    for i in xrange(30):
+    for i in range(30):
         # TERMINATE is a array. An item can be added to TERMINATE, from
         # outside this function to force termination of the loop
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         try:
             rsock.bind((local_addr, local_port))
             break
@@ -618,9 +618,9 @@ def udp_handshake(TERMINATE, rsock):
             pass
     keepalive_thread = threading.Thread(target=keepalive)
     keepalive_thread.start()
-    for i in xrange(900):
+    for i in range(900):
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         try:
             heartbeat = rsock.recv(10)
             break
@@ -644,11 +644,11 @@ def tcp_connect(TERMINATE, stop, rsock, peer_addr, peer_port):
     # will be forever blocked in the connect, so we put a reasonable timeout.
     rsock.settimeout(10) 
     # We wait for 
-    for i in xrange(30):
+    for i in range(30):
         if stop:
             break
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         try:
             rsock.connect((peer_addr, peer_port))
             sock = rsock
@@ -674,11 +674,11 @@ def tcp_listen(TERMINATE, stop, lsock, local_addr, local_port):
     retrydelay = 1.0
     # We try to bind to the local virtual interface. 
     # It might not exist yet so we wait in a loop.
-    for i in xrange(30):
+    for i in range(30):
         if stop:
             break
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         try:
             lsock.bind((local_addr, local_port))
             break
@@ -695,9 +695,9 @@ def tcp_listen(TERMINATE, stop, lsock, local_addr, local_port):
     # The other side might not be ready yet, so we also wait in a loop for timeouts.
     timeout = 1
     lsock.listen(1)
-    for i in xrange(30):
+    for i in range(30):
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         rlist, wlist, xlist = select.select([lsock], [], [], timeout)
         if stop:
             break
@@ -751,11 +751,11 @@ def tcp_establish(TERMINATE, local_addr, local_port, peer_addr, peer_port):
   
     end = False
     sock = None
-    for i in xrange(0, 50):
+    for i in range(0, 50):
         if end:
             break
         if TERMINATE:
-            raise OSError, "Killed"
+            raise OSError("Killed")
         hand = struct.pack("!L", random.randint(0, 2**30))
         stop = []
         lresult = []
@@ -786,5 +786,5 @@ def tcp_establish(TERMINATE, local_addr, local_port, peer_addr, peer_port):
                 end = True
 
     if not sock:
-        raise OSError, "Error: tcp_establish could not establish connection."
+        raise OSError("Error: tcp_establish could not establish connection.")
     return sock