2019-02-14 16:17:38 +03:00
|
|
|
#pragma once
|
|
|
|
|
2019-02-14 17:17:30 +03:00
|
|
|
#include <AK/HashTable.h>
|
2019-08-09 05:41:06 +03:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2019-06-21 19:58:45 +03:00
|
|
|
#include <AK/RefCounted.h>
|
2019-08-06 16:40:38 +03:00
|
|
|
#include <AK/RefPtr.h>
|
2019-07-09 15:46:23 +03:00
|
|
|
#include <Kernel/FileSystem/File.h>
|
2019-03-07 00:14:31 +03:00
|
|
|
#include <Kernel/KResult.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/Lock.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-02-14 16:17:38 +03:00
|
|
|
|
2019-07-03 22:17:35 +03:00
|
|
|
enum class SocketRole : u8 {
|
2019-05-28 12:53:16 +03:00
|
|
|
None,
|
|
|
|
Listener,
|
|
|
|
Accepted,
|
|
|
|
Connected,
|
|
|
|
Connecting
|
|
|
|
};
|
2019-06-07 18:13:23 +03:00
|
|
|
enum class ShouldBlock {
|
2019-05-28 12:53:16 +03:00
|
|
|
No = 0,
|
|
|
|
Yes = 1
|
|
|
|
};
|
2019-02-14 18:01:08 +03:00
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
class FileDescription;
|
2019-05-03 21:15:54 +03:00
|
|
|
|
2019-05-03 21:42:43 +03:00
|
|
|
class Socket : public File {
|
2019-02-14 16:17:38 +03:00
|
|
|
public:
|
2019-06-21 19:37:47 +03:00
|
|
|
static KResultOr<NonnullRefPtr<Socket>> create(int domain, int type, int protocol);
|
2019-05-03 21:42:43 +03:00
|
|
|
virtual ~Socket() override;
|
2019-02-14 16:17:38 +03:00
|
|
|
|
|
|
|
int domain() const { return m_domain; }
|
|
|
|
int type() const { return m_type; }
|
|
|
|
int protocol() const { return m_protocol; }
|
|
|
|
|
2019-02-14 19:18:35 +03:00
|
|
|
bool can_accept() const { return !m_pending.is_empty(); }
|
2019-06-21 19:37:47 +03:00
|
|
|
RefPtr<Socket> accept();
|
2019-02-14 19:18:35 +03:00
|
|
|
bool is_connected() const { return m_connected; }
|
2019-02-14 17:17:30 +03:00
|
|
|
|
2019-03-07 00:14:31 +03:00
|
|
|
virtual KResult bind(const sockaddr*, socklen_t) = 0;
|
2019-06-07 10:36:51 +03:00
|
|
|
virtual KResult connect(FileDescription&, const sockaddr*, socklen_t, ShouldBlock) = 0;
|
2019-08-06 16:40:38 +03:00
|
|
|
virtual KResult listen(int) = 0;
|
2019-05-20 21:33:03 +03:00
|
|
|
virtual bool get_local_address(sockaddr*, socklen_t*) = 0;
|
|
|
|
virtual bool get_peer_address(sockaddr*, socklen_t*) = 0;
|
2019-02-14 17:55:19 +03:00
|
|
|
virtual bool is_local() const { return false; }
|
2019-03-12 17:51:42 +03:00
|
|
|
virtual bool is_ipv4() const { return false; }
|
2019-06-07 10:36:51 +03:00
|
|
|
virtual void attach(FileDescription&) = 0;
|
|
|
|
virtual void detach(FileDescription&) = 0;
|
|
|
|
virtual ssize_t sendto(FileDescription&, const void*, size_t, int flags, const sockaddr*, socklen_t) = 0;
|
|
|
|
virtual ssize_t recvfrom(FileDescription&, void*, size_t, int flags, sockaddr*, socklen_t*) = 0;
|
2019-02-14 18:01:08 +03:00
|
|
|
|
2019-03-13 15:13:23 +03:00
|
|
|
KResult setsockopt(int level, int option, const void*, socklen_t);
|
|
|
|
KResult getsockopt(int level, int option, void*, socklen_t*);
|
|
|
|
|
2019-02-14 19:18:35 +03:00
|
|
|
pid_t origin_pid() const { return m_origin_pid; }
|
|
|
|
|
2019-03-13 15:13:23 +03:00
|
|
|
timeval receive_deadline() const { return m_receive_deadline; }
|
|
|
|
timeval send_deadline() const { return m_send_deadline; }
|
|
|
|
|
2019-03-14 02:20:44 +03:00
|
|
|
void set_connected(bool connected) { m_connected = connected; }
|
|
|
|
|
|
|
|
Lock& lock() { return m_lock; }
|
|
|
|
|
2019-08-05 11:03:19 +03:00
|
|
|
// ^File
|
|
|
|
virtual ssize_t read(FileDescription&, u8*, ssize_t) override final;
|
|
|
|
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override final;
|
2019-06-07 10:36:51 +03:00
|
|
|
virtual String absolute_path(const FileDescription&) const override;
|
2019-05-03 21:42:43 +03:00
|
|
|
|
2019-02-14 16:17:38 +03:00
|
|
|
protected:
|
|
|
|
Socket(int domain, int type, int protocol);
|
|
|
|
|
2019-08-09 05:41:06 +03:00
|
|
|
KResult queue_connection_from(NonnullRefPtr<Socket>);
|
2019-02-14 19:18:35 +03:00
|
|
|
|
2019-03-13 15:13:23 +03:00
|
|
|
void load_receive_deadline();
|
|
|
|
void load_send_deadline();
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
int backlog() const { return m_backlog; }
|
|
|
|
void set_backlog(int backlog) { m_backlog = backlog; }
|
|
|
|
|
2019-05-03 21:42:43 +03:00
|
|
|
virtual const char* class_name() const override { return "Socket"; }
|
|
|
|
|
2019-02-14 16:17:38 +03:00
|
|
|
private:
|
2019-05-03 21:42:43 +03:00
|
|
|
virtual bool is_socket() const final { return true; }
|
|
|
|
|
2019-05-02 04:28:20 +03:00
|
|
|
Lock m_lock { "Socket" };
|
2019-02-14 19:18:35 +03:00
|
|
|
pid_t m_origin_pid { 0 };
|
2019-02-14 16:17:38 +03:00
|
|
|
int m_domain { 0 };
|
|
|
|
int m_type { 0 };
|
|
|
|
int m_protocol { 0 };
|
2019-02-14 17:17:30 +03:00
|
|
|
int m_backlog { 0 };
|
2019-02-14 19:18:35 +03:00
|
|
|
bool m_connected { false };
|
2019-02-14 16:17:38 +03:00
|
|
|
|
2019-03-13 15:13:23 +03:00
|
|
|
timeval m_receive_timeout { 0, 0 };
|
|
|
|
timeval m_send_timeout { 0, 0 };
|
|
|
|
|
|
|
|
timeval m_receive_deadline { 0, 0 };
|
|
|
|
timeval m_send_deadline { 0, 0 };
|
|
|
|
|
2019-08-09 05:41:06 +03:00
|
|
|
NonnullRefPtrVector<Socket> m_pending;
|
2019-02-14 17:17:30 +03:00
|
|
|
};
|
2019-03-14 11:19:24 +03:00
|
|
|
|
2019-08-09 10:16:12 +03:00
|
|
|
template<typename SocketType>
|
2019-03-14 11:19:24 +03:00
|
|
|
class SocketHandle {
|
|
|
|
public:
|
2019-05-28 12:53:16 +03:00
|
|
|
SocketHandle() {}
|
2019-03-14 11:19:24 +03:00
|
|
|
|
2019-08-09 10:16:12 +03:00
|
|
|
SocketHandle(NonnullRefPtr<SocketType>&& socket)
|
2019-03-14 11:19:24 +03:00
|
|
|
: m_socket(move(socket))
|
|
|
|
{
|
|
|
|
if (m_socket)
|
|
|
|
m_socket->lock().lock();
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketHandle(SocketHandle&& other)
|
|
|
|
: m_socket(move(other.m_socket))
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
~SocketHandle()
|
|
|
|
{
|
|
|
|
if (m_socket)
|
|
|
|
m_socket->lock().unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
SocketHandle(const SocketHandle&) = delete;
|
|
|
|
SocketHandle& operator=(const SocketHandle&) = delete;
|
|
|
|
|
|
|
|
operator bool() const { return m_socket; }
|
|
|
|
|
2019-08-09 10:16:12 +03:00
|
|
|
SocketType* operator->() { return &socket(); }
|
|
|
|
const SocketType* operator->() const { return &socket(); }
|
2019-03-14 11:19:24 +03:00
|
|
|
|
2019-08-09 10:16:12 +03:00
|
|
|
SocketType& socket() { return *m_socket; }
|
|
|
|
const SocketType& socket() const { return *m_socket; }
|
2019-03-14 11:19:24 +03:00
|
|
|
|
|
|
|
private:
|
2019-08-09 10:16:12 +03:00
|
|
|
RefPtr<SocketType> m_socket;
|
2019-03-14 11:19:24 +03:00
|
|
|
};
|