Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
R
rust-tps
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Mamadu-lamarana Bah
rust-tps
Commits
e505d5b0
Commit
e505d5b0
authored
1 month ago
by
Mamadu-lamarana Bah
Browse files
Options
Downloads
Patches
Plain Diff
connexion ftp avec ip
parent
258a20a8
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
.gitignore
+5
-0
5 additions, 0 deletions
.gitignore
tp1/ips.txt
+7
-0
7 additions, 0 deletions
tp1/ips.txt
tp1/output.txt
+3
-0
3 additions, 0 deletions
tp1/output.txt
tp1/src/main.rs
+64
-16
64 additions, 16 deletions
tp1/src/main.rs
with
79 additions
and
16 deletions
.gitignore
+
5
−
0
View file @
e505d5b0
...
...
@@ -18,3 +18,8 @@ Cargo.lock
*.pdb
# End of https://www.toptal.com/developers/gitignore/api/rust
# Added by cargo
/target
This diff is collapsed.
Click to expand it.
tp1/ips.txt
0 → 100644
+
7
−
0
View file @
e505d5b0
127.0.0.1
::1
192.168.100.40
Obviously malformed ligne
10.0.278.1
10.0.0.1
This diff is collapsed.
Click to expand it.
tp1/output.txt
0 → 100644
+
3
−
0
View file @
e505d5b0
Hello, world!
Parsing ips file
Connecting to 127.0.0.1...
\ No newline at end of file
This diff is collapsed.
Click to expand it.
tp1/src/main.rs
+
64
−
16
View file @
e505d5b0
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
)
}
}
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment