Skip to content
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
1 change: 0 additions & 1 deletion include/Server/MultiSocketWebserver.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#pragma once

#include <cstring> // For strerror
#include <unordered_map>
#include <vector>

Expand Down
1 change: 0 additions & 1 deletion include/Server/PollFdManager.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ class PollFdManager {
pollfd* data();
[[nodiscard]] size_t size() const;
[[nodiscard]] std::vector<pollfd> getPolls() const;
[[nodiscard]] std::vector<pollfd> getShuffledPolls() const;

private:
PollFdManager() = default;
Expand Down
6 changes: 5 additions & 1 deletion include/Server/Socket.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ class Socket {
void setSocketOpt() const;
int _socketFd;
int _port;
ServerConfig& _default_config;
ServerConfig &_default_config;
std::vector<ServerConfig> _configs;
sockaddr_in _addr;
};

uint32_t my_inet_addr(const std::string &ipStr);

std::string my_inet_ntoa(const in_addr &in);
3 changes: 2 additions & 1 deletion src/Server/ClientConnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <sys/socket.h>
#include <unistd.h>

#include <Socket.hpp>
#include <algorithm> // For std::search
#include <array>
#include <cstring> // For strerror
Expand Down Expand Up @@ -41,7 +42,7 @@ ClientConnection::ClientConnection(const int clientFd, const sockaddr_in clientA
_clientAddr(clientAddr),
_requestHandler(_currentConfig) {
LOG_INFO(_log("New client connection established"));
LOG_INFO("Client address: " + std::string(inet_ntoa(_clientAddr.sin_addr)) +
LOG_INFO("Client address: " + std::string(my_inet_ntoa(_clientAddr.sin_addr)) +
" Port: " + std::to_string(ntohs(_clientAddr.sin_port)));

if (fcntl(_clientFd, F_SETFL, O_NONBLOCK) == -1) {
Expand Down
3 changes: 1 addition & 2 deletions src/Server/MultiSocketWebserver.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "MultiSocketWebserver.hpp"

#include <arpa/inet.h>
#include <sys/poll.h>
#include <sys/sysctl.h>
#include <unistd.h>
Expand Down Expand Up @@ -114,7 +113,7 @@ void MultiSocketWebserver::_acceptConnection(const int server_fd) {
_clients.emplace(clientFd,
std::make_unique<ClientConnection>(clientFd, clientAddr, _sockets.at(server_fd)->getConfig()));
_polls.addFd(clientFd);
LOG_INFO("Accepted connection from " + std::string(inet_ntoa(clientAddr.sin_addr)) + " on socket " +
LOG_INFO("Accepted connection from " + std::string(my_inet_ntoa(clientAddr.sin_addr)) + " on socket " +
std::to_string(clientFd));
} catch (const std::exception& e) {
LOG_ERROR("Failed to create ClientConnection: " + std::string(e.what()));
Expand Down
10 changes: 0 additions & 10 deletions src/Server/PollFdManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include <algorithm>
#include <random>

#include "Logger.hpp"

PollFdManager& PollFdManager::getInstance() {
static PollFdManager instance;
return instance;
Expand All @@ -23,11 +21,3 @@ pollfd* PollFdManager::data() { return _pollFds.data(); }
size_t PollFdManager::size() const { return _pollFds.size(); }

std::vector<pollfd> PollFdManager::getPolls() const { return _pollFds; }

std::vector<pollfd> PollFdManager::getShuffledPolls() const {
std::vector<pollfd> shuffledFds = _pollFds;
std::random_device rd;
std::mt19937 g(rd());
std::shuffle(shuffledFds.begin(), shuffledFds.end(), g);
return shuffledFds;
}
65 changes: 59 additions & 6 deletions src/Server/Socket.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include "Socket.hpp"

#include <arpa/inet.h>
#include <unistd.h>

#include <cstdio>
Expand Down Expand Up @@ -39,7 +38,7 @@ Socket::~Socket() {

void Socket::bind() {
LOG_DEBUG("Binding socket to IP " + _default_config.getHostIP() + " and port " + std::to_string(_port));
if (::bind(_socketFd, reinterpret_cast<sockaddr*>(&_addr), sizeof(_addr)) == -1) {
if (::bind(_socketFd, reinterpret_cast<sockaddr *>(&_addr), sizeof(_addr)) == -1) {
throw std::runtime_error("Bind failed on IP " + _default_config.getHostIP() + " and port " +
std::to_string(_port) + ": " + std::string(strerror(errno)));
}
Expand All @@ -56,10 +55,10 @@ void Socket::listen() const {
int Socket::getSocketFd() const { return _socketFd; }

void Socket::setupAddress() {
_addr = sockaddr_in{}; // Value-initialize sockaddr_in
_addr.sin_family = AF_INET; // IPv4
_addr.sin_addr.s_addr = inet_addr(_default_config.getHostIP().c_str()); // Convert IP address to network byte order
_addr.sin_port = htons(_port); // Convert port to network byte order
_addr = sockaddr_in{}; // Value-initialize sockaddr_in
_addr.sin_family = AF_INET; // IPv4
_addr.sin_addr.s_addr = my_inet_addr(_default_config.getHostIP()); // Convert IP address to network byte order
_addr.sin_port = htons(_port); // Convert port to network byte order
}

void Socket::setSocketOpt() const {
Expand All @@ -71,3 +70,57 @@ void Socket::setSocketOpt() const {
}

std::vector<ServerConfig> Socket::getConfig() const { return _configs; }

uint32_t my_inet_addr(const std::string &ipStr) {
uint32_t result = 0;
int partsCount = 0;

std::stringstream ss(ipStr);
std::string part;

// Split the input string by '.'
while (std::getline(ss, part, '.')) {
if (partsCount >= 4)
throw std::runtime_error("Invalid IPv4 address format: " + ipStr);
// Convert the current part to an integer [0..255]
int value = 0;
try {
value = std::stoi(part);
} catch (...) {
throw std::runtime_error("Invalid integer in IP address: " + part);
}
if (value < 0 || value > 255)
throw std::runtime_error("IP octet out of range (0-255): " + part);

// Shift the current result by 8 bits and add the new value
result = (result << 8) | static_cast<uint32_t>(value & 0xFF);
++partsCount;
}

// Make sure we had exactly 4 parts
if (partsCount != 4) {
throw std::runtime_error("Invalid IPv4 address format: " + ipStr);
}

return result;
}

std::string my_inet_ntoa(const in_addr &in) {
// The address is stored in network byte order (big-endian).
// For example, 192.168.0.1 -> 0xC0A80001 in memory.

uint32_t ip = in.s_addr; // 32-bit IP in network byte order

// Extract each octet by shifting and masking
unsigned char b1 = (ip >> 24) & 0xFF;
unsigned char b2 = (ip >> 16) & 0xFF;
unsigned char b3 = (ip >> 8) & 0xFF;
unsigned char b4 = ip & 0xFF;

// Build the dot-decimal string
std::ostringstream oss;
oss << static_cast<int>(b1) << "." << static_cast<int>(b2) << "." << static_cast<int>(b3) << "."
<< static_cast<int>(b4);

return oss.str();
}
Loading