Commit 9f4af819 authored by Youness Alaoui's avatar Youness Alaoui

Removing useless socket files

parent d79a6983
#!/bin/sh
set -e
./udp-echo-server &
server_pid=$!
# give server a chance to bind to socket
sleep 1
output=`echo foo | ./udp-client`
kill $server_pid
test "$output" = foo || exit 1
exit 0
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* 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
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include "udp.h"
#include "udp-fake.h"
int
main (void)
{
NiceUDPSocketFactory man;
NiceUDPSocket sock;
NiceAddress addr;
gint len;
gchar buf[1024];
memset (&addr, 0, sizeof (addr));
nice_udp_fake_socket_factory_init (&man);
memset (buf, '\0', 1024);
/* create fake socket */
if (!nice_udp_socket_factory_make (&man, &sock, NULL))
g_assert_not_reached ();
/* test recv */
memcpy (buf, "he\0lo", 5);
len = 5;
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);
memset (&addr, '\0', sizeof (addr));
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.s.ip4.sin_addr.s_addr == htonl (0x01020304));
g_assert (nice_address_get_port (&addr) == 2345);
/* test send */
memcpy (buf, "la\0la", 5);
len = 5;
nice_udp_socket_send (&sock, &addr, len, buf);
memset (buf, '\0', len);
memset (&addr, '\0', sizeof (addr));
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.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);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* 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
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include "udp-bsd.h"
gint
main (void)
{
NiceUDPSocketFactory man;
NiceUDPSocket sock;
NiceAddress addr;
nice_udp_bsd_socket_factory_init (&man);
if (!nice_udp_socket_factory_make (&man, &sock, NULL))
g_assert_not_reached ();
if (!nice_address_set_from_string (&addr, "127.0.0.1"))
g_assert_not_reached ();
nice_address_set_port (&addr, 9999);
for (;;)
{
gchar buf[1024];
gint length;
if (fgets (buf, sizeof (buf), stdin) == NULL)
break;
nice_udp_socket_send (&sock, &addr, strlen (buf), buf);
length = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf);
g_print (buf);
}
nice_udp_socket_close (&sock);
nice_udp_socket_factory_close (&man);
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* 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
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "udp-bsd.h"
#include <string.h>
gint
main (void)
{
NiceUDPSocketFactory factory;
NiceUDPSocket sock;
NiceAddress addr;
nice_udp_bsd_socket_factory_init (&factory);
nice_address_set_ipv4 (&addr, 0);
nice_address_set_port (&addr, 9999);
if (!nice_udp_socket_factory_make (&factory, &sock, &addr))
{
g_debug ("failed to bind to port 9999: server already running?");
return 1;
}
for (;;)
{
gchar buf[1024];
gint length;
length = nice_udp_socket_recv (&sock, &addr, sizeof (buf), buf);
#ifdef DEBUG
{
gchar ip[NICE_ADDRESS_STRING_LEN];
nice_address_to_string (&addr, ip);
g_debug ("%s:%d", ip, addr.port);
}
#endif
nice_udp_socket_send (&sock, &addr, length, buf);
}
return 0;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* 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
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/uio.h>
#include <stdlib.h>
#include <pthread.h>
#include <glib.h>
#include "udp-fake.h"
typedef struct _UDPFakeSocketPriv UDPFakeSocketPriv;
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,
const NiceAddress *to,
guint len,
const gchar *buf)
{
return do_send (sock->fileno, buf, len, to) == (ssize_t)len;
}
static gint
fake_recv (
NiceUDPSocket *sock,
NiceAddress *from,
guint len,
gchar *buf)
{
return do_recv (sock->fileno, buf, len, from);
}
static void
fake_close (NiceUDPSocket *sock)
{
UDPFakeSocketPriv *priv;
close (sock->fileno);
priv = (UDPFakeSocketPriv *) sock->priv;
close (priv->net_sock);
g_slice_free (UDPFakeSocketPriv, priv);
}
/* XXX: copied INADDR_ANY to sock->addr rather than using a valid address */
static gboolean
fake_socket_init (
G_GNUC_UNUSED
NiceUDPSocketFactory *man,
NiceUDPSocket *sock,
NiceAddress *addr)
{
int fds[2];
static unsigned int port = 1;
UDPFakeSocketPriv *priv;
if (socketpair (AF_LOCAL, SOCK_STREAM, 0, fds) != 0)
return FALSE;
priv = g_slice_new0 (UDPFakeSocketPriv);
priv->net_sock = fds[0];
sock->fileno = fds[1];
if (addr)
sock->addr = *addr;
else
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;
sock->priv = priv;
sock->close = fake_close;
return TRUE;
}
NICEAPI_EXPORT void
nice_udp_fake_socket_push_recv (
NiceUDPSocket *sock,
const NiceAddress *from,
guint len,
const gchar *buf)
{
UDPFakeSocketPriv *priv;
priv = (UDPFakeSocketPriv *) sock->priv;
if (do_send (priv->net_sock, buf, len, from) != (ssize_t)len)
/* Not much we can do here */
abort ();
}
NICEAPI_EXPORT guint
nice_udp_fake_socket_pop_send (
NiceUDPSocket *sock,
NiceAddress *to,
guint len,
gchar *buf)
{
UDPFakeSocketPriv *priv;
priv = (UDPFakeSocketPriv *) sock->priv;
return do_recv (priv->net_sock, buf, len, to);
}
NICEAPI_EXPORT guint
nice_udp_fake_socket_get_peer_fd (
NiceUDPSocket *sock)
{
UDPFakeSocketPriv *priv;
priv = (UDPFakeSocketPriv *) sock->priv;
return priv->net_sock;
}
static void
fake_socket_factory_close (
G_GNUC_UNUSED
NiceUDPSocketFactory *man)
{
}
NICEAPI_EXPORT void
nice_udp_fake_socket_factory_init (NiceUDPSocketFactory *man)
{
man->init = fake_socket_init;
man->close = fake_socket_factory_close;
man->priv = NULL;
}
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006, 2007 Collabora Ltd.
* Contact: Dafydd Harries
* (C) 2006, 2007 Nokia Corporation. All rights reserved.
* Contact: Kai Vehmanen
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the Nice GLib ICE library.
*
* The Initial Developers of the Original Code are Collabora Ltd and Nokia
* Corporation. All Rights Reserved.
*
* Contributors:
* Dafydd Harries, Collabora Ltd.
*
* 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
* case the provisions of LGPL are applicable instead of those above. If you
* wish to allow use of your version of this file only under the terms of the
* LGPL and not to allow others to use your version of this file under the
* MPL, indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL. If you do
* not delete the provisions above, a recipient may use your version of this
* file under either the MPL or the LGPL.
*/
#ifndef _UDP_FAKE_H
#define _UDP_FAKE_H
#include "udp.h"
G_BEGIN_DECLS
void
nice_udp_fake_socket_factory_init (NiceUDPSocketFactory *man);
void
nice_udp_fake_socket_push_recv (
NiceUDPSocket *man,
const NiceAddress *from,
guint len,
const gchar *buf);
guint
nice_udp_fake_socket_pop_send (
NiceUDPSocket *man,
NiceAddress *to,
guint len,
gchar *buf);
guint
nice_udp_fake_socket_get_peer_fd (
NiceUDPSocket *sock);
G_END_DECLS
#endif /* _UDP_FAKE_H */
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