rustpbx 0.4.6

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
//! SDP Bridge for WebRTC ↔ RTP interop
//!
//! This module handles SDP transformation between WebRTC and RTP formats:
//! - WebRTC: UDP/TLS/RTP/SAVPF, DTLS fingerprint, ICE candidates, SRTP
//! - RTP: RTP/AVP, plain RTP, no encryption

use anyhow::{Result, anyhow};
use rustrtc::{MediaKind, SdpType, SessionDescription};

/// ICE credentials for WebRTC connections
#[derive(Debug, Clone)]
pub struct IceCredentials {
    pub ufrag: String,
    pub pwd: String,
}

impl IceCredentials {
    /// Generate random ICE credentials
    pub fn generate() -> Self {
        use rand::RngExt;
        let mut rng = rand::rng();

        // Generate random ufrag (4-8 characters as per RFC 5245)
        const CHARS: &[u8] = b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        let ufrag_len = rng.random_range(4..=8);
        let ufrag: String = (0..ufrag_len)
            .map(|_| CHARS[rng.random_range(0..CHARS.len())] as char)
            .collect();

        // Generate random pwd (22-256 characters as per RFC 5245)
        let pwd_len = rng.random_range(22..=32);
        let pwd: String = (0..pwd_len)
            .map(|_| CHARS[rng.random_range(0..CHARS.len())] as char)
            .collect();

        Self { ufrag, pwd }
    }
}

/// DTLS certificate info for WebRTC connections
#[derive(Debug, Clone)]
pub struct DtlsInfo {
    pub fingerprint: String,
    pub setup: String,
}

impl DtlsInfo {
    /// Generate a placeholder DTLS fingerprint
    ///
    /// TODO: In production, this should be derived from the actual DTLS certificate
    /// stored in the server's TLS configuration
    pub fn generate_placeholder() -> Self {
        // Generate a random SHA-256 fingerprint
        // In production, this should come from the actual certificate
        use rand::RngExt;
        let mut rng = rand::rng();
        let bytes: Vec<u8> = (0..32).map(|_| rng.random::<u8>()).collect();
        let fingerprint = bytes
            .iter()
            .map(|b| format!("{:02X}", b))
            .collect::<Vec<_>>()
            .join(":");

        Self {
            fingerprint,
            setup: "passive".to_string(),
        }
    }

    /// Get fingerprint from server configuration
    ///
    /// This should be called with the server's TLS certificate fingerprint
    pub fn from_certificate(fingerprint: &str) -> Self {
        Self {
            fingerprint: fingerprint.to_string(),
            setup: "passive".to_string(),
        }
    }
}

/// SDP format type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SdpFormat {
    /// WebRTC SDP with DTLS, ICE, SRTP
    WebRtc,
    /// Plain RTP SDP
    Rtp,
}

/// Detect SDP format from content
pub fn detect_sdp_format(sdp: &str) -> SdpFormat {
    if sdp.contains("UDP/TLS/RTP/SAVPF")
        || sdp.contains("a=fingerprint:")
        || sdp.contains("a=ice-ufrag:")
    {
        SdpFormat::WebRtc
    } else {
        SdpFormat::Rtp
    }
}

/// SDP Bridge for converting between WebRTC and RTP formats
pub struct SdpBridge;

