xphone 0.6.0

SIP telephony library with event-driven API — handles SIP signaling, RTP media, codecs, and call state
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
//! TURN/ICE integration tests against a running CoTURN server.
//!
//! Start with: cd testutil/docker && docker compose up -d
//! Run with:   cargo test --features integration --test turn_test -- --nocapture --test-threads=1
//! Stop with:  cd testutil/docker && docker compose down
//!
//! Environment variables:
//!   TURN_HOST     — TURN server address (default: 127.0.0.1)
//!   TURN_PORT     — TURN server port (default: 3478)
//!   TURN_USER     — TURN username (default: testuser)
//!   TURN_PASS     — TURN password (default: testpass)

#![cfg(feature = "integration")]

use std::net::{SocketAddr, UdpSocket};
use std::sync::Arc;
use std::time::Duration;

use xphone::ice::{self, IceAgent, IceSdpParams};
use xphone::stun;
use xphone::turn::{self, TurnClient};

fn turn_addr() -> SocketAddr {
    let host = std::env::var("TURN_HOST").unwrap_or_else(|_| "127.0.0.1".into());
    let port: u16 = std::env::var("TURN_PORT")
        .ok()
        .and_then(|s| s.parse().ok())
        .unwrap_or(3478);
    format!("{}:{}", host, port).parse().unwrap()
}

fn turn_user() -> String {
    std::env::var("TURN_USER").unwrap_or_else(|_| "testuser".into())
}

fn turn_pass() -> String {
    std::env::var("TURN_PASS").unwrap_or_else(|_| "testpass".into())
}

// ─── STUN tests (CoTURN also serves as a STUN server) ───────────────────

/// T1: STUN Binding Request against CoTURN returns a mapped address.
#[test]
fn stun_binding_via_coturn() {
    let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
    let server = turn_addr();

    let addr = stun::stun_mapped_address(&socket, server, Duration::from_secs(5)).unwrap();
    assert!(
        !addr.ip().is_unspecified(),
        "mapped IP should not be 0.0.0.0"
    );
    assert_ne!(addr.port(), 0, "mapped port should not be 0");
    println!("STUN mapped address: {}", addr);
}

// ─── TURN Allocate tests ────────────────────────────────────────────────

/// T2: Allocate a relay address on CoTURN.
#[test]
fn turn_allocate() {
    let socket = Arc::new(UdpSocket::bind("0.0.0.0:0").unwrap());
    let client = TurnClient::new(socket, turn_addr(), turn_user(), turn_pass());

    let relay = client.allocate().unwrap();
    println!("TURN relay address: {}", relay);

    assert!(!relay.ip().is_unspecified());
    assert_ne!(relay.port(), 0);

    // Relay address should be returned by accessor too.
    assert_eq!(client.relay_addr(), Some(relay));

    client.stop();
}

/// T3: Allocate with wrong credentials should fail.
#[test]
fn turn_allocate_wrong_credentials() {
    let socket = Arc::new(UdpSocket::bind("0.0.0.0:0").unwrap());
    let client = TurnClient::new(socket, turn_addr(), "wrong".into(), "wrong".into());

    let result = client.allocate();
    assert!(result.is_err(), "should fail with wrong credentials");
    println!("Expected error: {}", result.unwrap_err());
}

// ─── TURN CreatePermission + ChannelBind ────────────────────────────────

