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>
24 from optparse import OptionParser, SUPPRESS_HELP
26 # TODO: GRE OPTION!! CONFIGURE THE VIF-UP IN GRE MODE!!
30 def create_socket(socket_name):
31 sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
32 sock.bind(socket_name)
39 while '\n' not in chunk:
41 chunk = conn.recv(1024)
42 except (OSError, socket.error), e:
43 if e[0] != errno.EINTR:
54 msg = ''.join(msg).split('\n')[0]
55 decoded = base64.b64decode(msg)
56 return decoded.rstrip()
58 def send_reply(conn, reply):
59 encoded = base64.b64encode(reply)
60 conn.send("%s\n" % encoded)
65 def reply_action(msg):
66 return "Reply to: %s" % msg
69 usage = ("usage: %prog -t <vif-type> -a <ip4-address> -n <net-prefix> "
70 "-s <snat> -p <pointopoint> -f <if-name-file> -S <socket-name>")
72 parser = OptionParser(usage = usage)
74 parser.add_option("-t", "--vif-type", dest="vif_type",
75 help = "Virtual interface type. Either IFF_TAP or IFF_TUN. "
76 "Defaults to IFF_TAP. ", type="str")
78 parser.add_option("-a", "--ip4-address", dest="ip4_address",
79 help = "IPv4 address to assign to interface. It must belong to the "
80 "network segment owned by the slice, given by the vsys_vnet tag. ",
83 parser.add_option("-n", "--net-prefix", dest="net_prefix",
84 help = "IPv4 network prefix for the interface. It must be the one "
85 "given by the slice's vsys_vnet tag. ",
88 parser.add_option("-s", "--snat", dest="snat", default = False,
89 action="store_true", help="Enable SNAT for the interface")
91 parser.add_option("-p", "--pointopoint", dest="pointopoint",
92 help = "Peer end point for the interface ", default = None,
95 parser.add_option("-f", "--if-name-file", dest="if_name_file",
96 help = "File to store the interface name assigned by the OS",
97 default = "if_name", type="str")
99 parser.add_option("-S", "--socket-name", dest="socket_name",
100 help = "Name for the unix socket used to interact with this process",
101 default = "tap.sock", type="str")
103 (options, args) = parser.parse_args()
105 vif_type = vsys.IFF_TAP
106 if options.vif_type and options.vif_type == "IFF_TUN":
107 vif_type = vsys.IFF_TUN
109 return (vif_type, options.ip4_address, options.net_prefix, options.snat,
110 options.pointopoint, options.if_name_file, options.socket_name)
112 if __name__ == '__main__':
114 (vif_type, ip4_address, net_prefix, snat, pointopoint,
115 if_name_file, socket_name) = get_options()
117 (fd, if_name) = vsys.fd_tuntap(vif_type)
118 vsys.vif_up(if_name, ip4_address, net_prefix, snat, pointopoint)
120 # Saving interface name to 'if_name_file
121 f = open(if_name_file, 'w')
125 # create unix socket to receive instructions
126 sock = create_socket(socket_name)
129 # wait for messages to arrive and process them
133 conn, addr = sock.accept()
139 except socket.timeout, e:
144 # Ignore - connection lost
149 reply = stop_action()
151 reply = reply_action(msg)
154 send_reply(conn, reply)