1 /* $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 
spell_ttl(char * buf,int size,unsigned ttl)28 static 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 
dump_query(unsigned index,const pj_dns_parsed_query * q)78 static 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 
dump_answer(unsigned index,const pj_dns_parsed_rr * rr)88 static 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     char addr[PJ_INET6_ADDRSTRLEN];
94 
95     if (name->slen == 0)
96 	name = &root_name;
97 
98     PJ_LOG(3,(THIS_FILE, " %d. %s record (type=%d)",
99 			 index, pj_dns_get_type_name(rr->type),
100 			rr->type));
101     PJ_LOG(3,(THIS_FILE, "    Name: %.*s", (int)name->slen, name->ptr));
102     PJ_LOG(3,(THIS_FILE, "    TTL: %u (%s)", rr->ttl,
103 			  spell_ttl(ttl_words, sizeof(ttl_words), rr->ttl)));
104     PJ_LOG(3,(THIS_FILE, "    Data length: %u", rr->rdlength));
105 
106     if (rr->type == PJ_DNS_TYPE_SRV) {
107 	PJ_LOG(3,(THIS_FILE, "    SRV: prio=%d, weight=%d %.*s:%d",
108 			     rr->rdata.srv.prio, rr->rdata.srv.weight,
109 			     (int)rr->rdata.srv.target.slen,
110 			     rr->rdata.srv.target.ptr,
111 			     rr->rdata.srv.port));
112     } else if (rr->type == PJ_DNS_TYPE_CNAME ||
113 	       rr->type == PJ_DNS_TYPE_NS ||
114 	       rr->type == PJ_DNS_TYPE_PTR)
115     {
116 	PJ_LOG(3,(THIS_FILE, "    Name: %.*s",
117 		  (int)rr->rdata.cname.name.slen,
118 		  rr->rdata.cname.name.ptr));
119     } else if (rr->type == PJ_DNS_TYPE_A) {
120 	PJ_LOG(3,(THIS_FILE, "    IP address: %s",
121 		  pj_inet_ntop2(pj_AF_INET(), &rr->rdata.a.ip_addr,
122 		  		addr, sizeof(addr))));
123     } else if (rr->type == PJ_DNS_TYPE_AAAA) {
124 	PJ_LOG(3,(THIS_FILE, "    IPv6 address: %s",
125 		  pj_inet_ntop2(pj_AF_INET6(), &rr->rdata.aaaa.ip_addr,
126 			        addr, sizeof(addr))));
127     }
128 }
129 
130 
pj_dns_dump_packet(const pj_dns_parsed_packet * res)131 PJ_DEF(void) pj_dns_dump_packet(const pj_dns_parsed_packet *res)
132 {
133     unsigned i;
134 
135     PJ_ASSERT_ON_FAIL(res != NULL, return);
136 
137     /* Header part */
138     PJ_LOG(3,(THIS_FILE, "Domain Name System packet (%s):",
139 		(PJ_DNS_GET_QR(res->hdr.flags) ? "response" : "query")));
140     PJ_LOG(3,(THIS_FILE, " Transaction ID: %d", res->hdr.id));
141     PJ_LOG(3,(THIS_FILE,
142 	      " Flags: opcode=%d, authoritative=%d, truncated=%d, rcode=%d",
143 		 PJ_DNS_GET_OPCODE(res->hdr.flags),
144 		 PJ_DNS_GET_AA(res->hdr.flags),
145 		 PJ_DNS_GET_TC(res->hdr.flags),
146 		 PJ_DNS_GET_RCODE(res->hdr.flags)));
147     PJ_LOG(3,(THIS_FILE, " Nb of queries: %d", res->hdr.qdcount));
148     PJ_LOG(3,(THIS_FILE, " Nb of answer RR: %d", res->hdr.anscount));
149     PJ_LOG(3,(THIS_FILE, " Nb of authority RR: %d", res->hdr.nscount));
150     PJ_LOG(3,(THIS_FILE, " Nb of additional RR: %d", res->hdr.arcount));
151     PJ_LOG(3,(THIS_FILE, ""));
152 
153     /* Dump queries */
154     if (res->hdr.qdcount) {
155 	PJ_LOG(3,(THIS_FILE, " Queries:"));
156 
157 	for (i=0; i<res->hdr.qdcount; ++i) {
158 	    dump_query(i, &res->q[i]);
159 	}
160 	PJ_LOG(3,(THIS_FILE, ""));
161     }
162 
163     /* Dump answers */
164     if (res->hdr.anscount) {
165 	PJ_LOG(3,(THIS_FILE, " Answers RR:"));
166 
167 	for (i=0; i<res->hdr.anscount; ++i) {
168 	    dump_answer(i, &res->ans[i]);
169 	}
170 	PJ_LOG(3,(THIS_FILE, ""));
171     }
172 
173     /* Dump NS sections */
174     if (res->hdr.nscount) {
175 	PJ_LOG(3,(THIS_FILE, " NS Authority RR:"));
176 
177 	for (i=0; i<res->hdr.nscount; ++i) {
178 	    dump_answer(i, &res->ns[i]);
179 	}
180 	PJ_LOG(3,(THIS_FILE, ""));
181     }
182 
183     /* Dump Additional info sections */
184     if (res->hdr.arcount) {
185 	PJ_LOG(3,(THIS_FILE, " Additional Info RR:"));
186 
187 	for (i=0; i<res->hdr.arcount; ++i) {
188 	    dump_answer(i, &res->arr[i]);
189 	}
190 	PJ_LOG(3,(THIS_FILE, ""));
191     }
192 
193 }
194 
195