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
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
#include <AK/Error.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>
|
2022-08-19 21:53:40 +03:00
|
|
|
#include <Kernel/Library/LockRefPtr.h>
|
|
|
|
#include <Kernel/Library/NonnullLockRefPtrVector.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 {
|
|
|
|
|
2021-09-07 14:39:11 +03:00
|
|
|
class OpenFileDescription;
|
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:
|
2022-08-19 21:53:40 +03:00
|
|
|
static ErrorOr<NonnullLockRefPtr<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);
|
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual Role role(OpenFileDescription const&) const { return m_role; }
|
2019-08-11 16:38:20 +03:00
|
|
|
|
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(); }
|
2022-08-19 21:53:40 +03:00
|
|
|
LockRefPtr<Socket> accept();
|
2019-02-14 17:17:30 +03:00
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> shutdown(int how);
|
2020-02-08 02:52:33 +03:00
|
|
|
|
2022-08-21 17:33:09 +03:00
|
|
|
virtual ErrorOr<void> bind(Credentials const&, Userspace<sockaddr const*>, socklen_t) = 0;
|
2022-08-21 17:35:03 +03:00
|
|
|
virtual ErrorOr<void> connect(Credentials const&, OpenFileDescription&, Userspace<sockaddr const*>, socklen_t) = 0;
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> 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; }
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<size_t> sendto(OpenFileDescription&, UserOrKernelBuffer const&, size_t, int flags, Userspace<sockaddr const*>, socklen_t) = 0;
|
2022-08-21 17:45:42 +03:00
|
|
|
virtual ErrorOr<size_t> recvfrom(OpenFileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&, bool blocking) = 0;
|
2019-02-14 18:01:08 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<void> setsockopt(int level, int option, Userspace<void const*>, socklen_t);
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> getsockopt(OpenFileDescription&, 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; }
|
2022-08-19 21:53:40 +03:00
|
|
|
LockRefPtr<NetworkAdapter> const bound_interface() const { return m_bound_interface; }
|
2019-02-14 19:18:35 +03:00
|
|
|
|
2021-08-29 14:10:55 +03:00
|
|
|
Mutex& mutex() { return m_mutex; }
|
2019-03-14 02:20:44 +03:00
|
|
|
|
2019-08-05 11:03:19 +03:00
|
|
|
// ^File
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<size_t> read(OpenFileDescription&, u64, UserOrKernelBuffer&, size_t) override final;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<size_t> write(OpenFileDescription&, u64, UserOrKernelBuffer const&, size_t) override final;
|
2021-12-17 13:22:27 +03:00
|
|
|
virtual ErrorOr<struct stat> stat() const override;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const&) 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(); }
|
2022-04-01 20:58:27 +03:00
|
|
|
Time const& 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(); }
|
2022-04-01 20:58:27 +03:00
|
|
|
Time const& 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);
|
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
ErrorOr<void> queue_connection_from(NonnullLockRefPtr<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-10-03 01:24:00 +03:00
|
|
|
virtual StringView class_name() const override { return "Socket"sv; }
|
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-12-28 17:16:57 +03:00
|
|
|
ErrorOr<void> so_error() const
|
|
|
|
{
|
2022-01-29 02:47:18 +03:00
|
|
|
VERIFY(m_mutex.is_exclusively_locked_by_current_thread());
|
2021-12-28 17:16:57 +03:00
|
|
|
return m_so_error;
|
|
|
|
}
|
2021-11-08 02:51:39 +03:00
|
|
|
|
|
|
|
Error set_so_error(ErrnoCode error_code)
|
|
|
|
{
|
2021-12-28 17:16:57 +03:00
|
|
|
MutexLocker locker(mutex());
|
2021-11-08 02:51:39 +03:00
|
|
|
auto error = Error::from_errno(error_code);
|
|
|
|
m_so_error = error;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
Error set_so_error(Error error)
|
2021-08-01 18:27:23 +03:00
|
|
|
{
|
2021-12-28 17:16:57 +03:00
|
|
|
MutexLocker locker(mutex());
|
2021-08-01 18:27:23 +03:00
|
|
|
m_so_error = error;
|
|
|
|
return error;
|
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
void clear_so_error()
|
|
|
|
{
|
|
|
|
m_so_error = {};
|
|
|
|
}
|
|
|
|
|
2021-08-29 02:30:05 +03:00
|
|
|
void set_origin(Process const&);
|
|
|
|
void set_acceptor(Process const&);
|
|
|
|
|
2021-08-29 03:04:30 +03:00
|
|
|
void set_role(Role role) { m_role = role; }
|
|
|
|
|
2019-12-06 20:38:36 +03:00
|
|
|
ucred m_origin { 0, 0, 0 };
|
|
|
|
ucred m_acceptor { 0, 0, 0 };
|
2021-12-02 02:01:02 +03:00
|
|
|
bool m_routing_disabled { false };
|
2019-12-06 20:38:36 +03:00
|
|
|
|
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-08-29 14:10:55 +03:00
|
|
|
Mutex m_mutex { "Socket"sv };
|
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
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
LockRefPtr<NetworkAdapter> m_bound_interface { nullptr };
|
2020-04-04 23:46:45 +03:00
|
|
|
|
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-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> m_so_error;
|
2021-08-01 18:27:23 +03:00
|
|
|
|
2022-08-19 21:53:40 +03:00
|
|
|
NonnullLockRefPtrVector<Socket> m_pending;
|
2019-02-14 17:17:30 +03:00
|
|
|
};
|
2019-03-14 11:19:24 +03:00
|
|
|
|
2021-09-07 16:05:51 +03:00
|
|
|
// This is a special variant of TRY() that also updates the socket's SO_ERROR field on error.
|
|
|
|
#define SOCKET_TRY(expression) \
|
|
|
|
({ \
|
|
|
|
auto result = (expression); \
|
|
|
|
if (result.is_error()) \
|
|
|
|
return set_so_error(result.release_error()); \
|
|
|
|
result.release_value(); \
|
|
|
|
})
|
|
|
|
|
2020-02-16 03:27:42 +03:00
|
|
|
}
|