summaryrefslogtreecommitdiff
path: root/tools/resolv-test.c
blob: db5b3fe8a932ea3feecece351aa5713914db9ffe (plain)
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
/*
 *
 *  Connection Manager
 *
 *  Copyright (C) 2007-2010  Intel Corporation. All rights reserved.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 2 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <unistd.h>
#include <string.h>
#include <resolv.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <arpa/nameser.h>

static int do_connect(const char *server)
{
	struct sockaddr_in sin;
	int sk;

	sk = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
	//sk = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
	if (sk < 0)
		return -1;

	memset(&sin, 0, sizeof(sin));
	sin.sin_family = AF_INET;
	sin.sin_port = htons(53);
	sin.sin_addr.s_addr = inet_addr(server);

	if (connect(sk, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
		close(sk);
		return -1;
	}

	return sk;
}

int main(int argc, char *argv[])
{
	ns_msg msg;
	ns_rr rr;
	int rcode;
	const char *nameserver;
	unsigned char buf[4096];
	int i, sk, err, len, off = 0;

	if (argc < 2) {
		printf("missing argument\n");
		return 1;
	}

	if (argc > 2)
		nameserver = argv[2];
	else
		nameserver = "127.0.0.1";

	sk = do_connect(nameserver);
	if (sk < 0) {
		printf("Can't connect\n");
		return 1;
	}

	len = res_mkquery(ns_o_query, argv[1], ns_c_in, ns_t_a,
				NULL, 0, NULL, buf + off, sizeof(buf) - off);
	printf("query len: %d\n", len);

	if (off > 0) {
		buf[0] = len >> 8;
		buf[1] = len & 0xff;
	}

	//for (i = 0; i < len + off; i++)
	//	printf("%02x ", buf[i]);
	//printf("\n");

	err = send(sk, buf, len + off, 0);
	printf("send result: %d\n", err);

	len = recv(sk, buf, sizeof(buf), 0);
	printf("answer len: %d\n", len);

	//for (i = 0; i < len + off; i++)
	//	printf("%02x ", buf[i]);
	//printf("\n");

	close(sk);

	ns_initparse(buf + off, len - off, &msg);

	rcode = ns_msg_getflag(msg, ns_f_rcode);

	printf("msg id: 0x%04x\n", ns_msg_id(msg));
	printf("msg rcode: %d\n", rcode);
	printf("msg count: %d\n", ns_msg_count(msg, ns_s_an));

	for (i = 0; i < ns_msg_count(msg, ns_s_an); i++) {
		char result[100];

		ns_parserr(&msg, ns_s_an, i, &rr);

		if (ns_rr_class(rr) != ns_c_in)
			continue;

		if (ns_rr_type(rr) != ns_t_a)
			continue;

		if (ns_rr_rdlen(rr) != NS_INADDRSZ)
			continue;

		inet_ntop(AF_INET, ns_rr_rdata(rr), result, sizeof(result));

		printf("result: %s\n", result);
	}

	return 0;
}