summaryrefslogtreecommitdiffstats
path: root/contrib/bind9/lib/bind/irs/gen_ho.c
blob: e9e2c89097641926f174768db332ad7f64d976de (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/*
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 1996,1999 by Internet Software Consortium.
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#if defined(LIBC_SCCS) && !defined(lint)
static const char rcsid[] = "$Id: gen_ho.c,v 1.1.206.2 2004/03/17 01:49:39 marka Exp $";
#endif /* LIBC_SCCS and not lint */

/* Imports */

#include "port_before.h"

#include <sys/types.h>

#include <netinet/in.h>
#include <arpa/nameser.h>

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

#include <isc/memcluster.h>
#include <irs.h>

#include "port_after.h"

#include "irs_p.h"
#include "gen_p.h"

/* Definitions */

struct pvt {
	struct irs_rule *	rules;
	struct irs_rule *	rule;
	struct irs_ho *		ho;
	struct __res_state *	res;
	void			(*free_res)(void *);
};

/* Forwards */

static void		ho_close(struct irs_ho *this);
static struct hostent *	ho_byname(struct irs_ho *this, const char *name);
static struct hostent *	ho_byname2(struct irs_ho *this, const char *name,
				   int af);
static struct hostent *	ho_byaddr(struct irs_ho *this, const void *addr,
				  int len, int af);
static struct hostent *	ho_next(struct irs_ho *this);
static void		ho_rewind(struct irs_ho *this);
static void		ho_minimize(struct irs_ho *this);
static struct __res_state * ho_res_get(struct irs_ho *this);
static void		ho_res_set(struct irs_ho *this,
				   struct __res_state *res,
				   void (*free_res)(void *));
static struct addrinfo * ho_addrinfo(struct irs_ho *this, const char *name,
				     const struct addrinfo *pai);

static int		init(struct irs_ho *this);

/* Exports */

struct irs_ho *
irs_gen_ho(struct irs_acc *this) {
	struct gen_p *accpvt = (struct gen_p *)this->private;
	struct irs_ho *ho;
	struct pvt *pvt;

	if (!(pvt = memget(sizeof *pvt))) {
		errno = ENOMEM;
		return (NULL);
	}
	memset(pvt, 0, sizeof *pvt);
	if (!(ho = memget(sizeof *ho))) {
		memput(pvt, sizeof *pvt);
		errno = ENOMEM;
		return (NULL);
	}
	memset(ho, 0x5e, sizeof *ho);
	pvt->rules = accpvt->map_rules[irs_ho];
	pvt->rule = pvt->rules;
	ho->private = pvt;
	ho->close = ho_close;
	ho->byname = ho_byname;
	ho->byname2 = ho_byname2;
	ho->byaddr = ho_byaddr;
	ho->next = ho_next;
	ho->rewind = ho_rewind;
	ho->minimize = ho_minimize;
	ho->res_get = ho_res_get;
	ho->res_set = ho_res_set;
	ho->addrinfo = ho_addrinfo;
	return (ho);
}

/* Methods. */

static void
ho_close(struct irs_ho *this) {
	struct pvt *pvt = (struct pvt *)this->private;

	ho_minimize(this);
	if (pvt->res && pvt->free_res)
		(*pvt->free_res)(pvt->res);
	memput(pvt, sizeof *pvt);
	memput(this, sizeof *this);
}

static struct hostent *
ho_byname(struct irs_ho *this, const char *name) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct irs_rule *rule;
	struct hostent *rval;
	struct irs_ho *ho;
	int therrno = NETDB_INTERNAL;
	int softerror = 0;

	if (init(this) == -1)
		return (NULL);

	for (rule = pvt->rules; rule; rule = rule->next) {
		ho = rule->inst->ho;
		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
		errno = 0;
		rval = (*ho->byname)(ho, name);
		if (rval != NULL)
			return (rval);
		if (softerror == 0 &&
		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
		    pvt->res->res_h_errno != NETDB_INTERNAL) {
			softerror = 1;
			therrno = pvt->res->res_h_errno;
		}
		if (rule->flags & IRS_CONTINUE)
			continue;
		/*
		 * The value TRY_AGAIN can mean that the service
		 * is not available, or just that this particular name
		 * cannot be resolved now.  We use the errno ECONNREFUSED
		 * to distinguish.  If a lookup sets that errno when
		 * H_ERRNO is TRY_AGAIN, we continue to try other lookup
		 * functions, otherwise we return the TRY_AGAIN error.
		 */
		if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
			break;
	}
	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
		RES_SET_H_ERRNO(pvt->res, therrno);
	return (NULL);
}

static struct hostent *
ho_byname2(struct irs_ho *this, const char *name, int af) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct irs_rule *rule;
	struct hostent *rval;
	struct irs_ho *ho;
	int therrno = NETDB_INTERNAL;
	int softerror = 0;

	if (init(this) == -1)
		return (NULL);

	for (rule = pvt->rules; rule; rule = rule->next) {
		ho = rule->inst->ho;
		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
		errno = 0;
		rval = (*ho->byname2)(ho, name, af);
		if (rval != NULL)
			return (rval);
		if (softerror == 0 &&
		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
		    pvt->res->res_h_errno != NETDB_INTERNAL) {
			softerror = 1;
			therrno = pvt->res->res_h_errno;
		}
		if (rule->flags & IRS_CONTINUE)
			continue;
		/*
		 * See the comments in ho_byname() explaining
		 * the interpretation of TRY_AGAIN and ECONNREFUSED.
		 */
		if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
			break;
	}
	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
		RES_SET_H_ERRNO(pvt->res, therrno);
	return (NULL);
}

