Skip to content
Merged
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
15 changes: 15 additions & 0 deletions api/net/tcp/tcp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,21 @@ namespace net {
*/
void transmit(tcp::Packet_ptr);

/**
* @brief Creates an outgoing TCP packet.
*
* @return A tcp packet ptr
*/
tcp::Packet_ptr create_outgoing_packet();

/**
* @brief Sends a TCP reset based on the values of the incoming packet.
* Used when packet are addressed to closed ports or already dead connections.
*
* @param[in] incoming The incoming tcp packet "to reset".
*/
void send_reset(const tcp::Packet& incoming);

/**
* @brief Generate a unique initial sequence number (ISS).
*
Expand Down
8 changes: 3 additions & 5 deletions src/net/tcp/connection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -318,11 +318,9 @@ Connection::~Connection() {
rtx_clear();
}

Packet_ptr Connection::create_outgoing_packet() {
auto packet = static_unique_ptr_cast<net::tcp::Packet>((host_.inet_).create_packet());
//auto packet = std::static_pointer_cast<TCP::Packet>(create_packet());

packet->init();
Packet_ptr Connection::create_outgoing_packet()
{
auto packet = host_.create_outgoing_packet();
// Set Source (local == the current connection)
packet->set_source(local());
// Set Destination (remote)
Expand Down
7 changes: 7 additions & 0 deletions src/net/tcp/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ void Listener::segment_arrived(Packet_ptr packet) {
// if it's a new attempt (SYN)
else
{
// don't waste time if the packet does not have SYN
if(UNLIKELY(not packet->isset(SYN)))
{
host_.send_reset(*packet);
return;
}

// Stat increment number of connection attempts
host_.connection_attempts_++;

Expand Down
24 changes: 24 additions & 0 deletions src/net/tcp/tcp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ void TCP::receive(net::Packet_ptr packet_ptr) {
return;
}

// Send a reset
send_reset(*packet);

drop(*packet);
}

Expand Down Expand Up @@ -263,6 +266,27 @@ void TCP::transmit(tcp::Packet_ptr packet) {
_network_layer_out(std::move(packet));
}

tcp::Packet_ptr TCP::create_outgoing_packet()
{
auto packet = static_unique_ptr_cast<net::tcp::Packet>(inet_.create_packet());
packet->init();
return packet;
}

void TCP::send_reset(const tcp::Packet& in)
{
// TODO: maybe worth to just swap the fields in
// the incoming packet and send that one
auto out = create_outgoing_packet();
// increase incoming SEQ and ACK by 1 and set RST + ACK
out->set_seq(in.ack()+1).set_ack(in.seq()+1).set_flags(RST | ACK);
// swap dest and src
out->set_source(in.destination());
out->set_destination(in.source());

transmit(std::move(out));
}

seq_t TCP::generate_iss() {
// Do something to get a iss.
return rand();
Expand Down