aorecomp

Anarchy Online classic in the browser

git clone https://aorecomp.ubeagle.com/code/aorecomp.git


commit 1cb0e6eb0203803b6105bc8de43018d9f6f3fc70
parent 87d9f8a4992a21e39e6140318608de50a5ff7250
Author: anon <anon@example.com>
Date:   Thu, 15 Oct 2026 11:04:31 +0000

Translate native socket endpoint addresses to the guest ABI

Diffstat:
Mdecompiled/runtime/linux_network.c | 34++++++++++++++++++++++++++++++++++
Mdecompiled/tests/network_startup.c | 56++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 90 insertions(+), 0 deletions(-)

diff --git a/decompiled/runtime/linux_network.c b/decompiled/runtime/linux_network.c @@ -190,6 +190,39 @@ static void connect_socket(void) g_eax = error ? UINT32_MAX : 0; } +static void socket_address(int peer) +{ + uint32_t handle = MEM32(g_esp + 4), output = MEM32(g_esp + 8), length = MEM32(g_esp + 12); + g_esp += 16; + g_eax = UINT32_MAX; + int descriptor = socket_descriptor(handle, 0); + if (descriptor < 0) return; + uint32_t error = 0; + if (!output || !length || (int32_t)MEM32(length) < 16) error = 10014; + else { + struct sockaddr_in address = {0}; + socklen_t size = sizeof(address); + int result = peer ? getpeername(descriptor, (struct sockaddr *)&address, &size) : + getsockname(descriptor, (struct sockaddr *)&address, &size); + if (result) error = socket_error(errno); + else if (!peer && !address.sin_port) error = 10022; /* Winsock rejects unbound sockets. */ + else { + if (address.sin_family != AF_INET) abort(); + MEM16(output) = 2; + MEM16(output + 2) = address.sin_port; + MEM32(output + 4) = address.sin_addr.s_addr; + memset((void *)ADDR(output + 8), 0, 8); + MEM32(length) = 16; + g_eax = 0; + } + } + if (close(descriptor)) abort(); + MEM32(g_fs_base + 0x34) = error; +} + +static void local_socket_address(void) { socket_address(0); } +static void peer_socket_address(void) { socket_address(1); } + static void transfer_socket(int sending) { uint32_t handle = MEM32(g_esp + 4), buffer = MEM32(g_esp + 8), flags = MEM32(g_esp + 16); @@ -481,6 +514,7 @@ recomp_func_t linux_network_lookup(const char *name, uint32_t ordinal) {"select", 18, select_sockets}, {"send", 19, send_socket}, {"recv", 16, receive_socket}, {"shutdown", 22, shutdown_socket}, + {"getsockname", 6, local_socket_address}, {"getpeername", 5, peer_socket_address}, }; for (unsigned i = 0; i < sizeof(functions) / sizeof(functions[0]); ++i) if (name ? !strcmp(name, functions[i].name) : ordinal == functions[i].ordinal) diff --git a/decompiled/tests/network_startup.c b/decompiled/tests/network_startup.c @@ -195,6 +195,55 @@ static int socket_shutdown(uint32_t handle, uint32_t how) #endif } +static int socket_name(int peer, uint32_t handle, unsigned char *bytes, int32_t *length) +{ +#ifdef _WIN32 + return peer ? getpeername(handle, (struct sockaddr *)bytes, (int *)length) : + getsockname(handle, (struct sockaddr *)bytes, (int *)length); +#else + MEM32(0x50000b00) = 0x12345678; + MEM32(0x50000b1c) = 0x87654321; + if (bytes) memcpy((void *)ADDR(0x50000b04), bytes, 24); + if (length) MEM32(0x50000b20) = *length; + uint32_t arguments[] = {handle, bytes ? 0x50000b04 : 0, length ? 0x50000b20 : 0}; + int result = call(peer ? 5 : 6, 3, arguments); + if (bytes) memcpy(bytes, (void *)ADDR(0x50000b04), 24); + if (length) *length = MEM32(0x50000b20); + assert(MEM32(0x50000b00) == 0x12345678 && MEM32(0x50000b1c) == 0x87654321); + return result; +#endif +} + +static void socket_addresses(uint32_t handle, const struct sockaddr_in *local, + const struct sockaddr_in *remote) +{ + for (int peer = 0; peer < 2; ++peer) { + for (unsigned size = 0; size < 4; ++size) { + const int32_t lengths[] = {0, 15, 16, 24}; + int32_t length = lengths[size]; + unsigned char bytes[24]; + memset(bytes, 0xa5, sizeof(bytes)); + set_error(777); + + int result = socket_name(peer, handle, bytes, &length); + + if (size < 2) { + assert(result == -1 && get_error() == 10014 && length == lengths[size]); + for (unsigned i = 0; i < sizeof(bytes); ++i) assert(bytes[i] == 0xa5); + } else { + assert(result == 0 && get_error() == 0 && length == 16); + assert(!memcmp(bytes, peer ? remote : local, 16)); + for (unsigned i = 16; i < sizeof(bytes); ++i) assert(bytes[i] == 0xa5); + } + } + int32_t length = 16; + assert(socket_name(peer, handle, NULL, &length) == -1 && get_error() == 10014); + unsigned char bytes[24]; + memset(bytes, 0xa5, sizeof(bytes)); + assert(socket_name(peer, handle, bytes, NULL) == -1 && get_error() == 10014); + } +} + static int socket_control(uint32_t handle, uint32_t command, uint32_t *value) { #ifdef _WIN32 @@ -335,6 +384,7 @@ static void loopback_connect(int nonblocking, int listening) length = sizeof(peer); uint32_t accepted = accept(listener, (struct sockaddr *)&peer, &length); assert(accepted != UINT32_MAX && peer.sin_addr.s_addr == htonl(0x7f000001)); + socket_addresses(handle, &peer, &address); struct socket_set read = {1, {handle}}, write = {1, {handle}}, except = {1, {handle}}; int32_t timeout[] = {0, 10000}; double start = monotonic_seconds(); @@ -432,6 +482,11 @@ static void socket_lifetime(int initialized) continue; } assert(handle != UINT32_MAX && get_error() == 0); + unsigned char local[24]; + memset(local, 0xa5, sizeof(local)); + int32_t local_length = sizeof(local); + assert(socket_name(0, handle, local, &local_length) == -1 && get_error() == 10022); + assert(socket_name(1, handle, local, &local_length) == -1 && get_error() == 10057); assert(socket_connect(handle, NULL, 16) == -1 && get_error() == 10014); struct sockaddr_in address = {0}; assert(socket_connect(handle, &address, 15) == -1 && get_error() == 10014); @@ -601,5 +656,6 @@ int main(void) puts("select: pending connect success/failure, read/write readiness, timeout, infinite wait, invalid sets and unread bytes passed"); puts("transport: binary send/recv, peek, partial reads, would-block, graceful close, invalid lengths and canaries passed"); puts("shutdown: send/receive half-close, peer EOF, disabled directions, invalid handles/modes and guest ABI passed"); + puts("socket addresses: local/peer endpoints, 16-byte IPv4 layout, buffer lengths, nulls, unbound sockets and canaries passed"); return 0; }