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 #include <arpa/inet.h>
35 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
38 * getnameinfo() non-thread-safe IPv4-only implementation,
39 * Address-family-independent address to hostname translation
40 * (reverse DNS lookup in case of IPv4).
42 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
43 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
47 getnameinfo (const struct sockaddr
*sa
, socklen_t salen
,
48 char *host
, int hostlen
, char *serv
, int servlen
, int flags
)
50 if (((size_t)salen
< sizeof (struct sockaddr_in
))
51 || (sa
->sa_family
!= AF_INET
))
53 else if (flags
& (~_NI_MASK
))
57 const struct sockaddr_in
*addr
;
59 addr
= (const struct sockaddr_in
*)sa
;
63 /* host name resolution */
64 if (!(flags
& NI_NUMERICHOST
))
66 if (flags
& NI_NAMEREQD
)
70 /* inet_ntoa() is not thread-safe, do not use it */
71 uint32_t ipv4
= ntohl (addr
->sin_addr
.s_addr
);
73 if (snprintf (host
, hostlen
, "%u.%u.%u.%u", ipv4
>> 24,
74 (ipv4
>> 16) & 0xff, (ipv4
>> 8) & 0xff,
75 ipv4
& 0xff) >= (int)hostlen
)
81 if (snprintf (serv
, servlen
, "%u",
82 (unsigned int)ntohs (addr
->sin_port
)) >= (int)servlen
)
89 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
91 * Converts the current herrno error value into an EAI_* error code.
92 * That error code is normally returned by getnameinfo() or getaddrinfo().
95 gai_error_from_herrno (void)
103 # if (NO_ADDRESS != NO_DATA)
118 * Internal function that builds an addrinfo struct.
120 static struct addrinfo
*
121 makeaddrinfo (int af
, int type
, int proto
,
122 const struct sockaddr
*addr
, size_t addrlen
,
123 const char *canonname
)
125 struct addrinfo
*res
;
127 res
= (struct addrinfo
*)malloc (sizeof (struct addrinfo
));
132 res
->ai_socktype
= type
;
133 res
->ai_protocol
= proto
;
134 res
->ai_addrlen
= addrlen
;
135 res
->ai_addr
= malloc (addrlen
);
136 res
->ai_canonname
= NULL
;
139 if (res
->ai_addr
!= NULL
)
141 memcpy (res
->ai_addr
, addr
, addrlen
);
143 if (canonname
!= NULL
)
145 res
->ai_canonname
= strdup (canonname
);
146 if (res
->ai_canonname
!= NULL
)
147 return res
; /* success ! */
158 static struct addrinfo
*
159 makeipv4info (int type
, int proto
, u_long ip
, u_short port
, const char *name
)
161 struct sockaddr_in addr
;
163 memset (&addr
, 0, sizeof (addr
));
164 addr
.sin_family
= AF_INET
;
166 addr
.sin_len
= sizeof (addr
);
168 addr
.sin_port
= port
;
169 addr
.sin_addr
.s_addr
= ip
;
171 return makeaddrinfo (AF_INET
, type
, proto
,
172 (struct sockaddr
*)&addr
, sizeof (addr
), name
);
176 * getaddrinfo() non-thread-safe IPv4-only implementation
177 * Address-family-independent hostname to address resolution.
179 * This is meant for IPv6-unaware systems that do probably not provide
180 * getaddrinfo(), but still have old function gethostbyname().
182 * Only UDP and TCP over IPv4 are supported here.
185 getaddrinfo (const char *node
, const char *service
,
186 const struct addrinfo
*hints
, struct addrinfo
**res
)
188 struct addrinfo
*info
;
191 int protocol
= 0, flags
= 0;
192 const char *name
= NULL
;
196 flags
= hints
->ai_flags
;
198 if (flags
& ~_AI_MASK
)
200 /* only accept AF_INET and AF_UNSPEC */
201 if (hints
->ai_family
&& (hints
->ai_family
!= AF_INET
))
204 /* protocol sanity check */
205 switch (hints
->ai_socktype
)
208 protocol
= IPPROTO_TCP
;
212 protocol
= IPPROTO_UDP
;
224 if (hints
->ai_protocol
&& protocol
225 && (protocol
!= hints
->ai_protocol
))
234 if (flags
& AI_PASSIVE
)
235 ip
= htonl (INADDR_ANY
);
237 ip
= htonl (INADDR_LOOPBACK
);
240 if ((ip
= inet_addr (node
)) == INADDR_NONE
)
242 struct hostent
*entry
= NULL
;
244 /* hostname resolution */
245 if (!(flags
& AI_NUMERICHOST
))
246 entry
= gethostbyname (node
);
249 return gai_error_from_herrno ();
251 if ((entry
->h_length
!= 4) || (entry
->h_addrtype
!= AF_INET
))
254 ip
= *((u_long
*) entry
->h_addr
);
255 if (flags
& AI_CANONNAME
)
256 name
= entry
->h_name
;
259 if ((flags
& AI_CANONNAME
) && (name
== NULL
))
262 /* service resolution */
270 d
= strtoul (service
, &end
, 0);
271 if (end
[0] || (d
> 65535u))
274 port
= htons ((u_short
)d
);
277 /* building results... */
278 if ((!protocol
) || (protocol
== IPPROTO_UDP
))
280 info
= makeipv4info (SOCK_DGRAM
, IPPROTO_UDP
, ip
, port
, name
);
286 if (flags
& AI_PASSIVE
)
287 info
->ai_flags
|= AI_PASSIVE
;
290 if ((!protocol
) || (protocol
== IPPROTO_TCP
))
292 info
= makeipv4info (SOCK_STREAM
, IPPROTO_TCP
, ip
, port
, name
);
298 info
->ai_next
= *res
;
299 if (flags
& AI_PASSIVE
)
300 info
->ai_flags
|= AI_PASSIVE
;