* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
#include <stddef.h> /* size_t */
#include <string.h> /* strlen(), memcpy(), memset(), strchr() */
#ifndef HAVE_GAI_STRERROR
-static struct
+static const struct
{
- int code;
- const char *msg;
-} const __gai_errlist[] =
+ int code;
+ const char msg[41];
+} gai_errlist[] =
{
{ 0, "Error 0" },
{ EAI_BADFLAGS, "Invalid flag used" },
{ EAI_MEMORY, "Memory allocation failure" },
{ EAI_OVERFLOW, "Buffer overflow" },
{ EAI_SYSTEM, "System error" },
- { 0, NULL }
+ { 0, "" },
};
-static const char *__gai_unknownerr = "Unrecognized error number";
+static const char gai_unknownerr[] = "Unrecognized error number";
/****************************************************************************
* Converts an EAI_* error code into human readable english text.
****************************************************************************/
const char *vlc_gai_strerror (int errnum)
{
- for (unsigned i = 0; __gai_errlist[i].msg != NULL; i++)
- if (errnum == __gai_errlist[i].code)
- return __gai_errlist[i].msg;
+ for (unsigned i = 0; *gai_errlist[i].msg; i++)
+ if (errnum == gai_errlist[i].code)
+ return gai_errlist[i].msg;
- return __gai_unknownerr;
+ return gai_unknownerr;
}
#else /* ifndef HAVE_GAI_STRERROR */
const char *vlc_gai_strerror (int errnum)
NI_DGRAM)
/*
* getnameinfo() non-thread-safe IPv4-only implementation,
- * Address-family-independant address to hostname translation
+ * Address-family-independent address to hostname translation
* (reverse DNS lookup in case of IPv4).
*
* This is meant for use on old IP-enabled systems that are not IPv6-aware,
getnameinfo (const struct sockaddr *sa, socklen_t salen,
char *host, DWORD hostlen, char *serv, DWORD servlen, int flags)
#else
-static int
+static int
getnameinfo (const struct sockaddr *sa, socklen_t salen,
char *host, int hostlen, char *serv, int servlen, int flags)
#endif
/*
* getaddrinfo() non-thread-safe IPv4-only implementation
- * Address-family-independant hostname to address resolution.
+ * Address-family-independent hostname to address resolution.
*
* This is meant for IPv6-unaware systems that do probably not provide
* getaddrinfo(), but still have old function gethostbyname().
getaddrinfo (const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res)
#else
-static int
+static int
getaddrinfo (const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res)
#endif
port = 0;
else
{
- long d;
+ unsigned long d;
char *end;
d = strtoul (service, &end, 0);
if (entry == NULL)
{
/* not found, use replacement API instead */
- entry = getnameinfo;
+ entry = getnameinfo;
}
/* call API before replacing function pointer to avoid crash */
if ((entry == NULL) || (freentry == NULL))
{
/* not found, use replacement API instead */
- entry = getaddrinfo;
- freentry = freeaddrinfo;
+ entry = getaddrinfo;
+ freentry = freeaddrinfo;
}
/* call API before replacing function pointer to avoid crash */
result = entry (node, service, hints, res);
memset (&hints, 0, sizeof (hints));
if (p_hints != NULL)
{
+ const int safe_flags =
+ AI_PASSIVE |
+ AI_CANONNAME |
+ AI_NUMERICHOST |
+ AI_NUMERICSERV |
+#ifdef AI_ALL
+ AI_ALL |
+#endif
+#ifdef AI_ADDRCONFIG
+ AI_ADDRCONFIG |
+#endif
+#ifdef AI_V4MAPPED
+ AI_V4MAPPED |
+#endif
+ 0;
+
hints.ai_family = p_hints->ai_family;
hints.ai_socktype = p_hints->ai_socktype;
hints.ai_protocol = p_hints->ai_protocol;
- hints.ai_flags = p_hints->ai_flags & (AI_NUMERICHOST|AI_PASSIVE|AI_CANONNAME);
+ /* Unfortunately, some flags chang the layout of struct addrinfo, so
+ * they cannot be copied blindly from p_hints to &hints. Therefore, we
+ * only copy flags that we know for sure are "safe".
+ */
+ hints.ai_flags = p_hints->ai_flags & safe_flags;
}
-#ifdef AI_NUMERICSERV
- /* we only ever use port *numbers* */
+
+ /* We only ever use port *numbers* */
hints.ai_flags |= AI_NUMERICSERV;
-#endif
if( hints.ai_family == AF_UNSPEC )
{
- vlc_value_t val;
-
- var_Create( p_this, "ipv4", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
- var_Get( p_this, "ipv4", &val );
- if( val.b_bool )
- hints.ai_family = AF_INET;
-
#ifdef AF_INET6
- var_Create( p_this, "ipv6", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
- var_Get( p_this, "ipv6", &val );
- if( val.b_bool )
+ if (var_CreateGetBool (p_this, "ipv6"))
hints.ai_family = AF_INET6;
+ else
#endif
+ if (var_CreateGetBool (p_this, "ipv4"))
+ hints.ai_family = AF_INET;
}
/*
hints.ai_flags &= ~AI_NUMERICHOST;
}
#endif
-#if defined (HAVE_GETADDRINFO)
-# ifdef AI_IDN
+#ifdef AI_IDN
/* Run-time I18n Domain Names support */
- static vlc_bool_t b_idn = VLC_TRUE; /* beware of thread-safety */
-
- if (b_idn)
- {
- hints.ai_flags |= AI_IDN;
- int ret = getaddrinfo (psz_node, psz_service, &hints, res);
-
- if (ret != EAI_BADFLAGS)
- return ret;
-
- /* IDN not available: disable and retry without it */
- hints.ai_flags &= ~AI_IDN;
- b_idn = VLC_FALSE;
- msg_Info (p_this, "International Domain Names not supported");
- }
-# endif
- return getaddrinfo (psz_node, psz_service, &hints, res);
-#else
- int ret;
- vlc_value_t lock;
-
- var_Create (p_this->p_libvlc, "getaddrinfo_mutex", VLC_VAR_MUTEX);
- var_Get (p_this->p_libvlc, "getaddrinfo_mutex", &lock);
- vlc_mutex_lock (lock.p_address);
+ hints.ai_flags |= AI_IDN;
+ int ret = getaddrinfo (psz_node, psz_service, &hints, res);
+ if (ret != EAI_BADFLAGS)
+ return ret;
- ret = getaddrinfo (psz_node, psz_service, &hints, res);
- vlc_mutex_unlock (lock.p_address);
- return ret;
+ /* IDN not available: disable and retry without it */
+ hints.ai_flags &= ~AI_IDN;
#endif
+ return getaddrinfo (psz_node, psz_service, &hints, res);
}