freeswitch-types 1.5.0-beta.6

FreeSWITCH ESL protocol types: channel state, events, headers, commands, and variables
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
//! Typed mod_sofia / SIP channel variable names.

use sip_header::SipHeader;

sip_header::define_header_enum! {
    tests_mod: sofia_variable_generated_tests,
    error_type: ParseSofiaVariableError => "unknown sofia variable",
    /// mod_sofia / SIP channel variable names (the part after the `variable_` prefix).
    ///
    /// Use with [`HeaderLookup::variable()`](crate::HeaderLookup::variable) for type-safe lookups.
    /// Core FreeSWITCH variables belong in [`ChannelVariable`](super::ChannelVariable).
    pub enum SofiaVariable {
        // --- SIP From ---
        SipFromUser => "sip_from_user",
        SipFromHost => "sip_from_host",
        SipFromPort => "sip_from_port",
        SipFromUri => "sip_from_uri",
        SipFromDisplay => "sip_from_display",
        SipFromTag => "sip_from_tag",
        SipFromComment => "sip_from_comment",
        SipFromUserStripped => "sip_from_user_stripped",
        SipFullFrom => "sip_full_from",

        // --- SIP To ---
        SipToUser => "sip_to_user",
        SipToHost => "sip_to_host",
        SipToPort => "sip_to_port",
        SipToUri => "sip_to_uri",
        SipToDisplay => "sip_to_display",
        SipToTag => "sip_to_tag",
        SipToComment => "sip_to_comment",
        SipFullTo => "sip_full_to",

        // --- SIP Contact ---
        SipContactUser => "sip_contact_user",
        SipContactHost => "sip_contact_host",
        SipContactPort => "sip_contact_port",
        SipContactUri => "sip_contact_uri",
        SipContactParams => "sip_contact_params",

        // --- SIP Request ---
        SipReqUser => "sip_req_user",
        SipReqHost => "sip_req_host",
        SipReqPort => "sip_req_port",
        SipReqUri => "sip_req_uri",

        // --- SIP Via ---
        SipViaHost => "sip_via_host",
        SipViaPort => "sip_via_port",
        SipViaRport => "sip_via_rport",
        SipViaProtocol => "sip_via_protocol",
        SipFullVia => "sip_full_via",
        SipFullRoute => "sip_full_route",

        // --- SIP Session ---
        SipCallId => "sip_call_id",
        SipCseq => "sip_cseq",
        SipUserAgent => "sip_user_agent",
        SipSubject => "sip_subject",
        SipAllow => "sip_allow",
        SipAcceptLanguage => "sip_accept_language",
        SipCallInfo => "sip_call_info",
        SipDateEpochTime => "sip_date_epoch_time",

        // --- SIP Network ---
        SipReceivedIp => "sip_received_ip",
        SipReceivedPort => "sip_received_port",
        SipNetworkIp => "sip_network_ip",
        SipNetworkPort => "sip_network_port",
        SipNatDetected => "sip_nat_detected",
        SipTransport => "sip_transport",
        SipReplyHost => "sip_reply_host",

        // --- SIP Auth ---
        SipAuthUsername => "sip_auth_username",
        SipAuthPassword => "sip_auth_password",
        SipAuthorized => "sip_authorized",
        SipAclAuthedBy => "sip_acl_authed_by",
        SipAclToken => "sip_acl_token",
        SipChallengeRealm => "sip_challenge_realm",

        // --- SIP Failure / Hangup ---
        SipInviteFailureStatus => "sip_invite_failure_status",
        SipInviteFailurePhrase => "sip_invite_failure_phrase",
        SipHangupDisposition => "sip_hangup_disposition",
        SipTermStatus => "sip_term_status",
        SipTermCause => "sip_term_cause",
        SipReason => "sip_reason",

        // --- SIP Identity / Privacy ---
        SipPAssertedIdentity => "sip_P-Asserted-Identity",
        SipPPreferredIdentity => "sip_P-Preferred-Identity",
        SipPrivacy => "sip_Privacy",
        SipRemotePartyId => "sip_Remote-Party-ID",
        SipStirShakenAttest => "sip_stir_shaken_attest",
        SipVerstat => "sip_verstat",
        SipVerstatDetailed => "sip_verstat_detailed",

        // --- SIP Invite Details ---
        SipInviteCallId => "sip_invite_call_id",
        SipInviteCseq => "sip_invite_cseq",
        SipInviteFullFrom => "sip_invite_full_from",
        SipInviteFullTo => "sip_invite_full_to",
        SipInviteFullVia => "sip_invite_full_via",
        SipInviteFromUri => "sip_invite_from_uri",
        SipInviteToUri => "sip_invite_to_uri",
        SipInviteReqUri => "sip_invite_req_uri",
        SipInviteRecordRoute => "sip_invite_record_route",
        SipInviteRouteUri => "sip_invite_route_uri",
        SipInviteDomain => "sip_invite_domain",
        SipInviteParams => "sip_invite_params",

        // --- SIP Features ---
        SipAutoAnswer => "sip_auto_answer",
        SipAutoSimplify => "sip_auto_simplify",
        SipEnableSoa => "sip_enable_soa",
        SipCopyCustomHeaders => "sip_copy_custom_headers",
        SipCopyMultipart => "sip_copy_multipart",
        SipLoopedCall => "sip_looped_call",

        // --- SIP Redirect / Transfer ---
        SipRedirectedTo => "sip_redirected_to",
        SipRedirectedBy => "sip_redirected_by",
        SipRedirectDialstring => "sip_redirect_dialstring",
        SipReferReply => "sip_refer_reply",
        SipReferStatusCode => "sip_refer_status_code",
        SipReferredByFull => "sip_referred_by_full",
        SipReferredByCid => "sip_referred_by_cid",
        SipReinviteSdp => "sip_reinvite_sdp",

        // --- SIP Gateway ---
        SipGateway => "sip_gateway",
        SipGatewayName => "sip_gateway_name",
        SipUseGateway => "sip_use_gateway",
        SipDestinationUrl => "sip_destination_url",

        // --- Sofia Profile ---
        SipProfileName => "sip_profile_name",
        SofiaProfileName => "sofia_profile_name",
        SofiaProfileUrl => "sofia_profile_url",
        SofiaProfileDomainName => "sofia_profile_domain_name",

        // --- RTP / SRTP (set via mod_sofia / switch_core_media) ---
        RtpSecureMediaConfirmed => "rtp_secure_media_confirmed",
        Rtp2833SendPayload => "rtp_2833_send_payload",
        Rtp2833RecvPayload => "rtp_2833_recv_payload",
        RtpDisableHold => "rtp_disable_hold",
        RtpJitterBufferPlc => "rtp_jitter_buffer_plc",
        RtpVideoMaxBandwidthIn => "rtp_video_max_bandwidth_in",
        RtpVideoMaxBandwidthOut => "rtp_video_max_bandwidth_out",

        // --- SIP Callee / Display ---
        SipCalleeIdName => "sip_callee_id_name",
        SipCalleeIdNumber => "sip_callee_id_number",
        SipCidType => "sip_cid_type",

        // --- SIP RTP Stats ---
        SipRtpRxstat => "sip_rtp_rxstat",
        SipRtpTxstat => "sip_rtp_txstat",
        SipPRtpStat => "sip_p_rtp_stat",

        // --- SIP History ---
        SipHistoryInfo => "sip_history_info",
        SipGeolocation => "sip_geolocation",

        // --- SIP Body ---
        SipMultipart => "sip_multipart",
    }
}

