rustpbx 0.4.4

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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
//! E2E tests for the `CallSessionHook` lifecycle mechanism.
//!
//! Covers:
//! - `on_call_connected` fires after both legs are connected
//! - `on_call_ended` fires during session cleanup with duration info
//! - `on_call_held` / `on_call_unheld` fires via `CallCommand::Hold/Unhold`
//! - Wholesale/trunk scenario: caller is not pre-registered; hook still fires

use super::test_helpers;
use super::test_ua::{TestUa, TestUaConfig, TestUaEvent};
use crate::call::domain::{CallCommand, LegId};
use crate::call::user::SipUser;
use crate::callrecord::CallRecordHangupReason;
use crate::config::{MediaProxyMode, ProxyConfig};
use crate::proxy::routing::TrunkConfig;
use crate::proxy::{
    acl::AclModule,
    auth::AuthModule,
    call::CallModule,
    locator::MemoryLocator,
    proxy_call::session_hooks::{CallSessionContext, CallSessionHook},
    registrar::RegistrarModule,
    server::SipServerBuilder,
    user::MemoryUserBackend,
};
use anyhow::Result;
use async_trait::async_trait;
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tokio::sync::Mutex;
use tokio::time::sleep;
use tokio_util::sync::CancellationToken;
use tracing::info;

// ─── Recording hook ──────────────────────────────────────────────────────────

#[derive(Debug, Clone)]
enum HookEvent {
    Connected(CallSessionContext),
    #[allow(dead_code)]
    Held(CallSessionContext, String),
    #[allow(dead_code)]
    Unheld(CallSessionContext, String),
    Ended(CallSessionContext, u64),
}

#[derive(Clone)]
struct RecordingHook {
    events: Arc<Mutex<Vec<HookEvent>>>,
}

impl RecordingHook {
    fn new() -> (Self, Arc<Mutex<Vec<HookEvent>>>) {
        let events = Arc::new(Mutex::new(Vec::new()));
        (
            Self {
                events: events.clone(),
            },
            events,
        )
    }
}

#[async_trait]
impl CallSessionHook for RecordingHook {
    async fn on_call_connected(&self, ctx: &CallSessionContext) {
        self.events
            .lock()
            .await
            .push(HookEvent::Connected(ctx.clone()));
    }

    async fn on_call_held(&self, ctx: &CallSessionContext, leg_id: &str) {
        self.events
            .lock()
            .await
            .push(HookEvent::Held(ctx.clone(), leg_id.to_string()));
    }

    async fn on_call_unheld(&self, ctx: &CallSessionContext, leg_id: &str) {
        self.events
            .lock()
            .await
            .push(HookEvent::Unheld(ctx.clone(), leg_id.to_string()));
    }

    async fn on_call_ended(
        &self,
        ctx: &CallSessionContext,
        _reason: Option<&CallRecordHangupReason>,
        duration_secs: u64,
    ) {
        self.events
            .lock()
            .await
            .push(HookEvent::Ended(ctx.clone(), duration_secs));
    }
}

// ─── Helpers ─────────────────────────────────────────────────────────────────

fn make_sdp(port: u16) -> String {
    test_helpers::pcmu_sdp("127.0.0.1", port)
}

struct TestEnv {
    port: u16,
    registry: Arc<crate::proxy::active_call_registry::ActiveProxyCallRegistry>,
    events: Arc<Mutex<Vec<HookEvent>>>,
    cancel_token: CancellationToken,
    _server_handle: tokio::task::JoinHandle<()>,
}

impl Drop for TestEnv {
    fn drop(&mut self) {
        self.cancel_token.cancel();
    }
}

async fn start_server_with_hook() -> Result<TestEnv> {
    let port = portpicker::pick_unused_port().unwrap_or(15080);
    let mut proxy_config = test_helpers::test_proxy_config(port);
    proxy_config.media_proxy = MediaProxyMode::Auto;
    proxy_config.ensure_user = Some(false);
    proxy_config.enable_latching = false;
    let config = Arc::new(proxy_config);

    let user_backend = MemoryUserBackend::new(None);
    for user in test_users() {
        user_backend.create_user(user).await?;
    }

    let (hook, events) = RecordingHook::new();
    let cancel_token = CancellationToken::new();

    let builder = test_helpers::register_standard_modules(
        SipServerBuilder::new(config)
            .with_user_backend(Box::new(user_backend))
            .with_locator(Box::new(MemoryLocator::new()))
            .with_cancel_token(cancel_token.clone())
            .with_session_hook(Arc::new(hook)),
    );

    let server = Arc::new(builder.build().await?);
    let registry = server.get_inner().active_call_registry.clone();
    let ct = cancel_token.clone();
    let _server_handle = tokio::spawn(async move {
        tokio::select! {
            _ = ct.cancelled() => {}
            result = server.serve() => {
                if let Err(e) = result {
                    tracing::warn!("Test server error: {:?}", e);
                }
            }
        }
    });

    sleep(Duration::from_millis(200)).await;

    Ok(TestEnv {
        port,
        registry,
        events,
        cancel_token,
        _server_handle,
    })
}

