Module net/addr
net/addr
Network address types — IP addresses and socket addresses.
Types
IpAddr
enum
IpAddr
An IP address, either IPv4 or IPv6.
Variants
| Variant | Fields | Description |
|---|---|---|
V4 | a: u8, b: u8, c: u8, d: u8 | An IPv4 address represented as four octets. |
V6 | segments: [u16; 8] | An IPv6 address represented as eight 16-bit segments. |
Trait Implementations
impl(IpAddr, ...)
parse_v4 : (IpAddr) fn(s : String, using(exn : Exception)) -> IpAddrloopback_v4 : (IpAddr) fn() -> IpAddrReturn the IPv4 loopback address (127.0.0.1).
Returns: IpAddr
loopback_v6 : (IpAddr) fn() -> IpAddrReturn the IPv6 loopback address (::1).
Returns: IpAddr
any_v4 : (IpAddr) fn() -> IpAddrReturn the IPv4 unspecified address (0.0.0.0).
Returns: IpAddr
is_loopback : (IpAddr) fn(self : IpAddr) -> boolis_v4 : (IpAddr) fn(self : IpAddr) -> boolis_v6 : (IpAddr) fn(self : IpAddr) -> boolimpl(IpAddr, ToString(...))
to_string : (self -> {
return match(self.*,
.V4(a, b, c, d) => {
buf := Array(u8, usize(16)).fill(u8(0));
snprintf(*(char)(&(buf(usize(0)))), usize(16), "%d.%d.%d.%d", i32(a), i32(b), i32(c), i32(d));
String.from_cstr(&(buf(usize(0)))).unwrap()
},
.V6(segs) => {
w := Writer.new();
i := usize(0);
while runtime((i < usize(8))), {
cond(
(i > usize(0)) => { w.write_str(":"); },
true => ()
);
w.write_hex(u64(segs(i)));
i = (i + usize(1));
};
w.to_string()
}
);
})
SocketAddr
struct
SocketAddr
An IP address paired with a port number.
Fields
| Name | Type | Description |
|---|---|---|
ip | IpAddr | The IP address. |
port | u16 | The port number. |
Trait Implementations
impl(SocketAddr, ...)
new : (SocketAddr) fn(ip : IpAddr, port : u16) -> SocketAddrCreate a new socket address from an IP and port.
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
ip | IpAddr | The IP address. | |
port | u16 | The port number. |
Returns: SocketAddr
loopback : (SocketAddr) fn(port : u16) -> SocketAddrCreate an IPv4 loopback socket address (127.0.0.1) with the given port.
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
port | u16 | The port number. |
Returns: SocketAddr
any : (SocketAddr) fn(port : u16) -> SocketAddrCreate an IPv4 unspecified socket address (0.0.0.0) with the given port.
Parameters
| Name | Type | Notes | Description |
|---|---|---|---|
port | u16 | The port number. |
Returns: SocketAddr
impl(SocketAddr, ToString(...))
to_string : (self -> {
ip_str := self.ip.to_string();
buf := Array(u8, usize(8)).fill(u8(0));
snprintf(*(char)(&(buf(usize(0)))), usize(8), "%d", i32(self.port));
port_str := String.from_cstr(&(buf(usize(0)))).unwrap();
return match(self.ip,
.V4(_, _, _, _) => `${ip_str}:${port_str}`,
.V6(_) => `[${ip_str}]:${port_str}`
);
})