Reduce transaction ids independent of host byte order.

Bug: webrtc:9972
Change-Id: I91df2f2c4854bec6d581c3beb9f57235a1ce47b1
Reviewed-on: https://webrtc-review.googlesource.com/c/112926
Reviewed-by: Qingsi Wang <qingsi@webrtc.org>
Reviewed-by: Björn Terelius <terelius@webrtc.org>
Commit-Queue: Zach Stein <zstein@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#25939}
This commit is contained in:
Zach Stein 2018-12-07 11:25:12 -08:00 committed by Commit Bot
parent 168456c128
commit ff71a49b30
2 changed files with 13 additions and 6 deletions

View File

@ -32,13 +32,12 @@ uint32_t ReduceTransactionId(const std::string& transaction_id) {
RTC_DCHECK(transaction_id.length() == cricket::kStunTransactionIdLength ||
transaction_id.length() ==
cricket::kStunLegacyTransactionIdLength);
uint32_t transaction_id_as_ints[4];
memcpy(transaction_id_as_ints, transaction_id.c_str(),
transaction_id.length());
ByteBufferReader reader(transaction_id.c_str(), transaction_id.length(),
rtc::ByteBuffer::ORDER_NETWORK);
uint32_t result = 0;
for (size_t i = 0; i < transaction_id.length() / 4; ++i) {
result ^= transaction_id_as_ints[i];
uint32_t next;
while (reader.ReadUInt32(&next)) {
result ^= next;
}
return result;
}

View File

@ -1561,4 +1561,12 @@ TEST_F(StunTest, CopyAttribute) {
}
}
TEST_F(StunTest, ReduceTransactionIdIsHostOrderIndependent) {
std::string transaction_id = "abcdefghijkl";
StunMessage message;
ASSERT_TRUE(message.SetTransactionID(transaction_id));
uint32_t reduced_transaction_id = message.reduced_transaction_id();
EXPECT_EQ(reduced_transaction_id, 1835954016u);
}
} // namespace cricket