1 /*
2     Copyright (c) 2011 Verisign, Inc. All rights reserved.
3 
4     Redistribution and use in source and binary forms, with or without
5     modification, are permitted provided that the following conditions
6     are met:
7 
8     1. Redistributions of source code must retain the above copyright
9        notice, this list of conditions and the following disclaimer.
10     2. Redistributions in binary form must reproduce the above copyright
11        notice, this list of conditions and the following disclaimer in the
12        documentation and/or other materials provided with the distribution.
13     3. The name of the authors may not be used to endorse or promote products
14        derived from this software without specific prior written permission.
15 
16     THIS SOFTWARE IS PROVIDED BY THE AUTHORS ``AS IS'' AND ANY EXPRESS OR
17     IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18     OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19     IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20     INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21     NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22     DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23     THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24     (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25     THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 
27 */
28 
29 #include <stdio.h>
30 #include <string.h>
31 #include <string>
32 #include <iostream>
33 #include <arpa/inet.h>
34 #include <vantages/dns_rr.h>
35 #include <vantages/dns_name.h>
36 
37 #include "functions.h"
38 
39 /**
40  * Convert a C styled string to an int representing an ip address
41  * @param ipstr
42  * @return an int representing an ip address
43  */
str_to_ip(char * ipstr)44 unsigned int str_to_ip(char *ipstr) {
45     int iRet = 0;
46 
47     struct in_addr tAddr;
48     memset(&tAddr, 0, sizeof (tAddr));
49     int iLen = strlen(ipstr);
50     if (iLen > 15) {
51         fprintf(stderr, "IPv4 addresses are not that long (%d chars)\n", iLen);
52     } else if (!inet_pton(AF_INET, ipstr, &tAddr)) {
53         fprintf(stderr, "Unable to convert IP '%s' to number.\n", ipstr);
54     } else {
55         iRet = (unsigned) htonl(tAddr.s_addr);
56     }
57 
58     return iRet;
59 }
60 
61 /**
62  * Output the header to stdout
63  * @param header
64  */
printHeader(DnsHeader & header)65 void printHeader(DnsHeader &header) {
66     int opcode = header.getOpcode();
67 
68     fprintf(stdout, "%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", header.response(),
69             opcode, header.get_aa(), header.get_tc(), header.get_rd(), header.get_ra(),
70             header.get_ad(), header.get_cd(), header.rcode(), header.qd_count(), header.an_count(),
71             header.ns_count(), header.ar_count());
72 
73 }
74 
75 /**
76  * Output the name, class and type to stdout
77  * @param packet
78  */
printNameClassType(DnsPacket & packet)79 void printNameClassType(DnsPacket &packet) {
80     RRList_t questions;
81     packet.getQuestions(questions);
82     DnsRR* question = questions.front();
83     fprintf(stdout, "%s %d %d\n",
84             question->get_name()->toString().c_str(),
85             question->get_class(),
86             (int)question->type());
87 }
88