Skip to content

Commit

Permalink
added get ip from the sender
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscaOrtegaG committed Jan 8, 2024
1 parent 27e8c70 commit a6dbdf3
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
3 changes: 1 addition & 2 deletions src/client/client_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ pub trait ClientConnection: Copy {//: 'static + Sized + Send + Sync + Unpin
timeout:Duration) -> Self;

//Sends query
fn send(self,dns_query:DnsMessage) -> Result<Vec<u8>, ClientError>;

fn send(self, dns_query: DnsMessage) -> Result<(Vec<u8>, IpAddr), ClientError>;
fn get_ip(&self) -> IpAddr;
}

Expand Down
9 changes: 5 additions & 4 deletions src/client/tcp_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use super::client_error::ClientError;


use std::io::{Write, Read};
use std::net::{TcpStream,SocketAddr,IpAddr};
use std::net::{TcpStream,SocketAddr,IpAddr, Ipv4Addr};
use std::time::Duration;
use std::io::Error as IoError;
use std::io::ErrorKind;
Expand Down Expand Up @@ -34,7 +34,7 @@ impl ClientConnection for ClientTCPConnection {
}

/// creates socket tcp, sends query and receive response
fn send(self, dns_query: DnsMessage) -> Result<Vec<u8>, ClientError>{
fn send(self, dns_query: DnsMessage) -> Result<(Vec<u8>, IpAddr), ClientError>{

let timeout: Duration = self.get_timeout();
let bytes: Vec<u8> = dns_query.to_bytes();
Expand All @@ -58,6 +58,7 @@ impl ClientConnection for ClientTCPConnection {

let tcp_msg_len: u16 = (msg_size_response[0] as u16) << 8 | msg_size_response[1] as u16;
let mut vec_msg: Vec<u8> = Vec::new();
let ip = self.get_server_addr();

while vec_msg.len() < tcp_msg_len as usize {
let mut msg = [0; 512];
Expand All @@ -69,7 +70,7 @@ impl ClientConnection for ClientTCPConnection {
vec_msg.extend_from_slice(&msg[..number_of_bytes_msg]);
}

return Ok(vec_msg);
return Ok((vec_msg, ip));
}
}

Expand Down Expand Up @@ -186,7 +187,7 @@ mod tcp_connection_test{
0,
false,
1);
let response = conn_new.send(dns_query).unwrap();
let (response, ip) = conn_new.send(dns_query).unwrap();

assert!(DnsMessage::from_bytes(&response).unwrap().get_answer().len() > 0);
// FIXME:
Expand Down

0 comments on commit a6dbdf3

Please sign in to comment.