wks_11.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2004, 2007, 2009, 2011-2014  Internet Systems Consortium, Inc. ("ISC")
00003  * Copyright (C) 1999-2002  Internet Software Consortium.
00004  *
00005  * Permission to use, copy, modify, and/or distribute this software for any
00006  * purpose with or without fee is hereby granted, provided that the above
00007  * copyright notice and this permission notice appear in all copies.
00008  *
00009  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
00010  * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
00011  * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
00012  * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
00013  * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
00014  * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
00015  * PERFORMANCE OF THIS SOFTWARE.
00016  */
00017 
00018 /* $Id$ */
00019 
00020 /* Reviewed: Fri Mar 17 15:01:49 PST 2000 by explorer */
00021 
00022 #ifndef RDATA_IN_1_WKS_11_C
00023 #define RDATA_IN_1_WKS_11_C
00024 
00025 #include <limits.h>
00026 #include <stdlib.h>
00027 
00028 #include <isc/net.h>
00029 #include <isc/netdb.h>
00030 #include <isc/once.h>
00031 
00032 #define RRTYPE_WKS_ATTRIBUTES (0)
00033 
00034 static isc_mutex_t wks_lock;
00035 
00036 static void init_lock(void) {
00037         RUNTIME_CHECK(isc_mutex_init(&wks_lock) == ISC_R_SUCCESS);
00038 }
00039 
00040 static isc_boolean_t
00041 mygetprotobyname(const char *name, long *proto) {
00042         struct protoent *pe;
00043 
00044         LOCK(&wks_lock);
00045         pe = getprotobyname(name);
00046         if (pe != NULL)
00047                 *proto = pe->p_proto;
00048         UNLOCK(&wks_lock);
00049         return (ISC_TF(pe != NULL));
00050 }
00051 
00052 static isc_boolean_t
00053 mygetservbyname(const char *name, const char *proto, long *port) {
00054         struct servent *se;
00055 
00056         LOCK(&wks_lock);
00057         se = getservbyname(name, proto);
00058         if (se != NULL)
00059                 *port = ntohs(se->s_port);
00060         UNLOCK(&wks_lock);
00061         return (ISC_TF(se != NULL));
00062 }
00063 
00064 static inline isc_result_t
00065 fromtext_in_wks(ARGS_FROMTEXT) {
00066         static isc_once_t once = ISC_ONCE_INIT;
00067         isc_token_t token;
00068         isc_region_t region;
00069         struct in_addr addr;
00070         char *e;
00071         long proto;
00072         unsigned char bm[8*1024]; /* 64k bits */
00073         long port;
00074         long maxport = -1;
00075         const char *ps = NULL;
00076         unsigned int n;
00077         char service[32];
00078         int i;
00079 
00080         REQUIRE(type == 11);
00081         REQUIRE(rdclass == 1);
00082 
00083         UNUSED(type);
00084         UNUSED(origin);
00085         UNUSED(options);
00086         UNUSED(rdclass);
00087 
00088         RUNTIME_CHECK(isc_once_do(&once, init_lock) == ISC_R_SUCCESS);
00089 
00090         /*
00091          * IPv4 dotted quad.
00092          */
00093         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
00094                                       ISC_FALSE));
00095 
00096         isc_buffer_availableregion(target, &region);
00097         if (getquad(DNS_AS_STR(token), &addr, lexer, callbacks) != 1)
00098                 RETTOK(DNS_R_BADDOTTEDQUAD);
00099         if (region.length < 4)
00100                 return (ISC_R_NOSPACE);
00101         memmove(region.base, &addr, 4);
00102         isc_buffer_add(target, 4);
00103 
00104         /*
00105          * Protocol.
00106          */
00107         RETERR(isc_lex_getmastertoken(lexer, &token, isc_tokentype_string,
00108                                       ISC_FALSE));
00109 
00110         proto = strtol(DNS_AS_STR(token), &e, 10);
00111         if (*e == 0)
00112                 ;
00113         else if (!mygetprotobyname(DNS_AS_STR(token), &proto))
00114                 RETTOK(DNS_R_UNKNOWNPROTO);
00115 
00116         if (proto < 0 || proto > 0xff)
00117                 RETTOK(ISC_R_RANGE);
00118 
00119         if (proto == IPPROTO_TCP)
00120                 ps = "tcp";
00121         else if (proto == IPPROTO_UDP)
00122                 ps = "udp";
00123 
00124         RETERR(uint8_tobuffer(proto, target));
00125 
00126         memset(bm, 0, sizeof(bm));
00127         do {
00128                 RETERR(isc_lex_getmastertoken(lexer, &token,
00129                                               isc_tokentype_string, ISC_TRUE));
00130                 if (token.type != isc_tokentype_string)
00131                         break;
00132 
00133                 /*
00134                  * Lowercase the service string as some getservbyname() are
00135                  * case sensitive and the database is usually in lowercase.
00136                  */
00137                 strncpy(service, DNS_AS_STR(token), sizeof(service));
00138                 service[sizeof(service)-1] = '\0';
00139                 for (i = strlen(service) - 1; i >= 0; i--)
00140                         if (isupper(service[i]&0xff))
00141                                 service[i] = tolower(service[i]&0xff);
00142 
00143                 port = strtol(DNS_AS_STR(token), &e, 10);
00144                 if (*e == 0)
00145                         ;
00146                 else if (!mygetservbyname(service, ps, &port) &&
00147                          !mygetservbyname(DNS_AS_STR(token), ps, &port))
00148                         RETTOK(DNS_R_UNKNOWNSERVICE);
00149                 if (port < 0 || port > 0xffff)
00150                         RETTOK(ISC_R_RANGE);
00151                 if (port > maxport)
00152                         maxport = port;
00153                 bm[port / 8] |= (0x80 >> (port % 8));
00154         } while (1);
00155 
00156         /*
00157          * Let upper layer handle eol/eof.
00158          */
00159         isc_lex_ungettoken(lexer, &token);
00160 
00161         n = (maxport + 8) / 8;
00162         return (mem_tobuffer(target, bm, n));
00163 }
00164 
00165 static inline isc_result_t
00166 totext_in_wks(ARGS_TOTEXT) {
00167         isc_region_t sr;
00168         unsigned short proto;
00169         char buf[sizeof("65535")];
00170         unsigned int i, j;
00171 
00172         UNUSED(tctx);
00173 
00174         REQUIRE(rdata->type == 11);
00175         REQUIRE(rdata->rdclass == 1);
00176         REQUIRE(rdata->length >= 5);
00177 
00178         dns_rdata_toregion(rdata, &sr);
00179         RETERR(inet_totext(AF_INET, &sr, target));
00180         isc_region_consume(&sr, 4);
00181 
00182         proto = uint8_fromregion(&sr);
00183         sprintf(buf, "%u", proto);
00184         RETERR(str_totext(" ", target));
00185         RETERR(str_totext(buf, target));
00186         isc_region_consume(&sr, 1);
00187 
00188         INSIST(sr.length <= 8*1024);
00189         for (i = 0; i < sr.length; i++) {
00190                 if (sr.base[i] != 0)
00191                         for (j = 0; j < 8; j++)
00192                                 if ((sr.base[i] & (0x80 >> j)) != 0) {
00193                                         sprintf(buf, "%u", i * 8 + j);
00194                                         RETERR(str_totext(" ", target));
00195                                         RETERR(str_totext(buf, target));
00196                                 }
00197         }
00198 
00199         return (ISC_R_SUCCESS);
00200 }
00201 
00202 static inline isc_result_t
00203 fromwire_in_wks(ARGS_FROMWIRE) {
00204         isc_region_t sr;
00205         isc_region_t tr;
00206 
00207         REQUIRE(type == 11);
00208         REQUIRE(rdclass == 1);
00209 
00210         UNUSED(type);
00211         UNUSED(dctx);
00212         UNUSED(options);
00213         UNUSED(rdclass);
00214 
00215         isc_buffer_activeregion(source, &sr);
00216         isc_buffer_availableregion(target, &tr);
00217 
00218         if (sr.length < 5)
00219                 return (ISC_R_UNEXPECTEDEND);
00220         if (sr.length > 8 * 1024 + 5)
00221                 return (DNS_R_EXTRADATA);
00222         if (tr.length < sr.length)
00223                 return (ISC_R_NOSPACE);
00224 
00225         memmove(tr.base, sr.base, sr.length);
00226         isc_buffer_add(target, sr.length);
00227         isc_buffer_forward(source, sr.length);
00228 
00229         return (ISC_R_SUCCESS);
00230 }
00231 
00232 static inline isc_result_t
00233 towire_in_wks(ARGS_TOWIRE) {
00234         isc_region_t sr;
00235 
00236         UNUSED(cctx);
00237 
00238         REQUIRE(rdata->type == 11);
00239         REQUIRE(rdata->rdclass == 1);
00240         REQUIRE(rdata->length != 0);
00241 
00242         dns_rdata_toregion(rdata, &sr);
00243         return (mem_tobuffer(target, sr.base, sr.length));
00244 }
00245 
00246 static inline int
00247 compare_in_wks(ARGS_COMPARE) {
00248         isc_region_t r1;
00249         isc_region_t r2;
00250 
00251         REQUIRE(rdata1->type == rdata2->type);
00252         REQUIRE(rdata1->rdclass == rdata2->rdclass);
00253         REQUIRE(rdata1->type == 11);
00254         REQUIRE(rdata1->rdclass == 1);
00255         REQUIRE(rdata1->length != 0);
00256         REQUIRE(rdata2->length != 0);
00257 
00258         dns_rdata_toregion(rdata1, &r1);
00259         dns_rdata_toregion(rdata2, &r2);
00260         return (isc_region_compare(&r1, &r2));
00261 }
00262 
00263 static inline isc_result_t
00264 fromstruct_in_wks(ARGS_FROMSTRUCT) {
00265         dns_rdata_in_wks_t *wks = source;
00266         isc_uint32_t a;
00267 
00268         REQUIRE(type == 11);
00269         REQUIRE(rdclass == 1);
00270         REQUIRE(source != NULL);
00271         REQUIRE(wks->common.rdtype == type);
00272         REQUIRE(wks->common.rdclass == rdclass);
00273         REQUIRE((wks->map != NULL && wks->map_len <= 8*1024) ||
00274                  wks->map_len == 0);
00275 
00276         UNUSED(type);
00277         UNUSED(rdclass);
00278 
00279         a = ntohl(wks->in_addr.s_addr);
00280         RETERR(uint32_tobuffer(a, target));
00281         RETERR(uint8_tobuffer(wks->protocol, target));
00282         return (mem_tobuffer(target, wks->map, wks->map_len));
00283 }
00284 
00285 static inline isc_result_t
00286 tostruct_in_wks(ARGS_TOSTRUCT) {
00287         dns_rdata_in_wks_t *wks = target;
00288         isc_uint32_t n;
00289         isc_region_t region;
00290 
00291         REQUIRE(rdata->type == 11);
00292         REQUIRE(rdata->rdclass == 1);
00293         REQUIRE(rdata->length != 0);
00294 
00295         wks->common.rdclass = rdata->rdclass;
00296         wks->common.rdtype = rdata->type;
00297         ISC_LINK_INIT(&wks->common, link);
00298 
00299         dns_rdata_toregion(rdata, &region);
00300         n = uint32_fromregion(&region);
00301         wks->in_addr.s_addr = htonl(n);
00302         isc_region_consume(&region, 4);
00303         wks->protocol = uint8_fromregion(&region);
00304         isc_region_consume(&region, 1);
00305         wks->map_len = region.length;
00306         wks->map = mem_maybedup(mctx, region.base, region.length);
00307         if (wks->map == NULL)
00308                 return (ISC_R_NOMEMORY);
00309         wks->mctx = mctx;
00310         return (ISC_R_SUCCESS);
00311 }
00312 
00313 static inline void
00314 freestruct_in_wks(ARGS_FREESTRUCT) {
00315         dns_rdata_in_wks_t *wks = source;
00316 
00317         REQUIRE(source != NULL);
00318         REQUIRE(wks->common.rdtype == 11);
00319         REQUIRE(wks->common.rdclass == 1);
00320 
00321         if (wks->mctx == NULL)
00322                 return;
00323 
00324         if (wks->map != NULL)
00325                 isc_mem_free(wks->mctx, wks->map);
00326         wks->mctx = NULL;
00327 }
00328 
00329 static inline isc_result_t
00330 additionaldata_in_wks(ARGS_ADDLDATA) {
00331         UNUSED(rdata);
00332         UNUSED(add);
00333         UNUSED(arg);
00334 
00335         REQUIRE(rdata->type == 11);
00336         REQUIRE(rdata->rdclass == 1);
00337 
00338         return (ISC_R_SUCCESS);
00339 }
00340 
00341 static inline isc_result_t
00342 digest_in_wks(ARGS_DIGEST) {
00343         isc_region_t r;
00344 
00345         REQUIRE(rdata->type == 11);
00346         REQUIRE(rdata->rdclass == 1);
00347 
00348         dns_rdata_toregion(rdata, &r);
00349 
00350         return ((digest)(arg, &r));
00351 }
00352 
00353 static inline isc_boolean_t
00354 checkowner_in_wks(ARGS_CHECKOWNER) {
00355 
00356         REQUIRE(type == 11);
00357         REQUIRE(rdclass == 1);
00358 
00359         UNUSED(type);
00360         UNUSED(rdclass);
00361 
00362         return (dns_name_ishostname(name, wildcard));
00363 }
00364 
00365 static inline isc_boolean_t
00366 checknames_in_wks(ARGS_CHECKNAMES) {
00367 
00368         REQUIRE(rdata->type == 11);
00369         REQUIRE(rdata->rdclass == 1);
00370 
00371         UNUSED(rdata);
00372         UNUSED(owner);
00373         UNUSED(bad);
00374 
00375         return (ISC_TRUE);
00376 }
00377 
00378 static inline int
00379 casecompare_in_wks(ARGS_COMPARE) {
00380         return (compare_in_wks(rdata1, rdata2));
00381 }
00382 
00383 #endif  /* RDATA_IN_1_WKS_11_C */

Generated on Tue Apr 28 17:41:00 2015 by Doxygen 1.5.4 for BIND9 Internals 9.11.0pre-alpha