2020-01-18 09:38:21 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
#pragma once
|
|
|
|
|
2019-08-10 18:58:06 +03:00
|
|
|
#include <AK/InlineLinkedList.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
|
|
|
|
2020-02-16 02:15:33 +01:00
|
|
|
class LocalSocket final : public Socket
|
|
|
|
, public InlineLinkedListNode<LocalSocket> {
|
2019-08-10 18:58:06 +03:00
|
|
|
friend class InlineLinkedListNode<LocalSocket>;
|
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);
|
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
|
2019-03-06 22:14:31 +01:00
|
|
|
virtual KResult bind(const sockaddr*, socklen_t) override;
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual KResult connect(FileDescription&, 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;
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual void attach(FileDescription&) override;
|
|
|
|
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-08-04 18:02:23 +02:00
|
|
|
virtual KResultOr<size_t> sendto(FileDescription&, const void*, size_t, int, const sockaddr*, socklen_t) override;
|
|
|
|
virtual KResultOr<size_t> recvfrom(FileDescription&, void*, size_t, int flags, sockaddr*, socklen_t*) override;
|
2019-12-06 18:38:36 +01:00
|
|
|
virtual KResult getsockopt(FileDescription&, int level, int option, void*, 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:
|
|
|
|
explicit LocalSocket(int type);
|
2019-08-10 18:53:33 +03:00
|
|
|
virtual const char* 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;
|
2019-08-10 18:58:06 +03:00
|
|
|
static Lockable<InlineLinkedList<LocalSocket>>& all_sockets();
|
2019-12-01 17:36:06 +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
|
|
|
|
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
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
DoubleBuffer m_for_client;
|
|
|
|
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;
|
|
|
|
|
2019-08-10 18:58:06 +03:00
|
|
|
// for InlineLinkedList
|
|
|
|
LocalSocket* m_prev { nullptr };
|
|
|
|
LocalSocket* m_next { nullptr };
|
2019-02-14 14:17:38 +01:00
|
|
|
};
|
2020-02-16 01:27:42 +01:00
|
|
|
|
|
|
|
}
|