rdata.c revision c707e2b9861dfa3f86b3520b9c3630db70cb020c
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
7d32c065c7bb56f281651ae3dd2888f32ce4f1d9Bob Halley * Copyright (C) 2004-2015 Internet Systems Consortium, Inc. ("ISC")
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Copyright (C) 1998-2003 Internet Software Consortium.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Permission to use, copy, modify, and/or distribute this software for any
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * purpose with or without fee is hereby granted, provided that the above
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * copyright notice and this permission notice appear in all copies.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * PERFORMANCE OF THIS SOFTWARE.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
fc8a6561cbb0a1fea849950a3217aacd3b8f1eacDavid Lawrence/* $Id$ */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*! \file */
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <config.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <ctype.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/base64.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/hex.h>
fc8a6561cbb0a1fea849950a3217aacd3b8f1eacDavid Lawrence#include <isc/lex.h>
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson#include <isc/mem.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/parseint.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/print.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/string.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/stdlib.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <isc/util.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/callbacks.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/cert.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/compress.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/dsdigest.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/enumtype.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/keyflags.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/keyvalues.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/message.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rcode.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdata.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdataclass.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdatastruct.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/rdatatype.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/result.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/secalg.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/secproto.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/time.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include <dns/ttl.h>
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define RETERR(x) \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson do { \
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_result_t _r = (x); \
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (_r != ISC_R_SUCCESS) \
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return (_r); \
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson } while (0)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson#define RETTOK(x) \
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson do { \
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_result_t _r = (x); \
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (_r != ISC_R_SUCCESS) { \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_lex_ungettoken(lexer, &token); \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (_r); \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson } \
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson } while (0)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define DNS_AS_STR(t) ((t).value.as_textregion.base)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_FROMTEXT int rdclass, dns_rdatatype_t type, \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_lex_t *lexer, dns_name_t *origin, \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned int options, isc_buffer_t *target, \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdatacallbacks_t *callbacks
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_TOTEXT dns_rdata_t *rdata, dns_rdata_textctx_t *tctx, \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t *target
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_FROMWIRE int rdclass, dns_rdatatype_t type, \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t *source, dns_decompress_t *dctx, \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned int options, isc_buffer_t *target
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_TOWIRE dns_rdata_t *rdata, dns_compress_t *cctx, \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t *target
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_COMPARE const dns_rdata_t *rdata1, const dns_rdata_t *rdata2
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_FROMSTRUCT int rdclass, dns_rdatatype_t type, \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson void *source, isc_buffer_t *target
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson#define ARGS_TOSTRUCT dns_rdata_t *rdata, void *target, isc_mem_t *mctx
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_FREESTRUCT void *source
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_ADDLDATA dns_rdata_t *rdata, dns_additionaldatafunc_t add, \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson void *arg
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_DIGEST dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_CHECKOWNER dns_name_t *name, dns_rdataclass_t rdclass, \
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdatatype_t type, isc_boolean_t wildcard
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define ARGS_CHECKNAMES dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson/*%
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Context structure for the totext_ functions.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * Contains formatting options for rdata-to-text
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * conversion.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssontypedef struct dns_rdata_textctx {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_name_t *origin; /*%< Current origin, or NULL. */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned int flags; /*%< DNS_STYLEFLAG_* */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned int width; /*%< Width of rdata column. */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson const char *linebreak; /*%< Line break string. */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson} dns_rdata_textctx_t;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssontxt_totext(isc_region_t *source, isc_boolean_t quote, isc_buffer_t *target);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssontxt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssontxt_fromwire(isc_buffer_t *source, isc_buffer_t *target);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonmultitxt_totext(isc_region_t *source, isc_buffer_t *target);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonmultitxt_fromtext(isc_textregion_t *source, isc_buffer_t *target);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_boolean_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonname_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic unsigned int
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halleyname_length(dns_name_t *name);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halleystatic isc_result_t
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonstr_totext(const char *source, isc_buffer_t *target);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssoninet_totext(int af, isc_region_t *src, isc_buffer_t *target);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonstatic isc_boolean_t
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonbuffer_empty(isc_buffer_t *source);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonbuffer_fromregion(isc_buffer_t *buffer, isc_region_t *region);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonuint32_tobuffer(isc_uint32_t, isc_buffer_t *target);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonuint16_tobuffer(isc_uint32_t, isc_buffer_t *target);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonuint8_tobuffer(isc_uint32_t, isc_buffer_t *target);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonname_tobuffer(dns_name_t *name, isc_buffer_t *target);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonstatic isc_uint32_t
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafssonuint32_fromregion(isc_region_t *region);
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonstatic isc_uint16_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonuint16_fromregion(isc_region_t *region);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_uint8_t
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonuint8_fromregion(isc_region_t *region);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonstatic isc_uint8_t
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonuint8_consume_fromregion(isc_region_t *region);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonstatic isc_result_t
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halleymem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonstatic int
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonhexvalue(char value);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic int
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondecvalue(char value);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halleystatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonbtoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafssonatob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondefault_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *, ...)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ISC_FORMAT_PRINTF(2, 3);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonstatic void
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonfromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdatacallbacks_t *callbacks, const char *name,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned long line, isc_token_t *token, isc_result_t result);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonfromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonrdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t *target);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halleywarn_badname(dns_name_t *name, isc_lex_t *lexer,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdatacallbacks_t *callbacks);
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic void
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonwarn_badmx(isc_token_t *token, isc_lex_t *lexer,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdatacallbacks_t *callbacks);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_uint16_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonuint16_consume_fromregion(isc_region_t *region);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonunknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t *target);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*% INT16 Size */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define NS_INT16SZ 2
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*% IPv6 Address Size */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define NS_LOCATORSZ 8
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*%
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * convert presentation level address to network order binary form.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * \return
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * 1 if `src' is a valid [RFC1884 2.2] address, else 0.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * \note
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson * (1) does not touch `dst' unless it's returning 1.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic inline int
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonlocator_pton(const char *src, unsigned char *dst) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson static const char xdigits_l[] = "0123456789abcdef",
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson xdigits_u[] = "0123456789ABCDEF";
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned char tmp[NS_LOCATORSZ];
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson unsigned char *tp = tmp, *endp;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson const char *xdigits;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int ch, seen_xdigits;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson unsigned int val;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
5dbf258e2c780189fa30d51a872d8edb372e5589Bob Halley memset(tp, '\0', NS_LOCATORSZ);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson endp = tp + NS_LOCATORSZ;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson seen_xdigits = 0;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson val = 0;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson while ((ch = *src++) != '\0') {
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson const char *pch;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson pch = strchr((xdigits = xdigits_l), ch);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (pch == NULL)
b120e39deac3e9c323cdc7801ed2313f8ebbb744Andreas Gustafsson pch = strchr((xdigits = xdigits_u), ch);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (pch != NULL) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson val <<= 4;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson val |= (pch - xdigits);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (++seen_xdigits > 4)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (0);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson continue;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (ch == ':') {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (!seen_xdigits)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (0);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (tp + NS_INT16SZ > endp)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (0);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *tp++ = (unsigned char) (val >> 8) & 0xff;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *tp++ = (unsigned char) val & 0xff;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson seen_xdigits = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson val = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson continue;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (0);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (seen_xdigits) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (tp + NS_INT16SZ > endp)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (0);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *tp++ = (unsigned char) (val >> 8) & 0xff;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson *tp++ = (unsigned char) val & 0xff;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (tp != endp)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (0);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson memmove(dst, tmp, NS_LOCATORSZ);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (1);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic inline int
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssongetquad(const void *src, struct in_addr *dst,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson int result;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson struct in_addr *tmp;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = inet_aton(src, dst);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (result == 1 && callbacks != NULL &&
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson inet_pton(AF_INET, src, &tmp) != 1) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson const char *name = isc_lex_getsourcename(lexer);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (name == NULL)
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson name = "UNKNOWN";
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson (*callbacks->warn)(callbacks, "%s:%lu: \"%s\" "
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson "is not a decimal dotted quad", name,
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson isc_lex_getsourceline(lexer), src);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson }
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson return (result);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson}
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafssonstatic inline isc_result_t
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafssonname_duporclone(dns_name_t *source, isc_mem_t *mctx, dns_name_t *target) {
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson if (mctx != NULL)
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson return (dns_name_dup(source, mctx, target));
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson dns_name_clone(source, target);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson return (ISC_R_SUCCESS);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic inline void *
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonmem_maybedup(isc_mem_t *mctx, void *source, size_t length) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson void *copy;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (mctx == NULL)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (source);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson copy = isc_mem_allocate(mctx, length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (copy != NULL)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson memmove(copy, source, length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson return (copy);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic const char hexdigits[] = "0123456789abcdef";
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonstatic const char decdigits[] = "0123456789";
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#include "code.h"
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define META 0x0001
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson#define RESERVED 0x0002
f80ea74e1984e0b1dbe48dd86ecdd3a2380393cbBob Halley
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson/***
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *** Initialization
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ***/
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssonvoid
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssondns_rdata_init(dns_rdata_t *rdata) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(rdata != NULL);
e485d16462b0a66b835107fae04fe9d562e77af0Bob Halley
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson rdata->data = NULL;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson rdata->length = 0;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson rdata->rdclass = 0;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson rdata->type = 0;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson rdata->flags = 0;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson ISC_LINK_INIT(rdata, link);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson /* ISC_LIST_INIT(rdata->list); */
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson}
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssonvoid
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafssondns_rdata_reset(dns_rdata_t *rdata) {
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson REQUIRE(rdata != NULL);
8f79820c6930ee5ef6b4a54f36d2559400bdf47dAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(!ISC_LINK_LINKED(rdata, link));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson rdata->data = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson rdata->length = 0;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson rdata->rdclass = 0;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson rdata->type = 0;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson rdata->flags = 0;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson/***
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ***
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ***/
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonvoid
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssondns_rdata_clone(const dns_rdata_t *src, dns_rdata_t *target) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson REQUIRE(src != NULL);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson REQUIRE(target != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(DNS_RDATA_INITIALIZED(target));
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(src));
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(target));
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson target->data = src->data;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson target->length = src->length;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson target->rdclass = src->rdclass;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson target->type = src->type;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson target->flags = src->flags;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson/***
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson *** Comparisons
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ***/
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonint
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssondns_rdata_compare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson int result = 0;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_boolean_t use_default = ISC_FALSE;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(rdata1 != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(rdata2 != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(rdata1->length == 0 || rdata1->data != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(rdata2->length == 0 || rdata2->data != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (rdata1->rdclass != rdata2->rdclass)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (rdata1->type != rdata2->type)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return (rdata1->type < rdata2->type ? -1 : 1);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson COMPARESWITCH
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (use_default) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_region_t r1;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_region_t r2;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdata_toregion(rdata1, &r1);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_rdata_toregion(rdata2, &r2);
a243aeb92b5937c22d578deb0f98d3501bf86f4aAndreas Gustafsson result = isc_region_compare(&r1, &r2);
a243aeb92b5937c22d578deb0f98d3501bf86f4aAndreas Gustafsson }
a243aeb92b5937c22d578deb0f98d3501bf86f4aAndreas Gustafsson return (result);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson}
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonint
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssondns_rdata_casecompare(const dns_rdata_t *rdata1, const dns_rdata_t *rdata2) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson int result = 0;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_boolean_t use_default = ISC_FALSE;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(rdata1 != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(rdata2 != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(rdata1->length == 0 || rdata1->data != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(rdata2->length == 0 || rdata2->data != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(rdata1));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(rdata2));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (rdata1->rdclass != rdata2->rdclass)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return (rdata1->rdclass < rdata2->rdclass ? -1 : 1);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (rdata1->type != rdata2->type)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return (rdata1->type < rdata2->type ? -1 : 1);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson CASECOMPARESWITCH
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (use_default) {
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_region_t r1;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson isc_region_t r2;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_rdata_toregion(rdata1, &r1);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_rdata_toregion(rdata2, &r2);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = isc_region_compare(&r1, &r2);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson return (result);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff}
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson/***
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson *** Conversions
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson ***/
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonvoid
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssondns_rdata_fromregion(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson dns_rdatatype_t type, isc_region_t *r)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson{
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(rdata != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(DNS_RDATA_INITIALIZED(rdata));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(r != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson rdata->data = r->base;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson rdata->length = r->length;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson rdata->rdclass = rdclass;
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff rdata->type = type;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson rdata->flags = 0;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson}
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graffvoid
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssondns_rdata_toregion(const dns_rdata_t *rdata, isc_region_t *r) {
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(rdata != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(r != NULL);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson r->base = rdata->data;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson r->length = rdata->length;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_result_t
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssondns_rdata_fromwire(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_rdatatype_t type, isc_buffer_t *source,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson dns_decompress_t *dctx, unsigned int options,
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_buffer_t *target)
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson{
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_result_t result = ISC_R_NOTIMPLEMENTED;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_region_t region;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t ss;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t st;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_boolean_t use_default = ISC_FALSE;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson isc_uint32_t activelength;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson unsigned int length;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson REQUIRE(dctx != NULL);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson if (rdata != NULL) {
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson REQUIRE(DNS_RDATA_INITIALIZED(rdata));
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(source != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson REQUIRE(target != NULL);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (type == 0)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson return (DNS_R_FORMERR);
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson ss = *source;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson st = *target;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson activelength = isc_buffer_activelength(source);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson INSIST(activelength < 65536);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson FROMWIRESWITCH
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (use_default) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (activelength > isc_buffer_availablelength(target))
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = ISC_R_NOSPACE;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson else {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_buffer_putmem(target, isc_buffer_current(source),
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson activelength);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_forward(source, activelength);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = ISC_R_SUCCESS;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson }
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson /*
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * Reject any rdata that expands out to more than DNS_RDATA_MAXLENGTH
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * as we cannot transmit it.
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson result = DNS_R_FORMERR;
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson /*
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson * We should have consumed all of our buffer.
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (result == ISC_R_SUCCESS && !buffer_empty(source))
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson result = DNS_R_EXTRADATA;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (rdata != NULL && result == ISC_R_SUCCESS) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson region.base = isc_buffer_used(&st);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson region.length = length;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson dns_rdata_fromregion(rdata, rdclass, type, &region);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (result != ISC_R_SUCCESS) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson *source = ss;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson *target = st;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson return (result);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson}
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonisc_result_t
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafssondns_rdata_towire(dns_rdata_t *rdata, dns_compress_t *cctx,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t *target)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson{
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_result_t result = ISC_R_NOTIMPLEMENTED;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_boolean_t use_default = ISC_FALSE;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_region_t tr;
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_buffer_t st;
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson REQUIRE(rdata != NULL);
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson /*
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson * Some DynDNS meta-RRs have empty rdata.
7618f0551eb745354ee695907e568b0be1f2c8f5Andreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson INSIST(rdata->length == 0);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (ISC_R_SUCCESS);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson st = *target;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson TOWIRESWITCH
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson if (use_default) {
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson isc_buffer_availableregion(target, &tr);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (tr.length < rdata->length)
9b0e18da3d5c2290f90b285d122d368173f17c63Andreas Gustafsson return (ISC_R_NOSPACE);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson memmove(tr.base, rdata->data, rdata->length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_add(target, rdata->length);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson return (ISC_R_SUCCESS);
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson }
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson if (result != ISC_R_SUCCESS) {
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson *target = st;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson INSIST(target->used < 65536);
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff dns_compress_rollback(cctx, (isc_uint16_t)target->used);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson }
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff return (result);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson}
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson/*
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson * If the binary data in 'src' is valid uncompressed wire format
e44487bfc23599b6b240e09d83d1c862fecfcc82Michael Graff * rdata of class 'rdclass' and type 'type', return ISC_R_SUCCESS
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson * and copy the validated rdata to 'dest'. Otherwise return an error.
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson */
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafssonstatic isc_result_t
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafssonrdata_validate(isc_buffer_t *src, isc_buffer_t *dest, dns_rdataclass_t rdclass,
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson dns_rdatatype_t type)
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson{
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson dns_decompress_t dctx;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson isc_result_t result;
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson dns_decompress_init(&dctx, -1, DNS_DECOMPRESS_NONE);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson isc_buffer_setactive(src, isc_buffer_usedlength(src));
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson result = dns_rdata_fromwire(NULL, rdclass, type, src, &dctx, 0, dest);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson dns_decompress_invalidate(&dctx);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson return (result);
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson}
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafsson
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonstatic isc_result_t
a928d619170d61da40c3bff29800ff73709140daAndreas Gustafssonunknown_fromtext(dns_rdataclass_t rdclass, dns_rdatatype_t type,
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_lex_t *lexer, isc_mem_t *mctx, isc_buffer_t *target)
ed04318ef686581fc9a20965a5be02abfb4f1bd5Andreas Gustafsson{
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_result_t result;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_buffer_t *buf = NULL;
f951f076f3d321c52b824a866caff28ce4f8e06cAndreas Gustafsson isc_token_t token;
if (type == 0 || dns_rdatatype_ismeta(type))
return (DNS_R_METATYPE);
RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
ISC_FALSE));
if (token.value.as_ulong > 65535U)
return (ISC_R_RANGE);
result = isc_buffer_allocate(mctx, &buf, token.value.as_ulong);
if (result != ISC_R_SUCCESS)
return (result);
result = isc_hex_tobuffer(lexer, buf,
(unsigned int)token.value.as_ulong);
if (result != ISC_R_SUCCESS)
goto failure;
if (isc_buffer_usedlength(buf) != token.value.as_ulong) {
result = ISC_R_UNEXPECTEDEND;
goto failure;
}
if (dns_rdatatype_isknown(type)) {
result = rdata_validate(buf, target, rdclass, type);
} else {
isc_region_t r;
isc_buffer_usedregion(buf, &r);
result = isc_buffer_copyregion(target, &r);
}
if (result != ISC_R_SUCCESS)
goto failure;
isc_buffer_free(&buf);
return (ISC_R_SUCCESS);
failure:
isc_buffer_free(&buf);
return (result);
}
isc_result_t
dns_rdata_fromtext(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
dns_rdatatype_t type, isc_lex_t *lexer,
dns_name_t *origin, unsigned int options, isc_mem_t *mctx,
isc_buffer_t *target, dns_rdatacallbacks_t *callbacks)
{
isc_result_t result = ISC_R_NOTIMPLEMENTED;
isc_region_t region;
isc_buffer_t st;
isc_token_t token;
unsigned int lexoptions = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
char *name;
unsigned long line;
void (*callback)(dns_rdatacallbacks_t *, const char *, ...);
isc_result_t tresult;
unsigned int length;
isc_boolean_t unknown;
REQUIRE(origin == NULL || dns_name_isabsolute(origin) == ISC_TRUE);
if (rdata != NULL) {
REQUIRE(DNS_RDATA_INITIALIZED(rdata));
REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
}
if (callbacks != NULL) {
REQUIRE(callbacks->warn != NULL);
REQUIRE(callbacks->error != NULL);
}
st = *target;
if (callbacks != NULL)
callback = callbacks->error;
else
callback = default_fromtext_callback;
result = isc_lex_getmastertoken(lexer, &token, isc_tokentype_qstring,
ISC_FALSE);
if (result != ISC_R_SUCCESS) {
name = isc_lex_getsourcename(lexer);
line = isc_lex_getsourceline(lexer);
fromtext_error(callback, callbacks, name, line, NULL, result);
return (result);
}
unknown = ISC_FALSE;
if (token.type == isc_tokentype_string &&
strcmp(DNS_AS_STR(token), "\\#") == 0) {
/*
* If this is a TXT record '\#' could be a escaped '#'.
* Look to see if the next token is a number and if so
* treat it as a unknown record format.
*/
if (type == dns_rdatatype_txt) {
result = isc_lex_getmastertoken(lexer, &token,
isc_tokentype_number,
ISC_FALSE);
if (result == ISC_R_SUCCESS)
isc_lex_ungettoken(lexer, &token);
}
if (result == ISC_R_SUCCESS) {
unknown = ISC_TRUE;
result = unknown_fromtext(rdclass, type, lexer,
mctx, target);
} else
options |= DNS_RDATA_UNKNOWNESCAPE;
} else
isc_lex_ungettoken(lexer, &token);
if (!unknown)
FROMTEXTSWITCH
/*
* Consume to end of line / file.
* If not at end of line initially set error code.
* Call callback via fromtext_error once if there was an error.
*/
do {
name = isc_lex_getsourcename(lexer);
line = isc_lex_getsourceline(lexer);
tresult = isc_lex_gettoken(lexer, lexoptions, &token);
if (tresult != ISC_R_SUCCESS) {
if (result == ISC_R_SUCCESS)
result = tresult;
if (callback != NULL)
fromtext_error(callback, callbacks, name,
line, NULL, result);
break;
} else if (token.type != isc_tokentype_eol &&
token.type != isc_tokentype_eof) {
if (result == ISC_R_SUCCESS)
result = DNS_R_EXTRATOKEN;
if (callback != NULL) {
fromtext_error(callback, callbacks, name,
line, &token, result);
callback = NULL;
}
} else if (result != ISC_R_SUCCESS && callback != NULL) {
fromtext_error(callback, callbacks, name, line,
&token, result);
break;
} else {
if (token.type == isc_tokentype_eof)
fromtext_warneof(lexer, callbacks);
break;
}
} while (1);
length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
result = ISC_R_NOSPACE;
if (rdata != NULL && result == ISC_R_SUCCESS) {
region.base = isc_buffer_used(&st);
region.length = length;
dns_rdata_fromregion(rdata, rdclass, type, &region);
}
if (result != ISC_R_SUCCESS) {
*target = st;
}
return (result);
}
static isc_result_t
unknown_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
isc_buffer_t *target)
{
isc_result_t result;
char buf[sizeof("65535")];
isc_region_t sr;
strlcpy(buf, "\\# ", sizeof(buf));
result = str_totext(buf, target);
if (result != ISC_R_SUCCESS)
return (result);
dns_rdata_toregion(rdata, &sr);
INSIST(sr.length < 65536);
snprintf(buf, sizeof(buf), "%u", sr.length);
result = str_totext(buf, target);
if (result != ISC_R_SUCCESS)
return (result);
if (sr.length != 0U) {
if ((tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
result = str_totext(" ( ", target);
else
result = str_totext(" ", target);
if (result != ISC_R_SUCCESS)
return (result);
if (tctx->width == 0) /* No splitting */
result = isc_hex_totext(&sr, 0, "", target);
else
result = isc_hex_totext(&sr, tctx->width - 2,
tctx->linebreak,
target);
if (result == ISC_R_SUCCESS &&
(tctx->flags & DNS_STYLEFLAG_MULTILINE) != 0)
result = str_totext(" )", target);
}
return (result);
}
static isc_result_t
rdata_totext(dns_rdata_t *rdata, dns_rdata_textctx_t *tctx,
isc_buffer_t *target)
{
isc_result_t result = ISC_R_NOTIMPLEMENTED;
isc_boolean_t use_default = ISC_FALSE;
REQUIRE(rdata != NULL);
REQUIRE(tctx->origin == NULL ||
dns_name_isabsolute(tctx->origin) == ISC_TRUE);
/*
* Some DynDNS meta-RRs have empty rdata.
*/
if ((rdata->flags & DNS_RDATA_UPDATE) != 0) {
INSIST(rdata->length == 0);
return (ISC_R_SUCCESS);
}
TOTEXTSWITCH
if (use_default)
result = unknown_totext(rdata, tctx, target);
return (result);
}
isc_result_t
dns_rdata_totext(dns_rdata_t *rdata, dns_name_t *origin, isc_buffer_t *target)
{
dns_rdata_textctx_t tctx;
REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
/*
* Set up formatting options for single-line output.
*/
tctx.origin = origin;
tctx.flags = 0;
tctx.width = 60;
tctx.linebreak = " ";
return (rdata_totext(rdata, &tctx, target));
}
isc_result_t
dns_rdata_tofmttext(dns_rdata_t *rdata, dns_name_t *origin,
unsigned int flags, unsigned int width,
unsigned int split_width, const char *linebreak,
isc_buffer_t *target)
{
dns_rdata_textctx_t tctx;
REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
/*
* Set up formatting options for formatted output.
*/
tctx.origin = origin;
tctx.flags = flags;
if (split_width == 0xffffffff)
tctx.width = width;
else
tctx.width = split_width;
if ((flags & DNS_STYLEFLAG_MULTILINE) != 0)
tctx.linebreak = linebreak;
else {
if (split_width == 0xffffffff)
tctx.width = 60; /* Used for hex word length only. */
tctx.linebreak = " ";
}
return (rdata_totext(rdata, &tctx, target));
}
isc_result_t
dns_rdata_fromstruct(dns_rdata_t *rdata, dns_rdataclass_t rdclass,
dns_rdatatype_t type, void *source,
isc_buffer_t *target)
{
isc_result_t result = ISC_R_NOTIMPLEMENTED;
isc_buffer_t st;
isc_region_t region;
isc_boolean_t use_default = ISC_FALSE;
unsigned int length;
REQUIRE(source != NULL);
if (rdata != NULL) {
REQUIRE(DNS_RDATA_INITIALIZED(rdata));
REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
}
st = *target;
FROMSTRUCTSWITCH
if (use_default)
(void)NULL;
length = isc_buffer_usedlength(target) - isc_buffer_usedlength(&st);
if (result == ISC_R_SUCCESS && length > DNS_RDATA_MAXLENGTH)
result = ISC_R_NOSPACE;
if (rdata != NULL && result == ISC_R_SUCCESS) {
region.base = isc_buffer_used(&st);
region.length = length;
dns_rdata_fromregion(rdata, rdclass, type, &region);
}
if (result != ISC_R_SUCCESS)
*target = st;
return (result);
}
isc_result_t
dns_rdata_tostruct(dns_rdata_t *rdata, void *target, isc_mem_t *mctx) {
isc_result_t result = ISC_R_NOTIMPLEMENTED;
isc_boolean_t use_default = ISC_FALSE;
REQUIRE(rdata != NULL);
REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
TOSTRUCTSWITCH
if (use_default)
(void)NULL;
return (result);
}
void
dns_rdata_freestruct(void *source) {
dns_rdatacommon_t *common = source;
REQUIRE(source != NULL);
FREESTRUCTSWITCH
}
isc_result_t
dns_rdata_additionaldata(dns_rdata_t *rdata, dns_additionaldatafunc_t add,
void *arg)
{
isc_result_t result = ISC_R_NOTIMPLEMENTED;
isc_boolean_t use_default = ISC_FALSE;
/*
* Call 'add' for each name and type from 'rdata' which is subject to
* additional section processing.
*/
REQUIRE(rdata != NULL);
REQUIRE(add != NULL);
REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
ADDITIONALDATASWITCH
/* No additional processing for unknown types */
if (use_default)
result = ISC_R_SUCCESS;
return (result);
}
isc_result_t
dns_rdata_digest(dns_rdata_t *rdata, dns_digestfunc_t digest, void *arg) {
isc_result_t result = ISC_R_NOTIMPLEMENTED;
isc_boolean_t use_default = ISC_FALSE;
isc_region_t r;
/*
* Send 'rdata' in DNSSEC canonical form to 'digest'.
*/
REQUIRE(rdata != NULL);
REQUIRE(digest != NULL);
REQUIRE(DNS_RDATA_VALIDFLAGS(rdata));
DIGESTSWITCH
if (use_default) {
dns_rdata_toregion(rdata, &r);
result = (digest)(arg, &r);
}
return (result);
}
isc_boolean_t
dns_rdata_checkowner(dns_name_t *name, dns_rdataclass_t rdclass,
dns_rdatatype_t type, isc_boolean_t wildcard)
{
isc_boolean_t result;
CHECKOWNERSWITCH
return (result);
}
isc_boolean_t
dns_rdata_checknames(dns_rdata_t *rdata, dns_name_t *owner, dns_name_t *bad)
{
isc_boolean_t result;
CHECKNAMESSWITCH
return (result);
}
unsigned int
dns_rdatatype_attributes(dns_rdatatype_t type)
{
RDATATYPE_ATTRIBUTE_SW
return (DNS_RDATATYPEATTR_UNKNOWN);
}
isc_result_t
dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) {
unsigned int hash;
unsigned int n;
unsigned char a, b;
n = source->length;
if (n == 0)
return (DNS_R_UNKNOWN);
a = tolower((unsigned char)source->base[0]);
b = tolower((unsigned char)source->base[n - 1]);
hash = ((a + n) * b) % 256;
/*
* This switch block is inlined via \#define, and will use "return"
* to return a result to the caller if it is a valid (known)
* rdatatype name.
*/
RDATATYPE_FROMTEXT_SW(hash, source->base, n, typep);
if (source->length > 4 && source->length < (4 + sizeof("65000")) &&
strncasecmp("type", source->base, 4) == 0) {
char buf[sizeof("65000")];
char *endp;
unsigned int val;
strncpy(buf, source->base + 4, source->length - 4);
buf[source->length - 4] = '\0';
val = strtoul(buf, &endp, 10);
if (*endp == '\0' && val <= 0xffff) {
*typep = (dns_rdatatype_t)val;
return (ISC_R_SUCCESS);
}
}
return (DNS_R_UNKNOWN);
}
isc_result_t
dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) {
char buf[sizeof("TYPE65535")];
RDATATYPE_TOTEXT_SW
snprintf(buf, sizeof(buf), "TYPE%u", type);
return (str_totext(buf, target));
}
void
dns_rdatatype_format(dns_rdatatype_t rdtype,
char *array, unsigned int size)
{
isc_result_t result;
isc_buffer_t buf;
if (size == 0U)
return;
isc_buffer_init(&buf, array, size);
result = dns_rdatatype_totext(rdtype, &buf);
/*
* Null terminate.
*/
if (result == ISC_R_SUCCESS) {
if (isc_buffer_availablelength(&buf) >= 1)
isc_buffer_putuint8(&buf, 0);
else
result = ISC_R_NOSPACE;
}
if (result != ISC_R_SUCCESS)
strlcpy(array, "<unknown>", size);
}
/*
* Private function.
*/
static unsigned int
name_length(dns_name_t *name) {
return (name->length);
}
static isc_result_t
txt_totext(isc_region_t *source, isc_boolean_t quote, isc_buffer_t *target) {
unsigned int tl;
unsigned int n;
unsigned char *sp;
char *tp;
isc_region_t region;
isc_buffer_availableregion(target, &region);
sp = source->base;
tp = (char *)region.base;
tl = region.length;
n = *sp++;
REQUIRE(n + 1 <= source->length);
if (n == 0U)
REQUIRE(quote == ISC_TRUE);
if (quote) {
if (tl < 1)
return (ISC_R_NOSPACE);
*tp++ = '"';
tl--;
}
while (n--) {
/*
* \DDD space (0x20) if not quoting.
*/
if (*sp < (quote ? 0x20 : 0x21) || *sp >= 0x7f) {
if (tl < 4)
return (ISC_R_NOSPACE);
*tp++ = 0x5c;
*tp++ = 0x30 + ((*sp / 100) % 10);
*tp++ = 0x30 + ((*sp / 10) % 10);
*tp++ = 0x30 + (*sp % 10);
sp++;
tl -= 4;
continue;
}
/*
* Escape double quote and backslash. If we are not
* enclosing the string in double quotes also escape
* at sign and semicolon.
*/
if (*sp == 0x22 || *sp == 0x5c ||
(!quote && (*sp == 0x40 || *sp == 0x3b))) {
if (tl < 2)
return (ISC_R_NOSPACE);
*tp++ = '\\';
tl--;
}
if (tl < 1)
return (ISC_R_NOSPACE);
*tp++ = *sp++;
tl--;
}
if (quote) {
if (tl < 1)
return (ISC_R_NOSPACE);
*tp++ = '"';
tl--;
}
isc_buffer_add(target, (unsigned int)(tp - (char *)region.base));
isc_region_consume(source, *source->base + 1);
return (ISC_R_SUCCESS);
}
static isc_result_t
txt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
isc_region_t tregion;
isc_boolean_t escape;
unsigned int n, nrem;
char *s;
unsigned char *t;
int d;
int c;
isc_buffer_availableregion(target, &tregion);
s = source->base;
n = source->length;
t = tregion.base;
nrem = tregion.length;
escape = ISC_FALSE;
if (nrem < 1)
return (ISC_R_NOSPACE);
/*
* Length byte.
*/
nrem--;
t++;
/*
* Maximum text string length.
*/
if (nrem > 255)
nrem = 255;
while (n-- != 0) {
c = (*s++) & 0xff;
if (escape && (d = decvalue((char)c)) != -1) {
c = d;
if (n == 0)
return (DNS_R_SYNTAX);
n--;
if ((d = decvalue(*s++)) != -1)
c = c * 10 + d;
else
return (DNS_R_SYNTAX);
if (n == 0)
return (DNS_R_SYNTAX);
n--;
if ((d = decvalue(*s++)) != -1)
c = c * 10 + d;
else
return (DNS_R_SYNTAX);
if (c > 255)
return (DNS_R_SYNTAX);
} else if (!escape && c == '\\') {
escape = ISC_TRUE;
continue;
}
escape = ISC_FALSE;
if (nrem == 0)
return ((tregion.length <= 256U) ?
ISC_R_NOSPACE : DNS_R_SYNTAX);
*t++ = c;
nrem--;
}
if (escape)
return (DNS_R_SYNTAX);
*tregion.base = (unsigned char)(t - tregion.base - 1);
isc_buffer_add(target, *tregion.base + 1);
return (ISC_R_SUCCESS);
}
static isc_result_t
txt_fromwire(isc_buffer_t *source, isc_buffer_t *target) {
unsigned int n;
isc_region_t sregion;
isc_region_t tregion;
isc_buffer_activeregion(source, &sregion);
if (sregion.length == 0)
return (ISC_R_UNEXPECTEDEND);
n = *sregion.base + 1;
if (n > sregion.length)
return (ISC_R_UNEXPECTEDEND);
isc_buffer_availableregion(target, &tregion);
if (n > tregion.length)
return (ISC_R_NOSPACE);
if (tregion.base != sregion.base)
memmove(tregion.base, sregion.base, n);
isc_buffer_forward(source, n);
isc_buffer_add(target, n);
return (ISC_R_SUCCESS);
}
/*
* Conversion of TXT-like rdata fields without length limits.
*/
static isc_result_t
multitxt_totext(isc_region_t *source, isc_buffer_t *target) {
unsigned int tl;
unsigned int n0, n;
unsigned char *sp;
char *tp;
isc_region_t region;
isc_buffer_availableregion(target, &region);
sp = source->base;
tp = (char *)region.base;
tl = region.length;
if (tl < 1)
return (ISC_R_NOSPACE);
*tp++ = '"';
tl--;
do {
n = source->length;
n0 = source->length - 1;
while (n--) {
if (*sp < 0x20 || *sp >= 0x7f) {
if (tl < 4)
return (ISC_R_NOSPACE);
*tp++ = 0x5c;
*tp++ = 0x30 + ((*sp / 100) % 10);
*tp++ = 0x30 + ((*sp / 10) % 10);
*tp++ = 0x30 + (*sp % 10);
sp++;
tl -= 4;
continue;
}
/* double quote, semi-colon, backslash */
if (*sp == 0x22 || *sp == 0x3b || *sp == 0x5c) {
if (tl < 2)
return (ISC_R_NOSPACE);
*tp++ = '\\';
tl--;
}
if (tl < 1)
return (ISC_R_NOSPACE);
*tp++ = *sp++;
tl--;
}
isc_region_consume(source, n0 + 1);
} while (source->length != 0);
if (tl < 1)
return (ISC_R_NOSPACE);
*tp++ = '"';
tl--;
isc_buffer_add(target, (unsigned int)(tp - (char *)region.base));
return (ISC_R_SUCCESS);
}
static isc_result_t
multitxt_fromtext(isc_textregion_t *source, isc_buffer_t *target) {
isc_region_t tregion;
isc_boolean_t escape;
unsigned int n, nrem;
char *s;
unsigned char *t0, *t;
int d;
int c;
s = source->base;
n = source->length;
escape = ISC_FALSE;
do {
isc_buffer_availableregion(target, &tregion);
t0 = t = tregion.base;
nrem = tregion.length;
if (nrem < 1)
return (ISC_R_NOSPACE);
while (n != 0) {
--n;
c = (*s++) & 0xff;
if (escape && (d = decvalue((char)c)) != -1) {
c = d;
if (n == 0)
return (DNS_R_SYNTAX);
n--;
if ((d = decvalue(*s++)) != -1)
c = c * 10 + d;
else
return (DNS_R_SYNTAX);
if (n == 0)
return (DNS_R_SYNTAX);
n--;
if ((d = decvalue(*s++)) != -1)
c = c * 10 + d;
else
return (DNS_R_SYNTAX);
if (c > 255)
return (DNS_R_SYNTAX);
} else if (!escape && c == '\\') {
escape = ISC_TRUE;
continue;
}
escape = ISC_FALSE;
*t++ = c;
nrem--;
if (nrem == 0)
break;
}
if (escape)
return (DNS_R_SYNTAX);
isc_buffer_add(target, (unsigned int)(t - t0));
} while (n != 0);
return (ISC_R_SUCCESS);
}
static isc_boolean_t
name_prefix(dns_name_t *name, dns_name_t *origin, dns_name_t *target) {
int l1, l2;
if (origin == NULL)
goto return_false;
if (dns_name_compare(origin, dns_rootname) == 0)
goto return_false;
if (!dns_name_issubdomain(name, origin))
goto return_false;
l1 = dns_name_countlabels(name);
l2 = dns_name_countlabels(origin);
if (l1 == l2)
goto return_false;
/* Master files should be case preserving. */
dns_name_getlabelsequence(name, l1 - l2, l2, target);
if (!dns_name_caseequal(origin, target))
goto return_false;
dns_name_getlabelsequence(name, 0, l1 - l2, target);
return (ISC_TRUE);
return_false:
*target = *name;
return (ISC_FALSE);
}
static isc_result_t
str_totext(const char *source, isc_buffer_t *target) {
unsigned int l;
isc_region_t region;
isc_buffer_availableregion(target, &region);
l = strlen(source);
if (l > region.length)
return (ISC_R_NOSPACE);
memmove(region.base, source, l);
isc_buffer_add(target, l);
return (ISC_R_SUCCESS);
}
static isc_result_t
inet_totext(int af, isc_region_t *src, isc_buffer_t *target) {
char tmpbuf[64];
/* Note - inet_ntop doesn't do size checking on its input. */
if (inet_ntop(af, src->base, tmpbuf, sizeof(tmpbuf)) == NULL)
return (ISC_R_NOSPACE);
if (strlen(tmpbuf) > isc_buffer_availablelength(target))
return (ISC_R_NOSPACE);
isc_buffer_putstr(target, tmpbuf);
return (ISC_R_SUCCESS);
}
static isc_boolean_t
buffer_empty(isc_buffer_t *source) {
return((source->current == source->active) ? ISC_TRUE : ISC_FALSE);
}
static void
buffer_fromregion(isc_buffer_t *buffer, isc_region_t *region) {
isc_buffer_init(buffer, region->base, region->length);
isc_buffer_add(buffer, region->length);
isc_buffer_setactive(buffer, region->length);
}
static isc_result_t
uint32_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
isc_region_t region;
isc_buffer_availableregion(target, &region);
if (region.length < 4)
return (ISC_R_NOSPACE);
isc_buffer_putuint32(target, value);
return (ISC_R_SUCCESS);
}
static isc_result_t
uint16_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
isc_region_t region;
if (value > 0xffff)
return (ISC_R_RANGE);
isc_buffer_availableregion(target, &region);
if (region.length < 2)
return (ISC_R_NOSPACE);
isc_buffer_putuint16(target, (isc_uint16_t)value);
return (ISC_R_SUCCESS);
}
static isc_result_t
uint8_tobuffer(isc_uint32_t value, isc_buffer_t *target) {
isc_region_t region;
if (value > 0xff)
return (ISC_R_RANGE);
isc_buffer_availableregion(target, &region);
if (region.length < 1)
return (ISC_R_NOSPACE);
isc_buffer_putuint8(target, (isc_uint8_t)value);
return (ISC_R_SUCCESS);
}
static isc_result_t
name_tobuffer(dns_name_t *name, isc_buffer_t *target) {
isc_region_t r;
dns_name_toregion(name, &r);
return (isc_buffer_copyregion(target, &r));
}
static isc_uint32_t
uint32_fromregion(isc_region_t *region) {
isc_uint32_t value;
REQUIRE(region->length >= 4);
value = region->base[0] << 24;
value |= region->base[1] << 16;
value |= region->base[2] << 8;
value |= region->base[3];
return(value);
}
static isc_uint16_t
uint16_consume_fromregion(isc_region_t *region) {
isc_uint16_t r = uint16_fromregion(region);
isc_region_consume(region, 2);
return r;
}
static isc_uint16_t
uint16_fromregion(isc_region_t *region) {
REQUIRE(region->length >= 2);
return ((region->base[0] << 8) | region->base[1]);
}
static isc_uint8_t
uint8_fromregion(isc_region_t *region) {
REQUIRE(region->length >= 1);
return (region->base[0]);
}
static isc_uint8_t
uint8_consume_fromregion(isc_region_t *region) {
isc_uint8_t r = uint8_fromregion(region);
isc_region_consume(region, 1);
return r;
}
static isc_result_t
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
isc_region_t tr;
isc_buffer_availableregion(target, &tr);
if (length > tr.length)
return (ISC_R_NOSPACE);
if (tr.base != base)
memmove(tr.base, base, length);
isc_buffer_add(target, length);
return (ISC_R_SUCCESS);
}
static int
hexvalue(char value) {
const char *s;
unsigned char c;
c = (unsigned char)value;
if (!isascii(c))
return (-1);
if (isupper(c))
c = tolower(c);
if ((s = strchr(hexdigits, c)) == NULL)
return (-1);
return (int)(s - hexdigits);
}
static int
decvalue(char value) {
const char *s;
/*
* isascii() is valid for full range of int values, no need to
* mask or cast.
*/
if (!isascii(value))
return (-1);
if ((s = strchr(decdigits, value)) == NULL)
return (-1);
return (int)(s - decdigits);
}
static const char atob_digits[86] =
"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`" \
"abcdefghijklmnopqrstu";
/*
* Subroutines to convert between 8 bit binary bytes and printable ASCII.
* Computes the number of bytes, and three kinds of simple checksums.
* Incoming bytes are collected into 32-bit words, then printed in base 85:
* exp(85,5) > exp(2,32)
* The ASCII characters used are between '!' and 'u';
* 'z' encodes 32-bit zero; 'x' is used to mark the end of encoded data.
*
* Originally by Paul Rutter (philabs!per) and Joe Orost (petsd!joe) for
* the atob/btoa programs, released with the compress program, in mod.sources.
* Modified by Mike Schwartz 8/19/86 for use in BIND.
* Modified to be re-entrant 3/2/99.
*/
struct state {
isc_int32_t Ceor;
isc_int32_t Csum;
isc_int32_t Crot;
isc_int32_t word;
isc_int32_t bcount;
};
#define Ceor state->Ceor
#define Csum state->Csum
#define Crot state->Crot
#define word state->word
#define bcount state->bcount
#define times85(x) ((((((x<<2)+x)<<2)+x)<<2)+x)
static isc_result_t byte_atob(int c, isc_buffer_t *target,
struct state *state);
static isc_result_t putbyte(int c, isc_buffer_t *, struct state *state);
static isc_result_t byte_btoa(int c, isc_buffer_t *, struct state *state);
/*
* Decode ASCII-encoded byte c into binary representation and
* place into *bufp, advancing bufp.
*/
static isc_result_t
byte_atob(int c, isc_buffer_t *target, struct state *state) {
const char *s;
if (c == 'z') {
if (bcount != 0)
return(DNS_R_SYNTAX);
else {
RETERR(putbyte(0, target, state));
RETERR(putbyte(0, target, state));
RETERR(putbyte(0, target, state));
RETERR(putbyte(0, target, state));
}
} else if ((s = strchr(atob_digits, c)) != NULL) {
if (bcount == 0) {
word = (isc_int32_t)(s - atob_digits);
++bcount;
} else if (bcount < 4) {
word = times85(word);
word += (isc_int32_t)(s - atob_digits);
++bcount;
} else {
word = times85(word);
word += (isc_int32_t)(s - atob_digits);
RETERR(putbyte((word >> 24) & 0xff, target, state));
RETERR(putbyte((word >> 16) & 0xff, target, state));
RETERR(putbyte((word >> 8) & 0xff, target, state));
RETERR(putbyte(word & 0xff, target, state));
word = 0;
bcount = 0;
}
} else
return(DNS_R_SYNTAX);
return(ISC_R_SUCCESS);
}
/*
* Compute checksum info and place c into target.
*/
static isc_result_t
putbyte(int c, isc_buffer_t *target, struct state *state) {
isc_region_t tr;
Ceor ^= c;
Csum += c;
Csum += 1;
if ((Crot & 0x80000000)) {
Crot <<= 1;
Crot += 1;
} else {
Crot <<= 1;
}
Crot += c;
isc_buffer_availableregion(target, &tr);
if (tr.length < 1)
return (ISC_R_NOSPACE);
tr.base[0] = c;
isc_buffer_add(target, 1);
return (ISC_R_SUCCESS);
}
/*
* Read the ASCII-encoded data from inbuf, of length inbuflen, and convert
* it into T_UNSPEC (binary data) in outbuf, not to exceed outbuflen bytes;
* outbuflen must be divisible by 4. (Note: this is because outbuf is filled
* in 4 bytes at a time. If the actual data doesn't end on an even 4-byte
* boundary, there will be no problem...it will be padded with 0 bytes, and
* numbytes will indicate the correct number of bytes. The main point is
* that since the buffer is filled in 4 bytes at a time, even if there is
* not a full 4 bytes of data at the end, there has to be room to 0-pad the
* data, so the buffer must be of size divisible by 4). Place the number of
* output bytes in numbytes, and return a failure/success status.
*/
static isc_result_t
atob_tobuffer(isc_lex_t *lexer, isc_buffer_t *target) {
long oeor, osum, orot;
struct state statebuf, *state= &statebuf;
isc_token_t token;
char c;
char *e;
Ceor = Csum = Crot = word = bcount = 0;
RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
ISC_FALSE));
while (token.value.as_textregion.length != 0) {
if ((c = token.value.as_textregion.base[0]) == 'x') {
break;
} else
RETERR(byte_atob(c, target, state));
isc_textregion_consume(&token.value.as_textregion, 1);
}
/*
* Number of bytes.
*/
RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
ISC_FALSE));
if ((token.value.as_ulong % 4) != 0U)
isc_buffer_subtract(target, 4 - (token.value.as_ulong % 4));
/*
* Checksum.
*/
RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
ISC_FALSE));
oeor = strtol(DNS_AS_STR(token), &e, 16);
if (*e != 0)
return (DNS_R_SYNTAX);
/*
* Checksum.
*/
RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
ISC_FALSE));
osum = strtol(DNS_AS_STR(token), &e, 16);
if (*e != 0)
return (DNS_R_SYNTAX);
/*
* Checksum.
*/
RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
ISC_FALSE));
orot = strtol(DNS_AS_STR(token), &e, 16);
if (*e != 0)
return (DNS_R_SYNTAX);
if ((oeor != Ceor) || (osum != Csum) || (orot != Crot))
return(DNS_R_BADCKSUM);
return (ISC_R_SUCCESS);
}
/*
* Encode binary byte c into ASCII representation and place into *bufp,
* advancing bufp.
*/
static isc_result_t
byte_btoa(int c, isc_buffer_t *target, struct state *state) {
isc_region_t tr;
isc_buffer_availableregion(target, &tr);
Ceor ^= c;
Csum += c;
Csum += 1;
if ((Crot & 0x80000000)) {
Crot <<= 1;
Crot += 1;
} else {
Crot <<= 1;
}
Crot += c;
word <<= 8;
word |= c;
if (bcount == 3) {
if (word == 0) {
if (tr.length < 1)
return (ISC_R_NOSPACE);
tr.base[0] = 'z';
isc_buffer_add(target, 1);
} else {
register int tmp = 0;
register isc_int32_t tmpword = word;
if (tmpword < 0) {
/*
* Because some don't support u_long.
*/
tmp = 32;
tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
}
if (tmpword < 0) {
tmp = 64;
tmpword -= (isc_int32_t)(85 * 85 * 85 * 85 * 32);
}
if (tr.length < 5)
return (ISC_R_NOSPACE);
tr.base[0] = atob_digits[(tmpword /
(isc_int32_t)(85 * 85 * 85 * 85))
+ tmp];
tmpword %= (isc_int32_t)(85 * 85 * 85 * 85);
tr.base[1] = atob_digits[tmpword / (85 * 85 * 85)];
tmpword %= (85 * 85 * 85);
tr.base[2] = atob_digits[tmpword / (85 * 85)];
tmpword %= (85 * 85);
tr.base[3] = atob_digits[tmpword / 85];
tmpword %= 85;
tr.base[4] = atob_digits[tmpword];
isc_buffer_add(target, 5);
}
bcount = 0;
} else {
bcount += 1;
}
return (ISC_R_SUCCESS);
}
/*
* Encode the binary data from inbuf, of length inbuflen, into a
* target. Return success/failure status
*/
static isc_result_t
btoa_totext(unsigned char *inbuf, int inbuflen, isc_buffer_t *target) {
int inc;
struct state statebuf, *state = &statebuf;
char buf[sizeof("x 2000000000 ffffffff ffffffff ffffffff")];
Ceor = Csum = Crot = word = bcount = 0;
for (inc = 0; inc < inbuflen; inbuf++, inc++)
RETERR(byte_btoa(*inbuf, target, state));
while (bcount != 0)
RETERR(byte_btoa(0, target, state));
/*
* Put byte count and checksum information at end of buffer,
* delimited by 'x'
*/
snprintf(buf, sizeof(buf), "x %d %x %x %x", inbuflen, Ceor, Csum, Crot);
return (str_totext(buf, target));
}
static void
default_fromtext_callback(dns_rdatacallbacks_t *callbacks, const char *fmt,
...)
{
va_list ap;
UNUSED(callbacks);
va_start(ap, fmt);
vfprintf(stderr, fmt, ap);
va_end(ap);
fprintf(stderr, "\n");
}
static void
fromtext_warneof(isc_lex_t *lexer, dns_rdatacallbacks_t *callbacks) {
if (isc_lex_isfile(lexer) && callbacks != NULL) {
const char *name = isc_lex_getsourcename(lexer);
if (name == NULL)
name = "UNKNOWN";
(*callbacks->warn)(callbacks,
"%s:%lu: file does not end with newline",
name, isc_lex_getsourceline(lexer));
}
}
static void
warn_badmx(isc_token_t *token, isc_lex_t *lexer,
dns_rdatacallbacks_t *callbacks)
{
const char *file;
unsigned long line;
if (lexer != NULL) {
file = isc_lex_getsourcename(lexer);
line = isc_lex_getsourceline(lexer);
(*callbacks->warn)(callbacks, "%s:%u: warning: '%s': %s",
file, line, DNS_AS_STR(*token),
dns_result_totext(DNS_R_MXISADDRESS));
}
}
static void
warn_badname(dns_name_t *name, isc_lex_t *lexer,
dns_rdatacallbacks_t *callbacks)
{
const char *file;
unsigned long line;
char namebuf[DNS_NAME_FORMATSIZE];
if (lexer != NULL) {
file = isc_lex_getsourcename(lexer);
line = isc_lex_getsourceline(lexer);
dns_name_format(name, namebuf, sizeof(namebuf));
(*callbacks->warn)(callbacks, "%s:%u: warning: %s: %s",
file, line, namebuf,
dns_result_totext(DNS_R_BADNAME));
}
}
static void
fromtext_error(void (*callback)(dns_rdatacallbacks_t *, const char *, ...),
dns_rdatacallbacks_t *callbacks, const char *name,
unsigned long line, isc_token_t *token, isc_result_t result)
{
if (name == NULL)
name = "UNKNOWN";
if (token != NULL) {
switch (token->type) {
case isc_tokentype_eol:
(*callback)(callbacks, "%s: %s:%lu: near eol: %s",
"dns_rdata_fromtext", name, line,
dns_result_totext(result));
break;
case isc_tokentype_eof:
(*callback)(callbacks, "%s: %s:%lu: near eof: %s",
"dns_rdata_fromtext", name, line,
dns_result_totext(result));
break;
case isc_tokentype_number:
(*callback)(callbacks, "%s: %s:%lu: near %lu: %s",
"dns_rdata_fromtext", name, line,
token->value.as_ulong,
dns_result_totext(result));
break;
case isc_tokentype_string:
case isc_tokentype_qstring:
(*callback)(callbacks, "%s: %s:%lu: near '%s': %s",
"dns_rdata_fromtext", name, line,
DNS_AS_STR(*token),
dns_result_totext(result));
break;
default:
(*callback)(callbacks, "%s: %s:%lu: %s",
"dns_rdata_fromtext", name, line,
dns_result_totext(result));
break;
}
} else {
(*callback)(callbacks, "dns_rdata_fromtext: %s:%lu: %s",
name, line, dns_result_totext(result));
}
}
dns_rdatatype_t
dns_rdata_covers(dns_rdata_t *rdata) {
if (rdata->type == 46)
return (covers_rrsig(rdata));
return (covers_sig(rdata));
}
isc_boolean_t
dns_rdatatype_ismeta(dns_rdatatype_t type) {
if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_META) != 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
isc_boolean_t
dns_rdatatype_issingleton(dns_rdatatype_t type) {
if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_SINGLETON)
!= 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
isc_boolean_t
dns_rdatatype_notquestion(dns_rdatatype_t type) {
if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_NOTQUESTION)
!= 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
isc_boolean_t
dns_rdatatype_questiononly(dns_rdatatype_t type) {
if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_QUESTIONONLY)
!= 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
isc_boolean_t
dns_rdatatype_atparent(dns_rdatatype_t type) {
if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_ATPARENT) != 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
isc_boolean_t
dns_rdataclass_ismeta(dns_rdataclass_t rdclass) {
if (rdclass == dns_rdataclass_reserved0
|| rdclass == dns_rdataclass_none
|| rdclass == dns_rdataclass_any)
return (ISC_TRUE);
return (ISC_FALSE); /* Assume it is not a meta class. */
}
isc_boolean_t
dns_rdatatype_isdnssec(dns_rdatatype_t type) {
if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_DNSSEC) != 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
isc_boolean_t
dns_rdatatype_iszonecutauth(dns_rdatatype_t type) {
if ((dns_rdatatype_attributes(type)
& (DNS_RDATATYPEATTR_DNSSEC | DNS_RDATATYPEATTR_ZONECUTAUTH))
!= 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
isc_boolean_t
dns_rdatatype_isknown(dns_rdatatype_t type) {
if ((dns_rdatatype_attributes(type) & DNS_RDATATYPEATTR_UNKNOWN)
== 0)
return (ISC_TRUE);
return (ISC_FALSE);
}
void
dns_rdata_exists(dns_rdata_t *rdata, dns_rdatatype_t type) {
REQUIRE(rdata != NULL);
REQUIRE(DNS_RDATA_INITIALIZED(rdata));
rdata->data = NULL;
rdata->length = 0;
rdata->flags = DNS_RDATA_UPDATE;
rdata->type = type;
rdata->rdclass = dns_rdataclass_any;
}
void
dns_rdata_notexist(dns_rdata_t *rdata, dns_rdatatype_t type) {
REQUIRE(rdata != NULL);
REQUIRE(DNS_RDATA_INITIALIZED(rdata));
rdata->data = NULL;
rdata->length = 0;
rdata->flags = DNS_RDATA_UPDATE;
rdata->type = type;
rdata->rdclass = dns_rdataclass_none;
}
void
dns_rdata_deleterrset(dns_rdata_t *rdata, dns_rdatatype_t type) {
REQUIRE(rdata != NULL);
REQUIRE(DNS_RDATA_INITIALIZED(rdata));
rdata->data = NULL;
rdata->length = 0;
rdata->flags = DNS_RDATA_UPDATE;
rdata->type = type;
rdata->rdclass = dns_rdataclass_any;
}
void
dns_rdata_makedelete(dns_rdata_t *rdata) {
REQUIRE(rdata != NULL);
rdata->rdclass = dns_rdataclass_none;
}
const char *
dns_rdata_updateop(dns_rdata_t *rdata, dns_section_t section) {
REQUIRE(rdata != NULL);
REQUIRE(DNS_RDATA_INITIALIZED(rdata));
switch (section) {
case DNS_SECTION_PREREQUISITE:
switch (rdata->rdclass) {
case dns_rdataclass_none:
switch (rdata->type) {
case dns_rdatatype_any:
return ("domain doesn't exist");
default:
return ("rrset doesn't exist");
}
case dns_rdataclass_any:
switch (rdata->type) {
case dns_rdatatype_any:
return ("domain exists");
default:
return ("rrset exists (value independent)");
}
default:
return ("rrset exists (value dependent)");
}
case DNS_SECTION_UPDATE:
switch (rdata->rdclass) {
case dns_rdataclass_none:
return ("delete");
case dns_rdataclass_any:
switch (rdata->type) {
case dns_rdatatype_any:
return ("delete all rrsets");
default:
return ("delete rrset");
}
default:
return ("add");
}
}
return ("invalid");
}