5 * Copyright (C) 2007-2010 Intel Corporation. All rights reserved.
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.
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.
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
29 #include <sys/socket.h>
30 #include <arpa/inet.h>
31 #include <arpa/nameser.h>
33 static int do_connect(const char *server)
35 struct sockaddr_in sin;
38 sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
39 //sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
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);
48 if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
56 int main(int argc, char *argv[])
61 const char *nameserver;
62 unsigned char buf[4096];
63 int i, sk, err, len, off = 0;
66 printf("missing argument\n");
73 nameserver = "127.0.0.1";
75 sk = do_connect(nameserver);
77 printf("Can't connect\n");
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);
90 //for (i = 0; i < len + off; i++)
91 // printf("%02x ", buf[i]);
94 err = send(sk, buf, len + off, 0);
95 printf("send result: %d\n", err);
97 len = recv(sk, buf, sizeof(buf), 0);
98 printf("answer len: %d\n", len);
100 //for (i = 0; i < len + off; i++)
101 // printf("%02x ", buf[i]);
106 ns_initparse(buf + off, len - off, &msg);
108 rcode = ns_msg_getflag(msg, ns_f_rcode);
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));
114 for (i = 0; i < ns_msg_count(msg, ns_s_an); i++) {
117 ns_parserr(&msg, ns_s_an, i, &rr);
119 if (ns_rr_class(rr) != ns_c_in)
122 if (ns_rr_type(rr) != ns_t_a)
125 if (ns_rr_rdlen(rr) != NS_INADDRSZ)
128 inet_ntop(AF_INET, ns_rr_rdata(rr), result, sizeof(result));
130 printf("result: %s\n", result);