mirror of
https://github.com/facebookincubator/mvfst.git
synced 2025-11-24 04:01:07 +03:00
Functionality to encode/decode retry tokens
Summary: This diff defines functionality to encode and decode un-encrypted retry tokens. Reviewed By: mjoras Differential Revision: D21073343 fbshipit-source-id: 79c2e8ba3c743441edd4b162da69cc5e69873dd2
This commit is contained in:
committed by
Facebook GitHub Bot
parent
04c6d70a3b
commit
84c1ed50f0
@@ -128,6 +128,7 @@ enum class TransportErrorCode : uint16_t {
|
|||||||
INVALID_MIGRATION = 0x000C,
|
INVALID_MIGRATION = 0x000C,
|
||||||
CRYPTO_ERROR = 0x100,
|
CRYPTO_ERROR = 0x100,
|
||||||
CRYPTO_ERROR_MAX = 0x1ff,
|
CRYPTO_ERROR_MAX = 0x1ff,
|
||||||
|
INVALID_TOKEN = 0xb,
|
||||||
};
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -147,6 +147,8 @@ std::string toString(TransportErrorCode code) {
|
|||||||
return "Invalid migration";
|
return "Invalid migration";
|
||||||
case TransportErrorCode::SERVER_BUSY:
|
case TransportErrorCode::SERVER_BUSY:
|
||||||
return "Server busy";
|
return "Server busy";
|
||||||
|
case TransportErrorCode::INVALID_TOKEN:
|
||||||
|
return "Invalid token";
|
||||||
case TransportErrorCode::CRYPTO_ERROR:
|
case TransportErrorCode::CRYPTO_ERROR:
|
||||||
return cryptoErrorToString(code);
|
return cryptoErrorToString(code);
|
||||||
case TransportErrorCode::CRYPTO_ERROR_MAX:
|
case TransportErrorCode::CRYPTO_ERROR_MAX:
|
||||||
|
|||||||
@@ -649,6 +649,46 @@ HandshakeDoneFrame decodeHandshakeDoneFrame(folly::io::Cursor& /*cursor*/) {
|
|||||||
return HandshakeDoneFrame();
|
return HandshakeDoneFrame();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
folly::Expected<RetryToken, TransportErrorCode> parsePlaintextRetryToken(
|
||||||
|
folly::io::Cursor& cursor) {
|
||||||
|
// Read in the length of the odcid.
|
||||||
|
if (!cursor.canAdvance(sizeof(uint8_t))) {
|
||||||
|
return folly::makeUnexpected(TransportErrorCode::INVALID_TOKEN);
|
||||||
|
}
|
||||||
|
auto odcidLen = cursor.readBE<uint8_t>();
|
||||||
|
|
||||||
|
// Read in the odcid.
|
||||||
|
if (!cursor.canAdvance(odcidLen)) {
|
||||||
|
return folly::makeUnexpected(TransportErrorCode::INVALID_TOKEN);
|
||||||
|
}
|
||||||
|
ConnectionId connId(cursor, odcidLen);
|
||||||
|
|
||||||
|
// Read in the port.
|
||||||
|
if (!cursor.canAdvance(sizeof(uint16_t))) {
|
||||||
|
return folly::makeUnexpected(TransportErrorCode::INVALID_TOKEN);
|
||||||
|
}
|
||||||
|
auto clientPort = cursor.readBE<uint16_t>();
|
||||||
|
|
||||||
|
// Read in the length of the client ip address.
|
||||||
|
if (!cursor.canAdvance(sizeof(uint8_t))) {
|
||||||
|
return folly::makeUnexpected(TransportErrorCode::INVALID_TOKEN);
|
||||||
|
}
|
||||||
|
uint16_t ipAddrLen = cursor.readBE<uint8_t>();
|
||||||
|
|
||||||
|
if (!cursor.canAdvance(ipAddrLen)) {
|
||||||
|
return folly::makeUnexpected(TransportErrorCode::INVALID_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Read in the ip address.
|
||||||
|
std::string ipAddressStr = cursor.readFixedString(ipAddrLen);
|
||||||
|
auto ipAddress = folly::IPAddress::tryFromString(ipAddressStr);
|
||||||
|
if (!ipAddress.hasValue()) {
|
||||||
|
return folly::makeUnexpected(TransportErrorCode::INVALID_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
|
return RetryToken(connId, *ipAddress, clientPort);
|
||||||
|
}
|
||||||
|
|
||||||
QuicFrame parseFrame(
|
QuicFrame parseFrame(
|
||||||
BufQueue& queue,
|
BufQueue& queue,
|
||||||
const PacketHeader& header,
|
const PacketHeader& header,
|
||||||
|
|||||||
@@ -133,6 +133,9 @@ ReadNewTokenFrame decodeNewTokenFrame(folly::io::Cursor& cursor);
|
|||||||
|
|
||||||
HandshakeDoneFrame decodeHandshakeDoneFrame(folly::io::Cursor& cursor);
|
HandshakeDoneFrame decodeHandshakeDoneFrame(folly::io::Cursor& cursor);
|
||||||
|
|
||||||
|
folly::Expected<RetryToken, TransportErrorCode> parsePlaintextRetryToken(
|
||||||
|
folly::io::Cursor& cursor);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Parse the Invariant fields in Long Header.
|
* Parse the Invariant fields in Long Header.
|
||||||
*
|
*
|
||||||
|
|||||||
@@ -737,5 +737,31 @@ TEST_F(DecodeTest, DecodeExpiredStreamDataFrame) {
|
|||||||
EXPECT_EQ(result.minimumStreamOffset, 100);
|
EXPECT_EQ(result.minimumStreamOffset, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_F(DecodeTest, ParsePlaintextRetryToken) {
|
||||||
|
ConnectionId odcid = getTestConnectionId();
|
||||||
|
folly::IPAddress clientIp("109.115.3.49");
|
||||||
|
uint16_t clientPort = 42069;
|
||||||
|
RetryToken retryToken(odcid, clientIp, clientPort);
|
||||||
|
Buf plaintextRetryToken = retryToken.getPlaintextToken();
|
||||||
|
|
||||||
|
folly::io::Cursor cursor(plaintextRetryToken.get());
|
||||||
|
auto parseResult = parsePlaintextRetryToken(cursor);
|
||||||
|
|
||||||
|
EXPECT_TRUE(parseResult.hasValue());
|
||||||
|
EXPECT_EQ(parseResult->originalDstConnId, odcid);
|
||||||
|
EXPECT_EQ(parseResult->clientIp, clientIp);
|
||||||
|
EXPECT_EQ(parseResult->clientPort, clientPort);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DecodeTest, ParsePlaintextRetryTokenMalformed) {
|
||||||
|
Buf plaintextRetryToken = folly::IOBuf::copyBuffer("This is some garbage");
|
||||||
|
|
||||||
|
folly::io::Cursor cursor(plaintextRetryToken.get());
|
||||||
|
auto parseResult = parsePlaintextRetryToken(cursor);
|
||||||
|
|
||||||
|
EXPECT_TRUE(parseResult.hasError());
|
||||||
|
EXPECT_EQ(parseResult.error(), TransportErrorCode::INVALID_TOKEN);
|
||||||
|
}
|
||||||
|
|
||||||
} // namespace test
|
} // namespace test
|
||||||
} // namespace quic
|
} // namespace quic
|
||||||
|
|||||||
Reference in New Issue
Block a user