1
/* $Id: dns_dump.c 3553 2011-05-05 06:14:19Z nanang $ */
3
* Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4
* Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
6
* This program is free software; you can redistribute it and/or modify
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
11
* This program is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
* GNU General Public License for more details.
16
* You should have received a copy of the GNU General Public License
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
#include <pjlib-util/dns.h>
21
#include <pj/assert.h>
23
#include <pj/string.h>
25
#define THIS_FILE "dns_dump.c"
28
static const char *spell_ttl(char *buf, int size, unsigned ttl)
38
len = pj_ansi_snprintf(p, size, "%dd ", ttl/DAY);
47
len = pj_ansi_snprintf(p, size, "%dh ", ttl/HOUR);
56
len = pj_ansi_snprintf(p, size, "%dm ", ttl/MINUTE);
65
len = pj_ansi_snprintf(p, size, "%ds ", ttl);
78
static void dump_query(unsigned index, const pj_dns_parsed_query *q)
80
PJ_LOG(3,(THIS_FILE, " %d. Name: %.*s",
81
index, (int)q->name.slen, q->name.ptr));
82
PJ_LOG(3,(THIS_FILE, " Type: %s (%d)",
83
pj_dns_get_type_name(q->type), q->type));
84
PJ_LOG(3,(THIS_FILE, " Class: %s (%d)",
85
(q->dnsclass==1 ? "IN" : "<Unknown>"), q->dnsclass));
88
static void dump_answer(unsigned index, const pj_dns_parsed_rr *rr)
90
const pj_str_t root_name = { "<Root>", 6 };
91
const pj_str_t *name = &rr->name;
97
PJ_LOG(3,(THIS_FILE, " %d. %s record (type=%d)",
98
index, pj_dns_get_type_name(rr->type),
100
PJ_LOG(3,(THIS_FILE, " Name: %.*s", (int)name->slen, name->ptr));
101
PJ_LOG(3,(THIS_FILE, " TTL: %u (%s)", rr->ttl,
102
spell_ttl(ttl_words, sizeof(ttl_words), rr->ttl)));
103
PJ_LOG(3,(THIS_FILE, " Data length: %u", rr->rdlength));
105
if (rr->type == PJ_DNS_TYPE_SRV) {
106
PJ_LOG(3,(THIS_FILE, " SRV: prio=%d, weight=%d %.*s:%d",
107
rr->rdata.srv.prio, rr->rdata.srv.weight,
108
(int)rr->rdata.srv.target.slen,
109
rr->rdata.srv.target.ptr,
110
rr->rdata.srv.port));
111
} else if (rr->type == PJ_DNS_TYPE_CNAME ||
112
rr->type == PJ_DNS_TYPE_NS ||
113
rr->type == PJ_DNS_TYPE_PTR)
115
PJ_LOG(3,(THIS_FILE, " Name: %.*s",
116
(int)rr->rdata.cname.name.slen,
117
rr->rdata.cname.name.ptr));
118
} else if (rr->type == PJ_DNS_TYPE_A) {
119
PJ_LOG(3,(THIS_FILE, " IP address: %s",
120
pj_inet_ntoa(rr->rdata.a.ip_addr)));
121
} else if (rr->type == PJ_DNS_TYPE_AAAA) {
122
char addr[PJ_INET6_ADDRSTRLEN];
123
PJ_LOG(3,(THIS_FILE, " IPv6 address: %s",
124
pj_inet_ntop2(pj_AF_INET6(), &rr->rdata.aaaa.ip_addr,
125
addr, sizeof(addr))));
130
PJ_DEF(void) pj_dns_dump_packet(const pj_dns_parsed_packet *res)
134
PJ_ASSERT_ON_FAIL(res != NULL, return);
137
PJ_LOG(3,(THIS_FILE, "Domain Name System packet (%s):",
138
(PJ_DNS_GET_QR(res->hdr.flags) ? "response" : "query")));
139
PJ_LOG(3,(THIS_FILE, " Transaction ID: %d", res->hdr.id));
141
" Flags: opcode=%d, authoritative=%d, truncated=%d, rcode=%d",
142
PJ_DNS_GET_OPCODE(res->hdr.flags),
143
PJ_DNS_GET_AA(res->hdr.flags),
144
PJ_DNS_GET_TC(res->hdr.flags),
145
PJ_DNS_GET_RCODE(res->hdr.flags)));
146
PJ_LOG(3,(THIS_FILE, " Nb of queries: %d", res->hdr.qdcount));
147
PJ_LOG(3,(THIS_FILE, " Nb of answer RR: %d", res->hdr.anscount));
148
PJ_LOG(3,(THIS_FILE, " Nb of authority RR: %d", res->hdr.nscount));
149
PJ_LOG(3,(THIS_FILE, " Nb of additional RR: %d", res->hdr.arcount));
150
PJ_LOG(3,(THIS_FILE, ""));
153
if (res->hdr.qdcount) {
154
PJ_LOG(3,(THIS_FILE, " Queries:"));
156
for (i=0; i<res->hdr.qdcount; ++i) {
157
dump_query(i, &res->q[i]);
159
PJ_LOG(3,(THIS_FILE, ""));
163
if (res->hdr.anscount) {
164
PJ_LOG(3,(THIS_FILE, " Answers RR:"));
166
for (i=0; i<res->hdr.anscount; ++i) {
167
dump_answer(i, &res->ans[i]);
169
PJ_LOG(3,(THIS_FILE, ""));
172
/* Dump NS sections */
173
if (res->hdr.anscount) {
174
PJ_LOG(3,(THIS_FILE, " NS Authority RR:"));
176
for (i=0; i<res->hdr.nscount; ++i) {
177
dump_answer(i, &res->ns[i]);
179
PJ_LOG(3,(THIS_FILE, ""));
182
/* Dump Additional info sections */
183
if (res->hdr.arcount) {
184
PJ_LOG(3,(THIS_FILE, " Additional Info RR:"));
186
for (i=0; i<res->hdr.arcount; ++i) {
187
dump_answer(i, &res->arr[i]);
189
PJ_LOG(3,(THIS_FILE, ""));