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