impl SdpBridge {
    /// Convert WebRTC SDP to RTP SDP
    ///
    /// Changes:
    /// - Protocol: UDP/TLS/RTP/SAVPF → RTP/AVP
    /// - Remove: fingerprint, ice-ufrag, ice-pwd, setup, rtcp-mux
    /// - Add: standard RTP port
    /// - Codec: keep compatible codecs (Opus → optional, PCMU/PCMA preferred)
    pub fn webrtc_to_rtp(webrtc_sdp: &str) -> Result<String> {
        let parsed = SessionDescription::parse(SdpType::Offer, webrtc_sdp)
            .or_else(|_| SessionDescription::parse(SdpType::Answer, webrtc_sdp))
            .map_err(|e| anyhow!("Failed to parse WebRTC SDP: {}", e))?;

        let mut rtp_sdp = String::new();

        // Add session header
        rtp_sdp.push_str("v=0\r\n");
        rtp_sdp.push_str(&format!(
            "o=- {} {} IN IP4 {}\r\n",
            parsed.session.origin.session_id,
            parsed.session.origin.session_version,
            parsed.session.origin.unicast_address
        ));
        rtp_sdp.push_str("s=RustPBX Bridge\r\n");
        rtp_sdp.push_str(&format!(
            "c=IN IP4 {}\r\n",
            parsed.session.origin.unicast_address
        ));
        rtp_sdp.push_str("t=0 0\r\n");

        // Transform media sections
        for section in &parsed.media_sections {
            let is_audio = section.kind == MediaKind::Audio;
            let is_video = section.kind == MediaKind::Video;
            if !is_audio && !is_video {
                continue;
            }

            // Convert protocol and build format list
            let mut rtp_formats = Vec::new();
            let mut rtp_attrs = Vec::new();

            for format in &section.formats {
                if let Ok(pt) = format.parse::<u8>() {
                    rtp_formats.push(pt);
                }
            }

            // Find RTP map attributes for our formats
            let mut codec_found = false;
            for attr in &section.attributes {
                if attr.key == "rtpmap" {
                    if let Some(ref value) = attr.value {
                        // Parse rtpmap: <pt> <codec>/<rate>
                        let parts: Vec<&str> = value.splitn(2, ' ').collect();
                        if parts.len() == 2 {
                            let pt_str = parts[0];
                            let codec_info = parts[1];

                            if is_audio {
                                // Keep PCMU, PCMA, telephone-event (skip Opus for RTP)
                                if codec_info.contains("PCMU")
                                    || codec_info.contains("PCMA")
                                    || codec_info.contains("telephone-event")
                                    || codec_info.contains("G722")
                                    || codec_info.contains("G729")
                                {
                                    rtp_attrs
                                        .push(format!("a=rtpmap:{} {}\r\n", pt_str, codec_info));
                                    codec_found = true;
                                }
                            } else if is_video {
                                // Keep all video codecs as-is for RTP
                                rtp_attrs.push(format!("a=rtpmap:{} {}\r\n", pt_str, codec_info));
                                codec_found = true;
                            }
                        }
                    }
                } else if attr.key == "fmtp" {
                    if is_audio {
                        if let Some(ref value) = attr.value
                            && (value.contains("101") || value.contains("telephone-event"))
                        {
                            rtp_attrs.push(format!("a=fmtp:{}\r\n", value));
                        }
                    } else if is_video {
                        // Keep video fmtp (profile-level-id, packetization-mode, etc.)
                        if let Some(ref value) = attr.value {
                            rtp_attrs.push(format!("a=fmtp:{}\r\n", value));
                        }
                    }
                } else if attr.key == "sendrecv"
                    || attr.key == "sendonly"
                    || attr.key == "recvonly"
                    || attr.key == "inactive"
                {
                    rtp_attrs.push(format!("a={}\r\n", attr.key));
                } else if is_video && (attr.key == "rtcp-fb" || attr.key == "rtx") {
                    if let Some(ref value) = attr.value {
                        rtp_attrs.push(format!("a={}:{}\r\n", attr.key, value));
                    }
                }
                // Skip: fingerprint, setup, ice-ufrag, ice-pwd, rtcp-mux, candidate, mid
            }

            // If no compatible codec found, add default PCMU for audio
            if is_audio && !codec_found {
                rtp_formats = vec![0, 101];
                rtp_attrs.push("a=rtpmap:0 PCMU/8000\r\n".to_string());
                rtp_attrs.push("a=rtpmap:101 telephone-event/8000\r\n".to_string());
                rtp_attrs.push("a=fmtp:101 0-15\r\n".to_string());
            }

            // Build media line with RTP/AVP
            let format_str = rtp_formats
                .iter()
                .map(|p| p.to_string())
                .collect::<Vec<_>>()
                .join(" ");
            let media_type = if is_audio { "audio" } else { "video" };
            rtp_sdp.push_str(&format!(
                "m={} {} RTP/AVP {}\r\n",
                media_type, section.port, format_str
            ));

            // Add collected attributes
            for attr in rtp_attrs {
                rtp_sdp.push_str(&attr);
            }
        }

        Ok(rtp_sdp)
    }

