Commit 95a84cda authored by Rémi Denis-Courmont's avatar Rémi Denis-Courmont

Remove getnameinfo and getaddrinfo wrappers

They weren't thread-safe (and did not support IPv6).
parent c796d10a
...@@ -619,28 +619,9 @@ dnl getaddrinfo, getnameinfo and gai_strerror check ...@@ -619,28 +619,9 @@ dnl getaddrinfo, getnameinfo and gai_strerror check
dnl -lnsl and -lsocket are needed on Solaris; dnl -lnsl and -lsocket are needed on Solaris;
dnl we purposedly make the test fail on Windows dnl we purposedly make the test fail on Windows
LIBS_save="${LIBS}" LIBS_save="${LIBS}"
AH_TEMPLATE(HAVE_GETADDRINFO, [Define to 1 if you have the `getaddrinfo' function.]) AC_SEARCH_LIBS([getaddrinfo], [nsl],,, [${SOCKET_LIBS}])
AC_SEARCH_LIBS([getaddrinfo], [nsl], [AC_DEFINE(HAVE_GETADDRINFO)],, [${SOCKET_LIBS}])
dnl NOTE: we assume getaddrinfo will be present if getnameinfo or gai_strerro
dnl are
LIBS="${LIBS_gai}"
AC_CHECK_FUNCS([getnameinfo gai_strerror])
LIBS="${LIBS_save}" LIBS="${LIBS_save}"
AH_TEMPLATE(HAVE_ADDRINFO, [Define to 1 if <netdb.h> defines `struct addrinfo'.])
AC_CHECK_TYPES([struct addrinfo],[AC_DEFINE(HAVE_ADDRINFO)],,
[#include <sys/types.h>
#if defined( WIN32 ) || defined( UNDER_CE )
# if defined(UNDER_CE) && defined(sockaddr_storage)
# undef sockaddr_storage
# endif
# include <winsock2.h>
# include <ws2tcpip.h>
#else
# include <netdb.h>
#endif])
dnl Check for va_copy dnl Check for va_copy
AC_CACHE_CHECK([for va_copy], ac_cv_c_va_copy, AC_CACHE_CHECK([for va_copy], ac_cv_c_va_copy,
AC_TRY_LINK( AC_TRY_LINK(
......
...@@ -221,36 +221,10 @@ VLC_EXPORT (int, vlc_poll, (struct pollfd *fds, unsigned nfds, int timeout)); ...@@ -221,36 +221,10 @@ VLC_EXPORT (int, vlc_poll, (struct pollfd *fds, unsigned nfds, int timeout));
# endif # endif
# define NI_MAXNUMERICHOST 64 # define NI_MAXNUMERICHOST 64
# ifndef NI_NUMERICHOST
# define NI_NUMERICHOST 0x01
# define NI_NUMERICSERV 0x02
# define NI_NOFQDN 0x04
# define NI_NAMEREQD 0x08
# define NI_DGRAM 0x10
# endif
# ifndef HAVE_STRUCT_ADDRINFO
struct addrinfo
{
int ai_flags;
int ai_family;
int ai_socktype;
int ai_protocol;
size_t ai_addrlen;
struct sockaddr *ai_addr;
char *ai_canonname;
struct addrinfo *ai_next;
};
# define AI_PASSIVE 1
# define AI_CANONNAME 2
# define AI_NUMERICHOST 4
# endif /* if !HAVE_STRUCT_ADDRINFO */
#ifndef AI_NUMERICSERV #ifndef AI_NUMERICSERV
# define AI_NUMERICSERV 0 # define AI_NUMERICSERV 0
#endif #endif
VLC_EXPORT( const char *, vlc_gai_strerror, ( int ) );
VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) ); VLC_EXPORT( int, vlc_getnameinfo, ( const struct sockaddr *, int, char *, int, int *, int ) );
VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) ); VLC_EXPORT( int, vlc_getaddrinfo, ( vlc_object_t *, const char *, int, const struct addrinfo *, struct addrinfo ** ) );
VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) ); VLC_EXPORT( void, vlc_freeaddrinfo, ( struct addrinfo * ) );
......
...@@ -511,7 +511,6 @@ vlc_fourcc_IsYUV ...@@ -511,7 +511,6 @@ vlc_fourcc_IsYUV
vlc_fourcc_GetRGBFallback vlc_fourcc_GetRGBFallback
vlc_fourcc_GetYUVFallback vlc_fourcc_GetYUVFallback
vlc_fourcc_AreUVPlanesSwapped vlc_fourcc_AreUVPlanesSwapped
vlc_gai_strerror
vlc_gc_init vlc_gc_init
vlc_GetActionId vlc_GetActionId
vlc_getaddrinfo vlc_getaddrinfo
......
...@@ -36,505 +36,12 @@ ...@@ -36,505 +36,12 @@
#include <assert.h> #include <assert.h>
#include <sys/types.h> #include <sys/types.h>
#ifdef HAVE_ARPA_INET_H
# include <arpa/inet.h>
#endif
#ifdef HAVE_NETINET_IN_H
# include <netinet/in.h>
#endif
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
#include <vlc_network.h> #include <vlc_network.h>
#ifndef NO_ADDRESS
# define NO_ADDRESS NO_DATA
#endif
#ifndef INADDR_NONE
# define INADDR_NONE 0xFFFFFFFF
#endif
#ifndef AF_UNSPEC #ifndef AF_UNSPEC
# define AF_UNSPEC 0 # define AF_UNSPEC 0
#endif #endif
#ifndef HAVE_GAI_STRERROR
static const struct
{
int code;
const char msg[41];
} gai_errlist[] =
{
{ 0, "Error 0" },
{ EAI_BADFLAGS, "Invalid flag used" },
{ EAI_NONAME, "Host or service not found" },
{ EAI_AGAIN, "Temporary name service failure" },
{ EAI_FAIL, "Non-recoverable name service failure" },
{ EAI_NODATA, "No data for host name" },
{ EAI_FAMILY, "Unsupported address family" },
{ EAI_SOCKTYPE, "Unsupported socket type" },
{ EAI_SERVICE, "Incompatible service for socket type" },
{ EAI_ADDRFAMILY, "Unavailable address family for host name" },
{ EAI_MEMORY, "Memory allocation failure" },
{ EAI_OVERFLOW, "Buffer overflow" },
{ EAI_SYSTEM, "System error" },
{ 0, "" },
};
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; i++)
if (errnum == gai_errlist[i].code)
return gai_errlist[i].msg;
return gai_unknownerr;
}
#else /* ifndef HAVE_GAI_STRERROR */
const char *vlc_gai_strerror (int errnum)
{
return gai_strerror (errnum);
}
#endif
#ifndef HAVE_GETNAMEINFO
#define _NI_MASK (NI_NUMERICHOST|NI_NUMERICSERV|NI_NOFQDN|NI_NAMEREQD|\
NI_DGRAM)
/*
* getnameinfo() non-thread-safe IPv4-only implementation,
* 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,
* and probably do not have getnameinfo(), but have the old gethostbyaddr()
* function.
*
* GNU C library 2.0.x is known to lack this function, even though it defines
* getaddrinfo().
*/
#ifdef WIN32
static int WSAAPI
stub_getnameinfo (const struct sockaddr *sa, socklen_t salen,
char *host, DWORD hostlen, char *serv, DWORD servlen, int flags)
#else
static int
stub_getnameinfo (const struct sockaddr *sa, socklen_t salen,
char *host, int hostlen, char *serv, int servlen, int flags)
#endif
{
if (((size_t)salen < sizeof (struct sockaddr_in))
|| (sa->sa_family != AF_INET))
return EAI_FAMILY;
else if (flags & (~_NI_MASK))
return EAI_BADFLAGS;
else
{
const struct sockaddr_in *addr;
addr = (const struct sockaddr_in *)sa;
if (host != NULL)
{
/* host name resolution */
if (!(flags & NI_NUMERICHOST))
{
if (flags & NI_NAMEREQD)
return EAI_NONAME;
}
/* inet_ntoa() is not thread-safe, do not use it */
uint32_t ipv4 = ntohl (addr->sin_addr.s_addr);
if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24,
(ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff,
ipv4 & 0xff) >= (int)hostlen)
return EAI_OVERFLOW;
}
if (serv != NULL)
{
if (snprintf (serv, servlen, "%u",
(unsigned int)ntohs (addr->sin_port)) >= (int)servlen)
return EAI_OVERFLOW;
}
}
return 0;
}
#undef getnameinfo
#define getnameifo stub_getnameinfo
#endif /* if !HAVE_GETNAMEINFO */
#ifndef HAVE_GETADDRINFO
#define _AI_MASK (AI_PASSIVE|AI_CANONNAME|AI_NUMERICHOST)
/*
* Converts the current herrno error value into an EAI_* error code.
* That error code is normally returned by getnameinfo() or getaddrinfo().
*/
static int
gai_error_from_herrno (void)
{
switch (h_errno)
{
case HOST_NOT_FOUND:
return EAI_NONAME;
case NO_ADDRESS:
# if (NO_ADDRESS != NO_DATA)
case NO_DATA:
# endif
return EAI_NODATA;
case NO_RECOVERY:
return EAI_FAIL;
case TRY_AGAIN:
return EAI_AGAIN;
}
return EAI_SYSTEM;
}
/*
* This functions must be used to free the memory allocated by getaddrinfo().
*/
#ifdef WIN32
static void WSAAPI stub_freeaddrinfo (struct addrinfo *res)
#else
static void stub_freeaddrinfo (struct addrinfo *res)
#endif
{
if (res == NULL)
return;
free (res->ai_canonname);
free (res->ai_addr);
free (res->ai_next);
free (res);
}
/*
* Internal function that builds an addrinfo struct.
*/
static struct addrinfo *
makeaddrinfo (int af, int type, int proto,
const struct sockaddr *addr, size_t addrlen,
const char *canonname)
{
struct addrinfo *res;
res = (struct addrinfo *)malloc (sizeof (struct addrinfo));
if (res != NULL)
{
res->ai_flags = 0;
res->ai_family = af;
res->ai_socktype = type;
res->ai_protocol = proto;
res->ai_addrlen = addrlen;
res->ai_addr = malloc (addrlen);
res->ai_canonname = NULL;
res->ai_next = NULL;
if (res->ai_addr != NULL)
{
memcpy (res->ai_addr, addr, addrlen);
if (canonname != NULL)
{
res->ai_canonname = strdup (canonname);
if (res->ai_canonname != NULL)
return res; /* success ! */
}
else
return res;
}
}
/* failsafe */
vlc_freeaddrinfo (res);
return NULL;
}
static struct addrinfo *
makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
{
struct sockaddr_in addr;
memset (&addr, 0, sizeof (addr));
addr.sin_family = AF_INET;
# ifdef HAVE_SA_LEN
addr.sin_len = sizeof (addr);
# endif
addr.sin_port = port;
addr.sin_addr.s_addr = ip;
return makeaddrinfo (AF_INET, type, proto,
(struct sockaddr*)&addr, sizeof (addr), name);
}
/*
* getaddrinfo() non-thread-safe IPv4-only implementation
* 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().
*
* Only UDP and TCP over IPv4 are supported here.
*/
#ifdef WIN32
static int WSAAPI
stub_getaddrinfo (const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res)
#else
static int
stub_getaddrinfo (const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res)
#endif
{
struct addrinfo *info;
u_long ip;
u_short port;
int protocol = 0, flags = 0;
const char *name = NULL;
#ifdef WIN32
/*
* Maybe you knew already that Winsock does not handle TCP/RST packets
* properly, so that when a TCP connection fails, it will wait until it
* times out even if the remote host did return a TCP/RST. However, it
* still sees the TCP/RST as the error code is 10061 instead of 10060.
* Basically, we have the stupid brainfucked behavior with DNS queries...
* When the recursive DNS server returns an error, Winsock waits about
* 2 seconds before it returns to the callers, even though it should know
* that is pointless. I'd like to know how come this hasn't been fixed
* for the past decade, or maybe not.
*
* Anyway, this is causing a severe delay when the SAP listener tries
* to resolve more than ten IPv6 numeric addresses. Modern systems will
* eventually realize that it is an IPv6 address, and won't try to resolve
* it as a IPv4 address via the Domain Name Service. Old systems
* (including Windows XP without the IPv6 stack) will not. It is normally
* not an issue as the DNS server usually returns an error very quickly.
* But it IS a severe issue on Windows, given the bug explained above.
* So here comes one more bug-to-bug Windows compatibility fix.
*/
if ((node != NULL) && (strchr (node, ':') != NULL))
return EAI_NONAME;
#endif
if (hints != NULL)
{
flags = hints->ai_flags;
if (flags & ~_AI_MASK)
return EAI_BADFLAGS;
/* only accept AF_INET and AF_UNSPEC */
if (hints->ai_family && (hints->ai_family != AF_INET))
return EAI_FAMILY;
/* protocol sanity check */
switch (hints->ai_socktype)
{
case SOCK_STREAM:
protocol = IPPROTO_TCP;
break;
case SOCK_DGRAM:
protocol = IPPROTO_UDP;
break;
#ifndef SOCK_RAW
case SOCK_RAW:
#endif
case 0:
break;
default:
return EAI_SOCKTYPE;
}
if (hints->ai_protocol && protocol
&& (protocol != hints->ai_protocol))
return EAI_SERVICE;
}
*res = NULL;
/* default values */
if (node == NULL)
{
if (flags & AI_PASSIVE)
ip = htonl (INADDR_ANY);
else
ip = htonl (INADDR_LOOPBACK);
}
else
if ((ip = inet_addr (node)) == INADDR_NONE)
{
struct hostent *entry = NULL;
/* hostname resolution */
if (!(flags & AI_NUMERICHOST))
entry = gethostbyname (node);
if (entry == NULL)
return gai_error_from_herrno ();
if ((entry->h_length != 4) || (entry->h_addrtype != AF_INET))
return EAI_FAMILY;
ip = *((u_long *) entry->h_addr);
if (flags & AI_CANONNAME)
name = entry->h_name;
}
if ((flags & AI_CANONNAME) && (name == NULL))
name = node;
/* service resolution */
if (service == NULL)
port = 0;
else
{
unsigned long d;
char *end;
d = strtoul (service, &end, 0);
if (end[0] || (d > 65535u))
return EAI_SERVICE;
port = htons ((u_short)d);
}
/* building results... */
if ((!protocol) || (protocol == IPPROTO_UDP))
{
info = makeipv4info (SOCK_DGRAM, IPPROTO_UDP, ip, port, name);
if (info == NULL)
{
errno = ENOMEM;
return EAI_SYSTEM;
}
if (flags & AI_PASSIVE)
info->ai_flags |= AI_PASSIVE;
*res = info;
}
if ((!protocol) || (protocol == IPPROTO_TCP))
{
info = makeipv4info (SOCK_STREAM, IPPROTO_TCP, ip, port, name);
if (info == NULL)
{
errno = ENOMEM;
return EAI_SYSTEM;
}
info->ai_next = *res;
if (flags & AI_PASSIVE)
info->ai_flags |= AI_PASSIVE;
*res = info;
}
return 0;
}
#undef getaddrinfo
#define getaddrifo stub_getaddrinfo
#undef freeaddrinfo
#define freeaddrifo stub_freeaddrinfo
#endif /* if !HAVE_GETADDRINFO */
#if defined( WIN32 ) && !defined( UNDER_CE )
/*
* Here is the kind of kludge you need to keep binary compatibility among
* varying OS versions...
*/
typedef int (WSAAPI * GETNAMEINFO) ( const struct sockaddr FAR *, socklen_t,
char FAR *, DWORD, char FAR *, DWORD, int );
typedef int (WSAAPI * GETADDRINFO) (const char FAR *, const char FAR *,
const struct addrinfo FAR *,
struct addrinfo FAR * FAR *);
typedef void (WSAAPI * FREEADDRINFO) ( struct addrinfo FAR * );
static int WSAAPI _ws2_getnameinfo_bind ( const struct sockaddr FAR *, socklen_t,
char FAR *, DWORD, char FAR *, DWORD, int );
static int WSAAPI _ws2_getaddrinfo_bind (const char FAR *, const char FAR *,
const struct addrinfo FAR *,
struct addrinfo FAR * FAR *);
static GETNAMEINFO ws2_getnameinfo = _ws2_getnameinfo_bind;
static GETADDRINFO ws2_getaddrinfo = _ws2_getaddrinfo_bind;
static FREEADDRINFO ws2_freeaddrinfo;
static FARPROC ws2_find_api (LPCTSTR name)
{
FARPROC f = NULL;
HMODULE m = GetModuleHandle (TEXT("WS2_32"));
if (m != NULL)
f = GetProcAddress (m, name);
if (f == NULL)
{
/* Windows 2K IPv6 preview */
m = LoadLibrary (TEXT("WSHIP6"));
if (m != NULL)
f = GetProcAddress (m, name);
}
return f;
}
static WSAAPI int _ws2_getnameinfo_bind( const struct sockaddr FAR * sa, socklen_t salen,
char FAR *host, DWORD hostlen, char FAR *serv, DWORD servlen, int flags )
{
GETNAMEINFO entry = (GETNAMEINFO)ws2_find_api (TEXT("getnameinfo"));
int result;
if (entry == NULL)
{
/* not found, use replacement API instead */
entry = stub_getnameinfo;
}
/* call API before replacing function pointer to avoid crash */
result = entry (sa, salen, host, hostlen, serv, servlen, flags);
ws2_getnameinfo = entry;
return result;
}
#undef getnameinfo
#define getnameinfo ws2_getnameinfo
static WSAAPI int _ws2_getaddrinfo_bind(const char FAR *node, const char FAR *service,
const struct addrinfo FAR *hints, struct addrinfo FAR * FAR *res)
{
GETADDRINFO entry;
FREEADDRINFO freentry;
int result;
entry = (GETADDRINFO)ws2_find_api (TEXT("getaddrinfo"));
freentry = (FREEADDRINFO)ws2_find_api (TEXT("freeaddrinfo"));
if ((entry == NULL) || (freentry == NULL))
{
/* not found, use replacement API instead */
entry = stub_getaddrinfo;
freentry = stub_freeaddrinfo;
}
/* call API before replacing function pointer to avoid crash */
result = entry (node, service, hints, res);
ws2_freeaddrinfo = freentry;
ws2_getaddrinfo = entry;
return result;
}
#undef getaddrinfo
#undef freeaddrinfo
#define getaddrinfo ws2_getaddrinfo
#define freeaddrinfo ws2_freeaddrinfo
#define HAVE_GETADDRINFO
#endif
int vlc_getnameinfo( const struct sockaddr *sa, int salen, int vlc_getnameinfo( const struct sockaddr *sa, int salen,
char *host, int hostlen, int *portnum, int flags ) char *host, int hostlen, int *portnum, int flags )
{ {
......
...@@ -141,7 +141,7 @@ int *net_Listen (vlc_object_t *p_this, const char *psz_host, ...@@ -141,7 +141,7 @@ int *net_Listen (vlc_object_t *p_this, const char *psz_host,
if (i_val) if (i_val)
{ {
msg_Err (p_this, "Cannot resolve %s port %d : %s", psz_host, i_port, msg_Err (p_this, "Cannot resolve %s port %d : %s", psz_host, i_port,
vlc_gai_strerror (i_val)); gai_strerror (i_val));
return NULL; return NULL;
} }
......
...@@ -142,7 +142,7 @@ int net_Connect( vlc_object_t *p_this, const char *psz_host, int i_port, ...@@ -142,7 +142,7 @@ int net_Connect( vlc_object_t *p_this, const char *psz_host, int i_port,
if( i_val ) if( i_val )
{ {
msg_Err( p_this, "cannot resolve %s port %d : %s", psz_realhost, msg_Err( p_this, "cannot resolve %s port %d : %s", psz_realhost,
i_realport, vlc_gai_strerror( i_val ) ); i_realport, gai_strerror( i_val ) );
return -1; return -1;
} }
......
...@@ -155,7 +155,7 @@ static int net_ListenSingle (vlc_object_t *obj, const char *host, int port, ...@@ -155,7 +155,7 @@ static int net_ListenSingle (vlc_object_t *obj, const char *host, int port,
if (val) if (val)
{ {
msg_Err (obj, "Cannot resolve %s port %d : %s", host, port, msg_Err (obj, "Cannot resolve %s port %d : %s", host, port,
vlc_gai_strerror (val)); gai_strerror (val));
return -1; return -1;
} }
...@@ -661,7 +661,7 @@ int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port, ...@@ -661,7 +661,7 @@ int net_ConnectDgram( vlc_object_t *p_this, const char *psz_host, int i_port,
if( i_val ) if( i_val )
{ {
msg_Err( p_this, "cannot resolve [%s]:%d : %s", psz_host, i_port, msg_Err( p_this, "cannot resolve [%s]:%d : %s", psz_host, i_port,
vlc_gai_strerror( i_val ) ); gai_strerror( i_val ) );
return -1; return -1;
} }
...@@ -764,7 +764,7 @@ int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind, ...@@ -764,7 +764,7 @@ int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
if (val) if (val)
{ {
msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind, msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
vlc_gai_strerror (val)); gai_strerror (val));
return -1; return -1;
} }
...@@ -773,7 +773,7 @@ int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind, ...@@ -773,7 +773,7 @@ int net_OpenDgram( vlc_object_t *obj, const char *psz_bind, int i_bind,
if (val) if (val)
{ {
msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind, msg_Err (obj, "cannot resolve %s port %d : %s", psz_bind, i_bind,
vlc_gai_strerror (val)); gai_strerror (val));
vlc_freeaddrinfo (rem); vlc_freeaddrinfo (rem);
return -1; return -1;
} }
......
...@@ -307,7 +307,7 @@ int SAP_Add (sap_handler_t *p_sap, session_descriptor_t *p_session) ...@@ -307,7 +307,7 @@ int SAP_Add (sap_handler_t *p_sap, session_descriptor_t *p_session)
if( i ) if( i )
{ {
msg_Err( p_sap, "%s", vlc_gai_strerror( i ) ); msg_Err( p_sap, "%s", gai_strerror( i ) );
return VLC_EGENERIC; return VLC_EGENERIC;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment