Skip to content

Commit

Permalink
fix(Network): Fix socket address nullptr bug and add socket to IO mul…
Browse files Browse the repository at this point in the history
…tiplexer
  • Loading branch information
MasterLaplace committed Jan 9, 2024
1 parent 1c8aa3e commit 7595d02
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 4 deletions.
1 change: 1 addition & 0 deletions Flakkari/Network/IOMultiplexer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ void PSELECT::removeSocket(FileDescriptor socket)
throw std::runtime_error("Index out of range");
FD_CLR(socket, &_fds);
_sockets.erase(std::remove(_sockets.begin(), _sockets.end(), socket), _sockets.end());
_maxFd = *std::max_element(_sockets.begin(), _sockets.end());
}

int PSELECT::wait()
Expand Down
38 changes: 37 additions & 1 deletion Flakkari/Network/Socket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ Socket::Socket(std::shared_ptr<Address> address)
#endif

auto &addr = _address->getAddrInfo();

if (addr == nullptr) {
FLAKKARI_LOG_ERROR("Address is nullptr");
return;
}

_socket = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (_socket == INVALID_SOCKET) {
FLAKKARI_LOG_FATAL("Failed to create socket, error: " + STD_ERROR);
Expand Down Expand Up @@ -64,6 +70,12 @@ Socket::Socket(Address address)
#endif

auto &addr = _address->getAddrInfo();

if (addr == nullptr) {
FLAKKARI_LOG_ERROR("Address is nullptr");
return;
}

_socket = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (_socket == INVALID_SOCKET) {
FLAKKARI_LOG_FATAL("Failed to create socket, error: " + STD_ERROR);
Expand All @@ -88,6 +100,12 @@ Socket::Socket(ip_t ip, port_t port, Address::IpType ip_type, Address::SocketTyp
#endif

auto &addr = _address->getAddrInfo();

if (addr == nullptr) {
FLAKKARI_LOG_ERROR("Address is nullptr");
return;
}

_socket = ::socket(addr->ai_family, addr->ai_socktype, addr->ai_protocol);
if (_socket == INVALID_SOCKET) {
FLAKKARI_LOG_FATAL("Failed to create socket, error: " + STD_ERROR);
Expand Down Expand Up @@ -117,6 +135,9 @@ void Socket::bind()
{
auto &addr = _address->getAddrInfo();

if (addr == nullptr)
return FLAKKARI_LOG_ERROR("Address is nullptr"), void();

if (::bind(_socket, addr->ai_addr, addr->ai_addrlen) == SOCKET_ERROR) {
FLAKKARI_LOG_FATAL("Failed to bind socket, error: " + STD_ERROR);
return;
Expand All @@ -135,6 +156,9 @@ void Socket::connect()
{
auto &addr = _address->getAddrInfo();

if (addr == nullptr)
return FLAKKARI_LOG_ERROR("Address is nullptr"), void();

if (::connect(_socket, addr->ai_addr, addr->ai_addrlen) == SOCKET_ERROR) {
FLAKKARI_LOG_FATAL("Failed to connect socket, error: " + STD_ERROR);
return;
Expand All @@ -152,7 +176,9 @@ std::shared_ptr<Socket> Socket::accept()
return nullptr;
}

auto _ip_type = (clientAddr.ss_family == AF_INET) ? Address::IpType::IPv4 : (clientAddr.ss_family == AF_INET6) ? Address::IpType::IPv6 : Address::IpType::None;
auto _ip_type = (clientAddr.ss_family == AF_INET)
? Address::IpType::IPv4 : (clientAddr.ss_family == AF_INET6)
? Address::IpType::IPv6 : Address::IpType::None;

auto clientAddress = std::make_shared<Address>(clientAddr, _address->getSocketType(), _ip_type);
return std::make_shared<Socket>(clientSocket, clientAddress);
Expand All @@ -178,6 +204,9 @@ void Socket::sendTo(const std::shared_ptr<Address> &address, const Buffer &data,
{
auto &addr = address->getAddrInfo();

if (addr == nullptr)
return FLAKKARI_LOG_ERROR("Address is nullptr"), void();

if (::sendto(_socket, data.getData(), data.getSize(), flags, addr->ai_addr, addr->ai_addrlen) == SOCKET_ERROR) {
FLAKKARI_LOG_ERROR("Failed to send \"" + std::string(data) + "\" to \"" + address->toString().value_or("No address") + "\", error: " + STD_ERROR);
return;
Expand All @@ -188,6 +217,9 @@ void Socket::sendTo(const std::shared_ptr<Address> &address, const byte *data, c
{
auto &addr = address->getAddrInfo();

if (addr == nullptr)
return FLAKKARI_LOG_ERROR("Address is nullptr"), void();

if (::sendto(_socket, data, size, flags, addr->ai_addr, addr->ai_addrlen) == SOCKET_ERROR) {
FLAKKARI_LOG_ERROR(
"Failed to send \""
Expand Down Expand Up @@ -234,6 +266,7 @@ std::optional<std::pair<std::shared_ptr<Address>, Buffer>> Socket::receiveFrom(s
FLAKKARI_LOG_ERROR("Failed to receive data from \"" + _address->toString().value_or("No address") + "\", error: " + STD_ERROR);
return {};
}

auto _ip_type = (addr.ss_family == AF_INET)
? Address::IpType::IPv4 : (addr.ss_family == AF_INET6)
? Address::IpType::IPv6 : Address::IpType::None;
Expand All @@ -248,6 +281,8 @@ std::optional<std::pair<std::shared_ptr<Address>, Buffer>> Socket::receiveFrom(i
socklen_t addrlen = sizeof(addr);

if (::recvfrom(_socket, &data[0], 4096, flags, (sockaddr*)&addr, &addrlen) == SOCKET_ERROR) {
if (errno == EAGAIN || errno == EWOULDBLOCK)
return {};
FLAKKARI_LOG_ERROR("Failed to receive data from \"" + _address->toString().value_or("No address") + "\", error: " + STD_ERROR);
return {};
}
Expand Down Expand Up @@ -283,6 +318,7 @@ void Socket::setBlocking(bool blocking)

int result = ::fcntl(_socket, F_SETFL, flags | (blocking ? 0 : O_NONBLOCK));
#endif

if (result == SOCKET_ERROR)
FLAKKARI_LOG_ERROR("Failed to set socket to " + std::string(blocking?"":"non") + "blocking, error: " + STD_ERROR);
}
Expand Down
7 changes: 4 additions & 3 deletions Flakkari/Server/UDPServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,17 @@ UDPServer::UDPServer(std::string ip, std::size_t port) :

_io = std::make_unique<Network::PSELECT>();
_io->addSocket(_socket.getSocket());
_io->addSocket(STDIN_FILENO);
}

bool UDPServer::handleTimeout(int event)
{
if (event != 0)
return false;
FLAKKARI_LOG_DEBUG("ppoll timed out");
ClientManager::checkInactiveClients();
FLAKKARI_LOG_DEBUG("ppoll timed out");
ClientManager::checkInactiveClients();
return true;
}
}

bool UDPServer::handleInput(int fd)
{
Expand Down

0 comments on commit 7595d02

Please sign in to comment.