From ab632711027ea7c8ee9b61fcadaffad1433c7646 Mon Sep 17 00:00:00 2001 From: femsci Date: Sun, 1 Aug 2021 23:50:45 +0200 Subject: [PATCH] Initial --- .gitignore | 1 + Cargo.lock | 7 ++++++ Cargo.toml | 8 +++++++ src/main.rs | 69 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 85 insertions(+) create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..241bbc3 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "ip2i" +version = "0.1.0" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..7eabac1 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ip2i" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..325738e --- /dev/null +++ b/src/main.rs @@ -0,0 +1,69 @@ +fn main() { + let args: Vec = std::env::args().collect(); + + if args.len() < 2 { + eprintln!("Missing arguments: ."); + std::process::exit(1); + } + + let ip_string = &args[1]; + + eprintln!("Result: \n{}", ip_to_int(ip_string)); +} + +fn ip_to_int(ip_string: &str) -> u32 { + let ip_octets: Vec<&str> = ip_string.split('.').collect(); + + if ip_octets.len() > 4 { + panic!("Invalid IP address: {}.", ip_string); + } + + let mut ip_address: u32 = 0; + + for i in 0..4 { + let octet: u32 = ip_octets.get(i).unwrap_or(&"0").parse().expect("Not a number."); + + if octet > 254 { + panic!("Invalid octet: {}", octet); + } + + ip_address = (ip_address << 8) + octet; + } + + return ip_address; +} + +#[cfg(test)] +mod tests { + use super::ip_to_int; + + #[test] + fn test_ip4octets_should_yield_u32() { + assert_eq!(ip_to_int("1.1.1.1"), 16843009); + assert_eq!(ip_to_int("10.0.0.1"), 167772161); + assert_eq!(ip_to_int("192.168.64.254"), 3232252158); + assert_eq!(ip_to_int("131.195.227.142"), 2210653070); + assert_eq!(ip_to_int("28.70.183.136"), 474396552); + assert_eq!(ip_to_int("186.199.97.154"), 3133628826); + } + + #[test] + fn test_ip3octets_should_yield_u32() { + assert_eq!(ip_to_int("1.1.1"), 16843008); + assert_eq!(ip_to_int("10.0.0.0"), 167772160); + assert_eq!(ip_to_int("192.168.64.0"), 3232251904); + assert_eq!(ip_to_int("131.195.227"), 2210652928); + assert_eq!(ip_to_int("28.70.183.0"), 474396416); + assert_eq!(ip_to_int("186.199.97"), 3133628672); + } + + #[test] + fn test_ip2octets_should_yield_u32() { + assert_eq!(ip_to_int("1.1.0.0"), 16842752); + assert_eq!(ip_to_int("10.0.0"), 167772160); + assert_eq!(ip_to_int("192.168"), 3232235520); + assert_eq!(ip_to_int("131.195"), 2210594816); + assert_eq!(ip_to_int("28.70.0"), 474349568); + assert_eq!(ip_to_int("186.199.0.0"), 3133603840); + } +}