Skip to content

Commit c55bb60

Browse files
committed
chore: add network and server setup. Fix makefile rules
1 parent e53622a commit c55bb60

File tree

5 files changed

+230
-4
lines changed

5 files changed

+230
-4
lines changed

Makefile

+1-4
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,4 @@ re: fclean all
3939
@echo "Rebuilding..."
4040

4141
run: all
42-
@./$(NAME) default.conf
43-
44-
run2: all
45-
@./$(NAME) arquivo.conf
42+
@./$(NAME) config/default.conf

src/network/NetworkSetup.cpp

+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
//
2+
// Created by vinicius on 11/3/24.
3+
//
4+
5+
#include "NetworkSetup.hpp"
6+
#include <sys/socket.h>
7+
#include <netinet/in.h>
8+
#include <arpa/inet.h>
9+
#include <sys/ioctl.h>
10+
#include <cerrno>
11+
#include <unistd.h> // for close()
12+
13+
NetworkSetup::NetworkSetup()
14+
{
15+
}
16+
17+
NetworkSetup::~NetworkSetup()
18+
{
19+
for (std::vector<int>::iterator it = server_sockets.begin(); it != server_sockets.end(); ++it)
20+
if (*it >= 0)
21+
close(*it);
22+
}
23+
24+
bool NetworkSetup::setupServer(const std::string& host, int port)
25+
{
26+
int server_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK, 0);
27+
if (server_fd < 0)
28+
return false;
29+
30+
// Allow reuse of address
31+
const int opt = 1;
32+
if (setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &opt, sizeof(opt)) < 0)
33+
{
34+
close(server_fd);
35+
return false;
36+
}
37+
38+
struct sockaddr_in address;
39+
address.sin_family = AF_INET;
40+
address.sin_port = htons(port);
41+
address.sin_addr.s_addr = INADDR_ANY; // Allow connections on any interface
42+
43+
// If specific host is provided, use it
44+
if (host != "0.0.0.0" && !host.empty())
45+
{
46+
if (inet_pton(AF_INET, host.c_str(), &address.sin_addr) <= 0)
47+
{
48+
close(server_fd);
49+
return false;
50+
}
51+
}
52+
53+
// Bind socket to address and port
54+
if (bind(server_fd, reinterpret_cast<struct sockaddr*>(&address),
55+
sizeof(address)) < 0)
56+
{
57+
close(server_fd);
58+
return false;
59+
}
60+
61+
// Start listening
62+
if (listen(server_fd, SOMAXCONN) < 0)
63+
{
64+
close(server_fd);
65+
return false;
66+
}
67+
68+
server_sockets.push_back(server_fd);
69+
return true;
70+
}
71+
72+
int NetworkSetup::acceptConnection(int server_fd)
73+
{
74+
struct sockaddr_in client_addr;
75+
socklen_t addr_len = sizeof(client_addr);
76+
77+
int client_fd = accept(server_fd,
78+
reinterpret_cast<struct sockaddr*>(&client_addr),
79+
&addr_len);
80+
81+
// TODO: verify correct return if client_fd is equal to `-1`
82+
if (client_fd < 0)
83+
return -1;
84+
85+
// Make client socket non-blocking
86+
int flags = 1;
87+
if (ioctl(client_fd, FIONBIO, &flags) < 0)
88+
{
89+
close(client_fd);
90+
return -1;
91+
}
92+
93+
return client_fd;
94+
}
95+
96+
const std::vector<int>& NetworkSetup::getServerSockets() const
97+
{
98+
return server_sockets;
99+
}

src/network/NetworkSetup.hpp

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// Created by vinicius on 11/3/24.
3+
//
4+
5+
#ifndef NETWORKSETUP_HPP
6+
#define NETWORKSETUP_HPP
7+
8+
#include <string>
9+
#include <vector>
10+
11+
class NetworkSetup
12+
{
13+
private:
14+
std::vector<int> server_sockets;
15+
16+
public:
17+
NetworkSetup();
18+
~NetworkSetup();
19+
20+
bool setupServer(const std::string& host, int port);
21+
static int acceptConnection(int server_fd);
22+
const std::vector<int>& getServerSockets() const;
23+
24+
private:
25+
// Prevent copying
26+
NetworkSetup(const NetworkSetup& other);
27+
NetworkSetup& operator=(const NetworkSetup& other);
28+
};
29+
30+
#endif // NETWORKSETUP_HPP

src/server/Server.cpp

+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// Created by vinicius on 11/3/24.
3+
//
4+
5+
#include "Server.hpp"
6+
#include <iostream>
7+
8+
Server_class::Server_class() : _is_running(false), port(0), client_max_size(0) {}
9+
10+
Server_class::~Server_class() {
11+
if (_is_running) {
12+
stop();
13+
}
14+
}
15+
16+
bool Server_class::start() {
17+
if (_is_running) {
18+
return false;
19+
}
20+
21+
// Validate server configuration
22+
if (host.empty() || port <= 0 || port > 65535) {
23+
std::cerr << "Invalid server configuration" << std::endl;
24+
return false;
25+
}
26+
27+
// Initialize network setup
28+
if (!_network.setupServer(host, port)) {
29+
std::cerr << "Failed to setup server on " << host << ":" << port << std::endl;
30+
return false;
31+
}
32+
33+
_is_running = true;
34+
return true;
35+
}
36+
37+
void Server_class::stop() {
38+
_is_running = false;
39+
}
40+
41+
bool Server_class::isRunning() const {
42+
return _is_running;
43+
}
44+
45+
int Server_class::acceptClient() {
46+
if (!_is_running) {
47+
return -1;
48+
}
49+
50+
const std::vector<int>& sockets = _network.getServerSockets();
51+
if (sockets.empty()) {
52+
return -1;
53+
}
54+
55+
// For now, we'll just use the first socket
56+
// You might want to implement a more sophisticated approach for multiple sockets
57+
return _network.acceptConnection(sockets[0]);
58+
}
59+
60+
const std::vector<int>& Server_class::getServerSockets() const {
61+
return _network.getServerSockets();
62+
}

src/server/Server.hpp

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
#ifndef SERVER_HPP
2+
#define SERVER_HPP
3+
4+
#include <string>
5+
#include <vector>
6+
#include <map>
7+
#include <exception>
8+
#include "../network/NetworkSetup.hpp"
9+
#include "../parser_config_file/ConfigTypes.hpp"
10+
11+
class Server_class
12+
{
13+
private:
14+
NetworkSetup _network;
15+
bool _is_running;
16+
Server_class(const Server_class& other);
17+
Server_class& operator=(const Server_class& other);
18+
19+
public:
20+
std::string host;
21+
int port;
22+
std::vector<std::string> server_names;
23+
size_t client_max_size;
24+
std::map<int, std::string> error_pages;
25+
std::vector<Location> locations;
26+
27+
Server_class();
28+
~Server_class();
29+
30+
bool start();
31+
void stop();
32+
bool isRunning() const;
33+
int acceptClient();
34+
const std::vector<int>& getServerSockets() const;
35+
36+
};
37+
38+
#endif // SERVER_HPP

0 commit comments

Comments
 (0)