byaddr.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2005, 2007, 2009, 2013  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 2000-2003  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: byaddr.c,v 1.41 2009/09/02 23:48:02 tbox Exp $ */
00019 
00020 /*! \file */
00021 
00022 #include <config.h>
00023 
00024 #include <isc/mem.h>
00025 #include <isc/netaddr.h>
00026 #include <isc/print.h>
00027 #include <isc/string.h>         /* Required for HP/UX (and others?) */
00028 #include <isc/task.h>
00029 #include <isc/util.h>
00030 
00031 #include <dns/byaddr.h>
00032 #include <dns/db.h>
00033 #include <dns/events.h>
00034 #include <dns/lookup.h>
00035 #include <dns/rdata.h>
00036 #include <dns/rdataset.h>
00037 #include <dns/rdatastruct.h>
00038 #include <dns/resolver.h>
00039 #include <dns/result.h>
00040 #include <dns/view.h>
00041 
00042 /*
00043  * XXXRTH  We could use a static event...
00044  */
00045 
00046 static char hex_digits[] = {
00047         '0', '1', '2', '3', '4', '5', '6', '7',
00048         '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
00049 };
00050 
00051 isc_result_t
00052 dns_byaddr_createptrname(isc_netaddr_t *address, isc_boolean_t nibble,
00053                          dns_name_t *name)
00054 {
00055         /*
00056          * We dropped bitstring labels, so all lookups will use nibbles.
00057          */
00058         UNUSED(nibble);
00059 
00060         return (dns_byaddr_createptrname2(address,
00061                                           DNS_BYADDROPT_IPV6INT, name));
00062 }
00063 
00064 isc_result_t
00065 dns_byaddr_createptrname2(isc_netaddr_t *address, unsigned int options,
00066                           dns_name_t *name)
00067 {
00068         char textname[128];
00069         unsigned char *bytes;
00070         int i;
00071         char *cp;
00072         isc_buffer_t buffer;
00073         unsigned int len;
00074 
00075         REQUIRE(address != NULL);
00076 
00077         /*
00078          * We create the text representation and then convert to a
00079          * dns_name_t.  This is not maximally efficient, but it keeps all
00080          * of the knowledge of wire format in the dns_name_ routines.
00081          */
00082 
00083         bytes = (unsigned char *)(&address->type);
00084         if (address->family == AF_INET) {
00085                 (void)snprintf(textname, sizeof(textname),
00086                                "%u.%u.%u.%u.in-addr.arpa.",
00087                                (bytes[3] & 0xff),
00088                                (bytes[2] & 0xff),
00089                                (bytes[1] & 0xff),
00090                                (bytes[0] & 0xff));
00091         } else if (address->family == AF_INET6) {
00092                 cp = textname;
00093                 for (i = 15; i >= 0; i--) {
00094                         *cp++ = hex_digits[bytes[i] & 0x0f];
00095                         *cp++ = '.';
00096                         *cp++ = hex_digits[(bytes[i] >> 4) & 0x0f];
00097                         *cp++ = '.';
00098                 }
00099                 if ((options & DNS_BYADDROPT_IPV6INT) != 0)
00100                         strcpy(cp, "ip6.int.");
00101                 else
00102                         strcpy(cp, "ip6.arpa.");
00103         } else
00104                 return (ISC_R_NOTIMPLEMENTED);
00105 
00106         len = (unsigned int)strlen(textname);
00107         isc_buffer_init(&buffer, textname, len);
00108         isc_buffer_add(&buffer, len);
00109         return (dns_name_fromtext(name, &buffer, dns_rootname, 0, NULL));
00110 }
00111 
00112 struct dns_byaddr {
00113         /* Unlocked. */
00114         unsigned int            magic;
00115         isc_mem_t *             mctx;
00116         isc_mutex_t             lock;
00117         dns_fixedname_t         name;
00118         /* Locked by lock. */
00119         unsigned int            options;
00120         dns_lookup_t *          lookup;
00121         isc_task_t *            task;
00122         dns_byaddrevent_t *     event;
00123         isc_boolean_t           canceled;
00124 };
00125 
00126 #define BYADDR_MAGIC                    ISC_MAGIC('B', 'y', 'A', 'd')
00127 #define VALID_BYADDR(b)                 ISC_MAGIC_VALID(b, BYADDR_MAGIC)
00128 
00129 #define MAX_RESTARTS 16
00130 
00131 static inline isc_result_t
00132 copy_ptr_targets(dns_byaddr_t *byaddr, dns_rdataset_t *rdataset) {
00133         isc_result_t result;
00134         dns_name_t *name;
00135         dns_rdata_t rdata = DNS_RDATA_INIT;
00136 
00137         /*
00138          * The caller must be holding the byaddr's lock.
00139          */
00140 
00141         result = dns_rdataset_first(rdataset);
00142         while (result == ISC_R_SUCCESS) {
00143                 dns_rdata_ptr_t ptr;
00144                 dns_rdataset_current(rdataset, &rdata);
00145                 result = dns_rdata_tostruct(&rdata, &ptr, NULL);
00146                 if (result != ISC_R_SUCCESS)
00147                         return (result);
00148                 name = isc_mem_get(byaddr->mctx, sizeof(*name));
00149                 if (name == NULL) {
00150                         dns_rdata_freestruct(&ptr);
00151                         return (ISC_R_NOMEMORY);
00152                 }
00153                 dns_name_init(name, NULL);
00154                 result = dns_name_dup(&ptr.ptr, byaddr->mctx, name);
00155                 dns_rdata_freestruct(&ptr);
00156                 if (result != ISC_R_SUCCESS) {
00157                         isc_mem_put(byaddr->mctx, name, sizeof(*name));
00158                         return (ISC_R_NOMEMORY);
00159                 }
00160                 ISC_LIST_APPEND(byaddr->event->names, name, link);
00161                 dns_rdata_reset(&rdata);
00162                 result = dns_rdataset_next(rdataset);
00163         }
00164         if (result == ISC_R_NOMORE)
00165                 result = ISC_R_SUCCESS;
00166 
00167         return (result);
00168 }
00169 
00170 static void
00171 lookup_done(isc_task_t *task, isc_event_t *event) {
00172         dns_byaddr_t *byaddr = event->ev_arg;
00173         dns_lookupevent_t *levent;
00174         isc_result_t result;
00175 
00176         REQUIRE(event->ev_type == DNS_EVENT_LOOKUPDONE);
00177         REQUIRE(VALID_BYADDR(byaddr));
00178         REQUIRE(byaddr->task == task);
00179 
00180         UNUSED(task);
00181 
00182         levent = (dns_lookupevent_t *)event;
00183 
00184         if (levent->result == ISC_R_SUCCESS) {
00185                 result = copy_ptr_targets(byaddr, levent->rdataset);
00186                 byaddr->event->result = result;
00187         } else
00188                 byaddr->event->result = levent->result;
00189         isc_event_free(&event);
00190         isc_task_sendanddetach(&byaddr->task, (isc_event_t **)&byaddr->event);
00191 }
00192 
00193 static void
00194 bevent_destroy(isc_event_t *event) {
00195         dns_byaddrevent_t *bevent;
00196         dns_name_t *name, *next_name;
00197         isc_mem_t *mctx;
00198 
00199         REQUIRE(event->ev_type == DNS_EVENT_BYADDRDONE);
00200         mctx = event->ev_destroy_arg;
00201         bevent = (dns_byaddrevent_t *)event;
00202 
00203         for (name = ISC_LIST_HEAD(bevent->names);
00204              name != NULL;
00205              name = next_name) {
00206                 next_name = ISC_LIST_NEXT(name, link);
00207                 ISC_LIST_UNLINK(bevent->names, name, link);
00208                 dns_name_free(name, mctx);
00209                 isc_mem_put(mctx, name, sizeof(*name));
00210         }
00211         isc_mem_put(mctx, event, event->ev_size);
00212 }
00213 
00214 isc_result_t
00215 dns_byaddr_create(isc_mem_t *mctx, isc_netaddr_t *address, dns_view_t *view,
00216                   unsigned int options, isc_task_t *task,
00217                   isc_taskaction_t action, void *arg, dns_byaddr_t **byaddrp)
00218 {
00219         isc_result_t result;
00220         dns_byaddr_t *byaddr;
00221         isc_event_t *ievent;
00222 
00223         byaddr = isc_mem_get(mctx, sizeof(*byaddr));
00224         if (byaddr == NULL)
00225                 return (ISC_R_NOMEMORY);
00226         byaddr->mctx = NULL;
00227         isc_mem_attach(mctx, &byaddr->mctx);
00228         byaddr->options = options;
00229 
00230         byaddr->event = isc_mem_get(mctx, sizeof(*byaddr->event));
00231         if (byaddr->event == NULL) {
00232                 result = ISC_R_NOMEMORY;
00233                 goto cleanup_byaddr;
00234         }
00235         ISC_EVENT_INIT(byaddr->event, sizeof(*byaddr->event), 0, NULL,
00236                        DNS_EVENT_BYADDRDONE, action, arg, byaddr,
00237                        bevent_destroy, mctx);
00238         byaddr->event->result = ISC_R_FAILURE;
00239         ISC_LIST_INIT(byaddr->event->names);
00240 
00241         byaddr->task = NULL;
00242         isc_task_attach(task, &byaddr->task);
00243 
00244         result = isc_mutex_init(&byaddr->lock);
00245         if (result != ISC_R_SUCCESS)
00246                 goto cleanup_event;
00247 
00248         dns_fixedname_init(&byaddr->name);
00249 
00250         result = dns_byaddr_createptrname2(address, options,
00251                                            dns_fixedname_name(&byaddr->name));
00252         if (result != ISC_R_SUCCESS)
00253                 goto cleanup_lock;
00254 
00255         byaddr->lookup = NULL;
00256         result = dns_lookup_create(mctx, dns_fixedname_name(&byaddr->name),
00257                                    dns_rdatatype_ptr, view, 0, task,
00258                                    lookup_done, byaddr, &byaddr->lookup);
00259         if (result != ISC_R_SUCCESS)
00260                 goto cleanup_lock;
00261 
00262         byaddr->canceled = ISC_FALSE;
00263         byaddr->magic = BYADDR_MAGIC;
00264 
00265         *byaddrp = byaddr;
00266 
00267         return (ISC_R_SUCCESS);
00268 
00269  cleanup_lock:
00270         DESTROYLOCK(&byaddr->lock);
00271 
00272  cleanup_event:
00273         ievent = (isc_event_t *)byaddr->event;
00274         isc_event_free(&ievent);
00275         byaddr->event = NULL;
00276 
00277         isc_task_detach(&byaddr->task);
00278 
00279  cleanup_byaddr:
00280         isc_mem_putanddetach(&mctx, byaddr, sizeof(*byaddr));
00281 
00282         return (result);
00283 }
00284 
00285 void
00286 dns_byaddr_cancel(dns_byaddr_t *byaddr) {
00287         REQUIRE(VALID_BYADDR(byaddr));
00288 
00289         LOCK(&byaddr->lock);
00290 
00291         if (!byaddr->canceled) {
00292                 byaddr->canceled = ISC_TRUE;
00293                 if (byaddr->lookup != NULL)
00294                         dns_lookup_cancel(byaddr->lookup);
00295         }
00296 
00297         UNLOCK(&byaddr->lock);
00298 }
00299 
00300 void
00301 dns_byaddr_destroy(dns_byaddr_t **byaddrp) {
00302         dns_byaddr_t *byaddr;
00303 
00304         REQUIRE(byaddrp != NULL);
00305         byaddr = *byaddrp;
00306         REQUIRE(VALID_BYADDR(byaddr));
00307         REQUIRE(byaddr->event == NULL);
00308         REQUIRE(byaddr->task == NULL);
00309         dns_lookup_destroy(&byaddr->lookup);
00310 
00311         DESTROYLOCK(&byaddr->lock);
00312         byaddr->magic = 0;
00313         isc_mem_putanddetach(&byaddr->mctx, byaddr, sizeof(*byaddr));
00314 
00315         *byaddrp = NULL;
00316 }

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