diff --git a/rules/net_if/tgt_refcount.rs b/rules/net_if/tgt_refcount.rs new file mode 100644 index 00000000..46fd4e58 --- /dev/null +++ b/rules/net_if/tgt_refcount.rs @@ -0,0 +1,14 @@ +// Copyright (c) 2022-present INESC-ID. +// Distributed under the MIT license that can be found in the LICENSE file. + +use libcc2rs::*; + +fn f1(a0: Ptr) -> u32 { + match nix::net::if_::if_nametoindex(a0.to_rust_string().as_str()) { + Ok(__i) => __i, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + 0 + } + } +} diff --git a/rules/src/modules.rs b/rules/src/modules.rs index f91da1da..9228261b 100644 --- a/rules/src/modules.rs +++ b/rules/src/modules.rs @@ -94,6 +94,8 @@ pub mod map_tgt_refcount; pub mod map_tgt_unsafe; #[path = r#"../math/tgt_unsafe.rs"#] pub mod math_tgt_unsafe; +#[path = r#"../net_if/tgt_refcount.rs"#] +pub mod net_if_tgt_refcount; #[path = r#"../net_if/tgt_unsafe.rs"#] pub mod net_if_tgt_unsafe; #[path = r#"../netdb/tgt_refcount.rs"#] diff --git a/tests/unit/ifaddrs.c b/tests/unit/ifaddrs.c index 0b5e80a5..aa3f8176 100644 --- a/tests/unit/ifaddrs.c +++ b/tests/unit/ifaddrs.c @@ -1,5 +1,6 @@ #include #include +#include #include #include #include @@ -28,9 +29,12 @@ int main(void) { struct sockaddr_in *mask = (struct sockaddr_in *)ifa->ifa_netmask; unsigned char mask_be[4] = {255, 0, 0, 0}; assert(memcmp(&mask->sin_addr, mask_be, 4) == 0); + assert(if_nametoindex(ifa->ifa_name) > 0); } } assert(found_loopback); freeifaddrs(list); + + assert(if_nametoindex("cpp2rust_no_such_if") == 0); return 0; } diff --git a/tests/unit/out/refcount/ifaddrs.rs b/tests/unit/out/refcount/ifaddrs.rs index 3e533a0b..20dc87a9 100644 --- a/tests/unit/out/refcount/ifaddrs.rs +++ b/tests/unit/out/refcount/ifaddrs.rs @@ -104,6 +104,20 @@ fn main_0() -> i32 { == 0) as i32) != 0) ); + assert!( + (((match nix::net::if_::if_nametoindex( + (*(*(*ifa.borrow()).upgrade().deref()).ifa_name.borrow()) + .to_rust_string() + .as_str() + ) { + Ok(__i) => __i, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + 0 + } + } > 0_u32) as i32) + != 0) + ); } let __rhs = (*(*(*ifa.borrow()).upgrade().deref()).ifa_next.borrow()).clone(); (*ifa.borrow_mut()) = __rhs; @@ -131,5 +145,19 @@ fn main_0() -> i32 { __cur = __next; } }; + assert!( + (((match nix::net::if_::if_nametoindex( + Ptr::from_string_literal(b"cpp2rust_no_such_if") + .to_rust_string() + .as_str() + ) { + Ok(__i) => __i, + Err(__e) => { + libcc2rs::cpp2rust_errno().write(__e as i32); + 0 + } + } == 0_u32) as i32) + != 0) + ); return 0; } diff --git a/tests/unit/out/unsafe/ifaddrs.rs b/tests/unit/out/unsafe/ifaddrs.rs index 9244e84e..44c3636e 100644 --- a/tests/unit/out/unsafe/ifaddrs.rs +++ b/tests/unit/out/unsafe/ifaddrs.rs @@ -79,10 +79,18 @@ unsafe fn main_0() -> i32 { }) == (0)) as i32) != 0) ); + assert!( + ((((libc::if_nametoindex(((*ifa).ifa_name).cast_const())) > (0_u32)) as i32) != 0) + ); } ifa = (*ifa).ifa_next; } assert!((found_loopback != 0)); libc::freeifaddrs(list); + assert!( + ((((libc::if_nametoindex((c"cpp2rust_no_such_if".as_ptr().cast_mut()).cast_const())) + == (0_u32)) as i32) + != 0) + ); return 0; }