dispatch.c revision 0c27b3fe77ac1d5094ba3521e8142d9e7973133f
/*
* Copyright (C) 1999-2009, 2011-2016 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 http://mozilla.org/MPL/2.0/.
*/
/*! \file */
#include <config.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdlib.h>
#include <dns/dispatch.h>
#include <dns/portlist.h>
typedef struct dispsocket dispsocket_t;
typedef struct dispportentry dispportentry_t;
typedef struct dns_qid {
unsigned int magic;
unsigned int qid_nbuckets; /*%< hash table size */
unsigned int qid_increment; /*%< id increment on collision */
} dns_qid_t;
struct dns_dispatchmgr {
/* Unlocked. */
unsigned int magic;
/* Locked by "lock". */
unsigned int state;
/* Locked by rng_lock. */
/* locked by buffer_lock */
unsigned int buffers; /*%< allocated buffers */
unsigned int buffersize; /*%< size of each buffer */
unsigned int maxbuffers; /*%< max buffers */
/* Locked internally. */
/*%
* Locked by qid->lock if qid exists; otherwise, can be used without
* being locked.
* Memory footprint considerations: this is a simple implementation of
* available ports, i.e., an ordered array of the actual port numbers.
* This will require about 256KB of memory in the worst case (128KB for
* each of IPv4 and IPv6). We could reduce it by representing it as a
* more sophisticated way such as a list (or array) of ranges that are
* searched to identify a specific port. Our decision here is the saved
* memory isn't worth the implementation complexity, considering the
* fact that the whole BIND9 process (which is mainly named) already
* requires a pretty large memory footprint. We may, however, have to
* revisit the decision when we want to use it as a separate module for
* an environment where memory requirement is severer.
*/
unsigned int nv4ports; /*%< # of available ports for IPv4 */
unsigned int nv6ports; /*%< # of available ports for IPv4 */
};
#define MGR_SHUTTINGDOWN 0x00000001U
struct dns_dispentry {
unsigned int magic;
unsigned int bucket;
void *arg;
};
/*%
* Maximum number of dispatch sockets that can be pooled for reuse. The
* appropriate value may vary, but experiments have shown a busy caching server
* may need more than 1000 sockets concurrently opened. The maximum allowable
* number of dispatch sockets (per manager) will be set to the double of this
* value.
*/
#ifndef DNS_DISPATCH_POOLSOCKS
#define DNS_DISPATCH_POOLSOCKS 2048
#endif
/*%
* Quota to control the number of dispatch sockets. If a dispatch has more
* than the quota of sockets, new queries will purge oldest ones, so that
* a massive number of outstanding queries won't prevent subsequent queries
* (especially if the older ones take longer time and result in timeout).
*/
#ifndef DNS_DISPATCH_SOCKSQUOTA
#define DNS_DISPATCH_SOCKSQUOTA 3072
#endif
struct dispsocket {
unsigned int magic;
unsigned int bucket;
};
/*%
* A port table entry. We remember every port we first open in a table with a
* reference counter so that we can 'reuse' the same port (with different
* destination addresses) using the SO_REUSEADDR socket option.
*/
struct dispportentry {
unsigned int refs;
};
#ifndef DNS_DISPATCH_PORTTABLESIZE
#define DNS_DISPATCH_PORTTABLESIZE 1024
#endif
#define INVALID_BUCKET (0xffffdead)
/*%
* Number of tasks for each dispatch that use separate sockets for different
* transactions. This must be a power of 2 as it will divide 32 bit numbers
* to get an uniformly random tasks selection. See get_dispsocket().
*/
#define MAX_INTERNAL_TASKS 64
struct dns_dispatch {
/* Unlocked. */
unsigned int magic; /*%< magic */
int ntasks;
/*%
* internal task buckets. We use multiple tasks to distribute various
* socket events well when using separate dispatch sockets. We use the
* 1st task (task[0]) for internal control events.
*/
unsigned int maxrequests; /*%< max requests */
/*% Locked by mgr->lock. */
/* Locked by "lock". */
unsigned int attributes;
unsigned int refcount; /*%< number of users */
unsigned int shutting_down : 1,
shutdown_out : 1,
connected : 1,
tcpmsg_valid : 1,
unsigned int nsockets;
unsigned int requests; /*%< how many requests we have */
unsigned int tcpbuffers; /*%< allocated buffers */
};
/*%
* Locking a query port buffer is a bit tricky. We access the buffer without
* locking until qid is created. Technically, there is a possibility of race
* between the creation of qid and access to the port buffer; in practice,
* however, this should be safe because qid isn't created until the first
* dispatch is created and there should be no contending situation until then.
*/
/*
* Statics.
*/
dns_messageid_t, in_port_t, unsigned int);
unsigned int maxrequests,
unsigned int attributes,
#define LVL(x) ISC_LOG_DEBUG(x)
static void
static void
char msgbuf[2048];
return;
}
static inline void
}
static inline void
}
static void
static void
char msgbuf[2048];
return;
}
static void
static void
{
char msgbuf[2048];
char peerbuf[256];
return;
if (VALID_RESPONSE(resp)) {
} else {
msgbuf);
}
}
/*
* Return a hash of the destination and message id.
*/
static isc_uint32_t
{
unsigned int ret;
return (ret);
}
/*
* Find the first entry in 'qid'. Returns NULL if there are no entries.
*/
static dns_dispentry_t *
unsigned int bucket;
bucket = 0;
return (ret);
bucket++;
}
return (NULL);
}
/*
* Find the next entry after 'resp' in 'qid'. Return NULL if there are
* no more entries.
*/
static dns_dispentry_t *
unsigned int bucket;
return (ret);
bucket++;
return (ret);
bucket++;
}
return (NULL);
}
/*
* The dispatch must be locked.
*/
static isc_boolean_t
{
return (ISC_FALSE);
if (disp->recv_pending != 0)
return (ISC_FALSE);
return (ISC_FALSE);
if (disp->shutting_down == 0)
return (ISC_FALSE);
return (ISC_TRUE);
}
/*
* Called when refcount reaches 0 (and safe to destroy).
*
* The dispatcher must be locked.
* The manager must not be locked.
*/
static void
int i;
"shutting down; detaching from sock %p, task %p",
}
}
if (killmgr)
destroy_mgr(&mgr);
}
/*%
* Manipulate port table per dispatch: find an entry for a given port number,
* create a new entry, and decrement a given entry with possible clean-up.
*/
static dispportentry_t *
return (portentry);
}
return (NULL);
}
static dispportentry_t *
return (portentry);
return (portentry);
}
/*%
* The caller must not hold the qid->lock.
*/
static void
}
/*
* Set '*portentryp' to NULL inside the lock so that
* dispsock->portentry does not change in socket_search.
*/
*portentryp = NULL;
}
/*%
* Find a dispsocket for socket address 'dest', and port number 'port'.
* Return NULL if no such entry exists. Requires qid->lock to be held.
*/
static dispsocket_t *
unsigned int bucket)
{
return (dispsock);
}
return (NULL);
}
/*%
* Make a new socket for a single dispatch with a random port number.
* The caller must hold the disp->lock
*/
static isc_result_t
{
int i;
isc_uint32_t r;
unsigned int bucket = 0;
unsigned int nports;
unsigned int bindoptions;
} else {
}
if (nports == 0)
return (ISC_R_ADDRNOTAVAIL);
} else {
return (ISC_R_NOMEMORY);
isc_random_get(&r);
}
/*
* Pick up a random UDP port and open a new socket with it. Avoid
* choosing ports that share the same destination because it will be
* very likely to fail in bind(2) or connect(2).
*/
for (i = 0; i < 64; i++) {
continue;
}
bindoptions = 0;
NULL);
if (result == ISC_R_SUCCESS) {
break;
}
} else {
}
break;
} else if (result == ISC_R_NOPERM) {
char buf[ISC_SOCKADDR_FORMATSIZE];
"open_socket(%s) -> %s: continuing",
} else if (result != ISC_R_ADDRINUSE)
break;
}
if (result == ISC_R_SUCCESS) {
} else {
/*
* We could keep it in the inactive list, but since this should
* be an exceptional case and might be resource shortage, we'd
* rather destroy it.
*/
}
return (result);
}
/*%
* Destroy a dedicated dispatch socket.
*/
static void
/*
* The dispatch must be locked.
*/
blink);
}
}
/*%
* Deactivate a dedicated dispatch socket. Move it to the inactive list for
* future reuse unless the total number of sockets are exceeding the maximum.
*/
static void
/*
* The dispatch must be locked.
*/
}
else {
blink);
if (result == ISC_R_SUCCESS)
else {
/*
* If the underlying system does not allow this
* optimization, destroy this temporary structure (and
* create a new one for a new transaction).
*/
}
}
}
/*
* Find an entry for query ID 'id', socket address 'dest', and port number
* 'port'.
* Return NULL if no such entry exists.
*/
static dns_dispentry_t *
{
return (res);
}
}
return (NULL);
}
static void
case isc_sockettype_tcp:
disp->tcpbuffers--;
break;
case isc_sockettype_udp:
break;
default:
INSIST(0);
break;
}
}
static void *
void *temp;
}
return (temp);
}
static inline void
}
static inline isc_socketevent_t *
{
void *deconst_arg;
return (NULL);
ev->n = 0;
ev->attributes = 0;
return (ev);
}
static inline void
disp->shutdown_out = 0;
return;
}
}
static inline dns_dispatchevent_t *
return (NULL);
return (ev);
}
static void
}
static void
}
/*
* General flow:
*
* If I/O result == CANCELED or error, free the buffer.
*
* If query, free the buffer, restart.
*
* If response:
* Allocate event, fill in details.
* If cannot allocate, free buffer, restart.
* find target. If not found, free buffer, restart.
* if event queue is not empty, queue. else, send.
* restart.
*/
static void
unsigned int flags;
unsigned int bucket;
int match;
int result;
"got packet: requests %d, buffers %d, recvs %d",
/*
* Unless the receive event was imported from a listening
* interface, in which case the event type is
* DNS_EVENT_IMPORTRECVDONE, receive operation must be pending.
*/
disp->recv_pending = 0;
}
/*
* dispsock->resp can be NULL if this transaction was canceled
* just after receiving a response. Since this socket is
* exclusively used and there should be at most one receive
* event the canceled event should have been no effect. So
* we can (and should) deactivate the socket right now.
*/
}
if (disp->shutting_down) {
/*
* This dispatcher is shutting down.
*/
if (killit)
return;
}
/*
* This is most likely a network error on a
* connected socket. It makes no sense to
* check the address or parse the packet, but it
* will help to return the error to the caller.
*/
goto sendresponse;
}
} else {
return;
}
"odd socket result in udp_recv(): %s",
return;
}
/*
* If this is from a blackholed address, drop it.
*/
match > 0)
{
char netaddrstr[ISC_NETADDR_FORMATSIZE];
sizeof(netaddrstr));
"blackholed packet from %s",
}
goto restart;
}
/*
* Peek into the buffer to see what we can see.
*/
if (dres != ISC_R_SUCCESS) {
goto restart;
}
"got valid DNS message header, /QR %c, id %u",
/*
* Look at flags. If query, drop it. If response,
* look to see where it goes.
*/
if ((flags & DNS_MESSAGEFLAG_QR) == 0) {
/* query */
goto restart;
}
/*
* Search for the corresponding response. If we are using an exclusive
* socket, we've already identified it and we can skip the search; but
* the ID and the address must match the expected ones.
*/
bucket);
"search for response in bucket %d: %s",
goto unlock;
}
"response to an exclusive socket doesn't match");
goto unlock;
}
/*
* Now that we have the original dispatch the query was sent
* from check that the address and port the response was
* sent to make sense.
*/
/*
* Check that the socket types and ports match.
*/
goto unlock;
}
/*
* If each dispatch is bound to a different address
* then fail.
*
* Note under Linux a packet can be sent out via IPv4 socket
* and the response be received via a IPv6 socket.
*
* Requests sent out via IPv6 should always come back in
* via IPv6.
*/
goto unlock;
}
goto unlock;
}
}
goto unlock;
}
/*
* At this point, rev contains the event we want to fill in, and
* resp contains the information on the place to send it to.
* Send the event off.
*/
if (queue_response) {
} else {
"[a] Sent event %p buffer %p len %d to task %p",
}
if (qidlocked)
/*
* Restart recv() to get the next packet.
*/
/*
* XXX: wired. There seems to be no recovery process other than
* deactivate this socket anyway (since we cannot start
* receiving, we won't be able to receive a cancel event
* from the user).
*/
}
}
/*
* General flow:
*
* If I/O result == CANCELED, EOF, or error, notify everyone as the
* various queues drain.
*
* If query, restart.
*
* If response:
* Allocate event, fill in details.
* If cannot allocate, restart.
* find target. If not found, restart.
* if event queue is not empty, queue. else, send.
* restart.
*/
static void
unsigned int flags;
unsigned int bucket;
int level;
char buf[ISC_SOCKADDR_FORMATSIZE];
"got TCP packet: requests %d, buffers %d, recvs %d",
disp->recv_pending = 0;
/*
* This dispatcher is shutting down. Force cancelation.
*/
}
case ISC_R_CANCELED:
break;
case ISC_R_EOF:
break;
case ISC_R_CONNECTIONRESET:
goto logit;
default:
"receive error: %s: %s", buf,
break;
}
/*
* The event is statically allocated in the tcpmsg
* structure, and destroy_disp() frees the tcpmsg, so we must
* free the event *before* calling destroy_disp().
*/
/*
* If the recv() was canceled pass the word on.
*/
if (killit)
return;
}
/*
* Peek into the buffer to see what we can see.
*/
if (dres != ISC_R_SUCCESS) {
goto restart;
}
"got valid DNS message header, /QR %c, id %u",
/*
* Allocate an event to send to the query or response client, and
* allocate a new buffer for our use.
*/
/*
* Look at flags. If query, drop it. If response,
* look to see where it goes.
*/
if ((flags & DNS_MESSAGEFLAG_QR) == 0) {
/*
* Query.
*/
goto restart;
}
/*
* Response.
*/
"search for response in bucket %d: %s",
goto unlock;
goto unlock;
/*
* At this point, rev contains the event we want to fill in, and
* resp contains the information on the place to send it to.
* Send the event off.
*/
disp->tcpbuffers++;
if (queue_response) {
} else {
"[b] Sent event %p buffer %p len %d to task %p",
}
/*
* Restart recv() to get the next packet.
*/
}
/*
* disp must be locked.
*/
static isc_result_t
return (ISC_R_SUCCESS);
return (ISC_R_SUCCESS);
return (ISC_R_SUCCESS);
return (ISC_R_NOMEMORY);
return (ISC_R_SUCCESS);
else
/*
* UDP reads are always maximal.
*/
case isc_sockettype_udp:
return (ISC_R_NOMEMORY);
return (ISC_R_NOMEMORY);
}
if (res != ISC_R_SUCCESS) {
return (res);
}
} else {
udp_shrecv, disp);
return (ISC_R_NOMEMORY);
}
if (res != ISC_R_SUCCESS) {
return (ISC_R_SUCCESS); /* recover by cancel */
}
}
break;
case isc_sockettype_tcp:
if (res != ISC_R_SUCCESS) {
return (ISC_R_SUCCESS); /* recover by cancel */
}
break;
default:
INSIST(0);
break;
}
return (ISC_R_SUCCESS);
}
/*
* Mgr must be locked when calling this function.
*/
static isc_boolean_t
"destroy_mgr_ok: shuttingdown=%d, listnonempty=%d, "
"depool=%d, rpool=%d, dpool=%d",
if (!MGR_IS_SHUTTINGDOWN(mgr))
return (ISC_FALSE);
return (ISC_FALSE);
return (ISC_FALSE);
return (ISC_FALSE);
return (ISC_FALSE);
return (ISC_TRUE);
}
/*
* Mgr must be unlocked when calling this function.
*/
static void
}
}
}
static isc_result_t
{
if (result != ISC_R_SUCCESS)
return (result);
} else if (dup_socket != NULL) {
if (result != ISC_R_SUCCESS)
return (result);
return (ISC_R_SUCCESS);
} else {
if (result != ISC_R_SUCCESS)
return (result);
}
#ifndef ISC_ALLOW_MAPPED
#endif
if (result != ISC_R_SUCCESS) {
else {
}
return (result);
}
return (ISC_R_SUCCESS);
}
/*%
* Create a temporary port list to set the initial default set of dispatch
* ports: [1024, 65535]. This is almost meaningless as the application will
* normally set the ports explicitly, but is provided to fill some minor corner
* cases.
*/
static isc_result_t
if (result != ISC_R_SUCCESS)
return (result);
return (ISC_R_SUCCESS);
}
/*
* Publics.
*/
{
return (ISC_R_NOMEMORY);
if (result != ISC_R_SUCCESS)
goto deallocate;
if (result != ISC_R_SUCCESS)
goto kill_lock;
if (result != ISC_R_SUCCESS)
goto kill_rng_lock;
if (result != ISC_R_SUCCESS)
goto kill_buffer_lock;
if (result != ISC_R_SUCCESS)
goto kill_depool_lock;
if (result != ISC_R_SUCCESS)
goto kill_rpool_lock;
if (result != ISC_R_SUCCESS)
goto kill_dpool_lock;
if (result != ISC_R_SUCCESS)
goto kill_bpool_lock;
goto kill_spool_lock;
}
goto kill_depool;
}
goto kill_rpool;
}
mgr->buffersize = 0;
mgr->maxbuffers = 0;
if (result == ISC_R_SUCCESS) {
if (result == ISC_R_SUCCESS) {
}
}
if (result != ISC_R_SUCCESS)
goto kill_dpool;
if (result != ISC_R_SUCCESS)
goto kill_dpool;
return (ISC_R_SUCCESS);
return (result);
}
void
}
}
void
{
/* This function is deprecated: use dns_dispatchmgr_setavailports(). */
return;
}
return (NULL); /* this function is deprecated */
}
{
if (nv4ports != 0) {
return (ISC_R_NOMEMORY);
}
if (nv6ports != 0) {
sizeof(in_port_t) *
}
return (ISC_R_NOMEMORY);
}
}
p = 0;
i4 = 0;
i6 = 0;
do {
if (isc_portset_isset(v4portset, p)) {
}
if (isc_portset_isset(v6portset, p)) {
}
} while (p++ < 65535);
}
}
return (ISC_R_SUCCESS);
}
static isc_result_t
unsigned int buffersize, unsigned int maxbuffers,
unsigned int maxrequests, unsigned int buckets,
unsigned int increment)
{
REQUIRE(maxbuffers > 0);
/*
* Keep some number of items around. This should be a config
* option. For now, keep 8, but later keep at least two even
* if the caller wants less. This allows us to ensure certain
* things, like an event can be "freed" and the next allocation
* will always succeed.
*
* Note that if limits are placed on anything here, we use one
* event internally, so the actual limit should be "wanted + 1."
*
* XXXMLG
*/
if (maxbuffers < 8)
maxbuffers = 8;
/* Create or adjust buffer pool */
/*
* We only increase the maxbuffers to avoid accidental buffer
* shortage. Ideally we'd separate the manager-wide maximum
* from per-dispatch limits and respect the latter within the
* global limit. But at this moment that's deemed to be
* overkilling and isn't worth additional implementation
* complexity.
*/
}
} else {
if (result != ISC_R_SUCCESS) {
return (result);
}
}
/* Create or adjust socket pool */
DNS_DISPATCH_POOLSOCKS * 2);
DNS_DISPATCH_POOLSOCKS * 2);
}
return (ISC_R_SUCCESS);
}
if (result != ISC_R_SUCCESS) {
goto cleanup;
}
if (result != ISC_R_SUCCESS)
goto cleanup;
return (ISC_R_SUCCESS);
return (result);
}
void
if (killit)
destroy_mgr(&mgr);
}
void
}
static int
return (-1);
return (0);
else
return (1);
}
static isc_boolean_t
{
unsigned int nports;
if (result != ISC_R_SUCCESS)
goto unlock;
}
} else {
}
goto unlock;
return (available);
}
static isc_boolean_t
return (ISC_TRUE);
/*
* Don't match wildcard ports unless the port is available in the
* current configuration.
*/
if (isc_sockaddr_getport(addr) == 0 &&
return (ISC_FALSE);
}
/*
* Check if we match the binding <address,port>.
*/
return (ISC_TRUE);
if (isc_sockaddr_getport(addr) == 0)
return (ISC_FALSE);
/*
* Check if we match a bound wildcard port <address,port>.
*/
return (ISC_FALSE);
if (result != ISC_R_SUCCESS)
return (ISC_FALSE);
}
/*
* Requires mgr be locked.
*
* No dispatcher can be locked by this thread when calling this function.
*
*
* NOTE:
* If a matching dispatcher is found, it is locked after this function
* returns, and must be unlocked by the caller.
*/
static isc_result_t
unsigned int attributes, unsigned int mask,
{
/*
* Make certain that we will not match a private or exclusive dispatch.
*/
if ((disp->shutting_down == 0)
break;
}
goto out;
}
out:
return (result);
}
static isc_result_t
{
unsigned int i;
return (ISC_R_NOMEMORY);
buckets * sizeof(dns_displist_t));
return (ISC_R_NOMEMORY);
}
if (needsocktable) {
sizeof(dispsocketlist_t));
buckets * sizeof(dns_displist_t));
return (ISC_R_NOMEMORY);
}
}
if (result != ISC_R_SUCCESS) {
buckets * sizeof(dispsocketlist_t));
}
buckets * sizeof(dns_displist_t));
return (result);
}
for (i = 0; i < buckets; i++) {
}
return (ISC_R_SUCCESS);
}
static void
}
}
/*
* Allocate and set important limits.
*/
static isc_result_t
{
/*
* Set up the dispatcher, mostly. Don't bother setting some of
* the options that are controlled by tcp vs. udp, etc.
*/
return (ISC_R_NOMEMORY);
disp->attributes = 0;
disp->recv_pending = 0;
disp->shutting_down = 0;
disp->shutdown_out = 0;
disp->tcpmsg_valid = 0;
disp->tcpbuffers = 0;
if (result != ISC_R_SUCCESS)
goto deallocate;
goto kill_lock;
}
return (ISC_R_SUCCESS);
/*
* error returns
*/
return (result);
}
/*
* MUST be unlocked, and not used by anything.
*/
static void
int i;
if (disp->tcpmsg_valid) {
disp->tcpmsg_valid = 0;
}
for (i = 0; i < DNS_DISPATCH_PORTTABLESIZE; i++)
sizeof(disp->port_table[0]) *
}
}
unsigned int maxbuffers, unsigned int maxrequests,
{
dispp));
}
unsigned int maxbuffers, unsigned int maxrequests,
{
/*
* dispatch_allocate() checks mgr for us.
* qid_allocate() checks buckets and increment for us.
*/
if (result != ISC_R_SUCCESS) {
return (result);
}
if (result != ISC_R_SUCCESS)
goto deallocate_dispatch;
if (result != ISC_R_SUCCESS)
goto kill_socket;
sizeof(isc_event_t));
goto kill_task;
}
switch (isc_sockaddr_pf(destaddr)) {
case AF_INET:
break;
case AF_INET6:
break;
}
}
} else
/*
* Append it to the dispatcher list.
*/
return (ISC_R_SUCCESS);
/*
* Error returns.
*/
return (result);
}
{
unsigned int attributes, mask;
if ((disp->shutting_down == 0) &&
&sockname);
if (result == ISC_R_SUCCESS)
&peeraddr);
if (result == ISC_R_SUCCESS &&
/* attach */
}
}
}
}
{
unsigned int attributes, mask;
/* First pass (same as dns_dispatch_gettcp()) */
if ((disp->shutting_down == 0) &&
&sockname);
if (result == ISC_R_SUCCESS)
&peeraddr);
if (result == ISC_R_SUCCESS &&
/* attach */
}
}
}
if (match) {
return (ISC_R_SUCCESS);
}
/* Second pass */
if ((disp->shutting_down == 0) &&
/* attach */
}
}
}
unsigned int buffersize,
unsigned int maxbuffers, unsigned int maxrequests,
unsigned int attributes, unsigned int mask,
{
REQUIRE(maxbuffers > 0);
if (result != ISC_R_SUCCESS)
return (result);
if ((attributes & DNS_DISPATCHATTR_EXCLUSIVE) != 0) {
goto createudp;
}
/*
* See if we have a dispatcher that matches.
*/
if (dup_dispatch == NULL) {
if (result == ISC_R_SUCCESS) {
&& (attributes & DNS_DISPATCHATTR_NOLISTEN) != 0)
{
if (disp->recv_pending != 0)
}
return (ISC_R_SUCCESS);
}
}
/*
* Nope, create one.
*/
dup_dispatch == NULL
? NULL
: dup_dispatch->socket);
if (result != ISC_R_SUCCESS) {
return (result);
}
return (ISC_R_SUCCESS);
}
unsigned int buffersize,
unsigned int maxbuffers, unsigned int maxrequests,
unsigned int attributes, unsigned int mask,
{
}
/*
* mgr should be locked.
*/
#ifndef DNS_DISPATCH_HELD
#define DNS_DISPATCH_HELD 20U
#endif
static isc_result_t
{
unsigned int i, j;
if (anyport) {
unsigned int nports;
/*
* If no port is specified, we first try to pick up a random
* port by ourselves.
*/
} else {
}
if (nports == 0)
return (ISC_R_ADDRNOTAVAIL);
for (i = 0; i < 1024; i++) {
nports)];
/*
* Continue if the port choosen is already in use
* or the OS has reserved it.
*/
if (result == ISC_R_NOPERM ||
continue;
return (result);
}
/*
* If this fails 1024 times, we then ask the kernel for
* choosing one.
*/
} else {
/* Allow to reuse address for non-random ports. */
if (result == ISC_R_SUCCESS)
return (result);
}
i = 0;
for (j = 0; j < 0xffffU; j++) {
if (result != ISC_R_SUCCESS)
goto end;
break;
isc_socket_detach(&held[i]);
if (i == DNS_DISPATCH_HELD)
i = 0;
}
if (j == 0xffffU) {
"avoid-v%s-udp-ports: unable to allocate "
"an available port",
goto end;
}
end:
for (i = 0; i < DNS_DISPATCH_HELD; i++) {
isc_socket_detach(&held[i]);
}
return (result);
}
static isc_result_t
unsigned int maxrequests,
unsigned int attributes,
{
int i = 0;
/*
* dispatch_allocate() checks mgr for us.
*/
if (result != ISC_R_SUCCESS)
return (result);
if ((attributes & DNS_DISPATCHATTR_EXCLUSIVE) == 0) {
if (result != ISC_R_SUCCESS)
goto deallocate_dispatch;
char addrbuf[ISC_SOCKADDR_FORMATSIZE];
" UDP dispatch for %s with socket fd %d\n",
}
} else {
/*
* For dispatches using exclusive sockets with a specific
* source address, we only check if the specified address is
* available on the system. Query sockets will be created later
* on demand.
*/
if (result != ISC_R_SUCCESS)
goto deallocate_dispatch;
}
sizeof(disp->port_table[0]) *
goto deallocate_dispatch;
for (i = 0; i < DNS_DISPATCH_PORTTABLESIZE; i++)
if (result != ISC_R_SUCCESS)
goto deallocate_dispatch;
}
if ((attributes & DNS_DISPATCHATTR_EXCLUSIVE) != 0)
else
if (result != ISC_R_SUCCESS) {
while (--i >= 0) {
}
goto kill_socket;
}
}
sizeof(isc_event_t));
goto kill_task;
}
{
goto kill_ctlevent;
}
if (result != ISC_R_SUCCESS)
goto kill_sepool;
/*
* Append it to the dispatcher list.
*/
return (result);
/*
* Error returns.
*/
return (result);
}
void
}
/*
* It is important to lock the manager while we are deleting the dispatch,
* since dns_dispatch_getudp will call dispatch_find, which returns to
* the caller a dispatch but does not attach to it until later. _getudp
* locks the manager, however, so locking it here will keep us from attaching
* to a dispatcher that is in the process of going away.
*/
void
if (disp->recv_pending > 0)
}
}
if (killit)
}
{
}
{
unsigned int bucket;
int i;
return (ISC_R_SHUTTINGDOWN);
}
return (ISC_R_QUOTA);
}
/*
* Kill oldest outstanding query if the number of sockets
* exceeds the quota to keep the room for new queries.
*/
ISC_EVENT_PTR(&rev));
}
}
/*
* Move this entry to the tail so that it won't (easily) be
* examined before actually being canceled.
*/
}
/*
* Get a separate UDP socket with a random port number.
*/
&localport);
if (result != ISC_R_SUCCESS) {
return (result);
}
} else {
}
/*
* Try somewhat hard to find an unique ID unless FIXEDID is set
* in which case we use the id passed in via *idp.
*/
if ((options & DNS_DISPATCHOPT_FIXEDID) != 0)
else
i = 0;
do {
break;
}
break;
id &= 0x0000ffff;
} while (i++ < 64);
if (!ok) {
return (ISC_R_NOMORE);
}
if (dispsocket != NULL)
return (ISC_R_NOMEMORY);
}
if (dispsocket != NULL)
if (result != ISC_R_SUCCESS) {
if (dispsocket != NULL)
return (result);
}
}
if (dispsocket != NULL)
return (ISC_R_SUCCESS);
}
{
}
void
}
}
void
{
unsigned int bucket;
unsigned int n;
} else {
}
if (disp->recv_pending > 0)
}
}
/*
* We've posted our event, but the caller hasn't gotten it
* yet. Take it back.
*/
/*
* We had better have gotten it back.
*/
INSIST(n == 1);
}
}
}
/*
* Free any buffered requests as well
*/
}
else
if (killit)
}
static void
return;
/*
* Search for the first response handler without packets outstanding
* unless a specific hander is given.
*/
/* Empty. */)
/*
* No one to send the cancel event to, so nothing to do.
*/
goto unlock;
/*
* Send the shutdown failsafe event to this resp.
*/
"cancel: failsafe event %p -> task %p",
}
}
else
return (NULL);
}
return (ISC_R_SUCCESS);
}
return (ISC_R_NOTIMPLEMENTED);
}
void
return;
}
return;
}
unsigned int
/*
* We don't bother locking disp here; it's the caller's responsibility
* to use only non volatile flags.
*/
return (disp->attributes);
}
void
unsigned int attributes, unsigned int mask)
{
/* Exclusive attribute can only be set on creation */
/* Also, a dispatch with randomport specified cannot start listening */
(attributes & DNS_DISPATCHATTR_NOLISTEN) == 0);
/* XXXMLG
* Should check for valid attributes here!
*/
if ((mask & DNS_DISPATCHATTR_NOLISTEN) != 0) {
(attributes & DNS_DISPATCHATTR_NOLISTEN) == 0) {
== 0 &&
(attributes & DNS_DISPATCHATTR_NOLISTEN) != 0) {
if (disp->recv_pending != 0)
}
}
}
void
void *buf;
newsevent = (isc_socketevent_t *)
disp, sizeof(isc_socketevent_t));
return;
return;
}
}
/* check that dispatch set is configured */
return (NULL);
return (disp);
}
dns_dispatchset_t **dsetp, int n)
{
int i, j;
return (ISC_R_NOMEMORY);
if (result != ISC_R_SUCCESS)
goto fail_alloc;
goto fail_lock;
}
for (i = 1; i < n; i++) {
&dset->dispatches[i],
if (result != ISC_R_SUCCESS)
goto fail;
}
return (ISC_R_SUCCESS);
fail:
for (j = 0; j < i; j++)
return (result);
}
void
int i;
}
}
void
int i;
}
void
}
}
#if 0
void
char foo[1024];
}
}
#endif