1 /*****************************************************************************
2 * getaddrinfo.c: getaddrinfo/getnameinfo replacement functions
3 *****************************************************************************
4 * Copyright (C) 2005 VideoLAN
5 * Copyright (C) 2002-2004 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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
27 #include <stddef.h> /* size_t */
28 #include <string.h> /* strncpy(), strlen(), memcpy(), memset(), strchr() */
29 #include <stdlib.h> /* malloc(), free(), strtoul() */
30 #include <sys/types.h>
31 #ifdef HAVE_ARPA_INET_H
32 # include <arpa/inet.h>
34 #ifdef HAVE_NETINET_IN_H
35 # include <netinet/in.h>
39 #if defined( WIN32 ) || defined( UNDER_CE )
40 # if defined(UNDER_CE) && defined(sockaddr_storage)
41 # undef sockaddr_storage
43 # include <winsock2.h>
44 # include <ws2tcpip.h>
46 # include <sys/socket.h>
47 # include <netinet/in.h>
48 # ifdef HAVE_ARPA_INET_H
49 # include <arpa/inet.h>
61 #define NO_ADDRESS NO_DATA
62 #define PF_INET AF_INET
63 #define INADDR_NONE 0xFFFFFFFF
67 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
69 # define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
72 #ifndef HAVE_GAI_STRERROR
77 } const __gai_errlist[] =
80 { EAI_BADFLAGS, "Invalid flag used" },
81 { EAI_NONAME, "Host or service not found" },
82 { EAI_AGAIN, "Temporary name service failure" },
83 { EAI_FAIL, "Non-recoverable name service failure" },
84 { EAI_NODATA, "No data for host name" },
85 { EAI_FAMILY, "Unsupported address family" },
86 { EAI_SOCKTYPE, "Unsupported socket type" },
87 { EAI_SERVICE, "Incompatible service for socket type" },
88 { EAI_ADDRFAMILY, "Unavailable address family for host name" },
89 { EAI_MEMORY, "Memory allocation failure" },
90 { EAI_SYSTEM, "System error" },
94 static const char *__gai_unknownerr = "Unrecognized error number";
96 /****************************************************************************
97 * Converts an EAI_* error code into human readable english text.
98 ****************************************************************************/
99 const char *vlc_gai_strerror( int errnum )
103 for (i = 0; __gai_errlist[i].msg != NULL; i++)
104 if (errnum == __gai_errlist[i].code)
105 return __gai_errlist[i].msg;
107 return __gai_unknownerr;
109 # undef _EAI_POSITIVE_MAX
110 #else /* ifndef HAVE_GAI_STRERROR */
111 const char *vlc_gai_strerror( int errnum )
113 return gai_strerror( errnum );
117 #if !(defined (HAVE_GETNAMEINFO) && defined (HAVE_GETADDRINFO))
119 * Converts the current herrno error value into an EAI_* error code.
120 * That error code is normally returned by getnameinfo() or getaddrinfo().
123 gai_error_from_herrno( void )
131 # if (NO_ADDRESS != NO_DATA)
144 #endif /* if !(HAVE_GETNAMEINFO && HAVE_GETADDRINFO) */
146 #ifndef HAVE_GETNAMEINFO
148 * getnameinfo() non-thread-safe IPv4-only implementation,
149 * Address-family-independant address to hostname translation
150 * (reverse DNS lookup in case of IPv4).
152 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
153 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
156 * GNU C library 2.0.x is known to lack this function, even though it defines
160 __getnameinfo( const struct sockaddr *sa, socklen_t salen,
161 char *host, int hostlen, char *serv, int servlen, int flags )
163 if (((unsigned)salen < sizeof (struct sockaddr_in))
164 || (sa->sa_family != AF_INET))
166 else if (flags & (~_NI_MASK))
170 const struct sockaddr_in *addr;
172 addr = (const struct sockaddr_in *)sa;
178 /* host name resolution */
179 if (!(flags & NI_NUMERICHOST))
181 struct hostent *hent;
183 hent = gethostbyaddr ((const void*)&addr->sin_addr,
188 strncpy (host, hent->h_name, hostlen);
189 host[hostlen - 1] = '\0';
192 * only keep first part of hostname
193 * if user don't want fully qualified
196 if (flags & NI_NOFQDN)
200 ptr = strchr (host, '.');
207 else if (flags & NI_NAMEREQD)
208 return gai_error_from_herrno ();
213 /* inet_ntoa() can't fail */
214 strncpy (host, inet_ntoa (addr->sin_addr), hostlen);
215 host[hostlen - 1] = '\0';
221 struct servent *sent = NULL;
223 #ifndef SYS_BEOS /* No getservbyport() */
226 /* service name resolution */
227 if (!(flags & NI_NUMERICSERV))
230 sent = getservbyport(addr->sin_port,
235 strncpy (serv, sent->s_name, servlen);
236 serv[servlen - 1] = 0;
245 snprintf (serv, servlen, "%u",
246 (unsigned int)ntohs (addr->sin_port));
247 serv[servlen - 1] = '\0';
254 #endif /* if !HAVE_GETNAMEINFO */
257 #ifndef HAVE_GETADDRINFO
259 * This functions must be used to free the memory allocated by getaddrinfo().
262 __freeaddrinfo (struct addrinfo *res)
266 if (res->ai_canonname != NULL)
267 free (res->ai_canonname);
268 if (res->ai_addr != NULL)
270 if (res->ai_next != NULL)
278 * Internal function that builds an addrinfo struct.
280 static struct addrinfo *
281 makeaddrinfo (int af, int type, int proto,
282 const struct sockaddr *addr, size_t addrlen,
283 const char *canonname)
285 struct addrinfo *res;
287 res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
292 res->ai_socktype = type;
293 res->ai_protocol = proto;
294 res->ai_addrlen = addrlen;
295 res->ai_addr = malloc (addrlen);
296 res->ai_canonname = NULL;
299 if (res->ai_addr != NULL)
301 memcpy (res->ai_addr, addr, addrlen);
303 if (canonname != NULL)
305 res->ai_canonname = strdup (canonname);
306 if (res->ai_canonname != NULL)
307 return res; /* success ! */
314 vlc_freeaddrinfo (res);
319 static struct addrinfo *
320 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
322 struct sockaddr_in addr;
324 memset (&addr, 0, sizeof (addr));
325 addr.sin_family = AF_INET;
327 addr.sin_len = sizeof (addr);
329 addr.sin_port = port;
330 addr.sin_addr.s_addr = ip;
332 return makeaddrinfo (PF_INET, type, proto,
333 (struct sockaddr*)&addr, sizeof (addr), name);
338 * getaddrinfo() non-thread-safe IPv4-only implementation
339 * Address-family-independant hostname to address resolution.
341 * This is meant for IPv6-unaware systems that do probably not provide
342 * getaddrinfo(), but still have old function gethostbyname().
344 * Only UDP and TCP over IPv4 are supported here.
347 __getaddrinfo (const char *node, const char *service,
348 const struct addrinfo *hints, struct addrinfo **res)
350 struct addrinfo *info;
353 int protocol = 0, flags = 0;
354 const char *name = NULL;
358 flags = hints->ai_flags;
360 if (flags & ~_AI_MASK)
362 /* only accept AF_INET and AF_UNSPEC */
363 if (hints->ai_family && (hints->ai_family != AF_INET))
366 /* protocol sanity check */
367 switch (hints->ai_socktype)
370 protocol = IPPROTO_TCP;
374 protocol = IPPROTO_UDP;
386 if (hints->ai_protocol && protocol
387 && (protocol != hints->ai_protocol))
396 if (flags & AI_PASSIVE)
397 ip = htonl (INADDR_ANY);
399 ip = htonl (INADDR_LOOPBACK);
402 if ((ip = inet_addr (node)) == INADDR_NONE)
404 struct hostent *entry = NULL;
406 /* hostname resolution */
407 if (!(flags & AI_NUMERICHOST))
408 entry = gethostbyname (node);
413 if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
416 ip = *((u_long *) entry->h_addr);
417 if (flags & AI_CANONNAME)
418 name = entry->h_name;
421 if ((flags & AI_CANONNAME) && (name == NULL))
424 /* service resolution */
432 d = strtoul (service, &end, 0);
433 if (end[0] /* service is not a number */
436 struct servent *entry;
437 const char *protoname;
453 entry = getservbyname (service, protoname);
457 port = entry->s_port;
460 port = htons ((u_short)d);
463 /* building results... */
464 if ((!protocol) || (protocol == IPPROTO_UDP))
466 info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
472 if (flags & AI_PASSIVE)
473 info->ai_flags |= AI_PASSIVE;
476 if ((!protocol) || (protocol == IPPROTO_TCP))
478 info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
484 info->ai_next = *res;
485 if (flags & AI_PASSIVE)
486 info->ai_flags |= AI_PASSIVE;
492 #endif /* if !HAVE_GETADDRINFO */
495 int vlc_getnameinfo( vlc_object_t *p_this, const struct sockaddr *sa, int salen,
496 char *host, int hostlen, char *serv, int servlen, int flags )
500 * Here is the kind of kludge you need to keep binary compatibility among
501 * varying OS versions...
503 typedef int (CALLBACK * GETNAMEINFO) ( const struct sockaddr*, socklen_t,
504 char*, DWORD, char*, DWORD, int );
505 HINSTANCE wship6_module;
506 GETNAMEINFO ws2_getnameinfo;
508 wship6_module = LoadLibrary( "wship6.dll" );
509 if( wship6_module != NULL )
511 ws2_getnameinfo = (GETNAMEINFO)GetProcAddress( wship6_module,
514 if( ws2_getnameinfo != NULL )
518 i_val = ws2_getnameinfo( sa, salen, host, hostlen, serv, servlen,
520 FreeLibrary( wship6_module );
524 FreeLibrary( wship6_module );
528 return getnameinfo( sa, salen, host, hostlen, serv, servlen, flags );
534 /* my getnameinfo implementation is not thread-safe as it uses
535 * gethostbyaddr and the likes */
536 var_Create( p_this->p_libvlc, "getnameinfo_mutex", VLC_VAR_MUTEX );
537 var_Get( p_this->p_libvlc, "getnameinfo_mutex", &lock );
538 vlc_mutex_lock( lock.p_address );
540 i_val = __getnameinfo( sa, salen, host, hostlen, serv, servlen, flags );
541 vlc_mutex_unlock( lock.p_address );
548 /* TODO: support for setting sin6_scope_id */
549 int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
550 const char *service, const struct addrinfo *p_hints,
551 struct addrinfo **res )
553 struct addrinfo hints;
554 char psz_buf[NI_MAXHOST], *psz_node;
556 /* Check if we have to force ipv4 or ipv6 */
557 if( p_hints == NULL )
558 memset( &hints, 0, sizeof( hints ) );
560 memcpy( &hints, p_hints, sizeof( hints ) );
562 if( hints.ai_family == AF_UNSPEC )
566 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
567 var_Get( p_this, "ipv4", &val );
569 hints.ai_family = AF_INET;
571 #ifdef HAVE_INET_PTON
572 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
573 var_Get( p_this, "ipv6", &val );
575 hints.ai_family = AF_INET6;
581 * - accept "" as NULL
582 * - ignore square brackets
584 if( ( node == NULL ) || (node[0] == '\0' ) )
588 strncpy( psz_buf, node, NI_MAXHOST );
589 psz_buf[NI_MAXHOST - 1] = '\0';
593 if( psz_buf[0] == '[' )
597 ptr = strrchr( psz_buf, ']' );
598 if( ( ptr != NULL ) && (ptr[1] == '\0' ) )
606 if( ( service != NULL ) && ( *service == '\0' ) )
607 /* We could put NULL, but you can't have both node and service NULL */
612 typedef int (CALLBACK * GETADDRINFO) ( const char *, const char *,
613 const struct addrinfo *,
614 struct addrinfo ** );
615 HINSTANCE wship6_module;
616 GETADDRINFO ws2_getaddrinfo;
618 wship6_module = LoadLibrary( "wship6.dll" );
619 if( wship6_module != NULL )
621 ws2_getaddrinfo = (GETADDRINFO)GetProcAddress( wship6_module,
624 if( ws2_getaddrinfo != NULL )
628 i_ret = ws2_getaddrinfo( psz_node, service, &hints, res );
629 FreeLibrary( wship6_module ); /* is this wise ? */
633 FreeLibrary( wship6_module );
638 return getaddrinfo( psz_node, service, &hints, res );
645 var_Create( p_this->p_libvlc, "getaddrinfo_mutex", VLC_VAR_MUTEX );
646 var_Get( p_this->p_libvlc, "getaddrinfo_mutex", &lock );
647 vlc_mutex_lock( lock.p_address );
649 i_ret = __getaddrinfo( psz_node, service, &hints, res );
650 vlc_mutex_unlock( lock.p_address );
657 void vlc_freeaddrinfo( struct addrinfo *infos )
660 typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo * );
661 HINSTANCE wship6_module;
662 FREEADDRINFO ws2_freeaddrinfo;
664 wship6_module = LoadLibrary( "wship6.dll" );
665 if( wship6_module != NULL )
667 ws2_freeaddrinfo = (FREEADDRINFO)GetProcAddress( wship6_module,
671 * NOTE: it is assumed that wship6.dll defines either both
672 * getaddrinfo and freeaddrinfo or none of them.
674 if( ws2_freeaddrinfo != NULL )
676 ws2_freeaddrinfo( infos );
677 FreeLibrary( wship6_module );
681 FreeLibrary( wship6_module );
684 #ifdef HAVE_GETADDRINFO
685 freeaddrinfo( infos );
687 __freeaddrinfo( infos );