apr_network_io.h 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951
  1. /* Licensed to the Apache Software Foundation (ASF) under one or more
  2. * contributor license agreements. See the NOTICE file distributed with
  3. * this work for additional information regarding copyright ownership.
  4. * The ASF licenses this file to You under the Apache License, Version 2.0
  5. * (the "License"); you may not use this file except in compliance with
  6. * the License. You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef APR_NETWORK_IO_H
  17. #define APR_NETWORK_IO_H
  18. /**
  19. * @file apr_network_io.h
  20. * @brief APR Network library
  21. */
  22. #include "apr.h"
  23. #include "apr_pools.h"
  24. #include "apr_file_io.h"
  25. #include "apr_errno.h"
  26. #include "apr_inherit.h"
  27. #include "apr_perms_set.h"
  28. #if APR_HAVE_NETINET_IN_H
  29. #include <netinet/in.h>
  30. #endif
  31. #if APR_HAVE_SYS_UN_H
  32. #include <sys/un.h>
  33. #endif
  34. #ifdef __cplusplus
  35. extern "C" {
  36. #endif /* __cplusplus */
  37. /**
  38. * @defgroup apr_network_io Network Routines
  39. * @ingroup APR
  40. * @{
  41. */
  42. #ifndef APR_MAX_SECS_TO_LINGER
  43. /** Maximum seconds to linger */
  44. #define APR_MAX_SECS_TO_LINGER 30
  45. #endif
  46. #ifndef APRMAXHOSTLEN
  47. /** Maximum hostname length */
  48. #define APRMAXHOSTLEN 256
  49. #endif
  50. #ifndef APR_ANYADDR
  51. /** Default 'any' address */
  52. #define APR_ANYADDR "0.0.0.0"
  53. #endif
  54. /**
  55. * @defgroup apr_sockopt Socket option definitions
  56. * @{
  57. */
  58. #define APR_SO_LINGER 1 /**< Linger */
  59. #define APR_SO_KEEPALIVE 2 /**< Keepalive */
  60. #define APR_SO_DEBUG 4 /**< Debug */
  61. #define APR_SO_NONBLOCK 8 /**< Non-blocking IO */
  62. #define APR_SO_REUSEADDR 16 /**< Reuse addresses */
  63. #define APR_SO_SNDBUF 64 /**< Send buffer */
  64. #define APR_SO_RCVBUF 128 /**< Receive buffer */
  65. #define APR_SO_DISCONNECTED 256 /**< Disconnected */
  66. #define APR_TCP_NODELAY 512 /**< For SCTP sockets, this is mapped
  67. * to STCP_NODELAY internally.
  68. */
  69. #define APR_TCP_NOPUSH 1024 /**< No push */
  70. #define APR_RESET_NODELAY 2048 /**< This flag is ONLY set internally
  71. * when we set APR_TCP_NOPUSH with
  72. * APR_TCP_NODELAY set to tell us that
  73. * APR_TCP_NODELAY should be turned on
  74. * again when NOPUSH is turned off
  75. */
  76. #define APR_INCOMPLETE_READ 4096 /**< Set on non-blocking sockets
  77. * (timeout != 0) on which the
  78. * previous read() did not fill a buffer
  79. * completely. the next apr_socket_recv()
  80. * will first call select()/poll() rather than
  81. * going straight into read(). (Can also
  82. * be set by an application to force a
  83. * select()/poll() call before the next
  84. * read, in cases where the app expects
  85. * that an immediate read would fail.)
  86. */
  87. #define APR_INCOMPLETE_WRITE 8192 /**< like APR_INCOMPLETE_READ, but for write
  88. * @see APR_INCOMPLETE_READ
  89. */
  90. #define APR_IPV6_V6ONLY 16384 /**< Don't accept IPv4 connections on an
  91. * IPv6 listening socket.
  92. */
  93. #define APR_TCP_DEFER_ACCEPT 32768 /**< Delay accepting of new connections
  94. * until data is available.
  95. * @see apr_socket_accept_filter
  96. */
  97. #define APR_SO_BROADCAST 65536 /**< Allow broadcast
  98. */
  99. #define APR_SO_FREEBIND 131072 /**< Allow binding to addresses not owned
  100. * by any interface
  101. */
  102. /** @} */
  103. /** Define what type of socket shutdown should occur. */
  104. typedef enum {
  105. APR_SHUTDOWN_READ, /**< no longer allow read request */
  106. APR_SHUTDOWN_WRITE, /**< no longer allow write requests */
  107. APR_SHUTDOWN_READWRITE /**< no longer allow read or write requests */
  108. } apr_shutdown_how_e;
  109. #define APR_IPV4_ADDR_OK 0x01 /**< @see apr_sockaddr_info_get() */
  110. #define APR_IPV6_ADDR_OK 0x02 /**< @see apr_sockaddr_info_get() */
  111. #if (!APR_HAVE_IN_ADDR)
  112. /**
  113. * We need to make sure we always have an in_addr type, so APR will just
  114. * define it ourselves, if the platform doesn't provide it.
  115. */
  116. struct in_addr {
  117. apr_uint32_t s_addr; /**< storage to hold the IP# */
  118. };
  119. #endif
  120. /** @def APR_INADDR_NONE
  121. * Not all platforms have a real INADDR_NONE. This macro replaces
  122. * INADDR_NONE on all platforms.
  123. */
  124. #ifdef INADDR_NONE
  125. #define APR_INADDR_NONE INADDR_NONE
  126. #else
  127. #define APR_INADDR_NONE ((unsigned int) 0xffffffff)
  128. #endif
  129. /**
  130. * @def APR_INET
  131. * Not all platforms have these defined, so we'll define them here
  132. * The default values come from FreeBSD 4.1.1
  133. */
  134. #define APR_INET AF_INET
  135. /** @def APR_UNSPEC
  136. * Let the system decide which address family to use
  137. */
  138. #ifdef AF_UNSPEC
  139. #define APR_UNSPEC AF_UNSPEC
  140. #else
  141. #define APR_UNSPEC 0
  142. #endif
  143. #if APR_HAVE_IPV6
  144. /** @def APR_INET6
  145. * IPv6 Address Family. Not all platforms may have this defined.
  146. */
  147. #define APR_INET6 AF_INET6
  148. #endif
  149. #if APR_HAVE_SOCKADDR_UN
  150. #if defined (AF_UNIX)
  151. #define APR_UNIX AF_UNIX
  152. #elif defined(AF_LOCAL)
  153. #define APR_UNIX AF_LOCAL
  154. #else
  155. #error "Neither AF_UNIX nor AF_LOCAL is defined"
  156. #endif
  157. #else /* !APR_HAVE_SOCKADDR_UN */
  158. #if defined (AF_UNIX)
  159. #define APR_UNIX AF_UNIX
  160. #elif defined(AF_LOCAL)
  161. #define APR_UNIX AF_LOCAL
  162. #else
  163. /* TODO: Use a smarter way to detect unique APR_UNIX value */
  164. #define APR_UNIX 1234
  165. #endif
  166. #endif
  167. /**
  168. * @defgroup IP_Proto IP Protocol Definitions for use when creating sockets
  169. * @{
  170. */
  171. #define APR_PROTO_TCP 6 /**< TCP */
  172. #define APR_PROTO_UDP 17 /**< UDP */
  173. #define APR_PROTO_SCTP 132 /**< SCTP */
  174. /** @} */
  175. /**
  176. * Enum used to denote either the local and remote endpoint of a
  177. * connection.
  178. */
  179. typedef enum {
  180. APR_LOCAL, /**< Socket information for local end of connection */
  181. APR_REMOTE /**< Socket information for remote end of connection */
  182. } apr_interface_e;
  183. /**
  184. * The specific declaration of inet_addr's ... some platforms fall back
  185. * inet_network (this is not good, but necessary)
  186. */
  187. #if APR_HAVE_INET_ADDR
  188. #define apr_inet_addr inet_addr
  189. #elif APR_HAVE_INET_NETWORK /* only DGUX, as far as I know */
  190. /**
  191. * @warning
  192. * not generally safe... inet_network() and inet_addr() perform
  193. * different functions */
  194. #define apr_inet_addr inet_network
  195. #endif
  196. /** A structure to represent sockets */
  197. typedef struct apr_socket_t apr_socket_t;
  198. /**
  199. * A structure to encapsulate headers and trailers for apr_socket_sendfile
  200. */
  201. typedef struct apr_hdtr_t apr_hdtr_t;
  202. /** A structure to represent in_addr */
  203. typedef struct in_addr apr_in_addr_t;
  204. /** A structure to represent an IP subnet */
  205. typedef struct apr_ipsubnet_t apr_ipsubnet_t;
  206. /** @remark use apr_uint16_t just in case some system has a short that isn't 16 bits... */
  207. typedef apr_uint16_t apr_port_t;
  208. /** @remark It's defined here as I think it should all be platform safe...
  209. * @see apr_sockaddr_t
  210. */
  211. typedef struct apr_sockaddr_t apr_sockaddr_t;
  212. /**
  213. * APRs socket address type, used to ensure protocol independence
  214. */
  215. struct apr_sockaddr_t {
  216. /** The pool to use... */
  217. apr_pool_t *pool;
  218. /** The hostname */
  219. char *hostname;
  220. /** Either a string of the port number or the service name for the port */
  221. char *servname;
  222. /** The numeric port */
  223. apr_port_t port;
  224. /** The family */
  225. apr_int32_t family;
  226. /** How big is the sockaddr we're using? */
  227. apr_socklen_t salen;
  228. /** How big is the ip address structure we're using? */
  229. int ipaddr_len;
  230. /** How big should the address buffer be? 16 for v4 or 46 for v6
  231. * used in inet_ntop... */
  232. int addr_str_len;
  233. /** This points to the IP address structure within the appropriate
  234. * sockaddr structure. */
  235. void *ipaddr_ptr;
  236. /** If multiple addresses were found by apr_sockaddr_info_get(), this
  237. * points to a representation of the next address. */
  238. apr_sockaddr_t *next;
  239. /** Union of either IPv4 or IPv6 sockaddr. */
  240. union {
  241. /** IPv4 sockaddr structure */
  242. struct sockaddr_in sin;
  243. #if APR_HAVE_IPV6
  244. /** IPv6 sockaddr structure */
  245. struct sockaddr_in6 sin6;
  246. #endif
  247. #if APR_HAVE_SA_STORAGE
  248. /** Placeholder to ensure that the size of this union is not
  249. * dependent on whether APR_HAVE_IPV6 is defined. */
  250. struct sockaddr_storage sas;
  251. #endif
  252. #if APR_HAVE_SOCKADDR_UN
  253. /** Unix domain socket sockaddr structure */
  254. struct sockaddr_un unx;
  255. #endif
  256. } sa;
  257. };
  258. #if APR_HAS_SENDFILE
  259. /**
  260. * Support reusing the socket on platforms which support it (from disconnect,
  261. * specifically Win32.
  262. * @remark Optional flag passed into apr_socket_sendfile()
  263. */
  264. #define APR_SENDFILE_DISCONNECT_SOCKET 1
  265. #endif
  266. /** A structure to encapsulate headers and trailers for apr_socket_sendfile */
  267. struct apr_hdtr_t {
  268. /** An iovec to store the headers sent before the file. */
  269. struct iovec* headers;
  270. /** number of headers in the iovec */
  271. int numheaders;
  272. /** An iovec to store the trailers sent after the file. */
  273. struct iovec* trailers;
  274. /** number of trailers in the iovec */
  275. int numtrailers;
  276. };
  277. /* function definitions */
  278. /**
  279. * Create a socket.
  280. * @param new_sock The new socket that has been set up.
  281. * @param family The address family of the socket (e.g., APR_INET).
  282. * @param type The type of the socket (e.g., SOCK_STREAM).
  283. * @param protocol The protocol of the socket (e.g., APR_PROTO_TCP).
  284. * @param cont The pool for the apr_socket_t and associated storage.
  285. * @note The pool will be used by various functions that operate on the
  286. * socket. The caller must ensure that it is not used by other threads
  287. * at the same time.
  288. */
  289. APR_DECLARE(apr_status_t) apr_socket_create(apr_socket_t **new_sock,
  290. int family, int type,
  291. int protocol,
  292. apr_pool_t *cont);
  293. /**
  294. * Shutdown either reading, writing, or both sides of a socket.
  295. * @param thesocket The socket to close
  296. * @param how How to shutdown the socket. One of:
  297. * <PRE>
  298. * APR_SHUTDOWN_READ no longer allow read requests
  299. * APR_SHUTDOWN_WRITE no longer allow write requests
  300. * APR_SHUTDOWN_READWRITE no longer allow read or write requests
  301. * </PRE>
  302. * @see apr_shutdown_how_e
  303. * @remark This does not actually close the socket descriptor, it just
  304. * controls which calls are still valid on the socket.
  305. */
  306. APR_DECLARE(apr_status_t) apr_socket_shutdown(apr_socket_t *thesocket,
  307. apr_shutdown_how_e how);
  308. /**
  309. * Close a socket.
  310. * @param thesocket The socket to close
  311. */
  312. APR_DECLARE(apr_status_t) apr_socket_close(apr_socket_t *thesocket);
  313. /**
  314. * Bind the socket to its associated port
  315. * @param sock The socket to bind
  316. * @param sa The socket address to bind to
  317. * @remark This may be where we will find out if there is any other process
  318. * using the selected port.
  319. */
  320. APR_DECLARE(apr_status_t) apr_socket_bind(apr_socket_t *sock,
  321. apr_sockaddr_t *sa);
  322. /**
  323. * Listen to a bound socket for connections.
  324. * @param sock The socket to listen on
  325. * @param backlog The number of outstanding connections allowed in the sockets
  326. * listen queue. If this value is less than zero, the listen
  327. * queue size is set to zero.
  328. */
  329. APR_DECLARE(apr_status_t) apr_socket_listen(apr_socket_t *sock,
  330. apr_int32_t backlog);
  331. /**
  332. * Accept a new connection request
  333. * @param new_sock A copy of the socket that is connected to the socket that
  334. * made the connection request. This is the socket which should
  335. * be used for all future communication.
  336. * @param sock The socket we are listening on.
  337. * @param connection_pool The pool for the new socket.
  338. * @note The pool will be used by various functions that operate on the
  339. * socket. The caller must ensure that it is not used by other threads
  340. * at the same time.
  341. */
  342. APR_DECLARE(apr_status_t) apr_socket_accept(apr_socket_t **new_sock,
  343. apr_socket_t *sock,
  344. apr_pool_t *connection_pool);
  345. /**
  346. * Issue a connection request to a socket either on the same machine
  347. * or a different one.
  348. * @param sock The socket we wish to use for our side of the connection
  349. * @param sa The address of the machine we wish to connect to.
  350. */
  351. APR_DECLARE(apr_status_t) apr_socket_connect(apr_socket_t *sock,
  352. apr_sockaddr_t *sa);
  353. /**
  354. * Determine whether the receive part of the socket has been closed by
  355. * the peer (such that a subsequent call to apr_socket_read would
  356. * return APR_EOF), if the socket's receive buffer is empty. This
  357. * function does not block waiting for I/O.
  358. *
  359. * @param sock The socket to check
  360. * @param atreadeof If APR_SUCCESS is returned, *atreadeof is set to
  361. * non-zero if a subsequent read would return APR_EOF
  362. * @return an error is returned if it was not possible to determine the
  363. * status, in which case *atreadeof is not changed.
  364. */
  365. APR_DECLARE(apr_status_t) apr_socket_atreadeof(apr_socket_t *sock,
  366. int *atreadeof);
  367. /**
  368. * Create apr_sockaddr_t from hostname, address family, and port.
  369. * @param sa The new apr_sockaddr_t.
  370. * @param hostname The hostname or numeric address string to resolve/parse, or
  371. * NULL to build an address that corresponds to 0.0.0.0 or ::
  372. * or in case of APR_UNIX family it is absolute socket filename.
  373. * @param family The address family to use, or APR_UNSPEC if the system should
  374. * decide.
  375. * @param port The port number.
  376. * @param flags Special processing flags:
  377. * <PRE>
  378. * APR_IPV4_ADDR_OK first query for IPv4 addresses; only look
  379. * for IPv6 addresses if the first query failed;
  380. * only valid if family is APR_UNSPEC and hostname
  381. * isn't NULL; mutually exclusive with
  382. * APR_IPV6_ADDR_OK
  383. * APR_IPV6_ADDR_OK first query for IPv6 addresses; only look
  384. * for IPv4 addresses if the first query failed;
  385. * only valid if family is APR_UNSPEC and hostname
  386. * isn't NULL and APR_HAVE_IPV6; mutually exclusive
  387. * with APR_IPV4_ADDR_OK
  388. * </PRE>
  389. * @param p The pool for the apr_sockaddr_t and associated storage.
  390. */
  391. APR_DECLARE(apr_status_t) apr_sockaddr_info_get(apr_sockaddr_t **sa,
  392. const char *hostname,
  393. apr_int32_t family,
  394. apr_port_t port,
  395. apr_int32_t flags,
  396. apr_pool_t *p);
  397. /**
  398. * Copy apr_sockaddr_t src to dst on pool p.
  399. * @param dst The destination apr_sockaddr_t.
  400. * @param src The source apr_sockaddr_t.
  401. * @param p The pool for the apr_sockaddr_t and associated storage.
  402. */
  403. APR_DECLARE(apr_status_t) apr_sockaddr_info_copy(apr_sockaddr_t **dst,
  404. const apr_sockaddr_t *src,
  405. apr_pool_t *p);
  406. /* Set the zone of an IPv6 link-local address object.
  407. * @param sa Socket address object
  408. * @param zone_id Zone ID (textual "eth0" or numeric "3").
  409. * @return Returns APR_EBADIP for non-IPv6 socket or an IPv6 address
  410. * which isn't link-local.
  411. */
  412. APR_DECLARE(apr_status_t) apr_sockaddr_zone_set(apr_sockaddr_t *sa,
  413. const char *zone_id);
  414. /* Retrieve the zone of an IPv6 link-local address object.
  415. * @param sa Socket address object
  416. * @param name If non-NULL, set to the textual representation of the zone id
  417. * @param id If non-NULL, set to the integer zone id
  418. * @param p Pool from which *name is allocated if used.
  419. * @return Returns APR_EBADIP for non-IPv6 socket or socket without any zone id
  420. * set, or other error if the interface could not be mapped to a name.
  421. * @remark Both name and id may be NULL, neither are modified if
  422. * non-NULL in error cases.
  423. */
  424. APR_DECLARE(apr_status_t) apr_sockaddr_zone_get(const apr_sockaddr_t *sa,
  425. const char **name,
  426. apr_uint32_t *id,
  427. apr_pool_t *p);
  428. /**
  429. * Look up the host name from an apr_sockaddr_t.
  430. * @param hostname The hostname.
  431. * @param sa The apr_sockaddr_t.
  432. * @param flags Special processing flags.
  433. * @remark Results can vary significantly between platforms
  434. * when processing wildcard socket addresses.
  435. */
  436. APR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname,
  437. apr_sockaddr_t *sa,
  438. apr_int32_t flags);
  439. /**
  440. * Parse hostname/IP address with scope id and port.
  441. *
  442. * Any of the following strings are accepted:
  443. * 8080 (just the port number)
  444. * www.apache.org (just the hostname)
  445. * www.apache.org:8080 (hostname and port number)
  446. * [fe80::1]:80 (IPv6 numeric address string only)
  447. * [fe80::1%eth0] (IPv6 numeric address string and scope id)
  448. *
  449. * Invalid strings:
  450. * (empty string)
  451. * [abc] (not valid IPv6 numeric address string)
  452. * abc:65536 (invalid port number)
  453. *
  454. * @param addr The new buffer containing just the hostname. On output, *addr
  455. * will be NULL if no hostname/IP address was specfied.
  456. * @param scope_id The new buffer containing just the scope id. On output,
  457. * *scope_id will be NULL if no scope id was specified.
  458. * @param port The port number. On output, *port will be 0 if no port was
  459. * specified.
  460. * ### FIXME: 0 is a legal port (per RFC 1700). this should
  461. * ### return something besides zero if the port is missing.
  462. * @param str The input string to be parsed.
  463. * @param p The pool from which *addr and *scope_id are allocated.
  464. * @remark If scope id shouldn't be allowed, check for scope_id != NULL in
  465. * addition to checking the return code. If addr/hostname should be
  466. * required, check for addr == NULL in addition to checking the
  467. * return code.
  468. */
  469. APR_DECLARE(apr_status_t) apr_parse_addr_port(char **addr,
  470. char **scope_id,
  471. apr_port_t *port,
  472. const char *str,
  473. apr_pool_t *p);
  474. /**
  475. * Get name of the current machine
  476. * @param buf A buffer to store the hostname in.
  477. * @param len The maximum length of the hostname that can be stored in the
  478. * buffer provided. The suggested length is APRMAXHOSTLEN + 1.
  479. * @param cont The pool to use.
  480. * @remark If the buffer was not large enough, an error will be returned.
  481. */
  482. APR_DECLARE(apr_status_t) apr_gethostname(char *buf, int len, apr_pool_t *cont);
  483. /**
  484. * Return the data associated with the current socket
  485. * @param data The user data associated with the socket.
  486. * @param key The key to associate with the user data.
  487. * @param sock The currently open socket.
  488. */
  489. APR_DECLARE(apr_status_t) apr_socket_data_get(void **data, const char *key,
  490. apr_socket_t *sock);
  491. /**
  492. * Set the data associated with the current socket.
  493. * @param sock The currently open socket.
  494. * @param data The user data to associate with the socket.
  495. * @param key The key to associate with the data.
  496. * @param cleanup The cleanup to call when the socket is destroyed.
  497. */
  498. APR_DECLARE(apr_status_t) apr_socket_data_set(apr_socket_t *sock, void *data,
  499. const char *key,
  500. apr_status_t (*cleanup)(void*));
  501. /**
  502. * Send data over a network.
  503. * @param sock The socket to send the data over.
  504. * @param buf The buffer which contains the data to be sent.
  505. * @param len On entry, the number of bytes to send; on exit, the number
  506. * of bytes sent.
  507. * @remark
  508. * <PRE>
  509. * This functions acts like a blocking write by default. To change
  510. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  511. * socket option.
  512. *
  513. * It is possible for both bytes to be sent and an error to be returned.
  514. *
  515. * APR_EINTR is never returned.
  516. * </PRE>
  517. */
  518. APR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf,
  519. apr_size_t *len);
  520. /**
  521. * Send multiple buffers over a network.
  522. * @param sock The socket to send the data over.
  523. * @param vec The array of iovec structs containing the data to send
  524. * @param nvec The number of iovec structs in the array
  525. * @param len Receives the number of bytes actually written
  526. * @remark
  527. * <PRE>
  528. * This functions acts like a blocking write by default. To change
  529. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  530. * socket option.
  531. * The number of bytes actually sent is stored in argument 4.
  532. *
  533. * It is possible for both bytes to be sent and an error to be returned.
  534. *
  535. * APR_EINTR is never returned.
  536. * </PRE>
  537. */
  538. APR_DECLARE(apr_status_t) apr_socket_sendv(apr_socket_t *sock,
  539. const struct iovec *vec,
  540. apr_int32_t nvec, apr_size_t *len);
  541. /**
  542. * @param sock The socket to send from
  543. * @param where The apr_sockaddr_t describing where to send the data
  544. * @param flags The flags to use
  545. * @param buf The data to send
  546. * @param len The length of the data to send
  547. */
  548. APR_DECLARE(apr_status_t) apr_socket_sendto(apr_socket_t *sock,
  549. apr_sockaddr_t *where,
  550. apr_int32_t flags, const char *buf,
  551. apr_size_t *len);
  552. /**
  553. * Read data from a socket. On success, the address of the peer from
  554. * which the data was sent is copied into the @a from parameter, and the
  555. * @a len parameter is updated to give the number of bytes written to
  556. * @a buf.
  557. *
  558. * @param from Updated with the address from which the data was received
  559. * @param sock The socket to use
  560. * @param flags The flags to use
  561. * @param buf The buffer to use
  562. * @param len The length of the available buffer
  563. */
  564. APR_DECLARE(apr_status_t) apr_socket_recvfrom(apr_sockaddr_t *from,
  565. apr_socket_t *sock,
  566. apr_int32_t flags, char *buf,
  567. apr_size_t *len);
  568. #if APR_HAS_SENDFILE || defined(DOXYGEN)
  569. /**
  570. * Send a file from an open file descriptor to a socket, along with
  571. * optional headers and trailers
  572. * @param sock The socket to which we're writing
  573. * @param file The open file from which to read
  574. * @param hdtr A structure containing the headers and trailers to send
  575. * @param offset Offset into the file where we should begin writing
  576. * @param len (input) - Number of bytes to send from the file
  577. * (output) - Number of bytes actually sent,
  578. * including headers, file, and trailers
  579. * @param flags APR flags that are mapped to OS specific flags
  580. * @remark This functions acts like a blocking write by default. To change
  581. * this behavior, use apr_socket_timeout_set() or the
  582. * APR_SO_NONBLOCK socket option.
  583. * The number of bytes actually sent is stored in the len parameter.
  584. * The offset parameter is passed by reference for no reason; its
  585. * value will never be modified by the apr_socket_sendfile() function.
  586. */
  587. APR_DECLARE(apr_status_t) apr_socket_sendfile(apr_socket_t *sock,
  588. apr_file_t *file,
  589. apr_hdtr_t *hdtr,
  590. apr_off_t *offset,
  591. apr_size_t *len,
  592. apr_int32_t flags);
  593. #endif /* APR_HAS_SENDFILE */
  594. /**
  595. * Read data from a network.
  596. * @param sock The socket to read the data from.
  597. * @param buf The buffer to store the data in.
  598. * @param len On entry, the number of bytes to receive; on exit, the number
  599. * of bytes received.
  600. * @remark
  601. * <PRE>
  602. * This functions acts like a blocking read by default. To change
  603. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  604. * socket option.
  605. * The number of bytes actually received is stored in argument 3.
  606. *
  607. * It is possible for both bytes to be received and an APR_EOF or
  608. * other error to be returned.
  609. *
  610. * APR_EINTR is never returned.
  611. * </PRE>
  612. */
  613. APR_DECLARE(apr_status_t) apr_socket_recv(apr_socket_t *sock,
  614. char *buf, apr_size_t *len);
  615. /**
  616. * Setup socket options for the specified socket
  617. * @param sock The socket to set up.
  618. * @param opt The option we would like to configure. One of:
  619. * <PRE>
  620. * APR_SO_DEBUG -- turn on debugging information
  621. * APR_SO_KEEPALIVE -- keep connections active
  622. * APR_SO_LINGER -- lingers on close if data is present
  623. * APR_SO_NONBLOCK -- Turns blocking on/off for socket
  624. * When this option is enabled, use
  625. * the APR_STATUS_IS_EAGAIN() macro to
  626. * see if a send or receive function
  627. * could not transfer data without
  628. * blocking.
  629. * APR_SO_REUSEADDR -- The rules used in validating addresses
  630. * supplied to bind should allow reuse
  631. * of local addresses.
  632. * APR_SO_SNDBUF -- Set the SendBufferSize
  633. * APR_SO_RCVBUF -- Set the ReceiveBufferSize
  634. * APR_SO_FREEBIND -- Allow binding to non-local IP address.
  635. * </PRE>
  636. * @param on Value for the option.
  637. */
  638. APR_DECLARE(apr_status_t) apr_socket_opt_set(apr_socket_t *sock,
  639. apr_int32_t opt, apr_int32_t on);
  640. /**
  641. * Setup socket timeout for the specified socket
  642. * @param sock The socket to set up.
  643. * @param t Value for the timeout.
  644. * <PRE>
  645. * t > 0 -- read and write calls return APR_TIMEUP if specified time
  646. * elapsess with no data read or written
  647. * t == 0 -- read and write calls never block
  648. * t < 0 -- read and write calls block
  649. * </PRE>
  650. */
  651. APR_DECLARE(apr_status_t) apr_socket_timeout_set(apr_socket_t *sock,
  652. apr_interval_time_t t);
  653. /**
  654. * Query socket options for the specified socket
  655. * @param sock The socket to query
  656. * @param opt The option we would like to query. One of:
  657. * <PRE>
  658. * APR_SO_DEBUG -- turn on debugging information
  659. * APR_SO_KEEPALIVE -- keep connections active
  660. * APR_SO_LINGER -- lingers on close if data is present
  661. * APR_SO_NONBLOCK -- Turns blocking on/off for socket
  662. * APR_SO_REUSEADDR -- The rules used in validating addresses
  663. * supplied to bind should allow reuse
  664. * of local addresses.
  665. * APR_SO_SNDBUF -- Set the SendBufferSize
  666. * APR_SO_RCVBUF -- Set the ReceiveBufferSize
  667. * APR_SO_DISCONNECTED -- Query the disconnected state of the socket.
  668. * (Currently only used on Windows)
  669. * </PRE>
  670. * @param on Socket option returned on the call.
  671. */
  672. APR_DECLARE(apr_status_t) apr_socket_opt_get(apr_socket_t *sock,
  673. apr_int32_t opt, apr_int32_t *on);
  674. /**
  675. * Query socket timeout for the specified socket
  676. * @param sock The socket to query
  677. * @param t Socket timeout returned from the query.
  678. */
  679. APR_DECLARE(apr_status_t) apr_socket_timeout_get(apr_socket_t *sock,
  680. apr_interval_time_t *t);
  681. /**
  682. * Query the specified socket if at the OOB/Urgent data mark
  683. * @param sock The socket to query
  684. * @param atmark Is set to true if socket is at the OOB/urgent mark,
  685. * otherwise is set to false.
  686. */
  687. APR_DECLARE(apr_status_t) apr_socket_atmark(apr_socket_t *sock,
  688. int *atmark);
  689. /**
  690. * Return an address associated with a socket; either the address to
  691. * which the socket is bound locally or the address of the peer
  692. * to which the socket is connected.
  693. * @param sa The returned apr_sockaddr_t.
  694. * @param which Whether to retrieve the local or remote address
  695. * @param sock The socket to use
  696. */
  697. APR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa,
  698. apr_interface_e which,
  699. apr_socket_t *sock);
  700. /**
  701. * Return the IP address (in numeric address string format) in
  702. * an APR socket address. APR will allocate storage for the IP address
  703. * string from the pool of the apr_sockaddr_t.
  704. * @param addr The IP address.
  705. * @param sockaddr The socket address to reference.
  706. */
  707. APR_DECLARE(apr_status_t) apr_sockaddr_ip_get(char **addr,
  708. apr_sockaddr_t *sockaddr);
  709. /**
  710. * Write the IP address (in numeric address string format) of the APR
  711. * socket address @a sockaddr into the buffer @a buf (of size @a buflen).
  712. * @param sockaddr The socket address to reference.
  713. */
  714. APR_DECLARE(apr_status_t) apr_sockaddr_ip_getbuf(char *buf, apr_size_t buflen,
  715. apr_sockaddr_t *sockaddr);
  716. /**
  717. * See if the IP addresses in two APR socket addresses are
  718. * equivalent. Appropriate logic is present for comparing
  719. * IPv4-mapped IPv6 addresses with IPv4 addresses.
  720. *
  721. * @param addr1 One of the APR socket addresses.
  722. * @param addr2 The other APR socket address.
  723. * @remark The return value will be non-zero if the addresses
  724. * are equivalent.
  725. */
  726. APR_DECLARE(int) apr_sockaddr_equal(const apr_sockaddr_t *addr1,
  727. const apr_sockaddr_t *addr2);
  728. /**
  729. * See if the IP address in an APR socket address refers to the wildcard
  730. * address for the protocol family (e.g., INADDR_ANY for IPv4).
  731. *
  732. * @param addr The APR socket address to examine.
  733. * @remark The return value will be non-zero if the address is
  734. * initialized and is the wildcard address.
  735. */
  736. APR_DECLARE(int) apr_sockaddr_is_wildcard(const apr_sockaddr_t *addr);
  737. /**
  738. * Return the type of the socket.
  739. * @param sock The socket to query.
  740. * @param type The returned type (e.g., SOCK_STREAM).
  741. */
  742. APR_DECLARE(apr_status_t) apr_socket_type_get(apr_socket_t *sock,
  743. int *type);
  744. /**
  745. * Given an apr_sockaddr_t and a service name, set the port for the service
  746. * @param sockaddr The apr_sockaddr_t that will have its port set
  747. * @param servname The name of the service you wish to use
  748. */
  749. APR_DECLARE(apr_status_t) apr_getservbyname(apr_sockaddr_t *sockaddr,
  750. const char *servname);
  751. /**
  752. * Build an ip-subnet representation from an IP address and optional netmask or
  753. * number-of-bits.
  754. * @param ipsub The new ip-subnet representation
  755. * @param ipstr The input IP address string
  756. * @param mask_or_numbits The input netmask or number-of-bits string, or NULL
  757. * @param p The pool to allocate from
  758. */
  759. APR_DECLARE(apr_status_t) apr_ipsubnet_create(apr_ipsubnet_t **ipsub,
  760. const char *ipstr,
  761. const char *mask_or_numbits,
  762. apr_pool_t *p);
  763. /**
  764. * Test the IP address in an apr_sockaddr_t against a pre-built ip-subnet
  765. * representation.
  766. * @param ipsub The ip-subnet representation
  767. * @param sa The socket address to test
  768. * @return non-zero if the socket address is within the subnet, 0 otherwise
  769. */
  770. APR_DECLARE(int) apr_ipsubnet_test(apr_ipsubnet_t *ipsub, apr_sockaddr_t *sa);
  771. #if APR_HAS_SO_ACCEPTFILTER || defined(DOXYGEN)
  772. /**
  773. * Set an OS level accept filter.
  774. * @param sock The socket to put the accept filter on.
  775. * @param name The accept filter
  776. * @param args Any extra args to the accept filter. Passing NULL here removes
  777. * the accept filter.
  778. * @bug name and args should have been declared as const char *, as they are in
  779. * APR 2.0
  780. */
  781. apr_status_t apr_socket_accept_filter(apr_socket_t *sock, char *name,
  782. char *args);
  783. #endif
  784. /**
  785. * Return the protocol of the socket.
  786. * @param sock The socket to query.
  787. * @param protocol The returned protocol (e.g., APR_PROTO_TCP).
  788. */
  789. APR_DECLARE(apr_status_t) apr_socket_protocol_get(apr_socket_t *sock,
  790. int *protocol);
  791. /**
  792. * Get the pool used by the socket.
  793. */
  794. APR_POOL_DECLARE_ACCESSOR(socket);
  795. /**
  796. * Set a socket to be inherited by child processes.
  797. */
  798. APR_DECLARE_INHERIT_SET(socket);
  799. /**
  800. * Unset a socket from being inherited by child processes.
  801. */
  802. APR_DECLARE_INHERIT_UNSET(socket);
  803. /**
  804. * Set socket permissions.
  805. */
  806. APR_PERMS_SET_IMPLEMENT(socket);
  807. /**
  808. * @defgroup apr_mcast IP Multicast
  809. * @{
  810. */
  811. /**
  812. * Join a Multicast Group
  813. * @param sock The socket to join a multicast group
  814. * @param join The address of the multicast group to join
  815. * @param iface Address of the interface to use. If NULL is passed, the
  816. * default multicast interface will be used. (OS Dependent)
  817. * @param source Source Address to accept transmissions from (non-NULL
  818. * implies Source-Specific Multicast)
  819. */
  820. APR_DECLARE(apr_status_t) apr_mcast_join(apr_socket_t *sock,
  821. apr_sockaddr_t *join,
  822. apr_sockaddr_t *iface,
  823. apr_sockaddr_t *source);
  824. /**
  825. * Leave a Multicast Group. All arguments must be the same as
  826. * apr_mcast_join.
  827. * @param sock The socket to leave a multicast group
  828. * @param addr The address of the multicast group to leave
  829. * @param iface Address of the interface to use. If NULL is passed, the
  830. * default multicast interface will be used. (OS Dependent)
  831. * @param source Source Address to accept transmissions from (non-NULL
  832. * implies Source-Specific Multicast)
  833. */
  834. APR_DECLARE(apr_status_t) apr_mcast_leave(apr_socket_t *sock,
  835. apr_sockaddr_t *addr,
  836. apr_sockaddr_t *iface,
  837. apr_sockaddr_t *source);
  838. /**
  839. * Set the Multicast Time to Live (ttl) for a multicast transmission.
  840. * @param sock The socket to set the multicast ttl
  841. * @param ttl Time to live to Assign. 0-255, default=1
  842. * @remark If the TTL is 0, packets will only be seen by sockets on
  843. * the local machine, and only when multicast loopback is enabled.
  844. */
  845. APR_DECLARE(apr_status_t) apr_mcast_hops(apr_socket_t *sock,
  846. apr_byte_t ttl);
  847. /**
  848. * Toggle IP Multicast Loopback
  849. * @param sock The socket to set multicast loopback
  850. * @param opt 0=disable, 1=enable
  851. */
  852. APR_DECLARE(apr_status_t) apr_mcast_loopback(apr_socket_t *sock,
  853. apr_byte_t opt);
  854. /**
  855. * Set the Interface to be used for outgoing Multicast Transmissions.
  856. * @param sock The socket to set the multicast interface on
  857. * @param iface Address of the interface to use for Multicast
  858. */
  859. APR_DECLARE(apr_status_t) apr_mcast_interface(apr_socket_t *sock,
  860. apr_sockaddr_t *iface);
  861. /** @} */
  862. /** @} */
  863. #ifdef __cplusplus
  864. }
  865. #endif
  866. #endif /* ! APR_NETWORK_IO_H */