IMPORTANT: To view this page as Markdown, append `.md` to the URL (e.g. /docs/manual/basics.md). For the complete Mojo documentation index, see llms.txt.
Skip to main content
Version: Nightly
For the complete Mojo documentation index, see llms.txt. Markdown versions of all pages are available by appending .md to any URL (e.g. /docs/manual/basics.md).

Ipv4Addr

struct Ipv4Addr

Represents an IPv4 address.

The address is stored as four octets in network byte order.

Implemented traits​

AnyType, Copyable, Deinitable, Equatable, ImplicitlyCopyable, Movable, Writable

comptime members​

broadcast​

comptime broadcast = Ipv4Addr(UInt8(255), UInt8(255), UInt8(255), UInt8(255))

The limited broadcast address, 255.255.255.255.

localhost​

comptime localhost = Ipv4Addr(UInt8(127), UInt8(0), UInt8(0), UInt8(1))

The loopback address, 127.0.0.1.

unspecified​

comptime unspecified = Ipv4Addr(UInt8(0), UInt8(0), UInt8(0), UInt8(0))

The unspecified address, 0.0.0.0.

Methods​

__init__​

def __init__(out self, a: UInt8, b: UInt8, c: UInt8, d: UInt8, /)

Constructs an address from its four octets, first octet first.

Args:

  • ​a (UInt8): The first octet.
  • ​b (UInt8): The second octet.
  • ​c (UInt8): The third octet.
  • ​d (UInt8): The fourth octet.

def __init__(out self, *, bits: UInt32)

Constructs an address from its 32-bit numeric value.

The most significant byte of bits becomes the first octet, so Ipv4Addr(bits=0x7F000001) is 127.0.0.1. A value that is already in network byte order, such as C's in_addr.s_addr, needs UInt32.from_endian[.big]() first.

Args:

  • ​bits (UInt32): The address as an integer in host byte order.

def __init__(out self, *, var octets: Array[UInt8, Int(4)])

Constructs an address from its octets in network byte order.

Args:

def __init__(out self, *, copy: Self)

Copies an address.

Args:

  • ​copy (Self): The address to copy.

bits​

def bits(self) -> UInt32

Returns the address as a 32-bit integer.

This is the inverse of the bits initializer: the first octet becomes the most significant byte.

Returns:

UInt32: The address as an integer in host byte order.

write_to​

def write_to(self, mut writer: T)

Writes the address in dotted-decimal notation, such as 127.0.0.1.

Args:

  • ​writer (T): The writer to write to.