Source Edit

This module implements a low-level cross-platform sockets interface. Look at the net module for the higher-level version.

Imports

os, options, since, strbasics, winlean

Types

  1. Domain = enum
  2. AF_UNSPEC = 0, ## unspecified domain (can be detected automatically by
  3. ## some procedures, such as getaddrinfo)
  4. AF_UNIX = 1, ## for local socket (using a file). Unsupported on Windows.
  5. AF_INET = 2, ## for network protocol IPv4 or
  6. AF_INET6 = 23

domain, which specifies the protocol family of the created socket. Other domains than those that are listed here are unsupported. Source Edit

  1. Hostent = object
  2. name*: string
  3. aliases*: seq[string]
  4. addrtype*: Domain
  5. length*: int
  6. addrList*: seq[string]

information about a given host Source Edit

  1. Port = distinct uint16

port type Source Edit

  1. Protocol = enum
  2. IPPROTO_TCP = 6, ## Transmission control protocol.
  3. IPPROTO_UDP = 17, ## User datagram protocol.
  4. IPPROTO_IP, ## Internet protocol.
  5. IPPROTO_IPV6, ## Internet Protocol Version 6.
  6. IPPROTO_RAW, ## Raw IP Packets Protocol. Unsupported on Windows.
  7. IPPROTO_ICMP, ## Internet Control message protocol.
  8. IPPROTO_ICMPV6 ## Internet Control message protocol for IPv6.

third argument to socket proc Source Edit

  1. Servent = object
  2. name*: string
  3. aliases*: seq[string]
  4. port*: Port
  5. proto*: string

information about a service Source Edit

  1. SockType = enum
  2. SOCK_STREAM = 1, ## reliable stream-oriented service or Stream Sockets
  3. SOCK_DGRAM = 2, ## datagram service or Datagram Sockets
  4. SOCK_RAW = 3, ## raw protocols atop the network layer.
  5. SOCK_SEQPACKET = 5 ## reliable sequenced packet service

second argument to socket proc Source Edit

Lets

  1. osInvalidSocket = INVALID_SOCKET

Source Edit

Consts

  1. FIONBIO = -2147195266'i32

Source Edit

  1. IOC_IN = -2147483648

Source Edit

  1. IOCPARM_MASK = 127

Source Edit

Procs

  1. proc `$`(p: Port): string {.borrow, ...raises: [], tags: [], forbids: [].}

Returns the port number as a string Source Edit

  1. proc `==`(a, b: Port): bool {.borrow, ...raises: [], tags: [], forbids: [].}

\== for ports. Source Edit

  1. proc accept(fd: SocketHandle; inheritable = defined(nimInheritHandles)): (
  2. SocketHandle, string) {....raises: [], tags: [], forbids: [].}

Accepts a new client connection.

inheritable decides if the resulting SocketHandle can be inherited by child processes.

Returns (osInvalidSocket, “”) if an error occurred.

Source Edit

  1. proc bindAddr(socket: SocketHandle; name: ptr SockAddr; namelen: SockLen): cint {.
  2. ...raises: [], tags: [], forbids: [].}

Source Edit

  1. proc close(socket: SocketHandle) {....raises: [], tags: [], forbids: [].}

Closes a socket. Source Edit

  1. proc createNativeSocket(domain: cint; sockType: cint; protocol: cint;
  2. inheritable: bool = defined(nimInheritHandles)): SocketHandle {.
  3. ...raises: [], tags: [], forbids: [].}

Creates a new socket; returns osInvalidSocket if an error occurs.

inheritable decides if the resulting SocketHandle can be inherited by child processes.

Use this overload if one of the enums specified above does not contain what you need.

Source Edit

  1. proc createNativeSocket(domain: Domain = AF_INET;
  2. sockType: SockType = SOCK_STREAM;
  3. protocol: Protocol = IPPROTO_TCP;
  4. inheritable: bool = defined(nimInheritHandles)): SocketHandle {.
  5. ...raises: [], tags: [], forbids: [].}

Creates a new socket; returns osInvalidSocket if an error occurs.

