86 lines
4.7 KiB
C++
86 lines
4.7 KiB
C++
#include "prx/libc/include/general/VabiMacros.hpp"
|
|
#include <array>
|
|
#include <cstdint>
|
|
#include <cstdlib>
|
|
#include <cstring>
|
|
#include <thread>
|
|
#include <chrono>
|
|
extern "C" {
|
|
int APS5_VABI socket_nid_postfix(int, int, int);
|
|
int APS5_VABI fcntl_nid_postfix(int, int, ...);
|
|
int APS5_VABI setsockopt_nid_postfix(int, int, int, const void*, std::uint32_t);
|
|
int APS5_VABI getsockopt_nid_postfix(int, int, int, void*, std::uint32_t*);
|
|
int APS5_VABI bind_nid_postfix(int, const void*, std::uint32_t);
|
|
int APS5_VABI getsockname_nid_postfix(int, void*, std::uint32_t*);
|
|
int APS5_VABI ioctl_nid_postfix(int, std::uint64_t, void*);
|
|
std::int64_t APS5_VABI sendto_nid_postfix(int, const void*, std::uint64_t, int, const void*, std::uint32_t);
|
|
std::int64_t APS5_VABI recvfrom_nid_postfix(int, void*, std::uint64_t, int, void*, std::uint32_t*);
|
|
int APS5_VABI close_nid_postfix(int);
|
|
const char* APS5_VABI inet_ntop_nid_postfix(int, const void*, char*, std::uint32_t);
|
|
int APS5_VABI inet_pton_nid_postfix(int, const char*, void*);
|
|
int* APS5_VABI __error_nid_postfix();
|
|
}
|
|
static void Require(bool value) { if (!value) std::abort(); }
|
|
int main() {
|
|
const int receiver = socket_nid_postfix(2, 2, 0);
|
|
const int sender = socket_nid_postfix(2, 2, 17);
|
|
Require(receiver >= 0 && sender >= 0 && receiver != sender);
|
|
std::array<unsigned char, 16> destination{16, 2, 0, 0, 127, 0, 0, 1};
|
|
Require(bind_nid_postfix(receiver, destination.data(), destination.size()) == 0);
|
|
std::uint32_t size = destination.size();
|
|
Require(getsockname_nid_postfix(receiver, destination.data(), &size) == 0 && size == 16);
|
|
Require(destination[2] || destination[3]);
|
|
int enabled = 1;
|
|
Require(setsockopt_nid_postfix(sender, 0xffff, 0x20, &enabled, sizeof(enabled)) == 0);
|
|
int option = 0;
|
|
std::uint32_t optionSize = sizeof(option);
|
|
Require(getsockopt_nid_postfix(sender, 0xffff, 0x20, &option, &optionSize) == 0 && option != 0);
|
|
Require(setsockopt_nid_postfix(sender, 0xffff, 12345, &enabled, sizeof(enabled)) == -1);
|
|
Require(*__error_nid_postfix() == 42);
|
|
Require(ioctl_nid_postfix(receiver, 0x8004667e, &enabled) == 0);
|
|
Require(fcntl_nid_postfix(receiver, 3) == 6);
|
|
Require(fcntl_nid_postfix(receiver, 4, 2) == 0);
|
|
Require(fcntl_nid_postfix(receiver, 3) == 2);
|
|
Require(fcntl_nid_postfix(receiver, 4, 6) == 0);
|
|
Require(fcntl_nid_postfix(receiver, 3) == 6);
|
|
Require(fcntl_nid_postfix(receiver, 4, 0x8000) == -1);
|
|
Require(fcntl_nid_postfix(receiver, 3) == 6);
|
|
char buffer[64]{};
|
|
Require(recvfrom_nid_postfix(receiver, buffer, sizeof(buffer), 0, nullptr, nullptr) == -1);
|
|
Require(*__error_nid_postfix() == 35);
|
|
const char message[] = "guest UDP loopback";
|
|
Require(sendto_nid_postfix(sender, message, sizeof(message), 0, destination.data(), destination.size()) == sizeof(message));
|
|
int queued = 0;
|
|
for (int i = 0; i < 100 && queued == 0; ++i) {
|
|
Require(ioctl_nid_postfix(receiver, 0x4004667f, &queued) == 0);
|
|
if (!queued) std::this_thread::sleep_for(std::chrono::milliseconds(1));
|
|
}
|
|
Require(queued == sizeof(message));
|
|
Require(recvfrom_nid_postfix(receiver, buffer, sizeof(buffer), 2, nullptr, nullptr) == sizeof(message));
|
|
std::array<unsigned char, 16> source{};
|
|
size = source.size();
|
|
Require(recvfrom_nid_postfix(receiver, buffer, sizeof(buffer), 0, source.data(), &size) == sizeof(message));
|
|
Require(std::strcmp(buffer, message) == 0 && source[1] == 2 && source[4] == 127);
|
|
Require(ioctl_nid_postfix(receiver, 0x4004667f, &queued) == 0 && queued == 0);
|
|
Require(ioctl_nid_postfix(receiver, 123, &queued) == -1 && *__error_nid_postfix() == 25);
|
|
Require(close_nid_postfix(receiver) == 0);
|
|
Require(close_nid_postfix(receiver) == -1 && *__error_nid_postfix() == 9);
|
|
Require(ioctl_nid_postfix(receiver, 0x4004667f, &queued) == -1);
|
|
Require(close_nid_postfix(sender) == 0);
|
|
unsigned char parsed[16]{};
|
|
Require(inet_pton_nid_postfix(2, "127.0.0.1", parsed) == 1);
|
|
Require(parsed[0] == 127 && parsed[1] == 0 && parsed[2] == 0 && parsed[3] == 1);
|
|
char text[64]{};
|
|
Require(inet_ntop_nid_postfix(2, parsed, text, sizeof(text)) == text);
|
|
Require(std::strcmp(text, "127.0.0.1") == 0);
|
|
Require(inet_pton_nid_postfix(28, "::1", parsed) == 1);
|
|
Require(inet_ntop_nid_postfix(28, parsed, text, sizeof(text)) == text);
|
|
Require(std::strcmp(text, "::1") == 0);
|
|
Require(inet_pton_nid_postfix(99, "127.0.0.1", parsed) == -1 && *__error_nid_postfix() == 47);
|
|
Require(inet_pton_nid_postfix(2, "not-an-address", parsed) == 0);
|
|
Require(inet_ntop_nid_postfix(2, parsed, text, 4) == nullptr && *__error_nid_postfix() == 28);
|
|
const int stream = socket_nid_postfix(2, 1, 0);
|
|
Require(stream >= 0);
|
|
Require(close_nid_postfix(stream) == 0);
|
|
}
|