1 /*        $NetBSD: hostent.h,v 1.3 2022/04/19 20:32:15 rillig Exp $   */
2 
3 /*-
4  * Copyright (c) 2013 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Christos Zoulas.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <stdio.h>
33 #include <netdb.h>
34 #include <stdarg.h>
35 
36 /*
37  * These are not being advertised because the interfaces are non-standard.
38  * There are versions by linux, aix, qnx, sun, etc. Our versions are used
39  * internally to provide thread safety; they mostly resemble qnx.
40  */
41 void sethostent_r(FILE **);
42 struct hostent      *gethostent_r(FILE *, struct hostent *, char *, size_t, int *);
43 void endhostent_r(FILE **);
44 
45 struct hostent *gethostbyname_r(const char *, struct hostent *, char *, size_t,
46     int *);
47 struct hostent *gethostbyname2_r(const char *, int, struct hostent *, char *,
48     size_t, int *);
49 struct hostent *gethostbyaddr_r(const void *, socklen_t, int, struct hostent *,
50     char *, size_t, int *);
51 
52 extern FILE *_h_file;
53 
54 /*
55  * The following are internal API's and are used only for testing.
56  */
57 struct getnamaddr {
58           struct hostent *hp;
59           char *buf;
60           size_t buflen;
61           int *he;
62 };
63 
64 /* /etc/hosts lookup */
65 void _hf_sethostsfile(const char *);
66 int _hf_gethtbyaddr(void *, void *, va_list);
67 int _hf_gethtbyname(void *, void *, va_list);
68 
69 /* DNS lookup */
70 int _dns_gethtbyaddr(void *, void *, va_list);
71 int _dns_gethtbyname(void *, void *, va_list);
72 
73 #ifdef YP
74 /* NIS lookup */
75 int _yp_gethtbyaddr(void *, void *, va_list);
76 int _yp_gethtbyname(void *, void *, va_list);
77 #endif
78 
79 #define HENT_ARRAY(dst, anum, ptr, len) \
80           do { \
81                     size_t _len = (anum + 1) * sizeof(*dst); \
82                     if (_len > len) \
83                               goto nospc; \
84                     dst = (void *)ptr; \
85                     ptr += _len; \
86                     len -= _len; \
87           } while (0)
88 
89 #define HENT_COPY(dst, src, slen, ptr, len) \
90           do { \
91                     if ((size_t)slen > len) \
92                               goto nospc; \
93                     memcpy(ptr, src, (size_t)slen); \
94                     dst = ptr; \
95                     ptr += slen; \
96                     len -= slen; \
97           } while (0)
98 
99 #define HENT_SCOPY(dst, src, ptr, len) \
100           do { \
101                     size_t _len = strlen(src) + 1; \
102                     HENT_COPY(dst, src, _len, ptr, len); \
103           } while (0)
104 
105 #define   MAXALIASES          35
106 #define   MAXADDRS  35
107