"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.11.23/bin/named/sortlist.c" (7 Sep 2020, 3969 Bytes) of package /linux/misc/dns/bind9/9.11.23/bind-9.11.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "sortlist.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /* $Id: sortlist.c,v 1.17 2007/09/14 01:46:05 marka Exp $ */
   13 
   14 /*! \file */
   15 
   16 #include <config.h>
   17 
   18 #include <isc/mem.h>
   19 #include <isc/util.h>
   20 
   21 #include <dns/acl.h>
   22 #include <dns/result.h>
   23 
   24 #include <named/globals.h>
   25 #include <named/server.h>
   26 #include <named/sortlist.h>
   27 
   28 ns_sortlisttype_t
   29 ns_sortlist_setup(dns_acl_t *acl, isc_netaddr_t *clientaddr,
   30           const void **argp)
   31 {
   32     unsigned int i;
   33 
   34     if (acl == NULL)
   35         goto dont_sort;
   36 
   37     for (i = 0; i < acl->length; i++) {
   38         /*
   39          * 'e' refers to the current 'top level statement'
   40          * in the sortlist (see ARM).
   41          */
   42         dns_aclelement_t *e = &acl->elements[i];
   43         dns_aclelement_t *try_elt;
   44         dns_aclelement_t *order_elt = NULL;
   45         const dns_aclelement_t *matched_elt = NULL;
   46 
   47         if (e->type == dns_aclelementtype_nestedacl) {
   48             dns_acl_t *inner = e->nestedacl;
   49 
   50             if (inner->length == 0)
   51                 try_elt = e;
   52             else if (inner->length > 2)
   53                 goto dont_sort;
   54             else if (inner->elements[0].negative)
   55                 goto dont_sort;
   56             else {
   57                 try_elt = &inner->elements[0];
   58                 if (inner->length == 2)
   59                     order_elt = &inner->elements[1];
   60             }
   61         } else {
   62             /*
   63              * BIND 8 allows bare elements at the top level
   64              * as an undocumented feature.
   65              */
   66             try_elt = e;
   67         }
   68 
   69         if (dns_aclelement_match(clientaddr, NULL, try_elt,
   70                      &ns_g_server->aclenv,
   71                      &matched_elt)) {
   72             if (order_elt != NULL) {
   73                 if (order_elt->type ==
   74                     dns_aclelementtype_nestedacl) {
   75                     *argp = order_elt->nestedacl;
   76                     return (NS_SORTLISTTYPE_2ELEMENT);
   77                 } else if (order_elt->type ==
   78                        dns_aclelementtype_localhost &&
   79                        ns_g_server->aclenv.localhost != NULL) {
   80                     *argp = ns_g_server->aclenv.localhost;
   81                     return (NS_SORTLISTTYPE_2ELEMENT);
   82                 } else if (order_elt->type ==
   83                        dns_aclelementtype_localnets &&
   84                        ns_g_server->aclenv.localnets != NULL) {
   85                     *argp = ns_g_server->aclenv.localnets;
   86                     return (NS_SORTLISTTYPE_2ELEMENT);
   87                 } else {
   88                     /*
   89                      * BIND 8 allows a bare IP prefix as
   90                      * the 2nd element of a 2-element
   91                      * sortlist statement.
   92                      */
   93                     *argp = order_elt;
   94                     return (NS_SORTLISTTYPE_1ELEMENT);
   95                 }
   96             } else {
   97                 INSIST(matched_elt != NULL);
   98                 *argp = matched_elt;
   99                 return (NS_SORTLISTTYPE_1ELEMENT);
  100             }
  101         }
  102     }
  103 
  104     /* No match; don't sort. */
  105  dont_sort:
  106     *argp = NULL;
  107     return (NS_SORTLISTTYPE_NONE);
  108 }
  109 
  110 int
  111 ns_sortlist_addrorder2(const isc_netaddr_t *addr, const void *arg) {
  112     const dns_acl_t *sortacl = (const dns_acl_t *) arg;
  113     int match;
  114 
  115     (void)dns_acl_match(addr, NULL, sortacl,
  116                 &ns_g_server->aclenv,
  117                 &match, NULL);
  118     if (match > 0)
  119         return (match);
  120     else if (match < 0)
  121         return (INT_MAX - (-match));
  122     else
  123         return (INT_MAX / 2);
  124 }
  125 
  126 int
  127 ns_sortlist_addrorder1(const isc_netaddr_t *addr, const void *arg) {
  128     const dns_aclelement_t *matchelt = (const dns_aclelement_t *) arg;
  129     if (dns_aclelement_match(addr, NULL, matchelt,
  130                  &ns_g_server->aclenv,
  131                  NULL)) {
  132         return (0);
  133     } else {
  134         return (INT_MAX);
  135     }
  136 }
  137 
  138 void
  139 ns_sortlist_byaddrsetup(dns_acl_t *sortlist_acl, isc_netaddr_t *client_addr,
  140                dns_addressorderfunc_t *orderp,
  141                const void **argp)
  142 {
  143     ns_sortlisttype_t sortlisttype;
  144 
  145     sortlisttype = ns_sortlist_setup(sortlist_acl, client_addr, argp);
  146 
  147     switch (sortlisttype) {
  148     case NS_SORTLISTTYPE_1ELEMENT:
  149         *orderp = ns_sortlist_addrorder1;
  150         break;
  151     case NS_SORTLISTTYPE_2ELEMENT:
  152         *orderp = ns_sortlist_addrorder2;
  153         break;
  154     case NS_SORTLISTTYPE_NONE:
  155         *orderp = NULL;
  156         break;
  157     default:
  158         UNEXPECTED_ERROR(__FILE__, __LINE__,
  159                  "unexpected return from ns_sortlist_setup(): "
  160                  "%d", sortlisttype);
  161         break;
  162     }
  163 }
  164