entrust_agent/
lib.rs

1use serde::Serialize;
2use serde::de::DeserializeOwned;
3use std::borrow::Cow;
4use std::io::{BufRead, ErrorKind, Write};
5use std::sync::LazyLock;
6use std::{env, io};
7
8pub mod client;
9pub mod server;
10
11#[cfg(windows)]
12pub const NO_AGENT_ERROR_KIND: ErrorKind = ErrorKind::NotFound;
13#[cfg(not(windows))]
14pub const NO_AGENT_ERROR_KIND: ErrorKind = ErrorKind::ConnectionRefused;
15
16/// ASCII 'End of Transmission'
17const EOT: u8 = 4;
18
19fn send_serialized<R: Serialize, C: Write>(request: &R, con: &mut C) -> io::Result<()> {
20    con.write_all(
21        bincode::serialize(request)
22            .map_err(io::Error::other)?
23            .as_slice(),
24    )?;
25    con.write_all([EOT].as_ref())
26}
27
28fn read_deserialized<R: DeserializeOwned, C: BufRead>(con: &mut C) -> io::Result<R> {
29    let mut buf = Vec::with_capacity(32);
30    con.read_until(EOT, &mut buf)?;
31    buf.pop();
32    bincode::deserialize_from(buf.as_slice()).map_err(io::Error::other)
33}
34
35static SOCKET_NAME: LazyLock<Cow<str>> = LazyLock::new(|| {
36    env::var("ENT_AGENT_SOCKET_NAME")
37        .map(Cow::Owned)
38        .unwrap_or(Cow::Borrowed("entrust-agent.sock"))
39});