mirror of
https://github.com/MacosPorters/betterxen.git
synced 2026-11-26 03:39:52 +00:00
Format guest IPv4 addresses through Winsock inet_ntop
Signed-off-by: Adeeb Rahman <therealabeeb@icloud.com>
This commit is contained in:
1 parent
7a91e11742
commit
342fcbd17d
2 files changed
+47
-1
No files matched your search
@@ -2,7 +2,7 @@
|
||||
|
||||
//! Process-local Winsock UDP loopback for the synthetic guest.
|
||||
|
||||
use super::ServicesState;
|
||||
use super::{ERROR_INVALID_PARAMETER, ServicesState};
|
||||
use crate::cpu::ExitReason;
|
||||
use crate::imports::{ImportRegistry, ServiceStatus};
|
||||
use crate::memory::GuestMemory;
|
||||
@@ -205,6 +205,34 @@ fn register_address_conversion(registry: &mut ImportRegistry, state: &Arc<Servic
|
||||
Ok(u64::from((args.rcx as u32).swap_bytes()))
|
||||
});
|
||||
}
|
||||
let format_state = Arc::clone(state);
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
"inet_ntop",
|
||||
ServiceStatus::Partial,
|
||||
move |args, cpu, memory| {
|
||||
let fail = |error| format_state.fail(cpu.gs_base, error, 0);
|
||||
if args.rcx as u32 != 2 {
|
||||
return fail(WSAEAFNOSUPPORT);
|
||||
}
|
||||
if args.r8 == 0 || args.r9 == 0 {
|
||||
return fail(ERROR_INVALID_PARAMETER);
|
||||
}
|
||||
let mut address = [0; 4];
|
||||
if args.rdx == 0 || memory.read_bytes(args.rdx, &mut address).is_err() {
|
||||
return fail(WSAEFAULT);
|
||||
}
|
||||
let mut text = std::net::Ipv4Addr::from(address).to_string().into_bytes();
|
||||
text.push(0);
|
||||
if (text.len() as u64) > args.r9 {
|
||||
return fail(ERROR_INVALID_PARAMETER);
|
||||
}
|
||||
if memory.write_bytes(args.r8, &text).is_err() {
|
||||
return fail(WSAEFAULT);
|
||||
}
|
||||
Ok(args.r8)
|
||||
},
|
||||
);
|
||||
let state = Arc::clone(state);
|
||||
registry.register_with_status(
|
||||
MODULE,
|
||||
|
||||
@@ -339,6 +339,7 @@ fn guest_udp_loopback_delivers_datagrams_through_bound_imports() {
|
||||
"WSACleanup",
|
||||
"WSAPoll",
|
||||
"inet_pton",
|
||||
"inet_ntop",
|
||||
"htons",
|
||||
];
|
||||
let imports = symbols.map(|symbol| ("ws2_32.dll", Symbol::Name(symbol)));
|
||||
@@ -378,6 +379,7 @@ fn guest_udp_loopback_delivers_datagrams_through_bound_imports() {
|
||||
let incoming = SCRATCH + 0x240;
|
||||
let text = SCRATCH + 0x260;
|
||||
let poll = SCRATCH + 0x280;
|
||||
let formatted = SCRATCH + 0x2a0;
|
||||
memory.write_u16(address, 2).unwrap();
|
||||
let port = call(&mut memory, "htons", &[5000]);
|
||||
memory.write_u16(address + 2, port as u16).unwrap();
|
||||
@@ -398,6 +400,22 @@ fn guest_udp_loopback_delivers_datagrams_through_bound_imports() {
|
||||
u64::from(u32::MAX)
|
||||
);
|
||||
assert_eq!(call(&mut memory, "WSAGetLastError", &[]), 10047);
|
||||
memory.write_u8(formatted, 0xaa).unwrap();
|
||||
assert_eq!(
|
||||
call(&mut memory, "inet_ntop", &[2, address + 4, formatted, 9]),
|
||||
0
|
||||
);
|
||||
assert_eq!(call(&mut memory, "WSAGetLastError", &[]), 87);
|
||||
assert_eq!(memory.read_u8(formatted).unwrap(), 0xaa);
|
||||
assert_eq!(
|
||||
call(&mut memory, "inet_ntop", &[2, address + 4, formatted, 10]),
|
||||
formatted
|
||||
);
|
||||
let mut formatted_address = [0; 10];
|
||||
memory
|
||||
.read_bytes(formatted, &mut formatted_address)
|
||||
.unwrap();
|
||||
assert_eq!(&formatted_address, b"127.0.0.1\0");
|
||||
assert_eq!(call(&mut memory, "WSAStartup", &[0x0202, SCRATCH]), 0);
|
||||
let receiver = call(&mut memory, "socket", &[2, 2, 17]);
|
||||
let sender = call(&mut memory, "socket", &[2, 2, 17]);
|
||||
|
||||
Reference in new issue
Block a user