    /// Convert RTP SDP to WebRTC SDP
    ///
    /// Changes:
    /// - Protocol: RTP/AVP → UDP/TLS/RTP/SAVPF  
    /// - Add: fingerprint, ice-ufrag, ice-pwd, setup, rtcp-mux
    /// - Keep: compatible codecs
    pub fn rtp_to_webrtc(
        rtp_sdp: &str,
        fingerprint: &str,
        ice_ufrag: &str,
        ice_pwd: &str,
    ) -> Result<String> {
        let parsed = SessionDescription::parse(SdpType::Offer, rtp_sdp)
            .or_else(|_| SessionDescription::parse(SdpType::Answer, rtp_sdp))
            .map_err(|e| anyhow!("Failed to parse RTP SDP: {}", e))?;

        let mut webrtc_sdp = String::new();

        // Add session header
        webrtc_sdp.push_str("v=0\r\n");
        webrtc_sdp.push_str(&format!(
            "o=- {} {} IN IP4 {}\r\n",
            parsed.session.origin.session_id,
            parsed.session.origin.session_version,
            parsed.session.origin.unicast_address
        ));
        webrtc_sdp.push_str("s=RustPBX Bridge\r\n");
        webrtc_sdp.push_str(&format!(
            "c=IN IP4 {}\r\n",
            parsed.session.origin.unicast_address
        ));
        webrtc_sdp.push_str("t=0 0\r\n");

        // Transform media sections
        for section in &parsed.media_sections {
            let is_audio = section.kind == MediaKind::Audio;
            let is_video = section.kind == MediaKind::Video;
            if !is_audio && !is_video {
                continue;
            }

            let mut webrtc_formats = Vec::new();
            let mut webrtc_attrs = Vec::new();

            // Collect formats
            for format in &section.formats {
                if let Ok(pt) = format.parse::<u8>() {
                    webrtc_formats.push(pt);
                }
            }

            // Process attributes
            for attr in &section.attributes {
                if attr.key == "rtpmap" {
                    if let Some(ref value) = attr.value {
                        webrtc_attrs.push(format!("a=rtpmap:{}\r\n", value));
                    }
                } else if attr.key == "fmtp" {
                    if let Some(ref value) = attr.value {
                        webrtc_attrs.push(format!("a=fmtp:{}\r\n", value));
                    }
                } else if attr.key == "sendrecv"
                    || attr.key == "sendonly"
                    || attr.key == "recvonly"
                    || attr.key == "inactive"
                {
                    webrtc_attrs.push(format!("a={}\r\n", attr.key));
                } else if is_video && (attr.key == "rtcp-fb" || attr.key == "rtx") {
                    if let Some(ref value) = attr.value {
                        webrtc_attrs.push(format!("a={}:{}\r\n", attr.key, value));
                    }
                }
            }

            // Add WebRTC-specific attributes
            webrtc_attrs.push(format!("a=fingerprint:sha-256 {}\r\n", fingerprint));
            webrtc_attrs.push("a=setup:passive\r\n".to_string());
            webrtc_attrs.push(format!("a=ice-ufrag:{}\r\n", ice_ufrag));
            webrtc_attrs.push(format!("a=ice-pwd:{}\r\n", ice_pwd));
            webrtc_attrs.push("a=rtcp-mux\r\n".to_string());
            webrtc_attrs.push("a=mid:0\r\n".to_string());

            // Build media line with SAVPF
            let format_str = webrtc_formats
                .iter()
                .map(|p| p.to_string())
                .collect::<Vec<_>>()
                .join(" ");
            let media_type = if is_audio { "audio" } else { "video" };
            webrtc_sdp.push_str(&format!(
                "m={} {} UDP/TLS/RTP/SAVPF {}\r\n",
                media_type, section.port, format_str
            ));

            // Add attributes
            for attr in webrtc_attrs {
                webrtc_sdp.push_str(&attr);
            }
        }

        Ok(webrtc_sdp)
    }

