"Fossies" - the Fresh Open Source Software Archive

Member "bind-9.16.7/lib/dns/rdata/in_1/srv_33.c" (4 Sep 2020, 9398 Bytes) of package /linux/misc/dns/bind9/9.16.7/bind-9.16.7.tar.xz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "srv_33.c" see the Fossies "Dox" file reference documentation.

    1 /*
    2  * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
    3  *
    4  * This Source Code Form is subject to the terms of the Mozilla Public
    5  * License, v. 2.0. If a copy of the MPL was not distributed with this
    6  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
    7  *
    8  * See the COPYRIGHT file distributed with this work for additional
    9  * information regarding copyright ownership.
   10  */
   11 
   12 /* RFC2782 */
   13 
   14 #ifndef RDATA_IN_1_SRV_33_C
   15 #define RDATA_IN_1_SRV_33_C
   16 
   17 #define RRTYPE_SRV_ATTRIBUTES (0)
   18 
   19 static inline isc_result_t
   20 fromtext_in_srv(ARGS_FROMTEXT) {
   21     isc_token_t token;
   22     dns_name_t name;
   23     isc_buffer_t buffer;
   24     bool ok;
   25 
   26     REQUIRE(type == dns_rdatatype_srv);
   27     REQUIRE(rdclass == dns_rdataclass_in);
   28 
   29     UNUSED(type);
   30     UNUSED(rdclass);
   31     UNUSED(callbacks);
   32 
   33     /*
   34      * Priority.
   35      */
   36     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   37                       false));
   38     if (token.value.as_ulong > 0xffffU) {
   39         RETTOK(ISC_R_RANGE);
   40     }
   41     RETERR(uint16_tobuffer(token.value.as_ulong, target));
   42 
   43     /*
   44      * Weight.
   45      */
   46     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   47                       false));
   48     if (token.value.as_ulong > 0xffffU) {
   49         RETTOK(ISC_R_RANGE);
   50     }
   51     RETERR(uint16_tobuffer(token.value.as_ulong, target));
   52 
   53     /*
   54      * Port.
   55      */
   56     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_number,
   57                       false));
   58     if (token.value.as_ulong > 0xffffU) {
   59         RETTOK(ISC_R_RANGE);
   60     }
   61     RETERR(uint16_tobuffer(token.value.as_ulong, target));
   62 
   63     /*
   64      * Target.
   65      */
   66     RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
   67                       false));
   68     dns_name_init(&name, NULL);
   69     buffer_fromregion(&buffer, &token.value.as_region);
   70     if (origin == NULL) {
   71         origin = dns_rootname;
   72     }
   73     RETTOK(dns_name_fromtext(&name, &buffer, origin, options, target));
   74     ok = true;
   75     if ((options & DNS_RDATA_CHECKNAMES) != 0) {
   76         ok = dns_name_ishostname(&name, false);
   77     }
   78     if (!ok && (options & DNS_RDATA_CHECKNAMESFAIL) != 0) {
   79         RETTOK(DNS_R_BADNAME);
   80     }
   81     if (!ok && callbacks != NULL) {
   82         warn_badname(&name, lexer, callbacks);
   83     }
   84     return (ISC_R_SUCCESS);
   85 }
   86 
   87 static inline isc_result_t
   88 totext_in_srv(ARGS_TOTEXT) {
   89     isc_region_t region;
   90     dns_name_t name;
   91     dns_name_t prefix;
   92     bool sub;
   93     char buf[sizeof("64000")];
   94     unsigned short num;
   95 
   96     REQUIRE(rdata->type == dns_rdatatype_srv);
   97     REQUIRE(rdata->rdclass == dns_rdataclass_in);
   98     REQUIRE(rdata->length != 0);
   99 
  100     dns_name_init(&name, NULL);
  101     dns_name_init(&prefix, NULL);
  102 
  103     /*
  104      * Priority.
  105      */
  106     dns_rdata_toregion(rdata, &region);
  107     num = uint16_fromregion(&region);
  108     isc_region_consume(&region, 2);
  109     snprintf(buf, sizeof(buf), "%u", num);
  110     RETERR(str_totext(buf, target));
  111     RETERR(str_totext(" ", target));
  112 
  113     /*
  114      * Weight.
  115      */
  116     num = uint16_fromregion(&region);
  117     isc_region_consume(&region, 2);
  118     snprintf(buf, sizeof(buf), "%u", num);
  119     RETERR(str_totext(buf, target));
  120     RETERR(str_totext(" ", target));
  121 
  122     /*
  123      * Port.
  124      */
  125     num = uint16_fromregion(&region);
  126     isc_region_consume(&region, 2);
  127     snprintf(buf, sizeof(buf), "%u", num);
  128     RETERR(str_totext(buf, target));
  129     RETERR(str_totext(" ", target));
  130 
  131     /*
  132      * Target.
  133      */
  134     dns_name_fromregion(&name, &region);
  135     sub = name_prefix(&name, tctx->origin, &prefix);
  136     return (dns_name_totext(&prefix, sub, target));
  137 }
  138 
  139 static inline isc_result_t
  140 fromwire_in_srv(ARGS_FROMWIRE) {
  141     dns_name_t name;
  142     isc_region_t sr;
  143 
  144     REQUIRE(type == dns_rdatatype_srv);
  145     REQUIRE(rdclass == dns_rdataclass_in);
  146 
  147     UNUSED(type);
  148     UNUSED(rdclass);
  149 
  150     dns_decompress_setmethods(dctx, DNS_COMPRESS_NONE);
  151 
  152     dns_name_init(&name, NULL);
  153 
  154     /*
  155      * Priority, weight, port.
  156      */
  157     isc_buffer_activeregion(source, &sr);
  158     if (sr.length < 6) {
  159         return (ISC_R_UNEXPECTEDEND);
  160     }
  161     RETERR(mem_tobuffer(target, sr.base, 6));
  162     isc_buffer_forward(source, 6);
  163 
  164     /*
  165      * Target.
  166      */
  167     return (dns_name_fromwire(&name, source, dctx, options, target));
  168 }
  169 
  170 static inline isc_result_t
  171 towire_in_srv(ARGS_TOWIRE) {
  172     dns_name_t name;
  173     dns_offsets_t offsets;
  174     isc_region_t sr;
  175 
  176     REQUIRE(rdata->type == dns_rdatatype_srv);
  177     REQUIRE(rdata->length != 0);
  178 
  179     dns_compress_setmethods(cctx, DNS_COMPRESS_NONE);
  180     /*
  181      * Priority, weight, port.
  182      */
  183     dns_rdata_toregion(rdata, &sr);
  184     RETERR(mem_tobuffer(target, sr.base, 6));
  185     isc_region_consume(&sr, 6);
  186 
  187     /*
  188      * Target.
  189      */
  190     dns_name_init(&name, offsets);
  191     dns_name_fromregion(&name, &sr);
  192     return (dns_name_towire(&name, cctx, target));
  193 }
  194 
  195 static inline int
  196 compare_in_srv(ARGS_COMPARE) {
  197     dns_name_t name1;
  198     dns_name_t name2;
  199     isc_region_t region1;
  200     isc_region_t region2;
  201     int order;
  202 
  203     REQUIRE(rdata1->type == rdata2->type);
  204     REQUIRE(rdata1->rdclass == rdata2->rdclass);
  205     REQUIRE(rdata1->type == dns_rdatatype_srv);
  206     REQUIRE(rdata1->rdclass == dns_rdataclass_in);
  207     REQUIRE(rdata1->length != 0);
  208     REQUIRE(rdata2->length != 0);
  209 
  210     /*
  211      * Priority, weight, port.
  212      */
  213     order = memcmp(rdata1->data, rdata2->data, 6);
  214     if (order != 0) {
  215         return (order < 0 ? -1 : 1);
  216     }
  217 
  218     /*
  219      * Target.
  220      */
  221     dns_name_init(&name1, NULL);
  222     dns_name_init(&name2, NULL);
  223 
  224     dns_rdata_toregion(rdata1, &region1);
  225     dns_rdata_toregion(rdata2, &region2);
  226 
  227     isc_region_consume(&region1, 6);
  228     isc_region_consume(&region2, 6);
  229 
  230     dns_name_fromregion(&name1, &region1);
  231     dns_name_fromregion(&name2, &region2);
  232 
  233     return (dns_name_rdatacompare(&name1, &name2));
  234 }
  235 
  236 static inline isc_result_t
  237 fromstruct_in_srv(ARGS_FROMSTRUCT) {
  238     dns_rdata_in_srv_t *srv = source;
  239     isc_region_t region;
  240 
  241     REQUIRE(type == dns_rdatatype_srv);
  242     REQUIRE(rdclass == dns_rdataclass_in);
  243     REQUIRE(srv != NULL);
  244     REQUIRE(srv->common.rdtype == type);
  245     REQUIRE(srv->common.rdclass == rdclass);
  246 
  247     UNUSED(type);
  248     UNUSED(rdclass);
  249 
  250     RETERR(uint16_tobuffer(srv->priority, target));
  251     RETERR(uint16_tobuffer(srv->weight, target));
  252     RETERR(uint16_tobuffer(srv->port, target));
  253     dns_name_toregion(&srv->target, &region);
  254     return (isc_buffer_copyregion(target, &region));
  255 }
  256 
  257 static inline isc_result_t
  258 tostruct_in_srv(ARGS_TOSTRUCT) {
  259     isc_region_t region;
  260     dns_rdata_in_srv_t *srv = target;
  261     dns_name_t name;
  262 
  263     REQUIRE(rdata->rdclass == dns_rdataclass_in);
  264     REQUIRE(rdata->type == dns_rdatatype_srv);
  265     REQUIRE(srv != NULL);
  266     REQUIRE(rdata->length != 0);
  267 
  268     srv->common.rdclass = rdata->rdclass;
  269     srv->common.rdtype = rdata->type;
  270     ISC_LINK_INIT(&srv->common, link);
  271 
  272     dns_name_init(&name, NULL);
  273     dns_rdata_toregion(rdata, &region);
  274     srv->priority = uint16_fromregion(&region);
  275     isc_region_consume(&region, 2);
  276     srv->weight = uint16_fromregion(&region);
  277     isc_region_consume(&region, 2);
  278     srv->port = uint16_fromregion(&region);
  279     isc_region_consume(&region, 2);
  280     dns_name_fromregion(&name, &region);
  281     dns_name_init(&srv->target, NULL);
  282     RETERR(name_duporclone(&name, mctx, &srv->target));
  283     srv->mctx = mctx;
  284     return (ISC_R_SUCCESS);
  285 }
  286 
  287 static inline void
  288 freestruct_in_srv(ARGS_FREESTRUCT) {
  289     dns_rdata_in_srv_t *srv = source;
  290 
  291     REQUIRE(srv != NULL);
  292     REQUIRE(srv->common.rdclass == dns_rdataclass_in);
  293     REQUIRE(srv->common.rdtype == dns_rdatatype_srv);
  294 
  295     if (srv->mctx == NULL) {
  296         return;
  297     }
  298 
  299     dns_name_free(&srv->target, srv->mctx);
  300     srv->mctx = NULL;
  301 }
  302 
  303 static inline isc_result_t
  304 additionaldata_in_srv(ARGS_ADDLDATA) {
  305     char buf[sizeof("_65000._tcp")];
  306     dns_fixedname_t fixed;
  307     dns_name_t name;
  308     dns_offsets_t offsets;
  309     isc_region_t region;
  310     uint16_t port;
  311     isc_result_t result;
  312 
  313     REQUIRE(rdata->type == dns_rdatatype_srv);
  314     REQUIRE(rdata->rdclass == dns_rdataclass_in);
  315 
  316     dns_name_init(&name, offsets);
  317     dns_rdata_toregion(rdata, &region);
  318     isc_region_consume(&region, 4);
  319     port = uint16_fromregion(&region);
  320     isc_region_consume(&region, 2);
  321     dns_name_fromregion(&name, &region);
  322 
  323     if (dns_name_equal(&name, dns_rootname)) {
  324         return (ISC_R_SUCCESS);
  325     }
  326 
  327     result = (add)(arg, &name, dns_rdatatype_a);
  328     if (result != ISC_R_SUCCESS) {
  329         return (result);
  330     }
  331 
  332     dns_fixedname_init(&fixed);
  333     snprintf(buf, sizeof(buf), "_%u._tcp", port);
  334     result = dns_name_fromstring2(dns_fixedname_name(&fixed), buf, NULL, 0,
  335                       NULL);
  336     if (result != ISC_R_SUCCESS) {
  337         return (ISC_R_SUCCESS);
  338     }
  339 
  340     result = dns_name_concatenate(dns_fixedname_name(&fixed), &name,
  341                       dns_fixedname_name(&fixed), NULL);
  342     if (result != ISC_R_SUCCESS) {
  343         return (ISC_R_SUCCESS);
  344     }
  345 
  346     return ((add)(arg, dns_fixedname_name(&fixed), dns_rdatatype_tlsa));
  347 }
  348 
  349 static inline isc_result_t
  350 digest_in_srv(ARGS_DIGEST) {
  351     isc_region_t r1, r2;
  352     dns_name_t name;
  353 
  354     REQUIRE(rdata->type == dns_rdatatype_srv);
  355     REQUIRE(rdata->rdclass == dns_rdataclass_in);
  356 
  357     dns_rdata_toregion(rdata, &r1);
  358     r2 = r1;
  359     isc_region_consume(&r2, 6);
  360     r1.length = 6;
  361     RETERR((digest)(arg, &r1));
  362     dns_name_init(&name, NULL);
  363     dns_name_fromregion(&name, &r2);
  364     return (dns_name_digest(&name, digest, arg));
  365 }
  366 
  367 static inline bool
  368 checkowner_in_srv(ARGS_CHECKOWNER) {
  369     REQUIRE(type == dns_rdatatype_srv);
  370     REQUIRE(rdclass == dns_rdataclass_in);
  371 
  372     UNUSED(name);
  373     UNUSED(type);
  374     UNUSED(rdclass);
  375     UNUSED(wildcard);
  376 
  377     return (true);
  378 }
  379 
  380 static inline bool
  381 checknames_in_srv(ARGS_CHECKNAMES) {
  382     isc_region_t region;
  383     dns_name_t name;
  384 
  385     REQUIRE(rdata->type == dns_rdatatype_srv);
  386     REQUIRE(rdata->rdclass == dns_rdataclass_in);
  387 
  388     UNUSED(owner);
  389 
  390     dns_rdata_toregion(rdata, &region);
  391     isc_region_consume(&region, 6);
  392     dns_name_init(&name, NULL);
  393     dns_name_fromregion(&name, &region);
  394     if (!dns_name_ishostname(&name, false)) {
  395         if (bad != NULL) {
  396             dns_name_clone(&name, bad);
  397         }
  398         return (false);
  399     }
  400     return (true);
  401 }
  402 
  403 static inline int
  404 casecompare_in_srv(ARGS_COMPARE) {
  405     return (compare_in_srv(rdata1, rdata2));
  406 }
  407 
  408 #endif /* RDATA_IN_1_SRV_33_C */