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 * $Id$
22 */
24 #ifdef HAVE_CONFIG_H
25 #include <config.h>
26 #endif
28 #define _SVID_SOURCE 1 /* Need this to get gethostbyname_r() */
30 #include <assert.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <netdb.h>
35 #include <errno.h>
37 #include "gethostbyname.h"
39 #if HAVE_GETIPNODEBYNAME
41 void
42 free_ghbnctx (struct ghbnctx *ctx)
43 {
44 assert (ctx != NULL);
46 if (ctx->hostent != NULL)
47 freehostent (ctx->hostent);
48 }
50 struct hostent *
51 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
52 {
53 assert (ctx != NULL);
55 memset (ctx, 0, sizeof (struct ghbnctx));
56 ctx->hostent = getipnodebyname (host, AF_UNSPEC, AI_ADDRCONFIG, &ctx->h_err);
57 return ctx->hostent;
58 }
60 int
61 h_error_ctx (struct ghbnctx *ctx)
62 {
63 assert (ctx != NULL);
65 return ctx->h_err;
66 }
68 #elif HAVE_GETHOSTBYNAME_R == 6
70 void
71 free_ghbnctx (struct ghbnctx *ctx)
72 {
73 assert (ctx != NULL);
75 if (ctx->hostbuf != NULL)
76 free (ctx->hostbuf);
77 }
79 struct hostent *
80 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
81 {
82 struct hostent *hp;
83 char *tmp;
84 int err;
86 assert (ctx != NULL);
88 memset (ctx, 0, sizeof (struct ghbnctx));
89 ctx->hostbuf_len = 2048;
90 if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
91 {
92 errno = ENOMEM;
93 return NULL;
94 }
95 while ((err = gethostbyname_r (host,
96 &ctx->hostent, ctx->hostbuf, ctx->hostbuf_len,
97 &hp, &ctx->h_err)) == ERANGE)
98 {
99 ctx->hostbuf_len += 1024;
100 if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
101 {
102 errno = ENOMEM;
103 return NULL;
104 }
105 ctx->hostbuf = tmp;
106 }
107 if (err != 0)
108 {
109 errno = err;
110 return NULL;
111 }
112 return hp;
113 }
115 int
116 h_error_ctx (struct ghbnctx *ctx)
117 {
118 assert (ctx != NULL);
120 return ctx->h_err;
121 }
123 #elif HAVE_GETHOSTBYNAME_R == 5
125 void
126 free_ghbnctx (struct ghbnctx *ctx)
127 {
128 assert (ctx != NULL);
130 if (ctx->hostbuf != NULL)
131 free (ctx->hostbuf);
132 }
134 struct hostent *
135 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
136 {
137 struct hostent *hp;
138 char *tmp;
140 assert (ctx != NULL);
142 memset (ctx, 0, sizeof (struct ghbnctx));
143 ctx->hostbuf_len = 2048;
144 if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
145 {
146 errno = ENOMEM;
147 return NULL;
148 }
149 while ((hp = gethostbyname_r (host, &ctx->hostent,
150 ctx->hostbuf, ctx->hostbuf_len,
151 &ctx->h_err)) == NULL && errno == ERANGE)
152 {
153 ctx->hostbuf_len += 1024;
154 if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
155 {
156 errno = ENOMEM;
157 return NULL;
158 }
159 ctx->hostbuf = tmp;
160 }
161 return hp;
162 }
164 int
165 h_error_ctx (struct ghbnctx *ctx)
166 {
167 assert (ctx != NULL);
169 return ctx->h_err;
170 }
172 #elif HAVE_GETHOSTBYNAME_R == 3
174 void
175 free_ghbnctx (struct ghbnctx *ctx)
176 {
177 assert (ctx != NULL);
179 /* FIXME: does this need to do anything? */
180 }
182 struct hostent *
183 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
184 {
185 assert (ctx != NULL);
187 if (!gethostbyname_r (host, &ctx->hostent, &ctx->hostent_data))
188 {
189 ctx->h_err = h_errno; /* FIXME: is this correct? */
190 return NULL;
191 }
192 return &ctx->hostent;
193 }
195 int
196 h_error_ctx (struct ghbnctx *ctx)
197 {
198 assert (ctx != NULL);
200 return ctx->h_err;
201 }
203 #else
205 void
206 free_ghbnctx (struct ghbnctx *ctx __attribute__ ((unused)))
207 {
208 assert (ctx != NULL);
209 }
211 struct hostent *
212 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
213 {
214 struct hostent *hp;
216 hp = gethostbyname (host);
217 if (hp == NULL)
218 ctx->h_err = h_errno;
219 return hp;
220 }
222 int
223 h_error_ctx (struct ghbnctx *ctx)
224 {
225 assert (ctx != NULL);
227 return ctx->h_err;
228 }
230 #endif