rustpbx 0.4.9

A SIP PBX implementation in Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
/// Outbound SIP REGISTER client for trunk registration.
///
/// When a trunk has `register_enabled = true`, this module will periodically
/// send REGISTER requests to the trunk's SIP server via rsipstack's
/// `Registration` client, handle digest authentication challenges, and
/// refresh the registration before it expires.
///
/// On reload, registrations for removed trunks are cancelled (un-REGISTER with
/// Expires: 0) and new/changed trunks with registration enabled are started.
use crate::proxy::routing::TrunkConfig;
use anyhow::Result;
use chrono::{DateTime, Utc};
use rsipstack::sip::StatusCode;
use rsipstack::{
    dialog::{authenticate::Credential, registration::Registration},
    transaction::endpoint::EndpointInnerRef,
};
use serde::Serialize;
use std::{
    collections::HashMap,
    sync::{Arc, RwLock},
    time::Duration,
};
use tokio::time::sleep;
use tokio_util::sync::CancellationToken;
use tracing::{debug, error, info, warn};

/// Default REGISTER expiry in seconds.
const DEFAULT_REGISTER_EXPIRES: u32 = 300;
/// Safety margin: refresh the registration this many seconds before it expires.
const REFRESH_MARGIN_SECS: u32 = 30;

/// Runtime status for a single trunk registration.
#[derive(Debug, Clone, Serialize)]
pub struct TrunkRegistrationStatus {
    pub trunk_name: String,
    pub registered: bool,
    pub last_register_at: Option<DateTime<Utc>>,
    pub expires: Option<u32>,
    pub error: Option<String>,
    pub remote_addr: Option<String>,
}

/// Manages outbound trunk registrations.
pub struct TrunkRegistrar {
    statuses: Arc<RwLock<HashMap<String, TrunkRegistrationStatus>>>,
    cancel_tokens: Arc<RwLock<HashMap<String, CancellationToken>>>,
    /// Global cancel token for the registrar (parent for all per-trunk tokens).
    parent_cancel: CancellationToken,
    /// SIP endpoint reference for creating transactions (set after server build).
    endpoint: RwLock<Option<EndpointInnerRef>>,
}

impl Default for TrunkRegistrar {
    fn default() -> Self {
        Self::new()
    }
}

impl TrunkRegistrar {
    pub fn new() -> Self {
        Self {
            statuses: Arc::new(RwLock::new(HashMap::new())),
            cancel_tokens: Arc::new(RwLock::new(HashMap::new())),
            parent_cancel: CancellationToken::new(),
            endpoint: RwLock::new(None),
        }
    }

    /// Set the SIP endpoint reference.  Must be called after the SipServer is
    /// built, before the first `reconcile`.
    pub fn set_endpoint(&self, endpoint: EndpointInnerRef) {
        *self.endpoint.write().unwrap() = Some(endpoint);
    }

    /// Get the registration status of all trunks.
    pub fn get_statuses(&self) -> HashMap<String, TrunkRegistrationStatus> {
        self.statuses.read().unwrap().clone()
    }

    /// Get the registration status of a specific trunk.
    pub fn get_status(&self, trunk_name: &str) -> Option<TrunkRegistrationStatus> {
        self.statuses.read().unwrap().get(trunk_name).cloned()
    }

