2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-03-14 14:20:38 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
#include <AK/Function.h>
|
2019-09-08 10:38:08 +03:00
|
|
|
#include <AK/HashMap.h>
|
|
|
|
#include <AK/SinglyLinkedList.h>
|
2019-08-09 05:34:32 +03:00
|
|
|
#include <AK/WeakPtr.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/IPv4Socket.h>
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-09-08 12:35:41 +03:00
|
|
|
class TCPSocket final : public IPv4Socket
|
|
|
|
, public Weakable<TCPSocket> {
|
2019-03-14 14:20:38 +03:00
|
|
|
public:
|
2020-04-18 12:50:35 +03:00
|
|
|
static void for_each(Function<void(const TCPSocket&)>);
|
2019-06-21 19:37:47 +03:00
|
|
|
static NonnullRefPtr<TCPSocket> create(int protocol);
|
2019-03-14 14:20:38 +03:00
|
|
|
virtual ~TCPSocket() override;
|
|
|
|
|
2019-08-09 05:48:28 +03:00
|
|
|
enum class Direction {
|
|
|
|
Unspecified,
|
|
|
|
Outgoing,
|
|
|
|
Incoming,
|
|
|
|
Passive,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* to_string(Direction direction)
|
|
|
|
{
|
|
|
|
switch (direction) {
|
|
|
|
case Direction::Unspecified:
|
|
|
|
return "Unspecified";
|
|
|
|
case Direction::Outgoing:
|
|
|
|
return "Outgoing";
|
|
|
|
case Direction::Incoming:
|
|
|
|
return "Incoming";
|
|
|
|
case Direction::Passive:
|
|
|
|
return "Passive";
|
|
|
|
default:
|
|
|
|
return "None";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-07 18:13:23 +03:00
|
|
|
enum class State {
|
2019-08-06 16:40:38 +03:00
|
|
|
Closed,
|
|
|
|
Listen,
|
|
|
|
SynSent,
|
|
|
|
SynReceived,
|
|
|
|
Established,
|
|
|
|
CloseWait,
|
|
|
|
LastAck,
|
|
|
|
FinWait1,
|
|
|
|
FinWait2,
|
|
|
|
Closing,
|
|
|
|
TimeWait,
|
2019-03-14 14:20:38 +03:00
|
|
|
};
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
static const char* to_string(State state)
|
|
|
|
{
|
|
|
|
switch (state) {
|
|
|
|
case State::Closed:
|
|
|
|
return "Closed";
|
|
|
|
case State::Listen:
|
|
|
|
return "Listen";
|
|
|
|
case State::SynSent:
|
|
|
|
return "SynSent";
|
|
|
|
case State::SynReceived:
|
|
|
|
return "SynReceived";
|
|
|
|
case State::Established:
|
|
|
|
return "Established";
|
|
|
|
case State::CloseWait:
|
|
|
|
return "CloseWait";
|
|
|
|
case State::LastAck:
|
|
|
|
return "LastAck";
|
|
|
|
case State::FinWait1:
|
|
|
|
return "FinWait1";
|
|
|
|
case State::FinWait2:
|
|
|
|
return "FinWait2";
|
|
|
|
case State::Closing:
|
|
|
|
return "Closing";
|
|
|
|
case State::TimeWait:
|
|
|
|
return "TimeWait";
|
|
|
|
default:
|
|
|
|
return "None";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-10 06:18:18 +03:00
|
|
|
enum class Error {
|
|
|
|
None,
|
|
|
|
FINDuringConnect,
|
|
|
|
RSTDuringConnect,
|
|
|
|
UnexpectedFlagsDuringConnect,
|
|
|
|
};
|
|
|
|
|
|
|
|
static const char* to_string(Error error)
|
|
|
|
{
|
|
|
|
switch (error) {
|
|
|
|
case Error::None:
|
|
|
|
return "None";
|
|
|
|
case Error::FINDuringConnect:
|
|
|
|
return "FINDuringConnect";
|
|
|
|
case Error::RSTDuringConnect:
|
|
|
|
return "RSTDuringConnect";
|
|
|
|
case Error::UnexpectedFlagsDuringConnect:
|
|
|
|
return "UnexpectedFlagsDuringConnect";
|
|
|
|
default:
|
|
|
|
return "Invalid";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:20:38 +03:00
|
|
|
State state() const { return m_state; }
|
2019-08-10 06:14:00 +03:00
|
|
|
void set_state(State state);
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-08-09 05:48:28 +03:00
|
|
|
Direction direction() const { return m_direction; }
|
|
|
|
|
2019-08-10 06:18:18 +03:00
|
|
|
bool has_error() const { return m_error != Error::None; }
|
|
|
|
Error error() const { return m_error; }
|
|
|
|
void set_error(Error error) { m_error = error; }
|
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
void set_ack_number(u32 n) { m_ack_number = n; }
|
|
|
|
void set_sequence_number(u32 n) { m_sequence_number = n; }
|
|
|
|
u32 ack_number() const { return m_ack_number; }
|
|
|
|
u32 sequence_number() const { return m_sequence_number; }
|
2019-08-08 05:32:35 +03:00
|
|
|
u32 packets_in() const { return m_packets_in; }
|
|
|
|
u32 bytes_in() const { return m_bytes_in; }
|
|
|
|
u32 packets_out() const { return m_packets_out; }
|
|
|
|
u32 bytes_out() const { return m_bytes_out; }
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2020-01-29 14:27:42 +03:00
|
|
|
void send_tcp_packet(u16 flags, const void* = nullptr, size_t = 0);
|
2019-09-08 10:38:08 +03:00
|
|
|
void send_outgoing_packets();
|
|
|
|
void receive_tcp_packet(const TCPPacket&, u16 size);
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
static Lockable<HashMap<IPv4SocketTuple, TCPSocket*>>& sockets_by_tuple();
|
2019-09-08 10:16:40 +03:00
|
|
|
static RefPtr<TCPSocket> from_tuple(const IPv4SocketTuple& tuple);
|
|
|
|
static RefPtr<TCPSocket> from_endpoints(const IPv4Address& local_address, u16 local_port, const IPv4Address& peer_address, u16 peer_port);
|
2019-03-14 14:28:30 +03:00
|
|
|
|
2020-02-08 17:52:32 +03:00
|
|
|
static Lockable<HashMap<IPv4SocketTuple, RefPtr<TCPSocket>>>& closing_sockets();
|
|
|
|
|
2019-09-08 10:16:40 +03:00
|
|
|
RefPtr<TCPSocket> create_client(const IPv4Address& local_address, u16 local_port, const IPv4Address& peer_address, u16 peer_port);
|
2019-09-08 12:35:41 +03:00
|
|
|
void set_originator(TCPSocket& originator) { m_originator = originator.make_weak_ptr(); }
|
|
|
|
bool has_originator() { return !!m_originator; }
|
2019-09-08 10:18:28 +03:00
|
|
|
void release_to_originator();
|
|
|
|
void release_for_accept(RefPtr<TCPSocket>);
|
2019-08-09 05:48:28 +03:00
|
|
|
|
2020-06-02 19:20:05 +03:00
|
|
|
virtual KResult close() override;
|
2020-02-08 17:52:32 +03:00
|
|
|
|
2019-08-09 05:48:28 +03:00
|
|
|
protected:
|
|
|
|
void set_direction(Direction direction) { m_direction = direction; }
|
|
|
|
|
2019-03-14 14:20:38 +03:00
|
|
|
private:
|
|
|
|
explicit TCPSocket(int protocol);
|
2019-05-03 21:42:43 +03:00
|
|
|
virtual const char* class_name() const override { return "TCPSocket"; }
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
static NetworkOrdered<u16> compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket&, u16 payload_size);
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2020-02-08 17:52:32 +03:00
|
|
|
virtual void shut_down_for_writing() override;
|
|
|
|
|
2019-08-04 22:07:50 +03:00
|
|
|
virtual int protocol_receive(const KBuffer&, void* buffer, size_t buffer_size, int flags) override;
|
2020-01-29 14:27:42 +03:00
|
|
|
virtual int protocol_send(const void*, size_t) override;
|
2019-06-07 10:36:51 +03:00
|
|
|
virtual KResult protocol_connect(FileDescription&, ShouldBlock) override;
|
2019-05-04 17:40:34 +03:00
|
|
|
virtual int protocol_allocate_local_port() override;
|
2019-03-14 17:23:32 +03:00
|
|
|
virtual bool protocol_is_disconnected() const override;
|
2019-05-03 22:51:40 +03:00
|
|
|
virtual KResult protocol_bind() override;
|
2019-08-06 16:40:38 +03:00
|
|
|
virtual KResult protocol_listen() override;
|
2019-03-14 14:20:38 +03:00
|
|
|
|
2019-09-08 12:35:41 +03:00
|
|
|
WeakPtr<TCPSocket> m_originator;
|
2019-09-08 10:18:28 +03:00
|
|
|
HashMap<IPv4SocketTuple, NonnullRefPtr<TCPSocket>> m_pending_release_for_accept;
|
2019-08-09 05:48:28 +03:00
|
|
|
Direction m_direction { Direction::Unspecified };
|
2019-08-10 06:18:18 +03:00
|
|
|
Error m_error { Error::None };
|
2020-02-08 02:19:46 +03:00
|
|
|
RefPtr<NetworkAdapter> m_adapter;
|
2019-07-03 22:17:35 +03:00
|
|
|
u32 m_sequence_number { 0 };
|
|
|
|
u32 m_ack_number { 0 };
|
2019-08-06 16:40:38 +03:00
|
|
|
State m_state { State::Closed };
|
2019-08-08 05:32:35 +03:00
|
|
|
u32 m_packets_in { 0 };
|
|
|
|
u32 m_bytes_in { 0 };
|
|
|
|
u32 m_packets_out { 0 };
|
|
|
|
u32 m_bytes_out { 0 };
|
2019-09-08 10:38:08 +03:00
|
|
|
|
|
|
|
struct OutgoingPacket {
|
2020-02-08 03:43:55 +03:00
|
|
|
u32 ack_number { 0 };
|
2019-09-08 10:38:08 +03:00
|
|
|
ByteBuffer buffer;
|
|
|
|
int tx_counter { 0 };
|
2020-02-08 03:43:55 +03:00
|
|
|
timeval tx_time { 0, 0 };
|
2019-09-08 10:38:08 +03:00
|
|
|
};
|
|
|
|
|
2019-11-23 23:44:32 +03:00
|
|
|
Lock m_not_acked_lock { "TCPSocket unacked packets" };
|
2019-09-08 10:38:08 +03:00
|
|
|
SinglyLinkedList<OutgoingPacket> m_not_acked;
|
2019-03-14 14:20:38 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|