Add example test tool for DNS resolver client
[platform/upstream/connman.git] / tools / resolv-test.c
1 /*
2  *
3  *  Connection Manager
4  *
5  *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
6  *
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License version 2 as
9  *  published by the Free Software Foundation.
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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include <config.h>
24 #endif
25
26 #include <unistd.h>
27 #include <string.h>
28 #include <resolv.h>
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
31 #include <arpa/nameser.h>
32
33 static int do_connect(const char *server)
34 {
35         struct sockaddr_in sin;
36         int sk;
37
38         sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
39         //sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
40         if (sk < 0)
41                 return -1;
42
43         memset(&sin, 0, sizeof(sin));
44         sin.sin_family = AF_INET;
45         sin.sin_port = htons(53);
46         sin.sin_addr.s_addr = inet_addr(server);
47
48         if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
49                 close(sk);
50                 return -1;
51         }
52
53         return sk;
54 }
55
56 int main(int argc, char *argv[])
57 {
58         ns_msg msg;
59         ns_rr rr;
60         int rcode;
61         const char *nameserver;
62         unsigned char buf[4096];
63         int i, sk, err, len, off = 0;
64
65         if (argc < 2) {
66                 printf("missing argument\n");
67                 return 1;
68         }
69
70         if (argc > 2)
71                 nameserver = argv[2];
72         else
73                 nameserver = "127.0.0.1";
74
75         sk = do_connect(nameserver);
76         if (sk < 0) {
77                 printf("Can't connect\n");
78                 return 1;
79         }
80
81         len = res_mkquery(ns_o_query, argv[1], ns_c_in, ns_t_a,
82                                 NULL, 0, NULL, buf + off, sizeof(buf) - off);
83         printf("query len: %d\n", len);
84
85         if (off > 0) {
86                 buf[0] = len >> 8;
87                 buf[1] = len & 0xff;
88         }
89
90         //for (i = 0; i < len + off; i++)
91         //      printf("%02x ", buf[i]);
92         //printf("\n");
93
94         err = send(sk, buf, len + off, 0);
95         printf("send result: %d\n", err);
96
97         len = recv(sk, buf, sizeof(buf), 0);
98         printf("answer len: %d\n", len);
99
100         //for (i = 0; i < len + off; i++)
101         //      printf("%02x ", buf[i]);
102         //printf("\n");
103
104         close(sk);
105
106         ns_initparse(buf + off, len - off, &msg);
107
108         rcode = ns_msg_getflag(msg, ns_f_rcode);
109
110         printf("msg id: 0x%04x\n", ns_msg_id(msg));
111         printf("msg rcode: %d\n", rcode);
112         printf("msg count: %d\n", ns_msg_count(msg, ns_s_an));
113
114         for (i = 0; i < ns_msg_count(msg, ns_s_an); i++) {
115                 char result[100];
116
117                 ns_parserr(&msg, ns_s_an, i, &rr);
118
119                 if (ns_rr_class(rr) != ns_c_in)
120                         continue;
121
122                 if (ns_rr_type(rr) != ns_t_a)
123                         continue;
124
125                 if (ns_rr_rdlen(rr) != NS_INADDRSZ)
126                         continue;
127
128                 inet_ntop(AF_INET, ns_rr_rdata(rr), result, sizeof(result));
129
130                 printf("result: %s\n", result);
131         }
132
133         return 0;
134 }