Skip to content

netaddr #

Network address processing library for V

netaddr supports IP (both IPv4 and IPv6) and EUI (EUI-48, EUI-64) addresses.

Features:

  • Parsing and validation of EUI, IP addresses and IP network addresses.
  • Converting addresses to/from different formats, e.g. various string representations, byte arrays, integers.
  • IP addresses and networks comparison.
  • IPv4-IPv6 interoperability.
  • IPv6 scopes support.
  • Parsing/creating Teredo (IPv4 over IPv6 tunneling) addresses.
  • Testing addresses and networks i.e. check is network intended for private use or not and many other tests.
  • Generating random EUI-48 (useful for virtual machines, etc).
  • Converting EUI to IPv6.
  • Calculating IP networks (both versions).
  • ...

Usage

IP address/network parsing and validation

Once you got an Ipv{4,6}Addr or Ipv{4,6}Net instance without errors — that's done, validation is passed. In the simplest case you can do:

if ip := netaddr.IpAddr.from_string('::1') {
    // address is valid
} else {
    // address is not valid
}

More concrete example that prints the address on success:

import netaddr

fn main() {
    addr := arguments()[1] or {
        panic('no such argument, specify an IP address')
    }
    ip := netaddr.IpAddr.from_string(addr) or {
        panic('${addr} is not valid IP address')
    }
    if ip is netaddr.Ipv4Net || ip is netaddr.Ipv6Net {
        panic('${ip} seems to be network, not a single host addresses')
    }
    println(addr)
}

Working with IP networks

Basic usage:

import netaddr

fn main() {
    network4 := netaddr.Ipv4Net.from_string('172.16.16.0/24')!
    network6 := netaddr.Ipv6Net.from_string('fe80:aaaa:bbbb:cccc::/64')!
    println(network4)
    println(network6)
}

The from_string() method of the Ipv4Net and Ipv6Net structs supports several different formats for network prefixes:

  • a single address without a prefix length will be considered as a network with a prefix of 32 or 128 depending on the IP version;
  • an address with an integer non-negative prefix length;
  • an address with a subnet mask;
  • an address with a host mask;
network := netaddr.Ipv4Net.from_string('203.0.113.99/0.0.0.255')!
assert network.network_address.str() == '203.0.113.0'
assert (network.host_address as netaddr.Ipv4Addr).str() == '203.0.113.99'

If host bits is set in the network address the optional host_address field will be filled with this host address. The network_address field always will contain the real network address. The host_address will equal none for single address "networks" such as 127.0.0.1/32, etc.

Iterating over network hosts

Ipv4Net and Ipv6Net has next() method that implements the V iterator mechanism which allow you use object in for loop in following maner:

network := netaddr.Ipv4Net.from_string('172.16.16.0/26')!
for host in network {
    // `host` is an Ipv4Addr instance
    if host == network.network_address || host == network.broadcast_address {
        continue
    }
    println(host)
}

Note that the iterator will iterate over all addresses in the network, including those that cannot be used as a host address: the network address and broadcast address. Exceptions are the networks with small prefixes: 31 (point-to-point) and 32 (single address) for IPv4, and 127 and 128 for IPv6 respectively.

If you just want to check is network contain some address use contains() method:

network := netaddr.Ipv4Net.from_string('172.16.0.0/26')!
addr := netaddr.Ipv4Addr.from_string('172.16.16.68')!
assert !network.contains(addr)

Networks intersection tests and subnetting

To choose the right prefix when planning a network, it is important to avoid overlapping network address spaces.

Check partial overlapping:

net_a := netaddr.Ipv4Net.from_string('100.64.0.0/22')!
net_b := netaddr.Ipv4Net.from_string('100.64.4.0/22')!
assert !net_a.overlaps(net_b)

Also you can check is the network a subnet or supernet of another one:

assert !net_a.is_subnet_of(net_b)
assert !net_a.is_supernet_of(net_b)

To split the network into equal prefixes, you can use the subnets() method:

network := netaddr.Ipv4Net.from_string('100.64.64.0/20')!
println(network)
mut subnets := []netaddr.Ipv4Net{}
for subnet in network.subnets(22)! {
    subnets << subnet
}
println(subnets)
// [100.64.64.0/22, 100.64.68.0/22, 100.64.72.0/22, 100.64.76.0/22]

IPv4-IPv6 interoperability

netaddr supports IP conversion between 4 and 6 versions in both directions.

The V REPL session below illustrates this:

>>> import netaddr
>>> ip4 := netaddr.Ipv4Addr.from_string('203.0.113.99')!
>>> ip4
203.0.113.99
>>> ip6 := ip4.ipv6()
>>> ip6
::ffff:203.0.113.99
>>> ip6.is_ipv4_mapped()
true
>>> ip6.is_ipv4_compat()
false
>>> ip6.ipv4()!
203.0.113.99
>>> ip4 == ip6.ipv4()!
true

IPv6 address cannot be converted to IPv4 if it is not the IPv4-mapped or IPv4-compatible per RFC 4291 Section 2.5.5.

Also several representation formats are supported:

