simulcrypt 0.1.1

DVB SimulCrypt (ETSI TS 103 197) head-end protocol: ECMG client, EMMG server, control words
Documentation
use std::{
    net::{
        IpAddr,
        SocketAddr,
    },
    time::Duration,
};

use crate::{
    CasRef,
    Error,
};

pub(super) const DEFAULT_RECONNECT_DELAY: Duration = Duration::from_secs(5);
pub(super) const DEFAULT_RESPONSE_TIMEOUT: Duration = Duration::from_secs(5);
pub(super) const DEFAULT_IDLE_PROBE_INTERVAL: Duration = Duration::from_secs(30);

/// One CAS description: the ECMG client and an optional EMMG server.
#[derive(Clone, Debug, PartialEq)]
pub struct CasConfig {
    /// CAS key: a repeated `add_cas` with the same `id` updates this CAS and returns its
    /// `CasRef`.
    pub id: String,
    /// Channel name for logs and `CasStatus::name`.
    pub name: String,
    /// `CA_system_id << 16 | subsystem_id`.
    pub super_cas_id: u32,
    /// First `stream_id` the crate assigns on this channel (usually 0 or 1).
    pub first_stream_id: u16,
    /// ECMG connection settings.
    pub ecmg: EcmgConfig,
    /// EMMG server settings, if this CAS accepts EMM.
    pub emmg: Option<EmmgConfig>,
}

/// ECMG server connection settings.
#[derive(Clone, Debug, PartialEq)]
pub struct EcmgConfig {
    /// `channel_id` as defined by ETSI TS 103 197.
    pub channel_id: u16,
    /// ECMG server address; name resolution is up to the caller.
    pub addr: SocketAddr,
    /// Delay before a reconnect attempt after the connection is lost.
    pub reconnect_delay: Duration,
    /// Timeout for the TCP connect and for a reply to a request (handshake, stream setup).
    pub response_timeout: Duration,
    /// How long to wait without a single message from the ECMG before sending our own
    /// `CHANNEL_TEST` - detects a half-open connection (TCP looks established, but no data flows
    /// in either direction) while idle between ECMs.
    pub idle_probe_interval: Duration,
}

impl EcmgConfig {
    /// Creates a configuration with the default reconnect delay and response timeout (5 seconds)
    /// and idle probe interval (30 seconds).
    pub fn new(channel_id: u16, addr: SocketAddr) -> Self {
        Self {
            channel_id,
            addr,
            reconnect_delay: DEFAULT_RECONNECT_DELAY,
            response_timeout: DEFAULT_RESPONSE_TIMEOUT,
            idle_probe_interval: DEFAULT_IDLE_PROBE_INTERVAL,
        }
    }
}

/// EMMG server settings of one CAS channel.
#[derive(Clone, Debug, PartialEq)]
pub struct EmmgConfig {
    /// Address to listen on for incoming EMMG connections.
    pub bind: IpAddr,
    /// TCP (and UDP, see `udp`) port. CAS channels with the same port share the listening socket.
    pub port: u16,
    /// Also accept `data_provision` datagrams on UDP `bind:port`; channel and stream setup always
    /// use TCP, and a datagram is accepted only from the IP of the TCP connection that set its
    /// channel up.
    pub udp: bool,
    /// Deliver EMM to every stream of the channel, including streams the EMM generator did not
    /// set up, not only to the stream it was sent on.
    pub broadcast: bool,
}

impl EmmgConfig {
    /// Creates a configuration on `0.0.0.0:port`, TCP only, without broadcast.
    #[must_use]
    pub fn new(port: u16) -> Self {
        Self {
            bind: IpAddr::from([0, 0, 0, 0]),
            port,
            udp: false,
            broadcast: false,
        }
    }
}

/// Scrambled service: one CW group shared by any number of CAS.
#[derive(Clone, Debug)]
pub struct ServiceConfig {
    /// Service name for logs.
    pub name: String,
    /// Crypto period shared by the whole CW group.
    pub crypto_period: Duration,
    /// CAS channels scrambling this service.
    pub cas: Vec<ServiceCas>,
}

impl ServiceConfig {
    /// `crypto_period` in `CW_PROVISION/STREAM_SETUP` units - hundreds of milliseconds.
    pub(super) fn nominal_cp_duration(&self) -> Result<u16, Error> {
        let hundred_ms = self.crypto_period.as_millis() / 100;
        u16::try_from(hundred_ms).map_err(|_| Error::InvalidCryptoPeriod {
            crypto_period: self.crypto_period,
        })
    }
}

/// One CAS channel of a service.
#[derive(Clone, Debug)]
pub struct ServiceCas {
    /// Channel returned by `Simulcrypt::add_cas`.
    pub cas: CasRef,
    /// Stream deduplication key within the CAS channel. Not sent on the wire.
    pub ecm_pid: u16,
    /// Access criteria sent in `CW_PROVISION` for this channel.
    pub access_criteria: Vec<u8>,
}