summaryrefslogtreecommitdiffstats
path: root/rfc1035/rfc1035search.c
blob: 8e69ad44028b3834eb9a3031e1cd9f8118d9e0dd (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
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
210
211
212
213
214
/*
** Copyright 1998 - 2011 Double Precision, Inc.
** See COPYING for distribution information.
*/

#include	"rfc1035.h"
#include	<string.h>
#include	<stdlib.h>


static int found(const struct rfc1035_reply *r,
	const struct rfc1035_rr *rrp, const char *h,
	unsigned qclass, unsigned qtype)
{
char	namebuf[RFC1035_MAXNAMESIZE+1];

	if ((rrp->rrtype != qtype &&
		rrp->rrtype != RFC1035_TYPE_CNAME) ||
			rrp->rrclass != qclass)	return (0);

	if (rfc1035_replyhostname(r, rrp->rrname, namebuf) == 0)
		return (0);

	if (rfc1035_hostnamecmp(namebuf, h) == 0)	return (1);
	return (0);
}

int rfc1035_replysearch_an(const struct rfc1035_res *res,
			   const struct rfc1035_reply *r, const char *h,
			   unsigned qtype, unsigned qclass, int pos)
{
	if (res->rfc1035_defaultdomain && strchr(h, '.') == NULL)
	{
		char *buf;
		int rc;

		/* Append default domain */

		if ((buf=malloc(strlen(h)+
				strlen(res->rfc1035_defaultdomain)+2)) == 0)
			return (-1);

		strcat(strcat(strcpy(buf, h), "."),
		       res->rfc1035_defaultdomain);

		rc=rfc1035_replysearch_an(res, r, buf, qtype, qclass, pos);
		free(buf);
		return (rc);
	}

	for ( ; pos >= 0 && pos < r->ancount; pos++)
		if (found(r, r->anptr+pos, h, qclass, qtype))	return (pos);
	return (-1);
}

int rfc1035_replysearch_ns(const struct rfc1035_res *res,
			   const struct rfc1035_reply *r, const char *h,
			   unsigned qtype, unsigned qclass, int pos)
{
	if (res->rfc1035_defaultdomain && strchr(h, '.') == NULL)
	{
		char *buf;
		int rc;

		/* Append default domain */

		if ((buf=malloc(strlen(h)+
				strlen(res->rfc1035_defaultdomain)+2)) == 0)
			return (-1);

		strcat(strcat(strcpy(buf, h), "."),
		       res->rfc1035_defaultdomain);

		rc=rfc1035_replysearch_ns(res, r, buf, qtype, qclass, pos);
		free(buf);
		return (rc);
	}

	for ( ; pos >= 0 && pos < r->nscount; pos++)
		if (found(r, r->nsptr+pos, h, qclass, qtype))	return (pos);
	return (-1);
}

int rfc1035_replysearch_ar(const struct rfc1035_res *res,
			   const struct rfc1035_reply *r, const char *h,
			   unsigned qtype, unsigned qclass, int pos)
{
	if (res->rfc1035_defaultdomain && strchr(h, '.') == NULL)
	{
		char *buf;
		int rc;

		/* Append default domain */

		if ((buf=malloc(strlen(h)+
				strlen(res->rfc1035_defaultdomain)+2)) == 0)
			return (-1);

		strcat(strcat(strcpy(buf, h), "."),
		       res->rfc1035_defaultdomain);

		rc=rfc1035_replysearch_ar(res, r, buf, qtype, qclass, pos);
		free(buf);
		return (rc);
	}

	for ( ; pos >= 0 && pos < r->arcount; pos++)
		if (found(r, r->arptr+pos, h, qclass, qtype))	return (pos);
	return (-1);
}

int rfc1035_replysearch_all(const struct rfc1035_res *res,
			   const struct rfc1035_reply *r, const char *h,
			    unsigned qtype, unsigned qclass, int pos)
{
	unsigned s;

	if (res->rfc1035_defaultdomain && strchr(h, '.') == NULL)
	{
		char *buf;
		int rc;

		/* Append default domain */

		if ((buf=malloc(strlen(h)+
				strlen(res->rfc1035_defaultdomain)+2)) == 0)
			return (-1);

		strcat(strcat(strcpy(buf, h), "."),
		       res->rfc1035_defaultdomain);

		rc=rfc1035_replysearch_all(res, r, buf, qtype, qclass, pos);
		free(buf);
		return (rc);
	}

	s=r->ancount+r->nscount+r->arcount;

	for ( ; pos >= 0 && pos < s; pos++)
		if (found(r, r->allrrs[pos], h, qclass, qtype))	return (pos);
	return (-1);
}

int rfc1035_resolve_cname(struct rfc1035_res *res, char *namebuf,
			  unsigned qtype,
			  unsigned qclass,
			  struct rfc1035_reply **ptr,
			  int x_flags)
{
int	n;
int	recursion_count=10;

	if ( (*ptr=rfc1035_resolve(res, RFC1035_OPCODE_QUERY,
			namebuf, qtype, qclass)) == 0)
		return (-1);

	if (x_flags && RFC1035_X_RANDOMIZE &&
	    (*ptr)->rcode == RFC1035_RCODE_NOERROR)
		rfc1035_rr_rand(*ptr);

	if ( (*ptr)->rcode != RFC1035_RCODE_NOERROR ||
		(n=rfc1035_replysearch_an( res,
					   *ptr, namebuf, qtype, qclass, 0))<0)
		return (-1);

	if ( (*ptr)->anptr[n].rrtype == qtype)	return (n);

	/* Must've gotten back a CNAME when we were looking for something else
	*/

	for (;;)
	{

		if (rfc1035_replyhostname( *ptr, (*ptr)->anptr[n].rr.domainname,
			namebuf) == 0)
		{
			rfc1035_replyfree( *ptr );
			*ptr=0;
			return (-1);
		}

	/* Let's see if we already have the answer for the canonical name */

		if ( (n=rfc1035_replysearch_all( res, *ptr, namebuf, qtype,
				qclass, 0)) >= 0)
		{
			if ( (*ptr)->anptr[n].rrtype == qtype)	return (n);
			if ( --recursion_count > 0)
				continue;

			rfc1035_replyfree( *ptr );	/* Recursive CNAME */
			*ptr=0;
			return (RFC1035_ERR_CNAME_RECURSIVE);
		}

		rfc1035_replyfree( *ptr );
		if ( (*ptr=rfc1035_resolve(res, RFC1035_OPCODE_QUERY,
				namebuf, qtype, qclass)) == 0)
			return (-1);

		if ( (*ptr)->rcode == RFC1035_RCODE_NOERROR &&
			(n=rfc1035_replysearch_an( res, *ptr, namebuf,
				qtype, qclass, 0)) >= 0)
		{
			if ( (*ptr)->anptr[n].rrtype == qtype)	return (n);
			if ( --recursion_count > 0)
				continue;
			rfc1035_replyfree( *ptr );	/* Recursive CNAME */
			*ptr=0;
			return (RFC1035_ERR_CNAME_RECURSIVE);
		}
		return (-1);
	}
}