lwdgnba.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2008, 2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000-2002  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id: lwdgnba.c,v 1.22 2008/01/14 23:46:56 tbox Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <isc/socket.h>
00025 #include <isc/string.h>         /* Required for HP/UX (and others?) */
00026 #include <isc/util.h>
00027 
00028 #include <dns/adb.h>
00029 #include <dns/byaddr.h>
00030 #include <dns/result.h>
00031 
00032 #include <named/types.h>
00033 #include <named/lwdclient.h>
00034 
00035 static void start_byaddr(ns_lwdclient_t *);
00036 
00037 static void
00038 byaddr_done(isc_task_t *task, isc_event_t *event) {
00039         ns_lwdclient_t *client;
00040         ns_lwdclientmgr_t *cm;
00041         dns_byaddrevent_t *bevent;
00042         int lwres;
00043         lwres_buffer_t lwb;
00044         dns_name_t *name;
00045         isc_result_t result;
00046         lwres_result_t lwresult;
00047         isc_region_t r;
00048         isc_buffer_t b;
00049         lwres_gnbaresponse_t *gnba;
00050         isc_uint16_t naliases;
00051 
00052         UNUSED(task);
00053 
00054         lwb.base = NULL;
00055         client = event->ev_arg;
00056         cm = client->clientmgr;
00057         INSIST(client->byaddr == (dns_byaddr_t *)event->ev_sender);
00058 
00059         bevent = (dns_byaddrevent_t *)event;
00060         gnba = &client->gnba;
00061 
00062         ns_lwdclient_log(50, "byaddr event result = %s",
00063                          isc_result_totext(bevent->result));
00064 
00065         result = bevent->result;
00066         if (result != ISC_R_SUCCESS) {
00067                 dns_byaddr_destroy(&client->byaddr);
00068                 isc_event_free(&event);
00069                 bevent = NULL;
00070 
00071                 if (client->na.family != AF_INET6 ||
00072                     (client->options & DNS_BYADDROPT_IPV6INT) != 0) {
00073                         if (result == DNS_R_NCACHENXDOMAIN ||
00074                             result == DNS_R_NCACHENXRRSET ||
00075                             result == DNS_R_NXDOMAIN ||
00076                             result == DNS_R_NXRRSET)
00077                                 lwresult = LWRES_R_NOTFOUND;
00078                         else
00079                                 lwresult = LWRES_R_FAILURE;
00080                         ns_lwdclient_errorpktsend(client, lwresult);
00081                         return;
00082                 }
00083 
00084                 /*
00085                  * Fall back to ip6.int reverse if the default ip6.arpa
00086                  * fails.
00087                  */
00088                 client->options |= DNS_BYADDROPT_IPV6INT;
00089 
00090                 start_byaddr(client);
00091                 return;
00092         }
00093 
00094         for (name = ISC_LIST_HEAD(bevent->names);
00095              name != NULL;
00096              name = ISC_LIST_NEXT(name, link))
00097         {
00098                 b = client->recv_buffer;
00099 
00100                 result = dns_name_totext(name, ISC_TRUE, &client->recv_buffer);
00101                 if (result != ISC_R_SUCCESS)
00102                         goto out;
00103                 ns_lwdclient_log(50, "found name '%.*s'",
00104                                  (int)(client->recv_buffer.used - b.used),
00105                                  (char *)(b.base) + b.used);
00106                 if (gnba->realname == NULL) {
00107                         gnba->realname = (char *)(b.base) + b.used;
00108                         gnba->realnamelen = client->recv_buffer.used - b.used;
00109                 } else {
00110                         naliases = gnba->naliases;
00111                         if (naliases >= LWRES_MAX_ALIASES)
00112                                 break;
00113                         gnba->aliases[naliases] = (char *)(b.base) + b.used;
00114                         gnba->aliaslen[naliases] =
00115                                 client->recv_buffer.used - b.used;
00116                         gnba->naliases++;
00117                 }
00118         }
00119 
00120         dns_byaddr_destroy(&client->byaddr);
00121         isc_event_free(&event);
00122 
00123         /*
00124          * Render the packet.
00125          */
00126         client->pkt.recvlength = LWRES_RECVLENGTH;
00127         client->pkt.authtype = 0; /* XXXMLG */
00128         client->pkt.authlength = 0;
00129         client->pkt.result = LWRES_R_SUCCESS;
00130 
00131         lwres = lwres_gnbaresponse_render(cm->lwctx,
00132                                           gnba, &client->pkt, &lwb);
00133         if (lwres != LWRES_R_SUCCESS)
00134                 goto out;
00135 
00136         r.base = lwb.base;
00137         r.length = lwb.used;
00138         client->sendbuf = r.base;
00139         client->sendlength = r.length;
00140         result = ns_lwdclient_sendreply(client, &r);
00141         if (result != ISC_R_SUCCESS)
00142                 goto out;
00143 
00144         NS_LWDCLIENT_SETSEND(client);
00145 
00146         return;
00147 
00148  out:
00149         if (client->byaddr != NULL)
00150                 dns_byaddr_destroy(&client->byaddr);
00151         if (lwb.base != NULL)
00152                 lwres_context_freemem(cm->lwctx,
00153                                       lwb.base, lwb.length);
00154 
00155         if (event != NULL)
00156                 isc_event_free(&event);
00157 }
00158 
00159 static void
00160 start_byaddr(ns_lwdclient_t *client) {
00161         isc_result_t result;
00162         ns_lwdclientmgr_t *cm;
00163 
00164         cm = client->clientmgr;
00165 
00166         INSIST(client->byaddr == NULL);
00167 
00168         result = dns_byaddr_create(cm->mctx, &client->na, cm->view,
00169                                    client->options, cm->task, byaddr_done,
00170                                    client, &client->byaddr);
00171         if (result != ISC_R_SUCCESS) {
00172                 ns_lwdclient_errorpktsend(client, LWRES_R_FAILURE);
00173                 return;
00174         }
00175 }
00176 
00177 static void
00178 init_gnba(ns_lwdclient_t *client) {
00179         int i;
00180 
00181         /*
00182          * Initialize the real name and alias arrays in the reply we're
00183          * going to build up.
00184          */
00185         for (i = 0; i < LWRES_MAX_ALIASES; i++) {
00186                 client->aliases[i] = NULL;
00187                 client->aliaslen[i] = 0;
00188         }
00189         for (i = 0; i < LWRES_MAX_ADDRS; i++) {
00190                 client->addrs[i].family = 0;
00191                 client->addrs[i].length = 0;
00192                 memset(client->addrs[i].address, 0, LWRES_ADDR_MAXLEN);
00193                 LWRES_LINK_INIT(&client->addrs[i], link);
00194         }
00195 
00196         client->gnba.naliases = 0;
00197         client->gnba.realname = NULL;
00198         client->gnba.aliases = client->aliases;
00199         client->gnba.realnamelen = 0;
00200         client->gnba.aliaslen = client->aliaslen;
00201         client->gnba.base = NULL;
00202         client->gnba.baselen = 0;
00203         isc_buffer_init(&client->recv_buffer, client->buffer, LWRES_RECVLENGTH);
00204 }
00205 
00206 void
00207 ns_lwdclient_processgnba(ns_lwdclient_t *client, lwres_buffer_t *b) {
00208         lwres_gnbarequest_t *req;
00209         isc_result_t result;
00210         isc_sockaddr_t sa;
00211         ns_lwdclientmgr_t *cm;
00212 
00213         REQUIRE(NS_LWDCLIENT_ISRECVDONE(client));
00214         INSIST(client->byaddr == NULL);
00215 
00216         cm = client->clientmgr;
00217         req = NULL;
00218 
00219         result = lwres_gnbarequest_parse(cm->lwctx,
00220                                          b, &client->pkt, &req);
00221         if (result != LWRES_R_SUCCESS)
00222                 goto out;
00223 
00224         client->options = 0;
00225         if (req->addr.family == LWRES_ADDRTYPE_V4) {
00226                 client->na.family = AF_INET;
00227                 if (req->addr.length != 4)
00228                         goto out;
00229                 memmove(&client->na.type.in, req->addr.address, 4);
00230         } else if (req->addr.family == LWRES_ADDRTYPE_V6) {
00231                 client->na.family = AF_INET6;
00232                 if (req->addr.length != 16)
00233                         goto out;
00234                 memmove(&client->na.type.in6, req->addr.address, 16);
00235         } else {
00236                 goto out;
00237         }
00238         isc_sockaddr_fromnetaddr(&sa, &client->na, 53);
00239 
00240         ns_lwdclient_log(50, "client %p looking for addrtype %08x",
00241                          client, req->addr.family);
00242 
00243         /*
00244          * We no longer need to keep this around.
00245          */
00246         lwres_gnbarequest_free(cm->lwctx, &req);
00247 
00248         /*
00249          * Initialize the real name and alias arrays in the reply we're
00250          * going to build up.
00251          */
00252         init_gnba(client);
00253         client->options = 0;
00254 
00255         /*
00256          * Start the find.
00257          */
00258         start_byaddr(client);
00259 
00260         return;
00261 
00262         /*
00263          * We're screwed.  Return an error packet to our caller.
00264          */
00265  out:
00266         if (req != NULL)
00267                 lwres_gnbarequest_free(cm->lwctx, &req);
00268 
00269         ns_lwdclient_errorpktsend(client, LWRES_R_FAILURE);
00270 }

Generated on Tue Apr 28 17:40:54 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha