lindows/lrss/lib/library.cxx
2024-03-05 17:46:15 -05:00

64 lines
1.5 KiB
C++

#include <cstring>
#include <cstdio>
#include <sys/socket.h>
#include <sys/un.h>
#include <unistd.h>
#include <lindows/lrss.h>
static const char SOCKET_ADDRESS[108] = {
0, 'L', 'R', 'S', 'S', 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
static int REGISTRY_SOCKET = -1;
NTSTATUS LrcSocketAddress(char Buffer[108]) {
std::memcpy(Buffer, SOCKET_ADDRESS, 108);
return STATUS_SUCCESS;
}
NTSTATUS LrcOpenRegistry() {
// This may not work if the thing got closed somehow :>
if (REGISTRY_SOCKET == -1) {
REGISTRY_SOCKET = socket(AF_UNIX, SOCK_SEQPACKET, 0);
if (REGISTRY_SOCKET < 0) {
REGISTRY_SOCKET = -1;
return STATUS_INTERNAL_ERROR;
}
int tmp = 1;
if (setsockopt(REGISTRY_SOCKET, SOL_SOCKET, SO_PASSCRED, &tmp, sizeof(tmp)) < 0) {
close(REGISTRY_SOCKET);
REGISTRY_SOCKET = -1;
return STATUS_INTERNAL_ERROR;
}
struct sockaddr_un sockaddr {
.sun_family = AF_UNIX,
.sun_path = {}
};
LrcSocketAddress(sockaddr.sun_path);
if (connect(REGISTRY_SOCKET, reinterpret_cast<struct sockaddr*>(&sockaddr), sizeof(sockaddr)) < 0) {
close(REGISTRY_SOCKET);
REGISTRY_SOCKET = -1;
return STATUS_INTERNAL_ERROR;
}
return STATUS_SUCCESS;
} else return STATUS_SUCCESS;
}