2019-03-12 14:51:42 +00:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-13 15:05:56 +00:00
|
|
|
#include <AK/HashMap.h>
|
2019-03-12 16:27:07 +00:00
|
|
|
#include <AK/SinglyLinkedList.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
2019-08-06 13:40:38 +00:00
|
|
|
#include <Kernel/KBuffer.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <Kernel/Lock.h>
|
|
|
|
#include <Kernel/Net/IPv4.h>
|
2019-08-06 13:40:38 +00:00
|
|
|
#include <Kernel/Net/IPv4SocketTuple.h>
|
2019-05-28 09:53:16 +00:00
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-03-12 14:51:42 +00:00
|
|
|
|
2019-03-13 22:14:30 +00:00
|
|
|
class NetworkAdapter;
|
|
|
|
class TCPPacket;
|
2019-03-14 11:20:38 +00:00
|
|
|
class TCPSocket;
|
2019-03-13 22:14:30 +00:00
|
|
|
|
2019-03-14 11:20:38 +00:00
|
|
|
class IPv4Socket : public Socket {
|
2019-03-12 14:51:42 +00:00
|
|
|
public:
|
2019-06-21 16:37:47 +00:00
|
|
|
static NonnullRefPtr<IPv4Socket> create(int type, int protocol);
|
2019-03-12 14:51:42 +00:00
|
|
|
virtual ~IPv4Socket() override;
|
|
|
|
|
2019-03-12 16:27:07 +00:00
|
|
|
static Lockable<HashTable<IPv4Socket*>>& all_sockets();
|
|
|
|
|
2019-03-12 14:51:42 +00:00
|
|
|
virtual KResult bind(const sockaddr*, socklen_t) override;
|
2019-06-07 07:36:51 +00:00
|
|
|
virtual KResult connect(FileDescription&, const sockaddr*, socklen_t, ShouldBlock = ShouldBlock::Yes) override;
|
2019-08-06 13:40:38 +00:00
|
|
|
virtual KResult listen(int) override;
|
2019-05-20 18:33:03 +00:00
|
|
|
virtual bool get_local_address(sockaddr*, socklen_t*) override;
|
|
|
|
virtual bool get_peer_address(sockaddr*, socklen_t*) override;
|
2019-06-07 07:36:51 +00:00
|
|
|
virtual void attach(FileDescription&) override;
|
|
|
|
virtual void detach(FileDescription&) override;
|
2019-11-04 13:03:14 +00:00
|
|
|
virtual bool can_read(const FileDescription&) const override;
|
|
|
|
virtual bool can_write(const FileDescription&) const override;
|
2019-06-07 07:36:51 +00:00
|
|
|
virtual ssize_t sendto(FileDescription&, const void*, size_t, int, const sockaddr*, socklen_t) override;
|
|
|
|
virtual ssize_t recvfrom(FileDescription&, void*, size_t, int flags, sockaddr*, socklen_t*) override;
|
2019-09-19 19:40:06 +00:00
|
|
|
virtual KResult setsockopt(int level, int option, const void*, socklen_t) override;
|
2019-12-06 17:38:36 +00:00
|
|
|
virtual KResult getsockopt(FileDescription&, int level, int option, void*, socklen_t*) override;
|
2019-03-12 16:27:07 +00:00
|
|
|
|
2019-09-23 17:06:03 +00:00
|
|
|
virtual int ioctl(FileDescription&, unsigned request, unsigned arg) override;
|
|
|
|
|
2019-11-04 13:03:14 +00:00
|
|
|
bool did_receive(const IPv4Address& peer_address, u16 peer_port, KBuffer&&);
|
2019-03-12 16:27:07 +00:00
|
|
|
|
2019-08-06 13:40:38 +00:00
|
|
|
const IPv4Address& local_address() const { return m_local_address; }
|
2019-07-03 19:17:35 +00:00
|
|
|
u16 local_port() const { return m_local_port; }
|
|
|
|
void set_local_port(u16 port) { m_local_port = port; }
|
2019-08-09 02:38:56 +00:00
|
|
|
bool has_specific_local_address() { return m_local_address.to_u32() != 0; }
|
2019-03-14 11:20:38 +00:00
|
|
|
|
2019-05-04 14:40:34 +00:00
|
|
|
const IPv4Address& peer_address() const { return m_peer_address; }
|
2019-07-03 19:17:35 +00:00
|
|
|
u16 peer_port() const { return m_peer_port; }
|
|
|
|
void set_peer_port(u16 port) { m_peer_port = port; }
|
2019-03-13 13:22:27 +00:00
|
|
|
|
2019-08-06 13:40:38 +00:00
|
|
|
IPv4SocketTuple tuple() const { return IPv4SocketTuple(m_local_address, m_local_port, m_peer_address, m_peer_port); }
|
|
|
|
|
2019-08-10 16:10:36 +00:00
|
|
|
String absolute_path(const FileDescription& description) const override;
|
|
|
|
|
2019-09-19 19:40:06 +00:00
|
|
|
u8 ttl() const { return m_ttl; }
|
|
|
|
|
2019-12-14 08:43:31 +00:00
|
|
|
enum class BufferMode {
|
|
|
|
Packets,
|
|
|
|
Bytes,
|
|
|
|
};
|
|
|
|
BufferMode buffer_mode() const { return m_buffer_mode; }
|
|
|
|
|
2019-03-14 11:20:38 +00:00
|
|
|
protected:
|
|
|
|
IPv4Socket(int type, int protocol);
|
2019-05-03 18:42:43 +00:00
|
|
|
virtual const char* class_name() const override { return "IPv4Socket"; }
|
2019-03-14 11:28:30 +00:00
|
|
|
|
2019-05-04 14:40:34 +00:00
|
|
|
int allocate_local_port_if_needed();
|
2019-03-14 11:20:38 +00:00
|
|
|
|
2019-05-03 19:51:40 +00:00
|
|
|
virtual KResult protocol_bind() { return KSuccess; }
|
2019-08-06 13:40:38 +00:00
|
|
|
virtual KResult protocol_listen() { return KSuccess; }
|
2019-08-04 19:07:50 +00:00
|
|
|
virtual int protocol_receive(const KBuffer&, void*, size_t, int) { return -ENOTIMPL; }
|
2019-03-14 11:20:38 +00:00
|
|
|
virtual int protocol_send(const void*, int) { return -ENOTIMPL; }
|
2019-06-07 07:36:51 +00:00
|
|
|
virtual KResult protocol_connect(FileDescription&, ShouldBlock) { return KSuccess; }
|
2019-05-04 14:40:34 +00:00
|
|
|
virtual int protocol_allocate_local_port() { return 0; }
|
2019-03-14 14:23:32 +00:00
|
|
|
virtual bool protocol_is_disconnected() const { return false; }
|
2019-03-13 22:14:30 +00:00
|
|
|
|
2019-08-06 13:40:38 +00:00
|
|
|
void set_local_address(IPv4Address address) { m_local_address = address; }
|
|
|
|
void set_peer_address(IPv4Address address) { m_peer_address = address; }
|
|
|
|
|
2019-03-12 14:51:42 +00:00
|
|
|
private:
|
|
|
|
virtual bool is_ipv4() const override { return true; }
|
|
|
|
|
2019-05-04 14:40:34 +00:00
|
|
|
IPv4Address m_local_address;
|
|
|
|
IPv4Address m_peer_address;
|
2019-03-12 14:51:42 +00:00
|
|
|
|
2019-05-04 01:27:50 +00:00
|
|
|
struct ReceivedPacket {
|
2019-05-04 14:40:34 +00:00
|
|
|
IPv4Address peer_address;
|
2019-07-03 19:17:35 +00:00
|
|
|
u16 peer_port;
|
2019-08-05 09:06:21 +00:00
|
|
|
Optional<KBuffer> data;
|
2019-05-04 01:27:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
SinglyLinkedList<ReceivedPacket> m_receive_queue;
|
2019-03-12 16:27:07 +00:00
|
|
|
|
2019-12-14 08:43:31 +00:00
|
|
|
DoubleBuffer m_receive_buffer;
|
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
u16 m_local_port { 0 };
|
|
|
|
u16 m_peer_port { 0 };
|
2019-03-13 13:22:27 +00:00
|
|
|
|
2019-07-03 19:17:35 +00:00
|
|
|
u32 m_bytes_received { 0 };
|
2019-03-14 14:28:23 +00:00
|
|
|
|
2019-09-19 19:40:06 +00:00
|
|
|
u8 m_ttl { 64 };
|
|
|
|
|
2019-03-12 16:27:07 +00:00
|
|
|
bool m_can_read { false };
|
2019-12-14 08:43:31 +00:00
|
|
|
|
|
|
|
BufferMode m_buffer_mode { BufferMode::Packets };
|
|
|
|
|
|
|
|
Optional<KBuffer> m_scratch_buffer;
|
2019-03-12 14:51:42 +00:00
|
|
|
};
|