Commit ce578135 authored by Dafydd Harries's avatar Dafydd Harries

nice_agent_send ()

darcs-hash:20070205130557-c9803-2d265e14320d3818b35eec91d60132ae25a9a3e2.gz
parent 1d25c460
......@@ -30,6 +30,7 @@ check_PROGRAMS = \
test \
test-add-stream \
test-recv \
test-send \
test-stun \
test-priority
......@@ -41,6 +42,8 @@ test_add_stream_LDADD = $(COMMON_LDADD)
test_recv_LDADD = $(COMMON_LDADD)
test_send_LDADD = $(COMMON_LDADD)
test_stun_LDADD = $(COMMON_LDADD)
test_priority_LDADD = $(COMMON_LDADD)
......
......@@ -704,6 +704,36 @@ nice_agent_poll_read (NiceAgent *agent, GSList *other_fds)
}
void
nice_agent_send (
NiceAgent *agent,
guint stream_id,
guint component_id,
guint len,
gchar *buf)
{
Stream *stream;
Component *component;
stream = _stream_lookup (agent, stream_id);
component = stream->component;
if (component->active_candidate != NULL)
{
NiceUDPSocket *sock;
NiceAddress *addr;
struct sockaddr_in sockaddr;
sock = &component->active_candidate->sock;
addr = component->peer_addr;
sockaddr.sin_family = AF_INET;
sockaddr.sin_addr.s_addr = htonl (addr->addr_ipv4);
sockaddr.sin_port = htons (addr->port);
nice_udp_socket_send (sock, &sockaddr, len, buf);
}
}
/**
* Set the STUN server from which to obtain server-reflexive candidates.
*/
......
......@@ -74,6 +74,14 @@ nice_agent_poll_read (
NiceAgent *agent,
GSList *other_fds);
void
nice_agent_send (
NiceAgent *agent,
guint stream_id,
guint component_id,
guint len,
gchar *buf);
const GSList *
nice_agent_get_local_candidates (
NiceAgent *agent);
......
#include <string.h>
#include "agent.h"
#include "stun.h"
#include "udp-fake.h"
void
send_connectivity_check (
NiceAgent *agent,
NiceUDPSocketFactory *factory,
NiceAddress *remote_addr)
{
NiceUDPSocket *sock;
NiceCandidate *local;
NiceCandidate *remote;
gchar *username;
struct sockaddr_in remote_sockaddr = {0,};
g_assert (agent->local_candidates);
g_assert (agent->local_candidates->data);
local = agent->local_candidates->data;
g_assert (local->id == 1);
g_assert (agent->remote_candidates);
g_assert (agent->remote_candidates->data);
remote = agent->remote_candidates->data;
sock = &local->sock;
username = g_strconcat (local->username, remote->username, NULL);
remote_sockaddr.sin_family = AF_INET;
remote_sockaddr.sin_addr.s_addr = htonl (remote_addr->addr_ipv4);
remote_sockaddr.sin_port = htons (remote_addr->port);
{
StunMessage *msg;
gchar *packed;
guint len;
msg = stun_message_new (STUN_MESSAGE_BINDING_REQUEST, NULL, 1);
msg->attributes[0] = stun_attribute_username_new (username);
len = stun_message_pack (msg, &packed);
nice_udp_fake_socket_push_recv (sock, &remote_sockaddr, len, packed);
g_free (packed);
stun_message_free (msg);
}
nice_agent_recv (agent, 1);
{
StunMessage *msg;
struct sockaddr_in addr = {0,};
gchar packed[1024];
gchar *dump;
guint len;
len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, packed);
g_assert (0 == memcmp (&addr, &remote_sockaddr, sizeof (addr)));
msg = stun_message_unpack (len, packed);
dump = stun_message_dump (msg);
g_assert (0 == strcmp (dump,
"BINDING-RESPONSE 00000000:00000000:00000000:00000000\n"
" MAPPED-ADDRESS 192.168.0.2:2345\n"
" USERNAME \"S9PObXR5username\"\n"));
g_free (dump);
stun_message_free (msg);
}
g_free (username);
}
int
main (void)
{
NiceUDPSocketFactory factory;
NiceAgent *agent;
NiceAddress local_addr = {0,};
NiceAddress remote_addr = {0,};
/* set up */
nice_udp_fake_socket_factory_init (&factory);
agent = nice_agent_new (&factory);
if (!nice_address_set_ipv4_from_string (&local_addr, "192.168.0.1"))
g_assert_not_reached ();
nice_agent_add_local_address (agent, &local_addr);
nice_agent_add_stream (agent, NULL, NULL);
if (!nice_address_set_ipv4_from_string (&remote_addr, "192.168.0.2"))
g_assert_not_reached ();
remote_addr.port = 2345;
nice_agent_add_remote_candidate (agent, 1, 1, NICE_CANDIDATE_TYPE_HOST,
&remote_addr, "username", "password");
/* test */
{
NiceUDPSocket *sock;
NiceCandidate *candidate;
struct sockaddr_in addr;
gchar buf[1024];
guint len;
candidate = agent->local_candidates->data;
sock = &candidate->sock;
/* 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
* get silently dropped.
*/
nice_agent_send (agent, 1, 1, 5, "hello");
g_assert (0 == nice_udp_fake_socket_pop_send (sock, &addr, 1024, buf));
send_connectivity_check (agent, &factory, &remote_addr);
/* Now that we've received a valid connectivity check, we have a local
* socket to send from, and a remote address to send to.
*/
nice_agent_send (agent, 1, 1, 5, "hello");
len = nice_udp_fake_socket_pop_send (sock, &addr, 1024, buf);
g_assert (len == 5);
g_assert (0 == strncmp (buf, "hello", len));
}
/* clean up */
nice_agent_free (agent);
nice_udp_socket_factory_close (&factory);
return 0;
}
......@@ -15,6 +15,7 @@ T nice_agent_new
T nice_agent_poll_read
T nice_agent_pop_event
T nice_agent_recv
T nice_agent_send
T nice_candidate_free
T nice_candidate_ice_priority
T nice_candidate_jingle_priority
......
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