summaryrefslogtreecommitdiff
path: root/pjlib-util/src/pjlib-util/dns_dump.c
blob: ab9ca684cf0e79ca2621d6c307cef6b73a79a765 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
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
/* $Id$ */
/* 
 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
 */
#include <pjlib-util/dns.h>
#include <pj/assert.h>
#include <pj/log.h>
#include <pj/string.h>

#define THIS_FILE   "dns_dump.c"
#define LEVEL	    3

static const char *spell_ttl(char *buf, int size, unsigned ttl)
{
#define DAY	(3600*24)
#define HOUR	(3600)
#define MINUTE	(60)

    char *p = buf;
    int len;

    if (ttl > DAY) {
	len = pj_ansi_snprintf(p, size, "%dd ", ttl/DAY);
	if (len < 1 || len >= size)
	    return "-err-";
	size -= len;
	p += len;
	ttl %= DAY;
    }

    if (ttl > HOUR) {
	len = pj_ansi_snprintf(p, size, "%dh ", ttl/HOUR);
	if (len < 1 || len >= size)
	    return "-err-";
	size -= len;
	p += len;
	ttl %= HOUR;
    }

    if (ttl > MINUTE) {
	len = pj_ansi_snprintf(p, size, "%dm ", ttl/MINUTE);
	if (len < 1 || len >= size)
	    return "-err-";
	size -= len;
	p += len;
	ttl %= MINUTE;
    }

    if (ttl > 0) {
	len = pj_ansi_snprintf(p, size, "%ds ", ttl);
	if (len < 1 || len >= size)
	    return "-err-";
	size -= len;
	p += len;
	ttl = 0;
    }

    *p = '\0';
    return buf;
}


static void dump_query(unsigned index, const pj_dns_parsed_query *q)
{
    PJ_LOG(3,(THIS_FILE, " %d. Name: %.*s", 
			 index, (int)q->name.slen, q->name.ptr));
    PJ_LOG(3,(THIS_FILE, "    Type: %s (%d)", 
			 pj_dns_get_type_name(q->type), q->type));
    PJ_LOG(3,(THIS_FILE, "    Class: %s (%d)", 
		         (q->dnsclass==1 ? "IN" : "<Unknown>"), q->dnsclass));
}

static void dump_answer(unsigned index, const pj_dns_parsed_rr *rr)
{
    const pj_str_t root_name = { "<Root>", 6 };
    const pj_str_t *name = &rr->name;
    char ttl_words[32];
    char addr[PJ_INET6_ADDRSTRLEN];

    if (name->slen == 0)
	name = &root_name;

    PJ_LOG(3,(THIS_FILE, " %d. %s record (type=%d)", 
			 index, pj_dns_get_type_name(rr->type),
			rr->type));
    PJ_LOG(3,(THIS_FILE, "    Name: %.*s", (int)name->slen, name->ptr));
    PJ_LOG(3,(THIS_FILE, "    TTL: %u (%s)", rr->ttl, 
			  spell_ttl(ttl_words, sizeof(ttl_words), rr->ttl)));
    PJ_LOG(3,(THIS_FILE, "    Data length: %u", rr->rdlength));

    if (rr->type == PJ_DNS_TYPE_SRV) {
	PJ_LOG(3,(THIS_FILE, "    SRV: prio=%d, weight=%d %.*s:%d", 
			     rr->rdata.srv.prio, rr->rdata.srv.weight,
			     (int)rr->rdata.srv.target.slen, 
			     rr->rdata.srv.target.ptr,
			     rr->rdata.srv.port));
    } else if (rr->type == PJ_DNS_TYPE_CNAME ||
	       rr->type == PJ_DNS_TYPE_NS ||
	       rr->type == PJ_DNS_TYPE_PTR) 
    {
	PJ_LOG(3,(THIS_FILE, "    Name: %.*s",
		  (int)rr->rdata.cname.name.slen,
		  rr->rdata.cname.name.ptr));
    } else if (rr->type == PJ_DNS_TYPE_A) {
	PJ_LOG(3,(THIS_FILE, "    IP address: %s",
		  pj_inet_ntop2(pj_AF_INET(), &rr->rdata.a.ip_addr,
		  		addr, sizeof(addr))));
    } else if (rr->type == PJ_DNS_TYPE_AAAA) {
	PJ_LOG(3,(THIS_FILE, "    IPv6 address: %s",
		  pj_inet_ntop2(pj_AF_INET6(), &rr->rdata.aaaa.ip_addr,
			        addr, sizeof(addr))));
    }
}


PJ_DEF(void) pj_dns_dump_packet(const pj_dns_parsed_packet *res)
{
    unsigned i;

    PJ_ASSERT_ON_FAIL(res != NULL, return);

    /* Header part */
    PJ_LOG(3,(THIS_FILE, "Domain Name System packet (%s):",
		(PJ_DNS_GET_QR(res->hdr.flags) ? "response" : "query")));
    PJ_LOG(3,(THIS_FILE, " Transaction ID: %d", res->hdr.id));
    PJ_LOG(3,(THIS_FILE, 
	      " Flags: opcode=%d, authoritative=%d, truncated=%d, rcode=%d",
		 PJ_DNS_GET_OPCODE(res->hdr.flags),
		 PJ_DNS_GET_AA(res->hdr.flags),
		 PJ_DNS_GET_TC(res->hdr.flags),
		 PJ_DNS_GET_RCODE(res->hdr.flags)));
    PJ_LOG(3,(THIS_FILE, " Nb of queries: %d", res->hdr.qdcount));
    PJ_LOG(3,(THIS_FILE, " Nb of answer RR: %d", res->hdr.anscount));
    PJ_LOG(3,(THIS_FILE, " Nb of authority RR: %d", res->hdr.nscount));
    PJ_LOG(3,(THIS_FILE, " Nb of additional RR: %d", res->hdr.arcount));
    PJ_LOG(3,(THIS_FILE, ""));

    /* Dump queries */
    if (res->hdr.qdcount) {
	PJ_LOG(3,(THIS_FILE, " Queries:"));

	for (i=0; i<res->hdr.qdcount; ++i) {
	    dump_query(i, &res->q[i]);
	}
	PJ_LOG(3,(THIS_FILE, ""));
    }

    /* Dump answers */
    if (res->hdr.anscount) {
	PJ_LOG(3,(THIS_FILE, " Answers RR:"));

	for (i=0; i<res->hdr.anscount; ++i) {
	    dump_answer(i, &res->ans[i]);
	}
	PJ_LOG(3,(THIS_FILE, ""));
    }

    /* Dump NS sections */
    if (res->hdr.nscount) {
	PJ_LOG(3,(THIS_FILE, " NS Authority RR:"));

	for (i=0; i<res->hdr.nscount; ++i) {
	    dump_answer(i, &res->ns[i]);
	}
	PJ_LOG(3,(THIS_FILE, ""));
    }

    /* Dump Additional info sections */
    if (res->hdr.arcount) {
	PJ_LOG(3,(THIS_FILE, " Additional Info RR:"));

	for (i=0; i<res->hdr.arcount; ++i) {
	    dump_answer(i, &res->arr[i]);
	}
	PJ_LOG(3,(THIS_FILE, ""));
    }

}