@@ -8,6 +8,7 @@ WinSocket::WinSocket()
8
8
this ->ip_address = " 127.0.0.1" ;
9
9
this ->port = 8000 ;
10
10
this ->max_connections = 10 ;
11
+ this ->logger = ConsoleLogger ();
11
12
12
13
if (WSAStartup (MAKEWORD (2 , 2 ), &wsa) != 0 )
13
14
{
@@ -28,13 +29,46 @@ WinSocket::WinSocket()
28
29
this ->server_address .sin_family = AF_INET;
29
30
this ->server_address .sin_port = htons (this ->port );
30
31
this ->server_address .sin_addr .s_addr = inet_addr (this ->ip_address .c_str ());
32
+
33
+ Utils::fillIPBlacklist (this ->ip_blacklist );
34
+ }
35
+
36
+ WinSocket::WinSocket (ConsoleLogger logger)
37
+ {
38
+ this ->ip_address = " 127.0.0.1" ;
39
+ this ->port = 8000 ;
40
+ this ->max_connections = 10 ;
41
+ this ->logger = logger;
42
+
43
+ if (WSAStartup (MAKEWORD (2 , 2 ), &wsa) != 0 )
44
+ {
45
+ std::printf (" Failed. Error Code : %d" , WSAGetLastError ());
46
+ WSACleanup ();
47
+ exit (EXIT_FAILURE);
48
+ }
49
+
50
+ std::cout << " Creating socket ..." << std::endl;
51
+ this ->server_socket = socket (AF_INET, SOCK_STREAM, 0 );
52
+ if (this ->server_socket == INVALID_SOCKET)
53
+ {
54
+ std::printf (" Could not create socket: %d\n " , WSAGetLastError ());
55
+ WSACleanup ();
56
+ exit (EXIT_FAILURE);
57
+ }
58
+
59
+ this ->server_address .sin_family = AF_INET;
60
+ this ->server_address .sin_port = htons (this ->port );
61
+ this ->server_address .sin_addr .s_addr = inet_addr (this ->ip_address .c_str ());
62
+
63
+ Utils::fillIPBlacklist (this ->ip_blacklist );
31
64
}
32
65
33
- WinSocket::WinSocket (unsigned int port)
66
+ WinSocket::WinSocket (ConsoleLogger logger, unsigned int port)
34
67
{
35
68
this ->ip_address = " 127.0.0.1" ;
36
69
this ->port = port;
37
70
this ->max_connections = 10 ;
71
+ this ->logger = logger;
38
72
39
73
if (WSAStartup (MAKEWORD (2 , 2 ), &wsa) != 0 )
40
74
{
@@ -55,13 +89,16 @@ WinSocket::WinSocket(unsigned int port)
55
89
this ->server_address .sin_family = AF_INET;
56
90
this ->server_address .sin_port = htons (this ->port );
57
91
this ->server_address .sin_addr .s_addr = inet_addr (this ->ip_address .c_str ());
92
+
93
+ Utils::fillIPBlacklist (this ->ip_blacklist );
58
94
}
59
95
60
- WinSocket::WinSocket (std::string ip_address, unsigned int port)
96
+ WinSocket::WinSocket (ConsoleLogger logger, std::string ip_address, unsigned int port)
61
97
{
62
98
this ->ip_address = ip_address;
63
99
this ->port = port;
64
100
this ->max_connections = 10 ;
101
+ this ->logger = logger;
65
102
66
103
if (WSAStartup (MAKEWORD (2 , 2 ), &wsa) != 0 )
67
104
{
@@ -82,13 +119,16 @@ WinSocket::WinSocket(std::string ip_address, unsigned int port)
82
119
this ->server_address .sin_family = AF_INET;
83
120
this ->server_address .sin_port = htons (this ->port );
84
121
this ->server_address .sin_addr .s_addr = inet_addr (this ->ip_address .c_str ());
122
+
123
+ Utils::fillIPBlacklist (this ->ip_blacklist );
85
124
}
86
125
87
- WinSocket::WinSocket (std::string ip_address, unsigned int port, unsigned int max_connections)
126
+ WinSocket::WinSocket (ConsoleLogger logger, std::string ip_address, unsigned int port, unsigned int max_connections)
88
127
{
89
128
this ->ip_address = ip_address;
90
129
this ->port = port;
91
130
this ->max_connections = 10 ;
131
+ this ->logger = logger;
92
132
93
133
if (WSAStartup (MAKEWORD (2 , 2 ), &wsa) != 0 )
94
134
{
@@ -109,6 +149,8 @@ WinSocket::WinSocket(std::string ip_address, unsigned int port, unsigned int max
109
149
this ->server_address .sin_family = AF_INET;
110
150
this ->server_address .sin_port = htons (this ->port );
111
151
this ->server_address .sin_addr .s_addr = inet_addr (this ->ip_address .c_str ());
152
+
153
+ Utils::fillIPBlacklist (this ->ip_blacklist );
112
154
}
113
155
114
156
void WinSocket::bindSocket ()
@@ -138,29 +180,60 @@ void WinSocket::listenToConnections()
138
180
void WinSocket::acceptConnection (SOCKET &client_socket, void *client_address)
139
181
{
140
182
int client_addr_size = sizeof (sockaddr_in);
141
- client_socket = accept (this ->server_socket , static_cast <sockaddr*>(client_address), &client_addr_size);
142
- if (client_socket == INVALID_SOCKET) {
183
+ client_socket = accept (this ->server_socket , static_cast <sockaddr *>(client_address), &client_addr_size);
184
+ if (client_socket == INVALID_SOCKET)
185
+ {
143
186
std::printf (" Error accepting connections: %d\n " , WSAGetLastError ());
144
187
WSACleanup ();
145
188
exit (EXIT_FAILURE);
146
189
}
190
+
191
+ // Assuming client_address is meant to store the result
192
+ if (client_address != nullptr )
193
+ {
194
+ std::memcpy (client_address, &client_addr_storage, client_addr_size);
195
+ }
196
+
197
+ char ip_str[INET6_ADDRSTRLEN] = {0 }; // Large enough for both IPv4 and IPv6
198
+ if (client_addr_storage.ss_family == AF_INET)
199
+ {
200
+ // IPv4
201
+ struct sockaddr_in *addr_in = (struct sockaddr_in *)&client_addr_storage;
202
+ inet_ntop (AF_INET, &addr_in->sin_addr , ip_str, INET_ADDRSTRLEN);
203
+ }
204
+ else if (client_addr_storage.ss_family == AF_INET6)
205
+ {
206
+ // IPv6
207
+ struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)&client_addr_storage;
208
+ inet_ntop (AF_INET6, &addr_in6->sin6_addr , ip_str, INET6_ADDRSTRLEN);
209
+ }
210
+
211
+ this ->client_ip = std::string (ip_str);
212
+
213
+ for (auto it : this ->ip_blacklist )
214
+ {
215
+ if (this ->client_ip == it)
216
+ {
217
+ throw IPBlackListedException ();
218
+ }
219
+ }
147
220
}
148
221
149
222
ssize_t WinSocket::receiveData (SOCKET client_socket, char *buffer, unsigned int buffer_size)
150
223
{
151
224
ssize_t data = recv (client_socket, buffer, buffer_size, 0 );
152
225
if (data < 0 )
153
226
{
154
- perror (" Receive error" );
155
- std::cout << " Error code: " + errno << std::endl ;
156
- exit (1 );
227
+ std::printf (" Receive error\n " );
228
+ WSACleanup () ;
229
+ exit (EXIT_FAILURE );
157
230
}
158
231
return data;
159
232
}
160
233
161
234
void WinSocket::sendData (SOCKET client_socket, const void *buffer, unsigned int buffer_size, int flags)
162
235
{
163
- send (client_socket, (char *)buffer, buffer_size, flags);
236
+ send (client_socket, (char *)buffer, buffer_size, flags);
164
237
}
165
238
166
239
void WinSocket::closeSocket ()
0 commit comments