rename src/nepi/ into just nepi/
[nepi.git] / nepi / resources / linux / scripts / linux-tap-passfd.py
1 import base64
2 import errno
3 import os
4 import passfd
5 import socket
6
7 from optparse import OptionParser
8
9 PASSFD_MSG = "PASSFD"
10
11 def get_options():
12     usage = ("usage: %prog -a <address> -S <vif-socket> ")
13     
14     parser = OptionParser(usage = usage)
15
16     parser.add_option("-a", "--address", dest="address",
17         help = "Socket address to send file descriptor to", type="str")
18     parser.add_option("-S", "--vif-socket", dest="vif_socket",
19         help = "Name for the unix socket to request the TAP file descriptor", 
20         default = "tap.sock", type="str")
21
22     (options, args) = parser.parse_args()
23        
24     return (options.address, options.vif_socket)
25
26 if __name__ == '__main__':
27
28     (address, vif_socket) = get_options()
29   
30     # This script sends a message (PASSFD_MSG) to the process that created 
31     # the TUN/TAP device to request that it sens the file descriptor associated
32     # to the TUN/TAP to another process. The other process is waiting for
33     # the file descriptor on 'address'
34     sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
35     sock.connect(vif_socket)
36     emsg = base64.b64encode(PASSFD_MSG)
37     eargs = address
38     encoded = "%s|%s\n" % (emsg, eargs)
39     sock.send(encoded)
40
41