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