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

DNS Resolver Implementation #4

Merged
merged 2 commits into from
Oct 22, 2022
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
Prev Previous commit
[dns_resolver] Implemented feature
  • Loading branch information
CosminPerRam committed Oct 22, 2022
commit d369341ccc3583e8d1083fb49c822ebe6f7fe5e6
7 changes: 5 additions & 2 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -22,11 +22,13 @@ pub enum GDError {
/// Unknown cast while translating a value to an enum.
UnknownEnumCast,
/// The server queried is not from the queried game.
BadGame(String)
BadGame(String),
/// Problems occurred while dns resolving.
DnsResolve(String),
}

impl fmt::Display for GDError {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
match self {
GDError::PacketOverflow(details) => write!(f, "Packet overflow: {details}"),
GDError::PacketUnderflow(details) => write!(f, "Packet underflow: {details}"),
@@ -36,6 +38,7 @@ impl fmt::Display for GDError {
GDError::Decompress(details) => write!(f, "Couldn't decompress data: {details}"),
GDError::UnknownEnumCast => write!(f, "Unknown enum cast encountered."),
GDError::BadGame(details) => write!(f, "Queried another game that the supposed one: {details}"),
GDError::DnsResolve(details) => write!(f, "DNS Resolve: {details}"),
}
}
}
14 changes: 7 additions & 7 deletions src/protocols/valve.rs
Original file line number Diff line number Diff line change
@@ -308,11 +308,11 @@ impl SplitPacket {
}

impl ValveProtocol {
fn new(address: &str, port: u16) -> Self {
Self {
fn new(address: &str, port: u16) -> GDResult<Self> {
Ok(Self {
socket: UdpSocket::bind("0.0.0.0:0").unwrap(),
complete_address: complete_address(address, port)
}
complete_address: complete_address(address, port)?
})
}

fn send(&self, data: &[u8]) -> GDResult<()> {
@@ -324,8 +324,8 @@ impl ValveProtocol {
let mut buf: Vec<u8> = vec![0; buffer_size];
let (amt, _) = self.socket.recv_from(&mut buf.as_mut_slice()).map_err(|e| GDError::PacketReceive(e.to_string()))?;

if amt < 9 {
return Err(GDError::PacketUnderflow("Any Valve Protocol response can't be under 9 bytes long.".to_string()));
if amt < 6 {
return Err(GDError::PacketUnderflow("Any Valve Protocol response can't be under 6 bytes long.".to_string()));
}

Ok(buf[..amt].to_vec())
@@ -489,7 +489,7 @@ impl ValveProtocol {
}

pub(crate) fn query(app: App, address: &str, port: u16, gather: GatheringSettings) -> Result<Response, GDError> {
let client = ValveProtocol::new(address, port);
let client = ValveProtocol::new(address, port)?;

let info = client.get_server_info(&app)?;

22 changes: 16 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
use std::ops::Add;
use trust_dns_resolver::config::{ResolverConfig, ResolverOpts};
use trust_dns_resolver::Resolver;
use crate::{GDResult, GDError};

pub fn complete_address(address: &str, port: u16) -> String {
String::from(address.to_owned() + ":").add(&*port.to_string())
fn resolve_dns(address: &str) -> GDResult<String> {
let resolver = Resolver::new(ResolverConfig::default(), ResolverOpts::default())
.map_err(|e| GDError::DnsResolve(e.to_string()))?;

let response = resolver.lookup_ip(address)
.map_err(|e| GDError::DnsResolve(e.to_string()))?;

Ok(response.iter().next().ok_or(GDError::DnsResolve("Couldn't resolve the DNS address.".to_string()))?.to_string())
}

pub fn complete_address(address: &str, port: u16) -> GDResult<String> {
Ok(resolve_dns(address)? + ":" + &*port.to_string())
}

pub mod buffer {
@@ -73,9 +84,8 @@ mod tests {

#[test]
fn complete_address_test() {
let address = "192.168.0.1";
let port = 27015;
assert_eq!(complete_address(address, port), "192.168.0.1:27015");
assert_eq!(complete_address("192.168.0.1", 27015).unwrap(), "192.168.0.1:27015");
assert!(complete_address("not_existent_address", 9999).is_err());
}

#[test]