Skip to content

Commit

Permalink
Cargo fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
izissise committed Oct 4, 2023
1 parent 34b997a commit c8e44c5
Show file tree
Hide file tree
Showing 6 changed files with 148 additions and 364 deletions.
3 changes: 1 addition & 2 deletions examples/register.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@ fn main() {
// Optionally, we can monitor the daemon events.
let monitor = mdns.monitor().expect("Failed to monitor the daemon");
let service_fullname = service_info.get_fullname().to_string();
mdns.register(service_info)
.expect("Failed to register mDNS service");
mdns.register(service_info).expect("Failed to register mDNS service");

println!("Registered service {}.{}", &instance_name, &service_type);

Expand Down
100 changes: 30 additions & 70 deletions src/dns_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,16 @@
use crate::log::{debug, error};
use crate::{Error, Result, ServiceInfo};
use if_addrs::Interface;
use std::{any::Any, cmp, collections::HashMap, fmt, net::{IpAddr, Ipv4Addr, Ipv6Addr}, str, time::SystemTime, convert::TryInto};
use std::{
any::Any,
cmp,
collections::HashMap,
convert::TryInto,
fmt,
net::{IpAddr, Ipv4Addr, Ipv6Addr},
str,
time::SystemTime,
};

pub(crate) const TYPE_A: u16 = 1; // IPv4 address
pub(crate) const TYPE_CNAME: u16 = 5;
Expand Down Expand Up @@ -50,12 +59,7 @@ pub(crate) struct DnsEntry {

impl DnsEntry {
fn new(name: String, ty: u16, class: u16) -> Self {
Self {
name,
ty,
class: class & CLASS_MASK,
unique: (class & CLASS_UNIQUE) != 0,
}
Self { name, ty, class: class & CLASS_MASK, unique: (class & CLASS_UNIQUE) != 0 }
}
}

Expand Down Expand Up @@ -85,13 +89,7 @@ impl DnsRecord {
let created = current_time_millis();
let refresh = get_expiration_time(created, ttl, 80);
let expires = get_expiration_time(created, ttl, 100);
Self {
entry: DnsEntry::new(name.to_string(), ty, class),
ttl,
created,
expires,
refresh,
}
Self { entry: DnsEntry::new(name.to_string(), ty, class), ttl, created, expires, refresh }
}

pub(crate) fn get_created(&self) -> u64 {
Expand Down Expand Up @@ -279,13 +277,7 @@ impl DnsSrv {
host: String,
) -> Self {
let record = DnsRecord::new(name, TYPE_SRV, class, ttl);
Self {
record,
priority,
weight,
host,
port,
}
Self { record, priority, weight, host, port }
}
}

Expand Down Expand Up @@ -431,12 +423,7 @@ pub(crate) struct DnsOutPacket {

impl DnsOutPacket {
pub(crate) fn new() -> Self {
Self {
data: Vec::new(),
size: 12,
state: PacketState::Init,
names: HashMap::new(),
}
Self { data: Vec::new(), size: 12, state: PacketState::Init, names: HashMap::new() }
}

fn write_question(&mut self, question: &DnsQuestion) {
Expand Down Expand Up @@ -520,11 +507,7 @@ impl DnsOutPacket {
fn write_name(&mut self, name: &str) {
// ignore the ending "." if exists
let end = name.len();
let end = if end > 0 && &name[end - 1..] == "." {
end - 1
} else {
end
};
let end = if end > 0 && &name[end - 1..] == "." { end - 1 } else { end };

let mut here = 0;
while here < end {
Expand Down Expand Up @@ -765,9 +748,7 @@ impl DnsOutgoing {
}

pub(crate) fn add_question(&mut self, name: &str, qtype: u16) {
let q = DnsQuestion {
entry: DnsEntry::new(name.to_string(), qtype, CLASS_IN),
};
let q = DnsQuestion { entry: DnsEntry::new(name.to_string(), qtype, CLASS_IN) };
self.questions.push(q);
}

Expand Down Expand Up @@ -904,9 +885,7 @@ impl DnsIncoming {
let class = u16_from_be_slice(&data[2..4]);
self.offset += 4;

self.questions.push(DnsQuestion {
entry: DnsEntry::new(name, ty, class),
});
self.questions.push(DnsQuestion { entry: DnsEntry::new(name, ty, class) });
}
Ok(())
}
Expand Down Expand Up @@ -952,27 +931,15 @@ impl DnsIncoming {

// decode RDATA based on the record type.
let rec: Option<DnsRecordBox> = match ty {
TYPE_A => Some(Box::new(DnsAddress::new(
&name,
ty,
class,
ttl,
self.read_ipv4().into(),
))),
TYPE_CNAME | TYPE_PTR => Some(Box::new(DnsPointer::new(
&name,
ty,
class,
ttl,
self.read_name()?,
))),
TYPE_TXT => Some(Box::new(DnsTxt::new(
&name,
ty,
class,
ttl,
self.read_vec(length),
))),
TYPE_A => {
Some(Box::new(DnsAddress::new(&name, ty, class, ttl, self.read_ipv4().into())))
}
TYPE_CNAME | TYPE_PTR => {
Some(Box::new(DnsPointer::new(&name, ty, class, ttl, self.read_name()?)))
}
TYPE_TXT => {
Some(Box::new(DnsTxt::new(&name, ty, class, ttl, self.read_vec(length))))
}
TYPE_SRV => Some(Box::new(DnsSrv::new(
&name,
class,
Expand All @@ -990,13 +957,9 @@ impl DnsIncoming {
self.read_char_string(),
self.read_char_string(),
))),
TYPE_AAAA => Some(Box::new(DnsAddress::new(
&name,
ty,
class,
ttl,
self.read_ipv6().into(),
))),
TYPE_AAAA => {
Some(Box::new(DnsAddress::new(&name, ty, class, ttl, self.read_ipv6().into())))
}
_ => {
debug!("Unknown DNS record type");
self.offset += length;
Expand Down Expand Up @@ -1109,10 +1072,7 @@ impl DnsIncoming {
}
_ => {
error!("self offset {}, data: {:x?}", &self.offset, data);
panic!(
"Bad domain name at length byte 0x{:x} (offset {})",
length, offset
);
panic!("Bad domain name at length byte 0x{:x} (offset {})", length, offset);
}
};
}
Expand Down
Loading

0 comments on commit c8e44c5

Please sign in to comment.