Skip to content

Commit 461a1fe

Browse files
Fix cookie contest pseudocode
The cookie contest pseudocode can sometimes produce RESPONDER - RESPONDER type of peers, because bit 31 is used both as a weight in a 64-bit 2's complement integer, and a sign bit. See the discussion here for a detailed brief: Haivision/srt#3208 This should be fixed asap in the RFC such that implementors do not end up with this bug in their protocol stack. We propose to avoid the use of bitmasks to 1) make the intention clearer 2) let the compiler handle the low-level details We also propose to add a statement, reminding implementors of the importance of considering endianness of the host and the network before comparing the cookies. squash
1 parent d44d05f commit 461a1fe

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

draft-sharabayko-srt.md

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1663,8 +1663,7 @@ than its peer's, it wins the cookie contest and becomes Initiator
16631663
(the other party becomes the Responder).
16641664

16651665
The intent is to let the side with the greater cookie value become the Initiator
1666-
(the other party becomes the Responder). However, with a special handling
1667-
of the higher bit of the difference.
1666+
(the other party becomes the Responder).
16681667

16691668
<CODE BEGINS>
16701669
enum ContestResult
@@ -1680,16 +1679,22 @@ of the higher bit of the difference.
16801679
// host - Local (host) cookie value.
16811680
int64_t contest = int64_t(host) - int64_t(peer);
16821681

1683-
if ((contest & 0xFFFFFFFF) == 0)
1682+
if (contest == 0)
16841683
return DRAW;
16851684

1686-
if (contest & 0x80000000)
1685+
if (contest < 0)
16871686
return RESPONDER;
16881687

16891688
return INITIATOR;
16901689
}
16911690
<CODE ENDS>
16921691

1692+
Note that implementations must:
1693+
- Treat cookies as signed integers in 2s complement format.
1694+
- The cookies must be sign-expanded to 64 bit signed integers prior to the contest.
1695+
- Implementations must consider network and host endianness and perform conversions as
1696+
appropriate.
1697+
16931698
#### The Waving State {#waving-state}
16941699

16951700
Both parties start in a Waving state.

0 commit comments

Comments
 (0)