From beea9b38be4efb5723d75d63d5a63241efd505cb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Cr=C3=AAte?= Date: Sun, 21 Oct 2018 12:01:55 +0200 Subject: tcp-passive: Clear connection on child closing If this isn't done, then there may be invalid points left inside the passive socket which could be used and cause a crash. Fixes #33 --- socket/tcp-bsd.c | 17 +++++++++++++++++ socket/tcp-bsd.h | 3 +++ socket/tcp-passive.c | 9 +++++++++ socket/tcp-passive.h | 3 +++ 4 files changed, 32 insertions(+) diff --git a/socket/tcp-bsd.c b/socket/tcp-bsd.c index 8f3ac89..49f5f25 100644 --- a/socket/tcp-bsd.c +++ b/socket/tcp-bsd.c @@ -46,6 +46,8 @@ #include "agent-priv.h" #include "socket-priv.h" +#include "tcp-passive.h" + #include #include #include @@ -70,6 +72,7 @@ typedef struct { gboolean reliable; NiceSocketWritableCb writable_cb; gpointer writable_data; + NiceSocket *passive_parent; } TcpPriv; #define MAX_QUEUE_LENGTH 20 @@ -226,6 +229,10 @@ socket_close (NiceSocket *sock) g_source_unref (priv->io_source); } + if (priv->passive_parent) { + nice_tcp_passive_socket_remove_connection (priv->passive_parent, &priv->remote_addr); + } + nice_socket_free_send_queue (&priv->send_queue); if (priv->context) @@ -458,3 +465,13 @@ socket_send_more ( g_mutex_unlock (&mutex); return TRUE; } + +void +nice_tcp_bsd_socket_set_passive_parent (NiceSocket *sock, NiceSocket *passive_parent) +{ + TcpPriv *priv = sock->priv; + + g_assert (priv->passive_parent == NULL); + + priv->passive_parent = passive_parent; +} diff --git a/socket/tcp-bsd.h b/socket/tcp-bsd.h index 58349cc..615b759 100644 --- a/socket/tcp-bsd.h +++ b/socket/tcp-bsd.h @@ -49,6 +49,9 @@ NiceSocket * nice_tcp_bsd_socket_new_from_gsock (GMainContext *ctx, GSocket *gsock, NiceAddress *remote_addr, NiceAddress *local_addr, gboolean reliable); +void +nice_tcp_bsd_socket_set_passive_parent (NiceSocket *socket, NiceSocket *passive_parent); + G_END_DECLS #endif /* _TCP_BSD_H */ diff --git a/socket/tcp-passive.c b/socket/tcp-passive.c index 131ff4b..d7684ad 100644 --- a/socket/tcp-passive.c +++ b/socket/tcp-passive.c @@ -310,6 +310,8 @@ nice_tcp_passive_socket_accept (NiceSocket *sock) if (new_socket) { NiceAddress *key = nice_address_dup (&remote_addr); + nice_tcp_bsd_socket_set_passive_parent (new_socket, sock); + nice_socket_set_writable_callback (new_socket, _child_writable_cb, sock); g_hash_table_insert (priv->connections, key, new_socket); } @@ -329,3 +331,10 @@ static guint nice_address_hash (const NiceAddress * key) return hash; } + +void nice_tcp_passive_socket_remove_connection (NiceSocket *sock, const NiceAddress *to) +{ + TcpPassivePriv *priv = sock->priv; + + g_hash_table_remove (priv->connections, to); +} diff --git a/socket/tcp-passive.h b/socket/tcp-passive.h index 37e780b..914f081 100644 --- a/socket/tcp-passive.h +++ b/socket/tcp-passive.h @@ -46,6 +46,9 @@ G_BEGIN_DECLS NiceSocket * nice_tcp_passive_socket_new (GMainContext *ctx, NiceAddress *addr); NiceSocket * nice_tcp_passive_socket_accept (NiceSocket *socket); +void nice_tcp_passive_socket_remove_connection (NiceSocket *socket, + const NiceAddress *to); + G_END_DECLS -- cgit v1.2.1