diff --git a/src/info/help.rs b/src/info/help.rs new file mode 100644 index 0000000..2079777 --- /dev/null +++ b/src/info/help.rs @@ -0,0 +1,7 @@ +#[allow(dead_code)] +pub fn run(){ + println!("IPv4 Subnet Calculator written in Rust"); + println!("USAGE: \n\trusnet [IP Address] [/Mask]\n"); + println!("\t--h, --help display this help and exit"); + println!("\t--v, --version output version information and exit\n"); +} \ No newline at end of file diff --git a/src/info/mod.rs b/src/info/mod.rs new file mode 100644 index 0000000..c9dddb7 --- /dev/null +++ b/src/info/mod.rs @@ -0,0 +1,2 @@ +pub mod help; +pub mod version; \ No newline at end of file diff --git a/src/info/version.rs b/src/info/version.rs new file mode 100644 index 0000000..8d5d52a --- /dev/null +++ b/src/info/version.rs @@ -0,0 +1,8 @@ +#[allow(dead_code)] +pub fn run(){ + println!("rusnet 0.1.0"); + println!("Copyright (C) 2022 Gabriel L. Pereira."); + println!("This program is free software: you can redistribute it and/or modify."); + println!("This program comes with ABSOLUTELY NO WARRANTY."); + println!("\nWritten by Gabriel L. Pereira "); +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 8aab3ab..9c9696d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,54 +1,86 @@ mod ip_fn; +mod info; use std::env; +/* rusnet -- IPv4 Subnet Calculator + Copyright (C) 2022 Gabriel L. Pereira. + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + */ + +/* + * author Gabriel L. Pereira aka + * version 0.1.0 + * since Jul 1, 2022 + */ +// Rewrite it with clap && add new mask functions fn main() { + // Get the arguments from the command line let args: Vec = env::args() .collect(); + + // Split the first argument into a vector(".") let ip: Vec<&str> = args[1] .split(".") .collect(); + + // Split the second argument("/") let mask: String = args[2] .split("/") .collect(); + + // Parse the mask into a u16 -- String to u16 let mask_parsed: u16 = mask .parse() .unwrap(); + // Parse the ip into a vector of u16 -- Vec<&str> to Vec let mut ip_parsed: Vec = vec![]; for i in 0..ip.len() { - let ip_int: u16 = ip[i] + let ip_u16: u16 = ip[i] .parse() .unwrap(); - ip_parsed.push(ip_int); + ip_parsed.push(ip_u16); } - println!("Net Address => {}.{}.{}.{}", ip_parsed[0], ip_parsed[1], ip_parsed[2], ip_parsed[3]); - let sn = ip_fn::subnet::sn(mask_parsed); + let subnet = ip_fn::subnet::sn(mask_parsed); println!("Subnet Mask => {}.{}.{}.{}", - sn[0], sn[1], sn[2], sn[3]); + subnet[0], subnet[1], subnet[2], subnet[3]); - let bc = ip_fn::broadcast::bc(sn[3]); + let broadcast = ip_fn::broadcast::bc(subnet[3]); println!("Broadcast Address => {}.{}.{}.{}", - ip_parsed[0], ip_parsed[1], ip_parsed[2], bc); + ip_parsed[0], ip_parsed[1], ip_parsed[2], broadcast); - let cl = ip_fn::class::class(ip_parsed[0]); - println!("Standard Class => {}", cl); + let class = ip_fn::class::class(ip_parsed[0]); + println!("Standard Class => {}", class); - let rg = ip_fn::range::rg(ip_parsed[3], sn[3]); + let range = ip_fn::range::rg(ip_parsed[3], subnet[3]); println!("Range => {} ~ {}", - rg[0], rg[1]); + range[0], range[1]); - println!("Net Address Binary => {:b}.{:b}.{:b}.{:b}", + println!("Net Address Binary => {:08b}.{:08b}.{:08b}.{:08b}", ip_parsed[0], ip_parsed[1], ip_parsed[2], ip_parsed[3]); - println!("Mask Binary => {:b}.{:b}.{:b}.{:b}", - sn[0], sn[1], sn[2], sn[3]); + println!("Mask Binary => {:08b}.{:08b}.{:08b}.{:08b}", + subnet[0], subnet[1], subnet[2], subnet[3]); - println!("Broadcast Address Binary => {:b}.{:b}.{:b}.{:b}", - ip_parsed[0], ip_parsed[1], ip_parsed[2], bc); + println!("Broadcast Address Binary => {:08b}.{:08b}.{:08b}.{:08b}", + ip_parsed[0], ip_parsed[1], ip_parsed[2], broadcast); -} \ No newline at end of file +}