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-02-14 15:17:30 +01:00
|
|
|
#include <AK/HashTable.h>
|
2019-08-09 12:41:06 +10:00
|
|
|
#include <AK/NonnullRefPtrVector.h>
|
2019-06-21 18:58:45 +02:00
|
|
|
#include <AK/RefCounted.h>
|
2019-08-06 23:40:38 +10:00
|
|
|
#include <AK/RefPtr.h>
|
2019-07-09 14:46:23 +02:00
|
|
|
#include <Kernel/FileSystem/File.h>
|
2019-03-06 22:14:31 +01:00
|
|
|
#include <Kernel/KResult.h>
|
2019-05-28 11:53:16 +02:00
|
|
|
#include <Kernel/Lock.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-02-14 14:17:38 +01:00
|
|
|
|
2019-06-07 17:13:23 +02:00
|
|
|
enum class ShouldBlock {
|
2019-05-28 11:53:16 +02:00
|
|
|
No = 0,
|
|
|
|
Yes = 1
|
|
|
|
};
|
2019-02-14 16:01:08 +01:00
|
|
|
|
2019-06-07 09:36:51 +02:00
|
|
|
class FileDescription;
|
2019-05-03 20:15:54 +02:00
|
|
|
|
2019-05-03 20:42:43 +02:00
|
|
|
class Socket : public File {
|
2019-02-14 14:17:38 +01:00
|
|
|
public:
|
2019-06-21 18:37:47 +02:00
|
|
|
static KResultOr<NonnullRefPtr<Socket>> create(int domain, int type, int protocol);
|
2019-05-03 20:42:43 +02:00
|
|
|
virtual ~Socket() override;
|
2019-02-14 14:17:38 +01:00
|
|
|
|
|
|
|
int domain() const { return m_domain; }
|
|
|
|
int type() const { return m_type; }
|
|
|
|
int protocol() const { return m_protocol; }
|
|
|
|
|
2019-08-10 13:17:00 +10:00
|
|
|
enum class SetupState {
|
|
|
|
Unstarted, // we haven't tried to set the socket up yet
|
|
|
|
InProgress, // we're in the process of setting things up - for TCP maybe we've sent a SYN packet
|
|
|
|
Completed, // the setup process is complete, but not necessarily successful
|
|
|
|
};
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
enum class Role : u8 {
|
|
|
|
None,
|
|
|
|
Listener,
|
|
|
|
Accepted,
|
|
|
|
Connected,
|
|
|
|
Connecting
|
|
|
|
};
|
|
|
|
|
2019-08-10 13:17:00 +10:00
|
|
|
static const char* to_string(SetupState setup_state)
|
|
|
|
{
|
|
|
|
switch (setup_state) {
|
|
|
|
case SetupState::Unstarted:
|
|
|
|
return "Unstarted";
|
|
|
|
case SetupState::InProgress:
|
|
|
|
return "InProgress";
|
|
|
|
case SetupState::Completed:
|
|
|
|
return "Completed";
|
|
|
|
default:
|
|
|
|
return "None";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 13:17:00 +10:00
|
|
|
bool is_connected() const { return m_connected; }
|
|
|
|
void set_connected(bool connected) { m_connected = connected; }
|
|
|
|
|
2019-02-14 17:18:35 +01:00
|
|
|
bool can_accept() const { return !m_pending.is_empty(); }
|
2019-06-21 18:37:47 +02:00
|
|
|
RefPtr<Socket> accept();
|
2019-02-14 15:17:30 +01:00
|
|
|
|
2019-03-06 22:14:31 +01:00
|
|
|
virtual KResult bind(const sockaddr*, socklen_t) = 0;
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual KResult connect(FileDescription&, const sockaddr*, socklen_t, ShouldBlock) = 0;
|
2019-08-06 23:40:38 +10:00
|
|
|
virtual KResult listen(int) = 0;
|
2019-05-20 20:33:03 +02:00
|
|
|
virtual bool get_local_address(sockaddr*, socklen_t*) = 0;
|
|
|
|
virtual bool get_peer_address(sockaddr*, socklen_t*) = 0;
|
2019-02-14 15:55:19 +01:00
|
|
|
virtual bool is_local() const { return false; }
|
2019-03-12 15:51:42 +01:00
|
|
|
virtual bool is_ipv4() const { return false; }
|
2019-06-07 09:36:51 +02:00
|
|
|
virtual void attach(FileDescription&) = 0;
|
|
|
|
virtual void detach(FileDescription&) = 0;
|
|
|
|
virtual ssize_t sendto(FileDescription&, const void*, size_t, int flags, const sockaddr*, socklen_t) = 0;
|
|
|
|
virtual ssize_t recvfrom(FileDescription&, void*, size_t, int flags, sockaddr*, socklen_t*) = 0;
|
2019-02-14 16:01:08 +01:00
|
|
|
|
2019-09-19 21:40:06 +02:00
|
|
|
virtual KResult setsockopt(int level, int option, const void*, socklen_t);
|
2019-12-06 18:38:36 +01:00
|
|
|
virtual KResult getsockopt(FileDescription&, int level, int option, void*, socklen_t*);
|
2019-03-13 13:13:23 +01:00
|
|
|
|
2019-12-06 18:38:36 +01:00
|
|
|
pid_t origin_pid() const { return m_origin.pid; }
|
|
|
|
uid_t origin_uid() const { return m_origin.uid; }
|
|
|
|
gid_t origin_gid() const { return m_origin.gid; }
|
|
|
|
pid_t acceptor_pid() const { return m_acceptor.pid; }
|
|
|
|
uid_t acceptor_uid() const { return m_acceptor.uid; }
|
|
|
|
gid_t acceptor_gid() const { return m_acceptor.gid; }
|
2019-02-14 17:18:35 +01:00
|
|
|
|
2019-03-13 13:13:23 +01:00
|
|
|
timeval receive_deadline() const { return m_receive_deadline; }
|
|
|
|
timeval send_deadline() const { return m_send_deadline; }
|
|
|
|
|
2019-03-14 00:20:44 +01:00
|
|
|
Lock& lock() { return m_lock; }
|
|
|
|
|
2019-08-05 10:03:19 +02:00
|
|
|
// ^File
|
|
|
|
virtual ssize_t read(FileDescription&, u8*, ssize_t) override final;
|
|
|
|
virtual ssize_t write(FileDescription&, const u8*, ssize_t) override final;
|
2020-01-07 07:23:16 +01:00
|
|
|
virtual String absolute_path(const FileDescription&) const override = 0;
|
2019-05-03 20:42:43 +02:00
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
protected:
|
|
|
|
Socket(int domain, int type, int protocol);
|
|
|
|
|
2019-08-09 12:41:06 +10:00
|
|
|
KResult queue_connection_from(NonnullRefPtr<Socket>);
|
2019-02-14 17:18:35 +01:00
|
|
|
|
2019-03-13 13:13:23 +01:00
|
|
|
void load_receive_deadline();
|
|
|
|
void load_send_deadline();
|
|
|
|
|
2019-08-06 23:40:38 +10:00
|
|
|
int backlog() const { return m_backlog; }
|
|
|
|
void set_backlog(int backlog) { m_backlog = backlog; }
|
|
|
|
|
2019-05-03 20:42:43 +02:00
|
|
|
virtual const char* class_name() const override { return "Socket"; }
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
Role m_role { Role::None };
|
|
|
|
|
2019-12-06 18:38:36 +01:00
|
|
|
protected:
|
|
|
|
ucred m_origin { 0, 0, 0 };
|
|
|
|
ucred m_acceptor { 0, 0, 0 };
|
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
private:
|
2019-05-03 20:42:43 +02:00
|
|
|
virtual bool is_socket() const final { return true; }
|
|
|
|
|
2019-05-02 03:28:20 +02:00
|
|
|
Lock m_lock { "Socket" };
|
2019-12-06 18:38:36 +01:00
|
|
|
|
2019-02-14 14:17:38 +01:00
|
|
|
int m_domain { 0 };
|
|
|
|
int m_type { 0 };
|
|
|
|
int m_protocol { 0 };
|
2019-02-14 15:17:30 +01:00
|
|
|
int m_backlog { 0 };
|
2019-08-10 13:17:00 +10:00
|
|
|
SetupState m_setup_state { SetupState::Unstarted };
|
2019-02-14 17:18:35 +01:00
|
|
|
bool m_connected { false };
|
2019-02-14 14:17:38 +01:00
|
|
|
|
2019-03-13 13:13:23 +01:00
|
|
|
timeval m_receive_timeout { 0, 0 };
|
|
|
|
timeval m_send_timeout { 0, 0 };
|
|
|
|
|
|
|
|
timeval m_receive_deadline { 0, 0 };
|
|
|
|
timeval m_send_deadline { 0, 0 };
|
|
|
|
|
2019-08-09 12:41:06 +10:00
|
|
|
NonnullRefPtrVector<Socket> m_pending;
|
2019-02-14 15:17:30 +01:00
|
|
|
};
|
2019-03-14 09:19:24 +01:00
|
|
|
|
2019-08-09 09:16:12 +02:00
|
|
|
template<typename SocketType>
|
2019-03-14 09:19:24 +01:00
|
|
|
class SocketHandle {
|
|
|
|
public:
|
2019-05-28 11:53:16 +02:00
|
|
|
SocketHandle() {}
|
2019-03-14 09:19:24 +01:00
|
|
|
|
2019-08-09 09:16:12 +02:00
|
|
|
SocketHandle(NonnullRefPtr<SocketType>&& socket)
|
2019-03-14 09:19:24 +01: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 09:16:12 +02:00
|
|
|
SocketType* operator->() { return &socket(); }
|
|
|
|
const SocketType* operator->() const { return &socket(); }
|
2019-03-14 09:19:24 +01:00
|
|
|
|
2019-08-09 09:16:12 +02:00
|
|
|
SocketType& socket() { return *m_socket; }
|
|
|
|
const SocketType& socket() const { return *m_socket; }
|
2019-03-14 09:19:24 +01:00
|
|
|
|
|
|
|
private:
|
2019-08-09 09:16:12 +02:00
|
|
|
RefPtr<SocketType> m_socket;
|
2019-03-14 09:19:24 +01:00
|
|
|
};
|