netdb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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/ip_addr.h"
  39. #include "lwip/api.h"
  40. #include <string.h>
  41. #include <stdlib.h>
  42. /** helper struct for gethostbyname_r to access the char* buffer */
  43. struct gethostbyname_r_helper {
  44. struct ip_addr *addrs;
  45. struct ip_addr addr;
  46. char *aliases;
  47. };
  48. /** h_errno is exported in netdb.h for access by applications. */
  49. #if LWIP_DNS_API_DECLARE_H_ERRNO
  50. int h_errno;
  51. #endif /* LWIP_DNS_API_DECLARE_H_ERRNO */
  52. /** define "hostent" variables storage: 0 if we use a static (but unprotected)
  53. * set of variables for lwip_gethostbyname, 1 if we use a local storage */
  54. #ifndef LWIP_DNS_API_HOSTENT_STORAGE
  55. #define LWIP_DNS_API_HOSTENT_STORAGE 0
  56. #endif
  57. /** define "hostent" variables storage */
  58. #if LWIP_DNS_API_HOSTENT_STORAGE
  59. #define HOSTENT_STORAGE
  60. #else
  61. #define HOSTENT_STORAGE static
  62. #endif /* LWIP_DNS_API_STATIC_HOSTENT */
  63. /**
  64. * Returns an entry containing addresses of address family AF_INET
  65. * for the host with name name.
  66. * Due to dns_gethostbyname limitations, only one address is returned.
  67. *
  68. * @param name the hostname to resolve
  69. * @return an entry containing addresses of address family AF_INET
  70. * for the host with name name
  71. */
  72. struct hostent*
  73. lwip_gethostbyname(const char *name)
  74. {
  75. err_t err;
  76. struct ip_addr addr;
  77. /* buffer variables for lwip_gethostbyname() */
  78. HOSTENT_STORAGE struct hostent s_hostent;
  79. HOSTENT_STORAGE char *s_aliases;
  80. HOSTENT_STORAGE struct ip_addr s_hostent_addr;
  81. HOSTENT_STORAGE struct ip_addr *s_phostent_addr;
  82. /* query host IP address */
  83. err = netconn_gethostbyname(name, &addr);
  84. if (err != ERR_OK) {
  85. LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
  86. h_errno = HOST_NOT_FOUND;
  87. return NULL;
  88. }
  89. /* fill hostent */
  90. s_hostent_addr = addr;
  91. s_phostent_addr = &s_hostent_addr;
  92. s_hostent.h_name = (char*)name;
  93. s_hostent.h_aliases = &s_aliases;
  94. s_hostent.h_addrtype = AF_INET;
  95. s_hostent.h_length = sizeof(struct ip_addr);
  96. s_hostent.h_addr_list = (char**)&s_phostent_addr;
  97. #if DNS_DEBUG
  98. /* dump hostent */
  99. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name == %s\n", s_hostent.h_name));
  100. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases == %p\n", s_hostent.h_aliases));
  101. if (s_hostent.h_aliases != NULL) {
  102. u8_t idx;
  103. for ( idx=0; s_hostent.h_aliases[idx]; idx++) {
  104. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases[%i]-> == %p\n", idx, s_hostent.h_aliases[idx]));
  105. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases[%i]-> == %s\n", idx, s_hostent.h_aliases[idx]));
  106. }
  107. }
  108. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype == %d\n", s_hostent.h_addrtype));
  109. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length == %d\n", s_hostent.h_length));
  110. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list == %p\n", s_hostent.h_addr_list));
  111. if (s_hostent.h_addr_list != NULL) {
  112. u8_t idx;
  113. for ( idx=0; s_hostent.h_addr_list[idx]; idx++) {
  114. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i] == %p\n", idx, s_hostent.h_addr_list[idx]));
  115. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, inet_ntoa(*((struct in_addr*)(s_hostent.h_addr_list[idx])))));
  116. }
  117. }
  118. #endif /* DNS_DEBUG */
  119. #if LWIP_DNS_API_HOSTENT_STORAGE
  120. /* this function should return the "per-thread" hostent after copy from s_hostent */
  121. return sys_thread_hostent(&s_hostent);
  122. #else
  123. return &s_hostent;
  124. #endif /* LWIP_DNS_API_HOSTENT_STORAGE */
  125. }
  126. /**
  127. * Thread-safe variant of lwip_gethostbyname: instead of using a static
  128. * buffer, this function takes buffer and errno pointers as arguments
  129. * and uses these for the result.
  130. *
  131. * @param name the hostname to resolve
  132. * @param ret pre-allocated struct where to store the result
  133. * @param buf pre-allocated buffer where to store additional data
  134. * @param buflen the size of buf
  135. * @param result pointer to a hostent pointer that is set to ret on success
  136. * and set to zero on error
  137. * @param h_errnop pointer to an int where to store errors (instead of modifying
  138. * the global h_errno)
  139. * @return 0 on success, non-zero on error, additional error information
  140. * is stored in *h_errnop instead of h_errno to be thread-safe
  141. */
  142. int
  143. lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  144. size_t buflen, struct hostent **result, int *h_errnop)
  145. {
  146. err_t err;
  147. struct gethostbyname_r_helper *h;
  148. char *hostname;
  149. size_t namelen;
  150. int lh_errno;
  151. if (h_errnop == NULL) {
  152. /* ensure h_errnop is never NULL */
  153. h_errnop = &lh_errno;
  154. }
  155. if (result == NULL) {
  156. /* not all arguments given */
  157. *h_errnop = EINVAL;
  158. return -1;
  159. }
  160. /* first thing to do: set *result to nothing */
  161. *result = NULL;
  162. if ((name == NULL) || (ret == NULL) || (buf == 0)) {
  163. /* not all arguments given */
  164. *h_errnop = EINVAL;
  165. return -1;
  166. }
  167. namelen = strlen(name);
  168. if (buflen < (sizeof(struct gethostbyname_r_helper) + namelen + 1 + (MEM_ALIGNMENT - 1))) {
  169. /* buf can't hold the data needed + a copy of name */
  170. *h_errnop = ERANGE;
  171. return -1;
  172. }
  173. h = (struct gethostbyname_r_helper*)LWIP_MEM_ALIGN(buf);
  174. hostname = ((char*)h) + sizeof(struct gethostbyname_r_helper);
  175. /* query host IP address */
  176. err = netconn_gethostbyname(name, &(h->addr));
  177. if (err != ERR_OK) {
  178. LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
  179. *h_errnop = ENSRNOTFOUND;
  180. return -1;
  181. }
  182. /* copy the hostname into buf */
  183. MEMCPY(hostname, name, namelen);
  184. hostname[namelen] = 0;
  185. /* fill hostent */
  186. h->addrs = &(h->addr);
  187. h->aliases = NULL;
  188. ret->h_name = (char*)hostname;
  189. ret->h_aliases = &(h->aliases);
  190. ret->h_addrtype = AF_INET;
  191. ret->h_length = sizeof(struct ip_addr);
  192. ret->h_addr_list = (char**)&(h->addrs);
  193. /* set result != NULL */
  194. *result = ret;
  195. /* return success */
  196. return 0;
  197. }
  198. /**
  199. * Frees one or more addrinfo structures returned by getaddrinfo(), along with
  200. * any additional storage associated with those structures. If the ai_next field
  201. * of the structure is not null, the entire list of structures is freed.
  202. *
  203. * @param ai struct addrinfo to free
  204. */
  205. void
  206. lwip_freeaddrinfo(struct addrinfo *ai)
  207. {
  208. struct addrinfo *next;
  209. while (ai != NULL) {
  210. if (ai->ai_addr != NULL) {
  211. mem_free(ai->ai_addr);
  212. }
  213. if (ai->ai_canonname != NULL) {
  214. mem_free(ai->ai_canonname);
  215. }
  216. next = ai->ai_next;
  217. mem_free(ai);
  218. ai = next;
  219. }
  220. }
  221. /**
  222. * Translates the name of a service location (for example, a host name) and/or
  223. * a service name and returns a set of socket addresses and associated
  224. * information to be used in creating a socket with which to address the
  225. * specified service.
  226. * Memory for the result is allocated internally and must be freed by calling
  227. * lwip_freeaddrinfo()!
  228. *
  229. * Due to a limitation in dns_gethostbyname, only the first address of a
  230. * host is returned.
  231. * Also, service names are not supported (only port numbers)!
  232. *
  233. * @param nodename descriptive name or address string of the host
  234. * (may be NULL -> local address)
  235. * @param servname port number as string of NULL
  236. * @param hints structure containing input values that set socktype and protocol
  237. * @param res pointer to a pointer where to store the result (set to NULL on failure)
  238. * @return 0 on success, non-zero on failure
  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. struct ip_addr addr;
  246. struct addrinfo *ai;
  247. struct sockaddr_in *sa = NULL;
  248. int port_nr = 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. addr.addr = INADDR_LOOPBACK;
  273. }
  274. ai = mem_malloc(sizeof(struct addrinfo));
  275. if (ai == NULL) {
  276. goto memerr;
  277. }
  278. memset(ai, 0, sizeof(struct addrinfo));
  279. sa = mem_malloc(sizeof(struct sockaddr_in));
  280. if (sa == NULL) {
  281. goto memerr;
  282. }
  283. memset(sa, 0, sizeof(struct sockaddr_in));
  284. /* set up sockaddr */
  285. sa->sin_addr.s_addr = addr.addr;
  286. sa->sin_family = AF_INET;
  287. sa->sin_len = sizeof(struct sockaddr_in);
  288. sa->sin_port = htons(port_nr);
  289. /* set up addrinfo */
  290. ai->ai_family = AF_INET;
  291. if (hints != NULL) {
  292. /* copy socktype & protocol from hints if specified */
  293. ai->ai_socktype = hints->ai_socktype;
  294. ai->ai_protocol = hints->ai_protocol;
  295. }
  296. if (nodename != NULL) {
  297. /* copy nodename to canonname if specified */
  298. size_t namelen = strlen(nodename);
  299. LWIP_ASSERT("namelen is too long", (namelen + 1) <= (mem_size_t)-1);
  300. ai->ai_canonname = mem_malloc((mem_size_t)(namelen + 1));
  301. if (ai->ai_canonname == NULL) {
  302. goto memerr;
  303. }
  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. mem_free(ai);
  314. }
  315. if (sa != NULL) {
  316. mem_free(sa);
  317. }
  318. return EAI_MEMORY;
  319. }
  320. #endif /* LWIP_DNS && LWIP_SOCKET */