Commit f61c50c3 authored by Youness Alaoui's avatar Youness Alaoui

Fix address.[ch] with its test for working and behaving correctly on windows

parent 600a0595
......@@ -121,6 +121,21 @@ nice_address_get_port (const NiceAddress *addr)
NICEAPI_EXPORT gboolean
nice_address_set_from_string (NiceAddress *addr, const gchar *str)
{
#ifdef G_OS_WIN32
union {
struct sockaddr addr;
struct sockaddr_in ip4;
struct sockaddr_in6 ip6;
} s;
int len4 = sizeof(s.ip4);
int len6 = sizeof(s.ip6);
if (WSAStringToAddress((char *)str, AF_INET, NULL, &s.addr, &len4) == 0)
nice_address_set_from_sockaddr (addr, &s.addr);
else if (WSAStringToAddress((char *)str, AF_INET6, NULL, &s.addr, &len6) == 0)
nice_address_set_from_sockaddr (addr, &s.addr);
else
return FALSE; /* Invalid address */
#else
union
{
struct in_addr ipv4;
......@@ -129,11 +144,11 @@ nice_address_set_from_string (NiceAddress *addr, const gchar *str)
if (inet_pton (AF_INET, str, &a.ipv4) > 0)
nice_address_set_ipv4 (addr, ntohl (a.ipv4.s_addr));
else
if (inet_pton (AF_INET6, str, &a.ipv6) > 0)
else if (inet_pton (AF_INET6, str, &a.ipv6) > 0)
nice_address_set_ipv6 (addr, a.ipv6.s6_addr);
else
return FALSE; /* Invalid address */
#endif
return TRUE;
}
......@@ -188,10 +203,29 @@ nice_address_copy_to_sockaddr (const NiceAddress *addr,
NICEAPI_EXPORT void
nice_address_to_string (const NiceAddress *addr, gchar *dst)
{
#ifdef G_OS_WIN32
DWORD len;
int ret;
switch (addr->s.addr.sa_family) {
case AF_INET:
len = INET_ADDRSTRLEN;
ret = WSAAddressToString ((LPSOCKADDR)&addr->s.ip4, sizeof(addr->s.ip6),
NULL, dst, &len);
break;
case AF_INET6:
len = INET6_ADDRSTRLEN;
ret = WSAAddressToString ((LPSOCKADDR)&addr->s.ip6, sizeof(addr->s.ip6),
NULL, dst, &len);
break;
default:
g_assert_not_reached();
}
#else
const gchar *ret = NULL;
switch (addr->s.addr.sa_family)
{
switch (addr->s.addr.sa_family) {
case AF_INET:
ret = inet_ntop (AF_INET, &addr->s.ip4.sin_addr, dst, INET_ADDRSTRLEN);
break;
......@@ -202,8 +236,7 @@ nice_address_to_string (const NiceAddress *addr, gchar *dst)
default:
g_assert_not_reached();
}
g_assert (ret == dst);
#endif
}
......
......@@ -42,6 +42,7 @@
#ifdef G_OS_WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
......@@ -93,7 +94,6 @@ nice_address_set_port (NiceAddress *addr, guint port);
guint
nice_address_get_port (const NiceAddress *addr);
G_GNUC_WARN_UNUSED_RESULT
gboolean
nice_address_set_from_string (NiceAddress *addr, const gchar *str);
......
......@@ -40,11 +40,6 @@
#endif
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "address.h"
static void
......@@ -53,11 +48,6 @@ test_ipv4 (void)
NiceAddress addr;
NiceAddress other;
gchar str[NICE_ADDRESS_STRING_LEN];
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons (9876);
g_assert (inet_pton (AF_INET, "1.2.3.4", &sin.sin_addr) > 0);
nice_address_init (&addr);
nice_address_init (&other);
......@@ -76,7 +66,8 @@ test_ipv4 (void)
/* from sockaddr_in */
nice_address_set_port (&other, 9876); /* in native byte order */
other.s.ip4.sin_family = AF_INET;
nice_address_set_from_sockaddr (&addr, (struct sockaddr*)&sin);
nice_address_set_from_string (&addr, "1.2.3.4");
nice_address_set_port (&addr, 9876); /* in native byte order */
nice_address_to_string (&addr, str);
nice_address_to_string (&other, str);
g_assert (TRUE == nice_address_equal (&addr, &other));
......@@ -112,10 +103,6 @@ test_ipv6 (void)
memset (&sin, 0, sizeof (sin));
memset (&sin2, 0, sizeof (sin2));
sin.sin6_family = AF_INET6;
sin.sin6_port = htons (9876);
g_assert (inet_pton (AF_INET6, "11:2233:4455:6677:8899:aabb:ccdd:eeff", &sin.sin6_addr) > 0);
nice_address_init (&addr);
nice_address_set_ipv6 (&addr, (guchar *)
"\x00\x11\x22\x33"
......@@ -128,8 +115,11 @@ test_ipv6 (void)
g_assert (0 == strcmp (str, "11:2233:4455:6677:8899:aabb:ccdd:eeff"));
nice_address_set_port (&addr, 9876); /* in native byte order */
nice_address_set_from_sockaddr (&other, (struct sockaddr*)&sin);
nice_address_set_from_string (&other, "11:2233:4455:6677:8899:aabb:ccdd:eeff");
nice_address_set_port (&other, 9876); /* in native byte order */
nice_address_copy_to_sockaddr (&other, (struct sockaddr*)&sin2);
nice_address_copy_to_sockaddr (&addr, (struct sockaddr*)&sin);
g_assert (memcmp (&sin, &sin2, sizeof(sin)) == 0);
nice_address_to_string (&addr, str);
nice_address_to_string (&other, str);
......@@ -160,8 +150,19 @@ test_ipv6 (void)
int
main (void)
{
#ifdef G_OS_WIN32
WSADATA w;
#endif
#ifdef G_OS_WIN32
WSAStartup(0x0202, &w);
#endif
test_ipv4 ();
test_ipv6 ();
#ifdef G_OS_WIN32
WSACleanup();
#endif
return 0;
}
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