PageRenderTime 52ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/isc/include/isc/socket.h

https://bitbucket.org/Gradwell/bind9-clone
C Header | 925 lines | 176 code | 62 blank | 687 comment | 0 complexity | b82746e0c7f09cc0944523a25572a459 MD5 | raw file
Possible License(s): ISC
  1. /*
  2. * Copyright (C) 2004-2008 Internet Systems Consortium, Inc. ("ISC")
  3. * Copyright (C) 1998-2002 Internet Software Consortium.
  4. *
  5. * Permission to use, copy, modify, and/or distribute this software for any
  6. * purpose with or without fee is hereby granted, provided that the above
  7. * copyright notice and this permission notice appear in all copies.
  8. *
  9. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  10. * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  11. * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  12. * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  13. * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  14. * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  15. * PERFORMANCE OF THIS SOFTWARE.
  16. */
  17. /* $Id: socket.h,v 1.72.128.12 2008/09/04 07:58:07 marka Exp $ */
  18. #ifndef ISC_SOCKET_H
  19. #define ISC_SOCKET_H 1
  20. /*****
  21. ***** Module Info
  22. *****/
  23. /*! \file isc/socket.h
  24. * \brief Provides TCP and UDP sockets for network I/O. The sockets are event
  25. * sources in the task system.
  26. *
  27. * When I/O completes, a completion event for the socket is posted to the
  28. * event queue of the task which requested the I/O.
  29. *
  30. * \li MP:
  31. * The module ensures appropriate synchronization of data structures it
  32. * creates and manipulates.
  33. * Clients of this module must not be holding a socket's task's lock when
  34. * making a call that affects that socket. Failure to follow this rule
  35. * can result in deadlock.
  36. * The caller must ensure that isc_socketmgr_destroy() is called only
  37. * once for a given manager.
  38. *
  39. * \li Reliability:
  40. * No anticipated impact.
  41. *
  42. * \li Resources:
  43. * TBS
  44. *
  45. * \li Security:
  46. * No anticipated impact.
  47. *
  48. * \li Standards:
  49. * None.
  50. */
  51. /***
  52. *** Imports
  53. ***/
  54. #include <isc/lang.h>
  55. #include <isc/types.h>
  56. #include <isc/event.h>
  57. #include <isc/eventclass.h>
  58. #include <isc/time.h>
  59. #include <isc/region.h>
  60. #include <isc/sockaddr.h>
  61. #include <isc/xml.h>
  62. ISC_LANG_BEGINDECLS
  63. /***
  64. *** Constants
  65. ***/
  66. /*%
  67. * Maximum number of buffers in a scatter/gather read/write. The operating
  68. * system in use must support at least this number (plus one on some.)
  69. */
  70. #define ISC_SOCKET_MAXSCATTERGATHER 8
  71. /*%
  72. * In isc_socket_bind() set socket option SO_REUSEADDR prior to calling
  73. * bind() if a non zero port is specified (AF_INET and AF_INET6).
  74. */
  75. #define ISC_SOCKET_REUSEADDRESS 0x01U
  76. /***
  77. *** Types
  78. ***/
  79. struct isc_socketevent {
  80. ISC_EVENT_COMMON(isc_socketevent_t);
  81. isc_result_t result; /*%< OK, EOF, whatever else */
  82. unsigned int minimum; /*%< minimum i/o for event */
  83. unsigned int n; /*%< bytes read or written */
  84. unsigned int offset; /*%< offset into buffer list */
  85. isc_region_t region; /*%< for single-buffer i/o */
  86. isc_bufferlist_t bufferlist; /*%< list of buffers */
  87. isc_sockaddr_t address; /*%< source address */
  88. isc_time_t timestamp; /*%< timestamp of packet recv */
  89. struct in6_pktinfo pktinfo; /*%< ipv6 pktinfo */
  90. isc_uint32_t attributes; /*%< see below */
  91. isc_eventdestructor_t destroy; /*%< original destructor */
  92. };
  93. typedef struct isc_socket_newconnev isc_socket_newconnev_t;
  94. struct isc_socket_newconnev {
  95. ISC_EVENT_COMMON(isc_socket_newconnev_t);
  96. isc_socket_t * newsocket;
  97. isc_result_t result; /*%< OK, EOF, whatever else */
  98. isc_sockaddr_t address; /*%< source address */
  99. };
  100. typedef struct isc_socket_connev isc_socket_connev_t;
  101. struct isc_socket_connev {
  102. ISC_EVENT_COMMON(isc_socket_connev_t);
  103. isc_result_t result; /*%< OK, EOF, whatever else */
  104. };
  105. /*@{*/
  106. /*!
  107. * _ATTACHED: Internal use only.
  108. * _TRUNC: Packet was truncated on receive.
  109. * _CTRUNC: Packet control information was truncated. This can
  110. * indicate that the packet is not complete, even though
  111. * all the data is valid.
  112. * _TIMESTAMP: The timestamp member is valid.
  113. * _PKTINFO: The pktinfo member is valid.
  114. * _MULTICAST: The UDP packet was received via a multicast transmission.
  115. */
  116. #define ISC_SOCKEVENTATTR_ATTACHED 0x80000000U /* internal */
  117. #define ISC_SOCKEVENTATTR_TRUNC 0x00800000U /* public */
  118. #define ISC_SOCKEVENTATTR_CTRUNC 0x00400000U /* public */
  119. #define ISC_SOCKEVENTATTR_TIMESTAMP 0x00200000U /* public */
  120. #define ISC_SOCKEVENTATTR_PKTINFO 0x00100000U /* public */
  121. #define ISC_SOCKEVENTATTR_MULTICAST 0x00080000U /* public */
  122. /*@}*/
  123. #define ISC_SOCKEVENT_ANYEVENT (0)
  124. #define ISC_SOCKEVENT_RECVDONE (ISC_EVENTCLASS_SOCKET + 1)
  125. #define ISC_SOCKEVENT_SENDDONE (ISC_EVENTCLASS_SOCKET + 2)
  126. #define ISC_SOCKEVENT_NEWCONN (ISC_EVENTCLASS_SOCKET + 3)
  127. #define ISC_SOCKEVENT_CONNECT (ISC_EVENTCLASS_SOCKET + 4)
  128. /*
  129. * Internal events.
  130. */
  131. #define ISC_SOCKEVENT_INTR (ISC_EVENTCLASS_SOCKET + 256)
  132. #define ISC_SOCKEVENT_INTW (ISC_EVENTCLASS_SOCKET + 257)
  133. typedef enum {
  134. isc_sockettype_udp = 1,
  135. isc_sockettype_tcp = 2,
  136. isc_sockettype_unix = 3,
  137. isc_sockettype_fdwatch = 4
  138. } isc_sockettype_t;
  139. /*@{*/
  140. /*!
  141. * How a socket should be shutdown in isc_socket_shutdown() calls.
  142. */
  143. #define ISC_SOCKSHUT_RECV 0x00000001 /*%< close read side */
  144. #define ISC_SOCKSHUT_SEND 0x00000002 /*%< close write side */
  145. #define ISC_SOCKSHUT_ALL 0x00000003 /*%< close them all */
  146. /*@}*/
  147. /*@{*/
  148. /*!
  149. * What I/O events to cancel in isc_socket_cancel() calls.
  150. */
  151. #define ISC_SOCKCANCEL_RECV 0x00000001 /*%< cancel recv */
  152. #define ISC_SOCKCANCEL_SEND 0x00000002 /*%< cancel send */
  153. #define ISC_SOCKCANCEL_ACCEPT 0x00000004 /*%< cancel accept */
  154. #define ISC_SOCKCANCEL_CONNECT 0x00000008 /*%< cancel connect */
  155. #define ISC_SOCKCANCEL_ALL 0x0000000f /*%< cancel everything */
  156. /*@}*/
  157. /*@{*/
  158. /*!
  159. * Flags for isc_socket_send() and isc_socket_recv() calls.
  160. */
  161. #define ISC_SOCKFLAG_IMMEDIATE 0x00000001 /*%< send event only if needed */
  162. #define ISC_SOCKFLAG_NORETRY 0x00000002 /*%< drop failed UDP sends */
  163. /*@}*/
  164. /*@{*/
  165. /*!
  166. * Flags for fdwatchcreate.
  167. */
  168. #define ISC_SOCKFDWATCH_READ 0x00000001 /*%< watch for readable */
  169. #define ISC_SOCKFDWATCH_WRITE 0x00000002 /*%< watch for writable */
  170. /*@}*/
  171. /***
  172. *** Socket and Socket Manager Functions
  173. ***
  174. *** Note: all Ensures conditions apply only if the result is success for
  175. *** those functions which return an isc_result.
  176. ***/
  177. isc_result_t
  178. isc_socket_fdwatchcreate(isc_socketmgr_t *manager,
  179. int fd,
  180. int flags,
  181. isc_sockfdwatch_t callback,
  182. void *cbarg,
  183. isc_task_t *task,
  184. isc_socket_t **socketp);
  185. /*%<
  186. * Create a new file descriptor watch socket managed by 'manager'.
  187. *
  188. * Note:
  189. *
  190. *\li 'fd' is the already-opened file descriptor.
  191. *\li This function is not available on Windows.
  192. *\li The callback function is called "in-line" - this means the function
  193. * needs to return as fast as possible, as all other I/O will be suspended
  194. * until the callback completes.
  195. *
  196. * Requires:
  197. *
  198. *\li 'manager' is a valid manager
  199. *
  200. *\li 'socketp' is a valid pointer, and *socketp == NULL
  201. *
  202. *\li 'fd' be opened.
  203. *
  204. * Ensures:
  205. *
  206. * '*socketp' is attached to the newly created fdwatch socket
  207. *
  208. * Returns:
  209. *
  210. *\li #ISC_R_SUCCESS
  211. *\li #ISC_R_NOMEMORY
  212. *\li #ISC_R_NORESOURCES
  213. *\li #ISC_R_UNEXPECTED
  214. */
  215. isc_result_t
  216. isc_socket_create(isc_socketmgr_t *manager,
  217. int pf,
  218. isc_sockettype_t type,
  219. isc_socket_t **socketp);
  220. /*%<
  221. * Create a new 'type' socket managed by 'manager'.
  222. *
  223. * For isc_sockettype_fdwatch sockets you should use isc_socket_fdwatchcreate()
  224. * rather than isc_socket_create().
  225. *
  226. * Note:
  227. *
  228. *\li 'pf' is the desired protocol family, e.g. PF_INET or PF_INET6.
  229. *
  230. * Requires:
  231. *
  232. *\li 'manager' is a valid manager
  233. *
  234. *\li 'socketp' is a valid pointer, and *socketp == NULL
  235. *
  236. *\li 'type' is not isc_sockettype_fdwatch
  237. *
  238. * Ensures:
  239. *
  240. * '*socketp' is attached to the newly created socket
  241. *
  242. * Returns:
  243. *
  244. *\li #ISC_R_SUCCESS
  245. *\li #ISC_R_NOMEMORY
  246. *\li #ISC_R_NORESOURCES
  247. *\li #ISC_R_UNEXPECTED
  248. */
  249. void
  250. isc_socket_cancel(isc_socket_t *sock, isc_task_t *task,
  251. unsigned int how);
  252. /*%<
  253. * Cancel pending I/O of the type specified by "how".
  254. *
  255. * Note: if "task" is NULL, then the cancel applies to all tasks using the
  256. * socket.
  257. *
  258. * Requires:
  259. *
  260. * \li "socket" is a valid socket
  261. *
  262. * \li "task" is NULL or a valid task
  263. *
  264. * "how" is a bitmask describing the type of cancelation to perform.
  265. * The type ISC_SOCKCANCEL_ALL will cancel all pending I/O on this
  266. * socket.
  267. *
  268. * \li ISC_SOCKCANCEL_RECV:
  269. * Cancel pending isc_socket_recv() calls.
  270. *
  271. * \li ISC_SOCKCANCEL_SEND:
  272. * Cancel pending isc_socket_send() and isc_socket_sendto() calls.
  273. *
  274. * \li ISC_SOCKCANCEL_ACCEPT:
  275. * Cancel pending isc_socket_accept() calls.
  276. *
  277. * \li ISC_SOCKCANCEL_CONNECT:
  278. * Cancel pending isc_socket_connect() call.
  279. */
  280. void
  281. isc_socket_shutdown(isc_socket_t *sock, unsigned int how);
  282. /*%<
  283. * Shutdown 'socket' according to 'how'.
  284. *
  285. * Requires:
  286. *
  287. * \li 'socket' is a valid socket.
  288. *
  289. * \li 'task' is NULL or is a valid task.
  290. *
  291. * \li If 'how' is 'ISC_SOCKSHUT_RECV' or 'ISC_SOCKSHUT_ALL' then
  292. *
  293. * The read queue must be empty.
  294. *
  295. * No further read requests may be made.
  296. *
  297. * \li If 'how' is 'ISC_SOCKSHUT_SEND' or 'ISC_SOCKSHUT_ALL' then
  298. *
  299. * The write queue must be empty.
  300. *
  301. * No further write requests may be made.
  302. */
  303. void
  304. isc_socket_attach(isc_socket_t *sock, isc_socket_t **socketp);
  305. /*%<
  306. * Attach *socketp to socket.
  307. *
  308. * Requires:
  309. *
  310. * \li 'socket' is a valid socket.
  311. *
  312. * \li 'socketp' points to a NULL socket.
  313. *
  314. * Ensures:
  315. *
  316. * \li *socketp is attached to socket.
  317. */
  318. void
  319. isc_socket_detach(isc_socket_t **socketp);
  320. /*%<
  321. * Detach *socketp from its socket.
  322. *
  323. * Requires:
  324. *
  325. * \li 'socketp' points to a valid socket.
  326. *
  327. * \li If '*socketp' is the last reference to the socket,
  328. * then:
  329. *
  330. * There must be no pending I/O requests.
  331. *
  332. * Ensures:
  333. *
  334. * \li *socketp is NULL.
  335. *
  336. * \li If '*socketp' is the last reference to the socket,
  337. * then:
  338. *
  339. * The socket will be shutdown (both reading and writing)
  340. * for all tasks.
  341. *
  342. * All resources used by the socket have been freed
  343. */
  344. isc_result_t
  345. isc_socket_open(isc_socket_t *sock);
  346. /*%<
  347. * Open a new socket file descriptor of the given socket structure. It simply
  348. * opens a new descriptor; all of the other parameters including the socket
  349. * type are inherited from the existing socket. This function is provided to
  350. * avoid overhead of destroying and creating sockets when many short-lived
  351. * sockets are frequently opened and closed. When the efficiency is not an
  352. * issue, it should be safer to detach the unused socket and re-create a new
  353. * one. This optimization may not be available for some systems, in which
  354. * case this function will return ISC_R_NOTIMPLEMENTED and must not be used.
  355. *
  356. * isc_socket_open() should not be called on sockets created by
  357. * isc_socket_fdwatchcreate().
  358. *
  359. * Requires:
  360. *
  361. * \li there must be no other reference to this socket.
  362. *
  363. * \li 'socket' is a valid and previously closed by isc_socket_close()
  364. *
  365. * \li 'sock->type' is not isc_sockettype_fdwatch
  366. *
  367. * Returns:
  368. * Same as isc_socket_create().
  369. * \li ISC_R_NOTIMPLEMENTED
  370. */
  371. isc_result_t
  372. isc_socket_close(isc_socket_t *sock);
  373. /*%<
  374. * Close a socket file descriptor of the given socket structure. This function
  375. * is provided as an alternative to destroying an unused socket when overhead
  376. * destroying/re-creating sockets can be significant, and is expected to be
  377. * used with isc_socket_open(). This optimization may not be available for some
  378. * systems, in which case this function will return ISC_R_NOTIMPLEMENTED and
  379. * must not be used.
  380. *
  381. * isc_socket_close() should not be called on sockets created by
  382. * isc_socket_fdwatchcreate().
  383. *
  384. * Requires:
  385. *
  386. * \li The socket must have a valid descriptor.
  387. *
  388. * \li There must be no other reference to this socket.
  389. *
  390. * \li There must be no pending I/O requests.
  391. *
  392. * \li 'sock->type' is not isc_sockettype_fdwatch
  393. *
  394. * Returns:
  395. * \li #ISC_R_NOTIMPLEMENTED
  396. */
  397. isc_result_t
  398. isc_socket_bind(isc_socket_t *sock, isc_sockaddr_t *addressp,
  399. unsigned int options);
  400. /*%<
  401. * Bind 'socket' to '*addressp'.
  402. *
  403. * Requires:
  404. *
  405. * \li 'socket' is a valid socket
  406. *
  407. * \li 'addressp' points to a valid isc_sockaddr.
  408. *
  409. * Returns:
  410. *
  411. * \li ISC_R_SUCCESS
  412. * \li ISC_R_NOPERM
  413. * \li ISC_R_ADDRNOTAVAIL
  414. * \li ISC_R_ADDRINUSE
  415. * \li ISC_R_BOUND
  416. * \li ISC_R_UNEXPECTED
  417. */
  418. isc_result_t
  419. isc_socket_filter(isc_socket_t *sock, const char *filter);
  420. /*%<
  421. * Inform the kernel that it should perform accept filtering.
  422. * If filter is NULL the current filter will be removed.:w
  423. */
  424. isc_result_t
  425. isc_socket_listen(isc_socket_t *sock, unsigned int backlog);
  426. /*%<
  427. * Set listen mode on the socket. After this call, the only function that
  428. * can be used (other than attach and detach) is isc_socket_accept().
  429. *
  430. * Notes:
  431. *
  432. * \li 'backlog' is as in the UNIX system call listen() and may be
  433. * ignored by non-UNIX implementations.
  434. *
  435. * \li If 'backlog' is zero, a reasonable system default is used, usually
  436. * SOMAXCONN.
  437. *
  438. * Requires:
  439. *
  440. * \li 'socket' is a valid, bound TCP socket or a valid, bound UNIX socket.
  441. *
  442. * Returns:
  443. *
  444. * \li ISC_R_SUCCESS
  445. * \li ISC_R_UNEXPECTED
  446. */
  447. isc_result_t
  448. isc_socket_accept(isc_socket_t *sock,
  449. isc_task_t *task, isc_taskaction_t action, const void *arg);
  450. /*%<
  451. * Queue accept event. When a new connection is received, the task will
  452. * get an ISC_SOCKEVENT_NEWCONN event with the sender set to the listen
  453. * socket. The new socket structure is sent inside the isc_socket_newconnev_t
  454. * event type, and is attached to the task 'task'.
  455. *
  456. * REQUIRES:
  457. * \li 'socket' is a valid TCP socket that isc_socket_listen() was called
  458. * on.
  459. *
  460. * \li 'task' is a valid task
  461. *
  462. * \li 'action' is a valid action
  463. *
  464. * RETURNS:
  465. * \li ISC_R_SUCCESS
  466. * \li ISC_R_NOMEMORY
  467. * \li ISC_R_UNEXPECTED
  468. */
  469. isc_result_t
  470. isc_socket_connect(isc_socket_t *sock, isc_sockaddr_t *addressp,
  471. isc_task_t *task, isc_taskaction_t action,
  472. const void *arg);
  473. /*%<
  474. * Connect 'socket' to peer with address *saddr. When the connection
  475. * succeeds, or when an error occurs, a CONNECT event with action 'action'
  476. * and arg 'arg' will be posted to the event queue for 'task'.
  477. *
  478. * Requires:
  479. *
  480. * \li 'socket' is a valid TCP socket
  481. *
  482. * \li 'addressp' points to a valid isc_sockaddr
  483. *
  484. * \li 'task' is a valid task
  485. *
  486. * \li 'action' is a valid action
  487. *
  488. * Returns:
  489. *
  490. * \li ISC_R_SUCCESS
  491. * \li ISC_R_NOMEMORY
  492. * \li ISC_R_UNEXPECTED
  493. *
  494. * Posted event's result code:
  495. *
  496. * \li ISC_R_SUCCESS
  497. * \li ISC_R_TIMEDOUT
  498. * \li ISC_R_CONNREFUSED
  499. * \li ISC_R_NETUNREACH
  500. * \li ISC_R_UNEXPECTED
  501. */
  502. isc_result_t
  503. isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp);
  504. /*%<
  505. * Get the name of the peer connected to 'socket'.
  506. *
  507. * Requires:
  508. *
  509. * \li 'socket' is a valid TCP socket.
  510. *
  511. * Returns:
  512. *
  513. * \li ISC_R_SUCCESS
  514. * \li ISC_R_TOOSMALL
  515. * \li ISC_R_UNEXPECTED
  516. */
  517. isc_result_t
  518. isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp);
  519. /*%<
  520. * Get the name of 'socket'.
  521. *
  522. * Requires:
  523. *
  524. * \li 'socket' is a valid socket.
  525. *
  526. * Returns:
  527. *
  528. * \li ISC_R_SUCCESS
  529. * \li ISC_R_TOOSMALL
  530. * \li ISC_R_UNEXPECTED
  531. */
  532. /*@{*/
  533. isc_result_t
  534. isc_socket_recv(isc_socket_t *sock, isc_region_t *region,
  535. unsigned int minimum,
  536. isc_task_t *task, isc_taskaction_t action, const void *arg);
  537. isc_result_t
  538. isc_socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
  539. unsigned int minimum,
  540. isc_task_t *task, isc_taskaction_t action, const void *arg);
  541. isc_result_t
  542. isc_socket_recv2(isc_socket_t *sock, isc_region_t *region,
  543. unsigned int minimum, isc_task_t *task,
  544. isc_socketevent_t *event, unsigned int flags);
  545. /*!
  546. * Receive from 'socket', storing the results in region.
  547. *
  548. * Notes:
  549. *
  550. *\li Let 'length' refer to the length of 'region' or to the sum of all
  551. * available regions in the list of buffers '*buflist'.
  552. *
  553. *\li If 'minimum' is non-zero and at least that many bytes are read,
  554. * the completion event will be posted to the task 'task.' If minimum
  555. * is zero, the exact number of bytes requested in the region must
  556. * be read for an event to be posted. This only makes sense for TCP
  557. * connections, and is always set to 1 byte for UDP.
  558. *
  559. *\li The read will complete when the desired number of bytes have been
  560. * read, if end-of-input occurs, or if an error occurs. A read done
  561. * event with the given 'action' and 'arg' will be posted to the
  562. * event queue of 'task'.
  563. *
  564. *\li The caller may not modify 'region', the buffers which are passed
  565. * into this function, or any data they refer to until the completion
  566. * event is received.
  567. *
  568. *\li For isc_socket_recvv():
  569. * On successful completion, '*buflist' will be empty, and the list of
  570. * all buffers will be returned in the done event's 'bufferlist'
  571. * member. On error return, '*buflist' will be unchanged.
  572. *
  573. *\li For isc_socket_recv2():
  574. * 'event' is not NULL, and the non-socket specific fields are
  575. * expected to be initialized.
  576. *
  577. *\li For isc_socket_recv2():
  578. * The only defined value for 'flags' is ISC_SOCKFLAG_IMMEDIATE. If
  579. * set and the operation completes, the return value will be
  580. * ISC_R_SUCCESS and the event will be filled in and not sent. If the
  581. * operation does not complete, the return value will be
  582. * ISC_R_INPROGRESS and the event will be sent when the operation
  583. * completes.
  584. *
  585. * Requires:
  586. *
  587. *\li 'socket' is a valid, bound socket.
  588. *
  589. *\li For isc_socket_recv():
  590. * 'region' is a valid region
  591. *
  592. *\li For isc_socket_recvv():
  593. * 'buflist' is non-NULL, and '*buflist' contain at least one buffer.
  594. *
  595. *\li 'task' is a valid task
  596. *
  597. *\li For isc_socket_recv() and isc_socket_recvv():
  598. * action != NULL and is a valid action
  599. *
  600. *\li For isc_socket_recv2():
  601. * event != NULL
  602. *
  603. * Returns:
  604. *
  605. *\li #ISC_R_SUCCESS
  606. *\li #ISC_R_INPROGRESS
  607. *\li #ISC_R_NOMEMORY
  608. *\li #ISC_R_UNEXPECTED
  609. *
  610. * Event results:
  611. *
  612. *\li #ISC_R_SUCCESS
  613. *\li #ISC_R_UNEXPECTED
  614. *\li XXX needs other net-type errors
  615. */
  616. /*@}*/
  617. /*@{*/
  618. isc_result_t
  619. isc_socket_send(isc_socket_t *sock, isc_region_t *region,
  620. isc_task_t *task, isc_taskaction_t action, const void *arg);
  621. isc_result_t
  622. isc_socket_sendto(isc_socket_t *sock, isc_region_t *region,
  623. isc_task_t *task, isc_taskaction_t action, const void *arg,
  624. isc_sockaddr_t *address, struct in6_pktinfo *pktinfo);
  625. isc_result_t
  626. isc_socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
  627. isc_task_t *task, isc_taskaction_t action, const void *arg);
  628. isc_result_t
  629. isc_socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
  630. isc_task_t *task, isc_taskaction_t action, const void *arg,
  631. isc_sockaddr_t *address, struct in6_pktinfo *pktinfo);
  632. isc_result_t
  633. isc_socket_sendto2(isc_socket_t *sock, isc_region_t *region,
  634. isc_task_t *task,
  635. isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
  636. isc_socketevent_t *event, unsigned int flags);
  637. /*!
  638. * Send the contents of 'region' to the socket's peer.
  639. *
  640. * Notes:
  641. *
  642. *\li Shutting down the requestor's task *may* result in any
  643. * still pending writes being dropped or completed, depending on the
  644. * underlying OS implementation.
  645. *
  646. *\li If 'action' is NULL, then no completion event will be posted.
  647. *
  648. *\li The caller may not modify 'region', the buffers which are passed
  649. * into this function, or any data they refer to until the completion
  650. * event is received.
  651. *
  652. *\li For isc_socket_sendv() and isc_socket_sendtov():
  653. * On successful completion, '*buflist' will be empty, and the list of
  654. * all buffers will be returned in the done event's 'bufferlist'
  655. * member. On error return, '*buflist' will be unchanged.
  656. *
  657. *\li For isc_socket_sendto2():
  658. * 'event' is not NULL, and the non-socket specific fields are
  659. * expected to be initialized.
  660. *
  661. *\li For isc_socket_sendto2():
  662. * The only defined values for 'flags' are ISC_SOCKFLAG_IMMEDIATE
  663. * and ISC_SOCKFLAG_NORETRY.
  664. *
  665. *\li If ISC_SOCKFLAG_IMMEDIATE is set and the operation completes, the
  666. * return value will be ISC_R_SUCCESS and the event will be filled
  667. * in and not sent. If the operation does not complete, the return
  668. * value will be ISC_R_INPROGRESS and the event will be sent when
  669. * the operation completes.
  670. *
  671. *\li ISC_SOCKFLAG_NORETRY can only be set for UDP sockets. If set
  672. * and the send operation fails due to a transient error, the send
  673. * will not be retried and the error will be indicated in the event.
  674. * Using this option along with ISC_SOCKFLAG_IMMEDIATE allows the caller
  675. * to specify a region that is allocated on the stack.
  676. *
  677. * Requires:
  678. *
  679. *\li 'socket' is a valid, bound socket.
  680. *
  681. *\li For isc_socket_send():
  682. * 'region' is a valid region
  683. *
  684. *\li For isc_socket_sendv() and isc_socket_sendtov():
  685. * 'buflist' is non-NULL, and '*buflist' contain at least one buffer.
  686. *
  687. *\li 'task' is a valid task
  688. *
  689. *\li For isc_socket_sendv(), isc_socket_sendtov(), isc_socket_send(), and
  690. * isc_socket_sendto():
  691. * action == NULL or is a valid action
  692. *
  693. *\li For isc_socket_sendto2():
  694. * event != NULL
  695. *
  696. * Returns:
  697. *
  698. *\li #ISC_R_SUCCESS
  699. *\li #ISC_R_INPROGRESS
  700. *\li #ISC_R_NOMEMORY
  701. *\li #ISC_R_UNEXPECTED
  702. *
  703. * Event results:
  704. *
  705. *\li #ISC_R_SUCCESS
  706. *\li #ISC_R_UNEXPECTED
  707. *\li XXX needs other net-type errors
  708. */
  709. /*@}*/
  710. isc_result_t
  711. isc_socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp);
  712. isc_result_t
  713. isc_socketmgr_create2(isc_mem_t *mctx, isc_socketmgr_t **managerp,
  714. unsigned int maxsocks);
  715. /*%<
  716. * Create a socket manager. If "maxsocks" is non-zero, it specifies the
  717. * maximum number of sockets that the created manager should handle.
  718. * isc_socketmgr_create() is equivalent of isc_socketmgr_create2() with
  719. * "maxsocks" being zero.
  720. *
  721. * Notes:
  722. *
  723. *\li All memory will be allocated in memory context 'mctx'.
  724. *
  725. * Requires:
  726. *
  727. *\li 'mctx' is a valid memory context.
  728. *
  729. *\li 'managerp' points to a NULL isc_socketmgr_t.
  730. *
  731. * Ensures:
  732. *
  733. *\li '*managerp' is a valid isc_socketmgr_t.
  734. *
  735. * Returns:
  736. *
  737. *\li #ISC_R_SUCCESS
  738. *\li #ISC_R_NOMEMORY
  739. *\li #ISC_R_UNEXPECTED
  740. *\li #ISC_R_NOTIMPLEMENTED
  741. */
  742. isc_result_t
  743. isc_socketmgr_getmaxsockets(isc_socketmgr_t *manager, unsigned int *nsockp);
  744. /*%<
  745. * Returns in "*nsockp" the maximum number of sockets this manager may open.
  746. *
  747. * Requires:
  748. *
  749. *\li '*manager' is a valid isc_socketmgr_t.
  750. *\li 'nsockp' is not NULL.
  751. *
  752. * Returns:
  753. *
  754. *\li #ISC_R_SUCCESS
  755. *\li #ISC_R_NOTIMPLEMENTED
  756. */
  757. void
  758. isc_socketmgr_destroy(isc_socketmgr_t **managerp);
  759. /*%<
  760. * Destroy a socket manager.
  761. *
  762. * Notes:
  763. *
  764. *\li This routine blocks until there are no sockets left in the manager,
  765. * so if the caller holds any socket references using the manager, it
  766. * must detach them before calling isc_socketmgr_destroy() or it will
  767. * block forever.
  768. *
  769. * Requires:
  770. *
  771. *\li '*managerp' is a valid isc_socketmgr_t.
  772. *
  773. *\li All sockets managed by this manager are fully detached.
  774. *
  775. * Ensures:
  776. *
  777. *\li *managerp == NULL
  778. *
  779. *\li All resources used by the manager have been freed.
  780. */
  781. isc_sockettype_t
  782. isc_socket_gettype(isc_socket_t *sock);
  783. /*%<
  784. * Returns the socket type for "sock."
  785. *
  786. * Requires:
  787. *
  788. *\li "sock" is a valid socket.
  789. */
  790. /*@{*/
  791. isc_boolean_t
  792. isc_socket_isbound(isc_socket_t *sock);
  793. void
  794. isc_socket_ipv6only(isc_socket_t *sock, isc_boolean_t yes);
  795. /*%<
  796. * If the socket is an IPv6 socket set/clear the IPV6_IPV6ONLY socket
  797. * option if the host OS supports this option.
  798. *
  799. * Requires:
  800. *\li 'sock' is a valid socket.
  801. */
  802. /*@}*/
  803. void
  804. isc_socket_cleanunix(isc_sockaddr_t *addr, isc_boolean_t active);
  805. /*%<
  806. * Cleanup UNIX domain sockets in the file-system. If 'active' is true
  807. * then just unlink the socket. If 'active' is false try to determine
  808. * if there is a listener of the socket or not. If no listener is found
  809. * then unlink socket.
  810. *
  811. * Prior to unlinking the path is tested to see if it a socket.
  812. *
  813. * Note: there are a number of race conditions which cannot be avoided
  814. * both in the filesystem and any application using UNIX domain
  815. * sockets (e.g. socket is tested between bind() and listen(),
  816. * the socket is deleted and replaced in the file-system between
  817. * stat() and unlink()).
  818. */
  819. isc_result_t
  820. isc_socket_permunix(isc_sockaddr_t *sockaddr, isc_uint32_t perm,
  821. isc_uint32_t owner, isc_uint32_t group);
  822. /*%<
  823. * Set ownership and file permissions on the UNIX domain socket.
  824. *
  825. * Note: On Solaris and SunOS this secures the directory containing
  826. * the socket as Solaris and SunOS do not honour the filesytem
  827. * permissions on the socket.
  828. *
  829. * Requires:
  830. * \li 'sockaddr' to be a valid UNIX domain sockaddr.
  831. *
  832. * Returns:
  833. * \li #ISC_R_SUCCESS
  834. * \li #ISC_R_FAILURE
  835. */
  836. void isc_socket_setname(isc_socket_t *socket, const char *name, void *tag);
  837. /*%<
  838. * Set the name and optional tag for a socket. This allows tracking of the
  839. * owner or purpose for this socket, and is useful for tracing and statistics
  840. * reporting.
  841. */
  842. const char *isc_socket_getname(isc_socket_t *socket);
  843. /*%<
  844. * Get the name associated with a socket, if any.
  845. */
  846. void *isc_socket_gettag(isc_socket_t *socket);
  847. /*%<
  848. * Get the tag associated with a socket, if any.
  849. */
  850. void
  851. isc__socketmgr_setreserved(isc_socketmgr_t *mgr, isc_uint32_t);
  852. /*%<
  853. * Temporary. For use by named only.
  854. */
  855. #ifdef HAVE_LIBXML2
  856. void
  857. isc_socketmgr_renderxml(isc_socketmgr_t *mgr, xmlTextWriterPtr writer);
  858. /*%<
  859. * Render internal statistics and other state into the XML document.
  860. */
  861. #endif /* HAVE_LIBXML2 */
  862. ISC_LANG_ENDDECLS
  863. #endif /* ISC_SOCKET_H */