aboutsummaryrefslogtreecommitdiff
path: root/lib/getaddrinfo.c
blob: c958da6c983e30c49b1156d0c477d5af7751d093 (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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*
 *  This file is part of libESMTP, a library for submission of RFC 2822
 *  formatted electronic mail messages using the SMTP protocol described
 *  in RFC 2821.
 *
 *  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
 */

/* An emulation of the RFC 2553 / Posix getaddrinfo resolver interface.
 */

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

/* Need to turn off Posix features in glibc to build this */
#undef _POSIX_C_SOURCE
#undef _XOPEN_SOURCE

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

#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>

#include <netdb.h>

#include "gethostbyname.h"
#include "getaddrinfo.h"

static struct addrinfo *
dup_addrinfo (struct addrinfo *info, void *addr, size_t addrlen)
{
  struct addrinfo *ret;

  ret = malloc (sizeof (struct addrinfo));
  if (ret == NULL)
    return NULL;
  memcpy (ret, info, sizeof (struct addrinfo));
  ret->ai_addr = malloc (addrlen);
  if (ret->ai_addr == NULL)
    {
      free (ret);
      return NULL;
    }
  memcpy (ret->ai_addr, addr, addrlen);
  ret->ai_addrlen = addrlen;
  return ret;
}

int
getaddrinfo (const char *nodename, const char *servname,
	     const struct addrinfo *hints, struct addrinfo **res)
{
  struct hostent *hp;
  struct servent *servent;
  const char *socktype;
  int port;
  struct addrinfo hint, result;
  struct addrinfo *ai, *sai, *eai;
  struct ghbnctx ghbnctx;
  char **addrs;
  int code;

  memset (&result, 0, sizeof result);

  /* default for hints */
  if (hints == NULL)
    {
      memset (&hint, 0, sizeof hint);
      hint.ai_family = PF_UNSPEC;
      hints = &hint;
    }

  /* servname must not be NULL in this implementation */
  if (servname == NULL)
    return EAI_NONAME;

  /* check for tcp or udp sockets only */
  if (hints->ai_socktype == SOCK_STREAM)
    socktype = "tcp";
  else if (hints->ai_socktype == SOCK_DGRAM)
    socktype = "udp";
  else
    return EAI_SERVICE;
  result.ai_socktype = hints->ai_socktype;

  /* Note: maintain port in host byte order to make debugging easier */
  if (isdigit (*servname))
    port = strtol (servname, NULL, 10);
  else if ((servent = getservbyname (servname, socktype)) != NULL)
    port = ntohs (servent->s_port);
  else
    return EAI_NONAME;

  /* if nodename == NULL refer to the local host for a client or any
     for a server */
  if (nodename == NULL)
    {
      struct sockaddr_in sin;

      /* check protocol family is PF_UNSPEC or PF_INET - could try harder
         for IPv6 but that's more code than I'm prepared to write */
      if (hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET)
	result.ai_family = AF_INET;
      else
	return EAI_FAMILY;

      sin.sin_family = result.ai_family;
      sin.sin_port = htons (port);
      if (hints->ai_flags & AI_PASSIVE)
        sin.sin_addr.s_addr = htonl (INADDR_ANY);
      else
        sin.sin_addr.s_addr = htonl (INADDR_LOOPBACK);
      /* Duplicate result and addr and return */
      *res = dup_addrinfo (&result, &sin, sizeof sin);
      return (*res == NULL) ? EAI_MEMORY : 0;
    }

  /* If AI_NUMERIC is specified, use inet_addr to translate numbers and
     dots notation. */
  if (hints->ai_flags & AI_NUMERICHOST)
    {
      struct sockaddr_in sin;

      /* check protocol family is PF_UNSPEC or PF_INET */
      if (hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET)
	result.ai_family = AF_INET;
      else
	return EAI_FAMILY;

      sin.sin_family = result.ai_family;
      sin.sin_port = htons (port);
      sin.sin_addr.s_addr = inet_addr (nodename);
      /* Duplicate result and addr and return */
      *res = dup_addrinfo (&result, &sin, sizeof sin);
      return (*res == NULL) ? EAI_MEMORY : 0;
    }

  errno = 0;
  hp = gethostbyname_ctx (nodename, &ghbnctx);
  if (hp == NULL)
    {
      if (errno != 0)
	{
	  free_ghbnctx (&ghbnctx);
	  return EAI_SYSTEM;
	}
      code = h_error_ctx (&ghbnctx);
      switch (code)
	{
	case HOST_NOT_FOUND: code = EAI_NODATA; break;
	case NO_DATA: code = EAI_NODATA; break;
#if defined(NO_ADDRESS) && NO_ADDRESS != NO_DATA
	case NO_ADDRESS: code = EAI_NODATA; break;
#endif
	case NO_RECOVERY: code = EAI_FAIL; break;
	case TRY_AGAIN: code = EAI_AGAIN; break;
	default: code = EAI_FAIL; break;
	}
      free_ghbnctx (&ghbnctx);
      return code;
    }

  /* Check that the address family is acceptable.
   */
  switch (hp->h_addrtype)
    {
    case AF_INET:
      if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET))
	goto eai_family;
      break;
#ifdef USE_IPV6
    case AF_INET6:
      if (!(hints->ai_family == PF_UNSPEC || hints->ai_family == PF_INET6))
	goto eai_family;
      break;
#endif
    default:
    eai_family:
      free_ghbnctx (&ghbnctx);
      return EAI_FAMILY;
    }

  /* For each element pointed to by hp, create an element in the
     result linked list. */
  sai = eai = NULL;
  for (addrs = hp->h_addr_list; *addrs != NULL; addrs++)
    {
      struct sockaddr sa;
      size_t addrlen;

      sa.sa_family = hp->h_addrtype;
      switch (hp->h_addrtype)
        {
        case AF_INET:
	  ((struct sockaddr_in *) &sa)->sin_port = htons (port);
	  memcpy (&((struct sockaddr_in *) &sa)->sin_addr,
	          *addrs, hp->h_length);
          addrlen = sizeof (struct sockaddr_in);
          break;
#ifdef USE_IPV6
        case AF_INET6:
# if SIN6_LEN
	  ((struct sockaddr_in6 *) &sa)->sin6_len = hp->h_length;
# endif
	  ((struct sockaddr_in6 *) &sa)->sin6_port = htons (port);
	  memcpy (&((struct sockaddr_in6 *) &sa)->sin6_addr,
	  	  *addrs, hp->h_length);
          addrlen = sizeof (struct sockaddr_in6);
          break;
#endif
        default:
          continue;
        }

      result.ai_family = hp->h_addrtype;
      ai = dup_addrinfo (&result, &sa, addrlen);
      if (ai == NULL)
        {
	  free_ghbnctx (&ghbnctx);
          freeaddrinfo (sai);
          return EAI_MEMORY;
        }
      if (sai == NULL)
	sai = ai;
      else
	eai->ai_next = ai;
      eai = ai;
    }

  if (sai == NULL)
    {
      free_ghbnctx (&ghbnctx);
      return EAI_NODATA;
    }
  
  if (hints->ai_flags & AI_CANONNAME) 
    {
      sai->ai_canonname = malloc (strlen (hp->h_name) + 1);
      if (sai->ai_canonname == NULL)
        {
	  free_ghbnctx (&ghbnctx);
          freeaddrinfo (sai);
          return EAI_MEMORY;
        }
      strcpy (sai->ai_canonname, hp->h_name);
    }

  free_ghbnctx (&ghbnctx);
  *res = sai;
  return 0;
}

