1999-01-06 05:42:58 +00:00
|
|
|
/*
|
2001-01-09 22:01:04 +00:00
|
|
|
* Copyright (C) 1999-2001 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
1999-01-06 05:42:58 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* 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
|
|
|
*
|
2000-07-27 09:55:03 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
|
|
|
|
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
|
|
|
* INTERNET SOFTWARE CONSORTIUM 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
|
|
|
*/
|
|
|
|
|
2001-05-09 18:51:44 +00:00
|
|
|
/* $Id: wire_test.c,v 1.58 2001/05/09 18:51:44 bwelling Exp $ */
|
2000-06-22 22:00:42 +00:00
|
|
|
|
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>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/mem.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/result.h>
|
1999-01-13 19:11:26 +00:00
|
|
|
|
1999-07-09 17:23:54 +00:00
|
|
|
#include "printmsg.h"
|
1999-01-06 05:42:58 +00:00
|
|
|
|
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
|
|
|
|
1999-01-06 05:42:58 +00:00
|
|
|
printf("bad input format: %02x\n", c);
|
|
|
|
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) {
|
|
|
|
fprintf(stderr, "wire_test [-p] [-b] [-s] [-r]\n");
|
|
|
|
fprintf(stderr, "\t-p\tPreserve order of the records in messages\n");
|
|
|
|
fprintf(stderr, "\t-b\tBest-effort parsing (ignore some errors)\n");
|
|
|
|
fprintf(stderr, "\t-s\tPrint memory statistics\n");
|
|
|
|
fprintf(stderr, "\t-r\tAfter parsing, re-render the message\n");
|
|
|
|
}
|
|
|
|
|
1999-01-06 05:42:58 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[]) {
|
1999-01-13 19:57:13 +00:00
|
|
|
char *rp, *wp;
|
1999-01-06 05:42:58 +00:00
|
|
|
unsigned char *bp;
|
1999-04-30 00:17:15 +00:00
|
|
|
isc_buffer_t source;
|
1999-01-06 05:42:58 +00:00
|
|
|
size_t len, i;
|
|
|
|
int n;
|
|
|
|
FILE *f;
|
|
|
|
isc_boolean_t need_close = ISC_FALSE;
|
2001-05-09 00:16:48 +00:00
|
|
|
unsigned char b[64 * 1024];
|
2001-03-05 21:15:47 +00:00
|
|
|
char s[4000];
|
1999-04-30 00:17:15 +00:00
|
|
|
dns_message_t *message;
|
1999-12-23 00:09:04 +00:00
|
|
|
isc_result_t result;
|
2001-03-05 21:15:47 +00:00
|
|
|
dns_compress_t cctx;
|
1999-04-30 00:17:15 +00:00
|
|
|
isc_mem_t *mctx;
|
2001-05-09 18:51:44 +00:00
|
|
|
int parseflags = 0;
|
|
|
|
isc_boolean_t printmemstats = ISC_FALSE;
|
|
|
|
isc_boolean_t dorender = ISC_FALSE;
|
|
|
|
int ch;
|
1999-04-30 00:17:15 +00:00
|
|
|
|
1999-05-12 19:47:43 +00:00
|
|
|
mctx = NULL;
|
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
|
|
|
|
2001-05-09 18:51:44 +00:00
|
|
|
while ((ch = isc_commandline_parse(argc, argv, "pbsr")) != -1) {
|
|
|
|
switch (ch) {
|
|
|
|
case 'p':
|
|
|
|
parseflags |= DNS_MESSAGEPARSE_PRESERVEORDER;
|
|
|
|
break;
|
|
|
|
case 'b':
|
|
|
|
parseflags |= DNS_MESSAGEPARSE_BESTEFFORT;
|
|
|
|
break;
|
|
|
|
case 's':
|
|
|
|
printmemstats = ISC_TRUE;
|
|
|
|
break;
|
|
|
|
case 'r':
|
|
|
|
dorender = ISC_TRUE;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
argc -= isc_commandline_index;
|
|
|
|
argv += isc_commandline_index;
|
|
|
|
|
1999-01-06 05:42:58 +00:00
|
|
|
if (argc > 1) {
|
|
|
|
f = fopen(argv[1], "r");
|
|
|
|
if (f == NULL) {
|
|
|
|
printf("fopen failed\n");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
need_close = ISC_TRUE;
|
|
|
|
} else
|
|
|
|
f = stdin;
|
|
|
|
|
|
|
|
bp = b;
|
|
|
|
while (fgets(s, sizeof s, f) != NULL) {
|
1999-01-13 19:57:13 +00:00
|
|
|
rp = s;
|
|
|
|
wp = s;
|
|
|
|
len = 0;
|
|
|
|
while (*rp != '\0') {
|
2000-03-16 01:09:13 +00:00
|
|
|
if (*rp == '#')
|
|
|
|
break;
|
1999-01-13 19:57:13 +00:00
|
|
|
if (*rp != ' ' && *rp != '\t' &&
|
|
|
|
*rp != '\r' && *rp != '\n') {
|
|
|
|
*wp++ = *rp;
|
|
|
|
len++;
|
|
|
|
}
|
|
|
|
rp++;
|
1999-01-06 05:42:58 +00:00
|
|
|
}
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
if (len % 2 != 0) {
|
|
|
|
printf("bad input format: %d\n", len);
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-01-13 19:57:13 +00:00
|
|
|
if (len > (sizeof b) * 2) {
|
1999-01-06 05:42:58 +00:00
|
|
|
printf("input too long\n");
|
|
|
|
exit(2);
|
|
|
|
}
|
1999-01-13 19:57:13 +00:00
|
|
|
rp = s;
|
1999-01-06 05:42:58 +00:00
|
|
|
for (i = 0; i < len; i += 2) {
|
1999-01-13 19:57:13 +00:00
|
|
|
n = fromhex(*rp++);
|
1999-01-06 05:42:58 +00:00
|
|
|
n *= 16;
|
1999-01-13 19:57:13 +00:00
|
|
|
n += fromhex(*rp++);
|
1999-01-06 05:42:58 +00:00
|
|
|
*bp++ = n;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (need_close)
|
|
|
|
fclose(f);
|
1999-01-06 20:05:09 +00:00
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_init(&source, b, sizeof(b));
|
1999-01-06 05:42:58 +00:00
|
|
|
isc_buffer_add(&source, bp - b);
|
1999-04-30 00:17:15 +00:00
|
|
|
|
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-05-09 18:51:44 +00:00
|
|
|
result = dns_message_parse(message, &source, parseflags);
|
|
|
|
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) {
|
|
|
|
/*
|
|
|
|
* XXXMLG
|
|
|
|
* Changing this here is a hack, and should not be done in
|
|
|
|
* reasonable application code, ever.
|
|
|
|
*/
|
|
|
|
message->from_to_wire = DNS_MESSAGE_INTENTRENDER;
|
|
|
|
memset(b, 0, sizeof(b));
|
|
|
|
isc_buffer_clear(&source);
|
1999-04-30 21:52:40 +00:00
|
|
|
|
2001-05-09 18:51:44 +00:00
|
|
|
for (i = 0 ; i < DNS_SECTION_MAX ; i++)
|
|
|
|
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-05-09 18:51:44 +00:00
|
|
|
result = dns_message_renderbegin(message, &cctx, &source);
|
|
|
|
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-05-09 18:51:44 +00:00
|
|
|
result = dns_message_parse(message, &source, parseflags);
|
|
|
|
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 21:52:40 +00:00
|
|
|
|
1999-04-30 00:17:15 +00:00
|
|
|
dns_message_destroy(&message);
|
|
|
|
|
2001-05-09 18:51:44 +00:00
|
|
|
if (printmemstats)
|
|
|
|
isc_mem_stats(mctx, stdout);
|
1999-04-30 00:17:15 +00:00
|
|
|
isc_mem_destroy(&mctx);
|
1999-01-13 19:11:26 +00:00
|
|
|
|
1999-01-06 05:42:58 +00:00
|
|
|
return (0);
|
|
|
|
}
|