>>> ip6.format(.dotted | .compact)
::ffff:203.0.113.99
>>> ip6.format(.dotted | .verbose)
0000:0000:0000:0000:0000:ffff:203.0.113.99
>>> ip6.format(.compact)
::ffff:cb00:7163
>>> ip6.format(.verbose)
0000:0000:0000:0000:0000:ffff:cb00:7163

Dealing with scoped IPv6 addresses

Ipv6Addr struct has optional zone_id field that contains the scope zone identifier if available. For example (V REPL session):

>>> ip6_scoped := netaddr.Ipv6Addr.from_string('fe80::d08e:6658:38bd:6391%wlan0')!
>>> ip6_scoped
fe80::d08e:6658:38bd:6391%wlan0
>>> ip6_scoped.zone_id
Option('wlan0')
>>> zone_id := ip6_scoped.zone_id as string
>>> zone_id
wlan0

For creating scoped address from big.Integer, u8, u16, etc use the optional zone_id parameter. e.g.:

// vfmt off
new := netaddr.Ipv6Addr.new(
    0xfe80, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x0000, 0x1234,
    zone_id: 'eth0'
)!
from_u8 := netaddr.Ipv6Addr.from_octets(
    [
        u8(0xfe), 0x80,
        0x0, 0x0,
        0x0, 0x0,
        0x0, 0x0,
        0x0, 0x0,
        0x0, 0x0,
        0x0, 0x0,
        0x12, 0x34
    ]!,
    zone_id: 'eth0'
)!
// vfmt on
println(new) // fe80::1234%eth0
println(from_u8) // fe80::1234%eth0

Also you can create new IPv6 address with zone_id from existing Ipv6Addr instance:

>>> ip6 := netaddr.Ipv6Addr.from_string('fe80::d08e:6658:38bd:6391')!
>>> new_ip6 := ip6.with_scope('eth1')!
>>> new_ip6
fe80::d08e:6658:38bd:6391%eth1

Scoped IPv6 networks are supported, but Ipv6Net struct does not have own zone_id field, refer to it's network_address as follows:

>>> ip6net := netaddr.Ipv6Net.from_string('fe80::%eth1/64')!
>>> ip6net
fe80::%eth1/64
>>> ip6net.network_address.zone_id
Option('eth1')

Getting global unicast IPv6 from EUI-48

This is a slightly synthetic example that shows how you can automatically get a global unicast IPv6 address for a host given the network prefix.

// Known network prefix
network := netaddr.Ipv6Net.from_string('2001:0db8::/64')!
// Lets generate random EUI-48
eui := netaddr.Eui48.random()
// ipv6() method converts EUI-48 to Modified EUI-64 and appends it to prefix per RFC 4291
ip := eui.ipv6(network.network_address)!
println(ip) // 2001:db8::8429:6bff:fedc:ef8b

Note that using EUI in IPv6 address may cause security issues. See RFC 4941 for details.

License

netaddr is released under LGPL 3.0 or later license.

SPDX Lincese ID: LGPL-3.0-or-later.

fn Eui.from_string #

fn Eui.from_string(addr string) !Eui

Eui.from_string parses addr string and returns EUI-48 or EUI-64.

Example

cmd := os.execute('ip -br link show wlan0')
interface_id := netaddr.Eui.from_string(cmd.output.split_by_space()[2])!
println(interface_id)

fn Eui48.from_octets #

fn Eui48.from_octets(addr [6]u8) Eui48

Eui48.from_octets creates new EUI-48 from six-element byte array.

fn Eui48.from_string #

fn Eui48.from_string(addr string) !Eui48

Eui48.from_string parses addr string and returns new EUI-48 instance.

Example

assert Eui48.from_string('a96:7a87:4ae3')!.str() == '0a-96-7a-87-4a-e3'

fn Eui48.new #

fn Eui48.new(a u8, b u8, c u8, d u8, e u8, f u8) Eui48

Eui48.new creates new EUI-48 from six octets.

fn Eui48.random #

fn Eui48.random(params Eui48RandomParams) Eui48

Eui48.random is guaranteed to return a locally administered unicast EUI-48. By default the WyRandRNG is used with default seed. You can set custom OUI if you don't want generate random one.

Example

>>> netaddr.Eui48.random()
be-8c-f7-90-b4-60
>>> netaddr.Eui48.random(oui: [u8(0x02), 0x0, 0x0]!)
02-00-00-2d-1d-01

fn Eui64.from_octets #

fn Eui64.from_octets(addr [8]u8) Eui64

Eui64.from_octets creates new EUI-64 from eight-element byte array.

fn Eui64.from_string #

fn Eui64.from_string(addr string) !Eui64

Eui64.from_string parses addr string and returns new EUI-64 instance.

fn Eui64.new #

fn Eui64.new(a u8, b u8, c u8, d u8, e u8, f u8, g u8, h u8) Eui64

Eui64.new creates new EUI-64 from eigth octets.

fn IpAddr.from_string #

fn IpAddr.from_string(addr string) !IpAddr

IpAddr.from_string parses the addr string and returns IP address or IP network. This is universal function that processes both internet protocol versions.

This function accepts all of the IP address and network formats allowed in Ipv4Addr.from_string, Ipv4Net.from_string, Ipv6Addr.from_string and Ipv6Net.from_string.

Example

ip := netaddr.IpAddr.from_string('2001:db8:beaf::/56')!
match ip {
	netaddr.Ipv4Addr {
		println('${ip} is IPv4 address')
	}
	netaddr.Ipv4Net {
		println('${ip} is IPv4 network')
	}
	netaddr.Ipv6Addr {
		println('${ip} is IPv6 address')
	}
	netaddr.Ipv6Net {
		println('${ip} is IPv6 network')
	}
}

fn Ipv4Addr.from_octets #

fn Ipv4Addr.from_octets(addr [4]u8) Ipv4Addr

Ipv4Addr.from_octets creates new Ipv4Addr instance from four-element byte array.

fn Ipv4Addr.from_string #

fn Ipv4Addr.from_string(addr string) !Ipv4Addr

Ipv4Addr.from_string parses addr string and creates new Ipv4Addr instance. Only dotted-decimal form is allowed e.g. 203.0.113.5.

fn Ipv4Addr.from_u32 #

fn Ipv4Addr.from_u32(addr u32) Ipv4Addr

Ipv4Addr.from_u32 creates new Ipv4Addr instance from unsigned 32 bit integer.

fn Ipv4Addr.new #

fn Ipv4Addr.new(a u8, b u8, c u8, d u8) Ipv4Addr

Ipv4Addr.new creates new Ipv4Addr instance from four octets.

fn Ipv4Net.from_string #

fn Ipv4Net.from_string(cidr string) !Ipv4Net

Ipv4Net.from_string parses cidr and creates new Ipv4Net. Allowed formats are:

  • single IP address without prefix length, 32 is applied;
  • network address with non-negative integer prefix length e.g. 172.16.16.0/24;
  • network address with host mask: 172.16.16.0/0.0.0.255;
  • network address with network mask: 172.16.16.0/255.255.255.0.

If prefix length is greather than 32 and host bits is set in the network address the optional host_address field will be filled with this host address. The network_address field always will contain the real network address.

fn Ipv4Net.from_u32 #

fn Ipv4Net.from_u32(addr u32, prefix int) !Ipv4Net

Ipv4Net.from_u32 creates new Ipv4Net from network addr with given prefix length.

fn Ipv4Net.new #

fn Ipv4Net.new(addr Ipv4Addr, prefix int) !Ipv4Net

Ipv4Net.new creates new Ipv4Net from network addr with given prefix length.

fn Ipv6Addr.from_bigint #

fn Ipv6Addr.from_bigint(addr big.Integer, params Ipv6AddrParams) !Ipv6Addr

Ipv6Addr.from_bigint creates new Ipv6Addr from big.Integer with optional scope zone_id. The integer sign will be discarded. addr must fit in 128 bit.

fn Ipv6Addr.from_octets #

fn Ipv6Addr.from_octets(addr [16]u8, params Ipv6AddrParams) !Ipv6Addr

Ipv6Addr.from_octets creates new Ipv6Addr instance from 16 octets with optional scope zone_id.

fn Ipv6Addr.from_segments #

fn Ipv6Addr.from_segments(seg [8]u16, params Ipv6AddrParams) !Ipv6Addr

Ipv6Addr.from_segments creates new Ipv6Addr instance from eight 16-bit segments with optional scope zone_id.

fn Ipv6Addr.from_string #

fn Ipv6Addr.from_string(addr string) !Ipv6Addr

Ipv6Addr.from_string parses addr and returns new Ipv6Addr instance. The allowed formats are:

  • full length hexadecimal colon-separated address e.g. aaaa:bbbb:cccc:dddd:eeee:ffff:0000:1111;
  • address with omitted leading zeros in hextets;
  • address with omitted all-zeros hextets e.g. ::1;
  • combined form with omitted all-zeros and leading zeros;
  • mixed with dotted-decimal format e.g. ::ffff:192.168.3.12;
  • address with scope zone identifier e.g. fe80::d08e:6658%eth0;
  • address in square brackets: [a:b:c:d:e:f:0:1].

fn Ipv6Addr.new #

fn Ipv6Addr.new(a u16, b u16, c u16, d u16, e u16, f u16, g u16, h u16, params Ipv6AddrParams) !Ipv6Addr

Ipv6Addr.new creates new Ipv6Addr instance from eight 16-bit segments with optional scope zone_id.

Example

import netaddr

ip := netaddr.Ipv6Addr.new(0x2001, 0x0db8, 0x0008, 0x0004, 0x0000, 0x0000, 0x0000, 0x0002)!
println(ip) // 2001:db8:8:4::2

fn Ipv6Net.from_bigint #

fn Ipv6Net.from_bigint(addr big.Integer, prefix int) !Ipv6Net

Ipv6Net.from_bigint creates new IPv6 network from given addr and prefix. addr must fit in 128 bits.

fn Ipv6Net.from_string #

fn Ipv6Net.from_string(cidr string) !Ipv6Net

Ipv6Net.from_string parses cidr and creates new Ipv6Net. All formats supported by Ipv6Addr.from_string is allowed here. See also Ipv4Net.from_string for additional info about parsing strategy and supported network/prefix variants.

fn Ipv6Net.new #

fn Ipv6Net.new(addr Ipv6Addr, prefix int) !Ipv6Net