    /// Reconcile registrations with the current set of trunks.
    ///
    /// - Starts registration for new trunks with `register_enabled`.
    /// - Sends un-REGISTER for trunks that were removed or had registration disabled.
    /// - Re-starts registration for trunks whose config changed.
    pub async fn reconcile(&self, trunks: &HashMap<String, TrunkConfig>) {
        let endpoint = { self.endpoint.read().unwrap().clone() };
        let Some(endpoint) = endpoint else {
            debug!("trunk registrar: no endpoint set yet, skipping reconcile");
            return;
        };

        let current_names: Vec<String> =
            { self.cancel_tokens.read().unwrap().keys().cloned().collect() };

        // Determine which trunks need registration.
        let mut desired: HashMap<String, &TrunkConfig> = HashMap::new();
        for (name, config) in trunks.iter() {
            if config.register_enabled.unwrap_or(false)
                && config.disabled.map(|d| !d).unwrap_or(true)
            {
                desired.insert(name.clone(), config);
            }
        }

        // Cancel registrations for removed / disabled trunks.
        for name in &current_names {
            if !desired.contains_key(name) {
                info!(trunk = %name, "cancelling trunk registration (removed or disabled)");
                self.stop_registration(name).await;
            }
        }

        // Start / restart registrations for desired trunks.
        for (name, config) in &desired {
            let already_running = self
                .cancel_tokens
                .read()
                .unwrap()
                .contains_key(name.as_str());
            if !already_running {
                info!(trunk = %name, dest = %config.dest, "starting trunk registration");
                self.start_registration(name.clone(), (*config).clone(), endpoint.clone());
            }
        }
    }

    /// Stop all registrations (used on shutdown).
    pub async fn stop_all(&self) {
        self.parent_cancel.cancel();
        let names: Vec<String> = self.cancel_tokens.read().unwrap().keys().cloned().collect();
        for name in names {
            self.stop_registration(&name).await;
        }
    }

    /// Stop registration for a single trunk, sending un-REGISTER.
    async fn stop_registration(&self, name: &str) {
        let token = { self.cancel_tokens.write().unwrap().remove(name) };
        if let Some(token) = token {
            token.cancel();
        }
        self.statuses.write().unwrap().remove(name);
    }

    /// Spawn a background task for trunk registration.
    fn start_registration(&self, name: String, config: TrunkConfig, endpoint: EndpointInnerRef) {
        let child_token = self.parent_cancel.child_token();
        {
            self.cancel_tokens
                .write()
                .unwrap()
                .insert(name.clone(), child_token.clone());
        }

        let statuses = self.statuses.clone();

        crate::utils::spawn(async move {
            registration_loop(name, config, child_token, statuses, endpoint).await;
        });
    }
}

/// Normalize a trunk destination string into an `rsipstack::sip::Uri`.
///
/// Handles bare `host:port`, strips angle brackets, and prepends `sip:` if no
/// scheme is present.  The `user@` part (if any) is kept — `Registration`
/// overwrites `to.uri.auth` from the credential anyway.
fn parse_server_uri(dest: &str) -> Result<rsipstack::sip::Uri> {
    let trimmed = dest.trim().trim_matches(|c| c == '<' || c == '>');
    let uri_str = if trimmed.starts_with("sip:") || trimmed.starts_with("sips:") {
        trimmed.to_string()
    } else {
        format!("sip:{}", trimmed)
    };
    rsipstack::sip::Uri::try_from(uri_str.as_str())
        .map_err(|e| anyhow::anyhow!("invalid SIP URI '{}': {}", uri_str, e))
}

