Commit ebc37d94 authored by Dafydd Harries's avatar Dafydd Harries

make UDP code use NiceAddress instead of sockaddr_in

darcs-hash:20070205161016-c9803-f3eefe210e39ebae3dd075409dd83211aeee7148.gz
parent ce578135
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/select.h>
#include <glib.h>
......@@ -202,7 +200,6 @@ nice_agent_add_local_host_candidate (
{
NiceRNG *rng;
NiceCandidate *candidate;
struct sockaddr_in sin;
candidate = nice_candidate_new (NICE_CANDIDATE_TYPE_HOST);
candidate->id = agent->next_candidate_id++;
......@@ -219,17 +216,13 @@ nice_agent_add_local_host_candidate (
nice_rng_generate_bytes_print (rng, 8, candidate->password);
nice_rng_free (rng);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (address->addr_ipv4);
sin.sin_port = 0;
/* XXX: handle error */
if (!nice_udp_socket_factory_make (agent->socket_factory,
&(candidate->sock), &sin))
&(candidate->sock), address))
g_assert_not_reached ();
candidate->addr.port = ntohs (candidate->sock.addr.sin_port);
candidate->base_addr.port = ntohs (candidate->sock.addr.sin_port);
candidate->addr = candidate->sock.addr;
candidate->base_addr = candidate->sock.addr;
}
......@@ -377,7 +370,7 @@ _handle_stun (
NiceAgent *agent,
Stream *stream,
NiceCandidate *local,
struct sockaddr_in from,
NiceAddress from,
StunMessage *msg)
{
GSList *i;
......@@ -490,7 +483,7 @@ RESPOND:
response = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE,
msg->transaction_id, 2);
response->attributes[0] = stun_attribute_mapped_address_new (
ntohl (from.sin_addr.s_addr), ntohs (from.sin_port));
from.addr_ipv4, from.port);
response->attributes[1] = stun_attribute_username_new (username);
len = stun_message_pack (response, &packed);
nice_udp_socket_send (&local->sock, &from, len, packed);
......@@ -537,7 +530,7 @@ _nice_agent_recv (
{
guint len;
gchar buf[1024];
struct sockaddr_in from;
NiceAddress from;
len = nice_udp_socket_recv (&(candidate->sock), &from,
sizeof (buf) / sizeof (gchar), buf);
......@@ -722,14 +715,10 @@ nice_agent_send (
{
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);
nice_udp_socket_send (sock, addr, len, buf);
}
}
......
......@@ -2,8 +2,6 @@
#ifndef _AGENT_H
#define _AGENT_H
#include <arpa/inet.h>
#include <glib.h>
#include "udp.h"
......
......@@ -23,7 +23,7 @@ nice_candidate_free (NiceCandidate *candidate)
{
/* better way of checking if socket is allocated? */
if (candidate->sock.addr.sin_addr.s_addr != 0)
if (candidate->sock.addr.addr_ipv4 != 0)
nice_udp_socket_close (&(candidate->sock));
g_slice_free (NiceCandidate, candidate);
......
......@@ -31,7 +31,6 @@ main (void)
NiceCandidate *candidate;
NiceUDPSocketFactory factory;
NiceUDPSocket *sock;
struct sockaddr_in from = {0,};
nice_udp_fake_socket_factory_init (&factory);
......@@ -45,7 +44,7 @@ main (void)
/* recieve an RTP packet */
candidate = agent->local_candidates->data;
sock = &(candidate->sock);
nice_udp_fake_socket_push_recv (sock, &from, 7, "\x80lalala");
nice_udp_fake_socket_push_recv (sock, &addr, 7, "\x80lalala");
nice_agent_recv (agent, candidate->id);
g_assert (cb_called == TRUE);
......
......@@ -15,7 +15,6 @@ send_connectivity_check (
NiceCandidate *local;
NiceCandidate *remote;
gchar *username;
struct sockaddr_in remote_sockaddr = {0,};
g_assert (agent->local_candidates);
g_assert (agent->local_candidates->data);
......@@ -30,10 +29,6 @@ send_connectivity_check (
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;
......@@ -42,7 +37,7 @@ send_connectivity_check (
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);
nice_udp_fake_socket_push_recv (sock, remote_addr, len, packed);
g_free (packed);
stun_message_free (msg);
}
......@@ -51,13 +46,13 @@ send_connectivity_check (
{
StunMessage *msg;
struct sockaddr_in addr = {0,};
NiceAddress 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)));
g_assert (nice_address_equal (&addr, remote_addr));
msg = stun_message_unpack (len, packed);
dump = stun_message_dump (msg);
g_assert (0 == strcmp (dump,
......@@ -102,7 +97,7 @@ main (void)
{
NiceUDPSocket *sock;
NiceCandidate *candidate;
struct sockaddr_in addr;
NiceAddress addr;
gchar buf[1024];
guint len;
......
......@@ -20,12 +20,12 @@ handle_recv (
static void
test_stun_no_password (
NiceAgent *agent,
struct sockaddr_in from)
NiceAddress from)
{
NiceCandidate *candidate;
NiceUDPSocket *sock;
NiceAddress to = {0,};
guint len;
struct sockaddr_in to = {0,};
gchar buf[1024];
guint packed_len;
gchar *packed;
......@@ -74,12 +74,12 @@ test_stun_no_password (
static void
test_stun_invalid_password (
NiceAgent *agent,
struct sockaddr_in from)
NiceAddress from)
{
NiceCandidate *candidate;
NiceUDPSocket *sock;
NiceAddress to = {0,};
guint len;
struct sockaddr_in to = {0,};
gchar buf[1024];
guint packed_len;
gchar *packed;
......@@ -129,13 +129,13 @@ test_stun_invalid_password (
static void
test_stun_valid_password (
NiceAgent *agent,
struct sockaddr_in from)
NiceAddress from)
{
NiceCandidate *candidate;
NiceUDPSocket *sock;
NiceAddress to = {0,};
guint len;
guint packed_len;
struct sockaddr_in to = {0,};
gchar buf[1024];
gchar *packed;
gchar *username;
......@@ -173,7 +173,7 @@ test_stun_valid_password (
bres = stun_message_new (STUN_MESSAGE_BINDING_RESPONSE,
"0123456789abcdef", 2);
bres->attributes[0] = stun_attribute_mapped_address_new (
ntohl (from.sin_addr.s_addr), 5678);
from.addr_ipv4, 5678);
bres->attributes[1] = stun_attribute_username_new (username);
packed_len = stun_message_pack (bres, &packed);
stun_message_free (bres);
......@@ -189,9 +189,7 @@ test_stun_valid_password (
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_assert (nice_address_equal (&to, &from));
g_free (packed);
}
......@@ -203,7 +201,6 @@ main (void)
NiceCandidate *candidate;
NiceUDPSocketFactory factory;
NiceUDPSocket *sock;
struct sockaddr_in from = {0,};
nice_udp_fake_socket_factory_init (&factory);
......@@ -211,10 +208,6 @@ main (void)
g_assert (nice_address_set_ipv4_from_string (&remote_addr, "192.168.0.5"));
remote_addr.port = 5678;
from.sin_family = AF_INET;
from.sin_addr.s_addr = htonl (remote_addr.addr_ipv4);
from.sin_port = htons (remote_addr.port);
/* set up agent */
agent = nice_agent_new (&factory);
nice_agent_add_local_address (agent, &local_addr);
......@@ -226,9 +219,9 @@ main (void)
sock = &(candidate->sock);
/* run tests */
test_stun_no_password (agent, from);
test_stun_invalid_password (agent, from);
test_stun_valid_password (agent, from);
test_stun_no_password (agent, remote_addr);
test_stun_invalid_password (agent, remote_addr);
test_stun_valid_password (agent, remote_addr);
/* clean up */
nice_agent_free (agent);
......
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <glib/gprintf.h>
......@@ -10,7 +12,7 @@
#include "stun.h"
static void
send_stun (NiceUDPSocket *udpsock, struct sockaddr_in sin, gchar *username)
send_stun (NiceUDPSocket *udpsock, NiceAddress addr, gchar *username)
{
gchar *packed;
guint packed_len;
......@@ -28,11 +30,11 @@ send_stun (NiceUDPSocket *udpsock, struct sockaddr_in sin, gchar *username)
}
packed_len = stun_message_pack (msg, &packed);
nice_udp_socket_send (udpsock, &sin, packed_len, packed);
nice_udp_socket_send (udpsock, &addr, packed_len, packed);
g_free (packed);
stun_message_free (msg);
packed_len = nice_udp_socket_recv (udpsock, &sin, 1024, buf);
packed_len = nice_udp_socket_recv (udpsock, &addr, 1024, buf);
g_assert (packed_len > 0);
msg = stun_message_unpack (packed_len, buf);
g_assert (msg);
......@@ -51,7 +53,6 @@ static void
handle_connection (guint sock)
{
gchar *line;
struct sockaddr_in sin;
NiceUDPSocketFactory man;
NiceUDPSocket udpsock;
NiceCandidate *candidate;
......@@ -75,17 +76,13 @@ handle_connection (guint sock)
nice_udp_bsd_socket_factory_init (&man);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = 0;
if (!nice_udp_socket_factory_make (&man, &udpsock, &sin))
if (!nice_udp_socket_factory_make (&man, &udpsock, NULL))
goto OUT;
// send local candidate
line = g_strdup_printf ("H/127.0.0.1/%d/lala/titi\n",
ntohs (udpsock.addr.sin_port));
ntohs (udpsock.addr.port));
if (write (sock, line, strlen (line)) != strlen (line))
g_assert_not_reached ();
......@@ -94,18 +91,15 @@ handle_connection (guint sock)
// agent doesn't initiate connectivity checks, so make our own for now
sin.sin_addr.s_addr = htonl (candidate->addr.addr_ipv4);
sin.sin_port = htons (candidate->addr.port);
{
gchar *username;
username = g_strdup_printf ("lala%s", candidate->username);
send_stun (&udpsock, sin, username);
send_stun (&udpsock, candidate->addr, username);
g_free (username);
}
nice_udp_socket_send (&udpsock, &sin, 6, "\x80hello");
nice_udp_socket_send (&udpsock, &candidate->addr, 6, "\x80hello");
nice_udp_socket_close (&udpsock);
OUT:
......
#include <string.h>
#include <arpa/inet.h>
#include <errno.h>
#include <unistd.h>
#include "nice.h"
#include "readline.h"
#include "util.h"
......@@ -44,8 +44,7 @@ make_agent (
g_assert (agent->local_candidates != NULL);
candidate = agent->local_candidates->data;
g_debug ("allocated socket %d port %d for candidate %d",
candidate->sock.fileno, ntohs (candidate->sock.addr.sin_port),
candidate->id);
candidate->sock.fileno, candidate->sock.addr.port, candidate->id);
*ret_agent = agent;
*ret_sock = &(candidate->sock);
......
......@@ -43,7 +43,8 @@ accept_connection (
NiceUDPSocket *sock)
{
NiceAgent *agent;
struct sockaddr_in sin_recv, sin_send;
NiceAddress recv_addr;
NiceAddress send_addr;
guint len;
gchar buf[1024];
guint ret = 0;
......@@ -53,7 +54,7 @@ accept_connection (
// accept incoming handshake
len = nice_udp_socket_recv (sock, &sin_recv, 1, buf);
len = nice_udp_socket_recv (sock, &recv_addr, 1, buf);
if (len != 1)
{
......@@ -71,16 +72,16 @@ accept_connection (
// send handshake reply
sin_send = sin_recv;
sin_send.sin_port = htons (1235);
nice_udp_socket_send (sock, &sin_send, 1, buf);
send_addr = recv_addr;
send_addr.port = 1235;
nice_udp_socket_send (sock, &send_addr, 1, buf);
// send codec
strcpy (buf, "1 0 PCMU 0 8000 0");
nice_udp_socket_send (sock, &sin_send, strlen (buf), buf);
nice_udp_socket_send (sock, &send_addr, strlen (buf), buf);
strcpy (buf, "1 0 LAST 0 0 0");
nice_udp_socket_send (sock, &sin_send, strlen (buf), buf);
nice_udp_socket_send (sock, &send_addr, strlen (buf), buf);
// send candidate
......@@ -90,7 +91,7 @@ accept_connection (
candidate = nice_agent_get_local_candidates (agent)->data;
len = g_snprintf (buf, 1024, "0 0 X1 127.0.0.1 %d %s %s",
candidate->addr.port, candidate->username, candidate->password);
nice_udp_socket_send (sock, &sin_send, len, buf);
nice_udp_socket_send (sock, &send_addr, len, buf);
}
// IO loop
......@@ -105,7 +106,7 @@ accept_connection (
if (nice_agent_poll_read (agent, fds) == NULL)
continue;
len = nice_udp_socket_recv (sock, &sin_recv, 1024, buf);
len = nice_udp_socket_recv (sock, &recv_addr, 1024, buf);
buf[len] = '\0';
g_debug ("%s", buf);
......@@ -141,16 +142,14 @@ main (gint argc, gchar *argv[])
{
NiceUDPSocketFactory factory;
NiceUDPSocket sock;
struct sockaddr_in sin;
NiceAddress addr = {0,};
guint ret;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons (1234);
addr.port = 1234;
nice_udp_bsd_socket_factory_init (&factory);
if (!nice_udp_socket_factory_make (&factory, &sock, &sin))
if (!nice_udp_socket_factory_make (&factory, &sock, &addr))
g_assert_not_reached ();
ret = accept_connection (&factory, &sock);
......
#include <arpa/inet.h>
#include <stdlib.h>
#include <string.h>
......
include $(top_srcdir)/common.mk
AM_CFLAGS = -Wall -Werror $(GLIB_CFLAGS)
AM_CFLAGS = -Wall -Werror $(GLIB_CFLAGS) -I $(top_srcdir)/address
COMMON_LDADD = libudp.la $(GLIB_LIBS)
COMMON_LDADD = libudp.la $(GLIB_LIBS) $(top_builddir)/address/libaddress.la
noinst_LTLIBRARIES = libudp.la
......
......@@ -9,7 +9,7 @@ main (void)
{
NiceUDPSocketFactory man;
NiceUDPSocket sock;
struct sockaddr_in sin = {0,};
NiceAddress addr = {0,};
guint len;
gchar buf[1024];
......@@ -19,43 +19,40 @@ main (void)
/* create fake socket */
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = 0;
if (!nice_udp_socket_factory_make (&man, &sock, &sin))
if (!nice_udp_socket_factory_make (&man, &sock, &addr))
g_assert_not_reached ();
/* test recv */
memcpy (buf, "he\0lo", 5);
len = 5;
sin.sin_addr.s_addr = htonl (0x01020304);
sin.sin_port = htons (2345);
nice_udp_fake_socket_push_recv (&sock, &sin, len, buf);
addr.addr_ipv4 = 0x01020304;
addr.port = 2345;
nice_udp_fake_socket_push_recv (&sock, &addr, len, buf);
memset (buf, '\0', 5);
memset (&sin, '\0', sizeof (sin));
memset (&addr, '\0', sizeof (addr));
len = nice_udp_socket_recv (&sock, &sin, sizeof (buf), buf);
len = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf);
g_assert (len == 5);
g_assert (memcmp (buf, "he\0lo", 5) == 0);
g_assert (ntohl (sin.sin_addr.s_addr) == 0x01020304);
g_assert (ntohs (sin.sin_port) == 2345);
g_assert (addr.addr_ipv4 == 0x01020304);
g_assert (addr.port == 2345);
/* test send */
memcpy (buf, "la\0la", 5);
len = 5;
nice_udp_socket_send (&sock, &sin, len, buf);
nice_udp_socket_send (&sock, &addr, len, buf);
memset (buf, '\0', len);
memset (&sin, '\0', sizeof (sin));
memset (&addr, '\0', sizeof (addr));
len = nice_udp_fake_socket_pop_send (&sock, &sin, sizeof (buf), buf);
len = nice_udp_fake_socket_pop_send (&sock, &addr, sizeof (buf), buf);
g_assert (len == 5);
g_assert (0 == memcmp (buf, "la\0la", 5));
g_assert (ntohl (sin.sin_addr.s_addr) == 0x01020304);
g_assert (ntohs (sin.sin_port) == 2345);
g_assert (addr.addr_ipv4 == 0x01020304);
g_assert (addr.port == 2345);
nice_udp_socket_close (&sock);
nice_udp_socket_factory_close (&man);
......
......@@ -4,6 +4,8 @@
* http://en.wikipedia.org/wiki/Berkeley_sockets.)
*/
#include <arpa/inet.h>
#include <unistd.h>
#include "udp-bsd.h"
......@@ -13,15 +15,20 @@
static gint
socket_recv (
NiceUDPSocket *sock,
struct sockaddr_in *from,
NiceAddress *from,
guint len,
gchar *buf)
{
gint recvd;
guint from_len = sizeof (struct sockaddr_in);
struct sockaddr_in sin = {0,};
guint from_len = sizeof (sin);
recvd = recvfrom (sock->fileno, buf, len, 0, (struct sockaddr *) &sin,
&from_len);
recvd = recvfrom (sock->fileno, buf, len, 0,
(struct sockaddr *) from, &from_len);
from->type = NICE_ADDRESS_TYPE_IPV4;
from->addr_ipv4 = ntohl (sin.sin_addr.s_addr);
from->port = ntohs (sin.sin_port);
return recvd;
}
......@@ -29,12 +36,17 @@ socket_recv (
static gboolean
socket_send (
NiceUDPSocket *sock,
struct sockaddr_in *to,
NiceAddress *to,
guint len,
gchar *buf)
{
sendto (sock->fileno, buf, len, 0, (struct sockaddr *) to,
sizeof (struct sockaddr_in));
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (to->addr_ipv4);
sin.sin_port = htons (to->port);
sendto (sock->fileno, buf, len, 0, (struct sockaddr *) &sin, sizeof (sin));
return TRUE;
}
......@@ -50,29 +62,49 @@ static gboolean
socket_factory_init_socket (
NiceUDPSocketFactory *man,
NiceUDPSocket *sock,
struct sockaddr_in *sin)
NiceAddress *addr)
{
gint sockfd;
guint name_len = sizeof (struct sockaddr_in);
struct sockaddr_in name = {0,};
guint name_len = sizeof (name);
sockfd = socket (PF_INET, SOCK_DGRAM, 0);
if (sock < 0)
return FALSE;
if (sin != NULL)
if (bind (sockfd, (struct sockaddr *) sin, sizeof (*sin)) != 0)
{
close (sockfd);
return FALSE;
}
name.sin_family = AF_INET;
if (addr != NULL)
{
if (addr->addr_ipv4 != 0)
name.sin_addr.s_addr = htonl (addr->addr_ipv4);
else
name.sin_addr.s_addr = INADDR_ANY;
if (addr->port != 0)
name.sin_port = htons (addr->port);
}
if (getsockname (sockfd, (struct sockaddr *) &(sock->addr), &name_len) != 0)
if (bind (sockfd, (struct sockaddr *) &name, sizeof (name)) != 0)
{
close (sockfd);
return FALSE;
}
if (getsockname (sockfd, (struct sockaddr *) &name, &name_len) != 0)
{
close (sockfd);
return FALSE;
}
if (name.sin_addr.s_addr == INADDR_ANY)
sock->addr.addr_ipv4 = 0;
else
sock->addr.addr_ipv4 = ntohl (name.sin_addr.s_addr);
sock->addr.port = ntohs (name.sin_port);
sock->fileno = sockfd;
sock->send = socket_send;
sock->recv = socket_recv;
......
......@@ -9,18 +9,17 @@ main (void)
{
NiceUDPSocketFactory man;
NiceUDPSocket sock;
struct sockaddr_in sin;
NiceAddress addr;
nice_udp_bsd_socket_factory_init (&man);
if (!nice_udp_socket_factory_make (&man, &sock, NULL))
g_assert_not_reached ();
if (inet_pton (AF_INET, "127.0.0.1", &(sin.sin_addr)) < 0)
if (!nice_address_set_ipv4_from_string (&addr, "127.0.0.1"))
g_assert_not_reached ();
sin.sin_family = AF_INET;
sin.sin_port = htons (9999);
addr.port = 9999;
for (;;)
{
......@@ -30,8 +29,8 @@ main (void)
if (fgets (buf, sizeof (buf), stdin) == NULL)
break;
nice_udp_socket_send (&sock, &sin, strlen (buf), buf);
length = nice_udp_socket_recv (&sock, NULL, sizeof (buf), buf);
nice_udp_socket_send (&sock, &addr, strlen (buf), buf);
length = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf);
g_print (buf);
}
......
......@@ -6,14 +6,12 @@ main (void)
{
NiceUDPSocketFactory man;
NiceUDPSocket sock;
struct sockaddr_in sin;
NiceAddress addr = {0,};
nice_udp_bsd_socket_factory_init (&man);
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = INADDR_ANY;
sin.sin_port = htons (9999);
addr.port = 9999;
if (!man.init (&man, &sock, &sin))
if (!man.init (&man, &sock, &addr))
{
g_debug ("failed to find to port 9999: server already running?");
return 1;
......@@ -24,11 +22,11 @@ main (void)
gchar buf[1024];
guint length;
length = sock.recv (&sock, &sin, sizeof (buf), buf);
length = sock.recv (&sock, &addr, sizeof (buf), buf);
#ifdef DEBUG
g_debug ("%s:%d", inet_ntoa (sin.sin_addr), ntohs (sin.sin_port));
#endif
sock.send (&sock, &sin, length, buf);
sock.send (&sock, &addr, length, buf);
}
return 0;
......
#include <string.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <glib.h>
......@@ -12,7 +11,7 @@ typedef struct _Packet Packet;
struct _Packet
{
struct sockaddr_in sin;
NiceAddress addr;
guint len;
gchar buf[1024];
};
......@@ -45,7 +44,7 @@ _g_slist_pop (GSList **list)
static gboolean
fake_send (
NiceUDPSocket *sock,
struct sockaddr_in *to,
NiceAddress *to,
guint len,
gchar *buf)
{
......@@ -54,7 +53,7 @@ fake_send (
packet = g_slice_new0 (Packet);
packet->len = len;
packet->sin = *to;
packet->addr = *to;
memcpy (packet->buf, buf, len);
priv = (UDPFakeSocketPriv *) sock->priv;
......@@ -66,7 +65,7 @@ fake_send (
static gint