Ipv6Net.new creates new IPv6 network from given Ipv6Addr and prefix.

type Eui #

type Eui = Eui48 | Eui64

fn (Eui) str #

fn (eui Eui) str() string

str returns the EUI string representation.

type IpAddr #

type IpAddr = Ipv4Addr | Ipv4Net | Ipv6Addr | Ipv6Net

fn (IpAddr) str #

fn (ip IpAddr) str() string

str returns the IP string representation.

enum Eui48Format #

enum Eui48Format {
	canonical // e.g. 0a-96-7a-87-4a-e3
	unix      // e.g. 0a:96:7a:87:4a:e3
	hextets   // e.g. 0a96.7a87.4ae3
	bare      // e.g. 0a967a874ae3
}

enum Eui64Format #

enum Eui64Format {
	canonical // e.g. 0a-96-7a-ff-fe-87-4a-e3
	unix      // e.g. 0a:96:7a:ff:fe:87:4a:e3
	hextets   // e.g. 0a96.7aff.ffe87.4ae3
	bare      // e.g. 0a967afffe874ae3
}

enum Ipv4NetFormat #

enum Ipv4NetFormat {
	with_prefix_len   // e.g. 198.51.100.0/24
	with_host_mask    // e.g. 198.51.100.0/0.0.0.255
	with_network_mask // e.g. 198.51.100.0/255.255.255.0
}

enum Ipv6AddrFormat #

@[flag]
enum Ipv6AddrFormat {
	compact // e.g. fe80::896:7aff:e87:4ae3
	verbose // e.g. fe80:0000:0000:0000:0896:7aff:0e87:4ae3
	dotted  // use dotted-decimal notation for IPv4-mapped and IPv4-compat addresses e.g. ::ffff:192.168.3.11
}

enum Ipv6NetFormat #

@[flag]
enum Ipv6NetFormat {
	compact
	verbose
	dotted
	with_prefix_len
	with_host_mask
	with_network_mask
}

enum Ipv6WithEmbeddedIpv4 #

enum Ipv6WithEmbeddedIpv4 {
	mapped // e.g. ::ffff:203.0.113.90
	compat // e.g. ::203.0.113.90, deprecated per RFC 4291 Section 4
}

See RFC 4291 Section 2.5.5.

struct Eui48 #

struct Eui48 {
	addr [6]u8
}

fn (Eui48) str #

fn (e Eui48) str() string

str returns EUI-48 string representation in canonical format.

fn (Eui48) format #

fn (e Eui48) format(fmt Eui48Format) string

format returns the MAC address as a string formatted according to the fmt rule.

fn (Eui48) u8_array #

fn (e Eui48) u8_array() []u8

u8_array returns EUI-48 as byte array.

fn (Eui48) u8_array_fixed #

fn (e Eui48) u8_array_fixed() [6]u8

u8_array_fixed returns EUI-48 as fixed size byte array.

fn (Eui48) bit_len #

fn (e Eui48) bit_len() int

bit_len returns number of bits required to represent the current EUI-48.

fn (Eui48) oui_bytes #

fn (e Eui48) oui_bytes() [3]u8

oui_bytes returns the 24 bit Organizationally Unique Identifier (OUI) as byte array.

fn (Eui48) ei_bytes #

fn (e Eui48) ei_bytes() [3]u8

ei_bytes returns the 24 bit Extended Identifier (EI) as byte array.

fn (Eui48) eui64 #

fn (e Eui48) eui64() Eui64

eui64 returns the EUI-64 converted from EUI-48 via extending address with FF-FE bytes.

fn (Eui48) modified_eui64 #

fn (e Eui48) modified_eui64() Eui64

modified_eui64 converts the EUI-48 to Modified EUI-64. This is the same as eui64(), but the U/L-bit (universal/local bit) is inverted.

fn (Eui48) ipv6 #

fn (e Eui48) ipv6(prefix Ipv6Addr) !Ipv6Addr

ipv6 creates new IPv6 address from EUI-48. EUI-48 will be converted to Modified EUI-64 and appended to network prefix. Byte-reversed prefix must fit in 64 bit.

fn (Eui48) is_universal #

fn (e Eui48) is_universal() bool

is_universal returns true if address is universally administreted.

fn (Eui48) is_local #

fn (e Eui48) is_local() bool

is_local returns true if address is locally administreted.

fn (Eui48) is_multicast #

fn (e Eui48) is_multicast() bool

is_multicast returns true if address is multicast.

fn (Eui48) is_unicast #

fn (e Eui48) is_unicast() bool

is_unicast returns true if address is unicast.

fn (Eui48) == #

fn (a Eui48) == (b Eui48) bool

== returns true if a is equals b.

struct Eui48RandomParams #

@[params]
struct Eui48RandomParams {
pub:
	oui  ?[3]u8 // the custom OUI which is used instead of the random one.
	seed []u32  // seed for PRNG
	prng rand.PRNG = wyrand.WyRandRNG{}
}

struct Eui64 #

struct Eui64 {
	addr [8]u8
}

fn (Eui64) str #

fn (e Eui64) str() string

str returns EUI-64 string representation in canonical format.

