Repository navigation
Expand file tree
/
Copy pathServer.cpp
More file actions
638 lines (545 loc) · 16.8 KB
/
Copy pathServer.cpp
File metadata and controls
638 lines (545 loc) · 16.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* Server.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: rcaillie <rcaillie@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2025/08/26 15:30:10 by kzhen-cl #+# #+# */
/* Updated: 2025/08/29 13:57:07 by rcaillie ### ########.fr */
/* */
/* ************************************************************************** */
#include "Server.hpp"
#include "Client.hpp"
// Initialisation des variables statiques
Server *Server::_instance = NULL;
bool Server::_running = true;
/* ****************************** |CDstructors| ****************************** */
Server::Server(const unsigned short &port, const std::string &pw)
: _port(port), _pw(pw), _fd(-1) {}
/*
Behaviour:
1- Ensures every event is ended.
2- Frees variables and close fds.
*/
Server::~Server(void)
{
std::size_t csize = _clients.size();
for (std::size_t i = 0; i < csize; ++i)
_clients[i]->sendMessage(formatMessage("NOTICE", _clients[i]->getNick().empty() ? "*" : _clients[i]->getNick(), "Server shutdown."));
while (!_clients.empty())
disconnectClient(_clients[0], "Server shutdown");
if (_fd >= 0)
close(_fd);
std::cout << "\nServer Shutdown !" << std::endl;
}
/* ****************************** |CDstructors| ****************************** */
/* ******************************* |Operators| ******************************* */
Server &Server::operator+=(Client *const cl)
{
_clients.push_back(cl);
return (*this);
}
Server &Server::operator-=(Client *const cl)
{
std::size_t csize = _clients.size();
for (std::size_t i = 0; i < csize; ++i)
{
if (_clients[i] == cl)
{
delete (cl);
_clients.erase(_clients.begin() + i);
break ;
}
}
return (*this);
}
Server &Server::operator+=(Channel *const ch)
{
_channels.push_back(ch);
return (*this);
}
Server &Server::operator-=(Channel *const ch)
{
std::size_t csize = _channels.size();
for (std::size_t i = 0; i < csize; ++i)
{
if (_channels[i] == ch)
{
delete (ch);
_channels.erase(_channels.begin() + i);
break ;
}
}
return (*this);
}
/* ******************************* |Operators| ******************************* */
/**
* Analyse une commande IRC et retourne ses paramètres
* @param message Le message complet
* @return Un vecteur contenant tous les paramètres
*/
const std::vector<std::string> parseIrcMessage(const std::string &message)
{
std::vector<std::string> params;
const size_t MAX_PARAMS = 15; // RFC 1459: Maximum 15 paramètres
params.clear();
size_t start = message.find_first_not_of(" \t\r\n\v\f");
if (start == std::string::npos)
return params;
if (message[start] == ':')
return params;
// Découper en paramètres
std::istringstream iss(message.substr(start));
std::string token;
while (iss >> token && params.size() < MAX_PARAMS)
{
if (token[0] == ':')
{
std::string trailing;
std::getline(iss, trailing);
params.push_back(token.substr(1) + trailing);
break;
}
else
{
params.push_back(token);
}
}
// Si limite paramètres atteint
if (params.size() >= MAX_PARAMS)
{
std::string remaining;
if (std::getline(iss, remaining) && !remaining.empty())
{
// Tronquer et ajouter un indicateur
params.push_back("ERR_TOOMANYPARAMS");
}
}
return params;
}
inline void Server::addEvent(const std::string &cmd, char (Server::*f)(Client *const, const std::vector<std::string> &tokens))
{
_events.push_back(std::make_pair(cmd, f));
}
/* **************************** |Channel Related| **************************** */
Channel *Server::getChannel(const std::string &name) const
{
std::size_t csize = _channels.size();
for (std::size_t i = 0; i < csize; ++i)
if (_channels[i]->getName() == name)
return (_channels[i]);
return (NULL);
}
/* **************************** |Channel Related| **************************** */
Client *Server::getClient(const std::string &nick) const
{
std::size_t csize = _clients.size();
for (std::size_t i = 0; i < csize; ++i)
{
Client *const current = _clients[i];
if (current->getNick() == nick && current->isRegistered())
return (current);
}
return (NULL);
}
/* ****************************** |Server Init| ****************************** */
void Server::initEvents(void)
{
addEvent("PASS", &Server::pass);
addEvent("NICK", &Server::nick);
addEvent("USER", &Server::user);
addEvent("WHOIS", &Server::whois);
addEvent("PING", &Server::ping);
addEvent("JOIN", &Server::join);
addEvent("PART", &Server::part);
addEvent("PRIVMSG", &Server::privmsg);
addEvent("TOPIC", &Server::topic);
addEvent("MODE", &Server::mode);
addEvent("KICK", &Server::kick);
addEvent("WHO", &Server::who);
addEvent("INVITE", &Server::invite);
}
bool Server::initServer(void)
{
// signaux
signal(SIGINT, Server::signalHandler);
signal(SIGQUIT, Server::signalHandler);
// socket
_fd = socket(AF_INET, SOCK_STREAM, 0);
if (_fd < 0)
{
std::cerr << "Error. Socket failed: " << strerror(errno) << std::endl;
return false;
}
// socket opt
if (!initSocketOptions())
return false;
// Lier socket et écouter
if (!bindAndListen())
return false;
initEvents();
std::cout << "Server listening on port " << _port << std::endl;
return true;
}
/* ****************************** |Server Init| ****************************** */
// Fonction utilitaire pour formater les messages de réponse selon RFC
inline std::string formatMessage(const std::string &code, const std::string &target, const std::string &message)
{
std::string serverIP = Server::getInstance() ? Server::getInstance()->getServerIP() : "127.0.0.1";
std::string source = ":" + serverIP;
return (source + " " + code + " " + target + " :" + message + "\r\n");
}
// Fonction utilitaire pour formater les messages d'erreur selon RFC
inline std::string formatError(const std::string &code, const std::string &target, const std::string &message)
{
return formatMessage(code, target, message);
}
void Server::mall(const std::string &msg) const
{
int csize = _clients.size();
for (int i = 0; i < csize; ++i)
if (_clients[i]->isRegistered())
_clients[i]->sendMessage(msg);
}
// Initialiser les options du socket
bool Server::initSocketOptions(void)
{
// Reutiliser port apres fermeture
int opt = 1;
if (setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
{
std::cerr << "Error. Setsockopt failed: " << strerror(errno) << std::endl;
close(_fd);
return false;
}
// Mode non bloquant
if (fcntl(_fd, F_SETFL, O_NONBLOCK) < 0)
{
std::cerr << "Error. Failed to set non-blocking mode: " << strerror(errno) << std::endl;
close(_fd);
return false;
}
return true;
}
// Lier le socket à un port et commencer à écouter
bool Server::bindAndListen(void)
{
sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
addr.sin_family = AF_INET;
addr.sin_port = htons(_port);
addr.sin_addr.s_addr = INADDR_ANY;
// Lier le socket à l'adresse et au port
if (bind(_fd, (struct sockaddr *)&addr, sizeof(addr)) < 0)
{
std::cerr << "Error. Bind failed: " << strerror(errno) << std::endl;
close(_fd);
return false;
}
// Écouter les connexions entrantes
if (listen(_fd, SOMAXCONN) < 0)
{
std::cerr << "Error. Listen failed: " << strerror(errno) << std::endl;
close(_fd);
return false;
}
// IP locale serveur
_serverIP = "127.0.0.1";
return true;
}
/* ****************************** |Server Loop| ****************************** */
void Server::run(void)
{
while (_running)
{
// Configurer pollfd serveur / clients
setupPollFds();
// Attendre activity 1000 ms = 1 seconde
int activity = poll(&_pollfds[0], _pollfds.size(), 1000);
// Check errors
if (!checkSocketErrors(activity))
continue;
// Si signal alors fermer
if (!_running)
break;
// Traiter messages/actions poll
if (activity > 0)
handlePollEvents(activity);
}
}
// signal handler for SIGINT and SIGQUIT
void Server::signalHandler(int signum)
{
std::cout << "\n🛑 Server shutdown signal (" << signum << ") received." << std::endl;
std::cout << "Closing all client connections..." << std::endl;
if (_instance)
_running = false;
}
// Configurer structures pollfd serveur et clients
void Server::setupPollFds(void)
{
_pollfds.clear();
// Ajouter socket serveur
pollfd server_pollfd;
server_pollfd.fd = _fd;
server_pollfd.events = POLLIN;
server_pollfd.revents = 0;
_pollfds.push_back(server_pollfd);
// Ajouter tous clients
std::size_t csize = _clients.size();
for (std::size_t i = 0; i < csize; ++i)
{
pollfd client_pollfd;
client_pollfd.fd = _clients[i]->getFd();
client_pollfd.events = POLLIN;
client_pollfd.revents = 0;
_pollfds.push_back(client_pollfd);
}
}
// Vérifier les erreurs de poll et décider de continuer ou non
bool Server::checkSocketErrors(int activity)
{
// Erreur poll
if (activity < 0)
{
// Signal detecté pour poll
if (errno == EINTR)
{
std::cout << "Poll interrupted by signal." << std::endl;
return false;
}
std::cerr << "Error. Poll failed: " << strerror(errno) << std::endl;
return false;
}
else if (activity == 0)
{
// Timeout, rien à faire
return false;
}
return true;
}
// Traiter les événements détectés par poll
void Server::handlePollEvents(int activity)
{
// Nouvelle connexion (ET binaire si true) sur server
if (_pollfds[0].revents & POLLIN)
{
handleNewConnection();
activity--;
}
// activité sockets clients si activity > 0
if (activity > 0)
{
std::size_t psize = _pollfds.size();
for (std::size_t i = 1; i < psize && activity > 0; ++i)
{
if (_pollfds[i].revents & POLLIN)
{
handleClientInput(i);
activity--;
}
else if (_pollfds[i].revents & (POLLHUP | POLLERR | POLLNVAL))
{
handleClientError(i);
activity--;
}
}
}
}
/* ********************* |Server Loop : Client Handling| ********************* */
// Gère les données envoyées par un client
void Server::handleClientInput(size_t pollfdIndex)
{
// Trouver le client correspondant au descripteur
std::size_t csize = _clients.size();
for (std::size_t j = 0; j < csize; ++j)
{
if (_clients[j]->getFd() == _pollfds[pollfdIndex].fd)
{
int result = handleClientMessage(_clients[j]);
if (result == 0)
{
// Client déconnecté brutalement => disconnectClient
disconnectClient(_clients[j], "Connection timeout");
break;
}
else if (result == 2)
{
// Client déconnecté QUIT
break;
}
// result == 1 OK
}
}
}
// Gère les erreurs de connexion avec un client
void Server::handleClientError(size_t pollfdIndex)
{
// Gérer la déconnexion ou l'erreur
std::size_t csize = _clients.size();
for (std::size_t j = 0; j < csize; ++j)
{
if (_clients[j]->getFd() == _pollfds[pollfdIndex].fd)
{
std::string nickname = _clients[j]->isRegistered() ? _clients[j]->getNick() : "unregistered";
std::cout << "📡 Client '" << nickname << "' disconnected abruptly (fd=" << _pollfds[pollfdIndex].fd << ")" << std::endl;
disconnectClient(_clients[j], "Connection lost");
break;
}
}
}
// Gère une nouvelle connexion au serveur
void Server::handleNewConnection(void)
{
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
int client_fd = accept(_fd, (struct sockaddr *)&client_addr, &addr_len);
if (client_fd < 0)
{
std::cerr << "Error. Accept failed" << std::endl;
return;
}
// Configurer le socket client
if (!configureClientSocket(client_fd))
return;
// Créer nouveau client
Client* client = createClient(client_fd, client_addr);
if (client)
client->sendMessage(formatMessage("NOTICE", "Auth", "*** Looking up your hostname...") +
formatMessage("NOTICE", "Auth", "*** Found your hostname") +
formatMessage("NOTICE", "Auth", "*** Please enter password with /PASS <password>"));
}
// Configure un socket client en mode non-bloquant
bool Server::configureClientSocket(int client_fd)
{
// Mode non bloquant client
if (fcntl(client_fd, F_SETFL, O_NONBLOCK) < 0)
{
std::cerr << "Error. Failed to set client socket non-blocking" << std::endl;
close(client_fd);
return false;
}
return true;
}
// Crée un nouveau client et l'ajoute à la liste
Client *Server::createClient(int client_fd, struct sockaddr_in &client_addr)
{
try {
Client *tmp = new Client(client_fd);
*this += tmp;
// Obtenir l'adresse IP du client
char ip_str[INET_ADDRSTRLEN];
// Convertir adresse IP en chaîne
inet_ntop(AF_INET, &(client_addr.sin_addr), ip_str, INET_ADDRSTRLEN);
// Stocker l'IP du client
tmp->setIpAddress(std::string(ip_str));
std::cout << "New client connected from " << ip_str << ":"
<< ntohs(client_addr.sin_port) << ", fd=" << client_fd << std::endl;
// ntohs(client_addr.sin_port) = recuperer adresse:port
return tmp;
}
catch (const std::exception &e) {
std::cerr << "Client error: " << e.what() << std::endl;
close(client_fd);
return NULL;
}
}
// Gère les messages reçus d'un client
// Retourne:
// 0 = Client s'est déconnecté brutalement (appeler disconnectClient)
// 1 = Tout va bien, continuer
// 2 = Client a quitté proprement via QUIT (ne pas appeler disconnectClient)
int Server::handleClientMessage(Client *client)
{
if (!client->readFromSocket())
{
return 0; // Client déconnecté brutalement
}
while (69 != *(int *)"UNICORN")
{
std::string buffer = client->getBuffer();
// Chercher la première fin de ligne complète
size_t pos = std::string::npos;
size_t lineEndSize = 0;
// Chercher \r\n (protocole IRC standard)
pos = buffer.find("\r\n");
if (pos != std::string::npos)
{
lineEndSize = 2;
}
else
{
// Sinon chercher juste \n (compatibilité Unix)
pos = buffer.find("\n");
if (pos != std::string::npos)
{
lineEndSize = 1;
}
}
if (pos == std::string::npos)
break ; // Pas de ligne complète, attendre plus de données
std::string message = buffer.substr(0, pos);
client->clearBuffer();
if (pos + lineEndSize < buffer.length())
{
// Conserver reste buffer prochain passage
client->appendToBuffer(buffer.substr(pos + lineEndSize));
}
if (message.length() > 510) // 512 - 2 (\r\n)
{
std::cout << "Message trop long du client fd=" << client->getFd() << " (" << message.length() << " octets), tronqué" << std::endl;
message = message.substr(0, 510);
client->sendMessage(formatError(ERR_INPUTTOOLONG, client->getNick().empty() ? "*" : client->getNick(), "Message trop long, tronqué à 510 caractères"));
}
std::cout << "Message complet reçu du client fd=" << client->getFd() << ": [" << message << "]" << std::endl;
// Découper le message en tokens pour identifier la commande
const std::vector<std::string> tokens = parseIrcMessage(message);
if (tokens.empty())
continue;
std::string command = tokens[0];
if (command == "QUIT")
{
this->quit(client, tokens);
return 2;
}
// Traiter les autres commandes normalement
std::size_t esize = _events.size();
for (std::size_t i = 0; i < esize; ++i)
{
if (_events[i].first == command)
{
// Si la commande retourne 0, le client doit être déconnecté
if (!((this->*_events[i].second)(client, tokens)))
return 0;
}
}
}
return 1; // Tout va bien
}
// Déconnecte proprement un client
void Server::disconnectClient(Client *client, const std::string &reason)
{
if (!client)
return;
std::cout << "Déconnexion du client fd=" << client->getFd() << std::endl;
// Sauvegarder les informations du client avant modification
bool isRegistered = client->isRegistered();
std::string nickname = isRegistered ? client->getNick() : "";
std::string ipAddress = client->getIpAddress();
// Envoyer le message de déconnexion AVANT de supprimer le client
if (isRegistered)
{
Channel **const chans = client->getChannels();
for (int i = 0; i < CHPERCL; ++i)
{
if (*(chans + i) != NULL)
{
(*(chans + i))->mall(":" + client->getNick() + "!" + client->getUsername() + "@" + ipAddress + " QUIT " + (*(chans + i))->getName() + " :" + reason + "\r\n");
**(chans + i) -= client;
}
}
}
// Supprimer le client à la fin
*this -= client;
}