v0.4.0

Addresses v0.1.0

Source
IPv4, IPv6 and socket addresses: reading them, writing them and classifying them.

Ipv4Address

pub struct Ipv4Address {
    pub octets: uint8[N];
}

An IPv4 address. Copy.

Source

Fields

NameTypeDescription
octetsuint8[N]The four octets, most significant first.

Constructors

Ipv4Address

pub func Ipv4Address(
    a: uint8,
    b: uint8,
    c: uint8,
    d: uint8
) -> Ipv4Address

The address a.b.c.d.

NameTypeDescription
auint8the first octet
buint8the second octet
cuint8the third octet
duint8the fourth octet

Returns: the address

Source

Associated functions

Localhost

pub func Localhost() -> Ipv4Address

127.0.0.1, this machine.

Returns: the loopback address

Source

Unspecified

pub func Unspecified() -> Ipv4Address

0.0.0.0, every address of this machine when binding.

Returns: the unspecified address

Source

Parse

pub func Parse(text: char8[..]) -> Ipv4Address?

Reads dotted-decimal text such as 192.168.0.1, each octet without leading zeros.

NameTypeDescription
textchar8[..]the text

Returns: the address, or none when the text is not one

Source

Methods

IsLoopback

pub func IsLoopback(&self) -> bool

Whether this is a loopback address, in 127.0.0.0/8.

Returns: true for loopback

Source

IsUnspecified

pub func IsUnspecified(&self) -> bool

Whether this is the unspecified address, 0.0.0.0, which binds every local address.

Returns: true for 0.0.0.0

Source

IsPrivate

pub func IsPrivate(&self) -> bool

Whether this is a private address of RFC 1918: in 10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16.

Returns: true for a private address

Source

pub func IsLinkLocal(&self) -> bool

Whether this is a link-local address, in 169.254.0.0/16.

Returns: true for a link-local address

Source

IsMulticast

pub func IsMulticast(&self) -> bool

Whether this is a multicast address, in 224.0.0.0/4.

Returns: true for a multicast address

Source

WriteText

pub func WriteText(&self, into: var char8[..]) -> uint

Writes the address in dotted-decimal form into into, at most 15 characters.

NameTypeDescription
intovar char8[..]where to write

Returns: how many characters the text takes, which is more than were written when into is too short

Source

Equals

pub func Equals(&self, other: Ipv4Address) -> bool

Whether two addresses are the same.

NameTypeDescription
otherIpv4Addressthe address to compare with

Returns: true when they are equal

Source

Implements Display

Ipv4Address conforms to Display.

WriteDisplay

pub func WriteDisplay(
    &self,
    writer: &var TextWriter,
    spec: FormatSpec
) throws FormatError

Writes the address in dotted-decimal form, under the placeholder's width, fill and alignment. No style is understood. Reports whatever the writer reported, and UnsupportedRequest for a style.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole address was accepted

Source

Ipv6Address

pub struct Ipv6Address {
    pub octets: uint8[N];
}

An IPv6 address. Copy.

Source

Fields

NameTypeDescription
octetsuint8[N]The sixteen octets, most significant first.

Associated functions

Localhost

pub func Localhost() -> Ipv6Address

::1, this machine.

Returns: the loopback address

Source

Unspecified

pub func Unspecified() -> Ipv6Address

::, every address of this machine when binding.

Returns: the unspecified address

Source

Parse

pub func Parse(text: char8[..]) -> Ipv6Address?

Reads text such as 2001:db8::1 or ::ffff:192.0.2.1. A zone such as %eth0 is not accepted.

NameTypeDescription
textchar8[..]the text

Returns: the address, or none when the text is not one

Source

Methods

IsLoopback

pub func IsLoopback(&self) -> bool

Whether this is the loopback address, ::1.

Returns: true for loopback

Source

IsUnspecified

pub func IsUnspecified(&self) -> bool

Whether this is the unspecified address, ::, which binds every local address.

Returns: true for ::

Source

IsPrivate

pub func IsPrivate(&self) -> bool

Whether this is a unique local address of RFC 4193, in fc00::/7: the IPv6 counterpart of a private IPv4 address.

Returns: true for a unique local address

Source

pub func IsLinkLocal(&self) -> bool

Whether this is a link-local address, in fe80::/10.

Returns: true for a link-local address

Source

IsMulticast

pub func IsMulticast(&self) -> bool

Whether this is a multicast address, in ff00::/8.

Returns: true for a multicast address

Source

AsMappedIpv4

pub func AsMappedIpv4(&self) -> Ipv4Address?

The IPv4 address this one maps, for an address in ::ffff:0:0/96, or none for any other.

Returns: the mapped IPv4 address

Source

WriteText

pub func WriteText(&self, into: var char8[..]) -> uint

Writes the address in the canonical form of RFC 5952 into into, at most 45 characters. An IPv4-mapped address keeps its IPv4 tail in dotted-decimal form, as ::ffff:192.0.2.1.

NameTypeDescription
intovar char8[..]where to write

Returns: how many characters the text takes, which is more than were written when into is too short

Source

Equals

pub func Equals(&self, other: Ipv6Address) -> bool

Whether two addresses are the same.

