fspr_network_io.h 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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 fspr_network_io.h
  20. * @brief APR Network library
  21. */
  22. #include "fspr.h"
  23. #include "fspr_pools.h"
  24. #include "fspr_file_io.h"
  25. #include "fspr_errno.h"
  26. #include "fspr_inherit.h"
  27. #if APR_HAVE_NETINET_IN_H
  28. #include <netinet/in.h>
  29. #endif
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif /* __cplusplus */
  33. /**
  34. * @defgroup fspr_network_io Network Routines
  35. * @ingroup APR
  36. * @{
  37. */
  38. #ifndef APR_MAX_SECS_TO_LINGER
  39. /** Maximum seconds to linger */
  40. #define APR_MAX_SECS_TO_LINGER 30
  41. #endif
  42. #ifndef APRMAXHOSTLEN
  43. /** Maximum hostname length */
  44. #define APRMAXHOSTLEN 256
  45. #endif
  46. #ifndef APR_ANYADDR
  47. /** Default 'any' address */
  48. #define APR_ANYADDR "0.0.0.0"
  49. #endif
  50. /**
  51. * @defgroup fspr_sockopt Socket option definitions
  52. * @{
  53. */
  54. #define APR_SO_LINGER 1 /**< Linger */
  55. #define APR_SO_KEEPALIVE 2 /**< Keepalive */
  56. #define APR_SO_DEBUG 4 /**< Debug */
  57. #define APR_SO_NONBLOCK 8 /**< Non-blocking IO */
  58. #define APR_SO_REUSEADDR 16 /**< Reuse addresses */
  59. #define APR_SO_SNDBUF 64 /**< Send buffer */
  60. #define APR_SO_RCVBUF 128 /**< Receive buffer */
  61. #define APR_SO_DISCONNECTED 256 /**< Disconnected */
  62. #define APR_TCP_NODELAY 512 /**< For SCTP sockets, this is mapped
  63. * to STCP_NODELAY internally.
  64. */
  65. #define APR_TCP_NOPUSH 1024 /**< No push */
  66. #define APR_RESET_NODELAY 2048 /**< This flag is ONLY set internally
  67. * when we set APR_TCP_NOPUSH with
  68. * APR_TCP_NODELAY set to tell us that
  69. * APR_TCP_NODELAY should be turned on
  70. * again when NOPUSH is turned off
  71. */
  72. #define APR_INCOMPLETE_READ 4096 /**< Set on non-blocking sockets
  73. * (timeout != 0) on which the
  74. * previous read() did not fill a buffer
  75. * completely. the next fspr_socket_recv()
  76. * will first call select()/poll() rather than
  77. * going straight into read(). (Can also
  78. * be set by an application to force a
  79. * select()/poll() call before the next
  80. * read, in cases where the app expects
  81. * that an immediate read would fail.)
  82. */
  83. #define APR_INCOMPLETE_WRITE 8192 /**< like APR_INCOMPLETE_READ, but for write
  84. * @see APR_INCOMPLETE_READ
  85. */
  86. #define APR_IPV6_V6ONLY 16384 /**< Don't accept IPv4 connections on an
  87. * IPv6 listening socket.
  88. */
  89. #define APR_TCP_DEFER_ACCEPT 32768 /**< Delay accepting of new connections
  90. * until data is available.
  91. * @see fspr_socket_accept_filter
  92. */
  93. /** @} */
  94. /** Define what type of socket shutdown should occur. */
  95. typedef enum {
  96. APR_SHUTDOWN_READ, /**< no longer allow read request */
  97. APR_SHUTDOWN_WRITE, /**< no longer allow write requests */
  98. APR_SHUTDOWN_READWRITE /**< no longer allow read or write requests */
  99. } fspr_shutdown_how_e;
  100. #define APR_IPV4_ADDR_OK 0x01 /**< @see fspr_sockaddr_info_get() */
  101. #define APR_IPV6_ADDR_OK 0x02 /**< @see fspr_sockaddr_info_get() */
  102. #if (!APR_HAVE_IN_ADDR)
  103. /**
  104. * We need to make sure we always have an in_addr type, so APR will just
  105. * define it ourselves, if the platform doesn't provide it.
  106. */
  107. struct in_addr {
  108. fspr_uint32_t s_addr; /**< storage to hold the IP# */
  109. };
  110. #endif
  111. /** @def APR_INADDR_NONE
  112. * Not all platforms have a real INADDR_NONE. This macro replaces
  113. * INADDR_NONE on all platforms.
  114. */
  115. #ifdef INADDR_NONE
  116. #define APR_INADDR_NONE INADDR_NONE
  117. #else
  118. #define APR_INADDR_NONE ((unsigned int) 0xffffffff)
  119. #endif
  120. /**
  121. * @def APR_INET
  122. * Not all platforms have these defined, so we'll define them here
  123. * The default values come from FreeBSD 4.1.1
  124. */
  125. #define APR_INET AF_INET
  126. /** @def APR_UNSPEC
  127. * Let the system decide which address family to use
  128. */
  129. #ifdef AF_UNSPEC
  130. #define APR_UNSPEC AF_UNSPEC
  131. #else
  132. #define APR_UNSPEC 0
  133. #endif
  134. #if APR_HAVE_IPV6
  135. /** @def APR_INET6
  136. * IPv6 Address Family. Not all platforms may have this defined.
  137. */
  138. #define APR_INET6 AF_INET6
  139. #endif
  140. /**
  141. * @defgroup IP_Proto IP Protocol Definitions for use when creating sockets
  142. * @{
  143. */
  144. #define APR_PROTO_TCP 6 /**< TCP */
  145. #define APR_PROTO_UDP 17 /**< UDP */
  146. #define APR_PROTO_SCTP 132 /**< SCTP */
  147. /** @} */
  148. /**
  149. * Enum to tell us if we're interested in remote or local socket
  150. */
  151. typedef enum {
  152. APR_LOCAL,
  153. APR_REMOTE
  154. } fspr_interface_e;
  155. /**
  156. * The specific declaration of inet_addr's ... some platforms fall back
  157. * inet_network (this is not good, but necessary)
  158. */
  159. #if APR_HAVE_INET_ADDR
  160. #define fspr_inet_addr inet_addr
  161. #elif APR_HAVE_INET_NETWORK /* only DGUX, as far as I know */
  162. /**
  163. * @warning
  164. * not generally safe... inet_network() and inet_addr() perform
  165. * different functions */
  166. #define fspr_inet_addr inet_network
  167. #endif
  168. /** A structure to represent sockets */
  169. typedef struct fspr_socket_t fspr_socket_t;
  170. /**
  171. * A structure to encapsulate headers and trailers for fspr_socket_sendfile
  172. */
  173. typedef struct fspr_hdtr_t fspr_hdtr_t;
  174. /** A structure to represent in_addr */
  175. typedef struct in_addr fspr_in_addr_t;
  176. /** A structure to represent an IP subnet */
  177. typedef struct fspr_ipsubnet_t fspr_ipsubnet_t;
  178. /** @remark use fspr_uint16_t just in case some system has a short that isn't 16 bits... */
  179. typedef fspr_uint16_t fspr_port_t;
  180. /** @remark It's defined here as I think it should all be platform safe...
  181. * @see fspr_sockaddr_t
  182. */
  183. typedef struct fspr_sockaddr_t fspr_sockaddr_t;
  184. /**
  185. * APRs socket address type, used to ensure protocol independence
  186. */
  187. struct fspr_sockaddr_t {
  188. /** The pool to use... */
  189. fspr_pool_t *pool;
  190. /** The hostname */
  191. char *hostname;
  192. /** Either a string of the port number or the service name for the port */
  193. char *servname;
  194. /** The numeric port */
  195. fspr_port_t port;
  196. /** The family */
  197. fspr_int32_t family;
  198. /** How big is the sockaddr we're using? */
  199. fspr_socklen_t salen;
  200. /** How big is the ip address structure we're using? */
  201. int ipaddr_len;
  202. /** How big should the address buffer be? 16 for v4 or 46 for v6
  203. * used in inet_ntop... */
  204. int addr_str_len;
  205. /** This points to the IP address structure within the appropriate
  206. * sockaddr structure. */
  207. void *ipaddr_ptr;
  208. /** If multiple addresses were found by fspr_sockaddr_info_get(), this
  209. * points to a representation of the next address. */
  210. fspr_sockaddr_t *next;
  211. /** Union of either IPv4 or IPv6 sockaddr. */
  212. union {
  213. /** IPv4 sockaddr structure */
  214. struct sockaddr_in sin;
  215. #if APR_HAVE_IPV6
  216. /** IPv6 sockaddr structure */
  217. struct sockaddr_in6 sin6;
  218. #endif
  219. #if APR_HAVE_SA_STORAGE
  220. /** Placeholder to ensure that the size of this union is not
  221. * dependent on whether APR_HAVE_IPV6 is defined. */
  222. struct sockaddr_storage sas;
  223. #endif
  224. } sa;
  225. };
  226. #if APR_HAS_SENDFILE
  227. /**
  228. * Support reusing the socket on platforms which support it (from disconnect,
  229. * specifically Win32.
  230. * @remark Optional flag passed into fspr_socket_sendfile()
  231. */
  232. #define APR_SENDFILE_DISCONNECT_SOCKET 1
  233. #endif
  234. /** A structure to encapsulate headers and trailers for fspr_socket_sendfile */
  235. struct fspr_hdtr_t {
  236. /** An iovec to store the headers sent before the file. */
  237. struct iovec* headers;
  238. /** number of headers in the iovec */
  239. int numheaders;
  240. /** An iovec to store the trailers sent after the file. */
  241. struct iovec* trailers;
  242. /** number of trailers in the iovec */
  243. int numtrailers;
  244. };
  245. /* function definitions */
  246. /**
  247. * Create a socket.
  248. * @param new_sock The new socket that has been set up.
  249. * @param family The address family of the socket (e.g., APR_INET).
  250. * @param type The type of the socket (e.g., SOCK_STREAM).
  251. * @param protocol The protocol of the socket (e.g., APR_PROTO_TCP).
  252. * @param cont The pool to use
  253. */
  254. APR_DECLARE(fspr_status_t) fspr_socket_create(fspr_socket_t **new_sock,
  255. int family, int type,
  256. int protocol,
  257. fspr_pool_t *cont);
  258. /**
  259. * Shutdown either reading, writing, or both sides of a socket.
  260. * @param thesocket The socket to close
  261. * @param how How to shutdown the socket. One of:
  262. * <PRE>
  263. * APR_SHUTDOWN_READ no longer allow read requests
  264. * APR_SHUTDOWN_WRITE no longer allow write requests
  265. * APR_SHUTDOWN_READWRITE no longer allow read or write requests
  266. * </PRE>
  267. * @see fspr_shutdown_how_e
  268. * @remark This does not actually close the socket descriptor, it just
  269. * controls which calls are still valid on the socket.
  270. */
  271. APR_DECLARE(fspr_status_t) fspr_socket_shutdown(fspr_socket_t *thesocket,
  272. fspr_shutdown_how_e how);
  273. /**
  274. * Close a socket.
  275. * @param thesocket The socket to close
  276. */
  277. APR_DECLARE(fspr_status_t) fspr_socket_close(fspr_socket_t *thesocket);
  278. /**
  279. * Bind the socket to its associated port
  280. * @param sock The socket to bind
  281. * @param sa The socket address to bind to
  282. * @remark This may be where we will find out if there is any other process
  283. * using the selected port.
  284. */
  285. APR_DECLARE(fspr_status_t) fspr_socket_bind(fspr_socket_t *sock,
  286. fspr_sockaddr_t *sa);
  287. /**
  288. * Listen to a bound socket for connections.
  289. * @param sock The socket to listen on
  290. * @param backlog The number of outstanding connections allowed in the sockets
  291. * listen queue. If this value is less than zero, the listen
  292. * queue size is set to zero.
  293. */
  294. APR_DECLARE(fspr_status_t) fspr_socket_listen(fspr_socket_t *sock,
  295. fspr_int32_t backlog);
  296. /**
  297. * Accept a new connection request
  298. * @param new_sock A copy of the socket that is connected to the socket that
  299. * made the connection request. This is the socket which should
  300. * be used for all future communication.
  301. * @param sock The socket we are listening on.
  302. * @param connection_pool The pool for the new socket.
  303. */
  304. APR_DECLARE(fspr_status_t) fspr_socket_accept(fspr_socket_t **new_sock,
  305. fspr_socket_t *sock,
  306. fspr_pool_t *connection_pool);
  307. /**
  308. * Issue a connection request to a socket either on the same machine
  309. * or a different one.
  310. * @param sock The socket we wish to use for our side of the connection
  311. * @param sa The address of the machine we wish to connect to.
  312. */
  313. APR_DECLARE(fspr_status_t) fspr_socket_connect(fspr_socket_t *sock,
  314. fspr_sockaddr_t *sa);
  315. /**
  316. * Create fspr_sockaddr_t from hostname, address family, and port.
  317. * @param sa The new fspr_sockaddr_t.
  318. * @param hostname The hostname or numeric address string to resolve/parse, or
  319. * NULL to build an address that corresponds to 0.0.0.0 or ::
  320. * @param family The address family to use, or APR_UNSPEC if the system should
  321. * decide.
  322. * @param port The port number.
  323. * @param flags Special processing flags:
  324. * <PRE>
  325. * APR_IPV4_ADDR_OK first query for IPv4 addresses; only look
  326. * for IPv6 addresses if the first query failed;
  327. * only valid if family is APR_UNSPEC and hostname
  328. * isn't NULL; mutually exclusive with
  329. * APR_IPV6_ADDR_OK
  330. * APR_IPV6_ADDR_OK first query for IPv6 addresses; only look
  331. * for IPv4 addresses if the first query failed;
  332. * only valid if family is APR_UNSPEC and hostname
  333. * isn't NULL and APR_HAVE_IPV6; mutually exclusive
  334. * with APR_IPV4_ADDR_OK
  335. * </PRE>
  336. * @param p The pool for the fspr_sockaddr_t and associated storage.
  337. */
  338. APR_DECLARE(fspr_status_t) fspr_sockaddr_info_get(fspr_sockaddr_t **sa,
  339. const char *hostname,
  340. fspr_int32_t family,
  341. fspr_port_t port,
  342. fspr_int32_t flags,
  343. fspr_pool_t *p);
  344. /**
  345. * Look up the host name from an fspr_sockaddr_t.
  346. * @param hostname The hostname.
  347. * @param sa The fspr_sockaddr_t.
  348. * @param flags Special processing flags.
  349. */
  350. APR_DECLARE(fspr_status_t) fspr_getnameinfo(char **hostname,
  351. fspr_sockaddr_t *sa,
  352. fspr_int32_t flags);
  353. /**
  354. * Parse hostname/IP address with scope id and port.
  355. *
  356. * Any of the following strings are accepted:
  357. * 8080 (just the port number)
  358. * www.apache.org (just the hostname)
  359. * www.apache.org:8080 (hostname and port number)
  360. * [fe80::1]:80 (IPv6 numeric address string only)
  361. * [fe80::1%eth0] (IPv6 numeric address string and scope id)
  362. *
  363. * Invalid strings:
  364. * (empty string)
  365. * [abc] (not valid IPv6 numeric address string)
  366. * abc:65536 (invalid port number)
  367. *
  368. * @param addr The new buffer containing just the hostname. On output, *addr
  369. * will be NULL if no hostname/IP address was specfied.
  370. * @param scope_id The new buffer containing just the scope id. On output,
  371. * *scope_id will be NULL if no scope id was specified.
  372. * @param port The port number. On output, *port will be 0 if no port was
  373. * specified.
  374. * ### FIXME: 0 is a legal port (per RFC 1700). this should
  375. * ### return something besides zero if the port is missing.
  376. * @param str The input string to be parsed.
  377. * @param p The pool from which *addr and *scope_id are allocated.
  378. * @remark If scope id shouldn't be allowed, check for scope_id != NULL in
  379. * addition to checking the return code. If addr/hostname should be
  380. * required, check for addr == NULL in addition to checking the
  381. * return code.
  382. */
  383. APR_DECLARE(fspr_status_t) fspr_parse_addr_port(char **addr,
  384. char **scope_id,
  385. fspr_port_t *port,
  386. const char *str,
  387. fspr_pool_t *p);
  388. /**
  389. * Get name of the current machine
  390. * @param buf A buffer to store the hostname in.
  391. * @param len The maximum length of the hostname that can be stored in the
  392. * buffer provided. The suggested length is APRMAXHOSTLEN + 1.
  393. * @param cont The pool to use.
  394. * @remark If the buffer was not large enough, an error will be returned.
  395. */
  396. APR_DECLARE(fspr_status_t) fspr_gethostname(char *buf, int len, fspr_pool_t *cont);
  397. /**
  398. * Return the data associated with the current socket
  399. * @param data The user data associated with the socket.
  400. * @param key The key to associate with the user data.
  401. * @param sock The currently open socket.
  402. */
  403. APR_DECLARE(fspr_status_t) fspr_socket_data_get(void **data, const char *key,
  404. fspr_socket_t *sock);
  405. /**
  406. * Set the data associated with the current socket.
  407. * @param sock The currently open socket.
  408. * @param data The user data to associate with the socket.
  409. * @param key The key to associate with the data.
  410. * @param cleanup The cleanup to call when the socket is destroyed.
  411. */
  412. APR_DECLARE(fspr_status_t) fspr_socket_data_set(fspr_socket_t *sock, void *data,
  413. const char *key,
  414. fspr_status_t (*cleanup)(void*));
  415. /**
  416. * Send data over a network.
  417. * @param sock The socket to send the data over.
  418. * @param buf The buffer which contains the data to be sent.
  419. * @param len On entry, the number of bytes to send; on exit, the number
  420. * of bytes sent.
  421. * @remark
  422. * <PRE>
  423. * This functions acts like a blocking write by default. To change
  424. * this behavior, use fspr_socket_timeout_set() or the APR_SO_NONBLOCK
  425. * socket option.
  426. *
  427. * It is possible for both bytes to be sent and an error to be returned.
  428. *
  429. * APR_EINTR is never returned.
  430. * </PRE>
  431. */
  432. APR_DECLARE(fspr_status_t) fspr_socket_send(fspr_socket_t *sock, const char *buf,
  433. fspr_size_t *len);
  434. /**
  435. * Send multiple packets of data over a network.
  436. * @param sock The socket to send the data over.
  437. * @param vec The array of iovec structs containing the data to send
  438. * @param nvec The number of iovec structs in the array
  439. * @param len Receives the number of bytes actually written
  440. * @remark
  441. * <PRE>
  442. * This functions acts like a blocking write by default. To change
  443. * this behavior, use fspr_socket_timeout_set() or the APR_SO_NONBLOCK
  444. * socket option.
  445. * The number of bytes actually sent is stored in argument 3.
  446. *
  447. * It is possible for both bytes to be sent and an error to be returned.
  448. *
  449. * APR_EINTR is never returned.
  450. * </PRE>
  451. */
  452. APR_DECLARE(fspr_status_t) fspr_socket_sendv(fspr_socket_t *sock,
  453. const struct iovec *vec,
  454. fspr_int32_t nvec, fspr_size_t *len);
  455. /**
  456. * @param sock The socket to send from
  457. * @param where The fspr_sockaddr_t describing where to send the data
  458. * @param flags The flags to use
  459. * @param buf The data to send
  460. * @param len The length of the data to send
  461. */
  462. APR_DECLARE(fspr_status_t) fspr_socket_sendto(fspr_socket_t *sock,
  463. fspr_sockaddr_t *where,
  464. fspr_int32_t flags, const char *buf,
  465. fspr_size_t *len);
  466. /**
  467. * @param from The fspr_sockaddr_t to fill in the recipient info
  468. * @param sock The socket to use
  469. * @param flags The flags to use
  470. * @param buf The buffer to use
  471. * @param len The length of the available buffer
  472. */
  473. APR_DECLARE(fspr_status_t) fspr_socket_recvfrom(fspr_sockaddr_t *from,
  474. fspr_socket_t *sock,
  475. fspr_int32_t flags, char *buf,
  476. fspr_size_t *len);
  477. #if APR_HAS_SENDFILE || defined(DOXYGEN)
  478. /**
  479. * Send a file from an open file descriptor to a socket, along with
  480. * optional headers and trailers
  481. * @param sock The socket to which we're writing
  482. * @param file The open file from which to read
  483. * @param hdtr A structure containing the headers and trailers to send
  484. * @param offset Offset into the file where we should begin writing
  485. * @param len (input) - Number of bytes to send from the file
  486. * (output) - Number of bytes actually sent,
  487. * including headers, file, and trailers
  488. * @param flags APR flags that are mapped to OS specific flags
  489. * @remark This functions acts like a blocking write by default. To change
  490. * this behavior, use fspr_socket_timeout_set() or the
  491. * APR_SO_NONBLOCK socket option.
  492. * The number of bytes actually sent is stored in the len parameter.
  493. * The offset parameter is passed by reference for no reason; its
  494. * value will never be modified by the fspr_socket_sendfile() function.
  495. */
  496. APR_DECLARE(fspr_status_t) fspr_socket_sendfile(fspr_socket_t *sock,
  497. fspr_file_t *file,
  498. fspr_hdtr_t *hdtr,
  499. fspr_off_t *offset,
  500. fspr_size_t *len,
  501. fspr_int32_t flags);
  502. #endif /* APR_HAS_SENDFILE */
  503. /**
  504. * Read data from a network.
  505. * @param sock The socket to read the data from.
  506. * @param buf The buffer to store the data in.
  507. * @param len On entry, the number of bytes to receive; on exit, the number
  508. * of bytes received.
  509. * @remark
  510. * <PRE>
  511. * This functions acts like a blocking read by default. To change
  512. * this behavior, use fspr_socket_timeout_set() or the APR_SO_NONBLOCK
  513. * socket option.
  514. * The number of bytes actually received is stored in argument 3.
  515. *
  516. * It is possible for both bytes to be received and an APR_EOF or
  517. * other error to be returned.
  518. *
  519. * APR_EINTR is never returned.
  520. * </PRE>
  521. */
  522. APR_DECLARE(fspr_status_t) fspr_socket_recv(fspr_socket_t *sock,
  523. char *buf, fspr_size_t *len);
  524. /**
  525. * Setup socket options for the specified socket
  526. * @param sock The socket to set up.
  527. * @param opt The option we would like to configure. One of:
  528. * <PRE>
  529. * APR_SO_DEBUG -- turn on debugging information
  530. * APR_SO_KEEPALIVE -- keep connections active
  531. * APR_SO_LINGER -- lingers on close if data is present
  532. * APR_SO_NONBLOCK -- Turns blocking on/off for socket
  533. * When this option is enabled, use
  534. * the APR_STATUS_IS_EAGAIN() macro to
  535. * see if a send or receive function
  536. * could not transfer data without
  537. * blocking.
  538. * APR_SO_REUSEADDR -- The rules used in validating addresses
  539. * supplied to bind should allow reuse
  540. * of local addresses.
  541. * APR_SO_SNDBUF -- Set the SendBufferSize
  542. * APR_SO_RCVBUF -- Set the ReceiveBufferSize
  543. * </PRE>
  544. * @param on Value for the option.
  545. */
  546. APR_DECLARE(fspr_status_t) fspr_socket_opt_set(fspr_socket_t *sock,
  547. fspr_int32_t opt, fspr_int32_t on);
  548. /**
  549. * Setup socket timeout for the specified socket
  550. * @param sock The socket to set up.
  551. * @param t Value for the timeout.
  552. * <PRE>
  553. * t > 0 -- read and write calls return APR_TIMEUP if specified time
  554. * elapsess with no data read or written
  555. * t == 0 -- read and write calls never block
  556. * t < 0 -- read and write calls block
  557. * </PRE>
  558. */
  559. APR_DECLARE(fspr_status_t) fspr_socket_timeout_set(fspr_socket_t *sock,
  560. fspr_interval_time_t t);
  561. /**
  562. * Query socket options for the specified socket
  563. * @param sock The socket to query
  564. * @param opt The option we would like to query. One of:
  565. * <PRE>
  566. * APR_SO_DEBUG -- turn on debugging information
  567. * APR_SO_KEEPALIVE -- keep connections active
  568. * APR_SO_LINGER -- lingers on close if data is present
  569. * APR_SO_NONBLOCK -- Turns blocking on/off for socket
  570. * APR_SO_REUSEADDR -- The rules used in validating addresses
  571. * supplied to bind should allow reuse
  572. * of local addresses.
  573. * APR_SO_SNDBUF -- Set the SendBufferSize
  574. * APR_SO_RCVBUF -- Set the ReceiveBufferSize
  575. * APR_SO_DISCONNECTED -- Query the disconnected state of the socket.
  576. * (Currently only used on Windows)
  577. * </PRE>
  578. * @param on Socket option returned on the call.
  579. */
  580. APR_DECLARE(fspr_status_t) fspr_socket_opt_get(fspr_socket_t *sock,
  581. fspr_int32_t opt, fspr_int32_t *on);
  582. /**
  583. * Get Socket fd for the socket passed
  584. * @param sock The socket to quesry for the socket fd
  585. */
  586. APR_DECLARE(int) fspr_socket_fd_get(fspr_socket_t *sock);
  587. /**
  588. * Query socket timeout for the specified socket
  589. * @param sock The socket to query
  590. * @param t Socket timeout returned from the query.
  591. */
  592. APR_DECLARE(fspr_status_t) fspr_socket_timeout_get(fspr_socket_t *sock,
  593. fspr_interval_time_t *t);
  594. /**
  595. * Query the specified socket if at the OOB/Urgent data mark
  596. * @param sock The socket to query
  597. * @param atmark Is set to true if socket is at the OOB/urgent mark,
  598. * otherwise is set to false.
  599. */
  600. APR_DECLARE(fspr_status_t) fspr_socket_atmark(fspr_socket_t *sock,
  601. int *atmark);
  602. /**
  603. * Return an fspr_sockaddr_t from an fspr_socket_t
  604. * @param sa The returned fspr_sockaddr_t.
  605. * @param which Which interface do we want the fspr_sockaddr_t for?
  606. * @param sock The socket to use
  607. */
  608. APR_DECLARE(fspr_status_t) fspr_socket_addr_get(fspr_sockaddr_t **sa,
  609. fspr_interface_e which,
  610. fspr_socket_t *sock);
  611. /**
  612. * Return the IP address (in numeric address string format) in
  613. * an APR socket address. APR will allocate storage for the IP address
  614. * string from the pool of the fspr_sockaddr_t.
  615. * @param addr The IP address.
  616. * @param sockaddr The socket address to reference.
  617. */
  618. APR_DECLARE(fspr_status_t) fspr_sockaddr_ip_get(char **addr,
  619. fspr_sockaddr_t *sockaddr);
  620. /**
  621. * See if the IP addresses in two APR socket addresses are
  622. * equivalent. Appropriate logic is present for comparing
  623. * IPv4-mapped IPv6 addresses with IPv4 addresses.
  624. *
  625. * @param addr1 One of the APR socket addresses.
  626. * @param addr2 The other APR socket address.
  627. * @remark The return value will be non-zero if the addresses
  628. * are equivalent.
  629. */
  630. APR_DECLARE(int) fspr_sockaddr_equal(const fspr_sockaddr_t *addr1,
  631. const fspr_sockaddr_t *addr2);
  632. /**
  633. * Return the type of the socket.
  634. * @param sock The socket to query.
  635. * @param type The returned type (e.g., SOCK_STREAM).
  636. */
  637. APR_DECLARE(fspr_status_t) fspr_socket_type_get(fspr_socket_t *sock,
  638. int *type);
  639. /**
  640. * Given an fspr_sockaddr_t and a service name, set the port for the service
  641. * @param sockaddr The fspr_sockaddr_t that will have its port set
  642. * @param servname The name of the service you wish to use
  643. */
  644. APR_DECLARE(fspr_status_t) fspr_getservbyname(fspr_sockaddr_t *sockaddr,
  645. const char *servname);
  646. /**
  647. * Build an ip-subnet representation from an IP address and optional netmask or
  648. * number-of-bits.
  649. * @param ipsub The new ip-subnet representation
  650. * @param ipstr The input IP address string
  651. * @param mask_or_numbits The input netmask or number-of-bits string, or NULL
  652. * @param p The pool to allocate from
  653. */
  654. APR_DECLARE(fspr_status_t) fspr_ipsubnet_create(fspr_ipsubnet_t **ipsub,
  655. const char *ipstr,
  656. const char *mask_or_numbits,
  657. fspr_pool_t *p);
  658. /**
  659. * Test the IP address in an fspr_sockaddr_t against a pre-built ip-subnet
  660. * representation.
  661. * @param ipsub The ip-subnet representation
  662. * @param sa The socket address to test
  663. * @return non-zero if the socket address is within the subnet, 0 otherwise
  664. */
  665. APR_DECLARE(int) fspr_ipsubnet_test(fspr_ipsubnet_t *ipsub, fspr_sockaddr_t *sa);
  666. #if APR_HAS_SO_ACCEPTFILTER || defined(DOXYGEN)
  667. /**
  668. * Set an OS level accept filter.
  669. * @param sock The socket to put the accept filter on.
  670. * @param name The accept filter
  671. * @param args Any extra args to the accept filter. Passing NULL here removes
  672. * the accept filter.
  673. */
  674. fspr_status_t fspr_socket_accept_filter(fspr_socket_t *sock, char *name,
  675. char *args);
  676. #endif
  677. /**
  678. * Return the protocol of the socket.
  679. * @param sock The socket to query.
  680. * @param protocol The returned protocol (e.g., APR_PROTO_TCP).
  681. */
  682. APR_DECLARE(fspr_status_t) fspr_socket_protocol_get(fspr_socket_t *sock,
  683. int *protocol);
  684. /**
  685. * Get the pool used by the socket.
  686. */
  687. APR_POOL_DECLARE_ACCESSOR(socket);
  688. /**
  689. * Set a socket to be inherited by child processes.
  690. */
  691. APR_DECLARE_INHERIT_SET(socket);
  692. /**
  693. * Unset a socket from being inherited by child processes.
  694. */
  695. APR_DECLARE_INHERIT_UNSET(socket);
  696. /**
  697. * @defgroup fspr_mcast IP Multicast
  698. * @{
  699. */
  700. /**
  701. * Join a Multicast Group
  702. * @param sock The socket to join a multicast group
  703. * @param join The address of the multicast group to join
  704. * @param iface Address of the interface to use. If NULL is passed, the
  705. * default multicast interface will be used. (OS Dependent)
  706. * @param source Source Address to accept transmissions from (non-NULL
  707. * implies Source-Specific Multicast)
  708. */
  709. APR_DECLARE(fspr_status_t) fspr_mcast_join(fspr_socket_t *sock,
  710. fspr_sockaddr_t *join,
  711. fspr_sockaddr_t *iface,
  712. fspr_sockaddr_t *source);
  713. /**
  714. * Leave a Multicast Group. All arguments must be the same as
  715. * fspr_mcast_join.
  716. * @param sock The socket to leave a multicast group
  717. * @param addr The address of the multicast group to leave
  718. * @param iface Address of the interface to use. If NULL is passed, the
  719. * default multicast interface will be used. (OS Dependent)
  720. * @param source Source Address to accept transmissions from (non-NULL
  721. * implies Source-Specific Multicast)
  722. */
  723. APR_DECLARE(fspr_status_t) fspr_mcast_leave(fspr_socket_t *sock,
  724. fspr_sockaddr_t *addr,
  725. fspr_sockaddr_t *iface,
  726. fspr_sockaddr_t *source);
  727. /**
  728. * Set the Multicast Time to Live (ttl) for a multicast transmission.
  729. * @param sock The socket to set the multicast ttl
  730. * @param ttl Time to live to Assign. 0-255, default=1
  731. * @remark If the TTL is 0, packets will only be seen by sockets on
  732. * the local machine, and only when multicast loopback is enabled.
  733. */
  734. APR_DECLARE(fspr_status_t) fspr_mcast_hops(fspr_socket_t *sock,
  735. fspr_byte_t ttl);
  736. /**
  737. * Toggle IP Multicast Loopback
  738. * @param sock The socket to set multicast loopback
  739. * @param opt 0=disable, 1=enable
  740. */
  741. APR_DECLARE(fspr_status_t) fspr_mcast_loopback(fspr_socket_t *sock,
  742. fspr_byte_t opt);
  743. /**
  744. * Set the Interface to be used for outgoing Multicast Transmissions.
  745. * @param sock The socket to set the multicast interface on
  746. * @param iface Address of the interface to use for Multicast
  747. */
  748. APR_DECLARE(fspr_status_t) fspr_mcast_interface(fspr_socket_t *sock,
  749. fspr_sockaddr_t *iface);
  750. /** @} */
  751. /** @} */
  752. #ifdef __cplusplus
  753. }
  754. #endif
  755. #endif /* ! APR_NETWORK_IO_H */