ipaddr

A library for manipulation of IP (IPv4/IPv6) and MAC address representations

ip
ipv4
ipv6
mac
address
network
cidr
moon add bobzhang/ipaddr@0.1.1
Download zip
Author
Version
0.1.1
License
ISC
Last updated
3 months ago
Downloads
28
README

#ipaddr: IP and MAC address manipulation for MoonBit

A MoonBit library for manipulation of IP (IPv4/IPv6) and MAC address representations, ported from the OCaml ipaddr library.

#Features

  • IPv4 and IPv6 address support
  • IPv4 and IPv6 CIDR prefix support
  • MAC-48 (Ethernet) address support
  • Address parsing from strings
  • Address formatting to strings
  • Address comparison and ordering
  • Subnet operations and containment checks
  • Comprehensive test suite

#Usage

///|
test "readme_examples" {
// IPv4 addresses
let addr = @ipaddr.ipv4(192, 168, 1, 1)
let formatted = addr.format()
if formatted != "192.168.1.1" {
fail("IPv4 formatting failed")
}

// IPv4 CIDR prefixes
let prefix = @ipaddr.ipv4_prefix(@ipaddr.ipv4(192, 168, 1, 0), 24)
let contains = prefix.contains(addr)
if !contains {
fail("Address should be contained in prefix")
}

// MAC addresses
let mac_addr = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let formatted_mac = mac_addr.format()
if formatted_mac != "00:11:22:33:44:55" {
fail("MAC formatting failed")
}
}

#License

ISC License (same as the original OCaml library)

#
IpAddrError

pub enum IpAddrError {
InvalidFormat
InvalidOctet(Int)
} derive(Eq, Hash,
Debug
)

Error types for IP address parsing and validation
impl Show for IpAddrError

#
Ipv4

pub struct Ipv4(Byte, Byte, Byte, Byte) derive(Compare, Eq, Hash,
Debug
)

impl Show for Ipv4

#
Ipv4::format

fn Ipv4::format(self : Ipv4) -> String

Formats the IPv4 address as a dotted decimal string.

Returns

A string representation in the format "a.b.c.d"

Examples

let addr = @ipaddr.ipv4(192, 168, 1, 1)
let _ = addr.format() // "192.168.1.1"

let localhost = @ipaddr.ipv4(127, 0, 0, 1)
let _ = localhost.format() // "127.0.0.1"

#
Ipv4::from_int

fn Ipv4::from_int(n : Int) -> Ipv4

Creates an IPv4 address from a 32-bit integer in network byte order.

The conversion follows the standard network byte order (big-endian):
  • Most significant byte becomes the first octet
  • Least significant byte becomes the fourth octet

Parameters

  • n: A 32-bit integer representing the IPv4 address

Returns

An IPv4 address constructed from the integer

Examples

let addr = @ipaddr.Ipv4::from_int(0x7F000001) // 127.0.0.1
let _ = addr.format() // "127.0.0.1"

let localhost = @ipaddr.Ipv4::from_int(0x7F000001) // 127.0.0.1
let _ = localhost.format() // "127.0.0.1"

let _ = @ipaddr.Ipv4::from_int(0) // 0.0.0.0

#
Ipv4::is_broadcast

fn Ipv4::is_broadcast(self : Ipv4) -> Bool

Checks if the IPv4 address is the limited broadcast address.

The limited broadcast address is 255.255.255.255, which is used to send packets to all hosts on the local network segment.

Returns

true if the address is 255.255.255.255, false otherwise

Examples

let broadcast = @ipaddr.ipv4(255, 255, 255, 255)
let _ = broadcast.is_broadcast() // true

let not_broadcast = @ipaddr.ipv4(192, 168, 1, 255)
let _ = not_broadcast.is_broadcast() // false

let almost_broadcast = @ipaddr.ipv4(255, 255, 255, 254)
let _ = almost_broadcast.is_broadcast() // false

#
Ipv4::is_loopback

fn Ipv4::is_loopback(self : Ipv4) -> Bool

