2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-22 10:10:06 +00:00
bind/bin/tests/system/wire-test.c

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

351 lines
7.7 KiB
C
Raw Normal View History

1999-01-06 05:42:58 +00:00
/*
* Copyright (C) Internet Systems Consortium, Inc. ("ISC")
1999-01-06 05:42:58 +00:00
*
* SPDX-License-Identifier: MPL-2.0
*
1999-01-06 05:42:58 +00:00
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at https://mozilla.org/MPL/2.0/.
*
1999-01-06 05:42:58 +00:00
* See the COPYRIGHT file distributed with this work for additional
2001-05-09 18:51:44 +00:00
* information regarding copyright ownership.
1999-01-06 05:42:58 +00:00
*/
#include <inttypes.h>
#include <stdbool.h>
1999-01-06 05:42:58 +00:00
#include <stdlib.h>
2000-05-30 23:20:18 +00:00
#include <isc/buffer.h>
2001-05-09 18:51:44 +00:00
#include <isc/commandline.h>
#include <isc/lib.h>
#include <isc/mem.h>
#include <isc/result.h>
#include <isc/string.h>
2000-04-28 02:12:16 +00:00
#include <isc/util.h>
1999-01-06 05:42:58 +00:00
#include <dns/lib.h>
#include <dns/message.h>
1999-01-13 19:11:26 +00:00
int parseflags = 0;
bool printmemstats = false;
bool dorender = false;
static void
process_message(isc_buffer_t *source);
static isc_result_t
printmessage(dns_message_t *msg);
static void
CHECKRESULT(isc_result_t result, const char *msg) {
if (result != ISC_R_SUCCESS) {
printf("%s: %s\n", msg, isc_result_totext(result));
1999-04-30 00:17:15 +00:00
exit(EXIT_FAILURE);
1999-04-30 00:17:15 +00:00
}
}
1999-01-06 05:42:58 +00:00
static int
fromhex(char c) {
if (c >= '0' && c <= '9') {
return c - '0';
} else if (c >= 'a' && c <= 'f') {
return c - 'a' + 10;
} else if (c >= 'A' && c <= 'F') {
return c - 'A' + 10;
}
1999-02-11 06:38:12 +00:00
fprintf(stderr, "bad input format: %02x\n", c);
1999-01-06 05:42:58 +00:00
exit(3);
}
1999-01-13 19:11:26 +00:00
2001-05-09 18:51:44 +00:00
static void
usage(void) {
fprintf(stderr, "wire_test [-b] [-d] [-p] [-r] [-s]\n");
fprintf(stderr, " [-m {usage|trace|record}]\n");
fprintf(stderr, " [filename]\n\n");
2001-05-09 18:51:44 +00:00
fprintf(stderr, "\t-b\tBest-effort parsing (ignore some errors)\n");
fprintf(stderr, "\t-d\tRead input as raw binary data\n");
fprintf(stderr, "\t-p\tPreserve order of the records in messages\n");
2001-05-09 18:51:44 +00:00
fprintf(stderr, "\t-r\tAfter parsing, re-render the message\n");
fprintf(stderr, "\t-s\tPrint memory statistics\n");
fprintf(stderr, "\t-t\tTCP mode - ignore the first 2 bytes\n");
2001-05-09 18:51:44 +00:00
}
static isc_result_t
printmessage(dns_message_t *msg) {
isc_buffer_t b;
char *buf = NULL;
int len = 1024;
isc_result_t result = ISC_R_SUCCESS;
do {
buf = isc_mem_get(isc_g_mctx, len);
isc_buffer_init(&b, buf, len);
result = dns_message_totext(msg, &dns_master_style_debug, 0,
&b);
if (result == ISC_R_NOSPACE) {
isc_mem_put(isc_g_mctx, buf, len);
len *= 2;
} else if (result == ISC_R_SUCCESS) {
printf("%.*s\n", (int)isc_buffer_usedlength(&b), buf);
}
} while (result == ISC_R_NOSPACE);
if (buf != NULL) {
isc_mem_put(isc_g_mctx, buf, len);
}
return result;
}
1999-01-06 05:42:58 +00:00
int
main(int argc, char *argv[]) {
isc_buffer_t *input = NULL;
bool need_close = false;
bool tcp = false;
bool rawdata = false;
isc_result_t result;
uint8_t c;
FILE *f;
2001-05-09 18:51:44 +00:00
int ch;
1999-04-30 00:17:15 +00:00
#define CMDLINE_FLAGS "bdm:prst"
/*
* Process memory debugging argument first.
*/
while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
switch (ch) {
case 'm':
if (strcasecmp(isc_commandline_argument, "record") == 0)
{
isc_mem_debugon(ISC_MEM_DEBUGRECORD);
}
if (strcasecmp(isc_commandline_argument, "trace") == 0)
{
isc_mem_debugon(ISC_MEM_DEBUGTRACE);
}
if (strcasecmp(isc_commandline_argument, "usage") == 0)
{
isc_mem_debugon(ISC_MEM_DEBUGUSAGE);
}
break;
default:
break;
}
}
isc_commandline_reset = true;
while ((ch = isc_commandline_parse(argc, argv, CMDLINE_FLAGS)) != -1) {
2001-05-09 18:51:44 +00:00
switch (ch) {
case 'b':
parseflags |= DNS_MESSAGEPARSE_BESTEFFORT;
break;
case 'd':
rawdata = true;
break;
case 'm':
break;
case 'p':
parseflags |= DNS_MESSAGEPARSE_PRESERVEORDER;
2001-05-09 18:51:44 +00:00
break;
case 'r':
dorender = true;
2001-05-09 18:51:44 +00:00
break;
case 's':
printmemstats = true;
break;
case 't':
tcp = true;
break;
2001-05-09 18:51:44 +00:00
default:
usage();
exit(EXIT_FAILURE);
2001-05-09 18:51:44 +00:00
}
}
argc -= isc_commandline_index;
argv += isc_commandline_index;
if (argc >= 1) {
f = fopen(argv[0], "r");
1999-01-06 05:42:58 +00:00
if (f == NULL) {
fprintf(stderr, "%s: fopen failed\n", argv[0]);
exit(EXIT_FAILURE);
1999-01-06 05:42:58 +00:00
}
need_close = true;
1999-01-06 05:42:58 +00:00
} else {
f = stdin;
}
1999-01-06 05:42:58 +00:00
isc_buffer_allocate(isc_g_mctx, &input, 64 * 1024);
if (rawdata) {
while (fread(&c, 1, 1, f) != 0) {
result = isc_buffer_reserve(input, 1);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
isc_buffer_putuint8(input, (uint8_t)c);
}
} else {
char s[BUFSIZ];
while (fgets(s, sizeof(s), f) != NULL) {
char *rp = s, *wp = s;
size_t i, len = 0;
while (*rp != '\0') {
if (*rp == '#') {
break;
}
if (*rp != ' ' && *rp != '\t' && *rp != '\r' &&
2022-11-02 19:33:14 +01:00
*rp != '\n')
{
*wp++ = *rp;
len++;
}
rp++;
}
if (len == 0U) {
continue;
}
if (len % 2 != 0U) {
fprintf(stderr, "bad input format: %lu\n",
(unsigned long)len);
exit(EXIT_FAILURE);
}
rp = s;
for (i = 0; i < len; i += 2) {
c = fromhex(*rp++);
c *= 16;
c += fromhex(*rp++);
result = isc_buffer_reserve(input, 1);
RUNTIME_CHECK(result == ISC_R_SUCCESS);
isc_buffer_putuint8(input, (uint8_t)c);
1999-01-13 19:57:13 +00:00
}
1999-01-06 05:42:58 +00:00
}
}
if (need_close) {
fclose(f);
}
1999-01-06 20:05:09 +00:00
2001-05-09 18:59:55 +00:00
if (tcp) {
while (isc_buffer_remaininglength(input) != 0) {
2015-08-11 12:59:34 +10:00
unsigned int tcplen;
2015-05-23 23:45:25 +00:00
if (isc_buffer_remaininglength(input) < 2) {
fprintf(stderr, "premature end of packet\n");
exit(EXIT_FAILURE);
}
tcplen = isc_buffer_getuint16(input);
if (isc_buffer_remaininglength(input) < tcplen) {
fprintf(stderr, "premature end of packet\n");
exit(EXIT_FAILURE);
}
process_message(input);
}
} else {
process_message(input);
}
isc_buffer_free(&input);
if (printmemstats) {
isc_mem_stats(isc_g_mctx, stdout);
}
return 0;
}
static void
process_message(isc_buffer_t *source) {
dns_message_t *message;
isc_result_t result;
int i;
1999-05-12 19:47:43 +00:00
message = NULL;
dns_message_create(isc_g_mctx, NULL, NULL, DNS_MESSAGE_INTENTPARSE,
&message);
1999-04-30 00:17:15 +00:00
result = dns_message_parse(message, source, parseflags);
2001-05-09 18:51:44 +00:00
if (result == DNS_R_RECOVERABLE) {
result = ISC_R_SUCCESS;
}
2001-05-09 18:51:44 +00:00
CHECKRESULT(result, "dns_message_parse failed");
1999-04-30 00:17:15 +00:00
result = printmessage(message);
CHECKRESULT(result, "printmessage() failed");
2001-05-09 18:51:44 +00:00
if (printmemstats) {
isc_mem_stats(isc_g_mctx, stdout);
}
1999-04-30 21:52:40 +00:00
2001-05-09 18:51:44 +00:00
if (dorender) {
unsigned char b2[65535];
isc_buffer_t buffer;
dns_compress_t cctx;
isc_buffer_init(&buffer, b2, sizeof(b2));
2001-05-09 18:51:44 +00:00
/*
* XXXMLG
* Changing this here is a hack, and should not be done in
* reasonable application code, ever.
*/
message->from_to_wire = DNS_MESSAGE_INTENTRENDER;
1999-04-30 21:52:40 +00:00
for (i = 0; i < DNS_SECTION_MAX; i++) {
2001-05-09 18:51:44 +00:00
message->counts[i] = 0; /* Another hack XXX */
}
1999-04-30 22:35:49 +00:00
dns_compress_init(&cctx, isc_g_mctx, 0);
result = dns_message_renderbegin(message, &cctx, &buffer);
2001-05-09 18:51:44 +00:00
CHECKRESULT(result, "dns_message_renderbegin() failed");
1999-04-30 21:52:40 +00:00
2001-05-09 18:51:44 +00:00
result = dns_message_rendersection(message,
DNS_SECTION_QUESTION, 0);
CHECKRESULT(result, "dns_message_rendersection(QUESTION) "
"failed");
1999-04-30 21:52:40 +00:00
2001-05-09 18:51:44 +00:00
result = dns_message_rendersection(message, DNS_SECTION_ANSWER,
0);
CHECKRESULT(result, "dns_message_rendersection(ANSWER) failed");
1999-04-30 21:52:40 +00:00
2001-05-09 18:51:44 +00:00
result = dns_message_rendersection(message,
DNS_SECTION_AUTHORITY, 0);
CHECKRESULT(result, "dns_message_rendersection(AUTHORITY) "
"failed");
1999-04-30 21:52:40 +00:00
2001-05-09 18:51:44 +00:00
result = dns_message_rendersection(message,
DNS_SECTION_ADDITIONAL, 0);
CHECKRESULT(result, "dns_message_rendersection(ADDITIONAL) "
"failed");
1999-04-30 21:52:40 +00:00
2001-05-09 18:51:44 +00:00
dns_message_renderend(message);
1999-04-30 21:52:40 +00:00
2001-05-09 18:51:44 +00:00
dns_compress_invalidate(&cctx);
2001-05-09 18:51:44 +00:00
message->from_to_wire = DNS_MESSAGE_INTENTPARSE;
dns_message_detach(&message);
1999-04-30 21:52:40 +00:00
2001-05-09 18:51:44 +00:00
printf("Message rendered.\n");
if (printmemstats) {
isc_mem_stats(isc_g_mctx, stdout);
}
1999-04-30 21:52:40 +00:00
dns_message_create(isc_g_mctx, NULL, NULL,
DNS_MESSAGE_INTENTPARSE, &message);
1999-04-30 21:52:40 +00:00
result = dns_message_parse(message, &buffer, parseflags);
2001-05-09 18:51:44 +00:00
CHECKRESULT(result, "dns_message_parse failed");
1999-04-30 21:52:40 +00:00
2001-05-09 18:51:44 +00:00
result = printmessage(message);
CHECKRESULT(result, "printmessage() failed");
}
dns_message_detach(&message);
1999-01-06 05:42:58 +00:00
}