wire_test.c 7.5 KB
Newer Older
Bob Halley's avatar
Bob Halley committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
/*
 * Copyright (C) 1999  Internet Software Consortium.
 * 
 * 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>
26
#include <isc/error.h>
Bob Halley's avatar
Bob Halley committed
27 28 29 30 31 32
#include <isc/boolean.h>
#include <isc/region.h>

#include <dns/types.h>
#include <dns/result.h>
#include <dns/name.h>
Bob Halley's avatar
Bob Halley committed
33
#include <dns/rdata.h>
34 35
#include <dns/rdataclass.h>
#include <dns/rdatatype.h>
36 37
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
Bob Halley's avatar
Bob Halley committed
38
#include <dns/compress.h>
39
#include <dns/message.h>
Bob Halley's avatar
Bob Halley committed
40

Bob Halley's avatar
Bob Halley committed
41
dns_decompress_t dctx;
Bob Halley's avatar
Bob Halley committed
42
unsigned int rdcount, rlcount, ncount;
Bob Halley's avatar
Bob Halley committed
43

Michael Graff's avatar
Michael Graff committed
44 45 46 47
void getmessage(dns_message_t *message, isc_buffer_t *source,
		isc_buffer_t *target);
dns_result_t printmessage(dns_message_t *message);

48 49 50 51 52 53 54 55 56 57 58
static inline void
CHECKRESULT(dns_result_t result, char *msg)
{
	if (result != DNS_R_SUCCESS) {
		printf("%s: %s\n", msg, dns_result_totext(result));

		exit(1);
	}
}


Bob Halley's avatar
Bob Halley committed
59
#ifdef NOISY
Bob Halley's avatar
Bob Halley committed
60 61 62 63 64 65 66 67 68 69
static void
print_wirename(isc_region_t *name) {
	unsigned char *ccurr, *cend;
		
	ccurr = name->base;
	cend = ccurr + name->length;
	while (ccurr != cend)
		printf("%02x ", *ccurr++);
	printf("\n");
}
Bob Halley's avatar
Bob Halley committed
70
#endif
Bob Halley's avatar
Bob Halley committed
71 72 73 74 75 76 77 78 79

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);
80

Bob Halley's avatar
Bob Halley committed
81 82
	printf("bad input format: %02x\n", c);
	exit(3);
83
	/* NOTREACHED */
Bob Halley's avatar
Bob Halley committed
84 85
}

Bob Halley's avatar
Bob Halley committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
static char *opcodetext[] = {
	"QUERY",
	"IQUERY",
	"STATUS",
	"RESERVED3",
	"NOTIFY",
	"UPDATE",
	"RESERVED6",
	"RESERVED7",
	"RESERVED8",
	"RESERVED9",
	"RESERVED10",
	"RESERVED11",
	"RESERVED12",
	"RESERVED13",
	"RESERVED14",
	"RESERVED15"
};

static char *rcodetext[] = {
	"NOERROR",
	"FORMERR",
	"SERVFAIL",
	"NXDOMAIN",
	"NOTIMPL",
	"REFUSED",
	"YXDOMAIN",
	"YXRRSET",
	"NXRRSET",
	"NOTAUTH",
	"NOTZONE",
	"RESERVED11",
	"RESERVED12",
	"RESERVED13",
	"RESERVED14",
	"RESERVED15"
};

