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 ...@@ -689,7 +689,7 @@ void agent_signal_new_selected_pair (NiceAgent *agent, guint stream_id, guint co
rf_copy = g_strdup (remote_foundation); rf_copy = g_strdup (remote_foundation);
if (component->selected_pair.local->type == NICE_CANDIDATE_TYPE_RELAYED) { 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); &component->selected_pair.remote->addr);
} }
...@@ -822,10 +822,14 @@ priv_add_new_candidate_discovery_turn (NiceAgent *agent, ...@@ -822,10 +822,14 @@ priv_add_new_candidate_discovery_turn (NiceAgent *agent,
} }
cdisco->nicesock = socket; cdisco->nicesock = socket;
} else { } else {
cdisco->nicesock = nice_tcp_turn_socket_new (agent, socket = nice_tcp_bsd_socket_new (agent, component->ctx, &turn->server);
component->ctx, if (turn->type == NICE_RELAY_TYPE_TURN_TLS &&
&turn->server, agent->compatibility == NICE_COMPATIBILITY_GOOGLE) {
priv_agent_to_turn_compatibility (agent)); 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) { if (!cdisco->nicesock) {
agent->discovery_list = g_slist_remove (modified_list, cdisco); agent->discovery_list = g_slist_remove (modified_list, cdisco);
g_slice_free (CandidateDiscovery, cdisco); g_slice_free (CandidateDiscovery, cdisco);
...@@ -1367,7 +1371,7 @@ _nice_agent_recv ( ...@@ -1367,7 +1371,7 @@ _nice_agent_recv (
if (cand->type == NICE_CANDIDATE_TYPE_RELAYED && if (cand->type == NICE_CANDIDATE_TYPE_RELAYED &&
cand->stream_id == stream->id && cand->stream_id == stream->id &&
cand->component_id == component->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); &from, len, buf, &from, buf, len);
} }
} }
......
...@@ -530,7 +530,7 @@ discovery_add_relay_candidate ( ...@@ -530,7 +530,7 @@ discovery_add_relay_candidate (
candidate->turn = turn; candidate->turn = turn;
/* step: link to the base candidate+socket */ /* 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, base_socket, &turn->server,
turn->username, turn->password, turn->username, turn->password,
agent_to_turn_socket_compatibility (agent)); agent_to_turn_socket_compatibility (agent));
......
...@@ -24,8 +24,12 @@ libsocket_la_SOURCES = \ ...@@ -24,8 +24,12 @@ libsocket_la_SOURCES = \
socket.c \ socket.c \
udp-bsd.h \ udp-bsd.h \
udp-bsd.c \ udp-bsd.c \
udp-turn.h \ tcp-bsd.h \
udp-turn.c \ tcp-bsd.c \
pseudossl.h \
pseudossl.c \
turn.h \
turn.c \
tcp-turn.h \ tcp-turn.h \
tcp-turn.c tcp-turn.c
...@@ -36,5 +40,5 @@ test_bsd_LDADD = $(COMMON_LDADD) ...@@ -36,5 +40,5 @@ test_bsd_LDADD = $(COMMON_LDADD)
TESTS = $(check_PROGRAMS) 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 @@ ...@@ -45,21 +45,14 @@
gint gint
nice_socket_recv ( nice_socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf)
NiceSocket *sock,
NiceAddress *from,
guint len,
gchar *buf)
{ {
return sock->recv (sock, from, len, buf); return sock->recv (sock, from, len, buf);
} }
gboolean gboolean
nice_socket_send ( nice_socket_send (NiceSocket *sock, const NiceAddress *to,
NiceSocket *sock, guint len, const gchar *buf)
const NiceAddress *to,
guint len,
const gchar *buf)
{ {
return sock->send (sock, to, len, buf); return sock->send (sock, to, len, buf);
} }
......
...@@ -69,18 +69,11 @@ struct _NiceSocket ...@@ -69,18 +69,11 @@ struct _NiceSocket
G_GNUC_WARN_UNUSED_RESULT G_GNUC_WARN_UNUSED_RESULT
gint gint
nice_socket_recv ( nice_socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf);
NiceSocket *sock,
NiceAddress *from,
guint len,
gchar *buf);
gboolean gboolean
nice_socket_send ( nice_socket_send (NiceSocket *sock, const NiceAddress *to,
NiceSocket *sock, guint len, const gchar *buf);
const NiceAddress *to,
guint len,
const gchar *buf);
gboolean gboolean
nice_socket_is_reliable (NiceSocket *sock); nice_socket_is_reliable (NiceSocket *sock);
...@@ -90,7 +83,9 @@ void ...@@ -90,7 +83,9 @@ void
nice_socket_free (NiceSocket *sock); nice_socket_free (NiceSocket *sock);
#include "udp-bsd.h" #include "udp-bsd.h"
#include "udp-turn.h" #include "tcp-bsd.h"
#include "pseudossl.h"
#include "turn.h"
#include "tcp-turn.h" #include "tcp-turn.h"
G_END_DECLS 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;
if (len <= 0)
return;
tbs = g_slice_new (struct to_be_sent);
tbs->buf = g_memdup (buf, len);
tbs->length = len;
if (head)
g_queue_push_head (&priv->send_queue, tbs);
else
g_queue_push_tail (&priv->send_queue, tbs);
if (priv->io_channel == NULL) {
priv->io_channel = g_io_channel_unix_new (sock->fileno);
priv->io_source = g_io_create_watch (priv->io_channel, G_IO_OUT);
g_source_set_callback (priv->io_source, (GSourceFunc) socket_send_more,
sock, NULL);
g_source_attach (priv->io_source, priv->context);
}
}
static void
free_to_be_sent (struct to_be_sent *tbs)
{
g_free (tbs->buf);
g_slice_free (struct to_be_sent, 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 _TCP_BSD_H
#define _TCP_BSD_H
#include "socket.h"
#include "agent.h"
G_BEGIN_DECLS
NiceSocket *
nice_tcp_bsd_socket_new (NiceAgent *agent, GMainContext *ctx, NiceAddress *addr);
G_END_DECLS
#endif /* _TCP_BSD_H */
...@@ -58,30 +58,57 @@ ...@@ -58,30 +58,57 @@
#endif #endif
typedef struct { typedef struct {
NiceUdpTurnSocketCompatibility compatibility; NiceTurnSocketCompatibility compatibility;
GQueue send_queue;
gchar recv_buf[65536]; gchar recv_buf[65536];
guint recv_buf_len; guint recv_buf_len;
guint expecting_len; guint expecting_len;
NiceAddress server_addr; NiceSocket *base_socket;
GMainContext *context;
GIOChannel *io_channel;
GSource *io_source;
} TurnTcpPriv; } TurnTcpPriv;
struct to_be_sent {
guint length;
gchar *buf;
};
/*** NiceSocket ***/ 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);
NiceSocket *
nice_tcp_turn_socket_new (NiceAgent *agent, NiceSocket *base_socket,
NiceTurnSocketCompatibility compatibility)
{
TurnTcpPriv *priv;
NiceSocket *sock = g_slice_new0 (NiceSocket);
sock->priv = priv = g_slice_new0 (TurnTcpPriv);
priv->compatibility = compatibility;
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;
return sock;
}
static void
socket_close (NiceSocket *sock)
{
TurnTcpPriv *priv = sock->priv;
if (priv->base_socket)
nice_socket_free (priv->base_socket);
g_slice_free(TurnTcpPriv, sock->priv);
}
static gint static gint
socket_recv ( socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf)
NiceSocket *sock,
NiceAddress *from,
guint len,
gchar *buf)
{ {
TurnTcpPriv *priv = sock->priv; TurnTcpPriv *priv = sock->priv;
int ret; int ret;
...@@ -90,32 +117,24 @@ socket_recv ( ...@@ -90,32 +117,24 @@ socket_recv (
if (priv->expecting_len == 0) { if (priv->expecting_len == 0) {
guint headerlen = 0; guint headerlen = 0;
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9)
headerlen = 4; headerlen = 4;
else if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE) else if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE)
headerlen = 2; headerlen = 2;
else else
g_assert_not_reached(); return -1;
ret = recv (sock->fileno, priv->recv_buf + priv->recv_buf_len, ret = nice_socket_recv (priv->base_socket, from,
headerlen - priv->recv_buf_len, 0); headerlen - priv->recv_buf_len, priv->recv_buf + priv->recv_buf_len);
if (ret < 0) { if (ret < 0)
#ifdef G_OS_WIN32
if (WSAGetLastError () == WSAEWOULDBLOCK)
#else
if (errno == EAGAIN)
#endif
return 0;
else
return ret; return ret;
}
priv->recv_buf_len += ret; priv->recv_buf_len += ret;
if (priv->recv_buf_len < headerlen) if (priv->recv_buf_len < headerlen)
return 0; return 0;
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) { if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
guint16 magic = ntohs (*(guint16*)priv->recv_buf); guint16 magic = ntohs (*(guint16*)priv->recv_buf);
guint16 packetlen = ntohs (*(guint16*)(priv->recv_buf + 2)); guint16 packetlen = ntohs (*(guint16*)(priv->recv_buf + 2));
...@@ -127,30 +146,24 @@ socket_recv ( ...@@ -127,30 +146,24 @@ socket_recv (
priv->expecting_len = 4 + packetlen; priv->expecting_len = 4 + packetlen;
} }
} }
else if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE) { else if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
guint len = ntohs (*(guint16*)priv->recv_buf); guint len = ntohs (*(guint16*)priv->recv_buf);
priv->expecting_len = len; priv->expecting_len = len;
priv->recv_buf_len = 0; priv->recv_buf_len = 0;
} }
} }
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9)
padlen = (priv->expecting_len % 4) ? 4 - (priv->expecting_len % 4) : 0; padlen = (priv->expecting_len % 4) ? 4 - (priv->expecting_len % 4) : 0;
else else
padlen = 0; padlen = 0;
ret = recv (sock->fileno, priv->recv_buf + priv->recv_buf_len, ret = nice_socket_recv (priv->base_socket, from,
priv->expecting_len + padlen - priv->recv_buf_len, 0); priv->expecting_len + padlen - priv->recv_buf_len,
if (ret < 0) { priv->recv_buf + priv->recv_buf_len);
#ifdef G_OS_WIN32
if (WSAGetLastError () == WSAEWOULDBLOCK) if (ret < 0)
#else
if (errno == EAGAIN)
#endif
return 0;
else
return ret; return ret;
}
priv->recv_buf_len += ret; priv->recv_buf_len += ret;
...@@ -159,223 +172,45 @@ socket_recv ( ...@@ -159,223 +172,45 @@ socket_recv (
memcpy (buf, priv->recv_buf, copy_len); memcpy (buf, priv->recv_buf, copy_len);
priv->expecting_len = 0; priv->expecting_len = 0;
priv->recv_buf_len = 0; priv->recv_buf_len = 0;
if (from)
*from = priv->server_addr;
return copy_len; return copy_len;
} }
return 0; return 0;
} }
static void
add_to_be_sent (NiceSocket *sock, const gchar *buf, guint len, gboolean head);
/*
* Returns:
* -1 = error
* 0 = have more to send
* 1 = sent everything
*/
static gboolean
socket_send_more (
GIOChannel *source,
G_GNUC_UNUSED
GIOCondition condition,
gpointer data)
{
NiceSocket *sock = (NiceSocket *) data;
TurnTcpPriv *priv = sock->priv;
struct to_be_sent *tbs = NULL;
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;
return FALSE;
}
return TRUE;
}
static void
add_to_be_sent (NiceSocket *sock, const gchar *buf, guint len, gboolean head)
{
TurnTcpPriv *priv = sock->priv;
struct to_be_sent *tbs = g_slice_new (struct to_be_sent);
if (len <= 0)
return;
tbs->buf = g_memdup (buf, len);
tbs->length = len;
if (head)
g_queue_push_head (&priv->send_queue, tbs);
else
g_queue_push_tail (&priv->send_queue, tbs);
if (priv->io_channel == NULL) {
priv->io_channel = g_io_channel_unix_new (sock->fileno);
priv->io_source = g_io_create_watch (priv->io_channel, G_IO_OUT);
g_source_set_callback (priv->io_source, (GSourceFunc) socket_send_more,
sock, NULL);
g_source_attach (priv->io_source, priv->context);
}
}
static gboolean static gboolean
socket_send ( socket_send (NiceSocket *sock, const NiceAddress *to,
NiceSocket *sock, guint len, const gchar *buf)
const NiceAddress *to,
guint len,
const gchar *buf)
{ {
int ret; gboolean ret = TRUE;
TurnTcpPriv *priv = sock->priv; TurnTcpPriv *priv = sock->priv;
gchar padbuf[3] = {0, 0, 0}; gchar padbuf[3] = {0, 0, 0};
int padlen = (len%4) ? 4 - (len%4) : 0; int padlen = (len%4) ? 4 - (len%4) : 0;
if (priv->compatibility != NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) if (priv->compatibility != NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9)
padlen = 0; padlen = 0;
/* First try to send the data, don't send it later if it can be sent now if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
this way we avoid allocating memory on every send */
if (g_queue_is_empty (&priv->send_queue)) {
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
guint16 tmpbuf = htons (len); guint16 tmpbuf = htons (len);
ret = send (sock->fileno, (void *) &tmpbuf, sizeof(guint16), 0); ret = nice_socket_send (priv->base_socket, to,
sizeof(guint16), (gchar *)&tmpbuf);
if (ret < 0) {
#ifdef G_OS_WIN32
if (WSAGetLastError () == WSAEWOULDBLOCK) {
#else
if (errno == EAGAIN) {
#endif
add_to_be_sent (sock, (gchar *) &tmpbuf, sizeof(guint16), FALSE);
add_to_be_sent (sock, buf, len, FALSE);
return TRUE;
} else {
return FALSE;
}
} else if ((guint)ret < sizeof(guint16)) {
add_to_be_sent (sock, ((gchar *) &tmpbuf) + ret,
sizeof(guint16) - ret, FALSE);
add_to_be_sent (sock, buf, len, FALSE);
return TRUE;
}
}
ret = send (sock->fileno, buf, len, 0);
if (ret < 0) { if (!ret)
#ifdef G_OS_WIN32 return ret;
if (WSAGetLastError () == WSAEWOULDBLOCK) {
#else
if (errno == EAGAIN) {
#endif
add_to_be_sent (sock, buf, len, FALSE);
add_to_be_sent (sock, padbuf, padlen, FALSE);
return TRUE;
} else {
return FALSE;
}
} else if ((guint)ret < len) {
add_to_be_sent (sock, buf + ret, len - ret, FALSE);
add_to_be_sent (sock, padbuf, padlen, FALSE);
return TRUE;
} }
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9 && ret = nice_socket_send (priv->base_socket, to, len, buf);
len % 4) {
ret = send (sock->fileno, padbuf, padlen, 0); if (!ret)
return ret;
if (ret < 0) {
#ifdef G_OS_WIN32
if (WSAGetLastError () == WSAEWOULDBLOCK) {
#else
if (errno == EAGAIN) {
#endif
add_to_be_sent (sock, padbuf, padlen, FALSE);
return TRUE;
} else {
return FALSE;
}
} else if (ret < padlen) {
add_to_be_sent (sock, padbuf, padlen - ret, FALSE);
return TRUE;
}
}
} else {
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
guint16 tmpbuf = htons (len);
add_to_be_sent (sock, (gchar*) &tmpbuf, sizeof(guint16), FALSE);
}
add_to_be_sent (sock, buf, len, FALSE);
add_to_be_sent (sock, padbuf, padlen, FALSE);
}
return TRUE; if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9)
} ret = nice_socket_send (priv->base_socket, to, padlen, padbuf);
static void return ret;
free_to_be_sent (struct to_be_sent *tbs)
{
g_free (tbs->buf);
g_slice_free (struct to_be_sent, tbs);
} }
static void
socket_close (NiceSocket *sock)
{
TurnTcpPriv *priv = sock->priv;
#ifdef G_OS_WIN32
closesocket(sock->fileno);
#else
close (sock->fileno);
#endif
g_queue_foreach (&priv->send_queue, (GFunc) free_to_be_sent, NULL);
g_queue_clear (&priv->send_queue);
if (priv->io_channel)
g_io_channel_unref (priv->io_channel);
if (priv->io_source) {
g_source_destroy (priv->io_source);
g_source_unref (priv->io_source);
}
g_slice_free(TurnTcpPriv, sock->priv);
}
static gboolean static gboolean
socket_is_reliable (NiceSocket *sock) socket_is_reliable (NiceSocket *sock)
...@@ -383,90 +218,3 @@ socket_is_reliable (NiceSocket *sock) ...@@ -383,90 +218,3 @@ socket_is_reliable (NiceSocket *sock)
return TRUE; return TRUE;
} }
NiceSocket *
nice_tcp_turn_socket_new (
NiceAgent *agent,
GMainContext *ctx,
NiceAddress *addr,
NiceUdpTurnSocketCompatibility compatibility)
{
int sockfd = -1;
int ret;
struct sockaddr_storage name;
guint name_len = sizeof (name);
NiceSocket *sock = g_slice_new0 (NiceSocket);
TurnTcpPriv *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 (TurnTcpPriv);
priv->compatibility = compatibility;
priv->server_addr = *addr;
priv->context = ctx;
sock->fileno = sockfd;
sock->send = socket_send;
sock->recv = socket_recv;
sock->is_reliable = socket_is_reliable;
sock->close = socket_close;
return sock;
}
...@@ -45,11 +45,8 @@ G_BEGIN_DECLS ...@@ -45,11 +45,8 @@ G_BEGIN_DECLS
NiceSocket * NiceSocket *
nice_tcp_turn_socket_new ( nice_tcp_turn_socket_new (NiceAgent *agent, NiceSocket *base_socket,
NiceAgent *agent, NiceTurnSocketCompatibility compatibility);
GMainContext *ctx,
NiceAddress *addr,
NiceUdpTurnSocketCompatibility compatibility);
G_END_DECLS G_END_DECLS
......
...@@ -38,8 +38,7 @@ ...@@ -38,8 +38,7 @@
*/ */
/* /*
* Implementation of UDP socket interface using Berkeley sockets. (See * Implementation of TURN
* http://en.wikipedia.org/wiki/Berkeley_sockets.)
*/ */
#ifdef HAVE_CONFIG_H #ifdef HAVE_CONFIG_H
# include "config.h" # include "config.h"
...@@ -49,8 +48,7 @@ ...@@ -49,8 +48,7 @@
#include <errno.h> #include <errno.h>
#include <fcntl.h> #include <fcntl.h>
#include "udp-turn.h" #include "turn.h"
#include "udp-bsd.h"
#include "stun/stunagent.h" #include "stun/stunagent.h"
#include "stun/usages/timer.h" #include "stun/usages/timer.h"
#include "agent-priv.h" #include "agent-priv.h"
...@@ -81,296 +79,232 @@ typedef struct { ...@@ -81,296 +79,232 @@ typedef struct {
size_t username_len; size_t username_len;
uint8_t *password; uint8_t *password;
size_t password_len; size_t password_len;
NiceUdpTurnSocketCompatibility compatibility; NiceTurnSocketCompatibility compatibility;
} turn_priv; } TurnPriv;
static void
priv_schedule_tick (turn_priv *priv);
static gboolean
priv_add_channel_binding (turn_priv *priv, NiceAddress *peer);
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 static void priv_process_pending_bindings (TurnPriv *priv);
priv_process_pending_bindings (turn_priv *priv) static gboolean priv_retransmissions_tick_unlocked (TurnPriv *priv);
static gboolean priv_retransmissions_tick (gpointer pointer);
static void priv_schedule_tick (TurnPriv *priv);
static void priv_send_turn_message (TurnPriv *priv, TURNMessage *msg);
static gboolean priv_send_channel_bind (TurnPriv *priv, StunMessage *resp,
uint16_t channel, NiceAddress *peer);
static gboolean priv_add_channel_binding (TurnPriv *priv, NiceAddress *peer);
NiceSocket *
nice_turn_socket_new (NiceAgent *agent, NiceAddress *addr,
NiceSocket *base_socket, NiceAddress *server_addr,
gchar *username, gchar *password, NiceTurnSocketCompatibility compatibility)
{ {
gboolean ret = FALSE; TurnPriv *priv = g_new0 (TurnPriv, 1);
while (priv->pending_bindings != NULL && ret == FALSE) { NiceSocket *sock = g_slice_new0 (NiceSocket);
NiceAddress *peer = priv->pending_bindings->data;
ret = priv_add_channel_binding (priv, peer); if (!sock) {
priv->pending_bindings = g_list_remove (priv->pending_bindings, peer); return NULL;
nice_address_free (peer);
} }
}
static gboolean if (compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
priv_retransmissions_tick_unlocked (turn_priv *priv) stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES,
{ STUN_COMPATIBILITY_RFC5389,
if (priv->current_binding_msg) { STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS);
guint timeout = stun_timer_refresh (&priv->current_binding_msg->timer); } else if (compatibility == NICE_TURN_SOCKET_COMPATIBILITY_MSN) {
switch (timeout) { stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES,
case -1: STUN_COMPATIBILITY_RFC3489,
/* Time out */ STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
g_free (priv->current_binding); STUN_AGENT_USAGE_NO_INDICATION_AUTH);
} else if (compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES,
STUN_COMPATIBILITY_RFC3489,
STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
STUN_AGENT_USAGE_IGNORE_CREDENTIALS);
}
priv->nice = agent;
priv->channels = NULL;
priv->current_binding = NULL; priv->current_binding = NULL;
g_free (priv->current_binding_msg); priv->base_socket = base_socket;
priv->current_binding_msg = NULL;
priv_process_pending_bindings (priv); if (compatibility == NICE_TURN_SOCKET_COMPATIBILITY_MSN) {
break; priv->username = g_base64_decode (username, &priv->username_len);
case 0: priv->password = g_base64_decode (password, &priv->password_len);
/* Retransmit */ } else {
nice_socket_send (priv->base_socket, &priv->server_addr, priv->username = (uint8_t *)g_strdup (username);
stun_message_length (&priv->current_binding_msg->message), priv->username_len = (size_t) strlen (username);
(gchar *)priv->current_binding_msg->buffer); if (compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
break; priv->password = NULL;
default: priv->password_len = 0;
break; } else {
priv->password = (uint8_t *)g_strdup (password);
priv->password_len = (size_t) strlen (password);
} }
} }
priv->server_addr = *server_addr;
priv_schedule_tick (priv); priv->compatibility = compatibility;
return FALSE; sock->addr = *addr;
sock->fileno = base_socket->fileno;
sock->send = socket_send;
sock->recv = socket_recv;
sock->is_reliable = socket_is_reliable;
sock->close = socket_close;
sock->priv = (void *) priv;
return sock;
} }
static gboolean static void
priv_retransmissions_tick (gpointer pointer) socket_close (NiceSocket *sock)
{ {
turn_priv *priv = pointer; TurnPriv *priv = (TurnPriv *) sock->priv;
gboolean ret; GList *i = NULL;
for (i = priv->channels; i; i = i->next) {
g_static_rec_mutex_lock (&priv->nice->mutex); ChannelBinding *b = i->data;
ret = priv_retransmissions_tick_unlocked (priv); g_free (b);
g_static_rec_mutex_unlock (&priv->nice->mutex); }
g_list_free (priv->channels);
return ret; for (i = priv->pending_bindings; i; i = i->next) {
} ChannelBinding *b = i->data;
g_free (b);
}
g_list_free (priv->pending_bindings);
static void
priv_schedule_tick (turn_priv *priv)
{
if (priv->tick_source != NULL) { if (priv->tick_source != NULL) {
g_source_destroy (priv->tick_source); g_source_destroy (priv->tick_source);
g_source_unref (priv->tick_source); g_source_unref (priv->tick_source);
priv->tick_source = NULL; priv->tick_source = NULL;
} }
if (priv->current_binding_msg) { g_free (priv->current_binding);
guint timeout = stun_timer_remainder (&priv->current_binding_msg->timer); g_free (priv->current_binding_msg);
if (timeout > 0) { g_free (priv->username);
priv->tick_source = agent_timeout_add_with_context (priv->nice, timeout, g_free (priv->password);
priv_retransmissions_tick, priv); g_free (priv);
} else {
priv_retransmissions_tick_unlocked (priv);
}
}
} }
static void static gint
priv_send_turn_message (turn_priv *priv, TURNMessage *msg) socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf)
{ {
size_t stun_len = stun_message_length (&msg->message); TurnPriv *priv = (TurnPriv *) sock->priv;
uint8_t recv_buf[STUN_MAX_MESSAGE_SIZE];
if (priv->current_binding_msg) { gint recv_len;
g_free (priv->current_binding_msg); NiceAddress recv_from;
priv->current_binding_msg = NULL; NiceSocket *dummy;;
}
nice_socket_send (priv->base_socket, &priv->server_addr,
stun_len, (gchar *)msg->buffer);
if (nice_socket_is_reliable (priv->base_socket)) { recv_len = nice_socket_recv (priv->base_socket, &recv_from,
stun_timer_start_reliable (&msg->timer); sizeof(recv_buf), (gchar *) recv_buf);
} else {
stun_timer_start (&msg->timer);
}
priv->current_binding_msg = msg; return nice_turn_socket_parse_recv (sock, &dummy, from, len, buf,
priv_schedule_tick (priv); &recv_from, (gchar *) recv_buf, (guint) recv_len);
} }
static gboolean static gboolean
priv_send_channel_bind (turn_priv *priv, StunMessage *resp, socket_send (NiceSocket *sock, const NiceAddress *to,
uint16_t channel, NiceAddress *peer) { guint len, const gchar *buf)
uint32_t channel_attr = channel << 16; {
size_t stun_len; TurnPriv *priv = (TurnPriv *) sock->priv;
StunMessage msg;
uint8_t buffer[STUN_MAX_MESSAGE_SIZE];
size_t msg_len;
struct sockaddr_storage sa; struct sockaddr_storage sa;
TURNMessage *msg = g_new0 (TURNMessage, 1); GList *i = priv->channels;
ChannelBinding *binding = NULL;
nice_address_copy_to_sockaddr (peer, (struct sockaddr *)&sa);
if (!stun_agent_init_request (&priv->agent, &msg->message, for (; i; i = i->next) {
msg->buffer, sizeof(msg->buffer), STUN_CHANNELBIND)) { ChannelBinding *b = i->data;
g_free (msg); if (nice_address_equal (&b->peer, to)) {
return FALSE; binding = b;
break;
} }
if (stun_message_append32 (&msg->message, STUN_ATTRIBUTE_CHANNEL_NUMBER,
channel_attr) != 0) {
g_free (msg);
return FALSE;
} }
if (stun_message_append_xor_addr (&msg->message, STUN_ATTRIBUTE_PEER_ADDRESS, nice_address_copy_to_sockaddr (to, (struct sockaddr *)&sa);
(struct sockaddr *)&sa, sizeof(sa)) != 0) {
g_free (msg); if (binding) {
return FALSE; if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9 &&
len + sizeof(uint32_t) <= sizeof(buffer)) {
uint16_t len16 = htons ((uint16_t) len);
uint16_t channel16 = htons (binding->channel);
memcpy (buffer, &channel16, sizeof(uint16_t));
memcpy (buffer + sizeof(uint16_t), &len16,sizeof(uint16_t));
memcpy (buffer + sizeof(uint32_t), buf, len);
msg_len = len + sizeof(uint32_t);
} else {
return nice_socket_send (priv->base_socket, &priv->server_addr, len, buf);
} }
} else {
if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
if (!stun_agent_init_indication (&priv->agent, &msg,
buffer, sizeof(buffer), STUN_IND_SEND))
goto send;
if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_PEER_ADDRESS,
(struct sockaddr *)&sa, sizeof(sa)) != 0)
goto send;
} else {
if (!stun_agent_init_request (&priv->agent, &msg,
buffer, sizeof(buffer), STUN_SEND))
goto send;
if (stun_message_append32 (&msg, STUN_ATTRIBUTE_MAGIC_COOKIE,
TURN_MAGIC_COOKIE) != 0)
goto send;
if (priv->username != NULL && priv->username_len > 0) { if (priv->username != NULL && priv->username_len > 0) {
if (stun_message_append_bytes (&msg->message, STUN_ATTRIBUTE_USERNAME, if (stun_message_append_bytes (&msg, STUN_ATTRIBUTE_USERNAME,
priv->username, priv->username_len) != 0) { priv->username, priv->username_len) != 0)
g_free (msg); goto send;
return FALSE;
}
} }
if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_DESTINATION_ADDRESS,
(struct sockaddr *)&sa, sizeof(sa)) != 0)
goto send;
if (resp) { if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE &&
uint8_t *realm; priv->current_binding &&
uint8_t *nonce; nice_address_equal (&priv->current_binding->peer, to)) {
uint16_t len; stun_message_append32 (&msg, STUN_ATTRIBUTE_OPTIONS, 1);
realm = (uint8_t *) stun_message_find (resp, STUN_ATTRIBUTE_REALM, &len);
if (realm != NULL) {
if (stun_message_append_bytes (&msg->message, STUN_ATTRIBUTE_REALM,
realm, len) != 0) {
g_free (msg);
return 0;
}
}
nonce = (uint8_t *) stun_message_find (resp, STUN_ATTRIBUTE_NONCE, &len);
if (nonce != NULL) {
if (stun_message_append_bytes (&msg->message, STUN_ATTRIBUTE_NONCE,
nonce, len) != 0) {
g_free (msg);
return 0;
}
} }
} }
stun_len = stun_agent_finish_message (&priv->agent, &msg->message, if (stun_message_append_bytes (&msg, STUN_ATTRIBUTE_DATA, buf, len) != 0)
priv->password, priv->password_len); goto send;
if (stun_len > 0) { msg_len = stun_agent_finish_message (&priv->agent, &msg,
priv_send_turn_message (priv, msg); priv->password, priv->password_len);
return TRUE;
} }
g_free (msg); if (msg_len > 0) {
return FALSE; return nice_socket_send (priv->base_socket, &priv->server_addr,
msg_len, (gchar *)buffer);
}
send:
return nice_socket_send (priv->base_socket, to, len, buf);
} }
static gboolean static gboolean
priv_add_channel_binding (turn_priv *priv, NiceAddress *peer) socket_is_reliable (NiceSocket *sock)
{ {
size_t stun_len; TurnPriv *priv = (TurnPriv *) sock->priv;
struct sockaddr_storage sa; return nice_socket_is_reliable (priv->base_socket);
}
nice_address_copy_to_sockaddr (peer, (struct sockaddr *)&sa);
if (priv->current_binding) {
NiceAddress * pending= nice_address_new ();
*pending = *peer;
priv->pending_bindings = g_list_append (priv->pending_bindings, pending);
return FALSE;
}
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
uint16_t channel = 0x4000;
GList *i = priv->channels;
for (; i; i = i->next) {
ChannelBinding *b = i->data;
if (channel == b->channel) {
i = priv->channels;
channel++;
continue;
}
}
if (channel >= 0x4000 && channel < 0xffff) {
gboolean ret = priv_send_channel_bind (priv, NULL, channel, peer);
if (ret) {
priv->current_binding = g_new0 (ChannelBinding, 1);
priv->current_binding->channel = channel;
priv->current_binding->peer = *peer;
}
return ret;
}
return FALSE;
} else if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_MSN) {
TURNMessage *msg = g_new0 (TURNMessage, 1);
if (!stun_agent_init_request (&priv->agent, &msg->message,
msg->buffer, sizeof(msg->buffer), STUN_OLD_SET_ACTIVE_DST)) {
g_free (msg);
return FALSE;
}
if (stun_message_append32 (&msg->message, STUN_ATTRIBUTE_MAGIC_COOKIE,
TURN_MAGIC_COOKIE) != 0) {
g_free (msg);
return FALSE;
}
if (priv->username != NULL && priv->username_len > 0) {
if (stun_message_append_bytes (&msg->message, STUN_ATTRIBUTE_USERNAME,
priv->username, priv->username_len) != 0) {
g_free (msg);
return FALSE;
}
}
if (stun_message_append_addr (&msg->message,
STUN_ATTRIBUTE_DESTINATION_ADDRESS,
(struct sockaddr *)&sa, sizeof(sa)) != 0) {
g_free (msg);
return FALSE;
}
stun_len = stun_agent_finish_message (&priv->agent, &msg->message,
priv->password, priv->password_len);
if (stun_len > 0) {
priv->current_binding = g_new0 (ChannelBinding, 1);
priv->current_binding->channel = 0;
priv->current_binding->peer = *peer;
priv_send_turn_message (priv, msg);
return TRUE;
}
g_free (msg);
return FALSE;
} else if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
priv->current_binding = g_new0 (ChannelBinding, 1);
priv->current_binding->channel = 0;
priv->current_binding->peer = *peer;
return TRUE;
} else {
return FALSE;
}
return FALSE;
}
gboolean
nice_udp_turn_socket_set_peer (NiceSocket *sock, NiceAddress *peer)
{
turn_priv *priv = (turn_priv *) sock->priv;
return priv_add_channel_binding (priv, peer);
}
gint gint
nice_udp_turn_socket_parse_recv ( nice_turn_socket_parse_recv (NiceSocket *sock, NiceSocket **from_sock,
NiceSocket *sock, NiceAddress *from, guint len, gchar *buf,
NiceSocket **from_sock, NiceAddress *recv_from, gchar *recv_buf, guint recv_len)
NiceAddress *from,
guint len,
gchar *buf,
NiceAddress *recv_from,
gchar *recv_buf,
guint recv_len)
{ {
turn_priv *priv = (turn_priv *) sock->priv; TurnPriv *priv = (TurnPriv *) sock->priv;
StunValidationStatus valid; StunValidationStatus valid;
StunMessage msg; StunMessage msg;
struct sockaddr_storage sa; struct sockaddr_storage sa;
...@@ -383,7 +317,7 @@ nice_udp_turn_socket_parse_recv ( ...@@ -383,7 +317,7 @@ nice_udp_turn_socket_parse_recv (
(uint8_t *) recv_buf, (size_t) recv_len, NULL, NULL); (uint8_t *) recv_buf, (size_t) recv_len, NULL, NULL);
if (valid == STUN_VALIDATION_SUCCESS) { if (valid == STUN_VALIDATION_SUCCESS) {
if (priv->compatibility != NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) { if (priv->compatibility != NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
uint32_t cookie; uint32_t cookie;
if (stun_message_find32 (&msg, STUN_ATTRIBUTE_MAGIC_COOKIE, if (stun_message_find32 (&msg, STUN_ATTRIBUTE_MAGIC_COOKIE,
&cookie) != 0) &cookie) != 0)
...@@ -394,7 +328,7 @@ nice_udp_turn_socket_parse_recv ( ...@@ -394,7 +328,7 @@ nice_udp_turn_socket_parse_recv (
if (stun_message_get_method (&msg) == STUN_SEND) { if (stun_message_get_method (&msg) == STUN_SEND) {
if (stun_message_get_class (&msg) == STUN_RESPONSE && if (stun_message_get_class (&msg) == STUN_RESPONSE &&
priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE) { priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
uint32_t opts = 0; uint32_t opts = 0;
if (stun_message_find32 (&msg, STUN_ATTRIBUTE_OPTIONS, &opts) == 0 && if (stun_message_find32 (&msg, STUN_ATTRIBUTE_OPTIONS, &opts) == 0 &&
opts & 0x1) opts & 0x1)
...@@ -412,7 +346,7 @@ nice_udp_turn_socket_parse_recv ( ...@@ -412,7 +346,7 @@ nice_udp_turn_socket_parse_recv (
priv->current_binding_msg = NULL; priv->current_binding_msg = NULL;
if (stun_message_get_class (&msg) == STUN_RESPONSE && if (stun_message_get_class (&msg) == STUN_RESPONSE &&
priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_MSN) { priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_MSN) {
goto msn_google_lock; goto msn_google_lock;
} else { } else {
g_free (priv->current_binding); g_free (priv->current_binding);
...@@ -482,7 +416,7 @@ nice_udp_turn_socket_parse_recv ( ...@@ -482,7 +416,7 @@ nice_udp_turn_socket_parse_recv (
uint16_t data_len; uint16_t data_len;
uint8_t *data; uint8_t *data;
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) { if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
if (stun_message_find_xor_addr (&msg, STUN_ATTRIBUTE_REMOTE_ADDRESS, if (stun_message_find_xor_addr (&msg, STUN_ATTRIBUTE_REMOTE_ADDRESS,
(struct sockaddr *)&sa, &from_len) != 0) (struct sockaddr *)&sa, &from_len) != 0)
goto recv; goto recv;
...@@ -512,7 +446,7 @@ nice_udp_turn_socket_parse_recv ( ...@@ -512,7 +446,7 @@ nice_udp_turn_socket_parse_recv (
recv: recv:
for (i = priv->channels; i; i = i->next) { for (i = priv->channels; i; i = i->next) {
ChannelBinding *b = i->data; ChannelBinding *b = i->data;
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) { if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
if (b->channel == ntohs(((uint16_t *)recv_buf)[0])) { if (b->channel == ntohs(((uint16_t *)recv_buf)[0])) {
recv_len = ntohs (((uint16_t *)recv_buf)[1]); recv_len = ntohs (((uint16_t *)recv_buf)[1]);
recv_buf += sizeof(uint32_t); recv_buf += sizeof(uint32_t);
...@@ -552,208 +486,269 @@ nice_udp_turn_socket_parse_recv ( ...@@ -552,208 +486,269 @@ nice_udp_turn_socket_parse_recv (
return 0; return 0;
} }
static gint gboolean
socket_recv ( nice_turn_socket_set_peer (NiceSocket *sock, NiceAddress *peer)
NiceSocket *sock,
NiceAddress *from,
guint len,
gchar *buf)
{ {
turn_priv *priv = (turn_priv *) sock->priv; TurnPriv *priv = (TurnPriv *) sock->priv;
uint8_t recv_buf[STUN_MAX_MESSAGE_SIZE]; return priv_add_channel_binding (priv, peer);
gint recv_len; }
NiceAddress recv_from;
NiceSocket *dummy;;
recv_len = nice_socket_recv (priv->base_socket, &recv_from,
sizeof(recv_buf), (gchar *) recv_buf);
return nice_udp_turn_socket_parse_recv (sock, &dummy, from, len, buf, static void
&recv_from, (gchar *) recv_buf, (guint) recv_len); priv_process_pending_bindings (TurnPriv *priv)
{
gboolean ret = FALSE;
while (priv->pending_bindings != NULL && ret == FALSE) {
NiceAddress *peer = priv->pending_bindings->data;
ret = priv_add_channel_binding (priv, peer);
priv->pending_bindings = g_list_remove (priv->pending_bindings, peer);
nice_address_free (peer);
}
} }
static gboolean static gboolean
socket_send ( priv_retransmissions_tick_unlocked (TurnPriv *priv)
NiceSocket *sock,
const NiceAddress *to,
guint len,
const gchar *buf)
{ {
turn_priv *priv = (turn_priv *) sock->priv; if (priv->current_binding_msg) {
StunMessage msg; guint timeout = stun_timer_refresh (&priv->current_binding_msg->timer);
uint8_t buffer[STUN_MAX_MESSAGE_SIZE]; switch (timeout) {
size_t msg_len; case -1:
struct sockaddr_storage sa; /* Time out */
GList *i = priv->channels; g_free (priv->current_binding);
ChannelBinding *binding = NULL; priv->current_binding = NULL;
g_free (priv->current_binding_msg);
for (; i; i = i->next) { priv->current_binding_msg = NULL;
ChannelBinding *b = i->data; priv_process_pending_bindings (priv);
if (nice_address_equal (&b->peer, to)) { break;
binding = b; case 0:
/* Retransmit */
nice_socket_send (priv->base_socket, &priv->server_addr,
stun_message_length (&priv->current_binding_msg->message),
(gchar *)priv->current_binding_msg->buffer);
break;
default:
break; break;
} }
} }
nice_address_copy_to_sockaddr (to, (struct sockaddr *)&sa); priv_schedule_tick (priv);
return FALSE;
}
if (binding) {
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9 && static gboolean
len + sizeof(uint32_t) <= sizeof(buffer)) { priv_retransmissions_tick (gpointer pointer)
uint16_t len16 = htons ((uint16_t) len); {
uint16_t channel16 = htons (binding->channel); TurnPriv *priv = pointer;
memcpy (buffer, &channel16, sizeof(uint16_t)); gboolean ret;
memcpy (buffer + sizeof(uint16_t), &len16,sizeof(uint16_t));
memcpy (buffer + sizeof(uint32_t), buf, len); g_static_rec_mutex_lock (&priv->nice->mutex);
msg_len = len + sizeof(uint32_t); ret = priv_retransmissions_tick_unlocked (priv);
} else { g_static_rec_mutex_unlock (&priv->nice->mutex);
return nice_socket_send (priv->base_socket, &priv->server_addr, len, buf);
return ret;
}
static void
priv_schedule_tick (TurnPriv *priv)
{
if (priv->tick_source != NULL) {
g_source_destroy (priv->tick_source);
g_source_unref (priv->tick_source);
priv->tick_source = NULL;
} }
if (priv->current_binding_msg) {
guint timeout = stun_timer_remainder (&priv->current_binding_msg->timer);
if (timeout > 0) {
priv->tick_source = agent_timeout_add_with_context (priv->nice, timeout,
priv_retransmissions_tick, priv);
} else { } else {
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) { priv_retransmissions_tick_unlocked (priv);
if (!stun_agent_init_indication (&priv->agent, &msg, }
buffer, sizeof(buffer), STUN_IND_SEND)) }
goto send; }
if (stun_message_append_xor_addr (&msg, STUN_ATTRIBUTE_PEER_ADDRESS,
(struct sockaddr *)&sa, sizeof(sa)) != 0) static void
goto send; priv_send_turn_message (TurnPriv *priv, TURNMessage *msg)
{
size_t stun_len = stun_message_length (&msg->message);
if (priv->current_binding_msg) {
g_free (priv->current_binding_msg);
priv->current_binding_msg = NULL;
}
nice_socket_send (priv->base_socket, &priv->server_addr,
stun_len, (gchar *)msg->buffer);
if (nice_socket_is_reliable (priv->base_socket)) {
stun_timer_start_reliable (&msg->timer);
} else { } else {
if (!stun_agent_init_request (&priv->agent, &msg, stun_timer_start (&msg->timer);
buffer, sizeof(buffer), STUN_SEND)) }
goto send;
if (stun_message_append32 (&msg, STUN_ATTRIBUTE_MAGIC_COOKIE, priv->current_binding_msg = msg;
TURN_MAGIC_COOKIE) != 0) priv_schedule_tick (priv);
goto send; }
if (priv->username != NULL && priv->username_len > 0) {
if (stun_message_append_bytes (&msg, STUN_ATTRIBUTE_USERNAME, static gboolean
priv->username, priv->username_len) != 0) priv_send_channel_bind (TurnPriv *priv, StunMessage *resp,
goto send; uint16_t channel, NiceAddress *peer)
{
uint32_t channel_attr = channel << 16;
size_t stun_len;
struct sockaddr_storage sa;
TURNMessage *msg = g_new0 (TURNMessage, 1);
nice_address_copy_to_sockaddr (peer, (struct sockaddr *)&sa);
if (!stun_agent_init_request (&priv->agent, &msg->message,
msg->buffer, sizeof(msg->buffer), STUN_CHANNELBIND)) {
g_free (msg);
return FALSE;
} }
if (stun_message_append_addr (&msg, STUN_ATTRIBUTE_DESTINATION_ADDRESS,
(struct sockaddr *)&sa, sizeof(sa)) != 0)
goto send;
if (priv->compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE && if (stun_message_append32 (&msg->message, STUN_ATTRIBUTE_CHANNEL_NUMBER,
priv->current_binding && channel_attr) != 0) {
nice_address_equal (&priv->current_binding->peer, to)) { g_free (msg);
stun_message_append32 (&msg, STUN_ATTRIBUTE_OPTIONS, 1); return FALSE;
}
if (stun_message_append_xor_addr (&msg->message, STUN_ATTRIBUTE_PEER_ADDRESS,
(struct sockaddr *)&sa, sizeof(sa)) != 0) {
g_free (msg);
return FALSE;
}
if (priv->username != NULL && priv->username_len > 0) {
if (stun_message_append_bytes (&msg->message, STUN_ATTRIBUTE_USERNAME,
priv->username, priv->username_len) != 0) {
g_free (msg);
return FALSE;
} }
} }
if (stun_message_append_bytes (&msg, STUN_ATTRIBUTE_DATA, buf, len) != 0) if (resp) {
goto send; uint8_t *realm;
uint8_t *nonce;
uint16_t len;
msg_len = stun_agent_finish_message (&priv->agent, &msg, realm = (uint8_t *) stun_message_find (resp, STUN_ATTRIBUTE_REALM, &len);
priv->password, priv->password_len); if (realm != NULL) {
if (stun_message_append_bytes (&msg->message, STUN_ATTRIBUTE_REALM,
realm, len) != 0) {
g_free (msg);
return 0;
}
}
nonce = (uint8_t *) stun_message_find (resp, STUN_ATTRIBUTE_NONCE, &len);
if (nonce != NULL) {
if (stun_message_append_bytes (&msg->message, STUN_ATTRIBUTE_NONCE,
nonce, len) != 0) {
g_free (msg);
return 0;
}
}
} }
if (msg_len > 0) { stun_len = stun_agent_finish_message (&priv->agent, &msg->message,
return nice_socket_send (priv->base_socket, &priv->server_addr, priv->password, priv->password_len);
msg_len, (gchar *)buffer);
if (stun_len > 0) {
priv_send_turn_message (priv, msg);
return TRUE;
} }
send:
return nice_socket_send (priv->base_socket, to, len, buf); g_free (msg);
return FALSE;
} }
static gboolean static gboolean
socket_is_reliable (NiceSocket *sock) priv_add_channel_binding (TurnPriv *priv, NiceAddress *peer)
{ {
turn_priv *priv = (turn_priv *) sock->priv; size_t stun_len;
return nice_socket_is_reliable (priv->base_socket); struct sockaddr_storage sa;
}
static void nice_address_copy_to_sockaddr (peer, (struct sockaddr *)&sa);
socket_close (NiceSocket *sock)
{ if (priv->current_binding) {
turn_priv *priv = (turn_priv *) sock->priv; NiceAddress * pending= nice_address_new ();
GList *i = NULL; *pending = *peer;
for (i = priv->channels; i; i = i->next) { priv->pending_bindings = g_list_append (priv->pending_bindings, pending);
ChannelBinding *b = i->data; return FALSE;
g_free (b);
} }
g_list_free (priv->channels);
for (i = priv->pending_bindings; i; i = i->next) { if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9) {
uint16_t channel = 0x4000;
GList *i = priv->channels;
for (; i; i = i->next) {
ChannelBinding *b = i->data; ChannelBinding *b = i->data;
g_free (b); if (channel == b->channel) {
i = priv->channels;
channel++;
continue;
} }
g_list_free (priv->pending_bindings);
if (priv->tick_source != NULL) {
g_source_destroy (priv->tick_source);
g_source_unref (priv->tick_source);
priv->tick_source = NULL;
} }
g_free (priv->current_binding); if (channel >= 0x4000 && channel < 0xffff) {
g_free (priv->current_binding_msg); gboolean ret = priv_send_channel_bind (priv, NULL, channel, peer);
g_free (priv->username); if (ret) {
g_free (priv->password); priv->current_binding = g_new0 (ChannelBinding, 1);
g_free (priv); priv->current_binding->channel = channel;
} priv->current_binding->peer = *peer;
}
return ret;
}
return FALSE;
} else if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_MSN) {
TURNMessage *msg = g_new0 (TURNMessage, 1);
if (!stun_agent_init_request (&priv->agent, &msg->message,
msg->buffer, sizeof(msg->buffer), STUN_OLD_SET_ACTIVE_DST)) {
g_free (msg);
return FALSE;
}
NiceSocket * if (stun_message_append32 (&msg->message, STUN_ATTRIBUTE_MAGIC_COOKIE,
nice_udp_turn_socket_new ( TURN_MAGIC_COOKIE) != 0) {
NiceAgent *agent, g_free (msg);
NiceAddress *addr, return FALSE;
NiceSocket *base_socket, }
NiceAddress *server_addr,
gchar *username,
gchar *password,
NiceUdpTurnSocketCompatibility compatibility)
{
turn_priv *priv = g_new0 (turn_priv, 1);
NiceSocket *sock = g_slice_new0 (NiceSocket);
if (!sock) { if (priv->username != NULL && priv->username_len > 0) {
return NULL; if (stun_message_append_bytes (&msg->message, STUN_ATTRIBUTE_USERNAME,
priv->username, priv->username_len) != 0) {
g_free (msg);
return FALSE;
}
} }
if (compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9) { if (stun_message_append_addr (&msg->message,
stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES, STUN_ATTRIBUTE_DESTINATION_ADDRESS,
STUN_COMPATIBILITY_RFC5389, (struct sockaddr *)&sa, sizeof(sa)) != 0) {
STUN_AGENT_USAGE_LONG_TERM_CREDENTIALS); g_free (msg);
} else if (compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_MSN) { return FALSE;
stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES,
STUN_COMPATIBILITY_RFC3489,
STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
STUN_AGENT_USAGE_NO_INDICATION_AUTH);
} else if (compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
stun_agent_init (&priv->agent, STUN_ALL_KNOWN_ATTRIBUTES,
STUN_COMPATIBILITY_RFC3489,
STUN_AGENT_USAGE_SHORT_TERM_CREDENTIALS |
STUN_AGENT_USAGE_IGNORE_CREDENTIALS);
} }
priv->nice = agent; stun_len = stun_agent_finish_message (&priv->agent, &msg->message,
priv->channels = NULL; priv->password, priv->password_len);
priv->current_binding = NULL;
priv->base_socket = base_socket;
if (compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_MSN) { if (stun_len > 0) {
priv->username = g_base64_decode (username, &priv->username_len); priv->current_binding = g_new0 (ChannelBinding, 1);
priv->password = g_base64_decode (password, &priv->password_len); priv->current_binding->channel = 0;
} else { priv->current_binding->peer = *peer;
priv->username = (uint8_t *)g_strdup (username); priv_send_turn_message (priv, msg);
priv->username_len = (size_t) strlen (username); return TRUE;
if (compatibility == NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
priv->password = NULL;
priv->password_len = 0;
} else {
priv->password = (uint8_t *)g_strdup (password);
priv->password_len = (size_t) strlen (password);
} }
g_free (msg);
return FALSE;
} else if (priv->compatibility == NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE) {
priv->current_binding = g_new0 (ChannelBinding, 1);
priv->current_binding->channel = 0;
priv->current_binding->peer = *peer;
return TRUE;
} else {
return FALSE;
} }
priv->server_addr = *server_addr;
priv->compatibility = compatibility; return FALSE;
sock->addr = *addr;
sock->fileno = base_socket->fileno;
sock->send = socket_send;
sock->recv = socket_recv;
sock->is_reliable = socket_is_reliable;
sock->close = socket_close;
sock->priv = (void *) priv;
return sock;
} }
...@@ -35,15 +35,15 @@ ...@@ -35,15 +35,15 @@
* file under either the MPL or the LGPL. * file under either the MPL or the LGPL.
*/ */
#ifndef _UDP_TURN_H #ifndef _TURN_H
#define _UDP_TURN_H #define _TURN_H
typedef enum { typedef enum {
NICE_UDP_TURN_SOCKET_COMPATIBILITY_DRAFT9, NICE_TURN_SOCKET_COMPATIBILITY_DRAFT9,
NICE_UDP_TURN_SOCKET_COMPATIBILITY_GOOGLE, NICE_TURN_SOCKET_COMPATIBILITY_GOOGLE,
NICE_UDP_TURN_SOCKET_COMPATIBILITY_MSN, NICE_TURN_SOCKET_COMPATIBILITY_MSN,
} NiceUdpTurnSocketCompatibility; } NiceTurnSocketCompatibility;
#include "socket.h" #include "socket.h"
#include "agent.h" #include "agent.h"
...@@ -52,31 +52,20 @@ typedef enum { ...@@ -52,31 +52,20 @@ typedef enum {
G_BEGIN_DECLS G_BEGIN_DECLS
gint gint
nice_udp_turn_socket_parse_recv ( nice_turn_socket_parse_recv (NiceSocket *sock, NiceSocket **from_sock,
NiceSocket *sock, NiceAddress *from, guint len, gchar *buf,
NiceSocket **from_sock, NiceAddress *recv_from, gchar *recv_buf, guint recv_len);
NiceAddress *from,
guint len,
gchar *buf,
NiceAddress *recv_from,
gchar *recv_buf,
guint recv_len);
gboolean gboolean
nice_udp_turn_socket_set_peer (NiceSocket *sock, NiceAddress *peer); nice_turn_socket_set_peer (NiceSocket *sock, NiceAddress *peer);
NiceSocket * NiceSocket *
nice_udp_turn_socket_new ( nice_turn_socket_new (NiceAgent *agent, NiceAddress *addr,
NiceAgent *agent, NiceSocket *base_socket, NiceAddress *server_addr,
NiceAddress *addr, gchar *username, gchar *password, NiceTurnSocketCompatibility compatibility);
NiceSocket *udp_socket,
NiceAddress *server_addr,
gchar *username,
gchar *password,
NiceUdpTurnSocketCompatibility compatibility);
G_END_DECLS G_END_DECLS
#endif /* _UDP_TURN_H */ #endif /* _TURN_H */
...@@ -58,84 +58,13 @@ typedef unsigned long ssize_t; ...@@ -58,84 +58,13 @@ typedef unsigned long ssize_t;
#include <unistd.h> #include <unistd.h>
#endif #endif
/*** NiceSocket ***/
static int sock_recv_err (int fd)
{
#ifdef MSG_ERRQUEUE
/* Silently dequeue any error message if any */
struct msghdr hdr;
int saved = errno, val;
memset (&hdr, 0, sizeof (hdr));
val = recvmsg (fd, &hdr, MSG_ERRQUEUE);
errno = saved;
return val == 0;
#else
return 0;
#endif
}
static gint
socket_recv (
NiceSocket *sock,
NiceAddress *from,
guint len,
gchar *buf)
{
gint recvd;
struct sockaddr_storage sa;
socklen_t from_len = sizeof (sa);
recvd = recvfrom (sock->fileno, buf, len, 0, (struct sockaddr *) &sa,
&from_len);
if (recvd == -1)
{
sock_recv_err (sock->fileno);
return -1;
}
nice_address_set_from_sockaddr (from, (struct sockaddr *)&sa);
return recvd;
}
static gboolean
socket_send (
NiceSocket *sock,
const NiceAddress *to,
guint len,
const gchar *buf)
{
struct sockaddr_storage sa;
ssize_t sent;
nice_address_copy_to_sockaddr (to, (struct sockaddr *)&sa);
do
sent = sendto (sock->fileno, buf, len, 0, (struct sockaddr *) &sa,
sa.ss_family == AF_INET? sizeof (struct sockaddr_in) :
sizeof(struct sockaddr_in6));
while ((sent == -1) && sock_recv_err (sock->fileno));
return sent == (ssize_t)len;
}
static gboolean
socket_is_reliable (NiceSocket *sock)
{
return FALSE;
}
static void
socket_close (NiceSocket *sock)
{
#ifdef G_OS_WIN32
closesocket(sock->fileno);
#else
close (sock->fileno);
#endif
}
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);
NiceSocket * NiceSocket *
nice_udp_bsd_socket_new (NiceAddress *addr) nice_udp_bsd_socket_new (NiceAddress *addr)
...@@ -149,54 +78,14 @@ nice_udp_bsd_socket_new (NiceAddress *addr) ...@@ -149,54 +78,14 @@ nice_udp_bsd_socket_new (NiceAddress *addr)
return NULL; return NULL;
} }
if (addr != NULL) if (addr != NULL) {
{
nice_address_copy_to_sockaddr(addr, (struct sockaddr *)&name); nice_address_copy_to_sockaddr(addr, (struct sockaddr *)&name);
} } else {
else
{
memset (&name, 0, sizeof (name)); memset (&name, 0, sizeof (name));
name.ss_family = AF_UNSPEC; name.ss_family = AF_UNSPEC;
} }
#if 0 if (name.ss_family == AF_UNSPEC || name.ss_family == AF_INET) {
if ((name.ss_family == AF_INET6) || (name.ss_family == AF_UNSPEC))
{
sockfd = socket (PF_INET6, SOCK_DGRAM, 0);
if (sockfd != -1)
{
int v6 = name.ss_family == AF_INET6;
#if defined (IPV6_V6ONLY)
if (setsockopt (sockfd, IPPROTO_IPV6, IPV6_V6ONLY, &v6, sizeof (v6)))
#else
if (!v6)
#endif
{
#ifdef G_OS_WIN32
closesocket(sock->fileno);
#else
close (sockfd);
#endif
sockfd = -1;
}
else
{
# ifdef IPV6_RECVERR
int yes = 1;
setsockopt (sockfd, SOL_IPV6, IPV6_RECVERR, &yes, sizeof (yes));
# endif
name.ss_family = AF_INET6;
# ifdef HAVE_SA_LEN
name.ss_len = sizeof (struct sockaddr_in6);
# endif
}
}
}
#endif
if ((sockfd == -1)
&& ((name.ss_family == AF_UNSPEC) || (name.ss_family == AF_INET)))
{
sockfd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); sockfd = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP);
name.ss_family = AF_INET; name.ss_family = AF_INET;
#ifdef HAVE_SA_LEN #ifdef HAVE_SA_LEN
...@@ -209,14 +98,6 @@ nice_udp_bsd_socket_new (NiceAddress *addr) ...@@ -209,14 +98,6 @@ nice_udp_bsd_socket_new (NiceAddress *addr)
return NULL; return NULL;
} }
#ifdef IP_RECVERR
else
{
int yes = 1;
setsockopt (sockfd, SOL_IP, IP_RECVERR, &yes, sizeof (yes));
}
#endif
#ifdef FD_CLOEXEC #ifdef FD_CLOEXEC
fcntl (sockfd, F_SETFD, fcntl (sockfd, F_GETFD) | FD_CLOEXEC); fcntl (sockfd, F_SETFD, fcntl (sockfd, F_GETFD) | FD_CLOEXEC);
#endif #endif
...@@ -249,11 +130,61 @@ nice_udp_bsd_socket_new (NiceAddress *addr) ...@@ -249,11 +130,61 @@ nice_udp_bsd_socket_new (NiceAddress *addr)
} }
nice_address_set_from_sockaddr (&sock->addr, (struct sockaddr *)&name); nice_address_set_from_sockaddr (&sock->addr, (struct sockaddr *)&name);
sock->fileno = sockfd; sock->fileno = sockfd;
sock->send = socket_send; sock->send = socket_send;
sock->recv = socket_recv; sock->recv = socket_recv;
sock->is_reliable = socket_is_reliable; sock->is_reliable = socket_is_reliable;
sock->close = socket_close; sock->close = socket_close;
return sock; return sock;
} }
static void
socket_close (NiceSocket *sock)
{
#ifdef G_OS_WIN32
closesocket(sock->fileno);
#else
close (sock->fileno);
#endif
}
static gint
socket_recv (NiceSocket *sock, NiceAddress *from, guint len, gchar *buf)
{
gint recvd;
struct sockaddr_storage sa;
socklen_t from_len = sizeof (sa);
recvd = recvfrom (sock->fileno, buf, len, 0, (struct sockaddr *) &sa,
&from_len);
if (recvd > 0)
nice_address_set_from_sockaddr (from, (struct sockaddr *)&sa);
return recvd;
}
static gboolean
socket_send (NiceSocket *sock, const NiceAddress *to,
guint len, const gchar *buf)
{
struct sockaddr_storage sa;
ssize_t sent;
nice_address_copy_to_sockaddr (to, (struct sockaddr *)&sa);
sent = sendto (sock->fileno, buf, len, 0, (struct sockaddr *) &sa,
sa.ss_family == AF_INET? sizeof (struct sockaddr_in) :
sizeof(struct sockaddr_in6));
return sent == (ssize_t)len;
}
static gboolean
socket_is_reliable (NiceSocket *sock)
{
return FALSE;
}
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