stun_rs/attributes/discovery/
other_address.rs

1use crate::attributes::address_port::{address_port_attribute, address_port_tests};
2use crate::{Decode, Encode};
3
4const OTHER_ADDRESS: u16 = 0x802c;
5
6address_port_attribute!(
7    /// The other address attribute is used in Binding Responses.  It informs
8    /// the client of the source IP address and port that would be used if
9    /// the client requested the "change IP" and "change port" behavior.
10    /// This attribute MUST NOT be inserted into a Binding Response unless the
11    /// server has a second IP address.
12    ///
13    /// # Examples
14    ///```rust
15    /// # use std::net::{IpAddr, Ipv4Addr, SocketAddr};
16    /// # use stun_rs::attributes::discovery::OtherAddress;
17    /// let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
18    /// let attr = OtherAddress::from(socket);
19    ///
20    /// assert_eq!(attr.socket_address().port(), 8080);
21    /// assert!(attr.socket_address().is_ipv4());
22    ///```
23    OtherAddress,
24    OTHER_ADDRESS
25);
26
27address_port_tests!(OtherAddress, super);
28
29#[cfg(test)]
30mod tests {
31    use super::*;
32    use crate::StunAttribute;
33    use std::net::{IpAddr, Ipv4Addr, SocketAddr};
34
35    #[test]
36    fn other_address_server_stunt_attribute() {
37        let socket = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8080);
38        let attr = StunAttribute::OtherAddress(OtherAddress::from(socket));
39        assert!(attr.is_other_address());
40        assert!(attr.as_other_address().is_ok());
41        assert!(attr.as_error_code().is_err());
42
43        assert!(!attr.attribute_type().is_comprehension_required());
44        assert!(attr.attribute_type().is_comprehension_optional());
45
46        let dbg_fmt = format!("{:?}", attr);
47        assert_eq!("OtherAddress(OtherAddress(127.0.0.1:8080))", dbg_fmt);
48    }
49}