Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 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 | |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 32 | #include <arpa/inet.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 33 | #include <arpa/nameser.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 34 | #include <assert.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 35 | #include <errno.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 36 | #include <netdb.h> |
| 37 | #include <netinet/in.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 38 | #include <resolv.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 39 | #include <stdlib.h> |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 40 | #include <string.h> |
| 41 | #include <sys/param.h> |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 42 | |
| 43 | #include "hostent.h" |
| 44 | #include "resolv_private.h" |
Bernie Innocenti | bf0b3bc | 2019-10-11 19:45:56 +0900 | [diff] [blame] | 45 | |
| 46 | constexpr int MAXALIASES = 35; |
| 47 | constexpr int MAXADDRS = 35; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 48 | |
| 49 | #define ALIGNBYTES (sizeof(uintptr_t) - 1) |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 50 | #define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES) |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 51 | |
Bernie Innocenti | c939de0 | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 52 | static void sethostent_r(FILE** hf) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 53 | if (!*hf) |
| 54 | *hf = fopen(_PATH_HOSTS, "re"); |
| 55 | else |
| 56 | rewind(*hf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 57 | } |
| 58 | |
Bernie Innocenti | c939de0 | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 59 | static void endhostent_r(FILE** hf) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 60 | if (*hf) { |
| 61 | (void) fclose(*hf); |
| 62 | *hf = NULL; |
| 63 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 64 | } |
| 65 | |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 66 | // TODO: Consider returning a boolean result as files_getaddrinfo() does because the error code |
| 67 | // does not currently return to netd. |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 68 | int _hf_gethtbyname2(const char* name, int af, getnamaddr* info) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 69 | struct hostent *hp, hent; |
| 70 | char *buf, *ptr; |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 71 | size_t len, num, i; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 72 | char* aliases[MAXALIASES]; |
| 73 | char* addr_ptrs[MAXADDRS]; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 74 | |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 75 | FILE* hf = NULL; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 76 | sethostent_r(&hf); |
| 77 | if (hf == NULL) { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 78 | // TODO: Consider converting to a private extended EAI_* error code. |
| 79 | // Currently, the EAI_* value has no corresponding error code for invalid argument socket |
| 80 | // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM |
| 81 | // and EINVAL, to EAI_FAIL. |
| 82 | return EAI_FAIL; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 83 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 84 | |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 85 | if ((ptr = buf = (char*) malloc(len = info->buflen)) == NULL) { |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 86 | return EAI_MEMORY; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 87 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 88 | |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 89 | hent.h_name = NULL; |
| 90 | hent.h_addrtype = 0; |
| 91 | hent.h_length = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 92 | |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 93 | size_t anum = 0; |
| 94 | for (num = 0; num < MAXADDRS; /**/) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 95 | info->hp->h_addrtype = af; |
| 96 | info->hp->h_length = 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 97 | |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 98 | int he; |
| 99 | hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &he); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 100 | if (hp == NULL) { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 101 | if (he == NETDB_INTERNAL && errno == ENOSPC) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 102 | goto nospc; // glibc compatibility. |
| 103 | } |
| 104 | break; |
| 105 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 106 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 107 | if (strcasecmp(hp->h_name, name) != 0) { |
| 108 | char** cp; |
| 109 | for (cp = hp->h_aliases; *cp != NULL; cp++) |
| 110 | if (strcasecmp(*cp, name) == 0) break; |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 111 | // NOTE: does not increment num |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 112 | if (*cp == NULL) continue; |
| 113 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 114 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 115 | if (num == 0) { |
Bernie Innocenti | c939de0 | 2018-09-12 17:59:17 +0900 | [diff] [blame] | 116 | hent.h_addrtype = hp->h_addrtype; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 117 | hent.h_length = hp->h_length; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 118 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 119 | HENT_SCOPY(hent.h_name, hp->h_name, ptr, len); |
| 120 | for (anum = 0; hp->h_aliases[anum]; anum++) { |
| 121 | if (anum >= MAXALIASES) goto nospc; |
| 122 | HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len); |
| 123 | } |
Bernie Innocenti | 9c57593 | 2018-09-07 21:10:25 +0900 | [diff] [blame] | 124 | ptr = (char*) ALIGN(ptr); |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 125 | if ((size_t)(ptr - buf) >= info->buflen) goto nospc; |
| 126 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 127 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 128 | if (num >= MAXADDRS) goto nospc; |
| 129 | HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len); |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 130 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 131 | num++; |
| 132 | } |
| 133 | endhostent_r(&hf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 134 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 135 | if (num == 0) { |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 136 | free(buf); |
Hungming Chen | 9676a3a | 2018-12-28 17:52:01 +0800 | [diff] [blame] | 137 | // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead. |
| 138 | // The original return error number is h_errno HOST_NOT_FOUND which was converted to |
| 139 | // EAI_NODATA. |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 140 | return EAI_NODATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 141 | } |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 142 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 143 | hp = info->hp; |
| 144 | ptr = info->buf; |
| 145 | len = info->buflen; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 146 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 147 | hp->h_addrtype = hent.h_addrtype; |
| 148 | hp->h_length = hent.h_length; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 149 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 150 | HENT_ARRAY(hp->h_aliases, anum, ptr, len); |
| 151 | HENT_ARRAY(hp->h_addr_list, num, ptr, len); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 152 | |
nuccachen | 9227b7f | 2018-09-18 13:38:48 +0800 | [diff] [blame] | 153 | for (i = 0; i < num; i++) { |
| 154 | HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len); |
| 155 | |
| 156 | // reserve space for mapping IPv4 address to IPv6 address in place |
| 157 | if (hp->h_addrtype == AF_INET) { |
| 158 | HENT_COPY(ptr, NAT64_PAD, sizeof(NAT64_PAD), ptr, len); |
| 159 | } |
| 160 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 161 | hp->h_addr_list[num] = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 162 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 163 | HENT_SCOPY(hp->h_name, hent.h_name, ptr, len); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 164 | |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 165 | for (i = 0; i < anum; i++) { |
| 166 | HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len); |
| 167 | } |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 168 | hp->h_aliases[anum] = NULL; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 169 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 170 | free(buf); |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 171 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 172 | nospc: |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 173 | free(buf); |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 174 | return EAI_MEMORY; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 175 | } |
| 176 | |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 177 | // TODO: Consider returning a boolean result as files_getaddrinfo() does because the error code |
| 178 | // does not currently return to netd. |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 179 | int _hf_gethtbyaddr(const unsigned char* uaddr, int len, int af, getnamaddr* info) { |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 180 | info->hp->h_length = len; |
| 181 | info->hp->h_addrtype = af; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 182 | |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 183 | FILE* hf = NULL; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 184 | sethostent_r(&hf); |
| 185 | if (hf == NULL) { |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 186 | // TODO: Consider converting to a private extended EAI_* error code. |
| 187 | // Currently, the EAI_* value has no corresponding error code for invalid argument socket |
| 188 | // length. In order to not rely on errno, convert the original error code pair, EAI_SYSTEM |
| 189 | // and EINVAL, to EAI_FAIL. |
| 190 | return EAI_FAIL; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 191 | } |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 192 | struct hostent* hp; |
Hungming Chen | a6914a6 | 2019-01-19 15:07:04 +0800 | [diff] [blame] | 193 | int he; |
| 194 | while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, &he)) != NULL) |
Bernie Innocenti | 9f61dbd | 2018-09-12 20:03:11 +0900 | [diff] [blame] | 195 | if (!memcmp(hp->h_addr_list[0], uaddr, (size_t) hp->h_length)) break; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 196 | endhostent_r(&hf); |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 197 | |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 198 | if (hp == NULL) { |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 199 | if (errno == ENOSPC) return EAI_MEMORY; // glibc compatibility. |
Hungming Chen | 9676a3a | 2018-12-28 17:52:01 +0800 | [diff] [blame] | 200 | // TODO: Perhaps convert HOST_NOT_FOUND to EAI_NONAME instead. |
| 201 | // The original return error number is h_errno HOST_NOT_FOUND which was converted to |
| 202 | // EAI_NODATA. |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 203 | return EAI_NODATA; |
Bernie Innocenti | 8ad893f | 2018-08-31 14:09:46 +0900 | [diff] [blame] | 204 | } |
Hungming Chen | 6c84a3d | 2018-12-26 16:14:17 +0800 | [diff] [blame] | 205 | return 0; |
Bernie Innocenti | 318ed2d | 2018-08-30 04:05:20 +0900 | [diff] [blame] | 206 | } |