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 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
|
/****************************************************************************
** File: dns.c
**
** Author: Mike Borella
**
** Comments: Dump DNS header information
**
** $Log: dns.c,v $
** Revision 1.2 1998/06/12 21:00:59 mborella
** Added log tag
**
*****************************************************************************/
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include "config.h"
#include "dns.h"
extern u_char *packet_end;
/*----------------------------------------------------------------------------
**
** dump_dns()
**
** Parse DNS packet and dump fields
**
**----------------------------------------------------------------------------
*/
void dump_dns(u_char *bp, int length)
{
u_char *ep = bp + length;
u_char *p;
DNSHdr dns_fixed;
int i, t;
u_int16_t qt, qc;
char *dns_query_type(u_int16_t);
u_char *dump_rr(u_char *, u_char *, u_char *);
u_char *parse_labels(u_char *, u_char *, u_char *);
#ifdef DEBUG
void dump_ascii(u_char *, u_char *);
#endif
/*
* Make sure we don't run off the end of the packet
*/
if (ep > packet_end)
ep = packet_end;
/*
* Overlay the first 12 bytes, which are fixed
*/
memcpy((void *) &dns_fixed, bp, sizeof(dns_fixed));
/*
* Print it
*/
printf("-----------------------------------------------------------------\n");
printf(" DNS Packet\n");
printf("-----------------------------------------------------------------\n");
printf("Identification: %d\n", ntohs(dns_fixed.dns_id));
printf("Flags: query/response: %d ", dns_fixed.dns_fl_qr);
if (dns_fixed.dns_fl_qr == 0)
printf("(query)\n");
else
printf("(response)\n");
printf(" opcode %d ", dns_fixed.dns_fl_opcode);
switch(dns_fixed.dns_fl_opcode)
{
case 0: printf("(standard)\n");
break;
case 1: printf("(inverse)\n");
break;
case 2: printf("(server status)\n");
break;
}
printf(" auth answer %d\n", dns_fixed.dns_fl_aa);
printf(" truncated %d\n", dns_fixed.dns_fl_tc);
printf(" recursion req %d\n", dns_fixed.dns_fl_rd);
printf(" recursion avail %d\n", dns_fixed.dns_fl_ra);
printf(" zero %d\n", dns_fixed.dns_fl_zero);
printf(" return code %d ", dns_fixed.dns_fl_rcode);
switch(dns_fixed.dns_fl_rcode)
{
case 0: printf("(no error)\n");
break;
case 1: printf("(format error)\n");
break;
case 2: printf("(server error)\n");
break;
case 3: printf("(name error)\n");
break;
case 4: printf("(not implemented)\n");
break;
case 5: printf("(service refused)\n");
break;
}
printf("# of questions %d\n", ntohs(dns_fixed.dns_num_q));
printf("# of answer RRs %d\n", ntohs(dns_fixed.dns_num_ans));
printf("# of authorization RRs %d\n", ntohs(dns_fixed.dns_num_auth));
printf("# of additional RRs %d\n", ntohs(dns_fixed.dns_num_add));
/*
* Do the question part of the packet.
*/
p = bp + sizeof(DNSHdr);
i = ntohs(dns_fixed.dns_num_q);
while (i > 0)
{
printf("Question: query name ");
p = parse_labels(p, bp, ep);
memcpy((void *) &qt, p, sizeof(qt));
p = p + sizeof(qt);
memcpy((void *) &qc, p, sizeof(qc));
p = p + sizeof(qc);
printf(" query type %d %s\n", ntohs(qt),
dns_query_type(ntohs(qt)));
printf(" query class %d", ntohs(qc));
if (ntohs(qc) == 1)
printf(" (Internet)");
printf("\n");
i--;
}
/*
* dump the resource records for the answers
*/
i = ntohs(dns_fixed.dns_num_ans);
t = i;
while (i > 0)
{
printf("Answer %d: ", t-i+1);
p = dump_rr(p, bp, ep);
i--;
}
/*
* dump the resource records for the authoritative answers
*/
i = ntohs(dns_fixed.dns_num_auth);
t = i;
while (i > 0)
{
printf("Auth %d: ", t-i+1);
p = dump_rr(p, bp, ep);
i--;
}
/*
* dump the resource records for the additional info
*/
i = ntohs(dns_fixed.dns_num_add);
t = i;
while (i > 0)
{
printf("Adtnl %d: ", t-i+1);
#ifdef DEBUG
dump_ascii(p, ep);
#endif
p = dump_rr(p, bp, ep);
i--;
}
}
/*----------------------------------------------------------------------------
**
** dns_query_type()
**
** Return a string describing the numeric value of a DNS query type
**
**----------------------------------------------------------------------------
*/
char *dns_query_type(u_int16_t t)
{
static char answer[32];
switch(t)
{
case 1: strncpy(answer, "(IP address)", 32);
break;
case 2: strncpy(answer, "(name server)", 32);
break;
case 5: strncpy(answer, "(canonical name)", 32);
break;
case 12: strncpy(answer, "(pointer record)", 32);
break;
case 13: strncpy(answer, "(host info)", 32);
break;
case 15: strncpy(answer, "(MX record)", 32);
break;
case 252: strncpy(answer, "(request zone transfer)", 32);
break;
case 255: strncpy(answer, "(request all records)", 32);
break;
default: answer[0] = '\0';
break;
}
return answer;
}
/*----------------------------------------------------------------------------
**
** dump_rr()
**
** Print the contents of a resource record
**
**----------------------------------------------------------------------------
*/
u_char *dump_rr(u_char *p, u_char *bp, u_char *ep)
{
int i;
u_int16_t qt, qc;
u_int32_t ttl;
u_int16_t reslen;
struct in_addr ipaddr;
u_char *parse_labels(u_char *, u_char *, u_char *);
printf("server name ");
p = parse_labels(p, bp, ep);
/*
* Do type and class
*/
memcpy((void *) &qt, p, sizeof(qt));
p = p + sizeof(qt);
memcpy((void *) &qc, p, sizeof(qc));
p = p + sizeof(qc);
printf(" type %d %s\n", ntohs(qt),
dns_query_type(ntohs(qt)));
printf(" class %d", ntohs(qc));
if (ntohs(qc) == 1)
printf(" (Internet)");
printf("\n");
/*
* Do TTL
*/
memcpy((void *) &ttl, p, sizeof(ttl));
p = p + sizeof(ttl);
printf(" ttl %d seconds\n", ntohs(ttl));
/*
* Do resource length
*/
memcpy((void *) &reslen, p, sizeof(reslen));
p = p + sizeof(reslen);
printf(" length %d\n", ntohs(reslen));
/*
* Do resource data.
*/
switch(ntohs(qt))
{
case 1:
for (i=1; i<= ntohs(reslen); i += 4)
{
memcpy((void *) &ipaddr, p, sizeof(ipaddr));
p = p + sizeof(ipaddr);
printf(" IP address %s\n", inet_ntoa(ipaddr));
}
break;
case 2:
printf(" auth host ");
p = parse_labels(p, bp, ep);
break;
case 5:
printf(" canon host ");
p = parse_labels(p, bp, ep);
break;
default:
p = p + ntohs(reslen);
}
return p;
}
/*----------------------------------------------------------------------------
**
** parse_labels()
**
** Recursively parse a label entry in a DNS packet
**
**----------------------------------------------------------------------------
*/
u_char *parse_labels(u_char *p, u_char *bp, u_char *ep)
{
u_int8_t count;
u_int16_t offset;
while(1)
{
count = (u_int8_t) *p;
if (count >= 192)
{
/*
* There's a pointer in this label. Sigh. Let's grab the
* 14 low-order bits and run with them...
*/
p++;
offset = count - 192;
offset = offset << 8;
offset = offset + *p;
p++;
parse_labels(bp+offset, bp, ep);
return p;
}
else
p++;
if (count == 0)
break;
while (count > 0)
{
if (p <= ep)
printf("%c", *p);
else
{
printf("\nPacket length exceeded\n");
return p;
}
p++;
count--;
}
printf(".");
}
printf("\n");
return p;
}
|