apr_network_io.h 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953
  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. /**
  407. * Set the zone of an IPv6 link-local address object.
  408. * @param sa Socket address object
  409. * @param zone_id Zone ID (textual "eth0" or numeric "3").
  410. * @return Returns APR_EBADIP for non-IPv6 socket or an IPv6 address
  411. * which isn't link-local.
  412. */
  413. APR_DECLARE(apr_status_t) apr_sockaddr_zone_set(apr_sockaddr_t *sa,
  414. const char *zone_id);
  415. /**
  416. * Retrieve the zone of an IPv6 link-local address object.
  417. * @param sa Socket address object
  418. * @param name If non-NULL, set to the textual representation of the zone id
  419. * @param id If non-NULL, set to the integer zone id
  420. * @param p Pool from which *name is allocated if used.
  421. * @return Returns APR_EBADIP for non-IPv6 socket or socket without any zone id
  422. * set, or other error if the interface could not be mapped to a name.
  423. * @remark Both name and id may be NULL, neither are modified if
  424. * non-NULL in error cases.
  425. */
  426. APR_DECLARE(apr_status_t) apr_sockaddr_zone_get(const apr_sockaddr_t *sa,
  427. const char **name,
  428. apr_uint32_t *id,
  429. apr_pool_t *p);
  430. /**
  431. * Look up the host name from an apr_sockaddr_t.
  432. * @param hostname The hostname.
  433. * @param sa The apr_sockaddr_t.
  434. * @param flags Special processing flags.
  435. * @remark Results can vary significantly between platforms
  436. * when processing wildcard socket addresses.
  437. */
  438. APR_DECLARE(apr_status_t) apr_getnameinfo(char **hostname,
  439. apr_sockaddr_t *sa,
  440. apr_int32_t flags);
  441. /**
  442. * Parse hostname/IP address with scope id and port.
  443. *
  444. * Any of the following strings are accepted:
  445. * 8080 (just the port number)
  446. * www.apache.org (just the hostname)
  447. * www.apache.org:8080 (hostname and port number)
  448. * [fe80::1]:80 (IPv6 numeric address string only)
  449. * [fe80::1%eth0] (IPv6 numeric address string and scope id)
  450. *
  451. * Invalid strings:
  452. * (empty string)
  453. * [abc] (not valid IPv6 numeric address string)
  454. * abc:65536 (invalid port number)
  455. *
  456. * @param addr The new buffer containing just the hostname. On output, *addr
  457. * will be NULL if no hostname/IP address was specfied.
  458. * @param scope_id The new buffer containing just the scope id. On output,
  459. * *scope_id will be NULL if no scope id was specified.
  460. * @param port The port number. On output, *port will be 0 if no port was
  461. * specified.
  462. * ### FIXME: 0 is a legal port (per RFC 1700). this should
  463. * ### return something besides zero if the port is missing.
  464. * @param str The input string to be parsed.
  465. * @param p The pool from which *addr and *scope_id are allocated.
  466. * @remark If scope id shouldn't be allowed, check for scope_id != NULL in
  467. * addition to checking the return code. If addr/hostname should be
  468. * required, check for addr == NULL in addition to checking the
  469. * return code.
  470. */
  471. APR_DECLARE(apr_status_t) apr_parse_addr_port(char **addr,
  472. char **scope_id,
  473. apr_port_t *port,
  474. const char *str,
  475. apr_pool_t *p);
  476. /**
  477. * Get name of the current machine
  478. * @param buf A buffer to store the hostname in.
  479. * @param len The maximum length of the hostname that can be stored in the
  480. * buffer provided. The suggested length is APRMAXHOSTLEN + 1.
  481. * @param cont The pool to use.
  482. * @remark If the buffer was not large enough, an error will be returned.
  483. */
  484. APR_DECLARE(apr_status_t) apr_gethostname(char *buf, int len, apr_pool_t *cont);
  485. /**
  486. * Return the data associated with the current socket
  487. * @param data The user data associated with the socket.
  488. * @param key The key to associate with the user data.
  489. * @param sock The currently open socket.
  490. */
  491. APR_DECLARE(apr_status_t) apr_socket_data_get(void **data, const char *key,
  492. apr_socket_t *sock);
  493. /**
  494. * Set the data associated with the current socket.
  495. * @param sock The currently open socket.
  496. * @param data The user data to associate with the socket.
  497. * @param key The key to associate with the data.
  498. * @param cleanup The cleanup to call when the socket is destroyed.
  499. */
  500. APR_DECLARE(apr_status_t) apr_socket_data_set(apr_socket_t *sock, void *data,
  501. const char *key,
  502. apr_status_t (*cleanup)(void*));
  503. /**
  504. * Send data over a network.
  505. * @param sock The socket to send the data over.
  506. * @param buf The buffer which contains the data to be sent.
  507. * @param len On entry, the number of bytes to send; on exit, the number
  508. * of bytes sent.
  509. * @remark
  510. * <PRE>
  511. * This functions acts like a blocking write by default. To change
  512. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  513. * socket option.
  514. *
  515. * It is possible for both bytes to be sent and an error to be returned.
  516. *
  517. * APR_EINTR is never returned.
  518. * </PRE>
  519. */
  520. APR_DECLARE(apr_status_t) apr_socket_send(apr_socket_t *sock, const char *buf,
  521. apr_size_t *len);
  522. /**
  523. * Send multiple buffers over a network.
  524. * @param sock The socket to send the data over.
  525. * @param vec The array of iovec structs containing the data to send
  526. * @param nvec The number of iovec structs in the array
  527. * @param len Receives the number of bytes actually written
  528. * @remark
  529. * <PRE>
  530. * This functions acts like a blocking write by default. To change
  531. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  532. * socket option.
  533. * The number of bytes actually sent is stored in argument 4.
  534. *
  535. * It is possible for both bytes to be sent and an error to be returned.
  536. *
  537. * APR_EINTR is never returned.
  538. * </PRE>
  539. */
  540. APR_DECLARE(apr_status_t) apr_socket_sendv(apr_socket_t *sock,
  541. const struct iovec *vec,
  542. apr_int32_t nvec, apr_size_t *len);
  543. /**
  544. * @param sock The socket to send from
  545. * @param where The apr_sockaddr_t describing where to send the data
  546. * @param flags The flags to use
  547. * @param buf The data to send
  548. * @param len The length of the data to send
  549. */
  550. APR_DECLARE(apr_status_t) apr_socket_sendto(apr_socket_t *sock,
  551. apr_sockaddr_t *where,
  552. apr_int32_t flags, const char *buf,
  553. apr_size_t *len);
  554. /**
  555. * Read data from a socket. On success, the address of the peer from
  556. * which the data was sent is copied into the @a from parameter, and the
  557. * @a len parameter is updated to give the number of bytes written to
  558. * @a buf.
  559. *
  560. * @param from Updated with the address from which the data was received
  561. * @param sock The socket to use
  562. * @param flags The flags to use
  563. * @param buf The buffer to use
  564. * @param len The length of the available buffer
  565. */
  566. APR_DECLARE(apr_status_t) apr_socket_recvfrom(apr_sockaddr_t *from,
  567. apr_socket_t *sock,
  568. apr_int32_t flags, char *buf,
  569. apr_size_t *len);
  570. #if APR_HAS_SENDFILE || defined(DOXYGEN)
  571. /**
  572. * Send a file from an open file descriptor to a socket, along with
  573. * optional headers and trailers
  574. * @param sock The socket to which we're writing
  575. * @param file The open file from which to read
  576. * @param hdtr A structure containing the headers and trailers to send
  577. * @param offset Offset into the file where we should begin writing
  578. * @param len (input) - Number of bytes to send from the file
  579. * (output) - Number of bytes actually sent,
  580. * including headers, file, and trailers
  581. * @param flags APR flags that are mapped to OS specific flags
  582. * @remark This functions acts like a blocking write by default. To change
  583. * this behavior, use apr_socket_timeout_set() or the
  584. * APR_SO_NONBLOCK socket option.
  585. * The number of bytes actually sent is stored in the len parameter.
  586. * The offset parameter is passed by reference for no reason; its
  587. * value will never be modified by the apr_socket_sendfile() function.
  588. */
  589. APR_DECLARE(apr_status_t) apr_socket_sendfile(apr_socket_t *sock,
  590. apr_file_t *file,
  591. apr_hdtr_t *hdtr,
  592. apr_off_t *offset,
  593. apr_size_t *len,
  594. apr_int32_t flags);
  595. #endif /* APR_HAS_SENDFILE */
  596. /**
  597. * Read data from a network.
  598. * @param sock The socket to read the data from.
  599. * @param buf The buffer to store the data in.
  600. * @param len On entry, the number of bytes to receive; on exit, the number
  601. * of bytes received.
  602. * @remark
  603. * <PRE>
  604. * This functions acts like a blocking read by default. To change
  605. * this behavior, use apr_socket_timeout_set() or the APR_SO_NONBLOCK
  606. * socket option.
  607. * The number of bytes actually received is stored in argument 3.
  608. *
  609. * It is possible for both bytes to be received and an APR_EOF or
  610. * other error to be returned.
  611. *
  612. * APR_EINTR is never returned.
  613. * </PRE>
  614. */
  615. APR_DECLARE(apr_status_t) apr_socket_recv(apr_socket_t *sock,
  616. char *buf, apr_size_t *len);
  617. /**
  618. * Setup socket options for the specified socket
  619. * @param sock The socket to set up.
  620. * @param opt The option we would like to configure. One of:
  621. * <PRE>
  622. * APR_SO_DEBUG -- turn on debugging information
  623. * APR_SO_KEEPALIVE -- keep connections active
  624. * APR_SO_LINGER -- lingers on close if data is present
  625. * APR_SO_NONBLOCK -- Turns blocking on/off for socket
  626. * When this option is enabled, use
  627. * the APR_STATUS_IS_EAGAIN() macro to
  628. * see if a send or receive function
  629. * could not transfer data without
  630. * blocking.
  631. * APR_SO_REUSEADDR -- The rules used in validating addresses
  632. * supplied to bind should allow reuse
  633. * of local addresses.
  634. * APR_SO_SNDBUF -- Set the SendBufferSize
  635. * APR_SO_RCVBUF -- Set the ReceiveBufferSize
  636. * APR_SO_FREEBIND -- Allow binding to non-local IP address.
  637. * </PRE>
  638. * @param on Value for the option.
  639. */
  640. APR_DECLARE(apr_status_t) apr_socket_opt_set(apr_socket_t *sock,
  641. apr_int32_t opt, apr_int32_t on);
  642. /**
  643. * Setup socket timeout for the specified socket
  644. * @param sock The socket to set up.
  645. * @param t Value for the timeout.
  646. * <PRE>
  647. * t > 0 -- read and write calls return APR_TIMEUP if specified time
  648. * elapsess with no data read or written
  649. * t == 0 -- read and write calls never block
  650. * t < 0 -- read and write calls block
  651. * </PRE>
  652. */
  653. APR_DECLARE(apr_status_t) apr_socket_timeout_set(apr_socket_t *sock,
  654. apr_interval_time_t t);
  655. /**
  656. * Query socket options for the specified socket
  657. * @param sock The socket to query
  658. * @param opt The option we would like to query. One of:
  659. * <PRE>
  660. * APR_SO_DEBUG -- turn on debugging information
  661. * APR_SO_KEEPALIVE -- keep connections active
  662. * APR_SO_LINGER -- lingers on close if data is present
  663. * APR_SO_NONBLOCK -- Turns blocking on/off for socket
  664. * APR_SO_REUSEADDR -- The rules used in validating addresses
  665. * supplied to bind should allow reuse
  666. * of local addresses.
  667. * APR_SO_SNDBUF -- Set the SendBufferSize
  668. * APR_SO_RCVBUF -- Set the ReceiveBufferSize
  669. * APR_SO_DISCONNECTED -- Query the disconnected state of the socket.
  670. * (Currently only used on Windows)
  671. * </PRE>
  672. * @param on Socket option returned on the call.
  673. */
  674. APR_DECLARE(apr_status_t) apr_socket_opt_get(apr_socket_t *sock,
  675. apr_int32_t opt, apr_int32_t *on);
  676. /**
  677. * Query socket timeout for the specified socket
  678. * @param sock The socket to query
  679. * @param t Socket timeout returned from the query.
  680. */
  681. APR_DECLARE(apr_status_t) apr_socket_timeout_get(apr_socket_t *sock,
  682. apr_interval_time_t *t);
  683. /**
  684. * Query the specified socket if at the OOB/Urgent data mark
  685. * @param sock The socket to query
  686. * @param atmark Is set to true if socket is at the OOB/urgent mark,
  687. * otherwise is set to false.
  688. */
  689. APR_DECLARE(apr_status_t) apr_socket_atmark(apr_socket_t *sock,
  690. int *atmark);
  691. /**
  692. * Return an address associated with a socket; either the address to
  693. * which the socket is bound locally or the address of the peer
  694. * to which the socket is connected.
  695. * @param sa The returned apr_sockaddr_t.
  696. * @param which Whether to retrieve the local or remote address
  697. * @param sock The socket to use
  698. */
  699. APR_DECLARE(apr_status_t) apr_socket_addr_get(apr_sockaddr_t **sa,
  700. apr_interface_e which,
  701. apr_socket_t *sock);
  702. /**
  703. * Return the IP address (in numeric address string format) in
  704. * an APR socket address. APR will allocate storage for the IP address
  705. * string from the pool of the apr_sockaddr_t.
  706. * @param addr The IP address.
  707. * @param sockaddr The socket address to reference.
  708. */
  709. APR_DECLARE(apr_status_t) apr_sockaddr_ip_get(char **addr,
  710. apr_sockaddr_t *sockaddr);
  711. /**
  712. * Write the IP address (in numeric address string format) of the APR
  713. * socket address @a sockaddr into the buffer @a buf (of size @a buflen).
  714. * @param sockaddr The socket address to reference.
  715. */
  716. APR_DECLARE(apr_status_t) apr_sockaddr_ip_getbuf(char *buf, apr_size_t buflen,
  717. apr_sockaddr_t *sockaddr);
  718. /**
  719. * See if the IP addresses in two APR socket addresses are
  720. * equivalent. Appropriate logic is present for comparing
  721. * IPv4-mapped IPv6 addresses with IPv4 addresses.
  722. *
  723. * @param addr1 One of the APR socket addresses.
  724. * @param addr2 The other APR socket address.
  725. * @remark The return value will be non-zero if the addresses
  726. * are equivalent.
  727. */
  728. APR_DECLARE(int) apr_sockaddr_equal(const apr_sockaddr_t *addr1,
  729. const apr_sockaddr_t *addr2);
  730. /**
  731. * See if the IP address in an APR socket address refers to the wildcard
  732. * address for the protocol family (e.g., INADDR_ANY for IPv4).
  733. *
  734. * @param addr The APR socket address to examine.
  735. * @remark The return value will be non-zero if the address is
  736. * initialized and is the wildcard address.
  737. */
  738. APR_DECLARE(int) apr_sockaddr_is_wildcard(const apr_sockaddr_t *addr);
  739. /**
  740. * Return the type of the socket.
  741. * @param sock The socket to query.
  742. * @param type The returned type (e.g., SOCK_STREAM).
  743. */
  744. APR_DECLARE(apr_status_t) apr_socket_type_get(apr_socket_t *sock,
  745. int *type);
  746. /**
  747. * Given an apr_sockaddr_t and a service name, set the port for the service
  748. * @param sockaddr The apr_sockaddr_t that will have its port set
  749. * @param servname The name of the service you wish to use
  750. */
  751. APR_DECLARE(apr_status_t) apr_getservbyname(apr_sockaddr_t *sockaddr,
  752. const char *servname);
  753. /**
  754. * Build an ip-subnet representation from an IP address and optional netmask or
  755. * number-of-bits.
  756. * @param ipsub The new ip-subnet representation
  757. * @param ipstr The input IP address string
  758. * @param mask_or_numbits The input netmask or number-of-bits string, or NULL
  759. * @param p The pool to allocate from
  760. */
  761. APR_DECLARE(apr_status_t) apr_ipsubnet_create(apr_ipsubnet_t **ipsub,
  762. const char *ipstr,
  763. const char *mask_or_numbits,
  764. apr_pool_t *p);
  765. /**
  766. * Test the IP address in an apr_sockaddr_t against a pre-built ip-subnet
  767. * representation.
  768. * @param ipsub The ip-subnet representation
  769. * @param sa The socket address to test
  770. * @return non-zero if the socket address is within the subnet, 0 otherwise
  771. */
  772. APR_DECLARE(int) apr_ipsubnet_test(apr_ipsubnet_t *ipsub, apr_sockaddr_t *sa);
  773. #if APR_HAS_SO_ACCEPTFILTER || defined(DOXYGEN)
  774. /**
  775. * Set an OS level accept filter.
  776. * @param sock The socket to put the accept filter on.
  777. * @param name The accept filter
  778. * @param args Any extra args to the accept filter. Passing NULL here removes
  779. * the accept filter.
  780. * @bug name and args should have been declared as const char *, as they are in
  781. * APR 2.0
  782. */
  783. apr_status_t apr_socket_accept_filter(apr_socket_t *sock, char *name,
  784. char *args);
  785. #endif
  786. /**
  787. * Return the protocol of the socket.
  788. * @param sock The socket to query.
  789. * @param protocol The returned protocol (e.g., APR_PROTO_TCP).
  790. */
  791. APR_DECLARE(apr_status_t) apr_socket_protocol_get(apr_socket_t *sock,
  792. int *protocol);
  793. /**
  794. * Get the pool used by the socket.
  795. */
  796. APR_POOL_DECLARE_ACCESSOR(socket);
  797. /**
  798. * Set a socket to be inherited by child processes.
  799. */
  800. APR_DECLARE_INHERIT_SET(socket);
  801. /**
  802. * Unset a socket from being inherited by child processes.
  803. */
  804. APR_DECLARE_INHERIT_UNSET(socket);
  805. /**
  806. * Set socket permissions.
  807. */
  808. APR_PERMS_SET_IMPLEMENT(socket);
  809. /**
  810. * @defgroup apr_mcast IP Multicast
  811. * @{
  812. */
  813. /**
  814. * Join a Multicast Group
  815. * @param sock The socket to join a multicast group
  816. * @param join The address of the multicast group to join
  817. * @param iface Address of the interface to use. If NULL is passed, the
  818. * default multicast interface will be used. (OS Dependent)
  819. * @param source Source Address to accept transmissions from (non-NULL
  820. * implies Source-Specific Multicast)
  821. */
  822. APR_DECLARE(apr_status_t) apr_mcast_join(apr_socket_t *sock,
  823. apr_sockaddr_t *join,
  824. apr_sockaddr_t *iface,
  825. apr_sockaddr_t *source);
  826. /**
  827. * Leave a Multicast Group. All arguments must be the same as
  828. * apr_mcast_join.
  829. * @param sock The socket to leave a multicast group
  830. * @param addr The address of the multicast group to leave
  831. * @param iface Address of the interface to use. If NULL is passed, the
  832. * default multicast interface will be used. (OS Dependent)
  833. * @param source Source Address to accept transmissions from (non-NULL
  834. * implies Source-Specific Multicast)
  835. */
  836. APR_DECLARE(apr_status_t) apr_mcast_leave(apr_socket_t *sock,
  837. apr_sockaddr_t *addr,
  838. apr_sockaddr_t *iface,
  839. apr_sockaddr_t *source);
  840. /**
  841. * Set the Multicast Time to Live (ttl) for a multicast transmission.
  842. * @param sock The socket to set the multicast ttl
  843. * @param ttl Time to live to Assign. 0-255, default=1
  844. * @remark If the TTL is 0, packets will only be seen by sockets on
  845. * the local machine, and only when multicast loopback is enabled.
  846. */
  847. APR_DECLARE(apr_status_t) apr_mcast_hops(apr_socket_t *sock,
  848. apr_byte_t ttl);
  849. /**
  850. * Toggle IP Multicast Loopback
  851. * @param sock The socket to set multicast loopback
  852. * @param opt 0=disable, 1=enable
  853. */
  854. APR_DECLARE(apr_status_t) apr_mcast_loopback(apr_socket_t *sock,
  855. apr_byte_t opt);
  856. /**
  857. * Set the Interface to be used for outgoing Multicast Transmissions.
  858. * @param sock The socket to set the multicast interface on
  859. * @param iface Address of the interface to use for Multicast
  860. */
  861. APR_DECLARE(apr_status_t) apr_mcast_interface(apr_socket_t *sock,
  862. apr_sockaddr_t *iface);
  863. /** @} */
  864. /** @} */
  865. #ifdef __cplusplus
  866. }
  867. #endif
  868. #endif /* ! APR_NETWORK_IO_H */