fn test_users() -> Vec<SipUser> {
    test_helpers::standard_test_users()
}

async fn create_ua(port: u16, username: &str, password: &str) -> Result<TestUa> {
    let local_port = portpicker::pick_unused_port().unwrap_or(25100);
    let proxy_addr = format!("127.0.0.1:{}", port).parse()?;
    let config = TestUaConfig {
        username: username.to_string(),
        password: password.to_string(),
        realm: "127.0.0.1".to_string(),
        local_port,
        proxy_addr,
    };
    let mut ua = TestUa::new(config);
    ua.start().await?;
    ua.register().await?;
    Ok(ua)
}

/// Build a TestUa that sends calls WITHOUT registering (simulates trunk / wholesale ingress).
async fn create_unregistered_ua(port: u16, username: &str) -> Result<TestUa> {
    let local_port = portpicker::pick_unused_port().unwrap_or(25200);
    let proxy_addr = format!("127.0.0.1:{}", port).parse()?;
    let config = TestUaConfig {
        username: username.to_string(),
        password: String::new(),
        realm: "127.0.0.1".to_string(),
        local_port,
        proxy_addr,
    };
    let mut ua = TestUa::new(config);
    ua.start().await?;
    // No register() call — behaves like an anonymous trunk caller
    Ok(ua)
}

/// Wait until a specific `HookEvent` variant appears, polling every 50 ms.
async fn wait_for_event<F>(
    events: &Arc<Mutex<Vec<HookEvent>>>,
    matcher: F,
    timeout: Duration,
) -> bool
where
    F: Fn(&HookEvent) -> bool,
{
    let deadline = tokio::time::Instant::now() + timeout;
    while tokio::time::Instant::now() < deadline {
        {
            let guard = events.lock().await;
            if guard.iter().any(&matcher) {
                return true;
            }
        }
        sleep(Duration::from_millis(50)).await;
    }
    false
}

/// Establish a call, returning (alice_dialog_id, bob_dialog_id).
async fn establish_call(
    env: &TestEnv,
    alice: &Arc<TestUa>,
    bob: &TestUa,
) -> Result<(rsipstack::dialog::DialogId, rsipstack::dialog::DialogId)> {
    let alice_sdp = make_sdp(21000);
    let bob_sdp = make_sdp(21010);

    let alice_clone = alice.clone();
    let caller_handle =
        tokio::spawn(async move { alice_clone.make_call("bob", Some(alice_sdp)).await });

    let mut bob_dialog_id = None;
    for _ in 0..50 {
        let events = bob.process_dialog_events().await?;
        for event in events {
            if let TestUaEvent::IncomingCall(id, _) = event {
                bob.answer_call(&id, Some(bob_sdp.clone())).await?;
                bob_dialog_id = Some(id);
                break;
            }
        }
        if bob_dialog_id.is_some() {
            break;
        }
        sleep(Duration::from_millis(50)).await;
    }

    let bob_dialog_id = bob_dialog_id.expect("Bob should receive the INVITE");
    let alice_dialog_id = tokio::time::timeout(Duration::from_secs(5), caller_handle)
        .await
        .expect("make_call timed out")
        .expect("task panicked")
        .expect("make_call failed");

    // Wait for registry entry
    let start = tokio::time::Instant::now();
    while start.elapsed() < Duration::from_secs(3) {
        if !env.registry.is_empty() {
            break;
        }
        sleep(Duration::from_millis(100)).await;
    }
    sleep(Duration::from_millis(200)).await;

    info!(%alice_dialog_id, %bob_dialog_id, "Call established");
    Ok((alice_dialog_id, bob_dialog_id))
}

// ─── Tests ───────────────────────────────────────────────────────────────────

