10 from optparse import OptionParser
15 TUNSETIFF = 0x400454ca
17 # Trak SIGTERM, and set global termination flag instead of dying
19 def _finalize(sig,frame):
21 TERMINATE.append(None)
22 signal.signal(signal.SIGTERM, _finalize)
24 # SIGUSR1 suspends forwading, SIGUSR2 resumes forwarding
26 def _suspend(sig,frame):
30 signal.signal(signal.SIGUSR1, _suspend)
32 def _resume(sig,frame):
36 signal.signal(signal.SIGUSR2, _resume)
38 def open_tap(vif_name, vif_type, pi):
45 fd = os.open("/dev/net/tun", os.O_RDWR)
47 err = fcntl.ioctl(fd, TUNSETIFF, struct.pack("16sH", vif_name, flags))
50 raise RuntimeError("Could not configure device %s" % vif_name)
55 usage = ("usage: %prog -N <vif_name> -t <vif-type> -p <pi> "
56 "-b <bwlimit> -c <cipher> -k <cipher-key> -q <txqueuelen> "
57 "-l <local-port-file> -r <remote-port-file> -H <remote-host> "
60 parser = OptionParser(usage = usage)
62 parser.add_option("-N", "--vif-name", dest="vif_name",
63 help = "The name of the virtual interface",
66 parser.add_option("-t", "--vif-type", dest="vif_type",
67 help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
68 "Defaults to IFF_TAP. ",
72 parser.add_option("-n", "--pi", dest="pi",
75 help="Enable PI header")
77 parser.add_option("-b", "--bwlimit", dest="bwlimit",
78 help = "Specifies the interface's emulated bandwidth in bytes ",
79 default = None, type="int")
81 parser.add_option("-q", "--txqueuelen", dest="txqueuelen",
82 help = "Specifies the interface's transmission queue length. ",
83 default = 1000, type="int")
85 parser.add_option("-c", "--cipher", dest="cipher",
86 help = "Cipher to encript communication. "
87 "One of PLAIN, AES, Blowfish, DES, DES3. ",
88 default = None, type="str")
90 parser.add_option("-k", "--cipher-key", dest="cipher_key",
91 help = "Specify a symmetric encryption key with which to protect "
92 "packets across the tunnel. python-crypto must be installed "
94 default = None, type="str")
96 parser.add_option("-l", "--local-port-file", dest="local_port_file",
97 help = "File where to store the local binded UDP port number ",
98 default = "local_port_file", type="str")
100 parser.add_option("-r", "--remote-port-file", dest="remote_port_file",
101 help = "File where to read the remote UDP port number to connect to",
102 default = "remote_port_file", type="str")
104 parser.add_option("-H", "--remote-host", dest="remote_host",
105 help = "Remote host IP",
106 default = "remote_host", type="str")
108 parser.add_option("-R", "--ret-file", dest="ret_file",
109 help = "File where to store return code (success of connection) ",
110 default = "ret_file", type="str")
112 (options, args) = parser.parse_args()
115 if options.vif_type and options.vif_type == "IFF_TUN":
118 return ( options.vif_name, vif_type, options.pi,
119 options.local_port_file, options.remote_port_file,
120 options.remote_host, options.ret_file, options.bwlimit,
121 options.cipher, options.cipher_key, options.txqueuelen )
123 if __name__ == '__main__':
125 ( vif_name, vif_type, pi, local_port_file, remote_port_file,
126 remote_host, ret_file, bwlimit, cipher, cipher_key, txqueuelen
129 # Get the file descriptor of the TAP device from the process
131 fd = open_tap(vif_name, vif_type, pi)
133 # Create a local socket to stablish the tunnel connection
134 hostaddr = socket.gethostbyname(socket.gethostname())
135 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
136 sock.bind((hostaddr, 0))
137 (local_host, local_port) = sock.getsockname()
139 # Save local port information to file
140 f = open(local_port_file, 'w')
141 f.write("%d\n" % local_port)
144 # Wait until remote port information is available
145 while not os.path.exists(remote_port_file):
149 # Read remote port from file
150 # Try until something is read...
151 # xxx: There seems to be a weird behavior where
152 # even if the file exists and had the port number,
153 # the read operation returns empty string!
154 # Maybe a race condition?
156 f = open(remote_port_file, 'r')
157 remote_port = f.read()
165 remote_port = remote_port.strip()
166 remote_port = int(remote_port)
168 # Connect local socket to remote port
169 sock.connect((remote_host, remote_port))
170 remote = os.fdopen(sock.fileno(), 'r+b', 0)
172 # TODO: Test connectivity!
174 # Create a ret_file to indicate success
175 f = open(ret_file, 'w')
180 tunchannel.tun_fwd(tun, remote,
181 with_pi = True, # Planetlab TAP devices add PI headers
182 ether_mode = (vif_type == IFF_TAP),
184 cipher_key = cipher_key,
186 TERMINATE = TERMINATE,
188 tunqueue = txqueuelen,