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
8 * Author: Rémi Denis-Courmont <rem # videolan.org>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
27 #include <stddef.h> /* size_t */
28 #include <string.h> /* strlen(), memcpy(), memset(), strchr() */
29 #include <stdlib.h> /* malloc(), free(), strtoul() */
32 #ifdef HAVE_SYS_TYPES_H
33 # include <sys/types.h>
35 #ifdef HAVE_ARPA_INET_H
36 # include <arpa/inet.h>
38 #ifdef HAVE_NETINET_IN_H
39 # include <netinet/in.h>
45 #include <vlc_network.h>
48 # define NO_ADDRESS NO_DATA
51 # define INADDR_NONE 0xFFFFFFFF
57 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
59 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
62 #ifndef HAVE_GAI_STRERROR
67 } const __gai_errlist[] =
70 { EAI_BADFLAGS, "Invalid flag used" },
71 { EAI_NONAME, "Host or service not found" },
72 { EAI_AGAIN, "Temporary name service failure" },
73 { EAI_FAIL, "Non-recoverable name service failure" },
74 { EAI_NODATA, "No data for host name" },
75 { EAI_FAMILY, "Unsupported address family" },
76 { EAI_SOCKTYPE, "Unsupported socket type" },
77 { EAI_SERVICE, "Incompatible service for socket type" },
78 { EAI_ADDRFAMILY, "Unavailable address family for host name" },
79 { EAI_MEMORY, "Memory allocation failure" },
80 { EAI_OVERFLOW, "Buffer overflow" },
81 { EAI_SYSTEM, "System error" },
85 static const char *__gai_unknownerr = "Unrecognized error number";
87 /****************************************************************************
88 * Converts an EAI_* error code into human readable english text.
89 ****************************************************************************/
90 const char *vlc_gai_strerror (int errnum)
92 for (unsigned i = 0; __gai_errlist[i].msg != NULL; i++)
93 if (errnum == __gai_errlist[i].code)
94 return __gai_errlist[i].msg;
96 return __gai_unknownerr;
98 #else /* ifndef HAVE_GAI_STRERROR */
99 const char *vlc_gai_strerror (int errnum)
101 return gai_strerror (errnum);
105 #ifndef HAVE_GETNAMEINFO
107 * getnameinfo() non-thread-safe IPv4-only implementation,
108 * Address-family-independant address to hostname translation
109 * (reverse DNS lookup in case of IPv4).
111 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
112 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
115 * GNU C library 2.0.x is known to lack this function, even though it defines
119 getnameinfo (const struct sockaddr *sa, socklen_t salen,
120 char *host, int hostlen, char *serv, int servlen, int flags)
122 if (((size_t)salen < sizeof (struct sockaddr_in))
123 || (sa->sa_family != AF_INET))
125 else if (flags & (~_NI_MASK))
129 const struct sockaddr_in *addr;
131 addr = (const struct sockaddr_in *)sa;
135 /* host name resolution */
136 if (!(flags & NI_NUMERICHOST))
138 if (flags & NI_NAMEREQD)
142 /* inet_ntoa() is not thread-safe, do not use it */
143 uint32_t ipv4 = ntohl (addr->sin_addr.s_addr);
145 if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24,
146 (ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff,
147 ipv4 & 0xff) >= hostlen)
153 if (snprintf (serv, servlen, "%u",
154 (unsigned int)ntohs (addr->sin_port)) >= servlen)
161 #endif /* if !HAVE_GETNAMEINFO */
163 #ifndef HAVE_GETADDRINFO
165 * Converts the current herrno error value into an EAI_* error code.
166 * That error code is normally returned by getnameinfo() or getaddrinfo().
169 gai_error_from_herrno (void)
177 # if (NO_ADDRESS != NO_DATA)
192 * This functions must be used to free the memory allocated by getaddrinfo().
194 static void freeaddrinfo (struct addrinfo *res)
198 if (res->ai_canonname != NULL)
199 free (res->ai_canonname);
200 if (res->ai_addr != NULL)
202 if (res->ai_next != NULL)
210 * Internal function that builds an addrinfo struct.
212 static struct addrinfo *
213 makeaddrinfo (int af, int type, int proto,
214 const struct sockaddr *addr, size_t addrlen,
215 const char *canonname)
217 struct addrinfo *res;
219 res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
224 res->ai_socktype = type;
225 res->ai_protocol = proto;
226 res->ai_addrlen = addrlen;
227 res->ai_addr = malloc (addrlen);
228 res->ai_canonname = NULL;
231 if (res->ai_addr != NULL)
233 memcpy (res->ai_addr, addr, addrlen);
235 if (canonname != NULL)
237 res->ai_canonname = strdup (canonname);
238 if (res->ai_canonname != NULL)
239 return res; /* success ! */
246 vlc_freeaddrinfo (res);
251 static struct addrinfo *
252 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
254 struct sockaddr_in addr;
256 memset (&addr, 0, sizeof (addr));
257 addr.sin_family = AF_INET;
259 addr.sin_len = sizeof (addr);
261 addr.sin_port = port;
262 addr.sin_addr.s_addr = ip;
264 return makeaddrinfo (AF_INET, type, proto,
265 (struct sockaddr*)&addr, sizeof (addr), name);
270 * getaddrinfo() non-thread-safe IPv4-only implementation
271 * Address-family-independant hostname to address resolution.
273 * This is meant for IPv6-unaware systems that do probably not provide
274 * getaddrinfo(), but still have old function gethostbyname().
276 * Only UDP and TCP over IPv4 are supported here.
279 getaddrinfo (const char *node, const char *service,
280 const struct addrinfo *hints, struct addrinfo **res)
282 struct addrinfo *info;
285 int protocol = 0, flags = 0;
286 const char *name = NULL;
290 * Maybe you knew already that Winsock does not handle TCP/RST packets
291 * properly, so that when a TCP connection fails, it will wait until it
292 * times out even if the remote host did return a TCP/RST. However, it
293 * still sees the TCP/RST as the error code is 10061 instead of 10060.
294 * Basically, we have the stupid brainfucked behavior with DNS queries...
295 * When the recursive DNS server returns an error, Winsock waits about
296 * 2 seconds before it returns to the callers, even though it should know
297 * that is pointless. I'd like to know how come this hasn't been fixed
298 * for the past decade, or maybe not.
300 * Anyway, this is causing a severe delay when the SAP listener tries
301 * to resolve more than ten IPv6 numeric addresses. Modern systems will
302 * eventually realize that it is an IPv6 address, and won't try to resolve
303 * it as a IPv4 address via the Domain Name Service. Old systems
304 * (including Windows XP without the IPv6 stack) will not. It is normally
305 * not an issue as the DNS server usually returns an error very quickly.
306 * But it IS a severe issue on Windows, given the bug explained above.
307 * So here comes one more bug-to-bug Windows compatibility fix.
309 if ((node != NULL) && (strchr (node, ':') != NULL))
315 flags = hints->ai_flags;
317 if (flags & ~_AI_MASK)
319 /* only accept AF_INET and AF_UNSPEC */
320 if (hints->ai_family && (hints->ai_family != AF_INET))
323 /* protocol sanity check */
324 switch (hints->ai_socktype)
327 protocol = IPPROTO_TCP;
331 protocol = IPPROTO_UDP;
343 if (hints->ai_protocol && protocol
344 && (protocol != hints->ai_protocol))
353 if (flags & AI_PASSIVE)
354 ip = htonl (INADDR_ANY);
356 ip = htonl (INADDR_LOOPBACK);
359 if ((ip = inet_addr (node)) == INADDR_NONE)
361 struct hostent *entry = NULL;
363 /* hostname resolution */
364 if (!(flags & AI_NUMERICHOST))
365 entry = gethostbyname (node);
368 return gai_error_from_herrno ();
370 if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
373 ip = *((u_long *) entry->h_addr);
374 if (flags & AI_CANONNAME)
375 name = entry->h_name;
378 if ((flags & AI_CANONNAME) && (name == NULL))
381 /* service resolution */
389 d = strtoul (service, &end, 0);
390 if (end[0] /* service is not a number */
393 struct servent *entry;
394 const char *protoname;
410 entry = getservbyname (service, protoname);
414 port = entry->s_port;
417 port = htons ((u_short)d);
420 /* building results... */
421 if ((!protocol) || (protocol == IPPROTO_UDP))
423 info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
429 if (flags & AI_PASSIVE)
430 info->ai_flags |= AI_PASSIVE;
433 if ((!protocol) || (protocol == IPPROTO_TCP))
435 info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
441 info->ai_next = *res;
442 if (flags & AI_PASSIVE)
443 info->ai_flags |= AI_PASSIVE;
449 #endif /* if !HAVE_GETADDRINFO */
451 #if defined( WIN32 ) && !defined( UNDER_CE )
453 * Here is the kind of kludge you need to keep binary compatibility among
454 * varying OS versions...
456 typedef int (WSAAPI * GETNAMEINFO) ( const struct sockaddr FAR *, socklen_t,
457 char FAR *, DWORD, char FAR *, DWORD, int );
458 typedef int (WSAAPI * GETADDRINFO) (const char FAR *, const char FAR *,
459 const struct addrinfo FAR *,
460 struct addrinfo FAR * FAR *);
462 typedef void (WSAAPI * FREEADDRINFO) ( struct addrinfo FAR * );
464 static int WSAAPI _ws2_getnameinfo_bind ( const struct sockaddr FAR *, socklen_t,
465 char FAR *, DWORD, char FAR *, DWORD, int );
466 static int WSAAPI _ws2_getaddrinfo_bind (const char FAR *, const char FAR *,
467 const struct addrinfo FAR *,
468 struct addrinfo FAR * FAR *);
470 static GETNAMEINFO ws2_getnameinfo = _ws2_getnameinfo_bind;
471 static GETADDRINFO ws2_getaddrinfo = _ws2_getaddrinfo_bind;
472 static FREEADDRINFO ws2_freeaddrinfo;
474 static FARPROC ws2_find_api (LPCTSTR name)
478 HMODULE m = GetModuleHandle (TEXT("WS2_32"));
480 f = GetProcAddress (m, name);
484 /* Windows 2K IPv6 preview */
485 m = LoadLibrary (TEXT("WSHIP6"));
487 f = GetProcAddress (m, name);
493 static WSAAPI int _ws2_getnameinfo_bind( const struct sockaddr FAR * sa, socklen_t salen,
494 char FAR *host, DWORD hostlen, char FAR *serv, DWORD servlen, int flags )
496 GETNAMEINFO entry = (GETNAMEINFO)ws2_find_api (TEXT("getnameinfo"));
499 /* call API before replacing function pointer to avoid crash */
500 int result = entry (sa, salen, host, hostlen, serv, servlen, flags);
501 ws2_getnameinfo = entry;
504 return getnameinfo (sa, salen, host, hostlen, serv, servlen, flags);
507 #define getnameinfo ws2_getnameinfo
509 /* So much for using different calling conventions */
510 static WSAAPI void call_freeaddrinfo (struct addrinfo *infos)
512 freeaddrinfo (infos);
515 static WSAAPI int _ws2_getaddrinfo_bind(const char FAR *node, const char FAR *service,
516 const struct addrinfo FAR *hints, struct addrinfo FAR * FAR *res)
519 FREEADDRINFO freentry;
521 entry = (GETADDRINFO)ws2_find_api (TEXT("getaddrinfo"));
522 freentry = (FREEADDRINFO)ws2_find_api (TEXT("freeaddrinfo"));
524 if ((entry != NULL) && (freentry != NULL))
526 /* call API before replacing function pointer to avoid crash */
527 int result = entry (node, service, hints, res);
528 ws2_freeaddrinfo = freentry;
529 ws2_getaddrinfo = entry;
532 ws2_freeaddrinfo = call_freeaddrinfo;
533 return getaddrinfo (node, service, hints, res);
537 #define getaddrinfo ws2_getaddrinfo
538 #define freeaddrinfo ws2_freeaddrinfo
539 #define HAVE_GETADDRINFO
544 int vlc_getnameinfo( const struct sockaddr *sa, int salen,
545 char *host, int hostlen, int *portnum, int flags )
547 char psz_servbuf[6], *psz_serv;
548 int i_servlen, i_val;
550 flags |= NI_NUMERICSERV;
551 if( portnum != NULL )
553 psz_serv = psz_servbuf;
554 i_servlen = sizeof( psz_servbuf );
562 i_val = getnameinfo(sa, salen, host, hostlen, psz_serv, i_servlen, flags);
564 if( portnum != NULL )
565 *portnum = atoi( psz_serv );
571 int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
572 int i_port, const struct addrinfo *p_hints,
573 struct addrinfo **res )
575 struct addrinfo hints;
576 char psz_buf[NI_MAXHOST], *psz_node, psz_service[6];
579 * In VLC, we always use port number as integer rather than strings
580 * for historical reasons (and portability).
582 if( ( i_port > 65535 ) || ( i_port < 0 ) )
584 msg_Err( p_this, "invalid port number %d specified", i_port );
588 /* cannot overflow */
589 snprintf( psz_service, 6, "%d", i_port );
591 /* Check if we have to force ipv4 or ipv6 */
592 memset (&hints, 0, sizeof (hints));
595 hints.ai_family = p_hints->ai_family;
596 hints.ai_socktype = p_hints->ai_socktype;
597 hints.ai_protocol = p_hints->ai_protocol;
598 hints.ai_flags = p_hints->ai_flags & (AI_NUMERICHOST|AI_PASSIVE);
600 /* we only ever use port *numbers* */
601 hints.ai_flags |= AI_NUMERICSERV;
603 if( hints.ai_family == AF_UNSPEC )
607 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
608 var_Get( p_this, "ipv4", &val );
610 hints.ai_family = AF_INET;
613 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
614 var_Get( p_this, "ipv6", &val );
616 hints.ai_family = AF_INET6;
622 * - accept "" as NULL
623 * - ignore square brackets
625 if( ( node == NULL ) || (node[0] == '\0' ) )
631 strlcpy( psz_buf, node, NI_MAXHOST );
635 if( psz_buf[0] == '[' )
639 ptr = strrchr( psz_buf, ']' );
640 if( ( ptr != NULL ) && (ptr[1] == '\0' ) )
650 * Winsock tries to resolve numerical IPv4 addresses as AAAA
651 * and IPv6 addresses as A... There comes the bug-to-bug fix.
653 if ((hints.ai_flags & AI_NUMERICHOST) == 0)
655 hints.ai_flags |= AI_NUMERICHOST;
657 if (getaddrinfo (psz_node, psz_service, &hints, res) == 0)
660 hints.ai_flags &= ~AI_NUMERICHOST;
663 #if defined (HAVE_GETADDRINFO)
665 /* Run-time I18n Domain Names support */
666 static vlc_bool_t b_idn = VLC_TRUE; /* beware of thread-safety */
670 hints.ai_flags |= AI_IDN;
671 int ret = getaddrinfo (psz_node, psz_service, &hints, res);
673 if (ret != EAI_BADFLAGS)
676 /* IDN not available: disable and retry without it */
677 hints.ai_flags &= ~AI_IDN;
679 msg_Info (p_this, "International Domain Names not supported");
682 return getaddrinfo (psz_node, psz_service, &hints, res);
687 var_Create (p_this->p_libvlc, "getaddrinfo_mutex", VLC_VAR_MUTEX);
688 var_Get (p_this->p_libvlc, "getaddrinfo_mutex", &lock);
689 vlc_mutex_lock (lock.p_address);
691 ret = getaddrinfo (psz_node, psz_service, &hints, res);
692 vlc_mutex_unlock (lock.p_address);
698 void vlc_freeaddrinfo( struct addrinfo *infos )
700 freeaddrinfo (infos);