-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathServer.cpp
More file actions
442 lines (391 loc) · 14.8 KB
/
Server.cpp
File metadata and controls
442 lines (391 loc) · 14.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
#include "Server.hpp"
#include "Channel.hpp"
#include "Client.hpp"
#include "CmdFactory.hpp"
#include "Config.hpp"
#include "ICommand.hpp"
#include "LogManager.hpp"
#include "ReplyHandler.hpp"
#include "consts.hpp"
#include "reply_codes.hpp"
#include "signal_handler.hpp"
#include "utils.hpp"
#include <arpa/inet.h> // hton*, ntoh*, inet_addr
#include <csignal>
#include <cstddef>
#include <cstdlib>
#include <cstring>
#include <exception>
#include <fcntl.h>
#include <iostream>
#include <netinet/in.h>
#include <signal.h>
#include <sstream>
#include <string>
#include <sys/poll.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h> // close
/************************************************************
* 🥚 CONSTRUCTORS & DESTRUCTOR *
************************************************************/
Server::Server(const unsigned short port, const std::string& password) :
_psswd(password), _name(ircConfig.get_name()), _port(port)
{
_serverSocket.tcp_bind(port);
_serverSocket.tcp_listen();
LOG_SERVER.info("Server " + _name + " start at port :" + Utils::to_string(port));
std::cout << "\n";
_listen_to_socket(_serverSocket.get_socket(), POLLIN);
}
Server::~Server() { _clean(); }
/*************************************************************
* 👁️ GETTERS and SETTERS *
*************************************************************/
std::string Server::get_password() const { return _psswd; }
std::string Server::get_name() const { return _name; }
int Server::get_port() const { return _port; }
int Server::get_socket_fd() const { return _serverSocket.get_socket(); }
/*************************************************************
* 🛠️ FUNCTIONS *
*************************************************************/
void Server::start()
{
while (globalSignal != SIGINT && globalSignal != SIGABRT) {
int pollResult = poll(_pfds.data(), _pfds.size(), POLL_TIMEOUT); // Timeout 1 second
if (pollResult == -1) {
LOG_W_SERVER("Poll failed", strerror(errno));
break;
}
if (pollResult == 0) {
continue;
}
LOG_DT_SERVER("event detected", pollResult);
for (int i = 0; i < static_cast<int>(_pfds.size()); i++) {
if (globalSignal == SIGINT && globalSignal == SIGABRT)
break;
if (i >= static_cast<int>(_pfds.size()))
break;
if (i == 0 && (_pfds[i].revents & POLLIN)) {
_pfds[i].revents = 0; // Reset events
_handle_new_connection(i);
} else if (i > 0) {
std::map<Socket, Client*>::iterator clientIt = _clients.find(_pfds[i].fd);
if (clientIt == _clients.end()) {
cleanup_socket_and_client(i--);
continue;
}
// Store revents before processing and reset it
short events = _pfds[i].revents;
_pfds[i].revents = 0;
if (events & (POLLHUP | POLLNVAL | POLLERR)) {
_handle_client_disconnection(i--);
} else if (events & POLLIN) {
Socket originalSocket = _pfds[i].fd;
size_t originalSize = _pfds.size();
_handle_client_input(i);
if (_pfds.size() < originalSize || i >= static_cast<int>(_pfds.size()) || _pfds[i].fd != originalSocket) {
i--; // Client was removed, adjust index
}
} else if (events & POLLOUT) {
_handle_client_output(i);
}
}
}
}
_clean();
}
void Server::update_bot_state(
Socket socketfd, Channel* targetChannel, const std::string& subCommand, const std::string& botReply, bool readyToSend)
{
BotState state;
state.socketfd = socketfd;
state.targetChannel = targetChannel;
state.subCommand = subCommand;
state.pendingMsg = botReply;
state.readyToSend = readyToSend;
_bots[socketfd] = state;
}
void Server::_handle_new_connection(int pfdIndex)
{
std::string pollEvent;
if (_pfds[pfdIndex].revents & POLLIN)
pollEvent.append("POLLIN ");
if (_pfds[pfdIndex].revents & POLLOUT)
pollEvent.append("POLLOUT ");
if (_pfds[pfdIndex].revents & POLLHUP)
pollEvent.append("POLLHUP ");
if (_pfds[pfdIndex].revents & POLLERR)
pollEvent.append("POLLERR ");
LOG_SOCKET.debug("Socket " + Utils::to_string(_pfds[pfdIndex].fd) + " events: " + pollEvent);
sockaddr_in clientAddr = {};
memset(&clientAddr, 0, sizeof(clientAddr));
socklen_t addrLen = sizeof(clientAddr);
Socket socket = accept(_serverSocket.get_socket(), reinterpret_cast<sockaddr*>(&clientAddr), &addrLen);
if (socket != -1) {
if (fcntl(socket, F_SETFL, O_NONBLOCK) == -1) {
LOG_ERR.error(std::string("Error while setting a non blocking client socket") + strerror(errno));
LOG_SOCKET.error(std::string("Error while setting a non blocking client socket") + strerror(errno));
close(socket);
} else {
Client* newClient = new Client(socket, clientAddr); // NOLINT
LOG_CONN.info(std::string("New connection accepted on socket ") + Utils::to_string(socket) + " => "
+ Utils::to_string(*newClient));
_clients[socket] = newClient;
_listen_to_socket(socket, POLLIN);
}
}
}
void Server::_handle_client_disconnection(int pfdIndex)
{
Socket socket = _pfds[pfdIndex].fd;
Client* client = _clients[socket];
if (!client) {
LOG_SERVER.error("client not found");
return;
}
socklen_t err = 0;
socklen_t errsize = sizeof(err);
if (getsockopt(socket, SOL_SOCKET, SO_ERROR, reinterpret_cast<char*>(&err), &errsize) == 0) {
if (err == 0) {
LOG_SERVER.debug("connection has been closed by client");
} else {
LOG_CONN.warning(std::string("socket error : ") + strerror(static_cast<int>(err)));
LOG_SOCKET.warning(std::string("socket error : ") + strerror(static_cast<int>(err)));
}
}
LOG_CONN.info(std::string("Client at ") + client->get_address() + ":" + Utils::to_string(client->get_port())
+ " disconnected");
cleanup_socket_and_client(pfdIndex);
}
void Server::cleanup_bot(Socket so) { _bots.erase(so); }
void Server::_handle_bot_input(int pfdIndex, Client* botClient, BotState& state)
{
LOG_D_SERVER("state", state.pendingMsg);
char buffer[CLIENT_READ_BUFFER_SIZE];
memset(static_cast<char*>(buffer), 0, CLIENT_READ_BUFFER_SIZE);
ssize_t bytesRead = recv(botClient->get_socket(), static_cast<char*>(buffer), CLIENT_READ_BUFFER_SIZE - 1, 0);
Socket so = botClient->get_socket();
if (bytesRead == 0) {
LOG_CONN.warning("Bot connection closed properly");
cleanup_bot(so);
cleanup_socket_and_client(pfdIndex);
return;
} else if (bytesRead == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
LOG_ERR.error("Reception failed:" + TO_STRING(strerror(errno)));
cleanup_bot(so);
cleanup_socket_and_client(pfdIndex);
}
return;
} else {
if (Utils::safe_at(buffer, bytesRead))
Utils::safe_at(buffer, bytesRead) = '\0';
LOG_D_SERVER("bot received", buffer);
botClient->append_to_read_buffer(std::string(static_cast<char*>(buffer)));
std::string response(botClient->get_read_buffer());
ReplyHandler rh = ReplyHandler::get_instance(this);
if (response.find("JOIN") != std::string::npos) {
LOG_DV_SERVER(response);
_handle_commands(pfdIndex);
state.targetChannel->broadcast_bot(
*this, TRANSFER_PRIVMSG, state.targetChannel->get_name(), botClient, state.pendingMsg);
rh.process_response(*botClient, TRANSFER_PRIVMSG, state.targetChannel->get_name());
state.readyToSend = true;
return;
}
if (response.find("PRIVMSG") != std::string::npos && state.readyToSend == true) {
LOG_DV_SERVER(state.pendingMsg);
_handle_commands(pfdIndex);
cleanup_bot(so);
cleanup_socket_and_client(pfdIndex);
}
}
}
void Server::_handle_client_input(int pfdIndex)
{
LOG_dt_SERVER("");
Socket socket = _pfds[pfdIndex].fd;
Client* client = _clients[socket];
if (!client) {
return;
}
if (_bots.count(socket)) {
_handle_bot_input(pfdIndex, client, _bots[socket]);
return;
}
char buffer[CLIENT_READ_BUFFER_SIZE];
memset(static_cast<char*>(buffer), 0, CLIENT_READ_BUFFER_SIZE);
ssize_t bytesRead = recv(socket, static_cast<char*>(buffer), CLIENT_READ_BUFFER_SIZE - 1, 0);
if (bytesRead == 0) {
LOG_CONN.warning("Connection closed properly");
_handle_client_disconnection(pfdIndex);
return;
} else if (bytesRead == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
LOG_ERR.error("Reception failed:" + TO_STRING(strerror(errno)));
_handle_client_disconnection(pfdIndex);
}
return;
} else {
if (Utils::safe_at(buffer, bytesRead))
Utils::safe_at(buffer, bytesRead) = '\0';
client->append_to_read_buffer(std::string(static_cast<char*>(buffer)));
bool clientDisconnected = this->_handle_commands(pfdIndex);
if (clientDisconnected) {
return;
}
}
}
void Server::_handle_client_output(int pfdIndex)
{
Socket socket = _pfds[pfdIndex].fd;
Client* client = _clients[socket];
if (!client) {
LOG_SERVER.error("client not found");
return;
}
std::string sendBuffer = client->get_send_buffer();
if (!sendBuffer.empty()) {
ssize_t bytesSent = send(socket, sendBuffer.c_str(), sendBuffer.length(), 0);
if (bytesSent == -1) {
if (errno != EAGAIN && errno != EWOULDBLOCK) {
LOG_SERVER.warning(std::string("Socket is not ready for sending ... trying later"));
} else {
LOG_SERVER.error(std::string("Error sending to client"));
_handle_client_disconnection(pfdIndex);
}
} else if (static_cast<size_t>(bytesSent) < sendBuffer.length()) {
LOG_SERVER.warning(std::string("Queue has been partially sent (") + Utils::to_string(bytesSent) + "/"
+ Utils::to_string(sendBuffer.length()) + ")");
client->set_send_buffer(sendBuffer.substr(bytesSent));
} else {
client->get_send_buffer().clear();
LOG_d_SERVER("Message sent, unsuscribe from POLLOUT");
_pfds[pfdIndex].events &= ~POLLOUT;
}
} else {
_pfds[pfdIndex].events &= ~POLLOUT;
}
}
bool Server::_handle_commands(int pfdIndex)
{
Socket socket = _pfds[pfdIndex].fd;
Client* client = _clients[socket];
size_t pos = std::string::npos;
std::string cmdName;
while ((pos = client->get_read_buffer().find("\r\n")) != std::string::npos) {
std::string line = client->get_read_buffer().substr(0, pos);
client->get_read_buffer().erase(0, pos + 2);
if (line.empty()) {
continue;
}
ICommand* cmd = _parse_command(*client, line);
if (cmd) {
cmd->execute(*this, *client);
delete cmd;
std::istringstream iss(line);
iss >> cmdName;
if (cmdName == "QUIT")
return true;
}
}
return false;
}
ICommand* Server::_parse_command(Client& client, std::string line)
{
LOG_CMD.receiving(__FILE_NAME__, __FUNCTION__, line, &client);
CmdFactory commandBuilder;
ICommand* cmd = commandBuilder.make_command(*this, client, line);
return cmd;
}
Client* Server::find_client_by_nickname(const std::string& nickname)
{
for (std::map<Socket, Client*>::iterator it = _clients.begin(); it != _clients.end(); it++) {
if (nickname == it->second->get_nickname())
return it->second;
}
return NULL;
}
int Server::index_of(Client& client)
{
Socket socket = client.get_socket();
if (socket == -1)
return -1;
for (size_t i = 0; i < _pfds.size(); ++i) {
if (_pfds[i].fd == socket)
return static_cast<int>(i);
}
return -1;
}
void Server::add_events_of(Client& client, int event)
{
int index = index_of(client);
LOG_dt_SERVER("Server::add_envents_of --> " + EVENT_TO_STR(event) + " on index: " + TO_STRING(index));
if (index >= 0) {
_pfds[index].events = static_cast<short>(_pfds[index].events | event); // ADD to existing events
}
}
void Server::_listen_to_socket(Socket toListen, uint32_t flags)
{
pollfd newPollFd = {.fd = toListen, .events = static_cast<short>(flags), .revents = 0};
_pfds.push_back(newPollFd);
}
void Server::_clean()
{
LOG_SERVER.debug(std::string("cleaning ") + TO_STRING(_clients.size()) + " clients");
for (int i = static_cast<int>(_pfds.size()) - 1; i >= 1; --i) {
cleanup_socket_and_client(i);
}
_bots.erase(_bots.begin(), _bots.end());
_bots.clear();
cleanup_channels();
globalSignal = 0;
LOG_SERVER.debug("Server cleaned and ready for reuse");
}
void Server::stop()
{
LOG_SERVER.debug("Server stop requested");
globalSignal = SIGINT;
}
void Server::cleanup_socket_and_client(int pfdIndex)
{
Client* c = _clients[_pfds[pfdIndex].fd];
close(_pfds[pfdIndex].fd);
_clients.erase(_pfds[pfdIndex].fd);
if (c) {
if (!c->get_nickname().empty())
_clientsByNick.erase(c->get_nickname());
LOG_SERVER.debug("cleanup: deleting client");
c->remove_from_all_channels();
delete c;
}
_pfds.erase(_pfds.begin() + pfdIndex);
}
void Server::cleanup_channels()
{
LOG_dt_SERVER(std::string("cleaning ") + TO_STRING(channels.size()) + " channels");
for (std::map<std::string, Channel*>::iterator it = channels.begin(); it != channels.end(); ++it) {
delete it->second; // NOLINT
}
channels.clear();
}
std::vector<Client*> Server::find_clients_by_pattern(const std::string& pattern) const
{
std::vector<Client*> result;
for (std::map<Socket, Client*>::const_iterator it = _clients.begin(); it != _clients.end(); it++) {
if (Utils::MatchPattern(pattern)(it->second)) {
LOG_D_CMD("pattern " + pattern + " matched", it->second->get_nickname());
result.push_back(it->second);
}
}
return result;
}
Channel* Server::find_channel_by_name(const std::string& name)
{
std::map<std::string, Channel*>::iterator chan = channels.find(name);
if (chan != channels.end()) {
return chan->second;
}
return NULL;
}