xref: /freebsd/lib/libc/net/netdb_private.h (revision 8aac90f18aef7c9eea906c3ff9a001ca7b94f375)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause
3  *
4  * Copyright (C) 2005 The FreeBSD Project.  All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  */
27 
28 #ifndef _NETDB_PRIVATE_H_
29 #define _NETDB_PRIVATE_H_
30 
31 #include <stdio.h>				/* XXX: for FILE */
32 
33 #define	NETDB_THREAD_ALLOC(name)					\
34 static struct name name;						\
35 static thread_key_t name##_key;						\
36 static once_t name##_init_once = ONCE_INITIALIZER;			\
37 static int name##_thr_keycreated = 0;					\
38 \
39 static void name##_free(void *);					\
40 \
41 static void								\
42 name##_keycreate(void)							\
43 {									\
44 	name##_thr_keycreated =						\
45 	    (thr_keycreate(&name##_key, name##_free) == 0);		\
46 }									\
47 \
48 struct name *								\
49 __##name##_init(void)							\
50 {									\
51 	struct name *he;						\
52 									\
53 	if (thr_main() != 0)						\
54 		return (&name);						\
55 	if (thr_once(&name##_init_once, name##_keycreate) != 0 ||	\
56 	    !name##_thr_keycreated)					\
57 		return (NULL);						\
58 	if ((he = thr_getspecific(name##_key)) != NULL)			\
59 		return (he);						\
60 	if ((he = calloc(1, sizeof(*he))) == NULL)			\
61 		return (NULL);						\
62 	if (thr_setspecific(name##_key, he) == 0)			\
63 		return (he);						\
64 	free(he);							\
65 	return (NULL);							\
66 }
67 
68 #define	_MAXALIASES	35
69 #define	_MAXLINELEN	1024
70 #define	_MAXADDRS	35
71 #define	_HOSTBUFSIZE	(8 * 1024)
72 #define	_NETBUFSIZE	1025
73 
74 struct hostent_data {
75 	uint32_t host_addr[4];			/* IPv4 or IPv6 */
76 	char *h_addr_ptrs[_MAXADDRS + 1];
77 	char *host_aliases[_MAXALIASES];
78 	char hostbuf[_HOSTBUFSIZE];
79 	FILE *hostf;
80 	int stayopen;
81 #ifdef YP
82 	char *yp_domain;
83 #endif
84 };
85 
86 struct netent_data {
87 	char *net_aliases[_MAXALIASES];
88 	char netbuf[_NETBUFSIZE];
89 	FILE *netf;
90 	int stayopen;
91 #ifdef YP
92 	char *yp_domain;
93 #endif
94 };
95 
96 struct protoent_data {
97 	FILE *fp;
98 	char *aliases[_MAXALIASES];
99 	int stayopen;
100 	char line[_MAXLINELEN + 1];
101 };
102 
103 struct hostdata {
104 	struct hostent host;
105 	char data[sizeof(struct hostent_data)];
106 };
107 
108 struct netdata {
109 	struct netent net;
110 	char data[sizeof(struct netent_data)];
111 };
112 
113 struct protodata {
114 	struct protoent proto;
115 	char data[sizeof(struct protoent_data)];
116 };
117 
118 struct hostdata *__hostdata_init(void);
119 struct hostent *__hostent_init(void);
120 struct hostent_data *__hostent_data_init(void);
121 struct netdata *__netdata_init(void);
122 struct netent_data *__netent_data_init(void);
123 struct protodata *__protodata_init(void);
124 struct protoent_data *__protoent_data_init(void);
125 int __copy_hostent(struct hostent *, struct hostent *, char *, size_t);
126 int __copy_netent(struct netent *, struct netent *, char *, size_t);
127 int __copy_protoent(struct protoent *, struct protoent *, char *, size_t);
128 
129 void __endprotoent_p(struct protoent_data *);
130 int __getprotoent_p(struct protoent *, struct protoent_data *);
131 void __setprotoent_p(int, struct protoent_data *);
132 void _endhostdnsent(void);
133 void _endhosthtent(struct hostent_data *);
134 void _endnetdnsent(void);
135 void _endnethtent(struct netent_data *);
136 void _map_v4v6_address(const char *, char *);
137 void _map_v4v6_hostent(struct hostent *, char **, char *);
138 void _sethostdnsent(int);
139 void _sethosthtent(int, struct hostent_data *);
140 void _setnetdnsent(int);
141 void _setnethtent(int, struct netent_data *);
142 
143 struct hostent *__dns_getanswer(const char *, int, const char *, int);
144 int _dns_gethostbyaddr(void *, void *, va_list);
145 int _dns_gethostbyname(void *, void *, va_list);
146 int _dns_getnetbyaddr(void *, void *, va_list);
147 int _dns_getnetbyname(void *, void *, va_list);
148 int _ht_gethostbyaddr(void *, void *, va_list);
149 int _ht_gethostbyname(void *, void *, va_list);
150 int _ht_getnetbyaddr(void *, void *, va_list);
151 int _ht_getnetbyname(void *, void *, va_list);
152 int _nis_gethostbyaddr(void *, void *, va_list);
153 int _nis_gethostbyname(void *, void *, va_list);
154 int _nis_getnetbyaddr(void *, void *, va_list);
155 int _nis_getnetbyname(void *, void *, va_list);
156 #ifdef NS_CACHING
157 int __proto_id_func(char *, size_t *, va_list, void *);
158 int __proto_marshal_func(char *, size_t *, void *, va_list, void *);
159 int __proto_unmarshal_func(char *, size_t, void *, va_list, void *);
160 #endif
161 
162 #endif /* _NETDB_PRIVATE_H_ */
163