le timeout to 600s, some servers don't follow rfc recommendations
aboutsummaryrefslogtreecommitdiffstats
path: root/libesmtp/gethostbyname.c
blob: cd74ec0aaf489273c90d12ced63c40aa9de6134e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
 *  This file is a ghastly hack because nobody can agree on
 *  gethostbyname_r()'s prototype.
 *
 *  Copyright (C) 2001,2002  Brian Stafford  <brian@stafford.uklinux.net>
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#define _SVID_SOURCE	1	/* Need this to get gethostbyname_r() */

#include <assert.h>

#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <errno.h>

#include "gethostbyname.h"

#if HAVE_GETIPNODEBYNAME

void
free_ghbnctx (struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  if (ctx->hostent != NULL)
    freehostent (ctx->hostent);
}

struct hostent *
gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  memset (ctx, 0, sizeof (struct ghbnctx));
  ctx->hostent = getipnodebyname (host, AF_UNSPEC, AI_ADDRCONFIG, &ctx->h_err);
  return ctx->hostent;
}

int
h_error_ctx (struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  return ctx->h_err;
}

#elif HAVE_GETHOSTBYNAME_R == 6

void
free_ghbnctx (struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  if (ctx->hostbuf != NULL)
    free (ctx->hostbuf);
}

struct hostent *
gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
{
  struct hostent *hp;
  char *tmp;
  int err;

  assert (ctx != NULL);

  memset (ctx, 0, sizeof (struct ghbnctx));
  ctx->hostbuf_len = 2048;
  if ((ctx->hostbuf = (char *)malloc (ctx->hostbuf_len)) == NULL)
    {
      errno = ENOMEM;
      return NULL;
    }
  while ((err = gethostbyname_r (host,
  				 &ctx->hostent, ctx->hostbuf, ctx->hostbuf_len,
  				 &hp, &ctx->h_err)) == ERANGE)
    {
      ctx->hostbuf_len += 1024;
      if ((tmp = (char *)realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
	{
	  errno = ENOMEM;
	  return NULL;
	}
      ctx->hostbuf = tmp;
    }
  if (err != 0)
    {
      errno = err;
      return NULL;
    }
  return hp;
}

int
h_error_ctx (struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  return ctx->h_err;
}

#elif HAVE_GETHOSTBYNAME_R == 5

void
free_ghbnctx (struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  if (ctx->hostbuf != NULL)
    free (ctx->hostbuf);
}

struct hostent *
gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
{
  struct hostent *hp;
  char *tmp;

  assert (ctx != NULL);

  memset (ctx, 0, sizeof (struct ghbnctx));
  ctx->hostbuf_len = 2048;
  if ((ctx->hostbuf = malloc (ctx->hostbuf_len)) == NULL)
    {
      errno = ENOMEM;
      return NULL;
    }
  while ((hp = gethostbyname_r (host, &ctx->hostent,
  				ctx->hostbuf, ctx->hostbuf_len,
  				&ctx->h_err)) == NULL && errno == ERANGE)
    {
      ctx->hostbuf_len += 1024;
      if ((tmp = realloc (ctx->hostbuf, ctx->hostbuf_len)) == NULL)
	{
	  errno = ENOMEM;
	  return NULL;
	}
      ctx->hostbuf = tmp;
    }
  return hp;
}

int
h_error_ctx (struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  return ctx->h_err;
}

#elif HAVE_GETHOSTBYNAME_R == 3

void
free_ghbnctx (struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  /* FIXME: does this need to do anything? */
}

struct hostent *
gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  if (!gethostbyname_r (host, &ctx->hostent, &ctx->hostent_data))
    {
      ctx->h_err = h_errno;	/* FIXME: is this correct? */
      return NULL;
    }
  return &ctx->hostent;
}
  
int
h_error_ctx (struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  return ctx->h_err;
}

#else

void
free_ghbnctx (struct ghbnctx *ctx )
{
  assert (ctx != NULL);
}

struct hostent *
gethostbyname_ctx (const char *host, struct ghbnctx *ctx)
{
  struct hostent *hp;

  hp = gethostbyname (host);
  if (hp == NULL)
    ctx->h_err = h_errno;
  return hp;
}

int
h_error_ctx (struct ghbnctx *ctx)
{
  assert (ctx != NULL);

  return ctx->h_err;
}

#endif