File: resolver.c

package info (click to toggle)
aiccu 20070115-9
  • links: PTS, VCS
  • area: main
  • in suites: lenny
  • size: 540 kB
  • ctags: 616
  • sloc: ansic: 4,660; sh: 442; makefile: 367
file content (209 lines) | stat: -rw-r--r-- 5,056 bytes parent folder | download | duplicates (4)
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
/**********************************************************
 SixXS - Automatic IPv6 Connectivity Configuration Utility
***********************************************************
 Copyright 2003-2005 SixXS - http://www.sixxs.net
***********************************************************
 common/resolver.c - Simple DNS RR lookup function
***********************************************************
 $Author: jeroen $
 $Id: resolver.c,v 1.3 2006-07-23 14:13:57 jeroen Exp $
 $Date: 2006-07-23 14:13:57 $
**********************************************************/

#include <stdlib.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#include "resolver.h"

#ifndef _WIN32
#include <unistd.h>
#include <netinet/in.h>
#include <arpa/nameser.h>
#include <resolv.h>
#include <netdb.h>

int getrrs(const char *label, int rrtype, void gotrec(unsigned int num, int type, const char *record))
{
#ifdef _LINUX
	struct __res_state	res;
#endif
	unsigned char		answer[8192];
	HEADER			*header = (HEADER *)answer;
	char			buf[2048];
	int			ret, count;
	unsigned int		i,j,k,rrnum = 0;
	unsigned char		*startptr, *endptr, *ptr;
	uint16_t		type = 0, class = 0;
	uint32_t		ttl = 0;

#ifdef _LINUX
	memset(&res, 0, sizeof(res));
	res.options = RES_DEBUG;
	res_ninit(&res);
#else
	res_init();
#endif

	memset(answer, 0, sizeof(answer));
#ifdef _LINUX
	ret = res_nquery(&res, label, C_IN, rrtype, answer, sizeof(answer));
#else
	ret = res_query(label, C_IN, rrtype, answer, sizeof(answer));
#endif
	if (ret < 0) return -1;

	/* Our start and end */
	startptr = &answer[0];
	endptr = &answer[ret];

	/* Skip the header */
	ptr = startptr + HFIXEDSZ;

	/* Skip Query part */
	for (count = ntohs(header->qdcount); count--; ptr += ret + QFIXEDSZ)
	{
		if ((ret = dn_skipname(ptr, endptr)) < 0) return -1;
	}

	/* Only look at the Answer section */
	count = ntohs(header->ancount);

	/* Go through all the Answer records */
	while (ptr < endptr && count > 0)
	{
		rrnum++;

		memset(buf, 0, sizeof(buf));
		ret = dn_expand (startptr, endptr, ptr, buf, sizeof(buf));
		if (ret < 0) break;
		ptr += ret;

		if (ptr + INT16SZ + INT16SZ + INT32SZ >= endptr) break;

		/* Get the type */
		NS_GET16(type, ptr);

		/* Get the class */
		NS_GET16(class, ptr);

		/* Get the TTL */
		NS_GET32(ttl, ptr);

		/* Get the RDLength */
		NS_GET16(ret, ptr);

		memset(buf, 0, sizeof(buf));

		switch (type)
		{
		case T_TXT:
			for (k = ret, j = 0; j < k && &ptr[j] < endptr; j += (i+1))
			{
				i = ptr[j];
				memcpy(buf, &ptr[j+1], i > sizeof(buf) ? sizeof(buf) : i);
				buf[i > sizeof(buf) ? sizeof(buf) : i] = '\0';
				if (rrtype == type || rrtype == T_ANY) gotrec(rrnum, type, buf);
			}
			break;

		case T_A:
			inet_ntop(AF_INET, ptr, buf, sizeof(buf));
			if (rrtype == type || rrtype == T_ANY) gotrec(rrnum, type, buf);
			break;

		case T_AAAA:
			inet_ntop(AF_INET6, ptr, buf, sizeof(buf));
			if (rrtype == type || rrtype == T_ANY) gotrec(rrnum, type, buf);
			break;

		case T_MX:
			/* Get the MX preference */
			NS_GET16(ttl, ptr);
			ret = dn_expand(startptr, endptr, ptr, buf, sizeof(buf));
			if (rrtype == type || rrtype == T_ANY) gotrec(rrnum, type, buf);
			break;

		case T_NS:
			ret = dn_expand(startptr, endptr, ptr, buf, sizeof(buf));
			if (rrtype == type || rrtype == T_ANY) gotrec(rrnum, type, buf);
			break;

		default:
			/* Unhandled */
			break;
		}

		ptr += ret;
		count--;
	}
	return 0;
}
#else
/*
 * Windows Resolver Code, as per:
 * http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dns/dns/dnsquery.asp
 * http://support.microsoft.com/default.aspx?scid=kb%3Ben-us%3B831226
 */
#include <windns.h>

int getrrs(const char *label, int rrtype, void gotrec(unsigned int num, int type, const char *record))
{
	DNS_STATUS	status;			/* Return value of DnsQuery_A() function */
	PDNS_RECORD	pResult, pRec;		/* Pointer to DNS_RECORD structure */
	unsigned int	rrnum = 0, i;
	uint16_t	type;

	status = DnsQuery(label,		/* Pointer to OwnerName */
		rrtype,				/* Type of the record to be queried */
		DNS_QUERY_STANDARD,		/* Standard Query */
		NULL,				/* Contains DNS server IP address */
		&pResult,			/* Resource record that contains the response */
		NULL);				/* Reserved for future use */

	if (status) return -1;
	else
	{
		for (pRec = pResult; pRec; pRec = pRec->pNext)
		{
			rrnum++;
			type = pRec->wType;

			if (rrtype != type && rrtype != ns_t_any) continue;

			switch (type)
			{
			case ns_t_txt:
				for (i=0; i < pRec->Data.TXT.dwStringCount; i++)
				{
					gotrec(rrnum, type, pRec->Data.TXT.pStringArray[i]);
				}
				break;

			case ns_t_a:
				gotrec(rrnum, type, (const char *)&pRec->Data.A.IpAddress);
				break;

			case ns_t_aaaa:
				gotrec(rrnum, type, (const char *)&pRec->Data.AAAA.Ip6Address);
				break;

			case ns_t_mx:
				gotrec(rrnum, type, pRec->Data.MX.pNameExchange);
				break;

			case ns_t_ns:
				gotrec(rrnum, type, pRec->Data.NS.pNameHost);
				break;
			}
		}
	}

	/* Free memory allocated for DNS records. */
	DnsRecordListFree(pResult, DnsFreeRecordListDeep);

	return 0;
}
#endif