View source with formatted comments or as raw
    1/*  Part of SWI-Prolog
    2
    3    Author:        Jeffrey Rosenwald and Jan Wielemaker
    4    E-mail:        jeffrose@acm.org
    5    WWW:           http://www.swi-prolog.org
    6    Copyright (c)  2012-2013, Jeffrey Rosenwald
    7		   2018-2020, CWI Amsterdam
    8    All rights reserved.
    9
   10    Redistribution and use in source and binary forms, with or without
   11    modification, are permitted provided that the following conditions
   12    are met:
   13
   14    1. Redistributions of source code must retain the above copyright
   15       notice, this list of conditions and the following disclaimer.
   16
   17    2. Redistributions in binary form must reproduce the above copyright
   18       notice, this list of conditions and the following disclaimer in
   19       the documentation and/or other materials provided with the
   20       distribution.
   21
   22    THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   23    "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   24    LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
   25    FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
   26    COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
   27    INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
   28    BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
   29    LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
   30    CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   31    LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
   32    ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
   33    POSSIBILITY OF SUCH DAMAGE.
   34*/
   35
   36:- module(udp_broadcast,
   37          [ udp_broadcast_initialize/2,         % +IPAddress, +Options
   38            udp_broadcast_close/1,		% +Scope
   39
   40            udp_peer_add/2,                     % +Scope, +IP
   41            udp_peer_del/2,                     % +Scope, ?IP
   42            udp_peer/2                          % +Scope, -IP
   43          ]).   44:- autoload(library(apply),[maplist/2,maplist/3]).   45:- autoload(library(backcomp),[thread_at_exit/1]).   46:- autoload(library(broadcast),
   47	    [broadcast_request/1,broadcast/1,listening/3,listen/3]).   48:- use_module(library(debug),[debug/3]).   49:- autoload(library(error),
   50	    [must_be/2,syntax_error/1,domain_error/2,existence_error/2]).   51:- autoload(library(option),[option/3]).   52:- autoload(library(socket),
   53	    [ tcp_close_socket/1,
   54	      udp_socket/1,
   55	      tcp_bind/2,
   56	      tcp_getopt/2,
   57	      tcp_setopt/2,
   58	      udp_receive/4,
   59	      udp_send/4
   60	    ]).   61
   62
   63% :- debug(udp(broadcast)).
   64
   65/** <module> A UDP broadcast proxy
   66
   67SWI-Prolog's broadcast library provides a  means   that  may  be used to
   68facilitate publish and subscribe communication regimes between anonymous
   69members of a community of interest.  The   members  of the community are
   70however, necessarily limited to a  single   instance  of Prolog. The UDP
   71broadcast library removes that restriction.   With  this library loaded,
   72any member on your local IP subnetwork that also has this library loaded
   73may hear and respond to your broadcasts.
   74
   75This library support three styles of networking as described below. Each
   76of these networks have their own   advantages  and disadvantages. Please
   77study the literature to understand the consequences.
   78
   79  $ broadcast :
   80  Broadcast messages are sent to the LAN subnet. The broadcast
   81  implementation uses two UDP ports: a public to address the whole
   82  group and a private one to address a specific node.  Broadcasting
   83  is generally a good choice if the subnet is small and traffic is
   84  low.
   85
   86  $ unicast :
   87  Unicast sends copies of packages to known peers.  Unicast networks
   88  can easily be routed.  The unicast version uses a single UDP port
   89  per node.  Unicast is generally a good choice for a small party,
   90  in particular if the peers are in different networks.
   91
   92  $ multicast :
   93  Multicast is like broadcast, but it can be configured to
   94  work accross networks and may work more efficiently on VLAN networks.
   95  Like the broadcast setup, two UDP ports are used.  Multicasting can
   96  in general deliver the most efficient LAN and WAN networks, but
   97  requires properly configured routing between the peers.
   98
   99After initialization and, in the case   of  a _unicast_ network managing
  100the  set  of  peers,   communication    happens   through   broadcast/1,
  101broadcast_request/1 and listen/1,2,3.
  102
  103A broadcast/1 or broadcast_request/1 of the   shape  udp(Scope, Term) or
  104udp(Scope, Term, TimeOut) is forwarded over the UDP network to all peers
  105that joined the same `Scope`.  To   prevent  the  potential for feedback
  106loops, only the plain `Term`  is   broadcasted  locally.  The timeout is
  107optional. It specifies the amount to time  to wait for replies to arrive
  108in response to a  broadcast_request/1.  The   default  period  is  0.250
  109seconds. The timeout is ignored for broadcasts.
  110
  111An example of three separate processes   cooperating in the same _scope_
  112called `peers`:
  113
  114==
  115Process A:
  116
  117   ?- listen(number(X), between(1, 5, X)).
  118   true.
  119
  120   ?-
  121
  122Process B:
  123
  124   ?- listen(number(X), between(7, 9, X)).
  125   true.
  126
  127   ?-
  128
  129Process C:
  130
  131   ?- findall(X, broadcast_request(udp(peers, number(X))), Xs).
  132   Xs = [1, 2, 3, 4, 5, 7, 8, 9].
  133
  134   ?-
  135==
  136
  137It is also  possible  to  carry  on   a  private  dialog  with  a single
  138responder. To do this, you supply a   compound of the form, Term:PortId,
  139to a UDP scoped broadcast/1 or  broadcast_request/1, where PortId is the
  140ip-address and port-id of  the  intended   listener.  If  you  supply an
  141unbound variable, PortId, to broadcast_request, it  will be unified with
  142the address of the listener  that  responds   to  Term.  You  may send a
  143directed broadcast to a specific member by simply providing this address
  144in a similarly structured compound  to   a  UDP  scoped broadcast/1. The
  145message is sent via unicast to that member   only by way of the member's
  146broadcast listener. It is received by  the   listener  just as any other
  147broadcast would be. The listener does not know the difference.
  148
  149For example, in order to discover who responded with a particular value:
  150
  151==
  152Host B Process 1:
  153
  154   ?- listen(number(X), between(1, 5, X)).
  155   true.
  156
  157   ?-
  158
  159Host A Process 1:
  160
  161
  162   ?- listen(number(X), between(7, 9, X)).
  163   true.
  164
  165   ?-
  166
  167Host A Process 2:
  168
  169   ?- listen(number(X), between(1, 5, X)).
  170   true.
  171
  172   ?- bagof(X, broadcast_request(udp(peers,number(X):From,1)), Xs).
  173   From = ip(192, 168, 1, 103):34855,
  174   Xs = [7, 8, 9] ;
  175   From = ip(192, 168, 1, 103):56331,
  176   Xs = [1, 2, 3, 4, 5] ;
  177   From = ip(192, 168, 1, 104):3217,
  178   Xs = [1, 2, 3, 4, 5].
  179==
  180
  181All incomming trafic is handled  by  a   single  thread  with  the alias
  182`udp_inbound_proxy`. This thread also performs  the internal dispatching
  183using broadcast/1 and broadcast_request/1. Future   versions may provide
  184for handling these requests in separate threads.
  185
  186
  187## Caveats {#udp-broadcase-caveats}
  188
  189While the implementation is mostly transparent, there are some important
  190and subtle differences that must be taken into consideration:
  191
  192    * UDP broadcast requires an initialization step in order to
  193    launch the broadcast listener proxy. See
  194    udp_broadcast_initialize/2.
  195
  196    * Prolog's broadcast_request/1 is nondet. It sends the request,
  197    then evaluates the replies synchronously, backtracking as needed
  198    until a satisfactory reply is received. The remaining potential
  199    replies are not evaluated.  With UDP, all peers will send all
  200    answers to the query.  The receiver may however stop listening.
  201
  202    * A UDP broadcast/1 is completely asynchronous.
  203
  204    * A  UDP broadcast_request/1 is partially synchronous. A
  205    broadcast_request/1 is sent, then the sender balks for a period of
  206    time (default: 250 ms) while the replies are collected. Any reply
  207    that is received after this period is silently discarded. A
  208    optional second argument is provided so that a sender may specify
  209    more (or less) time for replies.
  210
  211    * Replies are presented to the user as a choice point on arrival,
  212    until the broadcast request timer finally expires. This
  213    allows traffic to propagate through the system faster and provides
  214    the requestor with the opportunity to terminate a broadcast request
  215    early if desired, by simply cutting choice points.
  216
  217    * Please beware that broadcast request transactions remain active
  218    and resources consumed until broadcast_request finally fails on
  219    backtracking, an uncaught exception occurs, or until choice points
  220    are cut. Failure to properly manage this will likely result in
  221    chronic exhaustion of UDP sockets.
  222
  223    * If a listener is connected to a generator that always succeeds
  224    (e.g. a random number generator), then the broadcast request will
  225    never terminate and trouble is bound to ensue.
  226
  227    * broadcast_request/1 with =|udp_subnet|= scope is _not_ reentrant.
  228    If a listener performs a broadcast_request/1 with UDP scope
  229    recursively, then disaster looms certain. This caveat does not apply
  230    to a UDP scoped broadcast/1, which can safely be performed from a
  231    listener context.
  232
  233    * UDP broadcast's capacity is not infinite. While it can tolerate
  234    substantial bursts of activity, it is designed for short bursts of
  235    small messages. Unlike TIPC, UDP is unreliable and has no QOS
  236    protections. Congestion is likely to cause trouble in the form of
  237    non-Byzantine failure. That is, late, lost (e.g. infinitely late),
  238    or duplicate datagrams. Caveat emptor.
  239
  240    * A UDP broadcast_request/1 term that is grounded is considered to
  241    be a broadcast only. No replies are collected unless the there is at
  242    least one unbound variable to unify.
  243
  244    * A UDP broadcast/1 always succeeds, even if there are no
  245    listeners.
  246
  247    * A UDP broadcast_request/1 that receives no replies will fail.
  248
  249    * Replies may be coming from many different places in the network
  250    (or none at all). No ordering of replies is implied.
  251
  252    * Prolog terms are sent to others after first converting them to
  253    atoms using term_string/3.  Serialization does not deal with cycles,
  254    attributes or sharing.   The hook udp_term_string_hook/3 may be
  255    defined to change the message serialization and support different
  256    message formats and/or encryption.
  257
  258    * The broadcast model is based on anonymity and a presumption of
  259    trust--a perfect recipe for compromise. UDP is an Internet protocol.
  260    A UDP broadcast listener exposes a public port, which is
  261    static and shared by all listeners, and a private port, which is
  262    semi-static and unique to the listener instance. Both can be seen
  263    from off-cluster nodes and networks. Usage of this module exposes
  264    the node and consequently, the cluster to significant security
  265    risks. So have a care when designing your application. You must talk
  266    only to those who share and contribute to your concerns using a
  267    carefully prescribed protocol.
  268
  269    * UDP broadcast categorically and silently ignores all message
  270    traffic originating from or terminating on nodes that are not
  271    members of the local subnet. This security measure only keeps honest
  272    people honest!
  273
  274@author    Jeffrey Rosenwald (JeffRose@acm.org), Jan Wielemaker
  275@license   BSD-2
  276@see       tipc.pl
  277*/
  278
  279:- multifile
  280    udp_term_string_hook/3,                     % +Scope, ?Term, ?String
  281    udp_unicast_join_hook/3,                    % +Scope, +From, +Data
  282    black_list/1.                               % +Term
  283
  284:- meta_predicate
  285    safely(0),
  286    safely_det(0).  287
  288safely(Predicate) :-
  289    Err = error(_,_),
  290    catch(Predicate, Err,
  291          print_message_fail(Err)).
  292
  293safely_det(Predicate) :-
  294    Err = error(_,_),
  295    catch(Predicate, Err,
  296          print_message_fail(Err)),
  297    !.
  298safely_det(_).
  299
  300print_message_fail(Term) :-
  301    print_message(error, Term),
  302    fail.
  303
  304udp_broadcast_address(IPAddress, Subnet, BroadcastAddress) :-
  305    IPAddress = ip(A1, A2, A3, A4),
  306    Subnet = ip(S1, S2, S3, S4),
  307    BroadcastAddress = ip(B1, B2, B3, B4),
  308
  309    B1 is A1 \/ (S1 xor 255),
  310    B2 is A2 \/ (S2 xor 255),
  311    B3 is A3 \/ (S3 xor 255),
  312    B4 is A4 \/ (S4 xor 255).
  313
  314%!  udp_broadcast_service(?Scope, ?Address) is nondet.
  315%
  316%   provides the UDP broadcast address for   a  given Scope. At present,
  317%   only one scope is supported, =|udp_subnet|=.
  318
  319%!  udp_scope(?ScopeName, ?ScopeDef)
  320
  321:- dynamic
  322    udp_scope/2,
  323    udp_scope_peer/2.  324:- volatile
  325    udp_scope/2,
  326    udp_scope_peer/2.  327%
  328%  Here's a UDP proxy to Prolog's broadcast library
  329%
  330%  A sender may extend a broadcast  to  a   subnet  of  a UDP network by
  331%  specifying a =|udp_subnet|= scoping qualifier   in his/her broadcast.
  332%  The qualifier has the effect of  selecting the appropriate multi-cast
  333%  address for the transmission. Thus,  the   sender  of the message has
  334%  control over the scope of his/her traffic on a per-message basis.
  335%
  336%  All in-scope listeners receive the   broadcast and simply rebroadcast
  337%  the message locally. All broadcast replies, if any, are sent directly
  338%  to the sender via the port-id that   was received with the broadcast.
  339%
  340%  Each listener exposes two UDP ports,  a   shared  public port that is
  341%  bound to a well-known port number and   a  private port that uniquely
  342%  indentifies the listener. Broadcasts are received  on the public port
  343%  and replies are  sent  on  the   private  port.  Directed  broadcasts
  344%  (unicasts) are received on the private port   and replies are sent on
  345%  the private port.
  346
  347%  Thread 1 listens for directed traffic on the private port.
  348%
  349
  350:- dynamic
  351    udp_private_socket/3,                       % Port, Socket, FileNo
  352    udp_public_socket/4,                        % Scope, Port, Socket, FileNo
  353    udp_closed/1.				% Scope
  354
  355udp_inbound_proxy(Master) :-
  356    thread_at_exit(inbound_proxy_died),
  357    make_private_socket,
  358    thread_send_message(Master, udp_inbound_ready),
  359    udp_inbound_proxy_loop.
  360
  361udp_inbound_proxy_loop :-
  362    forall(udp_scope(Scope, ScopeData),
  363           make_public_socket(ScopeData, Scope)),
  364    retractall(udp_closed(_)),
  365    findall(FileNo, udp_socket_file_no(FileNo), FileNos),
  366    catch(dispatch_inbound(FileNos),
  367          E, dispatch_exception(E)),
  368    udp_inbound_proxy_loop.
  369
  370dispatch_exception(E) :-
  371    E = error(_,_),
  372    !,
  373    print_message(warning, E).
  374dispatch_exception(_).
  375
  376
  377%!  make_private_socket is det.
  378%
  379%   Create our private socket. This socket is used for messages that are
  380%   directed to me. Note that we only  need this for broadcast networks.
  381%   If we use a unicast network we use   our public port to contact this
  382%   specific server.
  383
  384make_private_socket :-
  385    udp_private_socket(_Port, S, _F),
  386    !,
  387    (   (   udp_scope(Scope, broadcast(_,_,_))
  388        ;   udp_scope(Scope, multicast(_,_))
  389        ),
  390        \+ udp_closed(Scope)
  391    ->  true
  392    ;   tcp_close_socket(S),
  393        retractall(udp_private_socket(_,_,_))
  394    ).
  395make_private_socket :-
  396    udp_scope(_, broadcast(_,_,_)),
  397    !,
  398    udp_socket(S),
  399    tcp_bind(S, Port),
  400    tcp_getopt(S, file_no(F)),
  401    tcp_setopt(S, broadcast),
  402    assertz(udp_private_socket(Port, S, F)).
  403make_private_socket :-
  404    udp_scope(_, multicast(_,_)),
  405    !,
  406    udp_socket(S),
  407    tcp_bind(S, Port),
  408    tcp_getopt(S, file_no(F)),
  409    assertz(udp_private_socket(Port, S, F)).
  410make_private_socket.
  411
  412%!  make_public_socket(+ScopeData, +Scope)
  413%
  414%   Create the public port Scope.
  415
  416make_public_socket(_, Scope) :-
  417    udp_public_socket(Scope, _Port, S, _),
  418    !,
  419    (   udp_closed(Scope)
  420    ->  tcp_close_socket(S),
  421        retractall(udp_public_socket(Scope, _, _, _))
  422    ;   true
  423    ).
  424make_public_socket(broadcast(_SubNet, _Broadcast, Port), Scope) :-
  425    udp_socket(S),
  426    tcp_setopt(S, reuseaddr),
  427    tcp_bind(S, Port),
  428    tcp_getopt(S, file_no(F)),
  429    assertz(udp_public_socket(Scope, Port, S, F)).
  430make_public_socket(multicast(Group, Port), Scope) :-
  431    udp_socket(S),
  432    tcp_setopt(S, reuseaddr),
  433    tcp_bind(S, Port),
  434    tcp_setopt(S, ip_add_membership(Group)),
  435    tcp_getopt(S, file_no(F)),
  436    assertz(udp_public_socket(Scope, Port, S, F)).
  437make_public_socket(unicast(Port), Scope) :-
  438    udp_socket(S),
  439    tcp_bind(S, Port),
  440    tcp_getopt(S, file_no(F)),
  441    assertz(udp_public_socket(Scope, Port, S, F)).
  442
  443udp_socket_file_no(FileNo) :-
  444    udp_private_socket(_,_,FileNo).
  445udp_socket_file_no(FileNo) :-
  446    udp_public_socket(_,_,_,FileNo).
  447
  448%!  dispatch_inbound(+FileNos)
  449%
  450%   Dispatch inbound traffic. This loop   uses  wait_for_input/3 to wait
  451%   for one or more UDP sockets and   dispatches  the requests using the
  452%   internal broadcast service. For an  incomming broadcast _request_ we
  453%   send the reply only to the  requester   and  therefore we must use a
  454%   socket that is not in broadcast mode.
  455
  456dispatch_inbound(FileNos) :-
  457    debug(udp(broadcast), 'Waiting for ~p', [FileNos]),
  458    wait_for_input(FileNos, Ready, infinite),
  459    debug(udp(broadcast), 'Ready: ~p', [Ready]),
  460    maplist(dispatch_ready, Ready),
  461    dispatch_inbound(FileNos).
  462
  463dispatch_ready(FileNo) :-
  464    udp_private_socket(_Port, Private, FileNo),
  465    !,
  466    udp_receive(Private, Data, From, [max_message_size(65535)]),
  467    debug(udp(broadcast), 'Inbound on private port', []),
  468    (   in_scope(Scope, From),
  469        udp_term_string(Scope, Term, Data) % only accept valid data
  470    ->  ld_dispatch(Private, Term, From, Scope)
  471    ;   true
  472    ).
  473dispatch_ready(FileNo) :-
  474    udp_public_socket(Scope, _PublicPort, Public, FileNo),
  475    !,
  476    udp_receive(Public, Data, From, [max_message_size(65535)]),
  477    debug(udp(broadcast), 'Inbound on public port from ~p for scope ~p',
  478          [From, Scope]),
  479    (   in_scope(Scope, From),
  480        udp_term_string(Scope, Term, Data) % only accept valid data
  481    ->  (   udp_scope(Scope, unicast(_))
  482        ->  ld_dispatch(Public, Term, From, Scope)
  483        ;   udp_private_socket(_PrivatePort, Private, _FileNo),
  484            ld_dispatch(Private, Term, From, Scope)
  485        )
  486    ;   udp_scope(Scope, unicast(_)),
  487        udp_term_string(Scope, Term, Data),
  488        unicast_out_of_scope_request(Scope, From, Term)
  489    ->  true
  490    ;   true
  491    ).
  492
  493in_scope(Scope, Address) :-
  494    udp_scope(Scope, ScopeData),
  495    in_scope(ScopeData, Scope, Address),
  496    !.
  497in_scope(Scope, From) :-
  498    debug(udp(broadcast), 'Out-of-scope ~p datagram from ~p',
  499          [Scope, From]),
  500    fail.
  501
  502in_scope(broadcast(Subnet, Broadcast, _PublicPort), _Scope, IP:_FromPort) :-
  503    udp_broadcast_address(IP, Subnet, Broadcast).
  504in_scope(multicast(_Group, _Port), _Scope, _From).
  505in_scope(unicast(_PublicPort), Scope, IP:_) :-
  506    udp_peer(Scope, IP:_).
  507
  508
  509%!  ld_dispatch(+PrivateSocket, +Term, +From, +Scope)
  510%
  511%   Locally dispatch Term received from From. If it concerns a broadcast
  512%   request, send the replies to PrivateSocket   to  From. The multifile
  513%   hook black_list/1 can be used to ignore certain messages.
  514
  515ld_dispatch(_S, Term, From, _Scope) :-
  516    debug(udp(broadcast), 'ld_dispatch(~p) from ~p', [Term, From]),
  517    fail.
  518ld_dispatch(_S, Term, _From, _Scope) :-
  519    blacklisted(Term), !.
  520ld_dispatch(S, request(Key, Term), From, Scope) :-
  521    !,
  522    forall(safely(broadcast_request(Term)),
  523           safely((udp_term_string(Scope, reply(Key,Term), Message),
  524                   udp_send(S, Message, From, [])))).
  525ld_dispatch(_S, send(Term), _From, _Scope) :-
  526    !,
  527    safely_det(broadcast(Term)).
  528ld_dispatch(_S, reply(Key, Term), From, _Scope) :-
  529    (   reply_queue(Key, Queue)
  530    ->  safely(thread_send_message(Queue, Term:From))
  531    ;   true
  532    ).
  533
  534blacklisted(send(Term))      :- forwarding(Term), !.
  535blacklisted(request(_,Term)) :- forwarding(Term), !.
  536blacklisted(send(Term))      :- black_list(Term).
  537blacklisted(request(_,Term)) :- black_list(Term).
  538blacklisted(reply(_,Term))   :- black_list(Term).
  539
  540%   forwarding(@Term) is semidet.
  541%
  542%   True if Term would ask this node to send a UDP message.  Such
  543%   requests from the network are ignored as they would make this node
  544%   relay messages to arbitrary addresses and block the inbound proxy
  545%   while waiting for replies.
  546
  547forwarding(Term) :-
  548    var(Term),
  549    !.
  550forwarding(udp(_,_)).
  551forwarding(udp(_,_,_)).
  552forwarding(udp_subnet(_)).
  553forwarding(udp_subnet(_,_)).
  554
  555
  556%!  reload_udp_proxy
  557%
  558%   Update the UDP relaying proxy service.   The proxy consists of three
  559%   forwarding mechanisms:
  560%
  561%     - Listen on our _scope_.  If any messages are received, hand them
  562%       to udp_broadcast/3 to be broadcasted to _scope_ or sent to a
  563%       specific recipient.
  564%     - Listen on the _scope_ public port. Incomming messages are
  565%       relayed to the internal broadcast mechanism and replies are sent
  566%       to from our private socket.
  567%     - Listen on our private port and reply using the same port.
  568
  569reload_udp_proxy :-
  570    reload_outbound_proxy,
  571    reload_inbound_proxy.
  572
  573reload_outbound_proxy :-
  574    listening(udp_broadcast, udp(_,_), _),
  575    !.
  576reload_outbound_proxy :-
  577    listen(udp_broadcast, udp(Scope,Message),
  578           udp_broadcast(Message, Scope, 0.25)),
  579    listen(udp_broadcast, udp(Scope,Message,Timeout),
  580           udp_broadcast(Message, Scope, Timeout)),
  581    listen(udp_broadcast, udp_subnet(Message),  % backward compatibility
  582           udp_broadcast(Message, subnet, 0.25)),
  583    listen(udp_broadcast, udp_subnet(Message,Timeout),
  584           udp_broadcast(Message, subnet, Timeout)).
  585
  586reload_inbound_proxy :-
  587    catch(thread_signal(udp_inbound_proxy, throw(udp_reload)),
  588          error(existence_error(thread, _),_),
  589          fail),
  590    !.
  591reload_inbound_proxy :-
  592    thread_self(Me),
  593    thread_create(udp_inbound_proxy(Me), _,
  594                  [ alias(udp_inbound_proxy),
  595                    detached(true)
  596                  ]),
  597    thread_get_message(Me, udp_inbound_ready, [timeout(10)]).
  598
  599inbound_proxy_died :-
  600    thread_self(Self),
  601    thread_property(Self, status(Status)),
  602    (   catch(recreate_proxy(Status), _, fail)
  603    ->  print_message(informational,
  604                      httpd_restarted_worker(Self))
  605    ;   done_status_message_level(Status, Level),
  606        print_message(Level,
  607                      httpd_stopped_worker(Self, Status))
  608    ).
  609
  610recreate_proxy(exception(Error)) :-
  611    recreate_on_error(Error),
  612    reload_inbound_proxy.
  613
  614recreate_on_error('$aborted').          % old
  615recreate_on_error(unwind(abort)).
  616recreate_on_error(time_limit_exceeded).
  617
  618done_status_message_level(true, silent) :- !.
  619done_status_message_level(exception('$aborted'), silent) :- !.
  620done_status_message_level(exception(unwind(abort)), silent) :- !.
  621done_status_message_level(_, informational).
  622
  623
  624%!  udp_broadcast_close(+Scope)
  625%
  626%   Close a UDP broadcast scope.
  627
  628udp_broadcast_close(Scope) :-
  629    udp_scope(Scope, _ScopeData),
  630    !,
  631    assert(udp_closed(Scope)),
  632    reload_udp_proxy.
  633udp_broadcast_close(_).
  634
  635
  636%!  udp_broadcast(+What, +Scope, +TimeOut)
  637%
  638%   Send a broadcast request to my UDP peers in Scope. What is either of
  639%   the shape `Term:Address` to send Term to a specific address or query
  640%   the address from which term is answered or it is a plain `Term`.
  641%
  642%   If `Term` is  nonground,  it  is   considered  is  a  _request_ (see
  643%   broadcast_request/1) and the predicate  succeeds   for  each  answer
  644%   received within TimeOut seconds. If Term is ground it is considered
  645%   an asynchronous broadcast and udp_broadcast/3 is deterministic.
  646
  647udp_broadcast(Term:To, Scope, _Timeout) :-
  648    ground(Term), ground(To),           % broadcast to single listener
  649    !,
  650    udp_basic_broadcast(send(Term), Scope, single(To)).
  651udp_broadcast(Term, Scope, _Timeout) :-
  652    ground(Term),                       % broadcast to all listeners
  653    !,
  654    udp_basic_broadcast(send(Term), Scope, broadcast).
  655udp_broadcast(Term:To, Scope, Timeout) :-
  656    ground(To),                         % request to single listener
  657    !,
  658    setup_call_cleanup(
  659        request_queue(Id, Queue),
  660        ( udp_basic_broadcast(request(Id, Term), Scope, single(To)),
  661          udp_br_collect_replies(Queue, Timeout, Term:To)
  662        ),
  663        destroy_request_queue(Queue)).
  664udp_broadcast(Term:From, Scope, Timeout) :-
  665    !,                                  % request to all listeners, collect sender
  666    setup_call_cleanup(
  667        request_queue(Id, Queue),
  668        ( udp_basic_broadcast(request(Id, Term), Scope, broadcast),
  669          udp_br_collect_replies(Queue, Timeout, Term:From)
  670        ),
  671        destroy_request_queue(Queue)).
  672udp_broadcast(Term, Scope, Timeout) :-  % request to all listeners
  673    udp_broadcast(Term:_, Scope, Timeout).
  674
  675:- dynamic
  676    reply_queue/2.  677
  678request_queue(Id, Queue) :-
  679    Id is random(1<<63),
  680    message_queue_create(Queue),
  681    asserta(reply_queue(Id, Queue)).
  682
  683destroy_request_queue(Queue) :-         % leave queue to GC
  684    retractall(reply_queue(_, Queue)).
  685
  686
  687%!  udp_basic_broadcast(+Term, +Dest) is multi.
  688%
  689%   Create a UDP private socket and use it   to send Term to Address. If
  690%   Address is our broadcast address, set the socket in broadcast mode.
  691%
  692%   This predicate succeeds with a choice   point. Committing the choice
  693%   point closes S.
  694%
  695%   @arg Dest is one of single(Target) or `broadcast`.
  696
  697udp_basic_broadcast(Term, Scope, Dest) :-
  698    debug(udp(broadcast), 'UDP proxy outbound ~p to ~p', [Term, Dest]),
  699    udp_term_string(Scope, Term, String),
  700    udp_send_message(Dest, String, Scope).
  701
  702udp_send_message(single(Address), String, Scope) :-
  703    (   udp_scope(Scope, unicast(_))
  704    ->  udp_public_socket(Scope, _Port, S, _)
  705    ;   udp_private_socket(_Port, S, _F)
  706    ),
  707    safely(udp_send(S, String, Address, [])).
  708udp_send_message(broadcast, String, Scope) :-
  709    (   udp_scope(Scope, unicast(_))
  710    ->  udp_public_socket(Scope, _Port, S, _),
  711        forall(udp_peer(Scope, Address),
  712               ( debug(udp(broadcast), 'Unicast to ~p', [Address]),
  713                 safely(udp_send(S, String, Address, []))))
  714    ;   udp_scope(Scope, broadcast(_SubNet, Broadcast, Port))
  715    ->  udp_private_socket(_PrivatePort, S, _F),
  716        udp_send(S, String, Broadcast:Port, [])
  717    ;   udp_scope(Scope, multicast(Group, Port))
  718    ->  udp_private_socket(_PrivatePort, S, _F),
  719        udp_send(S, String, Group:Port, [])
  720    ).
  721
  722% ! udp_br_collect_replies(+Queue, +TimeOut, -TermAndFrom) is nondet.
  723%
  724%   Collect replies on Socket for  TimeOut   seconds.  Succeed  for each
  725%   received message.
  726
  727udp_br_collect_replies(Queue, Timeout, Reply) :-
  728    get_time(Start),
  729    Deadline is Start+Timeout,
  730    repeat,
  731       (   thread_get_message(Queue, Reply,
  732                              [ deadline(Deadline)
  733                              ])
  734       ->  true
  735       ;   !,
  736           fail
  737       ).
  738
  739%!  udp_broadcast_initialize(+IPAddress, +Options) is semidet.
  740%
  741%   Initialized UDP broadcast bridge. IPAddress is the IP address on the
  742%   network we want to broadcast on.  IP addresses are terms ip(A,B,C,D)
  743%   or an atom or string of the format =|A.B.C.D|=.   Options processed:
  744%
  745%     - scope(+ScopeName)
  746%     Name of the scope.  Default is `subnet`.
  747%     - subnet_mask(+SubNet)
  748%     Subnet to broadcast on.  This uses the same syntax as IPAddress.
  749%     Default classifies the network as class A, B or C depending on
  750%     the the first octet and applies the default mask.
  751%     - port(+Port)
  752%     Public port to use.  Default is 20005.
  753%     - method(+Method)
  754%     Method to send a message to multiple peers.  One of
  755%       - broadcast
  756%       Use UDP broadcast messages to the LAN.  This is the
  757%       default
  758%       - multicast
  759%       Use UDP multicast messages.  This can be used on WAN networks,
  760%       provided the intermediate routers understand multicast.  Note
  761%       that messages are accepted from any host that can send UDP
  762%       datagrams to the port.
  763%       - unicast
  764%       Send the messages individually to all registered peers.
  765%
  766%   For compatibility reasons Options may be the subnet mask.
  767
  768udp_broadcast_initialize(IP, Options) :-
  769    with_mutex(udp_broadcast,
  770               udp_broadcast_initialize_sync(IP, Options)).
  771
  772udp_broadcast_initialize_sync(IP, Options) :-
  773    nonvar(Options),
  774    Options = ip(_,_,_,_),
  775    !,
  776    udp_broadcast_initialize(IP, [subnet_mask(Options)]).
  777udp_broadcast_initialize_sync(IP, Options) :-
  778    to_ip4(IP, IPAddress),
  779    option(method(Method), Options, broadcast),
  780    must_be(oneof([broadcast, multicast, unicast]), Method),
  781    udp_broadcast_initialize_sync(Method, IPAddress, Options),
  782    reload_udp_proxy.
  783
  784udp_broadcast_initialize_sync(broadcast, IPAddress, Options) :-
  785    option(subnet_mask(Subnet), Options, _),
  786    mk_subnet(Subnet, IPAddress, Subnet4),
  787    option(port(Port), Options, 20005),
  788    option(scope(Scope), Options, subnet),
  789
  790    udp_broadcast_address(IPAddress, Subnet4, Broadcast),
  791    udp_broadcast_close(Scope),
  792    assertz(udp_scope(Scope, broadcast(Subnet4, Broadcast, Port))).
  793udp_broadcast_initialize_sync(unicast, _IPAddress, Options) :-
  794    option(port(Port), Options, 20005),
  795    option(scope(Scope), Options, subnet),
  796    udp_broadcast_close(Scope),
  797    assertz(udp_scope(Scope, unicast(Port))).
  798udp_broadcast_initialize_sync(multicast, IPAddress, Options) :-
  799    option(port(Port), Options, 20005),
  800    option(scope(Scope), Options, subnet),
  801    udp_broadcast_close(Scope),
  802    multicast_address(IPAddress),
  803    assertz(udp_scope(Scope, multicast(IPAddress, Port))).
  804
  805to_ip4(Atomic, ip(A,B,C,D)) :-
  806    atomic(Atomic),
  807    !,
  808    (   split_string(Atomic, ".", "", Strings),
  809        maplist(number_string, [A,B,C,D], Strings)
  810    ->  true
  811    ;   syntax_error(illegal_ip_address)
  812    ).
  813to_ip4(IP, IP).
  814
  815mk_subnet(Var, IP, Subnet) :-
  816    var(Var),
  817    !,
  818    (   default_subnet(IP, Subnet)
  819    ->  true
  820    ;   domain_error(ip_with_subnet, IP)
  821    ).
  822mk_subnet(Subnet, _, Subnet4) :-
  823    to_ip4(Subnet, Subnet4).
  824
  825%!  default_subnet(+IP, -NetWork)
  826%
  827%   Determine the default network address from an IP address. This
  828%   classifies the network as class A, B or C.
  829%
  830%   @see https://docs.oracle.com/cd/E19504-01/802-5753/planning3-78185/index.html
  831
  832default_subnet(ip(A,_,_,_), ip(A,0,0,0)) :-
  833    between(0,127, A), !.
  834default_subnet(ip(A,B,_,_), ip(A,B,0,0)) :-
  835    between(128,191, A), !.
  836default_subnet(ip(A,B,C,_), ip(A,B,C,0)) :-
  837    between(192,223, A), !.
  838
  839multicast_address(ip(A,_,_,_)) :-
  840    between(224, 239, A),
  841    !.
  842multicast_address(IP) :-
  843    domain_error(multicast_network, IP).
  844
  845
  846		 /*******************************
  847		 *          UNICAST PEERS	*
  848		 *******************************/
  849
  850%!  udp_peer_add(+Scope, +Address) is det.
  851%!  udp_peer_del(+Scope, ?Address) is det.
  852%!  udp_peer(?Scope, ?Address) is nondet.
  853%
  854%   Manage and query the set  of  known   peers  for  a unicast network.
  855%   Address is either a term  IP:Port  or   a  plain  IP address. In the
  856%   latter case the default port registered with the scope is used.
  857%
  858%   @arg Address has canonical form ip(A,B,C,D):Port.
  859
  860udp_peer_add(Scope, Address) :-
  861    must_be(ground, Address),
  862    peer_address(Address, Scope, Canonical),
  863    (   udp_scope_peer(Scope, Canonical)
  864    ->  true
  865    ;   assertz(udp_scope_peer(Scope, Canonical))
  866    ).
  867
  868udp_peer_del(Scope, Address) :-
  869    peer_address(Address, Scope, Canonical),
  870    retractall(udp_scope_peer(Scope, Canonical)).
  871
  872udp_peer(Scope, IPAddress) :-
  873    udp_scope_peer(Scope, IPAddress).
  874
  875peer_address(IP:Port, _Scope, IPAddress:Port) :-
  876    !,
  877    to_ip4(IP, IPAddress).
  878peer_address(IP, Scope, IPAddress:Port) :-
  879    (   udp_scope(Scope, unicast(Port))
  880    ->  true
  881    ;   existence_error(udp_scope, Scope)
  882    ),
  883    to_ip4(IP, IPAddress).
  884
  885
  886
  887		 /*******************************
  888		 *             HOOKS		*
  889		 *******************************/
  890
  891%!  udp_term_string_hook(+Scope, +Term, -String) is det.
  892%!  udp_term_string_hook(+Scope, -Term, +String) is semidet.
  893%
  894%   Hook  for  serializing  the  message    Term.   The  default  writes
  895%   =|%prolog\n|=, followed by the Prolog term  in quoted notation while
  896%   ignoring operators. This hook may use alternative serialization such
  897%   as fast_term_serialized/2, use  library(ssl)   to  realise encrypted
  898%   messages, etc.
  899%
  900%   @arg Scope is the scope for which the message is broadcasted.  This
  901%   can be used to use different serialization for different scopes.
  902%   @arg Term encapsulates the term broadcasted by the application as
  903%   follows:
  904%
  905%     - send(ApplTerm)
  906%       Is sent by broadcast(udp(Scope, ApplTerm))
  907%     - request(Id,ApplTerm)
  908%       Is sent by broadcast_request/1, where Id is a unique large
  909%       (64 bit) integer.
  910%     - reply(Id,ApplTerm)
  911%       Is sent to reply on a broadcast_request/1 request that has
  912%       been received.  Arguments are the same as above.
  913%
  914%   @throws The hook may throw udp(invalid_message) to stop processing
  915%   the message.
  916
  917%!  udp_term_string(+Scope, +Term, -String) is det.
  918%!  udp_term_string(+Scope, -Term, +String) is semidet.
  919%
  920%   Serialize an arbitrary Prolog  term  as   a  string.  The  string is
  921%   prefixed by a magic key to ensure   we only accept messages that are
  922%   meant for us.
  923%
  924%   In mode (+,-), Term is written with the options ignore_ops(true) and
  925%   quoted(true).
  926%
  927%   This predicate first calls  udp_term_string_hook/3.
  928
  929udp_term_string(Scope, Term, String) :-
  930    catch(udp_term_string_hook(Scope, Term, String), udp(Error), true),
  931    !,
  932    (   var(Error)
  933    ->  true
  934    ;   Error == invalid_message
  935    ->  fail
  936    ;   throw(udp(Error))
  937    ).
  938udp_term_string(_Scope, Term, String) :-
  939    (   var(String)
  940    ->  format(string(String), '%-prolog-\n~W',
  941               [ Term,
  942                 [ ignore_ops(true),
  943                   quoted(true)
  944                 ]
  945               ])
  946    ;   sub_string(String, 0, _, _, '%-prolog-\n'),
  947        term_string(Term, String,
  948                    [ syntax_errors(quiet)
  949                    ])
  950    ).
  951
  952%!  unicast_out_of_scope_request(+Scope, +From, +Data) is semidet.
  953
  954%!  udp_unicast_join_hook(+Scope, +From, +Data) is semidet.
  955%
  956%   This multifile hook is called if an   UDP package is received on the
  957%   port of the unicast network identified by  Scope. From is the origin
  958%   IP and port and Data is  the   message  data that is deserialized as
  959%   defined for the scope (see udp_term_string/3).
  960%
  961%   This hook is intended to initiate a  new node joining the network of
  962%   peers. We could in theory also  omit   the  in-scope  test and use a
  963%   normal broadcast to join. Using a different channal however provides
  964%   a basic level of security. A   possibe  implementation is below. The
  965%   first fragment is a hook  added  to   the  server,  the  second is a
  966%   predicate added to a client and the   last  initiates the request in
  967%   the client. The excanged term (join(X)) can   be  used to exchange a
  968%   welcome handshake.
  969%
  970%
  971%   ```
  972%   :- multifile udp_broadcast:udp_unicast_join_hook/3.
  973%   udp_broadcast:udp_unicast_join_hook(Scope, From, join(welcome)) :-
  974%       udp_peer_add(Scope, From),
  975%   ```
  976%
  977%   ```
  978%   join_request(Scope, Address, Reply) :-
  979%       udp_peer_add(Scope, Address),
  980%       broadcast_request(udp(Scope, join(X))).
  981%   ```
  982%
  983%   ```
  984%   ?- join_request(myscope, "1.2.3.4":10001, Reply).
  985%   Reply = welcome.
  986%   ```
  987
  988unicast_out_of_scope_request(Scope, From, send(Term)) :-
  989    udp_unicast_join_hook(Scope, From, Term).
  990unicast_out_of_scope_request(Scope, From, request(Key, Term)) :-
  991    udp_unicast_join_hook(Scope, From, Term),
  992    udp_public_socket(Scope, _Port, Socket, _FileNo),
  993    safely((udp_term_string(Scope, reply(Key,Term), Message),
  994            udp_send(Socket, Message, From, [])))