]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/testcode/pktview.c
2 * testcode/pktview.c - debug program to disassemble a DNS packet.
4 * Copyright (c) 2007, NLnet Labs. All rights reserved.
6 * This software is open source.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * Redistributions of source code must retain the above copyright notice,
13 * this list of conditions and the following disclaimer.
15 * Redistributions in binary form must reproduce the above copyright notice,
16 * this list of conditions and the following disclaimer in the documentation
17 * and/or other materials provided with the distribution.
19 * Neither the name of the NLNET LABS nor the names of its contributors may
20 * be used to endorse or promote products derived from this software without
21 * specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
29 * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
30 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
31 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
32 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
33 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 * This program shows a dns packet wire format.
44 #include "util/data/dname.h"
45 #include "util/data/msgparse.h"
46 #include "testcode/unitmain.h"
47 #include "testcode/readhex.h"
48 #include "ldns/sbuffer.h"
49 #include "ldns/parseutil.h"
51 /** usage information for pktview */
52 static void usage(char* argv
[])
54 printf("usage: %s\n", argv
[0]);
55 printf("present hex packet on stdin.\n");
60 static void read_input(sldns_buffer
* pkt
, FILE* in
)
64 while(fgets(np
, (int)sizeof(buf
) - (np
-buf
), in
)) {
65 if(buf
[0] == ';') /* comment */
72 /** analyze domain name in packet, possibly compressed */
73 static void analyze_dname(sldns_buffer
* pkt
)
75 size_t oldpos
= sldns_buffer_position(pkt
);
77 printf("[pos %d] dname: ", (int)oldpos
);
78 dname_print(stdout
, pkt
, sldns_buffer_current(pkt
));
79 len
= pkt_dname_len(pkt
);
80 printf(" len=%d", (int)len
);
81 if(sldns_buffer_position(pkt
)-oldpos
!= len
)
82 printf(" comprlen=%d\n",
83 (int)(sldns_buffer_position(pkt
)-oldpos
));
87 /** analyze rdata in packet */
88 static void analyze_rdata(sldns_buffer
*pkt
, const sldns_rr_descriptor
* desc
,
92 int count
= (int)desc
->_dname_count
;
94 while(rdlen
> 0 && count
) {
95 switch(desc
->_wireformat
[rdf
]) {
96 case LDNS_RDF_TYPE_DNAME
:
97 oldpos
= sldns_buffer_position(pkt
);
99 rdlen
-= sldns_buffer_position(pkt
)-oldpos
;
103 case LDNS_RDF_TYPE_STR
:
104 len
= sldns_buffer_current(pkt
)[0] + 1;
107 len
= get_rdf_size(desc
->_wireformat
[rdf
]);
110 printf(" wf[%d]", (int)len
);
111 sldns_buffer_skip(pkt
, (ssize_t
)len
);
118 printf(" remain[%d]\n", (int)rdlen
);
119 for(i
=0; i
<rdlen
; i
++)
120 printf(" %2.2X", (unsigned)sldns_buffer_current(pkt
)[i
]);
124 sldns_buffer_skip(pkt
, (ssize_t
)rdlen
);
127 /** analyze rr in packet */
128 static void analyze_rr(sldns_buffer
* pkt
, int q
)
130 uint16_t type
, dclass
, len
;
133 type
= sldns_buffer_read_u16(pkt
);
134 dclass
= sldns_buffer_read_u16(pkt
);
135 printf("type %s(%d)", sldns_rr_descript(type
)?
136 sldns_rr_descript(type
)->_name
: "??" , (int)type
);
137 printf(" class %s(%d) ", sldns_lookup_by_id(sldns_rr_classes
,
138 (int)dclass
)?sldns_lookup_by_id(sldns_rr_classes
,
139 (int)dclass
)->name
:"??", (int)dclass
);
143 ttl
= sldns_buffer_read_u32(pkt
);
144 printf(" ttl %d (0x%x)", (int)ttl
, (unsigned)ttl
);
145 len
= sldns_buffer_read_u16(pkt
);
146 printf(" rdata len %d:\n", (int)len
);
147 if(sldns_rr_descript(type
))
148 analyze_rdata(pkt
, sldns_rr_descript(type
), len
);
149 else sldns_buffer_skip(pkt
, (ssize_t
)len
);
154 static void analyze(sldns_buffer
* pkt
)
156 uint16_t i
, f
, qd
, an
, ns
, ar
;
158 printf("packet length %d\n", (int)sldns_buffer_limit(pkt
));
159 if(sldns_buffer_limit(pkt
) < 12) return;
161 i
= sldns_buffer_read_u16(pkt
);
162 printf("id (hostorder): %d (0x%x)\n", (int)i
, (unsigned)i
);
163 f
= sldns_buffer_read_u16(pkt
);
164 printf("flags: 0x%x\n", (unsigned)f
);
165 qd
= sldns_buffer_read_u16(pkt
);
166 printf("qdcount: %d\n", (int)qd
);
167 an
= sldns_buffer_read_u16(pkt
);
168 printf("ancount: %d\n", (int)an
);
169 ns
= sldns_buffer_read_u16(pkt
);
170 printf("nscount: %d\n", (int)ns
);
171 ar
= sldns_buffer_read_u16(pkt
);
172 printf("arcount: %d\n", (int)ar
);
174 printf(";-- query section\n");
175 while(sldns_buffer_remaining(pkt
) > 0) {
177 printf(";-- answer section\n");
178 if(rrnum
== (int)qd
+(int)an
)
179 printf(";-- authority section\n");
180 if(rrnum
== (int)qd
+(int)an
+(int)ns
)
181 printf(";-- additional section\n");
182 printf("rr %d ", rrnum
);
183 analyze_rr(pkt
, rrnum
< (int)qd
);
188 /** main program for pktview */
189 int main(int argc
, char* argv
[])
191 sldns_buffer
* pkt
= sldns_buffer_new(65553);
195 if(!pkt
) fatal_exit("out of memory");
197 read_input(pkt
, stdin
);
200 sldns_buffer_free(pkt
);