2 * This file is a ghastly hack because nobody can agree on
3 * gethostbyname_r()'s prototype.
5 * Copyright (C) 2001,2002 Brian Stafford <brian@stafford.uklinux.net>
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.
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.
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
26 #define _SVID_SOURCE 1 /* Need this to get gethostbyname_r() */
35 #include "gethostbyname.h"
37 #if HAVE_GETIPNODEBYNAME
40 free_ghbnctx (struct ghbnctx *ctx)
44 if (ctx->hostent != NULL)
45 freehostent (ctx->hostent);
49 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
53 memset (ctx, 0, sizeof (struct ghbnctx));
54 ctx->hostent = getipnodebyname (host, AF_UNSPEC, AI_ADDRCONFIG, &ctx->h_err);
59 h_error_ctx (struct ghbnctx *ctx)
66 #elif HAVE_GETHOSTBYNAME_R == 6
69 free_ghbnctx (struct ghbnctx *ctx)
73 if (ctx->hostbuf != NULL)
78 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
86 memset (ctx, 0, sizeof (struct ghbnctx));
87 ctx->hostbuf_len = 2048;
88 if ((ctx->hostbuf = (char *)malloc (ctx->hostbuf_len)) == NULL)
93 while ((err = gethostbyname_r (host,
94 &ctx->hostent, ctx->hostbuf, ctx->hostbuf_len,
95 &hp, &ctx->h_err)) == ERANGE)
97 ctx->hostbuf_len += 1024;
98 if ((tmp = (char *)realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
114 h_error_ctx (struct ghbnctx *ctx)
116 assert (ctx != NULL);
121 #elif HAVE_GETHOSTBYNAME_R == 5
124 free_ghbnctx (struct ghbnctx *ctx)
126 assert (ctx != NULL);
128 if (ctx->hostbuf != NULL)
133 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
138 assert (ctx != NULL);
140 memset (ctx, 0, sizeof (struct ghbnctx));
141 ctx->hostbuf_len = 2048;
142 if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
147 while ((hp = gethostbyname_r (host, &ctx->hostent,
148 ctx->hostbuf, ctx->hostbuf_len,
149 &ctx->h_err)) == NULL && errno == ERANGE)
151 ctx->hostbuf_len += 1024;
152 if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
163 h_error_ctx (struct ghbnctx *ctx)
165 assert (ctx != NULL);
170 #elif HAVE_GETHOSTBYNAME_R == 3
173 free_ghbnctx (struct ghbnctx *ctx)
175 assert (ctx != NULL);
177 /* FIXME: does this need to do anything? */
181 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
183 assert (ctx != NULL);
185 if (!gethostbyname_r (host, &ctx->hostent, &ctx->hostent_data))
187 ctx->h_err = h_errno; /* FIXME: is this correct? */
190 return &ctx->hostent;
194 h_error_ctx (struct ghbnctx *ctx)
196 assert (ctx != NULL);
204 free_ghbnctx (struct ghbnctx *ctx )
206 assert (ctx != NULL);
210 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
214 hp = gethostbyname (host);
216 ctx->h_err = h_errno;
221 h_error_ctx (struct ghbnctx *ctx)
223 assert (ctx != NULL);