finalize_packet may fail

This commit is contained in:
Baptiste Wicht 2016-09-17 20:19:35 +02:00
parent 42cb0a97a8
commit 2f0ee33ba9
14 changed files with 47 additions and 35 deletions

View File

@ -24,7 +24,8 @@ void decode(network::interface_descriptor& interface, network::ethernet::packet&
std::expected<network::ethernet::packet> prepare_packet_query(network::interface_descriptor& interface, network::ip::address target_ip, uint16_t source_port, uint16_t identification, size_t payload_size);
std::expected<network::ethernet::packet> prepare_packet_query(char* buffer, network::interface_descriptor& interface, network::ip::address target_ip, uint16_t source_port, uint16_t identification, size_t payload_size);
void finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p);
std::expected<void> finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p);
} // end of dns namespace

View File

@ -28,7 +28,8 @@ void decode(network::interface_descriptor& interface, packet& packet);
std::expected<packet> prepare_packet(network::interface_descriptor& interface, size_t size, size_t destination, ether_type type);
std::expected<packet> prepare_packet(char* buffer, network::interface_descriptor& interface, size_t size, size_t destination, ether_type type);
void finalize_packet(network::interface_descriptor& interface, packet& p);
std::expected<void> finalize_packet(network::interface_descriptor& interface, packet& p);
} // end of ethernet namespace

View File

@ -26,7 +26,8 @@ void decode(network::interface_descriptor& interface, network::ethernet::packet&
std::expected<network::ethernet::packet> prepare_packet(network::interface_descriptor& interface, network::ip::address target_ip, size_t payload_size, type t, size_t code);
std::expected<network::ethernet::packet> prepare_packet(char* buffer, network::interface_descriptor& interface, network::ip::address target_ip, size_t payload_size, type t, size_t code);
void finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p);
std::expected<void> finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p);
void ping(network::interface_descriptor& interface, network::ip::address addr);

View File

@ -30,7 +30,7 @@ void decode(network::interface_descriptor& interface, network::ethernet::packet&
std::expected<network::ethernet::packet> prepare_packet(network::interface_descriptor& interface, size_t size, address& destination, size_t protocol);
std::expected<network::ethernet::packet> prepare_packet(char* buffer, network::interface_descriptor& interface, size_t size, address& destination, size_t protocol);
void finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p);
std::expected<void> finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p);
} // end of ip namespace

View File

@ -24,8 +24,8 @@ void decode(network::interface_descriptor& interface, network::ethernet::packet&
std::expected<network::ethernet::packet> prepare_packet(network::interface_descriptor& interface, network::ip::address target_ip, size_t source, size_t target, size_t payload_size);
std::expected<network::ethernet::packet> prepare_packet(char* buffer, network::socket& socket, size_t payload_size);
void finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p);
void finalize_packet(network::interface_descriptor& interface, network::socket& socket, network::ethernet::packet& p);
std::expected<void> finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p);
std::expected<void> finalize_packet(network::interface_descriptor& interface, network::socket& socket, network::ethernet::packet& p);
std::expected<void> connect(network::socket& socket, network::interface_descriptor& interface, size_t local_port, size_t server_port, network::ip::address server);
std::expected<void> disconnect(network::socket& socket);

View File

@ -29,7 +29,7 @@ void decode(network::interface_descriptor& interface, network::ethernet::packet&
std::expected<network::ethernet::packet> prepare_packet(network::interface_descriptor& interface, network::ip::address target_ip, size_t source, size_t target, size_t payload_size);
std::expected<network::ethernet::packet> prepare_packet(char* buffer, network::interface_descriptor& interface, network::ip::address target_ip, size_t source, size_t target, size_t payload_size);
void finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p);
std::expected<void> finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p);
} // end of upd namespace

View File

@ -214,9 +214,9 @@ std::expected<network::ethernet::packet> network::dns::prepare_packet_query(char
return packet;
}
void network::dns::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p) {
std::expected<void> network::dns::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p) {
p.index -= sizeof(header);
// Give the packet to the UDP layer for finalization
network::udp::finalize_packet(interface, p);
return network::udp::finalize_packet(interface, p);
}

View File

