def str_to_int(self, addr): """ @param addr: An IPv6 address in string form. @return: The equivalent unsigned integer for a given IPv6 address. """ if addr == '': raise AddrFormatError('Empty strings are not supported!') try: packed_int = _inet_pton(_AF_INET6, addr) return self.packed_to_int(packed_int) except Exception, e: raise AddrFormatError('%r is not a valid IPv6 address string!' \ % addr)
def str_to_int(self, addr): """ @param addr: An IPv4 dotted decimal address in string form. @return: An unsigned integer that is equivalent to value represented by the IPv4 dotted decimal address string. """ if addr == '': raise AddrFormatError('Empty strings are not supported!') try: return _struct.unpack('>I', _inet_aton(addr))[0] except: raise AddrFormatError('%r is not a valid IPv4 address string!' \ % addr)
def str_to_int(self, addr): """ @param addr: An EUI-48 or MAC address in string form. @return: An unsigned integer that is equivalent to value represented by EUI-48/MAC string address. """ words = [] if isinstance(addr, (str, unicode)): found_match = False for regexp in EUI48Strategy.RE_MAC_FORMATS: match_result = regexp.findall(addr) if len(match_result) != 0: found_match = True if isinstance(match_result[0], tuple): words = match_result[0] else: words = (match_result[0],) break if not found_match: raise AddrFormatError('%r is not a supported MAC format!' \ % addr) else: raise TypeError('%r is not str() or unicode()!' % addr) int_val = None if len(words) == 6: # 2 bytes x 6 (UNIX, Windows, EUI-48) int_val = int(''.join(['%.2x' % int(w, 16) for w in words]), 16) elif len(words) == 3: # 4 bytes x 3 (Cisco) int_val = int(''.join(['%.4x' % int(w, 16) for w in words]), 16) elif len(words) == 2: # 6 bytes x 2 (PostgreSQL) int_val = int(''.join(['%.6x' % int(w, 16) for w in words]), 16) elif len(words) == 1: # 12 bytes (bare, no delimiters) int_val = int('%012x' % int(words[0], 16), 16) else: raise AddrFormatError('unexpected word count in MAC address %r!' \ % addr) return int_val
def valid_str(self, addr): """ @param addr: An IP address in presentation (string) format. @return: C{True} if network address in string form is valid for this address type, C{False} otherwise. """ if addr == '': raise AddrFormatError('Empty strings are not supported!') try: _inet_aton(addr) except: return False return True
def valid_str(self, addr): """ @param addr: An IPv6 address in string form. @return: C{True} if IPv6 network address string is valid, C{False} otherwise. """ if addr == '': raise AddrFormatError('Empty strings are not supported!') try: _inet_pton(_AF_INET6, addr) except _socket.error: return False except TypeError: return False except ValueError: return False return True
def create_endpoint(self, hostname, orchestrator_id, workload_id, ip_list, mac=None): """ Create a single new endpoint object. Note, the endpoint will not be stored in ETCD until set_endpoint or update_endpoint is called. :param hostname: The hostname that the endpoint lives on. :param orchestrator_id: The workload that the endpoint belongs to. :param workload_id: The workload that the endpoint belongs to. :param ip_list: A list of ip addresses that the endpoint belongs to :param mac: The mac address that the endpoint belongs to :return: An Endpoint Object """ ep = Endpoint(hostname=hostname, orchestrator_id=orchestrator_id, workload_id=workload_id, endpoint_id=uuid.uuid1().hex, state="active", mac=mac) next_hops = self.get_default_next_hops(hostname) for ip in ip_list: try: next_hop = next_hops[ip.version] except KeyError: raise AddrFormatError( "This node is not configured for IPv%d." % ip.version) network = IPNetwork(ip) if network.version == 4: ep.ipv4_nets.add(network) ep.ipv4_gateway = next_hop else: ep.ipv6_nets.add(network) ep.ipv6_gateway = next_hop return ep