Apache Portable Runtime
apr_network_io.h
Go to the documentation of this file.
00001 /* Licensed to the Apache Software Foundation (ASF) under one or more
00002  * contributor license agreements.  See the NOTICE file distributed with
00003  * this work for additional information regarding copyright ownership.
00004  * The ASF licenses this file to You under the Apache License, Version 2.0
00005  * (the "License"); you may not use this file except in compliance with
00006  * the License.  You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #ifndef APR_NETWORK_IO_H
00018 #define APR_NETWORK_IO_H
00019 /**
00020  * @file apr_network_io.h
00021  * @brief APR Network library
00022  */
00023 
00024 #include "apr.h"
00025 #include "apr_pools.h"
00026 #include "apr_file_io.h"
00027 #include "apr_errno.h"
00028 #include "apr_inherit.h" 
00029 
00030 #if APR_HAVE_NETINET_IN_H
00031 #include <netinet/in.h>
00032 #endif
00033 
00034 #ifdef __cplusplus
00035 extern "C" {
00036 #endif /* __cplusplus */
00037 
00038 /**
00039  * @defgroup apr_network_io Network Routines
00040  * @ingroup APR 
00041  * @{
00042  */
00043 
00044 #ifndef APR_MAX_SECS_TO_LINGER
00045 /** Maximum seconds to linger */
00046 #define APR_MAX_SECS_TO_LINGER 30
00047 #endif
00048 
00049 #ifndef APRMAXHOSTLEN
00050 /** Maximum hostname length */
00051 #define APRMAXHOSTLEN 256
00052 #endif
00053 
00054 #ifndef APR_ANYADDR
00055 /** Default 'any' address */
00056 #define APR_ANYADDR "0.0.0.0"
00057 #endif
00058 
00059 /**
00060  * @defgroup apr_sockopt Socket option definitions
00061  * @{
00062  */
00063 #define APR_SO_LINGER        1    /**< Linger */
00064 #define APR_SO_KEEPALIVE     2    /**< Keepalive */
00065 #define APR_SO_DEBUG         4    /**< Debug */
00066 #define APR_SO_NONBLOCK      8    /**< Non-blocking IO */
00067 #define APR_SO_REUSEADDR     16   /**< Reuse addresses */
00068 #define APR_SO_SNDBUF        64   /**< Send buffer */
00069 #define APR_SO_RCVBUF        128  /**< Receive buffer */
00070 #define APR_SO_DISCONNECTED  256  /**< Disconnected */
00071 #define APR_TCP_NODELAY      512  /**< For SCTP sockets, this is mapped
00072                                    * to STCP_NODELAY internally.
00073                                    */
00074 #define APR_TCP_NOPUSH       1024 /**< No push */
00075 #define APR_RESET_NODELAY    2048 /**< This flag is ONLY set internally
00076                                    * when we set APR_TCP_NOPUSH with
00077                                    * APR_TCP_NODELAY set to tell us that
00078                                    * APR_TCP_NODELAY should be turned on
00079                                    * again when NOPUSH is turned off
00080                                    */
00081 #define APR_INCOMPLETE_READ 4096  /**< Set on non-blocking sockets
00082                                    * (timeout != 0) on which the
00083                                    * previous read() did not fill a buffer
00084                                    * completely.  the next apr_socket_recv() 
00085                                    * will first call select()/poll() rather than
00086                                    * going straight into read().  (Can also
00087                                    * be set by an application to force a
00088                                    * select()/poll() call before the next
00089                                    * read, in cases where the app expects
00090                                    * that an immediate read would fail.)
00091                                    */
00092 #define APR_INCOMPLETE_WRITE 8192 /**< like APR_INCOMPLETE_READ, but for write
00093                                    * @see APR_INCOMPLETE_READ
00094                                    */
00095 #define APR_IPV6_V6ONLY     16384 /**< Don't accept IPv4 connections on an
00096                                    * IPv6 listening socket.
00097                                    */
00098 #define APR_TCP_DEFER_ACCEPT 32768 /**< Delay accepting of new connections 
00099                                     * until data is available.
00100                                     * @see apr_socket_accept_filter
00101                                     */
00102 
00103 /** @} */
00104 
00105 /** Define what type of socket shutdown should occur. */
00106 typedef enum {
00107     APR_SHUTDOWN_READ,          /**< no longer allow read request */
00108     APR_SHUTDOWN_WRITE,         /**< no longer allow write requests */
00109     APR_SHUTDOWN_READWRITE      /**< no longer allow read or write requests */
00110 } apr_shutdown_how_e;
00111 
00112 #define APR_IPV4_ADDR_OK  0x01  /**< @see apr_sockaddr_info_get() */
00113 #define APR_IPV6_ADDR_OK  0x02  /**< @see apr_sockaddr_info_get() */
00114 
00115 #if (!APR_HAVE_IN_ADDR)
00116 /**
00117  * We need to make sure we always have an in_addr type, so APR will just
00118  * define it ourselves, if the platform doesn't provide it.
00119  */
00120 struct in_addr {
00121     apr_uint32_t  s_addr; /**< storage to hold the IP# */
00122 };
00123 #endif
00124 
00125 /** @def APR_INADDR_NONE
00126  * Not all platforms have a real INADDR_NONE.  This macro replaces
00127  * INADDR_NONE on all platforms.
00128  */
00129 #ifdef INADDR_NONE
00130 #define APR_INADDR_NONE INADDR_NONE
00131 #else
00132 #define APR_INADDR_NONE ((unsigned int) 0xffffffff)
00133 #endif
00134 
00135 /**
00136  * @def APR_INET
00137  * Not all platforms have these defined, so we'll define them here
00138  * The default values come from FreeBSD 4.1.1
00139  */
00140 #define APR_INET     AF_INET
00141 /** @def APR_UNSPEC
00142  * Let the system decide which address family to use
00143  */
00144 #ifdef AF_UNSPEC
00145 #define APR_UNSPEC   AF_UNSPEC
00146 #else
00147 #define APR_UNSPEC   0
00148 #endif
00149 #if APR_HAVE_IPV6
00150 /** @def APR_INET6
00151 * IPv6 Address Family. Not all platforms may have this defined.
00152 */
00153 
00154 #define APR_INET6    AF_INET6
00155 #endif
00156 
00157 /**
00158  * @defgroup IP_Proto IP Protocol Definitions for use when creating sockets
00159  * @{
00160  */
00161 #define APR_PROTO_TCP       6   /**< TCP  */
00162 #define APR_PROTO_UDP      17   /**< UDP  */
00163 #define APR_PROTO_SCTP    132   /**< SCTP */
00164 /** @} */
00165 
00166 /**
00167  * Enum used to denote either the local and remote endpoint of a
00168  * connection.
00169  */
00170 typedef enum {
00171     APR_LOCAL,   /**< Socket information for local end of connection */
00172     APR_REMOTE   /**< Socket information for remote end of connection */
00173 } apr_interface_e;
00174 
00175 /**
00176  * The specific declaration of inet_addr's ... some platforms fall back
00177  * inet_network (this is not good, but necessary)
00178  */
00179 
00180 #if APR_HAVE_INET_ADDR
00181 #define apr_inet_addr    inet_addr
00182 #elif APR_HAVE_INET_NETWORK        /* only DGUX, as far as I know */
00183 /**
00184  * @warning
00185  * not generally safe... inet_network() and inet_addr() perform
00186  * different functions */
00187 #define apr_inet_addr    inet_network
00188 #endif
00189 
00190 /** A structure to represent sockets */
00191 typedef struct apr_socket_t     apr_socket_t;
00192 /**
00193  * A structure to encapsulate headers and trailers for apr_socket_sendfile
00194  */
00195 typedef struct apr_hdtr_t       apr_hdtr_t;
00196 /** A structure to represent in_addr */
00197 typedef struct in_addr          apr_in_addr_t;
00198 /** A structure to represent an IP subnet */
00199 typedef struct apr_ipsubnet_t apr_ipsubnet_t;
00200 
00201 /** @remark use apr_uint16_t just in case some system has a short that isn't 16 bits... */
00202 typedef apr_uint16_t            apr_port_t;
00203 
00204 /** @remark It's defined here as I think it should all be platform safe...
00205  * @see apr_sockaddr_t
00206  */
00207 typedef struct apr_sockaddr_t apr_sockaddr_t;
00208 /**
00209  * APRs socket address type, used to ensure protocol independence
00210  */
00211 struct apr_sockaddr_t {
00212     /** The pool to use... */
00213     apr_pool_t *pool;
00214     /** The hostname */
00215     char *hostname;
00216     /** Either a string of the port number or the service name for the port */
00217     char *servname;
00218     /** The numeric port */
00219     apr_port_t port;
00220     /** The family */
00221     apr_int32_t family;
00222     /** How big is the sockaddr we're using? */
00223     apr_socklen_t salen;
00224     /** How big is the ip address structure we're using? */
00225     int ipaddr_len;
00226     /** How big should the address buffer be?  16 for v4 or 46 for v6
00227      *  used in inet_ntop... */
00228     int addr_str_len;
00229     /** This points to the IP address structure within the appropriate
00230      *  sockaddr structure.  */
00231     void *ipaddr_ptr;
00232     /** If multiple addresses were found by apr_sockaddr_info_get(), this 
00233      *  points to a representation of the next address. */
00234     apr_sockaddr_t *next;
00235     /** Union of either IPv4 or IPv6 sockaddr. */
00236     union {
00237         /** IPv4 sockaddr structure */
00238         struct sockaddr_in sin;
00239 #if APR_HAVE_IPV6
00240         /** IPv6 sockaddr structure */
00241         struct sockaddr_in6 sin6;
00242 #endif
00243 #if APR_HAVE_SA_STORAGE
00244         /** Placeholder to ensure that the size of this union is not
00245          * dependent on whether APR_HAVE_IPV6 is defined. */
00246         struct sockaddr_storage sas;
00247 #endif
00248     } sa;
00249 };
00250 
00251 #if APR_HAS_SENDFILE
00252 /** 
00253  * Support reusing the socket on platforms which support it (from disconnect,
00254  * specifically Win32.
00255  * @remark Optional flag passed into apr_socket_sendfile() 
00256  */
00257 #define APR_SENDFILE_DISCONNECT_SOCKET      1
00258 #endif
00259 
00260 /** A structure to encapsulate headers and trailers for apr_socket_sendfile */
00261 struct apr_hdtr_t {
00262     /** An iovec to store the headers sent before the file. */
00263     struct iovec* headers;
00264     /** number of headers in the iovec */
00265     int numheaders;
00266     /** An iovec to store the trailers sent after the file. */
00267     struct iovec* trailers;
00268     /** number of trailers in the iovec */
00269     int numtrailers;
00270 };
00271 
00272 /* function definitions */
00273 
00274 /**
00275  * Create a socket.
00276  * @param new_sock The new socket that has been set up.
00277  * @param family The address family of the socket (e.g., APR_INET).
00278  * @param type The type of the socket (e.g., SOCK_STREAM).
00279  * @param protocol The protocol of the socket (e.g., APR_PROTO_TCP).
00280  * @param cont The pool for the apr_socket_t and associated storage.
00281  */
00282 APR_DECLARE(apr_status_t) apr_socket_create(apr_socket_t **new_sock, 
00283                                             int family, int type,
00284                                             int protocol,
00285                                             apr_pool_t *cont);
00286 
00287 /**
00288  * Shutdown either reading, writing, or both sides of a socket.
00289  * @param thesocket The socket to close 
00290  * @param how How to shutdown the socket.  One of:
00291  * <PRE>
00292  *            APR_SHUTDOWN_READ         no longer allow read requests
00293  *            APR_SHUTDOWN_WRITE        no longer allow write requests
00294  *            APR_SHUTDOWN_READWRITE    no longer allow read or write requests 
00295  * </PRE>
00296  * @see apr_shutdown_how_e
00297  * @remark This does not actually close the socket descriptor, it just
00298  *      controls which calls are still valid on the socket.
00299  */
00300 APR_DECLARE(apr_status_t) apr_socket_shutdown(apr_socket_t *thesocket,
00301                                               apr_shutdown_how_e how);
00302 
00303 /**
00304  * Close a socket.
00305  * @param thesocket The socket to close 
00306  */
00307 APR_DECLARE(apr_status_t) apr_socket_close(apr_socket_t *thesocket);
00308 
00309 /**
00310  * Bind the socket to its associated port
00311  * @param sock The socket to bind 
00312  * @param sa The socket address to bind to
00313  * @remark This may be where we will find out if there is any other process
00314  *      using the selected port.
00315  */
00316 APR_DECLARE(apr_status_t) apr_socket_bind(apr_socket_t *sock, 
00317                                           apr_sockaddr_t *sa);
00318 
00319 /**
00320  * Listen to a bound socket for connections.
00321  * @param sock The socket to listen on 
00322  * @param backlog The number of outstanding connections allowed in the sockets
00323  *                listen queue.  If this value is less than zero, the listen
00324  *                queue size is set to zero.  
00325  */
00326 APR_DECLARE(apr_status_t) apr_socket_listen(apr_socket_t *sock, 
00327                                             apr_int32_t backlog);
00328 
00329 /**
00330  * Accept a new connection request
00331  * @param new_sock A copy of the socket that is connected to the socket that
00332  *                 made the connection request.  This is the socket which should
00333  *                 be used for all future communication.
00334  * @param sock The socket we are listening on.
00335  * @param connection_pool The pool for the new socket.
00336  */
00337 APR_DECLARE(apr_status_t) apr_socket_accept(apr_socket_t **new_sock, 
00338                                             apr_socket_t *sock,
00339                                             apr_pool_t *connection_pool);
00340 
00341 /**
00342  * Issue a connection request to a socket either on the same machine 
00343  * or a different one.
00344  * @param sock The socket we wish to use for our side of the connection 
00345  * @param sa The address of the machine we wish to connect to.
00346  */
00347 APR_DECLARE(apr_status_t) apr_socket_connect(apr_socket_t *sock,
00348                                              apr_sockaddr_t *sa);
00349 
00350 /**
00351  * Determine whether the receive part of the socket has been closed by
00352  * the peer (such that a subsequent call to apr_socket_read would
00353  * return APR_EOF), if the socket's receive buffer is empty.  This
00354  * function does not block waiting for I/O.
00355  *
00356  * @param sock The socket to check
00357  * @param atreadeof If APR_SUCCESS is returned, *atreadeof is set to
00358  *                  non-zero if a subsequent read would return APR_EOF
00359  * @return an error is returned if it was not possible to determine the
00360  *         status, in which case *atreadeof is not changed.
00361  */
00362 APR_DECLARE(apr_status_t) apr_socket_atreadeof(apr_socket_t *sock,
00363                                                int *atreadeof);
00364 
00365 /**
00366  * Create apr_sockaddr_t from hostname, address family, and port.
00367  * @param sa The new apr_sockaddr_t.
00368  * @param hostname The hostname or numeric address string to resolve/parse, or
00369  *               NULL to build an address that corresponds to 0.0.0.0 or ::
00370  * @param family The address family to use, or APR_UNSPEC if the system should 
00371  *               decide.
00372  * @param port The port number.
00373  * @param flags Special processing flags:
00374  * <PRE>
00375  *       APR_IPV4_ADDR_OK          first query for IPv4 addresses; only look
00376  *                                 for IPv6 addresses if the first query failed;
00377  *                                 only valid if family is APR_UNSPEC and hostname
00378  *                                 isn't NULL; mutually exclusive with
00379  *                                 APR_IPV6_ADDR_OK
00380  *       APR_IPV6_ADDR_OK          first query for IPv6 addresses; only look
00381  *                                 for IPv4 addresses if the first query failed;
00382  *                                 only valid if family is APR_UNSPEC and hostname
00383  *                                 isn't NULL and APR_HAVE_IPV6; mutually exclusive
00384  *                                 with APR_IPV4_ADDR_OK
00385  * </PRE>
00386  * @param p The pool for the apr_sockaddr_t and associated storage.
00387  */
00388 APR_DECLARE(apr_status_t) apr_sockaddr_info_get(apr_sockaddr_t **sa,
00389                                           const char *hostname,
00390                                           apr_int32_t family,
00391                                           apr_port_t port,
00392                                           apr_int32_t flags,
00393                                           apr_pool_t *p);
00394 
00395 /**
00396  * Look up the host name from an apr_sockaddr_t.
00397  * @param hostname The hostname.
00398  * @param sa The apr_sockaddr_t.
00399  * @param flags Special processing flags.
00400  */
00401 APR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname,
00402                                           apr_sockaddr_t *sa,
00403                                           apr_int32_t flags);
00404 
00405 /**
00406  * Parse hostname/IP address with scope id and port.
00407  *
00408  * Any of the following strings are accepted:
00409  *   8080                  (just the port number)
00410  *   www.apache.org        (just the hostname)
00411  *   www.apache.org:8080   (hostname and port number)
00412  *   [fe80::1]:80          (IPv6 numeric address string only)
00413  *   [fe80::1%eth0]        (IPv6 numeric address string and scope id)
00414  *
00415  * Invalid strings:
00416  *                         (empty string)
00417  *   [abc]                 (not valid IPv6 numeric address string)
00418  *   abc:65536             (invalid port number)
00419  *
00420  * @param addr The new buffer containing just the hostname.  On output, *addr 
00421  *             will be NULL if no hostname/IP address was specfied.
00422  * @param scope_id The new buffer containing just the scope id.  On output, 
00423  *                 *scope_id will be NULL if no scope id was specified.
00424  * @param port The port number.  On output, *port will be 0 if no port was 
00425  *             specified.
00426  *             ### FIXME: 0 is a legal port (per RFC 1700). this should
00427  *             ### return something besides zero if the port is missing.
00428  * @param str The input string to be parsed.
00429  * @param p The pool from which *addr and *scope_id are allocated.
00430  * @remark If scope id shouldn't be allowed, check for scope_id != NULL in 
00431  *         addition to checking the return code.  If addr/hostname should be 
00432  *         required, check for addr == NULL in addition to checking the 
00433  *         return code.
00434  */
00435 APR_DECLARE(apr_status_t) apr_parse_addr_port(char **addr,
00436                                               char **scope_id,
00437                                               apr_port_t *port,
00438                                               const char *str,
00439                                               apr_pool_t *p);
00440 
00441 /**
00442  * Get name of the current machine
00443  * @param buf A buffer to store the hostname in.
00444  * @param len The maximum length of the hostname that can be stored in the
00445  *            buffer provided.  The suggested length is APRMAXHOSTLEN + 1.
00446  * @param cont The pool to use.
00447  * @remark If the buffer was not large enough, an error will be returned.
00448  */
00449 APR_DECLARE(apr_status_t) apr_gethostname(char *buf, int len, apr_pool_t *cont);
00450 
00451 /**
00452  * Return the data associated with the current socket
00453  * @param data The user data associated with the socket.
00454  * @param key The key to associate with the user data.
00455  * @param sock The currently open socket.
00456  */
00457 APR_DECLARE(apr_status_t) apr_socket_data_get(void **data, const char *key,
00458                                               apr_socket_t *sock);
00459 
00460 /**
00461  * Set the data associated with the current socket.
00462  * @param sock The currently open socket.
00463  * @param data The user data to associate with the socket.
00464  * @param key The key to associate with the data.
00465  * @param cleanup The cleanup to call when the socket is destroyed.
00466  */
00467 APR_DECLARE(apr_status_t) apr_socket_data_set(apr_socket_t *sock, void *data,
00468                                               const char *key,
00469                                               apr_status_t (*cleanup)(void*));
00470 
00471 /**
00472  * Send data over a network.
00473  * @param sock The socket to send the data over.
00474  * @param buf The buffer which contains the data to be sent. 
00475  * @param len On entry, the number of bytes to send; on exit, the number
00476  *            of bytes sent.
00477  * @remark
00478  * <PRE>
00479  * This functions acts like a blocking write by default.  To change 
00480  * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
00481  * socket option.
00482  *
00483  * It is possible for both bytes to be sent and an error to be returned.
00484  *
00485  * APR_EINTR is never returned.
00486  * </PRE>
00487  */
00488 APR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf, 
00489                                           apr_size_t *len);
00490 
00491 /**
00492  * Send multiple packets of data over a network.
00493  * @param sock The socket to send the data over.
00494  * @param vec The array of iovec structs containing the data to send 
00495  * @param nvec The number of iovec structs in the array
00496  * @param len Receives the number of bytes actually written
00497  * @remark
00498  * <PRE>
00499  * This functions acts like a blocking write by default.  To change 
00500  * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
00501  * socket option.
00502  * The number of bytes actually sent is stored in argument 3.
00503  *
00504  * It is possible for both bytes to be sent and an error to be returned.
00505  *
00506  * APR_EINTR is never returned.
00507  * </PRE>
00508  */
00509 APR_DECLARE(apr_status_t) apr_socket_sendv(apr_socket_t *sock, 
00510                                            const struct iovec *vec,
00511                                            apr_int32_t nvec, apr_size_t *len);
00512 
00513 /**
00514  * @param sock The socket to send from
00515  * @param where The apr_sockaddr_t describing where to send the data
00516  * @param flags The flags to use
00517  * @param buf  The data to send
00518  * @param len  The length of the data to send
00519  */
00520 APR_DECLARE(apr_status_t) apr_socket_sendto(apr_socket_t *sock, 
00521                                             apr_sockaddr_t *where,
00522                                             apr_int32_t flags, const char *buf, 
00523                                             apr_size_t *len);
00524 
00525 /**
00526  * Read data from a socket.  On success, the address of the peer from
00527  * which the data was sent is copied into the @a from parameter, and the
00528  * @a len parameter is updated to give the number of bytes written to
00529  * @a buf.
00530  *
00531  * @param from Updated with the address from which the data was received
00532  * @param sock The socket to use
00533  * @param flags The flags to use
00534  * @param buf  The buffer to use
00535  * @param len  The length of the available buffer
00536  */
00537 
00538 APR_DECLARE(apr_status_t) apr_socket_recvfrom(apr_sockaddr_t *from, 
00539                                               apr_socket_t *sock,
00540                                               apr_int32_t flags, char *buf, 
00541                                               apr_size_t *len);
00542  
00543 #if APR_HAS_SENDFILE || defined(DOXYGEN)
00544 
00545 /**
00546  * Send a file from an open file descriptor to a socket, along with 
00547  * optional headers and trailers
00548  * @param sock The socket to which we're writing
00549  * @param file The open file from which to read
00550  * @param hdtr A structure containing the headers and trailers to send
00551  * @param offset Offset into the file where we should begin writing
00552  * @param len (input)  - Number of bytes to send from the file 
00553  *            (output) - Number of bytes actually sent, 
00554  *                       including headers, file, and trailers
00555  * @param flags APR flags that are mapped to OS specific flags
00556  * @remark This functions acts like a blocking write by default.  To change 
00557  *         this behavior, use apr_socket_timeout_set() or the
00558  *         APR_SO_NONBLOCK socket option.
00559  * The number of bytes actually sent is stored in the len parameter.
00560  * The offset parameter is passed by reference for no reason; its
00561  * value will never be modified by the apr_socket_sendfile() function.
00562  */
00563 APR_DECLARE(apr_status_t) apr_socket_sendfile(apr_socket_t *sock, 
00564                                               apr_file_t *file,
00565                                               apr_hdtr_t *hdtr,
00566                                               apr_off_t *offset,
00567                                               apr_size_t *len,
00568                                               apr_int32_t flags);
00569 
00570 #endif /* APR_HAS_SENDFILE */
00571 
00572 /**
00573  * Read data from a network.
00574  * @param sock The socket to read the data from.
00575  * @param buf The buffer to store the data in. 
00576  * @param len On entry, the number of bytes to receive; on exit, the number
00577  *            of bytes received.
00578  * @remark
00579  * <PRE>
00580  * This functions acts like a blocking read by default.  To change 
00581  * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
00582  * socket option.
00583  * The number of bytes actually received is stored in argument 3.
00584  *
00585  * It is possible for both bytes to be received and an APR_EOF or
00586  * other error to be returned.
00587  *
00588  * APR_EINTR is never returned.
00589  * </PRE>
00590  */
00591 APR_DECLARE(apr_status_t) apr_socket_recv(apr_socket_t *sock, 
00592                                    char *buf, apr_size_t *len);
00593 
00594 /**
00595  * Setup socket options for the specified socket
00596  * @param sock The socket to set up.
00597  * @param opt The option we would like to configure.  One of:
00598  * <PRE>
00599  *            APR_SO_DEBUG      --  turn on debugging information 
00600  *            APR_SO_KEEPALIVE  --  keep connections active
00601  *            APR_SO_LINGER     --  lingers on close if data is present
00602  *            APR_SO_NONBLOCK   --  Turns blocking on/off for socket
00603  *                                  When this option is enabled, use
00604  *                                  the APR_STATUS_IS_EAGAIN() macro to
00605  *                                  see if a send or receive function
00606  *                                  could not transfer data without
00607  *                                  blocking.
00608  *            APR_SO_REUSEADDR  --  The rules used in validating addresses
00609  *                                  supplied to bind should allow reuse
00610  *                                  of local addresses.
00611  *            APR_SO_SNDBUF     --  Set the SendBufferSize
00612  *            APR_SO_RCVBUF     --  Set the ReceiveBufferSize
00613  * </PRE>
00614  * @param on Value for the option.
00615  */
00616 APR_DECLARE(apr_status_t) apr_socket_opt_set(apr_socket_t *sock,
00617                                              apr_int32_t opt, apr_int32_t on);
00618 
00619 /**
00620  * Setup socket timeout for the specified socket
00621  * @param sock The socket to set up.
00622  * @param t Value for the timeout.
00623  * <PRE>
00624  *   t > 0  -- read and write calls return APR_TIMEUP if specified time
00625  *             elapsess with no data read or written
00626  *   t == 0 -- read and write calls never block
00627  *   t < 0  -- read and write calls block
00628  * </PRE>
00629  */
00630 APR_DECLARE(apr_status_t) apr_socket_timeout_set(apr_socket_t *sock,
00631                                                  apr_interval_time_t t);
00632 
00633 /**
00634  * Query socket options for the specified socket
00635  * @param sock The socket to query
00636  * @param opt The option we would like to query.  One of:
00637  * <PRE>
00638  *            APR_SO_DEBUG      --  turn on debugging information 
00639  *            APR_SO_KEEPALIVE  --  keep connections active
00640  *            APR_SO_LINGER     --  lingers on close if data is present
00641  *            APR_SO_NONBLOCK   --  Turns blocking on/off for socket
00642  *            APR_SO_REUSEADDR  --  The rules used in validating addresses
00643  *                                  supplied to bind should allow reuse
00644  *                                  of local addresses.
00645  *            APR_SO_SNDBUF     --  Set the SendBufferSize
00646  *            APR_SO_RCVBUF     --  Set the ReceiveBufferSize
00647  *            APR_SO_DISCONNECTED -- Query the disconnected state of the socket.
00648  *                                  (Currently only used on Windows)
00649  * </PRE>
00650  * @param on Socket option returned on the call.
00651  */
00652 APR_DECLARE(apr_status_t) apr_socket_opt_get(apr_socket_t *sock, 
00653                                              apr_int32_t opt, apr_int32_t *on);
00654 
00655 /**
00656  * Query socket timeout for the specified socket
00657  * @param sock The socket to query
00658  * @param t Socket timeout returned from the query.
00659  */
00660 APR_DECLARE(apr_status_t) apr_socket_timeout_get(apr_socket_t *sock, 
00661                                                  apr_interval_time_t *t);
00662 
00663 /**
00664  * Query the specified socket if at the OOB/Urgent data mark
00665  * @param sock The socket to query
00666  * @param atmark Is set to true if socket is at the OOB/urgent mark,
00667  *               otherwise is set to false.
00668  */
00669 APR_DECLARE(apr_status_t) apr_socket_atmark(apr_socket_t *sock, 
00670                                             int *atmark);
00671 
00672 /**
00673  * Return an address associated with a socket; either the address to
00674  * which the socket is bound locally or the the address of the peer
00675  * to which the socket is connected.
00676  * @param sa The returned apr_sockaddr_t.
00677  * @param which Whether to retrieve the local or remote address
00678  * @param sock The socket to use
00679  */
00680 APR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa,
00681                                               apr_interface_e which,
00682                                               apr_socket_t *sock);
00683  
00684 /**
00685  * Return the IP address (in numeric address string format) in
00686  * an APR socket address.  APR will allocate storage for the IP address 
00687  * string from the pool of the apr_sockaddr_t.
00688  * @param addr The IP address.
00689  * @param sockaddr The socket address to reference.
00690  */
00691 APR_DECLARE(apr_status_t) apr_sockaddr_ip_get(char **addr, 
00692                                               apr_sockaddr_t *sockaddr);
00693 
00694 /**
00695  * Write the IP address (in numeric address string format) of the APR
00696  * socket address @a sockaddr into the buffer @a buf (of size @a buflen).
00697  * @param sockaddr The socket address to reference.
00698  */
00699 APR_DECLARE(apr_status_t) apr_sockaddr_ip_getbuf(char *buf, apr_size_t buflen,
00700                                                  apr_sockaddr_t *sockaddr);
00701 
00702 /**
00703  * See if the IP addresses in two APR socket addresses are
00704  * equivalent.  Appropriate logic is present for comparing
00705  * IPv4-mapped IPv6 addresses with IPv4 addresses.
00706  *
00707  * @param addr1 One of the APR socket addresses.
00708  * @param addr2 The other APR socket address.
00709  * @remark The return value will be non-zero if the addresses
00710  * are equivalent.
00711  */
00712 APR_DECLARE(int) apr_sockaddr_equal(const apr_sockaddr_t *addr1,
00713                                     const apr_sockaddr_t *addr2);
00714 
00715 /**
00716 * Return the type of the socket.
00717 * @param sock The socket to query.
00718 * @param type The returned type (e.g., SOCK_STREAM).
00719 */
00720 APR_DECLARE(apr_status_t) apr_socket_type_get(apr_socket_t *sock,
00721                                               int *type);
00722  
00723 /**
00724  * Given an apr_sockaddr_t and a service name, set the port for the service
00725  * @param sockaddr The apr_sockaddr_t that will have its port set
00726  * @param servname The name of the service you wish to use
00727  */
00728 APR_DECLARE(apr_status_t) apr_getservbyname(apr_sockaddr_t *sockaddr, 
00729                                             const char *servname);
00730 /**
00731  * Build an ip-subnet representation from an IP address and optional netmask or
00732  * number-of-bits.
00733  * @param ipsub The new ip-subnet representation
00734  * @param ipstr The input IP address string
00735  * @param mask_or_numbits The input netmask or number-of-bits string, or NULL
00736  * @param p The pool to allocate from
00737  */
00738 APR_DECLARE(apr_status_t) apr_ipsubnet_create(apr_ipsubnet_t **ipsub, 
00739                                               const char *ipstr, 
00740                                               const char *mask_or_numbits, 
00741                                               apr_pool_t *p);
00742 
00743 /**
00744  * Test the IP address in an apr_sockaddr_t against a pre-built ip-subnet
00745  * representation.
00746  * @param ipsub The ip-subnet representation
00747  * @param sa The socket address to test
00748  * @return non-zero if the socket address is within the subnet, 0 otherwise
00749  */
00750 APR_DECLARE(int) apr_ipsubnet_test(apr_ipsubnet_t *ipsub, apr_sockaddr_t *sa);
00751 
00752 #if APR_HAS_SO_ACCEPTFILTER || defined(DOXYGEN)
00753 /**
00754  * Set an OS level accept filter.
00755  * @param sock The socket to put the accept filter on.
00756  * @param name The accept filter
00757  * @param args Any extra args to the accept filter.  Passing NULL here removes
00758  *             the accept filter. 
00759  */
00760 apr_status_t apr_socket_accept_filter(apr_socket_t *sock, char *name,
00761                                       char *args);
00762 #endif
00763 
00764 /**
00765  * Return the protocol of the socket.
00766  * @param sock The socket to query.
00767  * @param protocol The returned protocol (e.g., APR_PROTO_TCP).
00768  */
00769 APR_DECLARE(apr_status_t) apr_socket_protocol_get(apr_socket_t *sock,
00770                                                   int *protocol);
00771 
00772 /**
00773  * Get the pool used by the socket.
00774  */
00775 APR_POOL_DECLARE_ACCESSOR(socket);
00776 
00777 /**
00778  * Set a socket to be inherited by child processes.
00779  */
00780 APR_DECLARE_INHERIT_SET(socket);
00781 
00782 /**
00783  * Unset a socket from being inherited by child processes.
00784  */
00785 APR_DECLARE_INHERIT_UNSET(socket);
00786 
00787 /**
00788  * @defgroup apr_mcast IP Multicast
00789  * @{
00790  */
00791 
00792 /**
00793  * Join a Multicast Group
00794  * @param sock The socket to join a multicast group
00795  * @param join The address of the multicast group to join
00796  * @param iface Address of the interface to use.  If NULL is passed, the 
00797  *              default multicast interface will be used. (OS Dependent)
00798  * @param source Source Address to accept transmissions from (non-NULL 
00799  *               implies Source-Specific Multicast)
00800  */
00801 APR_DECLARE(apr_status_t) apr_mcast_join(apr_socket_t *sock,
00802                                          apr_sockaddr_t *join,
00803                                          apr_sockaddr_t *iface,
00804                                          apr_sockaddr_t *source);
00805 
00806 /**
00807  * Leave a Multicast Group.  All arguments must be the same as
00808  * apr_mcast_join.
00809  * @param sock The socket to leave a multicast group
00810  * @param addr The address of the multicast group to leave
00811  * @param iface Address of the interface to use.  If NULL is passed, the 
00812  *              default multicast interface will be used. (OS Dependent)
00813  * @param source Source Address to accept transmissions from (non-NULL 
00814  *               implies Source-Specific Multicast)
00815  */
00816 APR_DECLARE(apr_status_t) apr_mcast_leave(apr_socket_t *sock,
00817                                           apr_sockaddr_t *addr,
00818                                           apr_sockaddr_t *iface,
00819                                           apr_sockaddr_t *source);
00820 
00821 /**
00822  * Set the Multicast Time to Live (ttl) for a multicast transmission.
00823  * @param sock The socket to set the multicast ttl
00824  * @param ttl Time to live to Assign. 0-255, default=1
00825  * @remark If the TTL is 0, packets will only be seen by sockets on 
00826  * the local machine, and only when multicast loopback is enabled.
00827  */
00828 APR_DECLARE(apr_status_t) apr_mcast_hops(apr_socket_t *sock,
00829                                          apr_byte_t ttl);
00830 
00831 /**
00832  * Toggle IP Multicast Loopback
00833  * @param sock The socket to set multicast loopback
00834  * @param opt 0=disable, 1=enable
00835  */
00836 APR_DECLARE(apr_status_t) apr_mcast_loopback(apr_socket_t *sock,
00837                                              apr_byte_t opt);
00838 
00839 
00840 /**
00841  * Set the Interface to be used for outgoing Multicast Transmissions.
00842  * @param sock The socket to set the multicast interface on
00843  * @param iface Address of the interface to use for Multicast
00844  */
00845 APR_DECLARE(apr_status_t) apr_mcast_interface(apr_socket_t *sock,
00846                                               apr_sockaddr_t *iface);
00847 
00848 /** @} */
00849 
00850 /** @} */
00851 
00852 #ifdef __cplusplus
00853 }
00854 #endif
00855 
00856 #endif  /* ! APR_NETWORK_IO_H */
00857 
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines