def set_ip_configuration_option(rcp, option, value): "Set IP configuration for network options." value = network.parse_ipv4(value) rcp.set('ip_configuration', "%s_1" % option, "%03d" % value[0]) rcp.set('ip_configuration', "%s_2" % option, "%03d" % value[1]) rcp.set('ip_configuration', "%s_3" % option, "%03d" % value[2]) rcp.set('ip_configuration', "%s_4" % option, "%03d" % value[3])
def _intf_ipv4_gateway(self, ifname): """ Return the default gateway if it belongs to the interface """ defgw = self.route.get(self.default_dst_ipv4) xaddr = dumbnet.intf.get(self, ifname)["addr"] route = self.route.get(xaddr) if not defgw or (route and defgw != route): return None defgwstr = str(defgw) address = network.parse_ipv4(dumbnet.ip_ntoa(xaddr.ip)) netmask = network.bitmask_to_mask_ipv4(xaddr.bits) if network.ipv4_in_network(network.parse_ipv4(defgwstr), netmask, network.mask_ipv4(netmask, address)): return defgwstr
def set(self, d, name=None): """ Set the configuration for an interface from a dict like interfaces(5). """ if name is not None: d['name'] = name iface = self.get(d['name']) address = d.get('address', iface['address']) netmask = d.get('netmask', iface['netmask']) d['addr'] = dumbnet.addr("%s/%s" % (address, netmask)) newgateway = None delgateway = None if d.has_key('gateway'): if iface['type'] != 'eth' or iface['family'] != 'inet': raise NotImplementedError("This method only supports modify IPv4 gateway for ethernet interface") gwstr = network.format_ipv4(network.parse_ipv4("%s" % d['gateway'])) if iface.get('gateway', None) != gwstr: newgateway = dumbnet.addr("%s" % d['gateway']) # If d hasn't gateway but iface has a gateway, remove previous gateway. elif iface.has_key('gateway'): if iface['type'] != 'eth' or iface['family'] != 'inet': raise NotImplementedError("This method only supports modify IPv4 gateway for ethernet interface") delgateway = d['addr'] if d.has_key('pointopoint'): d['dst_addr'] = dumbnet.addr("%s" % d['pointopoint']) if d.has_key('hwaddress'): d['link_addr'] = dumbnet.addr("%s" % d['hwaddress'], dumbnet.ADDR_TYPE_ETH) dumbnet.intf.set(self, d) # If iface has previously a default gateway if delgateway: try: self.route.delete(self.default_dst_ipv4) except OSError, e: # If an error has occurred, rollback if iface.has_key('gateway'): del iface['gateway'] self.set(iface) raise OSError(str(e))
def set(self, d, name=None): """ Set the configuration for an interface from a dict like interfaces(5). """ if name is not None: d["name"] = name iface = self.get(d["name"]) address = d.get("address", iface["address"]) netmask = d.get("netmask", iface["netmask"]) d["addr"] = dumbnet.addr("%s/%s" % (address, netmask)) newgateway = None delgateway = None if d.has_key("gateway"): if iface["type"] != "eth" or iface["family"] != "inet": raise NotImplementedError("This method only supports modify IPv4 gateway for ethernet interface") gwstr = network.format_ipv4(network.parse_ipv4("%s" % d["gateway"])) if iface.get("gateway", None) != gwstr: newgateway = dumbnet.addr("%s" % d["gateway"]) # If d hasn't gateway but iface has a gateway, remove previous gateway. elif iface.has_key("gateway"): if iface["type"] != "eth" or iface["family"] != "inet": raise NotImplementedError("This method only supports modify IPv4 gateway for ethernet interface") delgateway = d["addr"] if d.has_key("pointopoint"): d["dst_addr"] = dumbnet.addr("%s" % d["pointopoint"]) if d.has_key("hwaddress"): d["link_addr"] = dumbnet.addr("%s" % d["hwaddress"], dumbnet.ADDR_TYPE_ETH) dumbnet.intf.set(self, d) # If iface has previously a default gateway if delgateway: try: self.route.delete(self.default_dst_ipv4) except OSError, e: # If an error has occurred, rollback if iface.has_key("gateway"): del iface["gateway"] self.set(iface) raise OSError(str(e))
def get_netiface_info(self, iface): try: info = self.netcfg.get(iface) except OSError: return False if not info or not info.has_key('family'): return False info['carrier'] = False info['physicalif'] = False info['vlanif'] = False info['vlan-id'] = None info['vlan-raw-device'] = None info['aliasif'] = False info['alias-raw-device'] = None info['hwtypeid'] = None info['options'] = None if network.is_linux_netdev_if(iface): info['carrier'] = network.is_interface_plugged(iface) info['flags'] = network.get_interface_flags(iface) info['physicalif'] = network.is_linux_phy_if(iface) info['dummyif'] = network.is_linux_dummy_if(iface) info['vlanif'] = network.is_linux_vlan_if(iface) info['hwtypeid'] = network.get_interface_hwtypeid(iface) if not info['physicalif'] and info.has_key('gateway'): del info['gateway'] if not info.has_key('hwaddress'): info['hwaddress'] = network.get_interface_hwaddress(iface) if not info.has_key('mtu'): info['mtu'] = network.get_interface_mtu(iface) else: if network.is_alias_if(iface): info['aliasif'] = True phyifname = network.phy_name_from_alias_if(iface) info['alias-raw-device'] = phyifname if network.is_linux_netdev_if(phyifname): if info.has_key('gateway'): try: phyinfo = self.netcfg.get(phyifname) if phyinfo.get('gateway') == info['gateway']: del info['gateway'] except OSError: pass info['carrier'] = network.is_interface_plugged(phyifname) info['hwtypeid'] = network.get_interface_hwtypeid(phyifname) if not info.has_key('flags'): info['flags'] = None if not info.has_key('hwaddress'): info['hwaddress'] = None if not info.has_key('mtu'): info['mtu'] = None if info['family'] in ('inet', 'inet6'): inetxparsed = self.inetxparser.get(iface) if inetxparsed: info['options'] = xivo_config.unreserved_interfaces_options(inetxparsed) xdict = dict(inetxparsed) info['method'] = xdict.get('method') info['vlan-id'] = xdict.get('vlan-id') info['vlan-raw-device'] = xdict.get('vlan-raw-device') if 'address' not in info \ and 'address' in xdict \ and 'netmask' in xdict: info['address'] = xdict.get('address') info['netmask'] = xdict['netmask'] if 'broadcast' in xdict: info['broadcast'] = xdict['broadcast'] if 'network' in xdict: info['network'] = xdict['network'] if info['family'] == 'inet' \ and not info.has_key('gateway') \ and info.has_key('netmask') \ and info.has_key('network') \ and xdict.has_key('gateway') \ and network.ipv4_in_network(network.parse_ipv4(xdict['gateway']), network.parse_ipv4(info['netmask']), network.parse_ipv4(info['network'])): info['gateway'] = xdict['gateway'] else: info['method'] = None if info['vlanif']: vconfig = network.get_vlan_info(iface) info['vlan-id'] = vconfig.get('vlan-id', info['vlan-id']) info['vlan-raw-device'] = vconfig.get('vlan-raw-device', info['vlan-raw-device']) return info
def is_ipv4_netmask_valid(netmask): "Verify if it is a valid netmask." return network.is_ipv4_address_valid(netmask) \ and network.plausible_netmask(network.parse_ipv4(netmask))
def get_netiface_info(self, iface): try: info = self.netcfg.get(iface) except OSError: return False if not info or not info.has_key("family"): return False info["carrier"] = False info["physicalif"] = False info["vlanif"] = False info["vlan-id"] = None info["vlan-raw-device"] = None info["aliasif"] = False info["alias-raw-device"] = None info["hwtypeid"] = None info["options"] = None if network.is_linux_netdev_if(iface): info["carrier"] = network.is_interface_plugged(iface) info["flags"] = network.get_interface_flags(iface) info["physicalif"] = network.is_linux_phy_if(iface) info["dummyif"] = network.is_linux_dummy_if(iface) info["vlanif"] = network.is_linux_vlan_if(iface) info["hwtypeid"] = network.get_interface_hwtypeid(iface) if not info["physicalif"] and info.has_key("gateway"): del info["gateway"] if not info.has_key("hwaddress"): info["hwaddress"] = network.get_interface_hwaddress(iface) if not info.has_key("mtu"): info["mtu"] = network.get_interface_mtu(iface) else: if network.is_alias_if(iface): info["aliasif"] = True phyifname = network.phy_name_from_alias_if(iface) info["alias-raw-device"] = phyifname if network.is_linux_netdev_if(phyifname): if info.has_key("gateway"): try: phyinfo = self.netcfg.get(phyifname) if phyinfo.get("gateway") == info["gateway"]: del info["gateway"] except OSError: pass info["carrier"] = network.is_interface_plugged(phyifname) info["hwtypeid"] = network.get_interface_hwtypeid(phyifname) if not info.has_key("flags"): info["flags"] = None if not info.has_key("hwaddress"): info["hwaddress"] = None if not info.has_key("mtu"): info["mtu"] = None if info["family"] in ("inet", "inet6"): inetxparsed = self.inetxparser.get(iface) if inetxparsed: info["options"] = xivo_config.unreserved_interfaces_options(inetxparsed) xdict = dict(inetxparsed) info["method"] = xdict.get("method") info["vlan-id"] = xdict.get("vlan-id") info["vlan-raw-device"] = xdict.get("vlan-raw-device") if "address" not in info and "address" in xdict and "netmask" in xdict: info["address"] = xdict.get("address") info["netmask"] = xdict["netmask"] if "broadcast" in xdict: info["broadcast"] = xdict["broadcast"] if "network" in xdict: info["network"] = xdict["network"] if ( info["family"] == "inet" and not info.has_key("gateway") and info.has_key("netmask") and info.has_key("network") and xdict.has_key("gateway") and network.ipv4_in_network( network.parse_ipv4(xdict["gateway"]), network.parse_ipv4(info["netmask"]), network.parse_ipv4(info["network"]), ) ): info["gateway"] = xdict["gateway"] else: info["method"] = None if info["vlanif"]: vconfig = network.get_vlan_info(iface) info["vlan-id"] = vconfig.get("vlan-id", info["vlan-id"]) info["vlan-raw-device"] = vconfig.get("vlan-raw-device", info["vlan-raw-device"]) return info