Winsock v0.1.0

Source
Starting Winsock, address families, sockets and their options, and name resolution.

WsaData

pub struct WsaData {
    // private fields
}

Room for a WSADATA, which WSAStartup fills and nothing here reads.

Source

Constructors

WsaData

pub func WsaData() -> WsaData

Zeroed storage for WSAStartup to fill.

Returns: the storage

Source

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.

Source

Fields

NameTypeDescription
flagsint32Flags such as AI_PASSIVE.
familyint32The address family, or zero for any.
socketTypeint32The socket type, or zero for any.
protocolint32The protocol, or zero for any.
addressLengthuint64The size of address in bytes.
canonicalName*char8The host's canonical name, or null.
address*opaqueThe address, a sockaddr.
next*var AddressInfoThe next result, or null after the last.

AF_INET

pub const AF_INET: int32 = 2;

AF_INET: the IPv4 address family.

Source

AF_INET6

pub const AF_INET6: int32 = 23;

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

SOL_SOCKET

pub const SOL_SOCKET: int32 = 0xFFFF;

SOL_SOCKET: the level of the options every socket has.

Source

SO_RCVTIMEO

pub const SO_RCVTIMEO: int32 = 0x1006;

SO_RCVTIMEO: how long a receive waits, in milliseconds.

Source

SO_SNDTIMEO

pub const SO_SNDTIMEO: int32 = 0x1005;

SO_SNDTIMEO: how long a send waits, in milliseconds.

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 = 27;

IPV6_V6ONLY: accept only IPv6 on an IPv6 socket.

Source

INVALID_SOCKET

pub const INVALID_SOCKET: uint64 = 0xFFFFFFFFFFFFFFFF;

INVALID_SOCKET: what a call that makes a socket returns on failure.

Source

FIONBIO

pub const FIONBIO: int32 = -2147195266;

FIONBIO: the ioctlsocket command that turns a socket's non-blocking mode on or off.

Source

POLLRDNORM

pub const POLLRDNORM: int16 = 0x100;

POLLRDNORM: WSAPoll reports a socket with data to read, or a connection to accept.

Source

POLLWRNORM

pub const POLLWRNORM: int16 = 0x10;

POLLWRNORM: WSAPoll reports a socket a write would not wait on.

Source

POLLERR

pub const POLLERR: int16 = 0x1;

POLLERR: the socket has an error pending; always reported.

Source

POLLHUP

pub const POLLHUP: int16 = 0x2;

POLLHUP: the connection was closed; always reported.

Source

POLLNVAL

pub const POLLNVAL: int16 = 0x4;

POLLNVAL: the socket is not valid; always reported.

Source

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.

Source

Fields

NameTypeDescription
socketuint64The socket.
eventsint16The events wanted, such as POLLRDNORM.
returnedint16The 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

Source

Parameters

NameTypeDescription
versionuint16the version wanted, 0x0202 for 2.2
data*var WsaDatareceives 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

Source

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

Source

Parameters

NameTypeDescription
familyint32the address family
kindint32the socket type
protocolint32the 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

Source

Parameters

NameTypeDescription
socketuint64the socket
address*opaquethe address, a sockaddr
lengthint32the 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

Source

Parameters

NameTypeDescription
socketuint64the socket
address*opaquethe address, a sockaddr
lengthint32the 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

Source

Parameters

NameTypeDescription
socketuint64the socket
backlogint32how 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

Source

Parameters

NameTypeDescription
socketuint64the listening socket
address*var opaquereceives the peer's address, or null
length*var int32the 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

Source

Parameters

NameTypeDescription
socketuint64the socket
buffer*opaquethe bytes
countint32how many
flagsint32flags, or zero
address*opaquethe destination, or null
lengthint32the 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

Source

Parameters

NameTypeDescription
socketuint64the socket
buffer*var opaquereceives the bytes
countint32how many fit
flagsint32flags, or zero
address*var opaquereceives the sender's address, or null
length*var int32the 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

Source

Parameters

NameTypeDescription
socketuint64the 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

Source

Parameters

NameTypeDescription
socketuint64the socket
howint320 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

Source

Parameters

NameTypeDescription
socketuint64the socket
levelint32the option's level
nameint32the option
value*opaquethe option's value
lengthint32the 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

Source

Parameters

NameTypeDescription
socketuint64the socket
address*var opaquereceives the address
length*var int32the 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

Source

Parameters

NameTypeDescription
socketuint64the socket
address*var opaquereceives the address
length*var int32the 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

Source

Parameters

NameTypeDescription
node*char8the host name or numeric address, NUL-terminated, or null
service*char8the service name or port number, NUL-terminated, or null
hints*AddressInfowhat kind of results are wanted, or null
result*var *var AddressInforeceives 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

Source

Parameters

NameTypeDescription
info*var AddressInfothe 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

Source

Parameters

NameTypeDescription
socketuint64the socket
commandint32the mode to set, such as FIONBIO
argument*var uint32the 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

Source

Parameters

NameTypeDescription
sockets*var WsaPollFdthe sockets and the events wanted of each
countuint32how many there are
timeoutint32milliseconds 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