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