Commit 1eb6564c authored by Dafydd Harries's avatar Dafydd Harries

Address: separate allocation from assignment in order to allow static allocation

darcs-hash:20070122161909-c9803-57d3fc94d42bbbff6fb3f28c2aed0a7f3be3f730.gz
parent 5e9c7fb1
......@@ -15,32 +15,40 @@
Address *
address_new_ipv4 (guint32 addr_ipv4)
address_new (void)
{
Address *addr;
return g_slice_new0 (Address);
}
addr = g_slice_new0 (Address);
void
address_set_ipv4 (Address *addr, guint32 addr_ipv4)
{
addr->type = ADDRESS_TYPE_IPV4;
addr->addr_ipv4 = addr_ipv4;
return addr;
}
/**
* address_new_ipv4_from_string ()
* address_set_ipv4_from_string ()
*
* Returns NULL on error.
* Returns FALSE on error.
*/
Address *
address_new_ipv4_from_string (gchar *str)
gboolean
address_set_ipv4_from_string (Address *addr, gchar *str)
{
struct in_addr iaddr;
if (inet_aton (str, &iaddr) != 0)
return address_new_ipv4 (ntohl (iaddr.s_addr));
{
address_set_ipv4 (addr, ntohl (iaddr.s_addr));
return TRUE;
}
else
/* invalid address */
return NULL;
{
/* invalid address */
return FALSE;
}
}
......
......@@ -36,15 +36,17 @@ struct _address
};
Address *
address_new_ipv4 (guint32 addr_ipv4);
Address *
address_new_ipv4_from_string (gchar *str);
address_new (void);
void
address_free (Address *addr);
void
address_set_ipv4 (Address *addr, guint32 addr_ipv4);
gboolean
address_set_ipv4_from_string (Address *addr, gchar *str);
gboolean
address_equal (Address *a, Address *b);
gchar *
address_to_string (Address *addr);
void
address_free (Address *addr);
/*** candidate ***/
......
......@@ -26,7 +26,7 @@ int
main (void)
{
Agent *agent;
Address *addr;
Address addr;
Candidate *candidate;
UDPSocketManager mgr;
UDPSocket *sock;
......@@ -36,9 +36,8 @@ main (void)
/* set up agent */
agent = ice_agent_new (&mgr);
addr = address_new_ipv4_from_string ("192.168.0.1");
ice_agent_add_local_address (agent, addr);
address_free (addr);
address_set_ipv4_from_string (&addr, "192.168.0.1");
ice_agent_add_local_address (agent, &addr);
ice_agent_add_stream (agent, MEDIA_TYPE_AUDIO, handle_recv);
g_assert (agent->local_candidates != NULL);
......
......@@ -13,19 +13,18 @@
int
main (void)
{
Address *addr;
Address addr;
Candidate *candidate;
gchar *str;
candidate = candidate_from_string ("x");
g_assert (candidate == NULL);
addr = address_new_ipv4_from_string ("192.168.0.1");
g_assert (address_set_ipv4_from_string (&addr, "192.168.0.1"));
candidate = candidate_from_string ("H/192.168.0.1/1234");
g_assert (candidate != NULL);
g_assert (address_equal (addr, candidate->addr));
g_assert (address_equal (&addr, candidate->addr));
g_assert (candidate->port == 1234);
address_free (addr);
str = candidate_to_string (candidate);
g_assert (0 == strcmp (str, "H/192.168.0.1/1234"));
......
......@@ -18,14 +18,14 @@ gint
main (void)
{
Agent *agent;
Address *addr_local, *addr_remote;
Address addr_local, addr_remote;
Candidate *candidate;
UDPSocketManager mgr;
udp_fake_socket_manager_init (&mgr);
addr_local = address_new_ipv4_from_string ("192.168.0.1");
addr_remote = address_new_ipv4_from_string ("192.168.0.2");
address_set_ipv4_from_string (&addr_local, "192.168.0.1");
address_set_ipv4_from_string (&addr_remote, "192.168.0.2");
agent = ice_agent_new (&mgr);
......@@ -34,12 +34,12 @@ main (void)
g_assert (ice_agent_pop_event (agent) == NULL);
/* add one local address */
ice_agent_add_local_address (agent, addr_local);
ice_agent_add_local_address (agent, &addr_local);
g_assert (agent->local_addresses != NULL);
g_assert (g_slist_length (agent->local_addresses) == 1);
g_assert (address_equal ((Address *) agent->local_addresses->data,
addr_local));
&addr_local));
/* no candidates should be generated until we have a stream */
g_assert (agent->local_candidates == NULL);
......@@ -51,24 +51,22 @@ main (void)
g_assert (agent->local_candidates != NULL);
g_assert (g_slist_length (agent->local_candidates) == 1);
candidate = (Candidate *) agent->local_candidates->data;
g_assert (address_equal (candidate->addr, addr_local));
g_assert (address_equal (candidate->addr, &addr_local));
g_assert (candidate->id == 1);
/* fake socket manager uses incremental port numbers starting at 1 */
g_assert (candidate->port == 1);
/* add remote candidate */
ice_agent_add_remote_candidate (agent, CANDIDATE_TYPE_HOST, addr_remote,
ice_agent_add_remote_candidate (agent, CANDIDATE_TYPE_HOST, &addr_remote,
2345);
g_assert (agent->remote_candidates != NULL);
g_assert (g_slist_length (agent->remote_candidates) == 1);
candidate = (Candidate *) agent->remote_candidates->data;
g_assert (address_equal (candidate->addr, addr_remote));
g_assert (address_equal (candidate->addr, &addr_remote));
g_assert (candidate->port == 2345);
/* check there's no unexpected events, and clean up */
g_assert (ice_agent_pop_event (agent) == NULL);
address_free (addr_local);
address_free (addr_remote);
ice_agent_free (agent);
return 0;
}
......
......@@ -73,7 +73,8 @@ candidate_from_string (const gchar *s)
port = strtol (last_slash + 1, NULL, 10);
candidate = candidate_new (type);
addr = address_new_ipv4 (ntohl (ip));
addr = address_new ();
address_set_ipv4 (addr, ntohl (ip));
candidate->addr = addr;
candidate->port = port;
......
......@@ -22,14 +22,13 @@ make_agent (
UDPSocket **ret_sock)
{
Agent *agent;
Address *addr_local;
Address addr_local;
Candidate *candidate;
agent = ice_agent_new (mgr);
addr_local = address_new_ipv4_from_string (ip);
ice_agent_add_local_address (agent, addr_local);
address_free (addr_local);
address_set_ipv4_from_string (&addr_local, ip);
ice_agent_add_local_address (agent, &addr_local);
ice_agent_add_stream (agent, MEDIA_TYPE_AUDIO, NULL);
......
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