fn (Eui64) format #

fn (e Eui64) format(fmt Eui64Format) string

format returns the EUI-64 as a string formatted according to the fmt rule.

fn (Eui64) u8_array #

fn (e Eui64) u8_array() []u8

u8_array returns EUI-64 as byte array.

fn (Eui64) u8_array_fixed #

fn (e Eui64) u8_array_fixed() [8]u8

u8_array_fixed returns EUI-64 as fixed size byte array.

fn (Eui64) bit_len #

fn (e Eui64) bit_len() int

bit_len returns number of bits required to represent the current EUI-64.

fn (Eui64) oui_bytes #

fn (e Eui64) oui_bytes() [3]u8

oui_bytes returns the 24 bit Organizationally Unique Identifier (OUI) as byte array.

fn (Eui64) ei_bytes #

fn (e Eui64) ei_bytes() [5]u8

ei_bytes returns the 40 bit Extended Identifier (EI) as byte array.

fn (Eui64) modified_eui64 #

fn (e Eui64) modified_eui64() Eui64

modified_eui64 returns the Modified EUI-64 Format Interface Identifier per RFC 4291 (Appendix A).

fn (Eui64) ipv6 #

fn (e Eui64) ipv6(prefix Ipv6Addr) !Ipv6Addr

ipv6 creates new IPv6 address from Modified EUI-64. Byte-reversed prefix must fit in 64 bit.

Example

pref := netaddr.Ipv6Net.from_string('2001:0db8:ef01:2345::/64')!
eui := netaddr.Eui64.from_string('aa-bb-cc-dd-ee-ff-00-11')!
ip6 := eui.ipv6(pref.network_address)!
println(ip6) // 2001:0db8:ef01:2345:a8bb:ccdd:eeff:11

fn (Eui64) is_universal #

fn (e Eui64) is_universal() bool

is_universal returns true if address is universally administreted.

fn (Eui64) is_local #

fn (e Eui64) is_local() bool

is_local returns true if address is locally administreted.

fn (Eui64) is_multicast #

fn (e Eui64) is_multicast() bool

is_multicast returns true if address is multicast.

fn (Eui64) is_unicast #

fn (e Eui64) is_unicast() bool

is_unicast returns true if address is unicast.

fn (Eui64) == #

fn (a Eui64) == (b Eui64) bool

== returns true if a is equals b.

struct Ipv4Addr #

struct Ipv4Addr {
	addr [4]u8
}

fn (Ipv4Addr) str #

fn (a Ipv4Addr) str() string

str returns string representation of IP address.

fn (Ipv4Addr) u32 #

fn (a Ipv4Addr) u32() u32

u32 returns IP address represented as unsigned 32 bit integer.

fn (Ipv4Addr) u8_array #

fn (a Ipv4Addr) u8_array() []u8

u8_array returns IP address represented as byte array.

fn (Ipv4Addr) u8_array_fixed #

fn (a Ipv4Addr) u8_array_fixed() [4]u8

u8_array_fixed returns IP address represented as fixed size byte array.

fn (Ipv4Addr) ipv6 #

fn (a Ipv4Addr) ipv6(params Ipv4ToIpv6Params) Ipv6Addr

ipv6 returns IPv4-mapped or IPv4-compatible IPv6 address per RFC 4291. By default returns the IPv4-mapped IPv6 address e.g. ::ffff:203.0.113.90.

fn (Ipv4Addr) bit_len #

fn (a Ipv4Addr) bit_len() int

bit_len returns number of bits required to represent IP address.

Example

assert netaddr.Ipv4Addr.new(0, 0, 255, 255).bit_len() == 16

fn (Ipv4Addr) family #

fn (a Ipv4Addr) family() net.AddrFamily

family returns the net.AddrFamily member corresponding to IP version.

fn (Ipv4Addr) reverse_pointer #

fn (a Ipv4Addr) reverse_pointer() string

reverse_pointer returns reverse DNS record for the IP address in .in-addr.arpa zone.

fn (Ipv4Addr) is_loopback #

fn (a Ipv4Addr) is_loopback() bool

is_loopback returns true if this is a loopback address.

fn (Ipv4Addr) is_multicast #

fn (a Ipv4Addr) is_multicast() bool

is_multicast returns true if the address is reserved for multicast use.

fn (Ipv4Addr) is_unicast #

fn (a Ipv4Addr) is_unicast() bool

is_unicast returns true if the address is unicast.

fn (Ipv4Addr) is_shared #

fn (a Ipv4Addr) is_shared() bool

is_shared returns true if the address is allocated in shared address space. See RFC 6598. Addresses from network 100.64.0.0/10 is both not "private" and not "global", so is_private and is_global methods returns false for it.

fn (Ipv4Addr) is_private #

fn (a Ipv4Addr) is_private() bool

is_private returns true if the address is not globally reachable.

fn (Ipv4Addr) is_global #

fn (a Ipv4Addr) is_global() bool

is_global return true if the address is globally reachable.

fn (Ipv4Addr) is_reserved #

fn (a Ipv4Addr) is_reserved() bool

is_reserved returns true if the address is IETF reserved.

fn (Ipv4Addr) is_unspecified #

fn (a Ipv4Addr) is_unspecified() bool

