getaddrinfo.c 8.0 KB

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