2
0
mirror of https://gitlab.isc.org/isc-projects/bind9 synced 2025-08-23 10:39:16 +00:00
bind/bin/tests/wire_test.c

199 lines
4.9 KiB
C
Raw Normal View History

1999-01-06 05:42:58 +00:00
/*
2000-02-03 23:08:31 +00:00
* Copyright (C) 1999, 2000 Internet Software Consortium.
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.
*
* 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.
*/
#include <config.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <isc/assertions.h>
1999-04-30 00:17:15 +00:00
#include <isc/error.h>
1999-01-06 05:42:58 +00:00
#include <isc/boolean.h>
#include <isc/region.h>
#include <dns/types.h>
#include <dns/result.h>
#include <dns/name.h>
1999-01-13 19:11:26 +00:00
#include <dns/rdata.h>
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
1999-01-15 03:30:36 +00:00
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
1999-01-06 20:05:09 +00:00
#include <dns/compress.h>
1999-04-30 00:17:15 +00:00
#include <dns/message.h>
1999-01-13 19:11:26 +00:00
#include "printmsg.h"
1999-01-06 05:42:58 +00:00
1999-04-30 00:17:15 +00:00
static inline void
CHECKRESULT(isc_result_t result, char *msg)
1999-04-30 00:17:15 +00:00
{
if (result != DNS_R_SUCCESS) {
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
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;
1999-01-13 19:11:26 +00:00
unsigned char b[1000];
char s[1000];
1999-04-30 00:17:15 +00:00
dns_message_t *message;
isc_result_t result;
1999-04-30 00:17:15 +00:00
isc_mem_t *mctx;
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);
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') {
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
1999-01-13 19:11:26 +00:00
isc_buffer_init(&source, b, sizeof b, ISC_BUFFERTYPE_BINARY);
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");
result = dns_message_parse(message, &source, ISC_FALSE);
1999-04-30 00:17:15 +00:00
CHECKRESULT(result, "dns_message_parse failed");
result = printmessage(message);
CHECKRESULT(result, "printmessage() failed");
1999-04-30 21:52:40 +00:00
isc_mem_stats(mctx, stdout);
/*
* XXXMLG
* Changing this here is a hack, and should not be done in reasonable
* application code, ever.
*/
1999-05-14 17:52:11 +00:00
message->from_to_wire = DNS_MESSAGE_INTENTRENDER;
1999-05-03 19:57:33 +00:00
memset(b, 0, sizeof(b));
1999-04-30 21:52:40 +00:00
isc_buffer_clear(&source);
1999-04-30 22:35:49 +00:00
for (i = 0 ; i < DNS_SECTION_MAX ; i++)
message->counts[i] = 0; /* Another hack XXX */
1999-04-30 21:52:40 +00:00
result = dns_message_renderbegin(message, &source);
CHECKRESULT(result, "dns_message_renderbegin() failed");
1999-12-22 03:22:59 +00:00
result = dns_message_rendersection(message, DNS_SECTION_QUESTION, 0);
1999-04-30 21:52:40 +00:00
CHECKRESULT(result, "dns_message_rendersection(QUESTION) failed");
1999-12-22 03:22:59 +00:00
result = dns_message_rendersection(message, DNS_SECTION_ANSWER, 0);
1999-04-30 21:52:40 +00:00
CHECKRESULT(result, "dns_message_rendersection(ANSWER) failed");
1999-12-22 03:22:59 +00:00
result = dns_message_rendersection(message, DNS_SECTION_AUTHORITY, 0);
1999-04-30 21:52:40 +00:00
CHECKRESULT(result, "dns_message_rendersection(AUTHORITY) failed");
1999-12-22 03:22:59 +00:00
result = dns_message_rendersection(message, DNS_SECTION_ADDITIONAL, 0);
1999-04-30 21:52:40 +00:00
CHECKRESULT(result, "dns_message_rendersection(ADDITIONAL) failed");
1999-12-22 03:22:59 +00:00
result = dns_message_rendersection(message, DNS_SECTION_TSIG, 0);
1999-04-30 21:52:40 +00:00
CHECKRESULT(result, "dns_message_rendersection(TSIG) failed");
dns_message_renderend(message);
1999-05-14 17:52:11 +00:00
message->from_to_wire = DNS_MESSAGE_INTENTPARSE;
1999-04-30 21:52:40 +00:00
dns_message_destroy(&message);
1999-05-21 00:47:49 +00:00
printf("Message rendered.\n");
1999-04-30 21:52:40 +00:00
isc_mem_stats(mctx, stdout);
1999-07-24 00:55:35 +00:00
result = dns_message_create(mctx, DNS_MESSAGE_INTENTPARSE, &message);
1999-04-30 21:52:40 +00:00
CHECKRESULT(result, "dns_message_create failed");
result = dns_message_parse(message, &source, ISC_FALSE);
1999-04-30 21:52:40 +00:00
CHECKRESULT(result, "dns_message_parse failed");
result = printmessage(message);
CHECKRESULT(result, "printmessage() failed");
1999-04-30 00:17:15 +00:00
dns_message_destroy(&message);
isc_mem_stats(mctx, stdout);
isc_mem_destroy(&mctx);
1999-01-13 19:11:26 +00:00
1999-01-06 05:42:58 +00:00
return (0);
}