1 /*****************************************************************************
2 * getaddrinfo.c: getaddrinfo/getnameinfo replacement functions
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
5 * Copyright (C) 2002-2007 Rémi Denis-Courmont
6 * Copyright (C) 2011 KO Myung-Hun
8 * Authors: KO Myung-Hun <komh@chollian.net>
9 * Rémi Denis-Courmont <rem # videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation; either version 2.1 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24 *****************************************************************************/
30 #include <vlc_common.h>
31 #include <vlc_network.h>
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
37 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
40 * getnameinfo() non-thread-safe IPv4-only implementation,
41 * Address-family-independent address to hostname translation
42 * (reverse DNS lookup in case of IPv4).
44 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
45 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
49 getnameinfo (const struct sockaddr
*sa
, socklen_t salen
,
50 char *host
, int hostlen
, char *serv
, int servlen
, int flags
)
52 if (((size_t)salen
< sizeof (struct sockaddr_in
))
53 || (sa
->sa_family
!= AF_INET
))
55 else if (flags
& (~_NI_MASK
))
59 const struct sockaddr_in
*addr
;
61 addr
= (const struct sockaddr_in
*)sa
;
65 /* host name resolution */
66 if (!(flags
& NI_NUMERICHOST
))
68 if (flags
& NI_NAMEREQD
)
72 /* inet_ntoa() is not thread-safe, do not use it */
73 uint32_t ipv4
= ntohl (addr
->sin_addr
.s_addr
);
75 if (snprintf (host
, hostlen
, "%u.%u.%u.%u", ipv4
>> 24,
76 (ipv4
>> 16) & 0xff, (ipv4
>> 8) & 0xff,
77 ipv4
& 0xff) >= (int)hostlen
)
83 if (snprintf (serv
, servlen
, "%u",
84 (unsigned int)ntohs (addr
->sin_port
)) >= (int)servlen
)
91 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
93 * Converts the current herrno error value into an EAI_* error code.
94 * That error code is normally returned by getnameinfo() or getaddrinfo().
97 gai_error_from_herrno (void)
105 # if (NO_ADDRESS != NO_DATA)
120 * Internal function that builds an addrinfo struct.
122 static struct addrinfo
*
123 makeaddrinfo (int af
, int type
, int proto
,
124 const struct sockaddr
*addr
, size_t addrlen
,
125 const char *canonname
)
127 struct addrinfo
*res
;
129 res
= (struct addrinfo
*)malloc (sizeof (struct addrinfo
));
134 res
->ai_socktype
= type
;
135 res
->ai_protocol
= proto
;
136 res
->ai_addrlen
= addrlen
;
137 res
->ai_addr
= malloc (addrlen
);
138 res
->ai_canonname
= NULL
;
141 if (res
->ai_addr
!= NULL
)
143 memcpy (res
->ai_addr
, addr
, addrlen
);
145 if (canonname
!= NULL
)
147 res
->ai_canonname
= strdup (canonname
);
148 if (res
->ai_canonname
!= NULL
)
149 return res
; /* success ! */
160 static struct addrinfo
*
161 makeipv4info (int type
, int proto
, u_long ip
, u_short port
, const char *name
)
163 struct sockaddr_in addr
;
165 memset (&addr
, 0, sizeof (addr
));
166 addr
.sin_family
= AF_INET
;
168 addr
.sin_len
= sizeof (addr
);
170 addr
.sin_port
= port
;
171 addr
.sin_addr
.s_addr
= ip
;
173 return makeaddrinfo (AF_INET
, type
, proto
,
174 (struct sockaddr
*)&addr
, sizeof (addr
), name
);
178 * getaddrinfo() non-thread-safe IPv4-only implementation
179 * Address-family-independent hostname to address resolution.
181 * This is meant for IPv6-unaware systems that do probably not provide
182 * getaddrinfo(), but still have old function gethostbyname().
184 * Only UDP and TCP over IPv4 are supported here.
187 getaddrinfo (const char *node
, const char *service
,
188 const struct addrinfo
*hints
, struct addrinfo
**res
)
190 struct addrinfo
*info
;
193 int protocol
= 0, flags
= 0;
194 const char *name
= NULL
;
198 flags
= hints
->ai_flags
;
200 if (flags
& ~_AI_MASK
)
202 /* only accept AF_INET and AF_UNSPEC */
203 if (hints
->ai_family
&& (hints
->ai_family
!= AF_INET
))
206 /* protocol sanity check */
207 switch (hints
->ai_socktype
)
210 protocol
= IPPROTO_TCP
;
214 protocol
= IPPROTO_UDP
;
226 if (hints
->ai_protocol
&& protocol
227 && (protocol
!= hints
->ai_protocol
))
236 if (flags
& AI_PASSIVE
)
237 ip
= htonl (INADDR_ANY
);
239 ip
= htonl (INADDR_LOOPBACK
);
242 if ((ip
= inet_addr (node
)) == INADDR_NONE
)
244 struct hostent
*entry
= NULL
;
246 /* hostname resolution */
247 if (!(flags
& AI_NUMERICHOST
))
248 entry
= gethostbyname (node
);
251 return gai_error_from_herrno ();
253 if ((entry
->h_length
!= 4) || (entry
->h_addrtype
!= AF_INET
))
256 ip
= *((u_long
*) entry
->h_addr
);
257 if (flags
& AI_CANONNAME
)
258 name
= entry
->h_name
;
261 if ((flags
& AI_CANONNAME
) && (name
== NULL
))
264 /* service resolution */
272 d
= strtoul (service
, &end
, 0);
273 if (end
[0] || (d
> 65535u))
276 port
= htons ((u_short
)d
);
279 /* building results... */
280 if ((!protocol
) || (protocol
== IPPROTO_UDP
))
282 info
= makeipv4info (SOCK_DGRAM
, IPPROTO_UDP
, ip
, port
, name
);
288 if (flags
& AI_PASSIVE
)
289 info
->ai_flags
|= AI_PASSIVE
;
292 if ((!protocol
) || (protocol
== IPPROTO_TCP
))
294 info
= makeipv4info (SOCK_STREAM
, IPPROTO_TCP
, ip
, port
, name
);
300 info
->ai_next
= *res
;
301 if (flags
& AI_PASSIVE
)
302 info
->ai_flags
|= AI_PASSIVE
;