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