confip.c revision 90adbb5f1cfeac900fecf654c9000d7f26c06b98
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/*
a7038d1a0513c8e804937ebc95fc9cb3a46c04f5Mark Andrews * Copyright (C) 1999 Internet Software Consortium.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * Permission to use, copy, modify, and distribute this software for any
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * purpose with or without fee is hereby granted, provided that the above
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley * copyright notice and this permission notice appear in all copies.
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
15a44745412679c30a6d022733925af70a38b715David Lawrence * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
15a44745412679c30a6d022733925af70a38b715David Lawrence * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
15a44745412679c30a6d022733925af70a38b715David Lawrence * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
15a44745412679c30a6d022733925af70a38b715David Lawrence * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
15a44745412679c30a6d022733925af70a38b715David Lawrence * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
15a44745412679c30a6d022733925af70a38b715David Lawrence * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
15a44745412679c30a6d022733925af70a38b715David Lawrence * SOFTWARE.
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews#include <config.h>
9c3531d72aeaad6c5f01efe6a1c82023e1379e4dDavid Lawrence
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff#include <string.h>
110d1702731f42dd620879c1d765ebe91f3920ceMichael Graff
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley#include <isc/assertions.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <isc/error.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence#include <isc/magic.h>
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews#include <dns/confip.h>
364a82f7c25b62967678027043425201a5e5171aBob Halley#include <dns/confcommon.h>
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#include <dns/log.h>
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley#define IPLIST_MAGIC 0x49706c73 /* Ipls */ /* dns_c_iplist */
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley#define IPMDIRECT_MAGIC 0x49506d64 /* IPmd */ /* dns_c_ipmatch_direct */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define IPMINDIRECT_MAGIC 0x69506d69 /* iPmi */ /* dns_c_ipmatch_indirect */
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews#define IPMELEM_MAGIC 0x49704d65 /* IpMe */ /* dns_c_ipmatch_element */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define IPMLIST_MAGIC 0x69706d6c /* ipml */ /* dns_c_ipmatchlist */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define DNS_IPLIST_VALID(ipl) ISC_MAGIC_VALID(ipl,IPLIST_MAGIC)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define DNS_IPDIRECT_VALID(ipmld) ISC_MAGIC_VALID(ipmld, IPMDIRECT_MAGIC)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define DNS_IPINDIRECT_VALID(ipmlid) ISC_MAGIC_VALID(ipmlid, IPMINDIRECT_MAGIC)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define DNS_IPMELEM_VALID(impe) ISC_MAGIC_VALID(impe, IPMELEM_MAGIC)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley#define DNS_IPMLIST_VALID(ipml) ISC_MAGIC_VALID(ipml, IPMLIST_MAGIC)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley/* Flag for dns_c_ipmatch_element */
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley#define DNS_C_IPMATCH_NEGATE 0x01 /* match means deny access */
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halleystatic isc_result_t checkmask(isc_sockaddr_t *address, isc_uint32_t bits);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleystatic isc_result_t bits2v6mask(struct in6_addr *addr, isc_uint32_t bits);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halleyisc_result_t
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halleydns_c_ipmatchelement_new(isc_log_t *lctx,
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff isc_mem_t *mem, dns_c_ipmatchelement_t **result)
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrews{
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley dns_c_ipmatchelement_t *ime ;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley (void) lctx;
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley REQUIRE(result != NULL);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley *result = NULL;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ime = isc_mem_get(mem, sizeof *ime);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (ime == NULL) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (ISC_R_NOMEMORY);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley }
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ime->magic = IPMELEM_MAGIC;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ime->type = dns_c_ipmatch_none;
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley ime->flags = 0;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley memset(&ime->u, 0x0, sizeof ime->u);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ISC_LINK_INIT(ime, next);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley *result = ime;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (ISC_R_SUCCESS);
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley}
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrewsisc_boolean_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleydns_c_ipmatchelement_isneg(isc_log_t *lctx,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley dns_c_ipmatchelement_t *elem)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley{
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley (void) lctx;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley REQUIRE(DNS_IPMELEM_VALID(elem));
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (ISC_TF((elem->flags & DNS_C_IPMATCH_NEGATE) ==
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley DNS_C_IPMATCH_NEGATE));
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley}
2cd0c38115b1efb043ed3104c0d08e51ceade0d7Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleyisc_result_t
078d49b63324f01d98301ee21671abee0c41fcdeBob Halleydns_c_ipmatchelement_delete(isc_log_t *lctx,
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley isc_mem_t *mem, dns_c_ipmatchelement_t **ipme)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley{
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley dns_c_ipmatchelement_t *elem;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley REQUIRE(mem != NULL);
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley REQUIRE(ipme != NULL);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (*ipme == NULL) {
ca67883a666bdf314d3da958d5195e7215b1f797Bob Halley return (ISC_R_SUCCESS);
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley }
b12f0228b32775ee688ed21ddbf3a116c1adfb43Michael Graff
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrews elem = *ipme;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley REQUIRE(DNS_IPMELEM_VALID(elem));
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
80b782f356f0692c11b4e52e8dd46ec41704e5a2Mark Andrews switch (elem->type) {
e496615043400500492fa7b891c515c8e7cb7d08Bob Halley case dns_c_ipmatch_localhost:
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews case dns_c_ipmatch_localnets:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley case dns_c_ipmatch_pattern:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley /* nothing */
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley break;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley case dns_c_ipmatch_indirect:
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley INSIST(elem->u.indirect.list != NULL);
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence dns_c_ipmatchlist_delete(lctx, &elem->u.indirect.list);
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley if (elem->u.indirect.refname.base != NULL) {
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley isc_mem_put(mem, elem->u.indirect.refname.base,
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley elem->u.indirect.refname.length);
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley }
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley break;
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley case dns_c_ipmatch_key:
28640d1da26d561f4137122fe64e9e8cc08bf11eBob Halley isc_mem_free(mem, elem->u.key );
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley break;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson case dns_c_ipmatch_acl:
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley isc_mem_free(mem, elem->u.aclname);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley break;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley case dns_c_ipmatch_none:
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson isc_log_write(lctx, DNS_LOGCATEGORY_CONFIG,
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson DNS_LOGMODULE_CONFIG, ISC_LOG_CRITICAL,
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff "dns_ipmath_none element type");
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley return (ISC_R_FAILURE);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley }
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley isc_mem_put(mem, elem, sizeof *elem);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley *ipme = NULL;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson return (ISC_R_SUCCESS);
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson}
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleyisc_result_t
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halleydns_c_ipmatchelement_copy(isc_log_t *lctx,
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley isc_mem_t *mem,
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley dns_c_ipmatchelement_t **dest,
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley dns_c_ipmatchelement_t *src)
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley{
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley isc_result_t result;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley dns_c_ipmatchelement_t *newel;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley REQUIRE(mem != NULL);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley REQUIRE(dest != NULL);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley REQUIRE(DNS_IPMELEM_VALID(src));
e02884167b7c969b56413f76c48c3802c4dca14dAndreas Gustafsson
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley result = dns_c_ipmatchelement_new(lctx, mem, &newel);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley if (result != ISC_R_SUCCESS) {
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley return (result);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley }
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley newel->type = src->type;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley newel->flags = src->flags;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley switch(src->type) {
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley case dns_c_ipmatch_pattern:
732e0731dec1922747bb3b3147cf2c3d16b22eaaBob Halley newel->u.direct.address = src->u.direct.address;
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews newel->u.direct.mask = src->u.direct.mask;
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews break;
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley case dns_c_ipmatch_indirect:
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley result = dns_c_ipmatchlist_copy(lctx, mem,
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley &newel->u.indirect.list,
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley src->u.indirect.list);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley break;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley case dns_c_ipmatch_localhost:
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley break;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley case dns_c_ipmatch_localnets:
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley break;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley case dns_c_ipmatch_key:
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley newel->u.key = isc_mem_strdup(mem, src->u.key);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley break;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley case dns_c_ipmatch_acl:
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley newel->u.aclname = isc_mem_strdup(mem, src->u.aclname);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley break;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley case dns_c_ipmatch_none:
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley isc_log_write(lctx, DNS_LOGCATEGORY_CONFIG,
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley DNS_LOGMODULE_CONFIG, ISC_LOG_CRITICAL,
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley "ipmatch 'none' element type");
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley return (ISC_R_FAILURE);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley }
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley *dest = newel;
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley return (ISC_R_SUCCESS);
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley}
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
7837d146219db7a85a4b444a9cdf6602254a4f75Bob Halley
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halleyisc_boolean_t
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halleydns_c_ipmatchelement_equal(dns_c_ipmatchelement_t *e1,
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley dns_c_ipmatchelement_t *e2)
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley{
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley REQUIRE(DNS_IPMELEM_VALID(e1));
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley REQUIRE(DNS_IPMELEM_VALID(e2));
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley if ((e1->type != e2->type) || (e1->flags != e2->flags))
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley return (ISC_FALSE);
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley switch (e1->type) {
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence case dns_c_ipmatch_pattern:
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley if (e1->u.direct.mask != e2->u.direct.mask)
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley return (ISC_FALSE);
1c724c986de1449e3b2f1eeae4c724dc0d97603cBob Halley return (isc_sockaddr_equal(&e1->u.direct.address,
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff &e2->u.direct.address));
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley case dns_c_ipmatch_indirect:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (dns_c_ipmatchlist_equal(e1->u.indirect.list,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley e2->u.indirect.list));
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley case dns_c_ipmatch_localhost:
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley break;
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley case dns_c_ipmatch_localnets:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley break;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley case dns_c_ipmatch_key:
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff return (ISC_TF(strcmp(e1->u.key, e2->u.key) == 0));
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley case dns_c_ipmatch_acl:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley return (ISC_TF(strcmp(e1->u.aclname, e2->u.aclname) == 0));
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley case dns_c_ipmatch_none:
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley break;
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley }
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley return (ISC_TRUE);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley}
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleyisc_result_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleydns_c_ipmatchlocalhost_new(isc_log_t *lctx,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley isc_mem_t *mem, dns_c_ipmatchelement_t **result)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley{
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley dns_c_ipmatchelement_t *ime = NULL;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley isc_result_t res;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley REQUIRE(mem != NULL);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley REQUIRE(result != NULL);
078d49b63324f01d98301ee21671abee0c41fcdeBob Halley
0180ccf72c79b98eb8ee5abbb7331aec6951dd9fBob Halley *result = NULL;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley res = dns_c_ipmatchelement_new(lctx, mem, &ime);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (res == ISC_R_SUCCESS) {
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley ime->type = dns_c_ipmatch_localhost;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley }
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews *result = ime;
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (res);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson}
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonisc_result_t
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssondns_c_ipmatchlocalnets_new(isc_log_t *lctx,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_mem_t *mem,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson dns_c_ipmatchelement_t **result)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson{
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson dns_c_ipmatchelement_t *ime = NULL;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_result_t res;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews REQUIRE(mem != NULL);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews REQUIRE(result != NULL);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews *result = NULL;
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington res = dns_c_ipmatchelement_new(lctx, mem, &ime);
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley if (res == ISC_R_SUCCESS) {
88a6fef4944a00d8350ffd8b64ef58c694b8335eMark Andrews ime->type = dns_c_ipmatch_localnets;
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff }
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews *result = ime;
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff return (res);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley}
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrewsisc_result_t
5619558151f1aa4249b3ead979e76876e29278b6Bob Halleydns_c_ipmatchindirect_new(isc_log_t *lctx,
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews isc_mem_t *mem,
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews dns_c_ipmatchelement_t **result,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley dns_c_ipmatchlist_t *iml,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley const char *name)
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley{
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley dns_c_ipmatchelement_t *ime;
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley dns_c_ipmatchlist_t *iml_copy;
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley isc_result_t res;
ccbfddc70ef38263daca312d29bb8c5077e24785Bob Halley
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson REQUIRE(mem != NULL);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews REQUIRE(result != NULL);
54f959d12b5a1f9315fbf6a776c6d349316e9686Bob Halley REQUIRE(DNS_IPMLIST_VALID(iml));
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley *result = NULL;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley res = dns_c_ipmatchlist_copy(lctx, mem, &iml_copy, iml);
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley if (res != ISC_R_SUCCESS) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (res);
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley }
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley res = dns_c_ipmatchelement_new(lctx, mem, &ime);
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley if (res == ISC_R_SUCCESS) {
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington ime->type = dns_c_ipmatch_indirect;
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington ime->u.indirect.list = iml_copy;
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington if (name != NULL) {
b2ca6fd3a8293440b4d263723525396059cf2400Brian Wellington ime->u.indirect.refname.length = strlen(name) + 1;
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington ime->u.indirect.refname.base =
89d8adb6663b13435ff9ae1eb53e45da7fa79275Bob Halley isc_mem_get(mem,
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley ime->u.indirect.refname.length);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley RUNTIME_CHECK(ime->u.indirect.refname.base != NULL);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff strcpy(ime->u.indirect.refname.base, name);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff } else {
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley dns_c_ipmatchlist_delete(lctx, &iml_copy);
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley }
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson *result = ime;
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (res);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews}
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
93d6dfaf66258337985427c86181f01fc51f0bb4Mark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonisc_result_t
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrewsdns_c_ipmatchpattern_new(isc_log_t *lctx,
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews isc_mem_t *mem,
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews dns_c_ipmatchelement_t **result,
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews isc_sockaddr_t address,
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews isc_uint32_t maskbits)
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews{
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews dns_c_ipmatchelement_t *ime ;
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews isc_result_t res;
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews REQUIRE(result != NULL);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews REQUIRE(mem != NULL);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson *result = NULL;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson res = checkmask(&address, maskbits);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (res != ISC_R_SUCCESS) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (res);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson }
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson res = dns_c_ipmatchelement_new(lctx, mem, &ime);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (res != ISC_R_SUCCESS) {
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (res);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson }
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson ime->type = dns_c_ipmatch_pattern;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews ime->u.direct.address = address;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley ime->u.direct.mask = maskbits;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley *result = ime;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley return (ISC_R_SUCCESS);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews}
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonisc_result_t
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssondns_c_ipmatchkey_new(isc_log_t *lctx,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_mem_t *mem,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson dns_c_ipmatchelement_t **result,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson const char *key)
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews{
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews dns_c_ipmatchelement_t *ipme;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews isc_result_t res;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews REQUIRE(result != NULL);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews REQUIRE(mem != NULL);
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews REQUIRE(key != NULL);
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews *result = NULL;
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews res = dns_c_ipmatchelement_new(lctx, mem, &ipme);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews if (res != ISC_R_SUCCESS) {
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews return (res);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews ipme->type = dns_c_ipmatch_key;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews ipme->u.key = isc_mem_strdup(mem, key);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews *result = ipme;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (ISC_R_SUCCESS);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley}
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrewsisc_result_t
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrewsdns_c_ipmatch_aclnew(isc_log_t *lctx,
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews isc_mem_t *mem,
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrews dns_c_ipmatchelement_t **result,
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrews const char *aclname)
61fb42c4ef45d88e115bd769c30c4f36b461870bMark Andrews{
def8e47c688e2480a4539d69c3d1a0a28a7c0550Mark Andrews dns_c_ipmatchelement_t *ipme;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_result_t res;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
abc93ada82b37282122f8162f48761eb99736571Mark Andrews REQUIRE(result != NULL);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews REQUIRE(mem != NULL);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews REQUIRE(aclname != NULL);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews REQUIRE(strlen(aclname) > 0);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson *result = NULL;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson res = dns_c_ipmatchelement_new(lctx, mem, &ipme);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley if (res != ISC_R_SUCCESS) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley return (res);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews }
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews ipme->type = dns_c_ipmatch_acl;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews ipme->u.aclname = isc_mem_strdup(mem, aclname);
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews *result = ipme;
2dd99c098ca162f985b7ef3c8142a964ad8281aeMark Andrews
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley return (ISC_R_SUCCESS);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley}
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrewsisc_result_t
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halleydns_c_ipmatch_negate(isc_log_t *lctx,
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff dns_c_ipmatchelement_t *ipe)
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff{
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence REQUIRE(DNS_IPMELEM_VALID(ipe));
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews (void) lctx;
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson if ((ipe->flags & DNS_C_IPMATCH_NEGATE) == DNS_C_IPMATCH_NEGATE) {
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff ipe->flags &= ~DNS_C_IPMATCH_NEGATE;
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff } else {
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley ipe->flags |= DNS_C_IPMATCH_NEGATE;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff }
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff return (ISC_R_SUCCESS);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff}
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halleyisc_result_t
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halleydns_c_ipmatchlist_new(isc_log_t *lctx,
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews isc_mem_t *mem, dns_c_ipmatchlist_t **ptr)
22f735acbce7ffe95af20bb58bb8929b6f1d674fMichael Graff{
904a5734375869ffb504ed8cde6b68cafadb6d64Bob Halley dns_c_ipmatchlist_t *newlist;
bf345589ce0b0b64533d4566e4992a0e63aac6f5Bob Halley
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff (void) lctx;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff REQUIRE(ptr != NULL);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff REQUIRE(mem != NULL);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff newlist = isc_mem_get(mem, sizeof *newlist);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff if (newlist == NULL) {
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff return (ISC_R_NOMEMORY);
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff }
1a69a1a78cfaa86f3b68bbc965232b7876d4da2aDavid Lawrence
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff newlist->magic = IPMLIST_MAGIC;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley newlist->mem = mem;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson newlist->refcount = 1;
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews
43b8fb75704f290a48fc96461140b42b2488348dMark Andrews ISC_LIST_INIT(newlist->elements);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
c03bb27f0675a6e60ceea66b451548e8481bc05cMark Andrews *ptr = newlist;
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graff
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_SUCCESS);
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley}
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence
69be7837c920fac5c71a73e8fad586f9a2711e96Michael Graffisc_result_t
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrencedns_c_ipmatchlist_delete(isc_log_t *lctx,
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence dns_c_ipmatchlist_t **ml)
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews{
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews dns_c_ipmatchelement_t *ime;
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff dns_c_ipmatchelement_t *iptr;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley dns_c_ipmatchlist_t *iml;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley isc_mem_t *mem;
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(ml != NULL);
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley iml = *ml;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson if (iml == NULL) {
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley return (ISC_R_SUCCESS);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson }
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff *ml = NULL;
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff REQUIRE(DNS_IPMLIST_VALID(iml));
c3b708aaf1bb0a118e0e11befa1b732acfb1d079Bob Halley INSIST(iml->refcount > 0);
76883e8cee593f45c65b0936e5d6e8f778d6e3efMichael Graff
ccbfddc70ef38263daca312d29bb8c5077e24785Bob Halley iml->refcount--;
ccbfddc70ef38263daca312d29bb8c5077e24785Bob Halley if (iml->refcount > 0) {
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews return (ISC_R_SUCCESS);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews }
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley mem = iml->mem;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews INSIST(mem != NULL);
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews
8af4b7042d84e0699f7c1de306526098030b74a9Mark Andrews ime = ISC_LIST_HEAD(iml->elements);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews while (ime != NULL) {
92296c744ef9ef36de499eb10b3e27800d8b24a5Mark Andrews iptr = ISC_LIST_NEXT(ime, next);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews dns_c_ipmatchelement_delete(lctx, mem, &ime);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence ime = iptr;
13eaef2df27683658868c3828c18880d7419a16aDavid Lawrence }
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley isc_mem_put(mem, iml, sizeof *iml);
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halley
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews return (ISC_R_SUCCESS);
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews}
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrews
c569a0e4d518bf30be1b2ba9cc3592fe470e9822Mark Andrewsdns_c_ipmatchlist_t *
8dfa9caeec8e68db0c937e347a3d6629e7627d54Bob Halleydns_c_ipmatchlist_attach(isc_log_t *lctx,
5619558151f1aa4249b3ead979e76876e29278b6Bob Halley dns_c_ipmatchlist_t *ipml)
d981ca645597116d227a48bf37cc5edc061c854dBob Halley{
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews (void) lctx;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews REQUIRE(DNS_IPMLIST_VALID(ipml));
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews INSIST(ipml->refcount > 0);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington ipml->refcount++;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews return (ipml);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews}
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellingtonisc_result_t
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrewsdns_c_ipmatchlist_empty(isc_log_t *lctx,
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews dns_c_ipmatchlist_t *ipml)
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews{
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews dns_c_ipmatchelement_t *ime ;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews dns_c_ipmatchelement_t *imptmp;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews isc_result_t res = ISC_R_SUCCESS;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews REQUIRE(DNS_IPMLIST_VALID(ipml));
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington ime = ISC_LIST_HEAD(ipml->elements);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews while (ime != NULL) {
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews imptmp = ISC_LIST_NEXT(ime, next);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews res = dns_c_ipmatchelement_delete(lctx, ipml->mem, &ime);
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews if (res != ISC_R_SUCCESS) {
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews break;
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington }
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington ime = imptmp;
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews }
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson return (res);
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson}
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafssonisc_result_t
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellingtondns_c_ipmatchlist_copy(isc_log_t *lctx, isc_mem_t *mem,
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson dns_c_ipmatchlist_t **dest, dns_c_ipmatchlist_t *src)
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson{
03f91269f5453bcbd924910ef85a8f8496cf2661Mark Andrews dns_c_ipmatchelement_t *ime;
84185d19c7a9ef1ac23cc6236c8773697d4efeb1Brian Wellington dns_c_ipmatchelement_t *ptr;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson dns_c_ipmatchlist_t *newlist;
febaa091847ab004f40500cc475a819f2c73fcddAndreas Gustafsson isc_result_t result;
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff
d981ca645597116d227a48bf37cc5edc061c854dBob Halley REQUIRE(mem != NULL);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley REQUIRE(dest != NULL);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley REQUIRE(DNS_IPMLIST_VALID(src));
88a6fef4944a00d8350ffd8b64ef58c694b8335eMark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graff *dest = NULL;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley result = dns_c_ipmatchlist_new(lctx, mem, &newlist);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (result != ISC_R_SUCCESS) {
d981ca645597116d227a48bf37cc5edc061c854dBob Halley return (result);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley ime = ISC_LIST_HEAD(src->elements);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley while (ime != NULL) {
d981ca645597116d227a48bf37cc5edc061c854dBob Halley result = dns_c_ipmatchelement_copy(lctx, mem, &ptr, ime);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley if (result != ISC_R_SUCCESS) {
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff dns_c_ipmatchlist_delete(lctx, &newlist);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley return (result);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
d981ca645597116d227a48bf37cc5edc061c854dBob Halley ISC_LIST_APPEND(newlist->elements, ptr, next);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff ime = ISC_LIST_NEXT(ime, next);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley }
43b8fb75704f290a48fc96461140b42b2488348dMark Andrews
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff *dest = newlist;
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_SUCCESS);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley}
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graffisc_boolean_t
d981ca645597116d227a48bf37cc5edc061c854dBob Halleydns_c_ipmatchlist_equal(dns_c_ipmatchlist_t *l1, dns_c_ipmatchlist_t *l2) {
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews dns_c_ipmatchelement_t *e1, *e2;
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews REQUIRE(l1 == NULL || DNS_IPMLIST_VALID(l1));
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews REQUIRE(l2 == NULL || DNS_IPMLIST_VALID(l2));
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews if (l1 == NULL && l2 == NULL)
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews return (ISC_TRUE);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews if (l1 != NULL || l2 != NULL)
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews return (ISC_FALSE);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews e1 = ISC_LIST_HEAD(l1->elements);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews e2 = ISC_LIST_HEAD(l2->elements);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews while (e1 != NULL && e2 != NULL) {
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews if (!dns_c_ipmatchelement_equal(e1, e2))
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews return (ISC_FALSE);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews e1 = ISC_LIST_NEXT(e1, next);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews e2 = ISC_LIST_NEXT(e2, next);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews }
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews if (l1 != NULL || l2 != NULL)
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews return (ISC_FALSE);
35541328a8c18ba1f984300dfe30ec8713c90031Mark Andrews return (ISC_TRUE);
}
isc_result_t
dns_c_ipmatchlist_append(isc_log_t *lctx,
dns_c_ipmatchlist_t *dest,
dns_c_ipmatchlist_t *src,
isc_boolean_t negate)
{
dns_c_ipmatchelement_t *ime;
dns_c_ipmatchelement_t *ime_copy;
isc_result_t result = ISC_R_SUCCESS;
REQUIRE(DNS_IPMLIST_VALID(dest));
REQUIRE(DNS_IPMLIST_VALID(src));
ime = ISC_LIST_HEAD(src->elements);
while (ime != NULL) {
result = dns_c_ipmatchelement_copy(lctx, dest->mem,
&ime_copy,
ime);
if (result != ISC_R_SUCCESS) {
break;
}
if (negate) {
dns_c_ipmatch_negate(lctx, ime_copy);
}
ISC_LIST_APPEND(dest->elements, ime_copy, next);
ime = ISC_LIST_NEXT(ime, next);
}
return (result);
}
isc_result_t
dns_c_ipmatchelement_print(isc_log_t *lctx,
FILE *fp, int indent,
dns_c_ipmatchelement_t *ipme)
{
int bits;
REQUIRE(fp != NULL);
REQUIRE(DNS_IPMELEM_VALID(ipme));
if ((ipme->flags & DNS_C_IPMATCH_NEGATE) == DNS_C_IPMATCH_NEGATE) {
fputc('!', fp);
} else {
fputc(' ', fp);
}
switch (ipme->type) {
case dns_c_ipmatch_pattern:
dns_c_print_ipaddr(lctx, fp, &ipme->u.direct.address);
bits = ipme->u.direct.mask;
if (bits > 0) {
fprintf(fp, "/%d", bits);
}
break;
case dns_c_ipmatch_indirect:
if (ipme->u.indirect.refname.base != NULL) {
fprintf(fp, "%s", ipme->u.indirect.refname.base);
} else {
dns_c_ipmatchlist_print(lctx, fp, indent,
ipme->u.indirect.list);
}
break;
case dns_c_ipmatch_key:
fprintf(fp, "key %s", ipme->u.key);
break;
case dns_c_ipmatch_localhost:
fprintf(fp, "localhost");
break;
case dns_c_ipmatch_localnets:
fprintf(fp, "localnets");
break;
case dns_c_ipmatch_none:
isc_log_write(lctx, DNS_LOGCATEGORY_CONFIG,
DNS_LOGMODULE_CONFIG, ISC_LOG_CRITICAL,
"dns_ipmatch_none element type");
return (ISC_R_FAILURE);
case dns_c_ipmatch_acl:
fprintf(fp, "%s", ipme->u.aclname);
break;
}
return (ISC_R_SUCCESS);
}
isc_result_t
dns_c_ipmatchlist_print(isc_log_t *lctx,
FILE *fp, int indent, dns_c_ipmatchlist_t *ml)
{
dns_c_ipmatchelement_t *ipme ;
REQUIRE(DNS_IPMLIST_VALID(ml));
REQUIRE(fp != NULL);
/* no indent on first line. */
fprintf(fp, "{\n");
ipme = ISC_LIST_HEAD(ml->elements);
if (ipme == NULL) {
dns_c_printtabs(lctx, fp, indent);
fprintf(fp,
"/* this list intentionally left blank */\n");
} else {
while (ipme != NULL) {
dns_c_printtabs(lctx, fp, indent);
dns_c_ipmatchelement_print(lctx, fp, indent + 1, ipme);
fprintf(fp, ";\n");
ipme = ISC_LIST_NEXT(ipme, next);
}
}
dns_c_printtabs(lctx, fp, indent - 1);
fprintf(fp, "}");
return (ISC_R_SUCCESS);
}
isc_result_t
dns_c_iplist_new(isc_log_t *lctx,
isc_mem_t *mem, int length, dns_c_iplist_t **newlist)
{
dns_c_iplist_t *list;
size_t bytes;
(void) lctx;
REQUIRE(mem != NULL);
REQUIRE(length > 0);
REQUIRE(newlist != NULL);
list = isc_mem_get(mem, sizeof *list);
if (list == NULL) {
return (ISC_R_NOMEMORY);
}
bytes = sizeof (isc_sockaddr_t) * length;
list->ips = isc_mem_get(mem, bytes);
if (list->ips == NULL) {
isc_mem_put(mem, list, sizeof *list);
return (ISC_R_NOMEMORY);
}
memset(list->ips, 0x0, bytes);
list->magic = IPLIST_MAGIC;
list->size = length;
list->nextidx = 0;
list->mem = mem;
list->refcount = 1;
*newlist = list;
return (ISC_R_SUCCESS);
}
isc_result_t
dns_c_iplist_delete(isc_log_t *lctx,
dns_c_iplist_t **list)
{
dns_c_iplist_t *l ;
(void) lctx;
REQUIRE(list != NULL);
l = *list;
if (l == NULL) {
return (ISC_R_SUCCESS);
}
REQUIRE(DNS_IPLIST_VALID(l));
INSIST(l->refcount > 0);
l->refcount--;
if (l->refcount == 0) {
isc_mem_put(l->mem, l->ips, sizeof (isc_sockaddr_t) * l->size);
isc_mem_put(l->mem, l, sizeof *l);
}
*list = NULL;
return (ISC_R_SUCCESS);
}
dns_c_iplist_t *
dns_c_iplist_attach(isc_log_t *lctx,
dns_c_iplist_t *list)
{
(void) lctx;
REQUIRE(DNS_IPLIST_VALID(list));
INSIST(list->refcount > 0);
list->refcount++;
return (list);
}
isc_result_t
dns_c_iplist_copy(isc_log_t *lctx,
isc_mem_t *mem, dns_c_iplist_t **dest, dns_c_iplist_t *src)
{
dns_c_iplist_t *newl;
isc_result_t res;
isc_uint32_t i;
REQUIRE(dest != NULL);
REQUIRE(DNS_IPLIST_VALID(src));
res = dns_c_iplist_new(lctx, mem, src->size, &newl);
if (res != ISC_R_SUCCESS) {
return (res);
}
for (i = 0 ; i < src->nextidx ; i++) {
newl->ips[i] = src->ips[i];
}
newl->nextidx = src->nextidx;
*dest = newl;
return (ISC_R_SUCCESS);
}
isc_boolean_t
dns_c_iplist_equal(dns_c_iplist_t *list1, dns_c_iplist_t *list2) {
isc_uint32_t i;
REQUIRE(DNS_IPLIST_VALID(list1));
REQUIRE(DNS_IPLIST_VALID(list2));
if (list1->nextidx != list2->nextidx)
return (ISC_FALSE);
for (i = 0 ; i < list1->nextidx ; i++) {
if (!isc_sockaddr_equal(&list1->ips[i], &list2->ips[i]))
return (ISC_FALSE);
}
return (ISC_TRUE);
}
void
dns_c_iplist_print(isc_log_t *lctx,
FILE *fp, int indent, dns_c_iplist_t *list)
{
isc_uint32_t i;
REQUIRE(DNS_IPLIST_VALID(list));
fprintf(fp, "{\n");
if (list->nextidx == 0) {
dns_c_printtabs(lctx, fp, indent);
fprintf(fp, "/* no ip addresses defined */\n");
} else {
for (i = 0 ; i < list->nextidx ; i++) {
dns_c_printtabs(lctx, fp, indent);
dns_c_print_ipaddr(lctx, fp, &list->ips[i]);
fprintf(fp, ";\n");
}
}
dns_c_printtabs(lctx, fp, indent - 1);
fprintf(fp, "};\n");
}
isc_result_t
dns_c_iplist_append(isc_log_t *lctx,
dns_c_iplist_t *list, isc_sockaddr_t newaddr)
{
isc_uint32_t i;
(void) lctx;
REQUIRE(DNS_IPLIST_VALID(list));
for (i = 0 ; i < list->nextidx ; i++) {
if (memcmp(&list->ips[i], &newaddr, sizeof newaddr) == 0) {
break;
}
}
if (i < list->nextidx) {
return (ISC_R_FAILURE);
}
if (list->nextidx == list->size) {
isc_sockaddr_t *newlist;
size_t newbytes;
size_t oldbytes = list->size * sizeof (list->ips[0]);
size_t newsize = list->size + 10;
newbytes = sizeof (list->ips[0]) * newsize;
newlist = isc_mem_get(list->mem, newbytes);
if (newlist == NULL) {
return (ISC_R_NOMEMORY);
}
memset(newlist, 0x0, newbytes);
memcpy(newlist, list->ips, oldbytes);
isc_mem_put(list->mem, list->ips, oldbytes);
list->ips = newlist;
list->size = newsize;
}
list->ips[i] = newaddr;
list->nextidx++;
return (ISC_R_SUCCESS);
}
isc_result_t
dns_c_iplist_remove(isc_log_t *lctx,
dns_c_iplist_t *list, isc_sockaddr_t newaddr)
{
isc_uint32_t i;
(void) lctx;
REQUIRE(DNS_IPLIST_VALID(list));
for (i = 0 ; i < list->nextidx ; i++) {
if (memcmp(&list->ips[0], &newaddr, sizeof newaddr) == 0) {
break;
}
}
if (i == list->nextidx) {
return (ISC_R_FAILURE);
}
list->nextidx--;
for ( /* nothing */ ; i < list->nextidx ; i++) {
list->ips[i] = list->ips[i + 1];
}
return (ISC_R_SUCCESS);
}
/*
* Check that the address given is a network address with the given number
* of high order bits.
*/
static isc_result_t
checkmask(isc_sockaddr_t *address, isc_uint32_t bits)
{
if (bits > 0) {
if (address->type.sa.sa_family == AF_INET) {
isc_uint32_t mask;
mask = ntohl(0xffffffffU << (32 - bits));
if ((mask & address->type.sin.sin_addr.s_addr) !=
address->type.sin.sin_addr.s_addr) {
return (ISC_R_FAILURE);
}
} else if (address->type.sa.sa_family == AF_INET6) {
struct in6_addr iaddr;
unsigned char *maskp;
unsigned char *addrp;
int i;
if (bits2v6mask(&iaddr, bits) != ISC_R_SUCCESS) {
return (ISC_R_FAILURE);
}
addrp = (unsigned char *)&address->type.sin6.sin6_addr;
maskp = (unsigned char *)&iaddr;
for (i = 0 ; i < 16 ; i++) {
if ((addrp[i] & maskp[i]) != addrp[i]) {
return (ISC_R_FAILURE);
}
}
}
}
return (ISC_R_SUCCESS);
}
/*
* Create a 128 bits mask in network byte order in the the IPv6 address
* section of the sockaddr. The bits argument is the number of high bits
* that are to be set to 1.
*/
static isc_result_t
bits2v6mask(struct in6_addr *addr, isc_uint32_t bits)
{
int i;
isc_uint32_t bitmask[4];
char addrbuff [ sizeof "ffff:ffff:ffff:ffff:ffff:ffff:ffff:ffff" + 1 ];
INSIST(bits < 128);
/* Break the 128 bits up into 32-bit sections */
bitmask[0] = bitmask[1] = bitmask[2] = bitmask[3] = 0U;
if (bits > 32) {
bitmask[0] = 0xffffffffU;
} else if (bits > 0) {
bitmask[0] = 0xffffffffU << (32 - bits);
}
if (bits > 64) {
bitmask[1] = 0xffffffffU;
} else if (bits > 32) {
bitmask[1] = 0xffffffffU << (64 - bits);
}
if (bits > 96) {
bitmask[2] = 0xffffffffU;
bitmask[3] = 0xffffffffU << (128 - bits);
} else if (bits > 64) {
bitmask[2] = 0xffffffffU << (96 - bits);
}
memset(addr, 0x0, sizeof *addr);
sprintf(addrbuff, "%04x:%04x:%04x:%04x:%04x:%04x:%04x:%04x",
(((bitmask[0] & 0xffff0000U) >> 16) & 0xffffU),
(bitmask[0] & 0xffff),
(((bitmask[1] & 0xffff0000U) >> 16) & 0xffffU),
(bitmask[1] & 0xffff),
(((bitmask[2] & 0xffff0000U) >> 16) & 0xffffU),
(bitmask[2] & 0xffff),
(((bitmask[3] & 0xffff0000U) >> 16) & 0xffffU),
(bitmask[3] & 0xffff));
i = inet_pton(AF_INET6, addrbuff, &addr);
return (i == 1 ? ISC_R_SUCCESS : ISC_R_FAILURE);
}