Keyboard support + data requests via tcp server

This commit is contained in:
Ferdinand Schober
2022-09-19 17:20:50 +02:00
parent d4b4e7c269
commit 35414059a8
10 changed files with 451 additions and 174 deletions

40
src/dns.rs Normal file
View File

@@ -0,0 +1,40 @@
use std::{net::IpAddr, error::Error, fmt::Display};
use trust_dns_resolver::Resolver;
#[derive(Debug, Clone)]
struct InvalidConfigError;
#[derive(Debug, Clone)]
struct DnsError{
host: String,
}
impl Error for InvalidConfigError {}
impl Display for InvalidConfigError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "No hostname specified!")
}
}
impl Error for DnsError {}
impl Display for DnsError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "couldn't resolve host \"{}\"", self.host)
}
}
pub fn resolve(host: &Option<String>) -> Result<IpAddr, Box<dyn Error>> {
let host = match host {
Some(host) => host,
None => return Err(InvalidConfigError.into()),
};
let response = Resolver::from_system_conf()?.lookup_ip(host)?;
match response.iter().next() {
Some(ip) => Ok(ip),
None => Err(DnsError{host: host.clone()}.into()),
}
}