is_unspecified returns true if the address is unspecified i.e. equals 0.0.0.0.

fn (Ipv4Addr) is_netmask #

fn (a Ipv4Addr) is_netmask() bool

is_netmask returns true if IP address is network mask.

fn (Ipv4Addr) is_hostmask #

fn (a Ipv4Addr) is_hostmask() bool

is_hostmask returns true if IP address is host mask.

fn (Ipv4Addr) < #

fn (a Ipv4Addr) < (b Ipv4Addr) bool

< returns true if a is lesser than b.

fn (Ipv4Addr) == #

fn (a Ipv4Addr) == (b Ipv4Addr) bool

== returns true if a equals b.

struct Ipv4Net #

struct Ipv4Net {
pub:
	network_address   Ipv4Addr
	network_mask      Ipv4Addr
	host_mask         Ipv4Addr
	broadcast_address Ipv4Addr
	host_address      ?Ipv4Addr
	prefix_len        int
mut:
	current u32
}

fn (Ipv4Net) str #

fn (n Ipv4Net) str() string

str returns string representation of IPv4 network in CIDR format.

fn (Ipv4Net) format #

fn (n Ipv4Net) format(fmt Ipv4NetFormat) string

format returns the IPv4 network as a string formatted according to the fmt rule.

fn (Ipv4Net) capacity #

fn (n Ipv4Net) capacity() u64

capacity returns a total number of addresses in the network.

fn (Ipv4Net) next #

fn (mut n Ipv4Net) next() ?Ipv4Addr

next implements an iterator that iterates over all addresses in network including network and broadcast addresses.

Example

network := netaddr.Ipv4Net.from_string('10.0.10.2/29')!
for addr in network {
    println(addr)
}

fn (Ipv4Net) first #

fn (n Ipv4Net) first() Ipv4Addr

first returns the first usable host address in network.

fn (Ipv4Net) last #

fn (n Ipv4Net) last() Ipv4Addr

last returns the last usable host address in network.

fn (Ipv4Net) nth #

fn (n Ipv4Net) nth(num i64) !Ipv4Addr

nth returns the Nth address in network. Supports negative indexes.

fn (Ipv4Net) contains #

fn (n Ipv4Net) contains(addr Ipv4Addr) bool

contains returns true if IP address is in the network.

fn (Ipv4Net) overlaps #

fn (n Ipv4Net) overlaps(other Ipv4Net) bool

overlaps returns true if network partly contains in other, in other words if the networks addresses sets intersect.

fn (Ipv4Net) subnets #

fn (n Ipv4Net) subnets(prefix int) !Ipv4NetsIterator

subnets returns iterator that iterates over the network subnets partitioned by given prefix length.

Example

network := netaddr.Ipv4Net.from_string('198.51.100.0/24')!
subnets := network.subnets(26)!
for subnet in subnets {
	println(subnet)
}

fn (Ipv4Net) supernet #

fn (n Ipv4Net) supernet(prefix int) !Ipv4Net

supernet returns IPv4 network containing the current network.

fn (Ipv4Net) is_subnet_of #

fn (n Ipv4Net) is_subnet_of(other Ipv4Net) bool

is_subnet_of returns true if other contains the network.

fn (Ipv4Net) is_supernet_of #

fn (n Ipv4Net) is_supernet_of(other Ipv4Net) bool

is_supernet_of returns true if the network contains other.

fn (Ipv4Net) is_loopback #

fn (n Ipv4Net) is_loopback() bool

is_loopback returns true if this is a loopback network.

fn (Ipv4Net) is_multicast #

fn (n Ipv4Net) is_multicast() bool

is_multicast returns true if the network is reserved for multicast use.

fn (Ipv4Net) is_unicast #

fn (n Ipv4Net) is_unicast() bool

is_unicast returns true if the network is unicast.

fn (Ipv4Net) is_shared #

fn (n Ipv4Net) is_shared() bool

is_shared returns true if the network is in shared address space.

fn (Ipv4Net) is_private #

fn (n Ipv4Net) is_private() bool

is_private returns true if the network is not globally reachable.

fn (Ipv4Net) is_global #

fn (n Ipv4Net) is_global() bool

is_global return true if the network is globally reachable.

fn (Ipv4Net) is_reserved #

fn (n Ipv4Net) is_reserved() bool

is_reserved returns true if the network is IETF reserved.

fn (Ipv4Net) is_unspecified #

fn (n Ipv4Net) is_unspecified() bool

is_unspecified returns true if the network is 0.0.0.0/32.

fn (Ipv4Net) < #

fn (n Ipv4Net) < (other Ipv4Net) bool

< returns true if the network is lesser than other network.

fn (Ipv4Net) == #

fn (n Ipv4Net) == (other Ipv4Net) bool

== returns true if networks equals.

struct Ipv4NetsIterator #

struct Ipv4NetsIterator {
	prefix_len int
	step       u32
	end        u32
mut:
	current u32
}

fn (Ipv4NetsIterator) next #

fn (mut iter Ipv4NetsIterator) next() ?Ipv4Net

next implements the iterator interface for IP network subnets.

struct Ipv4ToIpv6Params #

