Commit c61b8e61 authored by Damien Fouilleul's avatar Damien Fouilleul

- getaddrinfo.c : cleanup replacement APIs for win32 and make sure the shim...

- getaddrinfo.c : cleanup replacement APIs for win32 and make sure the shim logic only searches for real APIs only once
parent eb72bd56
...@@ -113,9 +113,15 @@ const char *vlc_gai_strerror (int errnum) ...@@ -113,9 +113,15 @@ const char *vlc_gai_strerror (int errnum)
* GNU C library 2.0.x is known to lack this function, even though it defines * GNU C library 2.0.x is known to lack this function, even though it defines
* getaddrinfo(). * getaddrinfo().
*/ */
static int #ifdef WIN32
static int WSAAPI
getnameinfo (const struct sockaddr *sa, socklen_t salen,
char *host, DWORD hostlen, char *serv, DWORD servlen, int flags)
#else
static int
getnameinfo (const struct sockaddr *sa, socklen_t salen, getnameinfo (const struct sockaddr *sa, socklen_t salen,
char *host, int hostlen, char *serv, int servlen, int flags) char *host, int hostlen, char *serv, int servlen, int flags)
#endif
{ {
if (((size_t)salen < sizeof (struct sockaddr_in)) if (((size_t)salen < sizeof (struct sockaddr_in))
|| (sa->sa_family != AF_INET)) || (sa->sa_family != AF_INET))
...@@ -142,14 +148,14 @@ getnameinfo (const struct sockaddr *sa, socklen_t salen, ...@@ -142,14 +148,14 @@ getnameinfo (const struct sockaddr *sa, socklen_t salen,
if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24, if (snprintf (host, hostlen, "%u.%u.%u.%u", ipv4 >> 24,
(ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff, (ipv4 >> 16) & 0xff, (ipv4 >> 8) & 0xff,
ipv4 & 0xff) >= hostlen) ipv4 & 0xff) >= (int)hostlen)
return EAI_OVERFLOW; return EAI_OVERFLOW;
} }
if (serv != NULL) if (serv != NULL)
{ {
if (snprintf (serv, servlen, "%u", if (snprintf (serv, servlen, "%u",
(unsigned int)ntohs (addr->sin_port)) >= servlen) (unsigned int)ntohs (addr->sin_port)) >= (int)servlen)
return EAI_OVERFLOW; return EAI_OVERFLOW;
} }
} }
...@@ -190,7 +196,11 @@ gai_error_from_herrno (void) ...@@ -190,7 +196,11 @@ gai_error_from_herrno (void)
/* /*
* This functions must be used to free the memory allocated by getaddrinfo(). * This functions must be used to free the memory allocated by getaddrinfo().
*/ */
#ifdef WIN32
static void WSAAPI freeaddrinfo (struct addrinfo *res)
#else
static void freeaddrinfo (struct addrinfo *res) static void freeaddrinfo (struct addrinfo *res)
#endif
{ {
if (res != NULL) if (res != NULL)
{ {
...@@ -274,9 +284,15 @@ makeipv4info (int type, int proto, u_long ip, u_short port, const char *name) ...@@ -274,9 +284,15 @@ makeipv4info (int type, int proto, u_long ip, u_short port, const char *name)
* *
* Only UDP and TCP over IPv4 are supported here. * Only UDP and TCP over IPv4 are supported here.
*/ */
static int #ifdef WIN32
static int WSAAPI
getaddrinfo (const char *node, const char *service, getaddrinfo (const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res) const struct addrinfo *hints, struct addrinfo **res)
#else
static int
getaddrinfo (const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res)
#endif
{ {
struct addrinfo *info; struct addrinfo *info;
u_long ip; u_long ip;
...@@ -493,43 +509,43 @@ static WSAAPI int _ws2_getnameinfo_bind( const struct sockaddr FAR * sa, socklen ...@@ -493,43 +509,43 @@ static WSAAPI int _ws2_getnameinfo_bind( const struct sockaddr FAR * sa, socklen
char FAR *host, DWORD hostlen, char FAR *serv, DWORD servlen, int flags ) char FAR *host, DWORD hostlen, char FAR *serv, DWORD servlen, int flags )
{ {
GETNAMEINFO entry = (GETNAMEINFO)ws2_find_api (TEXT("getnameinfo")); GETNAMEINFO entry = (GETNAMEINFO)ws2_find_api (TEXT("getnameinfo"));
if (entry != NULL) int result;
if (entry == NULL)
{ {
/* call API before replacing function pointer to avoid crash */ /* not found, use replacement API instead */
int result = entry (sa, salen, host, hostlen, serv, servlen, flags); entry = getnameinfo;
ws2_getnameinfo = entry;
return result;
} }
return getnameinfo (sa, salen, host, hostlen, serv, servlen, flags); /* 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 #undef getnameinfo
#define getnameinfo ws2_getnameinfo #define getnameinfo ws2_getnameinfo
/* So much for using different calling conventions */
static WSAAPI void call_freeaddrinfo (struct addrinfo *infos)
{
freeaddrinfo (infos);
}
static WSAAPI int _ws2_getaddrinfo_bind(const char FAR *node, const char FAR *service, static WSAAPI int _ws2_getaddrinfo_bind(const char FAR *node, const char FAR *service,
const struct addrinfo FAR *hints, struct addrinfo FAR * FAR *res) const struct addrinfo FAR *hints, struct addrinfo FAR * FAR *res)
{ {
GETADDRINFO entry; GETADDRINFO entry;
FREEADDRINFO freentry; FREEADDRINFO freentry;
int result;
entry = (GETADDRINFO)ws2_find_api (TEXT("getaddrinfo")); entry = (GETADDRINFO)ws2_find_api (TEXT("getaddrinfo"));
freentry = (FREEADDRINFO)ws2_find_api (TEXT("freeaddrinfo")); freentry = (FREEADDRINFO)ws2_find_api (TEXT("freeaddrinfo"));
if ((entry != NULL) && (freentry != NULL)) if ((entry == NULL) || (freentry == NULL))
{ {
/* call API before replacing function pointer to avoid crash */ /* not found, use replacement API instead */
int result = entry (node, service, hints, res); entry = getaddrinfo;
ws2_freeaddrinfo = freentry; freentry = freeaddrinfo;
ws2_getaddrinfo = entry;
return result;
} }
ws2_freeaddrinfo = call_freeaddrinfo; /* call API before replacing function pointer to avoid crash */
return getaddrinfo (node, service, hints, res); result = entry (node, service, hints, res);
ws2_freeaddrinfo = freentry;
ws2_getaddrinfo = entry;
return result;
} }
#undef getaddrinfo #undef getaddrinfo
#undef freeaddrinfo #undef freeaddrinfo
......
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