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

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)
}
gchar *
nice_address_to_string (NiceAddress *addr)
void
nice_address_to_string (NiceAddress *addr, gchar *dst)
{
struct in_addr iaddr = {0,};
gchar ip_str[INET6_ADDRSTRLEN] = {0,};
const gchar *ret = NULL;
switch (addr->type)
{
case NICE_ADDRESS_TYPE_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;
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;
}
g_assert (ret == ip_str);
return g_strdup (ip_str);
g_assert (ret == dst);
}
......
......@@ -14,6 +14,8 @@ typedef enum
NICE_ADDRESS_TYPE_IPV6,
} NiceAddressType;
#define NICE_ADDRESS_STRING_LEN INET6_ADDRSTRLEN
typedef struct _NiceAddress NiceAddress;
/* 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);
gboolean
nice_address_equal (NiceAddress *a, NiceAddress *b);
gchar *
nice_address_to_string (NiceAddress *addr);
void
nice_address_to_string (NiceAddress *addr, gchar *dst);
gboolean
nice_address_is_private (NiceAddress *a);
......
......@@ -8,14 +8,13 @@ test_ipv4 (void)
{
NiceAddress addr = {0,};
NiceAddress other = {0,};
gchar *str;
gchar str[NICE_ADDRESS_STRING_LEN];
nice_address_set_ipv4 (&addr, 0x01020304);
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_free (str);
/* same address */
nice_address_set_ipv4 (&other, 0x01020304);
......@@ -35,7 +34,7 @@ static void
test_ipv6 (void)
{
NiceAddress addr = {0,};
gchar *str;
gchar str[NICE_ADDRESS_STRING_LEN];
nice_address_set_ipv6 (&addr,
"\x00\x11\x22\x33"
......@@ -44,9 +43,8 @@ test_ipv6 (void)
"\xcc\xdd\xee\xff");
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_free (str);
}
int
......
......@@ -670,12 +670,11 @@ RESPOND:
#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)",
stream->id, component->id, remote->id, ip, remote->addr.port);
g_free (ip);
}
#endif
......@@ -750,13 +749,12 @@ ERROR:
#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 invalid connectivity check for candidate %d (%s:%d)",
stream->id, component->id, remote->id, ip, remote->addr.port);
g_free (ip);
}
#endif
......
......@@ -12,11 +12,10 @@ main (void)
for (i = interfaces; i; i = i->next)
{
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_free (addr);
nice_interface_free (iface);
}
......
......@@ -75,7 +75,7 @@ ERROR:
gchar *
nice_candidate_to_string (NiceCandidate *candidate)
{
gchar *addr_tmp;
gchar addr_tmp[NICE_ADDRESS_STRING_LEN];
gchar *ret;
gchar type;
......@@ -97,10 +97,9 @@ nice_candidate_to_string (NiceCandidate *candidate)
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,
candidate->addr.port, candidate->username, candidate->password);
g_free (addr_tmp);
return ret;
}
......@@ -25,11 +25,10 @@ main (void)
length = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf);
#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_free (ip);
}
#endif
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