Move to 16-bit millisecond timestamp on wire

This commit is contained in:
Keith Winstein
2011-08-26 00:10:23 -04:00
parent e4b716f0d4
commit 86eb159a5b
2 changed files with 36 additions and 17 deletions
+30 -13
View File
@@ -26,13 +26,13 @@ Packet::Packet( string coded_packet, Session *session )
direction = (message.nonce.val() & 0x8000000000000000) ? TO_CLIENT : TO_SERVER; direction = (message.nonce.val() & 0x8000000000000000) ? TO_CLIENT : TO_SERVER;
seq = message.nonce.val() & 0x7FFFFFFFFFFFFFFF; seq = message.nonce.val() & 0x7FFFFFFFFFFFFFFF;
assert( message.text.size() >= 2 * sizeof( uint64_t ) ); assert( message.text.size() >= 2 * sizeof( uint16_t ) );
uint64_t *data = (uint64_t *)message.text.data(); uint16_t *data = (uint16_t *)message.text.data();
timestamp = be64toh( data[ 0 ] ); timestamp = be16toh( data[ 0 ] );
timestamp_reply = be64toh( data[ 1 ] ); timestamp_reply = be16toh( data[ 1 ] );
payload = string( message.text.begin() + 2 * sizeof( uint64_t ), message.text.end() ); payload = string( message.text.begin() + 2 * sizeof( uint16_t ), message.text.end() );
} }
/* Output coded string from packet */ /* Output coded string from packet */
@@ -40,16 +40,16 @@ string Packet::tostring( Session *session )
{ {
uint64_t direction_seq = (uint64_t( direction == TO_CLIENT ) << 63) | (seq & 0x7FFFFFFFFFFFFFFF); uint64_t direction_seq = (uint64_t( direction == TO_CLIENT ) << 63) | (seq & 0x7FFFFFFFFFFFFFFF);
uint64_t ts_net[ 2 ] = { htobe64( timestamp ), htobe64( timestamp_reply ) }; uint16_t ts_net[ 2 ] = { htobe16( timestamp ), htobe16( timestamp_reply ) };
string timestamps = string( (char *)ts_net, 2 * sizeof( uint64_t ) ); string timestamps = string( (char *)ts_net, 2 * sizeof( uint16_t ) );
return session->encrypt( Message( Nonce( direction_seq ), timestamps + payload ) ); return session->encrypt( Message( Nonce( direction_seq ), timestamps + payload ) );
} }
Packet Connection::new_packet( string &s_payload ) Packet Connection::new_packet( string &s_payload )
{ {
Packet p( next_seq++, direction, timestamp(), saved_timestamp, s_payload ); Packet p( next_seq++, direction, timestamp16(), saved_timestamp, s_payload );
saved_timestamp = -1; saved_timestamp = -1;
@@ -223,14 +223,13 @@ string Connection::recv( void )
expected_receiver_seq = p.seq + 1; /* this is security-sensitive because a replay attack could otherwise expected_receiver_seq = p.seq + 1; /* this is security-sensitive because a replay attack could otherwise
screw up the timestamp and targeting */ screw up the timestamp and targeting */
if ( p.timestamp != uint64_t(-1) ) { if ( p.timestamp != uint16_t(-1) ) {
saved_timestamp = p.timestamp; saved_timestamp = p.timestamp;
} }
if ( p.timestamp_reply != uint64_t(-1) ) { if ( p.timestamp_reply != uint16_t(-1) ) {
uint64_t now = timestamp(); uint16_t now = timestamp16();
assert( now >= p.timestamp_reply ); double R = timestamp_diff( now, p.timestamp_reply );
double R = now - p.timestamp_reply;
if ( R < 5000 ) { /* ignore large values, e.g. server was Ctrl-Zed */ if ( R < 5000 ) { /* ignore large values, e.g. server was Ctrl-Zed */
if ( !RTT_hit ) { /* first measurement */ if ( !RTT_hit ) { /* first measurement */
@@ -290,6 +289,24 @@ uint64_t Network::timestamp( void )
return millis; return millis;
} }
uint16_t Network::timestamp16( void )
{
return timestamp() % 65536;
}
uint16_t Network::timestamp_diff( uint16_t tsnew, uint16_t tsold )
{
int diff = tsnew - tsold;
if ( diff < 0 ) {
diff += 65536;
}
assert( diff >= 0 );
assert( diff <= 65535 );
return diff;
}
uint64_t Connection::timeout( void ) uint64_t Connection::timeout( void )
{ {
uint64_t RTO = lrint( ceil( SRTT + 4 * RTTVAR ) ); uint64_t RTO = lrint( ceil( SRTT + 4 * RTTVAR ) );
+6 -4
View File
@@ -15,6 +15,8 @@ using namespace Crypto;
namespace Network { namespace Network {
uint64_t timestamp( void ); uint64_t timestamp( void );
uint16_t timestamp16( void );
uint16_t timestamp_diff( uint16_t tsnew, uint16_t tsold );
class MTUException { class MTUException {
public: public:
@@ -38,11 +40,11 @@ namespace Network {
public: public:
uint64_t seq; uint64_t seq;
Direction direction; Direction direction;
uint64_t timestamp, timestamp_reply; uint16_t timestamp, timestamp_reply;
string payload; string payload;
Packet( uint64_t s_seq, Direction s_direction, Packet( uint64_t s_seq, Direction s_direction,
uint64_t s_timestamp, uint64_t s_timestamp_reply, string s_payload ) uint16_t s_timestamp, uint16_t s_timestamp_reply, string s_payload )
: seq( s_seq ), direction( s_direction ), : seq( s_seq ), direction( s_direction ),
timestamp( s_timestamp ), timestamp_reply( s_timestamp_reply ), payload( s_payload ) timestamp( s_timestamp ), timestamp_reply( s_timestamp_reply ), payload( s_payload )
{} {}
@@ -74,7 +76,7 @@ namespace Network {
Direction direction; Direction direction;
uint64_t next_seq; uint64_t next_seq;
uint64_t saved_timestamp; uint16_t saved_timestamp;
uint64_t expected_receiver_seq; uint64_t expected_receiver_seq;
bool RTT_hit; bool RTT_hit;
@@ -98,7 +100,7 @@ namespace Network {
uint64_t timeout( void ); uint64_t timeout( void );
double get_SRTT( void ) { return SRTT; } double get_SRTT( void ) { return SRTT; }
bool pending_timestamp( void ) { return ( saved_timestamp != uint64_t(-1) ); } bool pending_timestamp( void ) { return ( saved_timestamp != uint16_t(-1) ); }
}; };
} }