2 # NEPI, a framework to manage network experiments
3 # Copyright (C) 2013 INRIA
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
18 # Author: Alina Quereilhac <alina.quereilhac@inria.fr>
25 from optparse import OptionParser, SUPPRESS_HELP
27 # TODO: GRE OPTION!! CONFIGURE THE VIF-UP IN GRE MODE!!
32 def create_socket(socket_name):
33 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
34 sock.bind(socket_name)
41 while '\n' not in chunk:
43 chunk = conn.recv(1024)
44 except (OSError, socket.error), e:
45 if e[0] != errno.EINTR:
56 msg = ''.join(msg).split('\n')[0]
57 # The message might have arguments that will be appended
58 # as a '|' separated list after the message type
62 dmsg = base64.b64decode(msg)
65 darg = base64.b64decode(arg)
66 dargs.append(darg.rstrip())
68 return (dmsg.rstrip(), dargs)
70 def send_reply(conn, reply):
71 encoded = base64.b64encode(reply)
72 conn.send("%s\n" % encoded)
77 def passfd_action(fd, args):
80 sock = socket.socket(socket.AF_UNIX, socket.SOCK_DGRAM)
82 passfd.sendfd(sock, fd, '0')
86 usage = ("usage: %prog -t <vif-type> -a <ip4-address> -n <net-prefix> "
87 "-s <snat> -p <pointopoint> -f <if-name-file> -S <socket-name>")
89 parser = OptionParser(usage = usage)
91 parser.add_option("-t", "--vif-type", dest="vif_type",
92 help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
93 "Defaults to IFF_TAP. ", type="str")
95 parser.add_option("-a", "--ip4-address", dest="ip4_address",
96 help = "IPv4 address to assign to interface. It must belong to the "
97 "network segment owned by the slice, given by the vsys_vnet tag. ",
100 parser.add_option("-n", "--net-prefix", dest="net_prefix",
101 help = "IPv4 network prefix for the interface. It must be the one "
102 "given by the slice's vsys_vnet tag. ",
105 parser.add_option("-s", "--snat", dest="snat", default = False,
106 action="store_true", help="Enable SNAT for the interface")
108 parser.add_option("-p", "--pointopoint", dest="pointopoint",
109 help = "Peer end point for the interface ", default = None,
112 parser.add_option("-f", "--if-name-file", dest="if_name_file",
113 help = "File to store the interface name assigned by the OS",
114 default = "if_name", type="str")
116 parser.add_option("-S", "--socket-name", dest="socket_name",
117 help = "Name for the unix socket used to interact with this process",
118 default = "tap.sock", type="str")
120 (options, args) = parser.parse_args()
122 vif_type = vsys.IFF_TAP
123 if options.vif_type and options.vif_type == "IFF_TUN":
124 vif_type = vsys.IFF_TUN
126 return (vif_type, options.ip4_address, options.net_prefix, options.snat,
127 options.pointopoint, options.if_name_file, options.socket_name)
129 if __name__ == '__main__':
131 (vif_type, ip4_address, net_prefix, snat, pointopoint,
132 if_name_file, socket_name) = get_options()
134 (fd, if_name) = vsys.fd_tuntap(vif_type)
135 vsys.vif_up(if_name, ip4_address, net_prefix, snat, pointopoint)
137 # Saving interface name to 'if_name_file
138 f = open(if_name_file, 'w')
142 # create unix socket to receive instructions
143 sock = create_socket(socket_name)
146 # wait for messages to arrive and process them
150 conn, addr = sock.accept()
155 (msg, args) = recv_msg(conn)
156 except socket.timeout, e:
161 # Ignore - connection lost
166 reply = stop_action()
167 elif msg == PASSFD_MSG:
168 reply = passfd_action(fd, args)
171 send_reply(conn, reply)