Merge from vendor branch BINUTILS:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / isc / include / isc / socket.h
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1998-2001  Internet Software Consortium.
4  *
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.
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
18 /* $Id: socket.h,v 1.54.2.1 2004/03/09 06:12:02 marka Exp $ */
19
20 #ifndef ISC_SOCKET_H
21 #define ISC_SOCKET_H 1
22
23 /*****
24  ***** Module Info
25  *****/
26
27 /*
28  * Sockets
29  *
30  * Provides TCP and UDP sockets for network I/O.  The sockets are event
31  * sources in the task system.
32  *
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.
35  *
36  * MP:
37  *      The module ensures appropriate synchronization of data structures it
38  *      creates and manipulates.
39  *
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.
43  *
44  *      The caller must ensure that isc_socketmgr_destroy() is called only
45  *      once for a given manager.
46  *
47  * Reliability:
48  *      No anticipated impact.
49  *
50  * Resources:
51  *      <TBS>
52  *
53  * Security:
54  *      No anticipated impact.
55  *
56  * Standards:
57  *      None.
58  */
59
60 /***
61  *** Imports
62  ***/
63
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>
71
72 ISC_LANG_BEGINDECLS
73
74 /***
75  *** Constants
76  ***/
77
78 /*
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.)
81  */
82 #define ISC_SOCKET_MAXSCATTERGATHER     8
83
84 /***
85  *** Types
86  ***/
87
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 */
100 };
101
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 */
108 };
109
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 */
114 };
115
116 /*
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.
125  */
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 */
132
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)
138
139 /*
140  * Internal events.
141  */
142 #define ISC_SOCKEVENT_INTR      (ISC_EVENTCLASS_SOCKET + 256)
143 #define ISC_SOCKEVENT_INTW      (ISC_EVENTCLASS_SOCKET + 257)
144
145 typedef enum {
146         isc_sockettype_udp = 1,
147         isc_sockettype_tcp = 2
148 } isc_sockettype_t;
149
150 /*
151  * How a socket should be shutdown in isc_socket_shutdown() calls.
152  */
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 */
156
157 /*
158  * What I/O events to cancel in isc_socket_cancel() calls.
159  */
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 */
165
166 /*
167  * Flags for isc_socket_send() and isc_socket_recv() calls.
168  */
169 #define ISC_SOCKFLAG_IMMEDIATE  0x00000001      /* send event only if needed */
170 #define ISC_SOCKFLAG_NORETRY    0x00000002      /* drop failed UDP sends */
171
172 /***
173  *** Socket and Socket Manager Functions
174  ***
175  *** Note: all Ensures conditions apply only if the result is success for
176  *** those functions which return an isc_result.
177  ***/
178
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);
184 /*
185  * Create a new 'type' socket managed by 'manager'.
186  *
187  * Note:
188  *
189  *      'pf' is the desired protocol family, e.g. PF_INET or PF_INET6.
190  *
191  * Requires:
192  *
193  *      'manager' is a valid manager
194  *
195  *      'socketp' is a valid pointer, and *socketp == NULL
196  *
197  * Ensures:
198  *
199  *      '*socketp' is attached to the newly created socket
200  *
201  * Returns:
202  *
203  *      ISC_R_SUCCESS
204  *      ISC_R_NOMEMORY
205  *      ISC_R_NORESOURCES
206  *      ISC_R_UNEXPECTED
207  */
208
209 void
210 isc_socket_cancel(isc_socket_t *sock, isc_task_t *task,
211                   unsigned int how);
212 /*
213  * Cancel pending I/O of the type specified by "how".
214  *
215  * Note: if "task" is NULL, then the cancel applies to all tasks using the
216  * socket.
217  *
218  * Requires:
219  *
220  *      "socket" is a valid socket
221  *
222  *      "task" is NULL or a valid task
223  *
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.
227  *
228  * ISC_SOCKCANCEL_RECV:
229  *      Cancel pending isc_socket_recv() calls.
230  *
231  * ISC_SOCKCANCEL_SEND:
232  *      Cancel pending isc_socket_send() and isc_socket_sendto() calls.
233  *
234  * ISC_SOCKCANCEL_ACCEPT:
235  *      Cancel pending isc_socket_accept() calls.
236  *
237  * ISC_SOCKCANCEL_CONNECT:
238  *      Cancel pending isc_socket_connect() call.
239  */
240
241 void
242 isc_socket_shutdown(isc_socket_t *sock, unsigned int how);
243 /*
244  * Shutdown 'socket' according to 'how'.
245  *
246  * Requires:
247  *
248  *      'socket' is a valid socket.
249  *
250  *      'task' is NULL or is a valid task.
251  *
252  *      If 'how' is 'ISC_SOCKSHUT_RECV' or 'ISC_SOCKSHUT_ALL' then
253  *
254  *              The read queue must be empty.
255  *
256  *              No further read requests may be made.
257  *
258  *      If 'how' is 'ISC_SOCKSHUT_SEND' or 'ISC_SOCKSHUT_ALL' then
259  *
260  *              The write queue must be empty.
261  *
262  *              No further write requests may be made.
263  */
264
265 void
266 isc_socket_attach(isc_socket_t *sock, isc_socket_t **socketp);
267 /*
268  * Attach *socketp to socket.
269  *
270  * Requires:
271  *
272  *      'socket' is a valid socket.
273  *
274  *      'socketp' points to a NULL socket.
275  *
276  * Ensures:
277  *
278  *      *socketp is attached to socket.
279  */
280
281 void
282 isc_socket_detach(isc_socket_t **socketp);
283 /*
284  * Detach *socketp from its socket.
285  *
286  * Requires:
287  *
288  *      'socketp' points to a valid socket.
289  *
290  *      If '*socketp' is the last reference to the socket,
291  *      then:
292  *
293  *              There must be no pending I/O requests.
294  *
295  * Ensures:
296  *
297  *      *socketp is NULL.
298  *
299  *      If '*socketp' is the last reference to the socket,
300  *      then:
301  *
302  *              The socket will be shutdown (both reading and writing)
303  *              for all tasks.
304  *
305  *              All resources used by the socket have been freed
306  */
307
308 isc_result_t
309 isc_socket_bind(isc_socket_t *sock, isc_sockaddr_t *addressp);
310 /*
311  * Bind 'socket' to '*addressp'.
312  *
313  * Requires:
314  *
315  *      'socket' is a valid socket
316  *
317  *      'addressp' points to a valid isc_sockaddr.
318  *
319  * Returns:
320  *
321  *      ISC_R_SUCCESS
322  *      ISC_R_NOPERM
323  *      ISC_R_ADDRNOTAVAIL
324  *      ISC_R_ADDRINUSE
325  *      ISC_R_BOUND
326  *      ISC_R_UNEXPECTED
327  */
328
329 isc_result_t
330 isc_socket_listen(isc_socket_t *sock, unsigned int backlog);
331 /*
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().
334  *
335  * Notes:
336  *
337  *      'backlog' is as in the UNIX system call listen() and may be
338  *      ignored by non-UNIX implementations.
339  *
340  *      If 'backlog' is zero, a reasonable system default is used, usually
341  *      SOMAXCONN.
342  *
343  * Requires:
344  *
345  *      'socket' is a valid, bound TCP socket.
346  *
347  * Returns:
348  *
349  *      ISC_R_SUCCESS
350  *      ISC_R_UNEXPECTED
351  */
352
353 isc_result_t
354 isc_socket_accept(isc_socket_t *sock,
355                   isc_task_t *task, isc_taskaction_t action, const void *arg);
356 /*
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'.
361  *
362  * REQUIRES:
363  *      'socket' is a valid TCP socket that isc_socket_listen() was called
364  *      on.
365  *
366  *      'task' is a valid task
367  *
368  *      'action' is a valid action
369  *
370  * RETURNS:
371  *      ISC_R_SUCCESS
372  *      ISC_R_NOMEMORY
373  *      ISC_R_UNEXPECTED
374  */
375
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);
380 /*
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'.
384  *
385  * Requires:
386  *
387  *      'socket' is a valid TCP socket
388  *
389  *      'addressp' points to a valid isc_sockaddr
390  *
391  *      'task' is a valid task
392  *
393  *      'action' is a valid action
394  *
395  * Returns:
396  *
397  *      ISC_R_SUCCESS
398  *      ISC_R_NOMEMORY
399  *      ISC_R_UNEXPECTED
400  *
401  * Posted event's result code:
402  *
403  *      ISC_R_SUCCESS
404  *      ISC_R_TIMEDOUT
405  *      ISC_R_CONNREFUSED
406  *      ISC_R_NETUNREACH
407  *      ISC_R_UNEXPECTED
408  */
409
410 isc_result_t
411 isc_socket_getpeername(isc_socket_t *sock, isc_sockaddr_t *addressp);
412 /*
413  * Get the name of the peer connected to 'socket'.
414  *
415  * Requires:
416  *
417  *      'socket' is a valid TCP socket.
418  *
419  * Returns:
420  *
421  *      ISC_R_SUCCESS
422  *      ISC_R_TOOSMALL
423  *      ISC_R_UNEXPECTED
424  */
425
426 isc_result_t
427 isc_socket_getsockname(isc_socket_t *sock, isc_sockaddr_t *addressp);
428 /*
429  * Get the name of 'socket'.
430  *
431  * Requires:
432  *
433  *      'socket' is a valid socket.
434  *
435  * Returns:
436  *
437  *      ISC_R_SUCCESS
438  *      ISC_R_TOOSMALL
439  *      ISC_R_UNEXPECTED
440  */
441
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);
450
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);
455
456 /*
457  * Receive from 'socket', storing the results in region.
458  *
459  * Notes:
460  *
461  *      Let 'length' refer to the length of 'region' or to the sum of all
462  *      available regions in the list of buffers '*buflist'.
463  *
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.
469  *
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'.
474  *
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.
478  *
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.
483  *
484  *      For isc_socket_recv2():
485  *      'event' is not NULL, and the non-socket specific fields are
486  *      expected to be initialized.
487  *
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.
495  *
496  * Requires:
497  *
498  *      'socket' is a valid, bound socket.
499  *
500  *      For isc_socket_recv():
501  *      'region' is a valid region
502  *
503  *      For isc_socket_recvv():
504  *      'buflist' is non-NULL, and '*buflist' contain at least one buffer.
505  *
506  *      'task' is a valid task
507  *
508  *      For isc_socket_recv() and isc_socket_recvv():
509  *      action != NULL and is a valid action
510  *
511  *      For isc_socket_recv2():
512  *      event != NULL
513  *
514  * Returns:
515  *
516  *      ISC_R_SUCCESS
517  *      ISC_R_INPROGRESS
518  *      ISC_R_NOMEMORY
519  *      ISC_R_UNEXPECTED
520  *
521  * Event results:
522  *
523  *      ISC_R_SUCCESS
524  *      ISC_R_UNEXPECTED
525  *      XXX needs other net-type errors
526  */
527
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);
547
548 /*
549  * Send the contents of 'region' to the socket's peer.
550  *
551  * Notes:
552  *
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.
556  *
557  *      If 'action' is NULL, then no completion event will be posted.
558  *
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.
562  *
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.
567  *
568  *      For isc_socket_sendto2():
569  *      'event' is not NULL, and the non-socket specific fields are
570  *      expected to be initialized.
571  *
572  *      For isc_socket_sendto2():
573  *      The only defined values for 'flags' are ISC_SOCKFLAG_IMMEDIATE
574  *      and ISC_SOCKFLAG_NORETRY.
575  *
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.
581  *
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.
587  *
588  * Requires:
589  *
590  *      'socket' is a valid, bound socket.
591  *
592  *      For isc_socket_send():
593  *      'region' is a valid region
594  *
595  *      For isc_socket_sendv() and isc_socket_sendtov():
596  *      'buflist' is non-NULL, and '*buflist' contain at least one buffer.
597  *
598  *      'task' is a valid task
599  *
600  *      For isc_socket_sendv(), isc_socket_sendtov(), isc_socket_send(), and
601  *      isc_socket_sendto():
602  *      action == NULL or is a valid action
603  *
604  *      For isc_socket_sendto2():
605  *      event != NULL
606  *
607  * Returns:
608  *
609  *      ISC_R_SUCCESS
610  *      ISC_R_INPROGRESS
611  *      ISC_R_NOMEMORY
612  *      ISC_R_UNEXPECTED
613  *
614  * Event results:
615  *
616  *      ISC_R_SUCCESS
617  *      ISC_R_UNEXPECTED
618  *      XXX needs other net-type errors
619  */
620
621 isc_result_t
622 isc_socketmgr_create(isc_mem_t *mctx, isc_socketmgr_t **managerp);
623 /*
624  * Create a socket manager.
625  *
626  * Notes:
627  *
628  *      All memory will be allocated in memory context 'mctx'.
629  *
630  * Requires:
631  *
632  *      'mctx' is a valid memory context.
633  *
634  *      'managerp' points to a NULL isc_socketmgr_t.
635  *
636  * Ensures:
637  *
638  *      '*managerp' is a valid isc_socketmgr_t.
639  *
640  * Returns:
641  *
642  *      ISC_R_SUCCESS
643  *      ISC_R_NOMEMORY
644  *      ISC_R_UNEXPECTED
645  */
646
647 void
648 isc_socketmgr_destroy(isc_socketmgr_t **managerp);
649 /*
650  * Destroy a socket manager.
651  *
652  * Notes:
653  *
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.
658  *
659  * Requires:
660  *
661  *      '*managerp' is a valid isc_socketmgr_t.
662  *
663  *      All sockets managed by this manager are fully detached.
664  *
665  * Ensures:
666  *
667  *      *managerp == NULL
668  *
669  *      All resources used by the manager have been freed.
670  */
671
672 isc_sockettype_t
673 isc_socket_gettype(isc_socket_t *sock);
674 /*
675  * Returns the socket type for "sock."
676  *
677  * Requires:
678  *
679  *      "sock" is a valid socket.
680  */
681
682 isc_boolean_t
683 isc_socket_isbound(isc_socket_t *sock);
684
685 ISC_LANG_ENDDECLS
686
687 #endif /* ISC_SOCKET_H */