/*****************************************************************************
* getaddrinfo.c: getaddrinfo/getnameinfo replacement functions
*****************************************************************************
- * Copyright (C) 2005 the VideoLAN team
+ * Copyright (C) 2005 VLC authors and VideoLAN
* Copyright (C) 2002-2007 Rémi Denis-Courmont
* $Id$
*
* Author: Rémi Denis-Courmont <rem # videolan.org>
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser General Public License for more details.
*
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#ifdef HAVE_CONFIG_H
#endif
#include <vlc_common.h>
-#include <vlc_charset.h>
#include <stddef.h> /* size_t */
#include <string.h> /* strlen(), memcpy(), memset(), strchr() */
/**
* Resolves a host name to a list of socket addresses (like getaddrinfo()).
*
- * @param p_this a VLC object
* @param node host name to resolve (encoded as UTF-8), or NULL
* @param i_port port number for the socket addresses
* @param p_hints parameters (see getaddrinfo() manual page)
* @param res pointer set to the resulting chained list.
* @return 0 on success, a getaddrinfo() error otherwise.
* On failure, *res is undefined. On success, it must be freed with
- * vlc_freeaddrinfo().
+ * freeaddrinfo().
*/
-int vlc_getaddrinfo( vlc_object_t *p_this, const char *node,
- int i_port, const struct addrinfo *p_hints,
- struct addrinfo **res )
+int vlc_getaddrinfo (const char *node, unsigned port,
+ const struct addrinfo *p_hints, struct addrinfo **res)
{
struct addrinfo hints;
- char psz_buf[NI_MAXHOST], psz_service[6];
+ char psz_buf[NI_MAXHOST], portbuf[6], *servname;
/*
* In VLC, we always use port number as integer rather than strings
* for historical reasons (and portability).
*/
- if( ( i_port > 65535 ) || ( i_port < 0 ) )
+ if (port != 0)
{
- msg_Err( p_this, "invalid port number %d specified", i_port );
- return EAI_SERVICE;
+ if (port > 65535)
+ return EAI_SERVICE;
+ /* cannot overflow */
+ snprintf (portbuf, sizeof (portbuf), "%u", port);
+ servname = portbuf;
}
-
- /* cannot overflow */
- snprintf( psz_service, 6, "%d", i_port );
+ else
+ servname = NULL;
/* Check if we have to force ipv4 or ipv6 */
memset (&hints, 0, sizeof (hints));
hints.ai_flags = p_hints->ai_flags & safe_flags;
}
- /* We only ever use port *numbers* */
- hints.ai_flags |= AI_NUMERICSERV;
-
- if( hints.ai_family == AF_UNSPEC )
- {
-#ifdef AF_INET6
- if (var_CreateGetBool (p_this, "ipv6"))
- hints.ai_family = AF_INET6;
- else
-#endif
- if (var_CreateGetBool (p_this, "ipv4"))
- hints.ai_family = AF_INET;
- }
-
/*
* VLC extensions :
* - accept "" as NULL
}
int ret;
- node = ToLocale (node);
#ifdef WIN32
/*
* Winsock tries to resolve numerical IPv4 addresses as AAAA
if ((hints.ai_flags & AI_NUMERICHOST) == 0)
{
hints.ai_flags |= AI_NUMERICHOST;
- ret = getaddrinfo (node, psz_service, &hints, res);
+ ret = getaddrinfo (node, servname, &hints, res);
if (ret == 0)
goto out;
hints.ai_flags &= ~AI_NUMERICHOST;
#ifdef AI_IDN
/* Run-time I18n Domain Names support */
hints.ai_flags |= AI_IDN;
- ret = getaddrinfo (node, psz_service, &hints, res);
+ ret = getaddrinfo (node, servname, &hints, res);
if (ret != EAI_BADFLAGS)
goto out;
/* IDN not available: disable and retry without it */
hints.ai_flags &= ~AI_IDN;
#endif
- ret = getaddrinfo (node, psz_service, &hints, res);
+ ret = getaddrinfo (node, servname, &hints, res);
#if defined(AI_IDN) || defined(WIN32)
out:
#endif
- LocaleFree (node);
return ret;
}
-
-
-void vlc_freeaddrinfo( struct addrinfo *infos )
-{
- freeaddrinfo (infos);
-}
-
-/**
- * inet_pton() replacement
- */
-int vlc_inet_pton (int af, const char *src, void *dst)
-{
-#ifndef HAVE_INET_PTON
- /* Windows Vista has inet_pton(), but not XP. */
- /* We have a pretty good example of abstraction inversion here... */
- struct addrinfo hints = {
- .ai_family = af,
- .ai_socktype = SOCK_DGRAM, /* make sure we have... */
- .ai_protocol = IPPROTO_UDP, /* ...only one response */
- .ai_flags = AI_NUMERICHOST,
- }, *res;
-
- if (getaddrinfo (src, NULL, &hints, &res))
- return 0;
-
- const void *data;
- size_t len;
-
- switch (af)
- {
- case AF_INET:
- data = &((const struct sockaddr_in *)res->ai_addr)->sin_addr;
- len = sizeof (struct in_addr);
- break;
-#ifdef AF_INET6
- case AF_INET6:
- data = &((const struct sockaddr_in6 *)res->ai_addr)->sin6_addr;
- len = sizeof (struct in6_addr);
- break;
-#endif
- default:
- freeaddrinfo (res);
- return -1;
- }
- memcpy (dst, data, len);
- freeaddrinfo (res);
- return 1;
-#else /* HAVE_INET_PTON */
- return inet_pton( af, src, dst );
-#endif /* HAVE_INET_PTON */
-}
-
-/**
- * inet_ntop() replacement
- */
-const char *vlc_inet_ntop (int af, const void *src, char *dst, socklen_t cnt)
-{
-#ifndef HAVE_INET_NTOP
- int ret = EAI_FAMILY;
-
- switch (af)
- {
-#ifdef AF_INET6
- case AF_INET6:
- {
- struct sockaddr_in6 addr;
- memset (&addr, 0, sizeof(addr));
- addr.sin6_family = AF_INET6;
- addr.sin6_addr = *(struct in6_addr *)src;
- ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr),
- dst, cnt, NULL, 0, NI_NUMERICHOST);
- }
-
-#endif
- case AF_INET:
- {
- struct sockaddr_in addr;
- memset(&addr, 0, sizeof(addr));
- addr.sin_family = AF_INET;
- addr.sin_addr = *(struct in_addr *)src;
- ret = getnameinfo ((struct sockaddr *)&addr, sizeof (addr),
- dst, cnt, NULL, 0, NI_NUMERICHOST);
- }
- }
- return (ret == 0) ? dst : NULL;
-#else /* HAVE_INET_NTOP */
- return inet_ntop( af, src, dst, cnt );
-#endif /* HAVE_INET_NTOP */
-}
-