2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 01:24:48 -07:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 09:38:21 +01:00
|
|
|
*/
|
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
2021-05-26 02:38:32 -07:00
|
|
|
#include <AK/IntrusiveList.h>
|
2019-02-14 14:17:38 +01:00
|
|
|
#include <Kernel/DoubleBuffer.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/Net/Socket.h>
|
2019-02-14 14:17:38 +01:00
|
|
|
|
2020-02-16 01:27:42 +01:00
|
|
|
namespace Kernel {
|
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
class FileDescription;
|
2019-02-14 15:17:30 +01:00
|
|
|
|
2021-04-28 12:39:12 +02:00
|
|
|
struct SocketPair {
|
2021-07-28 01:49:04 -07:00
|
|
|
NonnullRefPtr<FileDescription> description0;
|
2021-04-28 12:39:12 +02:00
|
|
|
NonnullRefPtr<FileDescription> description1;
|
|
|
|
};
|
|
|
|
|
2021-05-26 02:38:32 -07:00
|
|
|
class LocalSocket final : public Socket {
|
2020-02-16 02:15:33 +01:00
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
public:
|
2020-01-23 18:11:14 +01:00
|
|
|
static KResultOr<NonnullRefPtr<Socket>> create(int type);
|
2021-04-28 12:39:12 +02:00
|
|
|
static KResultOr<SocketPair> create_connected_pair(int type);
|
2019-02-14 14:17:38 +01:00
|
|
|
virtual ~LocalSocket() override;
|
|
|
|
|
2020-06-25 10:51:30 +02:00
|
|
|
KResult sendfd(const FileDescription& socket_description, FileDescription& passing_description);
|
|
|
|
KResultOr<NonnullRefPtr<FileDescription>> recvfd(const FileDescription& socket_description);
|
2020-06-24 22:57:37 +02:00
|
|
|
|
2020-04-18 12:50:35 +03:00
|
|
|
static void for_each(Function<void(const LocalSocket&)>);
|
2019-08-10 18:55:54 +03:00
|
|
|
|
|
|
|
StringView socket_path() const;
|
2019-08-10 19:10:36 +03:00
|
|
|
String absolute_path(const FileDescription& description) const override;
|
|
|
|
|
2019-08-05 10:03:19 +02:00
|
|
|
// ^Socket
|
2020-08-09 15:13:43 -07:00
|
|
|
virtual KResult bind(Userspace<const sockaddr*>, socklen_t) override;
|
2020-09-11 21:11:07 -06:00
|
|
|
virtual KResult connect(FileDescription&, Userspace<const sockaddr*>, socklen_t, ShouldBlock = ShouldBlock::Yes) override;
|
2020-02-25 14:49:47 +01:00
|
|
|
virtual KResult listen(size_t) override;
|
2020-02-07 23:42:28 +01:00
|
|
|
virtual void get_local_address(sockaddr*, socklen_t*) override;
|
|
|
|
virtual void get_peer_address(sockaddr*, socklen_t*) override;
|
2020-09-17 13:51:09 -06:00
|
|
|
virtual KResult attach(FileDescription&) override;
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual void detach(FileDescription&) override;
|
2020-04-10 19:44:42 +10:00
|
|
|
virtual bool can_read(const FileDescription&, size_t) const override;
|
|
|
|
virtual bool can_write(const FileDescription&, size_t) const override;
|
2020-09-11 21:11:07 -06:00
|
|
|
virtual KResultOr<size_t> sendto(FileDescription&, const UserOrKernelBuffer&, size_t, int, Userspace<const sockaddr*>, socklen_t) override;
|
2021-02-28 02:48:45 +01:00
|
|
|
virtual KResultOr<size_t> recvfrom(FileDescription&, UserOrKernelBuffer&, size_t, int flags, Userspace<sockaddr*>, Userspace<socklen_t*>, Time&) override;
|
2020-08-07 02:29:05 -07:00
|
|
|
virtual KResult getsockopt(FileDescription&, int level, int option, Userspace<void*>, Userspace<socklen_t*>) override;
|
2020-05-28 17:32:20 +03:00
|
|
|
virtual KResult chown(FileDescription&, uid_t, gid_t) override;
|
|
|
|
virtual KResult chmod(FileDescription&, mode_t) override;
|
2019-02-14 16:01:08 +01:00
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
private:
|
2021-08-01 02:34:10 -07:00
|
|
|
explicit LocalSocket(int type, NonnullOwnPtr<DoubleBuffer> client_buffer, NonnullOwnPtr<DoubleBuffer> server_buffer);
|
2021-07-11 01:46:09 +02:00
|
|
|
virtual StringView class_name() const override { return "LocalSocket"; }
|
2019-02-14 15:55:19 +01:00
|
|
|
virtual bool is_local() const override { return true; }
|
2019-06-07 09:36:51 +02:00
|
|
|
bool has_attached_peer(const FileDescription&) const;
|
2020-12-23 20:24:00 +01:00
|
|
|
DoubleBuffer* receive_buffer_for(FileDescription&);
|
|
|
|
DoubleBuffer* send_buffer_for(FileDescription&);
|
2020-06-25 10:51:30 +02:00
|
|
|
NonnullRefPtrVector<FileDescription>& sendfd_queue_for(const FileDescription&);
|
|
|
|
NonnullRefPtrVector<FileDescription>& recvfd_queue_for(const FileDescription&);
|
2019-02-14 14:17:38 +01:00
|
|
|
|
2020-11-29 16:05:27 -07: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();
|
|
|
|
}
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
// An open socket file on the filesystem.
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<FileDescription> m_file;
|
2019-02-14 15:17:30 +01:00
|
|
|
|
2020-01-03 20:14:56 +01:00
|
|
|
uid_t m_prebind_uid { 0 };
|
2020-01-04 10:05:01 +01:00
|
|
|
gid_t m_prebind_gid { 0 };
|
2020-01-03 20:14:56 +01: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 };
|
|
|
|
FileDescription* m_connect_side_fd { nullptr };
|
|
|
|
|
|
|
|
virtual Role role(const FileDescription& description) const override
|
|
|
|
{
|
|
|
|
if (m_connect_side_fd == &description)
|
|
|
|
return m_connect_side_role;
|
|
|
|
return m_role;
|
|
|
|
}
|
|
|
|
|
2019-02-14 15:17:30 +01:00
|
|
|
bool m_bound { false };
|
2019-08-11 16:28:18 +03:00
|
|
|
bool m_accept_side_fd_open { false };
|
2020-01-26 09:33:47 +01:00
|
|
|
sockaddr_un m_address { 0, { 0 } };
|
2019-02-14 15:17:30 +01:00
|
|
|
|
2021-08-01 02:34:10 -07:00
|
|
|
NonnullOwnPtr<DoubleBuffer> m_for_client;
|
|
|
|
NonnullOwnPtr<DoubleBuffer> m_for_server;
|
2019-08-10 18:58:06 +03:00
|
|
|
|
2020-06-24 22:57:37 +02:00
|
|
|
NonnullRefPtrVector<FileDescription> m_fds_for_client;
|
|
|
|
NonnullRefPtrVector<FileDescription> m_fds_for_server;
|
|
|
|
|
2021-05-26 02:38:32 -07:00
|
|
|
IntrusiveListNode<LocalSocket> m_list_node;
|
|
|
|
|
|
|
|
public:
|
|
|
|
using List = IntrusiveList<LocalSocket, RawPtr<LocalSocket>, &LocalSocket::m_list_node>;
|
2019-02-14 14:17:38 +01:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|