use crate::secure_channel::{Addresses, RemoteRoute};
use crate::{Identifier, SecureChannelOptions};
use core::fmt;
use core::fmt::Formatter;
use minicbor::{CborLen, Decode, Encode};
use ockam_core::compat::sync::{Arc, RwLock};
use ockam_core::compat::vec::Vec;
use ockam_core::flow_control::{FlowControlId, FlowControls};
use ockam_core::{cbor_encode_preallocate, Message};
use ockam_core::{Address, Decodable, Encodable, Encoded, Result, Route};
use serde::Serialize;
#[derive(Debug, Clone)]
pub struct SecureChannel {
flow_controls: FlowControls,
their_identifier: Identifier,
encryptor_remote_route: Arc<RwLock<RemoteRoute>>,
addresses: Addresses,
is_key_exchange_only: bool,
flow_control_id: FlowControlId,
}
impl From<SecureChannel> for Address {
fn from(value: SecureChannel) -> Self {
value.addresses.encryptor
}
}
impl AsRef<Address> for SecureChannel {
fn as_ref(&self) -> &Address {
&self.addresses.encryptor
}
}
impl fmt::Display for SecureChannel {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"Encryptor: {}, FlowId: {}",
self.addresses.encryptor, self.flow_control_id
)
}
}
impl SecureChannel {
pub(crate) fn new(
flow_controls: FlowControls,
their_identifier: Identifier,
encryptor_remote_route: Arc<RwLock<RemoteRoute>>,
addresses: Addresses,
is_key_exchange_only: bool,
flow_control_id: FlowControlId,
) -> Self {
Self {
flow_controls,
their_identifier,
encryptor_remote_route,
addresses,
is_key_exchange_only,
flow_control_id,
}
}
pub fn encryptor_address(&self) -> &Address {
&self.addresses.encryptor
}
pub fn flow_control_id(&self) -> &FlowControlId {
&self.flow_control_id
}
pub fn encryptor_api_address(&self) -> &Address {
&self.addresses.encryptor_api
}
pub fn decryptor_api_address(&self) -> &Address {
&self.addresses.decryptor_api
}
pub fn decryptor_remote_address(&self) -> &Address {
&self.addresses.decryptor_remote
}
pub fn update_remote_node_route(&self, new_route: Route) -> Result<()> {
let next = new_route.next().ok().cloned();
let mut remote_route = self.encryptor_remote_route.write().unwrap();
let old_route = remote_route.clone();
let their_decryptor_address = old_route.route.recipient()?.clone();
let new_route = new_route + their_decryptor_address;
remote_route.route = new_route;
if let Some(next) = next {
SecureChannelOptions::setup_flow_control_consumer(
&self.flow_controls,
&self.addresses,
&next,
);
}
Ok(())
}
pub fn is_key_exchange_only(&self) -> bool {
self.is_key_exchange_only
}
pub fn their_identifier(&self) -> &Identifier {
&self.their_identifier
}
}
#[derive(Debug, Clone, Encode, Decode, CborLen, Serialize, Message)]
#[rustfmt::skip]
#[cbor(map)]
pub struct SecureChannelListener {
#[n(1)] address: Address,
#[n(2)] flow_control_id: FlowControlId,
#[n(3)] is_key_exchange_only: bool,
}
impl Encodable for SecureChannelListener {
fn encode(self) -> Result<Encoded> {
cbor_encode_preallocate(self)
}
}
impl Decodable for SecureChannelListener {
fn decode(e: &[u8]) -> Result<Self> {
Ok(minicbor::decode(e)?)
}
}
impl fmt::Display for SecureChannelListener {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(
f,
"Worker: {}, FlowId: {}",
self.address, self.flow_control_id
)
}
}
impl SecureChannelListener {
pub fn new(
address: Address,
is_key_exchange_only: bool,
flow_control_id: FlowControlId,
) -> Self {
Self {
address,
is_key_exchange_only,
flow_control_id,
}
}
pub fn address(&self) -> &Address {
&self.address
}
pub fn flow_control_id(&self) -> &FlowControlId {
&self.flow_control_id
}
pub fn is_key_exchange_only(&self) -> bool {
self.is_key_exchange_only
}
}
#[derive(Encode, Decode, CborLen, Debug, Default, Clone, Message)]
pub struct SecureChannelListenerList(#[n(0)] pub Vec<SecureChannelListener>);
impl Encodable for SecureChannelListenerList {
fn encode(self) -> Result<Encoded> {
cbor_encode_preallocate(self)
}
}
impl Decodable for SecureChannelListenerList {
fn decode(e: &[u8]) -> Result<Self> {
Ok(minicbor::decode(e)?)
}
}