@ -144,7 +144,7 @@ std::expected<network::ethernet::packet> network::ethernet::prepare_packet(char*
return p;
}
void network::ethernet::finalize_packet(network::interface_descriptor& interface, packet& p){
std::expected<void> network::ethernet::finalize_packet(network::interface_descriptor& interface, packet& p){
if(p.user){
// The packet will be handled by a kernel thread, needs to
// be copied to kernel memory
@ -160,4 +160,6 @@ void network::ethernet::finalize_packet(network::interface_descriptor& interface
} else {
interface.send(p);
}
return {};
}

View File

@ -136,7 +136,7 @@ std::expected<network::ethernet::packet> network::icmp::prepare_packet(char* buf
return packet;
}
void network::icmp::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& packet){
std::expected<void> network::icmp::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& packet){
packet.index -= sizeof(header) - sizeof(uint32_t);
auto* icmp_header = reinterpret_cast<header*>(packet.payload + packet.index);
@ -145,7 +145,7 @@ void network::icmp::finalize_packet(network::interface_descriptor& interface, ne
compute_checksum(icmp_header, 0);
// Give the packet to the IP layer for finalization
network::ip::finalize_packet(interface, packet);
return network::ip::finalize_packet(interface, packet);
}
void network::icmp::ping(network::interface_descriptor& interface, network::ip::address target_ip){

View File

@ -186,7 +186,7 @@ std::expected<network::ethernet::packet> network::ip::prepare_packet(char* buffe
return packet;
}
void network::ip::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p){
std::expected<void> network::ip::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p){
// Send the packet to the ethernet layer
network::ethernet::finalize_packet(interface, p);
return network::ethernet::finalize_packet(interface, p);
}

View File

@ -345,24 +345,23 @@ std::expected<void> network::finalize_packet(socket_fd_t socket_fd, size_t packe
auto& packet = socket.get_packet(packet_fd);
auto& interface = network::interface(packet.interface);
auto check_and_return = [&socket, &packet_fd](const std::expected<void>& ret){
if(ret){
socket.erase_packet(packet_fd);
}
return ret;
};
switch(socket.protocol){
case network::socket_protocol::ICMP:
network::icmp::finalize_packet(interface, packet);
socket.erase_packet(packet_fd);
return std::make_expected();
return check_and_return(network::icmp::finalize_packet(interface, packet));
case network::socket_protocol::TCP:
network::tcp::finalize_packet(interface, socket, packet);
socket.erase_packet(packet_fd);
return std::make_expected();
return check_and_return(network::tcp::finalize_packet(interface, packet));
case network::socket_protocol::DNS:
network::dns::finalize_packet(interface, packet);
socket.erase_packet(packet_fd);
return std::make_expected();
return check_and_return(network::dns::finalize_packet(interface, packet));
}
return std::make_unexpected<void>(std::ERROR_SOCKET_UNIMPLEMENTED);

View File

@ -297,17 +297,17 @@ std::expected<network::ethernet::packet> network::tcp::prepare_packet(char* buff
return packet;
}
void network::tcp::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p) {
std::expected<void> network::tcp::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p) {
p.index -= sizeof(header);
// Compute the checksum
compute_checksum(p);
// Give the packet to the IP layer for finalization
network::ip::finalize_packet(interface, p);
return network::ip::finalize_packet(interface, p);
}
void network::tcp::finalize_packet(network::interface_descriptor& interface, network::socket& socket, network::ethernet::packet& p) {
std::expected<void> network::tcp::finalize_packet(network::interface_descriptor& interface, network::socket& socket, network::ethernet::packet& p) {
p.index -= sizeof(header);
// Compute the checksum
@ -315,15 +315,14 @@ void network::tcp::finalize_packet(network::interface_descriptor& interface, net
if (!p.user) {
logging::logf(logging::log_level::ERROR, "tcp: Function uniquely implemented for user packets!\n");
return; //TODO Fail
return std::make_unexpected<void>(std::ERROR_SOCKET_UNIMPLEMENTED);
}
auto& connection = socket.get_data<tcp_connection>();
// Make sure stream sockets are connected
if(!connection.connected){
//TODO return std::make_unexpected<void>(std::ERROR_SOCKET_NOT_CONNECTED);
return;
return std::make_unexpected<void>(std::ERROR_SOCKET_NOT_CONNECTED);
}
connection.listening = true;
@ -335,7 +334,11 @@ void network::tcp::finalize_packet(network::interface_descriptor& interface, net
for(size_t t = 0; t < max_tries; ++t){
// Give the packet to the IP layer for finalization
network::ip::finalize_packet(interface, p);
auto result = network::ip::finalize_packet(interface, p);
if(!result){
return result;
}
auto before = timer::milliseconds();
auto after = before;
@ -387,8 +390,10 @@ void network::tcp::finalize_packet(network::interface_descriptor& interface, net
// Set the future sequence and acknowledgement numbers
connection.seq_number = ack;
connection.ack_number = seq;
return {};
} else {
//TODO We need to be able to make finalize fail!
return std::make_unexpected<void>(std::ERROR_SOCKET_TCP_ERROR);
}
}

View File

@ -97,7 +97,7 @@ std::expected<network::ethernet::packet> network::udp::prepare_packet(char* buff
return packet;
}
void network::udp::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p){
std::expected<void> network::udp::finalize_packet(network::interface_descriptor& interface, network::ethernet::packet& p){
p.index -= sizeof(header);
// Compute the checksum

View File

@ -44,6 +44,7 @@ constexpr const size_t ERROR_SOCKET_INVALID_PACKET_DESCRIPTOR = 29;
constexpr const size_t ERROR_SOCKET_INVALID_TYPE_PROTOCOL = 30;
constexpr const size_t ERROR_SOCKET_NOT_CONNECTED = 31;
constexpr const size_t ERROR_SOCKET_INVALID_CONNECTION = 32;
constexpr const size_t ERROR_SOCKET_TCP_ERROR = 33;
inline const char* error_message(size_t error){
switch(error){
@ -111,6 +112,8 @@ inline const char* error_message(size_t error){
return "The socket is not connected";
case ERROR_SOCKET_INVALID_CONNECTION:
return "Issue with the internal connection";
case ERROR_SOCKET_TCP_ERROR:
return "TCP packet was not acknowledged";
default:
return "Unknonwn error";
}