View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jan Wielemaker
    4    E-mail:        J.Wielemaker@vu.nl
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2000-2023, University of Amsterdam
    7                              VU University Amsterdam
    8                              CWI, Amsterdam
    9                              SWI-Prolog Solutions b.v.
   10    All rights reserved.
   11
   12    Redistribution and use in source and binary forms, with or without
   13    modification, are permitted provided that the following conditions
   14    are met:
   15
   16    1. Redistributions of source code must retain the above copyright
   17       notice, this list of conditions and the following disclaimer.
   18
   19    2. Redistributions in binary form must reproduce the above copyright
   20       notice, this list of conditions and the following disclaimer in
   21       the documentation and/or other materials provided with the
   22       distribution.
   23
   24    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   25    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   26    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   27    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   28    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   29    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   30    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   31    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   32    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   33    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   34    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   35    POSSIBILITY OF SUCH DAMAGE.
   36*/
   37
   38:- module(socket,
   39          [ socket_create/2,		% -Socket, +Options
   40	    tcp_socket/1,               % -Socket
   41            tcp_close_socket/1,         % +Socket
   42            tcp_open_socket/3,          % +Socket, -Read, -Write
   43            tcp_connect/2,              % +Socket, +Address
   44            tcp_connect/3,              % +Address, -StreamPair, +Options
   45            tcp_connect/4,              % +Socket, +Address, -Read, -Write)
   46            tcp_bind/2,                 % +Socket, +Address
   47            tcp_accept/3,               % +Master, -Slave, -PeerName
   48            tcp_listen/2,               % +Socket, +BackLog
   49            tcp_fcntl/3,                % +Socket, +Command, ?Arg
   50            tcp_setopt/2,               % +Socket, +Option
   51            tcp_getopt/2,               % +Socket, ?Option
   52	    host_address/3,		% ?HostName, ?Address, +Options
   53            tcp_host_to_address/2,      % ?HostName, ?Ip-nr
   54            tcp_select/3,               % +Inputs, -Ready, +Timeout
   55            gethostname/1,              % -HostName
   56
   57	    ip_name/2,			% ?Ip, ?Name
   58
   59            tcp_open_socket/2,          % +Socket, -StreamPair
   60
   61            udp_socket/1,               % -Socket
   62            udp_receive/4,              % +Socket, -Data, -Sender, +Options
   63            udp_send/4,                 % +Socket, +Data, +Sender, +Options
   64
   65            negotiate_socks_connection/2% +DesiredEndpoint, +StreamPair
   66          ]).   67:- autoload(library(debug), [assertion/1, debug/3]).   68:- autoload(library(lists), [last/2, member/2, append/3, append/2]).   69:- autoload(library(apply), [maplist/3, maplist/2]).   70:- autoload(library(error),
   71            [instantiation_error/1, syntax_error/1, must_be/2, domain_error/2]).   72:- autoload(library(option), [option/2, option/3]).   73
   74/** <module> Network socket (TCP and UDP) library
   75
   76The library(socket) provides  TCP  and   UDP  inet-domain  sockets  from
   77SWI-Prolog, both client and server-side  communication. The interface of
   78this library is very close to the  Unix socket interface, also supported
   79by the MS-Windows _winsock_ API. SWI-Prolog   applications  that wish to
   80communicate with multiple sources have two options:
   81
   82  - Use I/O multiplexing based on wait_for_input/3.  On Windows
   83    systems this can only be used for sockets, not for general
   84    (device-) file handles.
   85  - Use multiple threads, handling either a single blocking socket
   86    or a pool using I/O multiplexing as above.
   87
   88## Client applications  {#socket-server}
   89
   90Using this library to establish  a  TCP   connection  to  a server is as
   91simple as opening a file.  See also http_open/3.
   92
   93==
   94dump_swi_homepage :-
   95    setup_call_cleanup(
   96        tcp_connect('www.swi-prolog.org':http, Stream, []),
   97        ( format(Stream,
   98                 'GET / HTTP/1.1~n\c
   99                  Host: www.swi-prolog.org~n\c
  100                  Connection: close~n~n', []),
  101          flush_output(Stream),
  102          copy_stream_data(Stream, current_output)
  103        ),
  104        close(Stream)).
  105==
  106
  107To   deal   with   timeouts   and     multiple   connections,   threads,
  108wait_for_input/3 and/or non-blocking streams (see   tcp_fcntl/3)  can be
  109used.
  110
  111## Server applications  {#socket-client}
  112
  113The typical sequence for generating a server application is given below.
  114To close the server, use close/1 on `AcceptFd`.
  115
  116  ==
  117  create_server(Port) :-
  118        tcp_socket(Socket),
  119        tcp_bind(Socket, Port),
  120        tcp_listen(Socket, 5),
  121        tcp_open_socket(Socket, AcceptFd, _),
  122        <dispatch>
  123  ==
  124
  125There are various options for <dispatch>.  The most commonly used option
  126is to start a Prolog  thread   to  handle the connection. Alternatively,
  127input from multiple clients  can  be  handled   in  a  single  thread by
  128listening to these clients  using   wait_for_input/3.  Finally,  on Unix
  129systems, we can use fork/1 to handle   the  connection in a new process.
  130Note that fork/1 and threads do not  cooperate well. Combinations can be
  131realised  but  require  good   understanding    of   POSIX   thread  and
  132fork-semantics.
  133
  134Below  is  the  typical  example  using  a   thread.  Note  the  use  of
  135setup_call_cleanup/3 to guarantee that all resources are reclaimed, also
  136in case of failure or exceptions.
  137
  138  ==
  139  dispatch(AcceptFd) :-
  140          tcp_accept(AcceptFd, Socket, Peer),
  141          thread_create(process_client(Socket, Peer), _,
  142                        [ detached(true)
  143                        ]),
  144          dispatch(AcceptFd).
  145
  146  process_client(Socket, Peer) :-
  147          setup_call_cleanup(
  148              tcp_open_socket(Socket, StreamPair),
  149              handle_service(StreamPair),
  150              close(StreamPair)).
  151
  152  handle_service(StreamPair) :-
  153          ...
  154  ==
  155
  156## Socket exceptions			{#socket-exceptions}
  157
  158Errors that are trapped by  the  low-level   library  are  mapped  to an
  159exception of the shape below. In this term,  `Code` is a lower case atom
  160that corresponds to the C macro name,   e.g., `epipe` for a broken pipe.
  161`Message` is the human readable string for   the  error code returned by
  162the OS or  the  same  as  `Code`  if   the  OS  does  not  provide  this
  163functionality. Note that `Code` is derived from   a static set of macros
  164that may or may not be defines for the   target OS. If the macro name is
  165not known, `Code` is =|ERROR_nnn|=, where _nnn_ is an integer.
  166
  167    error(socket_error(Code, Message), _)
  168
  169Note that on Windows `Code` is a ``wsa*``   code  which makes it hard to
  170write portable code that handles specific   socket errors. Even on POSIX
  171systems the exact set of errors  produced   by  the network stack is not
  172defined.
  173
  174## Socket addresses (families)		{#socket-domains}
  175
  176The library supports both IP4 and IP6 addresses. On Unix systems it also
  177supports _Unix domain sockets_ (``AF_UNIX``).  The   address  of  a Unix
  178domain sockets is a file name.  Unix   domain  sockets are created using
  179socket_create/2 or unix_domain_socket/1.
  180
  181IP4 or IP6 sockets can be created using socket_create/2 or tcp_connect/3
  182with the `inet` (default, IP3) or  `inet6`   domain  option. Some of the
  183predicates produce or consume IP addresses as  a Prolog term. The format
  184of this term is one of:
  185
  186  - ip(A,B,C,D)
  187    Represents an IP4 address.  Each field is an integer in the range
  188    0..255 (8 bit).
  189  - ip(A,B,C,D,E,F,G,H)
  190    Represents an IP6 address.  Each field is an integer in the range
  191    0..65535 (16 bit).
  192
  193The  predicate  ip_name/2  translates  between   the  canonical  textual
  194representation and the above defined address terms.
  195
  196## Socket predicate reference           {#socket-predicates}
  197*/
  198
  199:- multifile
  200    tcp_connect_hook/3,             % +Socket, +Addr, -In, -Out
  201    tcp_connect_hook/4,             % +Socket, +Addr, -Stream
  202    proxy_for_url/3,                % +URL, +Host, -ProxyList
  203    try_proxy/4.                    % +Proxy, +Addr, -Socket, -Stream
  204
  205:- predicate_options(tcp_connect/3, 3,
  206                     [ bypass_proxy(boolean),
  207                       nodelay(boolean),
  208                       domain(oneof([inet,inet6]))
  209                     ]).  210
  211:- use_foreign_library(foreign(socket)).  212:- public tcp_debug/1.                  % set debugging.
  213
  214:- if(current_predicate(unix_domain_socket/1)).  215:- export(unix_domain_socket/1).  % -Socket
  216:- endif.  217
  218%!  socket_create(-SocketId, +Options) is det.
  219%
  220%   Create a socket according to Options.   Supported Options are:
  221%
  222%     - domain(+Domain)
  223%       One of `inet` (default), `inet6`, `unix` or `local` (same
  224%       as `unix`)
  225%     - type(+Type)
  226%       One of `stream` (default) to create a TCP connection or
  227%       `dgram` to create a UDP socket.
  228%
  229%   This   predicate   subsumes    tcp_socket/1m,   udp_socket/1   and
  230%   unix_domain_socket/1.
  231
  232%!  tcp_socket(-SocketId) is det.
  233%
  234%   Equivalent   to   socket_create(SocketId,    [])   or,   explicit,
  235%   socket_create(SocketId, [domain(inet), type(stream)]).
  236
  237%!  unix_domain_socket(-SocketId) is det.
  238%
  239%   Equivalent   to    socket_create(SocketId,   [domain(unix)])   or,
  240%   explicit, socket_create(SocketId, [domain(unix), type(stream)])
  241%
  242%   Unix  domain   socket  affect  tcp_connect/2  (for   clients)  and
  243%   tcp_bind/2 and tcp_accept/3 (for servers).  The address is an atom
  244%   or string  that is  handled as  a file name.  On most  systems the
  245%   length of this  file name is limited to 128  bytes (including null
  246%   terminator), but  according to the Linux  documentation (unix(7)),
  247%   portable applications must  keep the address below  92 bytes. Note
  248%   that  these lengths  are  in bytes.  Non-ascii  characters may  be
  249%   represented as multiple  bytes. If the length limit  is exceeded a
  250%   representation_error(af_unix_name) exception is raised.
  251
  252%!  tcp_close_socket(+SocketId) is det.
  253%
  254%   Closes the indicated socket, making  SocketId invalid. Normally,
  255%   sockets are closed by closing both   stream  handles returned by
  256%   open_socket/3. There are two cases   where tcp_close_socket/1 is
  257%   used because there are no stream-handles:
  258%
  259%     - If, after tcp_accept/3, the server uses fork/1 to handle the
  260%       client in a sub-process. In this case the accepted socket is
  261%       not longer needed from the main server and must be discarded
  262%       using tcp_close_socket/1.
  263%     - If, after discovering the connecting client with
  264%       tcp_accept/3, the server does not want to accept the
  265%       connection, it should discard the accepted socket
  266%       immediately using tcp_close_socket/1.
  267
  268%!  tcp_open_socket(+SocketId, -StreamPair) is det.
  269%
  270%   Create streams to communicate to  SocketId.   If  SocketId  is a
  271%   master socket (see tcp_bind/2), StreamPair   should  be used for
  272%   tcp_accept/3. If SocketId is a  connected (see tcp_connect/2) or
  273%   accepted socket (see tcp_accept/3), StreamPair   is unified to a
  274%   stream pair (see stream_pair/3) that can be used for reading and
  275%   writing. The stream or pair must   be closed with close/1, which
  276%   also closes SocketId.
  277
  278tcp_open_socket(Socket, Stream) :-
  279    tcp_open_socket(Socket, In, Out),
  280    (   var(Out)
  281    ->  Stream = In
  282    ;   stream_pair(Stream, In, Out)
  283    ).
  284
  285%!  tcp_open_socket(+SocketId, -InStream, -OutStream) is det.
  286%
  287%   Similar to tcp_open_socket/2, but creates   two separate sockets
  288%   where tcp_open_socket/2 would have created a stream pair.
  289%
  290%   @deprecated New code should use tcp_open_socket/2 because
  291%   closing a stream pair is much easier to perform safely.
  292
  293%!  tcp_bind(SocketId, ?Address) is det.
  294%
  295%   Bind  the  socket  to  Address  on  the  current  machine.  This
  296%   operation, together with tcp_listen/2 and tcp_accept/3 implement
  297%   the _server-side_ of the socket interface.  Address is either an
  298%   plain `Port` or a term HostPort. The first form binds the socket
  299%   to the given port on all interfaces, while the second only binds
  300%   to the matching interface. A typical   example is below, causing
  301%   the socket to listen only on port   8080  on the local machine's
  302%   network.
  303%
  304%     ==
  305%       tcp_bind(Socket, localhost:8080)
  306%     ==
  307%
  308%   If `Port` is unbound, the system   picks  an arbitrary free port
  309%   and unifies `Port` with the  selected   port  number.  `Port` is
  310%   either an integer or the name of  a registered service. See also
  311%   tcp_connect/4.
  312
  313%!  tcp_listen(+SocketId, +BackLog) is det.
  314%
  315%   Tells, after tcp_bind/2,  the  socket   to  listen  for incoming
  316%   requests for connections. Backlog  indicates   how  many pending
  317%   connection requests are allowed. Pending   requests are requests
  318%   that  are  not  yet  acknowledged  using  tcp_accept/3.  If  the
  319%   indicated number is exceeded,  the   requesting  client  will be
  320%   signalled  that  the  service  is  currently  not  available.  A
  321%   commonly used default value for Backlog is 5.
  322
  323%!  tcp_accept(+Socket, -Slave, -Peer) is det.
  324%
  325%   This predicate waits on a server socket  for a connection request by
  326%   a client. On success, it creates  a   new  socket for the client and
  327%   binds the identifier to Slave. Peer is   bound  to the IP-address of
  328%   the client or the atom `af_unix` if Socket is an AF_UNIX socket (see
  329%   unix_domain_socket/1).
  330
  331%!  tcp_connect(+SocketId, +Address) is det.
  332%
  333%   Connect SocketId. After successful completion, tcp_open_socket/3
  334%   can be used to create  I/O-Streams   to  the remote socket. This
  335%   predicate is part of the low level client API. A connection to a
  336%   particular host and port is realised using these steps:
  337%
  338%     ==
  339%         tcp_socket(Socket),
  340%         tcp_connect(Socket, Host:Port),
  341%         tcp_open_socket(Socket, StreamPair)
  342%     ==
  343%
  344%   Typical client applications should use  the high level interface
  345%   provided by tcp_connect/3 which  avoids   resource  leaking if a
  346%   step in the process fails, and can  be hooked to support proxies.
  347%   For example:
  348%
  349%     ==
  350%         setup_call_cleanup(
  351%             tcp_connect(Host:Port, StreamPair, []),
  352%             talk(StreamPair),
  353%             close(StreamPair))
  354%     ==
  355%
  356%   If SocketId is an AF_UNIX socket (see unix_domain_socket/1), Address
  357%   is an atom or string denoting a file name.
  358
  359
  360                 /*******************************
  361                 *      HOOKABLE CONNECT        *
  362                 *******************************/
  363
  364%!  tcp_connect(+Socket, +Address, -Read, -Write) is det.
  365%
  366%   Connect a (client) socket to Address and return a bi-directional
  367%   connection through the  stream-handles  Read   and  Write.  This
  368%   predicate may be hooked   by  defining socket:tcp_connect_hook/4
  369%   with the same signature. Hooking can be  used to deal with proxy
  370%   connections. E.g.,
  371%
  372%       ==
  373%       :- multifile socket:tcp_connect_hook/4.
  374%
  375%       socket:tcp_connect_hook(Socket, Address, Read, Write) :-
  376%           proxy(ProxyAdress),
  377%           tcp_connect(Socket, ProxyAdress),
  378%           tcp_open_socket(Socket, Read, Write),
  379%           proxy_connect(Address, Read, Write).
  380%       ==
  381%
  382%   @deprecated New code should use tcp_connect/3 called as
  383%   tcp_connect(+Address, -StreamPair, +Options).
  384
  385tcp_connect(Socket, Address, Read, Write) :-
  386    tcp_connect_hook(Socket, Address, Read, Write),
  387    !.
  388tcp_connect(Socket, Address, Read, Write) :-
  389    tcp_connect(Socket, Address),
  390    tcp_open_socket(Socket, Read, Write).
  391
  392
  393
  394%!  tcp_connect(+Address, -StreamPair, +Options) is det.
  395%!  tcp_connect(+Socket, +Address, -StreamPair) is det.
  396%
  397%   Establish a TCP communication as a  client.   The  +,-,+ mode is the
  398%   preferred way for a client to establish a connection. This predicate
  399%   can be hooked to support network proxies.   To use a proxy, the hook
  400%   proxy_for_url/3 must be defined. Permitted options are:
  401%
  402%      * bypass_proxy(+Boolean)
  403%        Defaults to =false=. If =true=, do not attempt to use any
  404%        proxies to obtain the connection
  405%
  406%      * nodelay(+Boolean)
  407%        Defaults to =false=. If =true=, set nodelay on the
  408%        resulting socket using tcp_setopt(Socket, nodelay)
  409%
  410%      * domain(+Domain)
  411%        One of `inet' or `inet6`.  When omitted we use host_address/2
  412%        with type(stream) and try the returned addresses in order.
  413%
  414%   The +,+,- mode is  deprecated  and   does  not  support  proxies. It
  415%   behaves  like  tcp_connect/4,  but  creates    a  stream  pair  (see
  416%   stream_pair/3).
  417%
  418%   @arg Address is either a Host:Port  term   or  a  file name (atom or
  419%   string). The latter connects  to  an   AF_UNIX  socket  and requires
  420%   unix_domain_socket/1.
  421%
  422%   @error proxy_error(tried(ResultList)) is raised by   mode (+,-,+) if
  423%   proxies are defines by proxy_for_url/3 but no proxy can establsh the
  424%   connection. `ResultList` contains one or  more   terms  of  the form
  425%   false(Proxy)  for  a  hook  that    simply  failed  or  error(Proxy,
  426%   ErrorTerm) for a hook that raised an exception.
  427%
  428%   @see library(http/http_proxy) defines a hook  that allows to connect
  429%   through HTTP proxies that support the =CONNECT= method.
  430
  431% Main mode: +,-,+
  432tcp_connect(Address, StreamPair, Options) :-
  433    var(StreamPair),
  434    !,
  435    (   memberchk(bypass_proxy(true), Options)
  436    ->  tcp_connect_direct(Address, Socket, StreamPair, Options)
  437    ;   findall(Result,
  438                try_a_proxy(Address, Result),
  439                ResultList),
  440        last(ResultList, Status)
  441    ->  (   Status = true(_Proxy, Socket, StreamPair)
  442        ->  true
  443        ;   throw(error(proxy_error(tried(ResultList)), _))
  444        )
  445    ;   tcp_connect_direct(Address, Socket, StreamPair, Options)
  446    ),
  447    (   memberchk(nodelay(true), Options)
  448    ->  tcp_setopt(Socket, nodelay)
  449    ;   true
  450    ).
  451% backward compatibility mode +,+,-
  452tcp_connect(Socket, Address, StreamPair) :-
  453    tcp_connect_hook(Socket, Address, StreamPair0),
  454    !,
  455    StreamPair = StreamPair0.
  456tcp_connect(Socket, Address, StreamPair) :-
  457    connect_stream_pair(Socket, Address, StreamPair).
  458
  459:- public tcp_connect_direct/3.   % used by HTTP proxy code.
  460tcp_connect_direct(Address, Socket, StreamPair) :-
  461    tcp_connect_direct(Address, Socket, StreamPair, []).
  462
  463%!  tcp_connect_direct(+Address, +Socket, -StreamPair, +Options) is det.
  464%
  465%   Make a direct connection to a TCP address, i.e., do not take proxy
  466%   rules into  account.  If  no explicit  domain (`inet`,  `inet6` is
  467%   given,  perform  a  getaddrinfo()  call  to  obtain  the  relevant
  468%   addresses.
  469
  470tcp_connect_direct(Host:Port, Socket, StreamPair, Options) :-
  471    \+ option(domain(_), Options),
  472    !,
  473    State = error(_),
  474    (   host_address(Host, Address, [type(stream)]),
  475	socket_create(Socket, [domain(Address.domain)]),
  476	E = error(_,_),
  477	catch(connect_or_discard_socket(Socket, Address.address:Port,
  478					StreamPair),
  479	      E, store_error_and_fail(State, E)),
  480	debug(socket, '~p: connected to ~p', [Host, Address.address])
  481    ->  true
  482    ;   arg(1, State, Error),
  483	assertion(nonvar(Error)),
  484	throw(Error)
  485    ).
  486tcp_connect_direct(Address, Socket, StreamPair, Options) :-
  487    make_socket(Address, Socket, Options),
  488    connect_or_discard_socket(Socket, Address, StreamPair).
  489
  490connect_or_discard_socket(Socket, Address, StreamPair) :-
  491    setup_call_catcher_cleanup(
  492	true,
  493	connect_stream_pair(Socket, Address, StreamPair),
  494	Catcher, cleanup(Catcher, Socket)).
  495
  496cleanup(exit, _) :- !.
  497cleanup(_, Socket) :-
  498    tcp_close_socket(Socket).
  499
  500connect_stream_pair(Socket, Address, StreamPair) :-
  501    tcp_connect(Socket, Address, Read, Write),
  502    stream_pair(StreamPair, Read, Write).
  503
  504store_error_and_fail(State, E) :-
  505    arg(1, State, E0),
  506    var(E0),
  507    nb_setarg(1, State, E),
  508    fail.
  509
  510:- if(current_predicate(unix_domain_socket/1)).  511make_socket(Address, Socket, _Options) :-
  512    (   atom(Address)
  513    ;   string(Address)
  514    ),
  515    !,
  516    unix_domain_socket(Socket).
  517:- endif.  518make_socket(_Address, Socket, Options) :-
  519    option(domain(Domain), Options, inet),
  520    socket_create(Socket, [domain(Domain)]).
  521
  522
  523%!  tcp_select(+ListOfStreams, -ReadyList, +TimeOut)
  524%
  525%   Same as the built-in wait_for_input/3. Used  to allow for interrupts
  526%   and timeouts on Windows. A redesign  of the Windows socket interface
  527%   makes  it  impossible  to  do  better  than  Windows  select()  call
  528%   underlying wait_for_input/3. As input multiplexing typically happens
  529%   in a background thread anyway we  accept   the  loss of timeouts and
  530%   interrupts.
  531%
  532%   @deprecated Use wait_for_input/3
  533
  534tcp_select(ListOfStreams, ReadyList, TimeOut) :-
  535    wait_for_input(ListOfStreams, ReadyList, TimeOut).
  536
  537
  538                 /*******************************
  539                 *        PROXY SUPPORT         *
  540                 *******************************/
  541
  542try_a_proxy(Address, Result) :-
  543    format(atom(URL), 'socket://~w', [Address]),
  544    (   Address = Host:_
  545    ->  true
  546    ;   Host = Address
  547    ),
  548    proxy_for_url(URL, Host, Proxy),
  549    debug(socket(proxy), 'Socket connecting via ~w~n', [Proxy]),
  550    (   catch(try_proxy(Proxy, Address, Socket, Stream), E, true)
  551    ->  (   var(E)
  552        ->  !, Result = true(Proxy, Socket, Stream)
  553        ;   Result = error(Proxy, E)
  554        )
  555    ;   Result = false(Proxy)
  556    ),
  557    debug(socket(proxy), 'Socket: ~w: ~p', [Proxy, Result]).
  558
  559%!  try_proxy(+Proxy, +TargetAddress, -Socket, -StreamPair) is semidet.
  560%
  561%   Attempt  a  socket-level  connection  via  the  given  proxy  to
  562%   TargetAddress. The Proxy argument must match the output argument
  563%   of proxy_for_url/3. The predicate tcp_connect/3 (and http_open/3
  564%   from the library(http/http_open)) collect the  results of failed
  565%   proxies and raise an exception no  proxy is capable of realizing
  566%   the connection.
  567%
  568%   The default implementation  recognises  the   values  for  Proxy
  569%   described    below.    The      library(http/http_proxy)    adds
  570%   proxy(Host,Port)  which  allows  for  HTTP   proxies  using  the
  571%   =CONNECT= method.
  572%
  573%     - direct
  574%     Do not use any proxy
  575%     - socks(Host, Port)
  576%     Use a SOCKS5 proxy
  577
  578:- multifile
  579    try_proxy/4.  580
  581try_proxy(direct, Address, Socket, StreamPair) :-
  582    !,
  583    tcp_connect_direct(Address, Socket, StreamPair).
  584try_proxy(socks(Host, Port), Address, Socket, StreamPair) :-
  585    !,
  586    tcp_connect_direct(Host:Port, Socket, StreamPair),
  587    catch(negotiate_socks_connection(Address, StreamPair),
  588          Error,
  589          ( close(StreamPair, [force(true)]),
  590            throw(Error)
  591          )).
  592
  593%!  proxy_for_url(+URL, +Hostname, -Proxy) is nondet.
  594%
  595%   This hook can be implemented  to  return   a  proxy  to try when
  596%   connecting to URL. Returned proxies are   tried  in the order in
  597%   which they are  returned  by   the  multifile  hook try_proxy/4.
  598%   Pre-defined proxy methods are:
  599%
  600%      * direct
  601%        connect directly to the resource
  602%      * proxy(Host, Port)
  603%        Connect to the resource using an HTTP proxy. If the
  604%        resource is not an HTTP URL, then try to connect using the
  605%        CONNECT verb, otherwise, use the GET verb.
  606%      * socks(Host, Port)
  607%        Connect to the resource via a SOCKS5 proxy
  608%
  609%   These correspond to the proxy  methods   defined  by  PAC [Proxy
  610%   auto-config](http://en.wikipedia.org/wiki/Proxy_auto-config).
  611%   Additional methods can  be  returned   if  suitable  clauses for
  612%   http:http_connection_over_proxy/6 or try_proxy/4 are defined.
  613
  614:- multifile
  615    proxy_for_url/3.  616
  617%!  udp_socket(-SocketId) is det.
  618%
  619%   Equivalent to socket_create(SocketId, [type(dgram)]) or, explicit,
  620%   socket_create(SocketId, [domain(inet), type(dgram)]).
  621
  622%!  udp_receive(+Socket, -Data, -From, +Options) is det.
  623%
  624%   Wait for and  return the next datagram. The Data  is returned as a
  625%   Prolog term  depending on Options.  From  is a term of  the format
  626%   Ip:Port indicating the sender of the message. Here, `Ip` is either
  627%   an  ip4  or  ip6  structure.   Socket  can  be  waited  for  using
  628%   wait_for_input/3. Defined Options:
  629%
  630%     - as(+Type)
  631%     Defines the type for Data.  Possible values are `atom`, `codes`,
  632%     `string` (default) or `term` (parse as Prolog term).
  633%     - encoding(+Encoding)
  634%     Specify the encoding used to interpret the message. It is one of
  635%     `octet`. `iso_latin_1`, `text` or `utf8`.
  636%     - max_message_size(+Size)
  637%     Specify  the  maximum  number  of  bytes  to  read  from  a  UDP
  638%     datagram. Size must be within the range 0-65535. If unspecified,
  639%     a maximum of 4096 bytes will be read.
  640%
  641%   For example:
  642%
  643%   ```
  644%   receive(Port) :-
  645%       udp_socket(Socket),
  646%       tcp_bind(Socket, Port),
  647%       repeat,
  648%           udp_receive(Socket, Data, From, [as(atom)]),
  649%           format('Got ~q from ~q~n', [Data, From]),
  650%           fail.
  651%   ```
  652
  653
  654%!  udp_send(+Socket, +Data, +To, +Options) is det.
  655%
  656%   Send a UDP message. Data is  a string, atom or code-list providing
  657%   the data.  To is an  address of the  form Host:Port where  Host is
  658%   either the hostname or an IP address. Defined Options are:
  659%
  660%     - encoding(+Encoding)
  661%       Specifies   the  encoding   to   use  for   the  string.   See
  662%       udp_receive/4 for details
  663%     - as(+Type)
  664%       This uses the  same values for Type as the  as(Type) option of
  665%       udp_receive/4. The are interpreted differently though. No Type
  666%       corresponds   to  CVT_ALL   of  PL_get_chars().    Using  atom
  667%       corresponds to CVT_ATOM  and any of string or  codes is mapped
  668%       to  CVT_STRING|CVT_LIST,  allowing  for  a  SWI-Prolog  string
  669%       object,  list  of  character  codes  or  list  of  characters.
  670%       Finally, `term` maps to CVT_WRITE_CANONICAL. This implies that
  671%       arbitrary Prolog terms  can be sent reliably  using the option
  672%       list `[as(term),encoding(utf8)])`, using  the same option list
  673%       for udp_receive/4.
  674%
  675%   For example
  676%
  677%   ```
  678%   send(Host, Port, Message) :-
  679%       udp_socket(S),
  680%       udp_send(S, Message, Host:Port, []),
  681%       tcp_close_socket(S).
  682%   ```
  683%
  684%   A  broadcast is  achieved by  using tcp_setopt(Socket,  broadcast)
  685%   prior  to  sending  the  datagram  and  using  the  local  network
  686%   broadcast address as a ip/4 term.
  687
  688
  689                 /*******************************
  690                 *            OPTIONS           *
  691                 *******************************/
  692
  693%!  tcp_setopt(+SocketId, +Option) is det.
  694%
  695%   Set options on the socket.  Defined options are:
  696%
  697%     - reuseaddr
  698%     Allow servers to reuse a port without the system being
  699%     completely sure the port is no longer in use.
  700%
  701%     - bindtodevice(+Device)
  702%     Bind the socket to Device (an atom). For example, the code
  703%     below binds the socket to the _loopback_ device that is
  704%     typically used to realise the _localhost_. See the manual
  705%     pages for setsockopt() and the socket interface (e.g.,
  706%     socket(7) on Linux) for details.
  707%
  708%       ==
  709%       tcp_socket(Socket),
  710%       tcp_setopt(Socket, bindtodevice(lo))
  711%       ==
  712%
  713%     - nodelay
  714%     - nodelay(true)
  715%     If =true=, disable the Nagle optimization on this socket,
  716%     which is enabled by default on almost all modern TCP/IP
  717%     stacks. The Nagle optimization joins small packages, which is
  718%     generally desirable, but sometimes not. Please note that the
  719%     underlying TCP_NODELAY setting to setsockopt() is not
  720%     available on all platforms and systems may require additional
  721%     privileges to change this option. If the option is not
  722%     supported, tcp_setopt/2 raises a domain_error exception. See
  723%     [Wikipedia](http://en.wikipedia.org/wiki/Nagle's_algorithm)
  724%     for details.
  725%
  726%     - broadcast
  727%     UDP sockets only: broadcast the package to all addresses
  728%     matching the address. The address is normally the address of
  729%     the local subnet (i.e. 192.168.1.255).  See udp_send/4.
  730%
  731%     - ip_add_membership(+MultiCastGroup)
  732%     - ip_add_membership(+MultiCastGroup, +LocalInterface)
  733%     - ip_add_membership(+MultiCastGroup, +LocalInterface, +InterfaceIndex)
  734%     - ip_drop_membership(+MultiCastGroup)
  735%     - ip_drop_membership(+MultiCastGroup, +LocalInterface)
  736%     - ip_drop_membership(+MultiCastGroup, +LocalInterface, +InterfaceIndex)
  737%     Join/leave a multicast group.  Calls setsockopt() with the
  738%     corresponding arguments.
  739%
  740%     - dispatch(+Boolean)
  741%     In GUI environments (using XPCE or the Windows =swipl-win.exe=
  742%     executable) this flags defines whether or not any events are
  743%     dispatched on behalf of the user interface. Default is
  744%     =true=. Only very specific situations require setting
  745%     this to =false=.
  746%
  747%     - sndbuf(+Integer)
  748%     Sets the send buffer size to Integer (bytes). On Windows this defaults
  749%     (now) to 64kb. Higher latency links may benefit from increasing this
  750%     further since the maximum theoretical throughput on a link is given by
  751%     buffer-size / latency.
  752%     See https://support.microsoft.com/en-gb/help/823764/slow-performance-occurs-when-you-copy-data-to-a-tcp-server-by-using-a
  753%     for Microsoft's discussion
  754
  755%!  tcp_fcntl(+Stream, +Action, ?Argument) is det.
  756%
  757%   Interface to the fcntl() call. Currently   only suitable to deal
  758%   switch stream to non-blocking mode using:
  759%
  760%     ==
  761%       tcp_fcntl(Stream, setfl, nonblock),
  762%     ==
  763%
  764%   An attempt to read from a non-blocking  stream while there is no
  765%   data available returns -1  (or   =end_of_file=  for read/1), but
  766%   at_end_of_stream/1    fails.    On      actual     end-of-input,
  767%   at_end_of_stream/1 succeeds.
  768
  769tcp_fcntl(Socket, setfl, nonblock) :-
  770    !,
  771    tcp_setopt(Socket, nonblock).
  772
  773%!  tcp_getopt(+Socket, ?Option) is semidet.
  774%
  775%   Get  information  about  Socket.  Defined    properties  are  below.
  776%   Requesting an unknown option results in a `domain_error` exception.
  777%
  778%     - file_no(-File)
  779%     Get the OS file handle as an integer.  This may be used for
  780%     debugging and integration.
  781
  782%!  host_address(+HostName, -Address, +Options) is nondet.
  783%!  host_address(-HostName, +Address, +Options) is det.
  784%
  785%   Translate  between a  machines  host-name  and it's  (IP-)address.
  786%   Supported options:
  787%
  788%     - domain(+Domain)
  789%       One of `inet` or `inet6` to limit the results to the given
  790%       family.
  791%     - type(+Type)
  792%       One of `stream` or `dgram`.
  793%     - canonname(+Boolean)
  794%       If `true` (default `false`), return the canonical host name
  795%       in the frist answer
  796%
  797%   In mode (+,-,+) Address is unified to a dict with the following keys:
  798%
  799%     - address
  800%       A Prolog terms describing the ip address.
  801%     - domain
  802%       One of `inet` or `inet6`.  The underlying getaddrinfo() calls
  803%       this `family`.  We use `domain` for consistency with
  804%       socket_create/2.
  805%     - type
  806%       Currently one of `stream` or `dgram`.
  807%     - host
  808%       Available if canonname(true) is specified on the first
  809%       returned address.  Holds the official canonical host name.
  810
  811host_address(HostName, Address, Options), ground(HostName) =>
  812    '$host_address'(HostName, Addresses, Options),
  813    member(Address, Addresses).
  814host_address(HostName, Address, Options), is_dict(Address) =>
  815    '$host_address'(HostName, Address.address, Options).
  816host_address(HostName, Address, Options), ground(Address) =>
  817    '$host_address'(HostName, Address, Options).
  818
  819%!  tcp_host_to_address(?HostName, ?Address) is det.
  820%
  821%   Translate between a machines  host-name   and  it's (IP-)address. If
  822%   HostName is an atom, it  is   resolved  using  getaddrinfo() and the
  823%   IP-number  is  unified  to  Address  using  a  term  of  the  format
  824%   ip(Byte1,Byte2,Byte3,Byte4). Otherwise, if Address is   bound  to an
  825%   ip(Byte1,Byte2,Byte3,Byte4) term, it is  resolved by gethostbyaddr()
  826%   and the canonical hostname is unified with HostName.
  827%
  828%   @deprecated New code should  use   host_address/3.  This  version is
  829%   bootstrapped from host_address/3 and only searches for IP4 addresses
  830%   that support TCP connections.
  831
  832tcp_host_to_address(Host, Address), ground(Address) =>
  833    host_address(Host, Address, []).
  834tcp_host_to_address(Host, Address), ground(Host) =>
  835    host_address(Host, [Dict|_], [domain(inet), type(stream)]),
  836    Address = Dict.address.
  837
  838
  839%!  gethostname(-Hostname) is det.
  840%
  841%   Return the canonical fully qualified name  of this host. This is
  842%   achieved by calling gethostname() and  return the canonical name
  843%   returned by getaddrinfo().
  844
  845
  846%!  ip_name(?IP, ?Name) is det.
  847%
  848%   Translate between the textual representation  of an IP address and
  849%   the  Prolog data  structure.  Prolog  represents ip4  addresses as
  850%   ip(A,B,C,D) and ip6 addresses as ip(A,B,C,D,E,F,H).  For example:
  851%
  852%       ?- ip_name(ip(1,2,3,4), Name)
  853%       Name = '1.2.3.4'.
  854%       ?- ip_name(IP, '::').
  855%       IP = ip(0,0,0,0,0,0,0,0).
  856%       ?- ip_name(IP, '1:2::3').
  857%       IP = ip(1,2,0,0,0,0,0,3).
  858
  859ip_name(Ip, Atom), ground(Atom) =>
  860    name_to_ip(Atom, Ip).
  861ip_name(Ip, Atom), ground(Ip) =>
  862    ip_to_name(Ip, Atom).
  863ip_name(Ip, _) =>
  864    instantiation_error(Ip).
  865
  866name_to_ip(Atom, Ip4) :-
  867    split_string(Atom, '.', '', Parts),
  868    length(Parts, 4),
  869    maplist(string_byte, Parts, Bytes),
  870    !,
  871    Ip4 =.. [ip|Bytes].
  872name_to_ip(Atom, Ip6) :-
  873    split_string(Atom, ':', '', Parts0),
  874    clean_ends(Parts0, Parts1),
  875    length(Parts1, Len),
  876    (   Len < 8
  877    ->  append(Pre, [""|Post], Parts1),
  878	Zeros is 8-(Len-1),
  879	length(ZList, Zeros),
  880	maplist(=("0"), ZList),
  881	append([Pre, ZList, Post], Parts)
  882    ;   Len == 8
  883    ->  Parts = Parts1
  884    ),
  885    !,
  886    maplist(string_short, Parts, Shorts),
  887    Ip6 =.. [ip|Shorts].
  888name_to_ip(Atom, _) :-
  889    syntax_error(ip_address(Atom)).
  890
  891clean_ends([""|T0], T) :-
  892    !,
  893    (   append(T1, [""], T0)
  894    ->  T = T1
  895    ;   T = T0
  896    ).
  897clean_ends(T0, T) :-
  898    append(T1, [""], T0),
  899    !,
  900    T = T1.
  901clean_ends(T, T).
  902
  903string_byte(String, Byte) :-
  904    number_string(Byte, String),
  905    must_be(between(0, 255), Byte).
  906
  907string_short(String, Short) :-
  908    string_concat('0x', String, String1),
  909    number_string(Short, String1),
  910    must_be(between(0, 65535), Short).
  911
  912ip_to_name(ip(A,B,C,D), Atom) :-
  913    !,
  914    atomic_list_concat([A,B,C,D], '.', Atom).
  915ip_to_name(IP, Atom) :-
  916    compound(IP),
  917    compound_name_arity(IP, ip, 8),
  918    !,
  919    IP =.. [ip|Parts],
  920    (   zero_seq(Parts, Pre, Post, Len),
  921        Len > 1,
  922        \+ ( zero_seq(Post, _, _, Len2),
  923	     Len2 > Len
  924	   )
  925    ->  append([Pre, [''], Post], Parts1),
  926	(   Pre == []
  927	->  Parts2 = [''|Parts1]
  928	;   Parts2 = Parts1
  929	),
  930	(   Post == []
  931	->  append(Parts2, [''], Parts3)
  932	;   Parts3 = Parts2
  933	)
  934    ;   Parts3 = Parts
  935    ),
  936    maplist(to_hex, Parts3, Parts4),
  937    atomic_list_concat(Parts4, ':', Atom).
  938ip_to_name(IP, _) :-
  939    domain_error(ip_address, IP).
  940
  941zero_seq(List, Pre, Post, Count) :-
  942    append(Pre, [0|Post0], List),
  943    leading_zeros(Post0, Post, 1, Count).
  944
  945leading_zeros([0|T0], T, C0, C) =>
  946    C1 is C0+1,
  947    leading_zeros(T0, T, C1, C).
  948leading_zeros(L0, L, C0, C) =>
  949    L = L0,
  950    C = C0.
  951
  952to_hex('', '') :-
  953    !.
  954to_hex(Num, Hex) :-
  955    format(string(Hex), '~16r', [Num]).
  956
  957
  958
  959                 /*******************************
  960                 *            SOCKS             *
  961                 *******************************/
  962
  963%!  negotiate_socks_connection(+DesiredEndpoint, +StreamPair) is det.
  964%
  965%   Negotiate  a  connection  to  DesiredEndpoint  over  StreamPair.
  966%   DesiredEndpoint should be in the form of either:
  967%
  968%      * hostname : port
  969%      * ip(A,B,C,D) : port
  970%
  971%   @error socks_error(Details) if the SOCKS negotiation failed.
  972
  973negotiate_socks_connection(Host:Port, StreamPair):-
  974    format(StreamPair, '~s', [[0x5,    % Version 5
  975                               0x1,    % 1 auth method supported
  976                               0x0]]), % which is 'no auth'
  977    flush_output(StreamPair),
  978    get_byte(StreamPair, ServerVersion),
  979    get_byte(StreamPair, AuthenticationMethod),
  980    (   ServerVersion =\= 0x05
  981    ->  throw(error(socks_error(invalid_version(5, ServerVersion)), _))
  982    ;   AuthenticationMethod =:= 0xff
  983    ->  throw(error(socks_error(invalid_authentication_method(
  984                                    0xff,
  985                                    AuthenticationMethod)), _))
  986    ;   true
  987    ),
  988    (   Host = ip(A,B,C,D)
  989    ->  AddressType = 0x1,                  % IPv4 Address
  990        format(atom(Address), '~s', [[A, B, C, D]])
  991    ;   AddressType = 0x3,                  % Domain
  992        atom_length(Host, Length),
  993        format(atom(Address), '~s~w', [[Length], Host])
  994    ),
  995    P1 is Port /\ 0xff,
  996    P2 is Port >> 8,
  997    format(StreamPair, '~s~w~s', [[0x5,   % Version 5
  998                                   0x1,   % Please establish a connection
  999                                   0x0,   % reserved
 1000                                   AddressType],
 1001                                  Address,
 1002                                  [P2, P1]]),
 1003    flush_output(StreamPair),
 1004    get_byte(StreamPair, _EchoedServerVersion),
 1005    get_byte(StreamPair, Status),
 1006    (   Status =:= 0                        % Established!
 1007    ->  get_byte(StreamPair, _Reserved),
 1008        get_byte(StreamPair, EchoedAddressType),
 1009        (   EchoedAddressType =:= 0x1
 1010        ->  get_byte(StreamPair, _),        % read IP4
 1011            get_byte(StreamPair, _),
 1012            get_byte(StreamPair, _),
 1013            get_byte(StreamPair, _)
 1014        ;   get_byte(StreamPair, Length),   % read host name
 1015            forall(between(1, Length, _),
 1016                   get_byte(StreamPair, _))
 1017        ),
 1018        get_byte(StreamPair, _),            % read port
 1019        get_byte(StreamPair, _)
 1020    ;   throw(error(socks_error(negotiation_rejected(Status)), _))
 1021    ).
 1022
 1023
 1024                 /*******************************
 1025                 *             MESSAGES         *
 1026                 *******************************/
 1027
 1028/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
 1029The C-layer generates exceptions of the  following format, where Message
 1030is extracted from the operating system.
 1031
 1032        error(socket_error(Code, Message), _)
 1033- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
 1034
 1035:- multifile
 1036    prolog:error_message//1. 1037
 1038prolog:error_message(socket_error(_Code, Message)) -->
 1039    [ 'Socket error: ~w'-[Message] ].
 1040prolog:error_message(socks_error(Error)) -->
 1041    socks_error(Error).
 1042prolog:error_message(proxy_error(tried(Tried))) -->
 1043    [ 'Failed to connect using a proxy.  Tried:'-[], nl],
 1044    proxy_tried(Tried).
 1045
 1046socks_error(invalid_version(Supported, Got)) -->
 1047    [ 'SOCKS: unsupported version: ~p (supported: ~p)'-
 1048      [ Got, Supported ] ].
 1049socks_error(invalid_authentication_method(Supported, Got)) -->
 1050    [ 'SOCKS: unsupported authentication method: ~p (supported: ~p)'-
 1051      [ Got, Supported ] ].
 1052socks_error(negotiation_rejected(Status)) -->
 1053    [ 'SOCKS: connection failed: ~p'-[Status] ].
 1054
 1055proxy_tried([]) --> [].
 1056proxy_tried([H|T]) -->
 1057    proxy_tried(H),
 1058    proxy_tried(T).
 1059proxy_tried(error(Proxy, Error)) -->
 1060    [ '~w: '-[Proxy] ],
 1061    '$messages':translate_message(Error).
 1062proxy_tried(false(Proxy)) -->
 1063    [ '~w: failed with unspecified error'-[Proxy] ]