use std::ffi::{OsStr, OsString};
use crate::role::decimal;
pub(crate) const VERSION: u32 = 2;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum CtrlEvent {
CtrlC,
CtrlBreak,
}
impl CtrlEvent {
#[cfg_attr(not(windows), allow(dead_code, reason = "only a Windows stop names its event"))]
pub(crate) fn name(self) -> &'static str {
match self {
CtrlEvent::CtrlC => "CTRL_C",
CtrlEvent::CtrlBreak => "CTRL_BREAK",
}
}
#[cfg_attr(
not(windows),
allow(dead_code, reason = "only the Windows caller side starts a proxy")
)]
fn token(self) -> &'static str {
match self {
CtrlEvent::CtrlC => "c",
CtrlEvent::CtrlBreak => "break",
}
}
fn from_token(token: &str) -> Option<CtrlEvent> {
match token {
"c" => Some(CtrlEvent::CtrlC),
"break" => Some(CtrlEvent::CtrlBreak),
_ => None,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) struct ProxyArgs {
pub(crate) target: u32,
pub(crate) target_key: u64,
pub(crate) event: CtrlEvent,
pub(crate) caller: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
pub(crate) enum ProxyArgsError {
#[error("the caller speaks protocol version {theirs}, this proxy {ours}")]
VersionMismatch { ours: u32, theirs: u32 },
#[error("the first argument is not a protocol version (a decimal number)")]
BadVersion,
#[error("expected a protocol version, a target pid, its key, an event and a caller pid")]
WrongArgumentCount,
#[error("a pid or a key is not a decimal number")]
BadNumber,
#[error("the event is neither `c` nor `break`")]
BadEvent,
}
impl ProxyArgsError {
pub(crate) fn answer(self) -> ProxyAnswer {
match self {
ProxyArgsError::VersionMismatch { .. } => ProxyAnswer::VersionMismatch,
_ => ProxyAnswer::Usage,
}
}
}
impl ProxyArgs {
#[cfg_attr(
not(windows),
allow(dead_code, reason = "only the Windows caller side starts a proxy")
)]
pub(crate) fn to_argv(self, exe: &OsStr) -> Vec<OsString> {
vec![
exe.to_owned(),
VERSION.to_string().into(),
self.target.to_string().into(),
self.target_key.to_string().into(),
self.event.token().into(),
self.caller.to_string().into(),
]
}
pub(crate) fn parse(argv: &[OsString]) -> Result<ProxyArgs, ProxyArgsError> {
let Some(version) = argv.get(1) else {
return Err(ProxyArgsError::WrongArgumentCount);
};
match version.to_str().and_then(|v| decimal::<u32>(v.as_bytes())) {
Some(VERSION) => {}
Some(theirs) => return Err(ProxyArgsError::VersionMismatch { ours: VERSION, theirs }),
None => return Err(ProxyArgsError::BadVersion),
}
let [_exe, _version, target, target_key, event, caller] = argv else {
return Err(ProxyArgsError::WrongArgumentCount);
};
let number = |a: &OsString| {
a.to_str()
.and_then(|a| decimal::<u64>(a.as_bytes()))
.ok_or(ProxyArgsError::BadNumber)
};
let pid = |a: &OsString| u32::try_from(number(a)?).map_err(|_| ProxyArgsError::BadNumber);
let event = event
.to_str()
.and_then(CtrlEvent::from_token)
.ok_or(ProxyArgsError::BadEvent)?;
Ok(ProxyArgs {
target: pid(target)?,
target_key: number(target_key)?,
event,
caller: pid(caller)?,
})
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub(crate) enum ProxyAnswer {
Sent = 150,
Usage = 151,
VersionMismatch = 152,
TargetGone = 153,
TargetHasNoConsole = 154,
ConsoleIsCallers = 155,
NothingSent = 156,
AttachDenied = 157,
Unconfirmed = 158,
NoProxyHere = 159,
TargetUnreadable = 160,
}
impl ProxyAnswer {
pub(crate) const fn code(self) -> i32 {
self as i32
}
#[cfg_attr(
not(windows),
allow(dead_code, reason = "only the Windows caller side reads a proxy's answer")
)]
pub(crate) fn from_code(code: i32) -> Option<ProxyAnswer> {
[
ProxyAnswer::Sent,
ProxyAnswer::Usage,
ProxyAnswer::VersionMismatch,
ProxyAnswer::TargetGone,
ProxyAnswer::TargetHasNoConsole,
ProxyAnswer::ConsoleIsCallers,
ProxyAnswer::NothingSent,
ProxyAnswer::AttachDenied,
ProxyAnswer::Unconfirmed,
ProxyAnswer::NoProxyHere,
ProxyAnswer::TargetUnreadable,
]
.into_iter()
.find(|answer| answer.code() == code)
}
}
#[cfg(test)]
#[path = "protocol_tests.rs"]
mod protocol_tests;