pub struct EncryptedSession {
pub salt: i64,
pub time_offset: i32,
/* private fields */
}Expand description
MTProto 2.0 encrypted session state.
Wraps an AuthKey and tracks per-session counters (session_id, seq_no,
last_msg_id, server salt). Use EncryptedSession::pack to encrypt
outgoing requests and EncryptedSession::unpack to decrypt incoming
server frames.
Fields§
§salt: i64Current server salt to include in outgoing messages.
time_offset: i32Clock skew in seconds vs. server.
Implementations§
Source§impl EncryptedSession
impl EncryptedSession
Sourcepub fn new(
auth_key: [u8; 256],
first_salt: i64,
time_offset: i32,
) -> EncryptedSession
pub fn new( auth_key: [u8; 256], first_salt: i64, time_offset: i32, ) -> EncryptedSession
Create a new encrypted session from the output of authentication::finish.
Sourcepub fn next_seq_no_ncr(&self) -> i32
pub fn next_seq_no_ncr(&self) -> i32
Return the current even seq_no WITHOUT advancing the counter.
Service messages (MsgsAck, containers, etc.) MUST use an even seqno
per the MTProto spec so the server does not expect a reply.
grammers reference: mtp/encrypted.rs: get_seq_no(content_related: bool)
Sourcepub fn correct_seq_no(&mut self, code: u32)
pub fn correct_seq_no(&mut self, code: u32)
Correct the outgoing sequence counter when the server reports a
bad_msg_notification with error codes 32 (seq_no too low) or
33 (seq_no too high).
grammers reference: mtp/encrypted.rs: handle_bad_notification() codes 32/33
Sourcepub fn correct_time_offset(&mut self, server_msg_id: i64)
pub fn correct_time_offset(&mut self, server_msg_id: i64)
Re-derive the clock skew from a server-provided msg_id.
Called on bad_msg_notification error codes 16 (msg_id too low) and
17 (msg_id too high) so clock drift is corrected at any point in the
session, not only at connect time.
grammers reference: mtp/encrypted.rs: correct_time_offset(msg_id)
Sourcepub fn alloc_msg_seqno(&mut self, content_related: bool) -> (i64, i32)
pub fn alloc_msg_seqno(&mut self, content_related: bool) -> (i64, i32)
Allocate a fresh (msg_id, seqno) pair for an inner container message
WITHOUT encrypting anything.
content_related = true → odd seqno, advances counter (regular RPCs)
content_related = false → even seqno, no advance (MsgsAck, container)
grammers reference: mtp/encrypted.rs: get_seq_no(content_related)
Sourcepub fn pack_body_with_msg_id(
&mut self,
body: &[u8],
content_related: bool,
) -> (Vec<u8>, i64)
pub fn pack_body_with_msg_id( &mut self, body: &[u8], content_related: bool, ) -> (Vec<u8>, i64)
Encrypt a pre-serialized TL body into a wire-ready MTProto frame.
content_related controls whether the seqno is odd (content, advances
the counter) or even (service, no advance).
Returns (encrypted_wire_bytes, msg_id).
Used for G-02 (bad_msg re-send) and G-07 (container inner messages).
Sourcepub fn pack_container(&mut self, container_body: &[u8]) -> Vec<u8> ⓘ
pub fn pack_container(&mut self, container_body: &[u8]) -> Vec<u8> ⓘ
Encrypt a pre-built msg_container body (the container itself is
a non-content-related message with an even seqno).
Returns encrypted_wire_bytes.
Used for G-07 (message container batching).
Sourcepub fn pack_serializable<S>(&mut self, call: &S) -> Vec<u8> ⓘwhere
S: Serializable,
pub fn pack_serializable<S>(&mut self, call: &S) -> Vec<u8> ⓘwhere
S: Serializable,
Serialize and encrypt a TL function into a wire-ready byte vector.
Sourcepub fn pack_serializable_with_msg_id<S>(&mut self, call: &S) -> (Vec<u8>, i64)where
S: Serializable,
pub fn pack_serializable_with_msg_id<S>(&mut self, call: &S) -> (Vec<u8>, i64)where
S: Serializable,
Like pack_serializable but also returns the msg_id.
Sourcepub fn pack_with_msg_id<R>(&mut self, call: &R) -> (Vec<u8>, i64)where
R: RemoteCall,
pub fn pack_with_msg_id<R>(&mut self, call: &R) -> (Vec<u8>, i64)where
R: RemoteCall,
Like [pack] but also returns the msg_id allocated for this message.
Sourcepub fn pack<R>(&mut self, call: &R) -> Vec<u8> ⓘwhere
R: RemoteCall,
pub fn pack<R>(&mut self, call: &R) -> Vec<u8> ⓘwhere
R: RemoteCall,
Encrypt and frame a RemoteCall into a ready-to-send MTProto message.
Sourcepub fn unpack(&self, frame: &mut [u8]) -> Result<DecryptedMessage, DecryptError>
pub fn unpack(&self, frame: &mut [u8]) -> Result<DecryptedMessage, DecryptError>
Decrypt an encrypted server frame.
Sourcepub fn auth_key_bytes(&self) -> [u8; 256]
pub fn auth_key_bytes(&self) -> [u8; 256]
Return the auth_key bytes (for persistence).
Sourcepub fn session_id(&self) -> i64
pub fn session_id(&self) -> i64
Return the current session_id.
Source§impl EncryptedSession
impl EncryptedSession
Sourcepub fn decrypt_frame(
auth_key: &[u8; 256],
session_id: i64,
frame: &mut [u8],
) -> Result<DecryptedMessage, DecryptError>
pub fn decrypt_frame( auth_key: &[u8; 256], session_id: i64, frame: &mut [u8], ) -> Result<DecryptedMessage, DecryptError>
Decrypt a frame using explicit key + session_id — no mutable state needed. Used by the split-reader task so it can decrypt without locking the writer.