asr_debug.c 8.41 KB
Newer Older
Eric Faurot's avatar
Eric Faurot committed
1
/*	$OpenBSD: asr_debug.c,v 1.14 2013/07/12 14:36:21 eric Exp $	*/
2
/*
Charles Longeau's avatar
Charles Longeau committed
3
 * Copyright (c) 2012 Eric Faurot <eric@openbsd.org>
4 5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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 THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR 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.
 */
17 18 19

#include "includes.h"

20 21 22 23
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
Charles Longeau's avatar
Charles Longeau committed
24
#include <arpa/inet.h>
25 26 27 28 29 30

#include <resolv.h>

#include "asr.h"
#include "asr_private.h"

31 32 33 34 35
static const char *rcodetostr(uint16_t);
static const char *print_dname(const char *, char *, size_t);
static const char *print_header(const struct header *, char *, size_t);
static const char *print_query(const struct query *, char *, size_t);
static const char *print_rr(const struct rr *, char *, size_t);
36

Charles Longeau's avatar
Charles Longeau committed
37
FILE *asr_debug = NULL;
38 39 40 41 42 43 44

#define OPCODE_SHIFT	11
#define Z_SHIFT		 4

static const char *
rcodetostr(uint16_t v)
{
45
	switch (v) {
Charles Longeau's avatar
Charles Longeau committed
46 47 48 49 50 51 52 53
	case NOERROR:	return "NOERROR";
	case FORMERR:	return "FORMERR";
	case SERVFAIL:	return "SERVFAIL";
	case NXDOMAIN:	return "NXDOMAIN";
	case NOTIMP:	return "NOTIMP";
	case REFUSED:	return "REFUSED";
	default:	return "?";
	}
54 55
}

56
static const char *
Charles Longeau's avatar
Charles Longeau committed
57
print_dname(const char *_dname, char *buf, size_t max)
58
{
Charles Longeau's avatar
Charles Longeau committed
59
	return (asr_strdname(_dname, buf, max));
60 61
}

62
static const char *
Charles Longeau's avatar
Charles Longeau committed
63
print_rr(const struct rr *rr, char *buf, size_t max)
64 65 66 67 68 69 70 71 72 73 74
{
	char	*res;
	char	 tmp[256];
	char	 tmp2[256];
	int	 r;

	res = buf;

	r = snprintf(buf, max, "%s %u %s %s ",
	    print_dname(rr->rr_dname, tmp, sizeof tmp),
	    rr->rr_ttl,
Charles Longeau's avatar
Charles Longeau committed
75 76
	    __p_class(rr->rr_class),
	    __p_type(rr->rr_type));
77 78
	if (r == -1) {
		buf[0] = '\0';
Charles Longeau's avatar
Charles Longeau committed
79
		return (buf);
80 81 82
	}

	if ((size_t)r >= max)
Charles Longeau's avatar
Charles Longeau committed
83
		return (buf);
84 85 86 87

	max -= r;
	buf += r;

88
	switch (rr->rr_type) {
89 90 91 92
	case T_CNAME:
		print_dname(rr->rr.cname.cname, buf, max);
		break;
	case T_MX:
Charles Longeau's avatar
Charles Longeau committed
93 94
		snprintf(buf, max, "%lu %s",
		    (unsigned long)rr->rr.mx.preference,
95 96 97 98 99 100 101 102 103
		    print_dname(rr->rr.mx.exchange, tmp, sizeof tmp));
		break;
	case T_NS:
		print_dname(rr->rr.ns.nsname, buf, max);
		break;
	case T_PTR:
		print_dname(rr->rr.ptr.ptrname, buf, max);
		break;
	case T_SOA:
Charles Longeau's avatar
Charles Longeau committed
104
		snprintf(buf, max, "%s %s %lu %lu %lu %lu %lu",
105 106
		    print_dname(rr->rr.soa.rname, tmp, sizeof tmp),
		    print_dname(rr->rr.soa.mname, tmp2, sizeof tmp2),
Charles Longeau's avatar
Charles Longeau committed
107 108 109 110 111
		    (unsigned long)rr->rr.soa.serial,
		    (unsigned long)rr->rr.soa.refresh,
		    (unsigned long)rr->rr.soa.retry,
		    (unsigned long)rr->rr.soa.expire,
		    (unsigned long)rr->rr.soa.minimum);
112 113 114 115
		break;
	case T_A:
		if (rr->rr_class != C_IN)
			goto other;
Charles Longeau's avatar
Charles Longeau committed
116 117
		snprintf(buf, max, "%s", inet_ntop(AF_INET,
		    &rr->rr.in_a.addr, tmp, sizeof tmp));
118 119 120 121
		break;
	case T_AAAA:
		if (rr->rr_class != C_IN)
			goto other;
Charles Longeau's avatar
Charles Longeau committed
122 123
		snprintf(buf, max, "%s", inet_ntop(AF_INET6,
		    &rr->rr.in_aaaa.addr6, tmp, sizeof tmp));
124 125 126
		break;
	default:
	other:
Charles Longeau's avatar
Charles Longeau committed
127
		snprintf(buf, max, "(rdlen=%i)", (int)rr->rr.other.rdlen);
128 129 130 131 132 133
		break;
	}

	return (res);
}