/// T4: Allocate, create permission, bind channel, send ChannelData through relay.
#[test]
fn turn_full_lifecycle() {
    let socket = Arc::new(UdpSocket::bind("0.0.0.0:0").unwrap());
    let client = TurnClient::new(socket.clone(), turn_addr(), turn_user(), turn_pass());

    // Allocate relay.
    let relay = client.allocate().unwrap();
    println!("Relay: {}", relay);

    // Create a peer socket that will receive relayed data.
    let peer_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
    peer_socket
        .set_read_timeout(Some(Duration::from_secs(3)))
        .unwrap();
    let peer_addr: SocketAddr = format!("127.0.0.1:{}", peer_socket.local_addr().unwrap().port())
        .parse()
        .unwrap();
    println!("Peer: {}", peer_addr);

    // Create permission for the peer.
    client.create_permission(peer_addr).unwrap();

    // Bind a channel to the peer.
    let channel = client.channel_bind(peer_addr).unwrap();
    assert!(channel >= 0x4000 && channel <= 0x7FFE);
    assert_eq!(client.channel_for_peer(&peer_addr), Some(channel));
    println!("Channel: 0x{:04X}", channel);

    // Send data through the relay via ChannelData.
    let test_payload = b"hello from TURN relay";
    let frame = turn::wrap_channel_data(channel, test_payload);
    socket.send_to(&frame, turn_addr()).unwrap();

    // The peer should receive the data from the relay address.
    let mut buf = [0u8; 256];
    match peer_socket.recv_from(&mut buf) {
        Ok((n, from)) => {
            println!("Peer received {} bytes from {}", n, from);
            // CoTURN relays from the relay address.
            assert_eq!(from.ip(), relay.ip());
            assert_eq!(&buf[..n], test_payload);
        }
        Err(e) => {
            // On some Docker setups (macOS), the relay can't reach localhost peers.
            // This is expected — log and pass.
            println!(
                "Peer did not receive data (expected on macOS Docker): {}",
                e
            );
        }
    }

    // Deallocate.
    client.stop();
    assert!(client.relay_addr().is_none());
}

/// T5: Peer sends data to relay, client receives via ChannelData.
#[test]
fn turn_receive_via_relay() {
    let socket = Arc::new(UdpSocket::bind("0.0.0.0:0").unwrap());
    socket
        .set_read_timeout(Some(Duration::from_secs(3)))
        .unwrap();
    let client = TurnClient::new(socket.clone(), turn_addr(), turn_user(), turn_pass());

    let relay = client.allocate().unwrap();
    println!("Relay: {}", relay);

    // Create a peer that sends to the relay.
    let peer_socket = UdpSocket::bind("0.0.0.0:0").unwrap();
    let peer_addr: SocketAddr = format!("127.0.0.1:{}", peer_socket.local_addr().unwrap().port())
        .parse()
        .unwrap();

    // Create permission for the peer.
    client.create_permission(peer_addr).unwrap();

    // Bind channel.
    let channel = client.channel_bind(peer_addr).unwrap();

    // Peer sends to the relay address.
    let test_data = b"response from peer";
    peer_socket.send_to(test_data, relay).unwrap();

    // Client should receive ChannelData from the TURN server.
    // Give a short wait for the relay.
    std::thread::sleep(Duration::from_millis(100));

    let mut buf = [0u8; 256];
    match socket.recv_from(&mut buf) {
        Ok((n, from)) => {
            assert_eq!(from, turn_addr(), "data should come from TURN server");
            // Should be ChannelData framing.
            if turn::is_channel_data(&buf[..n]) {
                let (ch, payload) = turn::parse_channel_data(&buf[..n]).unwrap();
                assert_eq!(ch, channel);
                assert_eq!(payload, test_data);
                println!(
                    "Received ChannelData: channel=0x{:04X}, {} bytes",
                    ch,
                    payload.len()
                );
            } else {
                println!(
                    "Received non-ChannelData ({} bytes) — may be STUN indication",
                    n
                );
            }
        }
        Err(e) => {
            println!(
                "Client did not receive relay data (expected on macOS Docker): {}",
                e
            );
        }
    }

    client.stop();
}

// ─── TURN Refresh ───────────────────────────────────────────────────────

/// T6: Explicit refresh should succeed after allocation.
#[test]
fn turn_refresh() {
    let socket = Arc::new(UdpSocket::bind("0.0.0.0:0").unwrap());
    let client = TurnClient::new(socket, turn_addr(), turn_user(), turn_pass());

    client.allocate().unwrap();
    client.refresh().unwrap();
    client.stop();
}