/// Relationship between a [`SofiaVariable`] and the SIP header behind it.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum CarriedHeader {
    /// The variable holds the header's field-value verbatim.
    Verbatim(SipHeader),
    /// The variable holds a field mod_sofia parsed out of the header. Lossy: the
    /// header is the source of truth and the parts cannot rebuild it.
    Derived(SipHeader),
}

impl SofiaVariable {
    /// The SIP header this variable's value came from, if any.
    ///
    /// `None` covers everything carrying no header field-value: switch-side config
    /// and knobs, transport addresses, the Request-URI, values computed rather than
    /// read off the wire, and values the caller sets for mod_sofia to build a header
    /// from.
    ///
    /// The match is exhaustive by design. A new variant has to be classified here,
    /// and a catch-all arm would answer `None` for it in silence.
    pub fn carried_header(&self) -> Option<CarriedHeader> {
        use CarriedHeader::{Derived, Verbatim};
        use SipHeader as H;

        match self {
            Self::SipFromUser
            | Self::SipFromHost
            | Self::SipFromPort
            | Self::SipFromUri
            | Self::SipFromDisplay
            | Self::SipFromTag
            | Self::SipFromComment
            | Self::SipFromUserStripped => Some(Derived(H::From)),
            Self::SipFullFrom => Some(Verbatim(H::From)),

            Self::SipToUser
            | Self::SipToHost
            | Self::SipToPort
            | Self::SipToUri
            | Self::SipToDisplay
            | Self::SipToTag
            | Self::SipToComment => Some(Derived(H::To)),
            Self::SipFullTo => Some(Verbatim(H::To)),

            Self::SipContactUser
            | Self::SipContactHost
            | Self::SipContactPort
            | Self::SipContactUri
            | Self::SipContactParams => Some(Derived(H::Contact)),

            // The Request-URI is on the request line, not in a header.
            Self::SipReqUser
            | Self::SipReqHost
            | Self::SipReqPort
            | Self::SipReqUri
            | Self::SipInviteReqUri => None,

            Self::SipViaHost | Self::SipViaPort | Self::SipViaRport | Self::SipViaProtocol => {
                Some(Derived(H::Via))
            }
            Self::SipFullVia => Some(Verbatim(H::Via)),
            Self::SipFullRoute => Some(Verbatim(H::Route)),

            Self::SipCallId => Some(Verbatim(H::CallId)),
            Self::SipCseq => Some(Verbatim(H::Cseq)),
            // Falls back to Server when the message that carried it was a response.
            Self::SipUserAgent => Some(Verbatim(H::UserAgent)),
            Self::SipSubject => Some(Verbatim(H::Subject)),
            Self::SipAllow => Some(Verbatim(H::Allow)),
            // Only the first entry of the list.
            Self::SipAcceptLanguage => Some(Derived(H::AcceptLanguage)),
            Self::SipCallInfo => Some(Verbatim(H::CallInfo)),
            Self::SipDateEpochTime => Some(Derived(H::Date)),

            Self::SipReceivedIp
            | Self::SipReceivedPort
            | Self::SipNetworkIp
            | Self::SipNetworkPort
            | Self::SipNatDetected
            | Self::SipTransport
            | Self::SipReplyHost => None,

            Self::SipAuthUsername
            | Self::SipAuthPassword
            | Self::SipAuthorized
            | Self::SipAclAuthedBy
            | Self::SipAclToken
            | Self::SipChallengeRealm => None,

            Self::SipInviteFailureStatus
            | Self::SipInviteFailurePhrase
            | Self::SipHangupDisposition
            | Self::SipTermStatus
            | Self::SipTermCause => None,
            Self::SipReason => Some(Verbatim(H::Reason)),

            Self::SipPAssertedIdentity => Some(Verbatim(H::PAssertedIdentity)),
            Self::SipPPreferredIdentity => Some(Verbatim(H::PPreferredIdentity)),
            Self::SipPrivacy => Some(Verbatim(H::Privacy)),
            Self::SipRemotePartyId => Some(Verbatim(H::RemotePartyId)),
            // A verification verdict and the attestation level asked of us, neither
            // of them a field of the Identity header they concern.
            Self::SipStirShakenAttest | Self::SipVerstat | Self::SipVerstatDetailed => None,

            Self::SipInviteCallId => Some(Verbatim(H::CallId)),
            Self::SipInviteCseq => Some(Verbatim(H::Cseq)),
            Self::SipInviteFullFrom => Some(Verbatim(H::From)),
            Self::SipInviteFullTo => Some(Verbatim(H::To)),
            Self::SipInviteFullVia => Some(Verbatim(H::Via)),
            Self::SipInviteFromUri => Some(Derived(H::From)),
            Self::SipInviteToUri => Some(Derived(H::To)),
            Self::SipInviteRecordRoute => Some(Verbatim(H::RecordRoute)),
            Self::SipInviteRouteUri => Some(Derived(H::Route)),
            Self::SipInviteDomain | Self::SipInviteParams => None,

            Self::SipAutoAnswer
            | Self::SipAutoSimplify
            | Self::SipEnableSoa
            | Self::SipCopyCustomHeaders
            | Self::SipCopyMultipart
            | Self::SipLoopedCall => None,

            Self::SipRedirectedTo => Some(Verbatim(H::Contact)),
            Self::SipRedirectedBy => Some(Verbatim(H::Diversion)),
            Self::SipReferredByFull => Some(Verbatim(H::ReferredBy)),
            Self::SipReferredByCid => Some(Derived(H::ReferredBy)),
            Self::SipRedirectDialstring
            | Self::SipReferReply
            | Self::SipReferStatusCode
            | Self::SipReinviteSdp => None,

            Self::SipGateway
            | Self::SipGatewayName
            | Self::SipUseGateway
            | Self::SipDestinationUrl
            | Self::SipProfileName
            | Self::SofiaProfileName
            | Self::SofiaProfileUrl
            | Self::SofiaProfileDomainName => None,

            Self::RtpSecureMediaConfirmed
            | Self::Rtp2833SendPayload
            | Self::Rtp2833RecvPayload
            | Self::RtpDisableHold
            | Self::RtpJitterBufferPlc
            | Self::RtpVideoMaxBandwidthIn
            | Self::RtpVideoMaxBandwidthOut => None,

            // Caller-supplied identity mod_sofia builds an outbound header from; which
            // header depends on sip_cid_type, so none of them carries one.
            Self::SipCalleeIdName | Self::SipCalleeIdNumber | Self::SipCidType => None,

            // P-RTP-Stat has no SipHeader variant to name.
            Self::SipRtpRxstat | Self::SipRtpTxstat | Self::SipPRtpStat => None,

            Self::SipHistoryInfo => Some(Verbatim(H::HistoryInfo)),
            Self::SipGeolocation => Some(Verbatim(H::Geolocation)),

            Self::SipMultipart => None,
        }
    }
}

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

    #[test]
    fn display_round_trip() {
        assert_eq!(SofiaVariable::SipCallId.to_string(), "sip_call_id");
        assert_eq!(
            SofiaVariable::SipFromDisplay.to_string(),
            "sip_from_display"
        );
        assert_eq!(
            SofiaVariable::SofiaProfileName.to_string(),
            "sofia_profile_name"
        );
    }

    #[test]
    fn as_ref_str() {
        let v: &str = SofiaVariable::SipNetworkIp.as_ref();
        assert_eq!(v, "sip_network_ip");
    }

    #[test]
    fn from_str_case_insensitive() {
        assert_eq!(
            "sip_call_id".parse::<SofiaVariable>(),
            Ok(SofiaVariable::SipCallId)
        );
        assert_eq!(
            "SIP_CALL_ID".parse::<SofiaVariable>(),
            Ok(SofiaVariable::SipCallId)
        );
    }

    #[test]
    fn from_str_unknown() {
        let err = "nonexistent_sip_var".parse::<SofiaVariable>();
        assert!(err.is_err());
    }

    #[test]
    fn carried_header_verbatim() {
        assert_eq!(
            SofiaVariable::SipPrivacy.carried_header(),
            Some(CarriedHeader::Verbatim(SipHeader::Privacy))
        );
        assert_eq!(
            SofiaVariable::SipPAssertedIdentity.carried_header(),
            Some(CarriedHeader::Verbatim(SipHeader::PAssertedIdentity))
        );
        assert_eq!(
            SofiaVariable::SipRemotePartyId.carried_header(),
            Some(CarriedHeader::Verbatim(SipHeader::RemotePartyId))
        );
        assert_eq!(
            SofiaVariable::SipFullFrom.carried_header(),
            Some(CarriedHeader::Verbatim(SipHeader::From))
        );
    }

    #[test]
    fn carried_header_derived() {
        assert_eq!(
            SofiaVariable::SipFromUser.carried_header(),
            Some(CarriedHeader::Derived(SipHeader::From))
        );
    }

    #[test]
    fn carried_header_none() {
        // The Request-URI is not a header, and a gateway name is switch config.
        assert_eq!(SofiaVariable::SipReqUri.carried_header(), None);
        assert_eq!(SofiaVariable::SipGateway.carried_header(), None);
    }

    /// A wire name that already spells the header keeps its exact canonical
    /// casing, so any typo in either table shows up as a mismatch here.
    #[test]
    fn hyphenated_wire_names_match_their_header() {
        for var in SofiaVariable::ALL {
            let Some(suffix) = var
                .as_str()
                .strip_prefix("sip_")
            else {
                continue;
            };
            if !suffix.contains('-') {
                continue;
            }
            let Some(carried) = var.carried_header() else {
                panic!("{} spells a header but carries none", var.as_str());
            };
            let CarriedHeader::Verbatim(header) = carried else {
                panic!("{} spells a header but is not verbatim", var.as_str());
            };
            assert_eq!(header.as_str(), suffix, "for {}", var.as_str());
        }
    }
}