Skip to content

Commit b6f0fd8

Browse files
committed
Add safe pton and ntop rules
1 parent 6b0264a commit b6f0fd8

5 files changed

Lines changed: 687 additions & 0 deletions

File tree

rules/arpa_inet/tgt_refcount.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2022-present INESC-ID.
2+
// Distributed under the MIT license that can be found in the LICENSE file.
3+
4+
use libcc2rs::*;
5+
6+
fn f5(a0: i32, a1: Ptr<u8>, a2: AnyPtr) -> i32 {
7+
if a0 == libc::AF_INET {
8+
match a1.to_rust_string().parse::<std::net::Ipv4Addr>() {
9+
Ok(__ip) => {
10+
let __octets = __ip.octets();
11+
for __i in 0..4 {
12+
a2.reinterpret_cast::<u8>().offset(__i).write(__octets[__i]);
13+
}
14+
1
15+
}
16+
Err(_) => 0,
17+
}
18+
} else if a0 == libc::AF_INET6 {
19+
match a1.to_rust_string().parse::<std::net::Ipv6Addr>() {
20+
Ok(__ip) => {
21+
let __octets = __ip.octets();
22+
for __i in 0..16 {
23+
a2.reinterpret_cast::<u8>().offset(__i).write(__octets[__i]);
24+
}
25+
1
26+
}
27+
Err(_) => 0,
28+
}
29+
} else {
30+
-1
31+
}
32+
}
33+
34+
fn f6(a0: i32, a1: AnyPtr, a2: Ptr<u8>, a3: u32) -> Ptr<u8> {
35+
let __text = if a0 == libc::AF_INET {
36+
let mut __b = [0u8; 4];
37+
for __i in 0..4 {
38+
__b[__i] = a1.reinterpret_cast::<u8>().offset(__i).read();
39+
}
40+
Some(std::net::Ipv4Addr::from(__b).to_string())
41+
} else if a0 == libc::AF_INET6 {
42+
let mut __b = [0u8; 16];
43+
for __i in 0..16 {
44+
__b[__i] = a1.reinterpret_cast::<u8>().offset(__i).read();
45+
}
46+
Some(std::net::Ipv6Addr::from(__b).to_string())
47+
} else {
48+
None
49+
};
50+
match __text {
51+
Some(__s) if (__s.len() as u32) < a3 => {
52+
for __i in 0..__s.len() {
53+
a2.offset(__i).write(__s.as_bytes()[__i]);
54+
}
55+
a2.offset(__s.len()).write(0);
56+
a2.clone()
57+
}
58+
_ => Ptr::null(),
59+
}
60+
}

rules/src/modules.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
pub mod algorithm_tgt_refcount;
33
#[path = r#"../algorithm/tgt_unsafe.rs"#]
44
pub mod algorithm_tgt_unsafe;
5+
#[path = r#"../arpa_inet/tgt_refcount.rs"#]
6+
pub mod arpa_inet_tgt_refcount;
57
#[path = r#"../arpa_inet/tgt_unsafe.rs"#]
68
pub mod arpa_inet_tgt_unsafe;
79
#[path = r#"../array/tgt_refcount.rs"#]

tests/unit/inet_pton_ntop.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#include <arpa/inet.h>
2+
#include <assert.h>
3+
#include <string.h>
4+
5+
int main(void) {
6+
unsigned char buf[16];
7+
assert(inet_pton(AF_INET, "1.2.3.4", buf) == 1);
8+
assert(buf[0] == 1 && buf[1] == 2 && buf[2] == 3 && buf[3] == 4);
9+
assert(inet_pton(AF_INET, "999.1.1.1", buf) == 0);
10+
assert(inet_pton(AF_INET, "not an ip", buf) == 0);
11+
assert(inet_pton(AF_INET6, "::1", buf) == 1);
12+
assert(buf[0] == 0 && buf[15] == 1);
13+
assert(inet_pton(AF_INET6, "2001:db8::5", buf) == 1);
14+
assert(buf[0] == 0x20 && buf[1] == 0x01 && buf[15] == 5);
15+
16+
char text[64];
17+
unsigned char four[4] = {10, 0, 0, 1};
18+
assert(strcmp(inet_ntop(AF_INET, four, text, sizeof(text)), "10.0.0.1") == 0);
19+
unsigned char sixteen[16] = {0};
20+
sixteen[15] = 1;
21+
assert(strcmp(inet_ntop(AF_INET6, sixteen, text, sizeof(text)), "::1") == 0);
22+
assert(inet_ntop(AF_INET, four, text, 4) == 0);
23+
return 0;
24+
}

0 commit comments

Comments
 (0)