Skip to content

Socket Native

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

Functions

getNativeAddressFamily

Spice
public f<int> getNativeAddressFamily(const IpAddress& address)

Returns the address family constant (AF_INET or AF_INET6) of the given address

Parameters

Name Type Description
address const IpAddress& IP address

Returns: int — Address family

openNativeSocket

Spice
public f<Result<int>> openNativeSocket(const IpAddress& address, unsigned short port, int socketType, unsigned long timeoutMs)

Opens a socket of the family of the given address and connects it to the given address and port. For a datagram socket, connecting only fixes the peer, so that plain reads and writes can be used and the kernel drops datagrams from any other sender.

Parameters

Name Type Description
address const IpAddress& Address to connect to
port unsigned short Port to connect to
socketType int SOCK_STREAM or SOCK_DGRAM
timeoutMs unsigned long Timeout for the single reads and writes on the socket in milliseconds, 0 for none

Returns: Result<int> — File descriptor of the connected socket

setNativeSocketTimeout

Spice
public f<bool> setNativeSocketTimeout(int sockFd, unsigned long timeoutMs)

Limits how long a single read or write on the given socket may block

Parameters

Name Type Description
sockFd int Socket file descriptor
timeoutMs unsigned long Timeout in milliseconds, 0 to block indefinitely

Returns: bool — Setting the timeout was successful or not

fillNativeRandomBytes

Spice
public f<bool> fillNativeRandomBytes(byte* buffer, unsigned long size)

Fills the given buffer with random bytes from the kernel, that are fit for values an attacker must not be able to predict (like the IDs of DNS queries)

Parameters

Name Type Description
buffer byte* Buffer to fill
size unsigned long Number of bytes to fill

Returns: bool — Filling the buffer was successful or not

readNativeSocket

Spice
public f<long> readNativeSocket(int sockFd, byte* buffer, unsigned long size)

Reads from the given socket. Blocks until data arrives, the timeout of the socket expires or the connection breaks.

Parameters

Name Type Description
sockFd int Socket file descriptor
buffer byte* Buffer to read into
size unsigned long Size of the buffer

Returns: long — Number of bytes read, 0 or less on timeout, error or end of the connection

writeNativeSocket

Spice
public f<long> writeNativeSocket(int sockFd, byte* buffer, unsigned long size)

Writes to the given socket. A single call may write only a part of the buffer.

Parameters

Name Type Description
sockFd int Socket file descriptor
buffer byte* Buffer to write
size unsigned long Number of bytes to write

Returns: long — Number of bytes written, 0 or less on timeout or error

closeNativeSocket

Spice
public p closeNativeSocket(int sockFd)

Closes the given socket

Parameters

Name Type Description
sockFd int Socket file descriptor

getNativeHostsFilePath

Spice
public f<String> getNativeHostsFilePath()

Returns the path of the hosts file of the system

Returns: String — Path of the hosts file

readNativeResolverConfig

Spice
public f<Result<String>> readNativeResolverConfig()

Reads the resolver configuration of the system in the format of resolv.conf(5)

Returns: Result<String> — Content of /etc/resolv.conf

getNativeMonotonicMillis

Spice
public f<long> getNativeMonotonicMillis()

Returns the time of a monotonic clock, that is not affected by changes of the system time. Only the difference between two values has a meaning.

Returns: long — Monotonic time in milliseconds