Checks if the IPv4 address is a loopback address.

Loopback addresses are in the range 127.0.0.0/8 (127.0.0.0 to 127.255.255.255). These addresses are used to refer to the local host.

Returns

true if the address is a loopback address, false otherwise

Examples

let localhost = @ipaddr.ipv4(127, 0, 0, 1)
let _ = localhost.is_loopback() // true

let loopback_variant = @ipaddr.ipv4(127, 1, 2, 3)
let _ = loopback_variant.is_loopback() // true

let not_loopback = @ipaddr.ipv4(192, 168, 1, 1)
let _ = not_loopback.is_loopback() // false

#
Ipv4::is_multicast

fn Ipv4::is_multicast(self : Ipv4) -> Bool

Checks if the IPv4 address is a multicast address.

Multicast addresses are in the range 224.0.0.0/4 (224.0.0.0 to 239.255.255.255). These addresses are used for multicast communication where data is sent to multiple recipients simultaneously.

Returns

true if the address is a multicast address, false otherwise

Examples

let multicast1 = @ipaddr.ipv4(224, 0, 0, 1)
let _ = multicast1.is_multicast() // true

let multicast2 = @ipaddr.ipv4(239, 255, 255, 255)
let _ = multicast2.is_multicast() // true

let unicast = @ipaddr.ipv4(192, 168, 1, 1)
let _ = unicast.is_multicast() // false

#
Ipv4::is_private

fn Ipv4::is_private(self : Ipv4) -> Bool

Checks if the IPv4 address is a private address as defined by RFC 1918.

Private address ranges are:
  • 10.0.0.0/8 (10.0.0.0 to 10.255.255.255)
  • 172.16.0.0/12 (172.16.0.0 to 172.31.255.255)
  • 192.168.0.0/16 (192.168.0.0 to 192.168.255.255)

Returns

true if the address is in a private range, false otherwise

Examples

let private1 = @ipaddr.ipv4(10, 0, 0, 1)
let _ = private1.is_private() // true

let private2 = @ipaddr.ipv4(172, 16, 0, 1)
let _ = private2.is_private() // true

let private3 = @ipaddr.ipv4(192, 168, 1, 1)
let _ = private3.is_private() // true

let public_addr = @ipaddr.ipv4(8, 8, 8, 8)
let _ = public_addr.is_private() // false

#
Ipv4::is_valid

fn Ipv4::is_valid(self : Ipv4) -> Bool

#
Ipv4::new

fn Ipv4::new(a : Byte, b : Byte, c : Byte, d : Byte) -> Ipv4

Creates a new IPv4 address from four octets (OO-style constructor).

Parameters

  • a: First octet (0-255)
  • b: Second octet (0-255)
  • c: Third octet (0-255)
  • d: Fourth octet (0-255)

Examples

let _ = @ipaddr.Ipv4::new(127, 0, 0, 1)
let _ = @ipaddr.Ipv4::new(192, 168, 1, 100)
let _ = @ipaddr.Ipv4::new(8, 8, 8, 8)

Note

This function does not validate that the octets are in the valid range (0-255). Use Ipv4::is_valid() to check if the resulting address is valid.

#
Ipv4::parse

fn Ipv4::parse(s : String) -> Ipv4?

#
Ipv4::parse_detailed

fn Ipv4::parse_detailed(s : String) -> Result[Ipv4, IpAddrError]

Parses an IPv4 address from a dotted decimal string with detailed error information.

This is an enhanced version of parse that provides specific error details instead of just returning None on failure.

Parameters

  • s: A string in the format "a.b.c.d" where each component is 0-255

Returns

Ok(Ipv4) if parsing succeeds, Err(IpAddrError) with specific error details

Examples

let _ = @ipaddr.Ipv4::parse_detailed("192.168.1.1")
let _ = @ipaddr.Ipv4::parse_detailed("256.1.1.1") // Err(InvalidOctet(256))
let _ = @ipaddr.Ipv4::parse_detailed("192.168.1") // Err(InvalidFormat)

