-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
459 lines (410 loc) · 14.8 KB
/
main.cpp
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* main.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: anon <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2022/05/08 13:42:50 by jcueille #+# #+# */
/* Updated: 2022/10/03 14:53:02 by anon ### ########.fr */
/* */
/* ************************************************************************** */
#include "includes/includes.hpp"
#include "includes/parsing.hpp"
# include <sstream>
user *users = NULL;
channel *channels = NULL;
struct pollfd fds[SOMAXCONN];
int nfds = 1;
static int check_args(int ac, char **av)
{
long int n;
if (ac != 3)
ft_exit("Wrong number of arguments\nHow to use: ./ircserv port password", 1, NULL);
n = strtol(av[1], NULL, 10);
if (n < 0 || n > 65535 || errno == ERANGE)
ft_exit("Wrong port number.", errno == ERANGE ? errno : 1, NULL);
return n;
}
int new_client(int id, struct pollfd *fd)
{
struct s_user *tmp = users;
struct s_user* new_user = new struct s_user();
if (new_user == NULL)
return -1;
new_user->next = NULL;
new_user->id = id;
new_user->fd = fd->fd;
new_user->idle = 0;
time(&new_user->signon);
new_user->nickname = "";
new_user->realname = "";
new_user->auth = 0;
memset(new_user->modes, 0, sizeof(new_user->modes));
new_user->hostname = "127.0.0.1";
std::stringstream ss;
ss << new_user->nickname << " id: " << new_user->id << " fd: " << new_user->fd;
pp(std::string(RED), ss.str());
if (!users)
{
users = new_user;
new_user->modes[OPERATOR_MODE] = 1;
new_user->prev = NULL;
}
else
{
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = new_user;
new_user->prev = tmp;
}
return 0;
}
void delete_client(user *u)
{
if (users == u && u->next == NULL)
users = NULL;
else if (users == u && u->next)
users = u->next;
if (u->next)
u->next->prev = u->prev;
if (u->prev)
u->prev->next = u->next;
for (std::vector<channel *>::iterator it = u->channels.begin(); it != u->channels.end(); it++)
{
for (std::vector<user *>::iterator ite = (*it)->users.begin(); ite != (*it)->users.end(); ite++)
{
if ((*ite) == u)
{
send_to_all_chan(channel_message("PART " + (*it)->name + " " + "connection closed", u), (*it));
(*it)->users.erase(ite);
return ;
}
}
}
std::vector<channel *>().swap(u->channels);
delete u;
}
void delete_channel(std::string name)
{
channel * tmp = channels;
while (tmp != NULL)
{
if (tmp->name == name)
{
tmp->next->prev = tmp->prev;
tmp->prev->next = tmp->next;
std::vector<user *>().swap(tmp->users);
delete tmp;
return ;
}
tmp = tmp->next;
}
}
channel *new_channel(std::string name)
{
channel *tmp = channels;
channel* new_channel = new channel();
if (new_channel == NULL)
return NULL;
new_channel->next = NULL;
new_channel->name = name;
if (!channels)
channels = new_channel;
else
{
while (tmp->next != NULL)
tmp = tmp->next;
tmp->next = new_channel;
new_channel->prev = tmp;
}
new_channel->key = "";
new_channel->topic = "";
new_channel->creation = current_time();
memset(new_channel->modes, 0, sizeof(new_channel->modes));
return new_channel;
}
void signalHandler( int signum ) {
std::cout << "Interrupt signal (" << signum << ") received." << std::endl;
ft_free_exit("SIGINT", SIGINT);
exit(signum);
}
int main (int argc, char *argv[])
{
signal(SIGINT, signalHandler);
int port = check_args(argc, argv);
int len, rc, on = 1, new_client_id = 0;
int listen_sd = -1, new_sd = -1;
int end_server = FALSE, compress = FALSE, client_running = TRUE;
int close_conn;
char buffer[513];
struct sockaddr_in6 addr;
int current_size = 0, i;
/*************************************************************/
/* Create an AF_INET6 stream socket to receive incoming */
/* connections on */
/*************************************************************/
if ( (listen_sd = socket(AF_INET6, SOCK_STREAM, 0)) == -1)
ft_exit(" socket creation failed.", errno, NULL);
/*************************************************************/
/* Allow socket descriptor to be reuseable */
/*************************************************************/
if ((rc = setsockopt(listen_sd, SOL_SOCKET, SO_REUSEADDR,
(char*)&on, sizeof(on))) < 0)
ft_exit(" setsockopt failed.", errno, &listen_sd);
/*************************************************************/
/* Set socket to be nonblocking. All of the sockets for */
/* the incoming connections will also be nonblocking since */
/* they will inherit that state from the listening socket. */
/*************************************************************/
if ( (rc = ioctl(listen_sd, FIONBIO, (char*)&on)) < 0)
ft_exit(" ioctl failed.", errno, &listen_sd);
/*************************************************************/
/* Bind the socket */
/*************************************************************/
memset(&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
memcpy(&addr.sin6_addr, &in6addr_any, sizeof(in6addr_any));
addr.sin6_port = htons(port);
if ((rc = bind(listen_sd,
(struct sockaddr *)&addr, sizeof(addr))) < 0)
ft_exit(" bind failed.", errno, &listen_sd);
/*************************************************************/
/* Set the listen back log */
/*************************************************************/
if ((rc = listen(listen_sd, SOMAXCONN)) < 0)
ft_exit(" listen failed.", errno, &listen_sd);
/*************************************************************/
/* Initialize the pollfd structure */
/*************************************************************/
memset(fds, 0 , sizeof(fds));
/*************************************************************/
/* Set up the initial listening socket */
/*************************************************************/
fds[0].fd = listen_sd;
fds[0].events = POLLIN;
/*************************************************************/
/* Loop waiting for incoming connects or for incoming data */
/* on any of the connected sockets. */
/*************************************************************/
//char astring[4000];
do
{
/***********************************************************/
/* Call poll() and wait 3 minutes for it to complete. */
/***********************************************************/
//printf("Waiting on poll()...\n");
if ((rc = poll(fds, nfds, -1)) < -1)
ft_exit(" poll failed.", errno, &listen_sd);
/***********************************************************/
/* Check to see if the 3 minute time out expired. */
/***********************************************************/
if (rc == 0)
{
printf(" poll() timed out. End program.\n");
break;
}
/***********************************************************/
/* One or more descriptors are readable. Need to */
/* determine which ones they are. */
/***********************************************************/
current_size = nfds;
for (i = 0; i < current_size; i++)
{
if (DEBUG)
{
std::stringstream ss;
ss << "nfds: " << nfds << "fd: " << fds[i].fd;
pp(std::string(CYAN), ss.str());
}
/*********************************************************/
/* Loop through to find the descriptors that returned */
/* POLLIN and determine whether it's the listening */
/* or the active connection. */
/*********************************************************/
if(fds[i].revents == 0)
continue;
/*********************************************************/
/* If revents is not POLLIN, it's an unexpected result, */
/* log and end the server. */
/*********************************************************/
if(fds[i].revents != POLLIN)
{
printf(" Error! revents = %d\n", fds[i].revents);
end_server = TRUE;
break;
}
if (fds[i].fd == listen_sd)
{
/*******************************************************/
/* Listening descriptor is readable. */
/*******************************************************/
printf(" Listening socket is readable\n");
/*******************************************************/
/* Accept all incoming connections that are */
/* queued up on the listening socket before we */
/* loop back and call poll again. */
/*******************************************************/
do
{
/*****************************************************/
/* Accept each incoming connection. If */
/* accept fails with EWOULDBLOCK, then we */
/* have accepted all of them. Any other */
/* failure on accept will cause us to end the */
/* server. */
/*****************************************************/
new_sd = accept(listen_sd, NULL, NULL);
if (new_sd < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" accept() failed");
end_server = TRUE;
}
break;
}
/*****************************************************/
/* Add the new incoming connection to the */
/* pollfd structure */
/*****************************************************/
printf(" New incoming connection - %d\n", new_sd);
fds[nfds].fd = new_sd;
fds[nfds].events = POLLIN;
if (new_client(new_client_id, &fds[nfds]) == -1)
ft_free_exit(" user creation failed.", -1);
nfds++;
new_client_id++;
/*****************************************************/
/* Loop back up and accept another incoming */
/* connection */
/*****************************************************/
} while (new_sd != -1);
}
/*********************************************************/
/* This is not the listening socket, therefore an */
/* existing connection must be readable */
/*********************************************************/
else
{
//printf(" Descriptor %d is readable\n", fds[i].fd);
close_conn = FALSE;
/*******************************************************/
/* Receive all incoming data on this socket */
/* before we loop back and call poll again. */
/*******************************************************/
do
{
/*****************************************************/
/* Receive data on this connection until the */
/* recv fails with EWOULDBLOCK. If any other */
/* failure occurs, we will close the */
/* connection. */
/*****************************************************/
memset(buffer, 0, sizeof(buffer));
rc = recv(fds[i].fd, buffer, sizeof(buffer) - 1, 0);
buffer[512] = '\0';
if (rc < 0)
{
if (errno != EWOULDBLOCK)
{
perror(" recv() failed");
close_conn = TRUE;
}
break;
}
/*****************************************************/
/* Check to see if the connection has been */
/* closed by the client */
/*****************************************************/
if (rc == 0)
{
printf(" Connection closed\n");
close_conn = TRUE;
break;
}
/*****************************************************/
/* Data was received */
/*****************************************************/
len = rc;
(void)len;
//printf(" %d bytes received\n", len);
if (DEBUG == 1)
{
std::stringstream ss;
ss << fds[i].fd;
pp(std::string(CYAN), ss.str());
}
user *tmp_user = find_user_by_fd(fds[i].fd);
if (DEBUG)
{
std::stringstream ss;
ss << fds[i].fd << " " << ( tmp_user ? tmp_user->nickname : "NULL");
pp(std::string(CYAN), ss.str());
}
std::string cmd = buffer;
std::stringstream ss(buffer);
std::string token;
while (std::getline(ss, token, '\n')) {
Command tmp_cmd(token);
if (DEBUG == 1)
{
pp(std::string(CYAN), tmp_user->nickname);
print_user(tmp_user);
}
if (tmp_cmd.parse(fds, &nfds, tmp_user, argv[2]))
{
delete_client(tmp_user);
close_conn = TRUE;
break ;
}
}
/*****************************************************/
/* Echo the data back to the client */
/*****************************************************/
//rc = send(fds[i].fd, buffer, len, 0);
if (rc < 0)
{
perror(" send() failed");
close_conn = TRUE;
break;
}
break;
} while(client_running == TRUE);
/*******************************************************/
/* If the close_conn flag was turned on, we need */
/* to clean up this active connection. This */
/*clean up process includes removing the */
/* descriptor. */
/*******************************************************/
if (close_conn)
{
if (DEBUG)
{
std::stringstream ss;
ss << "d:" << fds[i].fd;// << " " << ( tmp_user ? tmp_user->nickname : "NULL");
pp(std::string(CYAN), ss.str());
}
delete_client(find_user_by_fd(fds[i].fd));
close(fds[i].fd);
fds[i].fd = -1;
//current_size--;
compress = TRUE;
}
} /* End of existing connection is readable */
} /* End of loop through pollable descriptors */
/***********************************************************/
/* If the compress flag was turned on, we need */
/* to squeeze together the array and decrement the number */
/* of file descriptors. We do not need to move back the */
/* events and revents fields because the events will always*/
/* be POLLIN in this case, and revents is output. */
/***********************************************************/
if (compress)
{
compress = FALSE;
compress_array(fds, &nfds);
}
} while (end_server == FALSE); /* End of serving running. */
ft_free_exit("0", 0);
}