9 * This module defines functions useful in establishing and maintaining
10 * connections to other processes on local or remote hosts. The name is an
15 #include <sys/types.h> /* pid_t */
16 #include "dnsutil.h" /* IPAddress */
22 /* A definition for socket call-back functions (see NotifyOnDisconnection()). */
23 typedef void (*SocketFunction)(Socket);
27 * Attempts to establish a connection to the server listening to #addr#:#port#.
28 * If successful within #timeOut# seconds, returns 1 and sets #sock# to the
29 * socket bound to the connection, else returns 0. A #timeOut# of 0 will
30 * disable timeouts while a negative value will use adaptive timeouts.
33 CallAddr(IPAddress addr,
40 * Closes connections opened via calls to this module's functions. Each
41 * parameter is a boolean value indicated whether that type of connection
42 * should be included in those closed.
45 CloseConnections(int closeEars,
48 #define CloseAllConnections() CloseConnections(1, 1, 1)
52 * Tests all connections opened via calls to this module's functions and
53 * closes any that are no longer connected. Returns the number of connections
57 CloseDisconnections(void);
61 * Tears down #sock#. If #waitForPeer# is non-zero, the function waits this
62 * many seconds for the host on the other end to close the connection and fails
63 * if no such close is detected. If this parameter is zero, the function
64 * closes #sock# immediately. Returns 1 and sets #sock# to NO_SOCKET if
65 * successful, else returns 0.
68 CloseSocket(Socket *sock,
70 #define DROP_SOCKET(sock) CloseSocket(sock, 0)
74 * Removed all records of #sock# from our FD_SETs
77 ClearSocket(Socket sock);
80 ** Spawns a new process, a duplicate of the running one. Returns 1 if
81 ** successful, 0 otherwise. Returns in #pid# a 0 to the child process and the
82 ** process id of the child to the parent. Both processes are given a pair of
83 ** connections in the Socket parameters that can be used to communicate between
84 ** the parent and child. The parent process should send information to the
85 ** child via #parentToChild# and receive information via #childToParent#; the
86 ** child reads from the former and writes to the latter. The parameters may be
87 ** NULL indicating that communication is unidirectional (one parameter NULL),
88 ** or that no communication will take place (both NULL).
91 CreateLocalChild(pid_t *pid,
92 Socket *parentToChild,
93 Socket *childToParent);
97 ** Attempts to bind to any port between #startingPort# and #endingPort#,
98 ** inclusive. If successful, returns 1 and sets #ear# to the bound socket and
99 ** #earPort# to the port to which it is bound, else returns 0.
102 EstablishAnEar(unsigned short startingPort,
103 unsigned short endingPort,
105 unsigned short *earPort);
109 * Checks all connections established via calls to this module for
110 * incoming messages. If one is detected within #timeOut# seconds,
111 * returns 1, sets #sd# to the socket containing the message, and sets
112 * #ldap# to the type of message (0 if NWS, 1 if LDAP). If no message
113 * detected, returns 0.
115 * NOTE: you have to use SocketIsAvailable to notify IncomingRequest that
116 * the socket is again available (once the socket is been returned from
117 * IncomingRequest) */
119 IncomingRequest(double timeOut,
125 * When a socket is returned by IncomingRequest, that socket won't be
126 * listen till this function is called.
127 * Return 0 upon failure.
130 SocketIsAvailable(Socket sd);
133 * Tell NWS that a specific socket is still in use and IncomingRequest
134 * shouldn't listen to it.
137 SocketInUse(Socket sd);
140 ** Returns 1 or 0 depending on whether or not #sd# is connected to another
147 * returns 1 or 0 depending on wheter the socket is a pipe or not
154 ** Registers a function that should be called whenever a socket is disconnected,
155 ** either directly via a call to CloseSocket(), or indirectly because the peer
156 ** termintes the connection. The function is passed the closed socket after it
160 NotifyOnDisconnection(SocketFunction notifyFn);
164 ** Pass socket #sock# along to child #child# -- call after a successful call
165 ** to CreateLocalChild(). The parent process will no longer listen for
166 ** activity on #sock#. Closing the socket will be put off until after the
167 ** child dies, just in case the parent and child share descriptors.
170 PassSocket(Socket *sock,
175 * Receives #byteSize# bytes from connection #sd# and stores them in the
176 * memory referenced by #bytes#. The caller must assure that #bytes# is at
177 * least #byteSize# bytes long. Returns 1 if successful within #timeOut#
178 * seconds, else 0. If #timeOut# is zero, timeout are disabled alltogether.
187 * Sends #bytesSize# bytes from the memory pointed to by #bytes# on connection
188 * #sd#. Returns 1 if successful within #timeOut# seconds, else 0.
189 * If #timeOut# is zero, timeout are disabled alltogether..
199 ** A signal handler for dealing with signals (specifically SIGPIPE) that
200 ** indicate a bad socket.
203 SocketFailure(int sig);