9b0b514e539271b2dfe3289d05974d1a59c7cfe4
[nepi.git] / src / nepi / resources / planetlab / scripts / pl-vif-udp-connect.py
1 import base64
2 import errno
3 import os
4 import passfd
5 import signal
6 import socket
7 import time
8 import tunchannel
9 import vsys
10
11 from optparse import OptionParser, SUPPRESS_HELP
12
13 PASSFD_MSG = "PASSFD"
14
15 # Trak SIGTERM, and set global termination flag instead of dying
16 TERMINATE = []
17 def _finalize(sig,frame):
18     global TERMINATE
19     TERMINATE.append(None)
20 signal.signal(signal.SIGTERM, _finalize)
21
22 # SIGUSR1 suspends forwading, SIGUSR2 resumes forwarding
23 SUSPEND = []
24 def _suspend(sig,frame):
25     global SUSPEND
26     if not SUSPEND:
27         SUSPEND.append(None)
28 signal.signal(signal.SIGUSR1, _suspend)
29
30 def _resume(sig,frame):
31     global SUSPEND
32     if SUSPEND:
33         SUSPEND.remove(None)
34 signal.signal(signal.SIGUSR2, _resume)
35
36 def get_fd(socket_name):
37     # Socket to recive the file descriptor
38     fdsock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
39     fdsock.bind("")
40     address = fdsock.getsockname()
41
42     # Socket to connect to the pl-vif-create process 
43     # and send the PASSFD message
44     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
45     sock.connect(socket_name)
46     emsg = base64.b64encode(PASSFD_MSG)
47     eargs = base64.b64encode(address)
48     encoded = "%s|%s\n" % (emsg, eargs)
49     sock.send(encoded)
50
51     # Receive fd
52     (fd, msg) = passfd.recvfd(fdsock)
53     
54     # Receive reply
55     reply = sock.recv(1024)
56     reply = base64.b64decode(reply)
57
58     sock.close()
59     fdsock.close()
60     return fd
61
62 def get_options():
63     usage = ("usage: %prog -t <vif-type> -S <fd-socket-name> "
64         "-l <local-port-file> -r <remote-port-file> -H <remote-host> "
65         "-R <ret-file> ")
66     
67     parser = OptionParser(usage = usage)
68
69     parser.add_option("-t", "--vif-type", dest="vif_type",
70         help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
71             "Defaults to IFF_TAP. ", type="str")
72     parser.add_option("-S", "--fd-socket-name", dest="fd_socket_name",
73         help = "Name for the unix socket to request the TAP file descriptor", 
74         default = "tap.sock", type="str")
75     parser.add_option("-l", "--local-port-file", dest="local_port_file",
76         help = "File where to store the local binded UDP port number ", 
77         default = "local_port_file", type="str")
78     parser.add_option("-r", "--remote-port-file", dest="remote_port_file",
79         help = "File where to read the remote UDP port number to connect to", 
80         default = "remote_port_file", type="str")
81     parser.add_option("-H", "--remote-host", dest="remote_host",
82         help = "Remote host IP", 
83         default = "remote_host", type="str")
84     parser.add_option("-R", "--ret-file", dest="ret_file",
85         help = "File where to store return code (success of connection) ", 
86         default = "ret_file", type="str")
87
88     (options, args) = parser.parse_args()
89        
90     vif_type = vsys.IFF_TAP
91     if options.vif_type and options.vif_type == "IFF_TUN":
92         vif_type = vsys.IFF_TUN
93
94     return ( vif_type, options.fd_socket_name, options.local_port_file,
95             options.remote_port_file, options.remote_host,
96             options.ret_file )
97
98 if __name__ == '__main__':
99
100     ( vif_type, socket_name, local_port_file, remote_port_file,
101             remote_host, ret_file ) = get_options()
102    
103     # Get the file descriptor of the TAP device from the process
104     # that created it
105     fd = get_fd(socket_name)
106     tun = os.fdopen(int(fd), 'r+b', 0)
107
108     # Create a local socket to stablish the tunnel connection
109     hostaddr = socket.gethostbyname(socket.gethostname())
110     sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, 0)
111     sock.bind((hostaddr, 0))
112     (local_host, local_port) = sock.getsockname()
113
114     # Save local port information to file
115     f = open(local_port_file, 'w')
116     f.write("%d\n" % local_port)
117     f.close()
118
119     # Wait until remote port information is available
120     while not os.path.exists(remote_port_file):
121         time.sleep(2)
122
123     # Read remote port from file
124     f = open(remote_port_file, 'r')
125     remote_port = f.read()
126     f.close()
127     
128     remote_port = remote_port.strip()
129     remote_port = int(remote_port)
130
131     # Connect local socket to remote port
132     sock.connect((remote_host, remote_port))
133     remote = os.fdopen(sock.fileno(), 'r+b', 0)
134
135     # TODO: Test connectivity!    
136
137     # Create a ret_file to indicate success
138     f = open(ret_file, 'w')
139     f.write("0")
140     f.close()
141
142     # Establish tunnel
143     # TODO: ADD parameters tunqueue, tunkqueue, cipher_key
144     tunchannel.tun_fwd(tun, remote,
145         # Planetlab TAP devices add PI headers 
146         with_pi = True,
147         ether_mode = (vif_type == vsys.IFF_TAP),
148         cipher_key = None,
149         udp = True,
150         TERMINATE = TERMINATE,
151         SUSPEND = SUSPEND,
152         tunqueue = 1000,
153         tunkqueue = 500,
154     ) 
155  
156