Commit fb58b1ca authored by Dafydd Harries's avatar Dafydd Harries

agent: respond to inline STUN binding requests

darcs-hash:20070122182751-c9803-0d067208b074bf65b1e1891877312d35a36d3488.gz
parent c0fbb728
AM_CFLAGS = -Wall -Werror \
-I $(top_srcdir)/udp \
-I $(top_srcdir)/stun \
$(GLIB_CFLAGS)
noinst_LTLIBRARIES = libagent.la
......@@ -11,7 +12,13 @@ libagent_la_LIBADD = \
$(top_builddir)/udp/libudp.la \
$(top_builddir)/stun/libstun.la
check_PROGRAMS = test test-readline test-util test-add-stream test-recv
check_PROGRAMS = \
test \
test-readline \
test-util \
test-add-stream \
test-recv \
test-stun
test_LDADD = libagent.la $(GLIB_LIBS)
......@@ -27,5 +34,7 @@ test_add_stream_LDADD = libagent.la $(GLIB_LIBS)
test_recv_LDADD = libagent.la $(GLIB_LIBS)
TESTS = test test-readline test-util test-add-stream test-recv
test_stun_LDADD = libagent.la $(GLIB_LIBS)
TESTS = $(check_PROGRAMS)
......@@ -7,6 +7,7 @@
#include <glib.h>
#include <stun.h>
#include <udp.h>
#include <agent.h>
......@@ -558,7 +559,31 @@ ice_agent_recv (
}
else
{
/* maybe STUN */
StunMessage *msg;
msg = stun_message_unpack (len, buf);
if (msg == NULL)
return;
if (msg->type == STUN_MESSAGE_BINDING_REQUEST)
{
StunMessage *response;
guint len;
gchar *packed;
response = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE);
response->attributes = g_malloc0 (2 * sizeof (StunAttribute));
response->attributes[0] = stun_attribute_mapped_address_new (
ntohl (from.sin_addr.s_addr), ntohs (from.sin_port));
len = stun_message_pack (response, &packed);
udp_socket_send (&(candidate->sock), &from, len, packed);
g_free (packed);
stun_message_free (response);
}
stun_message_free (msg);
}
}
......
#include <string.h>
#include <arpa/inet.h>
#include <glib.h>
#include <stun.h>
#include <udp.h>
#include <udp-fake.h>
#include <agent.h>
void
handle_recv (Agent *agent, guint stream_id, guint len, gchar *buf)
{
g_assert_not_reached ();
}
int
main (void)
{
Agent *agent;
Address local_addr, remote_addr;
Candidate *candidate;
UDPSocketManager mgr;
UDPSocket *sock;
StunMessage *breq, *bres;
struct sockaddr_in from, to;
guint packed_len;
gchar *packed;
guint len;
gchar buf[1024];
memset (buf, '\0', 1024);
udp_fake_socket_manager_init (&mgr);
address_set_ipv4_from_string (&local_addr, "192.168.0.1");
address_set_ipv4_from_string (&remote_addr, "192.168.0.5");
from.sin_family = AF_INET;
from.sin_addr.s_addr = htonl (remote_addr.addr_ipv4);
from.sin_port = htons (5678);
/* set up agent */
agent = ice_agent_new (&mgr);
ice_agent_add_local_address (agent, &local_addr);
ice_agent_add_stream (agent, MEDIA_TYPE_AUDIO, handle_recv);
g_assert (agent->local_candidates != NULL);
candidate = (Candidate *) agent->local_candidates->data;
sock = &(candidate->sock);
/* send binding request */
breq = stun_message_new (STUN_MESSAGE_BINDING_REQUEST);
packed_len = stun_message_pack (breq, &packed);
udp_fake_socket_push_recv (sock, &from, packed_len, packed);
g_free (packed);
stun_message_free (breq);
/* tell the agent there's a packet waiting */
ice_agent_recv (agent, candidate->id);
/* construct expected response packet */
bres = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE);
bres->attributes = g_malloc0 (2 * sizeof (StunAttribute *));
bres->attributes[0] = stun_attribute_mapped_address_new (
remote_addr.addr_ipv4, 5678);
packed_len = stun_message_pack (bres, &packed);
g_assert (packed_len == 32);
/* compare sent packet to expected */
len = udp_fake_socket_pop_send (sock, &to, sizeof (buf) / sizeof (gchar),
buf);
g_assert (len == packed_len);
g_assert (0 == memcmp (buf, packed, len));
g_assert (to.sin_family == from.sin_family);
g_assert (to.sin_addr.s_addr == from.sin_addr.s_addr);
g_assert (to.sin_port == from.sin_port);
g_free (packed);
stun_message_free (bres);
/* clean up */
ice_agent_free (agent);
udp_socket_manager_close (&mgr);
return 0;
}
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