Skip to content
Merged
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
63 changes: 63 additions & 0 deletions rules/arpa_inet/tgt_refcount.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) 2022-present INESC-ID.
// Distributed under the MIT license that can be found in the LICENSE file.

use libcc2rs::*;

fn f5(a0: i32, a1: Ptr<u8>, a2: AnyPtr) -> i32 {
if a0 == libc::AF_INET {
match a1.to_rust_string().parse::<std::net::Ipv4Addr>() {
Ok(__ip) => {
a2.reinterpret_cast::<u8>()
.with_slice_mut(4, |__s| __s.copy_from_slice(&__ip.octets()));
1
}
Err(_) => 0,
}
} else if a0 == libc::AF_INET6 {
match a1.to_rust_string().parse::<std::net::Ipv6Addr>() {
Ok(__ip) => {
a2.reinterpret_cast::<u8>()
.with_slice_mut(16, |__s| __s.copy_from_slice(&__ip.octets()));
1
}
Err(_) => 0,
}
} else {
libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT);
-1
}
}

fn f6(a0: i32, a1: AnyPtr, a2: Ptr<u8>, a3: u32) -> Ptr<u8> {
let __text = if a0 == libc::AF_INET {
let __b: [u8; 4] = a1
.reinterpret_cast::<u8>()
.with_slice(4, |__s| __s.try_into().unwrap());
Some(std::net::Ipv4Addr::from(__b).to_string())
} else if a0 == libc::AF_INET6 {
let __b: [u8; 16] = a1
.reinterpret_cast::<u8>()
.with_slice(16, |__s| __s.try_into().unwrap());
Some(std::net::Ipv6Addr::from(__b).to_string())
} else {
None
};
match __text {
Some(__s) if (__s.len() as u32) < a3 => {
let __n = __s.len();
a2.with_slice_mut(__n + 1, |__sl| {
__sl[..__n].copy_from_slice(__s.as_bytes());
__sl[__n] = 0;
});
a2.clone()
}
Some(_) => {
libcc2rs::cpp2rust_errno().write(::libc::ENOSPC);
Ptr::null()
}
None => {
libcc2rs::cpp2rust_errno().write(::libc::EAFNOSUPPORT);
Ptr::null()
}
}
}
2 changes: 2 additions & 0 deletions rules/src/modules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
pub mod algorithm_tgt_refcount;
#[path = r#"../algorithm/tgt_unsafe.rs"#]
pub mod algorithm_tgt_unsafe;
#[path = r#"../arpa_inet/tgt_refcount.rs"#]
pub mod arpa_inet_tgt_refcount;
#[path = r#"../arpa_inet/tgt_unsafe.rs"#]
pub mod arpa_inet_tgt_unsafe;
#[path = r#"../array/tgt_refcount.rs"#]
Expand Down
24 changes: 24 additions & 0 deletions tests/unit/inet_pton_ntop.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <arpa/inet.h>
#include <assert.h>
#include <string.h>

int main(void) {
unsigned char buf[16];
assert(inet_pton(AF_INET, "1.2.3.4", buf) == 1);
assert(buf[0] == 1 && buf[1] == 2 && buf[2] == 3 && buf[3] == 4);
assert(inet_pton(AF_INET, "999.1.1.1", buf) == 0);
assert(inet_pton(AF_INET, "not an ip", buf) == 0);
assert(inet_pton(AF_INET6, "::1", buf) == 1);
assert(buf[0] == 0 && buf[15] == 1);
assert(inet_pton(AF_INET6, "2001:db8::5", buf) == 1);
assert(buf[0] == 0x20 && buf[1] == 0x01 && buf[15] == 5);

char text[64];
unsigned char four[4] = {10, 0, 0, 1};
assert(strcmp(inet_ntop(AF_INET, four, text, sizeof(text)), "10.0.0.1") == 0);
unsigned char sixteen[16] = {0};
sixteen[15] = 1;
assert(strcmp(inet_ntop(AF_INET6, sixteen, text, sizeof(text)), "::1") == 0);
assert(inet_ntop(AF_INET, four, text, 4) == 0);
return 0;
}
Loading
Loading