Sockets v0.1.0

Source
Address families, socket types and options, and the calls that open, bind and connect sockets.

AF_INET

pub const AF_INET: int32 = 2;

AF_INET: the IPv4 address family.

Source

AF_INET6

pub const AF_INET6: int32 = 10;

AF_INET6: the IPv6 address family.

Source

SOCK_STREAM

pub const SOCK_STREAM: int32 = 1;

SOCK_STREAM: a connected byte stream.

Source

SOCK_DGRAM

pub const SOCK_DGRAM: int32 = 2;

SOCK_DGRAM: individual datagrams.

Source

SOCK_CLOEXEC

pub const SOCK_CLOEXEC: int32 = 0x80000;

SOCK_CLOEXEC: close the socket when the process starts another program.

Source

SOL_SOCKET

pub const SOL_SOCKET: int32 = 1;

SOL_SOCKET: the level of the options every socket has.

Source

SO_REUSEADDR

pub const SO_REUSEADDR: int32 = 2;

SO_REUSEADDR: let a listening address be bound again while old connections to it wind down.

Source

SO_RCVTIMEO

pub const SO_RCVTIMEO: int32 = 20;

SO_RCVTIMEO: how long a receive waits.

Source

SO_SNDTIMEO

pub const SO_SNDTIMEO: int32 = 21;

SO_SNDTIMEO: how long a send waits.

Source

IPPROTO_TCP

pub const IPPROTO_TCP: int32 = 6;

IPPROTO_TCP: the level of TCP's options.

Source

TCP_NODELAY

pub const TCP_NODELAY: int32 = 1;

TCP_NODELAY: send small writes at once rather than gathering them.

Source

IPPROTO_IPV6

pub const IPPROTO_IPV6: int32 = 41;

IPPROTO_IPV6: the level of IPv6's options.

Source

IPV6_V6ONLY

pub const IPV6_V6ONLY: int32 = 26;

IPV6_V6ONLY: accept only IPv6 on an IPv6 socket.

Source

MSG_NOSIGNAL

pub const MSG_NOSIGNAL: int32 = 0x4000;

MSG_NOSIGNAL: report a closed peer as an error rather than with SIGPIPE.

Source

Socket

pub func Socket(
    domain: int32,
    kind: int32,
    protocol: int32
) -> int64

Opens a socket.

See also: https://man7.org/linux/man-pages/man2/socket.2.html

Source

Parameters

NameTypeDescription
domainint32the address family, such as AF_INET
kindint32the socket type, such as SOCK_STREAM, combined with SOCK_CLOEXEC
protocolint32the protocol, or zero for the family's default

Returns: the new descriptor, or a negative error number

Connect

pub func Connect(
    fd: FileDescriptor,
    address: *opaque,
    length: uint32
) -> int64

Connects a socket to address, waiting for a stream connection to be made.

See also: https://man7.org/linux/man-pages/man2/connect.2.html

Source

Parameters

NameTypeDescription
fdFileDescriptorthe socket
address*opaquethe address, a sockaddr
lengthuint32the address's size in bytes

Returns: zero, or a negative error number

Bind

pub func Bind(
    fd: FileDescriptor,
    address: *opaque,
    length: uint32
) -> int64

Gives a socket its local address.

See also: https://man7.org/linux/man-pages/man2/bind.2.html

Source

Parameters

NameTypeDescription
fdFileDescriptorthe socket
address*opaquethe address, a sockaddr
lengthuint32the address's size in bytes

Returns: zero, or a negative error number

Listen

pub func Listen(fd: FileDescriptor, backlog: int32) -> int64

Makes a bound stream socket accept connections.

See also: https://man7.org/linux/man-pages/man2/listen.2.html

Source

Parameters

NameTypeDescription
fdFileDescriptorthe socket
backlogint32how many connections may wait to be accepted

Returns: zero, or a negative error number

Accept4

pub func Accept4(
    fd: FileDescriptor,
    address: *var opaque,
    length: *var uint32,
    flags: int32
) -> int64

Accepts a waiting connection as a new socket.

See also: https://man7.org/linux/man-pages/man2/accept4.2.html

Source

Parameters

NameTypeDescription
fdFileDescriptorthe listening socket
address*var opaquereceives the peer's address, or null
length*var uint32the address buffer's size, then the address's, or null
flagsint32flags for the new socket, such as SOCK_CLOEXEC

Returns: the new descriptor, or a negative error number

SendTo

pub func SendTo(
    fd: FileDescriptor,
    buffer: *opaque,
    count: uint,
    flags: int32,
    address: *opaque,
    length: uint32
) -> int64

Sends bytes on a socket, to address when it is not connected.

See also: https://man7.org/linux/man-pages/man2/sendto.2.html

Source

Parameters

NameTypeDescription
fdFileDescriptorthe socket
buffer*opaquethe bytes
countuinthow many
flagsint32flags such as MSG_NOSIGNAL
address*opaquethe destination, or null for a connected socket
lengthuint32the destination's size

Returns: the bytes sent, or a negative error number

ReceiveFrom

pub func ReceiveFrom(
    fd: FileDescriptor,
    buffer: *var opaque,
    count: uint,
    flags: int32,
    address: *var opaque,
    length: *var uint32
) -> int64

Receives bytes from a socket, and where they came from.

See also: https://man7.org/linux/man-pages/man2/recvfrom.2.html

Source

Parameters

NameTypeDescription
fdFileDescriptorthe socket
buffer*var opaquereceives the bytes
countuinthow many fit
flagsint32flags, or zero
address*var opaquereceives the sender's address, or null
length*var uint32the address buffer's size, then the address's, or null

Returns: the bytes received, zero once a stream's peer closed it, or a negative error number

Shutdown

pub func Shutdown(fd: FileDescriptor, how: int32) -> int64

Ends one or both directions of a connection.

See also: https://man7.org/linux/man-pages/man2/shutdown.2.html

Source

Parameters

NameTypeDescription
fdFileDescriptorthe socket
howint32SHUT_RD, SHUT_WR or SHUT_RDWR

Returns: zero, or a negative error number

SetSocketOption

pub func SetSocketOption(
    fd: FileDescriptor,
    level: int32,
    name: int32,
    value: *opaque,
    length: uint32
) -> int64

Sets a socket option.

See also: https://man7.org/linux/man-pages/man2/setsockopt.2.html

Source

Parameters

NameTypeDescription
fdFileDescriptorthe socket
levelint32the option's level, such as SOL_SOCKET
nameint32the option
value*opaquethe option's value
lengthuint32the value's size in bytes

Returns: zero, or a negative error number

GetSocketName

pub func GetSocketName(
    fd: FileDescriptor,
    address: *var opaque,
    length: *var uint32
) -> int64

Reads a socket's local address.

See also: https://man7.org/linux/man-pages/man2/getsockname.2.html

Source

Parameters

NameTypeDescription
fdFileDescriptorthe socket
address*var opaquereceives the address
length*var uint32the buffer's size, then the address's

Returns: zero, or a negative error number

GetPeerName

pub func GetPeerName(
    fd: FileDescriptor,
    address: *var opaque,
    length: *var uint32
) -> int64

Reads the address of a connected socket's peer.

See also: https://man7.org/linux/man-pages/man2/getpeername.2.html

Source

Parameters

NameTypeDescription
fdFileDescriptorthe socket
address*var opaquereceives the address
length*var uint32the buffer's size, then the address's

Returns: zero, or a negative error number