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 INADDR_NONE 0xFFFFFFFF
66 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
68 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
71 #ifndef HAVE_GAI_STRERROR
76 } const __gai_errlist[] =
79 { EAI_BADFLAGS, "Invalid flag used" },
80 { EAI_NONAME, "Host or service not found" },
81 { EAI_AGAIN, "Temporary name service failure" },
82 { EAI_FAIL, "Non-recoverable name service failure" },
83 { EAI_NODATA, "No data for host name" },
84 { EAI_FAMILY, "Unsupported address family" },
85 { EAI_SOCKTYPE, "Unsupported socket type" },
86 { EAI_SERVICE, "Incompatible service for socket type" },
87 { EAI_ADDRFAMILY, "Unavailable address family for host name" },
88 { EAI_MEMORY, "Memory allocation failure" },
89 { EAI_SYSTEM, "System error" },
93 static const char *__gai_unknownerr = "Unrecognized error number";
95 /****************************************************************************
96 * Converts an EAI_* error code into human readable english text.
97 ****************************************************************************/
98 const char *vlc_gai_strerror( int errnum )
102 for (i = 0; __gai_errlist[i].msg != NULL; i++)
103 if (errnum == __gai_errlist[i].code)
104 return __gai_errlist[i].msg;
106 return __gai_unknownerr;
108 # undef _EAI_POSITIVE_MAX
109 #else /* ifndef HAVE_GAI_STRERROR */
110 const char *vlc_gai_strerror( int errnum )
112 return gai_strerror( errnum );
116 #if !(defined (HAVE_GETNAMEINFO) && defined (HAVE_GETADDRINFO))
118 * Converts the current herrno error value into an EAI_* error code.
119 * That error code is normally returned by getnameinfo() or getaddrinfo().
122 gai_error_from_herrno( void )
130 # if (NO_ADDRESS != NO_DATA)
143 #endif /* if !(HAVE_GETNAMEINFO && HAVE_GETADDRINFO) */
145 #ifndef HAVE_GETNAMEINFO
147 * getnameinfo() non-thread-safe IPv4-only implementation,
148 * Address-family-independant address to hostname translation
149 * (reverse DNS lookup in case of IPv4).
151 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
152 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
155 * GNU C library 2.0.x is known to lack this function, even though it defines
159 __getnameinfo( const struct sockaddr *sa, socklen_t salen,
160 char *host, int hostlen, char *serv, int servlen, int flags )
162 if (((unsigned)salen < sizeof (struct sockaddr_in))
163 || (sa->sa_family != AF_INET))
165 else if (flags & (~_NI_MASK))
169 const struct sockaddr_in *addr;
171 addr = (const struct sockaddr_in *)sa;
177 /* host name resolution */
178 if (!(flags & NI_NUMERICHOST))
180 struct hostent *hent;
182 hent = gethostbyaddr ((const void*)&addr->sin_addr,
187 strncpy (host, hent->h_name, hostlen);
188 host[hostlen - 1] = '\0';
191 * only keep first part of hostname
192 * if user don't want fully qualified
195 if (flags & NI_NOFQDN)
199 ptr = strchr (host, '.');
206 else if (flags & NI_NAMEREQD)
207 return gai_error_from_herrno ();
212 /* inet_ntoa() can't fail */
213 strncpy (host, inet_ntoa (addr->sin_addr), hostlen);
214 host[hostlen - 1] = '\0';
220 struct servent *sent = NULL;
222 #ifndef SYS_BEOS /* No getservbyport() */
225 /* service name resolution */
226 if (!(flags & NI_NUMERICSERV))
229 sent = getservbyport(addr->sin_port,
234 strncpy (serv, sent->s_name, servlen);
235 serv[servlen - 1] = 0;
244 snprintf (serv, servlen, "%u",
245 (unsigned int)ntohs (addr->sin_port));
246 serv[servlen - 1] = '\0';
253 #endif /* if !HAVE_GETNAMEINFO */
256 #ifndef HAVE_GETADDRINFO
258 * This functions must be used to free the memory allocated by getaddrinfo().
261 __freeaddrinfo (struct addrinfo *res)
265 if (res->ai_canonname != NULL)
266 free (res->ai_canonname);
267 if (res->ai_addr != NULL)
269 if (res->ai_next != NULL)
277 * Internal function that builds an addrinfo struct.
279 static struct addrinfo *
280 makeaddrinfo (int af, int type, int proto,
281 const struct sockaddr *addr, size_t addrlen,
282 const char *canonname)
284 struct addrinfo *res;
286 res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
291 res->ai_socktype = type;
292 res->ai_protocol = proto;
293 res->ai_addrlen = addrlen;
294 res->ai_addr = malloc (addrlen);
295 res->ai_canonname = NULL;
298 if (res->ai_addr != NULL)
300 memcpy (res->ai_addr, addr, addrlen);
302 if (canonname != NULL)
304 res->ai_canonname = strdup (canonname);
305 if (res->ai_canonname != NULL)
306 return res; /* success ! */
313 vlc_freeaddrinfo (res);
318 static struct addrinfo *
319 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
321 struct sockaddr_in addr;
323 memset (&addr, 0, sizeof (addr));
324 addr.sin_family = AF_INET;
326 addr.sin_len = sizeof (addr);
328 addr.sin_port = port;
329 addr.sin_addr.s_addr = ip;
331 return makeaddrinfo (AF_INET, type, proto,
332 (struct sockaddr*)&addr, sizeof (addr), name);
337 * getaddrinfo() non-thread-safe IPv4-only implementation
338 * Address-family-independant hostname to address resolution.
340 * This is meant for IPv6-unaware systems that do probably not provide
341 * getaddrinfo(), but still have old function gethostbyname().
343 * Only UDP and TCP over IPv4 are supported here.
346 __getaddrinfo (const char *node, const char *service,
347 const struct addrinfo *hints, struct addrinfo **res)
349 struct addrinfo *info;
352 int protocol = 0, flags = 0;
353 const char *name = NULL;
357 flags = hints->ai_flags;
359 if (flags & ~_AI_MASK)
361 /* only accept AF_INET and AF_UNSPEC */
362 if (hints->ai_family && (hints->ai_family != AF_INET))
365 /* protocol sanity check */
366 switch (hints->ai_socktype)
369 protocol = IPPROTO_TCP;
373 protocol = IPPROTO_UDP;
385 if (hints->ai_protocol && protocol
386 && (protocol != hints->ai_protocol))
395 if (flags & AI_PASSIVE)
396 ip = htonl (INADDR_ANY);
398 ip = htonl (INADDR_LOOPBACK);
401 if ((ip = inet_addr (node)) == INADDR_NONE)
403 struct hostent *entry = NULL;
405 /* hostname resolution */
406 if (!(flags & AI_NUMERICHOST))
407 entry = gethostbyname (node);
412 if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
415 ip = *((u_long *) entry->h_addr);
416 if (flags & AI_CANONNAME)
417 name = entry->h_name;
420 if ((flags & AI_CANONNAME) && (name == NULL))
423 /* service resolution */
431 d = strtoul (service, &end, 0);
432 if (end[0] /* service is not a number */
435 struct servent *entry;
436 const char *protoname;
452 entry = getservbyname (service, protoname);
456 port = entry->s_port;
459 port = htons ((u_short)d);
462 /* building results... */
463 if ((!protocol) || (protocol == IPPROTO_UDP))
465 info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
471 if (flags & AI_PASSIVE)
472 info->ai_flags |= AI_PASSIVE;
475 if ((!protocol) || (protocol == IPPROTO_TCP))
477 info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
483 info->ai_next = *res;
484 if (flags & AI_PASSIVE)
485 info->ai_flags |= AI_PASSIVE;
491 #endif /* if !HAVE_GETADDRINFO */
494 int vlc_getnameinfo( const struct sockaddr *sa, int salen,
495 char *host, int hostlen, int *portnum, int flags )
497 char psz_servbuf[6], *psz_serv;
498 int i_servlen, i_val;
500 flags |= NI_NUMERICSERV;
501 if( portnum != NULL )
503 psz_serv = psz_servbuf;
504 i_servlen = sizeof( psz_servbuf );
513 * Here is the kind of kludge you need to keep binary compatibility among
514 * varying OS versions...
516 typedef int (CALLBACK * GETNAMEINFO) ( const struct sockaddr*, socklen_t,
517 char*, DWORD, char*, DWORD, int );
518 HINSTANCE wship6_module;
519 GETNAMEINFO ws2_getnameinfo;
521 wship6_module = LoadLibrary( "wship6.dll" );
522 if( wship6_module != NULL )
524 ws2_getnameinfo = (GETNAMEINFO)GetProcAddress( wship6_module,
527 if( ws2_getnameinfo != NULL )
529 i_val = ws2_getnameinfo( sa, salen, host, hostlen, psz_serv,
531 FreeLibrary( wship6_module );
533 if( portnum != NULL )
534 *portnum = atoi( psz_serv );
538 FreeLibrary( wship6_module );
542 i_val = getnameinfo( sa, salen, host, hostlen, psz_serv, i_servlen,
546 # ifdef HAVE_USABLE_MUTEX_THAT_DONT_NEED_LIBVLC_POINTER
547 static vlc_value_t lock;
549 /* my getnameinfo implementation is not thread-safe as it uses
550 * gethostbyaddr and the likes */
551 vlc_mutex_lock( lock.p_address );
553 # warning FIXME : This is not thread-safe!
555 i_val = __getnameinfo( sa, salen, host, hostlen, psz_serv, i_servlen,
557 # ifdef HAVE_USABLE_MUTEX_THAT_DONT_NEED_LIBVLC_POINTER
558 vlc_mutex_unlock( lock.p_address );
563 if( portnum != NULL )
564 *portnum = atoi( psz_serv );
570 /* TODO: support for setting sin6_scope_id */
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 if( p_hints == NULL )
593 memset( &hints, 0, sizeof( hints ) );
595 memcpy( &hints, p_hints, sizeof( hints ) );
597 if( hints.ai_family == AF_UNSPEC )
601 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
602 var_Get( p_this, "ipv4", &val );
604 hints.ai_family = AF_INET;
607 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
608 var_Get( p_this, "ipv6", &val );
610 hints.ai_family = AF_INET6;
616 * - accept "" as NULL
617 * - ignore square brackets
619 if( ( node == NULL ) || (node[0] == '\0' ) )
625 strncpy( psz_buf, node, NI_MAXHOST );
626 psz_buf[NI_MAXHOST - 1] = '\0';
630 if( psz_buf[0] == '[' )
634 ptr = strrchr( psz_buf, ']' );
635 if( ( ptr != NULL ) && (ptr[1] == '\0' ) )
645 typedef int (CALLBACK * GETADDRINFO) ( const char *, const char *,
646 const struct addrinfo *,
647 struct addrinfo ** );
648 HINSTANCE wship6_module;
649 GETADDRINFO ws2_getaddrinfo;
651 wship6_module = LoadLibrary( "wship6.dll" );
652 if( wship6_module != NULL )
654 ws2_getaddrinfo = (GETADDRINFO)GetProcAddress( wship6_module,
657 if( ws2_getaddrinfo != NULL )
661 i_ret = ws2_getaddrinfo( psz_node, psz_service, &hints, res );
662 FreeLibrary( wship6_module ); /* is this wise ? */
666 FreeLibrary( wship6_module );
671 return getaddrinfo( psz_node, psz_service, &hints, res );
678 var_Create( p_this->p_libvlc, "getaddrinfo_mutex", VLC_VAR_MUTEX );
679 var_Get( p_this->p_libvlc, "getaddrinfo_mutex", &lock );
680 vlc_mutex_lock( lock.p_address );
682 i_ret = __getaddrinfo( psz_node, psz_service, &hints, res );
683 vlc_mutex_unlock( lock.p_address );
690 void vlc_freeaddrinfo( struct addrinfo *infos )
693 typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo * );
694 HINSTANCE wship6_module;
695 FREEADDRINFO ws2_freeaddrinfo;
697 wship6_module = LoadLibrary( "wship6.dll" );
698 if( wship6_module != NULL )
700 ws2_freeaddrinfo = (FREEADDRINFO)GetProcAddress( wship6_module,
704 * NOTE: it is assumed that wship6.dll defines either both
705 * getaddrinfo and freeaddrinfo or none of them.
707 if( ws2_freeaddrinfo != NULL )
709 ws2_freeaddrinfo( infos );
710 FreeLibrary( wship6_module );
714 FreeLibrary( wship6_module );
717 #ifdef HAVE_GETADDRINFO
718 freeaddrinfo( infos );
720 __freeaddrinfo( infos );