@[params]
struct Ipv4ToIpv6Params {
pub:
	kind Ipv6WithEmbeddedIpv4 = .mapped
}

struct Ipv6Addr #

struct Ipv6Addr {
	addr [16]u8
pub:
	zone_id ?string // the IPv6 scope zone identifier per RFC 4007
}

fn (Ipv6Addr) str #

fn (a Ipv6Addr) str() string

str returns string representation of IPv6 address in compact format.

fn (Ipv6Addr) format #

fn (a Ipv6Addr) format(fmt Ipv6AddrFormat) string

format returns the IPv6 address as a string formatted according to the fmt rule.

fn (Ipv6Addr) bigint #

fn (a Ipv6Addr) bigint() big.Integer

bigint returns IP address represented as big.Integer.

fn (Ipv6Addr) u8_array #

fn (a Ipv6Addr) u8_array() []u8

u8_array returns IP address represented as byte array.

fn (Ipv6Addr) u8_array_fixed #

fn (a Ipv6Addr) u8_array_fixed() [16]u8

u8_array_fixed returns IP address represented as fixed size byte array.

fn (Ipv6Addr) segments #

fn (a Ipv6Addr) segments() [8]u16

segments returns an array of eight 16-bit IP address segments.

fn (Ipv6Addr) with_scope #

fn (a Ipv6Addr) with_scope(zone_id string) !Ipv6Addr

with_scope returns IPv6 address with new zone_id.

Note: with_scope creates new Ipv6Addr, does not change the current.

fn (Ipv6Addr) ipv4 #

fn (a Ipv6Addr) ipv4() !Ipv4Addr

ipv4 returns IPv4 address converted from IPv4-mapped or IPv4-compatible IPv6 address.

Note: this function does not treat :: and ::1 addresses as IPv4-compatible ones.

fn (Ipv6Addr) six_to_four #

fn (a Ipv6Addr) six_to_four() !Ipv4Addr

six_to_four returns embedded IPv4 address if the IPv6 address is 6to4. See RFC 3056.

fn (Ipv6Addr) teredo #

fn (a Ipv6Addr) teredo() !TeredoAddr

teredo returns embedded Teredo address. See RFC 4380 and https://en.wikipedia.org/wiki/Teredo_tunneling

fn (Ipv6Addr) bit_len #

fn (a Ipv6Addr) bit_len() int

bit_len returns number of bits required to represent IP address.

fn (Ipv6Addr) family #

fn (a Ipv6Addr) family() net.AddrFamily

family returns the net.AddrFamily member corresponding to IP version.

fn (Ipv6Addr) reverse_pointer #

fn (a Ipv6Addr) reverse_pointer() string

reverse_pointer returns a reverse DNS pointer name for IPv6 address.

fn (Ipv6Addr) is_ipv4_mapped #

fn (a Ipv6Addr) is_ipv4_mapped() bool

is_ipv4_mapped returns true if IPv6 address is IPv4-mapped.

fn (Ipv6Addr) is_ipv4_compat #

fn (a Ipv6Addr) is_ipv4_compat() bool

is_ipv4_compat returns true if IPv6 address is IPv4-compatible.

Note: loopback and unspecified addresses (::1 and :: respectively) are not recognized as IPv4-compatible addresses.

fn (Ipv6Addr) is_site_local #

fn (a Ipv6Addr) is_site_local() bool

is_site_local returns true if the address is reserved for site local usage. See RFC 3879.

fn (Ipv6Addr) is_unique_local #

fn (a Ipv6Addr) is_unique_local() bool

is_unique_local returns true if the address is unique local. See RFC 4193, RFC 8190.

fn (Ipv6Addr) is_loopback #

fn (a Ipv6Addr) is_loopback() bool

is_loopback returns true if the address is loopback i.e equals ::1.

fn (Ipv6Addr) is_multicast #

fn (a Ipv6Addr) is_multicast() bool

is_multicast returns true if the address is reserved for multicast use.

fn (Ipv6Addr) is_unicast #

fn (a Ipv6Addr) is_unicast() bool

is_unicast returns true if the address is unicast.

fn (Ipv6Addr) is_private #

fn (a Ipv6Addr) is_private() bool

is_private returns true if the address is not globally reachable.

fn (Ipv6Addr) is_global #

fn (a Ipv6Addr) is_global() bool

is_global return true if the address is globally reachable.

fn (Ipv6Addr) is_reserved #

fn (a Ipv6Addr) is_reserved() bool

is_reserved returns true if the address is allocated in reserved networks.

fn (Ipv6Addr) is_unspecified #

fn (a Ipv6Addr) is_unspecified() bool

is_unspecified returns true if IP address is unspecified i.e equals ::.

fn (Ipv6Addr) is_netmask #

fn (a Ipv6Addr) is_netmask() bool

is_netmask returns true if IP address is network mask.

fn (Ipv6Addr) is_hostmask #

fn (a Ipv6Addr) is_hostmask() bool

is_hostmask returns true if IP address is host mask.

fn (Ipv6Addr) < #

fn (a Ipv6Addr) < (b Ipv6Addr) bool

< returns true if a is lesser than b.

fn (Ipv6Addr) == #

fn (a Ipv6Addr) == (b Ipv6Addr) bool

