blob: 7f9384c095e2a33bad30088c5a22ed3fe139bd3c [file] [log] [blame]
Bernie Innocenti318ed2d2018-08-30 04:05:20 +09001/* $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
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090032#include <arpa/inet.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090033#include <arpa/nameser.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090034#include <assert.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090035#include <errno.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090036#include <netdb.h>
37#include <netinet/in.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090038#include <resolv.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090039#include <stdlib.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090040#include <string.h>
41#include <sys/param.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090042
43#include "hostent.h"
44#include "resolv_private.h"
Bernie Innocentibf0b3bc2019-10-11 19:45:56 +090045
46constexpr int MAXALIASES = 35;
47constexpr int MAXADDRS = 35;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090048
Bernie Innocentic939de02018-09-12 17:59:17 +090049static void sethostent_r(FILE** hf) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090050 if (!*hf)
51 *hf = fopen(_PATH_HOSTS, "re");
52 else
53 rewind(*hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090054}
55
Bernie Innocentic939de02018-09-12 17:59:17 +090056static void endhostent_r(FILE** hf) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090057 if (*hf) {
58 (void) fclose(*hf);
59 *hf = NULL;
60 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090061}
62
Hungming Chena6914a62019-01-19 15:07:04 +080063// TODO: Consider returning a boolean result as files_getaddrinfo() does because the error code
64// does not currently return to netd.
Hungming Chen6c84a3d2018-12-26 16:14:17 +080065int _hf_gethtbyname2(const char* name, int af, getnamaddr* info) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090066 struct hostent *hp, hent;
67 char *buf, *ptr;
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +090068 size_t len, num, i;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090069 char* aliases[MAXALIASES];
70 char* addr_ptrs[MAXADDRS];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090071
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +090072 FILE* hf = NULL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090073 sethostent_r(&hf);
74 if (hf == NULL) {
Hungming Chena6914a62019-01-19 15:07:04 +080075 // TODO: Consider converting to a private extended EAI_* error code.
76 // Currently, the EAI_* value has no corresponding error code for invalid argument socket
77 // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
78 // and EINVAL, to EAI_FAIL.
79 return EAI_FAIL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090080 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090081
Bernie Innocenti9c575932018-09-07 21:10:25 +090082 if ((ptr = buf = (char*) malloc(len = info->buflen)) == NULL) {
Hungming Chen6c84a3d2018-12-26 16:14:17 +080083 return EAI_MEMORY;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090084 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090085
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +090086 hent.h_name = NULL;
87 hent.h_addrtype = 0;
88 hent.h_length = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090089
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +090090 size_t anum = 0;
91 for (num = 0; num < MAXADDRS; /**/) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090092 info->hp->h_addrtype = af;
93 info->hp->h_length = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090094
Hungming Chena6914a62019-01-19 15:07:04 +080095 int he;
96 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &he);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090097 if (hp == NULL) {
Hungming Chena6914a62019-01-19 15:07:04 +080098 if (he == NETDB_INTERNAL && errno == ENOSPC) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090099 goto nospc; // glibc compatibility.
100 }
101 break;
102 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900103
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900104 if (strcasecmp(hp->h_name, name) != 0) {
105 char** cp;
106 for (cp = hp->h_aliases; *cp != NULL; cp++)
107 if (strcasecmp(*cp, name) == 0) break;
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900108 // NOTE: does not increment num
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900109 if (*cp == NULL) continue;
110 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900111
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900112 if (num == 0) {
Bernie Innocentic939de02018-09-12 17:59:17 +0900113 hent.h_addrtype = hp->h_addrtype;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900114 hent.h_length = hp->h_length;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900115
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900116 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
117 for (anum = 0; hp->h_aliases[anum]; anum++) {
118 if (anum >= MAXALIASES) goto nospc;
119 HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len);
120 }
Bernie Innocentibc7d6c02021-06-22 16:59:52 +0900121 ptr = align_ptr(ptr);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900122 if ((size_t)(ptr - buf) >= info->buflen) goto nospc;
123 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900124
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900125 if (num >= MAXADDRS) goto nospc;
126 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len);
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900127
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900128 num++;
129 }
130 endhostent_r(&hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900131
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900132 if (num == 0) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900133 free(buf);
Hungming Chen9676a3a2018-12-28 17:52:01 +0800134 // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead.
135 // The original return error number is h_errno HOST_NOT_FOUND which was converted to
136 // EAI_NODATA.
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800137 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900138 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900139
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900140 hp = info->hp;
141 ptr = info->buf;
142 len = info->buflen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900143
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900144 hp->h_addrtype = hent.h_addrtype;
145 hp->h_length = hent.h_length;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900146
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900147 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
148 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900149
nuccachen9227b7f2018-09-18 13:38:48 +0800150 for (i = 0; i < num; i++) {
151 HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len);
152
153 // reserve space for mapping IPv4 address to IPv6 address in place
154 if (hp->h_addrtype == AF_INET) {
155 HENT_COPY(ptr, NAT64_PAD, sizeof(NAT64_PAD), ptr, len);
156 }
157 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900158 hp->h_addr_list[num] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900159
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900160 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900161
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900162 for (i = 0; i < anum; i++) {
163 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
164 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900165 hp->h_aliases[anum] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900166
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900167 free(buf);
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800168 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900169nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900170 free(buf);
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800171 return EAI_MEMORY;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900172}
173
Hungming Chena6914a62019-01-19 15:07:04 +0800174// TODO: Consider returning a boolean result as files_getaddrinfo() does because the error code
175// does not currently return to netd.
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800176int _hf_gethtbyaddr(const unsigned char* uaddr, int len, int af, getnamaddr* info) {
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900177 info->hp->h_length = len;
178 info->hp->h_addrtype = af;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900179
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900180 FILE* hf = NULL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900181 sethostent_r(&hf);
182 if (hf == NULL) {
Hungming Chena6914a62019-01-19 15:07:04 +0800183 // TODO: Consider converting to a private extended EAI_* error code.
184 // Currently, the EAI_* value has no corresponding error code for invalid argument socket
185 // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM
186 // and EINVAL, to EAI_FAIL.
187 return EAI_FAIL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900188 }
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900189 struct hostent* hp;
Hungming Chena6914a62019-01-19 15:07:04 +0800190 int he;
191 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &he)) != NULL)
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900192 if (!memcmp(hp->h_addr_list[0], uaddr, (size_t) hp->h_length)) break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900193 endhostent_r(&hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900194
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900195 if (hp == NULL) {
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800196 if (errno == ENOSPC) return EAI_MEMORY; // glibc compatibility.
Hungming Chen9676a3a2018-12-28 17:52:01 +0800197 // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead.
198 // The original return error number is h_errno HOST_NOT_FOUND which was converted to
199 // EAI_NODATA.
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800200 return EAI_NODATA;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900201 }
Hungming Chen6c84a3d2018-12-26 16:14:17 +0800202 return 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900203}