Sockets v0.1.0
AF_INET
pub const AF_INET: int32 = 2;
AF_INET: the IPv4 address family.
AF_INET6
pub const AF_INET6: int32 = 28;
AF_INET6: the IPv6 address family.
SOCK_STREAM
pub const SOCK_STREAM: int32 = 1;
SOCK_STREAM: a connected byte stream.
SOCK_DGRAM
pub const SOCK_DGRAM: int32 = 2;
SOCK_DGRAM: individual datagrams.
SOCK_CLOEXEC
pub const SOCK_CLOEXEC: int32 = 0x10000000;
SOCK_CLOEXEC: close the socket when the process starts another program.
SOL_SOCKET
pub const SOL_SOCKET: int32 = 0xffff;
SOL_SOCKET: the level of the options every socket has.
SO_REUSEADDR
pub const SO_REUSEADDR: int32 = 4;
SO_REUSEADDR: let a listening address be bound again while old connections to it wind down.
SO_RCVTIMEO
pub const SO_RCVTIMEO: int32 = 0x1006;
SO_RCVTIMEO: how long a receive waits.
SO_SNDTIMEO
pub const SO_SNDTIMEO: int32 = 0x1005;
SO_SNDTIMEO: how long a send waits.
IPPROTO_TCP
pub const IPPROTO_TCP: int32 = 6;
IPPROTO_TCP: the level of TCP's options.
TCP_NODELAY
pub const TCP_NODELAY: int32 = 1;
TCP_NODELAY: send small writes at once rather than gathering them.
IPPROTO_IPV6
pub const IPPROTO_IPV6: int32 = 41;
IPPROTO_IPV6: the level of IPv6's options.
IPV6_V6ONLY
pub const IPV6_V6ONLY: int32 = 27;
IPV6_V6ONLY: accept only IPv6 on an IPv6 socket.
MSG_NOSIGNAL
pub const MSG_NOSIGNAL: int32 = 0x20000;
MSG_NOSIGNAL: report a closed peer as an error rather than with SIGPIPE.
Socket
pub func Socket(
domain: int32,
kind: int32,
protocol: int32
) -> int64
Opens a socket.
See also: https://man.freebsd.org/cgi/man.cgi?query=socket&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
domain | int32 | the address family, such as AF_INET |
kind | int32 | the socket type, such as SOCK_STREAM, combined with SOCK_CLOEXEC |
protocol | int32 | the 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://man.freebsd.org/cgi/man.cgi?query=connect&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | the socket |
address | *opaque | the address, a sockaddr |
length | uint32 | the 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://man.freebsd.org/cgi/man.cgi?query=bind&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | the socket |
address | *opaque | the address, a sockaddr |
length | uint32 | the 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://man.freebsd.org/cgi/man.cgi?query=listen&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | the socket |
backlog | int32 | how 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://man.freebsd.org/cgi/man.cgi?query=accept4&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | the listening socket |
address | *var opaque | receives the peer's address, or null |
length | *var uint32 | the address buffer's size, then the address's, or null |
flags | int32 | flags 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://man.freebsd.org/cgi/man.cgi?query=sendto&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | the socket |
buffer | *opaque | the bytes |
count | uint | how many |
flags | int32 | flags such as MSG_NOSIGNAL |
address | *opaque | the destination, or null for a connected socket |
length | uint32 | the 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://man.freebsd.org/cgi/man.cgi?query=recvfrom&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | the socket |
buffer | *var opaque | receives the bytes |
count | uint | how many fit |
flags | int32 | flags, or zero |
address | *var opaque | receives the sender's address, or null |
length | *var uint32 | the 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://man.freebsd.org/cgi/man.cgi?query=shutdown&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | the socket |
how | int32 | SHUT_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://man.freebsd.org/cgi/man.cgi?query=setsockopt&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | the socket |
level | int32 | the option's level, such as SOL_SOCKET |
name | int32 | the option |
value | *opaque | the option's value |
length | uint32 | the 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://man.freebsd.org/cgi/man.cgi?query=getsockname&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | the socket |
address | *var opaque | receives the address |
length | *var uint32 | the 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://man.freebsd.org/cgi/man.cgi?query=getpeername&sektion=2
Parameters
| Name | Type | Description |
|---|---|---|
fd | FileDescriptor | the socket |
address | *var opaque | receives the address |
length | *var uint32 | the buffer's size, then the address's |
Returns: zero, or a negative error number