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

feat: add transport interface and tcp/udp sockets #34

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
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: 1 addition & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Checks: "
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-missing-std-forward,
-cppcoreguidelines-pro-type-vararg,
"
WarningsAsErrors: true
HeaderFilterRegex: ".*"
Expand Down
52 changes: 52 additions & 0 deletions include/runtime/cfdp_runtime/socket.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
#pragma once

#include <arpa/inet.h>
#include <sys/socket.h>

#include <cstdint>
#include <string>
#include <vector>

namespace cfdp::runtime::sockets
{
enum class SocketType
{
TCP = SOCK_STREAM,
UDP = SOCK_DGRAM,
};

class Socket
{
public:
Socket(SocketType sType, uint16_t port, const std::string& ipAddr);

int handle;
sockaddr_in addr{};
};

class UDPClient
{
public:
UDPClient(uint16_t port = 8000, const std::string& ipAddr = "127.0.0.1")
: socket(SocketType::UDP, port, ipAddr)
{}

void sendMessage(std::vector<uint8_t> message);

private:
Socket socket;
};

class UDPServer
{
public:
UDPServer(uint16_t port = 8000, const std::string& ipAddr = "0.0.0.0")
: socket(SocketType::UDP, port, ipAddr)
{}

void socketBind();

private:
Socket socket;
};
} // namespace cfdp::runtime::sockets
5 changes: 5 additions & 0 deletions include/runtime/cfdp_runtime/transport_interface.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

namespace cfdp::runtime::transport
{
}
77 changes: 77 additions & 0 deletions src/cfdp_runtime/socket.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#include <cfdp_core/utils.hpp>
#include <cfdp_runtime/socket.hpp>

#include <fcntl.h>

#include <bit>
#include <stdexcept>
#include <sys/fcntl.h>

namespace
{
// IP converter related retcodes.
constexpr int invalid_addr_in_addr_family = 0;

// File descriptor related retcodes.
constexpr int file_descriptor_error = -1;

// Socket related retcodes.
constexpr int socket_error = -1;
} // namespace

cfdp::runtime::sockets::Socket::Socket(SocketType sType, uint16_t port, const std::string& ipAddr)
: handle(socket(AF_INET, utils::toUnderlying(sType), 0))
{
if (handle == socket_error)
{
throw std::runtime_error{"Could not create a socket."};
}

addr.sin_family = AF_INET;
addr.sin_port = port;

auto result = inet_pton(AF_INET, ipAddr.c_str(), &addr.sin_addr);

if (result == invalid_addr_in_addr_family)
{
throw std::runtime_error{"Could not parse passed IP address."};
}

auto currentFlags = fcntl(handle, F_GETFL);

if (currentFlags == file_descriptor_error)
{
throw std::runtime_error{"Could not fetch file descriptor flags."};
}

auto status = fcntl(handle, F_SETFL, currentFlags | O_NONBLOCK);

if (status == file_descriptor_error)
{
throw std::runtime_error{"Could not set the socket to non-blocking."};
}
}

void cfdp::runtime::sockets::UDPClient::sendMessage(std::vector<uint8_t> message)
{
auto length = message.size();
auto addr = std::bit_cast<sockaddr*>(&socket.addr);

auto result = sendto(socket.handle, message.data(), length, 0, addr, sizeof(socket.addr));

if (result == socket_error)
{
throw std::runtime_error{"Could not send data via socket."};
}
}

void cfdp::runtime::sockets::UDPServer::socketBind()
{
auto addr = std::bit_cast<sockaddr*>(&socket.addr);
auto result = bind(socket.handle, addr, sizeof(socket.addr));

if (result == socket_error)
{
throw std::runtime_error{"Could not bind the socket."};
}
}