inheritable decides if the resulting SocketHandle can be inherited by child processes.

Source Edit

  1. proc getAddrInfo(address: string; port: Port; domain: Domain = AF_INET;
  2. sockType: SockType = SOCK_STREAM;
  3. protocol: Protocol = IPPROTO_TCP): ptr AddrInfo {.
  4. ...raises: [OSError], tags: [], forbids: [].}

Warning: The resulting ptr AddrInfo must be freed using freeAddrInfo!

Source Edit

  1. proc getAddrString(sockAddr: ptr SockAddr): string {.
  2. ...raises: [Exception, OSError, IOError], tags: [], forbids: [].}

Returns the string representation of address within sockAddr Source Edit

  1. proc getAddrString(sockAddr: ptr SockAddr; strAddress: var string) {.
  2. ...raises: [Exception, OSError, IOError], tags: [], forbids: [].}

Stores in strAddress the string representation of the address inside sockAddr

Note

  • strAddress must be initialized to 46 in length.

Source Edit

  1. proc getHostByAddr(ip: string): Hostent {....tags: [ReadIOEffect],
  2. raises: [OSError, IOError, Exception], forbids: [].}

This function will lookup the hostname of an IP Address. Source Edit

  1. proc getHostByName(name: string): Hostent {....tags: [ReadIOEffect],
  2. raises: [OSError], forbids: [].}

This function will lookup the IP address of a hostname. Source Edit

  1. proc getHostname(): string {....tags: [ReadIOEffect], raises: [OSError],
  2. forbids: [].}

Returns the local hostname (not the FQDN) Source Edit

  1. proc getLocalAddr(socket: SocketHandle; domain: Domain): (string, Port) {.
  2. ...raises: [OSError, Exception], tags: [], forbids: [].}

Returns the socket’s local address and port number.

Similar to POSIX’s getsockname.

Source Edit

  1. proc getPeerAddr(socket: SocketHandle; domain: Domain): (string, Port) {.
  2. ...raises: [OSError, Exception], tags: [], forbids: [].}

Returns the socket’s peer address and port number.

Similar to POSIX’s getpeername

Source Edit

  1. proc getProtoByName(name: string): int {....raises: [OSError], tags: [],
  2. forbids: [].}

Returns a protocol code from the database that matches the protocol name. Source Edit

  1. proc getServByName(name, proto: string): Servent {....tags: [ReadIOEffect],
  2. raises: [OSError], forbids: [].}

Searches the database from the beginning and finds the first entry for which the service name specified by name matches the s_name member and the protocol name specified by proto matches the s_proto member.

On posix this will search through the /etc/services file.

Source Edit

  1. proc getServByPort(port: Port; proto: string): Servent {....tags: [ReadIOEffect],
  2. raises: [OSError], forbids: [].}

Searches the database from the beginning and finds the first entry for which the port specified by port matches the s_port member and the protocol name specified by proto matches the s_proto member.

On posix this will search through the /etc/services file.

Source Edit

  1. proc getSockDomain(socket: SocketHandle): Domain {....raises: [OSError, IOError],
  2. tags: [], forbids: [].}

Returns the socket’s domain (AF_INET or AF_INET6). Source Edit

  1. proc getSockName(socket: SocketHandle): Port {....raises: [OSError], tags: [],
  2. forbids: [].}

Returns the socket’s associated port number. Source Edit

  1. proc getSockOptInt(socket: SocketHandle; level, optname: int): int {.
  2. ...tags: [ReadIOEffect], raises: [OSError], forbids: [].}

getsockopt for integer options. Source Edit

  1. proc ioctlsocket(s: SocketHandle; cmd: clong; argptr: ptr clong): cint {.
  2. stdcall, importc: "ioctlsocket", dynlib: "ws2_32.dll", ...raises: [], tags: [],
  3. forbids: [].}

Source Edit

  1. proc listen(socket: SocketHandle; backlog = SOMAXCONN): cint {.
  2. ...tags: [ReadIOEffect], raises: [], forbids: [].}

