gethostbyname.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. /******************************************************************************
  2. *
  3. * Nagios gethostbyname_r()'s prototype.
  4. *
  5. * License: GPL
  6. * Copyright (C) 2001,2002 Brian Stafford <brian@stafford.uklinux.net>
  7. *
  8. * Last Modified: $Date$
  9. *
  10. * Description:
  11. *
  12. * This file is a ghastly hack because nobody can agree on
  13. * gethostbyname_r()'s prototype.
  14. *
  15. * License Information:
  16. *
  17. * This program is free software; you can redistribute it and/or modify
  18. * it under the terms of the GNU General Public License as published by
  19. * the Free Software Foundation; either version 2 of the License, or
  20. * (at your option) any later version.
  21. *
  22. * This program is distributed in the hope that it will be useful,
  23. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. * GNU General Public License for more details.
  26. *
  27. * You should have received a copy of the GNU General Public License
  28. * along with this program; if not, write to the Free Software
  29. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  30. *
  31. * $Id$
  32. *
  33. *****************************************************************************/
  34. #ifdef HAVE_CONFIG_H
  35. #include <config.h>
  36. #endif
  37. #define _SVID_SOURCE 1 /* Need this to get gethostbyname_r() */
  38. #include <assert.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <netdb.h>
  42. #include <errno.h>
  43. #include "gethostbyname.h"
  44. #if HAVE_GETIPNODEBYNAME
  45. void
  46. free_ghbnctx (struct ghbnctx *ctx)
  47. {
  48. assert (ctx != NULL);
  49. if (ctx->hostent != NULL)
  50. freehostent (ctx->hostent);
  51. }
  52. struct hostent *
  53. gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
  54. {
  55. assert (ctx != NULL);
  56. memset (ctx, 0, sizeof (struct ghbnctx));
  57. ctx->hostent = getipnodebyname (host, AF_UNSPEC, AI_ADDRCONFIG, &ctx->h_err);
  58. return ctx->hostent;
  59. }
  60. int
  61. h_error_ctx (struct ghbnctx *ctx)
  62. {
  63. assert (ctx != NULL);
  64. return ctx->h_err;
  65. }
  66. #elif HAVE_GETHOSTBYNAME_R == 6
  67. void
  68. free_ghbnctx (struct ghbnctx *ctx)
  69. {
  70. assert (ctx != NULL);
  71. if (ctx->hostbuf != NULL)
  72. free (ctx->hostbuf);
  73. }
  74. struct hostent *
  75. gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
  76. {
  77. struct hostent *hp;
  78. char *tmp;
  79. int err;
  80. assert (ctx != NULL);
  81. memset (ctx, 0, sizeof (struct ghbnctx));
  82. ctx->hostbuf_len = 2048;
  83. if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
  84. {
  85. errno = ENOMEM;
  86. return NULL;
  87. }
  88. while ((err = gethostbyname_r (host,
  89. &ctx->hostent, ctx->hostbuf, ctx->hostbuf_len,
  90. &hp, &ctx->h_err)) == ERANGE)
  91. {
  92. ctx->hostbuf_len += 1024;
  93. if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
  94. {
  95. errno = ENOMEM;
  96. return NULL;
  97. }
  98. ctx->hostbuf = tmp;
  99. }
  100. if (err != 0)
  101. {
  102. errno = err;
  103. return NULL;
  104. }
  105. return hp;
  106. }
  107. int
  108. h_error_ctx (struct ghbnctx *ctx)
  109. {
  110. assert (ctx != NULL);
  111. return ctx->h_err;
  112. }
  113. #elif HAVE_GETHOSTBYNAME_R == 5
  114. void
  115. free_ghbnctx (struct ghbnctx *ctx)
  116. {
  117. assert (ctx != NULL);
  118. if (ctx->hostbuf != NULL)
  119. free (ctx->hostbuf);
  120. }
  121. struct hostent *
  122. gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
  123. {
  124. struct hostent *hp;
  125. char *tmp;
  126. assert (ctx != NULL);
  127. memset (ctx, 0, sizeof (struct ghbnctx));
  128. ctx->hostbuf_len = 2048;
  129. if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
  130. {
  131. errno = ENOMEM;
  132. return NULL;
  133. }
  134. while ((hp = gethostbyname_r (host, &ctx->hostent,
  135. ctx->hostbuf, ctx->hostbuf_len,
  136. &ctx->h_err)) == NULL && errno == ERANGE)
  137. {
  138. ctx->hostbuf_len += 1024;
  139. if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
  140. {
  141. errno = ENOMEM;
  142. return NULL;
  143. }
  144. ctx->hostbuf = tmp;
  145. }
  146. return hp;
  147. }
  148. int
  149. h_error_ctx (struct ghbnctx *ctx)
  150. {
  151. assert (ctx != NULL);
  152. return ctx->h_err;
  153. }
  154. #elif HAVE_GETHOSTBYNAME_R == 3
  155. void
  156. free_ghbnctx (struct ghbnctx *ctx)
  157. {
  158. assert (ctx != NULL);
  159. /* FIXME: does this need to do anything? */
  160. }
  161. struct hostent *
  162. gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
  163. {
  164. assert (ctx != NULL);
  165. if (!gethostbyname_r (host, &ctx->hostent, &ctx->hostent_data))
  166. {
  167. ctx->h_err = h_errno; /* FIXME: is this correct? */
  168. return NULL;
  169. }
  170. return &ctx->hostent;
  171. }
  172. int
  173. h_error_ctx (struct ghbnctx *ctx)
  174. {
  175. assert (ctx != NULL);
  176. return ctx->h_err;
  177. }
  178. #else
  179. void
  180. free_ghbnctx (struct ghbnctx *ctx __attribute__ ((unused)))
  181. {
  182. assert (ctx != NULL);
  183. }
  184. struct hostent *
  185. gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
  186. {
  187. struct hostent *hp;
  188. hp = gethostbyname (host);
  189. if (hp == NULL)
  190. ctx->h_err = h_errno;
  191. return hp;
  192. }
  193. int
  194. h_error_ctx (struct ghbnctx *ctx)
  195. {
  196. assert (ctx != NULL);
  197. return ctx->h_err;
  198. }
  199. #endif