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"
29362181Sdim#include "apr_perms_set.h"
30251875Speter
31251875Speter#if APR_HAVE_NETINET_IN_H
32251875Speter#include <netinet/in.h>
33251875Speter#endif
34362181Sdim#if APR_HAVE_SYS_UN_H
35362181Sdim#include <sys/un.h>
36362181Sdim#endif
37251875Speter
38251875Speter#ifdef __cplusplus
39251875Speterextern "C" {
40251875Speter#endif /* __cplusplus */
41251875Speter
42251875Speter/**
43251875Speter * @defgroup apr_network_io Network Routines
44251875Speter * @ingroup APR
45251875Speter * @{
46251875Speter */
47251875Speter
48251875Speter#ifndef APR_MAX_SECS_TO_LINGER
49251875Speter/** Maximum seconds to linger */
50251875Speter#define APR_MAX_SECS_TO_LINGER 30
51251875Speter#endif
52251875Speter
53251875Speter#ifndef APRMAXHOSTLEN
54251875Speter/** Maximum hostname length */
55251875Speter#define APRMAXHOSTLEN 256
56251875Speter#endif
57251875Speter
58251875Speter#ifndef APR_ANYADDR
59251875Speter/** Default 'any' address */
60251875Speter#define APR_ANYADDR "0.0.0.0"
61251875Speter#endif
62251875Speter
63251875Speter/**
64251875Speter * @defgroup apr_sockopt Socket option definitions
65251875Speter * @{
66251875Speter */
67251875Speter#define APR_SO_LINGER        1    /**< Linger */
68251875Speter#define APR_SO_KEEPALIVE     2    /**< Keepalive */
69251875Speter#define APR_SO_DEBUG         4    /**< Debug */
70251875Speter#define APR_SO_NONBLOCK      8    /**< Non-blocking IO */
71251875Speter#define APR_SO_REUSEADDR     16   /**< Reuse addresses */
72251875Speter#define APR_SO_SNDBUF        64   /**< Send buffer */
73251875Speter#define APR_SO_RCVBUF        128  /**< Receive buffer */
74251875Speter#define APR_SO_DISCONNECTED  256  /**< Disconnected */
75251875Speter#define APR_TCP_NODELAY      512  /**< For SCTP sockets, this is mapped
76251875Speter                                   * to STCP_NODELAY internally.
77251875Speter                                   */
78251875Speter#define APR_TCP_NOPUSH       1024 /**< No push */
79251875Speter#define APR_RESET_NODELAY    2048 /**< This flag is ONLY set internally
80251875Speter                                   * when we set APR_TCP_NOPUSH with
81251875Speter                                   * APR_TCP_NODELAY set to tell us that
82251875Speter                                   * APR_TCP_NODELAY should be turned on
83251875Speter                                   * again when NOPUSH is turned off
84251875Speter                                   */
85251875Speter#define APR_INCOMPLETE_READ 4096  /**< Set on non-blocking sockets
86251875Speter				   * (timeout != 0) on which the
87251875Speter				   * previous read() did not fill a buffer
88251875Speter				   * completely.  the next apr_socket_recv()
89251875Speter                                   * will first call select()/poll() rather than
90251875Speter				   * going straight into read().  (Can also
91251875Speter				   * be set by an application to force a
92251875Speter				   * select()/poll() call before the next
93251875Speter				   * read, in cases where the app expects
94251875Speter				   * that an immediate read would fail.)
95251875Speter				   */
96251875Speter#define APR_INCOMPLETE_WRITE 8192 /**< like APR_INCOMPLETE_READ, but for write
97251875Speter                                   * @see APR_INCOMPLETE_READ
98251875Speter                                   */
99251875Speter#define APR_IPV6_V6ONLY     16384 /**< Don't accept IPv4 connections on an
100251875Speter                                   * IPv6 listening socket.
101251875Speter                                   */
102251875Speter#define APR_TCP_DEFER_ACCEPT 32768 /**< Delay accepting of new connections
103251875Speter                                    * until data is available.
104251875Speter                                    * @see apr_socket_accept_filter
105251875Speter                                    */
106266735Speter#define APR_SO_BROADCAST     65536 /**< Allow broadcast
107266735Speter                                    */
108362181Sdim#define APR_SO_FREEBIND     131072 /**< Allow binding to addresses not owned
109362181Sdim                                    * by any interface
110362181Sdim                                    */
111251875Speter
112251875Speter/** @} */
113251875Speter
114251875Speter/** Define what type of socket shutdown should occur. */
115251875Spetertypedef enum {
116251875Speter    APR_SHUTDOWN_READ,          /**< no longer allow read request */
117251875Speter    APR_SHUTDOWN_WRITE,         /**< no longer allow write requests */
118251875Speter    APR_SHUTDOWN_READWRITE      /**< no longer allow read or write requests */
119251875Speter} apr_shutdown_how_e;
120251875Speter
121251875Speter#define APR_IPV4_ADDR_OK  0x01  /**< @see apr_sockaddr_info_get() */
122251875Speter#define APR_IPV6_ADDR_OK  0x02  /**< @see apr_sockaddr_info_get() */
123251875Speter
124251875Speter#if (!APR_HAVE_IN_ADDR)
125251875Speter/**
126251875Speter * We need to make sure we always have an in_addr type, so APR will just
127251875Speter * define it ourselves, if the platform doesn't provide it.
128251875Speter */
129251875Speterstruct in_addr {
130251875Speter    apr_uint32_t  s_addr; /**< storage to hold the IP# */
131251875Speter};
132251875Speter#endif
133251875Speter
134251875Speter/** @def APR_INADDR_NONE
135251875Speter * Not all platforms have a real INADDR_NONE.  This macro replaces
136251875Speter * INADDR_NONE on all platforms.
137251875Speter */
138251875Speter#ifdef INADDR_NONE
139251875Speter#define APR_INADDR_NONE INADDR_NONE
140251875Speter#else
141251875Speter#define APR_INADDR_NONE ((unsigned int) 0xffffffff)
142251875Speter#endif
143251875Speter
144251875Speter/**
145251875Speter * @def APR_INET
146251875Speter * Not all platforms have these defined, so we'll define them here
147251875Speter * The default values come from FreeBSD 4.1.1
148251875Speter */
149251875Speter#define APR_INET     AF_INET
150251875Speter/** @def APR_UNSPEC
151251875Speter * Let the system decide which address family to use
152251875Speter */
153251875Speter#ifdef AF_UNSPEC
154251875Speter#define APR_UNSPEC   AF_UNSPEC
155251875Speter#else
156251875Speter#define APR_UNSPEC   0
157251875Speter#endif
158251875Speter#if APR_HAVE_IPV6
159251875Speter/** @def APR_INET6
160251875Speter* IPv6 Address Family. Not all platforms may have this defined.
161251875Speter*/
162251875Speter
163251875Speter#define APR_INET6    AF_INET6
164251875Speter#endif
165251875Speter
166362181Sdim#if APR_HAVE_SOCKADDR_UN
167362181Sdim#if defined (AF_UNIX)
168362181Sdim#define APR_UNIX    AF_UNIX
169362181Sdim#elif defined(AF_LOCAL)
170362181Sdim#define APR_UNIX    AF_LOCAL
171362181Sdim#else
172362181Sdim#error "Neither AF_UNIX nor AF_LOCAL is defined"
173362181Sdim#endif
174362181Sdim#else /* !APR_HAVE_SOCKADDR_UN */
175362181Sdim#if defined (AF_UNIX)
176362181Sdim#define APR_UNIX    AF_UNIX
177362181Sdim#elif defined(AF_LOCAL)
178362181Sdim#define APR_UNIX    AF_LOCAL
179362181Sdim#else
180362181Sdim/* TODO: Use a smarter way to detect unique APR_UNIX value */
181362181Sdim#define APR_UNIX    1234
182362181Sdim#endif
183362181Sdim#endif
184362181Sdim
185251875Speter/**
186251875Speter * @defgroup IP_Proto IP Protocol Definitions for use when creating sockets
187251875Speter * @{
188251875Speter */
189251875Speter#define APR_PROTO_TCP       6   /**< TCP  */
190251875Speter#define APR_PROTO_UDP      17   /**< UDP  */
191251875Speter#define APR_PROTO_SCTP    132   /**< SCTP */
192251875Speter/** @} */
193251875Speter
194251875Speter/**
195251875Speter * Enum used to denote either the local and remote endpoint of a
196251875Speter * connection.
197251875Speter */
198251875Spetertypedef enum {
199251875Speter    APR_LOCAL,   /**< Socket information for local end of connection */
200251875Speter    APR_REMOTE   /**< Socket information for remote end of connection */
201251875Speter} apr_interface_e;
202251875Speter
203251875Speter/**
204251875Speter * The specific declaration of inet_addr's ... some platforms fall back
205251875Speter * inet_network (this is not good, but necessary)
206251875Speter */
207251875Speter
208251875Speter#if APR_HAVE_INET_ADDR
209251875Speter#define apr_inet_addr    inet_addr
210251875Speter#elif APR_HAVE_INET_NETWORK        /* only DGUX, as far as I know */
211251875Speter/**
212251875Speter * @warning
213251875Speter * not generally safe... inet_network() and inet_addr() perform
214251875Speter * different functions */
215251875Speter#define apr_inet_addr    inet_network
216251875Speter#endif
217251875Speter
218251875Speter/** A structure to represent sockets */
219251875Spetertypedef struct apr_socket_t     apr_socket_t;
220251875Speter/**
221251875Speter * A structure to encapsulate headers and trailers for apr_socket_sendfile
222251875Speter */
223251875Spetertypedef struct apr_hdtr_t       apr_hdtr_t;
224251875Speter/** A structure to represent in_addr */
225251875Spetertypedef struct in_addr          apr_in_addr_t;
226251875Speter/** A structure to represent an IP subnet */
227251875Spetertypedef struct apr_ipsubnet_t apr_ipsubnet_t;
228251875Speter
229251875Speter/** @remark use apr_uint16_t just in case some system has a short that isn't 16 bits... */
230251875Spetertypedef apr_uint16_t            apr_port_t;
231251875Speter
232251875Speter/** @remark It's defined here as I think it should all be platform safe...
233251875Speter * @see apr_sockaddr_t
234251875Speter */
235251875Spetertypedef struct apr_sockaddr_t apr_sockaddr_t;
236251875Speter/**
237251875Speter * APRs socket address type, used to ensure protocol independence
238251875Speter */
239251875Speterstruct apr_sockaddr_t {
240251875Speter    /** The pool to use... */
241251875Speter    apr_pool_t *pool;
242251875Speter    /** The hostname */
243251875Speter    char *hostname;
244251875Speter    /** Either a string of the port number or the service name for the port */
245251875Speter    char *servname;
246251875Speter    /** The numeric port */
247251875Speter    apr_port_t port;
248251875Speter    /** The family */
249251875Speter    apr_int32_t family;
250251875Speter    /** How big is the sockaddr we're using? */
251251875Speter    apr_socklen_t salen;
252251875Speter    /** How big is the ip address structure we're using? */
253251875Speter    int ipaddr_len;
254251875Speter    /** How big should the address buffer be?  16 for v4 or 46 for v6
255251875Speter     *  used in inet_ntop... */
256251875Speter    int addr_str_len;
257251875Speter    /** This points to the IP address structure within the appropriate
258251875Speter     *  sockaddr structure.  */
259251875Speter    void *ipaddr_ptr;
260251875Speter    /** If multiple addresses were found by apr_sockaddr_info_get(), this
261251875Speter     *  points to a representation of the next address. */
262251875Speter    apr_sockaddr_t *next;
263251875Speter    /** Union of either IPv4 or IPv6 sockaddr. */
264251875Speter    union {
265251875Speter        /** IPv4 sockaddr structure */
266251875Speter        struct sockaddr_in sin;
267251875Speter#if APR_HAVE_IPV6
268251875Speter        /** IPv6 sockaddr structure */
269251875Speter        struct sockaddr_in6 sin6;
270251875Speter#endif
271251875Speter#if APR_HAVE_SA_STORAGE
272251875Speter        /** Placeholder to ensure that the size of this union is not
273251875Speter         * dependent on whether APR_HAVE_IPV6 is defined. */
274251875Speter        struct sockaddr_storage sas;
275251875Speter#endif
276362181Sdim#if APR_HAVE_SOCKADDR_UN
277362181Sdim        /** Unix domain socket sockaddr structure */
278362181Sdim        struct sockaddr_un unx;
279362181Sdim#endif
280251875Speter    } sa;
281251875Speter};
282251875Speter
283251875Speter#if APR_HAS_SENDFILE
284251875Speter/**
285251875Speter * Support reusing the socket on platforms which support it (from disconnect,
286251875Speter * specifically Win32.
287251875Speter * @remark Optional flag passed into apr_socket_sendfile()
288251875Speter */
289251875Speter#define APR_SENDFILE_DISCONNECT_SOCKET      1
290251875Speter#endif
291251875Speter
292251875Speter/** A structure to encapsulate headers and trailers for apr_socket_sendfile */
293251875Speterstruct apr_hdtr_t {
294251875Speter    /** An iovec to store the headers sent before the file. */
295251875Speter    struct iovec* headers;
296251875Speter    /** number of headers in the iovec */
297251875Speter    int numheaders;
298251875Speter    /** An iovec to store the trailers sent after the file. */
299251875Speter    struct iovec* trailers;
300251875Speter    /** number of trailers in the iovec */
301251875Speter    int numtrailers;
302251875Speter};
303251875Speter
304251875Speter/* function definitions */
305251875Speter
306251875Speter/**
307251875Speter * Create a socket.
308251875Speter * @param new_sock The new socket that has been set up.
309251875Speter * @param family The address family of the socket (e.g., APR_INET).
310251875Speter * @param type The type of the socket (e.g., SOCK_STREAM).
311251875Speter * @param protocol The protocol of the socket (e.g., APR_PROTO_TCP).
312251875Speter * @param cont The pool for the apr_socket_t and associated storage.
313266735Speter * @note The pool will be used by various functions that operate on the
314266735Speter *       socket. The caller must ensure that it is not used by other threads
315266735Speter *       at the same time.
316251875Speter */
317251875SpeterAPR_DECLARE(apr_status_t) apr_socket_create(apr_socket_t **new_sock,
318251875Speter                                            int family, int type,
319251875Speter                                            int protocol,
320251875Speter                                            apr_pool_t *cont);
321251875Speter
322251875Speter/**
323251875Speter * Shutdown either reading, writing, or both sides of a socket.
324251875Speter * @param thesocket The socket to close
325251875Speter * @param how How to shutdown the socket.  One of:
326251875Speter * <PRE>
327251875Speter *            APR_SHUTDOWN_READ         no longer allow read requests
328251875Speter *            APR_SHUTDOWN_WRITE        no longer allow write requests
329251875Speter *            APR_SHUTDOWN_READWRITE    no longer allow read or write requests
330251875Speter * </PRE>
331251875Speter * @see apr_shutdown_how_e
332251875Speter * @remark This does not actually close the socket descriptor, it just
333251875Speter *      controls which calls are still valid on the socket.
334251875Speter */
335251875SpeterAPR_DECLARE(apr_status_t) apr_socket_shutdown(apr_socket_t *thesocket,
336251875Speter                                              apr_shutdown_how_e how);
337251875Speter
338251875Speter/**
339251875Speter * Close a socket.
340251875Speter * @param thesocket The socket to close
341251875Speter */
342251875SpeterAPR_DECLARE(apr_status_t) apr_socket_close(apr_socket_t *thesocket);
343251875Speter
344251875Speter/**
345251875Speter * Bind the socket to its associated port
346251875Speter * @param sock The socket to bind
347251875Speter * @param sa The socket address to bind to
348251875Speter * @remark This may be where we will find out if there is any other process
349251875Speter *      using the selected port.
350251875Speter */
351251875SpeterAPR_DECLARE(apr_status_t) apr_socket_bind(apr_socket_t *sock,
352251875Speter                                          apr_sockaddr_t *sa);
353251875Speter
354251875Speter/**
355251875Speter * Listen to a bound socket for connections.
356251875Speter * @param sock The socket to listen on
357251875Speter * @param backlog The number of outstanding connections allowed in the sockets
358251875Speter *                listen queue.  If this value is less than zero, the listen
359251875Speter *                queue size is set to zero.
360251875Speter */
361251875SpeterAPR_DECLARE(apr_status_t) apr_socket_listen(apr_socket_t *sock,
362251875Speter                                            apr_int32_t backlog);
363251875Speter
364251875Speter/**
365251875Speter * Accept a new connection request
366251875Speter * @param new_sock A copy of the socket that is connected to the socket that
367251875Speter *                 made the connection request.  This is the socket which should
368251875Speter *                 be used for all future communication.
369251875Speter * @param sock The socket we are listening on.
370251875Speter * @param connection_pool The pool for the new socket.
371266735Speter * @note The pool will be used by various functions that operate on the
372266735Speter *       socket. The caller must ensure that it is not used by other threads
373266735Speter *       at the same time.
374251875Speter */
375251875SpeterAPR_DECLARE(apr_status_t) apr_socket_accept(apr_socket_t **new_sock,
376251875Speter                                            apr_socket_t *sock,
377251875Speter                                            apr_pool_t *connection_pool);
378251875Speter
379251875Speter/**
380251875Speter * Issue a connection request to a socket either on the same machine
381251875Speter * or a different one.
382251875Speter * @param sock The socket we wish to use for our side of the connection
383251875Speter * @param sa The address of the machine we wish to connect to.
384251875Speter */
385251875SpeterAPR_DECLARE(apr_status_t) apr_socket_connect(apr_socket_t *sock,
386251875Speter                                             apr_sockaddr_t *sa);
387251875Speter
388251875Speter/**
389251875Speter * Determine whether the receive part of the socket has been closed by
390251875Speter * the peer (such that a subsequent call to apr_socket_read would
391251875Speter * return APR_EOF), if the socket's receive buffer is empty.  This
392251875Speter * function does not block waiting for I/O.
393251875Speter *
394251875Speter * @param sock The socket to check
395251875Speter * @param atreadeof If APR_SUCCESS is returned, *atreadeof is set to
396251875Speter *                  non-zero if a subsequent read would return APR_EOF
397251875Speter * @return an error is returned if it was not possible to determine the
398251875Speter *         status, in which case *atreadeof is not changed.
399251875Speter */
400251875SpeterAPR_DECLARE(apr_status_t) apr_socket_atreadeof(apr_socket_t *sock,
401251875Speter                                               int *atreadeof);
402251875Speter
403251875Speter/**
404251875Speter * Create apr_sockaddr_t from hostname, address family, and port.
405251875Speter * @param sa The new apr_sockaddr_t.
406251875Speter * @param hostname The hostname or numeric address string to resolve/parse, or
407251875Speter *               NULL to build an address that corresponds to 0.0.0.0 or ::
408362181Sdim *               or in case of APR_UNIX family it is absolute socket filename.
409251875Speter * @param family The address family to use, or APR_UNSPEC if the system should
410251875Speter *               decide.
411251875Speter * @param port The port number.
412251875Speter * @param flags Special processing flags:
413251875Speter * <PRE>
414251875Speter *       APR_IPV4_ADDR_OK          first query for IPv4 addresses; only look
415251875Speter *                                 for IPv6 addresses if the first query failed;
416251875Speter *                                 only valid if family is APR_UNSPEC and hostname
417251875Speter *                                 isn't NULL; mutually exclusive with
418251875Speter *                                 APR_IPV6_ADDR_OK
419251875Speter *       APR_IPV6_ADDR_OK          first query for IPv6 addresses; only look
420251875Speter *                                 for IPv4 addresses if the first query failed;
421251875Speter *                                 only valid if family is APR_UNSPEC and hostname
422251875Speter *                                 isn't NULL and APR_HAVE_IPV6; mutually exclusive
423251875Speter *                                 with APR_IPV4_ADDR_OK
424251875Speter * </PRE>
425251875Speter * @param p The pool for the apr_sockaddr_t and associated storage.
426251875Speter */
427251875SpeterAPR_DECLARE(apr_status_t) apr_sockaddr_info_get(apr_sockaddr_t **sa,
428251875Speter                                          const char *hostname,
429251875Speter                                          apr_int32_t family,
430251875Speter                                          apr_port_t port,
431251875Speter                                          apr_int32_t flags,
432251875Speter                                          apr_pool_t *p);
433251875Speter
434251875Speter/**
435362181Sdim * Copy apr_sockaddr_t src to dst on pool p.
436362181Sdim * @param dst The destination apr_sockaddr_t.
437362181Sdim * @param src The source apr_sockaddr_t.
438362181Sdim * @param p The pool for the apr_sockaddr_t and associated storage.
439362181Sdim */
440362181SdimAPR_DECLARE(apr_status_t) apr_sockaddr_info_copy(apr_sockaddr_t **dst,
441362181Sdim                                                 const apr_sockaddr_t *src,
442362181Sdim                                                 apr_pool_t *p);
443362181Sdim
444362181Sdim/* Set the zone of an IPv6 link-local address object.
445362181Sdim * @param sa Socket address object
446362181Sdim * @param zone_id Zone ID (textual "eth0" or numeric "3").
447362181Sdim * @return Returns APR_EBADIP for non-IPv6 socket or an IPv6 address
448362181Sdim * which isn't link-local.
449362181Sdim */
450362181SdimAPR_DECLARE(apr_status_t) apr_sockaddr_zone_set(apr_sockaddr_t *sa,
451362181Sdim                                                const char *zone_id);
452362181Sdim
453362181Sdim
454362181Sdim/* Retrieve the zone of an IPv6 link-local address object.
455362181Sdim * @param sa Socket address object
456362181Sdim * @param name If non-NULL, set to the textual representation of the zone id
457362181Sdim * @param id If non-NULL, set to the integer zone id
458362181Sdim * @param p Pool from which *name is allocated if used.
459362181Sdim * @return Returns APR_EBADIP for non-IPv6 socket or socket without any zone id
460362181Sdim * set, or other error if the interface could not be mapped to a name.
461362181Sdim * @remark Both name and id may be NULL, neither are modified if
462362181Sdim * non-NULL in error cases.
463362181Sdim */
464362181SdimAPR_DECLARE(apr_status_t) apr_sockaddr_zone_get(const apr_sockaddr_t *sa,
465362181Sdim                                                const char **name,
466362181Sdim                                                apr_uint32_t *id,
467362181Sdim                                                apr_pool_t *p);
468362181Sdim
469362181Sdim/**
470251875Speter * Look up the host name from an apr_sockaddr_t.
471251875Speter * @param hostname The hostname.
472251875Speter * @param sa The apr_sockaddr_t.
473251875Speter * @param flags Special processing flags.
474266735Speter * @remark Results can vary significantly between platforms
475266735Speter * when processing wildcard socket addresses.
476251875Speter */
477251875SpeterAPR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname,
478251875Speter                                          apr_sockaddr_t *sa,
479251875Speter                                          apr_int32_t flags);
480251875Speter
481251875Speter/**
482251875Speter * Parse hostname/IP address with scope id and port.
483251875Speter *
484251875Speter * Any of the following strings are accepted:
485251875Speter *   8080                  (just the port number)
486251875Speter *   www.apache.org        (just the hostname)
487251875Speter *   www.apache.org:8080   (hostname and port number)
488251875Speter *   [fe80::1]:80          (IPv6 numeric address string only)
489251875Speter *   [fe80::1%eth0]        (IPv6 numeric address string and scope id)
490251875Speter *
491251875Speter * Invalid strings:
492251875Speter *                         (empty string)
493251875Speter *   [abc]                 (not valid IPv6 numeric address string)
494251875Speter *   abc:65536             (invalid port number)
495251875Speter *
496251875Speter * @param addr The new buffer containing just the hostname.  On output, *addr
497251875Speter *             will be NULL if no hostname/IP address was specfied.
498251875Speter * @param scope_id The new buffer containing just the scope id.  On output,
499251875Speter *                 *scope_id will be NULL if no scope id was specified.
500251875Speter * @param port The port number.  On output, *port will be 0 if no port was
501251875Speter *             specified.
502251875Speter *             ### FIXME: 0 is a legal port (per RFC 1700). this should
503251875Speter *             ### return something besides zero if the port is missing.
504251875Speter * @param str The input string to be parsed.
505251875Speter * @param p The pool from which *addr and *scope_id are allocated.
506251875Speter * @remark If scope id shouldn't be allowed, check for scope_id != NULL in
507251875Speter *         addition to checking the return code.  If addr/hostname should be
508251875Speter *         required, check for addr == NULL in addition to checking the
509251875Speter *         return code.
510251875Speter */
511251875SpeterAPR_DECLARE(apr_status_t) apr_parse_addr_port(char **addr,
512251875Speter                                              char **scope_id,
513251875Speter                                              apr_port_t *port,
514251875Speter                                              const char *str,
515251875Speter                                              apr_pool_t *p);
516251875Speter
517251875Speter/**
518251875Speter * Get name of the current machine
519251875Speter * @param buf A buffer to store the hostname in.
520251875Speter * @param len The maximum length of the hostname that can be stored in the
521251875Speter *            buffer provided.  The suggested length is APRMAXHOSTLEN + 1.
522251875Speter * @param cont The pool to use.
523251875Speter * @remark If the buffer was not large enough, an error will be returned.
524251875Speter */
525251875SpeterAPR_DECLARE(apr_status_t) apr_gethostname(char *buf, int len, apr_pool_t *cont);
526251875Speter
527251875Speter/**
528251875Speter * Return the data associated with the current socket
529251875Speter * @param data The user data associated with the socket.
530251875Speter * @param key The key to associate with the user data.
531251875Speter * @param sock The currently open socket.
532251875Speter */
533251875SpeterAPR_DECLARE(apr_status_t) apr_socket_data_get(void **data, const char *key,
534251875Speter                                              apr_socket_t *sock);
535251875Speter
536251875Speter/**
537251875Speter * Set the data associated with the current socket.
538251875Speter * @param sock The currently open socket.
539251875Speter * @param data The user data to associate with the socket.
540251875Speter * @param key The key to associate with the data.
541251875Speter * @param cleanup The cleanup to call when the socket is destroyed.
542251875Speter */
543251875SpeterAPR_DECLARE(apr_status_t) apr_socket_data_set(apr_socket_t *sock, void *data,
544251875Speter                                              const char *key,
545251875Speter                                              apr_status_t (*cleanup)(void*));
546251875Speter
547251875Speter/**
548251875Speter * Send data over a network.
549251875Speter * @param sock The socket to send the data over.
550251875Speter * @param buf The buffer which contains the data to be sent.
551251875Speter * @param len On entry, the number of bytes to send; on exit, the number
552251875Speter *            of bytes sent.
553251875Speter * @remark
554251875Speter * <PRE>
555251875Speter * This functions acts like a blocking write by default.  To change
556251875Speter * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
557251875Speter * socket option.
558251875Speter *
559251875Speter * It is possible for both bytes to be sent and an error to be returned.
560251875Speter *
561251875Speter * APR_EINTR is never returned.
562251875Speter * </PRE>
563251875Speter */
564251875SpeterAPR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf,
565251875Speter                                          apr_size_t *len);
566251875Speter
567251875Speter/**
568266735Speter * Send multiple buffers over a network.
569251875Speter * @param sock The socket to send the data over.
570251875Speter * @param vec The array of iovec structs containing the data to send
571251875Speter * @param nvec The number of iovec structs in the array
572251875Speter * @param len Receives the number of bytes actually written
573251875Speter * @remark
574251875Speter * <PRE>
575251875Speter * This functions acts like a blocking write by default.  To change
576251875Speter * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
577251875Speter * socket option.
578266735Speter * The number of bytes actually sent is stored in argument 4.
579251875Speter *
580251875Speter * It is possible for both bytes to be sent and an error to be returned.
581251875Speter *
582251875Speter * APR_EINTR is never returned.
583251875Speter * </PRE>
584251875Speter */
585251875SpeterAPR_DECLARE(apr_status_t) apr_socket_sendv(apr_socket_t *sock,
586251875Speter                                           const struct iovec *vec,
587251875Speter                                           apr_int32_t nvec, apr_size_t *len);
588251875Speter
589251875Speter/**
590251875Speter * @param sock The socket to send from
591251875Speter * @param where The apr_sockaddr_t describing where to send the data
592251875Speter * @param flags The flags to use
593251875Speter * @param buf  The data to send
594251875Speter * @param len  The length of the data to send
595251875Speter */
596251875SpeterAPR_DECLARE(apr_status_t) apr_socket_sendto(apr_socket_t *sock,
597251875Speter                                            apr_sockaddr_t *where,
598251875Speter                                            apr_int32_t flags, const char *buf,
599251875Speter                                            apr_size_t *len);
600251875Speter
601251875Speter/**
602251875Speter * Read data from a socket.  On success, the address of the peer from
603251875Speter * which the data was sent is copied into the @a from parameter, and the
604251875Speter * @a len parameter is updated to give the number of bytes written to
605251875Speter * @a buf.
606251875Speter *
607251875Speter * @param from Updated with the address from which the data was received
608251875Speter * @param sock The socket to use
609251875Speter * @param flags The flags to use
610251875Speter * @param buf  The buffer to use
611251875Speter * @param len  The length of the available buffer
612251875Speter */
613251875Speter
614251875SpeterAPR_DECLARE(apr_status_t) apr_socket_recvfrom(apr_sockaddr_t *from,
615251875Speter                                              apr_socket_t *sock,
616251875Speter                                              apr_int32_t flags, char *buf,
617251875Speter                                              apr_size_t *len);
618251875Speter
619251875Speter#if APR_HAS_SENDFILE || defined(DOXYGEN)
620251875Speter
621251875Speter/**
622251875Speter * Send a file from an open file descriptor to a socket, along with
623251875Speter * optional headers and trailers
624251875Speter * @param sock The socket to which we're writing
625251875Speter * @param file The open file from which to read
626251875Speter * @param hdtr A structure containing the headers and trailers to send
627251875Speter * @param offset Offset into the file where we should begin writing
628251875Speter * @param len (input)  - Number of bytes to send from the file
629251875Speter *            (output) - Number of bytes actually sent,
630251875Speter *                       including headers, file, and trailers
631251875Speter * @param flags APR flags that are mapped to OS specific flags
632251875Speter * @remark This functions acts like a blocking write by default.  To change
633251875Speter *         this behavior, use apr_socket_timeout_set() or the
634251875Speter *         APR_SO_NONBLOCK socket option.
635251875Speter * The number of bytes actually sent is stored in the len parameter.
636251875Speter * The offset parameter is passed by reference for no reason; its
637251875Speter * value will never be modified by the apr_socket_sendfile() function.
638251875Speter */
639251875SpeterAPR_DECLARE(apr_status_t) apr_socket_sendfile(apr_socket_t *sock,
640251875Speter                                              apr_file_t *file,
641251875Speter                                              apr_hdtr_t *hdtr,
642251875Speter                                              apr_off_t *offset,
643251875Speter                                              apr_size_t *len,
644251875Speter                                              apr_int32_t flags);
645251875Speter
646251875Speter#endif /* APR_HAS_SENDFILE */
647251875Speter
648251875Speter/**
649251875Speter * Read data from a network.
650251875Speter * @param sock The socket to read the data from.
651251875Speter * @param buf The buffer to store the data in.
652251875Speter * @param len On entry, the number of bytes to receive; on exit, the number
653251875Speter *            of bytes received.
654251875Speter * @remark
655251875Speter * <PRE>
656251875Speter * This functions acts like a blocking read by default.  To change
657251875Speter * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
658251875Speter * socket option.
659251875Speter * The number of bytes actually received is stored in argument 3.
660251875Speter *
661251875Speter * It is possible for both bytes to be received and an APR_EOF or
662251875Speter * other error to be returned.
663251875Speter *
664251875Speter * APR_EINTR is never returned.
665251875Speter * </PRE>
666251875Speter */
667251875SpeterAPR_DECLARE(apr_status_t) apr_socket_recv(apr_socket_t *sock,
668251875Speter                                   char *buf, apr_size_t *len);
669251875Speter
670251875Speter/**
671251875Speter * Setup socket options for the specified socket
672251875Speter * @param sock The socket to set up.
673251875Speter * @param opt The option we would like to configure.  One of:
674251875Speter * <PRE>
675251875Speter *            APR_SO_DEBUG      --  turn on debugging information
676251875Speter *            APR_SO_KEEPALIVE  --  keep connections active
677251875Speter *            APR_SO_LINGER     --  lingers on close if data is present
678251875Speter *            APR_SO_NONBLOCK   --  Turns blocking on/off for socket
679251875Speter *                                  When this option is enabled, use
680251875Speter *                                  the APR_STATUS_IS_EAGAIN() macro to
681251875Speter *                                  see if a send or receive function
682251875Speter *                                  could not transfer data without
683251875Speter *                                  blocking.
684251875Speter *            APR_SO_REUSEADDR  --  The rules used in validating addresses
685251875Speter *                                  supplied to bind should allow reuse
686251875Speter *                                  of local addresses.
687251875Speter *            APR_SO_SNDBUF     --  Set the SendBufferSize
688251875Speter *            APR_SO_RCVBUF     --  Set the ReceiveBufferSize
689362181Sdim *            APR_SO_FREEBIND   --  Allow binding to non-local IP address.
690251875Speter * </PRE>
691251875Speter * @param on Value for the option.
692251875Speter */
693251875SpeterAPR_DECLARE(apr_status_t) apr_socket_opt_set(apr_socket_t *sock,
694251875Speter                                             apr_int32_t opt, apr_int32_t on);
695251875Speter
696251875Speter/**
697251875Speter * Setup socket timeout for the specified socket
698251875Speter * @param sock The socket to set up.
699251875Speter * @param t Value for the timeout.
700251875Speter * <PRE>
701251875Speter *   t > 0  -- read and write calls return APR_TIMEUP if specified time
702251875Speter *             elapsess with no data read or written
703251875Speter *   t == 0 -- read and write calls never block
704251875Speter *   t < 0  -- read and write calls block
705251875Speter * </PRE>
706251875Speter */
707251875SpeterAPR_DECLARE(apr_status_t) apr_socket_timeout_set(apr_socket_t *sock,
708251875Speter                                                 apr_interval_time_t t);
709251875Speter
710251875Speter/**
711251875Speter * Query socket options for the specified socket
712251875Speter * @param sock The socket to query
713251875Speter * @param opt The option we would like to query.  One of:
714251875Speter * <PRE>
715251875Speter *            APR_SO_DEBUG      --  turn on debugging information
716251875Speter *            APR_SO_KEEPALIVE  --  keep connections active
717251875Speter *            APR_SO_LINGER     --  lingers on close if data is present
718251875Speter *            APR_SO_NONBLOCK   --  Turns blocking on/off for socket
719251875Speter *            APR_SO_REUSEADDR  --  The rules used in validating addresses
720251875Speter *                                  supplied to bind should allow reuse
721251875Speter *                                  of local addresses.
722251875Speter *            APR_SO_SNDBUF     --  Set the SendBufferSize
723251875Speter *            APR_SO_RCVBUF     --  Set the ReceiveBufferSize
724251875Speter *            APR_SO_DISCONNECTED -- Query the disconnected state of the socket.
725251875Speter *                                  (Currently only used on Windows)
726251875Speter * </PRE>
727251875Speter * @param on Socket option returned on the call.
728251875Speter */
729251875SpeterAPR_DECLARE(apr_status_t) apr_socket_opt_get(apr_socket_t *sock,
730251875Speter                                             apr_int32_t opt, apr_int32_t *on);
731251875Speter
732251875Speter/**
733251875Speter * Query socket timeout for the specified socket
734251875Speter * @param sock The socket to query
735251875Speter * @param t Socket timeout returned from the query.
736251875Speter */
737251875SpeterAPR_DECLARE(apr_status_t) apr_socket_timeout_get(apr_socket_t *sock,
738251875Speter                                                 apr_interval_time_t *t);
739251875Speter
740251875Speter/**
741251875Speter * Query the specified socket if at the OOB/Urgent data mark
742251875Speter * @param sock The socket to query
743251875Speter * @param atmark Is set to true if socket is at the OOB/urgent mark,
744251875Speter *               otherwise is set to false.
745251875Speter */
746251875SpeterAPR_DECLARE(apr_status_t) apr_socket_atmark(apr_socket_t *sock,
747251875Speter                                            int *atmark);
748251875Speter
749251875Speter/**
750251875Speter * Return an address associated with a socket; either the address to
751266735Speter * which the socket is bound locally or the address of the peer
752251875Speter * to which the socket is connected.
753251875Speter * @param sa The returned apr_sockaddr_t.
754251875Speter * @param which Whether to retrieve the local or remote address
755251875Speter * @param sock The socket to use
756251875Speter */
757251875SpeterAPR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa,
758251875Speter                                              apr_interface_e which,
759251875Speter                                              apr_socket_t *sock);
760251875Speter
761251875Speter/**
762251875Speter * Return the IP address (in numeric address string format) in
763251875Speter * an APR socket address.  APR will allocate storage for the IP address
764251875Speter * string from the pool of the apr_sockaddr_t.
765251875Speter * @param addr The IP address.
766251875Speter * @param sockaddr The socket address to reference.
767251875Speter */
768251875SpeterAPR_DECLARE(apr_status_t) apr_sockaddr_ip_get(char **addr,
769251875Speter                                              apr_sockaddr_t *sockaddr);
770251875Speter
771251875Speter/**
772251875Speter * Write the IP address (in numeric address string format) of the APR
773251875Speter * socket address @a sockaddr into the buffer @a buf (of size @a buflen).
774251875Speter * @param sockaddr The socket address to reference.
775251875Speter */
776251875SpeterAPR_DECLARE(apr_status_t) apr_sockaddr_ip_getbuf(char *buf, apr_size_t buflen,
777251875Speter                                                 apr_sockaddr_t *sockaddr);
778251875Speter
779251875Speter/**
780251875Speter * See if the IP addresses in two APR socket addresses are
781251875Speter * equivalent.  Appropriate logic is present for comparing
782251875Speter * IPv4-mapped IPv6 addresses with IPv4 addresses.
783251875Speter *
784251875Speter * @param addr1 One of the APR socket addresses.
785251875Speter * @param addr2 The other APR socket address.
786251875Speter * @remark The return value will be non-zero if the addresses
787251875Speter * are equivalent.
788251875Speter */
789251875SpeterAPR_DECLARE(int) apr_sockaddr_equal(const apr_sockaddr_t *addr1,
790251875Speter                                    const apr_sockaddr_t *addr2);
791251875Speter
792251875Speter/**
793266735Speter * See if the IP address in an APR socket address refers to the wildcard
794266735Speter * address for the protocol family (e.g., INADDR_ANY for IPv4).
795266735Speter *
796266735Speter * @param addr The APR socket address to examine.
797266735Speter * @remark The return value will be non-zero if the address is
798266735Speter * initialized and is the wildcard address.
799266735Speter */
800266735SpeterAPR_DECLARE(int) apr_sockaddr_is_wildcard(const apr_sockaddr_t *addr);
801266735Speter
802266735Speter/**
803251875Speter* Return the type of the socket.
804251875Speter* @param sock The socket to query.
805251875Speter* @param type The returned type (e.g., SOCK_STREAM).
806251875Speter*/
807251875SpeterAPR_DECLARE(apr_status_t) apr_socket_type_get(apr_socket_t *sock,
808251875Speter                                              int *type);
809251875Speter
810251875Speter/**
811251875Speter * Given an apr_sockaddr_t and a service name, set the port for the service
812251875Speter * @param sockaddr The apr_sockaddr_t that will have its port set
813251875Speter * @param servname The name of the service you wish to use
814251875Speter */
815251875SpeterAPR_DECLARE(apr_status_t) apr_getservbyname(apr_sockaddr_t *sockaddr,
816251875Speter                                            const char *servname);
817251875Speter/**
818251875Speter * Build an ip-subnet representation from an IP address and optional netmask or
819251875Speter * number-of-bits.
820251875Speter * @param ipsub The new ip-subnet representation
821251875Speter * @param ipstr The input IP address string
822251875Speter * @param mask_or_numbits The input netmask or number-of-bits string, or NULL
823251875Speter * @param p The pool to allocate from
824251875Speter */
825251875SpeterAPR_DECLARE(apr_status_t) apr_ipsubnet_create(apr_ipsubnet_t **ipsub,
826251875Speter                                              const char *ipstr,
827251875Speter                                              const char *mask_or_numbits,
828251875Speter                                              apr_pool_t *p);
829251875Speter
830251875Speter/**
831251875Speter * Test the IP address in an apr_sockaddr_t against a pre-built ip-subnet
832251875Speter * representation.
833251875Speter * @param ipsub The ip-subnet representation
834251875Speter * @param sa The socket address to test
835251875Speter * @return non-zero if the socket address is within the subnet, 0 otherwise
836251875Speter */
837251875SpeterAPR_DECLARE(int) apr_ipsubnet_test(apr_ipsubnet_t *ipsub, apr_sockaddr_t *sa);
838251875Speter
839251875Speter#if APR_HAS_SO_ACCEPTFILTER || defined(DOXYGEN)
840251875Speter/**
841251875Speter * Set an OS level accept filter.
842251875Speter * @param sock The socket to put the accept filter on.
843251875Speter * @param name The accept filter
844251875Speter * @param args Any extra args to the accept filter.  Passing NULL here removes
845251875Speter *             the accept filter.
846253734Speter * @bug name and args should have been declared as const char *, as they are in
847253734Speter * APR 2.0
848251875Speter */
849251875Speterapr_status_t apr_socket_accept_filter(apr_socket_t *sock, char *name,
850251875Speter                                      char *args);
851251875Speter#endif
852251875Speter
853251875Speter/**
854251875Speter * Return the protocol of the socket.
855251875Speter * @param sock The socket to query.
856251875Speter * @param protocol The returned protocol (e.g., APR_PROTO_TCP).
857251875Speter */
858251875SpeterAPR_DECLARE(apr_status_t) apr_socket_protocol_get(apr_socket_t *sock,
859251875Speter                                                  int *protocol);
860251875Speter
861251875Speter/**
862251875Speter * Get the pool used by the socket.
863251875Speter */
864251875SpeterAPR_POOL_DECLARE_ACCESSOR(socket);
865251875Speter
866251875Speter/**
867251875Speter * Set a socket to be inherited by child processes.
868251875Speter */
869251875SpeterAPR_DECLARE_INHERIT_SET(socket);
870251875Speter
871251875Speter/**
872251875Speter * Unset a socket from being inherited by child processes.
873251875Speter */
874251875SpeterAPR_DECLARE_INHERIT_UNSET(socket);
875251875Speter
876251875Speter/**
877362181Sdim * Set socket permissions.
878362181Sdim */
879362181SdimAPR_PERMS_SET_IMPLEMENT(socket);
880362181Sdim
881362181Sdim/**
882251875Speter * @defgroup apr_mcast IP Multicast
883251875Speter * @{
884251875Speter */
885251875Speter
886251875Speter/**
887251875Speter * Join a Multicast Group
888251875Speter * @param sock The socket to join a multicast group
889251875Speter * @param join The address of the multicast group to join
890251875Speter * @param iface Address of the interface to use.  If NULL is passed, the
891251875Speter *              default multicast interface will be used. (OS Dependent)
892251875Speter * @param source Source Address to accept transmissions from (non-NULL
893251875Speter *               implies Source-Specific Multicast)
894251875Speter */
895251875SpeterAPR_DECLARE(apr_status_t) apr_mcast_join(apr_socket_t *sock,
896251875Speter                                         apr_sockaddr_t *join,
897251875Speter                                         apr_sockaddr_t *iface,
898251875Speter                                         apr_sockaddr_t *source);
899251875Speter
900251875Speter/**
901251875Speter * Leave a Multicast Group.  All arguments must be the same as
902251875Speter * apr_mcast_join.
903251875Speter * @param sock The socket to leave a multicast group
904251875Speter * @param addr The address of the multicast group to leave
905251875Speter * @param iface Address of the interface to use.  If NULL is passed, the
906251875Speter *              default multicast interface will be used. (OS Dependent)
907251875Speter * @param source Source Address to accept transmissions from (non-NULL
908251875Speter *               implies Source-Specific Multicast)
909251875Speter */
910251875SpeterAPR_DECLARE(apr_status_t) apr_mcast_leave(apr_socket_t *sock,
911251875Speter                                          apr_sockaddr_t *addr,
912251875Speter                                          apr_sockaddr_t *iface,
913251875Speter                                          apr_sockaddr_t *source);
914251875Speter
915251875Speter/**
916251875Speter * Set the Multicast Time to Live (ttl) for a multicast transmission.
917251875Speter * @param sock The socket to set the multicast ttl
918251875Speter * @param ttl Time to live to Assign. 0-255, default=1
919251875Speter * @remark If the TTL is 0, packets will only be seen by sockets on
920251875Speter * the local machine, and only when multicast loopback is enabled.
921251875Speter */
922251875SpeterAPR_DECLARE(apr_status_t) apr_mcast_hops(apr_socket_t *sock,
923251875Speter                                         apr_byte_t ttl);
924251875Speter
925251875Speter/**
926251875Speter * Toggle IP Multicast Loopback
927251875Speter * @param sock The socket to set multicast loopback
928251875Speter * @param opt 0=disable, 1=enable
929251875Speter */
930251875SpeterAPR_DECLARE(apr_status_t) apr_mcast_loopback(apr_socket_t *sock,
931251875Speter                                             apr_byte_t opt);
932251875Speter
933251875Speter
934251875Speter/**
935251875Speter * Set the Interface to be used for outgoing Multicast Transmissions.
936251875Speter * @param sock The socket to set the multicast interface on
937251875Speter * @param iface Address of the interface to use for Multicast
938251875Speter */
939251875SpeterAPR_DECLARE(apr_status_t) apr_mcast_interface(apr_socket_t *sock,
940251875Speter                                              apr_sockaddr_t *iface);
941251875Speter
942251875Speter/** @} */
943251875Speter
944251875Speter/** @} */
945251875Speter
946251875Speter#ifdef __cplusplus
947251875Speter}
948251875Speter#endif
949251875Speter
950251875Speter#endif  /* ! APR_NETWORK_IO_H */
951251875Speter
952