use crate::transport::{SipAddr, SipConnection};
use key::TransactionKey;
use std::time::Duration;
use tokio::sync::mpsc::{UnboundedReceiver, UnboundedSender};
pub use transaction::Transaction;
pub mod endpoint;
pub mod key;
pub mod message;
mod timer;
pub mod transaction;
pub use endpoint::Endpoint;
pub use endpoint::EndpointBuilder;
#[cfg(test)]
mod tests;
pub const TO_TAG_LEN: usize = 8;
pub const BRANCH_LEN: usize = 12;
pub const CNONCE_LEN: usize = 8;
pub const CALL_ID_LEN: usize = 22;
pub const DEFAULT_CALLID_SUFFIX: &str = "restsend.com";
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub enum CallIdFormat {
#[default]
UuidWithSuffix,
Uuid,
}
pub struct IncomingRequest {
pub request: crate::sip::Request,
pub connection: SipConnection,
pub from: SipAddr,
}
pub type TransactionReceiver = UnboundedReceiver<Transaction>;
pub type TransactionSender = UnboundedSender<Transaction>;
#[derive(Debug, Clone, PartialEq)]
pub enum TransactionState {
Nothing,
Calling,
Trying,
Proceeding,
Completed,
Confirmed,
Terminated,
}
impl std::fmt::Display for TransactionState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TransactionState::Nothing => write!(f, "Nothing"),
TransactionState::Calling => write!(f, "Calling"),
TransactionState::Trying => write!(f, "Trying"),
TransactionState::Proceeding => write!(f, "Proceeding"),
TransactionState::Completed => write!(f, "Completed"),
TransactionState::Confirmed => write!(f, "Confirmed"),
TransactionState::Terminated => write!(f, "Terminated"),
}
}
}
#[derive(Debug, PartialEq)]
pub enum TransactionType {
ClientInvite,
ClientNonInvite,
ServerInvite,
ServerNonInvite,
}
impl std::fmt::Display for TransactionType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TransactionType::ClientInvite => write!(f, "ClientInvite"),
TransactionType::ClientNonInvite => write!(f, "ClientNonInvite"),
TransactionType::ServerInvite => write!(f, "ServerInvite"),
TransactionType::ServerNonInvite => write!(f, "ServerNonInvite"),
}
}
}
pub enum TransactionTimer {
TimerA(TransactionKey, Duration),
TimerB(TransactionKey),
TimerC(TransactionKey),
TimerD(TransactionKey),
TimerK(TransactionKey),
TimerG(TransactionKey, Duration),
TimerCleanup(TransactionKey),
}
impl TransactionTimer {
pub fn key(&self) -> &TransactionKey {
match self {
TransactionTimer::TimerA(key, _) => key,
TransactionTimer::TimerB(key) => key,
TransactionTimer::TimerC(key) => key,
TransactionTimer::TimerD(key) => key,
TransactionTimer::TimerG(key, _) => key,
TransactionTimer::TimerK(key) => key,
TransactionTimer::TimerCleanup(key) => key,
}
}
}
impl std::fmt::Display for TransactionTimer {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
TransactionTimer::TimerA(key, duration) => {
write!(f, "TimerA: {} {}", key, duration.as_millis())
}
TransactionTimer::TimerB(key) => write!(f, "TimerB: {}", key),
TransactionTimer::TimerC(key) => write!(f, "TimerC: {}", key),
TransactionTimer::TimerD(key) => write!(f, "TimerD: {}", key),
TransactionTimer::TimerG(key, duration) => {
write!(f, "TimerG: {} {}", key, duration.as_millis())
}
TransactionTimer::TimerK(key) => write!(f, "TimerK: {}", key),
TransactionTimer::TimerCleanup(key) => write!(f, "TimerCleanup: {}", key),
}
}
}
pub fn make_via_branch() -> crate::sip::Param {
crate::sip::Param::Branch(format!("z9hG4bK{}", random_text(BRANCH_LEN)).into())
}
pub fn make_call_id(suffix: Option<&str>, format: CallIdFormat) -> crate::sip::headers::CallId {
match format {
CallIdFormat::UuidWithSuffix => format!(
"{}@{}",
make_uuid_v4(),
suffix.unwrap_or(DEFAULT_CALLID_SUFFIX)
)
.into(),
CallIdFormat::Uuid => make_uuid_v4().into(),
}
}
pub fn make_uuid_v4() -> String {
let mut b = [0u8; 16];
fill_random_bytes(&mut b);
b[6] = (b[6] & 0x0f) | 0x40;
b[8] = (b[8] & 0x3f) | 0x80;
format!(
"{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
b[0], b[1], b[2], b[3], b[4], b[5], b[6], b[7], b[8], b[9], b[10], b[11], b[12], b[13],
b[14], b[15]
)
}
#[cfg(not(target_family = "wasm"))]
fn fill_random_bytes(buf: &mut [u8]) {
use rand::Rng;
rand::rng().fill_bytes(buf);
}
#[cfg(target_family = "wasm")]
fn fill_random_bytes(buf: &mut [u8]) {
for byte in buf.iter_mut() {
*byte = (js_sys::Math::random() * 256.0) as u8;
}
}
pub fn make_tag() -> crate::sip::param::Tag {
random_text(TO_TAG_LEN).into()
}
#[cfg(not(target_family = "wasm"))]
pub fn random_text(count: usize) -> String {
use rand::RngExt;
rand::rng()
.sample_iter(rand::distr::Alphanumeric)
.take(count)
.map(char::from)
.collect::<String>()
}
#[cfg(target_family = "wasm")]
pub fn random_text(count: usize) -> String {
(0..count)
.map(|_| {
let r = js_sys::Math::random();
let c = (r * 16.0) as u8;
if c < 10 {
(c + 48) as char
} else {
(c + 87) as char
}
})
.collect()
}