#!/usr/bin/python
# VSYS script to configure routes on virtual network interfaces from the root slice
-# Thom Haddow - 06/10/09
+# Claudio Freire - 2011/05/19
#
# Gets slice name as argv[1]
# Takes routing rules on stdin, one per line
vnet_base = vnet_mask = None
if vnet_base is None:
- #print >>sys.stderr, "Could not find entry for slice %s in netblock config file %s" % (slicename, netblock_config)
- #sys.exit(1)
- vnet_base = '192.168.2.0'
- vnet_mask = 24
+ print >>sys.stderr, "Could not find entry for slice %s in netblock config file %s" % (slicename, netblock_config)
+ sys.exit(1)
vnet_int = struct.unpack('!L', socket.inet_aton(vnet_base))[0]
-vnet_int = (vnet_int >> vnet_mask) << vnet_mask
-
-mask_int = (0xffffffff << vnet_mask) & 0xffffffff
+vnet_int = (vnet_int >> (32-vnet_mask)) << (32-vnet_mask)
+mask_int = (0xffffffff >> (32-vnet_mask)) << (32-vnet_mask)
# rule line regex
rule_re = r"(?P<cmd>add|del)\s+(?P<targetbase>(?:\d{1,3}[.]){3}\d{1,3})(?:/(?P<targetprefix>\d{1,2}))?\s+gw\s+(?P<gw>(?:\d{1,3}[.]){3}\d{1,3}) (?P<dev>(?:tun|tap)%d-\d{1,5})" % (sliceid,)
# validating overall structure in the process
match = rule_re.match(argline)
if not match:
- print >>sys.stderr, "Invalid rule %r:" % (argline,)
+ print >>sys.stderr, "Invalid rule %r" % (argline,)
continue
# Validate IPs involved
if (gw_ip_int & mask_int) != vnet_int:
print >>sys.stderr, "Invalid rule %r: gateway must belong to the %s/%s network" % (argline, vnet_base, vnet_mask)
continue
-
+
# Revalidate command (just in case)
command = match.group("cmd")
if command not in ('add','del'):