Commit e0ed4fb3 authored by Jakub Adam's avatar Jakub Adam Committed by Philip Withnall

socket: refactor nice_socket_is_base_of()

• rename to nice_socket_is_based_on() and swap the order of arguments
   accordingly; the implementation doesn't have to use the confusing
   'return other->is_base_of()' pattern anymore
 • fix potential NULL dereferences

The argument order in agent_recv_message_unlocked() was already wrongly
swapped in 1732c7d6 and thus this commit isn't changing it back because
that order has become the correct one.
Reviewed-by: Philip Withnall's avatarPhilip Withnall <philip.withnall@collabora.co.uk>
Differential Revision: https://phabricator.freedesktop.org/D866
parent 38268e53
......@@ -1827,7 +1827,7 @@ _tcp_sock_is_writable (NiceSocket *sock, gpointer user_data)
/* Don't signal writable if the socket that has become writable is not
* the selected pair */
if (component->selected_pair.local == NULL ||
!nice_socket_is_base_of (sock, component->selected_pair.local->sockptr)) {
!nice_socket_is_based_on (component->selected_pair.local->sockptr, sock)) {
agent_unlock ();
return;
}
......@@ -3573,7 +3573,7 @@ agent_recv_message_unlocked (
if (cand->type == NICE_CANDIDATE_TYPE_RELAYED &&
cand->stream_id == stream->id &&
cand->component_id == component->id &&
nice_socket_is_base_of (cand->sockptr, nicesock)) {
nice_socket_is_based_on (cand->sockptr, nicesock)) {
retval = nice_udp_turn_socket_parse_recv_message (cand->sockptr, &nicesock,
message);
break;
......
......@@ -95,7 +95,7 @@ static gboolean socket_is_reliable (NiceSocket *sock);
static gboolean socket_can_send (NiceSocket *sock, NiceAddress *addr);
static void socket_set_writable_callback (NiceSocket *sock,
NiceSocketWritableCb callback, gpointer user_data);
static gboolean socket_is_base_of (NiceSocket *sock, NiceSocket *other);
static gboolean socket_is_based_on (NiceSocket *sock, NiceSocket *other);
NiceSocket *
nice_http_socket_new (NiceSocket *base_socket,
......@@ -127,7 +127,7 @@ nice_http_socket_new (NiceSocket *base_socket,
sock->is_reliable = socket_is_reliable;
sock->can_send = socket_can_send;
sock->set_writable_callback = socket_set_writable_callback;
sock->is_base_of = socket_is_base_of;
sock->is_based_on = socket_is_based_on;
sock->close = socket_close;
/* Send HTTP CONNECT */
......@@ -646,9 +646,10 @@ socket_set_writable_callback (NiceSocket *sock,
}
static gboolean
socket_is_base_of (NiceSocket *sock, NiceSocket *other)
socket_is_based_on (NiceSocket *sock, NiceSocket *other)
{
HttpPriv *priv = other->priv;
HttpPriv *priv = sock->priv;
return (sock == other) || nice_socket_is_base_of (sock, priv->base_socket);
return (sock == other) ||
(priv && nice_socket_is_based_on (priv->base_socket, other));
}
......@@ -116,7 +116,7 @@ static gboolean socket_is_reliable (NiceSocket *sock);
static gboolean socket_can_send (NiceSocket *sock, NiceAddress *addr);
static void socket_set_writable_callback (NiceSocket *sock,
NiceSocketWritableCb callback, gpointer user_data);
static gboolean socket_is_base_of (NiceSocket *sock, NiceSocket *other);
static gboolean socket_is_based_on (NiceSocket *sock, NiceSocket *other);
NiceSocket *
nice_pseudossl_socket_new (NiceSocket *base_socket,
......@@ -153,7 +153,7 @@ nice_pseudossl_socket_new (NiceSocket *base_socket,
sock->is_reliable = socket_is_reliable;
sock->can_send = socket_can_send;
sock->set_writable_callback = socket_set_writable_callback;
sock->is_base_of = socket_is_base_of;
sock->is_based_on = socket_is_based_on;
sock->close = socket_close;
/* We send 'to' NULL because it will always be to an already connected
......@@ -323,9 +323,10 @@ socket_set_writable_callback (NiceSocket *sock,
}
static gboolean
socket_is_base_of (NiceSocket *sock, NiceSocket *other)
socket_is_based_on (NiceSocket *sock, NiceSocket *other)
{
PseudoSSLPriv *priv = other->priv;
PseudoSSLPriv *priv = sock->priv;
return (sock == other) || nice_socket_is_base_of (sock, priv->base_socket);
return (sock == other) ||
(priv && nice_socket_is_based_on (priv->base_socket, other));
}
......@@ -266,10 +266,10 @@ nice_socket_set_writable_callback (NiceSocket *sock,
}
gboolean
nice_socket_is_base_of (NiceSocket *sock, NiceSocket *other)
nice_socket_is_based_on (NiceSocket *sock, NiceSocket *other)
{
if (other->is_base_of)
return other->is_base_of (sock, other);
if (sock->is_based_on)
return sock->is_based_on (sock, other);
return (sock == other);
}
......
......@@ -88,7 +88,7 @@ struct _NiceSocket
gboolean (*can_send) (NiceSocket *sock, NiceAddress *addr);
void (*set_writable_callback) (NiceSocket *sock,
NiceSocketWritableCb callback, gpointer user_data);
gboolean (*is_base_of) (NiceSocket *sock, NiceSocket *other);
gboolean (*is_based_on) (NiceSocket *sock, NiceSocket *other);
void (*close) (NiceSocket *sock);
void *priv;
};
......@@ -126,19 +126,21 @@ nice_socket_set_writable_callback (NiceSocket *sock,
NiceSocketWritableCb callback, gpointer user_data);
/**
* nice_socket_is_base_of:
* nice_socket_is_based_on:
* @sock: a #NiceSocket
* @other: another #NiceSocket
*
* Check whether @sock is equal to, or a base socket of, @other or one of
* @other's base sockets.
* Checks whether @sock wraps @other as a source and destination of its read and
* write operations. The function traverses the whole chain of @sock's base
* sockets until @other is found or the end is reached.
*
* Returns: %TRUE if @sock is a base socket of @other, %FALSE otherwise
* Returns: %TRUE if @sock is based on @other or if @sock and @other are
* the same socket, %FALSE otherwise.
*
* Since: UNRELEASED
*/
gboolean
nice_socket_is_base_of (NiceSocket *sock, NiceSocket *other);
nice_socket_is_based_on (NiceSocket *sock, NiceSocket *other);
void
nice_socket_free (NiceSocket *sock);
......
......@@ -81,7 +81,7 @@ static gboolean socket_is_reliable (NiceSocket *sock);
static gboolean socket_can_send (NiceSocket *sock, NiceAddress *addr);
static void socket_set_writable_callback (NiceSocket *sock,
NiceSocketWritableCb callback, gpointer user_data);
static gboolean socket_is_base_of (NiceSocket *sock, NiceSocket *other);
static gboolean socket_is_based_on (NiceSocket *sock, NiceSocket *other);
NiceSocket *
......@@ -109,7 +109,7 @@ nice_socks5_socket_new (NiceSocket *base_socket,
sock->is_reliable = socket_is_reliable;
sock->can_send = socket_can_send;
sock->set_writable_callback = socket_set_writable_callback;
sock->is_base_of = socket_is_base_of;
sock->is_based_on = socket_is_based_on;
sock->close = socket_close;
/* Send SOCKS5 handshake */
......@@ -492,9 +492,10 @@ socket_set_writable_callback (NiceSocket *sock,
}
static gboolean
socket_is_base_of (NiceSocket *sock, NiceSocket *other)
socket_is_based_on (NiceSocket *sock, NiceSocket *other)
{
Socks5Priv *priv = other->priv;
Socks5Priv *priv = sock->priv;
return (sock == other) || nice_socket_is_base_of (sock, priv->base_socket);
return (sock == other) ||
(priv && nice_socket_is_based_on (priv->base_socket, other));
}
......@@ -86,7 +86,7 @@ static gboolean socket_is_reliable (NiceSocket *sock);
static gboolean socket_can_send (NiceSocket *sock, NiceAddress *addr);
static void socket_set_writable_callback (NiceSocket *sock,
NiceSocketWritableCb callback, gpointer user_data);
static gboolean socket_is_base_of (NiceSocket *sock, NiceSocket *other);
static gboolean socket_is_based_on (NiceSocket *sock, NiceSocket *other);
NiceSocket *
nice_udp_turn_over_tcp_socket_new (NiceSocket *base_socket,
......@@ -108,7 +108,7 @@ nice_udp_turn_over_tcp_socket_new (NiceSocket *base_socket,
sock->is_reliable = socket_is_reliable;
sock->can_send = socket_can_send;
sock->set_writable_callback = socket_set_writable_callback;
sock->is_base_of = socket_is_base_of;
sock->is_based_on = socket_is_based_on;
sock->close = socket_close;
return sock;
......@@ -462,9 +462,10 @@ socket_set_writable_callback (NiceSocket *sock,
}
static gboolean
socket_is_base_of (NiceSocket *sock, NiceSocket *other)
socket_is_based_on (NiceSocket *sock, NiceSocket *other)
{
TurnTcpPriv *priv = other->priv;
TurnTcpPriv *priv = sock->priv;
return (sock == other) || nice_socket_is_base_of (sock, priv->base_socket);
return (sock == other) ||
(priv && nice_socket_is_based_on (priv->base_socket, other));
}
......@@ -125,7 +125,7 @@ static gboolean socket_is_reliable (NiceSocket *sock);
static gboolean socket_can_send (NiceSocket *sock, NiceAddress *addr);
static void socket_set_writable_callback (NiceSocket *sock,
NiceSocketWritableCb callback, gpointer user_data);
static gboolean socket_is_base_of (NiceSocket *sock, NiceSocket *other);
static gboolean socket_is_based_on (NiceSocket *sock, NiceSocket *other);
static void priv_process_pending_bindings (UdpTurnPriv *priv);
static gboolean priv_retransmissions_tick_unlocked (UdpTurnPriv *priv);
......@@ -244,7 +244,7 @@ nice_udp_turn_socket_new (GMainContext *ctx, NiceAddress *addr,
sock->is_reliable = socket_is_reliable;
sock->can_send = socket_can_send;
sock->set_writable_callback = socket_set_writable_callback;
sock->is_base_of = socket_is_base_of;
sock->is_based_on = socket_is_based_on;
sock->close = socket_close;
sock->priv = (void *) priv;
......@@ -954,11 +954,12 @@ socket_set_writable_callback (NiceSocket *sock,
}
static gboolean
socket_is_base_of (NiceSocket *sock, NiceSocket *other)
socket_is_based_on (NiceSocket *sock, NiceSocket *other)
{
UdpTurnPriv *priv = other->priv;
UdpTurnPriv *priv = sock->priv;
return (sock == other) || nice_socket_is_base_of (sock, priv->base_socket);
return (sock == other) ||
(priv && nice_socket_is_based_on (priv->base_socket, other));
}
static gboolean
......
......@@ -43,7 +43,7 @@ check_PROGRAMS = \
test-io-stream-cancelling \
test-io-stream-pollable \
test-send-recv \
test-socket-is-base-of \
test-socket-is-based-on \
test-priority \
test-mainloop \
test-fullmode \
......@@ -104,7 +104,7 @@ test_io_stream_pollable_LDADD = $(COMMON_LDADD)
test_send_recv_SOURCES = test-send-recv.c test-io-stream-common.c
test_send_recv_LDADD = $(COMMON_LDADD)
test_socket_is_base_of_LDADD = $(COMMON_LDADD)
test_socket_is_based_on_LDADD = $(COMMON_LDADD)
test_priority_LDADD = $(COMMON_LDADD)
......
......@@ -44,37 +44,37 @@ static NiceSocket *udp_turn_over_tcp;
static void
socket_base_udp_bsd (void)
{
g_assert (nice_socket_is_base_of (udp_bsd, udp_bsd));
g_assert (!nice_socket_is_base_of (udp_bsd, tcp_active));
g_assert (!nice_socket_is_base_of (udp_bsd, pseudossl));
g_assert (!nice_socket_is_base_of (udp_bsd, udp_turn_over_tcp));
g_assert (nice_socket_is_based_on (udp_bsd, udp_bsd));
g_assert (!nice_socket_is_based_on (udp_bsd, tcp_active));
g_assert (!nice_socket_is_based_on (udp_bsd, pseudossl));
g_assert (!nice_socket_is_based_on (udp_bsd, udp_turn_over_tcp));
}
static void
socket_base_tcp_active (void)
{
g_assert (!nice_socket_is_base_of (tcp_active, udp_bsd));
g_assert (nice_socket_is_base_of (tcp_active, tcp_active));
g_assert (nice_socket_is_base_of (tcp_active, pseudossl));
g_assert (nice_socket_is_base_of (tcp_active, udp_turn_over_tcp));
g_assert (!nice_socket_is_based_on (tcp_active, udp_bsd));
g_assert (nice_socket_is_based_on (tcp_active, tcp_active));
g_assert (!nice_socket_is_based_on (tcp_active, pseudossl));
g_assert (!nice_socket_is_based_on (tcp_active, udp_turn_over_tcp));
}
static void
socket_base_pseudossl (void)
{
g_assert (!nice_socket_is_base_of (pseudossl, udp_bsd));
g_assert (!nice_socket_is_base_of (pseudossl, tcp_active));
g_assert (nice_socket_is_base_of (pseudossl, pseudossl));
g_assert (nice_socket_is_base_of (pseudossl, udp_turn_over_tcp));
g_assert (!nice_socket_is_based_on (pseudossl, udp_bsd));
g_assert (nice_socket_is_based_on (pseudossl, tcp_active));
g_assert (nice_socket_is_based_on (pseudossl, pseudossl));
g_assert (!nice_socket_is_based_on (pseudossl, udp_turn_over_tcp));
}
static void
socket_base_udp_turn_over_tcp (void)
{
g_assert (!nice_socket_is_base_of (udp_turn_over_tcp, udp_bsd));
g_assert (!nice_socket_is_base_of (udp_turn_over_tcp, tcp_active));
g_assert (!nice_socket_is_base_of (udp_turn_over_tcp, pseudossl));
g_assert (nice_socket_is_base_of (udp_turn_over_tcp, udp_turn_over_tcp));
g_assert (!nice_socket_is_based_on (udp_turn_over_tcp, udp_bsd));
g_assert (nice_socket_is_based_on (udp_turn_over_tcp, tcp_active));
g_assert (nice_socket_is_based_on (udp_turn_over_tcp, pseudossl));
g_assert (nice_socket_is_based_on (udp_turn_over_tcp, udp_turn_over_tcp));
}
int
......
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