netdb.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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_aliases = NULL;
  97. s_hostent.h_aliases = &s_aliases;
  98. s_hostent.h_addrtype = AF_INET;
  99. s_hostent.h_length = sizeof(ip_addr_t);
  100. s_hostent.h_addr_list = (char**)&s_phostent_addr;
  101. #if DNS_DEBUG
  102. /* dump hostent */
  103. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_name == %s\n", s_hostent.h_name));
  104. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_aliases == %p\n", s_hostent.h_aliases));
  105. /* h_aliases are always empty */
  106. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addrtype == %d\n", s_hostent.h_addrtype));
  107. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_length == %d\n", s_hostent.h_length));
  108. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list == %p\n", s_hostent.h_addr_list));
  109. if (s_hostent.h_addr_list != NULL) {
  110. u8_t idx;
  111. for ( idx=0; s_hostent.h_addr_list[idx]; idx++) {
  112. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i] == %p\n", idx, s_hostent.h_addr_list[idx]));
  113. LWIP_DEBUGF(DNS_DEBUG, ("hostent.h_addr_list[%i]-> == %s\n", idx, ip_ntoa((ip_addr_t*)s_hostent.h_addr_list[idx])));
  114. }
  115. }
  116. #endif /* DNS_DEBUG */
  117. #if LWIP_DNS_API_HOSTENT_STORAGE
  118. /* this function should return the "per-thread" hostent after copy from s_hostent */
  119. return sys_thread_hostent(&s_hostent);
  120. #else
  121. return &s_hostent;
  122. #endif /* LWIP_DNS_API_HOSTENT_STORAGE */
  123. }
  124. /**
  125. * Thread-safe variant of lwip_gethostbyname: instead of using a static
  126. * buffer, this function takes buffer and errno pointers as arguments
  127. * and uses these for the result.
  128. *
  129. * @param name the hostname to resolve
  130. * @param ret pre-allocated struct where to store the result
  131. * @param buf pre-allocated buffer where to store additional data
  132. * @param buflen the size of buf
  133. * @param result pointer to a hostent pointer that is set to ret on success
  134. * and set to zero on error
  135. * @param h_errnop pointer to an int where to store errors (instead of modifying
  136. * the global h_errno)
  137. * @return 0 on success, non-zero on error, additional error information
  138. * is stored in *h_errnop instead of h_errno to be thread-safe
  139. */
  140. int
  141. lwip_gethostbyname_r(const char *name, struct hostent *ret, char *buf,
  142. size_t buflen, struct hostent **result, int *h_errnop)
  143. {
  144. err_t err;
  145. struct gethostbyname_r_helper *h;
  146. char *hostname;
  147. size_t namelen;
  148. int lh_errno;
  149. if (h_errnop == NULL) {
  150. /* ensure h_errnop is never NULL */
  151. h_errnop = &lh_errno;
  152. }
  153. if (result == NULL) {
  154. /* not all arguments given */
  155. *h_errnop = EINVAL;
  156. return -1;
  157. }
  158. /* first thing to do: set *result to nothing */
  159. *result = NULL;
  160. if ((name == NULL) || (ret == NULL) || (buf == NULL)) {
  161. /* not all arguments given */
  162. *h_errnop = EINVAL;
  163. return -1;
  164. }
  165. namelen = strlen(name);
  166. if (buflen < (sizeof(struct gethostbyname_r_helper) + namelen + 1 + (MEM_ALIGNMENT - 1))) {
  167. /* buf can't hold the data needed + a copy of name */
  168. *h_errnop = ERANGE;
  169. return -1;
  170. }
  171. h = (struct gethostbyname_r_helper*)LWIP_MEM_ALIGN(buf);
  172. hostname = ((char*)h) + sizeof(struct gethostbyname_r_helper);
  173. /* query host IP address */
  174. err = netconn_gethostbyname(name, &h->addr);
  175. if (err != ERR_OK) {
  176. LWIP_DEBUGF(DNS_DEBUG, ("lwip_gethostbyname(%s) failed, err=%d\n", name, err));
  177. *h_errnop = HOST_NOT_FOUND;
  178. return -1;
  179. }
  180. /* copy the hostname into buf */
  181. MEMCPY(hostname, name, namelen);
  182. hostname[namelen] = 0;
  183. /* fill hostent */
  184. h->addr_list[0] = &h->addr;
  185. h->addr_list[1] = NULL;
  186. h->aliases = NULL;
  187. ret->h_name = hostname;
  188. ret->h_aliases = &h->aliases;
  189. ret->h_addrtype = AF_INET;
  190. ret->h_length = sizeof(ip_addr_t);
  191. ret->h_addr_list = (char**)&h->addr_list;
  192. /* set result != NULL */
  193. *result = ret;
  194. /* return success */
  195. return 0;
  196. }
  197. /**
  198. * Frees one or more addrinfo structures returned by getaddrinfo(), along with
  199. * any additional storage associated with those structures. If the ai_next field
  200. * of the structure is not null, the entire list of structures is freed.
  201. *
  202. * @param ai struct addrinfo to free
  203. */
  204. void
  205. lwip_freeaddrinfo(struct addrinfo *ai)
  206. {
  207. struct addrinfo *next;
  208. while (ai != NULL) {
  209. next = ai->ai_next;
  210. memp_free(MEMP_NETDB, ai);
  211. ai = next;
  212. }
  213. }
  214. /**
  215. * Translates the name of a service location (for example, a host name) and/or
  216. * a service name and returns a set of socket addresses and associated
  217. * information to be used in creating a socket with which to address the
  218. * specified service.
  219. * Memory for the result is allocated internally and must be freed by calling
  220. * lwip_freeaddrinfo()!
  221. *
  222. * Due to a limitation in dns_gethostbyname, only the first address of a
  223. * host is returned.
  224. * Also, service names are not supported (only port numbers)!
  225. *
  226. * @param nodename descriptive name or address string of the host
  227. * (may be NULL -> local address)
  228. * @param servname port number as string of NULL
  229. * @param hints structure containing input values that set socktype and protocol
  230. * @param res pointer to a pointer where to store the result (set to NULL on failure)
  231. * @return 0 on success, non-zero on failure
  232. */
  233. int
  234. lwip_getaddrinfo(const char *nodename, const char *servname,
  235. const struct addrinfo *hints, struct addrinfo **res)
  236. {
  237. err_t err;
  238. ip_addr_t addr;
  239. struct addrinfo *ai;
  240. struct sockaddr_in *sa = NULL;
  241. int port_nr = 0;
  242. size_t total_size;
  243. size_t namelen = 0;
  244. if (res == NULL) {
  245. return EAI_FAIL;
  246. }
  247. *res = NULL;
  248. if ((nodename == NULL) && (servname == NULL)) {
  249. return EAI_NONAME;
  250. }
  251. if (servname != NULL) {
  252. /* service name specified: convert to port number
  253. * @todo?: currently, only ASCII integers (port numbers) are supported! */
  254. port_nr = atoi(servname);
  255. if ((port_nr <= 0) || (port_nr > 0xffff)) {
  256. return EAI_SERVICE;
  257. }
  258. }
  259. if (nodename != NULL) {
  260. /* service location specified, try to resolve */
  261. err = netconn_gethostbyname(nodename, &addr);
  262. if (err != ERR_OK) {
  263. return EAI_FAIL;
  264. }
  265. } else {
  266. /* service location specified, use loopback address */
  267. ip_addr_set_loopback(&addr);
  268. }
  269. total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_in);
  270. if (nodename != NULL) {
  271. namelen = strlen(nodename);
  272. LWIP_ASSERT("namelen is too long", (namelen + 1) <= (mem_size_t)-1);
  273. total_size += namelen + 1;
  274. }
  275. /* If this fails, please report to lwip-devel! :-) */
  276. LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
  277. total_size <= NETDB_ELEM_SIZE);
  278. ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
  279. if (ai == NULL) {
  280. return EAI_MEMORY;
  281. }
  282. memset(ai, 0, total_size);
  283. sa = (struct sockaddr_in*)((u8_t*)ai + sizeof(struct addrinfo));
  284. /* set up sockaddr */
  285. inet_addr_from_ipaddr(&sa->sin_addr, &addr);
  286. sa->sin_family = AF_INET;
  287. sa->sin_len = sizeof(struct sockaddr_in);
  288. sa->sin_port = htons((u16_t)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. ai->ai_canonname = ((char*)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
  299. MEMCPY(ai->ai_canonname, nodename, namelen);
  300. ai->ai_canonname[namelen] = 0;
  301. }
  302. ai->ai_addrlen = sizeof(struct sockaddr_in);
  303. ai->ai_addr = (struct sockaddr*)sa;
  304. *res = ai;
  305. return 0;
  306. }
  307. #endif /* LWIP_DNS && LWIP_SOCKET */