Commit b11a9ecc authored by Kai Vehmanen's avatar Kai Vehmanen
Browse files

Updated nice/address interface with better support for passing information to...

Updated nice/address interface with better support for passing information to and from sockaddr structs.

darcs-hash:20070521153312-77cd4-b5363484d77627e8560e2ebd3735bbf68500c0dc.gz
parent 9ab8d5b2
......@@ -23,6 +23,7 @@
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
* Kai Vehmanen, Nokia
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
......@@ -89,26 +90,60 @@ nice_address_set_ipv4_from_string (NiceAddress *addr, const gchar *str)
}
}
/**
* Sets address to match socket address struct 'sin'.
*/
void
nice_address_set_from_sockaddr_in (NiceAddress *addr, struct sockaddr_in *sin)
nice_address_set_from_sockaddr (NiceAddress *addr, const struct sockaddr *sin)
{
if (sin->sin_family == AF_INET6)
const struct sockaddr_in *sin4 = (const struct sockaddr_in *)sin;
const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)sin;
if (sin4->sin_family == AF_INET)
{
addr->type = NICE_ADDRESS_TYPE_IPV4;
nice_address_set_ipv4 (addr, ntohl (sin4->sin_addr.s_addr));
}
else if (sin4->sin_family == AF_INET6)
{
addr->type = NICE_ADDRESS_TYPE_IPV6;
nice_address_set_ipv6 (addr,
(gchar *) &((struct sockaddr_in6 *) sin)->sin6_addr);
(gchar *) &sin6->sin6_addr);
}
else
{
addr->type = NICE_ADDRESS_TYPE_IPV4;
nice_address_set_ipv4 (addr, ntohl (sin->sin_addr.s_addr));
}
g_assert_not_reached ();
addr->port = ntohs (sin->sin_port);
addr->port = ntohs (sin4->sin_port);
}
/**
* Copies IPv4 NiceAddrress to socket address struct 'sin'.
*/
void
nice_address_copy_to_sockaddr (const NiceAddress *addr, struct sockaddr *sin)
{
struct sockaddr_in *sin4 = (struct sockaddr_in *)sin;
struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)sin;
g_assert (sin);
if (addr->type == NICE_ADDRESS_TYPE_IPV4)
{
sin4->sin_family = AF_INET;
sin4->sin_addr.s_addr = htonl (addr->addr_ipv4);
}
else if (addr->type == NICE_ADDRESS_TYPE_IPV6)
{
sin4->sin_family = AF_INET6;
memcpy (&sin6->sin6_addr.s6_addr, addr->addr_ipv6, sizeof (addr->addr_ipv6));
}
else
g_assert_not_reached ();
sin4->sin_port = htons (addr->port);
}
void
nice_address_to_string (NiceAddress *addr, gchar *dst)
{
......@@ -131,7 +166,7 @@ nice_address_to_string (NiceAddress *addr, gchar *dst)
gboolean
nice_address_equal (NiceAddress *a, NiceAddress *b)
nice_address_equal (const NiceAddress *a, const NiceAddress *b)
{
if (a->type != b->type)
return FALSE;
......@@ -139,6 +174,9 @@ nice_address_equal (NiceAddress *a, NiceAddress *b)
if (a->type == NICE_ADDRESS_TYPE_IPV4)
return (a->addr_ipv4 == b->addr_ipv4) && (a->port == b->port);
if (a->type == NICE_ADDRESS_TYPE_IPV6)
return (memcmp (a->addr_ipv6, b->addr_ipv6, INET_ADDRSTRLEN) == 0) && (a->port == b->port);
g_assert_not_reached ();
}
......
......@@ -54,14 +54,14 @@ typedef enum
typedef struct _NiceAddress NiceAddress;
/* XXX: need access to fields to convert to sockaddr_in */
/* note: clients need to know the storage size, so needs to be public */
struct _NiceAddress
{
NiceAddressType type;
union
{
guint32 addr_ipv4;
guchar addr_ipv6[16];
guchar addr_ipv6[INET_ADDRSTRLEN];
};
guint16 port;
};
......@@ -86,10 +86,13 @@ gboolean
nice_address_set_ipv4_from_string (NiceAddress *addr, const gchar *str);
void
nice_address_set_from_sockaddr_in (NiceAddress *addr, struct sockaddr_in *sin);
nice_address_set_from_sockaddr (NiceAddress *addr, const struct sockaddr *sin);
void
nice_address_copy_to_sockaddr (const NiceAddress *addr, struct sockaddr *sin);
gboolean
nice_address_equal (NiceAddress *a, NiceAddress *b);
nice_address_equal (const NiceAddress *a, const NiceAddress *b);
void
nice_address_to_string (NiceAddress *addr, gchar *dst);
......
......@@ -23,6 +23,7 @@
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
* Kai Vehmanen, Nokia
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
......@@ -37,25 +38,46 @@
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include "address.h"
static void
test_ipv4 (void)
{
NiceAddress addr = {0,};
NiceAddress other = {0,};
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);
memset (&addr, 0, sizeof (addr));
memset (&other, 0, sizeof (other));
nice_address_set_ipv4 (&addr, 0x01020304);
g_assert (addr.type == NICE_ADDRESS_TYPE_IPV4);
nice_address_to_string (&addr, str);
g_assert (0 == strcmp (str, "1.2.3.4"));
nice_address_to_string (&addr, str);
/* same address */
nice_address_set_ipv4 (&other, 0x01020304);
g_assert (TRUE == nice_address_equal (&addr, &other));
/* from sockaddr_in */
other.port = 9876; /* in native byte order */
other.type = NICE_ADDRESS_TYPE_IPV4;
nice_address_set_from_sockaddr (&addr, (struct sockaddr*)&sin);
nice_address_to_string (&addr, str);
nice_address_to_string (&other, str);
g_assert (TRUE == nice_address_equal (&addr, &other));
/* different IP */
nice_address_set_ipv4 (&other, 0x01020305);
g_assert (FALSE == nice_address_equal (&addr, &other));
......@@ -69,9 +91,15 @@ test_ipv4 (void)
static void
test_ipv6 (void)
{
NiceAddress addr = {0,};
NiceAddress addr, other;
gchar str[NICE_ADDRESS_STRING_LEN];
struct sockaddr_in6 sin;
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);
memset (&addr, 0, sizeof (addr));
nice_address_set_ipv6 (&addr,
"\x00\x11\x22\x33"
"\x44\x55\x66\x77"
......@@ -81,6 +109,12 @@ test_ipv6 (void)
nice_address_to_string (&addr, str);
g_assert (0 == strcmp (str, "11:2233:4455:6677:8899:aabb:ccdd:eeff"));
addr.port = 9876; /* in native byte order */
nice_address_set_from_sockaddr (&other, (struct sockaddr*)&sin);
nice_address_to_string (&addr, str);
nice_address_to_string (&other, str);
g_assert (TRUE == nice_address_equal (&addr, &other));
}
int
......
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