1145117Sume/*-
2145117Sume * Copyright (C) 2005 The FreeBSD Project.  All rights reserved.
3145117Sume *
4145117Sume * Redistribution and use in source and binary forms, with or without
5145117Sume * modification, are permitted provided that the following conditions
6145117Sume * are met:
7145117Sume * 1. Redistributions of source code must retain the above copyright
8145117Sume *    notice, this list of conditions and the following disclaimer.
9145117Sume * 2. Redistributions in binary form must reproduce the above copyright
10145117Sume *    notice, this list of conditions and the following disclaimer in the
11145117Sume *    documentation and/or other materials provided with the distribution.
12145117Sume *
13145117Sume * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14145117Sume * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15145117Sume * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16145117Sume * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
17145117Sume * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18145117Sume * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19145117Sume * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20145117Sume * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21145117Sume * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22145117Sume * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
23145117Sume * SUCH DAMAGE.
24145117Sume *
25145117Sume * $FreeBSD$
26145117Sume */
27145117Sume
28145117Sume#ifndef _NETDB_PRIVATE_H_
29145117Sume#define _NETDB_PRIVATE_H_
30145117Sume
31145626Sume#include <stdio.h>				/* XXX: for FILE */
32145117Sume
33157779Sume#define	NETDB_THREAD_ALLOC(name)					\
34157779Sumestatic struct name name;						\
35157779Sumestatic thread_key_t name##_key;						\
36157779Sumestatic once_t name##_init_once = ONCE_INITIALIZER;			\
37157779Sumestatic int name##_thr_keycreated = 0;					\
38157779Sume\
39157779Sumestatic void name##_free(void *);					\
40157779Sume\
41157779Sumestatic void								\
42157779Sumename##_keycreate(void)							\
43157779Sume{									\
44157779Sume	name##_thr_keycreated =						\
45157779Sume	    (thr_keycreate(&name##_key, name##_free) == 0);		\
46157779Sume}									\
47157779Sume\
48157779Sumestruct name *								\
49157779Sume__##name##_init(void)							\
50157779Sume{									\
51157779Sume	struct name *he;						\
52157779Sume									\
53157779Sume	if (thr_main() != 0)						\
54157779Sume		return (&name);						\
55157779Sume	if (thr_once(&name##_init_once, name##_keycreate) != 0 ||	\
56157779Sume	    !name##_thr_keycreated)					\
57157779Sume		return (NULL);						\
58157779Sume	if ((he = thr_getspecific(name##_key)) != NULL)			\
59157779Sume		return (he);						\
60157779Sume	if ((he = calloc(1, sizeof(*he))) == NULL)			\
61157779Sume		return (NULL);						\
62157779Sume	if (thr_setspecific(name##_key, he) == 0)			\
63157779Sume		return (he);						\
64157779Sume	free(he);							\
65157779Sume	return (NULL);							\
66157779Sume}
67157779Sume
68145626Sume#define	_MAXALIASES	35
69145626Sume#define	_MAXLINELEN	1024
70145626Sume#define	_MAXADDRS	35
71145633Sume#define	_HOSTBUFSIZE	(8 * 1024)
72145626Sume#define	_NETBUFSIZE	1025
73145117Sume
74145633Sumestruct hostent_data {
75145633Sume	uint32_t host_addr[4];			/* IPv4 or IPv6 */
76145633Sume	char *h_addr_ptrs[_MAXADDRS + 1];
77145633Sume	char *host_aliases[_MAXALIASES];
78145633Sume	char hostbuf[_HOSTBUFSIZE];
79145633Sume	FILE *hostf;
80145633Sume	int stayopen;
81145633Sume#ifdef YP
82145633Sume	char *yp_domain;
83145633Sume#endif
84145633Sume};
85145633Sume
86145626Sumestruct netent_data {
87145626Sume	char *net_aliases[_MAXALIASES];
88145626Sume	char netbuf[_NETBUFSIZE];
89145626Sume	FILE *netf;
90145626Sume	int stayopen;
91145626Sume#ifdef YP
92145626Sume	char *yp_domain;
93145626Sume#endif
94145626Sume};
95145626Sume
96145279Sumestruct protoent_data {
97145279Sume	FILE *fp;
98145626Sume	char *aliases[_MAXALIASES];
99145279Sume	int stayopen;
100145626Sume	char line[_MAXLINELEN + 1];
101145279Sume};
102145279Sume
103145633Sumestruct hostdata {
104145633Sume	struct hostent host;
105157779Sume	char data[sizeof(struct hostent_data)];
106145633Sume};
107145633Sume
108145626Sumestruct netdata {
109145626Sume	struct netent net;
110157779Sume	char data[sizeof(struct netent_data)];
111145626Sume};
112145626Sume
113145626Sumestruct protodata {
114145626Sume	struct protoent proto;
115157779Sume	char data[sizeof(struct protoent_data)];
116145626Sume};
117145626Sume
118145633Sumestruct hostdata *__hostdata_init(void);
119157779Sumestruct hostent *__hostent_init(void);
120157779Sumestruct hostent_data *__hostent_data_init(void);
121145626Sumestruct netdata *__netdata_init(void);
122157779Sumestruct netent_data *__netent_data_init(void);
123145279Sumestruct protodata *__protodata_init(void);
124157779Sumestruct protoent_data *__protoent_data_init(void);
125157779Sumeint __copy_hostent(struct hostent *, struct hostent *, char *, size_t);
126157779Sumeint __copy_netent(struct netent *, struct netent *, char *, size_t);
127157779Sumeint __copy_protoent(struct protoent *, struct protoent *, char *, size_t);
128157779Sume
129157779Sumevoid __endprotoent_p(struct protoent_data *);
130157779Sumeint __getprotoent_p(struct protoent *, struct protoent_data *);
131157779Sumevoid __setprotoent_p(int, struct protoent_data *);
132145602Sumevoid _endhostdnsent(void);
133145633Sumevoid _endhosthtent(struct hostent_data *);
134145602Sumevoid _endnetdnsent(void);
135145626Sumevoid _endnethtent(struct netent_data *);
136158477Sumestruct hostent *_gethostbynisaddr(const void *, socklen_t, int);
137145602Sumestruct hostent *_gethostbynisname(const char *, int);
138145602Sumevoid _map_v4v6_address(const char *, char *);
139145635Sumevoid _map_v4v6_hostent(struct hostent *, char **, char *);
140145602Sumevoid _sethostdnsent(int);
141145633Sumevoid _sethosthtent(int, struct hostent_data *);
142145602Sumevoid _setnetdnsent(int);
143145626Sumevoid _setnethtent(int, struct netent_data *);
144145117Sume
145145117Sume#endif /* _NETDB_PRIVATE_H_ */
146