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-05-26 12:38:32 +03:00
|
|
|
#include <AK/IntrusiveList.h>
|
2019-02-14 16:17:38 +03:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
2019-05-28 12:53:16 +03:00
|
|
|
#include <Kernel/Net/Socket.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-02-14 17:17:30 +03:00
|
|
|
|
2021-04-28 13:39:12 +03:00
|
|
|
struct SocketPair {
|
2023-03-06 21:29:25 +03:00
|
|
|
NonnullRefPtr<OpenFileDescription> description0;
|
|
|
|
NonnullRefPtr<OpenFileDescription> description1;
|
2021-04-28 13:39:12 +03:00
|
|
|
};
|
|
|
|
|
2021-05-26 12:38:32 +03:00
|
|
|
class LocalSocket final : public Socket {
|
2020-02-16 04:15:33 +03:00
|
|
|
|
2019-02-14 16:17:38 +03:00
|
|
|
public:
|
2023-03-10 09:53:02 +03:00
|
|
|
static ErrorOr<NonnullRefPtr<LocalSocket>> try_create(int type);
|
2021-11-08 02:51:39 +03:00
|
|
|
static ErrorOr<SocketPair> try_create_connected_pair(int type);
|
2019-02-14 16:17:38 +03:00
|
|
|
virtual ~LocalSocket() override;
|
|
|
|
|
2023-03-06 21:29:25 +03:00
|
|
|
ErrorOr<void> sendfd(OpenFileDescription const& socket_description, NonnullRefPtr<OpenFileDescription> passing_description);
|
|
|
|
ErrorOr<NonnullRefPtr<OpenFileDescription>> recvfd(OpenFileDescription const& socket_description);
|
|
|
|
ErrorOr<Vector<NonnullRefPtr<OpenFileDescription>>> recvfds(OpenFileDescription const& socket_description, int n);
|
2020-06-24 23:57:37 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
static void for_each(Function<void(LocalSocket const&)>);
|
|
|
|
static ErrorOr<void> try_for_each(Function<ErrorOr<void>(LocalSocket const&)>);
|
2019-08-10 18:55:54 +03:00
|
|
|
|
|
|
|
StringView socket_path() const;
|
2022-04-01 20:58:27 +03:00
|
|
|
ErrorOr<NonnullOwnPtr<KString>> pseudo_path(OpenFileDescription const& description) const override;
|
2019-08-10 19:10:36 +03:00
|
|
|
|
2019-08-05 11:03:19 +03:00
|
|
|
// ^Socket
|
2022-08-21 17:33:09 +03:00
|
|
|
virtual ErrorOr<void> bind(Credentials const&, Userspace<sockaddr const*>, socklen_t) override;
|
2022-08-21 17:35:03 +03:00
|
|
|
virtual ErrorOr<void> connect(Credentials const&, OpenFileDescription&, Userspace<sockaddr const*>, socklen_t) override;
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> listen(size_t) override;
|
2020-02-08 01:42:28 +03:00
|
|
|
virtual void get_local_address(sockaddr*, socklen_t*) override;
|
|
|
|
virtual void get_peer_address(sockaddr*, socklen_t*) override;
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> attach(OpenFileDescription&) override;
|
2021-09-07 14:39:11 +03:00
|
|
|
virtual void detach(OpenFileDescription&) override;
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual bool can_read(OpenFileDescription const&, u64) const override;
|
|
|
|
virtual bool can_write(OpenFileDescription const&, u64) const override;
|
|
|
|
virtual ErrorOr<size_t> sendto(OpenFileDescription&, UserOrKernelBuffer const&, size_t, int, Userspace<sockaddr const*>, socklen_t) override;
|
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) override;
|
2021-11-08 02:51:39 +03:00
|
|
|
virtual ErrorOr<void> getsockopt(OpenFileDescription&, int level, int option, Userspace<void*>, Userspace<socklen_t*>) override;
|
|
|
|
virtual ErrorOr<void> ioctl(OpenFileDescription&, unsigned request, Userspace<void*> arg) override;
|
2022-08-21 17:15:29 +03:00
|
|
|
virtual ErrorOr<void> chown(Credentials const&, OpenFileDescription&, UserID, GroupID) override;
|
|
|
|
virtual ErrorOr<void> chmod(Credentials const&, OpenFileDescription&, mode_t) override;
|
2019-02-14 18:01:08 +03:00
|
|
|
|
2019-02-14 16:17:38 +03:00
|
|
|
private:
|
2021-08-01 12:34:10 +03:00
|
|
|
explicit LocalSocket(int type, NonnullOwnPtr<DoubleBuffer> client_buffer, NonnullOwnPtr<DoubleBuffer> server_buffer);
|
2021-10-03 01:24:00 +03:00
|
|
|
virtual StringView class_name() const override { return "LocalSocket"sv; }
|
2019-02-14 17:55:19 +03:00
|
|
|
virtual bool is_local() const override { return true; }
|
2022-04-01 20:58:27 +03:00
|
|
|
bool has_attached_peer(OpenFileDescription const&) const;
|
2021-09-07 14:39:11 +03:00
|
|
|
DoubleBuffer* receive_buffer_for(OpenFileDescription&);
|
|
|
|
DoubleBuffer* send_buffer_for(OpenFileDescription&);
|
2023-03-06 21:29:25 +03:00
|
|
|
Vector<NonnullRefPtr<OpenFileDescription>>& sendfd_queue_for(OpenFileDescription const&);
|
|
|
|
Vector<NonnullRefPtr<OpenFileDescription>>& recvfd_queue_for(OpenFileDescription const&);
|
2019-02-14 16:17:38 +03:00
|
|
|
|
2020-11-30 02:05:27 +03:00
|
|
|
void set_connect_side_role(Role connect_side_role, bool force_evaluate_block_conditions = false)
|
|
|
|
{
|
|
|
|
auto previous = m_connect_side_role;
|
|
|
|
m_connect_side_role = connect_side_role;
|
|
|
|
if (previous != m_connect_side_role || force_evaluate_block_conditions)
|
|
|
|
evaluate_block_conditions();
|
|
|
|
}
|
|
|
|
|
2021-11-08 02:51:39 +03:00
|
|
|
ErrorOr<void> try_set_path(StringView);
|
2021-08-29 02:59:34 +03:00
|
|
|
|
2021-09-16 03:10:03 +03:00
|
|
|
// The inode this socket is bound to.
|
2023-03-07 14:25:00 +03:00
|
|
|
RefPtr<Inode> m_inode;
|
2019-02-14 17:17:30 +03:00
|
|
|
|
2021-08-28 23:11:16 +03:00
|
|
|
UserID m_prebind_uid { 0 };
|
|
|
|
GroupID m_prebind_gid { 0 };
|
2020-01-03 22:14:56 +03:00
|
|
|
mode_t m_prebind_mode { 0 };
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
// A single LocalSocket is shared between two file descriptions
|
|
|
|
// on the connect side and the accept side; so we need to store
|
|
|
|
// an additional role for the connect side and differentiate
|
|
|
|
// between them.
|
|
|
|
Role m_connect_side_role { Role::None };
|
2021-09-07 14:39:11 +03:00
|
|
|
OpenFileDescription* m_connect_side_fd { nullptr };
|
2019-08-11 16:38:20 +03:00
|
|
|
|
2022-04-01 20:58:27 +03:00
|
|
|
virtual Role role(OpenFileDescription const& description) const override
|
2019-08-11 16:38:20 +03:00
|
|
|
{
|
|
|
|
if (m_connect_side_fd == &description)
|
|
|
|
return m_connect_side_role;
|
|
|
|
return m_role;
|
|
|
|
}
|
|
|
|
|
2019-02-14 17:17:30 +03:00
|
|
|
bool m_bound { false };
|
2019-08-11 16:28:18 +03:00
|
|
|
bool m_accept_side_fd_open { false };
|
2021-08-29 02:59:34 +03:00
|
|
|
OwnPtr<KString> m_path;
|
2019-02-14 17:17:30 +03:00
|
|
|
|
2021-08-01 12:34:10 +03:00
|
|
|
NonnullOwnPtr<DoubleBuffer> m_for_client;
|
|
|
|
NonnullOwnPtr<DoubleBuffer> m_for_server;
|
2019-08-10 18:58:06 +03:00
|
|
|
|
2023-03-06 21:29:25 +03:00
|
|
|
Vector<NonnullRefPtr<OpenFileDescription>> m_fds_for_client;
|
|
|
|
Vector<NonnullRefPtr<OpenFileDescription>> m_fds_for_server;
|
2020-06-24 23:57:37 +03:00
|
|
|
|
2021-05-26 12:38:32 +03:00
|
|
|
IntrusiveListNode<LocalSocket> m_list_node;
|
|
|
|
|
|
|
|
public:
|
2021-09-09 15:00:59 +03:00
|
|
|
using List = IntrusiveList<&LocalSocket::m_list_node>;
|
2019-02-14 16:17:38 +03:00
|
|
|
};
|
2020-02-16 03:27:42 +03:00
|
|
|
|
|
|
|
}
|