banan-os/kernel/include/kernel/Networking/NetworkSocket.h

52 lines
1.3 KiB
C
Raw Normal View History

2024-02-01 23:38:06 +02:00
#pragma once
#include <BAN/WeakPtr.h>
#include <kernel/FS/Socket.h>
2024-02-01 23:38:06 +02:00
#include <kernel/FS/TmpFS/Inode.h>
#include <kernel/Networking/NetworkInterface.h>
#include <kernel/Networking/NetworkLayer.h>
2024-02-01 23:38:06 +02:00
namespace Kernel
{
enum NetworkProtocol : uint8_t
{
ICMP = 0x01,
UDP = 0x11,
};
class NetworkSocket : public TmpInode, public BAN::Weakable<NetworkSocket>
2024-02-01 23:38:06 +02:00
{
2024-02-03 01:26:06 +02:00
BAN_NON_COPYABLE(NetworkSocket);
BAN_NON_MOVABLE(NetworkSocket);
2024-02-02 03:16:01 +02:00
public:
static constexpr uint16_t PORT_NONE = 0;
2024-02-01 23:38:06 +02:00
public:
void bind_interface_and_port(NetworkInterface*, uint16_t port);
~NetworkSocket();
2024-02-01 23:38:06 +02:00
NetworkInterface& interface() { ASSERT(m_interface); return *m_interface; }
2024-02-02 13:50:00 +02:00
virtual size_t protocol_header_size() const = 0;
virtual void add_protocol_header(BAN::ByteSpan packet, uint16_t dst_port, PseudoHeader) = 0;
virtual NetworkProtocol protocol() const = 0;
2024-02-02 03:16:01 +02:00
2024-02-09 17:05:07 +02:00
virtual void receive_packet(BAN::ConstByteSpan, const sockaddr_storage& sender) = 0;
bool is_bound() const { return m_interface != nullptr; }
2024-02-02 13:50:00 +02:00
2024-02-01 23:38:06 +02:00
protected:
NetworkSocket(NetworkLayer&, ino_t, const TmpInodeInfo&);
2024-02-01 23:38:06 +02:00
virtual BAN::ErrorOr<long> ioctl_impl(int request, void* arg) override;
2024-02-01 23:38:06 +02:00
protected:
NetworkLayer& m_network_layer;
2024-02-02 03:16:01 +02:00
NetworkInterface* m_interface = nullptr;
2024-02-09 17:05:07 +02:00
uint16_t m_port { PORT_NONE };
2024-02-01 23:38:06 +02:00
};
}