lwresd.c revision 45e1bd63587102c3bb361eaca42ee7b714fb3542
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Copyright (C) 2004, 2005 Internet Systems Consortium, Inc. ("ISC")
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Copyright (C) 2000-2003 Internet Software Consortium.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Permission to use, copy, modify, and distribute this software for any
c869993e79c1eafbec61a56bf6cea848fe754c71xy * purpose with or without fee is hereby granted, provided that the above
c869993e79c1eafbec61a56bf6cea848fe754c71xy * copyright notice and this permission notice appear in all copies.
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
c869993e79c1eafbec61a56bf6cea848fe754c71xy * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
c869993e79c1eafbec61a56bf6cea848fe754c71xy * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
c869993e79c1eafbec61a56bf6cea848fe754c71xy * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China * PERFORMANCE OF THIS SOFTWARE.
c869993e79c1eafbec61a56bf6cea848fe754c71xy/* $Id: lwresd.c,v 1.52 2006/02/28 02:39:51 marka Exp $ */
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Main program for the Lightweight Resolver Daemon.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * To paraphrase the old saying about X11, "It's not a lightweight deamon
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China * for resolvers, it's a deamon for lightweight resolvers".
da14cebe459d3275048785f25bd869cb09b5307fEric Cheng#define LWRESD_MAGIC ISC_MAGIC('L', 'W', 'R', 'D')
c869993e79c1eafbec61a56bf6cea848fe754c71xy#define VALID_LWRESLISTENER(l) ISC_MAGIC_VALID(l, LWRESLISTENER_MAGIC)
c869993e79c1eafbec61a56bf6cea848fe754c71xy * The total number of clients we can handle will be NTASKS * NRECVS.
c869993e79c1eafbec61a56bf6cea848fe754c71xy#define NTASKS 2 /*%< tasks to create to handle lwres queries */
c869993e79c1eafbec61a56bf6cea848fe754c71xytypedef ISC_LIST(ns_lwreslistener_t) ns_lwreslistenerlist_t;
c869993e79c1eafbec61a56bf6cea848fe754c71xystatic void
c869993e79c1eafbec61a56bf6cea848fe754c71xy RUNTIME_CHECK(isc_mutex_init(&listeners_lock) == ISC_R_SUCCESS);
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Wrappers around our memory management stuff, for the lwres functions.
c869993e79c1eafbec61a56bf6cea848fe754c71xy } while (0)
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Convert a resolv.conf file into a config structure.
c869993e79c1eafbec61a56bf6cea848fe754c71xyns_lwresd_parseeresolvconf(isc_mem_t *mctx, cfg_parser_t *pctx,
c869993e79c1eafbec61a56bf6cea848fe754c71xy lwresult = lwres_context_create(&lwctx, mctx, ns__lwresd_memalloc,
c869993e79c1eafbec61a56bf6cea848fe754c71xy lwresult = lwres_conf_parse(lwctx, lwresd_g_resolvconffile);
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Build the list of forwarders.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Build the sortlist
c869993e79c1eafbec61a56bf6cea848fe754c71xy unsigned int mask;
c869993e79c1eafbec61a56bf6cea848fe754c71xy "processing sortlist: '%s' is "
c869993e79c1eafbec61a56bf6cea848fe754c71xy "not a valid netmask",
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Build the search path
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Build the ndots line
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Build the listen-on line
c869993e79c1eafbec61a56bf6cea848fe754c71xy (char *)isc_buffer_base(&b));
c869993e79c1eafbec61a56bf6cea848fe754c71xy return (cfg_parse_buffer(pctx, &b, &cfg_type_namedconf, configp));
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Handle lwresd manager objects
c869993e79c1eafbec61a56bf6cea848fe754c71xyns_lwdmanager_create(isc_mem_t *mctx, const cfg_obj_t *lwres,
c869993e79c1eafbec61a56bf6cea848fe754c71xy const char *vname;
c869993e79c1eafbec61a56bf6cea848fe754c71xy RUNTIME_CHECK(isc_mutex_init(&lwresd->lock) == ISC_R_SUCCESS);
c869993e79c1eafbec61a56bf6cea848fe754c71xy vname = cfg_obj_asstring(cfg_tuple_get(viewobj, "name"));
c869993e79c1eafbec61a56bf6cea848fe754c71xy result = ns_config_getclass(obj, dns_rdataclass_in, &vclass);
c869993e79c1eafbec61a56bf6cea848fe754c71xy result = dns_viewlist_find(&ns_g_server->viewlist, vname, vclass,
c869993e79c1eafbec61a56bf6cea848fe754c71xy "couldn't create searchlist");
c869993e79c1eafbec61a56bf6cea848fe754c71xy const char *searchstr;
c869993e79c1eafbec61a56bf6cea848fe754c71xy "invalid name %s in searchlist",
c869993e79c1eafbec61a56bf6cea848fe754c71xy "couldn't update searchlist");
c869993e79c1eafbec61a56bf6cea848fe754c71xyns_lwdmanager_attach(ns_lwresd_t *source, ns_lwresd_t **targetp) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Handle listener objects
c869993e79c1eafbec61a56bf6cea848fe754c71xy listener = isc_mem_get(mctx, sizeof(ns_lwreslistener_t));
c869993e79c1eafbec61a56bf6cea848fe754c71xylistener_bind(ns_lwreslistener_t *listener, isc_sockaddr_t *address) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy if ((pf == AF_INET && isc_net_probeipv4() != ISC_R_SUCCESS) ||
c869993e79c1eafbec61a56bf6cea848fe754c71xy (pf == AF_INET6 && isc_net_probeipv6() != ISC_R_SUCCESS))
c869993e79c1eafbec61a56bf6cea848fe754c71xy "failed to create lwres socket: %s",
c869993e79c1eafbec61a56bf6cea848fe754c71xy "failed to add lwres socket: %s: %s",
c869993e79c1eafbec61a56bf6cea848fe754c71xystatic void
c869993e79c1eafbec61a56bf6cea848fe754c71xy isc_socket_attach(oldlistener->sock, &newlistener->sock);
c869993e79c1eafbec61a56bf6cea848fe754c71xy unsigned int i;
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Create the client managers.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Ensure that we have created at least one.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Walk the list of clients and start each one up.
c869993e79c1eafbec61a56bf6cea848fe754c71xy "could not start lwres "
c869993e79c1eafbec61a56bf6cea848fe754c71xy "client handler: %s",
c869993e79c1eafbec61a56bf6cea848fe754c71xystatic void
c869993e79c1eafbec61a56bf6cea848fe754c71xyfind_listener(isc_sockaddr_t *address, ns_lwreslistener_t **listenerp) {
c869993e79c1eafbec61a56bf6cea848fe754c71xyns_lwreslistener_unlinkcm(ns_lwreslistener_t *listener, ns_lwdclientmgr_t *cm)
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing Chinans_lwreslistener_linkcm(ns_lwreslistener_t *listener, ns_lwdclientmgr_t *cm) {
c869993e79c1eafbec61a56bf6cea848fe754c71xy * This does no locking, since it's called early enough that locking
c869993e79c1eafbec61a56bf6cea848fe754c71xy * isn't needed.
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing Chinaconfigure_listener(isc_sockaddr_t *address, ns_lwresd_t *lwresd,
c869993e79c1eafbec61a56bf6cea848fe754c71xy isc_sockaddr_format(address, socktext, sizeof(socktext));
c869993e79c1eafbec61a56bf6cea848fe754c71xy "lwres failed to configure %s: %s",
c869993e79c1eafbec61a56bf6cea848fe754c71xy * If there's already a listener, don't rebind the socket.
c869993e79c1eafbec61a56bf6cea848fe754c71xy isc_sockaddr_format(address, socktext, sizeof(socktext));
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Remove the old listener from the old list and shut it down.
c869993e79c1eafbec61a56bf6cea848fe754c71xy isc_sockaddr_format(address, socktext, sizeof(socktext));
c869993e79c1eafbec61a56bf6cea848fe754c71xyns_lwresd_configure(isc_mem_t *mctx, const cfg_obj_t *config) {
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China ns_lwreslistenerlist_t newlisteners;
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China isc_result_t result;
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China isc_uint32_t count = 0;
c869993e79c1eafbec61a56bf6cea848fe754c71xy RUNTIME_CHECK(isc_once_do(&once, initialize_mutex) == ISC_R_SUCCESS);
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Run through the new lwres address list, noting sockets that
c869993e79c1eafbec61a56bf6cea848fe754c71xy * are already being listened on and moving them to the new list.
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Identifying duplicates addr/port combinations is left to either
c869993e79c1eafbec61a56bf6cea848fe754c71xy * the underlying config code, or to the bind attempt getting an
c869993e79c1eafbec61a56bf6cea848fe754c71xy * address-in-use error.
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China lwres = cfg_listelt_value(element);
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China if (port == 0)
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China port = LWRES_UDP_PORT;
80a11ad227f9c82cd6e7cf5c8913a37f00b7af0echenlu chen - Sun Microsystems - Beijing China listenerslist = NULL;
c869993e79c1eafbec61a56bf6cea848fe754c71xy for (i = 0; i < count; i++)
c869993e79c1eafbec61a56bf6cea848fe754c71xy * Shutdown everything on the listeners list, and remove them from
c869993e79c1eafbec61a56bf6cea848fe754c71xy * the list. Then put all of the new listeners on it.