use alloc::collections::{BTreeMap, VecDeque};
use alloc::string::String;
use alloc::vec::Vec;
use super::{ConfirmedRequest, ConfirmedResponse, Initiate, Mms, ServiceError, service_error};
use crate::ber::{Cursor, Encoder, Tag};
use crate::common::{Error, EventQueue, Instant, Limits, Result};
use crate::proto::osi::acse::{self, Apdu, Associate};
use crate::proto::osi::cotp::{self, Tpdu, Tsel};
use crate::proto::osi::presentation::{ContextDefinition, ContextResult, Cp, Pdv, Ppdu, RESULT_ACCEPTANCE};
use crate::proto::osi::session::{self, Spdu};
use crate::proto::osi::{Oid, oid, tpkt};
pub const ACSE_CONTEXT: u16 = 1;
pub const MMS_CONTEXT: u16 = 3;
pub const PORT: u16 = 102;
const LOCAL_REF: u16 = 1;
pub const PORT_TLS: u16 = 3782;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Role {
Client,
Server,
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Selectors {
pub t_sel: Vec<u8>,
pub s_sel: Vec<u8>,
pub p_sel: Vec<u8>,
pub ap_title: Option<Vec<u32>>,
pub ae_qualifier: Option<i64>,
}
impl Selectors {
pub fn defaults() -> Selectors {
Selectors { t_sel: alloc::vec![0x00, 0x01], ..Selectors::default() }
}
pub fn from_address(a: &crate::model::OsiAddress) -> Selectors {
Selectors {
t_sel: a.t_sel.clone().unwrap_or_default(),
s_sel: a.s_sel.clone().unwrap_or_default(),
p_sel: a.p_sel.clone().unwrap_or_default(),
ap_title: a.ap_title.clone(),
ae_qualifier: a.ae_qualifier,
}
}
fn ap_title_element(&self) -> Option<Vec<u8>> {
let arcs = self.ap_title.as_ref()?;
let contents = oid::encode(arcs)?;
let mut e = Encoder::new();
e.primitive(Tag::universal(crate::ber::universal::OID, false), &contents).ok()?;
Some(e.into_vec())
}
fn ae_qualifier_element(&self) -> Option<Vec<u8>> {
let q = self.ae_qualifier?;
let mut e = Encoder::new();
e.integer(Tag::universal(crate::ber::universal::INTEGER, false), q).ok()?;
Some(e.into_vec())
}
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct AssociationConfig {
pub local: Selectors,
pub remote: Selectors,
pub max_pdu: u32,
pub tpdu_size_exp: u8,
pub max_outstanding: u8,
pub password: Option<String>,
pub connect_timeout_ms: u64,
pub request_timeout_ms: u64,
pub max_tsdu: usize,
pub limits: Limits,
pub event_capacity: usize,
}
impl Default for AssociationConfig {
fn default() -> AssociationConfig {
AssociationConfig {
local: Selectors::defaults(),
remote: Selectors::defaults(),
max_pdu: 64_000,
tpdu_size_exp: cotp::TPDU_SIZE_MAX_EXP,
max_outstanding: 10,
password: None,
connect_timeout_ms: 10_000,
request_timeout_ms: 30_000,
max_tsdu: 256 * 1024,
limits: Limits::DEFAULT,
event_capacity: 64,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct Negotiated {
pub max_pdu: usize,
pub max_outstanding: usize,
pub tpdu_data: usize,
pub mms_context: u16,
pub authenticated: bool,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum CloseReason {
Released,
PeerReleased,
Aborted,
ConnectTimeout,
Refused,
ProtocolError,
}
#[derive(Clone, Debug, PartialEq)]
pub enum AssociationEvent {
Established(Negotiated),
Refused {
layer: &'static str,
code: Option<i64>,
},
Response {
invoke_id: i64,
pdu: Vec<u8>,
},
Request {
invoke_id: i64,
pdu: Vec<u8>,
},
Cancelled {
invoke_id: i64,
},
CancelRefused {
invoke_id: i64,
},
Unconfirmed {
pdu: Vec<u8>,
},
Malformed {
invoke_id: Option<i64>,
error: Error,
},
Rejected {
invoke_id: Option<i64>,
reject: crate::proto::mms::reject::Reject,
},
Timeout {
invoke_id: i64,
},
Closed(CloseReason),
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub struct AssociationStats {
pub requests_sent: u64,
pub responses_received: u64,
pub reports_received: u64,
pub timeouts: u64,
pub malformed: u64,
pub rejected: u64,
pub packets_sent: u64,
pub packets_received: u64,
pub events_dropped: u64,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum State {
Idle,
Connecting,
Associating,
Established,
Releasing,
Closed,
}
#[derive(Debug)]
pub struct Association {
cfg: AssociationConfig,
role: Role,
state: State,
reader: tpkt::Reader,
reassembler: cotp::Reassembler,
out: VecDeque<Vec<u8>>,
current: Vec<u8>,
events: EventQueue<AssociationEvent>,
stats: AssociationStats,
outstanding: BTreeMap<i64, Instant>,
next_invoke: i64,
negotiated: Option<Negotiated>,
connect_deadline: Option<Instant>,
peer_max_pdu: usize,
tpdu_data: usize,
acse_context: u16,
mms_context: u16,
peer_ref: u16,
}
fn password_of(value: &[u8]) -> Option<&[u8]> {
let t = Cursor::new(value).next()?.ok()?;
(t.tag == Tag::context(0)).then_some(t.value)
}
fn same_secret(a: &[u8], b: &[u8]) -> bool {
let mut diff = u8::from(a.len() != b.len());
for (x, y) in a.iter().zip(b) {
diff |= x ^ y;
}
diff == 0
}
impl Association {
pub fn client(cfg: AssociationConfig) -> Association {
Association::new(cfg, Role::Client)
}
pub fn server(cfg: AssociationConfig) -> Association {
Association::new(cfg, Role::Server)
}
fn new(cfg: AssociationConfig, role: Role) -> Association {
let events = EventQueue::new(cfg.event_capacity);
let reassembler = cotp::Reassembler::new(cfg.max_tsdu);
let tpdu_data = cotp::tpdu_size(cfg.tpdu_size_exp).saturating_sub(3);
Association {
cfg,
role,
state: State::Idle,
reader: tpkt::Reader::new(),
reassembler,
out: VecDeque::new(),
current: Vec::new(),
events,
stats: AssociationStats::default(),
outstanding: BTreeMap::new(),
next_invoke: 1,
negotiated: None,
connect_deadline: None,
peer_max_pdu: 0,
tpdu_data,
acse_context: ACSE_CONTEXT,
mms_context: MMS_CONTEXT,
peer_ref: 0,
}
}
pub const fn state(&self) -> State {
self.state
}
pub const fn is_established(&self) -> bool {
matches!(self.state, State::Established)
}
pub const fn negotiated(&self) -> Option<Negotiated> {
self.negotiated
}
pub const fn stats(&self) -> AssociationStats {
self.stats
}
pub fn outstanding(&self) -> usize {
self.outstanding.len()
}
pub fn start(&mut self, now: Instant) -> Result<()> {
if self.role != Role::Client {
return Err(Error::InvalidValue("only a client association starts a connection"));
}
if self.state != State::Idle {
return Err(Error::InvalidValue("association already started"));
}
let cr = Tpdu::ConnectionRequest(cotp::Connect::request(
LOCAL_REF,
Tsel::new(self.cfg.local.t_sel.clone()),
Tsel::new(self.cfg.remote.t_sel.clone()),
self.cfg.tpdu_size_exp,
));
let mut body = Vec::new();
cr.write(&mut body)?;
self.queue_tpkt(&body)?;
self.state = State::Connecting;
self.arm_connect_deadline(now);
Ok(())
}
pub fn on_bytes(&mut self, now: Instant, bytes: &[u8]) {
if matches!(self.state, State::Closed) {
return;
}
self.reader.push(bytes);
loop {
let tpdu = match self.reader.next_tpdu() {
Ok(Some(t)) => t.to_vec(),
Ok(None) => return,
Err(_) => return self.close(CloseReason::ProtocolError),
};
self.stats.packets_received = self.stats.packets_received.saturating_add(1);
if let Err(e) = self.on_tpdu(now, &tpdu) {
self.stats.malformed = self.stats.malformed.saturating_add(1);
self.emit(AssociationEvent::Malformed { invoke_id: None, error: e });
self.close(CloseReason::ProtocolError);
return;
}
if matches!(self.state, State::Closed) {
return;
}
}
}
pub fn on_timeout(&mut self, now: Instant) {
if let Some(deadline) = self.connect_deadline
&& now >= deadline
{
match self.state {
State::Releasing => return self.close(CloseReason::Released),
State::Established | State::Closed => {}
_ => return self.close(CloseReason::ConnectTimeout),
}
}
if self.cfg.request_timeout_ms == 0 {
return;
}
let expired: Vec<i64> = self.outstanding.iter().filter(|(_, d)| now >= **d).map(|(id, _)| *id).collect();
for id in expired {
self.outstanding.remove(&id);
self.stats.timeouts = self.stats.timeouts.saturating_add(1);
self.emit(AssociationEvent::Timeout { invoke_id: id });
}
}
pub fn next_timeout(&self) -> Option<Instant> {
let handshake = if matches!(self.state, State::Established | State::Closed) { None } else { self.connect_deadline };
let request = if self.cfg.request_timeout_ms == 0 { None } else { self.outstanding.values().min().copied() };
match (handshake, request) {
(Some(a), Some(b)) => Some(a.min(b)),
(a, b) => a.or(b),
}
}
pub fn poll_transmit(&mut self) -> Option<&[u8]> {
self.current = self.out.pop_front()?;
self.stats.packets_sent = self.stats.packets_sent.saturating_add(1);
Some(&self.current)
}
pub fn poll_event(&mut self) -> Option<AssociationEvent> {
let e = self.events.pop();
self.stats.events_dropped = self.events.dropped();
e
}
pub fn call(&mut self, now: Instant, service: &ConfirmedRequest<'_>) -> Result<i64> {
if !self.is_established() {
return Err(Error::InvalidValue("association is not established"));
}
let allowed = self.negotiated.map_or(usize::from(self.cfg.max_outstanding), |n| n.max_outstanding);
if self.outstanding.len() >= allowed {
return Err(Error::LimitExceeded { limit: "max_outstanding", value: self.outstanding.len() + 1 });
}
let invoke_id = self.take_invoke_id();
let pdu = Mms::ConfirmedRequest { invoke_id, service: service.clone() };
self.send(&pdu)?;
let deadline = now.plus_millis(self.cfg.request_timeout_ms);
self.outstanding.insert(invoke_id, deadline);
self.stats.requests_sent = self.stats.requests_sent.saturating_add(1);
Ok(invoke_id)
}
pub fn extend_deadline(&mut self, invoke_id: i64, now: Instant) {
if let Some(deadline) = self.outstanding.get_mut(&invoke_id) {
*deadline = now.plus_millis(self.cfg.request_timeout_ms);
}
}
pub fn respond(&mut self, invoke_id: i64, service: &ConfirmedResponse<'_>) -> Result<()> {
self.send(&Mms::ConfirmedResponse { invoke_id, service: service.clone() })
}
pub fn cancel(&mut self, invoke_id: i64) -> Result<()> {
if !self.outstanding.contains_key(&invoke_id) {
return Err(Error::NotFound("no such outstanding request"));
}
self.send(&Mms::CancelRequest(invoke_id))
}
pub fn send(&mut self, pdu: &Mms<'_>) -> Result<()> {
if !matches!(self.state, State::Established) {
return Err(Error::InvalidValue("association is not established"));
}
let bytes = pdu.to_vec()?;
if self.peer_max_pdu > 0 && bytes.len() > self.peer_max_pdu {
return Err(Error::LimitExceeded { limit: "peer max PDU", value: bytes.len() });
}
let context = self.mms_context;
self.send_user_data(context, &bytes)
}
pub fn send_encoded(&mut self, pdu: &[u8]) -> Result<()> {
if !matches!(self.state, State::Established) {
return Err(Error::InvalidValue("association is not established"));
}
if self.peer_max_pdu > 0 && pdu.len() > self.peer_max_pdu {
return Err(Error::LimitExceeded { limit: "peer max PDU", value: pdu.len() });
}
let context = self.mms_context;
self.send_user_data(context, pdu)
}
pub fn release(&mut self, now: Instant) -> Result<()> {
if !matches!(self.state, State::Established) {
return Err(Error::InvalidValue("association is not established"));
}
let rlrq = Apdu::Release(Some(0)).to_vec()?;
let ppdu = Ppdu::UserData(alloc::vec![Pdv::single(self.acse_context, &rlrq)]).to_vec()?;
let mut spdu = Vec::new();
Spdu::Finish(&ppdu).write(&mut spdu)?;
self.send_tsdu(&spdu)?;
self.state = State::Releasing;
self.connect_deadline = Some(now.plus_millis(self.cfg.connect_timeout_ms));
Ok(())
}
pub fn abort(&mut self) {
if matches!(self.state, State::Closed) {
return;
}
let dr = Tpdu::DisconnectRequest { dst_ref: self.peer_ref, src_ref: LOCAL_REF, reason: 0 };
let mut body = Vec::new();
if dr.write(&mut body).is_ok() {
let _ = self.queue_tpkt(&body);
}
self.close(CloseReason::Aborted);
}
fn on_tpdu(&mut self, now: Instant, tpdu: &[u8]) -> Result<()> {
match Tpdu::parse(tpdu)? {
Tpdu::ConnectionRequest(c) => self.on_connection_request(now, &c),
Tpdu::ConnectionConfirm(c) => self.on_connection_confirm(&c),
Tpdu::DisconnectRequest { .. } => {
self.close(CloseReason::Aborted);
Ok(())
}
Tpdu::Data { eot, payload } => {
let tsdu = match self.reassembler.push(eot, payload)? {
Some(t) => t.to_vec(),
None => return Ok(()),
};
self.reassembler.take();
self.on_tsdu(&tsdu)
}
Tpdu::Other { .. } => {
self.close(CloseReason::ProtocolError);
Ok(())
}
}
}
fn on_connection_request(&mut self, now: Instant, c: &cotp::Connect) -> Result<()> {
if self.role != Role::Server {
self.close(CloseReason::ProtocolError);
return Ok(());
}
let exp = c.tpdu_size_exp.unwrap_or(cotp::TPDU_SIZE_MIN_EXP).min(self.cfg.tpdu_size_exp);
self.tpdu_data = cotp::tpdu_size(exp).saturating_sub(3);
self.peer_ref = c.src_ref;
let cc = cotp::Connect {
dst_ref: c.src_ref,
src_ref: LOCAL_REF,
class_options: 0,
tpdu_size_exp: Some(exp),
src_tsel: c.dst_tsel.clone(),
dst_tsel: c.src_tsel.clone(),
};
let mut body = Vec::new();
Tpdu::ConnectionConfirm(cc).write(&mut body)?;
self.queue_tpkt(&body)?;
self.state = State::Associating;
self.arm_connect_deadline(now);
Ok(())
}
fn on_connection_confirm(&mut self, c: &cotp::Connect) -> Result<()> {
if self.role != Role::Client || self.state != State::Connecting {
self.close(CloseReason::ProtocolError);
return Ok(());
}
let exp = c.tpdu_size_exp.unwrap_or(cotp::TPDU_SIZE_MIN_EXP).min(self.cfg.tpdu_size_exp);
self.tpdu_data = cotp::tpdu_size(exp).saturating_sub(3);
self.peer_ref = c.src_ref;
self.state = State::Associating;
self.send_associate_request()
}
fn on_tsdu(&mut self, tsdu: &[u8]) -> Result<()> {
match Spdu::parse(tsdu)? {
Spdu::Connect(c) => self.on_session_connect(c.user_data),
Spdu::Accept(c) => self.on_session_accept(c.user_data),
Spdu::Refuse { reason, .. } => {
self.emit(AssociationEvent::Refused { layer: "session", code: reason.map(i64::from) });
self.close(CloseReason::Refused);
Ok(())
}
Spdu::DataTransfer(p) => self.on_user_data(p),
Spdu::Finish(_) => self.on_peer_finish(),
Spdu::Disconnect(_) => {
self.close(CloseReason::Released);
Ok(())
}
Spdu::Abort(_) | Spdu::AbortAccept => {
self.close(CloseReason::Aborted);
Ok(())
}
Spdu::Other { .. } => {
self.close(CloseReason::ProtocolError);
Ok(())
}
}
}
fn on_session_connect(&mut self, ppdu_bytes: &[u8]) -> Result<()> {
if self.role != Role::Server {
self.close(CloseReason::ProtocolError);
return Ok(());
}
let Ppdu::Connect(cp) = Ppdu::parse(ppdu_bytes, true)? else {
self.close(CloseReason::ProtocolError);
return Ok(());
};
let mms_context = cp.context_for(Oid::MMS_ABSTRACT_SYNTAX).unwrap_or(MMS_CONTEXT);
let acse_context = cp.context_for(Oid::ACSE_ABSTRACT_SYNTAX).unwrap_or(ACSE_CONTEXT);
self.mms_context = mms_context;
self.acse_context = acse_context;
let aarq =
cp.user_data.iter().find(|p| p.context_id == acse_context).and_then(|p| p.values.single()).ok_or(Error::InvalidValue("CP carries no ACSE PDU"))?;
let Apdu::Associate(a) = Apdu::parse(aarq)? else {
self.close(CloseReason::ProtocolError);
return Ok(());
};
let initiate_bytes = a.mms_pdu().ok_or(Error::InvalidValue("AARQ carries no Initiate"))?;
let Mms::InitiateRequest(init) = Mms::parse(initiate_bytes, &self.cfg.limits)? else {
self.close(CloseReason::ProtocolError);
return Ok(());
};
let authenticated = match self.cfg.password.as_deref() {
None => false,
Some(expected) => {
if !a.authentication_value.and_then(password_of).is_some_and(|given| same_secret(given, expected.as_bytes())) {
self.refuse_associate(&cp, acse_context)?;
self.emit(AssociationEvent::Refused { layer: "acse", code: Some(13) });
self.close(CloseReason::Refused);
return Ok(());
}
true
}
};
self.peer_max_pdu = usize::try_from(init.local_detail.unwrap_or(0)).unwrap_or(0);
let mine = usize::try_from(init.max_serv_outstanding_called).unwrap_or(0).clamp(1, usize::from(self.cfg.max_outstanding).max(1));
self.answer_associate(&cp, mms_context, acse_context, &init)?;
self.established(Negotiated {
max_pdu: if self.peer_max_pdu == 0 { self.cfg.max_pdu as usize } else { self.peer_max_pdu },
max_outstanding: mine,
tpdu_data: self.tpdu_data,
mms_context,
authenticated,
});
Ok(())
}
fn refuse_associate(&mut self, cp: &Cp<'_>, acse_context: u16) -> Result<()> {
let diagnostic = acse::diagnostic_service_user(13);
let aare = Associate {
protocol_version: Some((7, &[0x80])),
context_name: Some(Oid::MMS_APPLICATION_CONTEXT),
result: Some(acse::RESULT_REJECTED_PERMANENT),
result_diagnostic: Some(&diagnostic),
..Associate::default()
};
let aare_bytes = Apdu::AssociateResponse(aare).to_vec()?;
let results: Vec<ContextResult<'_>> =
cp.contexts.iter().map(|_: &ContextDefinition<'_>| ContextResult { result: RESULT_ACCEPTANCE, transfer_syntax: Some(Oid::BER) }).collect();
let cpa = Cp {
protocol_version: Some((7, &[0x80])),
responding_psel: Some(&self.cfg.local.p_sel),
results,
requirements: Some((6, &[0x00])),
user_data: alloc::vec![Pdv::single(acse_context, &aare_bytes)],
..Cp::default()
};
let ppdu = Ppdu::Accept(cpa).to_vec()?;
let mut spdu = Vec::new();
Spdu::Accept(session::Connect::new(Some(&self.cfg.local.s_sel), Some(&self.cfg.remote.s_sel), &ppdu)).write(&mut spdu)?;
self.send_tsdu(&spdu)
}
fn on_session_accept(&mut self, ppdu_bytes: &[u8]) -> Result<()> {
if self.role != Role::Client {
self.close(CloseReason::ProtocolError);
return Ok(());
}
let ppdu = Ppdu::parse(ppdu_bytes, true)?;
let cp = match ppdu {
Ppdu::Accept(cp) => cp,
Ppdu::Reject(cp) => {
self.emit(AssociationEvent::Refused { layer: "presentation", code: cp.provider_reason });
self.close(CloseReason::Refused);
return Ok(());
}
_ => {
self.close(CloseReason::ProtocolError);
return Ok(());
}
};
if !cp.all_accepted() {
self.emit(AssociationEvent::Refused { layer: "presentation", code: cp.results.iter().map(|r| r.result).find(|r| *r != RESULT_ACCEPTANCE) });
self.close(CloseReason::Refused);
return Ok(());
}
let acse_context = self.acse_context;
let aare =
cp.user_data.iter().find(|p| p.context_id == acse_context).and_then(|p| p.values.single()).ok_or(Error::InvalidValue("CPA carries no ACSE PDU"))?;
let Apdu::AssociateResponse(a) = Apdu::parse(aare)? else {
self.close(CloseReason::ProtocolError);
return Ok(());
};
if !a.accepted() {
self.emit(AssociationEvent::Refused { layer: "acse", code: a.result });
self.close(CloseReason::Refused);
return Ok(());
}
let initiate_bytes = a.mms_pdu().ok_or(Error::InvalidValue("AARE carries no Initiate response"))?;
match Mms::parse(initiate_bytes, &self.cfg.limits)? {
Mms::InitiateResponse(init) => {
self.peer_max_pdu = usize::try_from(init.local_detail.unwrap_or(0)).unwrap_or(0);
let mine = usize::try_from(init.max_serv_outstanding_calling).unwrap_or(0);
self.established(Negotiated {
max_pdu: if self.peer_max_pdu == 0 { self.cfg.max_pdu as usize } else { self.peer_max_pdu },
max_outstanding: mine.clamp(1, usize::from(self.cfg.max_outstanding)),
tpdu_data: self.tpdu_data,
mms_context: self.mms_context,
authenticated: self.cfg.password.is_some(),
});
Ok(())
}
Mms::InitiateError(_) => {
self.emit(AssociationEvent::Refused { layer: "mms", code: None });
self.close(CloseReason::Refused);
Ok(())
}
_ => {
self.close(CloseReason::ProtocolError);
Ok(())
}
}
}
fn on_user_data(&mut self, ppdu_bytes: &[u8]) -> Result<()> {
let Ppdu::UserData(pdvs) = Ppdu::parse(ppdu_bytes, false)? else {
self.close(CloseReason::ProtocolError);
return Ok(());
};
for pdv in pdvs {
let Some(value) = pdv.values.single() else { continue };
if pdv.context_id == self.acse_context {
match Apdu::parse(value) {
Ok(Apdu::Release(_)) => return self.on_peer_finish(),
Ok(Apdu::ReleaseResponse(_)) => {
self.close(CloseReason::Released);
return Ok(());
}
Ok(Apdu::Abort(_)) => {
self.close(CloseReason::Aborted);
return Ok(());
}
_ => continue,
}
}
self.deliver_mms(value);
}
Ok(())
}
fn deliver_mms(&mut self, bytes: &[u8]) {
let pdu = match Mms::parse(bytes, &self.cfg.limits) {
Ok(p) => p,
Err(error) => {
self.stats.malformed = self.stats.malformed.saturating_add(1);
let invoke_id = Mms::peek_invoke_id(bytes).filter(|id| self.outstanding.remove(id).is_some());
self.emit(AssociationEvent::Malformed { invoke_id, error });
return;
}
};
let event = match &pdu {
Mms::ConfirmedResponse { invoke_id, .. } | Mms::ConfirmedError { invoke_id, .. } => {
let invoke_id = *invoke_id;
self.outstanding.remove(&invoke_id);
self.stats.responses_received = self.stats.responses_received.saturating_add(1);
AssociationEvent::Response { invoke_id, pdu: bytes.to_vec() }
}
Mms::ConfirmedRequest { invoke_id, .. } => AssociationEvent::Request { invoke_id: *invoke_id, pdu: bytes.to_vec() },
Mms::Unconfirmed(_) => {
self.stats.reports_received = self.stats.reports_received.saturating_add(1);
AssociationEvent::Unconfirmed { pdu: bytes.to_vec() }
}
Mms::Reject(reject) => {
let reject = *reject;
if let Some(id) = reject.original_invoke_id {
self.outstanding.remove(&id);
}
self.stats.rejected = self.stats.rejected.saturating_add(1);
AssociationEvent::Rejected { invoke_id: reject.original_invoke_id, reject }
}
Mms::CancelRequest(invoke_id) => {
let invoke_id = *invoke_id;
let error =
ServiceError::encode(Tag::context_constructed(1), service_error::SERVICE, service_error::PRIMITIVES_OUT_OF_SEQUENCE).unwrap_or_default();
if let Ok(tlv) = Cursor::new(&error).next_required() {
let _ = self.send_pdu_unchecked(&Mms::CancelError { invoke_id, error: tlv });
}
AssociationEvent::CancelRefused { invoke_id }
}
Mms::CancelResponse(invoke_id) => {
let invoke_id = *invoke_id;
self.outstanding.remove(&invoke_id);
AssociationEvent::Cancelled { invoke_id }
}
Mms::CancelError { invoke_id, .. } => AssociationEvent::CancelRefused { invoke_id: *invoke_id },
Mms::ConcludeRequest => {
let _ = self.send_pdu_unchecked(&Mms::ConcludeResponse);
self.close(CloseReason::PeerReleased);
return;
}
Mms::ConcludeResponse => {
self.close(CloseReason::Released);
return;
}
_ => AssociationEvent::Unconfirmed { pdu: bytes.to_vec() },
};
self.emit(event);
}
fn on_peer_finish(&mut self) -> Result<()> {
let rlre = Apdu::ReleaseResponse(Some(0)).to_vec()?;
let ppdu = Ppdu::UserData(alloc::vec![Pdv::single(self.acse_context, &rlre)]).to_vec()?;
let mut spdu = Vec::new();
Spdu::Disconnect(&ppdu).write(&mut spdu)?;
self.send_tsdu(&spdu)?;
self.close(CloseReason::PeerReleased);
Ok(())
}
fn send_associate_request(&mut self) -> Result<()> {
let initiate =
Mms::InitiateRequest(Initiate::request(i64::from(self.cfg.max_pdu), i64::from(self.cfg.max_outstanding), PARAMETER_CBB, SERVICES_SUPPORTED))
.to_vec()?;
let called = self.cfg.remote.ap_title_element();
let calling = self.cfg.local.ap_title_element();
let called_q = self.cfg.remote.ae_qualifier_element();
let calling_q = self.cfg.local.ae_qualifier_element();
let password;
let mut aarq = Associate::request(called.as_deref(), calling.as_deref(), i64::from(MMS_CONTEXT), &initiate);
aarq.called_ae_qualifier = called_q.as_deref();
aarq.calling_ae_qualifier = calling_q.as_deref();
if let Some(p) = &self.cfg.password {
let mut e = Encoder::new();
e.primitive(Tag::context(0), p.as_bytes())?;
password = e.into_vec();
aarq.sender_requirements = Some((7, &[0x80]));
aarq.mechanism_name = Some(Oid::PASSWORD_MECHANISM);
aarq.authentication_value = Some(&password);
}
let aarq_bytes = Apdu::Associate(aarq).to_vec()?;
let cp = Cp::connect(&self.cfg.local.p_sel, &self.cfg.remote.p_sel, ACSE_CONTEXT, MMS_CONTEXT, &aarq_bytes);
let ppdu = Ppdu::Connect(cp).to_vec()?;
let mut spdu = Vec::new();
Spdu::Connect(session::Connect::new(Some(&self.cfg.local.s_sel), Some(&self.cfg.remote.s_sel), &ppdu)).write(&mut spdu)?;
self.send_tsdu(&spdu)
}
fn answer_associate(&mut self, cp: &Cp<'_>, mms_context: u16, acse_context: u16, peer: &Initiate<'_>) -> Result<()> {
let max_pdu = i64::from(self.cfg.max_pdu).min(peer.local_detail.unwrap_or(i64::from(self.cfg.max_pdu)));
let mine = i64::from(self.cfg.max_outstanding);
let mut init = Initiate::request(max_pdu, mine, PARAMETER_CBB, SERVICES_SUPPORTED);
init.max_serv_outstanding_calling = mine.min(peer.max_serv_outstanding_calling.max(1));
init.max_serv_outstanding_called = mine.min(peer.max_serv_outstanding_called.max(1));
let initiate = Mms::InitiateResponse(init).to_vec()?;
let responding = self.cfg.local.ap_title_element();
let responding_q = self.cfg.local.ae_qualifier_element();
let aare = Associate {
protocol_version: Some((7, &[0x80])),
context_name: Some(Oid::MMS_APPLICATION_CONTEXT),
result: Some(acse::RESULT_ACCEPTED),
result_diagnostic: Some(acse::DIAGNOSTIC_SERVICE_USER_NULL),
responding_ap_title: responding.as_deref(),
responding_ae_qualifier: responding_q.as_deref(),
user_information: Some(acse::UserInformation {
direct_reference: Some(Oid::BER),
indirect_reference: Some(i64::from(mms_context)),
value: &initiate,
}),
..Associate::default()
};
let aare_bytes = Apdu::AssociateResponse(aare).to_vec()?;
let results: Vec<ContextResult<'_>> =
cp.contexts.iter().map(|_: &ContextDefinition<'_>| ContextResult { result: RESULT_ACCEPTANCE, transfer_syntax: Some(Oid::BER) }).collect();
let cpa = Cp {
protocol_version: Some((7, &[0x80])),
responding_psel: Some(&self.cfg.local.p_sel),
results,
requirements: Some((6, &[0x00])),
user_data: alloc::vec![Pdv::single(acse_context, &aare_bytes)],
..Cp::default()
};
let ppdu = Ppdu::Accept(cpa).to_vec()?;
let mut spdu = Vec::new();
Spdu::Accept(session::Connect::new(Some(&self.cfg.local.s_sel), Some(&self.cfg.remote.s_sel), &ppdu)).write(&mut spdu)?;
self.send_tsdu(&spdu)
}
fn send_user_data(&mut self, context: u16, value: &[u8]) -> Result<()> {
let ppdu = Ppdu::UserData(alloc::vec![Pdv::single(context, value)]).to_vec()?;
let mut spdu = Vec::new();
Spdu::DataTransfer(&ppdu).write(&mut spdu)?;
self.send_tsdu(&spdu)
}
fn send_pdu_unchecked(&mut self, pdu: &Mms<'_>) -> Result<()> {
let bytes = pdu.to_vec()?;
let context = self.mms_context;
self.send_user_data(context, &bytes)
}
fn send_tsdu(&mut self, tsdu: &[u8]) -> Result<()> {
let chunk = self.tpdu_data.max(1);
let mut at = 0usize;
loop {
let end = at.saturating_add(chunk).min(tsdu.len());
let payload = tsdu.get(at..end).unwrap_or(&[]);
let eot = end >= tsdu.len();
let mut body = Vec::with_capacity(payload.len() + 3);
Tpdu::Data { eot, payload }.write(&mut body)?;
self.queue_tpkt(&body)?;
at = end;
if eot {
return Ok(());
}
}
}
fn queue_tpkt(&mut self, tpdu: &[u8]) -> Result<()> {
let header = tpkt::header(tpdu.len())?;
let mut packet = Vec::with_capacity(tpkt::HEADER_LEN + tpdu.len());
packet.extend_from_slice(&header);
packet.extend_from_slice(tpdu);
self.out.push_back(packet);
Ok(())
}
fn established(&mut self, n: Negotiated) {
self.state = State::Established;
self.connect_deadline = None;
self.negotiated = Some(n);
self.emit(AssociationEvent::Established(n));
}
fn arm_connect_deadline(&mut self, now: Instant) {
if self.cfg.connect_timeout_ms > 0 {
self.connect_deadline = Some(now.plus_millis(self.cfg.connect_timeout_ms));
}
}
fn close(&mut self, reason: CloseReason) {
if matches!(self.state, State::Closed) {
return;
}
self.state = State::Closed;
self.connect_deadline = None;
self.outstanding.clear();
self.reassembler.reset();
self.emit(AssociationEvent::Closed(reason));
}
fn take_invoke_id(&mut self) -> i64 {
for _ in 0..=self.outstanding.len() {
let id = self.next_invoke;
self.next_invoke = if self.next_invoke >= i64::from(u32::MAX) { 1 } else { self.next_invoke + 1 };
if !self.outstanding.contains_key(&id) {
return id;
}
}
self.next_invoke
}
fn emit(&mut self, event: AssociationEvent) {
self.events.push(event);
self.stats.events_dropped = self.events.dropped();
}
}
const PARAMETER_CBB: (u8, &[u8]) = (5, &[0xF1, 0x00]);
const SERVICES_SUPPORTED: (u8, &[u8]) = (4, &[0xEE, 0x1C, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x41, 0xED, 0x20]);
#[cfg(test)]
mod tests {
use super::*;
use crate::proto::mms::{ObjectName, ObjectScope, Unconfirmed, VariableAccess, VariableSpecification, object_class};
fn pair() -> (Association, Association) {
let cfg = AssociationConfig::default();
(Association::client(cfg.clone()), Association::server(cfg))
}
fn pump(a: &mut Association, b: &mut Association, now: Instant) {
for _ in 0..32 {
let mut moved = false;
while let Some(packet) = a.poll_transmit() {
let packet = packet.to_vec();
b.on_bytes(now, &packet);
moved = true;
}
while let Some(packet) = b.poll_transmit() {
let packet = packet.to_vec();
a.on_bytes(now, &packet);
moved = true;
}
if !moved {
return;
}
}
panic!("the two ends never went quiet");
}
fn events(a: &mut Association) -> Vec<AssociationEvent> {
let mut v = Vec::new();
while let Some(e) = a.poll_event() {
v.push(e);
}
v
}
fn connect() -> (Association, Association) {
let (mut c, mut s) = pair();
c.start(Instant::ZERO).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
(c, s)
}
#[test]
fn an_answer_that_does_not_decode_fails_its_call_rather_than_hanging() {
let (mut c, mut s) = connect();
let _ = events(&mut c);
let request = ConfirmedRequest::GetNameList { object_class: object_class::NAMED_VARIABLE, scope: ObjectScope::VmdSpecific, continue_after: None };
let invoke = c.call(Instant::ZERO, &request).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
let _ = events(&mut s);
let mut e = Encoder::new();
e.constructed(Tag::context_constructed(1), |e| {
e.integer(Tag::universal(crate::ber::universal::INTEGER, false), invoke)?;
e.constructed(Tag::context_constructed(1), |e| {
e.primitive(Tag::context(9), &[0xFF])?;
Ok(())
})?;
Ok(())
})
.unwrap();
s.send_encoded(&e.into_vec()).unwrap();
pump(&mut s, &mut c, Instant::ZERO);
let seen = events(&mut c);
assert!(
seen.iter().any(|e| matches!(e, AssociationEvent::Malformed { invoke_id: Some(id), .. } if *id == invoke)),
"the failure has to name the call it ends: {seen:?}"
);
assert_eq!(c.outstanding(), 0);
}
#[test]
fn the_outstanding_call_budget_is_negotiated_down_from_both_sides() {
for (client_max, server_max) in [(2u8, 10u8), (10, 2), (4, 4)] {
let c_cfg = AssociationConfig { max_outstanding: client_max, ..AssociationConfig::default() };
let s_cfg = AssociationConfig { max_outstanding: server_max, ..AssociationConfig::default() };
let (mut c, mut s) = (Association::client(c_cfg), Association::server(s_cfg));
c.start(Instant::ZERO).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
let agreed = usize::from(client_max.min(server_max));
assert_eq!(c.negotiated().unwrap().max_outstanding, agreed, "client with {client_max} against server with {server_max}");
assert_eq!(s.negotiated().unwrap().max_outstanding, agreed, "server with {server_max} against client with {client_max}");
}
}
#[test]
fn a_client_may_not_exceed_the_budget_the_server_agreed_to() {
let c_cfg = AssociationConfig { max_outstanding: 10, request_timeout_ms: 0, ..AssociationConfig::default() };
let s_cfg = AssociationConfig { max_outstanding: 2, ..AssociationConfig::default() };
let (mut c, mut s) = (Association::client(c_cfg), Association::server(s_cfg));
c.start(Instant::ZERO).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
let req = ConfirmedRequest::Identify;
assert!(c.call(Instant::ZERO, &req).is_ok());
assert!(c.call(Instant::ZERO, &req).is_ok());
assert!(matches!(c.call(Instant::ZERO, &req), Err(Error::LimitExceeded { limit: "max_outstanding", .. })), "the third exceeds the agreed two");
}
#[test]
fn a_disconnect_request_names_the_reference_the_peer_issued() {
let mut c = Association::client(AssociationConfig::default());
c.start(Instant::ZERO).unwrap();
while c.poll_transmit().is_some() {}
let cc = cotp::Connect { dst_ref: 1, src_ref: 0x1234, class_options: 0, tpdu_size_exp: Some(cotp::TPDU_SIZE_MAX_EXP), src_tsel: None, dst_tsel: None };
let mut body = Vec::new();
Tpdu::ConnectionConfirm(cc).write(&mut body).unwrap();
let mut packet = tpkt::header(body.len()).unwrap().to_vec();
packet.extend_from_slice(&body);
c.on_bytes(Instant::ZERO, &packet);
while c.poll_transmit().is_some() {}
c.abort();
let dr = c.poll_transmit().expect("a disconnect request").to_vec();
let tpdu = Tpdu::parse(dr.get(tpkt::HEADER_LEN..).unwrap()).unwrap();
assert!(matches!(tpdu, Tpdu::DisconnectRequest { dst_ref: 0x1234, src_ref: LOCAL_REF, .. }), "{tpdu:?}");
}
#[test]
fn a_reject_answers_the_request_it_names_and_releases_it() {
use crate::proto::mms::reject::{Reject, RejectReason, UNRECOGNIZED_SERVICE};
let (mut c, mut s) = connect();
let id = c.call(Instant::ZERO, &ConfirmedRequest::Identify).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
assert_eq!(c.outstanding(), 1);
let _ = events(&mut c);
s.send(&Mms::Reject(Reject::confirmed_request(id, UNRECOGNIZED_SERVICE))).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
assert_eq!(c.outstanding(), 0, "the reject released the invoke identifier");
assert_eq!(c.stats().rejected, 1);
let seen = events(&mut c);
assert!(
matches!(
seen.as_slice(),
[AssociationEvent::Rejected { invoke_id: Some(got), reject: Reject { reason: RejectReason::ConfirmedRequest(UNRECOGNIZED_SERVICE), .. } }]
if *got == id
),
"{seen:?}"
);
assert_eq!(c.stats().reports_received, 0);
}
#[test]
fn a_reject_without_an_invoke_identifier_is_still_reported() {
use crate::proto::mms::reject::{INVALID_PDU, Reject, RejectReason};
let (mut c, mut s) = connect();
let id = c.call(Instant::ZERO, &ConfirmedRequest::Identify).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
let _ = events(&mut c);
s.send(&Mms::Reject(Reject::pdu_error(INVALID_PDU))).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
assert_eq!(c.outstanding(), 1, "nothing was named, so nothing is released");
let seen = events(&mut c);
assert!(
matches!(seen.as_slice(), [AssociationEvent::Rejected { invoke_id: None, reject: Reject { reason: RejectReason::PduError(INVALID_PDU), .. } }]),
"{seen:?}"
);
let _ = id;
}
#[test]
fn the_two_ends_complete_the_six_layer_handshake() {
let (mut c, mut s) = connect();
assert!(c.is_established(), "client state {:?}", c.state());
assert!(s.is_established(), "server state {:?}", s.state());
assert!(matches!(events(&mut c).as_slice(), [AssociationEvent::Established(_)]));
assert!(matches!(events(&mut s).as_slice(), [AssociationEvent::Established(_)]));
let n = c.negotiated().unwrap();
assert_eq!(n.mms_context, MMS_CONTEXT);
assert_eq!(n.tpdu_data, cotp::tpdu_size(cotp::TPDU_SIZE_MAX_EXP) - 3, "class 0 negotiates down, so both ends land on the smaller proposal");
assert_eq!(n.max_pdu, AssociationConfig::default().max_pdu as usize);
assert_eq!(c.next_timeout(), None, "an established association waits for nothing by itself");
}
#[test]
fn a_request_reaches_the_server_and_its_answer_comes_back() {
let (mut c, mut s) = connect();
let (_, _) = (events(&mut c), events(&mut s));
let now = Instant::ZERO;
let id = c.call(now, &ConfirmedRequest::Identify).unwrap();
assert_eq!(c.outstanding(), 1);
assert_eq!(c.next_timeout(), Some(now.plus_millis(30_000)));
pump(&mut c, &mut s, now);
let seen = events(&mut s);
let [AssociationEvent::Request { invoke_id, pdu }] = seen.as_slice() else {
panic!("the server saw no request: {seen:?}");
};
assert_eq!(*invoke_id, id);
assert!(matches!(Mms::parse(pdu, &Limits::DEFAULT).unwrap(), Mms::ConfirmedRequest { service: ConfirmedRequest::Identify, .. }));
s.respond(id, &ConfirmedResponse::Identify { vendor: "hupe1980", model: "iec61850-kit", revision: "0.1" }).unwrap();
pump(&mut c, &mut s, now);
let seen = events(&mut c);
let [AssociationEvent::Response { invoke_id, pdu }] = seen.as_slice() else {
panic!("the client saw no response: {seen:?}");
};
assert_eq!(*invoke_id, id);
let Mms::ConfirmedResponse { service: ConfirmedResponse::Identify { vendor, .. }, .. } = Mms::parse(pdu, &Limits::DEFAULT).unwrap() else {
panic!("not an Identify response");
};
assert_eq!(vendor, "hupe1980");
assert_eq!(c.outstanding(), 0, "the answer releases the invoke identifier");
assert_eq!(c.next_timeout(), None);
assert_eq!(c.stats().requests_sent, 1);
assert_eq!(c.stats().responses_received, 1);
}
#[test]
fn a_report_arrives_unsolicited() {
let (mut c, mut s) = connect();
let (_, _) = (events(&mut c), events(&mut s));
let report = Mms::Unconfirmed(Unconfirmed::InformationReport {
access: VariableAccess::VariableListName(ObjectName::DomainSpecific { domain: "IED1LD0", item: "LLN0$RP$urcb01" }),
results: Vec::new(),
});
s.send(&report).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
assert!(matches!(events(&mut c).as_slice(), [AssociationEvent::Unconfirmed { .. }]));
assert_eq!(c.stats().reports_received, 1);
}
#[test]
fn a_request_that_is_never_answered_times_out_and_frees_its_slot() {
let (mut c, mut s) = connect();
let (_, _) = (events(&mut c), events(&mut s));
let now = Instant::ZERO;
let id =
c.call(now, &ConfirmedRequest::GetNameList { object_class: object_class::DOMAIN, scope: ObjectScope::VmdSpecific, continue_after: None }).unwrap();
c.on_timeout(now.plus_millis(29_999));
assert!(events(&mut c).is_empty(), "not yet");
c.on_timeout(now.plus_millis(30_000));
assert_eq!(events(&mut c).as_slice(), [AssociationEvent::Timeout { invoke_id: id }]);
assert_eq!(c.outstanding(), 0);
assert!(c.is_established(), "one unanswered request does not end an association");
let _ = s;
}
#[test]
fn the_outstanding_limit_is_enforced_here_rather_than_discovered_as_a_reject() {
let cfg = AssociationConfig { max_outstanding: 2, ..AssociationConfig::default() };
let (mut c, mut s) = (Association::client(cfg.clone()), Association::server(cfg));
c.start(Instant::ZERO).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
let now = Instant::ZERO;
c.call(now, &ConfirmedRequest::Identify).unwrap();
c.call(now, &ConfirmedRequest::Identify).unwrap();
assert!(matches!(c.call(now, &ConfirmedRequest::Identify), Err(Error::LimitExceeded { limit: "max_outstanding", .. })));
}
#[test]
fn the_clients_budget_is_the_calling_one_and_the_servers_is_the_called_one() {
let cfg = AssociationConfig { max_outstanding: 40, ..AssociationConfig::default() };
let mut c = Association::client(cfg);
c.start(Instant::ZERO).unwrap();
let _cr = c.poll_transmit().map(<[u8]>::to_vec).unwrap();
c.on_bytes(
Instant::ZERO,
&tpkt_packet(&{
let cc = cotp::Connect { dst_ref: 1, src_ref: 2, class_options: 0, tpdu_size_exp: Some(10), src_tsel: None, dst_tsel: None };
let mut body = Vec::new();
Tpdu::ConnectionConfirm(cc).write(&mut body).unwrap();
body
}),
);
let _connect = c.poll_transmit().map(<[u8]>::to_vec).unwrap();
let mut init = Initiate::request(16_000, 10, PARAMETER_CBB, SERVICES_SUPPORTED);
init.max_serv_outstanding_calling = 5;
init.max_serv_outstanding_called = 1;
c.on_bytes(Instant::ZERO, &tpkt_packet(&server_accept(&init)));
assert!(c.is_established(), "state {:?}, events {:?}", c.state(), events(&mut c));
assert_eq!(c.negotiated().unwrap().max_outstanding, 5, "the calling budget, not the called one");
}
fn tpkt_packet(tpdu: &[u8]) -> Vec<u8> {
let mut p = tpkt::header(tpdu.len()).unwrap().to_vec();
p.extend_from_slice(tpdu);
p
}
fn server_accept(init: &Initiate<'_>) -> Vec<u8> {
let initiate = Mms::InitiateResponse(*init).to_vec().unwrap();
let aare = Apdu::AssociateResponse(Associate {
protocol_version: Some((7, &[0x80])),
context_name: Some(Oid::MMS_APPLICATION_CONTEXT),
result: Some(acse::RESULT_ACCEPTED),
user_information: Some(acse::UserInformation { direct_reference: Some(Oid::BER), indirect_reference: Some(3), value: &initiate }),
..Associate::default()
})
.to_vec()
.unwrap();
let cpa = Cp {
protocol_version: Some((7, &[0x80])),
results: alloc::vec![
ContextResult { result: RESULT_ACCEPTANCE, transfer_syntax: Some(Oid::BER) },
ContextResult { result: RESULT_ACCEPTANCE, transfer_syntax: Some(Oid::BER) },
],
user_data: alloc::vec![Pdv::single(ACSE_CONTEXT, &aare)],
..Cp::default()
};
let ppdu = Ppdu::Accept(cpa).to_vec().unwrap();
let mut spdu = Vec::new();
Spdu::Accept(session::Connect::new(None, None, &ppdu)).write(&mut spdu).unwrap();
let mut body = Vec::new();
Tpdu::Data { eot: true, payload: &spdu }.write(&mut body).unwrap();
body
}
#[test]
fn a_handshake_that_never_finishes_expires() {
let mut c = Association::client(AssociationConfig::default());
c.start(Instant::ZERO).unwrap();
assert_eq!(c.next_timeout(), Some(Instant::ZERO.plus_millis(10_000)));
c.on_timeout(Instant::ZERO.plus_millis(10_000));
assert_eq!(events(&mut c).as_slice(), [AssociationEvent::Closed(CloseReason::ConnectTimeout)]);
assert_eq!(c.state(), State::Closed);
assert!(c.call(Instant::ZERO, &ConfirmedRequest::Identify).is_err());
}
#[test]
fn an_orderly_release_ends_both_ends() {
let (mut c, mut s) = connect();
let (_, _) = (events(&mut c), events(&mut s));
c.release(Instant::ZERO).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
assert_eq!(events(&mut s).as_slice(), [AssociationEvent::Closed(CloseReason::PeerReleased)]);
assert_eq!(events(&mut c).as_slice(), [AssociationEvent::Closed(CloseReason::Released)]);
assert_eq!(c.state(), State::Closed);
}
#[test]
fn a_release_the_peer_never_confirms_is_still_a_release() {
let (mut c, mut s) = connect();
let (_, _) = (events(&mut c), events(&mut s));
c.release(Instant::ZERO).unwrap();
while c.poll_transmit().is_some() {}
c.on_timeout(Instant::ZERO.plus_millis(10_000));
assert_eq!(events(&mut c).as_slice(), [AssociationEvent::Closed(CloseReason::Released)], "not a handshake timeout");
assert_eq!(c.state(), State::Closed);
}
#[test]
fn a_tsdu_larger_than_a_tpdu_is_segmented_and_reassembled() {
let cfg = AssociationConfig { tpdu_size_exp: cotp::TPDU_SIZE_MIN_EXP, ..AssociationConfig::default() };
let (mut c, mut s) = (Association::client(cfg.clone()), Association::server(cfg));
c.start(Instant::ZERO).unwrap();
pump(&mut c, &mut s, Instant::ZERO);
assert!(c.is_established(), "even the handshake needs more than one TPDU here");
let (_, _) = (events(&mut c), events(&mut s));
let names: Vec<VariableSpecification<'_>> =
(0..40).map(|_| VariableSpecification::Name(ObjectName::DomainSpecific { domain: "IED1LD0", item: "MMXU1$MX$TotW$mag$f" })).collect();
let id = c.call(Instant::ZERO, &ConfirmedRequest::Read { specification_with_result: false, access: VariableAccess::ListOfVariable(names) }).unwrap();
let before = c.stats().packets_sent;
pump(&mut c, &mut s, Instant::ZERO);
assert!(c.stats().packets_sent > before + 1, "the read fitted one TPDU, so nothing was segmented: {:?}", c.stats());
let seen = events(&mut s);
let [AssociationEvent::Request { invoke_id, pdu }] = seen.as_slice() else {
panic!("the segmented request did not arrive whole: {seen:?}");
};
assert_eq!(*invoke_id, id);
let Mms::ConfirmedRequest { service: ConfirmedRequest::Read { access: VariableAccess::ListOfVariable(v), .. }, .. } =
Mms::parse(pdu, &Limits::DEFAULT).unwrap()
else {
panic!("not a read");
};
assert_eq!(v.len(), 40, "every variable survived the segmentation");
}
#[test]
fn a_password_travels_in_the_aarq_where_the_server_looks_for_it() {
let cfg = AssociationConfig {
password: Some(String::from("s3cret")),
local: Selectors { ap_title: Some(alloc::vec![1, 3, 9999, 33]), ae_qualifier: Some(12), ..Selectors::defaults() },
remote: Selectors { ap_title: Some(alloc::vec![1, 3, 9999, 23]), ..Selectors::defaults() },
..AssociationConfig::default()
};
let mut c = Association::client(cfg);
c.start(Instant::ZERO).unwrap();
let _cr = c.poll_transmit().map(<[u8]>::to_vec).unwrap();
c.on_bytes(Instant::ZERO, &{
let cc = cotp::Connect { dst_ref: 1, src_ref: 2, class_options: 0, tpdu_size_exp: Some(10), src_tsel: None, dst_tsel: None };
let mut body = Vec::new();
Tpdu::ConnectionConfirm(cc).write(&mut body).unwrap();
let mut p = tpkt::header(body.len()).unwrap().to_vec();
p.extend_from_slice(&body);
p
});
let connect = c.poll_transmit().map(<[u8]>::to_vec).expect("the session CONNECT");
let spdu = &connect[tpkt::HEADER_LEN + 3..];
let Spdu::Connect(sc) = Spdu::parse(spdu).unwrap() else { panic!("not a CONNECT") };
let Ppdu::Connect(cp) = Ppdu::parse(sc.user_data, true).unwrap() else { panic!("not a CP") };
let aarq = cp.user_data.iter().find(|p| p.context_id == ACSE_CONTEXT).and_then(|p| p.values.single()).unwrap();
let Apdu::Associate(a) = Apdu::parse(aarq).unwrap() else { panic!("not an AARQ") };
assert_eq!(a.mechanism_name, Some(Oid::PASSWORD_MECHANISM));
assert_eq!(a.authentication_value, Some(&[0x80, 6, b's', b'3', b'c', b'r', b'e', b't'][..]));
assert!(a.calling_ap_title.is_some() && a.called_ap_title.is_some());
assert_eq!(a.calling_ae_qualifier, Some(&[0x02, 0x01, 12][..]), "form 2 is an INTEGER element");
assert!(a.mms_pdu().is_some());
}
#[test]
fn a_stream_that_is_not_the_protocol_closes_the_association_rather_than_looping() {
let mut c = Association::client(AssociationConfig::default());
c.start(Instant::ZERO).unwrap();
c.on_bytes(Instant::ZERO, &[0x16, 0x03, 0x01, 0x00, 0x05]); assert_eq!(c.state(), State::Closed);
assert_eq!(events(&mut c).as_slice(), [AssociationEvent::Closed(CloseReason::ProtocolError)]);
}
#[test]
fn services_supported_names_exactly_the_services_that_exist() {
let (unused, bytes) = SERVICES_SUPPORTED;
let bit = |n: usize| bytes.get(n / 8).is_some_and(|b| b >> (7 - (n % 8)) & 1 == 1);
for n in [0, 1, 2, 4, 5, 6, 11, 12, 13, 65, 71, 72, 73, 74, 76, 77, 79, 82] {
assert!(bit(n), "service {n} is implemented and must be claimed");
}
for n in [3, 7, 46, 66, 75, 80, 81] {
assert!(!bit(n), "service {n} is not implemented and must not be claimed");
}
assert_eq!(bytes.len() * 8 - usize::from(unused), 84, "the highest claimed bit is 82");
}
#[test]
fn invoke_identifiers_do_not_collide_after_a_wrap() {
let (mut c, mut s) = connect();
let (_, _) = (events(&mut c), events(&mut s));
c.next_invoke = i64::from(u32::MAX) - 1;
let ids: Vec<i64> = (0..4).map(|_| c.call(Instant::ZERO, &ConfirmedRequest::Identify).unwrap()).collect();
assert_eq!(ids, [i64::from(u32::MAX) - 1, i64::from(u32::MAX), 1, 2]);
}
#[test]
fn a_server_with_a_password_refuses_a_client_without_it() {
let with = |password: Option<&str>| AssociationConfig { password: password.map(String::from), ..AssociationConfig::default() };
for (client, admitted) in [(None, false), (Some("wrong"), false), (Some("s3cret"), true)] {
let mut c = Association::client(with(client));
let mut s = Association::server(with(Some("s3cret")));
c.start(Instant(0)).unwrap();
for _ in 0..8 {
while let Some(p) = c.poll_transmit() {
let p = p.to_vec();
s.on_bytes(Instant(1), &p);
}
while let Some(p) = s.poll_transmit() {
let p = p.to_vec();
c.on_bytes(Instant(1), &p);
}
}
let server_events = events(&mut s);
let client_events = events(&mut c);
if admitted {
assert!(server_events.iter().any(|e| matches!(e, AssociationEvent::Established(n) if n.authenticated)), "{server_events:?}");
} else {
assert!(!server_events.iter().any(|e| matches!(e, AssociationEvent::Established(_))), "{client:?}: {server_events:?}");
assert!(client_events.iter().any(|e| matches!(e, AssociationEvent::Refused { layer: "acse", code: Some(_) })), "{client:?}: {client_events:?}");
}
}
assert!(same_secret(b"abc", b"abc") && !same_secret(b"abc", b"abd") && !same_secret(b"abc", b"ab"));
}
}