gethostbyname.c 4.3 KB

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