Skip to content
CryoCryo home
Stdlibnet::address

ip

import std::net::address::ip; · source

Types

IpV4Address

type struct IpV4Address {
    a: u8;
    b: u8;
    c: u8;
    d: u8;
}

new(a, b, c, d) builds an address from its octets; localhost(), unspecified(), and broadcast() are the named ones. parse reads dotted-quad text as a Result (failing with a ConversionError), parse_opt as an Option. to_u32 gives the address in host byte order, which is what the raw socket API wants after htonl. Display prints it in dotted decimal.

Properties

PropertyType
au8
bu8
cu8
du8

Constructors

static new(a: u8, b: u8, c: u8, d: u8) -> IpV4Address;

The address a.b.c.d.

static unspecified() -> IpV4Address;

0.0.0.0: "any interface" for a bound listener.

static localhost() -> IpV4Address;

127.0.0.1.

static broadcast() -> IpV4Address;

255.255.255.255.

static parse(source: Str) -> Result<IpV4Address, ConversionError>;

Parse canonical a.b.c.d. Returns Err(ConversionError) on any failure (extra whitespace, out-of-range octets, missing or extra dots, leading/trailing separators); the error is describable/printable. Use parse_opt if you only need presence, not the reason.

static parse_opt(source: Str) -> Option<IpV4Address>;

Detail-free variant of parse: None on any failure. Backs parse and suits callers that branch only on success/failure.

Methods

octet(&this, index: i64) -> u8;

The octet at index, counted in network order: 0 is the most significant (a in a.b.c.d), 3 the least. Panics when index is outside 0..4.

is_loopback(&this) -> boolean;

Whether the address is in the loopback block 127.0.0.0/8 - any address whose first octet is 127, not only 127.0.0.1.

is_unspecified(&this) -> boolean;

Whether the address is 0.0.0.0.

to_u32(&this) -> u32;

Host-byte-order u32 representation. High byte is a.

IpV6Address

type struct IpV6Address {
    segments: [u8; 16];
}

The same shape over sixteen bytes, with group_at reading one 16-bit group and from_bytes taking the sixteen bytes from a slice (None if it is not exactly sixteen long). parse reads the canonical text forms: full a:b:c:d:e:f:g:h, one ::-compressed run, and a trailing embedded IPv4 (::ffff:1.2.3.4).

Properties

PropertyType
segments[u8; 16]

Constructors

static unspecified() -> IpV6Address;

::, all sixteen octets zero: "any interface" for a bound listener.

static localhost() -> IpV6Address;

::1, the IPv6 loopback address.

static from_bytes(bytes: &Slice<u8>) -> Option<IpV6Address>;

The address held in sixteen network-order octets (e.g. the sin6_addr of a kernel-filled sockaddr_in6), or None unless bytes is exactly sixteen long.

static parse(source: Str) -> Result<IpV6Address, ConversionError>;

Parse canonical IPv6 text: full a:b:c:d:e:f:g:h, ::-compressed (one run at most), and a trailing embedded IPv4 (::ffff:1.2.3.4). Returns Err(ConversionError) on any malformed input; use parse_opt for the detail-free variant.

static parse_opt(source: Str) -> Option<IpV6Address>;

Detail-free variant of parse: None on any malformed input.

Methods

group_at(&this, group: i64) -> u16;

The two-byte group at group (0..8), big-endian (segments are in network order, so the high byte comes first).

is_loopback(&this) -> boolean;

::1.

is_unspecified(&this) -> boolean;

::.

IpAddress

type enum IpAddress {
    V4(IpV4Address);
    V6(IpV6Address);
}

The enum over the two, with the same parse pair: text containing a : goes to the v6 parser, anything else to v4. Display writes the family's own form.

import std::collections::str::{ Str };
import std::net::address::ip::{ IpAddress, IpV4Address };

const local: IpAddress = IpAddress::V4(IpV4Address::localhost());
match (IpAddress::parse(Str::new("2001:db8::1"))) {
    Result::Ok(address) => { /* address.is_ipv6() */ }
    Result::Err(error)  => { /* not an address */ }
}

All three are POD — copied by value, nothing to drop.

Variants

Variant
V4(IpV4Address)
V6(IpV6Address)

Constructors

static parse(source: Str) -> Result<IpAddress, ConversionError>;

Parse textual form. A ':' routes to the v6 parser; otherwise v4. Returns Err(ConversionError) if the selected parser rejects the input; use parse_opt for the detail-free variant.

static parse_opt(source: Str) -> Option<IpAddress>;

Detail-free variant of parse: None if the selected parser rejects.

Methods

is_ipv4(&this) -> boolean;

Whether this is an IPv4 address (IpAddress::V4).

is_ipv6(&this) -> boolean;

Whether this is an IPv6 address (IpAddress::V6).