Skip to main content

EncryptedSession

Struct EncryptedSession 

Source
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: i64

Current server salt to include in outgoing messages.

§time_offset: i32

Clock skew in seconds vs. server.

Implementations§

Source§

impl EncryptedSession

Source

pub fn new(auth_key: [u8; 256], first_salt: i64, time_offset: i32) -> Self

Create a new encrypted session from the output of authentication::finish.

Source

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)

Source

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

Source

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)

Source

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)

Source

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).

Source

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).

Source

pub fn pack_serializable<S: Serializable>(&mut self, call: &S) -> Vec<u8>

Serialize and encrypt a TL function into a wire-ready byte vector.

Source

pub fn pack_serializable_with_msg_id<S: Serializable>( &mut self, call: &S, ) -> (Vec<u8>, i64)

Like pack_serializable but also returns the msg_id.

Source

pub fn pack_with_msg_id<R: RemoteCall>(&mut self, call: &R) -> (Vec<u8>, i64)

Like [pack] but also returns the msg_id allocated for this message.

Source

pub fn pack<R: RemoteCall>(&mut self, call: &R) -> Vec<u8>

Encrypt and frame a RemoteCall into a ready-to-send MTProto message.

Source

pub fn unpack(&self, frame: &mut [u8]) -> Result<DecryptedMessage, DecryptError>

Decrypt an encrypted server frame.

Source

pub fn auth_key_bytes(&self) -> [u8; 256]

Return the auth_key bytes (for persistence).

Source

pub fn session_id(&self) -> i64

Return the current session_id.

Source§

impl EncryptedSession

Source

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.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.