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
use byteorder::{ByteOrder, NetworkEndian, WriteBytesExt};
use std::io::{Cursor, Write};
use std::net::SocketAddr;

use super::errors::AttributeEncodeError;
use super::types::StunAttributeType;
use super::utils::xor_byte_range;
use super::StunAttribute;

use crate::definitions::{StunTransactionId, STUN_MAGIC_COOKIE};

impl StunAttribute {
    // Wraps the encoded attribute data into TLV
    fn wrap_in_tlv(
        attr_type: StunAttributeType,
        attr_bytes: &[u8],
    ) -> Result<Vec<u8>, AttributeEncodeError> {
        let mut cursor = Cursor::new(Vec::new());

        let data_len = attr_bytes.len();

        // Write the attribute type
        cursor.write_u16::<NetworkEndian>(attr_type as u16)?;
        // Write the attribute data length
        cursor.write_u16::<NetworkEndian>(data_len as u16)?;
        // Write attribute data
        cursor.write_all(attr_bytes)?;

        // Calculate and add attribute padding
        // NOTE: As per [RFC5389 Section 15](https://tools.ietf.org/html/rfc5389#section-15) padding bytes may take any value.
        // In this implementation NULL bytes are used
        let padding = 4 - data_len % 4;
        if padding != 4 {
            cursor.write_all(&vec![0u8; padding])?;
        }

        Ok(cursor.get_ref().to_vec())
    }

    // Encodes MappedAddress/XorMappedAddress/AlternateServer attributes.
    fn encode_address(
        addr: &SocketAddr,
        is_xored: bool,
        transaction_id: StunTransactionId,
    ) -> Result<Vec<u8>, AttributeEncodeError> {
        let family = match addr {
            SocketAddr::V4(_) => 0x01,
            SocketAddr::V6(_) => 0x02,
        };

        // Process the port number
        let port = addr.port();
        let mut port_bytes = [0u8, 2];
        NetworkEndian::write_u16(&mut port_bytes, port);
        // XOR the port number bytes if the attribute type is XorMappedAddress
        if is_xored {
            xor_byte_range(&mut port_bytes, &STUN_MAGIC_COOKIE);
        }

        let ip_addr_bytes = match addr {
            SocketAddr::V4(addr_v4) => {
                let mut ip_data = addr_v4.ip().octets();

                // XOR the ip address bytes if the attribute type is XorMappedAddress
                if is_xored {
                    xor_byte_range(&mut ip_data, &STUN_MAGIC_COOKIE)
                }

                ip_data.to_vec()
            }
            SocketAddr::V6(addr_v6) => {
                let segments = addr_v6.ip().segments();

                let mut ip_cursor = Cursor::new(Vec::new());
                for segment in segments.iter() {
                    ip_cursor.write_u16::<NetworkEndian>(*segment)?;
                }

                let ip_addr_bytes = ip_cursor.get_mut();

                // XOR the ip address bytes if the attribute type is XorMappedAddress
                if is_xored {
                    xor_byte_range(&mut ip_addr_bytes[0..4], &STUN_MAGIC_COOKIE);
                    xor_byte_range(&mut ip_addr_bytes[4..16], &transaction_id);
                }

                ip_addr_bytes.to_vec()
            }
        };

        let mut cursor = Cursor::new(Vec::new());

        // Write leading zeroes
        cursor.write_u8(0)?;
        // Write ip address family
        cursor.write_u8(family)?;
        // Write socket port number
        cursor.write_all(&port_bytes)?;
        // Write (XORed) ip address bytes
        cursor.write_all(&ip_addr_bytes)?;

        Ok(cursor.get_ref().to_vec())
    }

    // Encodes attributes containing Unicode values
    fn encode_utf8_val(data: &str, limit: Option<usize>) -> Result<Vec<u8>, AttributeEncodeError> {
        let encoded_val = data.as_bytes().to_vec();

        // Make sure we don't cross the size limit
        match limit {
            None => Ok(encoded_val),
            Some(size_limit) => {
                if encoded_val.len() > size_limit {
                    Err(AttributeEncodeError::Utf8ValueTooBig {
                        limit: size_limit,
                        length: encoded_val.len(),
                    })
                } else {
                    Ok(encoded_val)
                }
            }
        }
    }

