https://github.com/pacatro/subnet
A library to create IPv4 subnetworks based on a given address and the number of hosts you want to connect.
https://github.com/pacatro/subnet
network rust subnetworks
Last synced: about 1 year ago
JSON representation
A library to create IPv4 subnetworks based on a given address and the number of hosts you want to connect.
- Host: GitHub
- URL: https://github.com/pacatro/subnet
- Owner: Pacatro
- Created: 2024-01-25T12:48:16.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2024-03-04T15:52:36.000Z (over 2 years ago)
- Last Synced: 2025-03-28T06:23:37.110Z (about 1 year ago)
- Topics: network, rust, subnetworks
- Language: Rust
- Homepage: https://docs.rs/subnet/0.1.0/subnet/
- Size: 16.6 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Subnet
A library to create IPv4 subnetworks based on a given address and the number of hosts you want to connect.
## ✏️ Usage
```rust
use std::net::Ipv4Addr;
use subnet::{self, SubnetData, SubnetError};
fn main() {
let addrs: Ipv4Addr = Ipv4Addr::new(192, 168, 20, 0);
let hosts: u32 = 120;
let subnet: SubnetData = subnet::create_subnet(addrs, hosts).unwrap_or_else(|err: SubnetError| {
println!("Error: {}", err);
std::process::exit(1);
});
let useful_range: Vec = subnet.useful_range();
let bin_subnet: String = subnet::ip_to_binary(subnet.subnet_addrs());
println!(
"Subnet address: {}\nBin subnet: {}\nBroadcast: {}\nMask: /{}\nUseful range: [{} - {}]",
subnet.subnet_addrs(), bin_subnet, subnet.broadcast(), subnet.mask(),
useful_range.first().unwrap(), useful_range.last().unwrap()
);
}
```
### Output
```terminal
Subnet address: 192.168.20.0
Bin subnet: 11000000101010000001010000000000
Broadcast: 192.168.20.127
Mask: /25
Useful range: [192.168.20.1 - 192.168.20.126]
```
## 📖 Add the library to your project
You can add the crate with `cargo add`
```terminal
cargo add subnet
```
Alternatively, you can manually add it to your project's Cargo.toml like this:
```toml
[dependencies]
subnet = "*" # Change the `*` to the current version
```
## 💻 Contributing
If you want to add new features to the libray, you need to follow this steps.
Clone this repository
```terminal
git clone git@github.com:Pacatro/subnet.git
cd subnet
```
Run tests
```terminal
cargo test
```
Run example
```terminal
cargo run --example subnet
```
## 🔑 License
[MIT](https://opensource.org/license/mit/) - Created by [**P4k0**](https://github.com/Pacatro).