1
/* -*- buffer-read-only: t -*- vi: set ro: */
2
/* DO NOT EDIT! GENERATED AUTOMATICALLY! */
3
/* Get address information (partial implementation).
4
Copyright (C) 1997, 2001-2002, 2004-2011 Free Software Foundation, Inc.
5
Contributed by Simon Josefsson <simon@josefsson.org>.
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 3, or (at your option)
12
This program 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
15
GNU General Public License for more details.
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software Foundation,
19
Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23
/* Don't use __attribute__ __nonnull__ in this compilation unit. Otherwise gcc
24
optimizes away the sa == NULL test below. */
25
#define _GL_ARG_NONNULL(params)
30
# include <netinet/in.h>
34
#include <arpa/inet.h>
39
/* Get memcpy, strdup. */
48
#define _(String) gettext (String)
49
#define N_(String) String
51
/* BeOS has AF_INET, but not PF_INET. */
53
# define PF_INET AF_INET
55
/* BeOS also lacks PF_UNSPEC. */
60
#if defined _WIN32 || defined __WIN32__
64
/* gl_sockets_startup */
68
typedef int (WSAAPI *getaddrinfo_func) (const char*, const char*,
69
const struct addrinfo*,
71
typedef void (WSAAPI *freeaddrinfo_func) (struct addrinfo*);
72
typedef int (WSAAPI *getnameinfo_func) (const struct sockaddr*,
73
socklen_t, char*, DWORD,
76
static getaddrinfo_func getaddrinfo_ptr = NULL;
77
static freeaddrinfo_func freeaddrinfo_ptr = NULL;
78
static getnameinfo_func getnameinfo_ptr = NULL;
87
return getaddrinfo_ptr ? 1 : 0;
91
h = GetModuleHandle ("ws2_32.dll");
95
getaddrinfo_ptr = (getaddrinfo_func) GetProcAddress (h, "getaddrinfo");
96
freeaddrinfo_ptr = (freeaddrinfo_func) GetProcAddress (h, "freeaddrinfo");
97
getnameinfo_ptr = (getnameinfo_func) GetProcAddress (h, "getnameinfo");
100
/* If either is missing, something is odd. */
101
if (!getaddrinfo_ptr || !freeaddrinfo_ptr || !getnameinfo_ptr)
103
getaddrinfo_ptr = NULL;
104
freeaddrinfo_ptr = NULL;
105
getnameinfo_ptr = NULL;
109
gl_sockets_startup (SOCKETS_1_1);
116
validate_family (int family)
118
/* FIXME: Support more families. */
120
if (family == PF_INET)
124
if (family == PF_INET6)
127
if (family == PF_UNSPEC)
132
/* Translate name of a service location and/or a service name to set of
135
getaddrinfo (const char *restrict nodename,
136
const char *restrict servname,
137
const struct addrinfo *restrict hints,
138
struct addrinfo **restrict res)
140
struct addrinfo *tmp;
147
struct addrinfo addrinfo;
148
struct sockaddr_in6 sockaddr_in6;
153
struct addrinfo addrinfo;
154
struct sockaddr_in sockaddr_in;
160
return getaddrinfo_ptr (nodename, servname, hints, res);
163
if (hints && (hints->ai_flags & ~(AI_CANONNAME|AI_PASSIVE)))
164
/* FIXME: Support more flags. */
167
if (hints && !validate_family (hints->ai_family))
171
hints->ai_socktype != SOCK_STREAM && hints->ai_socktype != SOCK_DGRAM)
172
/* FIXME: Support other socktype. */
173
return EAI_SOCKTYPE; /* FIXME: Better return code? */
177
if (!(hints->ai_flags & AI_PASSIVE))
181
nodename = (hints->ai_family == AF_INET6) ? "::" : "0.0.0.0";
183
nodename = "0.0.0.0";
189
struct servent *se = NULL;
191
(hints && hints->ai_socktype == SOCK_DGRAM) ? "udp" : "tcp";
193
if (hints == NULL || !(hints->ai_flags & AI_NUMERICSERV))
194
/* FIXME: Use getservbyname_r if available. */
195
se = getservbyname (servname, proto);
200
if (!(*servname >= '0' && *servname <= '9'))
202
port = strtoul (servname, &c, 10);
203
if (*c || port > 0xffff)
211
/* FIXME: Use gethostbyname_r if available. */
212
he = gethostbyname (nodename);
213
if (!he || he->h_addr_list[0] == NULL)
216
switch (he->h_addrtype)
220
size = sizeof (struct v6_pair);
226
size = sizeof (struct v4_pair);
234
storage = calloc (1, size);
238
switch (he->h_addrtype)
243
struct v6_pair *p = storage;
244
struct sockaddr_in6 *sinp = &p->sockaddr_in6;
248
sinp->sin6_port = port;
250
if (he->h_length != sizeof (sinp->sin6_addr))
253
return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
256
memcpy (&sinp->sin6_addr, he->h_addr_list[0], sizeof sinp->sin6_addr);
258
tmp->ai_addr = (struct sockaddr *) sinp;
259
tmp->ai_addrlen = sizeof *sinp;
267
struct v4_pair *p = storage;
268
struct sockaddr_in *sinp = &p->sockaddr_in;
272
sinp->sin_port = port;
274
if (he->h_length != sizeof (sinp->sin_addr))
277
return EAI_SYSTEM; /* FIXME: Better return code? Set errno? */
280
memcpy (&sinp->sin_addr, he->h_addr_list[0], sizeof sinp->sin_addr);
282
tmp->ai_addr = (struct sockaddr *) sinp;
283
tmp->ai_addrlen = sizeof *sinp;
293
if (hints && hints->ai_flags & AI_CANONNAME)
301
tmp->ai_canonname = strdup (cn);
302
if (!tmp->ai_canonname)
309
tmp->ai_protocol = (hints) ? hints->ai_protocol : 0;
310
tmp->ai_socktype = (hints) ? hints->ai_socktype : 0;
311
tmp->ai_addr->sa_family = he->h_addrtype;
312
tmp->ai_family = he->h_addrtype;
314
#ifdef HAVE_STRUCT_SOCKADDR_SA_LEN
315
switch (he->h_addrtype)
319
tmp->ai_addr->sa_len = sizeof (struct sockaddr_in);
324
tmp->ai_addr->sa_len = sizeof (struct sockaddr_in6);
330
/* FIXME: If more than one address, create linked list of addrinfo's. */
337
/* Free `addrinfo' structure AI including associated storage. */
339
freeaddrinfo (struct addrinfo *ai)
344
freeaddrinfo_ptr (ai);
351
struct addrinfo *cur;
356
free (cur->ai_canonname);
362
getnameinfo (const struct sockaddr *restrict sa, socklen_t salen,
363
char *restrict node, socklen_t nodelen,
364
char *restrict service, socklen_t servicelen,
369
return getnameinfo_ptr (sa, salen, node, nodelen,
370
service, servicelen, flags);
373
/* FIXME: Support other flags. */
374
if ((node && nodelen > 0 && !(flags & NI_NUMERICHOST)) ||
375
(service && servicelen > 0 && !(flags & NI_NUMERICHOST)) ||
376
(flags & ~(NI_NUMERICHOST|NI_NUMERICSERV)))
379
if (sa == NULL || salen < sizeof (sa->sa_family))
382
switch (sa->sa_family)
386
if (salen < sizeof (struct sockaddr_in))
392
if (salen < sizeof (struct sockaddr_in6))
400
if (node && nodelen > 0 && flags & NI_NUMERICHOST)
402
switch (sa->sa_family)
406
if (!inet_ntop (AF_INET,
407
&(((const struct sockaddr_in *) sa)->sin_addr),
415
if (!inet_ntop (AF_INET6,
416
&(((const struct sockaddr_in6 *) sa)->sin6_addr),
427
if (service && servicelen > 0 && flags & NI_NUMERICSERV)
428
switch (sa->sa_family)
437
unsigned short int port
438
= ntohs (((const struct sockaddr_in *) sa)->sin_port);
439
if (servicelen <= snprintf (service, servicelen, "%u", port))