Merge from vendor branch CVS:
[dragonfly.git] / contrib / bind-9.2.4rc7 / lib / dns / include / dns / dispatch.h
1 /*
2  * Copyright (C) 2004  Internet Systems Consortium, Inc. ("ISC")
3  * Copyright (C) 1999-2003  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: dispatch.h,v 1.45.2.3 2004/03/09 06:11:15 marka Exp $ */
19
20 #ifndef DNS_DISPATCH_H
21 #define DNS_DISPATCH_H 1
22
23 /*****
24  ***** Module Info
25  *****/
26
27 /*
28  * DNS Dispatch Management
29  *
30  *      Shared UDP and single-use TCP dispatches for queries and responses.
31  *
32  * MP:
33  *
34  *      All locking is performed internally to each dispatch.
35  *      Restrictions apply to dns_dispatch_removeresponse().
36  *
37  * Reliability:
38  *
39  * Resources:
40  *
41  * Security:
42  *
43  *      Depends on the isc_socket_t and dns_message_t for prevention of
44  *      buffer overruns.
45  *
46  * Standards:
47  *
48  *      None.
49  */
50
51 /***
52  *** Imports
53  ***/
54
55 #include <isc/buffer.h>
56 #include <isc/lang.h>
57 #include <isc/socket.h>
58 #include <dns/types.h>
59
60 #include <dns/types.h>
61
62 ISC_LANG_BEGINDECLS
63
64 /*
65  * This event is sent to a task when a response comes in.
66  * No part of this structure should ever be modified by the caller,
67  * other than parts of the buffer.  The holy parts of the buffer are
68  * the base and size of the buffer.  All other parts of the buffer may
69  * be used.  On event delivery the used region contains the packet.
70  *
71  * "id" is the received message id,
72  *
73  * "addr" is the host that sent it to us,
74  *
75  * "buffer" holds state on the received data.
76  *
77  * The "free" routine for this event will clean up itself as well as
78  * any buffer space allocated from common pools.
79  */
80
81 struct dns_dispatchevent {
82         ISC_EVENT_COMMON(dns_dispatchevent_t);  /* standard event common */
83         isc_result_t            result;         /* result code */
84         isc_int32_t             id;             /* message id */
85         isc_sockaddr_t          addr;           /* address recv'd from */
86         struct in6_pktinfo      pktinfo;        /* reply info for v6 */
87         isc_buffer_t            buffer;         /* data buffer */
88         isc_uint32_t            attributes;     /* mirrored from socket.h */
89 };
90
91 /*
92  * Attributes for added dispatchers.
93  *
94  * Values with the mask 0xffff0000 are application defined.
95  * Values with the mask 0x0000ffff are library defined.
96  *
97  * Insane values (like setting both TCP and UDP) are not caught.  Don't
98  * do that.
99  *
100  * _PRIVATE
101  *      The dispatcher cannot be shared.
102  *
103  * _TCP, _UDP
104  *      The dispatcher is a TCP or UDP socket.
105  *
106  * _IPV4, _IPV6
107  *      The dispatcher uses an ipv4 or ipv6 socket.
108  *
109  * _NOLISTEN
110  *      The dispatcher should not listen on the socket.
111  *
112  * _MAKEQUERY
113  *      The dispatcher can be used to issue queries to other servers, and
114  *      accept replies from them.
115  */
116 #define DNS_DISPATCHATTR_PRIVATE        0x00000001U
117 #define DNS_DISPATCHATTR_TCP            0x00000002U
118 #define DNS_DISPATCHATTR_UDP            0x00000004U
119 #define DNS_DISPATCHATTR_IPV4           0x00000008U
120 #define DNS_DISPATCHATTR_IPV6           0x00000010U
121 #define DNS_DISPATCHATTR_NOLISTEN       0x00000020U
122 #define DNS_DISPATCHATTR_MAKEQUERY      0x00000040U
123 #define DNS_DISPATCHATTR_CONNECTED      0x00000080U
124
125 isc_result_t
126 dns_dispatchmgr_create(isc_mem_t *mctx, isc_entropy_t *entropy,
127                        dns_dispatchmgr_t **mgrp);
128 /*
129  * Creates a new dispatchmgr object.
130  *
131  * Requires:
132  *      "mctx" be a valid memory context.
133  *
134  *      mgrp != NULL && *mgrp == NULL
135  *
136  *      "entropy" may be NULL, in which case an insecure random generator
137  *      will be used.  If it is non-NULL, it must be a valid entropy
138  *      source.
139  *
140  * Returns:
141  *      ISC_R_SUCCESS   -- all ok
142  *
143  *      anything else   -- failure
144  */
145
146
147 void
148 dns_dispatchmgr_destroy(dns_dispatchmgr_t **mgrp);
149 /*
150  * Destroys the dispatchmgr when it becomes empty.  This could be
151  * immediately.
152  *
153  * Requires:
154  *      mgrp != NULL && *mgrp is a valid dispatchmgr.
155  */
156
157
158 void
159 dns_dispatchmgr_setblackhole(dns_dispatchmgr_t *mgr, dns_acl_t *blackhole);
160 /*
161  * Sets the dispatcher's "blackhole list," a list of addresses that will
162  * be ignored by all dispatchers created by the dispatchmgr.
163  *
164  * Requires:
165  *      mgrp is a valid dispatchmgr
166  *      blackhole is a valid acl
167  */
168
169
170 dns_acl_t *
171 dns_dispatchmgr_getblackhole(dns_dispatchmgr_t *mgr);
172 /*
173  * Gets a pointer to the dispatcher's current blackhole list,
174  * without incrementing its reference count.
175  *
176  * Requires:
177  *      mgr is a valid dispatchmgr
178  * Returns:
179  *      A pointer to the current blackhole list, or NULL.
180  */
181
182
183 isc_result_t
184 dns_dispatch_getudp(dns_dispatchmgr_t *mgr, isc_socketmgr_t *sockmgr,
185                     isc_taskmgr_t *taskmgr, isc_sockaddr_t *localaddr,
186                     unsigned int buffersize,
187                     unsigned int maxbuffers, unsigned int maxrequests,
188                     unsigned int buckets, unsigned int increment,
189                     unsigned int attributes, unsigned int mask,
190                     dns_dispatch_t **dispp);
191 /*
192  * Attach to existing dns_dispatch_t if one is found with dns_dispatchmgr_find,
193  * otherwise create a new UDP dispatch.
194  *
195  * Requires:
196  *      All pointer parameters be valid for their respective types.
197  *
198  *      dispp != NULL && *disp == NULL
199  *
200  *      512 <= buffersize <= 64k
201  *
202  *      maxbuffers > 0
203  *
204  *      buckets < 2097169
205  *
206  *      increment > buckets
207  *
208  *      (attributes & DNS_DISPATCHATTR_TCP) == 0
209  *
210  * Returns:
211  *      ISC_R_SUCCESS   -- success.
212  *
213  *      Anything else   -- failure.
214  */
215
216 isc_result_t
217 dns_dispatch_createtcp(dns_dispatchmgr_t *mgr, isc_socket_t *sock,
218                        isc_taskmgr_t *taskmgr, unsigned int buffersize,
219                        unsigned int maxbuffers, unsigned int maxrequests,
220                        unsigned int buckets, unsigned int increment,
221                        unsigned int attributes, dns_dispatch_t **dispp);
222 /*
223  * Create a new dns_dispatch and attach it to the provided isc_socket_t.
224  *
225  * For all dispatches, "buffersize" is the maximum packet size we will
226  * accept.
227  *
228  * "maxbuffers" and "maxrequests" control the number of buffers in the
229  * overall system and the number of buffers which can be allocated to
230  * requests.
231  *
232  * "buckets" is the number of buckets to use, and should be prime.
233  *
234  * "increment" is used in a collision avoidance function, and needs to be
235  * a prime > buckets, and not 2.
236  *
237  * Requires:
238  *
239  *      mgr is a valid dispatch manager.
240  *
241  *      sock is a valid.
242  *
243  *      task is a valid task that can be used internally to this dispatcher.
244  *
245  *      512 <= buffersize <= 64k
246  *
247  *      maxbuffers > 0.
248  *
249  *      maxrequests <= maxbuffers.
250  *
251  *      buckets < 2097169 (the next prime after 65536 * 32)
252  *
253  *      increment > buckets (and prime).
254  *
255  *      attributes includes DNS_DISPATCHATTR_TCP and does not include
256  *      DNS_DISPATCHATTR_UDP.
257  *
258  * Returns:
259  *      ISC_R_SUCCESS   -- success.
260  *
261  *      Anything else   -- failure.
262  */
263
264 void
265 dns_dispatch_attach(dns_dispatch_t *disp, dns_dispatch_t **dispp);
266 /*
267  * Attach to a dispatch handle.
268  *
269  * Requires:
270  *      disp is valid.
271  *
272  *      dispp != NULL && *dispp == NULL
273  */
274
275 void
276 dns_dispatch_detach(dns_dispatch_t **dispp);
277 /*
278  * Detaches from the dispatch.
279  *
280  * Requires:
281  *      dispp != NULL and *dispp be a valid dispatch.
282  */
283
284 void
285 dns_dispatch_starttcp(dns_dispatch_t *disp);
286 /*
287  * Start processing of a TCP dispatch once the socket connects.
288  *
289  * Requires:
290  *      'disp' is valid.
291  */
292
293 isc_result_t
294 dns_dispatch_addresponse(dns_dispatch_t *disp, isc_sockaddr_t *dest,
295                          isc_task_t *task, isc_taskaction_t action, void *arg,
296                          isc_uint16_t *idp, dns_dispentry_t **resp);
297 /*
298  * Add a response entry for this dispatch.
299  *
300  * "*idp" is filled in with the assigned message ID, and *resp is filled in
301  * to contain the magic token used to request event flow stop.
302  *
303  * Arranges for the given task to get a callback for response packets.  When
304  * the event is delivered, it must be returned using dns_dispatch_freeevent()
305  * or through dns_dispatch_removeresponse() for another to be delivered.
306  *
307  * Requires:
308  *      "idp" be non-NULL.
309  *
310  *      "task" "action" and "arg" be set as appropriate.
311  *
312  *      "dest" be non-NULL and valid.
313  *
314  *      "resp" be non-NULL and *resp be NULL
315  *
316  * Ensures:
317  *
318  *      <id, dest> is a unique tuple.  That means incoming messages
319  *      are identifiable.
320  *
321  * Returns:
322  *
323  *      ISC_R_SUCCESS           -- all is well.
324  *      ISC_R_NOMEMORY          -- memory could not be allocated.
325  *      ISC_R_NOMORE            -- no more message ids can be allocated
326  *                                 for this destination.
327  */
328
329
330 void
331 dns_dispatch_removeresponse(dns_dispentry_t **resp,
332                             dns_dispatchevent_t **sockevent);
333 /*
334  * Stops the flow of responses for the provided id and destination.
335  * If "sockevent" is non-NULL, the dispatch event and associated buffer is
336  * also returned to the system.
337  *
338  * Requires:
339  *      "resp" != NULL and "*resp" contain a value previously allocated
340  *      by dns_dispatch_addresponse();
341  *
342  *      May only be called from within the task given as the 'task' 
343  *      argument to dns_dispatch_addresponse() when allocating '*resp'.
344  */
345
346
347 isc_socket_t *
348 dns_dispatch_getsocket(dns_dispatch_t *disp);
349 /*
350  * Return the socket associated with this dispatcher.
351  *
352  * Requires:
353  *      disp is valid.
354  *
355  * Returns:
356  *      The socket the dispatcher is using.
357  */
358
359 isc_result_t 
360 dns_dispatch_getlocaladdress(dns_dispatch_t *disp, isc_sockaddr_t *addrp);
361 /*
362  * Return the local address for this dispatch.
363  * This currently only works for dispatches using UDP sockets.
364  *
365  * Requires:
366  *      disp is valid.
367  *      addrp to be non null.
368  *
369  * Returns:
370  *      ISC_R_SUCCESS   
371  *      ISC_R_NOTIMPLEMENTED
372  */
373
374 void
375 dns_dispatch_cancel(dns_dispatch_t *disp);
376 /*
377  * cancel outstanding clients
378  *
379  * Requires:
380  *      disp is valid.
381  */
382
383 void
384 dns_dispatch_changeattributes(dns_dispatch_t *disp,
385                               unsigned int attributes, unsigned int mask);
386 /*
387  * Set the bits described by "mask" to the corresponding values in
388  * "attributes".
389  *
390  * That is:
391  *
392  *      new = (old & ~mask) | (attributes & mask)
393  *
394  * This function has a side effect when DNS_DISPATCHATTR_NOLISTEN changes. 
395  * When the flag becomes off, the dispatch will start receiving on the
396  * corresponding socket.  When the flag becomes on, receive events on the
397  * corresponding socket will be canceled.
398  *
399  * Requires:
400  *      disp is valid.
401  *
402  *      attributes are reasonable for the dispatch.  That is, setting the UDP
403  *      attribute on a TCP socket isn't reasonable.
404  */
405
406 void
407 dns_dispatch_importrecv(dns_dispatch_t *disp, isc_event_t *event);
408 /*
409  * Inform the dispatcher of a socket receive.  This is used for sockets
410  * shared between dispatchers and clients.  If the dispatcher fails to copy
411  * or send the event, nothing happens.
412  *
413  * Requires:
414  *      disp is valid, and the attribute DNS_DISPATCHATTR_NOLISTEN is set.
415  *      event != NULL
416  */
417
418 ISC_LANG_ENDDECLS
419
420 #endif /* DNS_DISPATCH_H */