stun_rs/attributes/discovery/
response_origin.rs

1use crate::attributes::address_port::{address_port_attribute, address_port_tests};
2use crate::{Decode, Encode};
3
4const RESPONSE_ORIGIN: u16 = 0x802b;
5
6address_port_attribute!(
7    /// The response origin attribute is inserted by the server and indicates
8    /// the source IP address and port the response was sent from.  It is
9    /// useful for detecting double NAT configurations.  It is only present
10    /// in Binding Responses.
11    ///
12    /// # Examples
13    ///```rust
14    /// # use std::net::{IpAddr, Ipv4Addr, SocketAddr};
15    /// # use stun_rs::attributes::discovery::ResponseOrigin;
16    /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
17    /// let attr = ResponseOrigin::from(socket);
18    ///
19    /// assert_eq!(attr.socket_address().port(), 8080);
20    /// assert!(attr.socket_address().is_ipv4());
21    ///```
22    ResponseOrigin,
23    RESPONSE_ORIGIN
24);
25
26address_port_tests!(ResponseOrigin, super);
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31    use crate::StunAttribute;
32    use std::net::{IpAddr, Ipv4Addr, SocketAddr};
33
34    #[test]
35    fn response_origin_server_stunt_attribute() {
36        let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
37        let attr = StunAttribute::ResponseOrigin(ResponseOrigin::from(socket));
38        assert!(attr.is_response_origin());
39        assert!(attr.as_response_origin().is_ok());
40        assert!(attr.as_error_code().is_err());
41
42        assert!(!attr.attribute_type().is_comprehension_required());
43        assert!(attr.attribute_type().is_comprehension_optional());
44
45        let dbg_fmt = format!("{:?}", attr);
46        assert_eq!("ResponseOrigin(ResponseOrigin(127.0.0.1:8080))", dbg_fmt);
47    }
48}