// ─── ICE candidate gathering ────────────────────────────────────────────

/// T7: Gather ICE candidates using STUN-discovered srflx address.
#[test]
fn ice_gather_with_stun() {
    let socket = UdpSocket::bind("0.0.0.0:0").unwrap();
    let local_port = socket.local_addr().unwrap().port();

    // Get srflx address via STUN.
    let srflx = stun::stun_mapped_address(&socket, turn_addr(), Duration::from_secs(5)).unwrap();
    println!("Server-reflexive: {}", srflx);

    let local_addr: SocketAddr = format!("127.0.0.1:{}", local_port).parse().unwrap();
    let candidates = ice::gather_candidates(local_addr, Some(srflx), None, 1);

    assert!(candidates.len() >= 2, "should have host + srflx candidates");
    println!("Candidates:");
    for c in &candidates {
        println!("  {}", c.to_sdp_value());
    }

    // Verify SDP encoding.
    for c in &candidates {
        let sdp = c.to_sdp_value();
        assert!(sdp.contains("UDP"), "candidate should use UDP");
    }
}

/// T8: Gather ICE candidates with TURN relay address.
#[test]
fn ice_gather_with_turn_relay() {
    let socket = Arc::new(UdpSocket::bind("0.0.0.0:0").unwrap());
    let client = TurnClient::new(socket.clone(), turn_addr(), turn_user(), turn_pass());

    let relay = client.allocate().unwrap();
    let srflx = stun::stun_mapped_address(&socket, turn_addr(), Duration::from_secs(5)).unwrap();

    let local_addr = socket.local_addr().unwrap();
    let candidates = ice::gather_candidates(local_addr, Some(srflx), Some(relay), 1);

    // Should have host + srflx + relay candidates.
    assert!(candidates.len() >= 3, "should have host + srflx + relay");
    println!("Candidates:");
    for c in &candidates {
        println!("  {}", c.to_sdp_value());
    }

    // Verify relay candidate exists.
    let has_relay = candidates
        .iter()
        .any(|c| c.to_sdp_value().contains("typ relay"));
    assert!(has_relay, "should have a relay candidate");

    client.stop();
}

// ─── ICE-Lite agent STUN responder ──────────────────────────────────────

/// T9: ICE agent handles a STUN Binding Request with MESSAGE-INTEGRITY.
#[test]
fn ice_agent_binding_request() {
    let creds = ice::generate_credentials();
    println!("Local ICE creds: ufrag={}, pwd={}", creds.ufrag, creds.pwd);

    let local_addr: SocketAddr = "127.0.0.1:5004".parse().unwrap();
    let candidates = ice::gather_candidates(local_addr, None, None, 1);
    let agent = IceAgent::new(creds.clone(), candidates);

    // Build a STUN Binding Request with correct USERNAME and MESSAGE-INTEGRITY.
    let remote_ufrag = "remoteufrag";
    let username = format!("{}:{}", creds.ufrag, remote_ufrag);
    let txn_id = stun::generate_txn_id();

    let mut request = stun::build_stun_message(
        stun::BINDING_REQUEST,
        &txn_id,
        &[stun::StunAttr {
            attr_type: stun::ATTR_USERNAME,
            value: username.as_bytes().to_vec(),
        }],
    );
    stun::append_message_integrity(&mut request, creds.pwd.as_bytes());

    let from: SocketAddr = "10.0.0.1:5000".parse().unwrap();
    let response = agent.handle_binding_request(&request, from);

    assert!(response.is_some(), "should produce a Binding Response");
    let resp = response.unwrap();
    assert!(stun::is_stun_message(&resp));

    let msg_type = stun::extract_msg_type(&resp).unwrap();
    assert_eq!(msg_type, stun::BINDING_RESPONSE);
    println!("ICE Binding Response: {} bytes", resp.len());
}

