From a98551ef592e9be6008e0141ceeb32efd586c5ef Mon Sep 17 00:00:00 2001 From: Mark Andrews Date: Wed, 20 Jan 1999 06:51:30 +0000 Subject: [PATCH] Implemented dns_rdatatype_fromtext(), dns_rdatatype_totext(), dns_rdataclass_fromtext() and dns_rdataclass_totext(). --- bin/tests/rdata_test.c | 31 +++++++++++++--- lib/dns/gen.c | 27 ++++++++++---- lib/dns/rdata.c | 84 +++++++++++++++++++++++++++++++++++++++++- 3 files changed, 127 insertions(+), 15 deletions(-) diff --git a/bin/tests/rdata_test.c b/bin/tests/rdata_test.c index 65521888b3..78bd9e73ac 100644 --- a/bin/tests/rdata_test.c +++ b/bin/tests/rdata_test.c @@ -27,6 +27,8 @@ #include #include #include +#include +#include isc_mem_t *mctx; isc_lex_t *lex; @@ -42,7 +44,7 @@ main(int argc, char *argv[]) { int stats = 0; unsigned int options = 0; unsigned int parens = 0; - int type; + dns_rdatatype_t type; char outbuf[1024]; char inbuf[1024]; char wirebuf[1024]; @@ -134,14 +136,31 @@ main(int argc, char *argv[]) { continue; } - if (token.type != isc_tokentype_number) + if (token.type == isc_tokentype_number) { + type = token.value.as_ulong; + isc_buffer_init(&tbuf, outbuf, sizeof(outbuf), + ISC_BUFFERTYPE_TEXT); + result = dns_rdatatype_totext(type, &tbuf); + fprintf(stdout, "type = %.*s(%d)\n", + (int)tbuf.used, (char*)tbuf.base, type); + } else if (token.type == isc_tokentype_string) { + result = dns_rdatatype_fromtext(&type, + &token.value.as_textregion); + if (result != DNS_R_SUCCESS) { + fprintf(stdout, + "dns_rdatatype_fromtext returned %s(%d)\n", + dns_result_totext(result), result); + fflush(stdout); + continue; + } + fprintf(stdout, "type = %.*s(%d)\n", + (int)token.value.as_textregion.length, + token.value.as_textregion.base, type); + } else continue; - dns_rdata_init(&rdata); - - type = token.value.as_ulong; - fprintf(stdout, "type = %d\n", type); fflush(stdout); + dns_rdata_init(&rdata); isc_buffer_init(&dbuf, inbuf, sizeof(inbuf), ISC_BUFFERTYPE_BINARY); result = dns_rdata_fromtext(&rdata, 1, type, lex, diff --git a/lib/dns/gen.c b/lib/dns/gen.c index bbcc2cf2cf..3c5fe3586d 100644 --- a/lib/dns/gen.c +++ b/lib/dns/gen.c @@ -15,7 +15,7 @@ * SOFTWARE. */ - /* $Id: gen.c,v 1.7 1999/01/20 05:20:18 marka Exp $ */ + /* $Id: gen.c,v 1.8 1999/01/20 06:51:29 marka Exp $ */ #include @@ -93,7 +93,8 @@ char copyright[] = struct cc { struct cc *next; - int type; + int class; + char classname[11]; } *classes; struct tt { @@ -241,16 +242,17 @@ add(int class, char *classname, int type, char *typename, char *dirname) { return; newcc = (struct cc *)malloc(sizeof *newcc); - newcc->type = type; + newcc->class = class; + strcpy(newcc->classname, classname); cc = classes; oldcc = NULL; - while ((cc != NULL) && (cc->type < type)) { + while ((cc != NULL) && (cc->class < class)) { oldcc = cc; cc = cc->next; } - if ((cc != NULL) && cc->type == type) { + if ((cc != NULL) && cc->class == class) { free((char *)newcc); return; } @@ -296,6 +298,7 @@ main(int argc, char **argv) { char classname[11]; struct dirent *dp; struct tt *tt; + struct cc *cc; struct tm *tm; time_t now; char year[11]; @@ -361,13 +364,21 @@ main(int argc, char **argv) { lasttype = 0; for (tt = types; tt != NULL ; tt = tt->next) if (tt->type != lasttype) - fprintf(stdout, - "\t{ %d, \"%s\" },%s\n", + fprintf(stdout, "\t{ %d, \"%s\" },%s\n", lasttype = tt->type, tt->typename, tt->next != NULL ? " \\" : ""); fputs("\n", stdout); - for (tt = types; tt ; tt = tt->next) + fprintf(stdout, "\n#define CLASSNAMES%s\n", + classes != NULL ? " \\" : ""); + + for (cc = classes; cc != NULL; cc = cc->next) + fprintf(stdout, "\t{ %d, \"%s\" },%s\n", cc->class, + cc->classname, cc->next != NULL ? " \\" : ""); + + + fputs("\n", stdout); + for (tt = types; tt != NULL ; tt = tt->next) fprintf(stdout, "#include \"%s/%s_%d.h\"\n", tt->dirname, tt->typename, tt->type); diff --git a/lib/dns/rdata.c b/lib/dns/rdata.c index 5e5ba8c9f1..096da35080 100644 --- a/lib/dns/rdata.c +++ b/lib/dns/rdata.c @@ -15,7 +15,7 @@ * SOFTWARE. */ - /* $Id: rdata.c,v 1.5 1999/01/20 05:20:18 marka Exp $ */ + /* $Id: rdata.c,v 1.6 1999/01/20 06:51:30 marka Exp $ */ #include #include @@ -23,6 +23,8 @@ #include #include #include +#include +#include #include #include @@ -48,6 +50,10 @@ static unsigned short uint16_fromregion(isc_region_t *region); #include "code.h" +struct tbl { + int value; + char *name; +} types[] = { TYPENAMES {0, NULL} }, classes[] = { CLASSNAMES { 0, NULL} }; /*** *** Initialization ***/ @@ -281,6 +287,82 @@ dns_rdata_tostruct(dns_rdata_t *rdata, void *target) { return (result); } +dns_result_t +dns_rdataclass_fromtext(dns_rdataclass_t *classp, isc_textregion_t *source) { + int i = 0; + unsigned int n; + + while (classes[i].name != NULL) { + n = strlen(classes[i].name); + if (n == source->length && + strncasecmp(source->base, classes[i].name, n) == 0) { + *classp = classes[i].value; + return(DNS_R_SUCCESS); + } + i++; + } + return (DNS_R_UNKNOWN); +} + +dns_result_t +dns_rdataclass_totext(dns_rdataclass_t class, isc_buffer_t *target) { + int i = 0; + unsigned int n; + isc_region_t region; + + while (classes[i].name != NULL) { + if (classes[i].value == class) { + isc_buffer_available(target, ®ion); + if ((n = strlen(classes[i].name)) > region.length) + return (DNS_R_NOSPACE); + memcpy(region.base, classes[i].name, n); + isc_buffer_add(target, n); + return(DNS_R_SUCCESS); + } + i++; + } + return (DNS_R_UNKNOWN); +} + +dns_result_t +dns_rdatatype_fromtext(dns_rdatatype_t *typep, isc_textregion_t *source) { + int i = 0; + unsigned int n; + + while (types[i].name != NULL) { + n = strlen(types[i].name); + if (n == source->length && + strncasecmp(source->base, types[i].name, n) == 0) { + *typep = types[i].value; + return(DNS_R_SUCCESS); + } + i++; + } + return (DNS_R_UNKNOWN); +} + +dns_result_t +dns_rdatatype_totext(dns_rdatatype_t type, isc_buffer_t *target) { + int i = 0; + unsigned int n; + isc_region_t region; + + while (types[i].name != NULL) { + if (types[i].value == type) { + isc_buffer_available(target, ®ion); + if ((n = strlen(types[i].name)) > region.length) + return (DNS_R_NOSPACE); + memcpy(region.base, types[i].name, n); + isc_buffer_add(target, n); + return(DNS_R_SUCCESS); + } + i++; + } + return (DNS_R_UNKNOWN); +} + + /* Private function */ + static unsigned int name_length(dns_name_t *name) { return (name->length);