netdb.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414
  1. /**
  2. * @file
  3. * API functions for name resolving
  4. *
  5. * @defgroup netdbapi NETDB API
  6. * @ingroup socket
  7. */
  8. /*
  9. * Redistribution and use in source and binary forms, with or without modification,
  10. * are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * 3. The name of the author may not be used to endorse or promote products
  18. * derived from this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  21. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  22. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
  23. * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  24. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
  25. * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  26. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  27. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
  28. * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
  29. * OF SUCH DAMAGE.
  30. *
  31. * This file is part of the lwIP TCP/IP stack.
  32. *
  33. * Author: Simon Goldschmidt
  34. *
  35. */
  36. #include "lwip/netdb.h"
  37. #if LWIP_DNS && LWIP_SOCKET
  38. #include "lwip/err.h"
  39. #include "lwip/mem.h"
  40. #include "lwip/memp.h"
  41. #include "lwip/ip_addr.h"
  42. #include "lwip/api.h"
  43. #include "lwip/dns.h"
  44. #include <string.h> /* memset */
  45. #include <stdlib.h> /* atoi */
  46. /** helper struct for gethostbyname_r to access the char* buffer */
  47. struct gethostbyname_r_helper {
  48. ip_addr_t *addr_list[2];
  49. ip_addr_t addr;
  50. char *aliases;
  51. };
  52. /** h_errno is exported in netdb.h for access by applications. */
  53. #if LWIP_DNS_API_DECLARE_H_ERRNO
  54. int h_errno;
  55. #endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
  56. /** define "hostent" variables storage: 0 if we use a static (but unprotected)
  57. * set of variables for lwip_gethostbyname, 1 if we use a local storage */
  58. #ifndef LWIP_DNS_API_HOSTENT_STORAGE
  59. #define LWIP_DNS_API_HOSTENT_STORAGE 0
  60. #endif
  61. /** define "hostent" variables storage */
  62. #if LWIP_DNS_API_HOSTENT_STORAGE
  63. #define HOSTENT_STORAGE
  64. #else
  65. #define HOSTENT_STORAGE static
  66. #endif /* LWIP_DNS_API_STATIC_HOSTENT */
  67. /**
  68. * Returns an entry containing addresses of address family AF_INET
  69. * for the host with name name.
  70. * Due to dns_gethostbyname limitations, only one address is returned.
  71. *
  72. * @param name the hostname to resolve
  73. * @return an entry containing addresses of address family AF_INET
  74. * for the host with name name
  75. */
  76. struct hostent *
  77. lwip_gethostbyname(const char *name)
  78. {
  79. err_t err;
  80. ip_addr_t addr;
  81. /* buffer variables for lwip_gethostbyname() */
  82. HOSTENT_STORAGE struct hostent s_hostent;
  83. HOSTENT_STORAGE char *s_aliases;
  84. HOSTENT_STORAGE ip_addr_t s_hostent_addr;
  85. HOSTENT_STORAGE ip_addr_t *s_phostent_addr[2];
  86. HOSTENT_STORAGE char s_hostname[DNS_MAX_NAME_LENGTH + 1];
  87. /* query host IP address */
  88. err = netconn_gethostbyname(name, &addr);
  89. if (err != ERR_OK) {
  90. LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
  91. h_errno = HOST_NOT_FOUND;
  92. return NULL;
  93. }
  94. /* fill hostent */
  95. s_hostent_addr = addr;
  96. s_phostent_addr[0] = &s_hostent_addr;
  97. s_phostent_addr[1] = NULL;
  98. strncpy(s_hostname, name, DNS_MAX_NAME_LENGTH);
  99. s_hostname[DNS_MAX_NAME_LENGTH] = 0;
  100. s_hostent.h_name = s_hostname;
  101. s_aliases = NULL;
  102. s_hostent.h_aliases = &s_aliases;
  103. s_hostent.h_addrtype = AF_INET;
  104. s_hostent.h_length = sizeof(ip_addr_t);
  105. s_hostent.h_addr_list = (char **)&s_phostent_addr;
  106. #if DNS_DEBUG
  107. /* dump hostent */
  108. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name == %s\n", s_hostent.h_name));
  109. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases == %p\n", (void *)s_hostent.h_aliases));
  110. /* h_aliases are always empty */
  111. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype == %d\n", s_hostent.h_addrtype));
  112. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length == %d\n", s_hostent.h_length));
  113. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list == %p\n", (void *)s_hostent.h_addr_list));
  114. if (s_hostent.h_addr_list != NULL) {
  115. u8_t idx;
  116. for (idx = 0; s_hostent.h_addr_list[idx]; idx++) {
  117. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i] == %p\n", idx, s_hostent.h_addr_list[idx]));
  118. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ipaddr_ntoa((ip_addr_t *)s_hostent.h_addr_list[idx])));
  119. }
  120. }
  121. #endif /* DNS_DEBUG */
  122. #if LWIP_DNS_API_HOSTENT_STORAGE
  123. /* this function should return the "per-thread" hostent after copy from s_hostent */
  124. return sys_thread_hostent(&s_hostent);
  125. #else
  126. return &s_hostent;
  127. #endif /* LWIP_DNS_API_HOSTENT_STORAGE */
  128. }
  129. /**
  130. * Thread-safe variant of lwip_gethostbyname: instead of using a static
  131. * buffer, this function takes buffer and errno pointers as arguments
  132. * and uses these for the result.
  133. *
  134. * @param name the hostname to resolve
  135. * @param ret pre-allocated struct where to store the result
  136. * @param buf pre-allocated buffer where to store additional data
  137. * @param buflen the size of buf
  138. * @param result pointer to a hostent pointer that is set to ret on success
  139. * and set to zero on error
  140. * @param h_errnop pointer to an int where to store errors (instead of modifying
  141. * the global h_errno)
  142. * @return 0 on success, non-zero on error, additional error information
  143. * is stored in *h_errnop instead of h_errno to be thread-safe
  144. */
  145. int
  146. lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  147. size_t buflen, struct hostent **result, int *h_errnop)
  148. {
  149. err_t err;
  150. struct gethostbyname_r_helper *h;
  151. char *hostname;
  152. size_t namelen;
  153. int lh_errno;
  154. if (h_errnop == NULL) {
  155. /* ensure h_errnop is never NULL */
  156. h_errnop = &lh_errno;
  157. }
  158. if (result == NULL) {
  159. /* not all arguments given */
  160. *h_errnop = EINVAL;
  161. return -1;
  162. }
  163. /* first thing to do: set *result to nothing */
  164. *result = NULL;
  165. if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
  166. /* not all arguments given */
  167. *h_errnop = EINVAL;
  168. return -1;
  169. }
  170. namelen = strlen(name);
  171. if (buflen < (sizeof(struct gethostbyname_r_helper) + LWIP_MEM_ALIGN_BUFFER(namelen + 1))) {
  172. /* buf can't hold the data needed + a copy of name */
  173. *h_errnop = ERANGE;
  174. return -1;
  175. }
  176. h = (struct gethostbyname_r_helper *)LWIP_MEM_ALIGN(buf);
  177. hostname = ((char *)h) + sizeof(struct gethostbyname_r_helper);
  178. /* query host IP address */
  179. err = netconn_gethostbyname(name, &h->addr);
  180. if (err != ERR_OK) {
  181. LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
  182. *h_errnop = HOST_NOT_FOUND;
  183. return -1;
  184. }
  185. /* copy the hostname into buf */
  186. MEMCPY(hostname, name, namelen);
  187. hostname[namelen] = 0;
  188. /* fill hostent */
  189. h->addr_list[0] = &h->addr;
  190. h->addr_list[1] = NULL;
  191. h->aliases = NULL;
  192. ret->h_name = hostname;
  193. ret->h_aliases = &h->aliases;
  194. ret->h_addrtype = AF_INET;
  195. ret->h_length = sizeof(ip_addr_t);
  196. ret->h_addr_list = (char **)&h->addr_list;
  197. /* set result != NULL */
  198. *result = ret;
  199. /* return success */
  200. return 0;
  201. }
  202. /**
  203. * Frees one or more addrinfo structures returned by getaddrinfo(), along with
  204. * any additional storage associated with those structures. If the ai_next field
  205. * of the structure is not null, the entire list of structures is freed.
  206. *
  207. * @param ai struct addrinfo to free
  208. */
  209. void
  210. lwip_freeaddrinfo(struct addrinfo *ai)
  211. {
  212. struct addrinfo *next;
  213. while (ai != NULL) {
  214. next = ai->ai_next;
  215. memp_free(MEMP_NETDB, ai);
  216. ai = next;
  217. }
  218. }
  219. /**
  220. * Translates the name of a service location (for example, a host name) and/or
  221. * a service name and returns a set of socket addresses and associated
  222. * information to be used in creating a socket with which to address the
  223. * specified service.
  224. * Memory for the result is allocated internally and must be freed by calling
  225. * lwip_freeaddrinfo()!
  226. *
  227. * Due to a limitation in dns_gethostbyname, only the first address of a
  228. * host is returned.
  229. * Also, service names are not supported (only port numbers)!
  230. *
  231. * @param nodename descriptive name or address string of the host
  232. * (may be NULL -> local address)
  233. * @param servname port number as string of NULL
  234. * @param hints structure containing input values that set socktype and protocol
  235. * @param res pointer to a pointer where to store the result (set to NULL on failure)
  236. * @return 0 on success, non-zero on failure
  237. *
  238. * @todo: implement AI_V4MAPPED, AI_ADDRCONFIG
  239. */
  240. int
  241. lwip_getaddrinfo(const char *nodename, const char *servname,
  242. const struct addrinfo *hints, struct addrinfo **res)
  243. {
  244. err_t err;
  245. ip_addr_t addr;
  246. struct addrinfo *ai;
  247. struct sockaddr_storage *sa = NULL;
  248. int port_nr = 0;
  249. size_t total_size;
  250. size_t namelen = 0;
  251. int ai_family;
  252. if (res == NULL) {
  253. return EAI_FAIL;
  254. }
  255. *res = NULL;
  256. if ((nodename == NULL) && (servname == NULL)) {
  257. return EAI_NONAME;
  258. }
  259. if (hints != NULL) {
  260. ai_family = hints->ai_family;
  261. if ((ai_family != AF_UNSPEC)
  262. #if LWIP_IPV4
  263. && (ai_family != AF_INET)
  264. #endif /* LWIP_IPV4 */
  265. #if LWIP_IPV6
  266. && (ai_family != AF_INET6)
  267. #endif /* LWIP_IPV6 */
  268. ) {
  269. return EAI_FAMILY;
  270. }
  271. } else {
  272. ai_family = AF_UNSPEC;
  273. }
  274. if (servname != NULL) {
  275. /* service name specified: convert to port number
  276. * @todo?: currently, only ASCII integers (port numbers) are supported (AI_NUMERICSERV)! */
  277. port_nr = atoi(servname);
  278. if ((port_nr <= 0) || (port_nr > 0xffff)) {
  279. return EAI_SERVICE;
  280. }
  281. }
  282. if (nodename != NULL) {
  283. /* service location specified, try to resolve */
  284. if ((hints != NULL) && (hints->ai_flags & AI_NUMERICHOST)) {
  285. /* no DNS lookup, just parse for an address string */
  286. if (!ipaddr_aton(nodename, &addr)) {
  287. return EAI_NONAME;
  288. }
  289. #if LWIP_IPV4 && LWIP_IPV6
  290. if ((IP_IS_V6_VAL(addr) && ai_family == AF_INET) ||
  291. (IP_IS_V4_VAL(addr) && ai_family == AF_INET6)) {
  292. return EAI_NONAME;
  293. }
  294. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  295. } else {
  296. #if LWIP_IPV4 && LWIP_IPV6
  297. /* AF_UNSPEC: prefer IPv4 */
  298. u8_t type = NETCONN_DNS_IPV4_IPV6;
  299. if (ai_family == AF_INET) {
  300. type = NETCONN_DNS_IPV4;
  301. } else if (ai_family == AF_INET6) {
  302. type = NETCONN_DNS_IPV6;
  303. }
  304. #endif /* LWIP_IPV4 && LWIP_IPV6 */
  305. err = netconn_gethostbyname_addrtype(nodename, &addr, type);
  306. if (err != ERR_OK) {
  307. return EAI_FAIL;
  308. }
  309. }
  310. } else {
  311. /* service location specified, use loopback address */
  312. if ((hints != NULL) && (hints->ai_flags & AI_PASSIVE)) {
  313. ip_addr_set_any_val(ai_family == AF_INET6, addr);
  314. } else {
  315. ip_addr_set_loopback_val(ai_family == AF_INET6, addr);
  316. }
  317. }
  318. total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_storage);
  319. if (nodename != NULL) {
  320. namelen = strlen(nodename);
  321. if (namelen > DNS_MAX_NAME_LENGTH) {
  322. /* invalid name length */
  323. return EAI_FAIL;
  324. }
  325. LWIP_ASSERT("namelen is too long", total_size + namelen + 1 > total_size);
  326. total_size += namelen + 1;
  327. }
  328. /* If this fails, please report to lwip-devel! :-) */
  329. LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
  330. total_size <= NETDB_ELEM_SIZE);
  331. ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
  332. if (ai == NULL) {
  333. return EAI_MEMORY;
  334. }
  335. memset(ai, 0, total_size);
  336. /* cast through void* to get rid of alignment warnings */
  337. sa = (struct sockaddr_storage *)(void *)((u8_t *)ai + sizeof(struct addrinfo));
  338. if (IP_IS_V6_VAL(addr)) {
  339. #if LWIP_IPV6
  340. struct sockaddr_in6 *sa6 = (struct sockaddr_in6 *)sa;
  341. /* set up sockaddr */
  342. inet6_addr_from_ip6addr(&sa6->sin6_addr, ip_2_ip6(&addr));
  343. sa6->sin6_family = AF_INET6;
  344. sa6->sin6_len = sizeof(struct sockaddr_in6);
  345. sa6->sin6_port = lwip_htons((u16_t)port_nr);
  346. sa6->sin6_scope_id = ip6_addr_zone(ip_2_ip6(&addr));
  347. ai->ai_family = AF_INET6;
  348. #endif /* LWIP_IPV6 */
  349. } else {
  350. #if LWIP_IPV4
  351. struct sockaddr_in *sa4 = (struct sockaddr_in *)sa;
  352. /* set up sockaddr */
  353. inet_addr_from_ip4addr(&sa4->sin_addr, ip_2_ip4(&addr));
  354. sa4->sin_family = AF_INET;
  355. sa4->sin_len = sizeof(struct sockaddr_in);
  356. sa4->sin_port = lwip_htons((u16_t)port_nr);
  357. ai->ai_family = AF_INET;
  358. #endif /* LWIP_IPV4 */
  359. }
  360. /* set up addrinfo */
  361. if (hints != NULL) {
  362. /* copy socktype & protocol from hints if specified */
  363. ai->ai_socktype = hints->ai_socktype;
  364. ai->ai_protocol = hints->ai_protocol;
  365. }
  366. if (nodename != NULL) {
  367. /* copy nodename to canonname if specified */
  368. ai->ai_canonname = ((char *)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_storage));
  369. MEMCPY(ai->ai_canonname, nodename, namelen);
  370. ai->ai_canonname[namelen] = 0;
  371. }
  372. ai->ai_addrlen = sizeof(struct sockaddr_storage);
  373. ai->ai_addr = (struct sockaddr *)sa;
  374. *res = ai;
  375. return 0;
  376. }
  377. #endif /* LWIP_DNS && LWIP_SOCKET */