Commit c9845896 authored by Dafydd Harries's avatar Dafydd Harries

agent: use UDP library for network operations

darcs-hash:20070122120338-c9803-3d6e60931c24ce9e96d66017f1887cfc005aaf7c.gz
parent 7b040ed5
AM_CFLAGS = -Wall -Werror $(GLIB_CFLAGS) AM_CFLAGS = -Wall -Werror \
-I $(top_srcdir)/udp \
$(GLIB_CFLAGS)
noinst_LTLIBRARIES = libagent.la noinst_LTLIBRARIES = libagent.la
libagent_la_SOURCES = agent.h agent.c libagent_la_SOURCES = agent.h agent.c
libagent_la_LIBADD = $(top_builddir)/udp/libudp.la
check_PROGRAMS = test test-readline test-util check_PROGRAMS = test test-readline test-util
test_LDADD = libagent.la $(GLIB_LIBS) test_LDADD = libagent.la $(GLIB_LIBS)
......
...@@ -7,6 +7,8 @@ ...@@ -7,6 +7,8 @@
#include <glib.h> #include <glib.h>
#include <udp.h>
#include <agent.h> #include <agent.h>
/*** address ***/ /*** address ***/
...@@ -277,6 +279,7 @@ candidate_pair_priority ( ...@@ -277,6 +279,7 @@ candidate_pair_priority (
/*** event ***/ /*** event ***/
#if 0
static Event * static Event *
event_new (EventType type) event_new (EventType type)
{ {
...@@ -286,43 +289,14 @@ event_new (EventType type) ...@@ -286,43 +289,14 @@ event_new (EventType type)
ev->type = type; ev->type = type;
return ev; return ev;
} }
#endif
static Event *
event_new_request_port (Address *addr, guint candidate_id)
{
Event *ev;
ev = event_new (EVENT_REQUEST_PORT);
ev->request_port.addr = address_dup (addr);
ev->request_port.candidate_id = candidate_id;
return ev;
}
static Event *
event_new_local_candidates_ready ()
{
return event_new (EVENT_LOCAL_CANDIDATES_READY);
}
void void
event_free (Event *ev) event_free (Event *ev)
{ {
switch (ev->type) switch (ev->type)
{ {
case EVENT_REQUEST_PORT:
address_free (ev->request_port.addr);
break;
case EVENT_LOCAL_CANDIDATES_READY:
break;
case EVENT_REQUEST_STUN_QUERY:
address_free (ev->request_stun_query.from);
address_free (ev->request_stun_query.to);
break;
case EVENT_CANDIDATE_SELECTED: case EVENT_CANDIDATE_SELECTED:
break; break;
} }
...@@ -335,11 +309,12 @@ event_free (Event *ev) ...@@ -335,11 +309,12 @@ event_free (Event *ev)
Agent * Agent *
ice_agent_new () ice_agent_new (UDPSocketManager *mgr)
{ {
Agent *agent; Agent *agent;
agent = g_slice_new0 (Agent); agent = g_slice_new0 (Agent);
agent->sockmgr = mgr;
agent->next_candidate_id = 1; agent->next_candidate_id = 1;
return agent; return agent;
} }
...@@ -376,6 +351,7 @@ ice_agent_add_local_host_candidate ( ...@@ -376,6 +351,7 @@ ice_agent_add_local_host_candidate (
Address *address) Address *address)
{ {
Candidate *candidate; Candidate *candidate;
struct sockaddr_in sin;
candidate = candidate_new (CANDIDATE_TYPE_HOST); candidate = candidate_new (CANDIDATE_TYPE_HOST);
candidate->id = agent->next_candidate_id++; candidate->id = agent->next_candidate_id++;
...@@ -384,31 +360,12 @@ ice_agent_add_local_host_candidate ( ...@@ -384,31 +360,12 @@ ice_agent_add_local_host_candidate (
agent->local_candidates = g_slist_append (agent->local_candidates, agent->local_candidates = g_slist_append (agent->local_candidates,
candidate); candidate);
/* request port for new candidate */ sin.sin_family = AF_INET;
ice_agent_push_event (agent, sin.sin_addr.s_addr = htonl (address->addr_ipv4);
event_new_request_port (address, candidate->id)); sin.sin_port = 0;
} /* XXX: handle error */
udp_socket_manager_alloc_socket (agent->sockmgr, &(candidate->sock), &sin);
candidate->port = ntohs (candidate->sock.addr.sin_port);
/* XXX: check that ID given matches one of the candidates we have? */
void
ice_agent_set_candidate_port (Agent *agent, guint candidate_id, guint port)
{
GSList *i;
gboolean local_candidates_ready = TRUE;
for (i = agent->local_candidates; i; i = i->next)
{
Candidate *c = (Candidate *) i->data;
if (c->id == candidate_id)
c->port = port;
else if (c->port == 0)
local_candidates_ready = FALSE;
}
if (local_candidates_ready)
ice_agent_push_event (agent, event_new_local_candidates_ready ());
} }
......
...@@ -75,6 +75,7 @@ struct _candidate ...@@ -75,6 +75,7 @@ struct _candidate
// guint component_id; // guint component_id;
// guint generation; // guint generation;
// gchar *foundation; // gchar *foundation;
UDPSocket sock;
}; };
...@@ -91,9 +92,6 @@ typedef enum event_type EventType; ...@@ -91,9 +92,6 @@ typedef enum event_type EventType;
enum event_type enum event_type
{ {
EVENT_REQUEST_PORT,
EVENT_LOCAL_CANDIDATES_READY,
EVENT_REQUEST_STUN_QUERY,
EVENT_CANDIDATE_SELECTED, EVENT_CANDIDATE_SELECTED,
}; };
...@@ -131,6 +129,7 @@ typedef struct _agent Agent; ...@@ -131,6 +129,7 @@ typedef struct _agent Agent;
struct _agent struct _agent
{ {
guint next_candidate_id; guint next_candidate_id;
UDPSocketManager *sockmgr;
GSList *local_addresses; GSList *local_addresses;
GSList *local_candidates; GSList *local_candidates;
GSList *remote_candidates; GSList *remote_candidates;
...@@ -140,7 +139,7 @@ struct _agent ...@@ -140,7 +139,7 @@ struct _agent
Agent * Agent *
ice_agent_new (); ice_agent_new (UDPSocketManager *mgr);
Event * Event *
ice_agent_pop_event (Agent *agent); ice_agent_pop_event (Agent *agent);
void void
...@@ -148,8 +147,6 @@ ice_agent_add_local_address (Agent *agent, Address *addr); ...@@ -148,8 +147,6 @@ ice_agent_add_local_address (Agent *agent, Address *addr);
void void
ice_agent_add_stream (Agent *agent, MediaType type); ice_agent_add_stream (Agent *agent, MediaType type);
void void
ice_agent_set_candidate_port (Agent *agent, guint candidate_id, guint port);
void
ice_agent_free (Agent *agent); ice_agent_free (Agent *agent);
void void
ice_agent_add_remote_candidate ( ice_agent_add_remote_candidate (
......
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include <glib.h> #include <glib.h>
#include <udp.h>
#include <agent.h> #include <agent.h>
#include <util.h> #include <util.h>
......
#include <arpa/inet.h>
#include <glib.h> #include <glib.h>
#include <udp.h>
#include <udp-fake.h>
#include <agent.h> #include <agent.h>
gint gint
...@@ -9,12 +14,14 @@ main (void) ...@@ -9,12 +14,14 @@ main (void)
Agent *agent; Agent *agent;
Address *addr_local, *addr_remote; Address *addr_local, *addr_remote;
Candidate *candidate; Candidate *candidate;
Event *event; UDPSocketManager mgr;
udp_fake_socket_manager_init (&mgr);
addr_local = address_new_ipv4_from_string ("192.168.0.1"); addr_local = address_new_ipv4_from_string ("192.168.0.1");
addr_remote = address_new_ipv4_from_string ("192.168.0.2"); addr_remote = address_new_ipv4_from_string ("192.168.0.2");
agent = ice_agent_new (); agent = ice_agent_new (&mgr);
g_assert (agent->local_addresses == NULL); g_assert (agent->local_addresses == NULL);
g_assert (agent->local_candidates == NULL); g_assert (agent->local_candidates == NULL);
...@@ -40,27 +47,8 @@ main (void) ...@@ -40,27 +47,8 @@ main (void)
candidate = (Candidate *) agent->local_candidates->data; 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); g_assert (candidate->id == 1);
g_assert (candidate->port == 0); /* fake socket manager uses incremental port numbers starting at 1 */
g_assert (candidate->port == 1);
/* there should be a port request for the new candidate */
event = ice_agent_pop_event (agent);
g_assert (ice_agent_pop_event (agent) == NULL);
g_assert (event != NULL);
g_assert (event->type == EVENT_REQUEST_PORT);
g_assert (address_equal (event->request_port.addr, addr_local));
g_assert (event->request_port.candidate_id == 1);
event_free (event);
/* assign a port */
ice_agent_set_candidate_port (agent, 1, 1234);
g_assert (candidate->port == 1234);
/* expect event: local candidates ready */
event = ice_agent_pop_event (agent);
g_assert (ice_agent_pop_event (agent) == NULL);
g_assert (event != NULL);
g_assert (event->type == EVENT_LOCAL_CANDIDATES_READY);
event_free (event);
/* add remote candidate */ /* 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,
......
...@@ -6,6 +6,8 @@ ...@@ -6,6 +6,8 @@
#include <glib.h> #include <glib.h>
#include <udp.h>
#include <agent.h> #include <agent.h>
/* format is: /* format is:
......
...@@ -7,7 +7,6 @@ EXTRA_libnice_la_SOURCES = libnice.ver ...@@ -7,7 +7,6 @@ EXTRA_libnice_la_SOURCES = libnice.ver
libnice_la_LIBADD = \ libnice_la_LIBADD = \
../local/liblocal.la \ ../local/liblocal.la \
../udp/libudp.la \
../stun/libstun.la \ ../stun/libstun.la \
../agent/libagent.la ../agent/libagent.la
...@@ -29,7 +28,6 @@ ice_test_server_SOURCES = ice-test-server.c \ ...@@ -29,7 +28,6 @@ ice_test_server_SOURCES = ice-test-server.c \
ice_test_server_LDADD = \ ice_test_server_LDADD = \
$(top_builddir)/agent/libagent.la \ $(top_builddir)/agent/libagent.la \
$(top_builddir)/udp/libudp.la \
$(top_builddir)/stun/libstun.la \ $(top_builddir)/stun/libstun.la \
$(GLIB_LIBS) $(GLIB_LIBS)
...@@ -7,8 +7,8 @@ ...@@ -7,8 +7,8 @@
#include <glib.h> #include <glib.h>
#include <agent.h>
#include <udp.h> #include <udp.h>
#include <agent.h>
#include <stun.h> #include <stun.h>
#include <readline.h> #include <readline.h>
#include <util.h> #include <util.h>
...@@ -23,11 +23,9 @@ make_agent ( ...@@ -23,11 +23,9 @@ make_agent (
{ {
Agent *agent; Agent *agent;
Address *addr_local; Address *addr_local;
Event *event; Candidate *candidate;
UDPSocket *sock;
struct sockaddr_in sin;
agent = ice_agent_new (); agent = ice_agent_new (mgr);
addr_local = address_new_ipv4_from_string (ip); addr_local = address_new_ipv4_from_string (ip);
ice_agent_add_local_address (agent, addr_local); ice_agent_add_local_address (agent, addr_local);
...@@ -35,37 +33,14 @@ make_agent ( ...@@ -35,37 +33,14 @@ make_agent (
ice_agent_add_stream (agent, MEDIA_TYPE_AUDIO); ice_agent_add_stream (agent, MEDIA_TYPE_AUDIO);
event = ice_agent_pop_event (agent); g_assert (agent->local_candidates != NULL);
g_assert (event != NULL); candidate = (Candidate *) agent->local_candidates->data;
g_assert (event->type == EVENT_REQUEST_PORT);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = 0;
sock = g_slice_new0 (UDPSocket);
if (!udp_socket_manager_alloc_socket (mgr, sock, &sin))
{
ice_agent_free (agent);
return FALSE;
}
g_debug ("allocated socket %d port %d for candidate %d", g_debug ("allocated socket %d port %d for candidate %d",
sock->fileno, ntohs (sock->addr.sin_port), candidate->sock.fileno, ntohs (candidate->sock.addr.sin_port),
event->request_port.candidate_id); candidate->id);
ice_agent_set_candidate_port (agent, event->request_port.candidate_id,
ntohs (sock->addr.sin_port));
event_free (event);
event = ice_agent_pop_event (agent);
g_assert (event != NULL);
g_assert (event->type == EVENT_LOCAL_CANDIDATES_READY);
event = ice_agent_pop_event (agent);
g_assert (event == NULL);
*ret_agent = agent; *ret_agent = agent;
*ret_sock = sock; *ret_sock = &(candidate->sock);
return TRUE; return TRUE;
} }
......
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