/// `on_call_connected` fires once both legs are established.
#[tokio::test]
async fn test_hook_on_call_connected() -> Result<()> {
    let _ = tracing_subscriber::fmt::try_init();
    let env = start_server_with_hook().await?;

    let alice = Arc::new(create_ua(env.port, "alice", "password123").await?);
    let bob = create_ua(env.port, "bob", "password456").await?;
    sleep(Duration::from_millis(100)).await;

    let _ = establish_call(&env, &alice, &bob).await?;

    let fired = wait_for_event(
        &env.events,
        |e| matches!(e, HookEvent::Connected(_)),
        Duration::from_secs(3),
    )
    .await;
    assert!(fired, "on_call_connected hook should have fired");

    let guard = env.events.lock().await;
    let ctx = guard
        .iter()
        .find_map(|e| {
            if let HookEvent::Connected(ctx) = e {
                Some(ctx.clone())
            } else {
                None
            }
        })
        .unwrap();

    assert!(!ctx.session_id.is_empty(), "session_id should be populated");
    assert!(
        ctx.caller.contains("alice"),
        "caller should contain 'alice', got: {}",
        ctx.caller
    );
    assert!(
        ctx.callee.contains("bob"),
        "callee should contain 'bob', got: {}",
        ctx.callee
    );

    Ok(())
}

/// `on_call_ended` fires after hangup with `duration_secs >= 0`.
#[tokio::test]
async fn test_hook_on_call_ended() -> Result<()> {
    let _ = tracing_subscriber::fmt::try_init();
    let env = start_server_with_hook().await?;

    let alice = Arc::new(create_ua(env.port, "alice", "password123").await?);
    let bob = create_ua(env.port, "bob", "password456").await?;
    sleep(Duration::from_millis(100)).await;

    let (alice_id, _bob_id) = establish_call(&env, &alice, &bob).await?;

    // Let the call run for a moment so duration > 0
    sleep(Duration::from_millis(500)).await;

    // Alice hangs up
    alice.hangup(&alice_id).await?;

    let fired = wait_for_event(
        &env.events,
        |e| matches!(e, HookEvent::Ended(_, _)),
        Duration::from_secs(5),
    )
    .await;
    assert!(fired, "on_call_ended hook should have fired after hangup");

    let guard = env.events.lock().await;
    let (ctx, _duration) = guard
        .iter()
        .find_map(|e| {
            if let HookEvent::Ended(ctx, dur) = e {
                Some((ctx.clone(), *dur))
            } else {
                None
            }
        })
        .unwrap();

    assert!(!ctx.session_id.is_empty(), "session_id should be populated");

    Ok(())
}

/// `on_call_held` and `on_call_unheld` fire when `CallCommand::Hold/Unhold` is sent.
#[tokio::test]
async fn test_hook_on_hold_unhold() -> Result<()> {
    let _ = tracing_subscriber::fmt::try_init();
    let env = start_server_with_hook().await?;

    let alice = Arc::new(create_ua(env.port, "alice", "password123").await?);
    let bob = create_ua(env.port, "bob", "password456").await?;
    sleep(Duration::from_millis(100)).await;

    let _ = establish_call(&env, &alice, &bob).await?;

    // Get the session handle from the registry
    let session_id = {
        let calls = env.registry.list_recent(1);
        calls
            .into_iter()
            .next()
            .map(|e| e.session_id)
            .expect("No active session found")
    };
    info!(%session_id, "Got active session");

    let handle = env
        .registry
        .get_handle(&session_id)
        .expect("Session handle should exist");

    // Pump Alice's dialog events in background so she can respond to proxy-initiated re-INVITEs
    let alice_bg = alice.clone();
    let pump_token = CancellationToken::new();
    let pump_cancel = pump_token.clone();
    tokio::spawn(async move {
        loop {
            tokio::select! {
                _ = pump_cancel.cancelled() => break,
                _ = sleep(Duration::from_millis(30)) => {
                    let _ = alice_bg.process_dialog_events().await;
                }
            }
        }
    });

    // Send Hold command
    let caller_leg = LegId::new("caller");
    handle
        .send_command(CallCommand::Hold {
            leg_id: caller_leg.clone(),
            music: None,
        })
        .expect("send_command(Hold) should succeed");

    let held_fired = wait_for_event(
        &env.events,
        |e| matches!(e, HookEvent::Held(_, _)),
        Duration::from_secs(5),
    )
    .await;
    assert!(held_fired, "on_call_held hook should have fired");

    // Send Unhold command
    handle
        .send_command(CallCommand::Unhold { leg_id: caller_leg })
        .expect("send_command(Unhold) should succeed");

    let unheld_fired = wait_for_event(
        &env.events,
        |e| matches!(e, HookEvent::Unheld(_, _)),
        Duration::from_secs(5),
    )
    .await;
    assert!(unheld_fired, "on_call_unheld hook should have fired");

    pump_token.cancel();
    Ok(())
}

