this->is extremely dangerous to our democracy

This commit is contained in:
Theis Pieter Hollebeek 2024-11-20 10:31:03 +01:00
parent 99087acd13
commit b2563125e7
2 changed files with 12 additions and 9 deletions

View File

@ -5,7 +5,7 @@
auto slige_rpc::ClientSocket::Read( auto slige_rpc::ClientSocket::Read(
uint8_t* buffer, size_t length) -> std::variant<size_t, slige_rpc::Ewwow> uint8_t* buffer, size_t length) -> std::variant<size_t, slige_rpc::Ewwow>
{ {
ssize_t bytes_read = recv(fd, buffer, length, 0); ssize_t bytes_read = recv(this->fd, buffer, length, 0);
if (bytes_read < 0) { if (bytes_read < 0) {
return Ewwow { .message = "unable to read" }; return Ewwow { .message = "unable to read" };
} }
@ -15,7 +15,7 @@ auto slige_rpc::ClientSocket::Read(
auto slige_rpc::ClientSocket::Write( auto slige_rpc::ClientSocket::Write(
uint8_t* buffer, size_t length) -> std::variant<size_t, slige_rpc::Ewwow> uint8_t* buffer, size_t length) -> std::variant<size_t, slige_rpc::Ewwow>
{ {
ssize_t bytes_written = send(fd, buffer, length, 0); ssize_t bytes_written = send(this->fd, buffer, length, 0);
if (bytes_written < 0) { if (bytes_written < 0) {
return Ewwow { .message = "unable to write" }; return Ewwow { .message = "unable to write" };
} }
@ -34,7 +34,9 @@ auto slige_rpc::Socket::Connect()
// live as long as child socket ('client)? // live as long as child socket ('client)?
// - immediate answer is no // - immediate answer is no
// 2) does it need to? // 2) does it need to?
if (connect(socket_fd, (struct sockaddr*)&address, sizeof(address)) < 0) { if (connect(
socket_fd, (struct sockaddr*)&this->address, sizeof(this->address))
< 0) {
return { Ewwow { return { Ewwow {
.message = "could not connect, is the server running?" } }; .message = "could not connect, is the server running?" } };
} }
@ -44,17 +46,17 @@ auto slige_rpc::Socket::Connect()
auto slige_rpc::ServerSocket::Accept() auto slige_rpc::ServerSocket::Accept()
-> std::variant<ClientSocket, slige_rpc::Ewwow> -> std::variant<ClientSocket, slige_rpc::Ewwow>
{ {
unsigned int address_size = sizeof(address); unsigned int address_size = sizeof(this->address);
// TODO: // TODO:
// 1) does address (presumably 'server) // 1) does address (presumably 'server)
// live as long as child socket ('socket)? // live as long as child socket ('socket)?
// - immediate answer is no // - immediate answer is no
// 2) does it need to? // 2) does it need to?
int client = accept(fd, (struct sockaddr*)&address, &address_size); int client
= accept(this->fd, (struct sockaddr*)&this->address, &address_size);
if (client < 0) { if (client < 0) {
return Ewwow { .message = "could not accept" }; return Ewwow { .message = "could not accept" };
} }
return client;
return { ClientSocket(client) }; return { ClientSocket(client) };
} }
@ -69,7 +71,8 @@ auto slige_rpc::Socket::Bind() -> std::variant<ServerSocket, slige_rpc::Ewwow>
// live as long as child socket ('server)? // live as long as child socket ('server)?
// - immediate answer is no // - immediate answer is no
// 2) does it need to? // 2) does it need to?
if (bind(socket_fd, (struct sockaddr*)&address, sizeof(address)) < 0) { if (bind(socket_fd, (struct sockaddr*)&this->address, sizeof(this->address))
< 0) {
return { Ewwow { .message = "could not bind" } }; return { Ewwow { .message = "could not bind" } };
} }
@ -80,5 +83,5 @@ auto slige_rpc::Socket::Bind() -> std::variant<ServerSocket, slige_rpc::Ewwow>
// 1) does this address get moved, copied or cloned? // 1) does this address get moved, copied or cloned?
// it should be represented as a u32, // it should be represented as a u32,
// so copying should be fine - but does it? // so copying should be fine - but does it?
return { ServerSocket(socket_fd, address) }; return { ServerSocket(socket_fd, this->address) };
} }

View File

@ -56,7 +56,7 @@ class Socket {
public: public:
Socket(uint16_t port) Socket(uint16_t port)
{ {
address = { this->address = {
.sin_family = AF_INET, .sin_family = AF_INET,
.sin_port = htons(port), .sin_port = htons(port),
.sin_addr = { .s_addr = inet_addr("127.0.0.1") }, .sin_addr = { .s_addr = inet_addr("127.0.0.1") },