Commit b80bc3a1 authored by Philip Withnall's avatar Philip Withnall

socket: Return early from socket functions if the socket is closed

Explicitly check whether the socket is closed (universally represented
as sock->priv == NULL) before doing anything else in the socket methods.
This should safely return from unusual situations where the socket has
been closed and part-destroyed but still ends up having send() or recv()
called on it.
parent bb338797
......@@ -195,6 +195,7 @@ socket_close (NiceSocket *sock)
nice_socket_free_send_queue (&priv->send_queue);
g_slice_free(HttpPriv, sock->priv);
sock->priv = NULL;
}
static void
......@@ -280,6 +281,10 @@ socket_recv_messages (NiceSocket *sock,
HttpPriv *priv = sock->priv;
gint ret = -1;
/* Socket has been closed: */
if (sock->priv == NULL)
return 0;
if (priv->state == HTTP_STATE_CONNECTED) {
guint i;
......@@ -571,6 +576,10 @@ socket_send_messages (NiceSocket *sock, const NiceAddress *to,
{
HttpPriv *priv = sock->priv;
/* Socket has been closed: */
if (sock->priv == NULL)
return -1;
if (priv->state == HTTP_STATE_CONNECTED) {
/* Fast path. */
if (!priv->base_socket)
......
......@@ -173,6 +173,7 @@ socket_close (NiceSocket *sock)
nice_socket_free_send_queue (&priv->send_queue);
g_slice_free(PseudoSSLPriv, sock->priv);
sock->priv = NULL;
}
static gboolean
......@@ -203,6 +204,10 @@ socket_recv_messages (NiceSocket *sock,
{
PseudoSSLPriv *priv = sock->priv;
/* Socket has been closed: */
if (sock->priv == NULL)
return 0;
if (priv->handshaken) {
if (priv->base_socket) {
/* Fast path: once we’ve done the handshake, pass straight through to the
......@@ -251,6 +256,10 @@ socket_send_messages (NiceSocket *sock, const NiceAddress *to,
{
PseudoSSLPriv *priv = sock->priv;
/* Socket has been closed: */
if (sock->priv == NULL)
return -1;
if (priv->handshaken) {
/* Fast path: pass directly through to the base socket once the handshake is
* complete. */
......
......@@ -155,6 +155,7 @@ socket_close (NiceSocket *sock)
nice_socket_free_send_queue (&priv->send_queue);
g_slice_free(Socks5Priv, sock->priv);
sock->priv = NULL;
}
......@@ -166,6 +167,10 @@ socket_recv_messages (NiceSocket *sock,
guint i;
gint ret = -1;
/* Socket has been closed: */
if (sock->priv == NULL)
return 0;
switch (priv->state) {
case SOCKS_STATE_CONNECTED:
/* Common case: fast pass-through to the base socket once we’re
......@@ -418,6 +423,10 @@ socket_send_messages (NiceSocket *sock, const NiceAddress *to,
{
Socks5Priv *priv = sock->priv;
/* Socket has been closed: */
if (sock->priv == NULL)
return -1;
if (priv->state == SOCKS_STATE_CONNECTED) {
/* Fast path: pass through to the base socket once connected. */
if (priv->base_socket == NULL)
......
......@@ -229,6 +229,10 @@ socket_recv_messages (NiceSocket *sock,
TcpPriv *priv = sock->priv;
guint i;
/* Socket has been closed: */
if (sock->priv == NULL)
return 0;
/* Don't try to access the socket if it had an error */
if (priv->error)
return -1;
......@@ -279,6 +283,10 @@ socket_send_message (NiceSocket *sock,
GError *gerr = NULL;
gsize message_len;
/* Socket has been closed: */
if (sock->priv == NULL)
return -1;
/* Don't try to access the socket if it had an error, otherwise we risk a
* crash with SIGPIPE (Broken pipe) */
if (priv->error)
......@@ -336,6 +344,10 @@ socket_send_messages (NiceSocket *sock, const NiceAddress *to,
{
guint i;
/* Socket has been closed: */
if (sock->priv == NULL)
return -1;
for (i = 0; i < n_messages; i++) {
const NiceOutputMessage *message = &messages[i];
gssize len;
......
......@@ -183,6 +183,10 @@ socket_recv_messages (NiceSocket *sock,
guint i;
gboolean error = FALSE;
/* Socket has been closed: */
if (sock->priv == NULL)
return 0;
/* Read messages into recv_messages until one fails or would block, or we
* reach the end. */
for (i = 0; i < n_recv_messages; i++) {
......@@ -285,6 +289,10 @@ socket_send_messages (NiceSocket *sock, const NiceAddress *to,
{
guint i;
/* Socket has been closed: */
if (sock->priv == NULL)
return -1;
for (i = 0; i < n_messages; i++) {
const NiceOutputMessage *message = &messages[i];
gssize len;
......
......@@ -122,6 +122,7 @@ socket_close (NiceSocket *sock)
nice_socket_free (priv->base_socket);
g_slice_free(TurnTcpPriv, sock->priv);
sock->priv = NULL;
}
static gssize
......@@ -133,6 +134,10 @@ socket_recv_message (NiceSocket *sock, NiceInputMessage *recv_message)
GInputVector local_recv_buf;
NiceInputMessage local_recv_message;
/* Socket has been closed: */
if (sock->priv == NULL)
return 0;
if (priv->expecting_len == 0) {
guint headerlen = 0;
......@@ -236,6 +241,10 @@ socket_recv_messages (NiceSocket *nicesock,
guint i;
gboolean error = FALSE;
/* Socket has been closed: */
if (nicesock->priv == NULL)
return 0;
for (i = 0; i < n_recv_messages; i++) {
gssize len;
......@@ -276,6 +285,10 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
} header_buf;
guint offset = 0;
/* Socket has been closed: */
if (sock->priv == NULL)
return -1;
/* Count the number of buffers. */
if (message->n_buffers == -1) {
n_bufs = 0;
......@@ -375,6 +388,10 @@ socket_send_messages (NiceSocket *sock, const NiceAddress *to,
{
guint i;
/* Socket has been closed: */
if (sock->priv == NULL)
return -1;
for (i = 0; i < n_messages; i++) {
const NiceOutputMessage *message = &messages[i];
gssize len;
......
......@@ -318,6 +318,8 @@ socket_close (NiceSocket *sock)
g_free (priv->username);
g_free (priv->password);
g_free (priv);
sock->priv = NULL;
}
static gint
......@@ -330,6 +332,10 @@ socket_recv_messages (NiceSocket *sock,
gboolean error = FALSE;
guint n_valid_messages;
/* Socket has been closed: */
if (sock->priv == NULL)
return 0;
nice_debug ("received message on TURN socket");
n_messages = nice_socket_recv_messages (priv->base_socket,
......@@ -687,6 +693,10 @@ socket_send_message (NiceSocket *sock, const NiceAddress *to,
ChannelBinding *binding = NULL;
gint ret;
/* Socket has been closed: */
if (sock->priv == NULL)
return -1;
for (; i; i = i->next) {
ChannelBinding *b = i->data;
if (nice_address_equal (&b->peer, to)) {
......@@ -858,6 +868,10 @@ socket_send_messages (NiceSocket *sock, const NiceAddress *to,
{
guint i;
/* Socket has been closed: */
if (sock->priv == NULL)
return -1;
for (i = 0; i < n_messages; i++) {
const NiceOutputMessage *message = &messages[i];
gssize len;
......
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