Skip to content

Commit 3a6440a

Browse files
committed
f use Hostname's validation
1 parent f49d571 commit 3a6440a

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

lightning/src/onion_message/dns_resolution.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ impl HumanReadableName {
169169
if user.is_empty() || domain.is_empty() {
170170
return Err(());
171171
}
172-
if !user.is_ascii() || !domain.is_ascii() {
172+
if !Hostname::str_is_valid_hostname(&user) || !Hostname::str_is_valid_hostname(&domain) {
173173
return Err(());
174174
}
175175
Ok(HumanReadableName { user, domain })

lightning/src/util/ser.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,6 +1490,16 @@ impl Hostname {
14901490
pub fn len(&self) -> u8 {
14911491
(&self.0).len() as u8
14921492
}
1493+
1494+
/// Check if the chars in `s` are allowed to be included in a [`Hostname`].
1495+
pub(crate) fn str_is_valid_hostname(s: &str) -> bool {
1496+
s.len() <= 255 &&
1497+
s.chars().all(|c|
1498+
c.is_ascii_alphanumeric() ||
1499+
c == '.' ||
1500+
c == '-'
1501+
)
1502+
}
14931503
}
14941504

14951505
impl core::fmt::Display for Hostname {
@@ -1525,11 +1535,7 @@ impl TryFrom<String> for Hostname {
15251535
type Error = ();
15261536

15271537
fn try_from(s: String) -> Result<Self, Self::Error> {
1528-
if s.len() <= 255 && s.chars().all(|c|
1529-
c.is_ascii_alphanumeric() ||
1530-
c == '.' ||
1531-
c == '-'
1532-
) {
1538+
if Hostname::str_is_valid_hostname(&s) {
15331539
Ok(Hostname(s))
15341540
} else {
15351541
Err(())

0 commit comments

Comments
 (0)