Skip to content
CryoCryo home
Stdlibnet::addr

ip

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

Types

IpV4Addr

type struct IpV4Addr {
    a: u8;
    b: u8;
    c: u8;
    d: u8;

    static new(a: u8, b: u8, c: u8, d: u8) -> IpV4Addr;
    static unspecified() -> IpV4Addr;
    static localhost() -> IpV4Addr;
    static broadcast() -> IpV4Addr;
    octet(&this, index: u64) -> u8;
    is_loopback(&this) -> boolean;
    is_unspecified(&this) -> boolean;
    to_u32(&this) -> u32;
    static parse(source: Str) -> Result<IpV4Addr, ConversionError>;
    static parse_opt(source: Str) -> Option<IpV4Addr>;
}

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, parse_opt as an Option. to_u32 gives the address in host byte order, which is what the raw socket API wants after htonl.

Properties

PropertyType
au8
bu8
cu8
du8

Constructors

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

0.0.0.0: "any interface" for a bound listener.

static localhost() -> IpV4Addr;

127.0.0.1.

static broadcast() -> IpV4Addr;

255.255.255.255.

static parse(source: Str) -> Result<IpV4Addr, 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<IpV4Addr>;

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

Methods

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

Returns the four octets in big-endian (network) order.

is_loopback(&this) -> boolean;
is_unspecified(&this) -> boolean;
to_u32(&this) -> u32;

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

IpV6Addr

type struct IpV6Addr {
    segments: [u8;
    16];

    static unspecified() -> IpV6Addr;
    static localhost() -> IpV6Addr;
    static from_buf(p: u8*) -> IpV6Addr;
    group_at(&this, group: u64) -> u16;
    is_loopback(&this) -> boolean;
    is_unspecified(&this) -> boolean;
    static parse(source: Str) -> Result<IpV6Addr, ConversionError>;
    static parse_opt(source: Str) -> Option<IpV6Addr>;
}

The same shape over sixteen bytes, with group_at reading one 16-bit group. 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() -> IpV6Addr;
static localhost() -> IpV6Addr;
static from_buf(p: u8*) -> IpV6Addr;

Build from sixteen network-order octets at p[0..16] (e.g. the sin6_addr of a kernel-filled sockaddr_in6).

static parse(source: Str) -> Result<IpV6Addr, 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<IpV6Addr>;

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

Methods

group_at(&this, group: u64) -> 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;

::.

IpAddr

type enum IpAddr {
    V4(IpV4Addr);
    V6(IpV6Addr);
}

implement enum IpAddr {
    is_ipv4(&this) -> boolean;
    is_ipv6(&this) -> boolean;
    static parse(source: Str) -> Result<IpAddr, ConversionError>;
    static parse_opt(source: Str) -> Option<IpAddr>;
}

The enum over the two, with the same parse pair: text containing a : goes to the v6 parser, anything else to v4.

import std::net::addr::ip;

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

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

Variants

Variant
V4(IpV4Addr)
V6(IpV6Addr)

Constructors

static parse(source: Str) -> Result<IpAddr, 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<IpAddr>;

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

Methods

is_ipv4(&this) -> boolean;
is_ipv6(&this) -> boolean;