Skip to content

Commit

Permalink
add: examples for dotcp, dotlc y doudp
Browse files Browse the repository at this point in the history
  • Loading branch information
FranciscaOrtegaG committed Dec 23, 2024
1 parent f473a1c commit 3d84179
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/example/cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "ej_rust"
version = "0.1.0"
edition = "2021"

[dependencies]
dns_rust = { path="../dns-rust"}
base64 = "0.22.1"
45 changes: 45 additions & 0 deletions src/example/dotcp.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
41 changes: 41 additions & 0 deletions src/example/dotls.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
45 changes: 45 additions & 0 deletions src/example/doudp.rs
Original file line number Diff line number Diff line change
@@ -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(())
}

0 comments on commit 3d84179

Please sign in to comment.