xref: /haiku/headers/posix/netdb.h (revision b5efad4eae1a0754ce3e4eb87458279e81f61f61)
1 /*
2  * Copyright 2002-2020, Haiku, Inc. All rights reserved.
3  * Distributed under the terms of the MIT License.
4  */
5 
6 /*
7  * Copyright (c) 1980, 1983, 1988, 1993
8  *	The Regents of the University of California.  All rights reserved.
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  * 3. Neither the name of the University nor the names of its contributors
19  *    may be used to endorse or promote products derived from this software
20  *    without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 
35 /*
36  * Portions Copyright (c) 1993 by Digital Equipment Corporation.
37  *
38  * Permission to use, copy, modify, and distribute this software for any
39  * purpose with or without fee is hereby granted, provided that the above
40  * copyright notice and this permission notice appear in all copies, and that
41  * the name of Digital Equipment Corporation not be used in advertising or
42  * publicity pertaining to distribution of the document or software without
43  * specific, written prior permission.
44  *
45  * THE SOFTWARE IS PROVIDED "AS IS" AND DIGITAL EQUIPMENT CORP. DISCLAIMS ALL
46  * WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES
47  * OF MERCHANTABILITY AND FITNESS.   IN NO EVENT SHALL DIGITAL EQUIPMENT
48  * CORPORATION BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
49  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
50  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
51  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
52  * SOFTWARE.
53  */
54 
55 #ifndef _NETDB_H_
56 #define _NETDB_H_
57 
58 #include <stdint.h>
59 #include <netinet/in.h>
60 
61 #ifdef __cplusplus
62 extern "C" {
63 #endif
64 
65 #ifndef _PATH_NETWORKS
66 #define	_PATH_NETWORKS	"/etc/networks"
67 #endif
68 
69 extern int * __h_errno(void);
70 #define	h_errno (*__h_errno())
71 
72 /*
73  * Structures returned by network data base library.  All addresses are
74  * supplied in host order, and returned in network order (suitable for
75  * use in system calls).
76  */
77 struct hostent {
78 	char		*h_name;			/* official name of host */
79 	char		**h_aliases;		/* alias list */
80 	int			h_addrtype;			/* host address type */
81 	int			h_length;			/* length of address */
82 	char		**h_addr_list;		/* list of addresses from name server */
83 #define	h_addr	h_addr_list[0]	/* address, for backward compatibility */
84 };
85 
86 struct netent {
87 	char		*n_name;			/* official name of net */
88 	char		**n_aliases;		/* alias list */
89 	int			n_addrtype;			/* net address type */
90 	in_addr_t	n_net;				/* network # */
91 };
92 
93 struct servent {
94 	char		*s_name;			/* official service name */
95 	char		**s_aliases;		/* alias list */
96 	int			s_port;				/* port # */
97 	char		*s_proto;			/* protocol to use */
98 };
99 
100 struct protoent {
101 	char		*p_name;			/* official protocol name */
102 	char		**p_aliases;		/* alias list */
103 	int			p_proto;			/* protocol # */
104 };
105 
106 struct addrinfo {
107 	int			ai_flags;			/* AI_PASSIVE, AI_CANONNAME */
108 	int			ai_family;			/* PF_xxx */
109 	int			ai_socktype;		/* SOCK_xxx */
110 	int			ai_protocol;		/* 0 or IPPROTO_xxx for IPv4 and IPv6 */
111 	socklen_t	ai_addrlen;			/* length of ai_addr */
112 	char		*ai_canonname;		/* canonical name for hostname */
113 	struct sockaddr	*ai_addr; 		/* binary address */
114 	struct addrinfo	*ai_next; 		/* next structure in linked list */
115 };
116 
117 /*
118  * Error return codes from gethostbyname() and gethostbyaddr()
119  * (left in extern int h_errno).
120  */
121 #define	NETDB_INTERNAL	-1		/* see errno */
122 #define	NETDB_SUCCESS	0		/* no problem */
123 #define	HOST_NOT_FOUND	1		/* Authoritative Answer Host not found */
124 #define	TRY_AGAIN		2
125 	/* Non-Authoritive Host not found, or SERVERFAIL */
126 #define	NO_RECOVERY		3
127 	/* Non recoverable errors, FORMERR, REFUSED, NOTIMP */
128 #define	NO_DATA			4
129 	/* Valid name, no data record of requested type */
130 #define	NO_ADDRESS		NO_DATA	/* no address, look for MX record */
131 
132 /*
133  * Error return codes from getaddrinfo()
134  */
135 #define	EAI_ADDRFAMILY	1	/* address family for hostname not supported */
136 #define	EAI_AGAIN	 	2	/* temporary failure in name resolution */
137 #define	EAI_BADFLAGS	3	/* invalid value for ai_flags */
138 #define	EAI_FAIL		4	/* non-recoverable failure in name resolution */
139 #define	EAI_FAMILY		5	/* ai_family not supported */
140 #define	EAI_MEMORY		6	/* memory allocation failure */
141 #define	EAI_NODATA		7	/* no address associated with hostname */
142 #define	EAI_NONAME		8	/* hostname nor servname provided, or not known */
143 #define	EAI_SERVICE		9	/* servname not supported for ai_socktype */
144 #define	EAI_SOCKTYPE	10	/* ai_socktype not supported */
145 #define	EAI_SYSTEM		11	/* system error returned in errno */
146 #define EAI_BADHINTS	12
147 #define EAI_PROTOCOL	13
148 #define EAI_OVERFLOW	14
149 #define EAI_MAX			15
150 
151 /*
152  * Flag values for getaddrinfo()
153  */
154 #define	AI_PASSIVE		0x00000001
155 #define	AI_CANONNAME	0x00000002
156 #define AI_NUMERICHOST	0x00000004
157 #define AI_NUMERICSERV	0x00000008
158 #define	AI_MASK			\
159 	(AI_PASSIVE | AI_CANONNAME | AI_NUMERICHOST | AI_NUMERICSERV \
160 		| AI_ADDRCONFIG)
161 
162 /*
163  * Flag values for getipnodebyname()
164  */
165 #define AI_ALL			0x00000100
166 #define AI_V4MAPPED_CFG	0x00000200
167 #define AI_ADDRCONFIG	0x00000400
168 #define AI_V4MAPPED		0x00000800
169 #define AI_DEFAULT		(AI_V4MAPPED_CFG | AI_ADDRCONFIG)
170 
171 /*
172  * Constants for getnameinfo()
173  */
174 #define	NI_MAXHOST		1025
175 #define	NI_MAXSERV		32
176 
177 /*
178  * Flag values for getnameinfo()
179  */
180 #define	NI_NOFQDN		0x00000001
181 #define	NI_NUMERICHOST	0x00000002
182 #define	NI_NAMEREQD		0x00000004
183 #define	NI_NUMERICSERV	0x00000008
184 #define	NI_DGRAM		0x00000010
185 #define	NI_WITHSCOPEID	0x00000020
186 #define NI_NUMERICSCOPE	0x00000040
187 
188 /*
189  * Scope delimit character
190  */
191 #define SCOPE_DELIMITER	'%'
192 
193 void			endhostent(void);
194 void			endnetent(void);
195 void			endprotoent(void);
196 void			endservent(void);
197 void			freehostent(struct hostent *host);
198 struct hostent	*gethostbyaddr(const void *address, socklen_t length, int type);
199 struct hostent	*gethostbyname(const char *name);
200 struct hostent	*gethostbyname2(const char *name, int type);
201 struct hostent	*gethostent(void);
202 struct hostent	*getipnodebyaddr(const void *, size_t, int, int *);
203 struct hostent	*getipnodebyname(const char *, int, int, int *);
204 struct netent	*getnetbyaddr(uint32_t, int);
205 struct netent	*getnetbyname(const char *);
206 struct netent	*getnetent(void);
207 struct protoent	*getprotobyname(const char *);
208 struct protoent	*getprotobynumber(int);
209 struct protoent	*getprotoent(void);
210 struct servent	*getservbyname(const char *, const char *);
211 struct servent	*getservbyport(int, const char *);
212 struct servent	*getservent(void);
213 void			herror(const char *);
214 const char		*hstrerror(int);
215 void			sethostent(int);
216 void			setnetent(int);
217 void			setprotoent(int);
218 void			setservent(int);
219 int				getaddrinfo(const char *, const char *,
220 					const struct addrinfo *, struct addrinfo **);
221 int				getnameinfo(const struct sockaddr *, socklen_t, char *,
222 					socklen_t, char *, socklen_t, int);
223 struct addrinfo	*allocaddrinfo(socklen_t);
224 void			freeaddrinfo(struct addrinfo *);
225 const char		*gai_strerror(int);
226 
227 struct hostent	*gethostent_r(struct hostent *, char *, int, int *);
228 void			sethostent_r(int);
229 void			endhostent_r(void);
230 
231 struct netent	*getnetbyname_r(const char *, struct netent *,
232 					char *, int);
233 struct netent	*getnetbyaddr_r(long, int, struct netent *,
234 					char *, int);
235 struct netent	*getnetent_r(struct netent *, char *, int);
236 void			setnetent_r(int);
237 void			endnetent_r(void);
238 
239 struct protoent_data;
240 struct protoent	*getprotobyname_r(const char *,
241 				struct protoent *, struct protoent_data *);
242 struct protoent	*getprotobynumber_r(int,
243 				struct protoent *, struct protoent_data *);
244 struct protoent	*getprotoent_r(struct protoent *, struct protoent_data *);
245 void			setprotoent_r(int, struct protoent_data *);
246 void			endprotoent_r(struct protoent_data *);
247 
248 struct servent_data;
249 struct servent	*getservbyname_r(const char *name, const char *,
250 					struct servent *, struct servent_data *);
251 struct servent	*getservbyport_r(int port, const char *,
252 					struct servent *, struct servent_data *);
253 struct servent	*getservent_r(struct servent *, struct servent_data *);
254 void			setservent_r(int, struct servent_data *);
255 void			endservent_r(struct servent_data *);
256 
257 #ifdef __cplusplus
258 }
259 #endif
260 
261 #endif /* _NETDB_H_ */
262