Skip to content

Fix the functions #1879

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions kclvm/runtime/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -441,19 +441,26 @@ pub extern "C-unwind" fn kclvm_net_hosts_in_CIDR(
if let Some(cidr) = get_call_arg_str(args, kwargs, 0, Some("cidr")) {
let parts: Vec<&str> = cidr.split('/').collect();
if parts.len() == 2 {
let ip = parts[0];
let mask = parts[1];
if let Ok(ip) = Ipv4Addr::from_str(ip) {
if let Ok(mask) = mask.parse::<u8>() {
let mask = u32::from_be_bytes(ip.octets()) & !((1 << (32 - mask)) - 1);
let mut hosts = vec![];
for i in 1..(1 << (32 - mask)) - 1 {
let ip = u32::from_be_bytes(ip.octets()) + i;
hosts.push(ValueRef::str(Ipv4Addr::from(ip).to_string().as_str()));
}
let hosts_refs: Vec<&ValueRef> = hosts.iter().collect();
return ValueRef::list(Some(&hosts_refs[..])).into_raw(ctx);
if let (Ok(ip), Ok(mask)) = (Ipv4Addr::from_str(parts[0]), parts[1].parse::<u8>()) {
if mask > 32 {
return ValueRef::list(None).into_raw(ctx);
}

let ip_u32 = u32::from(ip);
let netmask = !((1 << (32 - mask)) - 1);
let network = ip_u32 & netmask;
let broadcast = network | !netmask;

let mut hosts = vec![];

// Exclude network (first) and broadcast (last) addresses
for i in 1..(broadcast - network) {
let host_ip = Ipv4Addr::from(network + i);
hosts.push(ValueRef::str(host_ip.to_string().as_str()));
}

let hosts_refs: Vec<&ValueRef> = hosts.iter().collect();
return ValueRef::list(Some(&hosts_refs)).into_raw(ctx);
}
}
return ValueRef::list(None).into_raw(ctx);
Expand Down
Loading