# include "config.h"
#endif
-#include <vlc/vlc.h>
+#include <vlc_common.h>
#include <stddef.h> /* size_t */
#include <string.h> /* strlen(), memcpy(), memset(), strchr() */
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,
*/
#ifdef WIN32
static int WSAAPI
-getnameinfo (const struct sockaddr *sa, socklen_t salen,
+stub_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,
+stub_getnameinfo (const struct sockaddr *sa, socklen_t salen,
char *host, int hostlen, char *serv, int servlen, int flags)
#endif
{
}
return 0;
}
-
+#undef getnameinfo
+#define getnameifo stub_getnameinfo
#endif /* if !HAVE_GETNAMEINFO */
#ifndef HAVE_GETADDRINFO
* This functions must be used to free the memory allocated by getaddrinfo().
*/
#ifdef WIN32
-static void WSAAPI freeaddrinfo (struct addrinfo *res)
+static void WSAAPI stub_freeaddrinfo (struct addrinfo *res)
#else
-static void freeaddrinfo (struct addrinfo *res)
+static void stub_freeaddrinfo (struct addrinfo *res)
#endif
{
- if (res != NULL)
- {
- if (res->ai_canonname != NULL)
- free (res->ai_canonname);
- if (res->ai_addr != NULL)
- free (res->ai_addr);
- if (res->ai_next != NULL)
- free (res->ai_next);
- free (res);
- }
+ if (res == NULL)
+ return;
+ free (res->ai_canonname);
+ free (res->ai_addr);
+ free (res->ai_next);
+ free (res);
}
/*
* 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().
*/
#ifdef WIN32
static int WSAAPI
-getaddrinfo (const char *node, const char *service,
+stub_getaddrinfo (const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res)
#else
static int
-getaddrinfo (const char *node, const char *service,
+stub_getaddrinfo (const char *node, const char *service,
const struct addrinfo *hints, struct addrinfo **res)
#endif
{
return 0;
}
+#undef getaddrinfo
+#define getaddrifo stub_getaddrinfo
+#undef freeaddrinfo
+#define freeaddrifo stub_freeaddrinfo
#endif /* if !HAVE_GETADDRINFO */
#if defined( WIN32 ) && !defined( UNDER_CE )
if (entry == NULL)
{
/* not found, use replacement API instead */
- entry = getnameinfo;
-
+ entry = stub_getnameinfo;
}
/* call API before replacing function pointer to avoid crash */
result = entry (sa, salen, host, hostlen, serv, servlen, flags);
if ((entry == NULL) || (freentry == NULL))
{
/* not found, use replacement API instead */
- entry = getaddrinfo;
- freentry = freeaddrinfo;
+ entry = stub_getaddrinfo;
+ freentry = stub_freeaddrinfo;
}
/* call API before replacing function pointer to avoid crash */
result = entry (node, service, hints, res);
#endif
-
int vlc_getnameinfo( const struct sockaddr *sa, int salen,
char *host, int hostlen, int *portnum, int flags )
{
AI_PASSIVE |
AI_CANONNAME |
AI_NUMERICHOST |
-#ifdef AI_NUMERICSERV
AI_NUMERICSERV |
-#endif
#ifdef AI_ALL
AI_ALL |
#endif
hints.ai_flags = p_hints->ai_flags & safe_flags;
}
-#ifdef AI_NUMERICSERV
/* We only ever use port *numbers* */
hints.ai_flags |= AI_NUMERICSERV;
-#endif
if( hints.ai_family == AF_UNSPEC )
{
{
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 */
+}
+