#
Ipv4::to_int

fn Ipv4::to_int(self : Ipv4) -> Int

Converts the IPv4 address to a 32-bit integer in network byte order.

The conversion follows the standard network byte order (big-endian):
  • First octet becomes the most significant byte
  • Fourth octet becomes the least significant byte

Returns

A 32-bit integer representation of the IPv4 address

Examples

let addr = @ipaddr.ipv4(192, 168, 1, 1)
let _ = addr.to_int() // 3232235777

let localhost = @ipaddr.ipv4(127, 0, 0, 1)
let _ = localhost.to_int() // 2130706433

let zero = @ipaddr.ipv4(0, 0, 0, 0)
let _ = zero.to_int() // 0

#
Ipv4::try_from_int

fn Ipv4::try_from_int(n : Int) -> Ipv4?

Creates an IPv4 address from a 32-bit integer with validation.

This is a safe version of from_int that validates the input range. Unlike from_int, this function returns None for negative values or values that exceed the valid 32-bit unsigned range.

Parameters

  • n: A 32-bit integer (0 to 4294967295)

Returns

Some(Ipv4) if the integer is valid, None otherwise

Examples

let _ = @ipaddr.Ipv4::try_from_int(0x7F000001) // Some(127.0.0.1)
let _ = @ipaddr.Ipv4::try_from_int(0) // Some(0.0.0.0)
let _ = @ipaddr.Ipv4::try_from_int(-1) // None - negative

#
Ipv4Prefix

pub struct Ipv4Prefix {
addr : Ipv4
prefix_len : Int
} derive(Compare, Eq, Hash,
Debug
)

IPv4 CIDR prefix

impl Show for Ipv4Prefix

#
Ipv4Prefix::broadcast

fn Ipv4Prefix::broadcast(self : Ipv4Prefix) -> Ipv4

Gets the broadcast address for this IPv4 prefix.

The broadcast address is the last address in the subnet, obtained by setting all host bits to one. Packets sent to this address are delivered to all hosts in the subnet.

Returns

The broadcast address as an IPv4 address

Examples

let prefix = @ipaddr.ipv4_prefix(@ipaddr.ipv4(192, 168, 1, 0), 24)
let broadcast = prefix.broadcast() // 192.168.1.255
let _ = broadcast.format() // "192.168.1.255"

let prefix16 = @ipaddr.ipv4_prefix(@ipaddr.ipv4(10, 5, 0, 0), 16)
let _ = prefix16.broadcast() // 10.5.255.255

// Special case: /0 broadcast is 255.255.255.255
let default_route = @ipaddr.ipv4_prefix(@ipaddr.ipv4(0, 0, 0, 0), 0)
let _ = default_route.broadcast() // 255.255.255.255

#
Ipv4Prefix::contains

fn Ipv4Prefix::contains(self : Ipv4Prefix, addr : Ipv4) -> Bool

Checks if an IPv4 address is contained within this CIDR prefix/subnet.

This method determines if the given address belongs to the network defined by this prefix by comparing the network portions of both addresses.

Parameters

  • addr: The IPv4 address to check

Returns

true if the address is within the subnet, false otherwise

Examples

let subnet = @ipaddr.ipv4_prefix(@ipaddr.ipv4(192, 168, 1, 0), 24)

let addr1 = @ipaddr.ipv4(192, 168, 1, 1)
let _ = subnet.contains(addr1) // true

let addr2 = @ipaddr.ipv4(192, 168, 1, 255)
let _ = subnet.contains(addr2) // true

let addr3 = @ipaddr.ipv4(192, 168, 2, 1)
let _ = subnet.contains(addr3) // false

// Special case: /0 contains all addresses
let all_networks = @ipaddr.ipv4_prefix(@ipaddr.ipv4(0, 0, 0, 0), 0)
let _ = all_networks.contains(@ipaddr.ipv4(8, 8, 8, 8)) // true

#
Ipv4Prefix::host_count

