2 * This file is part of the Nice GLib ICE library.
4 * (C) 2007 Nokia Corporation. All rights reserved.
5 * Contact: Rémi Denis-Courmont
7 * The contents of this file are subject to the Mozilla Public License Version
8 * 1.1 (the "License"); you may not use this file except in compliance with
9 * the License. You may obtain a copy of the License at
10 * http://www.mozilla.org/MPL/
12 * Software distributed under the License is distributed on an "AS IS" basis,
13 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 * for the specific language governing rights and limitations under the
17 * The Original Code is the Nice GLib ICE library.
19 * The Initial Developers of the Original Code are Collabora Ltd and Nokia
20 * Corporation. All Rights Reserved.
23 * Rémi Denis-Courmont, Nokia
25 * Alternatively, the contents of this file may be used under the terms of the
26 * the GNU Lesser General Public License Version 2.1 (the "LGPL"), in which
27 * case the provisions of LGPL are applicable instead of those above. If you
28 * wish to allow use of your version of this file only under the terms of the
29 * LGPL and not to allow others to use your version of this file under the
30 * MPL, indicate your decision by deleting the provisions above and replace
31 * them with the notice and other provisions required by the LGPL. If you do
32 * not delete the provisions above, a recipient may use your version of this
33 * file under either the MPL or the LGPL.
42 #include <stdlib.h> /* div() */
45 #include <sys/types.h>
51 * Initial STUN timeout (milliseconds). The spec says it should be 100ms,
52 * but that's way too short for most types of wireless Internet access.
54 #define STUN_INIT_TIMEOUT 600
55 #define STUN_END_TIMEOUT 4800
57 #define STUN_RELIABLE_TIMEOUT 7900
60 * Clock used throughout the STUN code.
61 * STUN requires a monotonic 1kHz clock to operate properly.
63 static void stun_gettime (struct timespec
*restrict now
)
65 #if (_POSIX_MONOTONIC_CLOCK - 0) >= 0
66 if (clock_gettime (CLOCK_MONOTONIC
, now
))
68 { // fallback to wall clock
70 gettimeofday (&tv
, NULL
);
71 now
->tv_sec
= tv
.tv_sec
;
72 now
->tv_nsec
= tv
.tv_usec
* 1000;
77 static inline void add_delay (struct timespec
*ts
, unsigned delay
)
79 div_t d
= div (delay
, 1000);
81 ts
->tv_nsec
+= d
.rem
* 1000000;
83 while (ts
->tv_nsec
> 1000000000)
85 ts
->tv_nsec
-= 1000000000;
91 void stun_timer_start (stun_timer_t
*timer
)
93 stun_gettime (&timer
->deadline
);
94 add_delay (&timer
->deadline
, timer
->delay
= STUN_INIT_TIMEOUT
);
98 void stun_timer_start_reliable (stun_timer_t
*timer
)
100 stun_gettime (&timer
->deadline
);
101 add_delay (&timer
->deadline
, timer
->delay
= STUN_RELIABLE_TIMEOUT
);
106 unsigned stun_timer_remainder (const stun_timer_t
*timer
)
112 if (now
.tv_sec
> timer
->deadline
.tv_sec
)
115 delay
= timer
->deadline
.tv_sec
- now
.tv_sec
;
116 if ((delay
== 0) && (now
.tv_nsec
>= timer
->deadline
.tv_nsec
))
120 delay
+= ((signed)(timer
->deadline
.tv_nsec
- now
.tv_nsec
)) / 1000000;
125 int stun_timer_refresh (stun_timer_t
*timer
)
127 unsigned delay
= stun_timer_remainder (timer
);
130 #if STUN_RELIABLE_TIMEOUT < STUN_END_TIMEOUT
131 /* Reliable timeout MUST be bigger (or equal) to end timeout, so that
132 * retransmissions never happen with reliable transports. */
133 # error Inconsistent STUN timeout values!
135 if (timer
->delay
>= STUN_END_TIMEOUT
)
138 add_delay (&timer
->deadline
, timer
->delay
*= 2);