Skip to content

Socket Address

Spice
import "std/net/socket-address";

NativeSockAddr struct

A sockaddr_in or sockaddr_in6, ready to be handed to the socket calls

Fields

Name Type Description
bytes unsigned byte[28] Struct contents, large enough for a sockaddr_in6
size unsigned int Number of used bytes, i.e. the size of the struct

Functions

createNativeSockAddr

Spice
public f<NativeSockAddr> createNativeSockAddr(const IpAddress& address, unsigned short port, int family, SockAddrLayout layout)

Lays out the socket address struct for the given address and port. sin6_flowinfo and sin6_scope_id stay zero.

Parameters

Name Type Description
address const IpAddress& IPv4 or IPv6 address
port unsigned short Port in host byte order
family int Address family value of the platform (AF_INET or AF_INET6, matching the address)
layout SockAddrLayout Layout of the struct on the platform

Returns: NativeSockAddr — Socket address struct

readNativeSockAddr

Spice
public f<Result<IpAddress>> readNativeSockAddr(const unsigned byte* sockAddr, int ipv4Family, int ipv6Family, SockAddrLayout layout)

Reads the address from a socket address struct, that the system handed out

Parameters

Name Type Description
sockAddr const unsigned byte* Pointer to the struct, may be nil
ipv4Family int Address family value of IPv4 on the platform (AF_INET)
ipv6Family int Address family value of IPv6 on the platform (AF_INET6)
layout SockAddrLayout Layout of the struct on the platform

Returns: Result<IpAddress> — IP address

SockAddrLayout enum

Layout of the first two bytes of the socket address structs. The port and the address follow at the same offsets on all supported platforms.

Item Value Description
FAMILY_16_BIT