fn Ipv4Prefix::host_count(self : Ipv4Prefix) -> Int

Returns the number of host addresses in this subnet.

This includes the network and broadcast addresses. For usable host addresses, subtract 2 (except for /31 and /32 subnets which have special rules).

Returns

The total number of addresses in the subnet

Examples

let subnet = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(192, 168, 1, 0), 24)
let _ = subnet.host_count() // 256 (254 usable)

let host_route = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(10, 0, 0, 1), 32)
let _ = host_route.host_count() // 1

let point_to_point = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(10, 0, 0, 0), 31)
let _ = point_to_point.host_count() // 2

#
Ipv4Prefix::mask

fn Ipv4Prefix::mask(self : Ipv4Prefix) -> Ipv4

Returns the subnet mask for this CIDR prefix.

The subnet mask indicates which bits are used for the network portion of the address. For example, a /24 prefix has a mask of 255.255.255.0.

Returns

An Ipv4 address representing the subnet mask

Examples

let subnet = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(192, 168, 1, 0), 24)
let mask = subnet.mask()
let _ = mask.format() // "255.255.255.0"

let host_route = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(10, 0, 0, 1), 32)
let host_mask = host_route.mask()
let _ = host_mask.format() // "255.255.255.255"

let default_route = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(0, 0, 0, 0), 0)
let default_mask = default_route.mask()
let _ = default_mask.format() // "0.0.0.0"

#
Ipv4Prefix::network

fn Ipv4Prefix::network(self : Ipv4Prefix) -> Ipv4

Gets the network address for this IPv4 prefix.

The network address is the first address in the subnet, obtained by setting all host bits to zero. This is also known as the subnet address.

Returns

The network address as an IPv4 address

Examples

let prefix = @ipaddr.ipv4_prefix(@ipaddr.ipv4(192, 168, 1, 100), 24)
let network = prefix.network() // 192.168.1.0
let _ = network.format() // "192.168.1.0"

let prefix16 = @ipaddr.ipv4_prefix(@ipaddr.ipv4(10, 5, 10, 20), 16)
let _ = prefix16.network() // 10.5.0.0

// Special case: /0 network is 0.0.0.0
let default_route = @ipaddr.ipv4_prefix(@ipaddr.ipv4(8, 8, 8, 8), 0)
let _ = default_route.network() // 0.0.0.0

#
Ipv4Prefix::new

fn Ipv4Prefix::new(addr : Ipv4, prefix_len : Int) -> Ipv4Prefix

Creates a new IPv4 CIDR prefix from an address and prefix length (OO-style constructor).

A CIDR prefix represents a network subnet using an IPv4 address and a prefix length. The prefix length indicates how many bits from the left are used for the network portion.

Parameters

  • addr: The IPv4 address (typically the network address)
  • prefix_len: The prefix length in bits (0-32)

Returns

An Ipv4Prefix representing the network subnet

Examples

// Create a /24 subnet (255.255.255.0 netmask)
let _ = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(192, 168, 1, 0), 24)

// Create a /16 subnet (255.255.0.0 netmask)
let _ = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(10, 0, 0, 0), 16)

// Create a /32 host route (255.255.255.255 netmask)
let _ = @ipaddr.Ipv4Prefix::new(@ipaddr.Ipv4::new(8, 8, 8, 8), 32)

#
Ipv4Prefix::parse

fn Ipv4Prefix::parse(s : String) -> Ipv4Prefix?

Parses an IPv4 CIDR prefix from a string.

Parameters

  • s: A string in the format "a.b.c.d/n" where a.b.c.d is an IPv4 address and n is the prefix length (0-32)

Returns

Some(Ipv4Prefix) if parsing succeeds, None if the format is invalid

Examples

let _ = @ipaddr.Ipv4Prefix::parse("192.168.1.0/24")
let _ = @ipaddr.Ipv4Prefix::parse("10.0.0.0/8")
let _ = @ipaddr.Ipv4Prefix::parse("invalid") // None - wrong format

#
Mac

pub struct Mac(Byte, Byte, Byte, Byte, Byte, Byte) derive(Compare, Eq, Hash,
Debug
)

MAC address (6 bytes)

impl Show for Mac

#
Mac::format

fn Mac::format(self : Mac) -> String

Formats the MAC address as a colon-separated hexadecimal string.

The format follows the standard MAC address notation with lowercase hexadecimal digits separated by colons (e.g., "00:11:22:33:44:55").

Returns

A string representation in the format "xx:xx:xx:xx:xx:xx"

Examples

let addr = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = addr.format() // "00:11:22:33:44:55"

let _ = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
let broadcast = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
let _ = broadcast.format() // "ff:ff:ff:ff:ff:ff"

let zero_mac = @ipaddr.mac(0x00, 0x00, 0x00, 0x00, 0x00, 0x00)
let _ = zero_mac.format() // "00:00:00:00:00:00"

#
Mac::from_int

fn Mac::from_int(n : Int) -> Mac

Creates a MAC address from a 32-bit integer, setting only the lower 4 bytes.

This conversion creates a MAC address where the first two bytes (b0, b1) are set to zero, and the remaining four bytes are derived from the 32-bit integer. This is useful for creating MAC addresses from simple integer identifiers.

Parameters

  • n: A 32-bit integer to convert to MAC address

Returns

A MAC address with the first two bytes as 0x00 and the last four bytes from the integer

Examples

let mac_from_1 = @ipaddr.Mac::from_int(1) // 00:00:00:00:00:01
let _ = mac_from_1.format() // "00:00:00:00:00:01"

let mac_from_large = @ipaddr.Mac::from_int(0x12345678) // 00:00:12:34:56:78
let _ = mac_from_large.format() // "00:00:12:34:56:78"

// Round trip conversion
let original = @ipaddr.mac(0x00, 0x00, 0x22, 0x33, 0x44, 0x55)
let int_val = original.to_int()
let _ = @ipaddr.Mac::from_int(int_val) // Same as original

#
Mac::is_broadcast

fn Mac::is_broadcast(self : Mac) -> Bool

Checks if the MAC address is the broadcast address.

The broadcast MAC address is ff:ff:ff:ff:ff:ff, which is used to send frames to all devices on the local network segment.

Returns

true if the address is ff:ff:ff:ff:ff:ff, false otherwise

Examples

let broadcast = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
let _ = broadcast.is_broadcast() // true

let normal_mac = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = normal_mac.is_broadcast() // false

let almost_broadcast = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE)
let _ = almost_broadcast.is_broadcast() // false

#
Mac::is_global

fn Mac::is_global(self : Mac) -> Bool

Checks if the MAC address is globally unique.

A MAC address is globally unique if the second least significant bit of the first octet is set to 0. Globally unique addresses are assigned by the manufacturer and should be unique worldwide.

Returns

true if the address is globally unique, false otherwise

Examples

// Globally unique address (second bit of 0x00 is 0)
let global_mac = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = global_mac.is_global() // true