Marks socket as accepting connections. Backlog specifies the maximum length of the queue of pending connections. Source Edit

  1. proc ntohl(x: uint32): uint32 {....raises: [], tags: [], forbids: [].}

Converts 32-bit unsigned integers from network to host byte order. On machines where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 4-byte swap operation. Source Edit

  1. proc ntohs(x: uint16): uint16 {....raises: [], tags: [], forbids: [].}

Converts 16-bit unsigned integers from network to host byte order. On machines where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 2-byte swap operation. Source Edit

  1. proc selectRead(readfds: var seq[SocketHandle]; timeout = 500): int {.
  2. ...raises: [], tags: [], forbids: [].}

When a socket in readfds is ready to be read from then a non-zero value will be returned specifying the count of the sockets which can be read from. The sockets which cannot be read from will also be removed from readfds.

timeout is specified in milliseconds and -1 can be specified for an unlimited time.

Source Edit

  1. proc selectWrite(writefds: var seq[SocketHandle]; timeout = 500): int {.
  2. ...tags: [ReadIOEffect], raises: [], forbids: [].}

When a socket in writefds is ready to be written to then a non-zero value will be returned specifying the count of the sockets which can be written to. The sockets which cannot be written to will also be removed from writefds.

timeout is specified in milliseconds and -1 can be specified for an unlimited time.

Source Edit

  1. proc setBlocking(s: SocketHandle; blocking: bool) {....raises: [OSError], tags: [],
  2. forbids: [].}

Sets blocking mode on socket.

Raises OSError on error.

Source Edit

  1. proc setInheritable(s: SocketHandle; inheritable: bool): bool {.inline,
  2. ...raises: [], tags: [], forbids: [].}

Set whether a socket is inheritable by child processes. Returns true on success.

This function is not implemented on all platform, test for availability with declared() <system.html#declared,untyped>.

Source Edit

  1. proc setSockOptInt(socket: SocketHandle; level, optname, optval: int) {.
  2. ...tags: [WriteIOEffect], raises: [OSError], forbids: [].}

setsockopt for integer options. Source Edit

  1. proc toInt(domain: Domain): cint {....raises: [], tags: [], forbids: [].}

Converts the Domain enum to a platform-dependent cint. Source Edit

  1. proc toInt(p: Protocol): cint {....raises: [], tags: [], forbids: [].}

Converts the Protocol enum to a platform-dependent cint. Source Edit

  1. proc toInt(typ: SockType): cint {....raises: [], tags: [], forbids: [].}

Converts the SockType enum to a platform-dependent cint. Source Edit

  1. proc toKnownDomain(family: cint): Option[Domain] {....raises: [], tags: [],
  2. forbids: [].}

Converts the platform-dependent cint to the Domain or none(), if the cint is not known. Source Edit

  1. proc toSockType(protocol: Protocol): SockType {....raises: [], tags: [],
  2. forbids: [].}

Source Edit

Templates

  1. template htonl(x: uint32): untyped

Converts 32-bit unsigned integers from host to network byte order. On machines where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 4-byte swap operation. Source Edit

  1. template htons(x: uint16): untyped

Converts 16-bit unsigned integers from host to network byte order. On machines where the host byte order is the same as network byte order, this is a no-op; otherwise, it performs a 2-byte swap operation. Source Edit

Exports

WSAEWOULDBLOCK, WSAECONNRESET, WSAECONNABORTED, WSAENETRESET, WSANOTINITIALISED, WSAENOTSOCK, WSAEINPROGRESS, WSAEINTR, WSAEDISCON, ERROR_NETNAME_DELETED, SocketHandle, Sockaddr_in, AddrInfo, INADDR_ANY, SockAddr, SockLen, Sockaddr_in6, Sockaddr_storage, recv, recv, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, \==, connect, send, send, accept, recvfrom, sendto, freeAddrInfo, inet_ntoa, SO_ERROR, SOL_SOCKET, SOMAXCONN, SO_ACCEPTCONN, SO_BROADCAST, SO_DEBUG, SO_DONTROUTE, SO_KEEPALIVE, SO_OOBINLINE, SO_REUSEADDR, SO_REUSEPORT, MSG_PEEK