Skip to content

Commit

Permalink
refactor: removed ip blacklist exception
Browse files Browse the repository at this point in the history
  • Loading branch information
araujo88 committed Mar 29, 2024
1 parent 937c076 commit 363fd50
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 33 deletions.
9 changes: 0 additions & 9 deletions include/base_exceptions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,6 @@ namespace tpt
: BaseException(message, errorCode) {}
};

class IPBlackListedException : public BaseException
{
public:
IPBlackListedException(
const std::string &message = "IP is blacklisted",
int errorCode = -1)
: BaseException(message, errorCode) {}
};

class SocketCreationException : public BaseException
{
public:
Expand Down
2 changes: 1 addition & 1 deletion include/socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace tpt
public:
virtual void bindSocket() = 0;
virtual void listenToConnections() = 0;
virtual void acceptConnection(SOCKET &client_socket, void *client_address) = 0;
virtual bool acceptConnection(SOCKET &client_socket, void *client_address) = 0;
virtual ssize_t receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size) = 0;
virtual void sendData(SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags) = 0;
virtual void closeSocket() = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/unix_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace tpt
std::string getClientIp();
virtual void bindSocket() override;
virtual void listenToConnections() override;
virtual void acceptConnection(SOCKET &client_socket, void *client_address) override;
virtual bool acceptConnection(SOCKET &client_socket, void *client_address) override;
virtual ssize_t receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size) override;
virtual void sendData(SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags) override;
virtual void closeSocket() override;
Expand Down
2 changes: 1 addition & 1 deletion include/win_socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ namespace tpt
std::string getClientIp();
virtual void bindSocket() override;
virtual void listenToConnections() override;
virtual void acceptConnection(SOCKET &client_socket, void *client_address) override;
virtual bool acceptConnection(SOCKET &client_socket, void *client_address) override;
virtual ssize_t receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size) override;
virtual void sendData(SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags) override;
virtual void closeSocket() override;
Expand Down
18 changes: 9 additions & 9 deletions src/teapot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ Teapot::Teapot(std::string ip_address, unsigned int port, unsigned int max_conne
this->cors_middleware = CORSMiddleware();
this->security_middleware = SecurityMiddleware();
this->logger = ConsoleLogger();
// Conditional compilation based on the operating system

#ifdef __linux__
this->socket = tpt::UnixSocket(this->logger, this->ip_address, this->port, this->max_connections);
#endif
Expand Down Expand Up @@ -229,14 +229,14 @@ void Teapot::run()

try
{
socket.acceptConnection(client_socket, client_addr);
auto res = std::async(std::launch::async, &Teapot::requestHandler, this, client_socket);
// std::jthread th(&Teapot::requestHandler, this, client_socket);
}
catch (IPBlackListedException &e)
{
std::cout << e.what();
this->socket.closeSocket(client_socket);
if (socket.acceptConnection(client_socket, client_addr))
{
auto res = std::async(std::launch::async, &Teapot::requestHandler, this, client_socket);
}
else
{
this->socket.closeSocket(client_socket);
}
}
catch (SocketAcceptException &)
{
Expand Down
11 changes: 5 additions & 6 deletions src/unix_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ void UnixSocket::listenToConnections()
}
}

void UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
bool UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
{
struct sockaddr_storage client_addr_storage;
socklen_t client_addr_size = sizeof(client_addr_storage);
Expand All @@ -64,22 +64,19 @@ void UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
throw SocketAcceptException();
}

// Assuming client_address is meant to store the result
if (client_address != nullptr)
{
std::memcpy(client_address, &client_addr_storage, client_addr_size);
}

char ip_str[INET6_ADDRSTRLEN] = {0}; // Large enough for both IPv4 and IPv6
char ip_str[INET6_ADDRSTRLEN] = {0};
if (client_addr_storage.ss_family == AF_INET)
{
// IPv4
struct sockaddr_in *addr_in = (struct sockaddr_in *)&client_addr_storage;
inet_ntop(AF_INET, &addr_in->sin_addr, ip_str, INET_ADDRSTRLEN);
}
else if (client_addr_storage.ss_family == AF_INET6)
{
// IPv6
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&client_addr_storage;
inet_ntop(AF_INET6, &addr_in6->sin6_addr, ip_str, INET6_ADDRSTRLEN);
}
Expand All @@ -90,11 +87,13 @@ void UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
{
if (this->client_ip == it)
{
throw IPBlackListedException();
return false;
}
}

this->client_sockets.push_back(client_socket);

return true;
}

ssize_t UnixSocket::receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size)
Expand Down
13 changes: 7 additions & 6 deletions src/win_socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ void WinSocket::listenToConnections()
}
}

void WinSocket::acceptConnection(SOCKET &client_socket, void *client_address)
bool WinSocket::acceptConnection(SOCKET &client_socket, void *client_address)
{
struct sockaddr_storage client_addr_storage;
int client_addr_size = sizeof(client_addr_storage);
Expand All @@ -68,22 +68,19 @@ void WinSocket::acceptConnection(SOCKET &client_socket, void *client_address)
throw SocketAcceptException("error accepting connections", WSAGetLastError());
}

// Assuming client_address is meant to store the result
if (client_address != nullptr)
{
std::memcpy(client_address, &client_addr_storage, client_addr_size);
}

char ip_str[INET6_ADDRSTRLEN] = {0}; // Large enough for both IPv4 and IPv6
char ip_str[INET6_ADDRSTRLEN] = {0};
if (client_addr_storage.ss_family == AF_INET)
{
// IPv4
struct sockaddr_in *addr_in = (struct sockaddr_in *)&client_addr_storage;
inet_ntop(AF_INET, &addr_in->sin_addr, ip_str, INET_ADDRSTRLEN);
}
else if (client_addr_storage.ss_family == AF_INET6)
{
// IPv6
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&client_addr_storage;
inet_ntop(AF_INET6, &addr_in6->sin6_addr, ip_str, INET6_ADDRSTRLEN);
}
Expand All @@ -94,9 +91,13 @@ void WinSocket::acceptConnection(SOCKET &client_socket, void *client_address)
{
if (this->client_ip == it)
{
throw IPBlackListedException();
return false;
}
}

this->client_sockets.push_back(client_socket);

return true;
}

ssize_t WinSocket::receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size)
Expand Down

0 comments on commit 363fd50

Please sign in to comment.