Add BIND 9.2.4rc7.
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / include / isc / socket.h
blob2fb6e709c2001a7a0c445a02baddc548fae55f39
1 /*
2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 1998-2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and 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.
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.
18 /* $Id: socket.h,v 1.54.2.1 2004/03/09 06:12:02 marka Exp $ */
20 #ifndef ISC_SOCKET_H
21 #define ISC_SOCKET_H 1
23 /*****
24 ***** Module Info
25 *****/
28 * Sockets
30 * Provides TCP and UDP sockets for network I/O. The sockets are event
31 * sources in the task system.
33 * When I/O completes, a completion event for the socket is posted to the
34 * event queue of the task which requested the I/O.
36 * MP:
37 * The module ensures appropriate synchronization of data structures it
38 * creates and manipulates.
40 * Clients of this module must not be holding a socket's task's lock when
41 * making a call that affects that socket. Failure to follow this rule
42 * can result in deadlock.
44 * The caller must ensure that isc_socketmgr_destroy() is called only
45 * once for a given manager.
47 * Reliability:
48 * No anticipated impact.
50 * Resources:
51 * <TBS>
53 * Security:
54 * No anticipated impact.
56 * Standards:
57 * None.
60 /***
61 *** Imports
62 ***/
64 #include <isc/lang.h>
65 #include <isc/types.h>
66 #include <isc/event.h>
67 #include <isc/eventclass.h>
68 #include <isc/time.h>
69 #include <isc/region.h>
70 #include <isc/sockaddr.h>
72 ISC_LANG_BEGINDECLS
74 /***
75 *** Constants
76 ***/
79 * Maximum number of buffers in a scatter/gather read/write. The operating
80 * system in use must support at least this number (plus one on some.)
82 #define ISC_SOCKET_MAXSCATTERGATHER 8
84 /***
85 *** Types
86 ***/
88 struct isc_socketevent {
89 ISC_EVENT_COMMON(isc_socketevent_t);
90 isc_result_t result; /* OK, EOF, whatever else */
91 unsigned int minimum; /* minimum i/o for event */
92 unsigned int n; /* bytes read or written */
93 unsigned int offset; /* offset into buffer list */
94 isc_region_t region; /* for single-buffer i/o */
95 isc_bufferlist_t bufferlist; /* list of buffers */
96 isc_sockaddr_t address; /* source address */
97 isc_time_t timestamp; /* timestamp of packet recv */
98 struct in6_pktinfo pktinfo; /* ipv6 pktinfo */
99 isc_uint32_t attributes; /* see below */
102 typedef struct isc_socket_newconnev isc_socket_newconnev_t;
103 struct isc_socket_newconnev {
104 ISC_EVENT_COMMON(isc_socket_newconnev_t);
105 isc_socket_t * newsocket;
106 isc_result_t result; /* OK, EOF, whatever else */
107 isc_sockaddr_t address; /* source address */
110 typedef struct isc_socket_connev isc_socket_connev_t;
111 struct isc_socket_connev {
112 ISC_EVENT_COMMON(isc_socket_connev_t);
113 isc_result_t result; /* OK, EOF, whatever else */
117 * _ATTACHED: Internal use only.
118 * _TRUNC: Packet was truncated on receive.
119 * _CTRUNC: Packet control information was truncated. This can
120 * indicate that the packet is not complete, even though
121 * all the data is valid.
122 * _TIMESTAMP: The timestamp member is valid.
123 * _PKTINFO: The pktinfo member is valid.
124 * _MULTICAST: The UDP packet was received via a multicast transmission.
126 #define ISC_SOCKEVENTATTR_ATTACHED 0x80000000U /* internal */
127 #define ISC_SOCKEVENTATTR_TRUNC 0x00800000U /* public */
128 #define ISC_SOCKEVENTATTR_CTRUNC 0x00400000U /* public */
129 #define ISC_SOCKEVENTATTR_TIMESTAMP 0x00200000U /* public */
130 #define ISC_SOCKEVENTATTR_PKTINFO 0x00100000U /* public */
131 #define ISC_SOCKEVENTATTR_MULTICAST 0x00080000U /* public */
133 #define ISC_SOCKEVENT_ANYEVENT (0)
134 #define ISC_SOCKEVENT_RECVDONE (ISC_EVENTCLASS_SOCKET + 1)
135 #define ISC_SOCKEVENT_SENDDONE (ISC_EVENTCLASS_SOCKET + 2)
136 #define ISC_SOCKEVENT_NEWCONN (ISC_EVENTCLASS_SOCKET + 3)
137 #define ISC_SOCKEVENT_CONNECT (ISC_EVENTCLASS_SOCKET + 4)
140 * Internal events.
142 #define ISC_SOCKEVENT_INTR (ISC_EVENTCLASS_SOCKET + 256)
143 #define ISC_SOCKEVENT_INTW (ISC_EVENTCLASS_SOCKET + 257)
145 typedef enum {
146 isc_sockettype_udp = 1,
147 isc_sockettype_tcp = 2
148 } isc_sockettype_t;
151 * How a socket should be shutdown in isc_socket_shutdown() calls.
153 #define ISC_SOCKSHUT_RECV 0x00000001 /* close read side */
154 #define ISC_SOCKSHUT_SEND 0x00000002 /* close write side */
155 #define ISC_SOCKSHUT_ALL 0x00000003 /* close them all */
158 * What I/O events to cancel in isc_socket_cancel() calls.
160 #define ISC_SOCKCANCEL_RECV 0x00000001 /* cancel recv */
161 #define ISC_SOCKCANCEL_SEND 0x00000002 /* cancel send */
162 #define ISC_SOCKCANCEL_ACCEPT 0x00000004 /* cancel accept */
163 #define ISC_SOCKCANCEL_CONNECT 0x00000008 /* cancel connect */
164 #define ISC_SOCKCANCEL_ALL 0x0000000f /* cancel everything */
167 * Flags for isc_socket_send() and isc_socket_recv() calls.
169 #define ISC_SOCKFLAG_IMMEDIATE 0x00000001 /* send event only if needed */
170 #define ISC_SOCKFLAG_NORETRY 0x00000002 /* drop failed UDP sends */
172 /***
173 *** Socket and Socket Manager Functions
175 *** Note: all Ensures conditions apply only if the result is success for
176 *** those functions which return an isc_result.
177 ***/
179 isc_result_t
180 isc_socket_create(isc_socketmgr_t *manager,
181 int pf,
182 isc_sockettype_t type,
183 isc_socket_t **socketp);
185 * Create a new 'type' socket managed by 'manager'.
187 * Note:
189 * 'pf' is the desired protocol family, e.g. PF_INET or PF_INET6.
191 * Requires:
193 * 'manager' is a valid manager
195 * 'socketp' is a valid pointer, and *socketp == NULL
197 * Ensures:
199 * '*socketp' is attached to the newly created socket
201 * Returns:
203 * ISC_R_SUCCESS
204 * ISC_R_NOMEMORY
205 * ISC_R_NORESOURCES
206 * ISC_R_UNEXPECTED
209 void
210 isc_socket_cancel(isc_socket_t *sock, isc_task_t *task,
211 unsigned int how);
213 * Cancel pending I/O of the type specified by "how".
215 * Note: if "task" is NULL, then the cancel applies to all tasks using the
216 * socket.
218 * Requires:
220 * "socket" is a valid socket
222 * "task" is NULL or a valid task
224 * "how" is a bitmask describing the type of cancelation to perform.
225 * The type ISC_SOCKCANCEL_ALL will cancel all pending I/O on this
226 * socket.
228 * ISC_SOCKCANCEL_RECV:
229 * Cancel pending isc_socket_recv() calls.
231 * ISC_SOCKCANCEL_SEND:
232 * Cancel pending isc_socket_send() and isc_socket_sendto() calls.
234 * ISC_SOCKCANCEL_ACCEPT:
235 * Cancel pending isc_socket_accept() calls.
237 * ISC_SOCKCANCEL_CONNECT:
238 * Cancel pending isc_socket_connect() call.
241 void
242 isc_socket_shutdown(isc_socket_t *sock, unsigned int how);
244 * Shutdown 'socket' according to 'how'.
246 * Requires:
248 * 'socket' is a valid socket.
250 * 'task' is NULL or is a valid task.
252 * If 'how' is 'ISC_SOCKSHUT_RECV' or 'ISC_SOCKSHUT_ALL' then
254 * The read queue must be empty.
256 * No further read requests may be made.
258 * If 'how' is 'ISC_SOCKSHUT_SEND' or 'ISC_SOCKSHUT_ALL' then
260 * The write queue must be empty.
262 * No further write requests may be made.
265 void
266 isc_socket_attach(isc_socket_t *sock, isc_socket_t **socketp);
268 * Attach *socketp to socket.
270 * Requires:
272 * 'socket' is a valid socket.
274 * 'socketp' points to a NULL socket.
276 * Ensures:
278 * *socketp is attached to socket.
281 void
282 isc_socket_detach(isc_socket_t **socketp);
284 * Detach *socketp from its socket.
286 * Requires:
288 * 'socketp' points to a valid socket.
290 * If '*socketp' is the last reference to the socket,
291 * then:
293 * There must be no pending I/O requests.
295 * Ensures:
297 * *socketp is NULL.
299 * If '*socketp' is the last reference to the socket,
300 * then:
302 * The socket will be shutdown (both reading and writing)
303 * for all tasks.
305 * All resources used by the socket have been freed
308 isc_result_t
309 isc_socket_bind(isc_socket_t *sock, isc_sockaddr_t *addressp);
311 * Bind 'socket' to '*addressp'.
313 * Requires:
315 * 'socket' is a valid socket
317 * 'addressp' points to a valid isc_sockaddr.
319 * Returns:
321 * ISC_R_SUCCESS
322 * ISC_R_NOPERM
323 * ISC_R_ADDRNOTAVAIL
324 * ISC_R_ADDRINUSE
325 * ISC_R_BOUND
326 * ISC_R_UNEXPECTED
329 isc_result_t
330 isc_socket_listen(isc_socket_t *sock, unsigned int backlog);
332 * Set listen mode on the socket. After this call, the only function that
333 * can be used (other than attach and detach) is isc_socket_accept().
335 * Notes:
337 * 'backlog' is as in the UNIX system call listen() and may be
338 * ignored by non-UNIX implementations.
340 * If 'backlog' is zero, a reasonable system default is used, usually
341 * SOMAXCONN.
343 * Requires:
345 * 'socket' is a valid, bound TCP socket.
347 * Returns:
349 * ISC_R_SUCCESS
350 * ISC_R_UNEXPECTED
353 isc_result_t
354 isc_socket_accept(isc_socket_t *sock,
355 isc_task_t *task, isc_taskaction_t action, const void *arg);
357 * Queue accept event. When a new connection is received, the task will
358 * get an ISC_SOCKEVENT_NEWCONN event with the sender set to the listen
359 * socket. The new socket structure is sent inside the isc_socket_newconnev_t
360 * event type, and is attached to the task 'task'.
362 * REQUIRES:
363 * 'socket' is a valid TCP socket that isc_socket_listen() was called
364 * on.
366 * 'task' is a valid task
368 * 'action' is a valid action
370 * RETURNS:
371 * ISC_R_SUCCESS
372 * ISC_R_NOMEMORY
373 * ISC_R_UNEXPECTED
376 isc_result_t
377 isc_socket_connect(isc_socket_t *sock, isc_sockaddr_t *addressp,
378 isc_task_t *task, isc_taskaction_t action,
379 const void *arg);
381 * Connect 'socket' to peer with address *saddr. When the connection
382 * succeeds, or when an error occurs, a CONNECT event with action 'action'
383 * and arg 'arg' will be posted to the event queue for 'task'.
385 * Requires:
387 * 'socket' is a valid TCP socket
389 * 'addressp' points to a valid isc_sockaddr
391 * 'task' is a valid task
393 * 'action' is a valid action
395 * Returns:
397 * ISC_R_SUCCESS
398 * ISC_R_NOMEMORY
399 * ISC_R_UNEXPECTED
401 * Posted event's result code:
403 * ISC_R_SUCCESS
404 * ISC_R_TIMEDOUT
405 * ISC_R_CONNREFUSED
406 * ISC_R_NETUNREACH
407 * ISC_R_UNEXPECTED
410 isc_result_t
411 isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp);
413 * Get the name of the peer connected to 'socket'.
415 * Requires:
417 * 'socket' is a valid TCP socket.
419 * Returns:
421 * ISC_R_SUCCESS
422 * ISC_R_TOOSMALL
423 * ISC_R_UNEXPECTED
426 isc_result_t
427 isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp);
429 * Get the name of 'socket'.
431 * Requires:
433 * 'socket' is a valid socket.
435 * Returns:
437 * ISC_R_SUCCESS
438 * ISC_R_TOOSMALL
439 * ISC_R_UNEXPECTED
442 isc_result_t
443 isc_socket_recv(isc_socket_t *sock, isc_region_t *region,
444 unsigned int minimum,
445 isc_task_t *task, isc_taskaction_t action, const void *arg);
446 isc_result_t
447 isc_socket_recvv(isc_socket_t *sock, isc_bufferlist_t *buflist,
448 unsigned int minimum,
449 isc_task_t *task, isc_taskaction_t action, const void *arg);
451 isc_result_t
452 isc_socket_recv2(isc_socket_t *sock, isc_region_t *region,
453 unsigned int minimum, isc_task_t *task,
454 isc_socketevent_t *event, unsigned int flags);
457 * Receive from 'socket', storing the results in region.
459 * Notes:
461 * Let 'length' refer to the length of 'region' or to the sum of all
462 * available regions in the list of buffers '*buflist'.
464 * If 'minimum' is non-zero and at least that many bytes are read,
465 * the completion event will be posted to the task 'task.' If minimum
466 * is zero, the exact number of bytes requested in the region must
467 * be read for an event to be posted. This only makes sense for TCP
468 * connections, and is always set to 1 byte for UDP.
470 * The read will complete when the desired number of bytes have been
471 * read, if end-of-input occurs, or if an error occurs. A read done
472 * event with the given 'action' and 'arg' will be posted to the
473 * event queue of 'task'.
475 * The caller may not modify 'region', the buffers which are passed
476 * into this function, or any data they refer to until the completion
477 * event is received.
479 * For isc_socket_recvv():
480 * On successful completion, '*buflist' will be empty, and the list of
481 * all buffers will be returned in the done event's 'bufferlist'
482 * member. On error return, '*buflist' will be unchanged.
484 * For isc_socket_recv2():
485 * 'event' is not NULL, and the non-socket specific fields are
486 * expected to be initialized.
488 * For isc_socket_recv2():
489 * The only defined value for 'flags' is ISC_SOCKFLAG_IMMEDIATE. If
490 * set and the operation completes, the return value will be
491 * ISC_R_SUCCESS and the event will be filled in and not sent. If the
492 * operation does not complete, the return value will be
493 * ISC_R_INPROGRESS and the event will be sent when the operation
494 * completes.
496 * Requires:
498 * 'socket' is a valid, bound socket.
500 * For isc_socket_recv():
501 * 'region' is a valid region
503 * For isc_socket_recvv():
504 * 'buflist' is non-NULL, and '*buflist' contain at least one buffer.
506 * 'task' is a valid task
508 * For isc_socket_recv() and isc_socket_recvv():
509 * action != NULL and is a valid action
511 * For isc_socket_recv2():
512 * event != NULL
514 * Returns:
516 * ISC_R_SUCCESS
517 * ISC_R_INPROGRESS
518 * ISC_R_NOMEMORY
519 * ISC_R_UNEXPECTED
521 * Event results:
523 * ISC_R_SUCCESS
524 * ISC_R_UNEXPECTED
525 * XXX needs other net-type errors
528 isc_result_t
529 isc_socket_send(isc_socket_t *sock, isc_region_t *region,
530 isc_task_t *task, isc_taskaction_t action, const void *arg);
531 isc_result_t
532 isc_socket_sendto(isc_socket_t *sock, isc_region_t *region,
533 isc_task_t *task, isc_taskaction_t action, const void *arg,
534 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo);
535 isc_result_t
536 isc_socket_sendv(isc_socket_t *sock, isc_bufferlist_t *buflist,
537 isc_task_t *task, isc_taskaction_t action, const void *arg);
538 isc_result_t
539 isc_socket_sendtov(isc_socket_t *sock, isc_bufferlist_t *buflist,
540 isc_task_t *task, isc_taskaction_t action, const void *arg,
541 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo);
542 isc_result_t
543 isc_socket_sendto2(isc_socket_t *sock, isc_region_t *region,
544 isc_task_t *task,
545 isc_sockaddr_t *address, struct in6_pktinfo *pktinfo,
546 isc_socketevent_t *event, unsigned int flags);
549 * Send the contents of 'region' to the socket's peer.
551 * Notes:
553 * Shutting down the requestor's task *may* result in any
554 * still pending writes being dropped or completed, depending on the
555 * underlying OS implementation.
557 * If 'action' is NULL, then no completion event will be posted.
559 * The caller may not modify 'region', the buffers which are passed
560 * into this function, or any data they refer to until the completion
561 * event is received.
563 * For isc_socket_sendv() and isc_socket_sendtov():
564 * On successful completion, '*buflist' will be empty, and the list of
565 * all buffers will be returned in the done event's 'bufferlist'
566 * member. On error return, '*buflist' will be unchanged.
568 * For isc_socket_sendto2():
569 * 'event' is not NULL, and the non-socket specific fields are
570 * expected to be initialized.
572 * For isc_socket_sendto2():
573 * The only defined values for 'flags' are ISC_SOCKFLAG_IMMEDIATE
574 * and ISC_SOCKFLAG_NORETRY.
576 * If ISC_SOCKFLAG_IMMEDIATE is set and the operation completes, the
577 * return value will be ISC_R_SUCCESS and the event will be filled
578 * in and not sent. If the operation does not complete, the return
579 * value will be ISC_R_INPROGRESS and the event will be sent when
580 * the operation completes.
582 * ISC_SOCKFLAG_NORETRY can only be set for UDP sockets. If set
583 * and the send operation fails due to a transient error, the send
584 * will not be retried and the error will be indicated in the event.
585 * Using this option along with ISC_SOCKFLAG_IMMEDIATE allows the caller
586 * to specify a region that is allocated on the stack.
588 * Requires:
590 * 'socket' is a valid, bound socket.
592 * For isc_socket_send():
593 * 'region' is a valid region
595 * For isc_socket_sendv() and isc_socket_sendtov():
596 * 'buflist' is non-NULL, and '*buflist' contain at least one buffer.
598 * 'task' is a valid task
600 * For isc_socket_sendv(), isc_socket_sendtov(), isc_socket_send(), and
601 * isc_socket_sendto():
602 * action == NULL or is a valid action
604 * For isc_socket_sendto2():
605 * event != NULL
607 * Returns:
609 * ISC_R_SUCCESS
610 * ISC_R_INPROGRESS
611 * ISC_R_NOMEMORY
612 * ISC_R_UNEXPECTED
614 * Event results:
616 * ISC_R_SUCCESS
617 * ISC_R_UNEXPECTED
618 * XXX needs other net-type errors
621 isc_result_t
622 isc_socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp);
624 * Create a socket manager.
626 * Notes:
628 * All memory will be allocated in memory context 'mctx'.
630 * Requires:
632 * 'mctx' is a valid memory context.
634 * 'managerp' points to a NULL isc_socketmgr_t.
636 * Ensures:
638 * '*managerp' is a valid isc_socketmgr_t.
640 * Returns:
642 * ISC_R_SUCCESS
643 * ISC_R_NOMEMORY
644 * ISC_R_UNEXPECTED
647 void
648 isc_socketmgr_destroy(isc_socketmgr_t **managerp);
650 * Destroy a socket manager.
652 * Notes:
654 * This routine blocks until there are no sockets left in the manager,
655 * so if the caller holds any socket references using the manager, it
656 * must detach them before calling isc_socketmgr_destroy() or it will
657 * block forever.
659 * Requires:
661 * '*managerp' is a valid isc_socketmgr_t.
663 * All sockets managed by this manager are fully detached.
665 * Ensures:
667 * *managerp == NULL
669 * All resources used by the manager have been freed.
672 isc_sockettype_t
673 isc_socket_gettype(isc_socket_t *sock);
675 * Returns the socket type for "sock."
677 * Requires:
679 * "sock" is a valid socket.
682 isc_boolean_t
683 isc_socket_isbound(isc_socket_t *sock);
685 ISC_LANG_ENDDECLS
687 #endif /* ISC_SOCKET_H */