    // Encodes attributes containing DWORD values.
    fn encode_u32_val(value: u32) -> Result<Vec<u8>, AttributeEncodeError> {
        let mut buf: Vec<u8> = vec![0u8; 4];
        NetworkEndian::write_u32(&mut buf, value);

        Ok(buf)
    }

    // Encodes attributes containing QWORD values.
    fn encode_u64_val(value: u64) -> Result<Vec<u8>, AttributeEncodeError> {
        let mut buf: Vec<u8> = vec![0u8; 8];
        NetworkEndian::write_u64(&mut buf, value);

        Ok(buf)
    }

    // Encodes the ErrorCode attribute.
    fn encode_error_code(
        class: u8,
        number: u8,
        reason: &str,
    ) -> Result<Vec<u8>, AttributeEncodeError> {
        let mut cursor = Cursor::new(Vec::new());
        // Write leading zeroes
        cursor.write_u16::<NetworkEndian>(0)?;
        // Write error class
        cursor.write_u8(class)?;
        // Write error number
        cursor.write_u8(number)?;
        // Write readable error reason
        cursor.write_all(&Self::encode_utf8_val(reason, Some(763))?)?;

        Ok(cursor.get_ref().to_vec())
    }

    // Encodes the UnknownAttributes attribute.
    fn encode_unknown_attributes(unknown_attrs: Vec<u16>) -> Result<Vec<u8>, AttributeEncodeError> {
        let mut cursor = Cursor::new(Vec::new());

        // Write each attribute type into the list
        for attr in unknown_attrs.iter() {
            cursor.write_u16::<NetworkEndian>(*attr)?;
        }

        Ok(cursor.get_ref().to_vec())
    }

    /// Encodes StunAttribute into bytes
    pub(crate) fn encode(
        &self,
        transaction_id: StunTransactionId,
    ) -> Result<Vec<u8>, AttributeEncodeError> {
        let (attr_type, encoded_attr) = match self {
            StunAttribute::XorMappedAddress { socket_addr } => (
                StunAttributeType::XorMappedAddress,
                Self::encode_address(socket_addr, true, transaction_id),
            ),
            StunAttribute::MappedAddress { socket_addr } => (
                StunAttributeType::MappedAddress,
                Self::encode_address(socket_addr, false, transaction_id),
            ),
            StunAttribute::Username { value } => (
                StunAttributeType::Username,
                Self::encode_utf8_val(value, Some(513)),
            ),
            StunAttribute::MessageIntegrity { key } => {
                (StunAttributeType::MessageIntegrity, Ok(key.clone()))
            }
            StunAttribute::Software { description } => (
                StunAttributeType::Software,
                Self::encode_utf8_val(description, Some(763)),
            ),
            StunAttribute::AlternateServer { socket_addr } => (
                StunAttributeType::AlternateServer,
                Self::encode_address(socket_addr, false, transaction_id),
            ),
            StunAttribute::Realm { value } => (
                StunAttributeType::Realm,
                Self::encode_utf8_val(value, Some(763)),
            ),
            StunAttribute::Nonce { value } => (
                StunAttributeType::Nonce,
                Self::encode_utf8_val(value, Some(763)),
            ),
            StunAttribute::Fingerprint { value } => {
                (StunAttributeType::Fingerprint, Self::encode_u32_val(*value))
            }
            StunAttribute::IceControlled { tie_breaker } => (
                StunAttributeType::IceControlled,
                Self::encode_u64_val(*tie_breaker),
            ),
            StunAttribute::IceControlling { tie_breaker } => (
                StunAttributeType::IceControlling,
                Self::encode_u64_val(*tie_breaker),
            ),
            StunAttribute::Priority { value } => {
                (StunAttributeType::Priority, Self::encode_u32_val(*value))
            }
            StunAttribute::ErrorCode {
                class,
                number,
                reason,
            } => (
                StunAttributeType::ErrorCode,
                Self::encode_error_code(*class, *number, reason),
            ),
            StunAttribute::UnknownAttributes { types } => (
                StunAttributeType::UnknownAttributes,
                Self::encode_unknown_attributes(types.clone()),
            ),
            StunAttribute::UseCandidate => (StunAttributeType::UseCandidate, Ok(Vec::new())),
        };

        // Wrap the encoded attribute data into TLV
        Self::wrap_in_tlv(attr_type, &encoded_attr?)
    }
}