/// Wholesale / trunk scenario: caller is NOT registered.
/// The hook must still fire for `on_call_connected` and `on_call_ended`.
#[tokio::test]
async fn test_hook_wholesale_caller_not_registered() -> Result<()> {
    let _ = tracing_subscriber::fmt::try_init();

    // Build a server that has a trunk trusting 127.0.0.1 so the unregistered
    // caller is accepted without SIP authentication.
    let port = portpicker::pick_unused_port().unwrap_or(15090);
    let mut trunks = HashMap::new();
    trunks.insert(
        "test-trunk".to_string(),
        TrunkConfig {
            dest: format!("sip:127.0.0.1:{}", port),
            inbound_hosts: vec!["127.0.0.1".to_string()],
            ..Default::default()
        },
    );

    let mut proxy_config = test_helpers::test_proxy_config(port);
    proxy_config.modules = Some(vec![
        "acl".to_string(),
        "auth".to_string(),
        "registrar".to_string(),
        "call".to_string(),
    ]);
    proxy_config.media_proxy = MediaProxyMode::Auto;
    proxy_config.ensure_user = Some(false);
    proxy_config.enable_latching = false;
    proxy_config.trunks = trunks;
    let config = Arc::new(proxy_config);

    let user_backend = MemoryUserBackend::new(None);
    for user in test_users() {
        user_backend.create_user(user).await?;
    }

    let (hook, events) = RecordingHook::new();
    let cancel_token = CancellationToken::new();

    let builder = SipServerBuilder::new(config)
        .with_user_backend(Box::new(user_backend))
        .with_locator(Box::new(MemoryLocator::new()))
        .with_cancel_token(cancel_token.clone())
        .with_session_hook(Arc::new(hook))
        .register_module("acl", |inner, config| AclModule::create(inner, config))
        .register_module("registrar", |inner, config| {
            Ok(Box::new(RegistrarModule::new(inner, config)))
        })
        .register_module("auth", |inner, _config| {
            Ok(Box::new(AuthModule::new(
                inner.clone(),
                inner.proxy_config.clone(),
            )))
        })
        .register_module("call", |inner, config| {
            Ok(Box::new(CallModule::new(config, inner)))
        });

    let server = Arc::new(builder.build().await?);
    let _registry = server.get_inner().active_call_registry.clone();
    let ct = cancel_token.clone();
    let _server_handle = tokio::spawn(async move {
        tokio::select! {
            _ = ct.cancelled() => {}
            result = server.serve() => {
                if let Err(e) = result {
                    tracing::warn!("Test server error: {:?}", e);
                }
            }
        }
    });
    sleep(Duration::from_millis(200)).await;

    // Bob registers; "trunk" caller does not
    let bob = create_ua(port, "bob", "password456").await?;
    let trunk_caller = Arc::new(create_unregistered_ua(port, "trunk").await?);
    sleep(Duration::from_millis(100)).await;

    let trunk_sdp = make_sdp(22000);
    let bob_sdp = make_sdp(22010);

    let caller_clone = trunk_caller.clone();
    let caller_handle =
        tokio::spawn(async move { caller_clone.make_call("bob", Some(trunk_sdp)).await });

    let mut bob_dialog_id = None;
    for _ in 0..50 {
        let events_polled = bob.process_dialog_events().await?;
        for event in events_polled {
            if let TestUaEvent::IncomingCall(id, _) = event {
                bob.answer_call(&id, Some(bob_sdp.clone())).await?;
                bob_dialog_id = Some(id);
                break;
            }
        }
        if bob_dialog_id.is_some() {
            break;
        }
        sleep(Duration::from_millis(50)).await;
    }

    let bob_dialog_id = bob_dialog_id.expect("Bob should receive the INVITE from trunk");
    let trunk_dialog_id = tokio::time::timeout(Duration::from_secs(5), caller_handle)
        .await
        .expect("make_call timed out")
        .expect("task panicked")
        .expect("make_call failed");

    sleep(Duration::from_millis(500)).await;

    // Verify connected hook fires
    let connected_fired = wait_for_event(
        &events,
        |e| matches!(e, HookEvent::Connected(_)),
        Duration::from_secs(3),
    )
    .await;
    assert!(
        connected_fired,
        "on_call_connected hook should fire for unregistered (wholesale) caller"
    );

    // Hang up
    trunk_caller.hangup(&trunk_dialog_id).await?;
    let _ = bob_dialog_id;

    let ended_fired = wait_for_event(
        &events,
        |e| matches!(e, HookEvent::Ended(_, _)),
        Duration::from_secs(5),
    )
    .await;
    assert!(
        ended_fired,
        "on_call_ended hook should fire for wholesale call after hangup"
    );

    cancel_token.cancel();
    Ok(())
}