From 3d84179ab592dbb6db2caf5e7cc2362f4bd56a26 Mon Sep 17 00:00:00 2001 From: FranciscaOrtegaG Date: Fri, 20 Dec 2024 12:31:24 -0300 Subject: [PATCH] add: examples for dotcp, dotlc y doudp --- src/example/cargo.toml | 8 ++++++++ src/example/dotcp.rs | 45 ++++++++++++++++++++++++++++++++++++++++++ src/example/dotls.rs | 41 ++++++++++++++++++++++++++++++++++++++ src/example/doudp.rs | 45 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 139 insertions(+) create mode 100644 src/example/cargo.toml create mode 100644 src/example/dotcp.rs create mode 100644 src/example/dotls.rs create mode 100644 src/example/doudp.rs diff --git a/src/example/cargo.toml b/src/example/cargo.toml new file mode 100644 index 00000000..6242bf72 --- /dev/null +++ b/src/example/cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ej_rust" +version = "0.1.0" +edition = "2021" + +[dependencies] +dns_rust = { path="../dns-rust"} +base64 = "0.22.1" diff --git a/src/example/dotcp.rs b/src/example/dotcp.rs new file mode 100644 index 00000000..ca78cb8a --- /dev/null +++ b/src/example/dotcp.rs @@ -0,0 +1,45 @@ + +use std::net::{IpAddr, Ipv4Addr}; +use std::time::Duration; +use tokio::runtime::Runtime; +use crate::client::client_connection::ClientConnection; +use crate::client::tcp_connection::ClientTCPConnection; +use crate::domain_name::DomainName; +use crate::message::rclass::Rclass; +use crate::message::rrtype::Rrtype; +use crate::message::DnsMessage; +use crate::client::client_error::ClientError; + +fn main() -> Result<(), ClientError> { + let rt = Runtime::new().unwrap(); + + + let server_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)); + + let timeout = Duration::from_secs(5); + + + let tcp_connection = ClientTCPConnection::new_default(server_ip, timeout); + + + let dns_query = DnsMessage::new_query_message(DomainName::new_from_string("example.com".to_string()), + Rrtype::A, + Rclass::IN, + 0, + false, + 1); + + + rt.block_on(async { + match tcp_connection.send(dns_query).await { + Ok(response) => { + println!("Respuesta recibida: {:?}", response); + } + Err(e) => { + eprintln!("Error al enviar la consulta DNS: {:?}", e); + } + } + }); + + Ok(()) +} \ No newline at end of file diff --git a/src/example/dotls.rs b/src/example/dotls.rs new file mode 100644 index 00000000..a2ff3b5e --- /dev/null +++ b/src/example/dotls.rs @@ -0,0 +1,41 @@ +use std::net::{IpAddr, Ipv4Addr, Ipv6Addr}; +use std::time::Duration; +use tokio::runtime::Runtime; +use crate::client::client_connection::ClientConnection; +use crate::client::client_security::ClientSecurity; +use crate::client::tcp_connection::ClientTCPConnection; +use crate::client::tls_connection::ClientTLSConnection; +use crate::domain_name::DomainName; +use crate::message::rclass::Rclass; +use crate::message::rrtype::Rrtype; +use crate::message::DnsMessage; +use crate::client::client_error::ClientError; +fn main() -> Result<(), ClientError> { + let rt = Runtime::new().unwrap(); + + let server_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)); + + let timeout = Duration::from_secs(5); + + let tls_connection = ClientTLSConnection::new_default(server_ip, timeout); + + let dns_query = DnsMessage::new_query_message(DomainName::new_from_string("example.com".to_string()), + Rrtype::A, + Rclass::IN, + 0, + false, + 1);; + + rt.block_on(async { + match tls_connection.send(dns_query).await { + Ok(response) => { + println!("Respuesta recibida: {:?}", response); + } + Err(e) => { + eprintln!("Error al enviar la consulta DNS: {:?}", e); + } + } + }); + + Ok(()) +} \ No newline at end of file diff --git a/src/example/doudp.rs b/src/example/doudp.rs new file mode 100644 index 00000000..42f06aa3 --- /dev/null +++ b/src/example/doudp.rs @@ -0,0 +1,45 @@ +use std::net::{IpAddr, Ipv4Addr}; +use std::time::Duration; +use tokio::runtime::Runtime; +use crate::client::client_connection::ClientConnection; +use crate::client::udp_connection::ClientUDPConnection; +use crate::domain_name::DomainName; +use crate::message::rclass::Rclass; +use crate::message::rrtype::Rrtype; +use crate::message::DnsMessage; +use crate::client::client_error::ClientError; + +fn main() -> Result<(), ClientError> { + // Crear una instancia de Runtime para ejecutar tareas asincrónicas + let rt = Runtime::new().unwrap(); + + // Dirección IP del servidor DNS + let server_ip = IpAddr::V4(Ipv4Addr::new(192, 168, 0, 1)); + // Tiempo de espera para la conexión + let timeout = Duration::from_secs(5); + + // Crear una instancia de ClientUDPConnection + let udp_connection = ClientUDPConnection::new_default(server_ip, timeout); + + // Crear una consulta DNS (esto es solo un ejemplo, ajusta según tu implementación) + let dns_query = DnsMessage::new_query_message(DomainName::new_from_string("example.com".to_string()), + Rrtype::A, + Rclass::IN, + 0, + false, + 1); + + // Ejecutar la tarea asincrónica en el Runtime + rt.block_on(async { + match udp_connection.send(dns_query).await { + Ok(response) => { + println!("Respuesta recibida: {:?}", response); + } + Err(e) => { + eprintln!("Error al enviar la consulta DNS: {:?}", e); + } + } + }); + + Ok(()) +} \ No newline at end of file