Better encapsulation

This commit is contained in:
Baptiste Wicht 2016-09-28 23:24:27 +02:00
parent 048c5c445d
commit d8a7016d8d
No known key found for this signature in database
GPG Key ID: C5566B6C7F884532
4 changed files with 23 additions and 24 deletions

View File

@ -12,6 +12,7 @@
#include <atomic.hpp>
#include "net/ethernet_layer.hpp"
#include "net/connection_handler.hpp"
#include "net/ip_layer.hpp"
#include "net/network.hpp"
#include "net/socket.hpp"
@ -96,6 +97,10 @@ private:
std::expected<void> finalize_packet_direct(network::interface_descriptor& interface, network::ethernet::packet& p);
network::ip::layer* parent;
std::atomic<size_t> local_port;
network::connection_handler<network::tcp::tcp_connection> connections;
};
} // end of tcp namespace

View File

@ -9,10 +9,12 @@
#define NET_UDP_LAYER_H
#include <types.hpp>
#include <atomic.hpp>
#include "net/ethernet_layer.hpp"
#include "net/ip_layer.hpp"
#include "net/network.hpp"
#include "net/connection_handler.hpp"
namespace network {
@ -41,6 +43,17 @@ struct kernel_packet_descriptor {
network::ip::address target_ip;
};
struct udp_connection {
size_t local_port; ///< The local source port
size_t server_port; ///< The server port
network::ip::address server_address; ///< The server address
bool connected = false;
bool server = false;
network::socket* socket = nullptr;
};
struct layer {
layer(network::ip::layer* parent);
@ -109,6 +122,10 @@ private:
network::dns::layer* dns_layer;
network::dhcp::layer* dhcp_layer;
std::atomic<size_t> local_port;
network::connection_handler<udp_connection> connections;
};
} // end of upd namespace

View File

@ -6,11 +6,9 @@
//=======================================================================
#include <bit_field.hpp>
#include <atomic.hpp>
#include "conc/condition_variable.hpp"
#include "net/connection_handler.hpp"
#include "net/tcp_layer.hpp"
#include "net/dns_layer.hpp"
#include "net/checksum.hpp"
@ -23,10 +21,9 @@
namespace {
std::atomic<size_t> local_port;
static constexpr size_t timeout_ms = 1000;
static constexpr size_t max_tries = 5;
constexpr size_t default_tcp_header_length = 20;
using flag_data_offset = std::bit_field<uint16_t, uint8_t, 12, 4>;
using flag_reserved = std::bit_field<uint16_t, uint8_t, 9, 3>;
@ -40,8 +37,6 @@ using flag_rst = std::bit_field<uint16_t, uint8_t, 2, 1>;
using flag_syn = std::bit_field<uint16_t, uint8_t, 1, 1>;
using flag_fin = std::bit_field<uint16_t, uint8_t, 0, 1>;
network::connection_handler<network::tcp::tcp_connection> connections;
void compute_checksum(network::ethernet::packet& packet) {
auto* ip_header = reinterpret_cast<network::ip::header*>(packet.payload + packet.tag(1));
auto* tcp_header = reinterpret_cast<network::tcp::header*>(packet.payload + packet.tag(2));
@ -66,8 +61,6 @@ void compute_checksum(network::ethernet::packet& packet) {
tcp_header->checksum = switch_endian_16(network::checksum_finalize_nz(sum));
}
constexpr size_t default_tcp_header_length = 20;
uint16_t get_default_flags() {
uint16_t flags = 0; // By default

View File

@ -7,7 +7,6 @@
#include <atomic.hpp>
#include "net/connection_handler.hpp"
#include "net/udp_layer.hpp"
#include "net/dns_layer.hpp"
#include "net/dhcp_layer.hpp"
@ -19,21 +18,6 @@
namespace {
std::atomic<size_t> local_port;
struct udp_connection {
size_t local_port; ///< The local source port
size_t server_port; ///< The server port
network::ip::address server_address; ///< The server address
bool connected = false;
bool server = false;
network::socket* socket = nullptr;
};
network::connection_handler<udp_connection> connections;
void compute_checksum(network::ethernet::packet& packet){
auto* ip_header = reinterpret_cast<network::ip::header*>(packet.payload + packet.tag(1));
auto* udp_header = reinterpret_cast<network::udp::header*>(packet.payload + packet.tag(2));