    /// Check if SDP needs bridging (different formats between caller and callee)
    pub fn needs_bridging(caller_sdp: &str, callee_sdp: &str) -> bool {
        let caller_format = detect_sdp_format(caller_sdp);
        let callee_format = detect_sdp_format(callee_sdp);
        caller_format != callee_format
    }
}

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

    #[test]
    fn test_detect_webrtc_sdp() {
        let webrtc_sdp = "v=0\r\n\
            o=- 123 123 IN IP4 127.0.0.1\r\n\
            s=-\r\n\
            m=audio 1234 UDP/TLS/RTP/SAVPF 111\r\n\
            a=fingerprint:sha-256 AA:BB\r\n";
        assert_eq!(detect_sdp_format(webrtc_sdp), SdpFormat::WebRtc);
    }

    #[test]
    fn test_detect_rtp_sdp() {
        let rtp_sdp = "v=0\r\n\
            o=- 123 123 IN IP4 127.0.0.1\r\n\
            s=-\r\n\
            m=audio 1234 RTP/AVP 0\r\n";
        assert_eq!(detect_sdp_format(rtp_sdp), SdpFormat::Rtp);
    }

    #[test]
    fn test_webrtc_to_rtp_conversion() {
        let webrtc_sdp = "v=0\r\n\
o=- 123456 123456 IN IP4 127.0.0.1\r\n\
s=-\r\n\
c=IN IP4 127.0.0.1\r\n\
t=0 0\r\n\
m=audio 12345 UDP/TLS/RTP/SAVPF 111 101\r\n\
a=rtpmap:111 opus/48000/2\r\n\
a=rtpmap:101 telephone-event/8000\r\n\
a=fingerprint:sha-256 AA:BB:CC:DD\r\n\
a=setup:actpass\r\n\
a=ice-ufrag:abcd\r\n\
a=ice-pwd:xyz\r\n\
a=rtcp-mux\r\n\
a=sendrecv\r\n";

        let rtp_sdp = SdpBridge::webrtc_to_rtp(webrtc_sdp).unwrap();

        // Should use RTP/AVP
        assert!(rtp_sdp.contains("RTP/AVP"));
        assert!(!rtp_sdp.contains("SAVPF"));

        // Should remove WebRTC-specific attributes
        assert!(!rtp_sdp.contains("fingerprint"));
        assert!(!rtp_sdp.contains("ice-ufrag"));
        assert!(!rtp_sdp.contains("rtcp-mux"));

        // Should keep telephone-event
        assert!(rtp_sdp.contains("telephone-event"));
    }

    #[test]
    fn test_rtp_to_webrtc_conversion() {
        let rtp_sdp = "v=0\r\n\
o=- 123456 123456 IN IP4 127.0.0.1\r\n\
s=-\r\n\
c=IN IP4 127.0.0.1\r\n\
t=0 0\r\n\
m=audio 54321 RTP/AVP 0 101\r\n\
a=rtpmap:0 PCMU/8000\r\n\
a=rtpmap:101 telephone-event/8000\r\n\
a=sendrecv\r\n";

        let webrtc_sdp =
            SdpBridge::rtp_to_webrtc(rtp_sdp, "AA:BB:CC:DD:EE:FF", "ufrag123", "pwd456").unwrap();

        // Should use SAVPF
        assert!(webrtc_sdp.contains("UDP/TLS/RTP/SAVPF"));

        // Should add WebRTC-specific attributes
        assert!(webrtc_sdp.contains("fingerprint:sha-256 AA:BB:CC:DD:EE:FF"));
        assert!(webrtc_sdp.contains("ice-ufrag:ufrag123"));
        assert!(webrtc_sdp.contains("ice-pwd:pwd456"));
        assert!(webrtc_sdp.contains("rtcp-mux"));
        assert!(webrtc_sdp.contains("setup:passive"));

        // Should keep PCMU
        assert!(webrtc_sdp.contains("PCMU/8000"));
    }
}