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 *****************************************************************************/
28 #include <stddef.h> /* size_t */
29 #include <string.h> /* strncpy(), strlen(), memcpy(), memset(), strchr() */
30 #include <stdlib.h> /* malloc(), free(), strtoul() */
31 #include <sys/types.h>
32 #ifdef HAVE_ARPA_INET_H
33 # include <arpa/inet.h>
35 #ifdef HAVE_NETINET_IN_H
36 # include <netinet/in.h>
40 #if defined (UNDER_CE)
43 # include <winsock2.h>
44 # include <ws2tcpip.h>
50 #define NO_ADDRESS NO_DATA
51 #define PF_INET AF_INET
52 #define INADDR_NONE 0xFFFFFFFF
56 #define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
58 # define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
61 #ifndef HAVE_GAI_STRERROR
66 } const __gai_errlist[] =
69 { EAI_BADFLAGS, "Invalid flag used" },
70 { EAI_NONAME, "Host or service not found" },
71 { EAI_AGAIN, "Temporary name service failure" },
72 { EAI_FAIL, "Non-recoverable name service failure" },
73 { EAI_NODATA, "No data for host name" },
74 { EAI_FAMILY, "Unsupported address family" },
75 { EAI_SOCKTYPE, "Unsupported socket type" },
76 { EAI_SERVICE, "Incompatible service for socket type" },
77 { EAI_ADDRFAMILY, "Unavailable address family for host name" },
78 { EAI_MEMORY, "Memory allocation failure" },
79 { EAI_SYSTEM, "System error" },
83 static const char *__gai_unknownerr = "Unrecognized error number";
85 /****************************************************************************
86 * Converts an EAI_* error code into human readable english text.
87 ****************************************************************************/
88 const char *vlc_gai_strerror( int errnum )
92 for (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 # undef _EAI_POSITIVE_MAX
99 #else /* ifndef HAVE_GAI_STRERROR */
100 const char *vlc_gai_strerror( int errnum )
102 return gai_strerror( errnum );
106 #if !(defined (HAVE_GETNAMEINFO) && defined (HAVE_GETADDRINFO))
108 * Converts the current herrno error value into an EAI_* error code.
109 * That error code is normally returned by getnameinfo() or getaddrinfo().
112 gai_error_from_herrno( void )
120 # if (NO_ADDRESS != NO_DATA)
133 #endif /* if !(HAVE_GETNAMEINFO && HAVE_GETADDRINFO) */
135 #ifndef HAVE_GETNAMEINFO
137 * getnameinfo() non-thread-safe IPv4-only implementation,
138 * Address-family-independant address to hostname translation
139 * (reverse DNS lookup in case of IPv4).
141 * This is meant for use on old IP-enabled systems that are not IPv6-aware,
142 * and probably do not have getnameinfo(), but have the old gethostbyaddr()
145 * GNU C library 2.0.x is known to lack this function, even though it defines
149 __getnameinfo( const struct sockaddr *sa, socklen_t salen,
150 char *host, int hostlen, char *serv, int servlen, int flags )
152 if (((unsigned)salen < sizeof (struct sockaddr_in))
153 || (sa->sa_family != AF_INET))
155 else if (flags & (~_NI_MASK))
159 const struct sockaddr_in *addr;
161 addr = (const struct sockaddr_in *)sa;
167 /* host name resolution */
168 if (!(flags & NI_NUMERICHOST))
170 struct hostent *hent;
172 hent = gethostbyaddr ((const void*)&addr->sin_addr,
177 strncpy (host, hent->h_name, hostlen);
178 host[hostlen - 1] = '\0';
181 * only keep first part of hostname
182 * if user don't want fully qualified
185 if (flags & NI_NOFQDN)
189 ptr = strchr (host, '.');
196 else if (flags & NI_NAMEREQD)
197 return gai_error_from_herrno ();
202 /* inet_ntoa() can't fail */
203 strncpy (host, inet_ntoa (addr->sin_addr), hostlen);
204 host[hostlen - 1] = '\0';
210 struct servent *sent = NULL;
212 #ifndef SYS_BEOS /* No getservbyport() */
215 /* service name resolution */
216 if (!(flags & NI_NUMERICSERV))
219 sent = getservbyport(addr->sin_port,
224 strncpy (serv, sent->s_name, servlen);
225 serv[servlen - 1] = 0;
234 snprintf (serv, servlen, "%u",
235 (unsigned int)ntohs (addr->sin_port));
236 serv[servlen - 1] = '\0';
243 #endif /* if !HAVE_GETNAMEINFO */
246 #ifndef HAVE_GETADDRINFO
248 * This functions must be used to free the memory allocated by getaddrinfo().
251 __freeaddrinfo (struct addrinfo *res)
255 if (res->ai_canonname != NULL)
256 free (res->ai_canonname);
257 if (res->ai_addr != NULL)
259 if (res->ai_next != NULL)
267 * Internal function that builds an addrinfo struct.
269 static struct addrinfo *
270 makeaddrinfo (int af, int type, int proto,
271 const struct sockaddr *addr, size_t addrlen,
272 const char *canonname)
274 struct addrinfo *res;
276 res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
281 res->ai_socktype = type;
282 res->ai_protocol = proto;
283 res->ai_addrlen = addrlen;
284 res->ai_addr = malloc (addrlen);
285 res->ai_canonname = NULL;
288 if (res->ai_addr != NULL)
290 memcpy (res->ai_addr, addr, addrlen);
292 if (canonname != NULL)
294 res->ai_canonname = strdup (canonname);
295 if (res->ai_canonname != NULL)
296 return res; /* success ! */
303 vlc_freeaddrinfo (res);
308 static struct addrinfo *
309 makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
311 struct sockaddr_in addr;
313 memset (&addr, 0, sizeof (addr));
314 addr.sin_family = AF_INET;
316 addr.sin_len = sizeof (addr);
318 addr.sin_port = port;
319 addr.sin_addr.s_addr = ip;
321 return makeaddrinfo (PF_INET, type, proto,
322 (struct sockaddr*)&addr, sizeof (addr), name);
327 * getaddrinfo() non-thread-safe IPv4-only implementation
328 * Address-family-independant hostname to address resolution.
330 * This is meant for IPv6-unaware systems that do probably not provide
331 * getaddrinfo(), but still have old function gethostbyname().
333 * Only UDP and TCP over IPv4 are supported here.
336 __getaddrinfo (const char *node, const char *service,
337 const struct addrinfo *hints, struct addrinfo **res)
339 struct addrinfo *info;
342 int protocol = 0, flags = 0;
343 const char *name = NULL;
347 flags = hints->ai_flags;
349 if (flags & ~_AI_MASK)
351 /* only accept AF_INET and AF_UNSPEC */
352 if (hints->ai_family && (hints->ai_family != AF_INET))
355 /* protocol sanity check */
356 switch (hints->ai_socktype)
359 protocol = IPPROTO_TCP;
363 protocol = IPPROTO_UDP;
375 if (hints->ai_protocol && protocol
376 && (protocol != hints->ai_protocol))
385 if (flags & AI_PASSIVE)
386 ip = htonl (INADDR_ANY);
388 ip = htonl (INADDR_LOOPBACK);
391 if ((ip = inet_addr (node)) == INADDR_NONE)
393 struct hostent *entry = NULL;
395 /* hostname resolution */
396 if (!(flags & AI_NUMERICHOST))
397 entry = gethostbyname (node);
402 if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
405 ip = *((u_long *) entry->h_addr);
406 if (flags & AI_CANONNAME)
407 name = entry->h_name;
410 if ((flags & AI_CANONNAME) && (name == NULL))
413 /* service resolution */
421 d = strtoul (service, &end, 0);
422 if (end[0] /* service is not a number */
425 struct servent *entry;
426 const char *protoname;
442 entry = getservbyname (service, protoname);
446 port = entry->s_port;
449 port = htons ((u_short)d);
452 /* building results... */
453 if ((!protocol) || (protocol == IPPROTO_UDP))
455 info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
461 if (flags & AI_PASSIVE)
462 info->ai_flags |= AI_PASSIVE;
465 if ((!protocol) || (protocol == IPPROTO_TCP))
467 info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
473 info->ai_next = *res;
474 if (flags & AI_PASSIVE)
475 info->ai_flags |= AI_PASSIVE;
481 #endif /* if !HAVE_GETADDRINFO */
484 int vlc_getnameinfo( vlc_object_t *p_this, const struct sockaddr *sa, int salen,
485 char *host, int hostlen, char *serv, int servlen, int flags )
489 * Here is the kind of kludge you need to keep binary compatibility among
490 * varying OS versions...
492 typedef int (CALLBACK * GETNAMEINFO) ( const struct sockaddr*, socklen_t,
493 char*, DWORD, char*, DWORD, int );
494 HINSTANCE wship6_module;
495 GETNAMEINFO ws2_getnameinfo;
497 wship6_module = LoadLibrary( "wship6.dll" );
498 if( wship6_module != NULL )
500 ws2_getnameinfo = (GETNAMEINFO)GetProcAddress( wship6_module,
503 if( ws2_getnameinfo != NULL )
507 i_val = ws2_getnameinfo( sa, salen, host, hostlen, serv, servlen,
509 FreeLibrary( wship6_module );
513 FreeLibrary( wship6_module );
517 return getnameinfo( sa, salen, host, hostlen, serv, servlen, flags );
523 /* my getnameinfo implementation is not thread-safe as it uses
524 * gethostbyaddr and the likes */
525 var_Create( p_this->p_libvlc, "getnameinfo_mutex", VLC_VAR_MUTEX );
526 var_Get( p_this->p_libvlc, "getnameinfo_mutex", &lock );
527 vlc_mutex_lock( lock.p_address );
529 i_val = __getnameinfo( sa, salen, host, hostlen, serv, servlen, flags );
530 vlc_mutex_unlock( lock.p_address );
537 /* TODO: support for setting sin6_scope_id */
538 int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
539 const char *service, const struct addrinfo *p_hints,
540 struct addrinfo **res )
542 struct addrinfo hints;
543 char psz_buf[NI_MAXHOST], *psz_node;
545 /* Check if we have to force ipv4 or ipv6 */
546 if( p_hints == NULL )
547 memset( &hints, 0, sizeof( hints ) );
549 memcpy( &hints, p_hints, sizeof( hints ) );
551 if( hints.ai_family == AF_UNSPEC )
555 var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
556 var_Get( p_this, "ipv4", &val );
558 hints.ai_family = AF_INET;
560 #ifdef HAVE_INET_PTON
561 var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
562 var_Get( p_this, "ipv6", &val );
564 hints.ai_family = AF_INET6;
570 * - accept "" as NULL
571 * - ignore square brackets
573 if( ( node == NULL ) || (node[0] == '\0' ) )
577 strncpy( psz_buf, node, NI_MAXHOST );
578 psz_buf[NI_MAXHOST - 1] = '\0';
582 if( psz_buf[0] == '[' )
586 ptr = strrchr( psz_buf, ']' );
587 if( ( ptr != NULL ) && (ptr[1] == '\0' ) )
595 if( ( service != NULL ) && ( *service == '\0' ) )
596 /* We could put NULL, but you can't have both node and service NULL */
600 typedef int (CALLBACK * GETADDRINFO) ( const char *, const char *,
601 const struct addrinfo *,
602 struct addrinfo ** );
603 HINSTANCE wship6_module;
604 GETADDRINFO ws2_getaddrinfo;
606 wship6_module = LoadLibrary( "wship6.dll" );
607 if( wship6_module != NULL )
609 ws2_getaddrinfo = (GETADDRINFO)GetProcAddress( wship6_module,
612 if( ws2_getaddrinfo != NULL )
616 i_ret = ws2_getaddrinfo( psz_node, service, &hints, res );
617 FreeLibrary( wship6_module ); /* is this wise ? */
621 FreeLibrary( wship6_module );
625 return getaddrinfo( psz_node, service, &hints, res );
632 var_Create( p_this->p_libvlc, "getaddrinfo_mutex", VLC_VAR_MUTEX );
633 var_Get( p_this->p_libvlc, "getaddrinfo_mutex", &lock );
634 vlc_mutex_lock( lock.p_address );
636 i_ret = __getaddrinfo( psz_node, service, &hints, res );
637 vlc_mutex_unlock( lock.p_address );
644 void vlc_freeaddrinfo( struct addrinfo *infos )
647 typedef void (CALLBACK * FREEADDRINFO) ( struct addrinfo * );
648 HINSTANCE wship6_module;
649 FREEADDRINFO ws2_freeaddrinfo;
651 wship6_module = LoadLibrary( "wship6.dll" );
652 if( wship6_module != NULL )
654 ws2_freeaddrinfo = (FREEADDRINFO)GetProcAddress( wship6_module,
658 * NOTE: it is assumed that wship6.dll defines either both
659 * getaddrinfo and freeaddrinfo or none of them.
661 if( ws2_freeaddrinfo != NULL )
663 ws2_freeaddrinfo( infos );
664 FreeLibrary( wship6_module );
668 FreeLibrary( wship6_module );
671 #ifdef HAVE_GETADDRINFO
672 freeaddrinfo( infos );
674 __freeaddrinfo( infos );