Commit 0cf65d14 authored by Youness Alaoui's avatar Youness Alaoui

Socket layer refactorising and adding of tcp-bsd, pseudossl, and moved the tcp...

Socket layer refactorising and adding of tcp-bsd, pseudossl, and moved the tcp stuff from tcp-turn and made udp-turn into turn since it's generic
parent 24261d9d
......@@ -689,7 +689,7 @@ void agent_signal_new_selected_pair (NiceAgent *agent, guint stream_id, guint co
rf_copy = g_strdup (remote_foundation);
if (component->selected_pair.local->type == NICE_CANDIDATE_TYPE_RELAYED) {
nice_udp_turn_socket_set_peer (component->selected_pair.local->sockptr,
nice_turn_socket_set_peer (component->selected_pair.local->sockptr,
&component->selected_pair.remote->addr);
}
......@@ -822,10 +822,14 @@ priv_add_new_candidate_discovery_turn (NiceAgent *agent,
}
cdisco->nicesock = socket;
} else {
cdisco->nicesock = nice_tcp_turn_socket_new (agent,
component->ctx,
&turn->server,
priv_agent_to_turn_compatibility (agent));
socket = nice_tcp_bsd_socket_new (agent, component->ctx, &turn->server);
if (turn->type == NICE_RELAY_TYPE_TURN_TLS &&
agent->compatibility == NICE_COMPATIBILITY_GOOGLE) {
socket = nice_pseudossl_socket_new (agent, socket);
}
cdisco->nicesock = nice_tcp_turn_socket_new (agent, socket,
agent_to_turn_socket_compatibility (agent));
if (!cdisco->nicesock) {
agent->discovery_list = g_slist_remove (modified_list, cdisco);
g_slice_free (CandidateDiscovery, cdisco);
......@@ -1367,7 +1371,7 @@ _nice_agent_recv (
if (cand->type == NICE_CANDIDATE_TYPE_RELAYED &&
cand->stream_id == stream->id &&
cand->component_id == component->id) {
len = nice_udp_turn_socket_parse_recv (cand->sockptr, &socket,
len = nice_turn_socket_parse_recv (cand->sockptr, &socket,
&from, len, buf, &from, buf, len);
}
}
......
......@@ -530,7 +530,7 @@ discovery_add_relay_candidate (
candidate->turn = turn;
/* step: link to the base candidate+socket */
relay_socket = nice_udp_turn_socket_new (agent, address,
relay_socket = nice_turn_socket_new (agent, address,
base_socket, &turn->server,
turn->username, turn->password,
agent_to_turn_socket_compatibility (agent));
......
......@@ -24,8 +24,12 @@ libsocket_la_SOURCES = \
socket.c \
udp-bsd.h \
udp-bsd.c \
udp-turn.h \
udp-turn.c \
tcp-bsd.h \
tcp-bsd.c \
pseudossl.h \
pseudossl.c \
turn.h \
turn.c \
tcp-turn.h \
tcp-turn.c
......@@ -36,5 +40,5 @@ test_bsd_LDADD = $(COMMON_LDADD)
TESTS = $(check_PROGRAMS)
pkginclude_HEADERS = socket.h udp-bsd.h udp-turn.h tcp-turn.h
pkginclude_HEADERS = socket.h udp-bsd.h tcp-bsd.h pseudossl.h turn.h tcp-turn.h
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006-2008 Collabora Ltd.
* Contact: Dafydd Harries
* Contact: Olivier Crete
* (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.
* Olivier Crete, 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
* 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.
*/
/*
* Implementation of TCP relay socket interface using TCP Berkeley sockets. (See
* http://en.wikipedia.org/wiki/Berkeley_sockets.)
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "pseudossl.h"
#include <string.h>
#ifndef G_OS_WIN32
#include <unistd.h>
#endif
typedef struct {
gboolean handshaken;
NiceSocket *base_socket;
GQueue send_queue;
} PseudoSSLPriv;
struct to_be_sent {
guint length;
gchar *buf;
NiceAddress to;
};
static const gchar SSL_SERVER_HANDSHAKE[] = {
0x16, 0x03, 0x01, 0x00, 0x4a, 0x02, 0x00, 0x00,
0x46, 0x03, 0x01, 0x42, 0x85, 0x45, 0xa7, 0x27,
0xa9, 0x5d, 0xa0, 0xb3, 0xc5, 0xe7, 0x53, 0xda,
0x48, 0x2b, 0x3f, 0xc6, 0x5a, 0xca, 0x89, 0xc1,
0x58, 0x52, 0xa1, 0x78, 0x3c, 0x5b, 0x17, 0x46,
0x00, 0x85, 0x3f, 0x20, 0x0e, 0xd3, 0x06, 0x72,
0x5b, 0x5b, 0x1b, 0x5f, 0x15, 0xac, 0x13, 0xf9,
0x88, 0x53, 0x9d, 0x9b, 0xe8, 0x3d, 0x7b, 0x0c,
0x30, 0x32, 0x6e, 0x38, 0x4d, 0xa2, 0x75, 0x57,
0x41, 0x6c, 0x34, 0x5c, 0x00, 0x04, 0x00};
static const gchar SSL_CLIENT_HANDSHAKE[] = {
0x80, 0x46, 0x01, 0x03, 0x01, 0x00, 0x2d, 0x00,
0x00, 0x00, 0x10, 0x01, 0x00, 0x80, 0x03, 0x00,
0x80, 0x07, 0x00, 0xc0, 0x06, 0x00, 0x40, 0x02,
0x00, 0x80, 0x04, 0x00, 0x80, 0x00, 0x00, 0x04,
0x00, 0xfe, 0xff, 0x00, 0x00, 0x0a, 0x00, 0xfe,
0xfe, 0x00, 0x00, 0x09, 0x00, 0x00, 0x64, 0x00,
0x00, 0x62, 0x00, 0x00, 0x03, 0x00, 0x00, 0x06,
0x1f, 0x17, 0x0c, 0xa6, 0x2f, 0x00, 0x78, 0xfc,
0x46, 0x55, 0x2e, 0xb1, 0x83, 0x39, 0xf1, 0xea};
static void socket_close (NiceSocket *sock);
static gint socket_recv (NiceSocket *sock, NiceAddress *from,
guint len, gchar *buf);
static gboolean socket_send (NiceSocket *sock, const NiceAddress *to,
guint len, const gchar *buf);
static gboolean socket_is_reliable (NiceSocket *sock);
static void add_to_be_sent (NiceSocket *sock, const NiceAddress *to,
const gchar *buf, guint len);
NiceSocket *
nice_pseudossl_socket_new (NiceAgent *agent, NiceSocket *base_socket)
{
PseudoSSLPriv *priv;
NiceSocket *sock = g_slice_new0 (NiceSocket);
sock->priv = priv = g_slice_new0 (PseudoSSLPriv);
priv->handshaken = FALSE;
priv->base_socket = base_socket;
sock->fileno = priv->base_socket->fileno;
sock->addr = priv->base_socket->addr;
sock->send = socket_send;
sock->recv = socket_recv;
sock->is_reliable = socket_is_reliable;
sock->close = socket_close;
/* We send 'to' NULL because it will always be to an already connected
* TCP base socket, which ignores the destination */
nice_socket_send (priv->base_socket, NULL,
sizeof(SSL_CLIENT_HANDSHAKE), SSL_CLIENT_HANDSHAKE);
return sock;
}
static void
socket_close (NiceSocket *sock)
{
PseudoSSLPriv *priv = sock->priv;
if (priv->base_socket)
nice_socket_free (priv->base_socket);
g_slice_free(PseudoSSLPriv, sock->priv);
}
static gint
socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf)
{
PseudoSSLPriv *priv = sock->priv;
if (priv->handshaken) {
if (priv->base_socket)
return nice_socket_recv (priv->base_socket, from, len, buf);
} else {
gchar data[sizeof(SSL_SERVER_HANDSHAKE)];
gint ret = -1;
if (priv->base_socket)
ret = nice_socket_recv (priv->base_socket, from, sizeof(data), data);
if (ret <= 0) {
return ret;
} else if ((guint) ret == sizeof(SSL_SERVER_HANDSHAKE) &&
memcmp(SSL_SERVER_HANDSHAKE, data, sizeof(SSL_SERVER_HANDSHAKE)) == 0) {
priv->handshaken = TRUE;
struct to_be_sent *tbs = NULL;
while ((tbs = g_queue_pop_head (&priv->send_queue))) {
nice_socket_send (priv->base_socket, &tbs->to, tbs->length, tbs->buf);
g_free (tbs->buf);
g_slice_free (struct to_be_sent, tbs);
}
} else {
if (priv->base_socket)
nice_socket_free (priv->base_socket);
priv->base_socket = NULL;
}
}
return 0;
}
static gboolean
socket_send (NiceSocket *sock, const NiceAddress *to,
guint len, const gchar *buf)
{
PseudoSSLPriv *priv = sock->priv;
if (priv->handshaken) {
if (priv->base_socket)
return nice_socket_send (priv->base_socket, to, len, buf);
else
return FALSE;
} else {
add_to_be_sent (sock, to, buf, len);
}
return TRUE;
}
static gboolean
socket_is_reliable (NiceSocket *sock)
{
return TRUE;
}
static void
add_to_be_sent (NiceSocket *sock, const NiceAddress *to,
const gchar *buf, guint len)
{
PseudoSSLPriv *priv = sock->priv;
struct to_be_sent *tbs = NULL;
if (len <= 0)
return;
tbs = g_slice_new (struct to_be_sent);
tbs->buf = g_memdup (buf, len);
tbs->length = len;
tbs->to = *to;
g_queue_push_tail (&priv->send_queue, tbs);
}
/*
* 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 _PSEUDOSSL_H
#define _PSEUDOSSL_H
#include "socket.h"
#include "agent.h"
G_BEGIN_DECLS
NiceSocket *
nice_pseudossl_socket_new (NiceAgent *agent, NiceSocket *base_socket);
G_END_DECLS
#endif /* _PSEUDOSSL_H */
......@@ -45,21 +45,14 @@
gint
nice_socket_recv (
NiceSocket *sock,
NiceAddress *from,
guint len,
gchar *buf)
nice_socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf)
{
return sock->recv (sock, from, len, buf);
}
gboolean
nice_socket_send (
NiceSocket *sock,
const NiceAddress *to,
guint len,
const gchar *buf)
nice_socket_send (NiceSocket *sock, const NiceAddress *to,
guint len, const gchar *buf)
{
return sock->send (sock, to, len, buf);
}
......
......@@ -69,18 +69,11 @@ struct _NiceSocket
G_GNUC_WARN_UNUSED_RESULT
gint
nice_socket_recv (
NiceSocket *sock,
NiceAddress *from,
guint len,
gchar *buf);
nice_socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf);
gboolean
nice_socket_send (
NiceSocket *sock,
const NiceAddress *to,
guint len,
const gchar *buf);
nice_socket_send (NiceSocket *sock, const NiceAddress *to,
guint len, const gchar *buf);
gboolean
nice_socket_is_reliable (NiceSocket *sock);
......@@ -90,7 +83,9 @@ void
nice_socket_free (NiceSocket *sock);
#include "udp-bsd.h"
#include "udp-turn.h"
#include "tcp-bsd.h"
#include "pseudossl.h"
#include "turn.h"
#include "tcp-turn.h"
G_END_DECLS
......
/*
* This file is part of the Nice GLib ICE library.
*
* (C) 2006-2008 Collabora Ltd.
* Contact: Dafydd Harries
* Contact: Olivier Crete
* (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.
* Olivier Crete, 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
* 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.
*/
/*
* Implementation of TCP relay socket interface using TCP Berkeley sockets. (See
* http://en.wikipedia.org/wiki/Berkeley_sockets.)
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "tcp-bsd.h"
#include "agent-priv.h"
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#ifndef G_OS_WIN32
#include <unistd.h>
#endif
typedef struct {
NiceAgent *agent;
NiceAddress server_addr;
GQueue send_queue;
GMainContext *context;
GIOChannel *io_channel;
GSource *io_source;
} TcpPriv;
struct to_be_sent {
guint length;
gchar *buf;
};
static void socket_close (NiceSocket *sock);
static gint socket_recv (NiceSocket *sock, NiceAddress *from,
guint len, gchar *buf);
static gboolean socket_send (NiceSocket *sock, const NiceAddress *to,
guint len, const gchar *buf);
static gboolean socket_is_reliable (NiceSocket *sock);
static void add_to_be_sent (NiceSocket *sock, const gchar *buf, guint len,
gboolean head);
static void free_to_be_sent (struct to_be_sent *tbs);
static gboolean socket_send_more (GIOChannel *source, GIOCondition condition,
gpointer data);
NiceSocket *
nice_tcp_bsd_socket_new (NiceAgent *agent, GMainContext *ctx, NiceAddress *addr)
{
int sockfd = -1;
int ret;
struct sockaddr_storage name;
guint name_len = sizeof (name);
NiceSocket *sock = g_slice_new0 (NiceSocket);
TcpPriv *priv;
if (addr != NULL) {
nice_address_copy_to_sockaddr(addr, (struct sockaddr *)&name);
} else {
memset (&name, 0, sizeof (name));
name.ss_family = AF_UNSPEC;
}
if ((sockfd == -1) &&
((name.ss_family == AF_UNSPEC) ||
(name.ss_family == AF_INET))) {
sockfd = socket (PF_INET, SOCK_STREAM, 0);
name.ss_family = AF_INET;
#ifdef HAVE_SA_LEN
name.ss_len = sizeof (struct sockaddr_in);
#endif
}
if (sockfd == -1) {
g_slice_free (NiceSocket, sock);
return NULL;
}
#ifdef FD_CLOEXEC
fcntl (sockfd, F_SETFD, fcntl (sockfd, F_GETFD) | FD_CLOEXEC);
#endif
#ifdef O_NONBLOCK
fcntl (sockfd, F_SETFL, fcntl (sockfd, F_GETFL) | O_NONBLOCK);
#endif
name_len = name.ss_family == AF_INET? sizeof (struct sockaddr_in) :
sizeof(struct sockaddr_in6);
ret = connect (sockfd, (const struct sockaddr *)&name, name_len);
#ifdef G_OS_WIN32
if (ret < 0 && WSAGetLastError () != WSAEINPROGRESS) {
closesocket (sockfd);
#else
if (ret < 0 && errno != EINPROGRESS) {
close (sockfd);
#endif
g_slice_free (NiceSocket, sock);
return NULL;
}
name_len = name.ss_family == AF_INET? sizeof (struct sockaddr_in) :
sizeof(struct sockaddr_in6);
if (getsockname (sockfd, (struct sockaddr *) &name, &name_len) < 0) {
g_slice_free (NiceSocket, sock);
#ifdef G_OS_WIN32
closesocket(sockfd);
#else
close (sockfd);
#endif
return NULL;
}
nice_address_set_from_sockaddr (&sock->addr, (struct sockaddr *)&name);
sock->priv = priv = g_slice_new0 (TcpPriv);
priv->agent = agent;
priv->context = ctx;
priv->server_addr = *addr;
sock->fileno = sockfd;
sock->send = socket_send;
sock->recv = socket_recv;
sock->is_reliable = socket_is_reliable;
sock->close = socket_close;
return sock;
}
static void
socket_close (NiceSocket *sock)
{
TcpPriv *priv = sock->priv;
#ifdef G_OS_WIN32
closesocket(sock->fileno);
#else
close (sock->fileno);
#endif
if (priv->io_source) {
g_source_destroy (priv->io_source);
g_source_unref (priv->io_source);
}
if (priv->io_channel)
g_io_channel_unref (priv->io_channel);
g_queue_foreach (&priv->send_queue, (GFunc) free_to_be_sent, NULL);
g_queue_clear (&priv->send_queue);
g_slice_free(TcpPriv, sock->priv);
}
static gint
socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf)
{
TcpPriv *priv = sock->priv;
int ret;
ret = recv (sock->fileno, buf, len, 0);
if (ret < 0) {
#ifdef G_OS_WIN32
if (WSAGetLastError () == WSAEWOULDBLOCK)
#else
if (errno == EAGAIN)
#endif
return 0;
else
return ret;
}
if (from)
*from = priv->server_addr;
return ret;
}
static gboolean
socket_send (NiceSocket *sock, const NiceAddress *to,
guint len, const gchar *buf)
{
TcpPriv *priv = sock->priv;
int ret;
/* First try to send the data, don't send it later if it can be sent now
this way we avoid allocating memory on every send */
if (g_queue_is_empty (&priv->send_queue)) {
ret = send (sock->fileno, buf, len, 0);
if (ret < 0) {
#ifdef G_OS_WIN32
if (WSAGetLastError () == WSAEWOULDBLOCK) {
#else
if (errno == EAGAIN) {
#endif
add_to_be_sent (sock, buf, len, FALSE);
return TRUE;
} else {
return FALSE;
}
} else if ((guint)ret < len) {
add_to_be_sent (sock, buf + ret, len - ret, FALSE);
return TRUE;
}
} else {
add_to_be_sent (sock, buf, len, FALSE);
}
return TRUE;
}
static gboolean
socket_is_reliable (NiceSocket *sock)
{
return TRUE;
}
/*
* Returns:
* -1 = error
* 0 = have more to send
* 1 = sent everything
*/
static gboolean
socket_send_more (
GIOChannel *source,
GIOCondition condition,
gpointer data)
{
NiceSocket *sock = (NiceSocket *) data;
TcpPriv *priv = sock->priv;
struct to_be_sent *tbs = NULL;
g_static_rec_mutex_lock (&priv->agent->mutex);
while ((tbs = g_queue_pop_head (&priv->send_queue))) {
int ret;
ret = send (sock->fileno, tbs->buf, tbs->length, 0);
if (ret < 0) {
#ifdef G_OS_WIN32
if (WSAGetLastError () == WSAEWOULDBLOCK) {
#else
if (errno == EAGAIN) {
#endif
add_to_be_sent (sock, tbs->buf, tbs->length, TRUE);
g_free (tbs->buf);
g_slice_free (struct to_be_sent, tbs);
break;
}
} else if (ret < (int) tbs->length) {
add_to_be_sent (sock, tbs->buf + ret, tbs->length - ret, TRUE);
g_free (tbs->buf);
g_slice_free (struct to_be_sent, tbs);
break;
}
g_free (tbs->buf);
g_slice_free (struct to_be_sent, tbs);
}
if (g_queue_is_empty (&priv->send_queue)) {
g_io_channel_unref (priv->io_channel);
priv->io_channel = NULL;
g_source_destroy (priv->io_source);
g_source_unref (priv->io_source);
priv->io_source = NULL;
g_static_rec_mutex_unlock (&priv->agent->mutex);
return FALSE;
}
g_static_rec_mutex_unlock (&priv->agent->mutex);
return TRUE;
}
static void
add_to_be_sent (NiceSocket *sock, const gchar *buf, guint len, gboolean head)
{
TcpPriv *priv = sock->priv;
struct to_be_sent *tbs = NULL;