ipmi_rs_core/app/auth/
activate_session.rs

1use std::num::NonZeroU32;
2
3use crate::connection::{IpmiCommand, Message, NetFn};
4
5use super::{AuthError, AuthType, PrivilegeLevel};
6
7#[derive(Debug, Clone)]
8pub struct ActivateSession {
9    pub auth_type: AuthType,
10    pub maxiumum_privilege_level: PrivilegeLevel,
11    pub challenge_string: [u8; 16],
12    pub initial_sequence_number: u32,
13}
14
15#[derive(Debug, Clone)]
16pub struct BeginSessionInfo {
17    pub auth_type: AuthType,
18    pub session_id: NonZeroU32,
19    pub initial_sequence_number: u32,
20    pub maximum_privilege_level: PrivilegeLevel,
21}
22
23impl From<ActivateSession> for Message {
24    fn from(value: ActivateSession) -> Self {
25        let mut data = vec![0u8; 22];
26
27        data[0] = value.auth_type.into();
28        data[1] = value.maxiumum_privilege_level.into();
29        data[2..18].copy_from_slice(&value.challenge_string);
30        data[18..22].copy_from_slice(&value.initial_sequence_number.to_le_bytes());
31
32        Message::new_request(NetFn::App, 0x3A, data)
33    }
34}
35
36impl IpmiCommand for ActivateSession {
37    type Output = BeginSessionInfo;
38
39    type Error = AuthError;
40
41    fn parse_success_response(data: &[u8]) -> Result<Self::Output, Self::Error> {
42        if data.len() < 10 {
43            return Err(AuthError::NotEnoughData);
44        }
45
46        let auth_type = data[0]
47            .try_into()
48            .map_err(|_| AuthError::InvalidAuthType(data[0]))?;
49
50        let session_id = NonZeroU32::try_from(u32::from_le_bytes(data[1..5].try_into().unwrap()))
51            .map_err(|_| AuthError::InvalidZeroSession)?;
52        let initial_sequence_number = u32::from_le_bytes(data[5..9].try_into().unwrap());
53        let maximum_privilege_level = data[9]
54            .try_into()
55            .map_err(|_| AuthError::InvalidPrivilegeLevel(data[9]))?;
56
57        Ok(BeginSessionInfo {
58            auth_type,
59            session_id,
60            initial_sequence_number,
61            maximum_privilege_level,
62        })
63    }
64}