1 /* Get address information (partial implementation).
2 Copyright (C) 1997, 2001, 2002, 2004, 2005 Free Software Foundation, Inc.
3 Contributed by Simon Josefsson <simon@josefsson.org>.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software Foundation,
17 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 #include "getaddrinfo.h"
34 #define _(String) gettext (String)
35 #define N_(String) String
40 validate_family (int family
)
42 /* FIXME: Support more families. */
44 if (family
== PF_INET
)
48 if (family
== PF_INET6
)
51 if (family
== PF_UNSPEC
)
56 /* Translate name of a service location and/or a service name to set of
59 getaddrinfo (const char *restrict nodename
,
60 const char *restrict servname
,
61 const struct addrinfo
*restrict hints
,
62 struct addrinfo
**restrict res
)
69 if (hints
&& (hints
->ai_flags
& ~AI_CANONNAME
))
70 /* FIXME: Support more flags. */
73 if (hints
&& !validate_family (hints
->ai_family
))
77 hints
->ai_socktype
!= SOCK_STREAM
&& hints
->ai_socktype
!= SOCK_DGRAM
)
78 /* FIXME: Support other socktype. */
79 return EAI_SOCKTYPE
; /* FIXME: Better return code? */
82 /* FIXME: Support server bind mode. */
88 (hints
&& hints
->ai_socktype
== SOCK_DGRAM
) ? "udp" : "tcp";
90 /* FIXME: Use getservbyname_r if available. */
91 se
= getservbyname (servname
, proto
);
97 /* FIXME: Use gethostbyname_r if available. */
98 he
= gethostbyname (nodename
);
99 if (!he
|| he
->h_addr_list
[0] == NULL
)
102 switch (he
->h_addrtype
)
106 sinlen
= sizeof (struct sockaddr_in6
);
112 sinlen
= sizeof (struct sockaddr_in
);
120 tmp
= calloc (1, sizeof (*tmp
) + sinlen
);
124 switch (he
->h_addrtype
)
129 struct sockaddr_in6
*sinp
= (char *) tmp
+ sizeof (*tmp
);
132 sinp
->sin6_port
= se
->s_port
;
134 if (he
->h_length
!= sizeof (sinp
->sin6_addr
))
135 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
137 memcpy (&sinp
->sin6_addr
, he
->h_addr_list
[0], he
->h_length
);
139 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
140 tmp
->ai_addrlen
= sinlen
;
148 struct sockaddr_in
*sinp
= (char *) tmp
+ sizeof (*tmp
);
151 sinp
->sin_port
= se
->s_port
;
153 if (he
->h_length
!= sizeof (sinp
->sin_addr
))
154 return EAI_SYSTEM
; /* FIXME: Better return code? Set errno? */
156 memcpy (&sinp
->sin_addr
, he
->h_addr_list
[0], he
->h_length
);
158 tmp
->ai_addr
= (struct sockaddr
*) sinp
;
159 tmp
->ai_addrlen
= sinlen
;
169 if (hints
&& hints
->ai_flags
& AI_CANONNAME
)
177 tmp
->ai_canonname
= strdup (cn
);
178 if (!tmp
->ai_canonname
)
185 tmp
->ai_protocol
= (hints
) ? hints
->ai_protocol
: 0;
186 tmp
->ai_socktype
= (hints
) ? hints
->ai_socktype
: 0;
187 tmp
->ai_addr
->sa_family
= he
->h_addrtype
;
189 /* FIXME: If more than one address, create linked list of addrinfo's. */
196 /* Free `addrinfo' structure AI including associated storage. */
198 freeaddrinfo (struct addrinfo
*ai
)
202 struct addrinfo
*cur
;
207 if (cur
->ai_canonname
) free (cur
->ai_canonname
);