Commit d5d4b680 authored by Kai Vehmanen's avatar Kai Vehmanen

Updated udp module to use the new address API.

darcs-hash:20070830071158-77cd4-dca758b5e555e052afffd618ce830179a85ce9ab.gz
parent 4af42601
......@@ -54,27 +54,31 @@ main (void)
memset (&tmp, 0, sizeof (tmp));
nice_udp_bsd_socket_factory_init (&factory);
g_assert (nice_udp_socket_factory_make (&factory, &server, NULL));
if (!nice_udp_socket_factory_make (&factory, &server, NULL))
g_assert_not_reached();
// not bound to a particular interface
g_assert (server.addr.addr.addr_ipv4 == 0);
g_assert (server.addr.s.ip4.sin_addr.s_addr == 0);
// is bound to a particular port
g_assert (server.addr.port != 0);
g_assert (nice_address_get_port (&server.addr) != 0);
g_assert (nice_udp_socket_factory_make (&factory, &client, NULL));
// not bound to a particular interface
g_assert (client.addr.addr.addr_ipv4 == 0);
g_assert (client.addr.s.ip4.sin_addr.s_addr == 0);
// is bound to a particular port
g_assert (client.addr.port != 0);
g_assert (nice_address_get_port (&client.addr) != 0);
nice_udp_socket_send (&client, &server.addr, 5, "hello");
g_assert (5 == nice_udp_socket_recv (&server, &tmp, 5, buf));
g_assert (0 == strncmp (buf, "hello", 5));
g_assert (tmp.port == client.addr.port);
g_assert (nice_address_get_port (&tmp)
== nice_address_get_port (&client.addr));
nice_udp_socket_send (&server, &client.addr, 5, "uryyb");
g_assert (5 == nice_udp_socket_recv (&client, &tmp, 5, buf));
g_assert (0 == strncmp (buf, "uryyb", 5));
g_assert (tmp.port == server.addr.port);
g_assert (nice_address_get_port (&tmp)
== nice_address_get_port (&server.addr));
nice_udp_socket_close (&client);
nice_udp_socket_close (&server);
......
......@@ -49,7 +49,7 @@ main (void)
NiceUDPSocketFactory man;
NiceUDPSocket sock;
NiceAddress addr;
guint len;
gint len;
gchar buf[1024];
memset (&addr, 0, sizeof (addr));
......@@ -59,15 +59,15 @@ main (void)
/* create fake socket */
if (!nice_udp_socket_factory_make (&man, &sock, &addr))
if (!nice_udp_socket_factory_make (&man, &sock, NULL))
g_assert_not_reached ();
/* test recv */
memcpy (buf, "he\0lo", 5);
len = 5;
addr.addr.addr_ipv4 = 0x01020304;
addr.port = 2345;
nice_address_set_ipv4 (&addr, 0x01020304);
nice_address_set_port (&addr, 2345);
nice_udp_fake_socket_push_recv (&sock, &addr, len, buf);
memset (buf, '\0', 5);
......@@ -76,8 +76,8 @@ main (void)
len = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf);
g_assert (len == 5);
g_assert (memcmp (buf, "he\0lo", 5) == 0);
g_assert (addr.addr.addr_ipv4 == 0x01020304);
g_assert (addr.port == 2345);
g_assert (addr.s.ip4.sin_addr.s_addr == htonl (0x01020304));
g_assert (nice_address_get_port (&addr) == 2345);
/* test send */
......@@ -91,8 +91,8 @@ main (void)
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 (addr.addr.addr_ipv4 == 0x01020304);
g_assert (addr.port == 2345);
g_assert (addr.s.ip4.sin_addr.s_addr == htonl (0x01020304));
g_assert (nice_address_get_port (&addr) == 2345);
nice_udp_socket_close (&sock);
nice_udp_socket_factory_close (&man);
......
......@@ -23,6 +23,8 @@
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
* Rémi Denis-Courmont, Nokia
* Kai Vehmanen
*
* Alternatively, the contents of this file may be used under the terms of the
* the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
......@@ -77,11 +79,10 @@ socket_recv (
gchar *buf)
{
gint recvd;
struct sockaddr_in sin;
guint from_len = sizeof (sin);
struct sockaddr_storage sa;
guint from_len = sizeof (sa);
memset (&sin, 0, sizeof (sin));
recvd = recvfrom (sock->fileno, buf, len, 0, (struct sockaddr *) &sin,
recvd = recvfrom (sock->fileno, buf, len, 0, (struct sockaddr *) &sa,
&from_len);
if (recvd == -1)
{
......@@ -89,30 +90,25 @@ socket_recv (
return -1;
}
from->type = NICE_ADDRESS_TYPE_IPV4;
from->addr.addr_ipv4 = ntohl (sin.sin_addr.s_addr);
from->port = ntohs (sin.sin_port);
nice_address_set_from_sockaddr (from, (struct sockaddr *)&sa);
return recvd;
}
static gboolean
socket_send (
NiceUDPSocket *sock,
NiceAddress *to,
const NiceAddress *to,
guint len,
const gchar *buf)
{
struct sockaddr_in sin;
struct sockaddr_storage sa;
ssize_t sent;
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl (to->addr.addr_ipv4);
sin.sin_port = htons (to->port);
nice_address_copy_to_sockaddr (to, (struct sockaddr *)&sa);
do
sent = sendto (sock->fileno, buf, len, 0, (struct sockaddr *) &sin,
sizeof (sin));
sent = sendto (sock->fileno, buf, len, 0, (struct sockaddr *) &sa,
sizeof (sa));
while ((sent == -1) && sock_recv_err (sock->fileno));
return sent == (ssize_t)len;
......@@ -134,7 +130,7 @@ socket_factory_init_socket (
NiceAddress *addr)
{
gint sockfd;
struct sockaddr_in name;
struct sockaddr_storage name;
guint name_len = sizeof (name);
(void)man;
......@@ -151,20 +147,21 @@ socket_factory_init_socket (
}
#endif
name.sin_family = AF_INET;
if (addr != NULL)
{
if (addr->addr.addr_ipv4 != 0)
name.sin_addr.s_addr = htonl (addr->addr.addr_ipv4);
else
name.sin_addr.s_addr = INADDR_ANY;
if (addr->port != 0)
name.sin_port = htons (addr->port);
nice_address_copy_to_sockaddr(addr, (struct sockaddr *)&name);
}
else
{
name.ss_family = AF_INET;
#ifdef HAVE_SA_LEN
name.ss_len = sizeof (name);
#endif
}
if (bind (sockfd, (struct sockaddr *) &name, sizeof (name)) != 0)
if(bind (sockfd, (struct sockaddr *) &name, sizeof (name)) != 0)
{
close (sockfd);
return FALSE;
......@@ -176,12 +173,7 @@ socket_factory_init_socket (
return FALSE;
}
if (name.sin_addr.s_addr == INADDR_ANY)
sock->addr.addr.addr_ipv4 = 0;
else
sock->addr.addr.addr_ipv4 = ntohl (name.sin_addr.s_addr);
sock->addr.port = ntohs (name.sin_port);
nice_address_set_from_sockaddr (&sock->addr, (struct sockaddr *)&name);
sock->fileno = sockfd;
sock->send = socket_send;
......
......@@ -55,15 +55,15 @@ main (void)
if (!nice_udp_socket_factory_make (&man, &sock, NULL))
g_assert_not_reached ();
if (!nice_address_set_ipv4_from_string (&addr, "127.0.0.1"))
if (!nice_address_set_from_string (&addr, "127.0.0.1"))
g_assert_not_reached ();
addr.port = 9999;
nice_address_set_port (&addr, 9999);
for (;;)
{
gchar buf[1024];
guint length;
gint length;
if (fgets (buf, sizeof (buf), stdin) == NULL)
break;
......
......@@ -48,9 +48,9 @@ main (void)
NiceUDPSocket sock;
NiceAddress addr;
memset (&addr, 0, sizeof (addr));
nice_udp_bsd_socket_factory_init (&factory);
addr.port = 9999;
nice_address_set_ipv4 (&addr, 0);
nice_address_set_port (&addr, 9999);
if (!nice_udp_socket_factory_make (&factory, &sock, &addr))
{
......@@ -61,7 +61,7 @@ main (void)
for (;;)
{
gchar buf[1024];
guint length;
gint length;
length = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf);
#ifdef DEBUG
......
......@@ -43,6 +43,9 @@
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <stdlib.h>
#include <pthread.h>
#include <glib.h>
......@@ -55,18 +58,60 @@ struct _UDPFakeSocketPriv
guint net_sock;
};
static
ssize_t do_send (int fd, const void *buf, size_t len, const NiceAddress *to)
{
ssize_t total = sizeof (*to) + sizeof (len);
struct iovec iov[3];
iov[0].iov_base = (void *)to;
iov[0].iov_len = sizeof (*to);
iov[1].iov_base = &len;
iov[1].iov_len = sizeof (len);
iov[2].iov_base = (void *)buf;
iov[2].iov_len = len;
total += len;
if (writev (fd, iov, 3) != total)
return -1;
return len;
}
static
ssize_t do_recv (int fd, void *buf, size_t len, NiceAddress *from)
{
static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
struct iovec iov[2];
ssize_t res;
iov[0].iov_base = from;
iov[0].iov_len = sizeof (*from);
iov[1].iov_base = &len;
iov[1].iov_len = sizeof (len);
pthread_mutex_lock (&lock);
if ((readv (fd, iov, 2) != (sizeof (*from) + sizeof (len)))
|| (read (fd, buf, len) != (ssize_t)len))
res = -1;
else
res = len;
pthread_mutex_unlock (&lock);
return len;
}
static gboolean
fake_send (
NiceUDPSocket *sock,
NiceAddress *to,
const NiceAddress *to,
guint len,
const gchar *buf)
{
write (sock->fileno, to, sizeof (NiceAddress));
write (sock->fileno, &len, sizeof (guint));
write (sock->fileno, buf, len);
return TRUE;
return do_send (sock->fileno, buf, len, to) == (ssize_t)len;
}
static gint
......@@ -76,11 +121,7 @@ fake_recv (
guint len,
gchar *buf)
{
read (sock->fileno, from, sizeof (NiceAddress));
read (sock->fileno, &len, sizeof (guint));
read (sock->fileno, buf, len);
return len;
return do_recv (sock->fileno, buf, len, from);
}
static void
......@@ -104,7 +145,7 @@ fake_socket_init (
NiceAddress *addr)
{
int fds[2];
static int port = 1;
static unsigned int port = 1;
UDPFakeSocketPriv *priv;
if (socketpair (AF_LOCAL, SOCK_STREAM, 0, fds) != 0)
......@@ -114,13 +155,13 @@ fake_socket_init (
priv->net_sock = fds[0];
sock->fileno = fds[1];
sock->addr.type = addr->type;
sock->addr.addr.addr_ipv4 = addr->addr.addr_ipv4;
if (addr->port == 0)
sock->addr.port = port++;
if (addr)
sock->addr = *addr;
else
sock->addr.port = addr->port;
nice_address_set_ipv4 (&sock->addr, 0);
if (!addr || !nice_address_get_port (addr))
nice_address_set_port (&sock->addr, port++);
sock->send = fake_send;
sock->recv = fake_recv;
......@@ -132,7 +173,7 @@ fake_socket_init (
NICEAPI_EXPORT void
nice_udp_fake_socket_push_recv (
NiceUDPSocket *sock,
NiceAddress *from,
const NiceAddress *from,
guint len,
const gchar *buf)
{
......@@ -140,9 +181,9 @@ nice_udp_fake_socket_push_recv (
priv = (UDPFakeSocketPriv *) sock->priv;
write (priv->net_sock, from, sizeof (NiceAddress));
write (priv->net_sock, &len, sizeof (guint));
write (priv->net_sock, buf, len);
if (do_send (priv->net_sock, buf, len, from) != (ssize_t)len)
/* Not much we can do here */
abort ();
}
NICEAPI_EXPORT guint
......@@ -156,11 +197,7 @@ nice_udp_fake_socket_pop_send (
priv = (UDPFakeSocketPriv *) sock->priv;
read (priv->net_sock, to, sizeof (NiceAddress));
read (priv->net_sock, &len, sizeof (guint));
read (priv->net_sock, buf, len);
return len;
return do_recv (priv->net_sock, buf, len, to);
}
NICEAPI_EXPORT guint
......
......@@ -48,7 +48,7 @@ nice_udp_fake_socket_factory_init (NiceUDPSocketFactory *man);
void
nice_udp_fake_socket_push_recv (
NiceUDPSocket *man,
NiceAddress *from,
const NiceAddress *from,
guint len,
const gchar *buf);
......
......@@ -59,7 +59,7 @@ nice_udp_socket_factory_close (NiceUDPSocketFactory *man)
man->close (man);
}
NICEAPI_EXPORT guint
NICEAPI_EXPORT gint
nice_udp_socket_recv (
NiceUDPSocket *sock,
NiceAddress *from,
......@@ -72,7 +72,7 @@ nice_udp_socket_recv (
NICEAPI_EXPORT void
nice_udp_socket_send (
NiceUDPSocket *sock,
NiceAddress *to,
const NiceAddress *to,
guint len,
const gchar *buf)
{
......
......@@ -50,7 +50,7 @@ struct _NiceUDPSocket
guint fileno;
gint (*recv) (NiceUDPSocket *sock, NiceAddress *from, guint len,
gchar *buf);
gboolean (*send) (NiceUDPSocket *sock, NiceAddress *to, guint len,
gboolean (*send) (NiceUDPSocket *sock, const NiceAddress *to, guint len,
const gchar *buf);
void (*close) (NiceUDPSocket *sock);
void *priv;
......@@ -82,7 +82,7 @@ void
nice_udp_socket_factory_close (NiceUDPSocketFactory *man);
G_GNUC_WARN_UNUSED_RESULT
guint
gint
nice_udp_socket_recv (
NiceUDPSocket *sock,
NiceAddress *from,
......@@ -92,7 +92,7 @@ nice_udp_socket_recv (
void
nice_udp_socket_send (
NiceUDPSocket *sock,
NiceAddress *to,
const NiceAddress *to,
guint len,
const gchar *buf);
......
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