getaddrinfo.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /*
  2. * This file is part of libESMTP, a library for submission of RFC 2822
  3. * formatted electronic mail messages using the SMTP protocol described
  4. * in RFC 2821.
  5. *
  6. * Copyright (C) 2001,2002 Brian Stafford <brian@stafford.uklinux.net>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation; either
  11. * version 2.1 of the License, or (at your option) any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. /* An emulation of the RFC 2553 / Posix getaddrinfo resolver interface.
  23. */
  24. #ifdef HAVE_CONFIG_H
  25. #include <config.h>
  26. #endif
  27. /* Need to turn off Posix features in glibc to build this */
  28. #undef _POSIX_C_SOURCE
  29. #undef _XOPEN_SOURCE
  30. #include <stdlib.h>
  31. #include <string.h>
  32. #include <ctype.h>
  33. #include <errno.h>
  34. #include <sys/socket.h>
  35. #include <netinet/in.h>
  36. #include <arpa/inet.h>
  37. #include <netdb.h>
  38. #include "gethostbyname.h"
  39. #include "getaddrinfo.h"
  40. static struct addrinfo *
  41. dup_addrinfo (struct addrinfo *info, void *addr, size_t addrlen)
  42. {
  43. struct addrinfo *ret;
  44. ret = malloc (sizeof (struct addrinfo));
  45. if (ret == NULL)
  46. return NULL;
  47. memcpy (ret, info, sizeof (struct addrinfo));
  48. ret->ai_addr = malloc (addrlen);
  49. if (ret->ai_addr == NULL)
  50. {
  51. free (ret);
  52. return NULL;
  53. }
  54. memcpy (ret->ai_addr, addr, addrlen);
  55. ret->ai_addrlen = addrlen;
  56. return ret;
  57. }
  58. int
  59. getaddrinfo (const char *nodename, const char *servname,
  60. const struct addrinfo *hints, struct addrinfo **res)
  61. {
  62. struct hostent *hp;
  63. struct servent *servent;
  64. const char *socktype;
  65. int port;
  66. struct addrinfo hint, result;
  67. struct addrinfo *ai, *sai, *eai;
  68. struct ghbnctx ghbnctx;
  69. char **addrs;
  70. int code;
  71. memset (&result, 0, sizeof result);
  72. /* default for hints */
  73. if (hints == NULL)
  74. {
  75. memset (&hint, 0, sizeof hint);
  76. hint.ai_family = PF_UNSPEC;
  77. hints = &hint;
  78. }
  79. /* servname must not be NULL in this implementation */
  80. if (servname == NULL)
  81. return EAI_NONAME;
  82. /* check for tcp or udp sockets only */
  83. if (hints->ai_socktype == SOCK_STREAM)
  84. socktype = "tcp";
  85. else if (hints->ai_socktype == SOCK_DGRAM)
  86. socktype = "udp";
  87. else
  88. return EAI_SERVICE;
  89. result.ai_socktype = hints->ai_socktype;
  90. /* Note: maintain port in host byte order to make debugging easier */
  91. if (isdigit (*servname))
  92. port = strtol (servname, NULL, 10);
  93. else if ((servent = getservbyname (servname, socktype)) != NULL)
  94. port = ntohs (servent->s_port);
  95. else
  96. return EAI_NONAME;
  97. /* if nodename == NULL refer to the local host for a client or any
  98. for a server */
  99. if (nodename == NULL)
  100. {
  101. struct sockaddr_in sin;
  102. /* check protocol family is PF_UNSPEC or PF_INET - could try harder
  103. for IPv6 but that's more code than I'm prepared to write */
  104. if (hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET)
  105. result.ai_family = AF_INET;
  106. else
  107. return EAI_FAMILY;
  108. sin.sin_family = result.ai_family;
  109. sin.sin_port = htons (port);
  110. if (hints->ai_flags & AI_PASSIVE)
  111. sin.sin_addr.s_addr = htonl (INADDR_ANY);
  112. else
  113. sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
  114. /* Duplicate result and addr and return */
  115. *res = dup_addrinfo (&result, &sin, sizeof sin);
  116. return (*res == NULL) ? EAI_MEMORY : 0;
  117. }
  118. /* If AI_NUMERIC is specified, use inet_addr to translate numbers and
  119. dots notation. */
  120. if (hints->ai_flags & AI_NUMERICHOST)
  121. {
  122. struct sockaddr_in sin;
  123. /* check protocol family is PF_UNSPEC or PF_INET */
  124. if (hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET)
  125. result.ai_family = AF_INET;
  126. else
  127. return EAI_FAMILY;
  128. sin.sin_family = result.ai_family;
  129. sin.sin_port = htons (port);
  130. sin.sin_addr.s_addr = inet_addr (nodename);
  131. /* Duplicate result and addr and return */
  132. *res = dup_addrinfo (&result, &sin, sizeof sin);
  133. return (*res == NULL) ? EAI_MEMORY : 0;
  134. }
  135. errno = 0;
  136. hp = gethostbyname_ctx (nodename, &ghbnctx);
  137. if (hp == NULL)
  138. {
  139. if (errno != 0)
  140. {
  141. free_ghbnctx (&ghbnctx);
  142. return EAI_SYSTEM;
  143. }
  144. code = h_error_ctx (&ghbnctx);
  145. switch (code)
  146. {
  147. case HOST_NOT_FOUND: code = EAI_NODATA; break;
  148. case NO_DATA: code = EAI_NODATA; break;
  149. #if defined(NO_ADDRESS) && NO_ADDRESS != NO_DATA
  150. case NO_ADDRESS: code = EAI_NODATA; break;
  151. #endif
  152. case NO_RECOVERY: code = EAI_FAIL; break;
  153. case TRY_AGAIN: code = EAI_AGAIN; break;
  154. default: code = EAI_FAIL; break;
  155. }
  156. free_ghbnctx (&ghbnctx);
  157. return code;
  158. }
  159. /* Check that the address family is acceptable.
  160. */
  161. switch (hp->h_addrtype)
  162. {
  163. case AF_INET:
  164. if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET))
  165. goto eai_family;
  166. break;
  167. #ifdef USE_IPV6
  168. case AF_INET6:
  169. if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET6))
  170. goto eai_family;
  171. break;
  172. #endif
  173. default:
  174. eai_family:
  175. free_ghbnctx (&ghbnctx);
  176. return EAI_FAMILY;
  177. }
  178. /* For each element pointed to by hp, create an element in the
  179. result linked list. */
  180. sai = eai = NULL;
  181. for (addrs = hp->h_addr_list; *addrs != NULL; addrs++)
  182. {
  183. struct sockaddr sa;
  184. size_t addrlen;
  185. sa.sa_family = hp->h_addrtype;
  186. switch (hp->h_addrtype)
  187. {
  188. case AF_INET:
  189. ((struct sockaddr_in *) &sa)->sin_port = htons (port);
  190. memcpy (&((struct sockaddr_in *) &sa)->sin_addr,
  191. *addrs, hp->h_length);
  192. addrlen = sizeof (struct sockaddr_in);
  193. break;
  194. #ifdef USE_IPV6
  195. case AF_INET6:
  196. # if SIN6_LEN
  197. ((struct sockaddr_in6 *) &sa)->sin6_len = hp->h_length;
  198. # endif
  199. ((struct sockaddr_in6 *) &sa)->sin6_port = htons (port);
  200. memcpy (&((struct sockaddr_in6 *) &sa)->sin6_addr,
  201. *addrs, hp->h_length);
  202. addrlen = sizeof (struct sockaddr_in6);
  203. break;
  204. #endif
  205. default:
  206. continue;
  207. }
  208. result.ai_family = hp->h_addrtype;
  209. ai = dup_addrinfo (&result, &sa, addrlen);
  210. if (ai == NULL)
  211. {
  212. free_ghbnctx (&ghbnctx);
  213. freeaddrinfo (sai);
  214. return EAI_MEMORY;
  215. }
  216. if (sai == NULL)
  217. sai = ai;
  218. else
  219. eai->ai_next = ai;
  220. eai = ai;
  221. }
  222. if (sai == NULL)
  223. {
  224. free_ghbnctx (&ghbnctx);
  225. return EAI_NODATA;
  226. }
  227. if (hints->ai_flags & AI_CANONNAME)
  228. {
  229. sai->ai_canonname = malloc (strlen (hp->h_name) + 1);
  230. if (sai->ai_canonname == NULL)
  231. {
  232. free_ghbnctx (&ghbnctx);
  233. freeaddrinfo (sai);
  234. return EAI_MEMORY;
  235. }
  236. strcpy (sai->ai_canonname, hp->h_name);
  237. }
  238. free_ghbnctx (&ghbnctx);
  239. *res = sai;
  240. return 0;
  241. }
  242. void
  243. freeaddrinfo (struct addrinfo *ai)
  244. {
  245. struct addrinfo *next;
  246. while (ai != NULL)
  247. {
  248. next = ai->ai_next;
  249. if (ai->ai_canonname != NULL)
  250. free (ai->ai_canonname);
  251. if (ai->ai_addr != NULL)
  252. free (ai->ai_addr);
  253. free (ai);
  254. ai = next;
  255. }
  256. }
  257. const char *
  258. gai_strerror (int ecode)
  259. {
  260. static const char *eai_descr[] =
  261. {
  262. "no error",
  263. "address family for nodename not supported", /* EAI_ADDRFAMILY */
  264. "temporary failure in name resolution", /* EAI_AGAIN */
  265. "invalid value for ai_flags", /* EAI_BADFLAGS */
  266. "non-recoverable failure in name resolution", /* EAI_FAIL */
  267. "ai_family not supported", /* EAI_FAMILY */
  268. "memory allocation failure", /* EAI_MEMORY */
  269. "no address associated with nodename", /* EAI_NODATA */
  270. "nodename nor servname provided, or not known", /* EAI_NONAME */
  271. "servname not supported for ai_socktype", /* EAI_SERVICE */
  272. "ai_socktype not supported", /* EAI_SOCKTYPE */
  273. "system error returned in errno", /* EAI_SYSTEM */
  274. };
  275. if (ecode < 0 || ecode > (int) (sizeof eai_descr/ sizeof eai_descr[0]))
  276. return "unknown error";
  277. return eai_descr[ecode];
  278. }