summaryrefslogtreecommitdiffstats
path: root/lib/bind/irs/gethostent_r.c
blob: 96d2a574ebdce3981fdf136bcdd596f290703efd (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
/*
 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 1998-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: gethostent_r.c,v 1.5.18.4 2005/09/03 12:45:14 marka Exp $";
#endif /* LIBC_SCCS and not lint */

#include <port_before.h>
#if !defined(_REENTRANT) || !defined(DO_PTHREADS)
	static int gethostent_r_not_required = 0;
#else
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <sys/param.h>
#include <port_after.h>

#ifdef HOST_R_RETURN

static HOST_R_RETURN 
copy_hostent(struct hostent *, struct hostent *, HOST_R_COPY_ARGS);

HOST_R_RETURN
gethostbyname_r(const char *name,  struct hostent *hptr, HOST_R_ARGS) {
	struct hostent *he = gethostbyname(name);
#ifdef HOST_R_SETANSWER
	int n = 0;
#endif

#ifdef HOST_R_ERRNO
	HOST_R_ERRNO;
#endif

#ifdef HOST_R_SETANSWER
	if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0)
		*answerp = NULL;
	else
		*answerp = hptr;
	
	return (n);
#else
	if (he == NULL)
		return (HOST_R_BAD);

	return (copy_hostent(he, hptr, HOST_R_COPY));
#endif
}

HOST_R_RETURN
gethostbyaddr_r(const char *addr, int len, int type,
		struct hostent *hptr, HOST_R_ARGS) {
	struct hostent *he = gethostbyaddr(addr, len, type);
#ifdef HOST_R_SETANSWER
	int n = 0;
#endif

#ifdef HOST_R_ERRNO
	HOST_R_ERRNO;
#endif

#ifdef HOST_R_SETANSWER
	if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0)
		*answerp = NULL;
	else
		*answerp = hptr;
	
	return (n);
#else
	if (he == NULL)
		return (HOST_R_BAD);

	return (copy_hostent(he, hptr, HOST_R_COPY));
#endif
}

/*%
 *	These assume a single context is in operation per thread.
 *	If this is not the case we will need to call irs directly
 *	rather than through the base functions.
 */

HOST_R_RETURN
gethostent_r(struct hostent *hptr, HOST_R_ARGS) {
	struct hostent *he = gethostent();
#ifdef HOST_R_SETANSWER
	int n = 0;
#endif

#ifdef HOST_R_ERRNO
	HOST_R_ERRNO;
#endif

#ifdef HOST_R_SETANSWER
	if (he == NULL || (n = copy_hostent(he, hptr, HOST_R_COPY)) != 0)
		*answerp = NULL;
	else
		*answerp = hptr;
	
	return (n);
#else
	if (he == NULL)
		return (HOST_R_BAD);

	return (copy_hostent(he, hptr, HOST_R_COPY));
#endif
}

HOST_R_SET_RETURN
#ifdef HOST_R_ENT_ARGS
sethostent_r(int stay_open, HOST_R_ENT_ARGS)
#else
sethostent_r(int stay_open)
#endif
{
#ifdef HOST_R_ENT_ARGS
	UNUSED(hdptr);
#endif
	sethostent(stay_open);
#ifdef	HOST_R_SET_RESULT
	return (HOST_R_SET_RESULT);
#endif
}

HOST_R_END_RETURN
#ifdef HOST_R_ENT_ARGS
endhostent_r(HOST_R_ENT_ARGS)
#else
endhostent_r(void)
#endif
{
#ifdef HOST_R_ENT_ARGS
	UNUSED(hdptr);
#endif
	endhostent();
	HOST_R_END_RESULT(HOST_R_OK);
}

/* Private */

#ifndef HOSTENT_DATA
static HOST_R_RETURN
copy_hostent(struct hostent *he, struct hostent *hptr, HOST_R_COPY_ARGS) {
	char *cp;
	char **ptr;
	int i, n;
	int nptr, len;

	/* Find out the amount of space required to store the answer. */
	nptr = 2; /*%< NULL ptrs */
	len = (char *)ALIGN(buf) - buf;
	for (i = 0; he->h_addr_list[i]; i++, nptr++) {
		len += he->h_length;
	}
	for (i = 0; he->h_aliases[i]; i++, nptr++) {
		len += strlen(he->h_aliases[i]) + 1;
	}
	len += strlen(he->h_name) + 1;
	len += nptr * sizeof(char*);
	
	if (len > buflen) {
		errno = ERANGE;
		return (HOST_R_BAD);
	}

	/* copy address size and type */
	hptr->h_addrtype = he->h_addrtype;
	n = hptr->h_length = he->h_length;

	ptr = (char **)ALIGN(buf);
	cp = (char *)ALIGN(buf) + nptr * sizeof(char *);

	/* copy address list */
	hptr->h_addr_list = ptr;
	for (i = 0; he->h_addr_list[i]; i++ , ptr++) {
		memcpy(cp, he->h_addr_list[i], n);
		hptr->h_addr_list[i] = cp;
		cp += n;
	}
	hptr->h_addr_list[i] = NULL;
	ptr++;

	/* copy official name */
	n = strlen(he->h_name) + 1;
	strcpy(cp, he->h_name);
	hptr->h_name = cp;
	cp += n;

	/* copy aliases */
	hptr->h_aliases = ptr;
	for (i = 0 ; he->h_aliases[i]; i++) {
		n = strlen(he->h_aliases[i]) + 1;
		strcpy(cp, he->h_aliases[i]);
		hptr->h_aliases[i] = cp;
		cp += n;
	}
	hptr->h_aliases[i] = NULL;

	return (HOST_R_OK);
}
#else /* !HOSTENT_DATA */
static int
copy_hostent(struct hostent *he, struct hostent *hptr, HOST_R_COPY_ARGS) {
	char *cp, *eob;
	int i, n;

	/* copy address size and type */
	hptr->h_addrtype = he->h_addrtype;
	n = hptr->h_length = he->h_length;

	/* copy up to first 35 addresses */
	i = 0;
	cp = hdptr->hostbuf;
	eob = hdptr->hostbuf + sizeof(hdptr->hostbuf);
	hptr->h_addr_list = hdptr->h_addr_ptrs;
	while (he->h_addr_list[i] && i < (_MAXADDRS)) {
		if (n < (eob - cp)) {
			memcpy(cp, he->h_addr_list[i], n);
			hptr->h_addr_list[i] = cp;
			cp += n;
		} else {
			break;
		}
		i++;
	}
	hptr->h_addr_list[i] = NULL;

	/* copy official name */
	if ((n = strlen(he->h_name) + 1) < (eob - cp)) {
		strcpy(cp, he->h_name);
		hptr->h_name = cp;
		cp += n;
	} else {
		return (-1);
	}

	/* copy aliases */
	i = 0;
	hptr->h_aliases = hdptr->host_aliases;
	while (he->h_aliases[i] && i < (_MAXALIASES-1)) {
		if ((n = strlen(he->h_aliases[i]) + 1) < (eob - cp)) {
			strcpy(cp, he->h_aliases[i]);
			hptr->h_aliases[i] = cp;
			cp += n;
		} else {
			break;
		}
		i++;
	}
	hptr->h_aliases[i] = NULL;

	return (HOST_R_OK);
}
#endif /* !HOSTENT_DATA */
#else /* HOST_R_RETURN */
	static int gethostent_r_unknown_system = 0;
#endif /* HOST_R_RETURN */
#endif /* !defined(_REENTRANT) || !defined(DO_PTHREADS) */
/*! \file */
OpenPOWER on IntegriCloud