wire_debug.c 5.54 KB
Newer Older
Michael Graff's avatar
Michael Graff 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
26
#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>

#include <sys/types.h>
#include <sys/socket.h>

#include <isc/assertions.h>
#include <isc/error.h>
#include <isc/mem.h>
#include <isc/task.h>
#include <isc/thread.h>
#include <isc/result.h>
#include <isc/socket.h>
#include <isc/timer.h>

#include <dns/types.h>
#include <dns/result.h>
#include <dns/name.h>
#include <dns/rdata.h>
#include <dns/rdatalist.h>
#include <dns/rdataset.h>
#include <dns/compress.h>
Michael Graff's avatar
Michael Graff committed
27
#include <dns/db.h>
Michael Graff's avatar
Michael Graff committed
28

Michael Graff's avatar
Michael Graff committed
29
30
31
32
33
34
35
36
37
#define DNS_FLAG_QR		0x8000U
#define DNS_FLAG_AA		0x0400U
#define DNS_FLAG_TC		0x0200U
#define DNS_FLAG_RD		0x0100U
#define DNS_FLAG_RA		0x0080U

#define DNS_OPCODE_MASK		0x7000U
#define DNS_OPCODE_SHIFT	11
#define DNS_RCODE_MASK		0x000FU
Michael Graff's avatar
Michael Graff committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55

/*
 * XXX All of the following is for debugging only, and will eventually
 * be in a library or removed when we really answer queries.
 */
typedef struct dns_message {
	unsigned int		id;
	unsigned int		flags;
	unsigned int		qcount;
	unsigned int		ancount;
	unsigned int		aucount;
	unsigned int		adcount;
	dns_namelist_t		question;
	dns_namelist_t		answer;
	dns_namelist_t		authority;
	dns_namelist_t		additional;
} dns_message_t;

56
57
58
59
60
61
void
dump_packet(char *buf, u_int len);

dns_result_t
resolve_packet(dns_db_t *db, isc_buffer_t *source, isc_buffer_t *target);

Michael Graff's avatar
Michael Graff committed
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
/*
 * in wire_test.c
 */
void getmessage(dns_message_t *message, isc_buffer_t *source,
		isc_buffer_t *target);
dns_result_t printmessage(dns_message_t *message);

void
dump_packet(char *buf, u_int len)
{
	extern dns_decompress_t dctx;
	extern unsigned int rdcount, rlcount, ncount;
	char t[5000]; /* XXX */
	dns_message_t message;
	dns_result_t result;
	isc_buffer_t source, target;

	rdcount = 0;
	rlcount = 0;
	ncount = 0;

	dctx.allowed = DNS_COMPRESS_GLOBAL14;
	dns_name_init(&dctx.owner_name, NULL);

	isc_buffer_init(&source, buf, len, ISC_BUFFERTYPE_BINARY);
	isc_buffer_add(&source, len);
	isc_buffer_init(&target, t, sizeof(t), ISC_BUFFERTYPE_BINARY);

	getmessage(&message, &source, &target);
	result = printmessage(&message);
	if (result != DNS_R_SUCCESS)
		printf("printmessage() failed: %s\n",
		       dns_result_totext(result));
}
Michael Graff's avatar
Michael Graff committed
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238

static isc_uint16_t
getshort(isc_buffer_t *buffer) {
	isc_region_t r;

	isc_buffer_remaining(buffer, &r);
	if (r.length < 2) {
		printf("not enough input\n");
		exit(5);
	}

	return (isc_buffer_getuint16(buffer));
}

