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