Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

format the code with rustfmt #1

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 9 additions & 15 deletions stun-proto/src/agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,11 @@ impl StunAgent {

/// Create a new [`StunRequest`] for encapsulating the state required for handling a
/// [`MessageClass::Request`]
pub fn stun_request_transaction(&self, msg: &Message, addr: SocketAddr) -> Result<StunRequest, StunError> {
pub fn stun_request_transaction(
&self,
msg: &Message,
addr: SocketAddr,
) -> Result<StunRequest, StunError> {
if !msg.has_class(MessageClass::Request) {
return Err(StunError::WrongImplementation);
}
Expand Down Expand Up @@ -679,11 +683,7 @@ impl StunAgentState {
}
};
// only validate response if the original request had credentials
if request
.msg
.attribute::<MessageIntegrity>()
.is_some()
{
if request.msg.attribute::<MessageIntegrity>().is_some() {
if let Some(remote_creds) = &self.remote_credentials {
match msg.validate_integrity(orig_data, remote_creds) {
Ok(_) => {
Expand Down Expand Up @@ -754,9 +754,7 @@ pub(crate) mod tests {
.remote_addr(remote_addr)
.build();
let msg = Message::new_request(BINDING);
let request = agent
.stun_request_transaction(&msg, remote_addr)
.unwrap();
let request = agent.stun_request_transaction(&msg, remote_addr).unwrap();
let now = Instant::now();
let ret = request.poll(now).unwrap();
assert!(matches!(ret, StunRequestPollRet::SendData(_)));
Expand Down Expand Up @@ -791,9 +789,7 @@ pub(crate) mod tests {
.remote_addr(remote_addr)
.build();
let msg = Message::new_request(BINDING);
let request = agent
.stun_request_transaction(&msg, remote_addr)
.unwrap();
let request = agent.stun_request_transaction(&msg, remote_addr).unwrap();
let mut now = Instant::now();
loop {
match request.poll(now) {
Expand All @@ -816,9 +812,7 @@ pub(crate) mod tests {
.remote_addr(remote_addr)
.build();
let msg = Message::new_request(BINDING);
let request = agent
.stun_request_transaction(&msg, remote_addr)
.unwrap();
let request = agent.stun_request_transaction(&msg, remote_addr).unwrap();
let now = Instant::now();
let ret = request.poll(now).unwrap();
if let StunRequestPollRet::SendData(Transmit {
Expand Down
14 changes: 8 additions & 6 deletions stun-types/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use std::convert::TryInto;

use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr};

use crate::message::{TransactionId, MAGIC_COOKIE, StunParseError, StunWriteError};
use crate::message::{StunParseError, StunWriteError, TransactionId, MAGIC_COOKIE};

use byteorder::{BigEndian, ByteOrder};

Expand Down Expand Up @@ -261,7 +261,9 @@ impl std::fmt::Display for RawAttribute {
UnknownAttributes::TYPE => display_attr!(self, UnknownAttributes, malformed_str),
Realm::TYPE => display_attr!(self, Realm, malformed_str),
Nonce::TYPE => display_attr!(self, Nonce, malformed_str),
MessageIntegritySha256::TYPE => display_attr!(self, MessageIntegritySha256, malformed_str),
MessageIntegritySha256::TYPE => {
display_attr!(self, MessageIntegritySha256, malformed_str)
}
PasswordAlgorithm::TYPE => display_attr!(self, PasswordAlgorithm, malformed_str),
//UserHash::TYPE => display_attr!(self, UserHash, malformed_str),
XorMappedAddress::TYPE => display_attr!(self, XorMappedAddress, malformed_str),
Expand Down Expand Up @@ -1424,8 +1426,8 @@ impl MessageIntegrity {
)]
pub fn compute(data: &[u8], key: &[u8]) -> Result<[u8; 20], StunParseError> {
use hmac::{Hmac, Mac};
let mut hmac = Hmac::<sha1::Sha1>::new_from_slice(key)
.map_err(|_| StunParseError::InvalidData)?;
let mut hmac =
Hmac::<sha1::Sha1>::new_from_slice(key).map_err(|_| StunParseError::InvalidData)?;
hmac.update(data);
Ok(hmac.finalize().into_bytes().into())
}
Expand Down Expand Up @@ -1759,8 +1761,8 @@ impl MessageIntegritySha256 {
)]
pub fn compute(data: &[u8], key: &[u8]) -> Result<[u8; 32], StunParseError> {
use hmac::{Hmac, Mac};
let mut hmac = Hmac::<sha2::Sha256>::new_from_slice(key)
.map_err(|_| StunParseError::InvalidData)?;
let mut hmac =
Hmac::<sha2::Sha256>::new_from_slice(key).map_err(|_| StunParseError::InvalidData)?;
hmac.update(data);
let ret = hmac.finalize().into_bytes();
Ok(ret.into())
Expand Down
3 changes: 1 addition & 2 deletions stun-types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ impl std::fmt::Display for TransportType {
}

pub mod prelude {
pub use crate::attribute::{AttributeToRaw, AttributeFromRaw};
pub use crate::attribute::{AttributeFromRaw, AttributeToRaw};
}

#[cfg(test)]
Expand All @@ -78,4 +78,3 @@ pub(crate) mod tests {
});
}
}

56 changes: 30 additions & 26 deletions stun-types/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -860,7 +860,9 @@ impl Message {
if algorithm == IntegrityAlgorithm::Sha1 && self.has_attribute(MessageIntegrity::TYPE) {
return Err(StunWriteError::AlreadyExists);
}
if algorithm == IntegrityAlgorithm::Sha256 && self.has_attribute(MessageIntegritySha256::TYPE) {
if algorithm == IntegrityAlgorithm::Sha256
&& self.has_attribute(MessageIntegritySha256::TYPE)
{
return Err(StunWriteError::AlreadyExists);
}
if self.has_attribute(Fingerprint::TYPE) {
Expand Down Expand Up @@ -1035,9 +1037,7 @@ impl Message {
attribute_type = %A::TYPE,
)
)]
pub fn attribute<A: AttributeFromRaw<StunParseError>>(
&self,
) -> Option<A> {
pub fn attribute<A: AttributeFromRaw<StunParseError>>(&self) -> Option<A> {
let atype = A::TYPE;
self.attributes
.iter()
Expand Down Expand Up @@ -1140,15 +1140,15 @@ impl Message {
/// let unknown = error_msg.attribute::<UnknownAttributes>().unwrap();
/// assert!(unknown.has_attribute(Username::TYPE));
/// ```
pub fn unknown_attributes(
src: &Message,
attributes: &[AttributeType],
) -> Message {
pub fn unknown_attributes(src: &Message, attributes: &[AttributeType]) -> Message {
let mut out = Message::new_error(src);
out.add_attribute(Software::new("stun-types").unwrap()).unwrap();
out.add_attribute(ErrorCode::new(420, "Unknown Attributes").unwrap()).unwrap();
out.add_attribute(Software::new("stun-types").unwrap())
.unwrap();
out.add_attribute(ErrorCode::new(420, "Unknown Attributes").unwrap())
.unwrap();
if !attributes.is_empty() {
out.add_attribute(UnknownAttributes::new(attributes)).unwrap();
out.add_attribute(UnknownAttributes::new(attributes))
.unwrap();
}
out
}
Expand All @@ -1169,8 +1169,10 @@ impl Message {
/// ```
pub fn bad_request(src: &Message) -> Message {
let mut out = Message::new_error(src);
out.add_attribute(Software::new("stun-types").unwrap()).unwrap();
out.add_attribute(ErrorCode::new(400, "Bad Request").unwrap()).unwrap();
out.add_attribute(Software::new("stun-types").unwrap())
.unwrap();
out.add_attribute(ErrorCode::new(400, "Bad Request").unwrap())
.unwrap();
out
}

Expand Down Expand Up @@ -1256,9 +1258,7 @@ mod tests {
assert_eq!(msg.method(), src.method());
let err = msg.attribute::<ErrorCode>().unwrap();
assert_eq!(err.code(), 420);
let unknown_attrs = msg
.attribute::<UnknownAttributes>()
.unwrap();
let unknown_attrs = msg.attribute::<UnknownAttributes>().unwrap();
assert!(unknown_attrs.has_attribute(Software::TYPE));
}

Expand Down Expand Up @@ -1309,11 +1309,9 @@ mod tests {
let bytes: Vec<_> = msg.clone().into();
msg.validate_integrity(&bytes, &credentials).unwrap();
let orig_hmac = match algorithm {
IntegrityAlgorithm::Sha1 => msg
.attribute::<MessageIntegrity>()
.unwrap()
.hmac()
.to_vec(),
IntegrityAlgorithm::Sha1 => {
msg.attribute::<MessageIntegrity>().unwrap().hmac().to_vec()
}
IntegrityAlgorithm::Sha256 => msg
.attribute::<MessageIntegritySha256>()
.unwrap()
Expand Down Expand Up @@ -1349,11 +1347,19 @@ mod tests {
src.add_attribute(Priority::new(123)).unwrap();

// success case
let res = Message::check_attribute_types(&src, &[Username::TYPE, Priority::TYPE], &[Username::TYPE]);
let res = Message::check_attribute_types(
&src,
&[Username::TYPE, Priority::TYPE],
&[Username::TYPE],
);
assert!(res.is_none());

// fingerprint required but not present
let res = Message::check_attribute_types(&src, &[Username::TYPE, Priority::TYPE], &[Fingerprint::TYPE]);
let res = Message::check_attribute_types(
&src,
&[Username::TYPE, Priority::TYPE],
&[Fingerprint::TYPE],
);
assert!(res.is_some());
let res = res.unwrap();
assert!(res.has_class(MessageClass::Error));
Expand All @@ -1369,9 +1375,7 @@ mod tests {
assert!(res.has_method(src.method()));
let err = res.attribute::<ErrorCode>().unwrap();
assert_eq!(err.code(), 420);
let unknown = res
.attribute::<UnknownAttributes>()
.unwrap();
let unknown = res.attribute::<UnknownAttributes>().unwrap();
assert!(unknown.has_attribute(Priority::TYPE));
}

Expand Down
Loading