2019-02-14 14:17:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-03-16 13:18:22 +01:00
|
|
|
#include <Kernel/Lock.h>
|
2019-02-14 14:17:38 +01:00
|
|
|
#include <AK/Retainable.h>
|
|
|
|
#include <AK/RetainPtr.h>
|
2019-02-14 15:17:30 +01:00
|
|
|
#include <AK/HashTable.h>
|
|
|
|
#include <AK/Vector.h>
|
2019-02-14 14:38:30 +01:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-03-06 22:14:31 +01:00
|
|
|
#include <Kernel/KResult.h>
|
2019-02-14 14:17:38 +01:00
|
|
|
|
2019-03-20 02:33:51 +01:00
|
|
|
enum class SocketRole { None, Listener, Accepted, Connected, Connecting };
|
2019-04-08 04:52:21 +02:00
|
|
|
enum class ShouldBlock { No = 0, Yes = 1 };
|
2019-02-14 16:01:08 +01:00
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
class Socket : public Retainable<Socket> {
|
|
|
|
public:
|
2019-03-06 22:14:31 +01:00
|
|
|
static KResultOr<Retained<Socket>> create(int domain, int type, int protocol);
|
2019-02-14 14:17:38 +01:00
|
|
|
virtual ~Socket();
|
|
|
|
|
|
|
|
int domain() const { return m_domain; }
|
|
|
|
int type() const { return m_type; }
|
|
|
|
int protocol() const { return m_protocol; }
|
|
|
|
|
2019-02-14 17:18:35 +01:00
|
|
|
bool can_accept() const { return !m_pending.is_empty(); }
|
2019-02-14 15:17:30 +01:00
|
|
|
RetainPtr<Socket> accept();
|
2019-02-14 17:18:35 +01:00
|
|
|
bool is_connected() const { return m_connected; }
|
2019-03-06 22:14:31 +01:00
|
|
|
KResult listen(int backlog);
|
2019-02-14 15:17:30 +01:00
|
|
|
|
2019-03-06 22:14:31 +01:00
|
|
|
virtual KResult bind(const sockaddr*, socklen_t) = 0;
|
2019-04-08 04:52:21 +02:00
|
|
|
virtual KResult connect(const sockaddr*, socklen_t, ShouldBlock) = 0;
|
2019-02-14 15:17:30 +01:00
|
|
|
virtual bool get_address(sockaddr*, socklen_t*) = 0;
|
2019-02-14 15:55:19 +01:00
|
|
|
virtual bool is_local() const { return false; }
|
2019-03-12 15:51:42 +01:00
|
|
|
virtual bool is_ipv4() const { return false; }
|
2019-02-17 11:00:35 +01:00
|
|
|
virtual void attach_fd(SocketRole) = 0;
|
|
|
|
virtual void detach_fd(SocketRole) = 0;
|
2019-02-14 16:01:08 +01:00
|
|
|
virtual bool can_read(SocketRole) const = 0;
|
2019-02-25 21:19:57 +01:00
|
|
|
virtual ssize_t read(SocketRole, byte*, ssize_t) = 0;
|
|
|
|
virtual ssize_t write(SocketRole, const byte*, ssize_t) = 0;
|
2019-02-14 16:01:08 +01:00
|
|
|
virtual bool can_write(SocketRole) const = 0;
|
2019-03-12 15:51:42 +01:00
|
|
|
virtual ssize_t sendto(const void*, size_t, int flags, const sockaddr*, socklen_t) = 0;
|
2019-03-13 14:47:21 +01:00
|
|
|
virtual ssize_t recvfrom(void*, size_t, int flags, sockaddr*, socklen_t*) = 0;
|
2019-02-14 16:01:08 +01:00
|
|
|
|
2019-03-13 13:13:23 +01:00
|
|
|
KResult setsockopt(int level, int option, const void*, socklen_t);
|
|
|
|
KResult getsockopt(int level, int option, void*, socklen_t*);
|
|
|
|
|
2019-02-14 17:18:35 +01:00
|
|
|
pid_t origin_pid() const { return m_origin_pid; }
|
|
|
|
|
2019-03-13 13:13:23 +01:00
|
|
|
timeval receive_deadline() const { return m_receive_deadline; }
|
|
|
|
timeval send_deadline() const { return m_send_deadline; }
|
|
|
|
|
2019-03-14 00:20:44 +01:00
|
|
|
void set_connected(bool connected) { m_connected = connected; }
|
|
|
|
|
|
|
|
Lock& lock() { return m_lock; }
|
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
protected:
|
|
|
|
Socket(int domain, int type, int protocol);
|
|
|
|
|
2019-03-06 22:14:31 +01:00
|
|
|
KResult queue_connection_from(Socket&);
|
2019-02-14 17:18:35 +01:00
|
|
|
|
2019-03-13 13:13:23 +01:00
|
|
|
void load_receive_deadline();
|
|
|
|
void load_send_deadline();
|
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
private:
|
2019-05-02 03:28:20 +02:00
|
|
|
Lock m_lock { "Socket" };
|
2019-02-14 17:18:35 +01:00
|
|
|
pid_t m_origin_pid { 0 };
|
2019-02-14 14:17:38 +01:00
|
|
|
int m_domain { 0 };
|
|
|
|
int m_type { 0 };
|
|
|
|
int m_protocol { 0 };
|
2019-02-14 15:17:30 +01:00
|
|
|
int m_backlog { 0 };
|
2019-02-14 17:18:35 +01:00
|
|
|
bool m_connected { false };
|
2019-02-14 14:17:38 +01:00
|
|
|
|
2019-03-13 13:13:23 +01: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-02-14 15:17:30 +01:00
|
|
|
Vector<RetainPtr<Socket>> m_pending;
|
|
|
|
};
|
2019-03-14 09:19:24 +01:00
|
|
|
|
|
|
|
class SocketHandle {
|
|
|
|
public:
|
|
|
|
SocketHandle() { }
|
|
|
|
|
|
|
|
SocketHandle(RetainPtr<Socket>&& socket)
|
|
|
|
: 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; }
|
|
|
|
|
|
|
|
Socket* operator->() { return &socket(); }
|
|
|
|
const Socket* operator->() const { return &socket(); }
|
|
|
|
|
|
|
|
Socket& socket() { return *m_socket; }
|
|
|
|
const Socket& socket() const { return *m_socket; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
RetainPtr<Socket> m_socket;
|
|
|
|
};
|