Commit f159c3aa authored by Youness Alaoui's avatar Youness Alaoui

Make stun_timer timeouts configurable (breaks API)

parent ea418a22
...@@ -605,7 +605,8 @@ static gboolean priv_conn_keepalive_tick_unlocked (NiceAgent *agent) ...@@ -605,7 +605,8 @@ static gboolean priv_conn_keepalive_tick_unlocked (NiceAgent *agent)
agent, buf_len, p->keepalive.stun_message.buffer); agent, buf_len, p->keepalive.stun_message.buffer);
if (buf_len > 0) { if (buf_len > 0) {
stun_timer_start (&p->keepalive.timer); stun_timer_start (&p->keepalive.timer, STUN_TIMER_DEFAULT_TIMEOUT,
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
agent->media_after_tick = FALSE; agent->media_after_tick = FALSE;
...@@ -823,7 +824,8 @@ static void priv_turn_allocate_refresh_tick_unlocked (CandidateRefresh *cand) ...@@ -823,7 +824,8 @@ static void priv_turn_allocate_refresh_tick_unlocked (CandidateRefresh *cand)
} }
if (buffer_len > 0) { if (buffer_len > 0) {
stun_timer_start (&cand->timer); stun_timer_start (&cand->timer, STUN_TIMER_DEFAULT_TIMEOUT,
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
/* send the refresh */ /* send the refresh */
nice_socket_send (cand->nicesock, &cand->server, nice_socket_send (cand->nicesock, &cand->server,
...@@ -1642,7 +1644,8 @@ int conn_check_send (NiceAgent *agent, CandidateCheckPair *pair) ...@@ -1642,7 +1644,8 @@ int conn_check_send (NiceAgent *agent, CandidateCheckPair *pair)
} }
if (buffer_len > 0) { if (buffer_len > 0) {
stun_timer_start (&pair->timer); stun_timer_start (&pair->timer, STUN_TIMER_DEFAULT_TIMEOUT,
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
/* send the conncheck */ /* send the conncheck */
nice_socket_send (pair->local->sockptr, &pair->remote->addr, nice_socket_send (pair->local->sockptr, &pair->remote->addr,
......
...@@ -886,7 +886,8 @@ static gboolean priv_discovery_tick_unlocked (gpointer pointer) ...@@ -886,7 +886,8 @@ static gboolean priv_discovery_tick_unlocked (gpointer pointer)
} }
if (buffer_len > 0) { if (buffer_len > 0) {
stun_timer_start (&cand->timer); stun_timer_start (&cand->timer, 200,
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
/* send the conncheck */ /* send the conncheck */
nice_socket_send (cand->nicesock, &cand->server, nice_socket_send (cand->nicesock, &cand->server,
......
...@@ -205,6 +205,9 @@ stun_usage_ice_conncheck_use_candidate ...@@ -205,6 +205,9 @@ stun_usage_ice_conncheck_use_candidate
<TITLE>Timer</TITLE> <TITLE>Timer</TITLE>
StunTimer StunTimer
StunUsageTimerReturn StunUsageTimerReturn
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS
STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT
STUN_TIMER_DEFAULT_TIMEOUT
stun_timer_start stun_timer_start
stun_timer_start_reliable stun_timer_start_reliable
stun_timer_refresh stun_timer_refresh
......
...@@ -697,9 +697,11 @@ priv_send_turn_message (TurnPriv *priv, TURNMessage *msg) ...@@ -697,9 +697,11 @@ priv_send_turn_message (TurnPriv *priv, TURNMessage *msg)
stun_len, (gchar *)msg->buffer); stun_len, (gchar *)msg->buffer);
if (nice_socket_is_reliable (priv->base_socket)) { if (nice_socket_is_reliable (priv->base_socket)) {
stun_timer_start_reliable (&msg->timer); stun_timer_start_reliable (&msg->timer,
STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT);
} else { } else {
stun_timer_start (&msg->timer); stun_timer_start (&msg->timer, STUN_TIMER_DEFAULT_TIMEOUT,
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
} }
priv->current_binding_msg = msg; priv->current_binding_msg = msg;
......
...@@ -460,7 +460,8 @@ StunUsageBindReturn stun_usage_bind_run (const struct sockaddr *srv, ...@@ -460,7 +460,8 @@ StunUsageBindReturn stun_usage_bind_run (const struct sockaddr *srv,
return STUN_USAGE_BIND_RETURN_ERROR; return STUN_USAGE_BIND_RETURN_ERROR;
} }
stun_timer_start (&timer); stun_timer_start (&timer, STUN_TIMER_DEFAULT_TIMEOUT,
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
stun_debug ("STUN transaction started (timeout %dms).\n", stun_debug ("STUN transaction started (timeout %dms).\n",
stun_timer_remainder (&timer)); stun_timer_remainder (&timer));
...@@ -516,7 +517,8 @@ StunUsageBindReturn stun_usage_bind_run (const struct sockaddr *srv, ...@@ -516,7 +517,8 @@ StunUsageBindReturn stun_usage_bind_run (const struct sockaddr *srv,
if (val < -1) if (val < -1)
return STUN_USAGE_BIND_RETURN_ERROR; return STUN_USAGE_BIND_RETURN_ERROR;
stun_timer_start (&timer); stun_timer_start (&timer, STUN_TIMER_DEFAULT_TIMEOUT,
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
ret = STUN_USAGE_TRANS_RETURN_RETRY; ret = STUN_USAGE_TRANS_RETURN_RETRY;
} else if (bind_ret == STUN_USAGE_BIND_RETURN_INVALID) { } else if (bind_ret == STUN_USAGE_BIND_RETURN_INVALID) {
ret = STUN_USAGE_TRANS_RETURN_RETRY; ret = STUN_USAGE_TRANS_RETURN_RETRY;
......
...@@ -51,23 +51,6 @@ ...@@ -51,23 +51,6 @@
#include <stdlib.h> /* div() */ #include <stdlib.h> /* div() */
/*
* Initial STUN timeout (milliseconds). The spec says it should be 100ms,
* but that's way too short for most types of wireless Internet access.
*/
#define STUN_INIT_TIMEOUT 600
#define STUN_END_TIMEOUT 4800
#define STUN_RELIABLE_TIMEOUT 7900
#if STUN_RELIABLE_TIMEOUT < STUN_END_TIMEOUT
/* Reliable timeout MUST be bigger (or equal) to end timeout, so that
* retransmissions never happen with reliable transports. */
# error Inconsistent STUN timeout values!
#endif
/* /*
* Clock used throughout the STUN code. * Clock used throughout the STUN code.
* STUN requires a monotonic 1kHz clock to operate properly. * STUN requires a monotonic 1kHz clock to operate properly.
...@@ -117,17 +100,19 @@ static void add_delay (struct timeval *ts, unsigned delay) ...@@ -117,17 +100,19 @@ static void add_delay (struct timeval *ts, unsigned delay)
} }
void stun_timer_start (StunTimer *timer) void stun_timer_start (StunTimer *timer, unsigned int initial_timeout,
unsigned int max_retransmissions)
{ {
stun_gettime (&timer->deadline); stun_gettime (&timer->deadline);
add_delay (&timer->deadline, timer->delay = STUN_INIT_TIMEOUT); timer->delay = initial_timeout;
timer->max_retransmissions = max_retransmissions;
add_delay (&timer->deadline, timer->delay);
} }
void stun_timer_start_reliable (StunTimer *timer) void stun_timer_start_reliable (StunTimer *timer, unsigned int initial_timeout)
{ {
stun_gettime (&timer->deadline); stun_timer_start (timer, initial_timeout, 0);
add_delay (&timer->deadline, timer->delay = STUN_RELIABLE_TIMEOUT);
} }
...@@ -156,10 +141,11 @@ StunUsageTimerReturn stun_timer_refresh (StunTimer *timer) ...@@ -156,10 +141,11 @@ StunUsageTimerReturn stun_timer_refresh (StunTimer *timer)
unsigned delay = stun_timer_remainder (timer); unsigned delay = stun_timer_remainder (timer);
if (delay == 0) if (delay == 0)
{ {
if (timer->delay >= STUN_END_TIMEOUT) if (timer->retransmissions >= timer->max_retransmissions)
return STUN_USAGE_TIMER_RETURN_TIMEOUT; return STUN_USAGE_TIMER_RETURN_TIMEOUT;
add_delay (&timer->deadline, timer->delay *= 2); add_delay (&timer->deadline, timer->delay *= 2);
timer->retransmissions++;
return STUN_USAGE_TIMER_RETURN_RETRANSMIT; return STUN_USAGE_TIMER_RETURN_RETRANSMIT;
} }
......
...@@ -62,7 +62,8 @@ ...@@ -62,7 +62,8 @@
// Send the message and start the timer // Send the message and start the timer
send(socket, request, sizeof(request)); send(socket, request, sizeof(request));
stun_timer_start(&timer); stun_timer_start(&timer, STUN_TIMER_DEFAULT_TIMEOUT,
STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
// Loop until we get the response // Loop until we get the response
for (;;) { for (;;) {
...@@ -120,9 +121,32 @@ typedef struct stun_timer_s StunTimer; ...@@ -120,9 +121,32 @@ typedef struct stun_timer_s StunTimer;
struct stun_timer_s { struct stun_timer_s {
struct timeval deadline; struct timeval deadline;
unsigned delay; unsigned delay;
unsigned retransmissions;
unsigned max_retransmissions;
}; };
/**
* STUN_TIMER_DEFAULT_TIMEOUT:
*
* The default intial timeout to use for the timer
*/
#define STUN_TIMER_DEFAULT_TIMEOUT 600
/**
* STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS:
*
* The default maximum retransmissions allowed before a timer decides to timeout
*/
#define STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS 3
/**
* STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT:
*
* The default intial timeout to use for a reliable timer
*/
#define STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT 7900
/** /**
* StunUsageTimerReturn: * StunUsageTimerReturn:
* @STUN_USAGE_TIMER_RETURN_SUCCESS: The timer was refreshed successfully * @STUN_USAGE_TIMER_RETURN_SUCCESS: The timer was refreshed successfully
...@@ -145,15 +169,33 @@ typedef enum { ...@@ -145,15 +169,33 @@ typedef enum {
extern "C" { extern "C" {
# endif # endif
/** /**
* stun_timer_start: * stun_timer_start:
* @timer: The #StunTimer to start * @timer: The #StunTimer to start
* @initial_timeout: The initial timeout to use before the first retransmission
* @max_retransmissions: The maximum number of transmissions before the
* #StunTimer times out
* *
* Starts a STUN transaction retransmission timer. * Starts a STUN transaction retransmission timer.
* This should be called as soon as you send the message for the first time on * This should be called as soon as you send the message for the first time on
* a UDP socket * a UDP socket.
* The timeout before the next retransmission is set to @initial_timeout, then
* each time a packet is retransmited, that timeout will be doubled, until the
* @max_retransmissions retransmissions limit is reached.
* <para>
* To determine the total timeout value, one can use the following equation :
<programlisting>
total_timeout = initial_timeout * (2^(max_retransmissions + 1) - 1);
</programlisting>
* </para>
*
* See also: #STUN_TIMER_DEFAULT_TIMEOUT
*
* See also: #STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS
*/ */
void stun_timer_start (StunTimer *timer); void stun_timer_start (StunTimer *timer, unsigned int initial_timeout,
unsigned int max_retransmissions);
/** /**
* stun_timer_start_reliable: * stun_timer_start_reliable:
...@@ -163,7 +205,7 @@ void stun_timer_start (StunTimer *timer); ...@@ -163,7 +205,7 @@ void stun_timer_start (StunTimer *timer);
* This should be called as soon as you send the message for the first time on * This should be called as soon as you send the message for the first time on
* a TCP socket * a TCP socket
*/ */
void stun_timer_start_reliable (StunTimer *timer); void stun_timer_start_reliable (StunTimer *timer, unsigned int initial_timeout);
/** /**
* stun_timer_refresh: * stun_timer_refresh:
......
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