Fix host bug

This commit is contained in:
snow flurry 2023-01-17 00:00:57 -08:00
parent e7113d6772
commit f2c5d1073d
4 changed files with 14 additions and 3 deletions

1
Cargo.lock generated
View file

@ -1086,6 +1086,7 @@ name = "nzr-api"
version = "0.1.0" version = "0.1.0"
dependencies = [ dependencies = [
"figment", "figment",
"log",
"serde", "serde",
"tarpc", "tarpc",
"tokio", "tokio",

View file

@ -11,4 +11,5 @@ serde = { version = "1", features = ["derive"] }
tarpc = { version = "0.31", features = ["tokio1", "unix"] } tarpc = { version = "0.31", features = ["tokio1", "unix"] }
tokio = { version = "1.0", features = ["macros"] } tokio = { version = "1.0", features = ["macros"] }
uuid = "1.2.2" uuid = "1.2.2"
trust-dns-proto = { version = "0.22.0", features = ["serde-config"] } trust-dns-proto = { version = "0.22.0", features = ["serde-config"] }
log = "0.4.17"

View file

@ -162,6 +162,11 @@ impl CidrV4 {
/// This method is not affected by the object's host address. /// This method is not affected by the object's host address.
pub fn make_ip(&self, host_bits: u32) -> Result<Ipv4Addr, Error> { pub fn make_ip(&self, host_bits: u32) -> Result<Ipv4Addr, Error> {
if host_bits > !self.netmask { if host_bits > !self.netmask {
log::error!(
"Host bits too large ({:032b} vs {:032b})",
!self.netmask,
host_bits
);
Err(Error::HostBitsTooLarge) Err(Error::HostBitsTooLarge)
} else { } else {
Ok((host_bits | u32::from(self.network().addr)).into()) Ok((host_bits | u32::from(self.network().addr)).into())

View file

@ -140,14 +140,18 @@ impl Entity<Subnet> {
) -> Result<Entity<Lease>, Box<dyn std::error::Error>> { ) -> Result<Entity<Lease>, Box<dyn std::error::Error>> {
let tree = self.db.open_tree(self.lease_tree())?; let tree = self.db.open_tree(self.lease_tree())?;
let max_lease = match tree.last()? { let max_lease = match tree.last()? {
Some(lease) => u32::from_be_bytes(lease.0[..4].try_into().unwrap()), Some(lease) => {
// XXX: this is overkill, but a lazy hack for now
u32::from_be_bytes(lease.0[..4].try_into().unwrap())
& !u32::from(self.model.network.netmask())
}
None => self.model.start_bytes(), None => self.model.start_bytes(),
}; };
let new_ip = self let new_ip = self
.model .model
.network .network
.make_ip(max_lease + 1) .make_ip(max_lease + 1)
.map_err(|_| SubnetError::SubnetFull)?; .map_err(SubnetError::BadHost)?;
let lease_data = Lease { let lease_data = Lease {
subnet: String::from_utf8_lossy(&self.key).to_string(), subnet: String::from_utf8_lossy(&self.key).to_string(),
ipv4_addr: CidrV4::new(new_ip, self.model.network.cidr()), ipv4_addr: CidrV4::new(new_ip, self.model.network.cidr()),