// Another globally unique address (0x01 = 0b00000001, second bit is 0)
let global_mac2 = @ipaddr.mac(0x01, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = global_mac2.is_global() // true

// Locally administered address (second bit of 0x02 is 1)
let local_mac = @ipaddr.mac(0x02, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = local_mac.is_global() // false

#
Mac::is_local

fn Mac::is_local(self : Mac) -> Bool

Checks if the MAC address is locally administered.

A MAC address is locally administered if the second least significant bit of the first octet is set to 1. Locally administered addresses are assigned by the local network administrator rather than by the manufacturer.

Returns

true if the address is locally administered, false otherwise

Examples

// Locally administered address (second bit of 0x02 is 1)
let local_mac = @ipaddr.mac(0x02, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = local_mac.is_local() // true

// Another locally administered address (0x03 = 0b00000011)
let local_mac2 = @ipaddr.mac(0x03, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = local_mac2.is_local() // true

// Globally unique address (second bit of 0x00 is 0)
let global_mac = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = global_mac.is_local() // false

#
Mac::is_multicast

fn Mac::is_multicast(self : Mac) -> Bool

Checks if the MAC address is a multicast address.

A MAC address is multicast if the least significant bit of the first octet is set to 1. Multicast addresses are used to send frames to a group of devices rather than a single device.

Returns

true if the address is multicast, false otherwise

Examples

// Multicast address (first bit of 0x01 is 1)
let _ = @ipaddr.mac(0x01, 0x00, 0x5E, 0x00, 0x00, 0x01)
let multicast = @ipaddr.mac(0x01, 0x00, 0x5E, 0x00, 0x00, 0x01)
let _ = multicast.is_multicast() // true

// Broadcast is also multicast
let _ = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
let broadcast = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)
let _ = broadcast.is_multicast() // true

// Unicast address (first bit of 0x00 is 0)
let unicast = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = unicast.is_multicast() // false

#
Mac::is_unicast

fn Mac::is_unicast(self : Mac) -> Bool

#
Mac::new

fn Mac::new(b0 : Byte, b1 : Byte, b2 : Byte, b3 : Byte, b4 : Byte, b5 : Byte) -> Mac

Creates a new MAC address from six bytes (OO-style constructor).

MAC (Media Access Control) addresses are 48-bit identifiers used to uniquely identify network interfaces. They are typically displayed in hexadecimal format separated by colons (e.g., "00:11:22:33:44:55").

Parameters

  • b0: First byte (most significant)
  • b1: Second byte
  • b2: Third byte
  • b3: Fourth byte
  • b4: Fifth byte
  • b5: Sixth byte (least significant)

Examples

// Create a typical MAC address
let _ = @ipaddr.Mac::new(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)

// Create a broadcast MAC address
let _ = @ipaddr.Mac::new(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)

// Create a multicast MAC address (first bit of first byte is 1)
let _ = @ipaddr.Mac::new(0x01, 0x00, 0x5E, 0x00, 0x00, 0x01)

#
Mac::oui

fn Mac::oui(self : Mac) -> (Int, Int, Int)

Gets the Organizationally Unique Identifier (OUI) from the MAC address.

The OUI is the first three bytes of a MAC address, assigned by the IEEE to identify the manufacturer or organization. It uniquely identifies the vendor of the network interface.

Returns

A tuple containing the three OUI bytes (b0, b1, b2)

Examples

let addr = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let (_, _, _) = addr.oui() // (0x00, 0x11, 0x22)

// Apple's OUI is 00:17:F2
let apple_mac = @ipaddr.mac(0x00, 0x17, 0xF2, 0x12, 0x34, 0x56)
let (_, _, _) = apple_mac.oui() // (0x00, 0x17, 0xF2)

// Intel's OUI is 00:15:17
let intel_mac = @ipaddr.mac(0x00, 0x15, 0x17, 0xAB, 0xCD, 0xEF)
let (_, _, _) = intel_mac.oui() // (0x00, 0x15, 0x17)

#
Mac::parse

fn Mac::parse(s : String) -> Mac?

Parses a MAC address from a hexadecimal string.

Parameters

  • s: A string in the format "xx:xx:xx:xx:xx:xx" or "xx-xx-xx-xx-xx-xx" where each xx is a hexadecimal byte

Returns

Some(Mac) if parsing succeeds, None if the format is invalid

Examples

let _ = @ipaddr.Mac::parse("00:11:22:33:44:55")
let _ = @ipaddr.Mac::parse("FF-FF-FF-FF-FF-FF")
let _ = @ipaddr.Mac::parse("invalid") // None - wrong format

#
Mac::to_int

fn Mac::to_int(self : Mac) -> Int

Converts the MAC address to a 32-bit integer using the lower 4 bytes.

This conversion uses only the last 4 bytes (b2, b3, b4, b5) of the MAC address for simplicity, as full 48-bit integers are not commonly used. The first two bytes (b0, b1) are ignored in this conversion.

Returns

A 32-bit integer representation of the lower 4 bytes

Examples

let addr = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)
let _ = addr.to_int() // Uses bytes 0x22, 0x33, 0x44, 0x55

let simple_mac = @ipaddr.mac(0x00, 0x00, 0x00, 0x00, 0x00, 0x01)
let _ = simple_mac.to_int() // 1

let zero_mac = @ipaddr.mac(0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00)
let _ = zero_mac.to_int() // 0 (ignores first two 0xFF bytes)

#
Mac::try_from_int

fn Mac::try_from_int(n : Int) -> Mac?

Creates a MAC address from a 48-bit integer with validation.

This is a safe version of from_int that validates the input range. Unlike from_int, this function returns None for negative values or values that exceed the valid 48-bit unsigned range.

Parameters

  • n: A 48-bit integer (0 to 281474976710655)

Returns

Some(Mac) if the integer is valid, None otherwise

Examples

let _ = @ipaddr.Mac::try_from_int(0x22334455) // Some(00:00:22:33:44:55)
let _ = @ipaddr.Mac::try_from_int(0) // Some(00:00:00:00:00:00)
let _ = @ipaddr.Mac::try_from_int(-1) // None - negative

#
ipv4

fn ipv4(a : Byte, b : Byte, c : Byte, d : Byte) -> Ipv4

Creates a new IPv4 address from four octets.

Parameters

  • a: First octet (0-255)
  • b: Second octet (0-255)
  • c: Third octet (0-255)
  • d: Fourth octet (0-255)

Examples

let _ = @ipaddr.ipv4(127, 0, 0, 1)
let _ = @ipaddr.ipv4(192, 168, 1, 100)
let _ = @ipaddr.ipv4(8, 8, 8, 8)

Note

This function does not validate that the octets are in the valid range (0-255). Use Ipv4::is_valid() to check if the resulting address is valid.

#
ipv4_prefix

fn ipv4_prefix(addr : Ipv4, prefix_len : Int) -> Ipv4Prefix

Creates a new IPv4 CIDR prefix from an address and prefix length.

A CIDR prefix represents a network subnet using an IPv4 address and a prefix length. The prefix length indicates how many bits from the left are used for the network portion.

Parameters

  • addr: The IPv4 address (typically the network address)
  • prefix_len: The prefix length in bits (0-32)

Returns

An Ipv4Prefix representing the network subnet

Examples

// Create a /24 subnet (255.255.255.0 netmask)
let _ = @ipaddr.ipv4_prefix(@ipaddr.ipv4(192, 168, 1, 0), 24)

// Create a /16 subnet (255.255.0.0 netmask)
let _ = @ipaddr.ipv4_prefix(@ipaddr.ipv4(10, 0, 0, 0), 16)

// Create a /32 host route (255.255.255.255 netmask)
let _ = @ipaddr.ipv4_prefix(@ipaddr.ipv4(8, 8, 8, 8), 32)

#
mac

fn mac(b0 : Byte, b1 : Byte, b2 : Byte, b3 : Byte, b4 : Byte, b5 : Byte) -> Mac

Creates a new MAC address from six bytes.

MAC (Media Access Control) addresses are 48-bit identifiers used to uniquely identify network interfaces. They are typically displayed in hexadecimal format separated by colons (e.g., "00:11:22:33:44:55").

Parameters

  • b0: First byte (most significant)
  • b1: Second byte
  • b2: Third byte
  • b3: Fourth byte
  • b4: Fifth byte
  • b5: Sixth byte (least significant)

Examples

// Create a typical MAC address
let _ = @ipaddr.mac(0x00, 0x11, 0x22, 0x33, 0x44, 0x55)

// Create a broadcast MAC address
let _ = @ipaddr.mac(0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF)

// Create a multicast MAC address (first bit of first byte is 1)
let _ = @ipaddr.mac(0x01, 0x00, 0x5E, 0x00, 0x00, 0x01)

Powered by MoonBit

Site sourceReport issuePackagesBuild queueSkillsStatistics

© 2026 mooncakes.io