getaddrinfo.c 8.0 KB

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