1251875Speter/* Licensed to the Apache Software Foundation (ASF) under one or more
2251875Speter * contributor license agreements.  See the NOTICE file distributed with
3251875Speter * this work for additional information regarding copyright ownership.
4251875Speter * The ASF licenses this file to You under the Apache License, Version 2.0
5251875Speter * (the "License"); you may not use this file except in compliance with
6251875Speter * the License.  You may obtain a copy of the License at
7251875Speter *
8251875Speter *     http://www.apache.org/licenses/LICENSE-2.0
9251875Speter *
10251875Speter * Unless required by applicable law or agreed to in writing, software
11251875Speter * distributed under the License is distributed on an "AS IS" BASIS,
12251875Speter * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13251875Speter * See the License for the specific language governing permissions and
14251875Speter * limitations under the License.
15251875Speter */
16251875Speter
17251875Speter#ifndef APR_NETWORK_IO_H
18251875Speter#define APR_NETWORK_IO_H
19251875Speter/**
20251875Speter * @file apr_network_io.h
21251875Speter * @brief APR Network library
22251875Speter */
23251875Speter
24251875Speter#include "apr.h"
25251875Speter#include "apr_pools.h"
26251875Speter#include "apr_file_io.h"
27251875Speter#include "apr_errno.h"
28251875Speter#include "apr_inherit.h"
29251875Speter
30251875Speter#if APR_HAVE_NETINET_IN_H
31251875Speter#include <netinet/in.h>
32251875Speter#endif
33251875Speter
34251875Speter#ifdef __cplusplus
35251875Speterextern "C" {
36251875Speter#endif /* __cplusplus */
37251875Speter
38251875Speter/**
39251875Speter * @defgroup apr_network_io Network Routines
40251875Speter * @ingroup APR
41251875Speter * @{
42251875Speter */
43251875Speter
44251875Speter#ifndef APR_MAX_SECS_TO_LINGER
45251875Speter/** Maximum seconds to linger */
46251875Speter#define APR_MAX_SECS_TO_LINGER 30
47251875Speter#endif
48251875Speter
49251875Speter#ifndef APRMAXHOSTLEN
50251875Speter/** Maximum hostname length */
51251875Speter#define APRMAXHOSTLEN 256
52251875Speter#endif
53251875Speter
54251875Speter#ifndef APR_ANYADDR
55251875Speter/** Default 'any' address */
56251875Speter#define APR_ANYADDR "0.0.0.0"
57251875Speter#endif
58251875Speter
59251875Speter/**
60251875Speter * @defgroup apr_sockopt Socket option definitions
61251875Speter * @{
62251875Speter */
63251875Speter#define APR_SO_LINGER        1    /**< Linger */
64251875Speter#define APR_SO_KEEPALIVE     2    /**< Keepalive */
65251875Speter#define APR_SO_DEBUG         4    /**< Debug */
66251875Speter#define APR_SO_NONBLOCK      8    /**< Non-blocking IO */
67251875Speter#define APR_SO_REUSEADDR     16   /**< Reuse addresses */
68251875Speter#define APR_SO_SNDBUF        64   /**< Send buffer */
69251875Speter#define APR_SO_RCVBUF        128  /**< Receive buffer */
70251875Speter#define APR_SO_DISCONNECTED  256  /**< Disconnected */
71251875Speter#define APR_TCP_NODELAY      512  /**< For SCTP sockets, this is mapped
72251875Speter                                   * to STCP_NODELAY internally.
73251875Speter                                   */
74251875Speter#define APR_TCP_NOPUSH       1024 /**< No push */
75251875Speter#define APR_RESET_NODELAY    2048 /**< This flag is ONLY set internally
76251875Speter                                   * when we set APR_TCP_NOPUSH with
77251875Speter                                   * APR_TCP_NODELAY set to tell us that
78251875Speter                                   * APR_TCP_NODELAY should be turned on
79251875Speter                                   * again when NOPUSH is turned off
80251875Speter                                   */
81251875Speter#define APR_INCOMPLETE_READ 4096  /**< Set on non-blocking sockets
82251875Speter				   * (timeout != 0) on which the
83251875Speter				   * previous read() did not fill a buffer
84251875Speter				   * completely.  the next apr_socket_recv()
85251875Speter                                   * will first call select()/poll() rather than
86251875Speter				   * going straight into read().  (Can also
87251875Speter				   * be set by an application to force a
88251875Speter				   * select()/poll() call before the next
89251875Speter				   * read, in cases where the app expects
90251875Speter				   * that an immediate read would fail.)
91251875Speter				   */
92251875Speter#define APR_INCOMPLETE_WRITE 8192 /**< like APR_INCOMPLETE_READ, but for write
93251875Speter                                   * @see APR_INCOMPLETE_READ
94251875Speter                                   */
95251875Speter#define APR_IPV6_V6ONLY     16384 /**< Don't accept IPv4 connections on an
96251875Speter                                   * IPv6 listening socket.
97251875Speter                                   */
98251875Speter#define APR_TCP_DEFER_ACCEPT 32768 /**< Delay accepting of new connections
99251875Speter                                    * until data is available.
100251875Speter                                    * @see apr_socket_accept_filter
101251875Speter                                    */
102269847Speter#define APR_SO_BROADCAST     65536 /**< Allow broadcast
103269847Speter                                    */
104251875Speter
105251875Speter/** @} */
106251875Speter
107251875Speter/** Define what type of socket shutdown should occur. */
108251875Spetertypedef enum {
109251875Speter    APR_SHUTDOWN_READ,          /**< no longer allow read request */
110251875Speter    APR_SHUTDOWN_WRITE,         /**< no longer allow write requests */
111251875Speter    APR_SHUTDOWN_READWRITE      /**< no longer allow read or write requests */
112251875Speter} apr_shutdown_how_e;
113251875Speter
114251875Speter#define APR_IPV4_ADDR_OK  0x01  /**< @see apr_sockaddr_info_get() */
115251875Speter#define APR_IPV6_ADDR_OK  0x02  /**< @see apr_sockaddr_info_get() */
116251875Speter
117251875Speter#if (!APR_HAVE_IN_ADDR)
118251875Speter/**
119251875Speter * We need to make sure we always have an in_addr type, so APR will just
120251875Speter * define it ourselves, if the platform doesn't provide it.
121251875Speter */
122251875Speterstruct in_addr {
123251875Speter    apr_uint32_t  s_addr; /**< storage to hold the IP# */
124251875Speter};
125251875Speter#endif
126251875Speter
127251875Speter/** @def APR_INADDR_NONE
128251875Speter * Not all platforms have a real INADDR_NONE.  This macro replaces
129251875Speter * INADDR_NONE on all platforms.
130251875Speter */
131251875Speter#ifdef INADDR_NONE
132251875Speter#define APR_INADDR_NONE INADDR_NONE
133251875Speter#else
134251875Speter#define APR_INADDR_NONE ((unsigned int) 0xffffffff)
135251875Speter#endif
136251875Speter
137251875Speter/**
138251875Speter * @def APR_INET
139251875Speter * Not all platforms have these defined, so we'll define them here
140251875Speter * The default values come from FreeBSD 4.1.1
141251875Speter */
142251875Speter#define APR_INET     AF_INET
143251875Speter/** @def APR_UNSPEC
144251875Speter * Let the system decide which address family to use
145251875Speter */
146251875Speter#ifdef AF_UNSPEC
147251875Speter#define APR_UNSPEC   AF_UNSPEC
148251875Speter#else
149251875Speter#define APR_UNSPEC   0
150251875Speter#endif
151251875Speter#if APR_HAVE_IPV6
152251875Speter/** @def APR_INET6
153251875Speter* IPv6 Address Family. Not all platforms may have this defined.
154251875Speter*/
155251875Speter
156251875Speter#define APR_INET6    AF_INET6
157251875Speter#endif
158251875Speter
159251875Speter/**
160251875Speter * @defgroup IP_Proto IP Protocol Definitions for use when creating sockets
161251875Speter * @{
162251875Speter */
163251875Speter#define APR_PROTO_TCP       6   /**< TCP  */
164251875Speter#define APR_PROTO_UDP      17   /**< UDP  */
165251875Speter#define APR_PROTO_SCTP    132   /**< SCTP */
166251875Speter/** @} */
167251875Speter
168251875Speter/**
169251875Speter * Enum used to denote either the local and remote endpoint of a
170251875Speter * connection.
171251875Speter */
172251875Spetertypedef enum {
173251875Speter    APR_LOCAL,   /**< Socket information for local end of connection */
174251875Speter    APR_REMOTE   /**< Socket information for remote end of connection */
175251875Speter} apr_interface_e;
176251875Speter
177251875Speter/**
178251875Speter * The specific declaration of inet_addr's ... some platforms fall back
179251875Speter * inet_network (this is not good, but necessary)
180251875Speter */
181251875Speter
182251875Speter#if APR_HAVE_INET_ADDR
183251875Speter#define apr_inet_addr    inet_addr
184251875Speter#elif APR_HAVE_INET_NETWORK        /* only DGUX, as far as I know */
185251875Speter/**
186251875Speter * @warning
187251875Speter * not generally safe... inet_network() and inet_addr() perform
188251875Speter * different functions */
189251875Speter#define apr_inet_addr    inet_network
190251875Speter#endif
191251875Speter
192251875Speter/** A structure to represent sockets */
193251875Spetertypedef struct apr_socket_t     apr_socket_t;
194251875Speter/**
195251875Speter * A structure to encapsulate headers and trailers for apr_socket_sendfile
196251875Speter */
197251875Spetertypedef struct apr_hdtr_t       apr_hdtr_t;
198251875Speter/** A structure to represent in_addr */
199251875Spetertypedef struct in_addr          apr_in_addr_t;
200251875Speter/** A structure to represent an IP subnet */
201251875Spetertypedef struct apr_ipsubnet_t apr_ipsubnet_t;
202251875Speter
203251875Speter/** @remark use apr_uint16_t just in case some system has a short that isn't 16 bits... */
204251875Spetertypedef apr_uint16_t            apr_port_t;
205251875Speter
206251875Speter/** @remark It's defined here as I think it should all be platform safe...
207251875Speter * @see apr_sockaddr_t
208251875Speter */
209251875Spetertypedef struct apr_sockaddr_t apr_sockaddr_t;
210251875Speter/**
211251875Speter * APRs socket address type, used to ensure protocol independence
212251875Speter */
213251875Speterstruct apr_sockaddr_t {
214251875Speter    /** The pool to use... */
215251875Speter    apr_pool_t *pool;
216251875Speter    /** The hostname */
217251875Speter    char *hostname;
218251875Speter    /** Either a string of the port number or the service name for the port */
219251875Speter    char *servname;
220251875Speter    /** The numeric port */
221251875Speter    apr_port_t port;
222251875Speter    /** The family */
223251875Speter    apr_int32_t family;
224251875Speter    /** How big is the sockaddr we're using? */
225251875Speter    apr_socklen_t salen;
226251875Speter    /** How big is the ip address structure we're using? */
227251875Speter    int ipaddr_len;
228251875Speter    /** How big should the address buffer be?  16 for v4 or 46 for v6
229251875Speter     *  used in inet_ntop... */
230251875Speter    int addr_str_len;
231251875Speter    /** This points to the IP address structure within the appropriate
232251875Speter     *  sockaddr structure.  */
233251875Speter    void *ipaddr_ptr;
234251875Speter    /** If multiple addresses were found by apr_sockaddr_info_get(), this
235251875Speter     *  points to a representation of the next address. */
236251875Speter    apr_sockaddr_t *next;
237251875Speter    /** Union of either IPv4 or IPv6 sockaddr. */
238251875Speter    union {
239251875Speter        /** IPv4 sockaddr structure */
240251875Speter        struct sockaddr_in sin;
241251875Speter#if APR_HAVE_IPV6
242251875Speter        /** IPv6 sockaddr structure */
243251875Speter        struct sockaddr_in6 sin6;
244251875Speter#endif
245251875Speter#if APR_HAVE_SA_STORAGE
246251875Speter        /** Placeholder to ensure that the size of this union is not
247251875Speter         * dependent on whether APR_HAVE_IPV6 is defined. */
248251875Speter        struct sockaddr_storage sas;
249251875Speter#endif
250251875Speter    } sa;
251251875Speter};
252251875Speter
253251875Speter#if APR_HAS_SENDFILE
254251875Speter/**
255251875Speter * Support reusing the socket on platforms which support it (from disconnect,
256251875Speter * specifically Win32.
257251875Speter * @remark Optional flag passed into apr_socket_sendfile()
258251875Speter */
259251875Speter#define APR_SENDFILE_DISCONNECT_SOCKET      1
260251875Speter#endif
261251875Speter
262251875Speter/** A structure to encapsulate headers and trailers for apr_socket_sendfile */
263251875Speterstruct apr_hdtr_t {
264251875Speter    /** An iovec to store the headers sent before the file. */
265251875Speter    struct iovec* headers;
266251875Speter    /** number of headers in the iovec */
267251875Speter    int numheaders;
268251875Speter    /** An iovec to store the trailers sent after the file. */
269251875Speter    struct iovec* trailers;
270251875Speter    /** number of trailers in the iovec */
271251875Speter    int numtrailers;
272251875Speter};
273251875Speter
274251875Speter/* function definitions */
275251875Speter
276251875Speter/**
277251875Speter * Create a socket.
278251875Speter * @param new_sock The new socket that has been set up.
279251875Speter * @param family The address family of the socket (e.g., APR_INET).
280251875Speter * @param type The type of the socket (e.g., SOCK_STREAM).
281251875Speter * @param protocol The protocol of the socket (e.g., APR_PROTO_TCP).
282251875Speter * @param cont The pool for the apr_socket_t and associated storage.
283269847Speter * @note The pool will be used by various functions that operate on the
284269847Speter *       socket. The caller must ensure that it is not used by other threads
285269847Speter *       at the same time.
286251875Speter */
287251875SpeterAPR_DECLARE(apr_status_t) apr_socket_create(apr_socket_t **new_sock,
288251875Speter                                            int family, int type,
289251875Speter                                            int protocol,
290251875Speter                                            apr_pool_t *cont);
291251875Speter
292251875Speter/**
293251875Speter * Shutdown either reading, writing, or both sides of a socket.
294251875Speter * @param thesocket The socket to close
295251875Speter * @param how How to shutdown the socket.  One of:
296251875Speter * <PRE>
297251875Speter *            APR_SHUTDOWN_READ         no longer allow read requests
298251875Speter *            APR_SHUTDOWN_WRITE        no longer allow write requests
299251875Speter *            APR_SHUTDOWN_READWRITE    no longer allow read or write requests
300251875Speter * </PRE>
301251875Speter * @see apr_shutdown_how_e
302251875Speter * @remark This does not actually close the socket descriptor, it just
303251875Speter *      controls which calls are still valid on the socket.
304251875Speter */
305251875SpeterAPR_DECLARE(apr_status_t) apr_socket_shutdown(apr_socket_t *thesocket,
306251875Speter                                              apr_shutdown_how_e how);
307251875Speter
308251875Speter/**
309251875Speter * Close a socket.
310251875Speter * @param thesocket The socket to close
311251875Speter */
312251875SpeterAPR_DECLARE(apr_status_t) apr_socket_close(apr_socket_t *thesocket);
313251875Speter
314251875Speter/**
315251875Speter * Bind the socket to its associated port
316251875Speter * @param sock The socket to bind
317251875Speter * @param sa The socket address to bind to
318251875Speter * @remark This may be where we will find out if there is any other process
319251875Speter *      using the selected port.
320251875Speter */
321251875SpeterAPR_DECLARE(apr_status_t) apr_socket_bind(apr_socket_t *sock,
322251875Speter                                          apr_sockaddr_t *sa);
323251875Speter
324251875Speter/**
325251875Speter * Listen to a bound socket for connections.
326251875Speter * @param sock The socket to listen on
327251875Speter * @param backlog The number of outstanding connections allowed in the sockets
328251875Speter *                listen queue.  If this value is less than zero, the listen
329251875Speter *                queue size is set to zero.
330251875Speter */
331251875SpeterAPR_DECLARE(apr_status_t) apr_socket_listen(apr_socket_t *sock,
332251875Speter                                            apr_int32_t backlog);
333251875Speter
334251875Speter/**
335251875Speter * Accept a new connection request
336251875Speter * @param new_sock A copy of the socket that is connected to the socket that
337251875Speter *                 made the connection request.  This is the socket which should
338251875Speter *                 be used for all future communication.
339251875Speter * @param sock The socket we are listening on.
340251875Speter * @param connection_pool The pool for the new socket.
341269847Speter * @note The pool will be used by various functions that operate on the
342269847Speter *       socket. The caller must ensure that it is not used by other threads
343269847Speter *       at the same time.
344251875Speter */
345251875SpeterAPR_DECLARE(apr_status_t) apr_socket_accept(apr_socket_t **new_sock,
346251875Speter                                            apr_socket_t *sock,
347251875Speter                                            apr_pool_t *connection_pool);
348251875Speter
349251875Speter/**
350251875Speter * Issue a connection request to a socket either on the same machine
351251875Speter * or a different one.
352251875Speter * @param sock The socket we wish to use for our side of the connection
353251875Speter * @param sa The address of the machine we wish to connect to.
354251875Speter */
355251875SpeterAPR_DECLARE(apr_status_t) apr_socket_connect(apr_socket_t *sock,
356251875Speter                                             apr_sockaddr_t *sa);
357251875Speter
358251875Speter/**
359251875Speter * Determine whether the receive part of the socket has been closed by
360251875Speter * the peer (such that a subsequent call to apr_socket_read would
361251875Speter * return APR_EOF), if the socket's receive buffer is empty.  This
362251875Speter * function does not block waiting for I/O.
363251875Speter *
364251875Speter * @param sock The socket to check
365251875Speter * @param atreadeof If APR_SUCCESS is returned, *atreadeof is set to
366251875Speter *                  non-zero if a subsequent read would return APR_EOF
367251875Speter * @return an error is returned if it was not possible to determine the
368251875Speter *         status, in which case *atreadeof is not changed.
369251875Speter */
370251875SpeterAPR_DECLARE(apr_status_t) apr_socket_atreadeof(apr_socket_t *sock,
371251875Speter                                               int *atreadeof);
372251875Speter
373251875Speter/**
374251875Speter * Create apr_sockaddr_t from hostname, address family, and port.
375251875Speter * @param sa The new apr_sockaddr_t.
376251875Speter * @param hostname The hostname or numeric address string to resolve/parse, or
377251875Speter *               NULL to build an address that corresponds to 0.0.0.0 or ::
378251875Speter * @param family The address family to use, or APR_UNSPEC if the system should
379251875Speter *               decide.
380251875Speter * @param port The port number.
381251875Speter * @param flags Special processing flags:
382251875Speter * <PRE>
383251875Speter *       APR_IPV4_ADDR_OK          first query for IPv4 addresses; only look
384251875Speter *                                 for IPv6 addresses if the first query failed;
385251875Speter *                                 only valid if family is APR_UNSPEC and hostname
386251875Speter *                                 isn't NULL; mutually exclusive with
387251875Speter *                                 APR_IPV6_ADDR_OK
388251875Speter *       APR_IPV6_ADDR_OK          first query for IPv6 addresses; only look
389251875Speter *                                 for IPv4 addresses if the first query failed;
390251875Speter *                                 only valid if family is APR_UNSPEC and hostname
391251875Speter *                                 isn't NULL and APR_HAVE_IPV6; mutually exclusive
392251875Speter *                                 with APR_IPV4_ADDR_OK
393251875Speter * </PRE>
394251875Speter * @param p The pool for the apr_sockaddr_t and associated storage.
395251875Speter */
396251875SpeterAPR_DECLARE(apr_status_t) apr_sockaddr_info_get(apr_sockaddr_t **sa,
397251875Speter                                          const char *hostname,
398251875Speter                                          apr_int32_t family,
399251875Speter                                          apr_port_t port,
400251875Speter                                          apr_int32_t flags,
401251875Speter                                          apr_pool_t *p);
402251875Speter
403251875Speter/**
404251875Speter * Look up the host name from an apr_sockaddr_t.
405251875Speter * @param hostname The hostname.
406251875Speter * @param sa The apr_sockaddr_t.
407251875Speter * @param flags Special processing flags.
408269847Speter * @remark Results can vary significantly between platforms
409269847Speter * when processing wildcard socket addresses.
410251875Speter */
411251875SpeterAPR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname,
412251875Speter                                          apr_sockaddr_t *sa,
413251875Speter                                          apr_int32_t flags);
414251875Speter
415251875Speter/**
416251875Speter * Parse hostname/IP address with scope id and port.
417251875Speter *
418251875Speter * Any of the following strings are accepted:
419251875Speter *   8080                  (just the port number)
420251875Speter *   www.apache.org        (just the hostname)
421251875Speter *   www.apache.org:8080   (hostname and port number)
422251875Speter *   [fe80::1]:80          (IPv6 numeric address string only)
423251875Speter *   [fe80::1%eth0]        (IPv6 numeric address string and scope id)
424251875Speter *
425251875Speter * Invalid strings:
426251875Speter *                         (empty string)
427251875Speter *   [abc]                 (not valid IPv6 numeric address string)
428251875Speter *   abc:65536             (invalid port number)
429251875Speter *
430251875Speter * @param addr The new buffer containing just the hostname.  On output, *addr
431251875Speter *             will be NULL if no hostname/IP address was specfied.
432251875Speter * @param scope_id The new buffer containing just the scope id.  On output,
433251875Speter *                 *scope_id will be NULL if no scope id was specified.
434251875Speter * @param port The port number.  On output, *port will be 0 if no port was
435251875Speter *             specified.
436251875Speter *             ### FIXME: 0 is a legal port (per RFC 1700). this should
437251875Speter *             ### return something besides zero if the port is missing.
438251875Speter * @param str The input string to be parsed.
439251875Speter * @param p The pool from which *addr and *scope_id are allocated.
440251875Speter * @remark If scope id shouldn't be allowed, check for scope_id != NULL in
441251875Speter *         addition to checking the return code.  If addr/hostname should be
442251875Speter *         required, check for addr == NULL in addition to checking the
443251875Speter *         return code.
444251875Speter */
445251875SpeterAPR_DECLARE(apr_status_t) apr_parse_addr_port(char **addr,
446251875Speter                                              char **scope_id,
447251875Speter                                              apr_port_t *port,
448251875Speter                                              const char *str,
449251875Speter                                              apr_pool_t *p);
450251875Speter
451251875Speter/**
452251875Speter * Get name of the current machine
453251875Speter * @param buf A buffer to store the hostname in.
454251875Speter * @param len The maximum length of the hostname that can be stored in the
455251875Speter *            buffer provided.  The suggested length is APRMAXHOSTLEN + 1.
456251875Speter * @param cont The pool to use.
457251875Speter * @remark If the buffer was not large enough, an error will be returned.
458251875Speter */
459251875SpeterAPR_DECLARE(apr_status_t) apr_gethostname(char *buf, int len, apr_pool_t *cont);
460251875Speter
461251875Speter/**
462251875Speter * Return the data associated with the current socket
463251875Speter * @param data The user data associated with the socket.
464251875Speter * @param key The key to associate with the user data.
465251875Speter * @param sock The currently open socket.
466251875Speter */
467251875SpeterAPR_DECLARE(apr_status_t) apr_socket_data_get(void **data, const char *key,
468251875Speter                                              apr_socket_t *sock);
469251875Speter
470251875Speter/**
471251875Speter * Set the data associated with the current socket.
472251875Speter * @param sock The currently open socket.
473251875Speter * @param data The user data to associate with the socket.
474251875Speter * @param key The key to associate with the data.
475251875Speter * @param cleanup The cleanup to call when the socket is destroyed.
476251875Speter */
477251875SpeterAPR_DECLARE(apr_status_t) apr_socket_data_set(apr_socket_t *sock, void *data,
478251875Speter                                              const char *key,
479251875Speter                                              apr_status_t (*cleanup)(void*));
480251875Speter
481251875Speter/**
482251875Speter * Send data over a network.
483251875Speter * @param sock The socket to send the data over.
484251875Speter * @param buf The buffer which contains the data to be sent.
485251875Speter * @param len On entry, the number of bytes to send; on exit, the number
486251875Speter *            of bytes sent.
487251875Speter * @remark
488251875Speter * <PRE>
489251875Speter * This functions acts like a blocking write by default.  To change
490251875Speter * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
491251875Speter * socket option.
492251875Speter *
493251875Speter * It is possible for both bytes to be sent and an error to be returned.
494251875Speter *
495251875Speter * APR_EINTR is never returned.
496251875Speter * </PRE>
497251875Speter */
498251875SpeterAPR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf,
499251875Speter                                          apr_size_t *len);
500251875Speter
501251875Speter/**
502269847Speter * Send multiple buffers over a network.
503251875Speter * @param sock The socket to send the data over.
504251875Speter * @param vec The array of iovec structs containing the data to send
505251875Speter * @param nvec The number of iovec structs in the array
506251875Speter * @param len Receives the number of bytes actually written
507251875Speter * @remark
508251875Speter * <PRE>
509251875Speter * This functions acts like a blocking write by default.  To change
510251875Speter * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
511251875Speter * socket option.
512269847Speter * The number of bytes actually sent is stored in argument 4.
513251875Speter *
514251875Speter * It is possible for both bytes to be sent and an error to be returned.
515251875Speter *
516251875Speter * APR_EINTR is never returned.
517251875Speter * </PRE>
518251875Speter */
519251875SpeterAPR_DECLARE(apr_status_t) apr_socket_sendv(apr_socket_t *sock,
520251875Speter                                           const struct iovec *vec,
521251875Speter                                           apr_int32_t nvec, apr_size_t *len);
522251875Speter
523251875Speter/**
524251875Speter * @param sock The socket to send from
525251875Speter * @param where The apr_sockaddr_t describing where to send the data
526251875Speter * @param flags The flags to use
527251875Speter * @param buf  The data to send
528251875Speter * @param len  The length of the data to send
529251875Speter */
530251875SpeterAPR_DECLARE(apr_status_t) apr_socket_sendto(apr_socket_t *sock,
531251875Speter                                            apr_sockaddr_t *where,
532251875Speter                                            apr_int32_t flags, const char *buf,
533251875Speter                                            apr_size_t *len);
534251875Speter
535251875Speter/**
536251875Speter * Read data from a socket.  On success, the address of the peer from
537251875Speter * which the data was sent is copied into the @a from parameter, and the
538251875Speter * @a len parameter is updated to give the number of bytes written to
539251875Speter * @a buf.
540251875Speter *
541251875Speter * @param from Updated with the address from which the data was received
542251875Speter * @param sock The socket to use
543251875Speter * @param flags The flags to use
544251875Speter * @param buf  The buffer to use
545251875Speter * @param len  The length of the available buffer
546251875Speter */
547251875Speter
548251875SpeterAPR_DECLARE(apr_status_t) apr_socket_recvfrom(apr_sockaddr_t *from,
549251875Speter                                              apr_socket_t *sock,
550251875Speter                                              apr_int32_t flags, char *buf,
551251875Speter                                              apr_size_t *len);
552251875Speter
553251875Speter#if APR_HAS_SENDFILE || defined(DOXYGEN)
554251875Speter
555251875Speter/**
556251875Speter * Send a file from an open file descriptor to a socket, along with
557251875Speter * optional headers and trailers
558251875Speter * @param sock The socket to which we're writing
559251875Speter * @param file The open file from which to read
560251875Speter * @param hdtr A structure containing the headers and trailers to send
561251875Speter * @param offset Offset into the file where we should begin writing
562251875Speter * @param len (input)  - Number of bytes to send from the file
563251875Speter *            (output) - Number of bytes actually sent,
564251875Speter *                       including headers, file, and trailers
565251875Speter * @param flags APR flags that are mapped to OS specific flags
566251875Speter * @remark This functions acts like a blocking write by default.  To change
567251875Speter *         this behavior, use apr_socket_timeout_set() or the
568251875Speter *         APR_SO_NONBLOCK socket option.
569251875Speter * The number of bytes actually sent is stored in the len parameter.
570251875Speter * The offset parameter is passed by reference for no reason; its
571251875Speter * value will never be modified by the apr_socket_sendfile() function.
572251875Speter */
573251875SpeterAPR_DECLARE(apr_status_t) apr_socket_sendfile(apr_socket_t *sock,
574251875Speter                                              apr_file_t *file,
575251875Speter                                              apr_hdtr_t *hdtr,
576251875Speter                                              apr_off_t *offset,
577251875Speter                                              apr_size_t *len,
578251875Speter                                              apr_int32_t flags);
579251875Speter
580251875Speter#endif /* APR_HAS_SENDFILE */
581251875Speter
582251875Speter/**
583251875Speter * Read data from a network.
584251875Speter * @param sock The socket to read the data from.
585251875Speter * @param buf The buffer to store the data in.
586251875Speter * @param len On entry, the number of bytes to receive; on exit, the number
587251875Speter *            of bytes received.
588251875Speter * @remark
589251875Speter * <PRE>
590251875Speter * This functions acts like a blocking read by default.  To change
591251875Speter * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
592251875Speter * socket option.
593251875Speter * The number of bytes actually received is stored in argument 3.
594251875Speter *
595251875Speter * It is possible for both bytes to be received and an APR_EOF or
596251875Speter * other error to be returned.
597251875Speter *
598251875Speter * APR_EINTR is never returned.
599251875Speter * </PRE>
600251875Speter */
601251875SpeterAPR_DECLARE(apr_status_t) apr_socket_recv(apr_socket_t *sock,
602251875Speter                                   char *buf, apr_size_t *len);
603251875Speter
604251875Speter/**
605251875Speter * Setup socket options for the specified socket
606251875Speter * @param sock The socket to set up.
607251875Speter * @param opt The option we would like to configure.  One of:
608251875Speter * <PRE>
609251875Speter *            APR_SO_DEBUG      --  turn on debugging information
610251875Speter *            APR_SO_KEEPALIVE  --  keep connections active
611251875Speter *            APR_SO_LINGER     --  lingers on close if data is present
612251875Speter *            APR_SO_NONBLOCK   --  Turns blocking on/off for socket
613251875Speter *                                  When this option is enabled, use
614251875Speter *                                  the APR_STATUS_IS_EAGAIN() macro to
615251875Speter *                                  see if a send or receive function
616251875Speter *                                  could not transfer data without
617251875Speter *                                  blocking.
618251875Speter *            APR_SO_REUSEADDR  --  The rules used in validating addresses
619251875Speter *                                  supplied to bind should allow reuse
620251875Speter *                                  of local addresses.
621251875Speter *            APR_SO_SNDBUF     --  Set the SendBufferSize
622251875Speter *            APR_SO_RCVBUF     --  Set the ReceiveBufferSize
623251875Speter * </PRE>
624251875Speter * @param on Value for the option.
625251875Speter */
626251875SpeterAPR_DECLARE(apr_status_t) apr_socket_opt_set(apr_socket_t *sock,
627251875Speter                                             apr_int32_t opt, apr_int32_t on);
628251875Speter
629251875Speter/**
630251875Speter * Setup socket timeout for the specified socket
631251875Speter * @param sock The socket to set up.
632251875Speter * @param t Value for the timeout.
633251875Speter * <PRE>
634251875Speter *   t > 0  -- read and write calls return APR_TIMEUP if specified time
635251875Speter *             elapsess with no data read or written
636251875Speter *   t == 0 -- read and write calls never block
637251875Speter *   t < 0  -- read and write calls block
638251875Speter * </PRE>
639251875Speter */
640251875SpeterAPR_DECLARE(apr_status_t) apr_socket_timeout_set(apr_socket_t *sock,
641251875Speter                                                 apr_interval_time_t t);
642251875Speter
643251875Speter/**
644251875Speter * Query socket options for the specified socket
645251875Speter * @param sock The socket to query
646251875Speter * @param opt The option we would like to query.  One of:
647251875Speter * <PRE>
648251875Speter *            APR_SO_DEBUG      --  turn on debugging information
649251875Speter *            APR_SO_KEEPALIVE  --  keep connections active
650251875Speter *            APR_SO_LINGER     --  lingers on close if data is present
651251875Speter *            APR_SO_NONBLOCK   --  Turns blocking on/off for socket
652251875Speter *            APR_SO_REUSEADDR  --  The rules used in validating addresses
653251875Speter *                                  supplied to bind should allow reuse
654251875Speter *                                  of local addresses.
655251875Speter *            APR_SO_SNDBUF     --  Set the SendBufferSize
656251875Speter *            APR_SO_RCVBUF     --  Set the ReceiveBufferSize
657251875Speter *            APR_SO_DISCONNECTED -- Query the disconnected state of the socket.
658251875Speter *                                  (Currently only used on Windows)
659251875Speter * </PRE>
660251875Speter * @param on Socket option returned on the call.
661251875Speter */
662251875SpeterAPR_DECLARE(apr_status_t) apr_socket_opt_get(apr_socket_t *sock,
663251875Speter                                             apr_int32_t opt, apr_int32_t *on);
664251875Speter
665251875Speter/**
666251875Speter * Query socket timeout for the specified socket
667251875Speter * @param sock The socket to query
668251875Speter * @param t Socket timeout returned from the query.
669251875Speter */
670251875SpeterAPR_DECLARE(apr_status_t) apr_socket_timeout_get(apr_socket_t *sock,
671251875Speter                                                 apr_interval_time_t *t);
672251875Speter
673251875Speter/**
674251875Speter * Query the specified socket if at the OOB/Urgent data mark
675251875Speter * @param sock The socket to query
676251875Speter * @param atmark Is set to true if socket is at the OOB/urgent mark,
677251875Speter *               otherwise is set to false.
678251875Speter */
679251875SpeterAPR_DECLARE(apr_status_t) apr_socket_atmark(apr_socket_t *sock,
680251875Speter                                            int *atmark);
681251875Speter
682251875Speter/**
683251875Speter * Return an address associated with a socket; either the address to
684269847Speter * which the socket is bound locally or the address of the peer
685251875Speter * to which the socket is connected.
686251875Speter * @param sa The returned apr_sockaddr_t.
687251875Speter * @param which Whether to retrieve the local or remote address
688251875Speter * @param sock The socket to use
689251875Speter */
690251875SpeterAPR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa,
691251875Speter                                              apr_interface_e which,
692251875Speter                                              apr_socket_t *sock);
693251875Speter
694251875Speter/**
695251875Speter * Return the IP address (in numeric address string format) in
696251875Speter * an APR socket address.  APR will allocate storage for the IP address
697251875Speter * string from the pool of the apr_sockaddr_t.
698251875Speter * @param addr The IP address.
699251875Speter * @param sockaddr The socket address to reference.
700251875Speter */
701251875SpeterAPR_DECLARE(apr_status_t) apr_sockaddr_ip_get(char **addr,
702251875Speter                                              apr_sockaddr_t *sockaddr);
703251875Speter
704251875Speter/**
705251875Speter * Write the IP address (in numeric address string format) of the APR
706251875Speter * socket address @a sockaddr into the buffer @a buf (of size @a buflen).
707251875Speter * @param sockaddr The socket address to reference.
708251875Speter */
709251875SpeterAPR_DECLARE(apr_status_t) apr_sockaddr_ip_getbuf(char *buf, apr_size_t buflen,
710251875Speter                                                 apr_sockaddr_t *sockaddr);
711251875Speter
712251875Speter/**
713251875Speter * See if the IP addresses in two APR socket addresses are
714251875Speter * equivalent.  Appropriate logic is present for comparing
715251875Speter * IPv4-mapped IPv6 addresses with IPv4 addresses.
716251875Speter *
717251875Speter * @param addr1 One of the APR socket addresses.
718251875Speter * @param addr2 The other APR socket address.
719251875Speter * @remark The return value will be non-zero if the addresses
720251875Speter * are equivalent.
721251875Speter */
722251875SpeterAPR_DECLARE(int) apr_sockaddr_equal(const apr_sockaddr_t *addr1,
723251875Speter                                    const apr_sockaddr_t *addr2);
724251875Speter
725251875Speter/**
726269847Speter * See if the IP address in an APR socket address refers to the wildcard
727269847Speter * address for the protocol family (e.g., INADDR_ANY for IPv4).
728269847Speter *
729269847Speter * @param addr The APR socket address to examine.
730269847Speter * @remark The return value will be non-zero if the address is
731269847Speter * initialized and is the wildcard address.
732269847Speter */
733269847SpeterAPR_DECLARE(int) apr_sockaddr_is_wildcard(const apr_sockaddr_t *addr);
734269847Speter
735269847Speter/**
736251875Speter* Return the type of the socket.
737251875Speter* @param sock The socket to query.
738251875Speter* @param type The returned type (e.g., SOCK_STREAM).
739251875Speter*/
740251875SpeterAPR_DECLARE(apr_status_t) apr_socket_type_get(apr_socket_t *sock,
741251875Speter                                              int *type);
742251875Speter
743251875Speter/**
744251875Speter * Given an apr_sockaddr_t and a service name, set the port for the service
745251875Speter * @param sockaddr The apr_sockaddr_t that will have its port set
746251875Speter * @param servname The name of the service you wish to use
747251875Speter */
748251875SpeterAPR_DECLARE(apr_status_t) apr_getservbyname(apr_sockaddr_t *sockaddr,
749251875Speter                                            const char *servname);
750251875Speter/**
751251875Speter * Build an ip-subnet representation from an IP address and optional netmask or
752251875Speter * number-of-bits.
753251875Speter * @param ipsub The new ip-subnet representation
754251875Speter * @param ipstr The input IP address string
755251875Speter * @param mask_or_numbits The input netmask or number-of-bits string, or NULL
756251875Speter * @param p The pool to allocate from
757251875Speter */
758251875SpeterAPR_DECLARE(apr_status_t) apr_ipsubnet_create(apr_ipsubnet_t **ipsub,
759251875Speter                                              const char *ipstr,
760251875Speter                                              const char *mask_or_numbits,
761251875Speter                                              apr_pool_t *p);
762251875Speter
763251875Speter/**
764251875Speter * Test the IP address in an apr_sockaddr_t against a pre-built ip-subnet
765251875Speter * representation.
766251875Speter * @param ipsub The ip-subnet representation
767251875Speter * @param sa The socket address to test
768251875Speter * @return non-zero if the socket address is within the subnet, 0 otherwise
769251875Speter */
770251875SpeterAPR_DECLARE(int) apr_ipsubnet_test(apr_ipsubnet_t *ipsub, apr_sockaddr_t *sa);
771251875Speter
772251875Speter#if APR_HAS_SO_ACCEPTFILTER || defined(DOXYGEN)
773251875Speter/**
774251875Speter * Set an OS level accept filter.
775251875Speter * @param sock The socket to put the accept filter on.
776251875Speter * @param name The accept filter
777251875Speter * @param args Any extra args to the accept filter.  Passing NULL here removes
778251875Speter *             the accept filter.
779253734Speter * @bug name and args should have been declared as const char *, as they are in
780253734Speter * APR 2.0
781251875Speter */
782251875Speterapr_status_t apr_socket_accept_filter(apr_socket_t *sock, char *name,
783251875Speter                                      char *args);
784251875Speter#endif
785251875Speter
786251875Speter/**
787251875Speter * Return the protocol of the socket.
788251875Speter * @param sock The socket to query.
789251875Speter * @param protocol The returned protocol (e.g., APR_PROTO_TCP).
790251875Speter */
791251875SpeterAPR_DECLARE(apr_status_t) apr_socket_protocol_get(apr_socket_t *sock,
792251875Speter                                                  int *protocol);
793251875Speter
794251875Speter/**
795251875Speter * Get the pool used by the socket.
796251875Speter */
797251875SpeterAPR_POOL_DECLARE_ACCESSOR(socket);
798251875Speter
799251875Speter/**
800251875Speter * Set a socket to be inherited by child processes.
801251875Speter */
802251875SpeterAPR_DECLARE_INHERIT_SET(socket);
803251875Speter
804251875Speter/**
805251875Speter * Unset a socket from being inherited by child processes.
806251875Speter */
807251875SpeterAPR_DECLARE_INHERIT_UNSET(socket);
808251875Speter
809251875Speter/**
810251875Speter * @defgroup apr_mcast IP Multicast
811251875Speter * @{
812251875Speter */
813251875Speter
814251875Speter/**
815251875Speter * Join a Multicast Group
816251875Speter * @param sock The socket to join a multicast group
817251875Speter * @param join The address of the multicast group to join
818251875Speter * @param iface Address of the interface to use.  If NULL is passed, the
819251875Speter *              default multicast interface will be used. (OS Dependent)
820251875Speter * @param source Source Address to accept transmissions from (non-NULL
821251875Speter *               implies Source-Specific Multicast)
822251875Speter */
823251875SpeterAPR_DECLARE(apr_status_t) apr_mcast_join(apr_socket_t *sock,
824251875Speter                                         apr_sockaddr_t *join,
825251875Speter                                         apr_sockaddr_t *iface,
826251875Speter                                         apr_sockaddr_t *source);
827251875Speter
828251875Speter/**
829251875Speter * Leave a Multicast Group.  All arguments must be the same as
830251875Speter * apr_mcast_join.
831251875Speter * @param sock The socket to leave a multicast group
832251875Speter * @param addr The address of the multicast group to leave
833251875Speter * @param iface Address of the interface to use.  If NULL is passed, the
834251875Speter *              default multicast interface will be used. (OS Dependent)
835251875Speter * @param source Source Address to accept transmissions from (non-NULL
836251875Speter *               implies Source-Specific Multicast)
837251875Speter */
838251875SpeterAPR_DECLARE(apr_status_t) apr_mcast_leave(apr_socket_t *sock,
839251875Speter                                          apr_sockaddr_t *addr,
840251875Speter                                          apr_sockaddr_t *iface,
841251875Speter                                          apr_sockaddr_t *source);
842251875Speter
843251875Speter/**
844251875Speter * Set the Multicast Time to Live (ttl) for a multicast transmission.
845251875Speter * @param sock The socket to set the multicast ttl
846251875Speter * @param ttl Time to live to Assign. 0-255, default=1
847251875Speter * @remark If the TTL is 0, packets will only be seen by sockets on
848251875Speter * the local machine, and only when multicast loopback is enabled.
849251875Speter */
850251875SpeterAPR_DECLARE(apr_status_t) apr_mcast_hops(apr_socket_t *sock,
851251875Speter                                         apr_byte_t ttl);
852251875Speter
853251875Speter/**
854251875Speter * Toggle IP Multicast Loopback
855251875Speter * @param sock The socket to set multicast loopback
856251875Speter * @param opt 0=disable, 1=enable
857251875Speter */
858251875SpeterAPR_DECLARE(apr_status_t) apr_mcast_loopback(apr_socket_t *sock,
859251875Speter                                             apr_byte_t opt);
860251875Speter
861251875Speter
862251875Speter/**
863251875Speter * Set the Interface to be used for outgoing Multicast Transmissions.
864251875Speter * @param sock The socket to set the multicast interface on
865251875Speter * @param iface Address of the interface to use for Multicast
866251875Speter */
867251875SpeterAPR_DECLARE(apr_status_t) apr_mcast_interface(apr_socket_t *sock,
868251875Speter                                              apr_sockaddr_t *iface);
869251875Speter
870251875Speter/** @} */
871251875Speter
872251875Speter/** @} */
873251875Speter
874251875Speter#ifdef __cplusplus
875251875Speter}
876251875Speter#endif
877251875Speter
878251875Speter#endif  /* ! APR_NETWORK_IO_H */
879251875Speter
880