1 /* $NetBSD: sethostent.c,v 1.20 2014/03/17 13:24:23 christos Exp $ */
2
3 /*
4 * Copyright (c) 1985, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <arpa/inet.h>
33 #include <arpa/nameser.h>
34 #include <assert.h>
35 #include <errno.h>
36 #include <netdb.h>
37 #include <netinet/in.h>
38 #include <resolv.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/param.h>
43
44 #include "hostent.h"
45 #include "resolv_private.h"
46
47 #define ALIGNBYTES (sizeof(uintptr_t) - 1)
48 #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
49
sethostent_r(FILE ** hf)50 static void sethostent_r(FILE** hf) {
51 if (!*hf)
52 *hf = fopen(_PATH_HOSTS, "re");
53 else
54 rewind(*hf);
55 }
56
endhostent_r(FILE ** hf)57 static void endhostent_r(FILE** hf) {
58 if (*hf) {
59 (void) fclose(*hf);
60 *hf = NULL;
61 }
62 }
63
64 // TODO: Consider returning a boolean result as files_getaddrinfo() does because the error code
65 // does not currently return to netd.
_hf_gethtbyname2(const char * name,int af,getnamaddr * info)66 int _hf_gethtbyname2(const char* name, int af, getnamaddr* info) {
67 struct hostent *hp, hent;
68 char *buf, *ptr;
69 size_t len, num, i;
70 char* aliases[MAXALIASES];
71 char* addr_ptrs[MAXADDRS];
72
73 FILE* hf = NULL;
74 sethostent_r(&hf);
75 if (hf == NULL) {
76 // TODO: Consider converting to a private extended EAI_* error code.
77 // Currently, the EAI_* value has no corresponding error code for invalid argument socket
78 // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
79 // and EINVAL, to EAI_FAIL.
80 return EAI_FAIL;
81 }
82
83 if ((ptr = buf = (char*) malloc(len = info->buflen)) == NULL) {
84 return EAI_MEMORY;
85 }
86
87 hent.h_name = NULL;
88 hent.h_addrtype = 0;
89 hent.h_length = 0;
90
91 size_t anum = 0;
92 for (num = 0; num < MAXADDRS; /**/) {
93 info->hp->h_addrtype = af;
94 info->hp->h_length = 0;
95
96 int he;
97 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &he);
98 if (hp == NULL) {
99 if (he == NETDB_INTERNAL && errno == ENOSPC) {
100 goto nospc; // glibc compatibility.
101 }
102 break;
103 }
104
105 if (strcasecmp(hp->h_name, name) != 0) {
106 char** cp;
107 for (cp = hp->h_aliases; *cp != NULL; cp++)
108 if (strcasecmp(*cp, name) == 0) break;
109 // NOTE: does not increment num
110 if (*cp == NULL) continue;
111 }
112
113 if (num == 0) {
114 hent.h_addrtype = hp->h_addrtype;
115 hent.h_length = hp->h_length;
116
117 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
118 for (anum = 0; hp->h_aliases[anum]; anum++) {
119 if (anum >= MAXALIASES) goto nospc;
120 HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len);
121 }
122 ptr = (char*) ALIGN(ptr);
123 if ((size_t)(ptr - buf) >= info->buflen) goto nospc;
124 }
125
126 if (num >= MAXADDRS) goto nospc;
127 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len);
128
129 num++;
130 }
131 endhostent_r(&hf);
132
133 if (num == 0) {
134 free(buf);
135 // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead.
136 // The original return error number is h_errno HOST_NOT_FOUND which was converted to
137 // EAI_NODATA.
138 return EAI_NODATA;
139 }
140
141 hp = info->hp;
142 ptr = info->buf;
143 len = info->buflen;
144
145 hp->h_addrtype = hent.h_addrtype;
146 hp->h_length = hent.h_length;
147
148 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
149 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
150
151 for (i = 0; i < num; i++) {
152 HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len);
153
154 // reserve space for mapping IPv4 address to IPv6 address in place
155 if (hp->h_addrtype == AF_INET) {
156 HENT_COPY(ptr, NAT64_PAD, sizeof(NAT64_PAD), ptr, len);
157 }
158 }
159 hp->h_addr_list[num] = NULL;
160
161 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
162
163 for (i = 0; i < anum; i++) {
164 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
165 }
166 hp->h_aliases[anum] = NULL;
167
168 free(buf);
169 return 0;
170 nospc:
171 free(buf);
172 return EAI_MEMORY;
173 }
174
175 // TODO: Consider returning a boolean result as files_getaddrinfo() does because the error code
176 // does not currently return to netd.
_hf_gethtbyaddr(const unsigned char * uaddr,int len,int af,getnamaddr * info)177 int _hf_gethtbyaddr(const unsigned char* uaddr, int len, int af, getnamaddr* info) {
178 info->hp->h_length = len;
179 info->hp->h_addrtype = af;
180
181 FILE* hf = NULL;
182 sethostent_r(&hf);
183 if (hf == NULL) {
184 // TODO: Consider converting to a private extended EAI_* error code.
185 // Currently, the EAI_* value has no corresponding error code for invalid argument socket
186 // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
187 // and EINVAL, to EAI_FAIL.
188 return EAI_FAIL;
189 }
190 struct hostent* hp;
191 int he;
192 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &he)) != NULL)
193 if (!memcmp(hp->h_addr_list[0], uaddr, (size_t) hp->h_length)) break;
194 endhostent_r(&hf);
195
196 if (hp == NULL) {
197 if (errno == ENOSPC) return EAI_MEMORY; // glibc compatibility.
198 // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead.
199 // The original return error number is h_errno HOST_NOT_FOUND which was converted to
200 // EAI_NODATA.
201 return EAI_NODATA;
202 }
203 return 0;
204 }
205