Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multiple ports #58

Merged
merged 2 commits into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 22 additions & 23 deletions config/test.conf
Original file line number Diff line number Diff line change
@@ -1,26 +1,25 @@
http {
gobalsetting test;
server {
listen 8888;
server_name 0.0.0.0;
index index.html;
root www;
error_page ./data/404.html;
location /files {
root .;
auto_index on;
}
}
server {
listen 8888;
server_name 0.0.0.0;
index index.html;
root www;
error_page ./data/404.html;
location /files {
root .;
auto_index on;
}
}
server {
server_name 0.0.0.0;
port 4444;
404 ./data/404.html;
client_max_body_size 100000;
location {
root ./www;
auto-index true;
allow-method get;
}
}
server {
server_name 0.0.0.0;
port 8888;
404 ./data/404.html;
client_max_body_size 100000;
location {
root ./www;
auto-index true;
allow-method get;
}
}
}

47 changes: 23 additions & 24 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ extern sig_atomic_t g_signaled;
// Core Functions

void Server::run() {
std::cout << "\n*** Listening on ADDRESS: "
<< this->sockets_[0].getAddressString()
<< " PORT: " << this->sockets_[0].getPort() << " ***\n\n";
while (g_signaled == 0) {
if (poll(this->pollfds_, this->numfds_, TIMEOUT) == -1) {
perror("poll");
Expand All @@ -23,7 +20,7 @@ void Server::run() {
}
if (this->pollfds_[i].revents & POLLIN) {
if (this->sockets_[i].getState() == SERVER) {
newConnection();
newConnection(i);
break;
} else {
handleReceive(i);
Expand All @@ -45,21 +42,19 @@ void Server::run() {
}
}

int Server::startServer(int port) {
int Server::startServer(std::string ip, int port) {
pollfd new_poll;
Socket serv(0);
Socket serv(this->numfds_);
int opt = 1;
socklen_t addrlen = sizeof(serv.getAddress());

this->numfds_ = 1;
memset(pollfds_, -1, MAX_PORTS);
new_poll.fd = socket(AF_INET, SOCK_STREAM, 0);
serv.setState(SERVER);
if (new_poll.fd < 0) {
std::cout << "Error: can not start Socket." << std::endl;
return EXIT_FAILURE;
}
serv.setPort(port);
serv.setPort(ip, port);
if (setsockopt(new_poll.fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) ==
-1) {
std::cerr << "Error: cannot set socket opt." << std::endl;
Expand All @@ -78,8 +73,9 @@ int Server::startServer(int port) {
}
new_poll.events = POLLIN;
new_poll.revents = 0;
this->sockets_[0] = serv;
this->pollfds_[0] = new_poll;
this->sockets_[this->numfds_] = serv;
this->pollfds_[this->numfds_] = new_poll;
this->numfds_++;
return EXIT_SUCCESS;
}

Expand Down Expand Up @@ -225,7 +221,7 @@ void Server::sendResponse(int i) {
<< bytes_sent << std::endl;
}

void Server::newConnection() {
void Server::newConnection(int i) {
if (this->numfds_ > MAX_PORTS) {
std::cout << "Error: no new Connection possible." << std::endl;
return;
Expand All @@ -234,13 +230,15 @@ void Server::newConnection() {
socklen_t addrlen = sizeof(struct sockaddr);
pollfd new_poll;
size_t index = searchFreePoll();
new_poll.fd = accept(this->pollfds_[0].fd,
new_poll.fd = accept(this->pollfds_[i].fd,
(struct sockaddr*)&new_client.getAddress(), &addrlen);
if (new_poll.fd < 0) {
std::cout << "Error: Failed to accept connection." << std::endl;
return;
}
if (fcntl(new_poll.fd, F_SETFL, O_NONBLOCK, FD_CLOEXEC) == -1) {
std::cerr << "Error: fcntl." << std::endl;
return;
}
new_poll.events = POLLIN;
new_poll.revents = 0;
Expand All @@ -249,7 +247,7 @@ void Server::newConnection() {
this->sockets_[index] = new_client;
this->pollfds_[index] = new_poll;
if (index == this->numfds_) {
numfds_++;
this->numfds_++;
}
std::cout << "New connection success on : " << new_client.getAddressString()
<< " with socket nbr: " << new_poll.fd << std::endl;
Expand Down Expand Up @@ -308,13 +306,6 @@ void Server::checkSocketTimeout() {

//// Constructors and Operator overloads

Server::Server(const std::string& ip_addr, int port) : ip_addr_(ip_addr) {
if (startServer(port) == EXIT_FAILURE) {
std::cerr << "Error: Failed to start Server." << std::endl;
exit(EXIT_FAILURE);
}
}

Server::~Server() { exit(EXIT_SUCCESS); }

Server::Server(const Server& obj) { *this = obj; }
Expand All @@ -327,8 +318,16 @@ Server& Server::operator=(const Server& obj) {
}

Server::Server(const Settings& settings) : settings_(settings) {
if (startServer(this->settings_.getServers()[0].getPort()) != 0) {
std::cout << "Error: failed to start server with port: "
<< this->settings_.getServers()[0].getPort() << std::endl;
memset(this->pollfds_, -1, MAX_PORTS);
for (size_t i = 0; i < this->settings_.getServers().size(); ++i) {
if (startServer(this->settings_.getServers()[i].getName(),
this->settings_.getServers()[i].getPort())) {
std::cout << "Error: failed to start Server with port "
<< this->settings_.getServers()[i].getPort() << std::endl;
} else {
std::cout << "Startet server on "
<< this->settings_.getServers()[i].getName() << " with Port "
<< this->settings_.getServers()[i].getPort() << std::endl;
}
}
}
5 changes: 2 additions & 3 deletions src/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@

class Server {
public:
Server(const std::string& ip_addr, int port);
Server(const Settings& settings);
~Server();
Server(const Server& obj);
Expand All @@ -53,12 +52,12 @@ class Server {
Settings settings_;
char* cgi_env_[8];

int startServer(int port);
int startServer(std::string ip, int port);
int pollError(int i);
void removeFd(int i);
size_t searchFreePoll();
void sendResponse(int i);
void newConnection();
void newConnection(int i);
void handleReceive(int i);
void handleSend(int i);
void checkSocketTimeout();
Expand Down
4 changes: 2 additions & 2 deletions src/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ void Socket::setIndex(int i) { this->index_ = i; }

void Socket::setKeepalive(bool state) { this->keepalive_ = state; }

void Socket::setPort(int port) {
void Socket::setPort(std::string ip, int port) {
this->socketAddress_.sin_family = AF_INET;
this->socketAddress_.sin_port = htons(port);
this->socketAddress_.sin_addr.s_addr = INADDR_ANY;
this->socketAddress_.sin_addr.s_addr = inet_addr(ip.c_str());
}

void Socket::setState(sockState state) { this->state_ = state; }
Expand Down
2 changes: 1 addition & 1 deletion src/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class Socket {

void setIndex(int i);
void setKeepalive(bool state);
void setPort(int port);
void setPort(std::string ip, int port);
void setState(sockState stat);
void setRequest(HTTPRequest& req);
bool hasUnfinishedRequest() const;
Expand Down
12 changes: 10 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,17 @@ void handleSIGINT(int param) {
_exit(SIGINT);
}

int main() {
int main(int argc, char* argv[]) {
signal(SIGINT, handleSIGINT);
std::string conf_path("./config/default.conf");
std::string conf_path;
if (argc == 1) {
conf_path = "./config/default.conf";
} else if (argc == 2) {
conf_path = argv[1];
} else {
std::cerr << "Error: wrong amount of arguments" << std::endl;
return EXIT_FAILURE;
}
try {
Settings settings(conf_path);
Server server = Server(settings);
Expand Down
Loading