NameTypeDescription
otherIpv6Addressthe address to compare with

Returns: true when they are equal

Source

Implements Display

Ipv6Address conforms to Display.

WriteDisplay

pub func WriteDisplay(
    &self,
    writer: &var TextWriter,
    spec: FormatSpec
) throws FormatError

Writes the address in the canonical form of RFC 5952, under the placeholder's width, fill and alignment. No style is understood. Reports whatever the writer reported, and UnsupportedRequest for a style.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole address was accepted

Source

IpAddress

pub struct IpAddress {
    // private fields
}

An IPv4 or IPv6 address. Copy.

Source

Associated functions

FromIpv4

pub func FromIpv4(address: Ipv4Address) -> IpAddress

An IPv4 address as an IP address.

NameTypeDescription
addressIpv4Addressthe IPv4 address

Returns: the IP address

Source

FromIpv6

pub func FromIpv6(address: Ipv6Address) -> IpAddress

An IPv6 address as an IP address.

NameTypeDescription
addressIpv6Addressthe IPv6 address

Returns: the IP address

Source

Parse

pub func Parse(text: char8[..]) -> IpAddress?

Reads an IPv4 or IPv6 address.

NameTypeDescription
textchar8[..]the text

Returns: the address, or none when the text is neither

Source

Methods

IsIpv6

pub func IsIpv6(&self) -> bool

Whether this is an IPv6 address.

Returns: true for IPv6

Source

AsIpv4

pub func AsIpv4(&self) -> Ipv4Address?

The IPv4 address, or none for an IPv6 one.

Returns: the IPv4 address

Source

AsIpv6

pub func AsIpv6(&self) -> Ipv6Address?

The IPv6 address, or none for an IPv4 one.

Returns: the IPv6 address

Source

IsLoopback

pub func IsLoopback(&self) -> bool

Whether this is a loopback address.

Returns: true for loopback

Source

IsUnspecified

pub func IsUnspecified(&self) -> bool

Whether this is the unspecified address, 0.0.0.0 or ::.

Returns: true for the unspecified address

Source

IsPrivate

pub func IsPrivate(&self) -> bool

Whether this is a private IPv4 address or a unique local IPv6 address.

Returns: true for a private address

Source

pub func IsLinkLocal(&self) -> bool

Whether this is a link-local address.

Returns: true for a link-local address

Source

IsMulticast

pub func IsMulticast(&self) -> bool

Whether this is a multicast address.

Returns: true for a multicast address

Source

WriteText

pub func WriteText(&self, into: var char8[..]) -> uint

Writes the address into into, at most 45 characters.

NameTypeDescription
intovar char8[..]where to write

Returns: how many characters the text takes

Source

Equals

pub func Equals(&self, other: IpAddress) -> bool

Whether two addresses are the same, which an IPv4 and an IPv6 address never are.

NameTypeDescription
otherIpAddressthe address to compare with

Returns: true when they are equal

Source

Implements Display

IpAddress conforms to Display.

WriteDisplay

pub func WriteDisplay(
    &self,
    writer: &var TextWriter,
    spec: FormatSpec
) throws FormatError

Writes the address as its IPv4 or IPv6 form does, under the placeholder's width, fill and alignment. No style is understood. Reports whatever the writer reported, and UnsupportedRequest for a style.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole address was accepted

Source

SocketAddress

pub struct SocketAddress {
    pub ip: IpAddress;
    pub port: uint16;
}

An IP address and a port: where a socket is bound or connected. Copy.

Source

Fields

NameTypeDescription
ipIpAddressThe address.
portuint16The port.

Constructors

SocketAddress

pub func SocketAddress(
    ip: IpAddress,
    port: uint16
) -> SocketAddress

An address and a port.

NameTypeDescription
ipIpAddressthe address
portuint16the port

Returns: the socket address

Source

Associated functions

Parse

pub func Parse(text: char8[..]) -> SocketAddress?

Reads address:port, with an IPv6 address in brackets, as in [::1]:8080.

NameTypeDescription
textchar8[..]the text

Returns: the socket address, or none when the text is not one

Source

Methods

WriteText

pub func WriteText(&self, into: var char8[..]) -> uint

Writes address:port into into, an IPv6 address in brackets, at most 47 characters.

NameTypeDescription
intovar char8[..]where to write

Returns: how many characters the text takes

Source

ToString

pub func ToString(
    &self,
    allocator: Allocator
) -> String throws TextError

The text of the address, as WriteText writes it.

NameTypeDescription
allocatorAllocatorwhat the text is kept in

Returns: the text

Source

Equals

pub func Equals(&self, other: SocketAddress) -> bool

Whether two socket addresses are the same.

NameTypeDescription
otherSocketAddressthe address to compare with

Returns: true when they are equal

Source

Implements Display

SocketAddress conforms to Display.

WriteDisplay

pub func WriteDisplay(
    &self,
    writer: &var TextWriter,
    spec: FormatSpec
) throws FormatError

Writes the address and port, an IPv6 address in brackets, under the placeholder's width, fill and alignment. No style is understood. Reports whatever the writer reported, and UnsupportedRequest for a style.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole address was accepted

Source