5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews/*
a134177ed9f82189504191d90f3ed9e97c2b47cbTinderbox User * Copyright (C) 1998-2001, 2003-2005, 2007, 2009, 2012, 2015-2017 Internet Systems Consortium, Inc. ("ISC")
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence *
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * This Source Code Form is subject to the terms of the Mozilla Public
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * License, v. 2.0. If a copy of the MPL was not distributed with this
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * file, You can obtain one at http://mozilla.org/MPL/2.0/.
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews */
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
4b6dc226f78862286daa69fba761eac9fd5da16aAutomatic Updater/* $Id: mx_15.c,v 1.58 2009/12/04 22:06:37 tbox Exp $ */
96805adfc95a9e0c5b800869dd4afe55b5616f12James Brister
96805adfc95a9e0c5b800869dd4afe55b5616f12James Brister/* reviewed: Wed Mar 15 18:05:46 PST 2000 by brister */
3761c433912beabe43abeed2c3513b6201c59f64Mark Andrews
854d0238dbc2908490197984b3b9d558008a53dfMark Andrews#ifndef RDATA_GENERIC_MX_15_C
854d0238dbc2908490197984b3b9d558008a53dfMark Andrews#define RDATA_GENERIC_MX_15_C
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews#include <string.h>
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews#include <isc/net.h>
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews#include <dns/fixedname.h>
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff#define RRTYPE_MX_ATTRIBUTES (0)
6324997211a5e2d82528dcde98e8981190a35faeMichael Graff
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrewsstatic isc_boolean_t
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrewscheck_mx(isc_token_t *token) {
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews char tmp[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:123.123.123.123.")];
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews struct in_addr addr;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews struct in6_addr addr6;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (strlcpy(tmp, DNS_AS_STR(*token), sizeof(tmp)) >= sizeof(tmp))
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews return (ISC_TRUE);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (tmp[strlen(tmp) - 1] == '.')
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews tmp[strlen(tmp) - 1] = '\0';
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (inet_aton(tmp, &addr) == 1 ||
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews inet_pton(AF_INET6, tmp, &addr6) == 1)
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews return (ISC_FALSE);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews return (ISC_TRUE);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews}
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic inline isc_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencefromtext_mx(ARGS_FROMTEXT) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_token_t token;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_t name;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_buffer_t buffer;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_boolean_t ok;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(type == dns_rdatatype_mx);
f7b99290c31abeb20c55fc55391510450ce60423Mark Andrews
ae114ded82e773a4d9058f833f964a17514712a8Brian Wellington UNUSED(type);
96805adfc95a9e0c5b800869dd4afe55b5616f12James Brister UNUSED(rdclass);
f7b99290c31abeb20c55fc55391510450ce60423Mark Andrews
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington ISC_FALSE));
5eb91bd90e3ad3426e5e3213031556a737cf3809Mark Andrews if (token.value.as_ulong > 0xffffU)
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews RETTOK(ISC_R_RANGE);
deaaf94332abbfdb3aff53675546acfed16e5eb6Mark Andrews RETERR(uint16_tobuffer(token.value.as_ulong, target));
deaaf94332abbfdb3aff53675546acfed16e5eb6Mark Andrews
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
add4043305ca411202ed9cf1929a4179016515ceBrian Wellington ISC_FALSE));
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews ok = ISC_TRUE;
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if ((options & DNS_RDATA_CHECKMX) != 0)
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews ok = check_mx(&token);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (!ok && (options & DNS_RDATA_CHECKMXFAIL) != 0)
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews RETTOK(DNS_R_MXISADDRESS);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews if (!ok && callbacks != NULL)
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews warn_badmx(&token, lexer, callbacks);
c5223c9cb7c22620d5ee6611228673e95b48a270Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_init(&name, NULL);
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence buffer_fromregion(&buffer, &token.value.as_region);
e0a30050c8516a3d54a4f8dcdd88435704a8a3edMark Andrews if (origin == NULL)
e0a30050c8516a3d54a4f8dcdd88435704a8a3edMark Andrews origin = dns_rootname;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews ok = ISC_TRUE;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if ((options & DNS_RDATA_CHECKNAMES) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews ok = dns_name_ishostname(&name, ISC_FALSE);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (!ok && (options & DNS_RDATA_CHECKNAMESFAIL) != 0)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews RETTOK(DNS_R_BADNAME);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (!ok && callbacks != NULL)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews warn_badname(&name, lexer, callbacks);
9ac7076ebad044afb15e9e2687e3696868778538Mark Andrews return (ISC_R_SUCCESS);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic inline isc_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencetotext_mx(ARGS_TOTEXT) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_region_t region;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_t name;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_t prefix;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_boolean_t sub;
91cd0f93ad34d23e8b09dca337120f64fbe8f0a1Andreas Gustafsson char buf[sizeof("64000")];
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews unsigned short num;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(rdata->type == dns_rdatatype_mx);
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews REQUIRE(rdata->length != 0);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_init(&name, NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_init(&prefix, NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdata_toregion(rdata, &region);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews num = uint16_fromregion(&region);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_region_consume(&region, 2);
ad1317338af79edad878c9c3e4361798503310baMark Andrews snprintf(buf, sizeof(buf), "%u", num);
deaaf94332abbfdb3aff53675546acfed16e5eb6Mark Andrews RETERR(str_totext(buf, target));
62c1de9d9485003ea5af13061f1d30f081442ee9Michael Graff
deaaf94332abbfdb3aff53675546acfed16e5eb6Mark Andrews RETERR(str_totext(" ", target));
62c1de9d9485003ea5af13061f1d30f081442ee9Michael Graff
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_fromregion(&name, &region);
5fc7ba3e1ac5d72239e9971e0f469dd5796738f9Andreas Gustafsson sub = name_prefix(&name, tctx->origin, &prefix);
96805adfc95a9e0c5b800869dd4afe55b5616f12James Brister return (dns_name_totext(&prefix, sub, target));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic inline isc_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencefromwire_mx(ARGS_FROMWIRE) {
4b6dc226f78862286daa69fba761eac9fd5da16aAutomatic Updater dns_name_t name;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_region_t sregion;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(type == dns_rdatatype_mx);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews
ae114ded82e773a4d9058f833f964a17514712a8Brian Wellington UNUSED(type);
96805adfc95a9e0c5b800869dd4afe55b5616f12James Brister UNUSED(rdclass);
1ef8965366d91e02a4672c35a187d30aa4a4c72cMark Andrews
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson dns_decompress_setmethods(dctx, DNS_COMPRESS_GLOBAL14);
40f53fa8d9c6a4fc38c0014495e7a42b08f52481David Lawrence
4b6dc226f78862286daa69fba761eac9fd5da16aAutomatic Updater dns_name_init(&name, NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
6e49e91bd08778d7eae45a2229dcf41ed97cc636David Lawrence isc_buffer_activeregion(source, &sregion);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews if (sregion.length < 2)
419590499823ce15b5d2ad4fe71eaf04bd5a86c0Michael Graff return (ISC_R_UNEXPECTEDEND);
62c1de9d9485003ea5af13061f1d30f081442ee9Michael Graff RETERR(mem_tobuffer(target, sregion.base, 2));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_buffer_forward(source, 2);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (dns_name_fromwire(&name, source, dctx, options, target));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic inline isc_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencetowire_mx(ARGS_TOWIRE) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_t name;
cdc50af0bff41accc02c613b9c6d8cd41b171ffeBrian Wellington dns_offsets_t offsets;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_region_t region;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(rdata->type == dns_rdatatype_mx);
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews REQUIRE(rdata->length != 0);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
94a08e09db3dc844b6ee4841c368a2d7074a9c3fAndreas Gustafsson dns_compress_setmethods(cctx, DNS_COMPRESS_GLOBAL14);
52637f592f705ca93fadc218e403fd55e8ce4aeaMark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdata_toregion(rdata, &region);
62c1de9d9485003ea5af13061f1d30f081442ee9Michael Graff RETERR(mem_tobuffer(target, region.base, 2));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_region_consume(&region, 2);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
cdc50af0bff41accc02c613b9c6d8cd41b171ffeBrian Wellington dns_name_init(&name, offsets);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_fromregion(&name, &region);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
deaaf94332abbfdb3aff53675546acfed16e5eb6Mark Andrews return (dns_name_towire(&name, cctx, target));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
4529cdaedaf1a0a5f8ff89aeca510b7a4475446cBob Halleystatic inline int
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencecompare_mx(ARGS_COMPARE) {
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_t name1;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_t name2;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_region_t region1;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_region_t region2;
41aad56b6cc458cbf7b8483576d990a77ae9bac2Andreas Gustafsson int order;
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3d5cad69ec20157912e95cf3b79316dfb0a314f3Mark Andrews REQUIRE(rdata1->type == rdata2->type);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley REQUIRE(rdata1->rdclass == rdata2->rdclass);
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(rdata1->type == dns_rdatatype_mx);
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews REQUIRE(rdata1->length != 0);
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews REQUIRE(rdata2->length != 0);
f7b99290c31abeb20c55fc55391510450ce60423Mark Andrews
41aad56b6cc458cbf7b8483576d990a77ae9bac2Andreas Gustafsson order = memcmp(rdata1->data, rdata2->data, 2);
41aad56b6cc458cbf7b8483576d990a77ae9bac2Andreas Gustafsson if (order != 0)
41aad56b6cc458cbf7b8483576d990a77ae9bac2Andreas Gustafsson return (order < 0 ? -1 : 1);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_init(&name1, NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_init(&name2, NULL);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdata_toregion(rdata1, &region1);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_rdata_toregion(rdata2, &region2);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_region_consume(&region1, 2);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews isc_region_consume(&region2, 2);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_fromregion(&name1, &region1);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews dns_name_fromregion(&name2, &region2);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
7c0539bea56022274da04263eb41fbb5b8835c38Mark Andrews return (dns_name_rdatacompare(&name1, &name2));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic inline isc_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencefromstruct_mx(ARGS_FROMSTRUCT) {
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews dns_rdata_mx_t *mx = source;
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews isc_region_t region;
f7b99290c31abeb20c55fc55391510450ce60423Mark Andrews
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(type == dns_rdatatype_mx);
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews REQUIRE(source != NULL);
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews REQUIRE(mx->common.rdtype == type);
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews REQUIRE(mx->common.rdclass == rdclass);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
ae114ded82e773a4d9058f833f964a17514712a8Brian Wellington UNUSED(type);
63cef8bde8b92aeb30ccdcf21d4e44c9be9cc6e3Andreas Gustafsson UNUSED(rdclass);
63cef8bde8b92aeb30ccdcf21d4e44c9be9cc6e3Andreas Gustafsson
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews RETERR(uint16_tobuffer(mx->pref, target));
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews dns_name_toregion(&mx->mx, &region);
373ce67419680a398ba3dc51a14a486caaf0afb0Mark Andrews return (isc_buffer_copyregion(target, &region));
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic inline isc_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencetostruct_mx(ARGS_TOSTRUCT) {
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews isc_region_t region;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews dns_rdata_mx_t *mx = target;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews dns_name_t name;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(rdata->type == dns_rdatatype_mx);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews REQUIRE(target != NULL);
b589e90689c6e87bf9608424ca8d99571c18bc61Mark Andrews REQUIRE(rdata->length != 0);
f7b99290c31abeb20c55fc55391510450ce60423Mark Andrews
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews mx->common.rdclass = rdata->rdclass;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews mx->common.rdtype = rdata->type;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews ISC_LINK_INIT(&mx->common, link);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews dns_name_init(&name, NULL);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews dns_rdata_toregion(rdata, &region);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews mx->pref = uint16_fromregion(&region);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews isc_region_consume(&region, 2);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews dns_name_fromregion(&name, &region);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews dns_name_init(&mx->mx, NULL);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews RETERR(name_duporclone(&name, mctx, &mx->mx));
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews mx->mctx = mctx;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews return (ISC_R_SUCCESS);
5d51e67c3b4f35c1be742574aacc1d88fe6ed444Mark Andrews}
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews
4529cdaedaf1a0a5f8ff89aeca510b7a4475446cBob Halleystatic inline void
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencefreestruct_mx(ARGS_FREESTRUCT) {
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews dns_rdata_mx_t *mx = source;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews REQUIRE(source != NULL);
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(mx->common.rdtype == dns_rdatatype_mx);
82ca33427bdd4f3bc4ed3431e86bd810fe751674Andreas Gustafsson
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews if (mx->mctx == NULL)
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews return;
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews dns_name_free(&mx->mx, mx->mctx);
9281e7aa775026dc47c01745fdcc438645146877Mark Andrews mx->mctx = NULL;
94a3bcd132e515b4baa0884ba9dd0f361d2e17bcMark Andrews}
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrewsstatic unsigned char port25_offset[] = { 0, 3 };
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrewsstatic unsigned char port25_ndata[] = "\003_25\004_tcp";
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrewsstatic dns_name_t port25 =
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews DNS_NAME_INITNONABSOLUTE(port25_ndata, port25_offset);
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic inline isc_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrenceadditionaldata_mx(ARGS_ADDLDATA) {
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews isc_result_t result;
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews dns_fixedname_t fixed;
f1b0e9107d5fc7669920b76b4e32f93e9d16c85cBob Halley dns_name_t name;
cdc50af0bff41accc02c613b9c6d8cd41b171ffeBrian Wellington dns_offsets_t offsets;
f1b0e9107d5fc7669920b76b4e32f93e9d16c85cBob Halley isc_region_t region;
f1b0e9107d5fc7669920b76b4e32f93e9d16c85cBob Halley
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(rdata->type == dns_rdatatype_mx);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
cdc50af0bff41accc02c613b9c6d8cd41b171ffeBrian Wellington dns_name_init(&name, offsets);
f1b0e9107d5fc7669920b76b4e32f93e9d16c85cBob Halley dns_rdata_toregion(rdata, &region);
f1b0e9107d5fc7669920b76b4e32f93e9d16c85cBob Halley isc_region_consume(&region, 2);
f1b0e9107d5fc7669920b76b4e32f93e9d16c85cBob Halley dns_name_fromregion(&name, &region);
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews if (dns_name_equal(&name, dns_rootname))
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews return (ISC_R_SUCCESS);
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews result = (add)(arg, &name, dns_rdatatype_a);
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews if (result != ISC_R_SUCCESS)
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews return (result);
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews dns_fixedname_init(&fixed);
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews result = dns_name_concatenate(&port25, &name,
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews dns_fixedname_name(&fixed), NULL);
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews if (result != ISC_R_SUCCESS)
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews return (ISC_R_SUCCESS);
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews
bc09fd1365d1a48972fa99cd6ed2aa788a28ef33Mark Andrews return ((add)(arg, dns_fixedname_name(&fixed), dns_rdatatype_tlsa));
d981ca645597116d227a48bf37cc5edc061c854dBob Halley}
d981ca645597116d227a48bf37cc5edc061c854dBob Halley
3ddd814a97de1d152ba0913c592d6e6dc83d38a6Michael Graffstatic inline isc_result_t
6d12fdf96621801e80f3f4c2a8a569fe48766a20David Lawrencedigest_mx(ARGS_DIGEST) {
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley isc_region_t r1, r2;
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley dns_name_t name;
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(rdata->type == dns_rdatatype_mx);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley dns_rdata_toregion(rdata, &r1);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley r2 = r1;
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley isc_region_consume(&r2, 2);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley r1.length = 2;
62c1de9d9485003ea5af13061f1d30f081442ee9Michael Graff RETERR((digest)(arg, &r1));
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley dns_name_init(&name, NULL);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley dns_name_fromregion(&name, &r2);
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley return (dns_name_digest(&name, digest, arg));
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley}
e27a69f8bd9538e08f775265167ba6cc5f47c587Bob Halley
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline isc_boolean_t
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewscheckowner_mx(ARGS_CHECKOWNER) {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(type == dns_rdatatype_mx);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews UNUSED(type);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews UNUSED(rdclass);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (dns_name_ishostname(name, wildcard));
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews}
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewsstatic inline isc_boolean_t
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrewschecknames_mx(ARGS_CHECKNAMES) {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_region_t region;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_name_t name;
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
b0ba1a6059b6d6c4b3aa77d8bc84cc443b981e01Mukund Sivaraman REQUIRE(rdata->type == dns_rdatatype_mx);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews UNUSED(owner);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_rdata_toregion(rdata, &region);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews isc_region_consume(&region, 2);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_name_init(&name, NULL);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_name_fromregion(&name, &region);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (!dns_name_ishostname(&name, ISC_FALSE)) {
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews if (bad != NULL)
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews dns_name_clone(&name, bad);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (ISC_FALSE);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews }
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews return (ISC_TRUE);
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews}
2047977ce2dfcfe3a0fa2d638c3242841310fad3Mark Andrews
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrewsstatic inline int
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrewscasecompare_mx(ARGS_COMPARE) {
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews return (compare_mx(rdata1, rdata2));
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews}
3d17a3ba61a303d5c4d9867068d0fbe9f24d2988Mark Andrews
854d0238dbc2908490197984b3b9d558008a53dfMark Andrews#endif /* RDATA_GENERIC_MX_15_C */