Commit 89292b9f authored by Dafydd Harries's avatar Dafydd Harries

store the candidates in the components rather than in the agent

darcs-hash:20070217155355-c9803-ec94ff5a0483e801e8a708385bb5fad1c245b59b.gz
parent e3cac908
...@@ -39,6 +39,8 @@ struct _Component ...@@ -39,6 +39,8 @@ struct _Component
NiceAddress peer_addr; NiceAddress peer_addr;
guint id; guint id;
NiceComponentState state; NiceComponentState state;
GSList *local_candidates;
GSList *remote_candidates;
}; };
...@@ -58,6 +60,24 @@ component_new ( ...@@ -58,6 +60,24 @@ component_new (
static void static void
component_free (Component *cmp) component_free (Component *cmp)
{ {
GSList *i;
for (i = cmp->local_candidates; i; i = i->next)
{
NiceCandidate *candidate = i->data;
nice_candidate_free (candidate);
}
for (i = cmp->remote_candidates; i; i = i->next)
{
NiceCandidate *candidate = i->data;
nice_candidate_free (candidate);
}
g_slist_free (cmp->local_candidates);
g_slist_free (cmp->remote_candidates);
g_slice_free (Component, cmp); g_slice_free (Component, cmp);
} }
...@@ -336,6 +356,10 @@ nice_agent_add_local_host_candidate ( ...@@ -336,6 +356,10 @@ nice_agent_add_local_host_candidate (
{ {
NiceRNG *rng; NiceRNG *rng;
NiceCandidate *candidate; NiceCandidate *candidate;
Component *component;
if (!find_component (agent, stream_id, component_id, NULL, &component))
return;
candidate = nice_candidate_new (NICE_CANDIDATE_TYPE_HOST); candidate = nice_candidate_new (NICE_CANDIDATE_TYPE_HOST);
candidate->id = agent->next_candidate_id++; candidate->id = agent->next_candidate_id++;
...@@ -343,7 +367,7 @@ nice_agent_add_local_host_candidate ( ...@@ -343,7 +367,7 @@ nice_agent_add_local_host_candidate (
candidate->component_id = component_id; candidate->component_id = component_id;
candidate->addr = *address; candidate->addr = *address;
candidate->base_addr = *address; candidate->base_addr = *address;
agent->local_candidates = g_slist_append (agent->local_candidates, component->local_candidates = g_slist_append (component->local_candidates,
candidate); candidate);
/* generate username/password */ /* generate username/password */
...@@ -422,37 +446,6 @@ nice_agent_remove_stream ( ...@@ -422,37 +446,6 @@ nice_agent_remove_stream (
if (!stream) if (!stream)
return; return;
/* remove candidates */
{
GSList *i;
GSList *old_list = agent->local_candidates;
GSList *free_list = NULL;
GSList *new_list = NULL;
for (i = agent->local_candidates; i; i = i->next)
{
NiceCandidate *candidate = i->data;
if (candidate->stream_id == stream_id)
free_list = g_slist_append (free_list, candidate);
else
new_list = g_slist_append (new_list, candidate);
}
agent->local_candidates = new_list;
for (i = free_list; i; i = i->next)
{
NiceCandidate *candidate = i->data;
nice_candidate_free (candidate);
}
g_slist_free (free_list);
g_slist_free (old_list);
}
/* remove stream */ /* remove stream */
stream_free (stream); stream_free (stream);
...@@ -506,9 +499,11 @@ nice_agent_add_remote_candidate ( ...@@ -506,9 +499,11 @@ nice_agent_add_remote_candidate (
const gchar *username, const gchar *username,
const gchar *password) const gchar *password)
{ {
/* append to agent->remote_candidates */
NiceCandidate *candidate; NiceCandidate *candidate;
Component *component;
if (!find_component (agent, stream_id, component_id, NULL, &component))
return;
candidate = nice_candidate_new (type); candidate = nice_candidate_new (type);
candidate->stream_id = stream_id; candidate->stream_id = stream_id;
...@@ -519,7 +514,7 @@ nice_agent_add_remote_candidate ( ...@@ -519,7 +514,7 @@ nice_agent_add_remote_candidate (
strncpy (candidate->username, username, sizeof (candidate->username)); strncpy (candidate->username, username, sizeof (candidate->username));
strncpy (candidate->password, password, sizeof (candidate->password)); strncpy (candidate->password, password, sizeof (candidate->password));
agent->remote_candidates = g_slist_append (agent->remote_candidates, component->remote_candidates = g_slist_append (component->remote_candidates,
candidate); candidate);
/* later: for each component, generate a new check with the new candidate */ /* later: for each component, generate a new check with the new candidate */
...@@ -546,11 +541,11 @@ _local_candidate_lookup (NiceAgent *agent, guint candidate_id) ...@@ -546,11 +541,11 @@ _local_candidate_lookup (NiceAgent *agent, guint candidate_id)
static NiceCandidate * static NiceCandidate *
find_candidate_by_fd (NiceAgent *agent, guint fd) find_candidate_by_fd (Component *component, guint fd)
{ {
GSList *i; GSList *i;
for (i = agent->local_candidates; i; i = i->next) for (i = component->local_candidates; i; i = i->next)
{ {
NiceCandidate *c = i->data; NiceCandidate *c = i->data;
...@@ -622,7 +617,7 @@ _handle_stun_binding_request ( ...@@ -622,7 +617,7 @@ _handle_stun_binding_request (
* transport address didn't match. * transport address didn't match.
*/ */
for (i = agent->remote_candidates; i; i = i->next) for (i = component->remote_candidates; i; i = i->next)
{ {
guint len; guint len;
...@@ -904,21 +899,20 @@ nice_agent_recv ( ...@@ -904,21 +899,20 @@ nice_agent_recv (
guint max_fd = 0; guint max_fd = 0;
gint num_readable; gint num_readable;
GSList *i; GSList *i;
Stream *stream;
Component *component;
if (!find_component (agent, stream_id, component_id, &stream, &component))
return 0;
FD_ZERO (&fds); FD_ZERO (&fds);
for (i = agent->local_candidates; i; i = i->next) for (i = component->local_candidates; i; i = i->next)
{ {
NiceCandidate *candidate; NiceCandidate *candidate = i->data;
candidate = i->data;
if (candidate->stream_id == stream_id && FD_SET (candidate->sock.fileno, &fds);
candidate->component_id == component_id) max_fd = MAX (candidate->sock.fileno, max_fd);
{
FD_SET (candidate->sock.fileno, &fds);
max_fd = MAX (candidate->sock.fileno, max_fd);
}
} }
/* Loop on candidate sockets until we find one that has non-STUN data /* Loop on candidate sockets until we find one that has non-STUN data
...@@ -938,18 +932,9 @@ nice_agent_recv ( ...@@ -938,18 +932,9 @@ nice_agent_recv (
if (FD_ISSET (j, &fds)) if (FD_ISSET (j, &fds))
{ {
NiceCandidate *candidate; NiceCandidate *candidate;
Stream *stream;
Component *component;
candidate = find_candidate_by_fd (agent, j);
if (candidate == NULL)
continue;
if (!find_component (agent, candidate->stream_id,
candidate->component_id, &stream, &component))
continue;
candidate = find_candidate_by_fd (component, j);
g_assert (candidate);
len = _nice_agent_recv (agent, stream, component, candidate, len = _nice_agent_recv (agent, stream, component, candidate,
buf_len, buf); buf_len, buf);
...@@ -976,13 +961,14 @@ nice_agent_recv_sock ( ...@@ -976,13 +961,14 @@ nice_agent_recv_sock (
Stream *stream; Stream *stream;
Component *component; Component *component;
candidate = find_candidate_by_fd (agent, sock);
g_assert (candidate);
if (!find_component (agent, stream_id, component_id, &stream, &component)) if (!find_component (agent, stream_id, component_id, &stream, &component))
return 0; return 0;
return _nice_agent_recv (agent, stream, component, candidate, buf_len, buf); candidate = find_candidate_by_fd (component, sock);
g_assert (candidate);
return _nice_agent_recv (agent, stream, stream->component,
candidate, buf_len, buf);
} }
...@@ -1014,13 +1000,19 @@ nice_agent_poll_read ( ...@@ -1014,13 +1000,19 @@ nice_agent_poll_read (
FD_ZERO (&fds); FD_ZERO (&fds);
for (i = agent->local_candidates; i; i = i->next) for (i = agent->streams; i; i = i->next)
{ {
NiceCandidate *candidate; GSList *j;
Stream *stream = i->data;
Component *component = stream->component;
candidate = i->data; for (j = component->local_candidates; j; j = j->next)
FD_SET (candidate->sock.fileno, &fds); {
max_fd = MAX (candidate->sock.fileno, max_fd); NiceCandidate *candidate = j->data;
FD_SET (candidate->sock.fileno, &fds);
max_fd = MAX (candidate->sock.fileno, max_fd);
}
} }
for (i = other_fds; i; i = i->next) for (i = other_fds; i; i = i->next)
...@@ -1041,33 +1033,41 @@ nice_agent_poll_read ( ...@@ -1041,33 +1033,41 @@ nice_agent_poll_read (
for (j = 0; j <= max_fd; j++) for (j = 0; j <= max_fd; j++)
if (FD_ISSET (j, &fds)) if (FD_ISSET (j, &fds))
{ {
GSList *i;
if (g_slist_find (other_fds, GUINT_TO_POINTER (j))) if (g_slist_find (other_fds, GUINT_TO_POINTER (j)))
ret = g_slist_append (ret, GUINT_TO_POINTER (j)); ret = g_slist_append (ret, GUINT_TO_POINTER (j));
else else
for (i = agent->local_candidates; i; i = i->next) {
{ NiceCandidate *candidate = NULL;
NiceCandidate *candidate = i->data; Stream *stream;
gchar buf[1024];
if (candidate->sock.fileno == j) guint len;
{
Stream *stream; for (i = agent->streams; i; i = i->next)
Component *component; {
gchar buf[1024]; Stream *s = i->data;
guint len; Component *c = s->component;
if (!find_component (agent, candidate->stream_id, candidate = find_candidate_by_fd (c, j);
candidate->component_id, &stream, &component))
break; if (candidate != NULL)
break;
len = _nice_agent_recv (agent, stream, component, candidate, }
1024, buf);
if (candidate == NULL)
if (len && func != NULL) break;
func (agent, stream->id, component->id, len, buf, data);
} stream = find_stream (agent, candidate->stream_id);
}
if (stream == NULL)
break;
len = _nice_agent_recv (agent, stream, stream->component,
candidate, 1024, buf);
if (len && func != NULL)
func (agent, stream->id, candidate->component_id, len, buf,
data);
}
} }
return ret; return ret;
...@@ -1085,8 +1085,8 @@ nice_agent_send ( ...@@ -1085,8 +1085,8 @@ nice_agent_send (
Stream *stream; Stream *stream;
Component *component; Component *component;
if (!find_component (agent, stream_id, component_id, &stream, &component)) stream = find_stream (agent, stream_id);
return; component = stream->component;
if (component->active_candidate != NULL) if (component->active_candidate != NULL)
{ {
...@@ -1132,20 +1132,36 @@ nice_agent_get_local_candidates ( ...@@ -1132,20 +1132,36 @@ nice_agent_get_local_candidates (
NiceAgent *agent, NiceAgent *agent,
guint stream_id, guint stream_id,
guint component_id) guint component_id)
{ { Component *component;
GSList *candidates = NULL;
GSList *i;
for (i = agent->local_candidates; i; i = i->next) if (!find_component (agent, stream_id, component_id, NULL, &component))
{ return NULL;
NiceCandidate *candidate = i->data;
if (candidate->stream_id == stream_id && return g_slist_copy (component->local_candidates);
candidate->component_id == component_id) }
candidates = g_slist_append (candidates, candidate);
}
/**
* nice_agent_get_remote_candidates:
* @agent: A NiceAgent
*
* The caller owns the returned GSList but not the candidates contained within
* it.
*
* Returns: a GSList of remote candidates belonging to @agent
**/
GSList *
nice_agent_get_remote_candidates (
NiceAgent *agent,
guint stream_id,
guint component_id)
{
Component *component;
return candidates; if (!find_component (agent, stream_id, component_id, NULL, &component))
return NULL;
return g_slist_copy (component->remote_candidates);
} }
...@@ -1165,26 +1181,6 @@ nice_agent_dispose (GObject *object) ...@@ -1165,26 +1181,6 @@ nice_agent_dispose (GObject *object)
g_slist_free (agent->local_addresses); g_slist_free (agent->local_addresses);
agent->local_addresses = NULL; agent->local_addresses = NULL;
for (i = agent->local_candidates; i; i = i->next)
{
NiceCandidate *c = i->data;
nice_candidate_free (c);
}
g_slist_free (agent->local_candidates);
agent->local_candidates = NULL;
for (i = agent->remote_candidates; i; i = i->next)
{
NiceCandidate *c = i->data;
nice_candidate_free (c);
}
g_slist_free (agent->remote_candidates);
agent->remote_candidates = NULL;
for (i = agent->streams; i; i = i->next) for (i = agent->streams; i; i = i->next)
{ {
Stream *s = i->data; Stream *s = i->data;
...@@ -1271,27 +1267,26 @@ nice_agent_main_context_attach ( ...@@ -1271,27 +1267,26 @@ nice_agent_main_context_attach (
NiceAgentRecvFunc func, NiceAgentRecvFunc func,
gpointer data) gpointer data)
{ {
GSList *i;
if (agent->main_context_set) if (agent->main_context_set)
return FALSE; return FALSE;
/* attach candidates */ /* attach candidates */
for (i = agent->streams; i; i = i->next)
{ {
GSList *i; GSList *j;
Stream *stream = i->data;
Component *component = stream->component;
for (i = agent->local_candidates; i; i = i->next) for (j = component->local_candidates; j; j = j->next)
{ {
NiceCandidate *candidate = i->data; NiceCandidate *candidate = j->data;
GIOChannel *io; GIOChannel *io;
GSource *source; GSource *source;
Stream *stream;
Component *component;
IOCtx *ctx; IOCtx *ctx;
if (!find_component (agent, candidate->stream_id,
candidate->component_id, &stream, &component))
continue;
io = g_io_channel_unix_new (candidate->sock.fileno); io = g_io_channel_unix_new (candidate->sock.fileno);
source = g_io_create_watch (io, G_IO_IN); source = g_io_create_watch (io, G_IO_IN);
ctx = io_ctx_new (agent, stream, component, candidate); ctx = io_ctx_new (agent, stream, component, candidate);
......
...@@ -52,8 +52,6 @@ struct _NiceAgent ...@@ -52,8 +52,6 @@ struct _NiceAgent
guint next_stream_id; guint next_stream_id;
NiceUDPSocketFactory *socket_factory; NiceUDPSocketFactory *socket_factory;
GSList *local_addresses; GSList *local_addresses;
GSList *local_candidates;
GSList *remote_candidates;
GSList *streams; GSList *streams;
gboolean main_context_set; gboolean main_context_set;
GMainContext *main_context; GMainContext *main_context;
...@@ -134,6 +132,12 @@ nice_agent_get_local_candidates ( ...@@ -134,6 +132,12 @@ nice_agent_get_local_candidates (
guint stream_id, guint stream_id,
guint component_id); guint component_id);
GSList *
nice_agent_get_remote_candidates (
NiceAgent *agent,
guint stream_id,
guint component_id);
gboolean gboolean
nice_agent_main_context_attach ( nice_agent_main_context_attach (
NiceAgent *agent, NiceAgent *agent,
......
...@@ -24,15 +24,12 @@ main (void) ...@@ -24,15 +24,12 @@ main (void)
g_assert (nice_agent_add_stream (agent, 1) == 3); g_assert (nice_agent_add_stream (agent, 1) == 3);
g_assert (NULL != agent->streams); g_assert (NULL != agent->streams);
g_assert (NULL != agent->local_candidates);
nice_agent_remove_stream (agent, 1); nice_agent_remove_stream (agent, 1);
nice_agent_remove_stream (agent, 2); nice_agent_remove_stream (agent, 2);
nice_agent_remove_stream (agent, 3); nice_agent_remove_stream (agent, 3);
g_assert (NULL == agent->streams); g_assert (NULL == agent->streams);
/* check no local candidates were left behind when streams were removed*/
g_assert (NULL == agent->local_candidates);
g_object_unref (agent); g_object_unref (agent);
nice_udp_socket_factory_close (&factory); nice_udp_socket_factory_close (&factory);
......
...@@ -43,9 +43,12 @@ main (void) ...@@ -43,9 +43,12 @@ main (void)
{ {
NiceUDPSocket *sock; NiceUDPSocket *sock;
NiceCandidate *candidate; NiceCandidate *candidate;
GSList *candidates;
candidate = agent->local_candidates->data; candidates = nice_agent_get_local_candidates (agent, 1, 1);
candidate = candidates->data;
sock = &candidate->sock; sock = &candidate->sock;
g_slist_free (candidates);
nice_udp_fake_socket_push_recv (sock, &addr, 6, "\x80hello"); nice_udp_fake_socket_push_recv (sock, &addr, 6, "\x80hello");
} }
......
...@@ -32,7 +32,6 @@ main (void) ...@@ -32,7 +32,6 @@ main (void)
NiceAgent *agent; NiceAgent *agent;
NiceAddress addr = {0,}; NiceAddress addr = {0,};
NiceUDPSocketFactory factory; NiceUDPSocketFactory factory;
NiceCandidate *candidate;
NiceUDPSocket *sock; NiceUDPSocket *sock;
gint pipe_fds[2]; gint pipe_fds[2];
GSList *fds = NULL; GSList *fds = NULL;
...@@ -50,8 +49,15 @@ main (void) ...@@ -50,8 +49,15 @@ main (void)
nice_agent_add_local_address (agent, &addr); nice_agent_add_local_address (agent, &addr);
nice_agent_add_stream (agent, 1); nice_agent_add_stream (agent, 1);
candidate = agent->local_candidates->data; {
sock = &candidate->sock; GSList *candidates;
NiceCandidate *candidate;
candidates = nice_agent_get_local_candidates (agent, 1, 1);
candidate = candidates->data;
sock = &candidate->sock;
g_slist_free (candidates);
}
/* set up pipe and fd list */ /* set up pipe and fd list */
......
...@@ -20,7 +20,6 @@ main (void) ...@@ -20,7 +20,6 @@ main (void)
g_assert (nice_address_set_ipv4_from_string (&addr, "192.168.0.1")); g_assert (nice_address_set_ipv4_from_string (&addr, "192.168.0.1"));
nice_agent_add_local_address (agent, &addr); nice_agent_add_local_address (agent, &addr);
nice_agent_add_stream (agent, 1); nice_agent_add_stream (agent, 1);
g_assert (agent->local_candidates != NULL);
/* recieve an RTP packet */ /* recieve an RTP packet */
...@@ -29,8 +28,11 @@ main (void) ...@@ -29,8 +28,11 @@ main (void)
NiceUDPSocket *sock; NiceUDPSocket *sock;
guint len; guint len;
gchar buf[1024]; gchar buf[1024];
GSList *candidates;
candidate = agent->local_candidates->data; candidates = nice_agent_get_local_candidates (agent, 1, 1);
candidate = candidates->data;
g_slist_free (candidates);
sock = &(candidate->sock); sock = &(candidate->sock);
nice_udp_fake_socket_push_recv (sock, &addr, 7, "\x80lalala"); nice_udp_fake_socket_push_recv (sock, &addr, 7, "\x80lalala");
len = nice_agent_recv (agent, candidate->stream_id, len = nice_agent_recv (agent, candidate->stream_id,
......
...@@ -59,14 +59,24 @@ send_connectivity_check ( ...@@ -59,14 +59,24 @@ send_connectivity_check (
NiceCandidate *remote; NiceCandidate *remote;
gchar *username; gchar *username;
g_assert (agent->local_candidates); {
g_assert (agent->local_candidates->data); GSList *candidates;
local = agent->local_candidates->data;
g_assert (local->id == 1); candidates = nice_agent_get_local_candidates (agent, 1, 1);
g_assert (g_slist_length (candidates) > 0);
local = candidates->data;
g_assert (local->id == 1);
g_slist_free (candidates);
}
g_assert (agent->remote_candidates); {
g_assert (agent->remote_candidates->data); GSList *candidates;
remote = agent->remote_candidates->data;
candidates = nice_agent_get_remote_candidates (agent, 1, 1);
g_assert (g_slist_length (candidates) > 0);
remote = candidates->data;
g_slist_free (candidates);
}
sock = &local->sock; sock = &local->sock;
...@@ -165,13 +175,19 @@ main (void) ...@@ -165,13 +175,19 @@ main (void)
{ {
NiceUDPSocket *sock; NiceUDPSocket *sock;
NiceCandidate *candidate;
NiceAddress addr; NiceAddress addr;
gchar buf[1024]; gchar buf[1024];
guint len; guint len;
candidate = agent->local_candidates->data; {
sock = &candidate->sock; GSList *candidates;
NiceCandidate *candidate;
candidates = nice_agent_get_local_candidates (agent, 1, 1);
candidate = candidates->data;
sock = &candidate->sock;
g_slist_free (candidates);
}
/* If we send data before we've received a connectivity check, we won't /* If we send data before we've received a connectivity check, we won't
* have an affinity for any of the remote candidates, so the packet will * have an affinity for any of the remote candidates, so the packet will
......
...@@ -8,10 +8,9 @@ ...@@ -8,10 +8,9 @@
static void static void
test_stun_no_password ( test_stun_no_password (
NiceAgent *agent, NiceAgent *agent,
NiceAddress from) NiceAddress from,
NiceUDPSocket *sock)
{ {
NiceCandidate *candidate;
NiceUDPSocket *sock;
NiceAddress to = {0,}; NiceAddress to = {0,};
guint len; guint len;
gchar buf[1024]; gchar buf[1024];
...@@ -20,9 +19,6 @@ test_stun_no_password ( ...@@ -20,9 +19,6 @@ test_stun_no_password (
memset (buf, '\0', 1024); memset (buf, '\0', 1024);
candidate = agent->local_candidates->data;
sock = &candidate->sock;
{ {
StunMessage *breq; StunMessage *breq;
...@@ -62,10 +58,9 @@ test_stun_no_password ( ...@@ -62,10 +58,9 @@ test_stun_no_password (
static void static void
test_stun_invalid_password ( test_stun_invalid_password (
NiceAgent *agent, NiceAgent *agent,
NiceAddress from) NiceAddress from,
NiceUDPSocket *sock)
{ {
NiceCandidate *candidate;
NiceUDPSocket *sock;
NiceAddress to = {0,}; NiceAddress to = {0,};
guint len; guint len;
gchar buf[1024]; gchar buf[1024];
...@@ -74,9 +69,6 @@ test_stun_invalid_password ( ...@@ -74,9 +69,6 @@ test_stun_invalid_password (
memset (buf, '\0', 1024); memset (buf, '\0', 1024);
candidate = agent->local_candidates->data;
sock = &candidate->sock;
{ {
StunMessage *breq; StunMessage *breq;
...@@ -117,10 +109,10 @@ test_stun_invalid_password ( ...@@ -117,10 +109,10 @@ test_stun_invalid_password (
static void static void
test_stun_valid_password ( test_stun_valid_password (
NiceAgent *agent, NiceAgent *agent,
NiceAddress from) NiceAddress from,
NiceCandidate *candidate,
NiceUDPSocket *sock)
{ {
NiceCandidate *candidate;
NiceUDPSocket *sock;
NiceAddress to = {0,}; NiceAddress to = {0,};
guint len; guint len;
guint packed_len; guint packed_len;
...@@ -130,13 +122,7 @@ test_stun_valid_password ( ...@@ -130,13 +122,7 @@ test_stun_valid_password (
memset (buf, '\0', 1024); memset (buf, '\0', 1024);
candidate = agent->local_candidates->data; username = g_strconcat (candidate->username, "username", NULL);
sock = &candidate->sock;
username = g_strconcat (
((NiceCandidate *) agent->local_candidates->data)->username,
"username",
NULL);
{ {
StunMessage *breq; StunMessage *breq;
...@@ -190,6 +176,7 @@ main (void) ...@@ -190,6 +176,7 @@ main (void)
NiceCandidate *candidate; NiceCandidate *candidate;
NiceUDPSocketFactory factory; NiceUDPSocketFactory factory;
NiceUDPSocket *sock; NiceUDPSocket *sock;
GSList *candidates;
g_type_init (); g_type_init ();
...@@ -205,14 +192,16 @@ main (void) ...@@ -205,14 +192,16 @@ main (void)
nice_agent_add_stream (agent, 1); nice_agent_add_stream (agent, 1);
nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST, nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST,
&remote_addr, "username", "password"); &remote_addr, "username", "password");
g_assert (agent->local_candidates != NULL);
candidate = agent->local_candidates->data; candidates = nice_agent_get_local_candidates (agent, 1, 1);
candidate = candidates->data;
sock = &(candidate->sock); sock = &(candidate->sock);
g_slist_free (candidates);
/* run tests */ /* run tests */
test_stun_no_password (agent, remote_addr); test_stun_no_password (agent, remote_addr, sock);
test_stun_invalid_password (agent, remote_addr); test_stun_invalid_password (agent, remote_addr, sock);
test_stun_valid_password (agent, remote_addr); test_stun_valid_password (agent, remote_addr, candidate, sock);
/* clean up */ /* clean up */
g_object_unref (agent); g_object_unref (agent);
......
...@@ -11,6 +11,7 @@ main (void) ...@@ -11,6 +11,7 @@ main (void)
NiceAddress addr_local = {0,}, addr_remote = {0,}; NiceAddress addr_local = {0,}, addr_remote = {0,};
NiceCandidate *candidate; NiceCandidate *candidate;
NiceUDPSocketFactory factory; NiceUDPSocketFactory factory;
GSList *candidates;
g_type_init (); g_type_init ();
...@@ -23,7 +24,6 @@ main (void) ...@@ -23,7 +24,6 @@ main (void)
agent = nice_agent_new (&factory); agent = nice_agent_new (&factory);
g_assert (agent->local_addresses == NULL); g_assert (agent->local_addresses == NULL);
g_assert (agent->local_candidates == NULL);
/* add one local address */ /* add one local address */
nice_agent_add_local_address (agent, &addr_local); nice_agent_add_local_address (agent, &addr_local);
...@@ -32,33 +32,33 @@ main (void) ...@@ -32,33 +32,33 @@ main (void)
g_assert (g_slist_length (agent->local_addresses) == 1); g_assert (g_slist_length (agent->local_addresses) == 1);
g_assert (nice_address_equal (agent->local_addresses->data, &addr_local)); g_assert (nice_address_equal (agent->local_addresses->data, &addr_local));
/* no candidates should be generated until we have a stream */
g_assert (agent->local_candidates == NULL);
/* add a stream */ /* add a stream */
nice_agent_add_stream (agent, 1); nice_agent_add_stream (agent, 1);
/* adding a stream should cause host candidates to be generated */ /* adding a stream should cause host candidates to be generated */
g_assert (agent->local_candidates != NULL); candidates = nice_agent_get_local_candidates (agent, 1, 1);
g_assert (g_slist_length (agent->local_candidates) == 1); g_assert (g_slist_length (candidates) == 1);
candidate = agent->local_candidates->data; candidate = candidates->data;
/* fake socket manager uses incremental port numbers starting at 1 */ /* fake socket manager uses incremental port numbers starting at 1 */
addr_local.port = 1; addr_local.port = 1;
g_assert (nice_address_equal (&(candidate->addr), &addr_local)); g_assert (nice_address_equal (&(candidate->addr), &addr_local));
g_assert (candidate->id == 1); g_assert (candidate->id == 1);
g_slist_free (candidates);
/* add remote candidate */ /* add remote candidate */
nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST, nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST,
&addr_remote, "username", "password"); &addr_remote, "username", "password");
g_assert (agent->remote_candidates != NULL); candidates = nice_agent_get_remote_candidates (agent, 1, 1);
g_assert (g_slist_length (agent->remote_candidates) == 1); g_assert (candidates != NULL);
candidate = agent->remote_candidates->data; g_assert (g_slist_length (candidates) == 1);
candidate = candidates->data;
g_assert (nice_address_equal (&(candidate->addr), &addr_remote)); g_assert (nice_address_equal (&(candidate->addr), &addr_remote));
g_assert (candidate->stream_id == 1); g_assert (candidate->stream_id == 1);
g_assert (candidate->component_id == 1); g_assert (candidate->component_id == 1);
g_assert (candidate->type == NICE_CANDIDATE_TYPE_HOST); g_assert (candidate->type == NICE_CANDIDATE_TYPE_HOST);
g_assert (0 == strcmp (candidate->username, "username")); g_assert (0 == strcmp (candidate->username, "username"));
g_assert (0 == strcmp (candidate->password, "password")); g_assert (0 == strcmp (candidate->password, "password"));
g_slist_free (candidates);
/* clean up */ /* clean up */
g_object_unref (agent); g_object_unref (agent);
......
...@@ -118,11 +118,15 @@ main (gint argc, gchar *argv[]) ...@@ -118,11 +118,15 @@ main (gint argc, gchar *argv[])
{ {
NiceCandidate *candidate; NiceCandidate *candidate;
GSList *candidates;
candidate = agent->local_candidates->data; candidates = nice_agent_get_local_candidates (agent, 1, 1);
g_assert (candidates);
candidate = candidates->data;
len = g_snprintf (buf, 1024, "0 0 X1 127.0.0.1 %d %s %s", len = g_snprintf (buf, 1024, "0 0 X1 127.0.0.1 %d %s %s",
candidate->addr.port, candidate->username, candidate->password); candidate->addr.port, candidate->username, candidate->password);
nice_udp_socket_send (&sock, &send_addr, len, buf); nice_udp_socket_send (&sock, &send_addr, len, buf);
g_slist_free (candidates);
} }
// set up signalling callback // set up signalling callback
......
...@@ -22,14 +22,24 @@ send_connectivity_check ( ...@@ -22,14 +22,24 @@ send_connectivity_check (
NiceCandidate *remote; NiceCandidate *remote;
gchar *username; gchar *username;
g_assert (agent->local_candidates); {
g_assert (agent->local_candidates->data); GSList *candidates;
local = agent->local_candidates->data;
g_assert (local->id == 1); candidates = nice_agent_get_local_candidates (agent, 1, 1);
g_assert (candidates);
local = candidates->data;
g_assert (local->id == 1);
g_slist_free (candidates);
}
g_assert (agent->remote_candidates); {
g_assert (agent->remote_candidates->data); GSList *candidates;
remote = agent->remote_candidates->data;
candidates = nice_agent_get_remote_candidates (agent, 1, 1);
g_assert (candidates);
remote = candidates->data;
g_slist_free (candidates);
}
sock = &local->sock; sock = &local->sock;
...@@ -99,12 +109,22 @@ recv_cb ( ...@@ -99,12 +109,22 @@ recv_cb (
/* return value is whether to keep the source */ /* return value is whether to keep the source */
NiceAgent *agent = data; NiceAgent *agent = data;
NiceCandidate *candidate = agent->local_candidates->data; NiceCandidate *candidate;
NiceUDPSocket *sock = &candidate->sock; NiceUDPSocket *sock;
NiceAddress from; NiceAddress from;
guint len; guint len;
gchar buf[1024]; gchar buf[1024];
{
GSList *candidates;
candidates = nice_agent_get_local_candidates (agent, 1, 1);
g_assert (candidates);
candidate = candidates->data;
g_slist_free (candidates);
}
sock = &candidate->sock;
len = nice_udp_fake_socket_pop_send ( len = nice_udp_fake_socket_pop_send (
sock, &from, 1024, buf); sock, &from, 1024, buf);
...@@ -137,9 +157,9 @@ main (gint argc, gchar *argv[]) ...@@ -137,9 +157,9 @@ main (gint argc, gchar *argv[])
nice_agent_add_local_address (agent, &addr); nice_agent_add_local_address (agent, &addr);
nice_address_set_ipv4 (&addr, 0xc0a80002); nice_address_set_ipv4 (&addr, 0xc0a80002);
addr.port = 2345; addr.port = 2345;
nice_agent_add_stream (agent, 1);
nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST, nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST,
&addr, "username", "password"); &addr, "username", "password");
nice_agent_add_stream (agent, 1);
// send connectivity check so that sending works // send connectivity check so that sending works
...@@ -150,9 +170,13 @@ main (gint argc, gchar *argv[]) ...@@ -150,9 +170,13 @@ main (gint argc, gchar *argv[])
GSource *source; GSource *source;
NiceCandidate *candidate; NiceCandidate *candidate;
NiceUDPSocket *sock; NiceUDPSocket *sock;
GSList *candidates;
candidate = agent->local_candidates->data; candidates = nice_agent_get_local_candidates (agent, 1, 1);
g_assert (candidates);
candidate = candidates->data;
sock = &candidate->sock; sock = &candidate->sock;
g_slist_free (candidates);
// send test packet // send test packet
......
...@@ -26,6 +26,7 @@ make_agent ( ...@@ -26,6 +26,7 @@ make_agent (
NiceAgent *agent; NiceAgent *agent;
NiceAddress addr_local; NiceAddress addr_local;
NiceCandidate *candidate; NiceCandidate *candidate;
GSList *candidates;
agent = nice_agent_new (factory); agent = nice_agent_new (factory);
...@@ -35,10 +36,12 @@ make_agent ( ...@@ -35,10 +36,12 @@ make_agent (
nice_agent_add_local_address (agent, &addr_local); nice_agent_add_local_address (agent, &addr_local);
nice_agent_add_stream (agent, 1); nice_agent_add_stream (agent, 1);
g_assert (agent->local_candidates != NULL); candidates = nice_agent_get_local_candidates (agent, 1, 1);
candidate = agent->local_candidates->data; g_assert (candidates != NULL);
candidate = candidates->data;
g_debug ("allocated socket %d port %d for candidate %d", g_debug ("allocated socket %d port %d for candidate %d",
candidate->sock.fileno, candidate->sock.addr.port, candidate->id); candidate->sock.fileno, candidate->sock.addr.port, candidate->id);
g_slist_free (candidates);
*ret_agent = agent; *ret_agent = agent;
*ret_sock = &(candidate->sock); *ret_sock = &(candidate->sock);
...@@ -91,12 +94,17 @@ handle_connection (guint fileno, const struct sockaddr_in *sin, gpointer data) ...@@ -91,12 +94,17 @@ handle_connection (guint fileno, const struct sockaddr_in *sin, gpointer data)
if (!make_agent ((gchar *) data, &factory, &agent, &sock)) if (!make_agent ((gchar *) data, &factory, &agent, &sock))
return; return;
/* send first local candidate to remote end */ {
candidate_str = nice_candidate_to_string ( GSList *candidates;
agent->local_candidates->data);
send (fileno, candidate_str, strlen (candidate_str), 0); /* send first local candidate to remote end */
send (fileno, "\n", 1, 0); candidates = nice_agent_get_local_candidates (agent, 1, 1);
g_free (candidate_str); candidate_str = nice_candidate_to_string (candidates->data);
send (fileno, candidate_str, strlen (candidate_str), 0);
send (fileno, "\n", 1, 0);
g_free (candidate_str);
g_slist_free (candidates);
}
/* event loop */ /* event loop */
......
...@@ -88,11 +88,14 @@ accept_connection ( ...@@ -88,11 +88,14 @@ accept_connection (
{ {
NiceCandidate *candidate; NiceCandidate *candidate;
GSList *candidates;
candidate = agent->local_candidates->data; candidates = nice_agent_get_local_candidates (agent, 1, 1);
candidate = candidates->data;
len = g_snprintf (buf, 1024, "0 0 X1 127.0.0.1 %d %s %s", len = g_snprintf (buf, 1024, "0 0 X1 127.0.0.1 %d %s %s",
candidate->addr.port, candidate->username, candidate->password); candidate->addr.port, candidate->username, candidate->password);
nice_udp_socket_send (sock, &send_addr, len, buf); nice_udp_socket_send (sock, &send_addr, len, buf);
g_slist_free (candidates);
} }
// IO loop // IO loop
......
...@@ -12,6 +12,7 @@ T nice_agent_add_local_address ...@@ -12,6 +12,7 @@ T nice_agent_add_local_address
T nice_agent_add_remote_candidate T nice_agent_add_remote_candidate
T nice_agent_add_stream T nice_agent_add_stream
T nice_agent_get_local_candidates T nice_agent_get_local_candidates
T nice_agent_get_remote_candidates
T nice_agent_get_type T nice_agent_get_type
T nice_agent_main_context_attach T nice_agent_main_context_attach
T nice_agent_new T nice_agent_new
......
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