You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
5.9 KiB

2 years ago
/**
* @file Master.cpp
* @brief The master sockets class which receive each incomming new client.
* @author Narnaud
* @version 0.1
* @date 2023-01-12
*/
2 years ago
#include "webserv.hpp"
/**
* @brief Destructor
* Close master socket descriptor.
*/
2 years ago
Master::~Master(void) {
close(_fd);
2 years ago
if (DEBUG) cout << "Destroyed master socket\n";
2 years ago
}
2 years ago
/**
* @brief Constructor
* Try to create a socket listening to ip and port defined by input.
2 years ago
* If no exception if thrown, the creation success and the socket is then ready to listen for new clients.
*
* @param list An ip_port_t struct which contain the ip and the port the master listen.
*/
Master::Master(ip_port_t list) : _listen(list) {
2 years ago
int x = 1, port = _listen.port;
2 years ago
string ip = _listen.ip;
_fd = socket(AF_INET, SOCK_STREAM, 0);
2 years ago
if (_fd == 0) throw std::runtime_error("socket() error" + string(strerror(errno)));
if (setsockopt(_fd, SOL_SOCKET, SO_REUSEADDR, (char *)&x, sizeof(x)) < 0 && close(_fd) <= 0)
throw std::runtime_error("setsockopt() error: " + string(strerror(errno)));
2 years ago
_address.sin_family = AF_INET;
_address.sin_addr.s_addr = inet_addr(ip.c_str());
_address.sin_port = htons(port);
2 years ago
if (bind(_fd, (struct sockaddr *)&_address, sizeof(_address)) && close(_fd) <= 0)
throw std::runtime_error("bind() error: " + string(strerror(errno)));
2 years ago
if (listen(_fd, 3) < 0 && close(_fd) <= 0) throw std::runtime_error("listen() error: " + string(strerror(errno)));
#ifdef __APPLE__
fcntl(socket, F_SETFL, O_NONBLOCK);
#endif
cout << "New master socket with fd " << _fd << " which listen " << ip << ":" << port << "\n";
_pollfds[_poll_id_amount].fd = _fd;
_pollfds[_poll_id_amount].events = POLLIN | POLLPRI;
_poll_id = _poll_id_amount;
_poll_id_amount++;
2 years ago
}
/**
2 years ago
* @brief Check master and his clients sockets after poll performed.
*/
2 years ago
void Master::check_socket(void) {
int addrlen = sizeof(_address);
2 years ago
if (_pollfds[_poll_id].revents & POLLIN) { /// < incomming master request
2 years ago
int new_socket = accept(_fd, (struct sockaddr *)&_address, (socklen_t *)&addrlen);
if (new_socket < 0) throw std::runtime_error("accept() error:" + string(strerror(errno)));
2 years ago
#ifdef __APPLE__
fcntl(new_socket, F_SETFL, O_NONBLOCK);
#endif
2 years ago
ip_port_t cli_listen = get_ip_port_t(inet_ntoa(_address.sin_addr), ntohs(_address.sin_port));
Client *new_cli = new Client(new_socket, cli_listen, this);
2 years ago
if (_poll_id_amount > MAX_CLIENTS) {
new_cli->send_error(503);
delete new_cli;
} else {
_childs.push_back(new_cli);
for (int i = _first_cli_id; i < MAX_CLIENTS; i++) {
if (_pollfds[i].fd != 0) continue;
_pollfds[i].fd = new_socket;
2 years ago
_pollfds[i].events = POLLIN | POLLPRI;
2 years ago
new_cli->_poll_id = i;
_poll_id_amount++;
break;
}
}
2 years ago
}
2 years ago
}
/**
* @brief Handle master's childs.
* Loop along _childs
* if poll even incomming on the child socket:
* - give 1023 next sockets bits to Client methods to handle.
* - if Request is fully received, does the answer and flag the socket for outcomming event.
* - if request isn't fully received, flag the socket for incomming event.
* - if read returned 0, delete client.
* @param env The environment object.
*/
void Master::check_childs(Env *env) {
2 years ago
int child_fd;
2 years ago
for (std::vector<Client *>::iterator it = _childs.begin(); it < _childs.end(); it++) {
2 years ago
child_fd = (*it)->_fd;
int i = (*it)->_poll_id;
if (_pollfds[i].fd > 0 && _pollfds[i].revents & POLLIN) {
2 years ago
char buffer[1024];
int valread = read(child_fd, buffer, 1023);
buffer[valread] = '\0';
2 years ago
if (valread == 0) {
delete (*it);
_childs.erase(it);
2 years ago
} else if ((*it)->getRequest(env, buffer)) {
(*it)->handleRequest();
2 years ago
_pollfds[i].events = POLLOUT;
if ((*it)->_finish) {
delete (*it);
_childs.erase(it);
}
2 years ago
} else _pollfds[i].events = POLLIN | POLLPRI;
2 years ago
}
}
}
2 years ago
/**
*
* @brief Choose the server which must handle a request
* Each server can lsiten multiple range_ip:port and each range_it:port can be listen by multiple servers.
* So for each request, we must look at the socket which given us the client to know how the client came. If multiple
* servers listen the range from where the client came, ones with exact correspondance are prefered. If there are
* multiples servers listening exactly the ip the client try to reach or which listen a range which contain it, the
* first one which have the same server_name as the host the client used to reach server is used, else it's the first
* one of exact correspondance or first one which have the ip requested in his listen range.
*
* @param env The environment object.
* @param host The host the client used to reached the server.
*
* @return The server object choosen to handle the request.
*/
2 years ago
Server *Master::choose_server(Env *env, string host) {
2 years ago
std::vector<Server *> exact, inrange;
vec_string ip_listen, ip_required;
2 years ago
ip_required = split(_listen.ip, ".");
2 years ago
for (std::vector<Server *>::iterator server = env->_servers.begin(); server < env->_servers.end(); server++) {
std::vector<ip_port_t> serv_listens = (*server)->_listens;
for (std::vector<ip_port_t>::iterator it = serv_listens.begin(); it < serv_listens.end(); it++) {
if (_listen.port != (*it).port) continue;
2 years ago
if (_listen.ip == (*it).ip) {
2 years ago
exact.push_back(*server);
2 years ago
continue;
}
bool is_inrange = true;
ip_listen = split((*it).ip, ".");
vec_string::iterator r = ip_required.end();
vec_string::iterator l = ip_listen.end();
while (r > ip_required.begin()) {
if (*(--l) != *(--r) && *l != "0") is_inrange = false;
2 years ago
}
2 years ago
if (is_inrange) inrange.push_back(*server);
2 years ago
}
}
2 years ago
if (DEBUG) std::cout << "req: " << _listen.ip << ":" << _listen.port << "\n";
2 years ago
if (exact.size() == 0) {
2 years ago
for (std::vector<Server *>::iterator server = inrange.begin(); server < inrange.end(); server++) {
if (host == (*server)->getName()) return *server;
2 years ago
}
return inrange.front();
} else {
2 years ago
for (std::vector<Server *>::iterator server = exact.begin(); server < exact.end(); server++) {
if (host == (*server)->getName()) return *server;
}
2 years ago
return exact.front();
}
2 years ago
}