import base64
import errno
import os
-import time
+import passfd
import signal
import socket
+import time
import tunchannel
-import passfd
from optparse import OptionParser
+IFF_TAP = 0x0002
+
# Trak SIGTERM, and set global termination flag instead of dying
TERMINATE = []
+STARTED = False
+
def _finalize(sig,frame):
global TERMINATE
- TERMINATE.append(None)
+ global STARTED
+
+ if STARTED:
+ TERMINATE.append(None)
+ else:
+ signal.signal(signal.SIGTERM, signal.SIG_DFL)
+ os.kill(os.getpid(), signal.SIGTERM)
+
signal.signal(signal.SIGTERM, _finalize)
# SIGUSR1 suspends forwading, SIGUSR2 resumes forwarding
signal.signal(signal.SIGUSR2, _resume)
def get_options():
- usage = ("usage: %prog -a <address> "
- "-b <bwlimit> -c <cipher> -k <cipher-key> -q <txqueuelen> "
- "-l <local-port-file> -r <remote-port-file> -H <remote-host> "
- "-R <ret-file> ")
+ usage = ("usage: %prog -a <address> -b <bwlimit> -c <cipher> "
+ "- k <cipher-key> -q <txqueuelen> -p <local-port-file> "
+ "-P <remote-port-file> -o <local-ip> -O <remote-ip> "
+ "-r <ret-file> ")
parser = OptionParser(usage = usage)
"on the system." ,
default=None, type="str")
- parser.add_option("-l", "--local-port-file", dest="local_port_file",
+ parser.add_option("-p", "--local-port-file", dest="local_port_file",
help = "File where to store the local binded UDP port number ",
default = "local_port_file", type="str")
- parser.add_option("-r", "--remote-port-file", dest="remote_port_file",
+ parser.add_option("-P", "--remote-port-file", dest="remote_port_file",
help = "File where to read the remote UDP port number to connect to",
default = "remote_port_file", type="str")
- parser.add_option("-H", "--remote-host", dest="remote_host",
- help = "Remote host IP", default = "remote_host", type="str")
+ parser.add_option("-o", "--local-ip", dest="local_ip",
+ help = "Local host IP", type="str")
+ parser.add_option("-O", "--remote-ip", dest="remote_ip",
+ help = "Remote host IP", type="str")
parser.add_option("-R", "--ret-file", dest="ret_file",
help = "File where to store return code (success of connection) ",
default = "ret_file", type="str")
address = base64.b64decode(options.address)
- return ( address,
- options.local_port_file, options.remote_port_file,
- options.remote_host, options.ret_file, options.bwlimit,
- options.cipher, options.cipher_key, options.txqueuelen )
+ return (address, options.local_port_file, options.remote_port_file,
+ options.local_ip, options.remote_ip, options.ret_file,
+ options.bwlimit, options.cipher, options.cipher_key,
+ options.txqueuelen)
if __name__ == '__main__':
- (address, local_port_file, remote_port_file, remote_host, ret_file,
- bwlimit, cipher, cipher_key, txqueuelen) = get_options()
+ (address, local_port_file, remote_port_file, local_ip, remote_ip,
+ ret_file, bwlimit, cipher, cipher_key, txqueuelen) = get_options()
# Create a local socket to stablish the tunnel connection
- hostaddr = socket.gethostbyname(socket.gethostname())
rsock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
- rsock.bind((hostaddr, 0))
+ rsock.bind((local_ip, 0))
(local_host, local_port) = rsock.getsockname()
# Save local port information to file
remote_port = int(remote_port)
# Connect local socket to remote port
- rsock.connect((remote_host, remote_port))
+ rsock.connect((remote_ip, remote_port))
remote = os.fdopen(rsock.fileno(), 'r+b', 0)
# create local socket to pass to fd-net-device
f.write("0")
f.close()
+ STARTED = True
+
# Establish tunnel
tunchannel.tun_fwd(local, remote,
with_pi = False, # No PI headers