diff --git a/.gitignore b/.gitignore index 389159a37c53aaee3a6142a65ba90f98eab7cfdd..93636762ff7545c76fec1f6eb3ed968671778319 100644 --- a/.gitignore +++ b/.gitignore @@ -18,3 +18,8 @@ Cargo.lock *.pdb # End of https://www.toptal.com/developers/gitignore/api/rust + + +# Added by cargo + +/target diff --git a/tp1/ips.txt b/tp1/ips.txt new file mode 100644 index 0000000000000000000000000000000000000000..c035839b84e94fb402774fa9a68aec8071f7ae38 --- /dev/null +++ b/tp1/ips.txt @@ -0,0 +1,7 @@ +127.0.0.1 +::1 +192.168.100.40 +Obviously malformed ligne + +10.0.278.1 +10.0.0.1 diff --git a/tp1/output.txt b/tp1/output.txt new file mode 100644 index 0000000000000000000000000000000000000000..bd0d95de13c3a6ceea627aaa9716e783c0b535a3 --- /dev/null +++ b/tp1/output.txt @@ -0,0 +1,3 @@ +Hello, world! +Parsing ips file +Connecting to 127.0.0.1... \ No newline at end of file diff --git a/tp1/src/main.rs b/tp1/src/main.rs index fa87dccb77504febb08b13888c344c15799bdc37..7d5b0c85de3969f1c8cb0b211bb345bf2d992100 100644 --- a/tp1/src/main.rs +++ b/tp1/src/main.rs @@ -1,4 +1,6 @@ use std::{io, net::IpAddr}; +use std::net::{TcpStream, SocketAddr}; +use std::time::Duration; pub enum PasswordError { Empty, @@ -28,7 +30,7 @@ impl PasswordChecker { } } -fn main() { +fn main() -> Result<(), io::Error>{ println!("Hello, world!"); // let x: i8 = 42; @@ -43,33 +45,79 @@ fn main() { // check_password(); - println!("Enter password: "); - let mut input = String::new(); - let _ = io::stdin().read_line(&mut input); - let checker = PasswordChecker::new("Rust"); - - // Call the check function on the checker. - // This function should have the same return type as - // the check_password function of the previous exercise - match checker.check(&input) { - Ok(()) => println!("ok"), - Err(PasswordError::Empty) => println!("Empty Password"), - Err(PasswordError::Wrong) => println!("Wrong password"), + // println!("Enter password: "); + // let mut input = String::new(); + // let _ = io::stdin().read_line(&mut input); + // let checker = PasswordChecker::new("Rust"); + + // // Call the check function on the checker. + // // This function should have the same return type as + // // the check_password function of the previous exercise + // match checker.check(&input) { + // Ok(()) => println!("ok"), + // Err(PasswordError::Empty) => println!("Empty Password"), + // Err(PasswordError::Wrong) => println!("Wrong password"), + // } + + let ports = vec![20, 80, 443]; + println!("Parsing ips file"); + match get_addrs("ips.txt") { + Ok(ips) => { + for ip in &ips { + println!("Connecting to {} ....", *ip); + for port in &ports { + let _ = try_connect(*ip, *port); + } + } + } + Err(e) => eprintln!("Erreur lors de la lecture du fichier : {}", e), } + + Ok(()) } /// Reads a file that contains an address per line and returns a Vector with all well-formed /// addresses and prints a warning on the standard error output for every malformed lines. /// /// Addresses should be ipv4 or ipv6 addresses. -fn get_addrs(path: &str) -> Result<Vec<IpAddr>, std::io::Error> { todo!{} } +fn get_addrs(path: &str) -> Result<Vec<IpAddr>, std::io::Error> { + let content = std::fs::read_to_string(path)?; + let mut addresses = Vec::new(); + + for line in content.lines() { + let line = line.trim(); // Supprimer les espaces inutiles + if line.is_empty() { + continue; // Ignorer les lignes vides + } + + match line.parse::<IpAddr>() { + Ok(addr) => addresses.push(addr), // Ajouter l'adresse seulement si elle est valide + Err(_) => eprintln!("'{}' : is not a correct IP address invalid IP address syntax", line), + } + } + + Ok(addresses) // Retourner uniquement les adresses valides +} /// Tries to connect to a given host with a TCP Connection with a one second timeout. /// /// Returns Ok(()) or a corresponding error if connection fails. -fn try_connect(addr: IpAddr, port: u16) -> Result<(), std::io::Error> { todo!() } - +/// +fn try_connect(addr: IpAddr, port: u16) -> Result<(), std::io::Error> { + let socket = SocketAddr::new(addr, port); + let timeout = Duration::from_secs(1); + match TcpStream::connect_timeout(&socket, timeout) { + Ok(_) => { + println!(" port {port}: alive"); + Ok(()) + } + Err(e) => { + eprintln!(" port {port}: down (Connection refused ({e}))"); + Err(e) + } + } +}