dns_result_t
resolve_packet(dns_db_t *db, isc_buffer_t *source, isc_buffer_t *target)
{
	dns_decompress_t dctx;
	dns_compress_t cctx;
	dns_result_t result;
	unsigned int count;
	dns_message_t message;
	dns_name_t name;
	isc_uint16_t qtype;
	isc_uint16_t qclass;
	unsigned char t[256];
	isc_buffer_t tbuf;
	isc_uint16_t status;
	dns_dbnode_t *node;
	dns_rdataset_t rdataset;

	count = 0;
	status = 0;

	message.id = getshort(source);
	message.flags = getshort(source);
	message.qcount = getshort(source);
	message.ancount = getshort(source);
	message.aucount = getshort(source);
	message.adcount = getshort(source);

	INSIST(message.qcount == 1);
	INSIST(message.ancount == 0);
	INSIST(message.aucount == 0);
	INSIST(message.adcount == 0);

	dctx.allowed = DNS_COMPRESS_GLOBAL14;
	dns_name_init(&dctx.owner_name, NULL);

	cctx.allowed = DNS_COMPRESS_GLOBAL14;
	dns_name_init(&cctx.owner_name, NULL);

	/*
	 * Expand the name requested into buffer (tbuf)
	 */
	isc_buffer_init(&tbuf, t, sizeof(t), ISC_BUFFERTYPE_BINARY);
	dns_name_init(&name, NULL);
	result = dns_name_fromwire(&name, source, &dctx, ISC_FALSE, &tbuf);
	qtype = getshort(source);
	qclass = getshort(source);

	/*
	 * Look it up in the database.  XXX Uses many hard coded bits.
	 */

	node = NULL;
	result = dns_db_findnode(db, &name, ISC_FALSE, &node);
	if (result == DNS_R_NOTFOUND) {
		status = 3;  /* NXDOMAIN */
		goto out;
	}
	if (result != DNS_R_SUCCESS) {
		status = 2;  /* SERVFAIL */
		goto out;
	}

	dns_rdataset_init(&rdataset);
	result = dns_db_findrdataset(db, node, NULL, qtype, &rdataset);
	dns_db_detachnode(db, &node);
	if (result == DNS_R_NOTFOUND) {
		status = 3;  /* NXDOMAIN */
		goto out;
	}
	if (result != DNS_R_SUCCESS) {
		status = 2;  /* SERVFAIL */
		goto out;
	}

 out:
	/*
	 * XXX This should really use the buffer functions correctly, but...
	 */

	/*
	 * Write the header.
	 */
	isc_buffer_putuint16(target, message.id);

	message.flags |= DNS_FLAG_QR;
	message.flags &= ~(DNS_FLAG_AA | DNS_FLAG_TC | DNS_FLAG_RA);
	message.flags &= ~(DNS_RCODE_MASK);
	message.flags |= status;
	isc_buffer_putuint16(target, message.flags);

	isc_buffer_putuint16(target, message.qcount);
	isc_buffer_putuint16(target, message.ancount);  /* XXX fix up later */
	isc_buffer_putuint16(target, message.aucount);
	isc_buffer_putuint16(target, message.adcount);

	/*
	 * Write the question.  Note that we reconstruct it...
	 */
	dns_name_towire(&name, &cctx, target);
	isc_buffer_putuint16(target, qtype);
	isc_buffer_putuint16(target, qclass);

	/*
	 * Now, scribble out the answer.  If this works we will update the
	 * answer count.  If it fails, we will update the status instead.
	 */
	if (status == 0) {
		unsigned int oldused;

		count = 0;
		result = dns_rdataset_towire(&rdataset, &name, &cctx,
					     target, &count);
		if (result != DNS_R_SUCCESS) { /* Should just return fail? */
			oldused = target->used;
			target->used = 2;  /* Hack! XXX */
			message.flags &= ~(DNS_RCODE_MASK);
			message.flags |= 2; /* SERVFAIL */
			isc_buffer_putuint16(target, message.flags);
			target->used = oldused;
		} else {
			oldused = target->used;
			target->used = 6;  /* Another hack! XXX */
			isc_buffer_putuint16(target, count);
			target->used = oldused;
		}
	}

	return (DNS_R_SUCCESS);
}