wire_test.c revision 1.3
1/* $NetBSD: wire_test.c,v 1.3 2019/01/09 16:55:00 christos Exp $ */ 2 3/* 4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC") 5 * 6 * This Source Code Form is subject to the terms of the Mozilla Public 7 * License, v. 2.0. If a copy of the MPL was not distributed with this 8 * file, You can obtain one at http://mozilla.org/MPL/2.0/. 9 * 10 * See the COPYRIGHT file distributed with this work for additional 11 * information regarding copyright ownership. 12 */ 13 14#include <config.h> 15 16#include <inttypes.h> 17#include <stdbool.h> 18#include <stdlib.h> 19 20#include <isc/buffer.h> 21#include <isc/commandline.h> 22#include <isc/file.h> 23#include <isc/mem.h> 24#include <isc/print.h> 25#include <isc/string.h> 26#include <isc/util.h> 27 28#include <dns/message.h> 29#include <dns/result.h> 30 31int parseflags = 0; 32isc_mem_t *mctx = NULL; 33bool printmemstats = false; 34bool dorender = false; 35 36static void 37process_message(isc_buffer_t *source); 38 39static isc_result_t 40printmessage(dns_message_t *msg); 41 42static inline void 43CHECKRESULT(isc_result_t result, const char *msg) { 44 if (result != ISC_R_SUCCESS) { 45 printf("%s: %s\n", msg, dns_result_totext(result)); 46 47 exit(1); 48 } 49} 50 51static int 52fromhex(char c) { 53 if (c >= '0' && c <= '9') 54 return (c - '0'); 55 else if (c >= 'a' && c <= 'f') 56 return (c - 'a' + 10); 57 else if (c >= 'A' && c <= 'F') 58 return (c - 'A' + 10); 59 60 fprintf(stderr, "bad input format: %02x\n", c); 61 exit(3); 62 /* NOTREACHED */ 63} 64 65static void 66usage(void) { 67 fprintf(stderr, "wire_test [-b] [-d] [-p] [-r] [-s]\n"); 68 fprintf(stderr, " [-m {usage|trace|record|size|mctx}]\n"); 69 fprintf(stderr, " [filename]\n\n"); 70 fprintf(stderr, "\t-b\tBest-effort parsing (ignore some errors)\n"); 71 fprintf(stderr, "\t-d\tRead input as raw binary data\n"); 72 fprintf(stderr, "\t-p\tPreserve order of the records in messages\n"); 73 fprintf(stderr, "\t-r\tAfter parsing, re-render the message\n"); 74 fprintf(stderr, "\t-s\tPrint memory statistics\n"); 75 fprintf(stderr, "\t-t\tTCP mode - ignore the first 2 bytes\n"); 76} 77 78static isc_result_t 79printmessage(dns_message_t *msg) { 80 isc_buffer_t b; 81 char *buf = NULL; 82 int len = 1024; 83 isc_result_t result = ISC_R_SUCCESS; 84 85 do { 86 buf = isc_mem_get(mctx, len); 87 if (buf == NULL) { 88 result = ISC_R_NOMEMORY; 89 break; 90 } 91 92 isc_buffer_init(&b, buf, len); 93 result = dns_message_totext(msg, &dns_master_style_debug, 94 0, &b); 95 if (result == ISC_R_NOSPACE) { 96 isc_mem_put(mctx, buf, len); 97 len *= 2; 98 } else if (result == ISC_R_SUCCESS) 99 printf("%.*s\n", (int) isc_buffer_usedlength(&b), buf); 100 } while (result == ISC_R_NOSPACE); 101 102 if (buf != NULL) 103 isc_mem_put(mctx, buf, len); 104 105 return (result); 106} 107 108int 109main(int argc, char *argv[]) { 110 isc_buffer_t *input = NULL; 111 bool need_close = false; 112 bool tcp = false; 113 bool rawdata = false; 114 isc_result_t result; 115 uint8_t c; 116 FILE *f; 117 int ch; 118 119#define CMDLINE_FLAGS "bdm:prst" 120 /* 121 * Process memory debugging argument first. 122 */ 123 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) { 124 switch (ch) { 125 case 'm': 126 if (strcasecmp(isc_commandline_argument, "record") == 0) 127 isc_mem_debugging |= ISC_MEM_DEBUGRECORD; 128 if (strcasecmp(isc_commandline_argument, "trace") == 0) 129 isc_mem_debugging |= ISC_MEM_DEBUGTRACE; 130 if (strcasecmp(isc_commandline_argument, "usage") == 0) 131 isc_mem_debugging |= ISC_MEM_DEBUGUSAGE; 132 if (strcasecmp(isc_commandline_argument, "size") == 0) 133 isc_mem_debugging |= ISC_MEM_DEBUGSIZE; 134 if (strcasecmp(isc_commandline_argument, "mctx") == 0) 135 isc_mem_debugging |= ISC_MEM_DEBUGCTX; 136 break; 137 default: 138 break; 139 } 140 } 141 isc_commandline_reset = true; 142 143 RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS); 144 145 while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) { 146 switch (ch) { 147 case 'b': 148 parseflags |= DNS_MESSAGEPARSE_BESTEFFORT; 149 break; 150 case 'd': 151 rawdata = true; 152 break; 153 case 'm': 154 break; 155 case 'p': 156 parseflags |= DNS_MESSAGEPARSE_PRESERVEORDER; 157 break; 158 case 'r': 159 dorender = true; 160 break; 161 case 's': 162 printmemstats = true; 163 break; 164 case 't': 165 tcp = true; 166 break; 167 default: 168 usage(); 169 exit(1); 170 } 171 } 172 173 argc -= isc_commandline_index; 174 argv += isc_commandline_index; 175 176 if (argc >= 1) { 177 f = fopen(argv[0], "r"); 178 if (f == NULL) { 179 fprintf(stderr, "%s: fopen failed\n", argv[0]); 180 exit(1); 181 } 182 need_close = true; 183 } else 184 f = stdin; 185 186 result = isc_buffer_allocate(mctx, &input, 64 * 1024); 187 RUNTIME_CHECK(result == ISC_R_SUCCESS); 188 189 if (rawdata) { 190 while (fread(&c, 1, 1, f) != 0) { 191 result = isc_buffer_reserve(&input, 1); 192 RUNTIME_CHECK(result == ISC_R_SUCCESS); 193 isc_buffer_putuint8(input, (uint8_t) c); 194 } 195 } else { 196 char s[BUFSIZ]; 197 198 while (fgets(s, sizeof(s), f) != NULL) { 199 char *rp = s, *wp = s; 200 size_t i, len = 0; 201 202 while (*rp != '\0') { 203 if (*rp == '#') 204 break; 205 if (*rp != ' ' && *rp != '\t' && 206 *rp != '\r' && *rp != '\n') { 207 *wp++ = *rp; 208 len++; 209 } 210 rp++; 211 } 212 if (len == 0U) 213 continue; 214 if (len % 2 != 0U) { 215 fprintf(stderr, "bad input format: %lu\n", 216 (unsigned long)len); 217 exit(1); 218 } 219 220 rp = s; 221 for (i = 0; i < len; i += 2) { 222 c = fromhex(*rp++); 223 c *= 16; 224 c += fromhex(*rp++); 225 result = isc_buffer_reserve(&input, 1); 226 RUNTIME_CHECK(result == ISC_R_SUCCESS); 227 isc_buffer_putuint8(input, (uint8_t) c); 228 } 229 } 230 } 231 232 if (need_close) 233 fclose(f); 234 235 if (tcp) { 236 while (isc_buffer_remaininglength(input) != 0) { 237 unsigned int tcplen; 238 239 if (isc_buffer_remaininglength(input) < 2) { 240 fprintf(stderr, "premature end of packet\n"); 241 exit(1); 242 } 243 tcplen = isc_buffer_getuint16(input); 244 245 if (isc_buffer_remaininglength(input) < tcplen) { 246 fprintf(stderr, "premature end of packet\n"); 247 exit(1); 248 } 249 process_message(input); 250 } 251 } else 252 process_message(input); 253 254 if (input != NULL) 255 isc_buffer_free(&input); 256 257 if (printmemstats) 258 isc_mem_stats(mctx, stdout); 259 isc_mem_destroy(&mctx); 260 261 return (0); 262} 263 264static void 265process_message(isc_buffer_t *source) { 266 dns_message_t *message; 267 isc_result_t result; 268 int i; 269 270 message = NULL; 271 result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &message); 272 CHECKRESULT(result, "dns_message_create failed"); 273 274 result = dns_message_parse(message, source, parseflags); 275 if (result == DNS_R_RECOVERABLE) 276 result = ISC_R_SUCCESS; 277 CHECKRESULT(result, "dns_message_parse failed"); 278 279 result = printmessage(message); 280 CHECKRESULT(result, "printmessage() failed"); 281 282 if (printmemstats) 283 isc_mem_stats(mctx, stdout); 284 285 if (dorender) { 286 unsigned char b2[64 * 1024]; 287 isc_buffer_t buffer; 288 dns_compress_t cctx; 289 290 isc_buffer_init(&buffer, b2, sizeof(b2)); 291 292 /* 293 * XXXMLG 294 * Changing this here is a hack, and should not be done in 295 * reasonable application code, ever. 296 */ 297 message->from_to_wire = DNS_MESSAGE_INTENTRENDER; 298 299 for (i = 0; i < DNS_SECTION_MAX; i++) 300 message->counts[i] = 0; /* Another hack XXX */ 301 302 result = dns_compress_init(&cctx, -1, mctx); 303 CHECKRESULT(result, "dns_compress_init() failed"); 304 305 result = dns_message_renderbegin(message, &cctx, &buffer); 306 CHECKRESULT(result, "dns_message_renderbegin() failed"); 307 308 result = dns_message_rendersection(message, 309 DNS_SECTION_QUESTION, 0); 310 CHECKRESULT(result, 311 "dns_message_rendersection(QUESTION) failed"); 312 313 result = dns_message_rendersection(message, 314 DNS_SECTION_ANSWER, 0); 315 CHECKRESULT(result, 316 "dns_message_rendersection(ANSWER) failed"); 317 318 result = dns_message_rendersection(message, 319 DNS_SECTION_AUTHORITY, 0); 320 CHECKRESULT(result, 321 "dns_message_rendersection(AUTHORITY) failed"); 322 323 result = dns_message_rendersection(message, 324 DNS_SECTION_ADDITIONAL, 0); 325 CHECKRESULT(result, 326 "dns_message_rendersection(ADDITIONAL) failed"); 327 328 dns_message_renderend(message); 329 330 dns_compress_invalidate(&cctx); 331 332 message->from_to_wire = DNS_MESSAGE_INTENTPARSE; 333 dns_message_destroy(&message); 334 335 printf("Message rendered.\n"); 336 if (printmemstats) 337 isc_mem_stats(mctx, stdout); 338 339 result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, 340 &message); 341 CHECKRESULT(result, "dns_message_create failed"); 342 343 result = dns_message_parse(message, &buffer, parseflags); 344 CHECKRESULT(result, "dns_message_parse failed"); 345 346 result = printmessage(message); 347 CHECKRESULT(result, "printmessage() failed"); 348 } 349 dns_message_destroy(&message); 350} 351