Skip to content

Commit 363fd50

Browse files
committed
refactor: removed ip blacklist exception
1 parent 937c076 commit 363fd50

7 files changed

+24
-33
lines changed

include/base_exceptions.hpp

-9
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,6 @@ namespace tpt
5454
: BaseException(message, errorCode) {}
5555
};
5656

57-
class IPBlackListedException : public BaseException
58-
{
59-
public:
60-
IPBlackListedException(
61-
const std::string &message = "IP is blacklisted",
62-
int errorCode = -1)
63-
: BaseException(message, errorCode) {}
64-
};
65-
6657
class SocketCreationException : public BaseException
6758
{
6859
public:

include/socket.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace tpt
1515
public:
1616
virtual void bindSocket() = 0;
1717
virtual void listenToConnections() = 0;
18-
virtual void acceptConnection(SOCKET &client_socket, void *client_address) = 0;
18+
virtual bool acceptConnection(SOCKET &client_socket, void *client_address) = 0;
1919
virtual ssize_t receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size) = 0;
2020
virtual void sendData(SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags) = 0;
2121
virtual void closeSocket() = 0;

include/unix_socket.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ namespace tpt
4242
std::string getClientIp();
4343
virtual void bindSocket() override;
4444
virtual void listenToConnections() override;
45-
virtual void acceptConnection(SOCKET &client_socket, void *client_address) override;
45+
virtual bool acceptConnection(SOCKET &client_socket, void *client_address) override;
4646
virtual ssize_t receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size) override;
4747
virtual void sendData(SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags) override;
4848
virtual void closeSocket() override;

include/win_socket.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ namespace tpt
4646
std::string getClientIp();
4747
virtual void bindSocket() override;
4848
virtual void listenToConnections() override;
49-
virtual void acceptConnection(SOCKET &client_socket, void *client_address) override;
49+
virtual bool acceptConnection(SOCKET &client_socket, void *client_address) override;
5050
virtual ssize_t receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size) override;
5151
virtual void sendData(SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags) override;
5252
virtual void closeSocket() override;

src/teapot.cpp

+9-9
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,7 @@ Teapot::Teapot(std::string ip_address, unsigned int port, unsigned int max_conne
195195
this->cors_middleware = CORSMiddleware();
196196
this->security_middleware = SecurityMiddleware();
197197
this->logger = ConsoleLogger();
198-
// Conditional compilation based on the operating system
198+
199199
#ifdef __linux__
200200
this->socket = tpt::UnixSocket(this->logger, this->ip_address, this->port, this->max_connections);
201201
#endif
@@ -229,14 +229,14 @@ void Teapot::run()
229229

230230
try
231231
{
232-
socket.acceptConnection(client_socket, client_addr);
233-
auto res = std::async(std::launch::async, &Teapot::requestHandler, this, client_socket);
234-
// std::jthread th(&Teapot::requestHandler, this, client_socket);
235-
}
236-
catch (IPBlackListedException &e)
237-
{
238-
std::cout << e.what();
239-
this->socket.closeSocket(client_socket);
232+
if (socket.acceptConnection(client_socket, client_addr))
233+
{
234+
auto res = std::async(std::launch::async, &Teapot::requestHandler, this, client_socket);
235+
}
236+
else
237+
{
238+
this->socket.closeSocket(client_socket);
239+
}
240240
}
241241
catch (SocketAcceptException &)
242242
{

src/unix_socket.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void UnixSocket::listenToConnections()
5252
}
5353
}
5454

55-
void UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
55+
bool UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
5656
{
5757
struct sockaddr_storage client_addr_storage;
5858
socklen_t client_addr_size = sizeof(client_addr_storage);
@@ -64,22 +64,19 @@ void UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
6464
throw SocketAcceptException();
6565
}
6666

67-
// Assuming client_address is meant to store the result
6867
if (client_address != nullptr)
6968
{
7069
std::memcpy(client_address, &client_addr_storage, client_addr_size);
7170
}
7271

73-
char ip_str[INET6_ADDRSTRLEN] = {0}; // Large enough for both IPv4 and IPv6
72+
char ip_str[INET6_ADDRSTRLEN] = {0};
7473
if (client_addr_storage.ss_family == AF_INET)
7574
{
76-
// IPv4
7775
struct sockaddr_in *addr_in = (struct sockaddr_in *)&client_addr_storage;
7876
inet_ntop(AF_INET, &addr_in->sin_addr, ip_str, INET_ADDRSTRLEN);
7977
}
8078
else if (client_addr_storage.ss_family == AF_INET6)
8179
{
82-
// IPv6
8380
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&client_addr_storage;
8481
inet_ntop(AF_INET6, &addr_in6->sin6_addr, ip_str, INET6_ADDRSTRLEN);
8582
}
@@ -90,11 +87,13 @@ void UnixSocket::acceptConnection(SOCKET &client_socket, void *client_address)
9087
{
9188
if (this->client_ip == it)
9289
{
93-
throw IPBlackListedException();
90+
return false;
9491
}
9592
}
9693

9794
this->client_sockets.push_back(client_socket);
95+
96+
return true;
9897
}
9998

10099
ssize_t UnixSocket::receiveData(SOCKET client_socket, char *buffer, unsigned int buffer_size)

src/win_socket.cpp

+7-6
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void WinSocket::listenToConnections()
5656
}
5757
}
5858

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

71-
// Assuming client_address is meant to store the result
7271
if (client_address != nullptr)
7372
{
7473
std::memcpy(client_address, &client_addr_storage, client_addr_size);
7574
}
7675

77-
char ip_str[INET6_ADDRSTRLEN] = {0}; // Large enough for both IPv4 and IPv6
76+
char ip_str[INET6_ADDRSTRLEN] = {0};
7877
if (client_addr_storage.ss_family == AF_INET)
7978
{
80-
// IPv4
8179
struct sockaddr_in *addr_in = (struct sockaddr_in *)&client_addr_storage;
8280
inet_ntop(AF_INET, &addr_in->sin_addr, ip_str, INET_ADDRSTRLEN);
8381
}
8482
else if (client_addr_storage.ss_family == AF_INET6)
8583
{
86-
// IPv6
8784
struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&client_addr_storage;
8885
inet_ntop(AF_INET6, &addr_in6->sin6_addr, ip_str, INET6_ADDRSTRLEN);
8986
}
@@ -94,9 +91,13 @@ void WinSocket::acceptConnection(SOCKET &client_socket, void *client_address)
9491
{
9592
if (this->client_ip == it)
9693
{
97-
throw IPBlackListedException();
94+
return false;
9895
}
9996
}
97+
98+
this->client_sockets.push_back(client_socket);
99+
100+
return true;
100101
}
101102

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

0 commit comments

Comments
 (0)