netdb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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. /* query host IP address */
  85. err = netconn_gethostbyname(name, &addr);
  86. if (err != ERR_OK) {
  87. LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
  88. h_errno = HOST_NOT_FOUND;
  89. return NULL;
  90. }
  91. /* fill hostent */
  92. s_hostent_addr = addr;
  93. s_phostent_addr[0] = &s_hostent_addr;
  94. s_phostent_addr[1] = NULL;
  95. s_hostent.h_name = (char*)name;
  96. s_hostent.h_aliases = &s_aliases;
  97. s_hostent.h_addrtype = AF_INET;
  98. s_hostent.h_length = sizeof(ip_addr_t);
  99. s_hostent.h_addr_list = (char**)&s_phostent_addr;
  100. #if DNS_DEBUG
  101. /* dump hostent */
  102. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name == %s\n", s_hostent.h_name));
  103. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases == %p\n", s_hostent.h_aliases));
  104. if (s_hostent.h_aliases != NULL) {
  105. u8_t idx;
  106. for ( idx=0; s_hostent.h_aliases[idx]; idx++) {
  107. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases[%i]-> == %p\n", idx, s_hostent.h_aliases[idx]));
  108. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases[%i]-> == %s\n", idx, s_hostent.h_aliases[idx]));
  109. }
  110. }
  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", 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, ip_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) + namelen + 1 + (MEM_ALIGNMENT - 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. 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_in *sa = NULL;
  246. int port_nr = 0;
  247. size_t total_size;
  248. size_t namelen = 0;
  249. if (res == NULL) {
  250. return EAI_FAIL;
  251. }
  252. *res = NULL;
  253. if ((nodename == NULL) && (servname == NULL)) {
  254. return EAI_NONAME;
  255. }
  256. if (servname != NULL) {
  257. /* service name specified: convert to port number
  258. * @todo?: currently, only ASCII integers (port numbers) are supported! */
  259. port_nr = atoi(servname);
  260. if ((port_nr <= 0) || (port_nr > 0xffff)) {
  261. return EAI_SERVICE;
  262. }
  263. }
  264. if (nodename != NULL) {
  265. /* service location specified, try to resolve */
  266. err = netconn_gethostbyname(nodename, &addr);
  267. if (err != ERR_OK) {
  268. return EAI_FAIL;
  269. }
  270. } else {
  271. /* service location specified, use loopback address */
  272. ip_addr_set_loopback(&addr);
  273. }
  274. total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_in);
  275. if (nodename != NULL) {
  276. namelen = strlen(nodename);
  277. LWIP_ASSERT("namelen is too long", (namelen + 1) <= (mem_size_t)-1);
  278. total_size += namelen + 1;
  279. }
  280. /* If this fails, please report to lwip-devel! :-) */
  281. LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
  282. total_size <= NETDB_ELEM_SIZE);
  283. ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
  284. if (ai == NULL) {
  285. goto memerr;
  286. }
  287. memset(ai, 0, total_size);
  288. sa = (struct sockaddr_in*)((u8_t*)ai + sizeof(struct addrinfo));
  289. /* set up sockaddr */
  290. inet_addr_from_ipaddr(&sa->sin_addr, &addr);
  291. sa->sin_family = AF_INET;
  292. sa->sin_len = sizeof(struct sockaddr_in);
  293. sa->sin_port = htons((u16_t)port_nr);
  294. /* set up addrinfo */
  295. ai->ai_family = AF_INET;
  296. if (hints != NULL) {
  297. /* copy socktype & protocol from hints if specified */
  298. ai->ai_socktype = hints->ai_socktype;
  299. ai->ai_protocol = hints->ai_protocol;
  300. }
  301. if (nodename != NULL) {
  302. /* copy nodename to canonname if specified */
  303. ai->ai_canonname = ((char*)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
  304. MEMCPY(ai->ai_canonname, nodename, namelen);
  305. ai->ai_canonname[namelen] = 0;
  306. }
  307. ai->ai_addrlen = sizeof(struct sockaddr_in);
  308. ai->ai_addr = (struct sockaddr*)sa;
  309. *res = ai;
  310. return 0;
  311. memerr:
  312. if (ai != NULL) {
  313. memp_free(MEMP_NETDB, ai);
  314. }
  315. return EAI_MEMORY;
  316. }
  317. #endif /* LWIP_DNS && LWIP_SOCKET */