-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.cpp
More file actions
331 lines (302 loc) · 9.14 KB
/
server.cpp
File metadata and controls
331 lines (302 loc) · 9.14 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
#include <errno.h>
#include <ctime>
#include "server.hpp"
#include <iostream>
#include <vector>
/* Constructors and Deconstructors */
//===================================================//
bool Server::_active = true;
Server::Server(std::string serverPass, int port): fd_server(), clients(), msg(), handler(), _serverName("OurServer"), _channelList(), _port(port), _pass(serverPass)
{
std::vector<User *> _userList;
std::cout << "Server Object Created" << std::endl;
}
Server::~Server()
{
std::cout << "Deconstructor Called" << std::endl;
}
/* Getters and Setters */
//===================================================//
void Server::setPass(std::string pass) {
this->_pass = pass;
}
std::string Server::getPass() const {
return (this->_pass);
}
void Server::addChannel(Channel *newChannel)
{
this->_channelList.push_back(newChannel);
}
/* Checkers */
//===================================================//
User* Server::findByFd(int clientFd) {
std::vector<User*>::iterator itr;
for (itr=this->_userList.begin(); itr != this->_userList.end(); itr++) {
if (clientFd == (*itr)->getFd())
return (*itr);
}
std::cerr << "User FD not found" << std::endl;
return (NULL);
}
User* Server::findByNick(std::string nick) {
std::cout << "Reached here 6" << std::endl;
std::vector<User*>::iterator itr;
if (this->_userList.empty()) {
return (NULL); }
else {
std::cout << "Reached here 7" << std::endl;
for (itr=this->_userList.begin(); itr != this->_userList.end(); itr++) {
if (nick == (*itr)->getNick())
return (*itr);
}
}
std::cerr << "User nickname not found" << std::endl;
return (NULL);
}
Channel* Server::findChannel(std::string name) {
std::vector<Channel*>::iterator itr;
for (itr=this->_channelList.begin(); itr != this->_channelList.end(); itr++) {
if (name == (*itr)->getName())
return (*itr);
}
std::cerr << "Channel not found by name" << std::endl;
return (NULL);
}
/* Server Actions */
//==============================================//
void Server::killUser(User* user)
{
std::vector<Channel*> activeChannelList = user->getChannels();
std::vector<Channel*>::iterator start = activeChannelList.begin();
std::vector<Channel*>::iterator end = activeChannelList.end();
if (user->getChannels().size() > 1)
{
while (start != end)
{
(*start)->deleteUser(user->getNick()); // delete user from userloglist in channel
start++;
}
}
else if (user->getChannels().size() != 0)
{
(*start)->deleteUser(user->getNick());
//start++;
}
std::vector<User*>::iterator start_U = _userList.begin();
std::vector<User*>::iterator end_U = _userList.end();
if (_userList.size() > 0)
{
while (start_U != end_U)
{
if ((*start_U)->getNick() == user->getNick()) // delete user from server
{
_userList.erase(start_U);
std::cout << "erasing user" << std::endl;
break ;
}
start_U++;
}
}
delete user;
}
/* Socket Actions */
//===================================================//
/*
* setsocktopt: sets certain options when using the associated socket
* SOL_SOCKET: indicates we are setting options at socket level
* SO_REUSEADDR: allows the socket to be bound to an address that is already in use
* **************************************
* fcntl(): manipulates filedescriptors;
* O_NONBLOCK: Do not block an open, a read, or a write on the file (do not wait for terminal input)
*/
int Server::createServer(void)
{
int listening = socket(AF_INET, SOCK_STREAM, 0);
if (listening == -1)
{
std::cerr << "Can't create a socket!" << std::endl;
return (-1);
}
int opt = 1;
if (setsockopt(listening, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)))
std::cerr << "Error during setting socket options\n";
if (fcntl(listening, F_SETFL, O_NONBLOCK) == -1)
std::cerr << "Error during setting socket to NON_BLOCKING\n";
struct sockaddr_in hint = {};
// setting everything in the struct hint to zero. prevents certain segfaults
bzero((char*) &hint, sizeof(hint));
hint.sin_family = AF_INET; // IPv4 only
hint.sin_addr.s_addr = htonl(INADDR_ANY); // puts host IP
// inet_pton(AF_INET, "0.0.0.0", &hint.sin_addr);
hint.sin_port = htons(this->_port); // convert host bytes to network bytes
if (bind(listening, (sockaddr *)&hint, sizeof(hint)) == -1) // binding socket to host IP
{
std::cerr << "Can't bind to IP/Port!" << std::endl;
std::cerr << errno << std::endl;
return (-2);
}
if (listen(listening, SOMAXCONN) == -1) // socket waiting for connection
{
std::cerr << "Can't listen!" << std::endl;
return (-3);
}
//ft_log("Server listening");
this->fd_server = listening;
return (1);
}
/* Reads input from an fd, returns message if received successfully. */
int Server::readInput(int client_no)
{
char buf[4096];
// int i = 0;
memset(buf, 0, 4096);
int bytesRecv = recv(this->clients[client_no].fd, buf, 4096, 0);
if (bytesRecv == -1)
{
std::cerr << "There was a connection issue!" << std::endl;
return (-1);
}
else if (bytesRecv == 0)
{
if (close(this->clients[client_no].fd) == -1)
std::cerr << "close!" << std::endl;
std::cerr << "The Client disconnected!" << std::endl;
this->clients[client_no].fd *= -1; // it will be ignored in future
return (-1);
}
else // data received from client
{
// Display message
std::cout << "Received: " << std::string(buf, 0, bytesRecv) << std::endl;
}
this->msg = std::string(buf, 0, bytesRecv);
return (1);
}
/*
* pollfd{fd,events,revents}: struct defines the relevant socket, what kind of event to wait for, and
* the type of event to respond with;
***********************************************
* POLLIN: read in data (from client);
* poll(): waits for action (I/O) on a filedescriptor in a set of filedescriptors
* returns -1 if faied, 0 if timed out or num of fd's connected;
* POLLHUP: Connection disconnected
*/
int Server::acceptCall()
{
//time_t timeNow = time(NULL);
for (int i = 0; i < 1024; i++)
{
if ((this->clients[i].revents & POLLIN) == POLLIN) // fd is ready for reading
{
std::cout << "\n ----- New message incoming from fd: " << this->clients[i].fd << std::endl;
if (this->clients[i].fd == this->fd_server) // request for new connection
{
std::cout << "New Connection " << std::endl;
sockaddr_in user;
socklen_t userSize = sizeof(user);
char host[NI_MAXHOST];
char service[NI_MAXSERV];
memset(host, 0, NI_MAXHOST);
memset(service, 0, NI_MAXSERV);
int userSocket = accept(this->fd_server, (sockaddr *)&user, &userSize);
if (userSocket == -1)
{
std::cerr << "Problem with client connecting!" << std::endl;
return (-1);
}
// Next 10 lines deal with client info:
int result = getnameinfo((sockaddr *)&user, userSize, host, NI_MAXHOST, service, NI_MAXSERV, 0);
if (result)
{
std::cout << host << " connected on " << service << std::endl;
}
else
{
inet_ntop(AF_INET, &user.sin_addr, host, NI_MAXHOST);
std::cout << host << " connected on " << ntohs(user.sin_port) << std::endl;
}
// ... until here
int j = 0;
for (; j < 1024; j++)
if (clients[j].fd == -1)
break;
if (j < 1024)
{
clients[j].fd = userSocket;
clients[j].events = POLLIN;
clients[j].revents = 0;
std::cout << "New connection data:\n fd_server: " << this->fd_server << std::endl;
std::cout << "handler: " << this->handler << std::endl;
std::cout << "msg: " << this->msg << std::endl;
std::cout << "pass: " << this->_pass << std::endl;
std::cout << "port: " << this->_port << std::endl;
std::cout << "host: " << host << std::endl;
std::cout << "service: " << service << std::endl;
std::cout << "*********************************************************************************" << std::endl;
User *newUser = new User(clients[j], host, service, this);
//std::cout << "Reached debugging point 1" << std::endl;
this->_userList.push_back(newUser);
//}
}
else
{
std::cout << "Server Capacity for Clients is Full" << std::endl;
close(userSocket);
}
}
else // data from an existing connection, recieve it
{
User* foundUser = this->findByFd(this->clients[i].fd);
if (this->readInput(i) == -1)
return (-1);
else
{
std::cout << "Reached Accept Call Function, in else if: Start" << std::endl;
handler->start(foundUser, msg);
}
}
}
}
return (1);
}
void Server::initClient()
{
for (int i = 0; i < 1024; i++)
{
this->clients[i].fd = -1;
this->clients[i].events = 0;
this->clients[i].revents = 0;
}
}
bool Server::free_everything() {
std::cout << "This function will free and kill all users quit" << std::endl;
// kill all users, then all channels will be killed
std::vector<User *>::iterator iterU = this->_userList.begin();
while (!this->_userList.empty())
{
this->killUser(*iterU);
//iterU++;
}
std::cout << "free_everything(): All users killed. Server ready to shut down." << std::endl;
// server is killed outside;
return (false);
}
void Server::pollLoop()
{
bool _activeLoop = true;
while (_activeLoop)
{
switch (poll(this->clients, 1024, 10000))
{
case 0:
break;
case -1:
break;
default:
this->acceptCall();
break;
}
if (_active == false)
_activeLoop = this->free_everything();
}
}