summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYouness Alaoui <youness.alaoui@collabora.co.uk>2010-09-29 00:08:42 -0400
committerYouness Alaoui <youness.alaoui@collabora.co.uk>2010-09-29 00:08:45 -0400
commitf159c3aa7a57d971f4c9cc37aae300c19830ea3f (patch)
tree053b9401e60ee61f1c7f1b0d2f30c197d2241e53
parentea418a22ecd069211717a2697e1b99c69f470411 (diff)
downloadlibnice-f159c3aa7a57d971f4c9cc37aae300c19830ea3f.tar.gz
Make stun_timer timeouts configurable (breaks API)
-rw-r--r--agent/conncheck.c9
-rw-r--r--agent/discovery.c3
-rw-r--r--docs/reference/libnice/libnice-sections.txt3
-rw-r--r--socket/turn.c6
-rw-r--r--stun/usages/bind.c6
-rw-r--r--stun/usages/timer.c34
-rw-r--r--stun/usages/timer.h50
7 files changed, 75 insertions, 36 deletions
diff --git a/agent/conncheck.c b/agent/conncheck.c
index 5079530..211eed8 100644
--- a/agent/conncheck.c
+++ b/agent/conncheck.c
@@ -605,7 +605,8 @@ static gboolean priv_conn_keepalive_tick_unlocked (NiceAgent *agent)
agent, buf_len, p->keepalive.stun_message.buffer);
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;
@@ -823,7 +824,8 @@ static void priv_turn_allocate_refresh_tick_unlocked (CandidateRefresh *cand)
}
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 */
nice_socket_send (cand->nicesock, &cand->server,
@@ -1642,7 +1644,8 @@ int conn_check_send (NiceAgent *agent, CandidateCheckPair *pair)
}
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 */
nice_socket_send (pair->local->sockptr, &pair->remote->addr,
diff --git a/agent/discovery.c b/agent/discovery.c
index ea40035..c8cf976 100644
--- a/agent/discovery.c
+++ b/agent/discovery.c
@@ -886,7 +886,8 @@ static gboolean priv_discovery_tick_unlocked (gpointer pointer)
}
if (buffer_len > 0) {
- stun_timer_start (&cand->timer);
+ stun_timer_start (&cand->timer, 200,
+ STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
/* send the conncheck */
nice_socket_send (cand->nicesock, &cand->server,
diff --git a/docs/reference/libnice/libnice-sections.txt b/docs/reference/libnice/libnice-sections.txt
index 073a68c..f5d213f 100644
--- a/docs/reference/libnice/libnice-sections.txt
+++ b/docs/reference/libnice/libnice-sections.txt
@@ -205,6 +205,9 @@ stun_usage_ice_conncheck_use_candidate
<TITLE>Timer</TITLE>
StunTimer
StunUsageTimerReturn
+STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS
+STUN_TIMER_DEFAULT_RELIABLE_TIMEOUT
+STUN_TIMER_DEFAULT_TIMEOUT
stun_timer_start
stun_timer_start_reliable
stun_timer_refresh
diff --git a/socket/turn.c b/socket/turn.c
index 7837d5c..0c76866 100644
--- a/socket/turn.c
+++ b/socket/turn.c
@@ -697,9 +697,11 @@ priv_send_turn_message (TurnPriv *priv, TURNMessage *msg)
stun_len, (gchar *)msg->buffer);
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 {
- stun_timer_start (&msg->timer);
+ stun_timer_start (&msg->timer, STUN_TIMER_DEFAULT_TIMEOUT,
+ STUN_TIMER_DEFAULT_MAX_RETRANSMISSIONS);
}
priv->current_binding_msg = msg;
diff --git a/stun/usages/bind.c b/stun/usages/bind.c
index cef9051..ba0b6fb 100644
--- a/stun/usages/bind.c
+++ b/stun/usages/bind.c
@@ -460,7 +460,8 @@ StunUsageBindReturn stun_usage_bind_run (const struct sockaddr *srv,
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_timer_remainder (&timer));
@@ -516,7 +517,8 @@ StunUsageBindReturn stun_usage_bind_run (const struct sockaddr *srv,
if (val < -1)
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;
} else if (bind_ret == STUN_USAGE_BIND_RETURN_INVALID) {
ret = STUN_USAGE_TRANS_RETURN_RETRY;
diff --git a/stun/usages/timer.c b/stun/usages/timer.c
index 4b82311..62655b9 100644
--- a/stun/usages/timer.c
+++ b/stun/usages/timer.c
@@ -51,23 +51,6 @@
#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.
* STUN requires a monotonic 1kHz clock to operate properly.
@@ -99,7 +82,7 @@ static void stun_gettime (struct timeval *now)
{ // fallback to wall clock
gettimeofday (now, NULL);
}
-#endif
+#endif
}
@@ -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);
- 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);
- add_delay (&timer->deadline, timer->delay = STUN_RELIABLE_TIMEOUT);
+ stun_timer_start (timer, initial_timeout, 0);
}
@@ -156,10 +141,11 @@ StunUsageTimerReturn stun_timer_refresh (StunTimer *timer)
unsigned delay = stun_timer_remainder (timer);
if (delay == 0)
{
- if (timer->delay >= STUN_END_TIMEOUT)
+ if (timer->retransmissions >= timer->max_retransmissions)
return STUN_USAGE_TIMER_RETURN_TIMEOUT;
add_delay (&timer->deadline, timer->delay *= 2);
+ timer->retransmissions++;
return STUN_USAGE_TIMER_RETURN_RETRANSMIT;
}
diff --git a/stun/usages/timer.h b/stun/usages/timer.h
index 760f563..e8f0786 100644
--- a/stun/usages/timer.h
+++ b/stun/usages/timer.h
@@ -62,7 +62,8 @@
// Send the message and start the timer
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
for (;;) {
@@ -120,10 +121,33 @@ typedef struct stun_timer_s StunTimer;
struct stun_timer_s {
struct timeval deadline;
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:
* @STUN_USAGE_TIMER_RETURN_SUCCESS: The timer was refreshed successfully
* and there is nothing to be done
@@ -145,15 +169,33 @@ typedef enum {
extern "C" {
# endif
+
/**
* stun_timer_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.
* 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:
@@ -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
* a TCP socket
*/
-void stun_timer_start_reliable (StunTimer *timer);
+void stun_timer_start_reliable (StunTimer *timer, unsigned int initial_timeout);
/**
* stun_timer_refresh: