PageRenderTime 65ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/liblwip/api/netdb.c

https://gitlab.com/mbalamat/minix
C | 352 lines | 203 code | 36 blank | 113 comment | 49 complexity | 8402ca0b187c109c9faf4e0182987c0e MD5 | raw file
  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 *addrs;
  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 == 0)) {
  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 = ENSRNOTFOUND;
  183. return -1;
  184. }
  185. /* copy the hostname into buf */
  186. MEMCPY(hostname, name, namelen);
  187. hostname[namelen] = 0;
  188. /* fill hostent */
  189. h->addrs = &(h->addr);
  190. h->aliases = NULL;
  191. ret->h_name = (char*)hostname;
  192. ret->h_aliases = &(h->aliases);
  193. ret->h_addrtype = AF_INET;
  194. ret->h_length = sizeof(ip_addr_t);
  195. ret->h_addr_list = (char**)&(h->addrs);
  196. /* set result != NULL */
  197. *result = ret;
  198. /* return success */
  199. return 0;
  200. }
  201. /**
  202. * Frees one or more addrinfo structures returned by getaddrinfo(), along with
  203. * any additional storage associated with those structures. If the ai_next field
  204. * of the structure is not null, the entire list of structures is freed.
  205. *
  206. * @param ai struct addrinfo to free
  207. */
  208. void
  209. lwip_freeaddrinfo(struct addrinfo *ai)
  210. {
  211. struct addrinfo *next;
  212. while (ai != NULL) {
  213. next = ai->ai_next;
  214. memp_free(MEMP_NETDB, ai);
  215. ai = next;
  216. }
  217. }
  218. /**
  219. * Translates the name of a service location (for example, a host name) and/or
  220. * a service name and returns a set of socket addresses and associated
  221. * information to be used in creating a socket with which to address the
  222. * specified service.
  223. * Memory for the result is allocated internally and must be freed by calling
  224. * lwip_freeaddrinfo()!
  225. *
  226. * Due to a limitation in dns_gethostbyname, only the first address of a
  227. * host is returned.
  228. * Also, service names are not supported (only port numbers)!
  229. *
  230. * @param nodename descriptive name or address string of the host
  231. * (may be NULL -> local address)
  232. * @param servname port number as string of NULL
  233. * @param hints structure containing input values that set socktype and protocol
  234. * @param res pointer to a pointer where to store the result (set to NULL on failure)
  235. * @return 0 on success, non-zero on failure
  236. */
  237. int
  238. lwip_getaddrinfo(const char *nodename, const char *servname,
  239. const struct addrinfo *hints, struct addrinfo **res)
  240. {
  241. err_t err;
  242. ip_addr_t addr;
  243. struct addrinfo *ai;
  244. struct sockaddr_in *sa = NULL;
  245. int port_nr = 0;
  246. size_t total_size;
  247. size_t namelen = 0;
  248. if (res == NULL) {
  249. return EAI_FAIL;
  250. }
  251. *res = NULL;
  252. if ((nodename == NULL) && (servname == NULL)) {
  253. return EAI_NONAME;
  254. }
  255. if (servname != NULL) {
  256. /* service name specified: convert to port number
  257. * @todo?: currently, only ASCII integers (port numbers) are supported! */
  258. port_nr = atoi(servname);
  259. if ((port_nr <= 0) || (port_nr > 0xffff)) {
  260. return EAI_SERVICE;
  261. }
  262. }
  263. if (nodename != NULL) {
  264. /* service location specified, try to resolve */
  265. err = netconn_gethostbyname(nodename, &addr);
  266. if (err != ERR_OK) {
  267. return EAI_FAIL;
  268. }
  269. } else {
  270. /* service location specified, use loopback address */
  271. ip_addr_set_loopback(&addr);
  272. }
  273. total_size = sizeof(struct addrinfo) + sizeof(struct sockaddr_in);
  274. if (nodename != NULL) {
  275. namelen = strlen(nodename);
  276. LWIP_ASSERT("namelen is too long", (namelen + 1) <= (mem_size_t)-1);
  277. total_size += namelen + 1;
  278. }
  279. /* If this fails, please report to lwip-devel! :-) */
  280. LWIP_ASSERT("total_size <= NETDB_ELEM_SIZE: please report this!",
  281. total_size <= NETDB_ELEM_SIZE);
  282. ai = (struct addrinfo *)memp_malloc(MEMP_NETDB);
  283. if (ai == NULL) {
  284. goto memerr;
  285. }
  286. memset(ai, 0, total_size);
  287. sa = (struct sockaddr_in*)((u8_t*)ai + sizeof(struct addrinfo));
  288. /* set up sockaddr */
  289. inet_addr_from_ipaddr(&sa->sin_addr, &addr);
  290. sa->sin_family = AF_INET;
  291. sa->sin_len = sizeof(struct sockaddr_in);
  292. sa->sin_port = htons((u16_t)port_nr);
  293. /* set up addrinfo */
  294. ai->ai_family = AF_INET;
  295. if (hints != NULL) {
  296. /* copy socktype & protocol from hints if specified */
  297. ai->ai_socktype = hints->ai_socktype;
  298. ai->ai_protocol = hints->ai_protocol;
  299. }
  300. if (nodename != NULL) {
  301. /* copy nodename to canonname if specified */
  302. ai->ai_canonname = ((char*)ai + sizeof(struct addrinfo) + sizeof(struct sockaddr_in));
  303. MEMCPY(ai->ai_canonname, nodename, namelen);
  304. ai->ai_canonname[namelen] = 0;
  305. }
  306. ai->ai_addrlen = sizeof(struct sockaddr_in);
  307. ai->ai_addr = (struct sockaddr*)sa;
  308. *res = ai;
  309. return 0;
  310. memerr:
  311. if (ai != NULL) {
  312. memp_free(MEMP_NETDB, ai);
  313. }
  314. return EAI_MEMORY;
  315. }
  316. #endif /* LWIP_DNS && LWIP_SOCKET */