Commit 25be0027 authored by Fabrice Bellet's avatar Fabrice Bellet Committed by Olivier Crête

conncheck: reorder some chunks of code

With this patch we simplify the levels of code indentation.

Differential Revision: https://phabricator.freedesktop.org/D1758
parent 5a42089a
......@@ -608,23 +608,33 @@ static gboolean priv_conn_check_tick_stream (NiceStream *stream, NiceAgent *agen
gboolean keep_timer_going = FALSE;
GSList *i;
CandidateCheckPair *pair;
unsigned int timeout;
/* step: process ongoing STUN transactions */
for (i = stream->conncheck_list; i ; i = i->next) {
CandidateCheckPair *p = i->data;
gchar tmpbuf1[INET6_ADDRSTRLEN], tmpbuf2[INET6_ADDRSTRLEN];
NiceComponent *component;
if (!agent_find_component (agent, p->stream_id, p->component_id,
NULL, &component))
continue;
if (p->state != NICE_CHECK_IN_PROGRESS)
continue;
if (p->state == NICE_CHECK_IN_PROGRESS) {
if (p->stun_message.buffer == NULL) {
nice_debug ("Agent %p : STUN connectivity check was cancelled, marking as done.", agent);
p->state = NICE_CHECK_FAILED;
nice_debug ("Agent %p : pair %p state FAILED", agent, p);
} else if (priv_timer_expired (&p->next_tick, now)) {
continue;
}
if (!priv_timer_expired (&p->next_tick, now))
continue;
switch (stun_timer_refresh (&p->timer)) {
case STUN_USAGE_TIMER_RETURN_TIMEOUT:
{
gchar tmpbuf1[INET6_ADDRSTRLEN], tmpbuf2[INET6_ADDRSTRLEN];
NiceComponent *component;
timer_timeout:
/* case: conncheck cancelled due to in-progress incoming
* check, requeing the pair, ICE spec, sect 7.2.1.4
......@@ -651,15 +661,10 @@ timer_timeout:
* in priv_conn_check_tick_stream(), which is a condition to
* make the transition connected to ready.
*/
if (agent_find_component (agent, p->stream_id, p->component_id,
NULL, &component))
priv_update_check_list_state_for_ready (agent, stream,
component);
priv_update_check_list_state_for_ready (agent, stream, component);
break;
}
case STUN_USAGE_TIMER_RETURN_RETRANSMIT:
{
unsigned int timeout = stun_timer_remainder (&p->timer);
timeout = stun_timer_remainder (&p->timer);
/* case: retransmission stopped, due to the nomination of
* a pair with a higher priority than this in-progress pair,
......@@ -691,10 +696,8 @@ timer_timeout:
g_time_val_add (&p->next_tick, timeout * 1000);
return TRUE;
}
case STUN_USAGE_TIMER_RETURN_SUCCESS:
{
unsigned int timeout = stun_timer_remainder (&p->timer);
timeout = stun_timer_remainder (&p->timer);
/* note: convert from milli to microseconds for g_time_val_add() */
p->next_tick = *now;
......@@ -702,14 +705,11 @@ timer_timeout:
keep_timer_going = TRUE;
break;
}
default:
/* Nothing to do. */
break;
}
}
}
}
/* step: perform an ordinary check, ICE spec, 5.8 "Scheduling Checks"
* note: This code is executed when the triggered checks list is
......@@ -2628,7 +2628,6 @@ int conn_check_send (NiceAgent *agent, CandidateCheckPair *pair)
if (NICE_AGENT_IS_COMPATIBLE_WITH_RFC5245_OR_OC2007R2 (agent)) {
switch (agent->nomination_mode) {
case NICE_NOMINATION_MODE_REGULAR:
{
/* We are doing regular nomination, so we set the use-candidate
* attrib, when the controlling agent decided which valid pair to
* resend with this flag in priv_conn_check_tick_stream()
......@@ -2637,9 +2636,7 @@ int conn_check_send (NiceAgent *agent, CandidateCheckPair *pair)
nice_debug ("Agent %p : %s: set cand_use=%d "
"(regular nomination).", agent, G_STRFUNC, cand_use);
break;
}
case NICE_NOMINATION_MODE_AGGRESSIVE:
{
/* We are doing aggressive nomination, we set the use-candidate
* attrib in every check we send, when we are the controlling
* agent, RFC 5245, 8.1.1.2
......@@ -2648,7 +2645,6 @@ int conn_check_send (NiceAgent *agent, CandidateCheckPair *pair)
nice_debug ("Agent %p : %s: set cand_use=%d "
"(aggressive nomination).", agent, G_STRFUNC, cand_use);
break;
}
default:
/* Nothing to do. */
break;
......@@ -2656,7 +2652,13 @@ int conn_check_send (NiceAgent *agent, CandidateCheckPair *pair)
} else if (cand_use)
pair->nominated = controlling;
if (uname_len > 0) {
if (uname_len == 0) {
nice_debug ("Agent %p: no credentials found, cancelling conncheck", agent);
pair->stun_message.buffer = NULL;
pair->stun_message.buffer_len = 0;
return -1;
}
buffer_len = stun_usage_ice_conncheck_create (&component->stun_agent,
&pair->stun_message, pair->stun_buffer, sizeof(pair->stun_buffer),
uname, uname_len, password, password_len,
......@@ -2673,10 +2675,16 @@ int conn_check_send (NiceAgent *agent, CandidateCheckPair *pair)
g_free (password);
}
if (buffer_len > 0) {
if (nice_socket_is_reliable(pair->sockptr)) {
if (buffer_len == 0) {
nice_debug ("Agent %p: buffer is empty, cancelling conncheck", agent);
pair->stun_message.buffer = NULL;
pair->stun_message.buffer_len = 0;
return -1;
}
if (nice_socket_is_reliable(pair->sockptr))
stun_timer_start_reliable(&pair->timer, agent->stun_reliable_timeout);
} else {
else {
StunTimer *timer = &pair->timer;
if (pair->recheck_on_timeout)
......@@ -2723,10 +2731,9 @@ int conn_check_send (NiceAgent *agent, CandidateCheckPair *pair)
pair->sockptr = new_socket;
_priv_set_socket_tos (agent, pair->sockptr, stream2->tos);
if (agent->reliable) {
if (agent->reliable)
nice_socket_set_writable_callback (pair->sockptr,
_tcp_sock_is_writable, component2);
}
nice_component_attach_socket (component2, new_socket);
}
......@@ -2736,27 +2743,14 @@ int conn_check_send (NiceAgent *agent, CandidateCheckPair *pair)
agent_socket_send (pair->sockptr, &pair->remote->addr,
buffer_len, (gchar *)pair->stun_buffer);
if (agent->compatibility == NICE_COMPATIBILITY_OC2007R2) {
if (agent->compatibility == NICE_COMPATIBILITY_OC2007R2)
ms_ice2_legacy_conncheck_send (&pair->stun_message, pair->sockptr,
&pair->remote->addr);
}
timeout = stun_timer_remainder (&pair->timer);
/* note: convert from milli to microseconds for g_time_val_add() */
g_get_current_time (&pair->next_tick);
g_time_val_add (&pair->next_tick, timeout * 1000);
} else {
nice_debug ("Agent %p: buffer is empty, cancelling conncheck", agent);
pair->stun_message.buffer = NULL;
pair->stun_message.buffer_len = 0;
return -1;
}
} else {
nice_debug ("Agent %p: no credentials found, cancelling conncheck", agent);
pair->stun_message.buffer = NULL;
pair->stun_message.buffer_len = 0;
return -1;
}
return 0;
}
......@@ -2876,13 +2870,14 @@ static gboolean priv_schedule_triggered_check (NiceAgent *agent, NiceStream *str
nice_debug ("Agent %p : Found a matching pair %p (%s) (state=%c) ...",
agent, p, p->foundation, priv_state_to_gchar (p->state));
if (p->state == NICE_CHECK_WAITING ||
p->state == NICE_CHECK_FROZEN) {
switch (p->state) {
case NICE_CHECK_WAITING:
case NICE_CHECK_FROZEN:
nice_debug ("Agent %p : pair %p added for a triggered check.",
agent, p);
priv_add_pair_to_triggered_check_queue (agent, p);
}
else if (p->state == NICE_CHECK_IN_PROGRESS) {
break;
case NICE_CHECK_IN_PROGRESS:
/* note: according to ICE SPEC sect 7.2.1.4 "Triggered Checks"
* we cancel the in-progress transaction, and after the
* retransmission timeout, we create a new connectivity check
......@@ -2894,20 +2889,17 @@ static gboolean priv_schedule_triggered_check (NiceAgent *agent, NiceStream *str
* so there's no reason to recheck this pair, since it can in
* no way replace the nominated one.
*/
if (!nice_socket_is_reliable (p->sockptr)) {
if (p->retransmit_on_timeout) {
if (!nice_socket_is_reliable (p->sockptr) &&
p->retransmit_on_timeout) {
nice_debug ("Agent %p : pair %p will be rechecked "
"on stun timer timeout.", agent, p);
/* this flag will determine the action at the retransmission
* timeout of the stun timer
*/
p->recheck_on_timeout = TRUE;
} else
nice_debug ("Agent %p : pair %p won't be retransmitted.",
agent, p);
}
}
else if (p->state == NICE_CHECK_SUCCEEDED) {
break;
case NICE_CHECK_SUCCEEDED:
nice_debug ("Agent %p : nothing to do for pair %p.", agent, p);
/* note: this is a bit unsure corner-case -- let's do the
same state update as for processing responses to our own checks */
......@@ -2917,7 +2909,8 @@ static gboolean priv_schedule_triggered_check (NiceAgent *agent, NiceStream *str
* that causes the ready -> connected transition.
*/
priv_update_check_list_state_for_ready (agent, stream, component);
} else if (p->state == NICE_CHECK_FAILED) {
break;
case NICE_CHECK_FAILED:
/* 7.2.1.4 Triggered Checks
* If the state of the pair is Failed, it is changed to Waiting
* and the agent MUST create a new connectivity check for that
......@@ -2941,9 +2934,10 @@ static gboolean priv_schedule_triggered_check (NiceAgent *agent, NiceStream *str
component->id, NICE_COMPONENT_STATE_CONNECTING);
conn_check_schedule_next (agent);
}
} else
nice_debug ("Agent %p : pair %p won't be retransmitted.",
agent, p);
}
break;
default:
break;
}
/* note: the spec says the we SHOULD retransmit in-progress
......@@ -3271,18 +3265,21 @@ static gboolean priv_map_reply_to_conn_check_request (NiceAgent *agent, NiceStre
socklen_t socklen = sizeof (sockaddr);
GSList *i;
StunUsageIceReturn res;
gboolean trans_found = FALSE;
StunTransactionId discovery_id;
StunTransactionId response_id;
stun_message_id (resp, response_id);
for (i = stream->conncheck_list; i && trans_found != TRUE; i = i->next) {
for (i = stream->conncheck_list; i; i = i->next) {
CandidateCheckPair *p = i->data;
if (p->stun_message.buffer) {
if (p->stun_message.buffer == NULL)
continue;
stun_message_id (&p->stun_message, discovery_id);
if (memcmp (discovery_id, response_id, sizeof(StunTransactionId)) == 0) {
if (memcmp (discovery_id, response_id, sizeof(StunTransactionId)))
continue;
res = stun_usage_ice_conncheck_process (resp,
&sockaddr.storage, &socklen,
agent_to_ice_compatibility (agent));
......@@ -3302,7 +3299,7 @@ static gboolean priv_map_reply_to_conn_check_request (NiceAgent *agent, NiceStre
/* step: verify that response came from the same IP address we
* sent the original request to (see 7.1.2.1. "Failure
* Cases") */
if (nice_address_equal (from, &p->remote->addr) != TRUE) {
if (nice_address_equal (from, &p->remote->addr) == FALSE) {
p->state = NICE_CHECK_FAILED;
if (nice_debug_is_enabled ()) {
......@@ -3316,8 +3313,7 @@ static gboolean priv_map_reply_to_conn_check_request (NiceAgent *agent, NiceStre
tmpbuf, nice_address_get_port (&p->remote->addr),
tmpbuf2, nice_address_get_port (from));
}
trans_found = TRUE;
break;
return TRUE;
}
/* note: CONNECTED but not yet READY, see docs */
......@@ -3328,19 +3324,15 @@ static gboolean priv_map_reply_to_conn_check_request (NiceAgent *agent, NiceStre
* "Discovering Peer Reflexive Candidates" ICE ID-19) */
if (res == STUN_USAGE_ICE_RETURN_NO_MAPPED_ADDRESS) {
/* note: this is same as "adding to VALID LIST" in the spec
text */
p->state = NICE_CHECK_SUCCEEDED;
p->valid = TRUE;
g_assert_not_reached ();
nice_debug ("Agent %p : Mapped address not found."
" conncheck %p SUCCEEDED.", agent, p);
nice_component_add_valid_candidate (component, p->remote);
} else {
} else
ok_pair = priv_process_response_check_for_reflexive (agent,
stream, component, p, sockptr, &sockaddr.addr,
local_candidate, remote_candidate);
}
/* note: The success of this check might also
* cause the state of other checks to change as well, ICE
......@@ -3414,19 +3406,16 @@ static gboolean priv_map_reply_to_conn_check_request (NiceAgent *agent, NiceStre
", got a nominated pair");
/* Do not step down to CONNECTED if we're already at state READY*/
if (component->state != NICE_COMPONENT_STATE_READY) {
if (component->state != NICE_COMPONENT_STATE_READY)
/* step: notify the client of a new component state (must be done
* before the possible check list state update step */
agent_signal_component_state_change (agent,
stream->id, component->id, NICE_COMPONENT_STATE_CONNECTED);
}
}
/* step: update pair states (ICE 7.1.2.2.3 "Updating pair
states" and 8.1.2 "Updating States", ID-19) */
priv_update_check_list_state_for_ready (agent, stream, component);
trans_found = TRUE;
} else if (res == STUN_USAGE_ICE_RETURN_ROLE_CONFLICT) {
/* case: role conflict error, need to restart with new role */
nice_debug ("Agent %p : conncheck %p ROLE CONFLICT, restarting", agent, p);
......@@ -3460,19 +3449,16 @@ static gboolean priv_map_reply_to_conn_check_request (NiceAgent *agent, NiceStre
p->stun_message.buffer_len = 0;
priv_add_pair_to_triggered_check_queue (agent, p);
}
trans_found = TRUE;
} else {
/* case: STUN error, the check STUN context was freed */
nice_debug ("Agent %p : conncheck %p FAILED.", agent, p);
p->stun_message.buffer = NULL;
p->stun_message.buffer_len = 0;
trans_found = TRUE;
}
}
}
return TRUE;
}
return trans_found;
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