2 * Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
3 * Copyright (C) 2000, 2001 Internet Software Consortium.
5 * Permission to use, copy, modify, and distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
9 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
10 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
11 * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
12 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
13 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
14 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
15 * PERFORMANCE OF THIS SOFTWARE.
18 /* $Id: sortlist.c,v 1.5.2.1 2004/03/09 06:09:20 marka Exp $ */
26 #include <dns/result.h>
28 #include <named/globals.h>
29 #include <named/server.h>
30 #include <named/sortlist.h>
33 ns_sortlist_setup(dns_acl_t *acl, isc_netaddr_t *clientaddr, void **argp) {
39 for (i = 0; i < acl->length; i++) {
41 * 'e' refers to the current 'top level statement'
42 * in the sortlist (see ARM).
44 dns_aclelement_t *e = &acl->elements[i];
45 dns_aclelement_t *matchelt = NULL;
48 if (e->type != dns_aclelementtype_nestedacl)
51 inner = e->u.nestedacl;
53 if (inner->length < 1 || inner->length > 2)
56 if (inner->elements[0].negative)
59 if (dns_aclelement_match(clientaddr, NULL,
63 if (inner->length == 2) {
64 dns_aclelement_t *elt1 = &inner->elements[1];
65 if (elt1->type == dns_aclelementtype_nestedacl)
66 *argp = elt1->u.nestedacl;
67 else if (elt1->type == dns_aclelementtype_localhost &&
68 ns_g_server->aclenv.localhost != NULL)
69 *argp = ns_g_server->aclenv.localhost;
70 else if (elt1->type == dns_aclelementtype_localnets &&
71 ns_g_server->aclenv.localnets != NULL)
72 *argp = ns_g_server->aclenv.localnets;
75 return (NS_SORTLISTTYPE_2ELEMENT);
77 INSIST(matchelt != NULL);
79 return (NS_SORTLISTTYPE_1ELEMENT);
84 /* No match; don't sort. */
87 return (NS_SORTLISTTYPE_NONE);
91 ns_sortlist_addrorder2(isc_netaddr_t *addr, void *arg) {
92 dns_acl_t *sortacl = (dns_acl_t *) arg;
95 (void)dns_acl_match(addr, NULL, sortacl,
101 return (INT_MAX - (-match));
103 return (INT_MAX / 2);
107 ns_sortlist_addrorder1(isc_netaddr_t *addr, void *arg) {
108 dns_aclelement_t *matchelt = (dns_aclelement_t *) arg;
109 if (dns_aclelement_match(addr, NULL, matchelt,
110 &ns_g_server->aclenv,
119 ns_sortlist_byaddrsetup(dns_acl_t *sortlist_acl, isc_netaddr_t *client_addr,
120 dns_addressorderfunc_t *orderp,
123 ns_sortlisttype_t sortlisttype;
125 sortlisttype = ns_sortlist_setup(sortlist_acl, client_addr, argp);
127 switch (sortlisttype) {
128 case NS_SORTLISTTYPE_1ELEMENT:
129 *orderp = ns_sortlist_addrorder1;
131 case NS_SORTLISTTYPE_2ELEMENT:
132 *orderp = ns_sortlist_addrorder2;
134 case NS_SORTLISTTYPE_NONE:
138 UNEXPECTED_ERROR(__FILE__, __LINE__,
139 "unexpected return from ns_sortlist_setup(): "