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

Added E164 address type support to AddressAvp #11

Merged
merged 2 commits into from
Jul 14, 2024
Merged
Changes from 1 commit
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
32 changes: 24 additions & 8 deletions src/avp/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::io::Write;
use std::net::Ipv4Addr;
use std::net::Ipv6Addr;

use super::octetstring::OctetString;
use super::utf8string::UTF8String;

#[derive(Debug, Clone)]
pub enum Value {
IPv4(Ipv4Addr),
IPv6(Ipv6Addr),
E164(OctetString), // TODO
E164(UTF8String),
lwlee2608 marked this conversation as resolved.
Show resolved Hide resolved
}

#[derive(Debug, Clone)]
Expand All @@ -31,8 +31,8 @@ impl Address {
Address(Value::IPv6(ip))
}

pub fn from_e164(octet: OctetString) -> Address {
Address(Value::E164(octet))
pub fn from_e164(str: UTF8String) -> Address {
Address(Value::E164(str))
}

pub fn decode_from<R: Read>(reader: &mut R, len: usize) -> Result<Address> {
Expand Down Expand Up @@ -67,7 +67,10 @@ impl Address {
Address(Value::IPv6(ip))
}
[0, 8] => {
todo!("E164 not implemented")
if len > 17 {
return Err(Error::DecodeError("Invalid address length".into()));
lwlee2608 marked this conversation as resolved.
Show resolved Hide resolved
}
Address(Value::E164(UTF8String::decode_from(reader, len)?))
}
_ => return Err(Error::DecodeError("Unsupported address type".into())),
};
Expand All @@ -84,7 +87,10 @@ impl Address {
writer.write_all(&[0, 2])?;
writer.write_all(&ip.octets())?;
}
Value::E164(_) => todo!(),
Value::E164(str) => {
writer.write_all(&[0, 8])?;
let _ = str.encode_to(writer);
}
};
Ok(())
}
Expand All @@ -93,7 +99,7 @@ impl Address {
match &self.0 {
Value::IPv4(_) => 6,
Value::IPv6(_) => 18,
Value::E164(_) => todo!(),
Value::E164(utf8string) => utf8string.length(),
}
}
}
Expand All @@ -103,7 +109,7 @@ impl fmt::Display for Value {
match self {
Value::IPv4(ip) => write!(f, "{}", ip),
Value::IPv6(ip) => write!(f, "{}", ip),
Value::E164(octet) => write!(f, "{}", octet),
Value::E164(str) => write!(f, "{}", str),
}
}
}
Expand Down Expand Up @@ -140,4 +146,14 @@ mod tests {
let avp = Address::decode_from(&mut cursor, 18).unwrap();
assert_eq!(avp.0.to_string(), "::1");
}

#[test]
fn test_encode_decode_e164() {
let avp = Address::new(Value::E164(UTF8String::new("359898000135")));
let mut encoded = Vec::new();
avp.encode_to(&mut encoded).unwrap();
let mut cursor = Cursor::new(&encoded);
let avp = Address::decode_from(&mut cursor, 12).unwrap();
lwlee2608 marked this conversation as resolved.
Show resolved Hide resolved
assert_eq!(avp.0.to_string(), "359898000135");
}
}