66//
77#include " td/mtproto/AuthData.h"
88
9- #include " td/utils/format.h"
109#include " td/utils/logging.h"
1110#include " td/utils/Random.h"
1211#include " td/utils/SliceBuilder.h"
1716namespace td {
1817namespace mtproto {
1918
20- Status check_message_id_duplicates (uint64 *saved_message_ids, size_t max_size, size_t &end_pos, uint64 message_id) {
19+ Status check_message_id_duplicates (MessageId *saved_message_ids, size_t max_size, size_t &end_pos,
20+ MessageId message_id) {
2121 // In addition, the identifiers (msg_id) of the last N messages received from the other side must be stored, and if
2222 // a message comes in with msg_id lower than all or equal to any of the stored values, that message is to be
2323 // ignored. Otherwise, the new message msg_id is added to the set, and, if the number of stored msg_id values is
@@ -32,13 +32,12 @@ Status check_message_id_duplicates(uint64 *saved_message_ids, size_t max_size, s
3232 return Status::OK ();
3333 }
3434 if (end_pos >= max_size && message_id < saved_message_ids[0 ]) {
35- return Status::Error (2 , PSLICE () << " Ignore very old message " << format::as_hex (message_id)
36- << " older than the oldest known message "
37- << format::as_hex (saved_message_ids[0 ]));
35+ return Status::Error (
36+ 2 , PSLICE () << " Ignore very old " << message_id << " older than the oldest known " << saved_message_ids[0 ]);
3837 }
3938 auto it = std::lower_bound (&saved_message_ids[0 ], &saved_message_ids[end_pos], message_id);
4039 if (*it == message_id) {
41- return Status::Error (1 , PSLICE () << " Ignore already processed message " << format::as_hex ( message_id) );
40+ return Status::Error (1 , PSLICE () << " Ignore already processed " << message_id);
4241 }
4342 std::copy_backward (it, &saved_message_ids[end_pos], &saved_message_ids[end_pos + 1 ]);
4443 *it = message_id;
@@ -105,39 +104,39 @@ std::vector<ServerSalt> AuthData::get_future_salts() const {
105104 return res;
106105}
107106
108- uint64 AuthData::next_message_id (double now) {
107+ MessageId AuthData::next_message_id (double now) {
109108 double server_time = get_server_time (now);
110109 auto t = static_cast <uint64>(server_time * (static_cast <uint64>(1 ) << 32 ));
111110
112111 // randomize lower bits for clocks with low precision
113112 // TODO(perf) do not do this for systems with good precision?..
114113 auto rx = Random::secure_int32 ();
115114 auto to_xor = rx & ((1 << 22 ) - 1 );
116- auto to_mul = ((rx >> 22 ) & 1023 ) + 1 ;
117115
118116 t ^= to_xor;
119- auto result = t & static_cast <uint64>(-4 );
117+ auto result = MessageId ( t & static_cast <uint64>(-4 ) );
120118 if (last_message_id_ >= result) {
121- result = last_message_id_ + 8 * to_mul;
119+ auto to_mul = ((rx >> 22 ) & 1023 ) + 1 ;
120+ result = MessageId (last_message_id_.get () + 8 * to_mul);
122121 }
123- LOG (DEBUG ) << " Create message identifier " << format::as_hex ( result) << " at " << now;
122+ LOG (DEBUG ) << " Create identifier for " << result << " at " << now;
124123 last_message_id_ = result;
125124 return result;
126125}
127126
128- bool AuthData::is_valid_outbound_msg_id (uint64 message_id, double now) const {
127+ bool AuthData::is_valid_outbound_msg_id (MessageId message_id, double now) const {
129128 double server_time = get_server_time (now);
130- auto id_time = static_cast <double >(message_id) / static_cast <double >(static_cast <uint64>(1 ) << 32 );
129+ auto id_time = static_cast <double >(message_id. get () ) / static_cast <double >(static_cast <uint64>(1 ) << 32 );
131130 return server_time - 150 < id_time && id_time < server_time + 30 ;
132131}
133132
134- bool AuthData::is_valid_inbound_msg_id (uint64 message_id, double now) const {
133+ bool AuthData::is_valid_inbound_msg_id (MessageId message_id, double now) const {
135134 double server_time = get_server_time (now);
136- auto id_time = static_cast <double >(message_id) / static_cast <double >(static_cast <uint64>(1 ) << 32 );
135+ auto id_time = static_cast <double >(message_id. get () ) / static_cast <double >(static_cast <uint64>(1 ) << 32 );
137136 return server_time - 300 < id_time && id_time < server_time + 30 ;
138137}
139138
140- Status AuthData::check_packet (uint64 session_id, uint64 message_id, double now, bool &time_difference_was_updated) {
139+ Status AuthData::check_packet (uint64 session_id, MessageId message_id, double now, bool &time_difference_was_updated) {
141140 // Client is to check that the session_id field in the decrypted message indeed equals to that of an active session
142141 // created by the client.
143142 if (get_session_id () != session_id) {
@@ -147,22 +146,21 @@ Status AuthData::check_packet(uint64 session_id, uint64 message_id, double now,
147146
148147 // Client must check that msg_id has even parity for messages from client to server, and odd parity for messages
149148 // from server to client.
150- if ((message_id & 1 ) == 0 ) {
151- return Status::Error (PSLICE () << " Receive invalid message identifier " << format::as_hex ( message_id) );
149+ if ((message_id. get () & 1 ) == 0 ) {
150+ return Status::Error (PSLICE () << " Receive invalid " << message_id);
152151 }
153152
154153 TRY_STATUS (duplicate_checker_.check (message_id));
155154
156- LOG (DEBUG ) << " Receive packet " << format::as_hex (message_id) << " from session " << format::as_hex (session_id)
157- << " at " << now;
158- time_difference_was_updated = update_server_time_difference (static_cast <uint32>(message_id >> 32 ) - now);
155+ LOG (DEBUG ) << " Receive packet in " << message_id << " from session " << session_id << " at " << now;
156+ time_difference_was_updated = update_server_time_difference (static_cast <uint32>(message_id.get () >> 32 ) - now);
159157
160158 // In addition, msg_id values that belong over 30 seconds in the future or over 300 seconds in the past are to be
161159 // ignored (recall that msg_id approximately equals unixtime * 2^32). This is especially important for the server.
162160 // The client would also find this useful (to protect from a replay attack), but only if it is certain of its time
163161 // (for example, if its time has been synchronized with that of the server).
164162 if (server_time_difference_was_updated_ && !is_valid_inbound_msg_id (message_id, now)) {
165- return Status::Error (PSLICE () << " Ignore too old or too new message " << format::as_hex ( message_id) );
163+ return Status::Error (PSLICE () << " Ignore too old or too new " << message_id);
166164 }
167165
168166 return Status::OK ();
0 commit comments