Function stun_rs::get_input_text

source ·
pub fn get_input_text<A>(buffer: &[u8]) -> Option<Vec<u8>>
Expand description

Gets the input text used by attributes that requires validation. The text used as input for validation is the STUN message, up to and including the attribute preceding the specified attribute. The Length field of the STUN message header is adjusted to point to the end of the value of this attribute.

§Examples

 // Sample buffer
 let sample_ipv4_response = [
     0x01, 0x01, 0x00, 0x3c, // Response type and message length
     0x21, 0x12, 0xa4, 0x42, // Magic cookie
     0xb7, 0xe7, 0xa7, 0x01, // }
     0xbc, 0x34, 0xd6, 0x86, // }  Transaction ID
     0xfa, 0x87, 0xdf, 0xae, // }
     0x80, 0x22, 0x00, 0x0b, // SOFTWARE attribute header
     0x74, 0x65, 0x73, 0x74, // }
     0x20, 0x76, 0x65, 0x63, // }  UTF-8 server name (1 byte padding)
     0x74, 0x6f, 0x72, 0x20, // }
     0x00, 0x20, 0x00, 0x08, // XOR-MAPPED-ADDRESS attribute header
     0x00, 0x01, 0xa1, 0x47, // Address family (IPv4) and xor'd mapped port number
     0xe1, 0x12, 0xa6, 0x43, // Xor'd mapped IPv4 address
     0x00, 0x08, 0x00, 0x14, // MESSAGE-INTEGRITY header
     0x2b, 0x91, 0xf5, 0x99, // }
     0xfd, 0x9e, 0x90, 0xc3, // }
     0x8c, 0x74, 0x89, 0xf9, // } HMAC-SHA1 fingerprint
     0x2a, 0xf9, 0xba, 0x53, // }
     0xf0, 0x6b, 0xe7, 0xd7, // }
     0x80, 0x28, 0x00, 0x04, // FINGERPRINT attribute header
     0xc0, 0x7d, 0x4c, 0x96, // Reserved for CRC32 fingerprint
 ];

 // No message integrity SHA256 attribute in this buffer
 assert_eq!(get_input_text::<MessageIntegritySha256>(&sample_ipv4_response), None);

 // Get input buffer to validate the MessageIntegrity attribute
 let input = get_input_text::<MessageIntegrity>(&sample_ipv4_response).unwrap();

 // Input buffer includes the whole STUN message up to and including
 // the attribute preceding the MESSAGE-INTEGRITY attribute, and the length
 // is adjusted to point at the end of the MESSAGE-INTEGRITY value (52 bytes)
 assert_eq!(input, [
     0x01, 0x01, 0x00, 0x34, // Response type and message length (52 bytes)
     0x21, 0x12, 0xa4, 0x42, // Magic cookie
     0xb7, 0xe7, 0xa7, 0x01, // }
     0xbc, 0x34, 0xd6, 0x86, // }  Transaction ID
     0xfa, 0x87, 0xdf, 0xae, // }
     0x80, 0x22, 0x00, 0x0b, // SOFTWARE attribute header
     0x74, 0x65, 0x73, 0x74, // }
     0x20, 0x76, 0x65, 0x63, // }  UTF-8 server name (1 byte padding)
     0x74, 0x6f, 0x72, 0x20, // }
     0x00, 0x20, 0x00, 0x08, // XOR-MAPPED-ADDRESS attribute header
     0x00, 0x01, 0xa1, 0x47, // Address family (IPv4) and xor'd mapped port number
     0xe1, 0x12, 0xa6, 0x43, // Xor'd mapped IPv4 address
 ]);