/// T10: ICE agent rejects Binding Request with wrong ufrag.
#[test]
fn ice_agent_rejects_wrong_ufrag() {
    let creds = ice::generate_credentials();
    let candidates = ice::gather_candidates("127.0.0.1:5004".parse().unwrap(), None, None, 1);
    let agent = IceAgent::new(creds.clone(), candidates);

    let txn_id = stun::generate_txn_id();
    let mut request = stun::build_stun_message(
        stun::BINDING_REQUEST,
        &txn_id,
        &[stun::StunAttr {
            attr_type: stun::ATTR_USERNAME,
            value: b"wrongufrag:remote".to_vec(),
        }],
    );
    stun::append_message_integrity(&mut request, creds.pwd.as_bytes());

    let from: SocketAddr = "10.0.0.1:5000".parse().unwrap();
    assert!(agent.handle_binding_request(&request, from).is_none());
}

// ─── SDP ICE integration ────────────────────────────────────────────────

/// T11: Build SDP offer with ICE candidates from real STUN/TURN.
#[test]
fn sdp_offer_with_real_candidates() {
    let socket = Arc::new(UdpSocket::bind("0.0.0.0:0").unwrap());
    let local_port = socket.local_addr().unwrap().port();
    let client = TurnClient::new(socket.clone(), turn_addr(), turn_user(), turn_pass());

    let relay = client.allocate().unwrap();
    let srflx = stun::stun_mapped_address(&socket, turn_addr(), Duration::from_secs(5)).unwrap();

    let local_addr: SocketAddr = format!("127.0.0.1:{}", local_port).parse().unwrap();
    let candidates = ice::gather_candidates(local_addr, Some(srflx), Some(relay), 1);
    let creds = ice::generate_credentials();

    let ice_params = IceSdpParams {
        ufrag: creds.ufrag.clone(),
        pwd: creds.pwd.clone(),
        candidates,
        ice_lite: true,
    };

    let sdp = xphone::sdp::build_offer_ice(
        "127.0.0.1",
        local_port as i32,
        &[0, 8],
        "sendrecv",
        &ice_params,
    );

    println!("SDP offer:\n{}", sdp);

    assert!(sdp.contains("a=ice-lite"), "should have ice-lite");
    assert!(sdp.contains(&format!("a=ice-ufrag:{}", creds.ufrag)));
    assert!(sdp.contains(&format!("a=ice-pwd:{}", creds.pwd)));
    assert!(sdp.contains("typ host"), "should have host candidate");
    assert!(sdp.contains("typ srflx"), "should have srflx candidate");
    assert!(sdp.contains("typ relay"), "should have relay candidate");

    client.stop();
}

// ─── ChannelData demux ──────────────────────────────────────────────────

/// T12: Verify packet demux correctly separates STUN, ChannelData, and RTP.
#[test]
fn packet_demux() {
    // STUN Binding Request.
    let stun_msg = stun::build_stun_message(stun::BINDING_REQUEST, &[0xAA; 12], &[]);
    assert!(stun::is_stun_message(&stun_msg));
    assert!(!turn::is_channel_data(&stun_msg));

    // ChannelData.
    let cd = turn::wrap_channel_data(0x4000, b"rtp payload");
    assert!(turn::is_channel_data(&cd));
    assert!(!stun::is_stun_message(&cd));

    // RTP (version 2, PT 0).
    let mut rtp = vec![0x80, 0x00, 0x00, 0x01];
    rtp.extend_from_slice(&[0u8; 8]); // timestamp + ssrc
    rtp.extend_from_slice(&[0xFFu8; 160]); // PCMU silence
    assert!(!stun::is_stun_message(&rtp));
    assert!(!turn::is_channel_data(&rtp));

    // ChannelData round-trip.
    let (ch, payload) = turn::parse_channel_data(&cd).unwrap();
    assert_eq!(ch, 0x4000);
    assert_eq!(payload, b"rtp payload");
}