Tizen RT Libs&Environment  v1.0 D5
netdb.h
Go to the documentation of this file.
1 /****************************************************************************
2  *
3  * Copyright 2016 Samsung Electronics All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing,
12  * software distributed under the License is distributed on an
13  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
14  * either express or implied. See the License for the specific
15  * language governing permissions and limitations under the License.
16  *
17  ****************************************************************************/
18 /****************************************************************************
19  * include/netdb.h
20  *
21  * Copyright (C) 2015 Gregory Nutt. All rights reserved.
22  * Author: Gregory Nutt <gnutt@nuttx.org>
23  *
24  * Reference: http://pubs.opengroup.org/onlinepubs/009695399/basedefs/netdb.h.html
25  *
26  * Redistribution and use in source and binary forms, with or without
27  * modification, are permitted provided that the following conditions
28  * are met:
29  *
30  * 1. Redistributions of source code must retain the above copyright
31  * notice, this list of conditions and the following disclaimer.
32  * 2. Redistributions in binary form must reproduce the above copyright
33  * notice, this list of conditions and the following disclaimer in
34  * the documentation and/or other materials provided with the
35  * distribution.
36  * 3. Neither the name NuttX nor the names of its contributors may be
37  * used to endorse or promote products derived from this software
38  * without specific prior written permission.
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
41  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
42  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
43  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
44  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
45  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
46  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
47  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
48  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
50  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
51  * POSSIBILITY OF SUCH DAMAGE.
52  *
53  ****************************************************************************/
65 #ifndef __INCLUDE_NETDB_H
66 #define __INCLUDE_NETDB_H
67 
68 /****************************************************************************
69  * Included Files
70  ****************************************************************************/
71 
72 #include <tinyara/config.h>
73 #include <tinyara/compiler.h>
74 
75 /* Inclusion of the <netdb.h> header may also make visible all symbols from
76  * <netinet/in.h>, <sys/socket.h>, and <inttypes.h>.
77  */
78 
79 #include <inttypes.h>
80 
81 #include <netinet/in.h>
82 #include <sys/socket.h>
83 
84 /****************************************************************************
85  * Pre-processor Definitions
86  ****************************************************************************/
87 
88 /* The <netdb.h> header shall define the IPPORT_RESERVED macro with the
89  * value of the highest reserved Internet port number.
90  */
91 
92 #define IPPORT_RESERVED 0xffff /* No reserved port numbers */
93 
94 /* The <netdb.h> header shall define the following macros that evaluate to
95  * bitwise-distinct integer constants for use in the flags field of the
96  * addrinfo structure:
97  *
98  * AI_PASSIVE - Socket address is intended for bind().
99  * AI_CANONNAME - Request for canonical name.
100  * AI_NUMERICHOST - Return numeric host address as name.
101  * AI_NUMERICSERV - Inhibit service name resolution.
102  * AI_V4MAPPED - If no IPv6 addresses are found, query for IPv4
103  * addresses and return them to the caller as IPv4-mapped
104  * IPv6 addresses.
105  * AI_ALL - Query for both IPv4 and IPv6 addresses.
106  * AI_ADDRCONFIG - Query for IPv4 addresses only when an IPv4 address is
107  * configured; query for IPv6 addresses only when an IPv6
108  * address is configured.
109  */
110 
111 #define AI_PASSIVE (1 << 0)
112 #define AI_CANONNAME (1 << 1)
113 #define AI_NUMERICHOST (1 << 2)
114 #define AI_NUMERICSERV (1 << 3)
115 #define AI_V4MAPPED (1 << 4)
116 #define AI_ALL (1 << 5)
117 #define AI_ADDRCONFIG (1 << 6)
118 
119 /* The <netdb.h> header shall define the following macros that evaluate to
120  * bitwise-distinct integer constants for use in the flags argument to
121  * getnameinfo():
122  *
123  * NI_NOFQDN - Only the nodename portion of the FQDN is returned for
124  * local hosts.
125  * NI_NUMERICHOST - The numeric form of the node's address is returned
126  * instead of its name.
127  * NI_NAMEREQD - Return an error if the node's name cannot be located
128  * in the database.
129  * NI_NUMERICSERV - The numeric form of the service address is returned
130  * instead of its name.
131  * NI_NUMERICSCOPE - For IPv6 addresses, the numeric form of the scope
132  * identifier is returned instead of its name.
133  * NI_DGRAM - Indicates that the service is a datagram service
134  * (SOCK_DGRAM).
135  */
136 
137 #define NI_NOFQDN (1 << 0)
138 #define NI_NUMERICHOST (1 << 1)
139 #define NI_NAMEREQD (1 << 2)
140 #define NI_NUMERICSERV (1 << 3)
141 #define NI_NUMERICSCOPE (1 << 4)
142 #define NI_DGRAM (1 << 5)
143 
144 /* The <netdb.h> header shall define the following macros for use as error
145  * values for gethostbyaddr() and gethostbyname()
146  */
147 
148 #define HOST_NOT_FOUND 1
149 #define NO_DATA 2
150 #define NO_RECOVERY 3
151 #define TRY_AGAIN 4
152 
153 /* Address Information Errors. The <netdb.h> header shall define the
154  * following macros for use as error values for getaddrinfo() and
155  * getnameinfo():
156  *
157  * EAI_AGAIN - The name could not be resolved at this time. Future
158  * attempts may succeed.
159  * EAI_BADFLAGS - The flags had an invalid value.EAI_FAILA non-
160  * recoverable error occurred.
161  * EAI_FAMILY - The address family was not recognized or the address
162  * length was invalid for the specified family.
163  * EAI_MEMORY - There was a memory allocation failure.
164  * EAI_NONAME - The name does not resolve for the supplied
165  * parameters. NI_NAMEREQD is set and the host's name
166  * cannot be located, or both nodename and servname were
167  * null.
168  * EAI_SERVICE - The service passed was not recognized for the
169  * specified socket type.
170  * EAI_SOCKTYPE - The intended socket type was not recognized.
171  * EAI_SYSTEM - A system error occurred. The error code can be found
172  * in errno.
173  * EAI_OVERFLOW - An argument buffer overflowed.
174  */
175 
176 #define EAI_AGAIN 1
177 #define EAI_BADFLAGS 2
178 #define EAI_FAMILY 3
179 #define EAI_MEMORY 4
180 #define EAI_NONAME 5
181 #define EAI_SERVICE 6
182 #define EAI_SOCKTYPE 7
183 #define EAI_SYSTEM 8
184 #define EAI_OVERFLOW 9
185 #define EAI_FAIL -1
186 
187 /* h_errno values that may be returned by gethosbyname(), gethostbyname_r(),
188  * gethostbyaddr(), or gethostbyaddr_r()
189  *
190  * HOST_NOT_FOUND - No such host is known.
191  *
192  * NO_DATA - The server recognized the request and the name, but no
193  * address is available. Another type of request to the name server
194  * for the domain might return an answer.
195  *
196  * NO_RECOVERY - An unexpected server failure occurred which cannot be
197  * recovered.
198  *
199  * TRY_AGAIN - A temporary and possibly transient error occurred, such as
200  * a failure of a server to respond.
201  */
202 
203 #define HOST_NOT_FOUND 1
204 #define NO_DATA 2
205 #define NO_RECOVERY 3
206 #define TRY_AGAIN 4
207 
208 #define _PATH_SERVICES "/etc/services"
209 
210 /****************************************************************************
211  * Public Types
212  ****************************************************************************/
214 struct hostent {
215  FAR char *h_name; /* Official name of the host. */
216  FAR char **h_aliases; /* A pointer to an array of pointers to alternative
217  * host names, terminated by a null pointer. */
218  int h_addrtype; /* Address type. */
219  int h_length; /* The length, in bytes, of the address. */
220  FAR char **h_addr_list; /* A pointer to an array of pointers to network
221  * addresses (in network byte order) for the host,
222  * terminated by a null pointer. */
223 };
224 
225 #define h_addr h_addr_list[0] /* For backward compatibility */
226 
227 struct netent {
228  FAR char *n_name; /* Official, fully-qualified (including the domain)
229  * name of the host. */
230  FAR char **n_aliases; /* A pointer to an array of pointers to alternative
231  * network names, terminated by a null pointer. */
232  int n_addrtype; /* The address type of the network. */
233  uint32_t n_net; /* The network number, in host byte order. */
234 };
235 
236 struct protoent {
237  FAR char *p_name; /* Official name of the protocol. */
238  FAR char **p_aliases; /* A pointer to an array of pointers to
239  * alternative protocol names, terminated by a
240  * null pointer. */
241  int p_proto; /* The protocol number. */
242 };
243 
244 struct servent {
245  FAR char *s_name; /* Official name of the service. */
246  FAR char **s_aliases; /* A pointer to an array of pointers to
247  * alternative service names, terminated by a
248  * null pointer. */
249  int s_port; /* The port number at which the service resides,
250  * in network byte order. */
251  FAR char *s_proto; /* The name of the protocol to use when
252  * contacting the service. */
253 };
254 
255 struct addrinfo {
256  int ai_flags; /* Input flags. */
257  int ai_family; /* Address family of socket. */
258  int ai_socktype; /* Socket type. */
259  int ai_protocol; /* Protocol of socket. */
260  socklen_t ai_addrlen; /* Length of socket address. */
261 
262  FAR struct sockaddr *ai_addr; /* Socket address of socket. */
263  FAR char *ai_canonname; /* Canonical name of service location. */
264  FAR struct addrinfo *ai_next; /* Pointer to next in list. */
265 };
266 
267 struct servent_data {
268  void *fp;
269  char **aliases;
271  int stayopen;
272  char *line;
273 };
274 
275 /****************************************************************************
276  * Public Data
277  ****************************************************************************/
278 
279 #ifdef __cplusplus
280 #define EXTERN extern "C"
281 extern "C" {
282 #else
283 #define EXTERN extern
284 #endif
285 
286 /* When the <netdb.h> header is included, h_errno shall be available as a
287  * modifiable lvalue of type int. It is unspecified whether h_errno is a
288  * macro or an identifier declared with external linkage.
289  */
290 
291 /* REVISIT: This should at least be per-task? */
293 
294 /****************************************************************************
295  * Public Function Prototypes
296  ****************************************************************************/
297 
298 #ifdef CONFIG_LIBC_NETDB
299 #if 0 /* None of these are yet supported */
300 
301 void endhostent(void);
302 void endnetent(void);
303 void endprotoent(void);
304 
305 FAR const char *gai_strerror(int);
306 
307 #endif
308 
309 // @cond
310 void endservent(void);
311 // @endcond
312 
322 int getaddrinfo(const char *host, const char *service, const struct addrinfo *hints, struct addrinfo **res);
323 
330 void freeaddrinfo(FAR struct addrinfo *ai);
331 
338 FAR struct hostent *gethostbyaddr(FAR const void *addr, socklen_t len, int type);
339 
346 FAR struct hostent *gethostbyname(FAR const char *name);
347 
348 #if 0 /* None of these are yet supported */
349 FAR struct hostent *gethostent(void);
350 
351 FAR struct netent *getnetbyaddr(uint32_t, int);
352 FAR struct netent *getnetbyname(FAR const char *);
353 FAR struct netent *getnetent(void);
354 FAR struct protoent *getprotobyname(FAR const char *);
355 FAR struct protoent *getprotobynumber(int);
356 FAR struct protoent *getprotoent(void);
357 FAR struct servent *getservbyport(int, FAR const char *);
358 void sethostent(int);
359 void setnetent(int);
360 void setprotoent(int);
361 
362 #endif /* None of these are yet supported */
363 
364 // @cond
365 FAR struct servent *getservbyname(FAR const char *, FAR const char *);
366 FAR struct servent *getservent(void);
367 void setservent(int);
368 
369 int getnameinfo(const struct sockaddr *sa, size_t salen, char *host, size_t hostlen, char *serv, size_t servlen, int flags);
370 
371 /* Non-standard interfaces similar to Glibc 2 interfaces */
372 
373 int gethostbyaddr_r(FAR const void *addr, socklen_t len, int type, FAR struct hostent *host, FAR char *buf, size_t buflen, int *h_errnop);
374 int gethostbyname_r(FAR const char *name, FAR struct hostent *host, FAR char *buf, size_t buflen, int *h_errnop);
375 // @endcond
376 
377 #endif /* CONFIG_LIBC_NETDB */
378 
379 #undef EXTERN
380 #ifdef __cplusplus
381 }
382 #endif
383 
384 #endif /* __INCLUDE_NETDB_H */
385  // end of SOCKET group
FAR char ** s_aliases
Definition: netdb.h:246
Definition: netdb.h:227
int getaddrinfo(const char *host, const char *service, const struct addrinfo *hints, struct addrinfo **res)
getaddrinfo() is a function that returns information on a particular host name (such as its IP addres...
char ** aliases
Definition: netdb.h:269
Definition: netdb.h:244
int ai_flags
Definition: netdb.h:256
int s_port
Definition: netdb.h:249
Definition: netdb.h:214
FAR struct hostent * gethostbyname(FAR const char *name)
gethostbyname() is the function sending DNS query to DNS server with hostname
FAR struct hostent * gethostbyaddr(FAR const void *addr, socklen_t len, int type)
gethostbyaddr() is the function returns a corresponding hostname with given IP addresses ...
int ai_family
Definition: netdb.h:257
int n_addrtype
Definition: netdb.h:232
FAR struct addrinfo * ai_next
Definition: netdb.h:264
FAR char * s_proto
Definition: netdb.h:251
uint32_t n_net
Definition: netdb.h:233
EXTERN int h_errno
Definition: netdb.h:292
FAR char ** p_aliases
Definition: netdb.h:238
char * line
Definition: netdb.h:272
void freeaddrinfo(FAR struct addrinfo *ai)
freeaddrinfo() frees the memory allocated by the getaddrinfo() function.
int maxaliases
Definition: netdb.h:270
int ai_protocol
Definition: netdb.h:259
int stayopen
Definition: netdb.h:271
socklen_t ai_addrlen
Definition: netdb.h:260
#define EXTERN
Definition: netdb.h:283
FAR char ** n_aliases
Definition: netdb.h:230
FAR char * ai_canonname
Definition: netdb.h:263
Inttypes APIs.
int h_length
Definition: netdb.h:219
FAR char * s_name
Definition: netdb.h:245
FAR char * n_name
Definition: netdb.h:228
FAR char * p_name
Definition: netdb.h:237
int p_proto
Definition: netdb.h:241
void * fp
Definition: netdb.h:268
FAR char ** h_aliases
Definition: netdb.h:216
FAR char ** h_addr_list
Definition: netdb.h:220
FAR char * h_name
Definition: netdb.h:215
int h_addrtype
Definition: netdb.h:218
Socket APIs.
FAR struct sockaddr * ai_addr
Definition: netdb.h:262
int ai_socktype
Definition: netdb.h:258