== returns true if a equals b.

struct Ipv6AddrParams #

@[params]
struct Ipv6AddrParams {
pub:
	zone_id ?string
}

struct Ipv6Net #

struct Ipv6Net {
pub:
	network_address   Ipv6Addr
	network_mask      Ipv6Addr
	host_mask         Ipv6Addr
	broadcast_address Ipv6Addr
	host_address      ?Ipv6Addr
	prefix_len        int
mut:
	current big.Integer
}

fn (Ipv6Net) str #

fn (n Ipv6Net) str() string

str returns string representation of IPv6 network in CIDR format.

fn (Ipv6Net) format #

fn (n Ipv6Net) format(fmt Ipv6NetFormat) string

format returns the IPv6 network as a string formatted according to the fmt rule.

fn (Ipv6Net) capacity #

fn (n Ipv6Net) capacity() big.Integer

capacity returns a total number of addresses in the network.

fn (Ipv6Net) next #

fn (mut n Ipv6Net) next() ?Ipv6Addr

next implements an iterator that iterates over all addresses in network including network and broadcast addresses.

Example

network := netaddr.Ipv6Net.from_string('fe80::/124')!
for addr in network {
    println(addr)
}

fn (Ipv6Net) first #

fn (n Ipv6Net) first() Ipv6Addr

first returns the first usable host address in network.

fn (Ipv6Net) last #

fn (n Ipv6Net) last() Ipv6Addr

last returns the last usable host address in network.

fn (Ipv6Net) nth #

fn (n Ipv6Net) nth(num big.Integer) !Ipv6Addr

nth returns the Nth address in network. Supports negative indexes.

fn (Ipv6Net) contains #

fn (n Ipv6Net) contains(addr Ipv6Addr) bool

contains returns true if IP address is in the network.

fn (Ipv6Net) overlaps #

fn (n Ipv6Net) overlaps(other Ipv6Net) bool

overlaps returns true if network partly contains in other, in other words if the networks addresses sets intersect.

fn (Ipv6Net) subnets #

fn (n Ipv6Net) subnets(prefix int) !Ipv6NetsIterator

subnets returns iterator that iterates over the network subnets partitioned by given prefix length.

Example

network := netaddr.Ipv6Net.from_string('2001:db8:beaf::/56')!
subnets := network.subnets(64)!
for subnet in subnets {
 println(subnet)
}

fn (Ipv6Net) supernet #

fn (n Ipv6Net) supernet(prefix int) !Ipv6Net

supernet returns IPv6 network containing the current network.

fn (Ipv6Net) is_subnet_of #

fn (n Ipv6Net) is_subnet_of(other Ipv6Net) bool

is_subnet_of returns true if other contains the network.

fn (Ipv6Net) is_supernet_of #

fn (n Ipv6Net) is_supernet_of(other Ipv6Net) bool

is_supernet_of returns true if the network contains other.

fn (Ipv6Net) is_site_local #

fn (n Ipv6Net) is_site_local() bool

is_site_local returns true if the network is site-local.

fn (Ipv6Net) is_unique_local #

fn (n Ipv6Net) is_unique_local() bool

is_unique_local returns true if the network is unique-local.

fn (Ipv6Net) is_loopback #

fn (n Ipv6Net) is_loopback() bool

is_loopback returns true if this is a loopback network.

fn (Ipv6Net) is_multicast #

fn (n Ipv6Net) is_multicast() bool

is_multicast returns true if the network is reserved for multicast use.

fn (Ipv6Net) is_unicast #

fn (n Ipv6Net) is_unicast() bool

is_unicast returns true if the network is unicast.

fn (Ipv6Net) is_private #

fn (n Ipv6Net) is_private() bool

is_private returns true if the network is not globally reachable.

fn (Ipv6Net) is_global #

fn (n Ipv6Net) is_global() bool

is_global return true if the network is globally reachable.

fn (Ipv6Net) is_reserved #

fn (n Ipv6Net) is_reserved() bool

is_reserved returns true if the network is reserved.

fn (Ipv6Net) is_unspecified #

fn (n Ipv6Net) is_unspecified() bool

is_unspecified returns true if the network is ::/0.

fn (Ipv6Net) < #

fn (n Ipv6Net) < (other Ipv6Net) bool

< returns true if the network is lesser than other network.

fn (Ipv6Net) == #

fn (n Ipv6Net) == (other Ipv6Net) bool

== returns true if networks equals.

struct Ipv6NetsIterator #

struct Ipv6NetsIterator {
	prefix_len int
	step       big.Integer
	end        big.Integer
mut:
	current big.Integer
}

fn (Ipv6NetsIterator) next #

fn (mut iter Ipv6NetsIterator) next() ?Ipv6Net

next implements the iterator interface for IP network subnets.

struct TeredoAddr #

struct TeredoAddr {
pub:
	server Ipv4Addr
	flags  u16
	port   u16
	client Ipv4Addr
}

TeredoAddr represents the parsed Teredo address. See RFC 4380 Section 4.

fn (TeredoAddr) ipv6 #

fn (t TeredoAddr) ipv6() Ipv6Addr

ipv6 returns Ipv6Addr created from Teredo address.