Winsock v0.1.0
WsaData
pub struct WsaData {
// private fields
}
Room for a WSADATA, which WSAStartup fills and nothing here reads.
Constructors
WsaData
pub func WsaData() -> WsaData
Zeroed storage for WSAStartup to fill.
Returns: the storage
AddressInfo
pub struct AddressInfo {
pub flags: int32;
pub family: int32;
pub socketType: int32;
pub protocol: int32;
pub addressLength: uint64;
pub canonicalName: *char8;
pub address: *opaque;
pub next: *var AddressInfo;
}
Winsock's addrinfo: one result of getaddrinfo, linked to the next.
Fields
| Name | Type | Description |
|---|---|---|
flags | int32 | Flags such as AI_PASSIVE. |
family | int32 | The address family, or zero for any. |
socketType | int32 | The socket type, or zero for any. |
protocol | int32 | The protocol, or zero for any. |
addressLength | uint64 | The size of address in bytes. |
canonicalName | *char8 | The host's canonical name, or null. |
address | *opaque | The address, a sockaddr. |
next | *var AddressInfo | The next result, or null after the last. |
AF_INET
pub const AF_INET: int32 = 2;
AF_INET: the IPv4 address family.
AF_INET6
pub const AF_INET6: int32 = 23;
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.
SOL_SOCKET
pub const SOL_SOCKET: int32 = 0xFFFF;
SOL_SOCKET: the level of the options every socket has.
SO_RCVTIMEO
pub const SO_RCVTIMEO: int32 = 0x1006;
SO_RCVTIMEO: how long a receive waits, in milliseconds.
SO_SNDTIMEO
pub const SO_SNDTIMEO: int32 = 0x1005;
SO_SNDTIMEO: how long a send waits, in milliseconds.
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.
INVALID_SOCKET
pub const INVALID_SOCKET: uint64 = 0xFFFFFFFFFFFFFFFF;
INVALID_SOCKET: what a call that makes a socket returns on failure.
FIONBIO
pub const FIONBIO: int32 = -2147195266;
FIONBIO: the ioctlsocket command that turns a socket's non-blocking mode on or off.
POLLRDNORM
pub const POLLRDNORM: int16 = 0x100;
POLLRDNORM: WSAPoll reports a socket with data to read, or a connection to accept.
POLLWRNORM
pub const POLLWRNORM: int16 = 0x10;
POLLWRNORM: WSAPoll reports a socket a write would not wait on.
POLLERR
pub const POLLERR: int16 = 0x1;
POLLERR: the socket has an error pending; always reported.
POLLHUP
pub const POLLHUP: int16 = 0x2;
POLLHUP: the connection was closed; always reported.
POLLNVAL
pub const POLLNVAL: int16 = 0x4;
POLLNVAL: the socket is not valid; always reported.
WsaPollFd
pub struct WsaPollFd {
pub socket: uint64;
pub events: int16;
pub returned: int16;
}
WSAPOLLFD: one socket WSAPoll watches, the events wanted, and the events it reports.
Fields
| Name | Type | Description |
|---|---|---|
socket | uint64 | The socket. |
events | int16 | The events wanted, such as POLLRDNORM. |
returned | int16 | The events that happened, filled by WSAPoll. |
WSAStartup
pub extern func WSAStartup(
version: uint16,
data: *var WsaData
) -> int32
Starts Winsock for this process.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsastartup
Parameters
| Name | Type | Description |
|---|---|---|
version | uint16 | the version wanted, 0x0202 for 2.2 |
data | *var WsaData | receives details of the implementation |
Returns: zero, or an error code
WSAGetLastError
pub extern func WSAGetLastError() -> int32
The calling thread's last Winsock error.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-wsagetlasterror
Returns: the error code
socket
pub extern func socket(
family: int32,
kind: int32,
protocol: int32
) -> uint64
Opens a socket.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-socket
Parameters
| Name | Type | Description |
|---|---|---|
family | int32 | the address family |
kind | int32 | the socket type |
protocol | int32 | the protocol, or zero |
Returns: the socket, or INVALID_SOCKET
connect
pub extern func connect(
socket: uint64,
address: *opaque,
length: int32
) -> int32
Connects a socket to address.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-connect
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
address | *opaque | the address, a sockaddr |
length | int32 | the address's size |
Returns: zero, or -1 with the reason in WSAGetLastError
bind
pub extern func bind(
socket: uint64,
address: *opaque,
length: int32
) -> int32
Gives a socket its local address.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-bind
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
address | *opaque | the address, a sockaddr |
length | int32 | the address's size |
Returns: zero, or -1 with the reason in WSAGetLastError
listen
pub extern func listen(
socket: uint64,
backlog: int32
) -> int32
Makes a bound stream socket accept connections.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-listen
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
backlog | int32 | how many connections may wait |
Returns: zero, or -1 with the reason in WSAGetLastError
accept
pub extern func accept(
socket: uint64,
address: *var opaque,
length: *var int32
) -> uint64
Accepts a waiting connection.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-accept
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the listening socket |
address | *var opaque | receives the peer's address, or null |
length | *var int32 | the buffer's size, then the address's, or null |
Returns: the new socket, or INVALID_SOCKET
sendto
pub extern func sendto(
socket: uint64,
buffer: *opaque,
count: int32,
flags: int32,
address: *opaque,
length: int32
) -> int32
Sends bytes on a socket, to address when it is not connected.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-sendto
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
buffer | *opaque | the bytes |
count | int32 | how many |
flags | int32 | flags, or zero |
address | *opaque | the destination, or null |
length | int32 | the destination's size |
Returns: the bytes sent, or -1 with the reason in WSAGetLastError
recvfrom
pub extern func recvfrom(
socket: uint64,
buffer: *var opaque,
count: int32,
flags: int32,
address: *var opaque,
length: *var int32
) -> int32
Receives bytes from a socket, and where they came from.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-recvfrom
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
buffer | *var opaque | receives the bytes |
count | int32 | how many fit |
flags | int32 | flags, or zero |
address | *var opaque | receives the sender's address, or null |
length | *var int32 | the buffer's size, then the address's, or null |
Returns: the bytes received, zero once a stream's peer closed it, or -1 with the reason in WSAGetLastError
closesocket
pub extern func closesocket(socket: uint64) -> int32
Closes a socket.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-closesocket
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
Returns: zero, or -1 with the reason in WSAGetLastError
shutdown
pub extern func shutdown(
socket: uint64,
how: int32
) -> int32
Ends one or both directions of a connection.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-shutdown
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
how | int32 | 0 to stop receiving, 1 to stop sending, 2 for both |
Returns: zero, or -1 with the reason in WSAGetLastError
setsockopt
pub extern func setsockopt(
socket: uint64,
level: int32,
name: int32,
value: *opaque,
length: int32
) -> int32
Sets a socket option.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-setsockopt
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
level | int32 | the option's level |
name | int32 | the option |
value | *opaque | the option's value |
length | int32 | the value's size |
Returns: zero, or -1 with the reason in WSAGetLastError
getsockname
pub extern func getsockname(
socket: uint64,
address: *var opaque,
length: *var int32
) -> int32
Reads a socket's local address.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getsockname
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
address | *var opaque | receives the address |
length | *var int32 | the buffer's size, then the address's |
Returns: zero, or -1 with the reason in WSAGetLastError
getpeername
pub extern func getpeername(
socket: uint64,
address: *var opaque,
length: *var int32
) -> int32
Reads the address of a connected socket's peer.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-getpeername
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
address | *var opaque | receives the address |
length | *var int32 | the buffer's size, then the address's |
Returns: zero, or -1 with the reason in WSAGetLastError
getaddrinfo
pub extern func getaddrinfo(
node: *char8,
service: *char8,
hints: *AddressInfo,
result: *var *var AddressInfo
) -> int32
Looks up the addresses of a host and service.
See also: https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo
Parameters
| Name | Type | Description |
|---|---|---|
node | *char8 | the host name or numeric address, NUL-terminated, or null |
service | *char8 | the service name or port number, NUL-terminated, or null |
hints | *AddressInfo | what kind of results are wanted, or null |
result | *var *var AddressInfo | receives the first result, freed with freeaddrinfo |
Returns: zero, or a Winsock error code
freeaddrinfo
pub extern func freeaddrinfo(info: *var AddressInfo)
Frees the results of getaddrinfo.
See also: https://learn.microsoft.com/en-us/windows/win32/api/ws2tcpip/nf-ws2tcpip-freeaddrinfo
Parameters
| Name | Type | Description |
|---|---|---|
info | *var AddressInfo | the first result |
ioctlsocket
pub extern func ioctlsocket(
socket: uint64,
command: int32,
argument: *var uint32
) -> int32
Sets a socket's mode, such as non-blocking with FIONBIO.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock/nf-winsock-ioctlsocket
Parameters
| Name | Type | Description |
|---|---|---|
socket | uint64 | the socket |
command | int32 | the mode to set, such as FIONBIO |
argument | *var uint32 | the mode's value: for FIONBIO, nonzero for non-blocking |
Returns: zero, or -1 with the reason in WSAGetLastError
WSAPoll
pub extern func WSAPoll(
sockets: *var WsaPollFd,
count: uint32,
timeout: int32
) -> int32
Waits until one of count sockets is ready or timeout milliseconds pass, filling each one's returned.
See also: https://learn.microsoft.com/en-us/windows/win32/api/winsock2/nf-winsock2-wsapoll
Parameters
| Name | Type | Description |
|---|---|---|
sockets | *var WsaPollFd | the sockets and the events wanted of each |
count | uint32 | how many there are |
timeout | int32 | milliseconds to wait, zero to only look, or -1 to wait without end |
Returns: how many sockets have events, zero after the timeout, or -1 with the reason in WSAGetLastError