static struct hostent *
ho_byaddr(struct irs_ho *this, const void *addr, int len, int af) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct irs_rule *rule;
	struct hostent *rval;
	struct irs_ho *ho;
	int therrno = NETDB_INTERNAL;
	int softerror = 0;


	if (init(this) == -1)
		return (NULL);

	for (rule = pvt->rules; rule; rule = rule->next) {
		ho = rule->inst->ho;
		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
		errno = 0;
		rval = (*ho->byaddr)(ho, addr, len, af);
		if (rval != NULL)
			return (rval);
		if (softerror == 0 &&
		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
		    pvt->res->res_h_errno != NETDB_INTERNAL) {
			softerror = 1;
			therrno = pvt->res->res_h_errno;
		}

		if (rule->flags & IRS_CONTINUE)
			continue;
		/*
		 * See the comments in ho_byname() explaining
		 * the interpretation of TRY_AGAIN and ECONNREFUSED.
		 */
		if (pvt->res->res_h_errno != TRY_AGAIN || errno != ECONNREFUSED)
			break;
	}
	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
		RES_SET_H_ERRNO(pvt->res, therrno);
	return (NULL);
}

static struct hostent *
ho_next(struct irs_ho *this) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct hostent *rval;
	struct irs_ho *ho;

	while (pvt->rule) {
		ho = pvt->rule->inst->ho;
		rval = (*ho->next)(ho);
		if (rval)
			return (rval);
		if (!(pvt->rule->flags & IRS_CONTINUE))
			break;
		pvt->rule = pvt->rule->next;
		if (pvt->rule) {
			ho = pvt->rule->inst->ho;
			(*ho->rewind)(ho);
		}
	}
	return (NULL);
}

static void
ho_rewind(struct irs_ho *this) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct irs_ho *ho;

	pvt->rule = pvt->rules;
	if (pvt->rule) {
		ho = pvt->rule->inst->ho;
		(*ho->rewind)(ho);
	}
}

static void
ho_minimize(struct irs_ho *this) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct irs_rule *rule;

	if (pvt->res)
		res_nclose(pvt->res);
	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
		struct irs_ho *ho = rule->inst->ho;

		(*ho->minimize)(ho);
	}
}

static struct __res_state *
ho_res_get(struct irs_ho *this) {
	struct pvt *pvt = (struct pvt *)this->private;

	if (!pvt->res) {
		struct __res_state *res;
		res = (struct __res_state *)malloc(sizeof *res);
		if (!res) {
			errno = ENOMEM;
			return (NULL);
		}
		memset(res, 0, sizeof *res);
		ho_res_set(this, res, free);
	}

	return (pvt->res);
}

static void
ho_res_set(struct irs_ho *this, struct __res_state *res,
		void (*free_res)(void *)) {
	struct pvt *pvt = (struct pvt *)this->private;
	struct irs_rule *rule;

	if (pvt->res && pvt->free_res) {
		res_nclose(pvt->res);
		(*pvt->free_res)(pvt->res);
	}

	pvt->res = res;
	pvt->free_res = free_res;

	for (rule = pvt->rules; rule != NULL; rule = rule->next) {
		struct irs_ho *ho = rule->inst->ho;

		(*ho->res_set)(ho, pvt->res, NULL);
	}
}

static struct addrinfo *
ho_addrinfo(struct irs_ho *this, const char *name, const struct addrinfo *pai)
{
	struct pvt *pvt = (struct pvt *)this->private;
	struct irs_rule *rule;
	struct addrinfo *rval = NULL;
	struct irs_ho *ho;
	int therrno = NETDB_INTERNAL;
	int softerror = 0;

	if (init(this) == -1)
		return (NULL);

	for (rule = pvt->rules; rule; rule = rule->next) {
		ho = rule->inst->ho;
		RES_SET_H_ERRNO(pvt->res, NETDB_INTERNAL);
		errno = 0;
		if (ho->addrinfo == NULL) /* for safety */
			continue;
		rval = (*ho->addrinfo)(ho, name, pai);
		if (rval != NULL)
			return (rval);
		if (softerror == 0 &&
		    pvt->res->res_h_errno != HOST_NOT_FOUND &&
		    pvt->res->res_h_errno != NETDB_INTERNAL) {
			softerror = 1;
			therrno = pvt->res->res_h_errno;
		}
		if (rule->flags & IRS_CONTINUE)
			continue;
		/*
		 * See the comments in ho_byname() explaining
		 * the interpretation of TRY_AGAIN and ECONNREFUSED.
		 */
		if (pvt->res->res_h_errno != TRY_AGAIN ||
		    errno != ECONNREFUSED)
			break;
	}
	if (softerror != 0 && pvt->res->res_h_errno == HOST_NOT_FOUND)
		RES_SET_H_ERRNO(pvt->res, therrno);
	if (rval)
		freeaddrinfo(rval);
	return (NULL);
}

static int
init(struct irs_ho *this) {
	struct pvt *pvt = (struct pvt *)this->private;

        if (!pvt->res && !ho_res_get(this))
                return (-1);

        if (((pvt->res->options & RES_INIT) == 0U) &&
            (res_ninit(pvt->res) == -1))
                return (-1);

        return (0);
}
OpenPOWER on IntegriCloud