diff options
author | Linus Nordberg <linus@nordu.net> | 2016-03-28 12:05:52 +0200 |
---|---|---|
committer | Linus Nordberg <linus@nordu.net> | 2016-03-28 12:05:52 +0200 |
commit | 46a9ea323cc28100988db8762303be853bf40e98 (patch) | |
tree | 8e8e4fe04f059d25497d0e1f0494fee8924e8b44 | |
parent | 56d70baa79ae5907b11445364bbea9b31ee4cd20 (diff) |
Use a list of dicts; return errcode, not -errcode.
-rw-r--r-- | tools/dnssec/dns-wire2text.c | 137 |
1 files changed, 79 insertions, 58 deletions
diff --git a/tools/dnssec/dns-wire2text.c b/tools/dnssec/dns-wire2text.c index 8e4b55d..896fc6e 100644 --- a/tools/dnssec/dns-wire2text.c +++ b/tools/dnssec/dns-wire2text.c @@ -1,6 +1,6 @@ /* Read RR's in getdns wire format and print them in presentation - format on stdout. + format. */ #include <stdio.h> @@ -34,28 +34,13 @@ hd(const char *buf, size_t buf_len) #define hd(a,b) #endif -/* Return value: - <0 -- error, the return value being -errorcode - 0 -- done - >0 -- not done yet, call me again -*/ -static int -read_rr(const uint8_t **buf, size_t *buf_len, getdns_dict **rr_dict) -{ - getdns_return_t r = getdns_wire2rr_dict_scan(buf, buf_len, rr_dict); - - if (r) - return -r; - return *buf_len; -} - -#define INBUFLEN 4096 +#define CHUNKSIZE 4096 static size_t read_inbuf(FILE *infp, uint8_t **bufp_out) { size_t nread = 0; - uint8_t *wirebuf = malloc(INBUFLEN); + uint8_t *wirebuf = malloc(CHUNKSIZE); int chunks = 1; if (wirebuf == NULL) @@ -63,12 +48,12 @@ read_inbuf(FILE *infp, uint8_t **bufp_out) while (1) { - size_t n = fread(wirebuf + nread, 1, INBUFLEN, infp); + size_t n = fread(wirebuf + nread, 1, CHUNKSIZE, infp); nread += n; - if (n < INBUFLEN) + if (n < CHUNKSIZE) break; /* Done. */ - wirebuf = realloc(wirebuf, ++chunks * INBUFLEN); + wirebuf = realloc(wirebuf, ++chunks * CHUNKSIZE); if (wirebuf == NULL) break; } @@ -79,71 +64,107 @@ read_inbuf(FILE *infp, uint8_t **bufp_out) return nread; } +static getdns_return_t +wire_rrs2list(const uint8_t *buf, size_t buf_len, getdns_list **list_out) +{ + getdns_return_t r = GETDNS_RETURN_GOOD; + getdns_list *list = getdns_list_create(); + getdns_dict *dict = NULL; + size_t rr_count = 0; + + if (list == NULL) + return GETDNS_RETURN_MEMORY_ERROR; + while (buf_len > 0) + { + r = getdns_wire2rr_dict_scan(&buf, &buf_len, &dict); + if (r) + break; + r = getdns_list_set_dict(list, rr_count, dict); + getdns_dict_destroy(dict); /* The list has a copy. */ + if (r) + break; + rr_count++; + } + + if (list_out) + *list_out = list; + return r; +} + int main(int argc, char *argv[]) { - int rrv = 0; - uint8_t *inbuf = NULL; - const uint8_t *bufp = NULL; - size_t inbuf_len = 0; - getdns_dict *rr_dict = NULL; - getdns_return_t r = 0; + /* Read from file in argv[1] or from stdin. */ FILE *infp = stdin; - if (argc > 1) { infp = fopen(argv[1], "r"); if (infp == NULL) { perror("fopen(argv[1])"); - return -errno; + return errno; } } - inbuf_len = read_inbuf(infp, &inbuf); + + /* Read RRs in wire format. */ + uint8_t *inbuf = NULL; + size_t inbuf_len = read_inbuf(infp, &inbuf); if (inbuf == NULL) { perror("read_inbuf"); - return -errno; + return errno; } if (infp != stdin) if (fclose(infp)) perror("fclose"); - hd((const char *) wirebuf, n); + hd((const char *) wirebuf, n); /* DEBUG */ - bufp = inbuf; - while (1) + /* Convert to getdns dicts in a getdns list. */ + getdns_return_t r = 0; + getdns_list *rrs_list = NULL; + r = wire_rrs2list(inbuf, inbuf_len, &rrs_list); + free(inbuf); + if (r) { - char *stringbuf = NULL; + fprintf(stderr, "error converting input: %s\n", + getdns_get_errorstr_by_id(r)); + getdns_list_destroy(rrs_list); + return r; + } - rrv = read_rr(&bufp, &inbuf_len, &rr_dict); - if (rrv < 0) - break; + /* Print dicts in list. */ + size_t rrs_list_len; + r = getdns_list_get_length(rrs_list, &rrs_list_len); + if (r) + { + fprintf(stderr, "error getting list length: %s\n", + getdns_get_errorstr_by_id(r)); + return r; + } + for (size_t i = 0; i < rrs_list_len; i++) + { + char *stringbuf = NULL; + getdns_dict *rr_dict = NULL; + r = getdns_list_get_dict(rrs_list, i, &rr_dict); + if (r) + { + fprintf(stderr, "error getting dict from list: %s\n", + getdns_get_errorstr_by_id(r)); + break; + } r = getdns_rr_dict2str(rr_dict, &stringbuf); if (r) - break; + { + fprintf(stderr, "error converting dict to string: %s\n", + getdns_get_errorstr_by_id(r)); + break; + } - getdns_dict_destroy(rr_dict); printf("%s", stringbuf); free(stringbuf); - - if (rrv == 0) - break; /* Done. */ - } - free(inbuf); - - if (rrv < 0) - { - fprintf(stderr, "parsing input failed: %s\n", - getdns_get_errorstr_by_id(-rrv)); - return rrv; - } - if (r) - { - fprintf(stderr, "converting dict to string failed: %s\n", - getdns_get_errorstr_by_id(r)); - return -r; } + getdns_list_destroy(rrs_list); - return 0; + return r; } |