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()
114 if options.vif_type == "IFF_TUN":
117 return ( options.vif_name, vif_type, options.pi,
118 options.local_port_file, options.remote_port_file,
119 options.remote_host, options.ret_file, options.bwlimit,
120 options.cipher, options.cipher_key, options.txqueuelen )
122 if __name__ == '__main__':
124 ( vif_name, vif_type, pi, local_port_file, remote_port_file,
125 remote_host, ret_file, bwlimit, cipher, cipher_key, txqueuelen
128 # Get the file descriptor of the TAP device from the process
130 fd = open_tap(vif_name, vif_type, pi)
132 # Create a local socket to stablish the tunnel connection
133 hostaddr = socket.gethostbyname(socket.gethostname())
134 sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
135 sock.bind((hostaddr, 0))
136 (local_host, local_port) = sock.getsockname()
138 # Save local port information to file
139 f = open(local_port_file, 'w')
140 f.write("%d\n" % local_port)
143 # Wait until remote port information is available
144 while not os.path.exists(remote_port_file):
148 # Read remote port from file
149 # Try until something is read...
150 # xxx: There seems to be a weird behavior where
151 # even if the file exists and had the port number,
152 # the read operation returns empty string!
153 # Maybe a race condition?
155 f = open(remote_port_file, 'r')
156 remote_port = f.read()
164 remote_port = remote_port.strip()
165 remote_port = int(remote_port)
167 # Connect local socket to remote port
168 sock.connect((remote_host, remote_port))
169 remote = os.fdopen(sock.fileno(), 'r+b', 0)
171 # TODO: Test connectivity!
173 # Create a ret_file to indicate success
174 f = open(ret_file, 'w')
179 tunchannel.tun_fwd(tun, remote,
180 with_pi = True, # Planetlab TAP devices add PI headers
181 ether_mode = (vif_type == IFF_TAP),
183 cipher_key = cipher_key,
185 TERMINATE = TERMINATE,
187 tunqueue = txqueuelen,