Commit 5ca6a461 authored by Dafydd Harries's avatar Dafydd Harries

make nice_address_to_string() write result into passed-in buffer; define constant for buffer size

darcs-hash:20070314144042-c9803-efe6600250d3bad6dd392dda7a3734db99f60cb1.gz
parent 688d3cb9
...@@ -73,26 +73,24 @@ nice_address_set_from_sockaddr_in (NiceAddress *addr, struct sockaddr_in *sin) ...@@ -73,26 +73,24 @@ nice_address_set_from_sockaddr_in (NiceAddress *addr, struct sockaddr_in *sin)
} }
gchar * void
nice_address_to_string (NiceAddress *addr) nice_address_to_string (NiceAddress *addr, gchar *dst)
{ {
struct in_addr iaddr = {0,}; struct in_addr iaddr = {0,};
gchar ip_str[INET6_ADDRSTRLEN] = {0,};
const gchar *ret = NULL; const gchar *ret = NULL;
switch (addr->type) switch (addr->type)
{ {
case NICE_ADDRESS_TYPE_IPV4: case NICE_ADDRESS_TYPE_IPV4:
iaddr.s_addr = htonl (addr->addr_ipv4); iaddr.s_addr = htonl (addr->addr_ipv4);
ret = inet_ntop (AF_INET, &iaddr, ip_str, INET_ADDRSTRLEN); ret = inet_ntop (AF_INET, &iaddr, dst, INET_ADDRSTRLEN);
break; break;
case NICE_ADDRESS_TYPE_IPV6: case NICE_ADDRESS_TYPE_IPV6:
ret = inet_ntop (AF_INET6, &addr->addr_ipv6, ip_str, INET6_ADDRSTRLEN); ret = inet_ntop (AF_INET6, &addr->addr_ipv6, dst, INET6_ADDRSTRLEN);
break; break;
} }
g_assert (ret == ip_str); g_assert (ret == dst);
return g_strdup (ip_str);
} }
......
...@@ -14,6 +14,8 @@ typedef enum ...@@ -14,6 +14,8 @@ typedef enum
NICE_ADDRESS_TYPE_IPV6, NICE_ADDRESS_TYPE_IPV6,
} NiceAddressType; } NiceAddressType;
#define NICE_ADDRESS_STRING_LEN INET6_ADDRSTRLEN
typedef struct _NiceAddress NiceAddress; typedef struct _NiceAddress NiceAddress;
/* XXX: need access to fields to convert to sockaddr_in */ /* XXX: need access to fields to convert to sockaddr_in */
...@@ -53,8 +55,8 @@ nice_address_set_from_sockaddr_in (NiceAddress *addr, struct sockaddr_in *sin); ...@@ -53,8 +55,8 @@ nice_address_set_from_sockaddr_in (NiceAddress *addr, struct sockaddr_in *sin);
gboolean gboolean
nice_address_equal (NiceAddress *a, NiceAddress *b); nice_address_equal (NiceAddress *a, NiceAddress *b);
gchar * void
nice_address_to_string (NiceAddress *addr); nice_address_to_string (NiceAddress *addr, gchar *dst);
gboolean gboolean
nice_address_is_private (NiceAddress *a); nice_address_is_private (NiceAddress *a);
......
...@@ -8,14 +8,13 @@ test_ipv4 (void) ...@@ -8,14 +8,13 @@ test_ipv4 (void)
{ {
NiceAddress addr = {0,}; NiceAddress addr = {0,};
NiceAddress other = {0,}; NiceAddress other = {0,};
gchar *str; gchar str[NICE_ADDRESS_STRING_LEN];
nice_address_set_ipv4 (&addr, 0x01020304); nice_address_set_ipv4 (&addr, 0x01020304);
g_assert (addr.type == NICE_ADDRESS_TYPE_IPV4); g_assert (addr.type == NICE_ADDRESS_TYPE_IPV4);
str = nice_address_to_string (&addr); nice_address_to_string (&addr, str);
g_assert (0 == strcmp (str, "1.2.3.4")); g_assert (0 == strcmp (str, "1.2.3.4"));
g_free (str);
/* same address */ /* same address */
nice_address_set_ipv4 (&other, 0x01020304); nice_address_set_ipv4 (&other, 0x01020304);
...@@ -35,7 +34,7 @@ static void ...@@ -35,7 +34,7 @@ static void
test_ipv6 (void) test_ipv6 (void)
{ {
NiceAddress addr = {0,}; NiceAddress addr = {0,};
gchar *str; gchar str[NICE_ADDRESS_STRING_LEN];
nice_address_set_ipv6 (&addr, nice_address_set_ipv6 (&addr,
"\x00\x11\x22\x33" "\x00\x11\x22\x33"
...@@ -44,9 +43,8 @@ test_ipv6 (void) ...@@ -44,9 +43,8 @@ test_ipv6 (void)
"\xcc\xdd\xee\xff"); "\xcc\xdd\xee\xff");
g_assert (addr.type == NICE_ADDRESS_TYPE_IPV6); g_assert (addr.type == NICE_ADDRESS_TYPE_IPV6);
str = nice_address_to_string (&addr); nice_address_to_string (&addr, str);
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"));
g_free (str);
} }
int int
......
...@@ -670,12 +670,11 @@ RESPOND: ...@@ -670,12 +670,11 @@ RESPOND:
#ifdef DEBUG #ifdef DEBUG
{ {
gchar *ip; gchar ip[NICE_ADDRESS_STRING_LEN];
ip = nice_address_to_string (&remote->addr); nice_address_to_string (&remote->addr, ip);
g_debug ("s%d:%d: got valid connectivity check for candidate %d (%s:%d)", g_debug ("s%d:%d: got valid connectivity check for candidate %d (%s:%d)",
stream->id, component->id, remote->id, ip, remote->addr.port); stream->id, component->id, remote->id, ip, remote->addr.port);
g_free (ip);
} }
#endif #endif
...@@ -750,13 +749,12 @@ ERROR: ...@@ -750,13 +749,12 @@ ERROR:
#ifdef DEBUG #ifdef DEBUG
{ {
gchar *ip; gchar ip[NICE_ADDRESS_STRING_LEN];
ip = nice_address_to_string (&remote->addr); nice_address_to_string (&remote->addr, ip);
g_debug ( g_debug (
"s%d:%d: got invalid connectivity check for candidate %d (%s:%d)", "s%d:%d: got invalid connectivity check for candidate %d (%s:%d)",
stream->id, component->id, remote->id, ip, remote->addr.port); stream->id, component->id, remote->id, ip, remote->addr.port);
g_free (ip);
} }
#endif #endif
......
...@@ -12,11 +12,10 @@ main (void) ...@@ -12,11 +12,10 @@ main (void)
for (i = interfaces; i; i = i->next) for (i = interfaces; i; i = i->next)
{ {
NiceInterface *iface = i->data; NiceInterface *iface = i->data;
gchar *addr; gchar addr[NICE_ADDRESS_STRING_LEN];
addr = nice_address_to_string (&iface->addr); nice_address_to_string (&iface->addr, addr);
g_print ("%s: %s\n", iface->name, addr); g_print ("%s: %s\n", iface->name, addr);
g_free (addr);
nice_interface_free (iface); nice_interface_free (iface);
} }
......
...@@ -75,7 +75,7 @@ ERROR: ...@@ -75,7 +75,7 @@ ERROR:
gchar * gchar *
nice_candidate_to_string (NiceCandidate *candidate) nice_candidate_to_string (NiceCandidate *candidate)
{ {
gchar *addr_tmp; gchar addr_tmp[NICE_ADDRESS_STRING_LEN];
gchar *ret; gchar *ret;
gchar type; gchar type;
...@@ -97,10 +97,9 @@ nice_candidate_to_string (NiceCandidate *candidate) ...@@ -97,10 +97,9 @@ nice_candidate_to_string (NiceCandidate *candidate)
return NULL; return NULL;
} }
addr_tmp = nice_address_to_string (&(candidate->addr)); nice_address_to_string (&(candidate->addr), addr_tmp);
ret = g_strdup_printf ("%c/%s/%d/%s/%s", type, addr_tmp, ret = g_strdup_printf ("%c/%s/%d/%s/%s", type, addr_tmp,
candidate->addr.port, candidate->username, candidate->password); candidate->addr.port, candidate->username, candidate->password);
g_free (addr_tmp);
return ret; return ret;
} }
...@@ -25,11 +25,10 @@ main (void) ...@@ -25,11 +25,10 @@ main (void)
length = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf); length = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf);
#ifdef DEBUG #ifdef DEBUG
{ {
gchar *ip; gchar ip[NICE_ADDRESS_STRING_LEN];
ip = nice_address_to_string (&addr); nice_address_to_string (&addr, ip);
g_debug ("%s:%d", ip, addr.port); g_debug ("%s:%d", ip, addr.port);
g_free (ip);
} }
#endif #endif
nice_udp_socket_send (&sock, &addr, length, buf); nice_udp_socket_send (&sock, &addr, length, buf);
......
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