Skip to content

[WIP] fix net ip6 to ip4 #1937

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

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
52 changes: 52 additions & 0 deletions kclvm/runtime/src/net/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,35 @@ pub extern "C-unwind" fn kclvm_net_to_IP4(
kclvm_net_IP_string(ctx, args, kwargs)
}

#[no_mangle]
#[runtime_fn]
pub extern "C-unwind" fn kclvm_net_IP6_to_IP4_string(
ctx: *mut kclvm_context_t,
args: *const kclvm_value_ref_t,
kwargs: *const kclvm_value_ref_t,
) -> *const kclvm_value_ref_t {
let args = ptr_as_ref(args);
let kwargs = ptr_as_ref(kwargs);
let ctx = mut_ptr_as_ref(ctx);
if let Some(ip) = get_call_arg_str(args, kwargs, 0, Some("ip")) {
match Ipv6Addr::from_str(ip.as_ref()) {
Ok(addr) => {
if let Some(v4) = addr.to_ipv4() {
let s = format!("{v4}");
return ValueRef::str(s.as_ref()).into_raw(ctx);
}
let s = format!("can not parse {} ipv6 to ipv4!", ip);
return ValueRef::str(s.as_ref()).into_raw(ctx);
}
Err(e) => {
let s = format!("can not parse {} to ipv6:{}", ip, e);
return ValueRef::str(s.as_ref()).into_raw(ctx);
}
}
}
panic!("IP_string() missing 1 required positional argument: 'ip'");
}

// to_IP16(ip) -> int

#[no_mangle]
Expand Down Expand Up @@ -911,6 +940,29 @@ pub extern "C-unwind" fn kclvm_net_CIDR_netmask(
mod test_net {
use super::*;

#[test]
#[allow(non_snake_case)]
fn test_ip6_to_ip4() {
let cases = [
("::FFFF:192.168.1.10", "192.168.1.10"),
(
"::FFFF:192.168.x.10",
"can not parse ::FFFF:192.168.x.10 to ipv6:invalid IPv6 address syntax",
),
];
let mut ctx = Context::default();
for (ip6, expected) in cases.iter() {
unsafe {
let actual = &*kclvm_net_IP6_to_IP4_string(
&mut ctx,
&ValueRef::list(Some(&[&ValueRef::str(ip6)])),
&ValueRef::dict(None),
);
assert_eq!(&ValueRef::str(expected), actual, "{} positional", ip6,);
}
}
}

#[test]
fn test_split_host_port() {
let cases = [
Expand Down
3 changes: 3 additions & 0 deletions test/grammar/builtins/net/to_ip4/main.k
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import net

ip0 = net.to_IP4("::FFFF:192.168.1.10")
1 change: 1 addition & 0 deletions test/grammar/builtins/net/to_ip4/stdout.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ip0: 192.168.1.10
Loading