blob: 7c5b905224b9ebb33d68dbb4bf6d648497ea3210 [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 Innocentic939de02018-09-12 17:59:17 +090039#include <stdio.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090040#include <stdlib.h>
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090041#include <string.h>
42#include <sys/param.h>
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090043
44#include "hostent.h"
45#include "resolv_private.h"
46
47#define ALIGNBYTES (sizeof(uintptr_t) - 1)
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090048#define ALIGN(p) (((uintptr_t)(p) + ALIGNBYTES) & ~ALIGNBYTES)
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090049
Bernie Innocentic939de02018-09-12 17:59:17 +090050static void sethostent_r(FILE** hf) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090051 if (!*hf)
52 *hf = fopen(_PATH_HOSTS, "re");
53 else
54 rewind(*hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090055}
56
Bernie Innocentic939de02018-09-12 17:59:17 +090057static void endhostent_r(FILE** hf) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090058 if (*hf) {
59 (void) fclose(*hf);
60 *hf = NULL;
61 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090062}
63
Bernie Innocentic939de02018-09-12 17:59:17 +090064hostent* _hf_gethtbyname2(const char* name, int af, getnamaddr* info) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090065 struct hostent *hp, hent;
66 char *buf, *ptr;
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +090067 size_t len, num, i;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090068 char* aliases[MAXALIASES];
69 char* addr_ptrs[MAXADDRS];
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090070
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +090071 FILE* hf = NULL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090072 sethostent_r(&hf);
73 if (hf == NULL) {
74 errno = EINVAL;
75 *info->he = NETDB_INTERNAL;
76 return NULL;
77 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090078
Bernie Innocenti9c575932018-09-07 21:10:25 +090079 if ((ptr = buf = (char*) malloc(len = info->buflen)) == NULL) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090080 *info->he = NETDB_INTERNAL;
81 return NULL;
82 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090083
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +090084 hent.h_name = NULL;
85 hent.h_addrtype = 0;
86 hent.h_length = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090087
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +090088 size_t anum = 0;
89 for (num = 0; num < MAXADDRS; /**/) {
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090090 info->hp->h_addrtype = af;
91 info->hp->h_length = 0;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +090092
Bernie Innocenti8ad893f2018-08-31 14:09:46 +090093 hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he);
94 if (hp == NULL) {
95 if (*info->he == NETDB_INTERNAL && errno == ENOSPC) {
96 goto nospc; // glibc compatibility.
97 }
98 break;
99 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900100
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900101 if (strcasecmp(hp->h_name, name) != 0) {
102 char** cp;
103 for (cp = hp->h_aliases; *cp != NULL; cp++)
104 if (strcasecmp(*cp, name) == 0) break;
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900105 // NOTE: does not increment num
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900106 if (*cp == NULL) continue;
107 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900108
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900109 if (num == 0) {
Bernie Innocentic939de02018-09-12 17:59:17 +0900110 hent.h_addrtype = hp->h_addrtype;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900111 hent.h_length = hp->h_length;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900112
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900113 HENT_SCOPY(hent.h_name, hp->h_name, ptr, len);
114 for (anum = 0; hp->h_aliases[anum]; anum++) {
115 if (anum >= MAXALIASES) goto nospc;
116 HENT_SCOPY(aliases[anum], hp->h_aliases[anum], ptr, len);
117 }
Bernie Innocenti9c575932018-09-07 21:10:25 +0900118 ptr = (char*) ALIGN(ptr);
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900119 if ((size_t)(ptr - buf) >= info->buflen) goto nospc;
120 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900121
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900122 if (num >= MAXADDRS) goto nospc;
123 HENT_COPY(addr_ptrs[num], hp->h_addr_list[0], hp->h_length, ptr, len);
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900124
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900125 num++;
126 }
127 endhostent_r(&hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900128
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900129 if (num == 0) {
130 *info->he = HOST_NOT_FOUND;
131 free(buf);
132 return NULL;
133 }
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900134
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900135 hp = info->hp;
136 ptr = info->buf;
137 len = info->buflen;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900138
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900139 hp->h_addrtype = hent.h_addrtype;
140 hp->h_length = hent.h_length;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900141
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900142 HENT_ARRAY(hp->h_aliases, anum, ptr, len);
143 HENT_ARRAY(hp->h_addr_list, num, ptr, len);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900144
nuccachen9227b7f2018-09-18 13:38:48 +0800145 for (i = 0; i < num; i++) {
146 HENT_COPY(hp->h_addr_list[i], addr_ptrs[i], hp->h_length, ptr, len);
147
148 // reserve space for mapping IPv4 address to IPv6 address in place
149 if (hp->h_addrtype == AF_INET) {
150 HENT_COPY(ptr, NAT64_PAD, sizeof(NAT64_PAD), ptr, len);
151 }
152 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900153 hp->h_addr_list[num] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900154
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900155 HENT_SCOPY(hp->h_name, hent.h_name, ptr, len);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900156
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900157 for (i = 0; i < anum; i++) {
158 HENT_SCOPY(hp->h_aliases[i], aliases[i], ptr, len);
159 }
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900160 hp->h_aliases[anum] = NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900161
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900162 free(buf);
163 return hp;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900164nospc:
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900165 *info->he = NETDB_INTERNAL;
166 free(buf);
167 errno = ENOSPC;
168 return NULL;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900169}
170
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900171bool _hf_gethtbyaddr(const unsigned char* uaddr, int len, int af, getnamaddr* info) {
172 info->hp->h_length = len;
173 info->hp->h_addrtype = af;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900174
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900175 FILE* hf = NULL;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900176 sethostent_r(&hf);
177 if (hf == NULL) {
178 *info->he = NETDB_INTERNAL;
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900179 return false;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900180 }
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900181 struct hostent* hp;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900182 while ((hp = netbsd_gethostent_r(hf, info->hp, info->buf, info->buflen, info->he)) != NULL)
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900183 if (!memcmp(hp->h_addr_list[0], uaddr, (size_t) hp->h_length)) break;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900184 endhostent_r(&hf);
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900185
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900186 if (hp == NULL) {
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900187 if (errno == ENOSPC) return false; // glibc compatibility.
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900188 *info->he = HOST_NOT_FOUND;
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900189 return false;
Bernie Innocenti8ad893f2018-08-31 14:09:46 +0900190 }
Bernie Innocenti9f61dbd2018-09-12 20:03:11 +0900191 return true;
Bernie Innocenti318ed2d2018-08-30 04:05:20 +0900192}