Skip to content

Commit

Permalink
Fix expiry of nonces
Browse files Browse the repository at this point in the history
Don't fail to expire nonces when the ?NONCE_LIFETIME has passed.
  • Loading branch information
weiss committed Jul 20, 2023
1 parent 17512c4 commit 9abcafa
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
# Version 1.2.8

* Fix expiry of nonces.

# Version 1.2.7

* Handle transport autodetection error gracefully
Expand Down
6 changes: 3 additions & 3 deletions src/stun.erl
Original file line number Diff line number Diff line change
Expand Up @@ -681,7 +681,7 @@ clean_treap(Treap, CleanPriority) ->
Treap;
false ->
{_Key, {TS, _}, _Value} = treap:get_root(Treap),
if TS > CleanPriority ->
if TS < CleanPriority ->
clean_treap(treap:delete_root(Treap), CleanPriority);
true ->
Treap
Expand All @@ -692,12 +692,12 @@ make_nonce(Addr, Nonces) ->
Priority = now_priority(),
{TS, _} = Priority,
Nonce = integer_to_binary(rand_uniform(1 bsl 32)),
NewNonces = clean_treap(Nonces, TS + ?NONCE_LIFETIME),
NewNonces = clean_treap(Nonces, TS - ?NONCE_LIFETIME),
{Nonce, treap:insert(Nonce, Priority, Addr, NewNonces)}.

have_nonce(Nonce, Nonces) ->
TS = p1_time_compat:monotonic_time(micro_seconds),
NewNonces = clean_treap(Nonces, TS + ?NONCE_LIFETIME),
NewNonces = clean_treap(Nonces, TS - ?NONCE_LIFETIME),
case treap:lookup(Nonce, NewNonces) of
{ok, _, _} ->
{true, NewNonces};
Expand Down

0 comments on commit 9abcafa

Please sign in to comment.