Skip to content

Commit

Permalink
doing : tcp tls implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscaOrtegaG committed Aug 14, 2024
1 parent 3a681b5 commit 1b2ea16
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 7 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ base64 = "0.22.1"
tokio-tls = "0.3.1"
lru = "0.12.3"
rustls = "0.23.12"

openssl = "0.10.66"
tokio-rustls = "0.26.0"
webpki = "0.22.4"
[lib]
doctest = false
5 changes: 0 additions & 5 deletions src/client/client_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@ use std::net::IpAddr;
use tokio::time::Duration;
use super::client_error::ClientError;

use tokio::net::TcpStream;
use tokio_rustls::rustls::{ClientConfig, ServerName};
use tokio_rustls::TlsConnector;
use std::sync::Arc;
use webpki::DNSNameRef;
use async_trait::async_trait;


Expand Down
20 changes: 19 additions & 1 deletion src/client/tcp_connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::message::rdata::a_rdata::ARdata;
use crate::message::resource_record::ResourceRecord;
use super::client_error::ClientError;
use async_trait::async_trait;
use webpki::DNSNameRef;
use std::io::Error as IoError;
use std::io::ErrorKind;
use tokio::io::AsyncWriteExt;
Expand All @@ -14,6 +15,10 @@ use std::net::IpAddr;
use std::net::SocketAddr;
use tokio::time::Duration;
use tokio::time::timeout;
use tokio_rustls::rustls::ClientConfig;
use tokio_rustls::TlsConnector;
use std::sync::Arc;
use webpki::DnsNameRef;

#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ClientTCPConnection {
Expand Down Expand Up @@ -46,7 +51,7 @@ impl ClientConnection for ClientTCPConnection {

let conn_timeout: Duration = self.get_timeout();
let bytes: Vec<u8> = dns_query.to_bytes();
let server_addr:SocketAddr = SocketAddr::new(self.get_server_addr(), 53);
let server_addr:SocketAddr = SocketAddr::new(self.get_server_addr(), 853);

// let mut stream: TcpStream = TcpStream::connect_timeout(&server_addr,timeout)?;
let conn_task = TcpStream::connect(&server_addr);
Expand All @@ -60,6 +65,19 @@ impl ClientConnection for ClientTCPConnection {
let tcp_bytes_length: [u8; 2] = [(msg_length >> 8) as u8, msg_length as u8];
let full_msg: Vec<u8> = [&tcp_bytes_length, bytes.as_slice()].concat();

//get domain name
let server_name = dns_query.get_question().get_qname().get_name();
let dns_name = DnsNameRef::try_from_ascii_str(&server_name);
if dns_name.is_err() {
return Err(ClientError::Io(IoError::new(ErrorKind::InvalidInput, format!("Error: invalid domain name"))).into());
}

let mut config = ClientConfig::builder();
config.root_hint_subjects.add_server_trust_anchors(&webpki_roots::TLS_SERVER_ROOTS);
let config = Arc::new(config);

let dns_name = dns_name.unwrap();
let connector = TlsConnector::from(Arc::new(config));
// stream.set_read_timeout(Some(timeout))?; //-> Se hace con tokio

// stream.write(&full_msg)?;
Expand Down

0 comments on commit 1b2ea16

Please sign in to comment.