netdb.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /****************************************************************************
  2. * include/netdb.h
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one or more
  5. * contributor license agreements. See the NOTICE file distributed with
  6. * this work for additional information regarding copyright ownership. The
  7. * ASF licenses this file to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance with the
  9. * License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. * License for the specific language governing permissions and limitations
  17. * under the License.
  18. *
  19. ****************************************************************************/
  20. #ifndef __INCLUDE_NETDB_H
  21. #define __INCLUDE_NETDB_H
  22. /****************************************************************************
  23. * Included Files
  24. ****************************************************************************/
  25. #include <nuttx/config.h>
  26. #include <nuttx/compiler.h>
  27. /* Inclusion of the <netdb.h> header may also make visible all symbols from
  28. * <netinet/in.h>, <sys/socket.h>, and <inttypes.h>.
  29. */
  30. #include <inttypes.h>
  31. #include <netinet/in.h>
  32. #include <sys/socket.h>
  33. /****************************************************************************
  34. * Pre-processor Definitions
  35. ****************************************************************************/
  36. /* The <netdb.h> header shall define the IPPORT_RESERVED macro with the
  37. * value of the highest reserved Internet port number.
  38. */
  39. #define IPPORT_RESERVED 0xffff /* No reserved port numbers */
  40. /* The <netdb.h> header shall define the following macros that evaluate to
  41. * bitwise-distinct integer constants for use in the flags field of the
  42. * addrinfo structure:
  43. *
  44. * AI_PASSIVE - Socket address is intended for bind().
  45. * AI_CANONNAME - Request for canonical name.
  46. * AI_NUMERICHOST - Return numeric host address as name.
  47. * AI_NUMERICSERV - Inhibit service name resolution.
  48. * AI_V4MAPPED - If no IPv6 addresses are found, query for IPv4
  49. * addresses and return them to the caller as IPv4-mapped
  50. * IPv6 addresses.
  51. * AI_ALL - Query for both IPv4 and IPv6 addresses.
  52. * AI_ADDRCONFIG - Query for IPv4 addresses only when an IPv4 address is
  53. * configured; query for IPv6 addresses only when an IPv6
  54. * address is configured.
  55. */
  56. #define AI_PASSIVE (1 << 0)
  57. #define AI_CANONNAME (1 << 1)
  58. #define AI_NUMERICHOST (1 << 2)
  59. #define AI_NUMERICSERV (1 << 3)
  60. #define AI_V4MAPPED (1 << 4)
  61. #define AI_ALL (1 << 5)
  62. #define AI_ADDRCONFIG (1 << 6)
  63. /* The <netdb.h> header shall define the following macros that evaluate to
  64. * bitwise-distinct integer constants for use in the flags argument to
  65. * getnameinfo():
  66. *
  67. * NI_NOFQDN - Only the nodename portion of the FQDN is returned for
  68. * local hosts.
  69. * NI_NUMERICHOST - The numeric form of the node's address is returned
  70. * instead of its name.
  71. * NI_NAMEREQD - Return an error if the node's name cannot be located
  72. * in the database.
  73. * NI_NUMERICSERV - The numeric form of the service address is returned
  74. * instead of its name.
  75. * NI_NUMERICSCOPE - For IPv6 addresses, the numeric form of the scope
  76. * identifier is returned instead of its name.
  77. * NI_DGRAM - Indicates that the service is a datagram service
  78. * (SOCK_DGRAM).
  79. */
  80. #define NI_NOFQDN (1 << 0)
  81. #define NI_NUMERICHOST (1 << 1)
  82. #define NI_NAMEREQD (1 << 2)
  83. #define NI_NUMERICSERV (1 << 3)
  84. #define NI_NUMERICSCOPE (1 << 4)
  85. #define NI_DGRAM (1 << 5)
  86. /* Address Information Errors. The <netdb.h> header shall define the
  87. * following macros for use as error values for getaddrinfo() and
  88. * getnameinfo():
  89. *
  90. * EAI_AGAIN - The name could not be resolved at this time. Future
  91. * attempts may succeed.
  92. * EAI_BADFLAGS - The flags had an invalid value.
  93. * EAI_FAIL - A non-recoverable error occurred.
  94. * EAI_FAMILY - The address family was not recognized or the address
  95. * length was invalid for the specified family.
  96. * EAI_MEMORY - There was a memory allocation failure.
  97. * EAI_NONAME - The name does not resolve for the supplied
  98. * parameters. NI_NAMEREQD is set and the host's name
  99. * cannot be located, or both nodename and servname were
  100. * null.
  101. * EAI_SERVICE - The service passed was not recognized for the
  102. * specified socket type.
  103. * EAI_SOCKTYPE - The intended socket type was not recognized.
  104. * EAI_SYSTEM - A system error occurred. The error code can be found
  105. * in errno.
  106. * EAI_OVERFLOW - An argument buffer overflowed.
  107. */
  108. #define EAI_AGAIN 1
  109. #define EAI_BADFLAGS 2
  110. #define EAI_FAIL 3
  111. #define EAI_FAMILY 4
  112. #define EAI_MEMORY 5
  113. #define EAI_NONAME 6
  114. #define EAI_SERVICE 7
  115. #define EAI_SOCKTYPE 8
  116. #define EAI_SYSTEM 9
  117. #define EAI_OVERFLOW 10
  118. /* h_errno values that may be returned by gethosbyname(), gethostbyname_r(),
  119. * gethostbyaddr(), or gethostbyaddr_r()
  120. *
  121. * HOST_NOT_FOUND - No such host is known.
  122. *
  123. * NO_DATA - The server recognized the request and the name, but no
  124. * address is available. Another type of request to the name server
  125. * for the domain might return an answer.
  126. *
  127. * NO_RECOVERY - An unexpected server failure occurred which cannot be
  128. * recovered.
  129. *
  130. * TRY_AGAIN - A temporary and possibly transient error occurred, such as
  131. * a failure of a server to respond.
  132. *
  133. * These are obsolete and were removed in the Open Group Base Specifications
  134. * Issue 7, 2018 edition.
  135. */
  136. #define HOST_NOT_FOUND 1
  137. #define NO_DATA 2
  138. #define NO_ADDRESS NO_DATA
  139. #define NO_RECOVERY 3
  140. #define TRY_AGAIN 4
  141. /* NI_MAXHOST is the max of
  142. *
  143. * CONFIG_NETDB_DNSCLIENT_NAMESIZE + 1
  144. * INET6_ADDRSTRLEN
  145. * INET_ADDRSTRLEN
  146. *
  147. * Note: INETxxx_ADDRSTRLEN already includes the terminating NUL.
  148. * Note: INET6_ADDRSTRLEN > INET_ADDRSTRLEN is assumed.
  149. */
  150. #if defined(CONFIG_NET_IPv6)
  151. #define _INET_ADDRSTRLEN INET6_ADDRSTRLEN
  152. #else
  153. #define _INET_ADDRSTRLEN INET_ADDRSTRLEN
  154. #endif
  155. #if defined(CONFIG_NETDB_DNSCLIENT) && \
  156. (CONFIG_NETDB_DNSCLIENT_NAMESIZE + 1) > _INET_ADDRSTRLEN
  157. #define NI_MAXHOST (CONFIG_NETDB_DNSCLIENT_NAMESIZE + 1)
  158. #else
  159. #define NI_MAXHOST _INET_ADDRSTRLEN
  160. #endif
  161. /* Right now, g_services_db[] only has "ntp".
  162. * 16 should be large enough.
  163. */
  164. #define NI_MAXSERV 16
  165. /****************************************************************************
  166. * Public Types
  167. ****************************************************************************/
  168. struct hostent
  169. {
  170. FAR char *h_name; /* Official name of the host. */
  171. FAR char **h_aliases; /* A pointer to an array of pointers to the
  172. * alternative host names, terminated by a
  173. * null pointer. */
  174. int h_addrtype; /* Address type. */
  175. int h_length; /* The length, in bytes, of the address. */
  176. FAR char **h_addr_list; /* A pointer to an array of pointers to network
  177. * addresses (in network byte order) for the host,
  178. * terminated by a null pointer. */
  179. };
  180. #define h_addr h_addr_list[0] /* For backward compatibility */
  181. struct netent
  182. {
  183. FAR char *n_name; /* Official, fully-qualified(including the domain)
  184. * name of the host. */
  185. FAR char **n_aliases; /* A pointer to an array of pointers to the
  186. * alternative network names, terminated by a
  187. * null pointer. */
  188. int n_addrtype; /* The address type of the network. */
  189. uint32_t n_net; /* The network number, in host byte order. */
  190. };
  191. struct protoent
  192. {
  193. FAR char *p_name; /* Official name of the protocol. */
  194. FAR char **p_aliases; /* A pointer to an array of pointers to
  195. * alternative protocol names, terminated by a
  196. * null pointer. */
  197. int p_proto; /* The protocol number. */
  198. };
  199. struct servent
  200. {
  201. FAR char *s_name; /* Official name of the service. */
  202. FAR char **s_aliases; /* A pointer to an array of pointers to
  203. * alternative service names, terminated by a
  204. * null pointer. */
  205. int s_port; /* The port number at which the service resides,
  206. * in network byte order. */
  207. FAR char *s_proto; /* The name of the protocol to use when
  208. * contacting the service. */
  209. };
  210. struct addrinfo
  211. {
  212. int ai_flags; /* Input flags. */
  213. int ai_family; /* Address family of socket. */
  214. int ai_socktype; /* Socket type. */
  215. int ai_protocol; /* Protocol of socket. */
  216. socklen_t ai_addrlen; /* Length of socket address. */
  217. FAR struct sockaddr *ai_addr; /* Socket address of socket. */
  218. FAR char *ai_canonname; /* Canonical name of service location. */
  219. FAR struct addrinfo *ai_next; /* Pointer to next in list. */
  220. };
  221. /****************************************************************************
  222. * Public Data
  223. ****************************************************************************/
  224. #ifdef __cplusplus
  225. #define EXTERN extern "C"
  226. extern "C"
  227. {
  228. #else
  229. #define EXTERN extern
  230. #endif
  231. /* When the <netdb.h> header is included, h_errno shall be available as a
  232. * modifiable lvalue of type int. It is unspecified whether h_errno is a
  233. * macro or an identifier declared with external linkage.
  234. *
  235. * h_errno is obsolete and was removed in the Open Group Base Specifications
  236. * Issue 7, 2018 edition.
  237. */
  238. /* REVISIT: This should at least be per-task? */
  239. EXTERN int h_errno;
  240. /****************************************************************************
  241. * Public Function Prototypes
  242. ****************************************************************************/
  243. #ifdef CONFIG_LIBC_NETDB
  244. #if 0 /* None of these are yet supported */
  245. void endhostent(void);
  246. void endnetent(void);
  247. void endprotoent(void);
  248. void endservent(void);
  249. #endif
  250. void freeaddrinfo(FAR struct addrinfo *ai);
  251. FAR const char *gai_strerror(int);
  252. int getaddrinfo(FAR const char *nodename,
  253. FAR const char *servname,
  254. FAR const struct addrinfo *hints,
  255. FAR struct addrinfo **res);
  256. int getnameinfo(FAR const struct sockaddr *sa,
  257. socklen_t salen, FAR char *node,
  258. socklen_t nodelen, FAR char *service,
  259. socklen_t servicelen, int flags);
  260. FAR struct hostent *gethostbyaddr(FAR const void *addr, socklen_t len,
  261. int type);
  262. FAR struct hostent *gethostbyname(FAR const char *name);
  263. FAR struct hostent *gethostbyname2(FAR const char *name, int type);
  264. FAR struct servent *getservbyport(int port, FAR const char *proto);
  265. FAR struct servent *getservbyname(FAR const char *name,
  266. FAR const char *proto);
  267. #if 0 /* None of these are yet supported */
  268. FAR struct hostent *gethostent(void);
  269. FAR struct netent *getnetbyaddr(uint32_t net, int type);
  270. FAR struct netent *getnetbyname(FAR const char *name);
  271. FAR struct netent *getnetent(void);
  272. FAR struct protoent *getprotobyname(FAR const char *name);
  273. FAR struct protoent *getprotobynumber(int proto);
  274. FAR struct protoent *getprotoent(void);
  275. FAR struct servent *getservent(void);
  276. void sethostent(int);
  277. void setnetent(int stayopen);
  278. void setprotoent(int stayopen);
  279. void setservent(int);
  280. #endif /* None of these are yet supported */
  281. /* Non-standard interfaces similar to Glibc 2 interfaces */
  282. int gethostbyaddr_r(FAR const void *addr, socklen_t len, int type,
  283. FAR struct hostent *host, FAR char *buf,
  284. size_t buflen, FAR struct hostent **result,
  285. FAR int *h_errnop);
  286. int gethostbyname_r(FAR const char *name,
  287. FAR struct hostent *host, FAR char *buf,
  288. size_t buflen, FAR struct hostent **result,
  289. FAR int *h_errnop);
  290. int gethostbyname2_r(FAR const char *name, int type,
  291. FAR struct hostent *host, FAR char *buf,
  292. size_t buflen, FAR struct hostent **result,
  293. FAR int *h_errnop);
  294. int getservbyport_r(int port, FAR const char *proto,
  295. FAR struct servent *result_buf, FAR char *buf,
  296. size_t buflen, FAR struct servent **result);
  297. int getservbyname_r(FAR const char *name, FAR const char *proto,
  298. FAR struct servent *result_buf, FAR char *buf,
  299. size_t buflen, FAR struct servent **result);
  300. #endif /* CONFIG_LIBC_NETDB */
  301. #undef EXTERN
  302. #ifdef __cplusplus
  303. }
  304. #endif
  305. #endif /* __INCLUDE_NETDB_H */