2020-01-18 11:38:21 +03:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 11:24:48 +03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 11:38:21 +03:00
|
|
|
*/
|
|
|
|
|
2019-02-14 16:17:38 +03:00
|
|
|
#pragma once
|
|
|
|
|
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>
|
2021-02-28 04:48:45 +03:00
|
|
|
#include <AK/Time.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>
|
2021-07-18 10:10:27 +03:00
|
|
|
#include <Kernel/Locking/Mutex.h>
|
2020-04-04 23:46:45 +03:00
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-02-14 16:17:38 +03:00
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
namespace Kernel {
|
|
|
|
|
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; }
|
|
|
|
|
2020-02-08 02:52:33 +03:00
|
|
|
bool is_shut_down_for_writing() const { return m_shut_down_for_writing; }
|
|
|
|
bool is_shut_down_for_reading() const { return m_shut_down_for_reading; }
|
|
|
|
|
2019-08-10 06:17:00 +03:00
|
|
|
enum class SetupState {
|
2020-09-06 19:31:51 +03:00
|
|
|
Unstarted, // we haven't tried to set the socket up yet
|
2019-08-10 06:17:00 +03:00
|
|
|
InProgress, // we're in the process of setting things up - for TCP maybe we've sent a SYN packet
|
2020-09-06 19:31:51 +03:00
|
|
|
Completed, // the setup process is complete, but not necessarily successful
|
2019-08-10 06:17:00 +03:00
|
|
|
};
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
enum class Role : u8 {
|
|
|
|
None,
|
|
|
|
Listener,
|
|
|
|
Accepted,
|
|
|
|
Connected,
|
|
|
|
Connecting
|
|
|
|
};
|
|
|
|
|
2021-08-05 21:41:44 +03:00
|
|
|
static StringView to_string(SetupState setup_state)
|
2019-08-10 06:17:00 +03:00
|
|
|
{
|
|
|
|
switch (setup_state) {
|
|
|
|
case SetupState::Unstarted:
|
2021-08-05 21:41:44 +03:00
|
|
|
return "Unstarted"sv;
|
2019-08-10 06:17:00 +03:00
|
|
|
case SetupState::InProgress:
|
2021-08-05 21:41:44 +03:00
|
|
|
return "InProgress"sv;
|
2019-08-10 06:17:00 +03:00
|
|
|
case SetupState::Completed:
|
2021-08-05 21:41:44 +03:00
|
|
|
return "Completed"sv;
|
2019-08-10 06:17:00 +03:00
|
|
|
default:
|
2021-08-05 21:41:44 +03:00
|
|
|
return "None"sv;
|
2019-08-10 06:17:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetupState setup_state() const { return m_setup_state; }
|
|
|
|
void set_setup_state(SetupState setup_state);
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
virtual Role role(const FileDescription&) const { return m_role; }
|
|
|
|
|
2019-08-10 06:17:00 +03:00
|
|
|
bool is_connected() const { return m_connected; }
|
2020-12-13 21:15:42 +03:00
|
|
|
void set_connected(bool);
|
2019-08-10 06:17:00 +03:00
|
|
|
|
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 17:17:30 +03:00
|
|
|
|
2020-02-08 02:52:33 +03:00
|
|
|
KResult shutdown(int how);
|
|
|
|
|
2020-08-10 01:13:43 +03:00
|
|
|
virtual KResult bind(Userspace<const sockaddr*>, socklen_t) = 0;
|
2020-09-12 06:11:07 +03:00
|
|
|
virtual KResult connect(FileDescription&, Userspace<const sockaddr*>, socklen_t, ShouldBlock) = 0;
|
2020-02-25 16:49:47 +03:00
|
|
|
virtual KResult listen(size_t) = 0;
|
2020-02-08 01:42:28 +03:00
|
|
|
virtual void get_local_address(sockaddr*, socklen_t*) = 0;
|
|
|
|
virtual void 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; }
|
2020-09-12 06:11:07 +03:00
|
|
|
virtual KResultOr<size_t> sendto(FileDescription&, const UserOrKernelBuffer&, size_t, int flags, Userspace<const sockaddr*>, socklen_t) = 0;
|
2021-02-28 04:48:45 +03:00
|
|
|
virtual KResultOr<size_t> recvfrom(FileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&) = 0;
|
2019-02-14 18:01:08 +03:00
|
|
|
|
2020-08-07 10:18:20 +03:00
|
|
|
virtual KResult setsockopt(int level, int option, Userspace<const void*>, socklen_t);
|
2020-08-07 12:29:05 +03:00
|
|
|
virtual KResult getsockopt(FileDescription&, int level, int option, Userspace<void*>, Userspace<socklen_t*>);
|
2019-03-13 15:13:23 +03:00
|
|
|
|
2021-08-28 23:21:22 +03:00
|
|
|
ProcessID origin_pid() const { return m_origin.pid; }
|
2021-08-28 23:11:16 +03:00
|
|
|
UserID origin_uid() const { return m_origin.uid; }
|
|
|
|
GroupID origin_gid() const { return m_origin.gid; }
|
2021-08-28 23:21:22 +03:00
|
|
|
ProcessID acceptor_pid() const { return m_acceptor.pid; }
|
2021-08-28 23:11:16 +03:00
|
|
|
UserID acceptor_uid() const { return m_acceptor.uid; }
|
|
|
|
GroupID acceptor_gid() const { return m_acceptor.gid; }
|
2020-04-04 23:46:45 +03:00
|
|
|
const RefPtr<NetworkAdapter> bound_interface() const { return m_bound_interface; }
|
2019-02-14 19:18:35 +03:00
|
|
|
|
2021-07-17 22:09:51 +03:00
|
|
|
Mutex& lock() { return m_lock; }
|
2019-03-14 02:20:44 +03:00
|
|
|
|
2019-08-05 11:03:19 +03:00
|
|
|
// ^File
|
2021-03-17 15:18:51 +03:00
|
|
|
virtual KResultOr<size_t> read(FileDescription&, u64, UserOrKernelBuffer&, size_t) override final;
|
|
|
|
virtual KResultOr<size_t> write(FileDescription&, u64, const UserOrKernelBuffer&, size_t) override final;
|
2020-09-06 19:31:51 +03:00
|
|
|
virtual KResult stat(::stat&) const override;
|
2020-01-07 09:23:16 +03:00
|
|
|
virtual String absolute_path(const FileDescription&) const override = 0;
|
2019-05-03 21:42:43 +03:00
|
|
|
|
2021-02-28 04:48:45 +03:00
|
|
|
bool has_receive_timeout() const { return m_receive_timeout != Time::zero(); }
|
|
|
|
const Time& receive_timeout() const { return m_receive_timeout; }
|
2020-01-26 19:54:23 +03:00
|
|
|
|
2021-02-28 04:48:45 +03:00
|
|
|
bool has_send_timeout() const { return m_send_timeout != Time::zero(); }
|
|
|
|
const Time& send_timeout() const { return m_send_timeout; }
|
2020-01-26 19:54:23 +03:00
|
|
|
|
2020-09-16 19:32:45 +03:00
|
|
|
bool wants_timestamp() const { return m_timestamp; }
|
|
|
|
|
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
|
|
|
|
2020-02-25 16:49:47 +03:00
|
|
|
size_t backlog() const { return m_backlog; }
|
|
|
|
void set_backlog(size_t backlog) { m_backlog = backlog; }
|
2019-08-06 16:40:38 +03:00
|
|
|
|
2021-07-11 02:46:09 +03:00
|
|
|
virtual StringView class_name() const override { return "Socket"; }
|
2019-05-03 21:42:43 +03:00
|
|
|
|
2020-09-06 19:31:51 +03:00
|
|
|
virtual void shut_down_for_reading() { }
|
|
|
|
virtual void shut_down_for_writing() { }
|
2020-02-08 17:52:32 +03:00
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
Role m_role { Role::None };
|
|
|
|
|
2021-08-01 18:27:23 +03:00
|
|
|
KResult so_error() const { return m_so_error; }
|
|
|
|
KResult set_so_error(KResult error)
|
|
|
|
{
|
|
|
|
m_so_error = error;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-08-29 02:30:05 +03:00
|
|
|
void set_origin(Process const&);
|
|
|
|
void set_acceptor(Process const&);
|
|
|
|
|
2019-12-06 20:38:36 +03:00
|
|
|
protected:
|
|
|
|
ucred m_origin { 0, 0, 0 };
|
|
|
|
ucred m_acceptor { 0, 0, 0 };
|
|
|
|
|
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; }
|
|
|
|
|
2021-07-17 22:09:51 +03:00
|
|
|
Mutex m_lock { "Socket" };
|
2019-12-06 20:38:36 +03:00
|
|
|
|
2019-02-14 16:17:38 +03:00
|
|
|
int m_domain { 0 };
|
|
|
|
int m_type { 0 };
|
|
|
|
int m_protocol { 0 };
|
2020-02-25 16:49:47 +03:00
|
|
|
size_t m_backlog { 0 };
|
2019-08-10 06:17:00 +03:00
|
|
|
SetupState m_setup_state { SetupState::Unstarted };
|
2019-02-14 19:18:35 +03:00
|
|
|
bool m_connected { false };
|
2020-02-08 02:52:33 +03:00
|
|
|
bool m_shut_down_for_reading { false };
|
|
|
|
bool m_shut_down_for_writing { false };
|
2019-02-14 16:17:38 +03:00
|
|
|
|
2020-04-04 23:46:45 +03:00
|
|
|
RefPtr<NetworkAdapter> m_bound_interface { nullptr };
|
|
|
|
|
2021-02-28 04:48:45 +03:00
|
|
|
Time m_receive_timeout {};
|
|
|
|
Time m_send_timeout {};
|
2020-09-16 19:32:45 +03:00
|
|
|
int m_timestamp { 0 };
|
2019-03-13 15:13:23 +03:00
|
|
|
|
2021-08-01 18:27:23 +03:00
|
|
|
KResult m_so_error { KSuccess };
|
|
|
|
|
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
|
|
|
|
2020-09-06 19:31:51 +03:00
|
|
|
template<typename SocketType>
|
2019-03-14 11:19:24 +03:00
|
|
|
class SocketHandle {
|
|
|
|
public:
|
2021-02-28 16:42:08 +03:00
|
|
|
SocketHandle() = default;
|
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
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|