Bob Halley's avatar
Bob Halley committed
124
static dns_result_t
125 126
printsection(dns_message_t *msg, dns_section_t sectionid, char *section_name)
{
127
	dns_name_t *name, *print_name;
128
	dns_rdataset_t *rdataset;
Bob Halley's avatar
Bob Halley committed
129 130
	isc_buffer_t target;
	dns_result_t result;
Bob Halley's avatar
Bob Halley committed
131
	isc_region_t r;
132
	dns_name_t empty_name;
Bob Halley's avatar
Bob Halley committed
133
	char t[1000];
134
	isc_boolean_t first;
135 136 137 138 139 140
	isc_boolean_t no_rdata;

	if (sectionid == DNS_SECTION_QUESTION)
		no_rdata = ISC_TRUE;
	else
		no_rdata = ISC_FALSE;
Bob Halley's avatar
Bob Halley committed
141 142

	printf("\n;; %s SECTION:\n", section_name);
143 144 145 146 147 148 149 150 151 152 153 154 155

	dns_name_init(&empty_name, NULL);

	result = dns_message_firstname(msg, sectionid);
	if (result == DNS_R_NOMORE)
		return (DNS_R_SUCCESS);
	else if (result != DNS_R_SUCCESS)
		return (result);

	for (;;) {
		name = NULL;
		dns_message_currentname(msg, sectionid, &name);

Bob Halley's avatar
Bob Halley committed
156
		isc_buffer_init(&target, t, sizeof t, ISC_BUFFERTYPE_TEXT);
157 158
		first = ISC_TRUE;
		print_name = name;
159 160 161 162 163 164 165

		for (rdataset = ISC_LIST_HEAD(name->list);
		     rdataset != NULL;
		     rdataset = ISC_LIST_NEXT(rdataset, link)) {
			result = dns_rdataset_totext(rdataset, print_name,
						     ISC_FALSE, &target,
						     no_rdata);
Bob Halley's avatar
Bob Halley committed
166 167
			if (result != DNS_R_SUCCESS)
				return (result);
168 169 170 171 172 173
#ifdef USEINITALWS
			if (first) {
				print_name = &empty_name;
				first = ISC_FALSE;
			}
#endif
Bob Halley's avatar
Bob Halley committed
174
		}
Bob Halley's avatar
Bob Halley committed
175 176
		isc_buffer_used(&target, &r);
		printf("%.*s", (int)r.length, (char *)r.base);
177 178 179 180 181 182

		result = dns_message_nextname(msg, sectionid);
		if (result == DNS_R_NOMORE)
			break;
		else if (result != DNS_R_SUCCESS)
			return (result);
Bob Halley's avatar
Bob Halley committed
183
	}
Bob Halley's avatar
Bob Halley committed
184 185
	
	return (DNS_R_SUCCESS);
Bob Halley's avatar
Bob Halley committed
186 187
}

188
dns_result_t
189
printmessage(dns_message_t *msg) {
Bob Halley's avatar
Bob Halley committed
190
	isc_boolean_t did_flag = ISC_FALSE;
Bob Halley's avatar
Bob Halley committed
191
	dns_result_t result;
Bob Halley's avatar
Bob Halley committed
192

193 194
	result = DNS_R_UNEXPECTED;

Bob Halley's avatar
Bob Halley committed
195
	printf(";; ->>HEADER<<- opcode: %s, status: %s, id: %u\n",
196 197
	       opcodetext[msg->opcode], rcodetext[msg->rcode], msg->id);

Bob Halley's avatar
Bob Halley committed
198
	printf(";; flags: ");
199
	if ((msg->flags & DNS_MESSAGEFLAG_QR) != 0) {
Bob Halley's avatar
Bob Halley committed
200 201 202
		printf("qr");
		did_flag = ISC_TRUE;
	}
203
	if ((msg->flags & DNS_MESSAGEFLAG_AA) != 0) {
Bob Halley's avatar
Bob Halley committed
204 205 206
		printf("%saa", did_flag ? " " : "");
		did_flag = ISC_TRUE;
	}
207
	if ((msg->flags & DNS_MESSAGEFLAG_TC) != 0) {
Bob Halley's avatar
Bob Halley committed
208 209 210
		printf("%stc", did_flag ? " " : "");
		did_flag = ISC_TRUE;
	}
211
	if ((msg->flags & DNS_MESSAGEFLAG_RD) != 0) {
Bob Halley's avatar
Bob Halley committed
212 213 214
		printf("%srd", did_flag ? " " : "");
		did_flag = ISC_TRUE;
	}
215
	if ((msg->flags & DNS_MESSAGEFLAG_RA) != 0) {
Bob Halley's avatar
Bob Halley committed
216 217 218
		printf("%sra", did_flag ? " " : "");
		did_flag = ISC_TRUE;
	}
Bob Halley's avatar
Bob Halley committed
219
	printf("; QUERY: %u, ANSWER: %u, AUTHORITY: %u, ADDITIONAL: %u\n",
220 221 222 223 224 225 226 227 228 229 230 231
	       msg->counts[DNS_SECTION_QUESTION],
	       msg->counts[DNS_SECTION_ANSWER],
	       msg->counts[DNS_SECTION_AUTHORITY],
	       msg->counts[DNS_SECTION_ADDITIONAL]);
	printf("; PSEUDOSECTIONS: OPT: %u, TSIG: %u\n",
	       msg->counts[DNS_SECTION_OPT],
	       msg->counts[DNS_SECTION_TSIG]);

	result = printsection(msg, DNS_SECTION_QUESTION, "QUESTION");
	if (result != DNS_R_SUCCESS)
		return (result);
	result = printsection(msg, DNS_SECTION_ANSWER, "ANSWER");
Bob Halley's avatar
Bob Halley committed
232 233
	if (result != DNS_R_SUCCESS)
		return (result);
234 235 236 237 238 239 240 241 242 243
	result = printsection(msg, DNS_SECTION_AUTHORITY, "AUTHORITY");
	if (result != DNS_R_SUCCESS)
		return (result);
	result = printsection(msg, DNS_SECTION_ADDITIONAL, "ADDITIONAL");
	if (result != DNS_R_SUCCESS)
		return (result);
	result = printsection(msg, DNS_SECTION_OPT, "PSEUDOSECTION OPT");
	if (result != DNS_R_SUCCESS)
		return (result);
	result = printsection(msg, DNS_SECTION_TSIG, "PSEUDOSECTION TSIG");
Bob Halley's avatar
Bob Halley committed
244 245 246 247
	if (result != DNS_R_SUCCESS)
		return (result);

	return (result);
Bob Halley's avatar
Bob Halley committed
248 249
}