void
freeaddrinfo (struct addrinfo *ai)
{
  struct addrinfo *next;

  while (ai != NULL)
    {
      next = ai->ai_next;
      if (ai->ai_canonname != NULL)
        free (ai->ai_canonname);
      if (ai->ai_addr != NULL)
        free (ai->ai_addr);
      free (ai);
      ai = next;
    }
}

const char *
gai_strerror (int ecode)
{
  static const char *eai_descr[] =
    {
      "no error",
      "address family for nodename not supported",	/* EAI_ADDRFAMILY */
      "temporary failure in name resolution",		/* EAI_AGAIN */
      "invalid value for ai_flags",	 		/* EAI_BADFLAGS */
      "non-recoverable failure in name resolution",	/* EAI_FAIL */
      "ai_family not supported",			/* EAI_FAMILY */
      "memory allocation failure",			/* EAI_MEMORY */
      "no address associated with nodename",		/* EAI_NODATA */
      "nodename nor servname provided, or not known",	/* EAI_NONAME */
      "servname not supported for ai_socktype",		/* EAI_SERVICE */
      "ai_socktype not supported",			/* EAI_SOCKTYPE */
      "system error returned in errno",			/* EAI_SYSTEM */
    };

  if (ecode < 0 || ecode > (int) (sizeof eai_descr/ sizeof eai_descr[0]))
    return "unknown error";
  return eai_descr[ecode];
}