netutils

Network-related utilities and helper functions.

oslo_utils.netutils.escape_ipv6(address)

Escape an IP address in square brackets if IPv6

Parameters:

address (string) – address to optionaly escape

Returns:

string

Added in version 3.29.0.

oslo_utils.netutils.get_ipv6_addr_by_EUI64(prefix, mac)

Calculate IPv6 address using EUI-64 specification.

This method calculates the IPv6 address using the EUI-64 addressing scheme as explained in rfc2373.

Parameters:
  • prefix – IPv6 prefix.

  • mac – IEEE 802 48-bit MAC address.

Returns:

IPv6 address on success.

Raises:

ValueError, TypeError – For any invalid input.

Added in version 1.4.

oslo_utils.netutils.get_mac_addr_by_ipv6(ipv6, dialect=<class 'netaddr.strategy.eui48.mac_unix_expanded'>)

Extract MAC address from interface identifier based IPv6 address.

For example from link-local addresses (fe80::/10) generated from MAC.

Parameters:
  • ipv6 – An interface identifier (i.e. mostly MAC) based IPv6 address as a netaddr.IPAddress() object.

  • dialect – The netaddr dialect of the the object returned. Defaults to netaddr.mac_unix_expanded.

Returns:

A MAC address as a netaddr.EUI() object.

See also: * https://tools.ietf.org/html/rfc4291#appendix-A * https://tools.ietf.org/html/rfc4291#section-2.5.6

Added in version 4.3.0.

oslo_utils.netutils.get_my_ipv4()

Returns the actual ipv4 of the local machine.

This code figures out what source address would be used if some traffic were to be sent out to some well known address on the Internet. In this case, IP from RFC5737 is used, but the specific address does not matter much. No traffic is actually sent.

Added in version 1.1.

Changed in version 1.2.1: Return '127.0.0.1' if there is no default interface.

oslo_utils.netutils.get_my_ipv6()

Returns the actual IPv6 address of the local machine.

This code figures out what source address would be used if some traffic were to be sent out to some well known address on the Internet. In this case, IPv6 from RFC3849 is used, but the specific address does not matter much. No traffic is actually sent.

Added in version 6.1: Return '::1' if there is no default interface.

oslo_utils.netutils.get_noscope_ipv6(address)

Take an IPv6 address and trim scope out if present.

Parameters:

address (string) – Value to change

Returns:

string

oslo_utils.netutils.is_ipv6_enabled()

Check if IPv6 support is enabled on the platform.

This api will look into the proc entries of the platform to figure out the status of IPv6 support on the platform.

Returns:

True if the platform has IPv6 support, False otherwise.

Added in version 1.4.

oslo_utils.netutils.is_valid_cidr(address)

Verify that address represents a valid CIDR address.

Parameters:

address (string) – Value to verify

Returns:

bool

Added in version 3.8.

oslo_utils.netutils.is_valid_icmp_code(code)

Verify if ICMP code is valid.

Parameters:

code – ICMP code field can be valid integer or None

Returns:

bool

ICMP code field can be either None or valid integer having a value of 0 up to and including 255.

oslo_utils.netutils.is_valid_icmp_type(type)

Verify if ICMP type is valid.

Parameters:

type – ICMP type field can only be a valid integer

Returns:

bool

ICMP type field can be valid integer having a value of 0 up to and including 255.

oslo_utils.netutils.is_valid_ip(address)

Verify that address represents a valid IP address.

Parameters:

address (string) – Value to verify

Returns:

bool

Added in version 1.1.

oslo_utils.netutils.is_valid_ipv4(address, strict=True)

Verify that address represents a valid IPv4 address.

Parameters:
  • address (string) – Value to verify

  • strict – flag allowing users to restrict validation to IP addresses in presentation format (a.b.c.d) as opposed to address format (a.b.c.d, a.b.c, a.b, a).

Returns:

bool

Added in version 1.1.

Changed in version 4.8.0: Allow to restrict validation to IP addresses in presentation format (a.b.c.d) as opposed to address format (a.b.c.d, a.b.c, a.b, a).

oslo_utils.netutils.is_valid_ipv6(address)

Verify that address represents a valid IPv6 address.

Parameters:

address (string) – Value to verify

Returns:

bool

Added in version 1.1.

oslo_utils.netutils.is_valid_ipv6_cidr(address)

Verify that address represents a valid IPv6 CIDR address.

Parameters:

address (string) – address to verify

Returns:

true if address is valid, false otherwise

Added in version 3.17.

oslo_utils.netutils.is_valid_mac(address)

Verify the format of a MAC address.

Check if a MAC address is valid and contains six octets. Accepts colon-separated format only.

Parameters:

address – MAC address to be validated.

Returns:

True if valid. False if not.

Added in version 3.17.

oslo_utils.netutils.is_valid_port(port)

Verify that port represents a valid port number.

Port can be valid integer having a value of 0 up to and including 65535.

Added in version 1.1.1.

oslo_utils.netutils.parse_host_port(address, default_port=None)

Interpret a string as a host:port pair.

An IPv6 address MUST be escaped if accompanied by a port, because otherwise ambiguity ensues: 2001:db8:85a3::8a2e:370:7334 means both [2001:db8:85a3::8a2e:370:7334] and [2001:db8:85a3::8a2e:370]:7334.

>>> parse_host_port('server01:80')
('server01', 80)
>>> parse_host_port('server01')
('server01', None)
>>> parse_host_port('server01', default_port=1234)
('server01', 1234)
>>> parse_host_port('[::1]:80')
('::1', 80)
>>> parse_host_port('[::1]')
('::1', None)
>>> parse_host_port('[::1]', default_port=1234)
('::1', 1234)
>>> parse_host_port('2001:db8:85a3::8a2e:370:7334', default_port=1234)
('2001:db8:85a3::8a2e:370:7334', 1234)
>>> parse_host_port(None)
(None, None)
oslo_utils.netutils.set_tcp_keepalive(sock, tcp_keepalive=True, tcp_keepidle=None, tcp_keepalive_interval=None, tcp_keepalive_count=None)

Set values for tcp keepalive parameters

This function configures tcp keepalive parameters if users wish to do so.

Parameters:
  • tcp_keepalive – Boolean, turn on or off tcp_keepalive. If users are not sure, this should be True, and default values will be used.

  • tcp_keepidle – time to wait before starting to send keepalive probes

  • tcp_keepalive_interval – time between successive probes, once the initial wait time is over

  • tcp_keepalive_count – number of probes to send before the connection is killed

oslo_utils.netutils.urlsplit(url, scheme='', allow_fragments=True)

Parse a URL using urlparse.urlsplit(), splitting query and fragments. This function papers over Python issue9374 when needed.

The parameters are the same as urlparse.urlsplit.