From e505d5b0c1de5a8ec72d4986147bf88ed27df4be Mon Sep 17 00:00:00 2001
From: Mamadou BAH <mamadulamarana.bah.etu@univ-lille.fr>
Date: Wed, 5 Mar 2025 17:41:46 +0100
Subject: [PATCH] connexion ftp avec ip

---
 .gitignore      |  5 ++++
 tp1/ips.txt     |  7 +++++
 tp1/output.txt  |  3 ++
 tp1/src/main.rs | 80 +++++++++++++++++++++++++++++++++++++++----------
 4 files changed, 79 insertions(+), 16 deletions(-)
 create mode 100644 tp1/ips.txt
 create mode 100644 tp1/output.txt

diff --git a/.gitignore b/.gitignore
index 389159a..9363676 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 0000000..c035839
--- /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 0000000..bd0d95d
--- /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 fa87dcc..7d5b0c8 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)
+        }
+    }
+}
 
 
 
-- 
GitLab