a6_38.h revision 901b5fb3b3fa409a42ac7e93b7643f373997305b
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley/*
ca41b452ede6feaa9d8739ec3cae19389a7b0d03Bob Halley * Copyright (C) 1999 Internet Software Consortium.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley *
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * Permission to use, copy, modify, and distribute this software for any
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * purpose with or without fee is hereby granted, provided that the above
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * copyright notice and this permission notice appear in all copies.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley *
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM DISCLAIMS
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL INTERNET SOFTWARE
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley * SOFTWARE.
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley /* $Id: a6_38.h,v 1.1 1999/02/02 04:52:31 marka Exp $ */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley /* draft-ietf-ipngwg-dns-lookups-03.txt */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#ifndef RDATA_IN_1_A6_28_H
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#define RDATA_IN_1_A6_28_H
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
6a759e38699d133302d0df120d7afcd4af721951David Lawrence#include <string.h>
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#include <sys/types.h>
f73e404eb9e1d314f85128cae48cdee036b405ecAndreas Gustafsson#include <sys/socket.h>
f73e404eb9e1d314f85128cae48cdee036b405ecAndreas Gustafsson
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#include <netinet/in.h>
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#include <arpa/inet.h>
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#include <isc/inet.h>
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#ifndef MAX
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#define MAX(A, B) ((A > B) ? (A) : (B))
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley#endif
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleystatic dns_result_t
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleyfromtext_in_a6(dns_rdataclass_t class, dns_rdatatype_t type,
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_lex_t *lexer, dns_name_t *origin,
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_boolean_t downcase, isc_buffer_t *target)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley{
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_token_t token;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley unsigned char addr[16];
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley unsigned char prefixlen;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley unsigned char octets;
941f41d6139c868e357892869c94c4693b943373Bob Halley unsigned char mask;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley dns_name_t name;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_buffer_t buffer;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley REQUIRE(type == 38);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley REQUIRE(class == 1);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley /* prefix length */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley RETERR(gettoken(lexer, &token, isc_tokentype_number, ISC_FALSE));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (token.value.as_ulong > 128)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley return (DNS_R_RANGE);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley prefixlen = token.value.as_ulong;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley RETERR(mem_tobuffer(target, &prefixlen, 1));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley /* suffix */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (prefixlen != 128) {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley /* prefix 0..127 */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley octets = prefixlen/8;
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley /* octets 0..15 */
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley RETERR(gettoken(lexer, &token, isc_tokentype_string,
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley ISC_FALSE));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (isc_inet_pton(AF_INET6, token.value.as_pointer, &addr) != 1)
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley return (DNS_R_UNEXPECTED);
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley mask = 0xff >> (prefixlen % 8);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley addr[octets] &= mask;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley RETERR(mem_tobuffer(target, &addr[octets], 16 - octets));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley }
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (prefixlen == 0)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley return (DNS_R_SUCCESS);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley RETERR(gettoken(lexer, &token, isc_tokentype_string, ISC_FALSE));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley dns_name_init(&name, NULL);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley buffer_fromregion(&buffer, &token.value.as_region,
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley ISC_BUFFERTYPE_TEXT);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley origin = (origin != NULL) ? origin : dns_rootname;
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley return (dns_name_fromtext(&name, &buffer, origin, downcase, target));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley}
6a759e38699d133302d0df120d7afcd4af721951David Lawrence
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleystatic dns_result_t
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleytotext_in_a6(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target) {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_region_t tr;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_region_t sr;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley unsigned char addr[16];
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley unsigned char prefixlen;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley unsigned char octets;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley unsigned char mask;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley char buf[sizeof "128"];
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley dns_name_t name;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley dns_name_t prefix;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_boolean_t sub;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley REQUIRE(rdata->type == 38);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley REQUIRE(rdata->class == 1);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley dns_rdata_toregion(rdata, &sr);
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley prefixlen = sr.base[0];
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley INSIST(prefixlen <= 128);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_region_consume(&sr, 1);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley sprintf(buf, "%u", prefixlen);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley RETERR(str_totext(buf, target));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley RETERR(str_totext(" ", target));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (prefixlen != 128) {
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley octets = prefixlen/8;
c1bfdd299587e790f59378351df66f6168f1e09eAndreas Gustafsson memset(&addr, 0, sizeof addr);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley memcpy(&addr[octets], sr.base, 16 - octets);
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley mask = 0xff >> (prefixlen % 8);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley addr[octets] &= mask;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_buffer_available(target, &tr);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (isc_inet_ntop(AF_INET6, &addr,
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley (char *)tr.base, tr.length) == NULL)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley return (DNS_R_NOSPACE);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_buffer_add(target, strlen((char *)tr.base));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_region_consume(&sr, 16 - octets);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley }
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (prefixlen == 0)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley return (DNS_R_SUCCESS);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley RETERR(str_totext(" ", target));
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley dns_name_init(&name, NULL);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley dns_name_init(&prefix, NULL);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley dns_name_fromregion(&name, &sr);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley sub = name_prefix(&name, origin, &prefix);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley return(dns_name_totext(&prefix, sub, target));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley}
1d9b3491ab3216c8f278dd821455a7a3b08ddfeeBob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleystatic dns_result_t
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halleyfromwire_in_a6(dns_rdataclass_t class, dns_rdatatype_t type,
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley isc_buffer_t *source, dns_decompress_t *dctx,
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_boolean_t downcase, isc_buffer_t *target) {
e4653123ecc6cdbfc0b9eda6e98e44af3b1f9a08Mark Andrews isc_region_t sr;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley unsigned char prefixlen;
e4653123ecc6cdbfc0b9eda6e98e44af3b1f9a08Mark Andrews unsigned char octets;
e4653123ecc6cdbfc0b9eda6e98e44af3b1f9a08Mark Andrews unsigned char mask;
e4653123ecc6cdbfc0b9eda6e98e44af3b1f9a08Mark Andrews dns_name_t name;
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley REQUIRE(type == 38);
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley REQUIRE(class == 1);
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley isc_buffer_active(source, &sr);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley /* prefix length */
01cc5813ca716d48c46960302d9845ef7c984f93Bob Halley if (sr.length < 1)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley return (DNS_R_UNEXPECTEDEND);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley prefixlen = sr.base[0];
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (prefixlen > 128)
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley return (DNS_R_RANGE);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_region_consume(&sr, 1);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley RETERR(mem_tobuffer(target, &prefixlen, 1));
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley isc_buffer_forward(source, 1);
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley /* suffix */
68d654930939926e4ddbe2b68234dd7c2da97de5Bob Halley if (prefixlen != 128) {
octets = 16 - prefixlen / 8;
if (sr.length < octets)
return (DNS_R_UNEXPECTEDEND);
mask = 0xff >> (prefixlen % 8);
sr.base[0] &= mask; /* ensure pad bits are zero */
RETERR(mem_tobuffer(target, sr.base, octets));
isc_buffer_forward(source, octets);
}
if (prefixlen == 0)
return (DNS_R_SUCCESS);
dns_name_init(&name, NULL);
return (dns_name_fromwire(&name, source, dctx, downcase, target));
}
static dns_result_t
towire_in_a6(dns_rdata_t *rdata, dns_compress_t *cctx, isc_buffer_t *target) {
isc_region_t sr;
dns_name_t name;
unsigned char prefixlen;
unsigned char octets;
REQUIRE(rdata->type == 38);
REQUIRE(rdata->class == 1);
dns_rdata_toregion(rdata, &sr);
prefixlen = sr.base[0];
INSIST(prefixlen <= 128);
octets = 1 + 16 - prefixlen / 8;
RETERR(mem_tobuffer(target, sr.base, octets));
isc_region_consume(&sr, octets);
if (prefixlen == 0)
return (DNS_R_SUCCESS);
dns_name_init(&name, NULL);
dns_name_fromregion(&name, &sr);
return (dns_name_towire(&name, cctx, target));
}
static int
compare_in_a6(dns_rdata_t *rdata1, dns_rdata_t *rdata2) {
int result;
unsigned char prefixlen;
unsigned char octets;
dns_name_t name1;
dns_name_t name2;
isc_region_t region1;
isc_region_t region2;
REQUIRE(rdata1->type == rdata2->type);
REQUIRE(rdata1->class == rdata2->class);
REQUIRE(rdata1->type == 38);
REQUIRE(rdata1->class == 1);
dns_rdata_toregion(rdata1, &region1);
dns_rdata_toregion(rdata2, &region2);
prefixlen = MAX(region1.base[0], region2.base[0]);
octets = 1 + 16 - prefixlen / 8;
result = memcmp(region1.base, region2.base, octets);
if (result != 0)
result = (result < 0) ? -1 : 1;
isc_region_consume(&region1, octets);
isc_region_consume(&region2, octets);
dns_name_init(&name1, NULL);
dns_name_init(&name2, NULL);
dns_name_fromregion(&name1, &region1);
dns_name_fromregion(&name2, &region2);
return (dns_name_compare(&name1, &name2));
}
static dns_result_t
fromstruct_in_a6(dns_rdataclass_t class, dns_rdatatype_t type, void *source,
isc_buffer_t *target) {
REQUIRE(type == 1);
REQUIRE(class == 1);
source = source;
target = target;
return (DNS_R_NOTIMPLEMENTED);
}
static dns_result_t
tostruct_in_a6(dns_rdata_t *rdata, void *target) {
REQUIRE(rdata->type == 38);
REQUIRE(rdata->class == 1);
target = target;
return (DNS_R_NOTIMPLEMENTED);
}
#endif /* RDATA_IN_1_A6_38_H */