% This LaTeX document was generated using the LaTeX backend of PlDoc, % The SWI-Prolog documentation system \section{library(socket): Network socket (TCP and UDP) library} \label{sec:socket} The \file{library(socket)} provides TCP and UDP inet-domain sockets from SWI-Prolog, both client and server-side communication. The interface of this library is very close to the Unix socket interface, also supported by the MS-Windows \textit{winsock} API. SWI-Prolog applications that wish to communicate with multiple sources have two options: \begin{itemize} \item Use I/O multiplexing based on \predref{wait_for_input}{3}. On Windows systems this can only be used for sockets, not for general (device-) file handles. \item Use multiple threads, handling either a single blocking socket or a pool using I/O multiplexing as above. \end{itemize} \subsection{Client applications} \label{sec:socket-server} Using this library to establish a TCP connection to a server is as simple as opening a file. See also \predref{http_open}{3}. \begin{code} dump_swi_homepage :- setup_call_cleanup( tcp_connect('www.swi-prolog.org':http, Stream, []), ( format(Stream, 'GET / HTTP/1.1~n\c Host: www.swi-prolog.org~n\c Connection: close~n~n', []), flush_output(Stream), copy_stream_data(Stream, current_output) ), close(Stream)). \end{code} To deal with timeouts and multiple connections, threads, \predref{wait_for_input}{3} and/or non-blocking streams (see \predref{tcp_fcntl}{3}) can be used. \subsection{Server applications} \label{sec:socket-client} The typical sequence for generating a server application is given below. To close the server, use \predref{close}{1} on \arg{AcceptFd}. \begin{code} create_server(Port) :- tcp_socket(Socket), tcp_bind(Socket, Port), tcp_listen(Socket, 5), tcp_open_socket(Socket, AcceptFd, _), \end{code} There are various options for $<$dispatch$>$. The most commonly used option is to start a Prolog thread to handle the connection. Alternatively, input from multiple clients can be handled in a single thread by listening to these clients using \predref{wait_for_input}{3}. Finally, on Unix systems, we can use \predref{fork}{1} to handle the connection in a new process. Note that \predref{fork}{1} and threads do not cooperate well. Combinations can be realised but require good understanding of POSIX thread and fork-semantics. Below is the typical example using a thread. Note the use of \predref{setup_call_cleanup}{3} to guarantee that all resources are reclaimed, also in case of failure or exceptions. \begin{code} dispatch(AcceptFd) :- tcp_accept(AcceptFd, Socket, Peer), thread_create(process_client(Socket, Peer), _, [ detached(true) ]), dispatch(AcceptFd). process_client(Socket, Peer) :- setup_call_cleanup( tcp_open_socket(Socket, StreamPair), handle_service(StreamPair), close(StreamPair)). handle_service(StreamPair) :- ... \end{code} \subsection{Socket exceptions} \label{sec:socket-exceptions} Errors that are trapped by the low-level library are mapped to an exception of the shape below. In this term, \arg{Code} is a lower case atom that corresponds to the C macro name, e.g., \const{epipe} for a broken pipe. \arg{Message} is the human readable string for the error code returned by the OS or the same as \arg{Code} if the OS does not provide this functionality. Note that \arg{Code} is derived from a static set of macros that may or may not be defines for the target OS. If the macro name is not known, \arg{Code} is \verb$ERROR_nnn$, where \textit{nnn} is an integer. \begin{code} error(socket_error(Code, Message), _) \end{code} Note that on Windows \arg{Code} is a \verb$wsa*$ code which makes it hard to write portable code that handles specific socket errors. Even on POSIX systems the exact set of errors produced by the network stack is not defined. \subsection{Socket addresses (families)} \label{sec:socket-domains} The library supports both IP4 and IP6 addresses. On Unix systems it also supports \textit{Unix domain sockets} (\verb$AF_UNIX$). The address of a Unix domain sockets is a file name. Unix domain sockets are created using \predref{socket_create}{2} or \predref{unix_domain_socket}{1}. IP4 or IP6 sockets can be created using \predref{socket_create}{2} or \predref{tcp_connect}{3} with the \const{inet} (default, IP3) or \const{inet6} domain option. Some of the predicates produce or consume IP addresses as a Prolog term. The format of this term is one of: \begin{description} \termitem{ip}{A, B, C, D} Represents an IP4 address. Each field is an integer in the range 0..255 (8 bit). \termitem{ip}{A, B, C, D, E, F, G, H} Represents an IP6 address. Each field is an integer in the range 0..65535 (16 bit). \end{description} The predicate \predref{ip_name}{2} translates between the canonical textual representation and the above defined address terms. \subsection{Socket predicate reference} \label{sec:socket-predicates}\vspace{0.7cm} \begin{description} \predicate[det]{socket_create}{2}{-SocketId, +Options} Create a socket according to \arg{Options}. Supported \arg{Options} are: \begin{description} \termitem{domain}{+Domain} One of \const{inet} (default), \const{inet6}, \const{unix} or \const{local} (same as \const{unix}) \termitem{type}{+Type} One of \const{stream} (default) to create a TCP connection or \const{dgram} to create a UDP socket. \end{description} This predicate subsumes tcp_socket/1m, \predref{udp_socket}{1} and \predref{unix_domain_socket}{1}. \predicate[det]{tcp_socket}{1}{-SocketId} Equivalent to \verb$socket_create(SocketId, [])$ or, explicit, \verb$socket_create(SocketId, [domain(inet), type(stream)])$. \predicate[det]{unix_domain_socket}{1}{-SocketId} Equivalent to \verb$socket_create(SocketId, [domain(unix)])$ or, explicit, \verb$socket_create(SocketId, [domain(unix), type(stream)])$ Unix domain socket affect \predref{tcp_connect}{2} (for clients) and \predref{tcp_bind}{2} and \predref{tcp_accept}{3} (for servers). The address is an atom or string that is handled as a file name. On most systems the length of this file name is limited to 128 bytes (including null terminator), but according to the Linux documentation (\verb$unix(7)$), portable applications must keep the address below 92 bytes. Note that these lengths are in bytes. Non-ascii characters may be represented as multiple bytes. If the length limit is exceeded a \verb$representation_error(af_unix_name)$ exception is raised. \predicate[det]{tcp_close_socket}{1}{+SocketId} Closes the indicated socket, making \arg{SocketId} invalid. Normally, sockets are closed by closing both stream handles returned by \predref{open_socket}{3}. There are two cases where \predref{tcp_close_socket}{1} is used because there are no stream-handles: \begin{itemize} \item If, after \predref{tcp_accept}{3}, the server uses \predref{fork}{1} to handle the client in a sub-process. In this case the accepted socket is not longer needed from the main server and must be discarded using \predref{tcp_close_socket}{1}. \item If, after discovering the connecting client with \predref{tcp_accept}{3}, the server does not want to accept the connection, it should discard the accepted socket immediately using \predref{tcp_close_socket}{1}. \end{itemize} \predicate[det]{tcp_open_socket}{2}{+SocketId, -StreamPair} Create streams to communicate to \arg{SocketId}. If \arg{SocketId} is a master socket (see \predref{tcp_bind}{2}), \arg{StreamPair} should be used for \predref{tcp_accept}{3}. If \arg{SocketId} is a connected (see \predref{tcp_connect}{2}) or accepted socket (see \predref{tcp_accept}{3}), \arg{StreamPair} is unified to a stream pair (see \predref{stream_pair}{3}) that can be used for reading and writing. The stream or pair must be closed with \predref{close}{1}, which also closes \arg{SocketId}. \predicate[det]{tcp_open_socket}{3}{+SocketId, -InStream, -OutStream} Similar to \predref{tcp_open_socket}{2}, but creates two separate sockets where \predref{tcp_open_socket}{2} would have created a stream pair. \begin{tags} \tag{deprecated} New code should use \predref{tcp_open_socket}{2} because closing a stream pair is much easier to perform safely. \end{tags} \predicate[det]{tcp_bind}{2}{SocketId, ?Address} Bind the socket to \arg{Address} on the current machine. This operation, together with \predref{tcp_listen}{2} and \predref{tcp_accept}{3} implement the \textit{server-side} of the socket interface. \arg{Address} is either an plain \arg{Port} or a term HostPort. The first form binds the socket to the given port on all interfaces, while the second only binds to the matching interface. A typical example is below, causing the socket to listen only on port 8080 on the local machine's network. \begin{code} tcp_bind(Socket, localhost:8080) \end{code} If \arg{Port} is unbound, the system picks an arbitrary free port and unifies \arg{Port} with the selected port number. \arg{Port} is either an integer or the name of a registered service. See also \predref{tcp_connect}{4}. \predicate[det]{tcp_listen}{2}{+SocketId, +BackLog} Tells, after \predref{tcp_bind}{2}, the socket to listen for incoming requests for connections. Backlog indicates how many pending connection requests are allowed. Pending requests are requests that are not yet acknowledged using \predref{tcp_accept}{3}. If the indicated number is exceeded, the requesting client will be signalled that the service is currently not available. A commonly used default value for Backlog is 5. \predicate[det]{tcp_accept}{3}{+Socket, -Slave, -Peer} This predicate waits on a server socket for a connection request by a client. On success, it creates a new socket for the client and binds the identifier to \arg{Slave}. \arg{Peer} is bound to the IP-address of the client or the atom \verb$af_unix$ if \arg{Socket} is an AF_UNIX socket (see \predref{unix_domain_socket}{1}). \predicate[det]{tcp_connect}{2}{+SocketId, +Address} Connect \arg{SocketId}. After successful completion, \predref{tcp_open_socket}{3} can be used to create I/O-Streams to the remote socket. This predicate is part of the low level client API. A connection to a particular host and port is realised using these steps: \begin{code} tcp_socket(Socket), tcp_connect(Socket, Host:Port), tcp_open_socket(Socket, StreamPair) \end{code} Typical client applications should use the high level interface provided by \predref{tcp_connect}{3} which avoids resource leaking if a step in the process fails, and can be hooked to support proxies. For example: \begin{code} setup_call_cleanup( tcp_connect(Host:Port, StreamPair, []), talk(StreamPair), close(StreamPair)) \end{code} If \arg{SocketId} is an AF_UNIX socket (see \predref{unix_domain_socket}{1}), \arg{Address} is an atom or string denoting a file name. \predicate[det]{tcp_connect}{4}{+Socket, +Address, -Read, -Write} Connect a (client) socket to \arg{Address} and return a bi-directional connection through the stream-handles \arg{Read} and \arg{Write}. This predicate may be hooked by defining \qpredref{socket}{tcp_connect_hook}{4} with the same signature. Hooking can be used to deal with proxy connections. E.g., \begin{code} :- multifile socket:tcp_connect_hook/4. socket:tcp_connect_hook(Socket, Address, Read, Write) :- proxy(ProxyAdress), tcp_connect(Socket, ProxyAdress), tcp_open_socket(Socket, Read, Write), proxy_connect(Address, Read, Write). \end{code} \begin{tags} \tag{deprecated} New code should use \predref{tcp_connect}{3} called as \verb$tcp_connect(+Address, -StreamPair, +Options)$. \end{tags} \predicate[det]{tcp_connect}{3}{+Address, -StreamPair, +Options} \nodescription \predicate[det]{tcp_connect}{3}{+Socket, +Address, -StreamPair} Establish a TCP communication as a client. The +,-,+ mode is the preferred way for a client to establish a connection. This predicate can be hooked to support network proxies. To use a proxy, the hook \predref{proxy_for_url}{3} must be defined. Permitted options are: \begin{description} \termitem{bypass_proxy}{+Boolean} Defaults to \const{false}. If \const{true}, do not attempt to use any proxies to obtain the connection \termitem{nodelay}{+Boolean} Defaults to \const{false}. If \const{true}, set nodelay on the resulting socket using \verb$tcp_setopt(Socket, nodelay)$ \termitem{domain}{+Domain} One of `inet' or \const{inet6}. When omitted we use \predref{host_address}{2} with \verb$type(stream)$ and try the returned addresses in order. \end{description} The +,+,- mode is deprecated and does not support proxies. It behaves like \predref{tcp_connect}{4}, but creates a stream pair (see \predref{stream_pair}{3}). \begin{arguments} \arg{Address} & is either a Host:Port term or a file name (atom or string). The latter connects to an AF_UNIX socket and requires \predref{unix_domain_socket}{1}. \\ \end{arguments} \begin{tags} \tag{Errors} \verb$proxy_error(tried(ResultList))$ is raised by mode (+,-,+) if proxies are defines by \predref{proxy_for_url}{3} but no proxy can establsh the connection. \arg{ResultList} contains one or more terms of the form \verb$false(Proxy)$ for a hook that simply failed or \verb$error(Proxy, ErrorTerm)$ for a hook that raised an exception. \tag{See also} \file{library(http/http_proxy)} defines a hook that allows to connect through HTTP proxies that support the \verb$CONNECT$ method. \end{tags} \predicate{tcp_select}{3}{+ListOfStreams, -ReadyList, +TimeOut} Same as the built-in \predref{wait_for_input}{3}. Used to allow for interrupts and timeouts on Windows. A redesign of the Windows socket interface makes it impossible to do better than Windows \verb$select()$ call underlying \predref{wait_for_input}{3}. As input multiplexing typically happens in a background thread anyway we accept the loss of timeouts and interrupts. \begin{tags} \tag{deprecated} Use \predref{wait_for_input}{3} \end{tags} \predicate[semidet,multifile]{try_proxy}{4}{+Proxy, +TargetAddress, -Socket, -StreamPair} Attempt a socket-level connection via the given proxy to \arg{TargetAddress}. The \arg{Proxy} argument must match the output argument of \predref{proxy_for_url}{3}. The predicate \predref{tcp_connect}{3} (and \predref{http_open}{3} from the \file{library(http/http_open)}) collect the results of failed proxies and raise an exception no proxy is capable of realizing the connection. The default implementation recognises the values for \arg{Proxy} described below. The \file{library(http/http_proxy)} adds \verb$proxy(Host,Port)$ which allows for HTTP proxies using the \verb$CONNECT$ method. \begin{description} \termitem{direct}{} Do not use any proxy \termitem{socks}{Host, Port} Use a SOCKS5 proxy \end{description} \predicate[nondet,multifile]{proxy_for_url}{3}{+URL, +Hostname, -Proxy} This hook can be implemented to return a proxy to try when connecting to \arg{URL}. Returned proxies are tried in the order in which they are returned by the multifile hook \predref{try_proxy}{4}. Pre-defined proxy methods are: \begin{description} \termitem{direct}{} connect directly to the resource \termitem{proxy}{Host, Port} Connect to the resource using an HTTP proxy. If the resource is not an HTTP \arg{URL}, then try to connect using the CONNECT verb, otherwise, use the GET verb. \termitem{socks}{Host, Port} Connect to the resource via a SOCKS5 proxy \end{description} These correspond to the proxy methods defined by PAC \href{http://en.wikipedia.org/wiki/Proxy_auto-config}{\arg{Proxy} auto-config}. Additional methods can be returned if suitable clauses for \qpredref{http}{http_connection_over_proxy}{6} or \predref{try_proxy}{4} are defined. \predicate[det]{udp_socket}{1}{-SocketId} Equivalent to \verb$socket_create(SocketId, [type(dgram)])$ or, explicit, \verb$socket_create(SocketId, [domain(inet), type(dgram)])$. \predicate[det]{udp_receive}{4}{+Socket, -Data, -From, +Options} Wait for and return the next datagram. The \arg{Data} is returned as a Prolog term depending on \arg{Options}. \arg{From} is a term of the format Ip:Port indicating the sender of the message. Here, \arg{Ip} is either an ip4 or ip6 structure. \arg{Socket} can be waited for using \predref{wait_for_input}{3}. Defined \arg{Options}: \begin{description} \termitem{as}{+Type} Defines the type for \arg{Data}. Possible values are \const{atom}, \const{codes}, \const{string} (default) or \const{term} (parse as Prolog term). \termitem{encoding}{+Encoding} Specify the encoding used to interpret the message. It is one of \const{octet}. \verb$iso_latin_1$, \const{text} or \const{utf8}. \termitem{max_message_size}{+Size} Specify the maximum number of bytes to read from a UDP datagram. \arg{Size} must be within the range 0-65535. If unspecified, a maximum of 4096 bytes will be read. \end{description} For example: \begin{code} receive(Port) :- udp_socket(Socket), tcp_bind(Socket, Port), repeat, udp_receive(Socket, Data, From, [as(atom)]), format('Got ~q from ~q~n', [Data, From]), fail. \end{code} \predicate[det]{udp_send}{4}{+Socket, +Data, +To, +Options} Send a UDP message. \arg{Data} is a string, atom or code-list providing the data. \arg{To} is an address of the form Host:Port where Host is either the hostname or an IP address. Defined \arg{Options} are: \begin{description} \termitem{encoding}{+Encoding} Specifies the encoding to use for the string. See \predref{udp_receive}{4} for details \termitem{as}{+Type} This uses the same values for \arg{Type} as the \verb$as(Type)$ option of \predref{udp_receive}{4}. The are interpreted differently though. No \arg{Type} corresponds to CVT_ALL of PL_get_chars(). Using atom corresponds to CVT_ATOM and any of string or codes is mapped to CVT_STRING\Sbar{}CVT_LIST, allowing for a SWI-Prolog string object, list of character codes or list of characters. Finally, \const{term} maps to CVT_WRITE_CANONICAL. This implies that arbitrary Prolog terms can be sent reliably using the option list `[\verb$as(term)$,\verb$encoding(utf8)$])`, using the same option list for \predref{udp_receive}{4}. \end{description} For example \begin{code} send(Host, Port, Message) :- udp_socket(S), udp_send(S, Message, Host:Port, []), tcp_close_socket(S). \end{code} A broadcast is achieved by using \verb$tcp_setopt(Socket, broadcast)$ prior to sending the datagram and using the local network broadcast address as a \predref{ip}{4} term. \predicate[det]{tcp_setopt}{2}{+SocketId, +Option} Set options on the socket. Defined options are: \begin{description} \termitem{reuseaddr}{} Allow servers to reuse a port without the system being completely sure the port is no longer in use. \termitem{bindtodevice}{+Device} Bind the socket to \arg{Device} (an atom). For example, the code below binds the socket to the \textit{loopback} device that is typically used to realise the \textit{localhost}. See the manual pages for \verb$setsockopt()$ and the socket interface (e.g., \verb$socket(7)$ on Linux) for details. \begin{code} tcp_socket(Socket), tcp_setopt(Socket, bindtodevice(lo)) \end{code} \termitem{nodelay}{} \termitem{nodelay}{true} If \const{true}, disable the Nagle optimization on this socket, which is enabled by default on almost all modern TCP/IP stacks. The Nagle optimization joins small packages, which is generally desirable, but sometimes not. Please note that the underlying TCP_NODELAY setting to \verb$setsockopt()$ is not available on all platforms and systems may require additional privileges to change this option. If the option is not supported, \predref{tcp_setopt}{2} raises a domain_error exception. See \href{http://en.wikipedia.org/wiki/Nagle's_algorithm}{Wikipedia} for details. \termitem{broadcast}{} UDP sockets only: broadcast the package to all addresses matching the address. The address is normally the address of the local subnet (i.e. 192.168.1.255). See \predref{udp_send}{4}. \termitem{ip_add_membership}{+MultiCastGroup} \termitem{ip_add_membership}{+MultiCastGroup, +LocalInterface} \termitem{ip_add_membership}{+MultiCastGroup, +LocalInterface, +InterfaceIndex} \termitem{ip_drop_membership}{+MultiCastGroup} \termitem{ip_drop_membership}{+MultiCastGroup, +LocalInterface} \termitem{ip_drop_membership}{+MultiCastGroup, +LocalInterface, +InterfaceIndex} Join/leave a multicast group. Calls \verb$setsockopt()$ with the corresponding arguments. \termitem{dispatch}{+Boolean} In GUI environments (using XPCE or the Windows \verb$swipl-win.exe$ executable) this flags defines whether or not any events are dispatched on behalf of the user interface. Default is \const{true}. Only very specific situations require setting this to \const{false}. \termitem{sndbuf}{+Integer} Sets the send buffer size to \arg{Integer} (bytes). On Windows this defaults (now) to 64kb. Higher latency links may benefit from increasing this further since the maximum theoretical throughput on a link is given by buffer-size / latency. See \url{https://support.microsoft.com/en-gb/help/823764/slow-performance-occurs-when-you-copy-data-to-a-tcp-server-by-using-a} for Microsoft's discussion \end{description} \predicate[det]{tcp_fcntl}{3}{+Stream, +Action, ?Argument} Interface to the \verb$fcntl()$ call. Currently only suitable to deal switch stream to non-blocking mode using: \begin{code} tcp_fcntl(Stream, setfl, nonblock), \end{code} An attempt to read from a non-blocking stream while there is no data available returns -1 (or \verb$end_of_file$ for \predref{read}{1}), but \predref{at_end_of_stream}{1} fails. On actual end-of-input, \predref{at_end_of_stream}{1} succeeds. \predicate[semidet]{tcp_getopt}{2}{+Socket, ?Option} Get information about \arg{Socket}. Defined properties are below. Requesting an unknown option results in a \verb$domain_error$ exception. \begin{description} \termitem{file_no}{-File} Get the OS file handle as an integer. This may be used for debugging and integration. \end{description} \predicate[nondet]{host_address}{3}{+HostName, -Address, +Options} \nodescription \predicate[det]{host_address}{3}{-HostName, +Address, +Options} Translate between a machines host-name and it's (IP-)address. Supported options: \begin{description} \termitem{domain}{+Domain} One of \const{inet} or \const{inet6} to limit the results to the given family. \termitem{type}{+Type} One of \const{stream} or \const{dgram}. \termitem{canonname}{+Boolean} If \const{true} (default \const{false}), return the canonical host name in the frist answer \end{description} In mode (+,-,+) \arg{Address} is unified to a dict with the following keys: \begin{description} \termitem{address}{} A Prolog terms describing the ip address. \termitem{domain}{} One of \const{inet} or \const{inet6}. The underlying \verb$getaddrinfo()$ calls this \const{family}. We use \const{domain} for consistency with \predref{socket_create}{2}. \termitem{type}{} Currently one of \const{stream} or \const{dgram}. \termitem{host}{} Available if \verb$canonname(true)$ is specified on the first returned address. Holds the official canonical host name. \end{description} \predicate[det]{tcp_host_to_address}{2}{?HostName, ?Address} Translate between a machines host-name and it's (IP-)address. If \arg{HostName} is an atom, it is resolved using \verb$getaddrinfo()$ and the IP-number is unified to \arg{Address} using a term of the format \verb$ip(Byte1,Byte2,Byte3,Byte4)$. Otherwise, if \arg{Address} is bound to an \verb$ip(Byte1,Byte2,Byte3,Byte4)$ term, it is resolved by \verb$gethostbyaddr()$ and the canonical hostname is unified with \arg{HostName}. \begin{tags} \tag{deprecated} New code should use \predref{host_address}{3}. This version is bootstrapped from \predref{host_address}{3} and only searches for IP4 addresses that support TCP connections. \end{tags} \predicate[det]{gethostname}{1}{-Hostname} Return the canonical fully qualified name of this host. This is achieved by calling \verb$gethostname()$ and return the canonical name returned by \verb$getaddrinfo()$. \predicate[det]{ip_name}{2}{?IP, ?Name} Translate between the textual representation of an \arg{IP} address and the Prolog data structure. Prolog represents ip4 addresses as \verb$ip(A,B,C,D)$ and ip6 addresses as \verb$ip(A,B,C,D,E,F,H)$. For example: \begin{code} ?- ip_name(ip(1,2,3,4), Name) Name = '1.2.3.4'. ?- ip_name(IP, '::'). IP = ip(0,0,0,0,0,0,0,0). ?- ip_name(IP, '1:2::3'). IP = ip(1,2,0,0,0,0,0,3). \end{code} \predicate[det]{negotiate_socks_connection}{2}{+DesiredEndpoint, +StreamPair} Negotiate a connection to \arg{DesiredEndpoint} over \arg{StreamPair}. \arg{DesiredEndpoint} should be in the form of either: \begin{shortlist} \item hostname : port \item \verb$ip(A,B,C,D)$ : port \end{shortlist} \begin{tags} \tag{Errors} \verb$socks_error(Details)$ if the SOCKS negotiation failed. \end{tags} \end{description}