250
#ifndef NOMAIN
Bob Halley's avatar
Bob Halley committed
251 252
int
main(int argc, char *argv[]) {
Bob Halley's avatar
Bob Halley committed
253
	char *rp, *wp;
Bob Halley's avatar
Bob Halley committed
254
	unsigned char *bp;
255
	isc_buffer_t source;
Bob Halley's avatar
Bob Halley committed
256 257 258 259
	size_t len, i;
	int n;
	FILE *f;
	isc_boolean_t need_close = ISC_FALSE;
Bob Halley's avatar
Bob Halley committed
260 261
	unsigned char b[1000];
	char s[1000];
262 263
	dns_message_t *message;
	dns_message_t *message2;
Bob Halley's avatar
Bob Halley committed
264
	dns_result_t result;
265 266 267
	isc_mem_t *mctx;

	RUNTIME_CHECK(isc_mem_create(0, 0, &mctx) == ISC_R_SUCCESS);
Bob Halley's avatar
Bob Halley committed
268 269 270 271 272 273 274 275 276 277 278 279 280
	
	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) {
Bob Halley's avatar
Bob Halley committed
281 282 283 284 285 286 287 288 289 290
		rp = s;
		wp = s;
		len = 0;
		while (*rp != '\0') {
			if (*rp != ' ' && *rp != '\t' &&
			    *rp != '\r' && *rp != '\n') {
				*wp++ = *rp;
				len++;
			}
			rp++;
Bob Halley's avatar
Bob Halley committed
291 292 293 294 295 296 297
		}
		if (len == 0)
			break;
		if (len % 2 != 0) {
			printf("bad input format: %d\n", len);
			exit(1);
		}
Bob Halley's avatar
Bob Halley committed
298
		if (len > (sizeof b) * 2) {
Bob Halley's avatar
Bob Halley committed
299 300 301
			printf("input too long\n");
			exit(2);
		}
Bob Halley's avatar
Bob Halley committed
302
		rp = s;
Bob Halley's avatar
Bob Halley committed
303
		for (i = 0; i < len; i += 2) {
Bob Halley's avatar
Bob Halley committed
304
			n = fromhex(*rp++);
Bob Halley's avatar
Bob Halley committed
305
			n *= 16;
Bob Halley's avatar
Bob Halley committed
306
			n += fromhex(*rp++);
Bob Halley's avatar
Bob Halley committed
307 308 309 310 311 312
			*bp++ = n;
		}
	}

	if (need_close)
		fclose(f);
Bob Halley's avatar
Bob Halley committed
313

314 315 316
	f = fopen("foo", "w");
	fwrite(b, bp - b, 1, f);
	fclose(f);
Bob Halley's avatar
Bob Halley committed
317

Bob Halley's avatar
Bob Halley committed
318
	isc_buffer_init(&source, b, sizeof b, ISC_BUFFERTYPE_BINARY);
Bob Halley's avatar
Bob Halley committed
319
	isc_buffer_add(&source, bp - b);
320 321 322 323 324 325 326 327 328 329 330 331 332 333

	result = dns_message_create(mctx, &message, DNS_MESSAGE_INTENT_PARSE);
	CHECKRESULT(result, "dns_message_create failed");

	result = dns_message_parse(message, &source);
	CHECKRESULT(result, "dns_message_parse failed");

	result = printmessage(message);
	CHECKRESULT(result, "printmessage() failed");

	dns_message_destroy(&message);

	isc_mem_stats(mctx, stdout);
	isc_mem_destroy(&mctx);
Bob Halley's avatar
Bob Halley committed
334

Bob Halley's avatar
Bob Halley committed
335 336
	return (0);
}
337
#endif /* !NOMAIN */