netdb.c 13 KB

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