mirror of
https://github.com/wichtounet/thor-os.git
synced 2025-09-13 22:47:15 -04:00
Store the UDP "connections"
This commit is contained in:
parent
0e5e27c9bb
commit
ef8182271e
@ -159,19 +159,19 @@ private:
|
|||||||
bool writer = false; ///< Boolean flag indicating if there is a writer
|
bool writer = false; ///< Boolean flag indicating if there is a writer
|
||||||
};
|
};
|
||||||
|
|
||||||
void writer_rw_lock::lock(){
|
inline void writer_rw_lock::lock(){
|
||||||
l.read_lock();
|
l.read_lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void writer_rw_lock::unlock(){
|
inline void writer_rw_lock::unlock(){
|
||||||
l.read_unlock();
|
l.read_unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void reader_rw_lock::lock(){
|
inline void reader_rw_lock::lock(){
|
||||||
l.write_lock();
|
l.write_lock();
|
||||||
}
|
}
|
||||||
|
|
||||||
void reader_rw_lock::unlock(){
|
inline void reader_rw_lock::unlock(){
|
||||||
l.write_unlock();
|
l.write_unlock();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -8,6 +8,8 @@
|
|||||||
#ifndef NET_CONNECTION_HANDLER_H
|
#ifndef NET_CONNECTION_HANDLER_H
|
||||||
#define NET_CONNECTION_HANDLER_H
|
#define NET_CONNECTION_HANDLER_H
|
||||||
|
|
||||||
|
#include <list.hpp>
|
||||||
|
|
||||||
#include "conc/rw_lock.hpp"
|
#include "conc/rw_lock.hpp"
|
||||||
|
|
||||||
namespace network {
|
namespace network {
|
||||||
@ -33,11 +35,7 @@ struct connection_handler {
|
|||||||
auto lock = connections_lock.writer_lock();
|
auto lock = connections_lock.writer_lock();
|
||||||
std::lock_guard<writer_rw_lock> l(lock);
|
std::lock_guard<writer_rw_lock> l(lock);
|
||||||
|
|
||||||
auto& connection = connections.emplace_back();
|
return connections.emplace_back();
|
||||||
|
|
||||||
connection.listening = false;
|
|
||||||
|
|
||||||
return connection;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void remove_connection(connection_type& connection) {
|
void remove_connection(connection_type& connection) {
|
||||||
|
@ -54,6 +54,10 @@ struct tcp_connection {
|
|||||||
uint32_t seq_number = 0; ///< The next sequence number
|
uint32_t seq_number = 0; ///< The next sequence number
|
||||||
|
|
||||||
network::socket* socket = nullptr;
|
network::socket* socket = nullptr;
|
||||||
|
|
||||||
|
tcp_connection(){
|
||||||
|
listening = false;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
network::connection_handler<tcp_connection> connections;
|
network::connection_handler<tcp_connection> connections;
|
||||||
|
@ -5,14 +5,29 @@
|
|||||||
// http://www.opensource.org/licenses/MIT)
|
// http://www.opensource.org/licenses/MIT)
|
||||||
//=======================================================================
|
//=======================================================================
|
||||||
|
|
||||||
|
#include "net/connection_handler.hpp"
|
||||||
#include "net/udp_layer.hpp"
|
#include "net/udp_layer.hpp"
|
||||||
#include "net/dns_layer.hpp"
|
#include "net/dns_layer.hpp"
|
||||||
#include "net/checksum.hpp"
|
#include "net/checksum.hpp"
|
||||||
|
|
||||||
|
#include "tlib/errors.hpp"
|
||||||
|
|
||||||
#include "kernel_utils.hpp"
|
#include "kernel_utils.hpp"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
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;
|
||||||
|
|
||||||
|
network::socket* socket = nullptr;
|
||||||
|
};
|
||||||
|
|
||||||
|
network::connection_handler<udp_connection> connections;
|
||||||
|
|
||||||
void compute_checksum(network::ethernet::packet& packet){
|
void compute_checksum(network::ethernet::packet& packet){
|
||||||
auto* ip_header = reinterpret_cast<network::ip::header*>(packet.payload + packet.tag(1));
|
auto* ip_header = reinterpret_cast<network::ip::header*>(packet.payload + packet.tag(1));
|
||||||
auto* udp_header = reinterpret_cast<network::udp::header*>(packet.payload + packet.index);
|
auto* udp_header = reinterpret_cast<network::udp::header*>(packet.payload + packet.index);
|
||||||
@ -109,10 +124,38 @@ std::expected<void> network::udp::finalize_packet(network::interface_descriptor&
|
|||||||
return network::ip::finalize_packet(interface, p);
|
return network::ip::finalize_packet(interface, p);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::expected<void> network::udp::client_bind(network::socket& socket, network::interface_descriptor& interface, size_t local_port, size_t server_port, network::ip::address server){
|
std::expected<void> network::udp::client_bind(network::socket& sock, network::interface_descriptor& interface, size_t local_port, size_t server_port, network::ip::address server){
|
||||||
|
// Create the connection
|
||||||
|
|
||||||
|
auto& connection = connections.create_connection();
|
||||||
|
|
||||||
|
connection.local_port = local_port;
|
||||||
|
connection.server_port = server_port;
|
||||||
|
connection.server_address = server;
|
||||||
|
|
||||||
|
// Link the socket and connection
|
||||||
|
sock.connection_data = &connection;
|
||||||
|
connection.socket = &sock;
|
||||||
|
|
||||||
|
// Mark the connection as connected
|
||||||
|
|
||||||
|
connection.connected = true;
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
|
||||||
std::expected<void> network::udp::client_unbind(network::socket& socket){
|
std::expected<void> network::udp::client_unbind(network::socket& sock){
|
||||||
|
auto& connection = sock.get_connection_data<udp_connection>();
|
||||||
|
|
||||||
|
if(!connection.connected){
|
||||||
|
return std::make_unexpected<void>(std::ERROR_SOCKET_NOT_CONNECTED);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark the connection as not connected
|
||||||
|
|
||||||
|
connection.connected = false;
|
||||||
|
|
||||||
|
connections.remove_connection(connection);
|
||||||
|
|
||||||
|
return {};
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user