* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
* See the COPYRIGHT file distributed with this work for additional
* information regarding copyright ownership.
* DNS Dispatch Management
* Shared UDP and single-use TCP dispatches for queries and responses.
*\li All locking is performed internally to each dispatch.
* Restrictions apply to dns_dispatch_removeresponse().
*\li Depends on the isc_socket_t and dns_message_t for prevention of
* This event is sent to a task when a response comes in.
* No part of this structure should ever be modified by the caller,
* other than parts of the buffer. The holy parts of the buffer are
* the base and size of the buffer. All other parts of the buffer may
* be used. On event delivery the used region contains the packet.
* "id" is the received message id,
* "addr" is the host that sent it to us,
* "buffer" holds state on the received data.
* The "free" routine for this event will clean up itself as well as
* any buffer space allocated from common pools.
struct dns_dispatchevent {
ISC_EVENT_COMMON(dns_dispatchevent_t); /*%< standard event common */
isc_result_t result; /*%< result code */
int32_t id; /*%< message id */
isc_sockaddr_t addr; /*%< address recv'd from */
struct in6_pktinfo pktinfo; /*%< reply info for v6 */
isc_buffer_t buffer; /*%< data buffer */
uint32_t attributes; /*%< mirrored from socket.h */
* This is a set of one or more dispatches which can be retrieved
dns_dispatch_t **dispatches;
* Attributes for added dispatchers.
* Values with the mask 0xffff0000 are application defined.
* Values with the mask 0x0000ffff are library defined.
* Insane values (like setting both TCP and UDP) are not caught. Don't
* The dispatcher cannot be shared.
* The dispatcher is a TCP or UDP socket.
* The dispatcher uses an IPv4 or IPv6 socket.
* The dispatcher should not listen on the socket.
* The dispatcher can be used to issue queries to other servers, and
* accept replies from them.
* Previously used to indicate that the port of a dispatch UDP must be
* chosen randomly. This behavior now always applies and the attribute
* A separate socket will be used on-demand for each transaction.
#define DNS_DISPATCHATTR_PRIVATE 0x00000001U
#define DNS_DISPATCHATTR_TCP 0x00000002U
#define DNS_DISPATCHATTR_UDP 0x00000004U
#define DNS_DISPATCHATTR_IPV4 0x00000008U
#define DNS_DISPATCHATTR_IPV6 0x00000010U
#define DNS_DISPATCHATTR_NOLISTEN 0x00000020U
#define DNS_DISPATCHATTR_MAKEQUERY 0x00000040U
#define DNS_DISPATCHATTR_CONNECTED 0x00000080U
#define DNS_DISPATCHATTR_FIXEDID 0x00000100U
#define DNS_DISPATCHATTR_EXCLUSIVE 0x00000200U
#define DNS_DISPATCHOPT_FIXEDID 0x00000001U
dns_dispatchmgr_create(isc_mem_t *mctx, isc_entropy_t *entropy,
dns_dispatchmgr_t **mgrp);
* Creates a new dispatchmgr object.
*\li "mctx" be a valid memory context.
*\li mgrp != NULL && *mgrp == NULL
*\li "entropy" may be NULL, in which case an insecure random generator
* will be used. If it is non-NULL, it must be a valid entropy
*\li ISC_R_SUCCESS -- all ok
*\li anything else -- failure
dns_dispatchmgr_destroy(dns_dispatchmgr_t **mgrp);
* Destroys the dispatchmgr when it becomes empty. This could be
*\li mgrp != NULL && *mgrp is a valid dispatchmgr.
dns_dispatchmgr_setblackhole(dns_dispatchmgr_t *mgr, dns_acl_t *blackhole);
* Sets the dispatcher's "blackhole list," a list of addresses that will
* be ignored by all dispatchers created by the dispatchmgr.
* \li mgrp is a valid dispatchmgr
* \li blackhole is a valid acl
dns_dispatchmgr_getblackhole(dns_dispatchmgr_t *mgr);
* Gets a pointer to the dispatcher's current blackhole list,
* without incrementing its reference count.
*\li mgr is a valid dispatchmgr
*\li A pointer to the current blackhole list, or NULL.
dns_dispatchmgr_setblackportlist(dns_dispatchmgr_t *mgr,
dns_portlist_t *portlist);
* This function is deprecated. Use dns_dispatchmgr_setavailports() instead.
*\li mgr is a valid dispatchmgr
dns_dispatchmgr_getblackportlist(dns_dispatchmgr_t *mgr);
* This function is deprecated and always returns NULL.
*\li mgr is a valid dispatchmgr
dns_dispatchmgr_setavailports(dns_dispatchmgr_t *mgr, isc_portset_t *v4portset,
isc_portset_t *v6portset);
* Sets a list of UDP ports that can be used for outgoing UDP messages.
*\li mgr is a valid dispatchmgr
*\li v4portset is NULL or a valid port set
*\li v6portset is NULL or a valid port set
dns_dispatchmgr_setstats(dns_dispatchmgr_t *mgr, isc_stats_t *stats);
* Sets statistics counter for the dispatchmgr. This function is expected to
* be called only on zone creation (when necessary).
* Once installed, it cannot be removed or replaced. Also, there is no
* interface to get the installed stats from the zone; the caller must keep the
* stats to reference (e.g. dump) it later.
*\li mgr is a valid dispatchmgr with no managed dispatch.
*\li stats is a valid statistics supporting resolver statistics counters
dns_dispatch_getudp(dns_dispatchmgr_t *mgr, isc_socketmgr_t *sockmgr,
isc_taskmgr_t *taskmgr, isc_sockaddr_t *localaddr,
unsigned int maxbuffers, unsigned int maxrequests,
unsigned int buckets, unsigned int increment,
unsigned int attributes, unsigned int mask,
dns_dispatch_getudp_dup(dns_dispatchmgr_t *mgr, isc_socketmgr_t *sockmgr,
isc_taskmgr_t *taskmgr, isc_sockaddr_t *localaddr,
unsigned int maxbuffers, unsigned int maxrequests,
unsigned int buckets, unsigned int increment,
unsigned int attributes, unsigned int mask,
dns_dispatch_t **dispp, dns_dispatch_t *dup);
* Attach to existing dns_dispatch_t if one is found with dns_dispatchmgr_find,
* otherwise create a new UDP dispatch.
*\li All pointer parameters be valid for their respective types.
*\li dispp != NULL && *disp == NULL
*\li 512 <= buffersize <= 64k
*\li (attributes & DNS_DISPATCHATTR_TCP) == 0
*\li ISC_R_SUCCESS -- success.
*\li Anything else -- failure.
dns_dispatch_createtcp(dns_dispatchmgr_t *mgr, isc_socket_t *sock,
isc_taskmgr_t *taskmgr, unsigned int buffersize,
unsigned int maxbuffers, unsigned int maxrequests,
unsigned int buckets, unsigned int increment,
unsigned int attributes, dns_dispatch_t **dispp);
dns_dispatch_createtcp2(dns_dispatchmgr_t *mgr, isc_socket_t *sock,
isc_taskmgr_t *taskmgr, isc_sockaddr_t *localaddr,
isc_sockaddr_t *destaddr, unsigned int buffersize,
unsigned int maxbuffers, unsigned int maxrequests,
unsigned int buckets, unsigned int increment,
unsigned int attributes, dns_dispatch_t **dispp);
* Create a new dns_dispatch and attach it to the provided isc_socket_t.
* For all dispatches, "buffersize" is the maximum packet size we will
* "maxbuffers" and "maxrequests" control the number of buffers in the
* overall system and the number of buffers which can be allocated to
* "buckets" is the number of buckets to use, and should be prime.
* "increment" is used in a collision avoidance function, and needs to be
* a prime > buckets, and not 2.
*\li mgr is a valid dispatch manager.
*\li task is a valid task that can be used internally to this dispatcher.
* \li 512 <= buffersize <= 64k
*\li maxrequests <= maxbuffers.
*\li buckets < 2097169 (the next prime after 65536 * 32)
*\li increment > buckets (and prime).
*\li attributes includes #DNS_DISPATCHATTR_TCP and does not include
*\li ISC_R_SUCCESS -- success.
*\li Anything else -- failure.
dns_dispatch_attach(dns_dispatch_t *disp, dns_dispatch_t **dispp);
* Attach to a dispatch handle.
*\li dispp != NULL && *dispp == NULL
dns_dispatch_detach(dns_dispatch_t **dispp);
* Detaches from the dispatch.
*\li dispp != NULL and *dispp be a valid dispatch.
dns_dispatch_starttcp(dns_dispatch_t *disp);
* Start processing of a TCP dispatch once the socket connects.
dns_dispatch_gettcp(dns_dispatchmgr_t *mgr, isc_sockaddr_t *destaddr,
isc_sockaddr_t *localaddr, dns_dispatch_t **dispp);
dns_dispatch_gettcp2(dns_dispatchmgr_t *mgr, isc_sockaddr_t *destaddr,
isc_sockaddr_t *localaddr, bool *connected,
* Attempt to connect to a existing TCP connection (connection completed
* for dns_dispatch_gettcp()).
dns_dispatch_addresponse3(dns_dispatch_t *disp, unsigned int options,
isc_sockaddr_t *dest, isc_task_t *task,
isc_taskaction_t action, void *arg,
uint16_t *idp, dns_dispentry_t **resp,
isc_socketmgr_t *sockmgr);
dns_dispatch_addresponse2(dns_dispatch_t *disp, isc_sockaddr_t *dest,
isc_task_t *task, isc_taskaction_t action, void *arg,
uint16_t *idp, dns_dispentry_t **resp,
isc_socketmgr_t *sockmgr);
dns_dispatch_addresponse(dns_dispatch_t *disp, isc_sockaddr_t *dest,
isc_task_t *task, isc_taskaction_t action, void *arg,
uint16_t *idp, dns_dispentry_t **resp);
* Add a response entry for this dispatch.
* "*idp" is filled in with the assigned message ID, and *resp is filled in
* to contain the magic token used to request event flow stop.
* Arranges for the given task to get a callback for response packets. When
* the event is delivered, it must be returned using dns_dispatch_freeevent()
* or through dns_dispatch_removeresponse() for another to be delivered.
*\li "task" "action" and "arg" be set as appropriate.
*\li "dest" be non-NULL and valid.
*\li "resp" be non-NULL and *resp be NULL
*\li "sockmgr" be NULL or a valid socket manager. If 'disp' has
* the DNS_DISPATCHATTR_EXCLUSIVE attribute, this must not be NULL,
* which also means dns_dispatch_addresponse() cannot be used.
*\li <id, dest> is a unique tuple. That means incoming messages
*\li ISC_R_SUCCESS -- all is well.
*\li ISC_R_NOMEMORY -- memory could not be allocated.
*\li ISC_R_NOMORE -- no more message ids can be allocated
dns_dispatch_removeresponse(dns_dispentry_t **resp,
dns_dispatchevent_t **sockevent);
* Stops the flow of responses for the provided id and destination.
* If "sockevent" is non-NULL, the dispatch event and associated buffer is
* also returned to the system.
*\li "resp" != NULL and "*resp" contain a value previously allocated
* by dns_dispatch_addresponse();
*\li May only be called from within the task given as the 'task'
* argument to dns_dispatch_addresponse() when allocating '*resp'.
dns_dispatch_getentrysocket(dns_dispentry_t *resp);
dns_dispatch_getsocket(dns_dispatch_t *disp);
* Return the socket associated with this dispatcher.
*\li The socket the dispatcher is using.
dns_dispatch_getlocaladdress(dns_dispatch_t *disp, isc_sockaddr_t *addrp);
* Return the local address for this dispatch.
* This currently only works for dispatches using UDP sockets.
*\li addrp to be non null.
*\li ISC_R_NOTIMPLEMENTED
dns_dispatch_cancel(dns_dispatch_t *disp);
* cancel outstanding clients
dns_dispatch_getattributes(dns_dispatch_t *disp);