10 from optparse import OptionParser
15 # Trak SIGTERM, and set global termination flag instead of dying
19 def _finalize(sig,frame):
24 TERMINATE.append(None)
26 signal.signal(signal.SIGTERM, signal.SIG_DFL)
27 os.kill(os.getpid(), signal.SIGTERM)
29 signal.signal(signal.SIGTERM, _finalize)
31 # SIGUSR1 suspends forwading, SIGUSR2 resumes forwarding
33 def _suspend(sig,frame):
37 signal.signal(signal.SIGUSR1, _suspend)
39 def _resume(sig,frame):
43 signal.signal(signal.SIGUSR2, _resume)
46 usage = ("usage: %prog -a <address> -b <bwlimit> -c <cipher> "
47 "- k <cipher-key> -q <txqueuelen> -p <local-port-file> "
48 "-P <remote-port-file> -o <local-ip> -O <remote-ip> "
51 parser = OptionParser(usage = usage)
53 parser.add_option("-a", "--address", dest="address",
54 help="Socket address to send file descriptor to", type="str")
56 parser.add_option("-b", "--bwlimit", dest="bwlimit",
57 help="Specifies the interface's emulated bandwidth in bytes ",
58 default=None, type="int")
59 parser.add_option("-q", "--txqueuelen", dest="txqueuelen",
60 help="Specifies the interface's transmission queue length. ",
61 default=1000, type="int")
62 parser.add_option("-c", "--cipher", dest="cipher",
63 help="Cipher to encript communication. "
64 "One of PLAIN, AES, Blowfish, DES, DES3. ",
65 default=None, type="str")
66 parser.add_option("-k", "--cipher-key", dest="cipher_key",
67 help="Specify a symmetric encryption key with which to protect "
68 "packets across the tunnel. python-crypto must be installed "
70 default=None, type="str")
72 parser.add_option("-p", "--local-port-file", dest="local_port_file",
73 help = "File where to store the local binded UDP port number ",
74 default = "local_port_file", type="str")
75 parser.add_option("-P", "--remote-port-file", dest="remote_port_file",
76 help = "File where to read the remote UDP port number to connect to",
77 default = "remote_port_file", type="str")
78 parser.add_option("-o", "--local-ip", dest="local_ip",
79 help = "Local host IP", type="str")
80 parser.add_option("-O", "--remote-ip", dest="remote_ip",
81 help = "Remote host IP", type="str")
82 parser.add_option("-R", "--ret-file", dest="ret_file",
83 help = "File where to store return code (success of connection) ",
84 default = "ret_file", type="str")
86 (options, args) = parser.parse_args()
88 address = base64.b64decode(options.address)
90 return (address, options.local_port_file, options.remote_port_file,
91 options.local_ip, options.remote_ip, options.ret_file,
92 options.bwlimit, options.cipher, options.cipher_key,
95 if __name__ == '__main__':
97 (address, local_port_file, remote_port_file, local_ip, remote_ip,
98 ret_file, bwlimit, cipher, cipher_key, txqueuelen) = get_options()
100 # Create a local socket to stablish the tunnel connection
101 rsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
102 rsock.bind((local_ip, 0))
103 (local_host, local_port) = rsock.getsockname()
105 # Save local port information to file
106 f = open(local_port_file, 'w')
107 f.write("%d\n" % local_port)
110 # Wait until remote port information is available
111 while not os.path.exists(remote_port_file):
115 # Read remote port from file
116 # Try until something is read...
117 # xxx: There seems to be a weird behavior where
118 # even if the file exists and had the port number,
119 # the read operation returns empty string!
120 # Maybe a race condition?
122 f = open(remote_port_file, 'r')
123 remote_port = f.read()
131 remote_port = remote_port.strip()
132 remote_port = int(remote_port)
134 # Connect local socket to remote port
135 rsock.connect((remote_ip, remote_port))
136 remote = os.fdopen(rsock.fileno(), 'r+b', 0)
138 # create local socket to pass to fd-net-device, the other is for the tunnel
139 fd, tun = socket.socketpair(socket.AF_UNIX, socket.SOCK_STREAM, 0)
141 # pass one end of the socket pair to the fd-net-device
142 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
143 sock.connect(address)
144 passfd.sendfd(sock, fd, '0')
146 # TODO: Test connectivity!
148 # Create a ret_file to indicate success
149 f = open(ret_file, 'w')
156 tunchannel.tun_fwd(tun, remote,
157 with_pi = True, # No PI headers
158 ether_mode = IFF_TAP, # Ns-3 generates ethernet pkts
160 cipher_key = cipher_key,
162 TERMINATE = TERMINATE,
164 tunqueue = txqueuelen,