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
65 # define PF_UNSPEC AF_UNSPEC
68 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
70 #define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
73 #ifndef HAVE_GAI_STRERROR
78 } const __gai_errlist[] =
81 { EAI_BADFLAGS, "Invalid flag used" },
82 { EAI_NONAME, "Host or service not found" },
83 { EAI_AGAIN, "Temporary name service failure" },
84 { EAI_FAIL, "Non-recoverable name service failure" },
85 { EAI_NODATA, "No data for host name" },
86 { EAI_FAMILY, "Unsupported address family" },
87 { EAI_SOCKTYPE, "Unsupported socket type" },
88 { EAI_SERVICE, "Incompatible service for socket type" },
89 { EAI_ADDRFAMILY, "Unavailable address family for host name" },
90 { EAI_MEMORY, "Memory allocation failure" },
91 { EAI_SYSTEM, "System error" },
95 static const char *__gai_unknownerr = "Unrecognized error number";
97 /****************************************************************************
98 * Converts an EAI_* error code into human readable english text.
99 ****************************************************************************/
100 const char *vlc_gai_strerror( int errnum )
104 for (i = 0; __gai_errlist[i].msg != NULL; i++)
105 if (errnum == __gai_errlist[i].code)
106 return __gai_errlist[i].msg;
108 return __gai_unknownerr;
110 # undef _EAI_POSITIVE_MAX
111 #else /* ifndef HAVE_GAI_STRERROR */
112 const char *vlc_gai_strerror( int errnum )
114 return gai_strerror( errnum );
118 #if !(defined (HAVE_GETNAMEINFO) && defined (HAVE_GETADDRINFO))
120 * Converts the current herrno error value into an EAI_* error code.
121 * That error code is normally returned by getnameinfo() or getaddrinfo().
124 gai_error_from_herrno( void )
132 # if (NO_ADDRESS != NO_DATA)
145 #endif /* if !(HAVE_GETNAMEINFO && HAVE_GETADDRINFO) */
147 #ifndef HAVE_GETNAMEINFO
149 * getnameinfo() non-thread-safe IPv4-only implementation,
150 * Address-family-independant address to hostname translation
151 * (reverse DNS lookup in case of IPv4).
153 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
154 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
157 * GNU C library 2.0.x is known to lack this function, even though it defines
161 __getnameinfo( const struct sockaddr *sa, socklen_t salen,
162 char *host, int hostlen, char *serv, int servlen, int flags )
164 if (((unsigned)salen < sizeof (struct sockaddr_in))
165 || (sa->sa_family != AF_INET))
167 else if (flags & (~_NI_MASK))
171 const struct sockaddr_in *addr;
173 addr = (const struct sockaddr_in *)sa;
179 /* host name resolution */
180 if (!(flags & NI_NUMERICHOST))
182 struct hostent *hent;
184 hent = gethostbyaddr ((const void*)&addr->sin_addr,
189 strncpy (host, hent->h_name, hostlen);
190 host[hostlen - 1] = '\0';
193 * only keep first part of hostname
194 * if user don't want fully qualified
197 if (flags & NI_NOFQDN)
201 ptr = strchr (host, '.');
208 else if (flags & NI_NAMEREQD)
209 return gai_error_from_herrno ();
214 /* inet_ntoa() can't fail */
215 strncpy (host, inet_ntoa (addr->sin_addr), hostlen);
216 host[hostlen - 1] = '\0';
222 struct servent *sent = NULL;
224 #ifndef SYS_BEOS /* No getservbyport() */
227 /* service name resolution */
228 if (!(flags & NI_NUMERICSERV))
231 sent = getservbyport(addr->sin_port,
236 strncpy (serv, sent->s_name, servlen);
237 serv[servlen - 1] = 0;
246 snprintf (serv, servlen, "%u",
247 (unsigned int)ntohs (addr->sin_port));
248 serv[servlen - 1] = '\0';
255 #endif /* if !HAVE_GETNAMEINFO */
258 #ifndef HAVE_GETADDRINFO
260 * This functions must be used to free the memory allocated by getaddrinfo().
263 __freeaddrinfo (struct addrinfo *res)
267 if (res->ai_canonname != NULL)
268 free (res->ai_canonname);
269 if (res->ai_addr != NULL)
271 if (res->ai_next != NULL)
279 * Internal function that builds an addrinfo struct.
281 static struct addrinfo *
282 makeaddrinfo (int af, int type, int proto,
283 const struct sockaddr *addr, size_t addrlen,
284 const char *canonname)
286 struct addrinfo *res;
288 res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
293 res->ai_socktype = type;
294 res->ai_protocol = proto;
295 res->ai_addrlen = addrlen;
296 res->ai_addr = malloc (addrlen);
297 res->ai_canonname = NULL;
300 if (res->ai_addr != NULL)
302 memcpy (res->ai_addr, addr, addrlen);
304 if (canonname != NULL)
306 res->ai_canonname = strdup (canonname);
307 if (res->ai_canonname != NULL)
308 return res; /* success ! */
315 vlc_freeaddrinfo (res);
320 static struct addrinfo *
321 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
323 struct sockaddr_in addr;
325 memset (&addr, 0, sizeof (addr));
326 addr.sin_family = AF_INET;
328 addr.sin_len = sizeof (addr);
330 addr.sin_port = port;
331 addr.sin_addr.s_addr = ip;
333 return makeaddrinfo (PF_INET, type, proto,
334 (struct sockaddr*)&addr, sizeof (addr), name);
339 * getaddrinfo() non-thread-safe IPv4-only implementation
340 * Address-family-independant hostname to address resolution.
342 * This is meant for IPv6-unaware systems that do probably not provide
343 * getaddrinfo(), but still have old function gethostbyname().
345 * Only UDP and TCP over IPv4 are supported here.
348 __getaddrinfo (const char *node, const char *service,
349 const struct addrinfo *hints, struct addrinfo **res)
351 struct addrinfo *info;
354 int protocol = 0, flags = 0;
355 const char *name = NULL;
359 flags = hints->ai_flags;
361 if (flags & ~_AI_MASK)
363 /* only accept AF_INET and AF_UNSPEC */
364 if (hints->ai_family && (hints->ai_family != AF_INET))
367 /* protocol sanity check */
368 switch (hints->ai_socktype)
371 protocol = IPPROTO_TCP;
375 protocol = IPPROTO_UDP;
387 if (hints->ai_protocol && protocol
388 && (protocol != hints->ai_protocol))
397 if (flags & AI_PASSIVE)
398 ip = htonl (INADDR_ANY);
400 ip = htonl (INADDR_LOOPBACK);
403 if ((ip = inet_addr (node)) == INADDR_NONE)
405 struct hostent *entry = NULL;
407 /* hostname resolution */
408 if (!(flags & AI_NUMERICHOST))
409 entry = gethostbyname (node);
414 if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
417 ip = *((u_long *) entry->h_addr);
418 if (flags & AI_CANONNAME)
419 name = entry->h_name;
422 if ((flags & AI_CANONNAME) && (name == NULL))
425 /* service resolution */
433 d = strtoul (service, &end, 0);
434 if (end[0] /* service is not a number */
437 struct servent *entry;
438 const char *protoname;
454 entry = getservbyname (service, protoname);
458 port = entry->s_port;
461 port = htons ((u_short)d);
464 /* building results... */
465 if ((!protocol) || (protocol == IPPROTO_UDP))
467 info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
473 if (flags & AI_PASSIVE)
474 info->ai_flags |= AI_PASSIVE;
477 if ((!protocol) || (protocol == IPPROTO_TCP))
479 info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
485 info->ai_next = *res;
486 if (flags & AI_PASSIVE)
487 info->ai_flags |= AI_PASSIVE;
493 #endif /* if !HAVE_GETADDRINFO */
496 int vlc_getnameinfo( const struct sockaddr *sa, int salen,
497 char *host, int hostlen, int *portnum, int flags )
499 char psz_servbuf[6], *psz_serv;
500 int i_servlen, i_val;
502 flags |= NI_NUMERICSERV;
503 if( portnum != NULL )
505 psz_serv = psz_servbuf;
506 i_servlen = sizeof( psz_servbuf );
515 * Here is the kind of kludge you need to keep binary compatibility among
516 * varying OS versions...
518 typedef int (CALLBACK * GETNAMEINFO) ( const struct sockaddr*, socklen_t,
519 char*, DWORD, char*, DWORD, int );
520 HINSTANCE wship6_module;
521 GETNAMEINFO ws2_getnameinfo;
523 wship6_module = LoadLibrary( "wship6.dll" );
524 if( wship6_module != NULL )
526 ws2_getnameinfo = (GETNAMEINFO)GetProcAddress( wship6_module,
529 if( ws2_getnameinfo != NULL )
531 i_val = ws2_getnameinfo( sa, salen, host, hostlen, psz_serv,
533 FreeLibrary( wship6_module );
535 if( portnum != NULL )
536 *portnum = atoi( psz_serv );
540 FreeLibrary( wship6_module );
544 i_val = getnameinfo( sa, salen, host, hostlen, psz_serv, i_servlen,
548 # ifdef HAVE_USABLE_MUTEX_THAT_DONT_NEED_LIBVLC_POINTER
549 static vlc_value_t lock;
551 /* my getnameinfo implementation is not thread-safe as it uses
552 * gethostbyaddr and the likes */
553 vlc_mutex_lock( lock.p_address );
555 # warning FIXME : This is not thread-safe!
557 i_val = __getnameinfo( sa, salen, host, hostlen, psz_serv, i_servlen,
559 # ifdef HAVE_USABLE_MUTEX_THAT_DONT_NEED_LIBVLC_POINTER
560 vlc_mutex_unlock( lock.p_address );
565 if( portnum != NULL )
566 *portnum = atoi( psz_serv );
572 /* TODO: support for setting sin6_scope_id */
573 int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
574 int i_port, const struct addrinfo *p_hints,
575 struct addrinfo **res )
577 struct addrinfo hints;
578 char psz_buf[NI_MAXHOST], *psz_node, psz_service[6];
581 * In VLC, we always use port number as integer rather than strings
582 * for historical reasons (and portability).
584 if( ( i_port > 65535 ) || ( i_port < 0 ) )
586 msg_Err( p_this, "invalid port number %d specified", i_port );
590 /* cannot overflow */
591 snprintf( psz_service, 6, "%d", i_port );
593 /* Check if we have to force ipv4 or ipv6 */
594 if( p_hints == NULL )
595 memset( &hints, 0, sizeof( hints ) );
597 memcpy( &hints, p_hints, sizeof( hints ) );
599 if( hints.ai_family == PF_UNSPEC )
603 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
604 var_Get( p_this, "ipv4", &val );
606 hints.ai_family = PF_INET;
609 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
610 var_Get( p_this, "ipv6", &val );
612 hints.ai_family = PF_INET6;
618 * - accept "" as NULL
619 * - ignore square brackets
621 if( ( node == NULL ) || (node[0] == '\0' ) )
627 strncpy( psz_buf, node, NI_MAXHOST );
628 psz_buf[NI_MAXHOST - 1] = '\0';
632 if( psz_buf[0] == '[' )
636 ptr = strrchr( psz_buf, ']' );
637 if( ( ptr != NULL ) && (ptr[1] == '\0' ) )
647 typedef int (CALLBACK * GETADDRINFO) ( const char *, const char *,
648 const struct addrinfo *,
649 struct addrinfo ** );
650 HINSTANCE wship6_module;
651 GETADDRINFO ws2_getaddrinfo;
653 wship6_module = LoadLibrary( "wship6.dll" );
654 if( wship6_module != NULL )
656 ws2_getaddrinfo = (GETADDRINFO)GetProcAddress( wship6_module,
659 if( ws2_getaddrinfo != NULL )
663 i_ret = ws2_getaddrinfo( psz_node, psz_service, &hints, res );
664 FreeLibrary( wship6_module ); /* is this wise ? */
668 FreeLibrary( wship6_module );
673 return getaddrinfo( psz_node, psz_service, &hints, res );
680 var_Create( p_this->p_libvlc, "getaddrinfo_mutex", VLC_VAR_MUTEX );
681 var_Get( p_this->p_libvlc, "getaddrinfo_mutex", &lock );
682 vlc_mutex_lock( lock.p_address );
684 i_ret = __getaddrinfo( psz_node, psz_service, &hints, res );
685 vlc_mutex_unlock( lock.p_address );
692 void vlc_freeaddrinfo( struct addrinfo *infos )
695 typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo * );
696 HINSTANCE wship6_module;
697 FREEADDRINFO ws2_freeaddrinfo;
699 wship6_module = LoadLibrary( "wship6.dll" );
700 if( wship6_module != NULL )
702 ws2_freeaddrinfo = (FREEADDRINFO)GetProcAddress( wship6_module,
706 * NOTE: it is assumed that wship6.dll defines either both
707 * getaddrinfo and freeaddrinfo or none of them.
709 if( ws2_freeaddrinfo != NULL )
711 ws2_freeaddrinfo( infos );
712 FreeLibrary( wship6_module );
716 FreeLibrary( wship6_module );
719 #ifdef HAVE_GETADDRINFO
720 freeaddrinfo( infos );
722 __freeaddrinfo( infos );