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