1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2  * contributor license agreements.  See the NOTICE file distributed with
3  * this work for additional information regarding copyright ownership.
4  * The ASF licenses this file to You under the Apache License, Version 2.0
5  * (the "License"); you may not use this file except in compliance with
6  * the License.  You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef APR_NETWORK_IO_H
18 #define APR_NETWORK_IO_H
19 /**
20  * @file apr_network_io.h
21  * @brief APR Network library
22  */
23 
24 #include "apr.h"
25 #include "apr_pools.h"
26 #include "apr_file_io.h"
27 #include "apr_errno.h"
28 #include "apr_inherit.h"
29 
30 #if APR_HAVE_NETINET_IN_H
31 #include <netinet/in.h>
32 #endif
33 
34 #ifdef __cplusplus
35 extern "C" {
36 #endif /* __cplusplus */
37 
38 /**
39  * @defgroup apr_network_io Network Routines
40  * @ingroup APR
41  * @{
42  */
43 
44 #ifndef APR_MAX_SECS_TO_LINGER
45 /** Maximum seconds to linger */
46 #define APR_MAX_SECS_TO_LINGER 30
47 #endif
48 
49 #ifndef APRMAXHOSTLEN
50 /** Maximum hostname length */
51 #define APRMAXHOSTLEN 256
52 #endif
53 
54 #ifndef APR_ANYADDR
55 /** Default 'any' address */
56 #define APR_ANYADDR "0.0.0.0"
57 #endif
58 
59 /**
60  * @defgroup apr_sockopt Socket option definitions
61  * @{
62  */
63 #define APR_SO_LINGER        1    /**< Linger */
64 #define APR_SO_KEEPALIVE     2    /**< Keepalive */
65 #define APR_SO_DEBUG         4    /**< Debug */
66 #define APR_SO_NONBLOCK      8    /**< Non-blocking IO */
67 #define APR_SO_REUSEADDR     16   /**< Reuse addresses */
68 #define APR_SO_SNDBUF        64   /**< Send buffer */
69 #define APR_SO_RCVBUF        128  /**< Receive buffer */
70 #define APR_SO_DISCONNECTED  256  /**< Disconnected */
71 #define APR_TCP_NODELAY      512  /**< For SCTP sockets, this is mapped
72                                    * to STCP_NODELAY internally.
73                                    */
74 #define APR_TCP_NOPUSH       1024 /**< No push */
75 #define APR_RESET_NODELAY    2048 /**< This flag is ONLY set internally
76                                    * when we set APR_TCP_NOPUSH with
77                                    * APR_TCP_NODELAY set to tell us that
78                                    * APR_TCP_NODELAY should be turned on
79                                    * again when NOPUSH is turned off
80                                    */
81 #define APR_INCOMPLETE_READ 4096  /**< Set on non-blocking sockets
82 				   * (timeout != 0) on which the
83 				   * previous read() did not fill a buffer
84 				   * completely.  the next apr_socket_recv()
85                                    * will first call select()/poll() rather than
86 				   * going straight into read().  (Can also
87 				   * be set by an application to force a
88 				   * select()/poll() call before the next
89 				   * read, in cases where the app expects
90 				   * that an immediate read would fail.)
91 				   */
92 #define APR_INCOMPLETE_WRITE 8192 /**< like APR_INCOMPLETE_READ, but for write
93                                    * @see APR_INCOMPLETE_READ
94                                    */
95 #define APR_IPV6_V6ONLY     16384 /**< Don't accept IPv4 connections on an
96                                    * IPv6 listening socket.
97                                    */
98 #define APR_TCP_DEFER_ACCEPT 32768 /**< Delay accepting of new connections
99                                     * until data is available.
100                                     * @see apr_socket_accept_filter
101                                     */
102 
103 /** @} */
104 
105 /** Define what type of socket shutdown should occur. */
106 typedef enum {
107     APR_SHUTDOWN_READ,          /**< no longer allow read request */
108     APR_SHUTDOWN_WRITE,         /**< no longer allow write requests */
109     APR_SHUTDOWN_READWRITE      /**< no longer allow read or write requests */
110 } apr_shutdown_how_e;
111 
112 #define APR_IPV4_ADDR_OK  0x01  /**< @see apr_sockaddr_info_get() */
113 #define APR_IPV6_ADDR_OK  0x02  /**< @see apr_sockaddr_info_get() */
114 
115 #if (!APR_HAVE_IN_ADDR)
116 /**
117  * We need to make sure we always have an in_addr type, so APR will just
118  * define it ourselves, if the platform doesn't provide it.
119  */
120 struct in_addr {
121     apr_uint32_t  s_addr; /**< storage to hold the IP# */
122 };
123 #endif
124 
125 /** @def APR_INADDR_NONE
126  * Not all platforms have a real INADDR_NONE.  This macro replaces
127  * INADDR_NONE on all platforms.
128  */
129 #ifdef INADDR_NONE
130 #define APR_INADDR_NONE INADDR_NONE
131 #else
132 #define APR_INADDR_NONE ((unsigned int) 0xffffffff)
133 #endif
134 
135 /**
136  * @def APR_INET
137  * Not all platforms have these defined, so we'll define them here
138  * The default values come from FreeBSD 4.1.1
139  */
140 #define APR_INET     AF_INET
141 /** @def APR_UNSPEC
142  * Let the system decide which address family to use
143  */
144 #ifdef AF_UNSPEC
145 #define APR_UNSPEC   AF_UNSPEC
146 #else
147 #define APR_UNSPEC   0
148 #endif
149 #if APR_HAVE_IPV6
150 /** @def APR_INET6
151 * IPv6 Address Family. Not all platforms may have this defined.
152 */
153 
154 #define APR_INET6    AF_INET6
155 #endif
156 
157 /**
158  * @defgroup IP_Proto IP Protocol Definitions for use when creating sockets
159  * @{
160  */
161 #define APR_PROTO_TCP       6   /**< TCP  */
162 #define APR_PROTO_UDP      17   /**< UDP  */
163 #define APR_PROTO_SCTP    132   /**< SCTP */
164 /** @} */
165 
166 /**
167  * Enum to tell us if we're interested in remote or local socket
168  */
169 typedef enum {
170     APR_LOCAL,
171     APR_REMOTE
172 } apr_interface_e;
173 
174 /**
175  * The specific declaration of inet_addr's ... some platforms fall back
176  * inet_network (this is not good, but necessary)
177  */
178 
179 #if APR_HAVE_INET_ADDR
180 #define apr_inet_addr    inet_addr
181 #elif APR_HAVE_INET_NETWORK        /* only DGUX, as far as I know */
182 /**
183  * @warning
184  * not generally safe... inet_network() and inet_addr() perform
185  * different functions */
186 #define apr_inet_addr    inet_network
187 #endif
188 
189 /** A structure to represent sockets */
190 typedef struct apr_socket_t     apr_socket_t;
191 /**
192  * A structure to encapsulate headers and trailers for apr_socket_sendfile
193  */
194 typedef struct apr_hdtr_t       apr_hdtr_t;
195 /** A structure to represent in_addr */
196 typedef struct in_addr          apr_in_addr_t;
197 /** A structure to represent an IP subnet */
198 typedef struct apr_ipsubnet_t apr_ipsubnet_t;
199 
200 /** @remark use apr_uint16_t just in case some system has a short that isn't 16 bits... */
201 typedef apr_uint16_t            apr_port_t;
202 
203 /** @remark It's defined here as I think it should all be platform safe...
204  * @see apr_sockaddr_t
205  */
206 typedef struct apr_sockaddr_t apr_sockaddr_t;
207 /**
208  * APRs socket address type, used to ensure protocol independence
209  */
210 struct apr_sockaddr_t {
211     /** The pool to use... */
212     apr_pool_t *pool;
213     /** The hostname */
214     char *hostname;
215     /** Either a string of the port number or the service name for the port */
216     char *servname;
217     /** The numeric port */
218     apr_port_t port;
219     /** The family */
220     apr_int32_t family;
221     /** How big is the sockaddr we're using? */
222     apr_socklen_t salen;
223     /** How big is the ip address structure we're using? */
224     int ipaddr_len;
225     /** How big should the address buffer be?  16 for v4 or 46 for v6
226      *  used in inet_ntop... */
227     int addr_str_len;
228     /** This points to the IP address structure within the appropriate
229      *  sockaddr structure.  */
230     void *ipaddr_ptr;
231     /** If multiple addresses were found by apr_sockaddr_info_get(), this
232      *  points to a representation of the next address. */
233     apr_sockaddr_t *next;
234     /** Union of either IPv4 or IPv6 sockaddr. */
235     union {
236         /** IPv4 sockaddr structure */
237         struct sockaddr_in sin;
238 #if APR_HAVE_IPV6
239         /** IPv6 sockaddr structure */
240         struct sockaddr_in6 sin6;
241 #endif
242 #if APR_HAVE_SA_STORAGE
243         /** Placeholder to ensure that the size of this union is not
244          * dependent on whether APR_HAVE_IPV6 is defined. */
245         struct sockaddr_storage sas;
246 #endif
247     } sa;
248 };
249 
250 #if APR_HAS_SENDFILE
251 /**
252  * Support reusing the socket on platforms which support it (from disconnect,
253  * specifically Win32.
254  * @remark Optional flag passed into apr_socket_sendfile()
255  */
256 #define APR_SENDFILE_DISCONNECT_SOCKET      1
257 #endif
258 
259 /** A structure to encapsulate headers and trailers for apr_socket_sendfile */
260 struct apr_hdtr_t {
261     /** An iovec to store the headers sent before the file. */
262     struct iovec* headers;
263     /** number of headers in the iovec */
264     int numheaders;
265     /** An iovec to store the trailers sent after the file. */
266     struct iovec* trailers;
267     /** number of trailers in the iovec */
268     int numtrailers;
269 };
270 
271 /* function definitions */
272 
273 /**
274  * Create a socket.
275  * @param new_sock The new socket that has been set up.
276  * @param family The address family of the socket (e.g., APR_INET).
277  * @param type The type of the socket (e.g., SOCK_STREAM).
278  * @param protocol The protocol of the socket (e.g., APR_PROTO_TCP).
279  * @param cont The pool to use
280  */
281 APR_DECLARE(apr_status_t) apr_socket_create(apr_socket_t **new_sock,
282                                             int family, int type,
283                                             int protocol,
284                                             apr_pool_t *cont);
285 
286 /**
287  * Shutdown either reading, writing, or both sides of a socket.
288  * @param thesocket The socket to close
289  * @param how How to shutdown the socket.  One of:
290  * <PRE>
291  *            APR_SHUTDOWN_READ         no longer allow read requests
292  *            APR_SHUTDOWN_WRITE        no longer allow write requests
293  *            APR_SHUTDOWN_READWRITE    no longer allow read or write requests
294  * </PRE>
295  * @see apr_shutdown_how_e
296  * @remark This does not actually close the socket descriptor, it just
297  *      controls which calls are still valid on the socket.
298  */
299 APR_DECLARE(apr_status_t) apr_socket_shutdown(apr_socket_t *thesocket,
300                                               apr_shutdown_how_e how);
301 
302 /**
303  * Close a socket.
304  * @param thesocket The socket to close
305  */
306 APR_DECLARE(apr_status_t) apr_socket_close(apr_socket_t *thesocket);
307 
308 /**
309  * Bind the socket to its associated port
310  * @param sock The socket to bind
311  * @param sa The socket address to bind to
312  * @remark This may be where we will find out if there is any other process
313  *      using the selected port.
314  */
315 APR_DECLARE(apr_status_t) apr_socket_bind(apr_socket_t *sock,
316                                           apr_sockaddr_t *sa);
317 
318 /**
319  * Listen to a bound socket for connections.
320  * @param sock The socket to listen on
321  * @param backlog The number of outstanding connections allowed in the sockets
322  *                listen queue.  If this value is less than zero, the listen
323  *                queue size is set to zero.
324  */
325 APR_DECLARE(apr_status_t) apr_socket_listen(apr_socket_t *sock,
326                                             apr_int32_t backlog);
327 
328 /**
329  * Accept a new connection request
330  * @param new_sock A copy of the socket that is connected to the socket that
331  *                 made the connection request.  This is the socket which should
332  *                 be used for all future communication.
333  * @param sock The socket we are listening on.
334  * @param connection_pool The pool for the new socket.
335  */
336 APR_DECLARE(apr_status_t) apr_socket_accept(apr_socket_t **new_sock,
337                                             apr_socket_t *sock,
338                                             apr_pool_t *connection_pool);
339 
340 /**
341  * Issue a connection request to a socket either on the same machine
342  * or a different one.
343  * @param sock The socket we wish to use for our side of the connection
344  * @param sa The address of the machine we wish to connect to.
345  */
346 APR_DECLARE(apr_status_t) apr_socket_connect(apr_socket_t *sock,
347                                              apr_sockaddr_t *sa);
348 
349 /**
350  * Create apr_sockaddr_t from hostname, address family, and port.
351  * @param sa The new apr_sockaddr_t.
352  * @param hostname The hostname or numeric address string to resolve/parse, or
353  *               NULL to build an address that corresponds to 0.0.0.0 or ::
354  * @param family The address family to use, or APR_UNSPEC if the system should
355  *               decide.
356  * @param port The port number.
357  * @param flags Special processing flags:
358  * <PRE>
359  *       APR_IPV4_ADDR_OK          first query for IPv4 addresses; only look
360  *                                 for IPv6 addresses if the first query failed;
361  *                                 only valid if family is APR_UNSPEC and hostname
362  *                                 isn't NULL; mutually exclusive with
363  *                                 APR_IPV6_ADDR_OK
364  *       APR_IPV6_ADDR_OK          first query for IPv6 addresses; only look
365  *                                 for IPv4 addresses if the first query failed;
366  *                                 only valid if family is APR_UNSPEC and hostname
367  *                                 isn't NULL and APR_HAVE_IPV6; mutually exclusive
368  *                                 with APR_IPV4_ADDR_OK
369  * </PRE>
370  * @param p The pool for the apr_sockaddr_t and associated storage.
371  */
372 APR_DECLARE(apr_status_t) apr_sockaddr_info_get(apr_sockaddr_t **sa,
373                                           const char *hostname,
374                                           apr_int32_t family,
375                                           apr_port_t port,
376                                           apr_int32_t flags,
377                                           apr_pool_t *p);
378 
379 /**
380  * Look up the host name from an apr_sockaddr_t.
381  * @param hostname The hostname.
382  * @param sa The apr_sockaddr_t.
383  * @param flags Special processing flags.
384  */
385 APR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname,
386                                           apr_sockaddr_t *sa,
387                                           apr_int32_t flags);
388 
389 /**
390  * Parse hostname/IP address with scope id and port.
391  *
392  * Any of the following strings are accepted:
393  *   8080                  (just the port number)
394  *   www.apache.org        (just the hostname)
395  *   www.apache.org:8080   (hostname and port number)
396  *   [fe80::1]:80          (IPv6 numeric address string only)
397  *   [fe80::1%eth0]        (IPv6 numeric address string and scope id)
398  *
399  * Invalid strings:
400  *                         (empty string)
401  *   [abc]                 (not valid IPv6 numeric address string)
402  *   abc:65536             (invalid port number)
403  *
404  * @param addr The new buffer containing just the hostname.  On output, *addr
405  *             will be NULL if no hostname/IP address was specfied.
406  * @param scope_id The new buffer containing just the scope id.  On output,
407  *                 *scope_id will be NULL if no scope id was specified.
408  * @param port The port number.  On output, *port will be 0 if no port was
409  *             specified.
410  *             ### FIXME: 0 is a legal port (per RFC 1700). this should
411  *             ### return something besides zero if the port is missing.
412  * @param str The input string to be parsed.
413  * @param p The pool from which *addr and *scope_id are allocated.
414  * @remark If scope id shouldn't be allowed, check for scope_id != NULL in
415  *         addition to checking the return code.  If addr/hostname should be
416  *         required, check for addr == NULL in addition to checking the
417  *         return code.
418  */
419 APR_DECLARE(apr_status_t) apr_parse_addr_port(char **addr,
420                                               char **scope_id,
421                                               apr_port_t *port,
422                                               const char *str,
423                                               apr_pool_t *p);
424 
425 /**
426  * Get name of the current machine
427  * @param buf A buffer to store the hostname in.
428  * @param len The maximum length of the hostname that can be stored in the
429  *            buffer provided.  The suggested length is APRMAXHOSTLEN + 1.
430  * @param cont The pool to use.
431  * @remark If the buffer was not large enough, an error will be returned.
432  */
433 APR_DECLARE(apr_status_t) apr_gethostname(char *buf, int len, apr_pool_t *cont);
434 
435 /**
436  * Return the data associated with the current socket
437  * @param data The user data associated with the socket.
438  * @param key The key to associate with the user data.
439  * @param sock The currently open socket.
440  */
441 APR_DECLARE(apr_status_t) apr_socket_data_get(void **data, const char *key,
442                                               apr_socket_t *sock);
443 
444 /**
445  * Set the data associated with the current socket.
446  * @param sock The currently open socket.
447  * @param data The user data to associate with the socket.
448  * @param key The key to associate with the data.
449  * @param cleanup The cleanup to call when the socket is destroyed.
450  */
451 APR_DECLARE(apr_status_t) apr_socket_data_set(apr_socket_t *sock, void *data,
452                                               const char *key,
453                                               apr_status_t (*cleanup)(void*));
454 
455 /**
456  * Send data over a network.
457  * @param sock The socket to send the data over.
458  * @param buf The buffer which contains the data to be sent.
459  * @param len On entry, the number of bytes to send; on exit, the number
460  *            of bytes sent.
461  * @remark
462  * <PRE>
463  * This functions acts like a blocking write by default.  To change
464  * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
465  * socket option.
466  *
467  * It is possible for both bytes to be sent and an error to be returned.
468  *
469  * APR_EINTR is never returned.
470  * </PRE>
471  */
472 APR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf,
473                                           apr_size_t *len);
474 
475 /**
476  * Send multiple packets of data over a network.
477  * @param sock The socket to send the data over.
478  * @param vec The array of iovec structs containing the data to send
479  * @param nvec The number of iovec structs in the array
480  * @param len Receives the number of bytes actually written
481  * @remark
482  * <PRE>
483  * This functions acts like a blocking write by default.  To change
484  * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
485  * socket option.
486  * The number of bytes actually sent is stored in argument 3.
487  *
488  * It is possible for both bytes to be sent and an error to be returned.
489  *
490  * APR_EINTR is never returned.
491  * </PRE>
492  */
493 APR_DECLARE(apr_status_t) apr_socket_sendv(apr_socket_t *sock,
494                                            const struct iovec *vec,
495                                            apr_int32_t nvec, apr_size_t *len);
496 
497 /**
498  * @param sock The socket to send from
499  * @param where The apr_sockaddr_t describing where to send the data
500  * @param flags The flags to use
501  * @param buf  The data to send
502  * @param len  The length of the data to send
503  */
504 APR_DECLARE(apr_status_t) apr_socket_sendto(apr_socket_t *sock,
505                                             apr_sockaddr_t *where,
506                                             apr_int32_t flags, const char *buf,
507                                             apr_size_t *len);
508 
509 /**
510  * @param from The apr_sockaddr_t to fill in the recipient info
511  * @param sock The socket to use
512  * @param flags The flags to use
513  * @param buf  The buffer to use
514  * @param len  The length of the available buffer
515  */
516 
517 APR_DECLARE(apr_status_t) apr_socket_recvfrom(apr_sockaddr_t *from,
518                                               apr_socket_t *sock,
519                                               apr_int32_t flags, char *buf,
520                                               apr_size_t *len);
521 
522 #if APR_HAS_SENDFILE || defined(DOXYGEN)
523 
524 /**
525  * Send a file from an open file descriptor to a socket, along with
526  * optional headers and trailers
527  * @param sock The socket to which we're writing
528  * @param file The open file from which to read
529  * @param hdtr A structure containing the headers and trailers to send
530  * @param offset Offset into the file where we should begin writing
531  * @param len (input)  - Number of bytes to send from the file
532  *            (output) - Number of bytes actually sent,
533  *                       including headers, file, and trailers
534  * @param flags APR flags that are mapped to OS specific flags
535  * @remark This functions acts like a blocking write by default.  To change
536  *         this behavior, use apr_socket_timeout_set() or the
537  *         APR_SO_NONBLOCK socket option.
538  * The number of bytes actually sent is stored in the len parameter.
539  * The offset parameter is passed by reference for no reason; its
540  * value will never be modified by the apr_socket_sendfile() function.
541  */
542 APR_DECLARE(apr_status_t) apr_socket_sendfile(apr_socket_t *sock,
543                                               apr_file_t *file,
544                                               apr_hdtr_t *hdtr,
545                                               apr_off_t *offset,
546                                               apr_size_t *len,
547                                               apr_int32_t flags);
548 
549 #endif /* APR_HAS_SENDFILE */
550 
551 /**
552  * Read data from a network.
553  * @param sock The socket to read the data from.
554  * @param buf The buffer to store the data in.
555  * @param len On entry, the number of bytes to receive; on exit, the number
556  *            of bytes received.
557  * @remark
558  * <PRE>
559  * This functions acts like a blocking read by default.  To change
560  * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
561  * socket option.
562  * The number of bytes actually received is stored in argument 3.
563  *
564  * It is possible for both bytes to be received and an APR_EOF or
565  * other error to be returned.
566  *
567  * APR_EINTR is never returned.
568  * </PRE>
569  */
570 APR_DECLARE(apr_status_t) apr_socket_recv(apr_socket_t *sock,
571                                    char *buf, apr_size_t *len);
572 
573 /**
574  * Setup socket options for the specified socket
575  * @param sock The socket to set up.
576  * @param opt The option we would like to configure.  One of:
577  * <PRE>
578  *            APR_SO_DEBUG      --  turn on debugging information
579  *            APR_SO_KEEPALIVE  --  keep connections active
580  *            APR_SO_LINGER     --  lingers on close if data is present
581  *            APR_SO_NONBLOCK   --  Turns blocking on/off for socket
582  *                                  When this option is enabled, use
583  *                                  the APR_STATUS_IS_EAGAIN() macro to
584  *                                  see if a send or receive function
585  *                                  could not transfer data without
586  *                                  blocking.
587  *            APR_SO_REUSEADDR  --  The rules used in validating addresses
588  *                                  supplied to bind should allow reuse
589  *                                  of local addresses.
590  *            APR_SO_SNDBUF     --  Set the SendBufferSize
591  *            APR_SO_RCVBUF     --  Set the ReceiveBufferSize
592  * </PRE>
593  * @param on Value for the option.
594  */
595 APR_DECLARE(apr_status_t) apr_socket_opt_set(apr_socket_t *sock,
596                                              apr_int32_t opt, apr_int32_t on);
597 
598 /**
599  * Setup socket timeout for the specified socket
600  * @param sock The socket to set up.
601  * @param t Value for the timeout.
602  * <PRE>
603  *   t > 0  -- read and write calls return APR_TIMEUP if specified time
604  *             elapsess with no data read or written
605  *   t == 0 -- read and write calls never block
606  *   t < 0  -- read and write calls block
607  * </PRE>
608  */
609 APR_DECLARE(apr_status_t) apr_socket_timeout_set(apr_socket_t *sock,
610                                                  apr_interval_time_t t);
611 
612 /**
613  * Query socket options for the specified socket
614  * @param sock The socket to query
615  * @param opt The option we would like to query.  One of:
616  * <PRE>
617  *            APR_SO_DEBUG      --  turn on debugging information
618  *            APR_SO_KEEPALIVE  --  keep connections active
619  *            APR_SO_LINGER     --  lingers on close if data is present
620  *            APR_SO_NONBLOCK   --  Turns blocking on/off for socket
621  *            APR_SO_REUSEADDR  --  The rules used in validating addresses
622  *                                  supplied to bind should allow reuse
623  *                                  of local addresses.
624  *            APR_SO_SNDBUF     --  Set the SendBufferSize
625  *            APR_SO_RCVBUF     --  Set the ReceiveBufferSize
626  *            APR_SO_DISCONNECTED -- Query the disconnected state of the socket.
627  *                                  (Currently only used on Windows)
628  * </PRE>
629  * @param on Socket option returned on the call.
630  */
631 APR_DECLARE(apr_status_t) apr_socket_opt_get(apr_socket_t *sock,
632                                              apr_int32_t opt, apr_int32_t *on);
633 
634 /**
635  * Get Socket fd for the socket passed
636  * @param sock The socket to quesry for the socket fd
637  */
638 APR_DECLARE(int) apr_socket_fd_get(apr_socket_t *sock);
639 
640 
641 /**
642  * Query socket timeout for the specified socket
643  * @param sock The socket to query
644  * @param t Socket timeout returned from the query.
645  */
646 APR_DECLARE(apr_status_t) apr_socket_timeout_get(apr_socket_t *sock,
647                                                  apr_interval_time_t *t);
648 
649 /**
650  * Query the specified socket if at the OOB/Urgent data mark
651  * @param sock The socket to query
652  * @param atmark Is set to true if socket is at the OOB/urgent mark,
653  *               otherwise is set to false.
654  */
655 APR_DECLARE(apr_status_t) apr_socket_atmark(apr_socket_t *sock,
656                                             int *atmark);
657 
658 /**
659  * Return an apr_sockaddr_t from an apr_socket_t
660  * @param sa The returned apr_sockaddr_t.
661  * @param which Which interface do we want the apr_sockaddr_t for?
662  * @param sock The socket to use
663  */
664 APR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa,
665                                               apr_interface_e which,
666                                               apr_socket_t *sock);
667 
668 /**
669  * Return the IP address (in numeric address string format) in
670  * an APR socket address.  APR will allocate storage for the IP address
671  * string from the pool of the apr_sockaddr_t.
672  * @param addr The IP address.
673  * @param sockaddr The socket address to reference.
674  */
675 APR_DECLARE(apr_status_t) apr_sockaddr_ip_get(char **addr,
676                                               apr_sockaddr_t *sockaddr);
677 
678 /**
679  * See if the IP addresses in two APR socket addresses are
680  * equivalent.  Appropriate logic is present for comparing
681  * IPv4-mapped IPv6 addresses with IPv4 addresses.
682  *
683  * @param addr1 One of the APR socket addresses.
684  * @param addr2 The other APR socket address.
685  * @remark The return value will be non-zero if the addresses
686  * are equivalent.
687  */
688 APR_DECLARE(int) apr_sockaddr_equal(const apr_sockaddr_t *addr1,
689                                     const apr_sockaddr_t *addr2);
690 
691 /**
692 * Return the type of the socket.
693 * @param sock The socket to query.
694 * @param type The returned type (e.g., SOCK_STREAM).
695 */
696 APR_DECLARE(apr_status_t) apr_socket_type_get(apr_socket_t *sock,
697                                               int *type);
698 
699 /**
700  * Given an apr_sockaddr_t and a service name, set the port for the service
701  * @param sockaddr The apr_sockaddr_t that will have its port set
702  * @param servname The name of the service you wish to use
703  */
704 APR_DECLARE(apr_status_t) apr_getservbyname(apr_sockaddr_t *sockaddr,
705                                             const char *servname);
706 /**
707  * Build an ip-subnet representation from an IP address and optional netmask or
708  * number-of-bits.
709  * @param ipsub The new ip-subnet representation
710  * @param ipstr The input IP address string
711  * @param mask_or_numbits The input netmask or number-of-bits string, or NULL
712  * @param p The pool to allocate from
713  */
714 APR_DECLARE(apr_status_t) apr_ipsubnet_create(apr_ipsubnet_t **ipsub,
715                                               const char *ipstr,
716                                               const char *mask_or_numbits,
717                                               apr_pool_t *p);
718 
719 /**
720  * Test the IP address in an apr_sockaddr_t against a pre-built ip-subnet
721  * representation.
722  * @param ipsub The ip-subnet representation
723  * @param sa The socket address to test
724  * @return non-zero if the socket address is within the subnet, 0 otherwise
725  */
726 APR_DECLARE(int) apr_ipsubnet_test(apr_ipsubnet_t *ipsub, apr_sockaddr_t *sa);
727 
728 #if APR_HAS_SO_ACCEPTFILTER || defined(DOXYGEN)
729 /**
730  * Set an OS level accept filter.
731  * @param sock The socket to put the accept filter on.
732  * @param name The accept filter
733  * @param args Any extra args to the accept filter.  Passing NULL here removes
734  *             the accept filter.
735  */
736 apr_status_t apr_socket_accept_filter(apr_socket_t *sock, char *name,
737                                       char *args);
738 #endif
739 
740 /**
741  * Return the protocol of the socket.
742  * @param sock The socket to query.
743  * @param protocol The returned protocol (e.g., APR_PROTO_TCP).
744  */
745 APR_DECLARE(apr_status_t) apr_socket_protocol_get(apr_socket_t *sock,
746                                                   int *protocol);
747 
748 /**
749  * Get the pool used by the socket.
750  */
751 APR_POOL_DECLARE_ACCESSOR(socket);
752 
753 /**
754  * Set a socket to be inherited by child processes.
755  */
756 APR_DECLARE_INHERIT_SET(socket);
757 
758 /**
759  * Unset a socket from being inherited by child processes.
760  */
761 APR_DECLARE_INHERIT_UNSET(socket);
762 
763 /**
764  * @defgroup apr_mcast IP Multicast
765  * @{
766  */
767 
768 /**
769  * Join a Multicast Group
770  * @param sock The socket to join a multicast group
771  * @param join The address of the multicast group to join
772  * @param iface Address of the interface to use.  If NULL is passed, the
773  *              default multicast interface will be used. (OS Dependent)
774  * @param source Source Address to accept transmissions from (non-NULL
775  *               implies Source-Specific Multicast)
776  */
777 APR_DECLARE(apr_status_t) apr_mcast_join(apr_socket_t *sock,
778                                          apr_sockaddr_t *join,
779                                          apr_sockaddr_t *iface,
780                                          apr_sockaddr_t *source);
781 
782 /**
783  * Leave a Multicast Group.  All arguments must be the same as
784  * apr_mcast_join.
785  * @param sock The socket to leave a multicast group
786  * @param addr The address of the multicast group to leave
787  * @param iface Address of the interface to use.  If NULL is passed, the
788  *              default multicast interface will be used. (OS Dependent)
789  * @param source Source Address to accept transmissions from (non-NULL
790  *               implies Source-Specific Multicast)
791  */
792 APR_DECLARE(apr_status_t) apr_mcast_leave(apr_socket_t *sock,
793                                           apr_sockaddr_t *addr,
794                                           apr_sockaddr_t *iface,
795                                           apr_sockaddr_t *source);
796 
797 /**
798  * Set the Multicast Time to Live (ttl) for a multicast transmission.
799  * @param sock The socket to set the multicast ttl
800  * @param ttl Time to live to Assign. 0-255, default=1
801  * @remark If the TTL is 0, packets will only be seen by sockets on
802  * the local machine, and only when multicast loopback is enabled.
803  */
804 APR_DECLARE(apr_status_t) apr_mcast_hops(apr_socket_t *sock,
805                                          apr_byte_t ttl);
806 
807 /**
808  * Toggle IP Multicast Loopback
809  * @param sock The socket to set multicast loopback
810  * @param opt 0=disable, 1=enable
811  */
812 APR_DECLARE(apr_status_t) apr_mcast_loopback(apr_socket_t *sock,
813                                              apr_byte_t opt);
814 
815 
816 /**
817  * Set the Interface to be used for outgoing Multicast Transmissions.
818  * @param sock The socket to set the multicast interface on
819  * @param iface Address of the interface to use for Multicast
820  */
821 APR_DECLARE(apr_status_t) apr_mcast_interface(apr_socket_t *sock,
822                                               apr_sockaddr_t *iface);
823 
824 /** @} */
825 
826 /** @} */
827 
828 #ifdef __cplusplus
829 }
830 #endif
831 
832 #endif  /* ! APR_NETWORK_IO_H */
833 
834