134
static const char *
Charles Longeau's avatar
Charles Longeau committed
135
print_query(const struct query *q, char *buf, size_t max)
136 137 138 139 140
{
	char b[256];

	snprintf(buf, max, "%s	%s %s",
	    print_dname(q->q_dname, b, sizeof b),
Charles Longeau's avatar
Charles Longeau committed
141
	    __p_class(q->q_class), __p_type(q->q_type));
142 143 144 145

	return (buf);
}

146
static const char *
Charles Longeau's avatar
Charles Longeau committed
147
print_header(const struct header *h, char *buf, size_t max)
148 149 150
{
	snprintf(buf, max,
	"id:0x%04x %s op:%i %s %s %s %s z:%i r:%s qd:%i an:%i ns:%i ar:%i",
Charles Longeau's avatar
Charles Longeau committed
151
	    ((int)h->id),
152 153 154 155 156
	    (h->flags & QR_MASK) ? "QR":"  ",
	    (int)(OPCODE(h->flags) >> OPCODE_SHIFT),
	    (h->flags & AA_MASK) ? "AA":"  ",
	    (h->flags & TC_MASK) ? "TC":"  ",
	    (h->flags & RD_MASK) ? "RD":"  ",
157
	    (h->flags & RA_MASK) ? "RA":"  ",
158 159 160 161 162 163 164 165
	    ((h->flags & Z_MASK) >> Z_SHIFT),
	    rcodetostr(RCODE(h->flags)),
	    h->qdcount, h->ancount, h->nscount, h->arcount);

	return (buf);
}

void
Charles Longeau's avatar
Charles Longeau committed
166
asr_dump_packet(FILE *f, const void *data, size_t len)
167 168
{
	char		buf[1024];
169
	struct unpack	p;
170 171 172 173 174 175 176 177
	struct header	h;
	struct query	q;
	struct rr	rr;
	int		i, an, ns, ar, n;

	if (f == NULL)
		return;

Eric Faurot's avatar
Eric Faurot committed
178
	asr_unpack_init(&p, data, len);
179

Eric Faurot's avatar
Eric Faurot committed
180
	if (asr_unpack_header(&p, &h) == -1) {
181 182 183 184
		fprintf(f, ";; BAD PACKET: %s\n", p.err);
		return;
	}

Charles Longeau's avatar
Charles Longeau committed
185
	fprintf(f, ";; HEADER %s\n", print_header(&h, buf, sizeof buf));
186 187 188 189

	if (h.qdcount)
		fprintf(f, ";; QUERY SECTION:\n");
	for (i = 0; i < h.qdcount; i++) {
Eric Faurot's avatar
Eric Faurot committed
190
		if (asr_unpack_query(&p, &q) == -1)
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
			goto error;
		fprintf(f, "%s\n", print_query(&q, buf, sizeof buf));
	}

	an = 0;
	ns = an + h.ancount;
	ar = ns + h.nscount;
	n = ar + h.arcount;

	for (i = 0; i < n; i++) {
		if (i == an)
			fprintf(f, "\n;; ANSWER SECTION:\n");
		if (i == ns)
			fprintf(f, "\n;; AUTHORITY SECTION:\n");
		if (i == ar)
			fprintf(f, "\n;; ADDITIONAL SECTION:\n");

Eric Faurot's avatar
Eric Faurot committed
208
		if (asr_unpack_rr(&p, &rr) == -1)
209 210 211 212 213 214 215 216 217 218 219 220 221
			goto error;
		fprintf(f, "%s\n", print_rr(&rr, buf, sizeof buf));
	}

	if (p.offset != len)
		fprintf(f, ";; REMAINING GARBAGE %zu\n", len - p.offset);

    error:
	if (p.err)
		fprintf(f, ";; ERROR AT OFFSET %zu/%zu: %s\n", p.offset, p.len,
		    p.err);
}

Charles Longeau's avatar
Charles Longeau committed
222 223
const char *
print_sockaddr(const struct sockaddr *sa, char *buf, size_t len)
224
{
Charles Longeau's avatar
Charles Longeau committed
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
	char	h[256];
	int	portno;
	union {
		const struct sockaddr		*sa;
		const struct sockaddr_in	*sin;
		const struct sockaddr_in6	*sin6;
	}	s;

	s.sa = sa;

	switch (sa->sa_family) {
	case AF_INET:
		inet_ntop(AF_INET, &s.sin->sin_addr, h, sizeof h);
		portno = ntohs(s.sin->sin_port);
		break;
	case AF_INET6:
		inet_ntop(AF_INET6, &s.sin6->sin6_addr, h, sizeof h);
		portno = ntohs(s.sin6->sin6_port);
		break;
	default:
		snprintf(buf, len, "?");
		return (buf);
	}

	snprintf(buf, len, "%s:%i", h, portno);
	return (buf);
251 252 253
}

