xref: /freebsd/lib/libc/net/netdb_private.h (revision 6be3386466ab79a84b48429ae66244f21526d3df)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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  * $FreeBSD$
28  */
29 
30 #ifndef _NETDB_PRIVATE_H_
31 #define _NETDB_PRIVATE_H_
32 
33 #include <stdio.h>				/* XXX: for FILE */
34 
35 #define	NETDB_THREAD_ALLOC(name)					\
36 static struct name name;						\
37 static thread_key_t name##_key;						\
38 static once_t name##_init_once = ONCE_INITIALIZER;			\
39 static int name##_thr_keycreated = 0;					\
40 \
41 static void name##_free(void *);					\
42 \
43 static void								\
44 name##_keycreate(void)							\
45 {									\
46 	name##_thr_keycreated =						\
47 	    (thr_keycreate(&name##_key, name##_free) == 0);		\
48 }									\
49 \
50 struct name *								\
51 __##name##_init(void)							\
52 {									\
53 	struct name *he;						\
54 									\
55 	if (thr_main() != 0)						\
56 		return (&name);						\
57 	if (thr_once(&name##_init_once, name##_keycreate) != 0 ||	\
58 	    !name##_thr_keycreated)					\
59 		return (NULL);						\
60 	if ((he = thr_getspecific(name##_key)) != NULL)			\
61 		return (he);						\
62 	if ((he = calloc(1, sizeof(*he))) == NULL)			\
63 		return (NULL);						\
64 	if (thr_setspecific(name##_key, he) == 0)			\
65 		return (he);						\
66 	free(he);							\
67 	return (NULL);							\
68 }
69 
70 #define	_MAXALIASES	35
71 #define	_MAXLINELEN	1024
72 #define	_MAXADDRS	35
73 #define	_HOSTBUFSIZE	(8 * 1024)
74 #define	_NETBUFSIZE	1025
75 
76 struct hostent_data {
77 	uint32_t host_addr[4];			/* IPv4 or IPv6 */
78 	char *h_addr_ptrs[_MAXADDRS + 1];
79 	char *host_aliases[_MAXALIASES];
80 	char hostbuf[_HOSTBUFSIZE];
81 	FILE *hostf;
82 	int stayopen;
83 #ifdef YP
84 	char *yp_domain;
85 #endif
86 };
87 
88 struct netent_data {
89 	char *net_aliases[_MAXALIASES];
90 	char netbuf[_NETBUFSIZE];
91 	FILE *netf;
92 	int stayopen;
93 #ifdef YP
94 	char *yp_domain;
95 #endif
96 };
97 
98 struct protoent_data {
99 	FILE *fp;
100 	char *aliases[_MAXALIASES];
101 	int stayopen;
102 	char line[_MAXLINELEN + 1];
103 };
104 
105 struct hostdata {
106 	struct hostent host;
107 	char data[sizeof(struct hostent_data)];
108 };
109 
110 struct netdata {
111 	struct netent net;
112 	char data[sizeof(struct netent_data)];
113 };
114 
115 struct protodata {
116 	struct protoent proto;
117 	char data[sizeof(struct protoent_data)];
118 };
119 
120 struct hostdata *__hostdata_init(void);
121 struct hostent *__hostent_init(void);
122 struct hostent_data *__hostent_data_init(void);
123 struct netdata *__netdata_init(void);
124 struct netent_data *__netent_data_init(void);
125 struct protodata *__protodata_init(void);
126 struct protoent_data *__protoent_data_init(void);
127 int __copy_hostent(struct hostent *, struct hostent *, char *, size_t);
128 int __copy_netent(struct netent *, struct netent *, char *, size_t);
129 int __copy_protoent(struct protoent *, struct protoent *, char *, size_t);
130 
131 void __endprotoent_p(struct protoent_data *);
132 int __getprotoent_p(struct protoent *, struct protoent_data *);
133 void __setprotoent_p(int, struct protoent_data *);
134 void _endhostdnsent(void);
135 void _endhosthtent(struct hostent_data *);
136 void _endnetdnsent(void);
137 void _endnethtent(struct netent_data *);
138 void _map_v4v6_address(const char *, char *);
139 void _map_v4v6_hostent(struct hostent *, char **, char *);
140 void _sethostdnsent(int);
141 void _sethosthtent(int, struct hostent_data *);
142 void _setnetdnsent(int);
143 void _setnethtent(int, struct netent_data *);
144 
145 struct hostent *__dns_getanswer(const char *, int, const char *, int);
146 int _dns_gethostbyaddr(void *, void *, va_list);
147 int _dns_gethostbyname(void *, void *, va_list);
148 int _dns_getnetbyaddr(void *, void *, va_list);
149 int _dns_getnetbyname(void *, void *, va_list);
150 int _ht_gethostbyaddr(void *, void *, va_list);
151 int _ht_gethostbyname(void *, void *, va_list);
152 int _ht_getnetbyaddr(void *, void *, va_list);
153 int _ht_getnetbyname(void *, void *, va_list);
154 int _nis_gethostbyaddr(void *, void *, va_list);
155 int _nis_gethostbyname(void *, void *, va_list);
156 int _nis_getnetbyaddr(void *, void *, va_list);
157 int _nis_getnetbyname(void *, void *, va_list);
158 #ifdef NS_CACHING
159 int __proto_id_func(char *, size_t *, va_list, void *);
160 int __proto_marshal_func(char *, size_t *, void *, va_list, void *);
161 int __proto_unmarshal_func(char *, size_t, void *, va_list, void *);
162 #endif
163 
164 #endif /* _NETDB_PRIVATE_H_ */
165