/// Long-running loop that maintains registration for a single trunk.
async fn registration_loop(
    name: String,
    config: TrunkConfig,
    cancel: CancellationToken,
    statuses: Arc<RwLock<HashMap<String, TrunkRegistrationStatus>>>,
    endpoint: EndpointInnerRef,
) {
    let expires = config.register_expires.unwrap_or(DEFAULT_REGISTER_EXPIRES);
    let refresh_interval = if expires > REFRESH_MARGIN_SECS {
        Duration::from_secs((expires - REFRESH_MARGIN_SECS) as u64)
    } else {
        Duration::from_secs(expires as u64 / 2)
    };

    // Build server URI.
    let server_uri = match parse_server_uri(&config.dest) {
        Ok(uri) => uri,
        Err(err) => {
            error!(trunk = %name, error = %err, "failed to parse trunk destination for registration");
            update_status(
                &statuses,
                &name,
                false,
                None,
                Some(format!("URI parse error: {err}")),
                None,
            );
            return;
        }
    };

    // Build credential if the trunk has auth configured.
    let credential = match (&config.username, &config.password) {
        (Some(user), Some(pass)) => Some(Credential {
            username: user.clone(),
            password: pass.clone(),
            realm: None, // extracted from server challenge
        }),
        (Some(user), None) => Some(Credential {
            username: user.clone(),
            password: String::new(),
            realm: None,
        }),
        _ => None,
    };

    let mut registration = Registration::new(endpoint, credential);

    let remote_str = server_uri.to_string();

    loop {
        let result = do_register(&name, &mut registration, &server_uri, expires).await;

        match result {
            Ok(actual_expires) => {
                update_status(
                    &statuses,
                    &name,
                    true,
                    Some(actual_expires),
                    None,
                    Some(remote_str.clone()),
                );
                info!(trunk = %name, expires = actual_expires, "trunk registration successful");
            }
            Err(err) => {
                update_status(
                    &statuses,
                    &name,
                    false,
                    None,
                    Some(err.to_string()),
                    Some(remote_str.clone()),
                );
                warn!(trunk = %name, error = %err, "trunk registration failed");
            }
        }

        tokio::select! {
            _ = cancel.cancelled() => {
                // Send un-REGISTER on cancellation.
                info!(trunk = %name, "sending un-REGISTER before shutdown");
                let _ = do_register(&name, &mut registration, &server_uri, 0).await;
                update_status(&statuses, &name, false, None, Some("cancelled".to_string()), None);
                return;
            }
            _ = sleep(refresh_interval) => {
                debug!(trunk = %name, "refreshing trunk registration");
            }
        }
    }
}

/// Perform a single REGISTER transaction via rsipstack's `Registration`.
///
/// Returns the effective expires value from the server response on success.
async fn do_register(
    trunk_name: &str,
    registration: &mut Registration,
    server_uri: &rsipstack::sip::Uri,
    expires: u32,
) -> Result<u32> {
    let response = registration
        .register(server_uri.clone(), Some(expires))
        .await
        .map_err(|e| anyhow::anyhow!("REGISTER transaction error: {e}"))?;

    match response.status_code {
        StatusCode::OK => {
            let actual_expires = registration.expires();
            debug!(trunk = %trunk_name, actual_expires, "REGISTER 200 OK");
            Ok(actual_expires)
        }
        code => Err(anyhow::anyhow!("REGISTER rejected: {}", code)),
    }
}

fn update_status(
    statuses: &Arc<RwLock<HashMap<String, TrunkRegistrationStatus>>>,
    name: &str,
    registered: bool,
    expires: Option<u32>,
    error: Option<String>,
    remote_addr: Option<String>,
) {
    let mut map = statuses.write().unwrap();
    let entry = map
        .entry(name.to_string())
        .or_insert_with(|| TrunkRegistrationStatus {
            trunk_name: name.to_string(),
            registered: false,
            last_register_at: None,
            expires: None,
            error: None,
            remote_addr: None,
        });
    entry.registered = registered;
    entry.last_register_at = Some(Utc::now());
    entry.expires = expires;
    entry.error = error;
    if let Some(addr) = remote_addr {
        entry.remote_addr = Some(addr);
    }
}

