Code

Initialise strings for Fedora Core 5 (Henning Schmiedehausen)
[nagiosplug.git] / plugins / gethostbyname.c
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 *****************************************************************************/
35 #ifdef HAVE_CONFIG_H
36 #include <config.h>
37 #endif
39 #define _SVID_SOURCE    1       /* Need this to get gethostbyname_r() */
41 #include <assert.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <netdb.h>
46 #include <errno.h>
48 #include "gethostbyname.h"
50 #if HAVE_GETIPNODEBYNAME
52 void
53 free_ghbnctx (struct ghbnctx *ctx)
54 {
55   assert (ctx != NULL);
57   if (ctx->hostent != NULL)
58     freehostent (ctx->hostent);
59 }
61 struct hostent *
62 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
63 {
64   assert (ctx != NULL);
66   memset (ctx, 0, sizeof (struct ghbnctx));
67   ctx->hostent = getipnodebyname (host, AF_UNSPEC, AI_ADDRCONFIG, &ctx->h_err);
68   return ctx->hostent;
69 }
71 int
72 h_error_ctx (struct ghbnctx *ctx)
73 {
74   assert (ctx != NULL);
76   return ctx->h_err;
77 }
79 #elif HAVE_GETHOSTBYNAME_R == 6
81 void
82 free_ghbnctx (struct ghbnctx *ctx)
83 {
84   assert (ctx != NULL);
86   if (ctx->hostbuf != NULL)
87     free (ctx->hostbuf);
88 }
90 struct hostent *
91 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
92 {
93   struct hostent *hp;
94   char *tmp;
95   int err;
97   assert (ctx != NULL);
99   memset (ctx, 0, sizeof (struct ghbnctx));
100   ctx->hostbuf_len = 2048;
101   if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
102     {
103       errno = ENOMEM;
104       return NULL;
105     }
106   while ((err = gethostbyname_r (host,
107                                  &ctx->hostent, ctx->hostbuf, ctx->hostbuf_len,
108                                  &hp, &ctx->h_err)) == ERANGE)
109     {
110       ctx->hostbuf_len += 1024;
111       if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
112         {
113           errno = ENOMEM;
114           return NULL;
115         }
116       ctx->hostbuf = tmp;
117     }
118   if (err != 0)
119     {
120       errno = err;
121       return NULL;
122     }
123   return hp;
126 int
127 h_error_ctx (struct ghbnctx *ctx)
129   assert (ctx != NULL);
131   return ctx->h_err;
134 #elif HAVE_GETHOSTBYNAME_R == 5
136 void
137 free_ghbnctx (struct ghbnctx *ctx)
139   assert (ctx != NULL);
141   if (ctx->hostbuf != NULL)
142     free (ctx->hostbuf);
145 struct hostent *
146 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
148   struct hostent *hp;
149   char *tmp;
151   assert (ctx != NULL);
153   memset (ctx, 0, sizeof (struct ghbnctx));
154   ctx->hostbuf_len = 2048;
155   if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
156     {
157       errno = ENOMEM;
158       return NULL;
159     }
160   while ((hp = gethostbyname_r (host, &ctx->hostent,
161                                 ctx->hostbuf, ctx->hostbuf_len,
162                                 &ctx->h_err)) == NULL && errno == ERANGE)
163     {
164       ctx->hostbuf_len += 1024;
165       if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
166         {
167           errno = ENOMEM;
168           return NULL;
169         }
170       ctx->hostbuf = tmp;
171     }
172   return hp;
175 int
176 h_error_ctx (struct ghbnctx *ctx)
178   assert (ctx != NULL);
180   return ctx->h_err;
183 #elif HAVE_GETHOSTBYNAME_R == 3
185 void
186 free_ghbnctx (struct ghbnctx *ctx)
188   assert (ctx != NULL);
190   /* FIXME: does this need to do anything? */
193 struct hostent *
194 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
196   assert (ctx != NULL);
198   if (!gethostbyname_r (host, &ctx->hostent, &ctx->hostent_data))
199     {
200       ctx->h_err = h_errno;     /* FIXME: is this correct? */
201       return NULL;
202     }
203   return &ctx->hostent;
205   
206 int
207 h_error_ctx (struct ghbnctx *ctx)
209   assert (ctx != NULL);
211   return ctx->h_err;
214 #else
216 void
217 free_ghbnctx (struct ghbnctx *ctx __attribute__ ((unused)))
219   assert (ctx != NULL);
222 struct hostent *
223 gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
225   struct hostent *hp;
227   hp = gethostbyname (host);
228   if (hp == NULL)
229     ctx->h_err = h_errno;
230   return hp;
233 int
234 h_error_ctx (struct ghbnctx *ctx)
236   assert (ctx != NULL);
238   return ctx->h_err;
241 #endif