void
Charles Longeau's avatar
Charles Longeau committed
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
asr_dump_config(FILE *f, struct asr *a)
{
	char		 buf[256];
	int		 i;
	struct asr_ctx	*ac;
	unsigned int	 o;

	if (f == NULL)
		return;

	ac = a->a_ctx;

	fprintf(f, "--------- ASR CONFIG ---------------\n");
	if (a->a_path)
		fprintf(f, "CONF FILE \"%s\"\n", a->a_path);
	else
		fprintf(f, "STATIC CONF\n");
	fprintf(f, "DOMAIN \"%s\"\n", ac->ac_domain);
	fprintf(f, "SEARCH\n");
273
	for (i = 0; i < ac->ac_domcount; i++)
Charles Longeau's avatar
Charles Longeau committed
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
		fprintf(f, "   \"%s\"\n", ac->ac_dom[i]);
	fprintf(f, "OPTIONS\n");
	fprintf(f, " options:");
	o = ac->ac_options;

#define PRINTOPT(flag, n) if (o & (flag)) { fprintf(f, " " n); o &= ~(flag); }
	PRINTOPT(RES_INIT, "INIT");
	PRINTOPT(RES_DEBUG, "DEBUG");
	PRINTOPT(RES_USEVC, "USEVC");
	PRINTOPT(RES_IGNTC, "IGNTC");
	PRINTOPT(RES_RECURSE, "RECURSE");
	PRINTOPT(RES_DEFNAMES, "DEFNAMES");
	PRINTOPT(RES_STAYOPEN, "STAYOPEN");
	PRINTOPT(RES_DNSRCH, "DNSRCH");
	PRINTOPT(RES_NOALIASES, "NOALIASES");
	PRINTOPT(RES_USE_EDNS0, "USE_EDNS0");
	PRINTOPT(RES_USE_DNSSEC, "USE_DNSSEC");
	if (o)
		fprintf(f, " 0x%08x", o);
	fprintf(f, "\n");

	fprintf(f, " ndots: %i\n", ac->ac_ndots);
	fprintf(f, " family:");
297
	for (i = 0; ac->ac_family[i] != -1; i++)
298
		fprintf(f, " %s", (ac->ac_family[i] == AF_INET)?"inet4":"inet6");
Charles Longeau's avatar
Charles Longeau committed
299 300
	fprintf(f, "\n");
	fprintf(f, "NAMESERVERS timeout=%i retry=%i\n",
301 302 303
		    ac->ac_nstimeout,
		    ac->ac_nsretries);
	for (i = 0; i < ac->ac_nscount; i++)
Charles Longeau's avatar
Charles Longeau committed
304 305 306
		fprintf(f, "	%s\n", print_sockaddr(ac->ac_ns[i], buf,
		    sizeof buf));
	fprintf(f, "HOSTFILE %s\n", ac->ac_hostfile);
307
	fprintf(f, "LOOKUP %s", ac->ac_db);
Charles Longeau's avatar
Charles Longeau committed
308 309 310 311 312 313 314
	fprintf(f, "\n------------------------------------\n");
}

#define CASE(n) case n: return #n

const char *
asr_statestr(int state)
315
{
Charles Longeau's avatar
Charles Longeau committed
316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
	switch (state) {
	CASE(ASR_STATE_INIT);
	CASE(ASR_STATE_NEXT_DOMAIN);
	CASE(ASR_STATE_NEXT_DB);
	CASE(ASR_STATE_SAME_DB);
	CASE(ASR_STATE_NEXT_FAMILY);
	CASE(ASR_STATE_NEXT_NS);
	CASE(ASR_STATE_UDP_SEND);
	CASE(ASR_STATE_UDP_RECV);
	CASE(ASR_STATE_TCP_WRITE);
	CASE(ASR_STATE_TCP_READ);
	CASE(ASR_STATE_PACKET);
	CASE(ASR_STATE_SUBQUERY);
	CASE(ASR_STATE_NOT_FOUND);
	CASE(ASR_STATE_HALT);
	default:
		return "?";
	}
};
335

Charles Longeau's avatar
Charles Longeau committed
336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
const char *
asr_querystr(int type)
{
	switch (type) {
	CASE(ASR_SEND);
	CASE(ASR_SEARCH);
	CASE(ASR_GETRRSETBYNAME);
	CASE(ASR_GETHOSTBYNAME);
	CASE(ASR_GETHOSTBYADDR);
	CASE(ASR_GETNETBYNAME);
	CASE(ASR_GETNETBYADDR);
	CASE(ASR_GETADDRINFO);
	CASE(ASR_GETNAMEINFO);
	default:
		return "?";
	}
352 353
}

Charles Longeau's avatar
Charles Longeau committed
354 355
const char *
asr_transitionstr(int type)
356
{
357
	switch (type) {
Charles Longeau's avatar
Charles Longeau committed
358 359 360 361 362 363
	CASE(ASYNC_COND);
	CASE(ASYNC_YIELD);
	CASE(ASYNC_DONE);
	default:
		return "?";
	}
364
}