// ---------- Tests ----------

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_server_uri() {
        let uri = parse_server_uri("sip:provider.com").unwrap();
        assert_eq!(uri.to_string(), "sip:provider.com");

        let uri = parse_server_uri("sip:provider.com:5060").unwrap();
        assert_eq!(uri.to_string(), "sip:provider.com:5060");

        let uri = parse_server_uri("sip:user@provider.com").unwrap();
        assert_eq!(uri.to_string(), "sip:user@provider.com");

        let uri = parse_server_uri("10.0.0.1:5060").unwrap();
        assert_eq!(uri.to_string(), "sip:10.0.0.1:5060");

        let uri = parse_server_uri("provider.com").unwrap();
        assert_eq!(uri.to_string(), "sip:provider.com");
    }

    #[test]
    fn test_trunk_registrar_creation() {
        let registrar = TrunkRegistrar::new();
        let statuses = registrar.get_statuses();
        assert!(statuses.is_empty());
        assert!(registrar.get_status("nonexistent").is_none());
    }

    #[test]
    fn test_update_status() {
        let statuses = Arc::new(RwLock::new(HashMap::new()));

        update_status(
            &statuses,
            "trunk1",
            true,
            Some(300),
            None,
            Some("sip:1.2.3.4:5060".to_string()),
        );

        let map = statuses.read().unwrap();
        let s = map.get("trunk1").unwrap();
        assert!(s.registered);
        assert_eq!(s.expires, Some(300));
        assert!(s.error.is_none());
        assert_eq!(s.remote_addr.as_deref(), Some("sip:1.2.3.4:5060"));
        assert!(s.last_register_at.is_some());
    }

    #[test]
    fn test_update_status_error() {
        let statuses = Arc::new(RwLock::new(HashMap::new()));

        update_status(
            &statuses,
            "trunk2",
            false,
            None,
            Some("timeout".to_string()),
            Some("sip:1.2.3.4:5060".to_string()),
        );

        let map = statuses.read().unwrap();
        let s = map.get("trunk2").unwrap();
        assert!(!s.registered);
        assert_eq!(s.error.as_deref(), Some("timeout"));
    }

    #[tokio::test]
    async fn test_reconcile_without_endpoint_is_noop() {
        let registrar = TrunkRegistrar::new();

        let mut trunks = HashMap::new();
        trunks.insert(
            "test-trunk".to_string(),
            TrunkConfig {
                dest: "sip:192.0.2.1:5060".to_string(),
                username: Some("user".to_string()),
                password: Some("pass".to_string()),
                register_enabled: Some(true),
                register_expires: Some(60),
                ..Default::default()
            },
        );

        // Without endpoint set, reconcile should be a no-op.
        registrar.reconcile(&trunks).await;
        {
            let tokens = registrar.cancel_tokens.read().unwrap();
            assert!(tokens.is_empty());
        }
    }

    #[tokio::test]
    async fn test_reconcile_before_set_endpoint_then_after() {
        let registrar = TrunkRegistrar::new();

        let mut trunks = HashMap::new();
        trunks.insert(
            "trunk-a".to_string(),
            TrunkConfig {
                dest: "sip:192.0.2.1:5060".to_string(),
                username: Some("user".to_string()),
                password: Some("pass".to_string()),
                register_enabled: Some(true),
                register_expires: Some(60),
                ..Default::default()
            },
        );

        // First call without endpoint — must be silent no-op.
        registrar.reconcile(&trunks).await;
        assert!(
            registrar.cancel_tokens.read().unwrap().is_empty(),
            "no token should be created before set_endpoint"
        );

        // Status should also be absent.
        assert!(registrar.get_status("trunk-a").is_none());
    }

    /// A disabled trunk must not be registered even when register_enabled=true.
    #[tokio::test]
    async fn test_reconcile_disabled_trunk_is_skipped() {
        let registrar = TrunkRegistrar::new();

        let mut trunks = HashMap::new();
        trunks.insert(
            "disabled-trunk".to_string(),
            TrunkConfig {
                dest: "sip:192.0.2.1:5060".to_string(),
                username: Some("u".to_string()),
                password: Some("p".to_string()),
                register_enabled: Some(true),
                disabled: Some(true),
                ..Default::default()
            },
        );

        registrar.reconcile(&trunks).await;
        assert!(
            registrar.cancel_tokens.read().unwrap().is_empty(),
            "disabled trunk must not spawn a registration task"
        );
    }
}