blob: c3c776ba43f156e931f366c32daca4ec894d3615 [file] [log] [blame]
Alexandre Lision8af73cb2013-12-10 14:11:20 -05001/* $Id$ */
2/*
3 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
5 *
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.
10 *
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.
15 *
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
19 */
20#include <pjlib-util/dns.h>
21#include <pj/assert.h>
22#include <pj/log.h>
23#include <pj/string.h>
24
25#define THIS_FILE "dns_dump.c"
26#define LEVEL 3
27
28static const char *spell_ttl(char *buf, int size, unsigned ttl)
29{
30#define DAY (3600*24)
31#define HOUR (3600)
32#define MINUTE (60)
33
34 char *p = buf;
35 int len;
36
37 if (ttl > DAY) {
38 len = pj_ansi_snprintf(p, size, "%dd ", ttl/DAY);
39 if (len < 1 || len >= size)
40 return "-err-";
41 size -= len;
42 p += len;
43 ttl %= DAY;
44 }
45
46 if (ttl > HOUR) {
47 len = pj_ansi_snprintf(p, size, "%dh ", ttl/HOUR);
48 if (len < 1 || len >= size)
49 return "-err-";
50 size -= len;
51 p += len;
52 ttl %= HOUR;
53 }
54
55 if (ttl > MINUTE) {
56 len = pj_ansi_snprintf(p, size, "%dm ", ttl/MINUTE);
57 if (len < 1 || len >= size)
58 return "-err-";
59 size -= len;
60 p += len;
61 ttl %= MINUTE;
62 }
63
64 if (ttl > 0) {
65 len = pj_ansi_snprintf(p, size, "%ds ", ttl);
66 if (len < 1 || len >= size)
67 return "-err-";
68 size -= len;
69 p += len;
70 ttl = 0;
71 }
72
73 *p = '\0';
74 return buf;
75}
76
77
78static void dump_query(unsigned index, const pj_dns_parsed_query *q)
79{
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));
86}
87
88static void dump_answer(unsigned index, const pj_dns_parsed_rr *rr)
89{
90 const pj_str_t root_name = { "<Root>", 6 };
91 const pj_str_t *name = &rr->name;
92 char ttl_words[32];
93
94 if (name->slen == 0)
95 name = &root_name;
96
97 PJ_LOG(3,(THIS_FILE, " %d. %s record (type=%d)",
98 index, pj_dns_get_type_name(rr->type),
99 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));
104
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)
114 {
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))));
126 }
127}
128
129
130PJ_DEF(void) pj_dns_dump_packet(const pj_dns_parsed_packet *res)
131{
132 unsigned i;
133
134 PJ_ASSERT_ON_FAIL(res != NULL, return);
135
136 /* Header part */
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));
140 PJ_LOG(3,(THIS_FILE,
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, ""));
151
152 /* Dump queries */
153 if (res->hdr.qdcount) {
154 PJ_LOG(3,(THIS_FILE, " Queries:"));
155
156 for (i=0; i<res->hdr.qdcount; ++i) {
157 dump_query(i, &res->q[i]);
158 }
159 PJ_LOG(3,(THIS_FILE, ""));
160 }
161
162 /* Dump answers */
163 if (res->hdr.anscount) {
164 PJ_LOG(3,(THIS_FILE, " Answers RR:"));
165
166 for (i=0; i<res->hdr.anscount; ++i) {
167 dump_answer(i, &res->ans[i]);
168 }
169 PJ_LOG(3,(THIS_FILE, ""));
170 }
171
172 /* Dump NS sections */
173 if (res->hdr.anscount) {
174 PJ_LOG(3,(THIS_FILE, " NS Authority RR:"));
175
176 for (i=0; i<res->hdr.nscount; ++i) {
177 dump_answer(i, &res->ns[i]);
178 }
179 PJ_LOG(3,(THIS_FILE, ""));
180 }
181
182 /* Dump Additional info sections */
183 if (res->hdr.arcount) {
184 PJ_LOG(3,(THIS_FILE, " Additional Info RR:"));
185
186 for (i=0; i<res->hdr.arcount; ++i) {
187 dump_answer(i, &res->arr[i]);
188 }
189 PJ_LOG(3,(THIS_FILE, ""));
190 }
191
192}
193