1999-01-06 05:42:58 +00:00
|
|
|
/*
|
2015-05-23 14:21:51 +02:00
|
|
|
* Copyright (C) 2004, 2005, 2007, 2015 Internet Systems Consortium, Inc. ("ISC")
|
2001-01-09 22:01:04 +00:00
|
|
|
* Copyright (C) 1999-2001 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2007-06-18 23:47:57 +00:00
|
|
|
* Permission to use, copy, modify, and/or distribute this software for any
|
1999-01-06 05:42:58 +00:00
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2004-03-05 05:14:21 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
|
|
|
|
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
|
|
|
* AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
|
|
|
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
|
|
|
|
* OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
|
|
|
* PERFORMANCE OF THIS SOFTWARE.
|
1999-01-06 05:42:58 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#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>
|
2015-08-17 22:41:44 -07:00
|
|
|
#include <isc/file.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/mem.h>
|
2015-05-23 14:21:51 +02:00
|
|
|
#include <isc/print.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-04-28 02:12:16 +00:00
|
|
|
#include <isc/util.h>
|
1999-01-06 05:42:58 +00:00
|
|
|
|
2015-08-10 20:51:22 -07:00
|
|
|
#include <dns/message.h>
|
1999-01-06 05:42:58 +00:00
|
|
|
#include <dns/result.h>
|
1999-01-13 19:11:26 +00:00
|
|
|
|
2001-09-29 18:23:56 +00:00
|
|
|
int parseflags = 0;
|
2015-08-17 22:41:44 -07:00
|
|
|
isc_mem_t *mctx = NULL;
|
2001-09-29 18:23:56 +00:00
|
|
|
isc_boolean_t printmemstats = ISC_FALSE;
|
|
|
|
isc_boolean_t dorender = ISC_FALSE;
|
|
|
|
|
|
|
|
static void
|
|
|
|
process_message(isc_buffer_t *source);
|
|
|
|
|
2015-08-10 20:51:22 -07:00
|
|
|
static isc_result_t
|
|
|
|
printmessage(dns_message_t *msg);
|
|
|
|
|
1999-04-30 00:17:15 +00:00
|
|
|
static inline void
|
2000-06-01 19:11:07 +00:00
|
|
|
CHECKRESULT(isc_result_t result, const char *msg) {
|
2000-04-06 22:03:35 +00:00
|
|
|
if (result != ISC_R_SUCCESS) {
|
1999-04-30 00:17:15 +00:00
|
|
|
printf("%s: %s\n", msg, dns_result_totext(result));
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-08-10 14:44:02 -07:00
|
|
|
fprintf(stderr, "bad input format: %02x\n", c);
|
1999-01-06 05:42:58 +00:00
|
|
|
exit(3);
|
1999-02-11 06:38:12 +00:00
|
|
|
/* NOTREACHED */
|
1999-01-06 05:42:58 +00:00
|
|
|
}
|
1999-01-13 19:11:26 +00:00
|
|
|
|
2001-05-09 18:51:44 +00:00
|
|
|
static void
|
|
|
|
usage(void) {
|
2015-08-17 22:41:44 -07:00
|
|
|
fprintf(stderr, "wire_test [-b] [-d] [-p] [-r] [-s]\n");
|
|
|
|
fprintf(stderr, " [-m {usage|trace|record|size|mctx}]\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");
|
2015-08-10 14:44:02 -07:00
|
|
|
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");
|
2015-08-10 14:44:02 -07:00
|
|
|
fprintf(stderr, "\t-s\tPrint memory statistics\n");
|
2001-05-09 18:56:29 +00:00
|
|
|
fprintf(stderr, "\t-t\tTCP mode - ignore the first 2 bytes\n");
|
2001-05-09 18:51:44 +00:00
|
|
|
}
|
|
|
|
|
2015-08-10 20:51:22 -07: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(mctx, len);
|
|
|
|
if (buf == NULL) {
|
|
|
|
result = ISC_R_NOMEMORY;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
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(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(mctx, buf, len);
|
|
|
|
|
|
|
|
return (result);
|
|
|
|
}
|
|
|
|
|
1999-01-06 05:42:58 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
2015-08-17 22:41:44 -07:00
|
|
|
isc_buffer_t *input = NULL;
|
1999-01-06 05:42:58 +00:00
|
|
|
isc_boolean_t need_close = ISC_FALSE;
|
2001-05-09 18:56:29 +00:00
|
|
|
isc_boolean_t tcp = ISC_FALSE;
|
2015-08-10 14:44:02 -07:00
|
|
|
isc_boolean_t rawdata = ISC_FALSE;
|
2015-08-17 22:41:44 -07:00
|
|
|
isc_result_t result;
|
|
|
|
isc_uint8_t c;
|
|
|
|
FILE *f;
|
2001-05-09 18:51:44 +00:00
|
|
|
int ch;
|
1999-04-30 00:17:15 +00:00
|
|
|
|
2015-08-17 22:41:44 -07: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_debugging |= ISC_MEM_DEBUGRECORD;
|
|
|
|
if (strcasecmp(isc_commandline_argument, "trace") == 0)
|
|
|
|
isc_mem_debugging |= ISC_MEM_DEBUGTRACE;
|
|
|
|
if (strcasecmp(isc_commandline_argument, "usage") == 0)
|
|
|
|
isc_mem_debugging |= ISC_MEM_DEBUGUSAGE;
|
|
|
|
if (strcasecmp(isc_commandline_argument, "size") == 0)
|
|
|
|
isc_mem_debugging |= ISC_MEM_DEBUGSIZE;
|
|
|
|
if (strcasecmp(isc_commandline_argument, "mctx") == 0)
|
|
|
|
isc_mem_debugging |= ISC_MEM_DEBUGCTX;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
isc_commandline_reset = ISC_TRUE;
|
|
|
|
|
1999-04-30 00:17:15 +00:00
|
|
|
RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
|
2000-08-01 01:33:37 +00:00
|
|
|
|
2015-08-17 22:41:44 -07:00
|
|
|
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;
|
2015-08-10 14:44:02 -07:00
|
|
|
case 'd':
|
|
|
|
rawdata = ISC_TRUE;
|
|
|
|
break;
|
2015-08-17 22:41:44 -07:00
|
|
|
case 'm':
|
|
|
|
break;
|
2015-08-10 14:44:02 -07:00
|
|
|
case 'p':
|
|
|
|
parseflags |= DNS_MESSAGEPARSE_PRESERVEORDER;
|
2001-05-09 18:51:44 +00:00
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
dorender = ISC_TRUE;
|
|
|
|
break;
|
2015-08-10 14:44:02 -07:00
|
|
|
case 's':
|
|
|
|
printmemstats = ISC_TRUE;
|
|
|
|
break;
|
2001-05-09 18:56:29 +00:00
|
|
|
case 't':
|
|
|
|
tcp = ISC_TRUE;
|
|
|
|
break;
|
2001-05-09 18:51:44 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= isc_commandline_index;
|
|
|
|
argv += isc_commandline_index;
|
|
|
|
|
2015-08-10 14:44:02 -07:00
|
|
|
if (argc >= 1) {
|
|
|
|
f = fopen(argv[0], "r");
|
1999-01-06 05:42:58 +00:00
|
|
|
if (f == NULL) {
|
2015-08-10 14:44:02 -07:00
|
|
|
fprintf(stderr, "%s: fopen failed\n", argv[0]);
|
1999-01-06 05:42:58 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
need_close = ISC_TRUE;
|
|
|
|
} else
|
|
|
|
f = stdin;
|
|
|
|
|
2015-08-23 19:29:40 +10:00
|
|
|
result = isc_buffer_allocate(mctx, &input, 64 * 1024);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
2015-08-17 22:41:44 -07:00
|
|
|
|
2015-08-10 14:44:02 -07:00
|
|
|
if (rawdata) {
|
2015-08-17 22:41:44 -07:00
|
|
|
while (fread(&c, 1, 1, f) != 0) {
|
|
|
|
result = isc_buffer_reserve(&input, 1);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
isc_buffer_putuint8(input, (isc_uint8_t) c);
|
|
|
|
}
|
2015-08-10 14:44:02 -07:00
|
|
|
} else {
|
2015-08-17 22:41:44 -07:00
|
|
|
char s[BUFSIZ];
|
|
|
|
|
2015-08-10 14:44:02 -07:00
|
|
|
while (fgets(s, sizeof(s), f) != NULL) {
|
2015-08-17 22:41:44 -07:00
|
|
|
char *rp = s, *wp = s;
|
|
|
|
size_t i, len = 0;
|
|
|
|
|
2015-08-10 14:44:02 -07:00
|
|
|
while (*rp != '\0') {
|
|
|
|
if (*rp == '#')
|
|
|
|
break;
|
|
|
|
if (*rp != ' ' && *rp != '\t' &&
|
|
|
|
*rp != '\r' && *rp != '\n') {
|
|
|
|
*wp++ = *rp;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
rp++;
|
|
|
|
}
|
|
|
|
if (len == 0U)
|
2015-09-18 11:39:31 -07:00
|
|
|
continue;
|
2015-08-10 14:44:02 -07:00
|
|
|
if (len % 2 != 0U) {
|
|
|
|
fprintf(stderr, "bad input format: %lu\n",
|
|
|
|
(unsigned long)len);
|
|
|
|
exit(1);
|
|
|
|
}
|
2015-08-17 22:41:44 -07:00
|
|
|
|
2015-08-10 14:44:02 -07:00
|
|
|
rp = s;
|
|
|
|
for (i = 0; i < len; i += 2) {
|
2015-08-17 22:41:44 -07:00
|
|
|
c = fromhex(*rp++);
|
|
|
|
c *= 16;
|
|
|
|
c += fromhex(*rp++);
|
|
|
|
result = isc_buffer_reserve(&input, 1);
|
|
|
|
RUNTIME_CHECK(result == ISC_R_SUCCESS);
|
|
|
|
isc_buffer_putuint8(input, (isc_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) {
|
2015-08-17 22:41:44 -07:00
|
|
|
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
|
|
|
|
2015-08-17 22:41:44 -07:00
|
|
|
if (isc_buffer_remaininglength(input) < 2) {
|
2015-08-10 14:44:02 -07:00
|
|
|
fprintf(stderr, "premature end of packet\n");
|
2001-09-29 18:23:56 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2015-08-17 22:41:44 -07:00
|
|
|
tcplen = isc_buffer_getuint16(input);
|
2001-09-29 18:23:56 +00:00
|
|
|
|
2015-08-17 22:41:44 -07:00
|
|
|
if (isc_buffer_remaininglength(input) < tcplen) {
|
2015-08-10 14:44:02 -07:00
|
|
|
fprintf(stderr, "premature end of packet\n");
|
2001-09-29 18:23:56 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
2015-08-17 22:41:44 -07:00
|
|
|
process_message(input);
|
2001-09-29 18:23:56 +00:00
|
|
|
}
|
2015-08-17 22:41:44 -07:00
|
|
|
} else
|
|
|
|
process_message(input);
|
|
|
|
|
|
|
|
if (input != NULL)
|
|
|
|
isc_buffer_free(&input);
|
2001-05-09 18:56:29 +00:00
|
|
|
|
2001-09-29 18:23:56 +00:00
|
|
|
if (printmemstats)
|
|
|
|
isc_mem_stats(mctx, stdout);
|
|
|
|
isc_mem_destroy(&mctx);
|
|
|
|
|
|
|
|
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;
|
1999-07-24 00:55:35 +00:00
|
|
|
result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &message);
|
1999-04-30 00:17:15 +00:00
|
|
|
CHECKRESULT(result, "dns_message_create failed");
|
|
|
|
|
2001-09-29 18:23:56 +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;
|
|
|
|
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(mctx, stdout);
|
1999-04-30 21:52:40 +00:00
|
|
|
|
2001-05-09 18:51:44 +00:00
|
|
|
if (dorender) {
|
2001-09-29 18:23:56 +00:00
|
|
|
unsigned char b2[64 * 1024];
|
|
|
|
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.
|
2015-05-23 23:45:25 +00:00
|
|
|
*/
|
2001-05-09 18:51:44 +00:00
|
|
|
message->from_to_wire = DNS_MESSAGE_INTENTRENDER;
|
1999-04-30 21:52:40 +00:00
|
|
|
|
2015-05-23 23:45:25 +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
|
|
|
|
2001-05-09 18:51:44 +00:00
|
|
|
result = dns_compress_init(&cctx, -1, mctx);
|
|
|
|
CHECKRESULT(result, "dns_compress_init() failed");
|
2001-03-05 21:15:47 +00:00
|
|
|
|
2001-09-29 18:23:56 +00:00
|
|
|
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-03-05 21:15:47 +00:00
|
|
|
|
2001-05-09 18:51:44 +00:00
|
|
|
message->from_to_wire = DNS_MESSAGE_INTENTPARSE;
|
|
|
|
dns_message_destroy(&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(mctx, stdout);
|
1999-04-30 21:52:40 +00:00
|
|
|
|
2001-05-09 18:51:44 +00:00
|
|
|
result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE,
|
|
|
|
&message);
|
|
|
|
CHECKRESULT(result, "dns_message_create failed");
|
1999-04-30 21:52:40 +00:00
|
|
|
|
2001-09-29 18:23:56 +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");
|
|
|
|
}
|
1999-04-30 00:17:15 +00:00
|
|
|
dns_message_destroy(&message);
|
1999-01-06 05:42:58 +00:00
|
|
|
}
|