Skip to main content

endpoint_sec/event/
event_openssh_logout.rs

1//! [`EventOpensshLogout`]
2
3use std::ffi::OsStr;
4use std::net::IpAddr;
5use std::str::FromStr;
6
7use endpoint_sec_sys::{es_address_type_t, es_event_openssh_logout_t, uid_t};
8
9/// OpenSSH logout event.
10#[doc(alias = "es_event_openssh_logout_t")]
11pub struct EventOpensshLogout<'a> {
12    /// Raw event
13    pub(crate) raw: &'a es_event_openssh_logout_t,
14}
15
16impl<'a> EventOpensshLogout<'a> {
17    /// Type of source address.
18    #[inline(always)]
19    pub fn source_address_type(&self) -> es_address_type_t {
20        self.raw.source_address_type
21    }
22
23    /// Source address of connection.
24    #[inline(always)]
25    pub fn source_address(&self) -> &'a OsStr {
26        // Safety: 'a tied to self, object obtained through ES
27        unsafe { self.raw.source_address.as_os_str() }
28    }
29
30    /// Username which got logged out.
31    #[inline(always)]
32    pub fn username(&self) -> &'a OsStr {
33        // Safety: 'a tied to self, object obtained through ES
34        unsafe { self.raw.username.as_os_str() }
35    }
36
37    /// UID of user that was logged out.
38    #[inline(always)]
39    pub fn uid(&self) -> uid_t {
40        self.raw.uid
41    }
42
43    /// Source address as an [`IpAddr`] from the standard library, if possible.
44    #[inline(always)]
45    pub fn source_address_std(&self) -> Option<IpAddr> {
46        let sa = self.source_address().to_str()?;
47        IpAddr::from_str(sa).ok()
48    }
49}
50
51// Safety: safe to send across threads: does not contain any interior mutability nor depend on current thread state
52unsafe impl Send for EventOpensshLogout<'_> {}
53// Safety: safe to share across threads: does not contain any interior mutability nor depend on current thread state
54unsafe impl Sync for EventOpensshLogout<'_> {}
55
56impl_debug_eq_hash_with_functions!(
57    EventOpensshLogout<'a>;
58    source_address_type, source_address, username, uid,
59);