mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:37:35 +00:00
Kernel: Move FIFO into FileSystem/ and Socket+LocalSocket into Net/.
This commit is contained in:
parent
f2580dcfeb
commit
644c887594
15 changed files with 20 additions and 20 deletions
|
@ -1,6 +1,6 @@
|
|||
#pragma once
|
||||
|
||||
#include <Kernel/Socket.h>
|
||||
#include <Kernel/Net/Socket.h>
|
||||
#include <Kernel/DoubleBuffer.h>
|
||||
#include <Kernel/Net/IPv4.h>
|
||||
#include <AK/HashMap.h>
|
||||
|
|
180
Kernel/Net/LocalSocket.cpp
Normal file
180
Kernel/Net/LocalSocket.cpp
Normal file
|
@ -0,0 +1,180 @@
|
|||
#include <Kernel/Net/LocalSocket.h>
|
||||
#include <Kernel/UnixTypes.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
||||
#include <LibC/errno_numbers.h>
|
||||
|
||||
//#define DEBUG_LOCAL_SOCKET
|
||||
|
||||
Retained<LocalSocket> LocalSocket::create(int type)
|
||||
{
|
||||
return adopt(*new LocalSocket(type));
|
||||
}
|
||||
|
||||
LocalSocket::LocalSocket(int type)
|
||||
: Socket(AF_LOCAL, type, 0)
|
||||
{
|
||||
#ifdef DEBUG_LOCAL_SOCKET
|
||||
kprintf("%s(%u) LocalSocket{%p} created with type=%u\n", current->process().name().characters(), current->pid(), this, type);
|
||||
#endif
|
||||
}
|
||||
|
||||
LocalSocket::~LocalSocket()
|
||||
{
|
||||
}
|
||||
|
||||
bool LocalSocket::get_address(sockaddr* address, socklen_t* address_size)
|
||||
{
|
||||
// FIXME: Look into what fallback behavior we should have here.
|
||||
if (*address_size != sizeof(sockaddr_un))
|
||||
return false;
|
||||
memcpy(address, &m_address, sizeof(sockaddr_un));
|
||||
*address_size = sizeof(sockaddr_un);
|
||||
return true;
|
||||
}
|
||||
|
||||
KResult LocalSocket::bind(const sockaddr* address, socklen_t address_size)
|
||||
{
|
||||
ASSERT(!is_connected());
|
||||
if (address_size != sizeof(sockaddr_un))
|
||||
return KResult(-EINVAL);
|
||||
if (address->sa_family != AF_LOCAL)
|
||||
return KResult(-EINVAL);
|
||||
|
||||
const sockaddr_un& local_address = *reinterpret_cast<const sockaddr_un*>(address);
|
||||
char safe_address[sizeof(local_address.sun_path) + 1];
|
||||
memcpy(safe_address, local_address.sun_path, sizeof(local_address.sun_path));
|
||||
|
||||
#ifdef DEBUG_LOCAL_SOCKET
|
||||
kprintf("%s(%u) LocalSocket{%p} bind(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
|
||||
#endif
|
||||
|
||||
auto result = VFS::the().open(safe_address, O_CREAT | O_EXCL, S_IFSOCK | 0666, current->process().cwd_inode());
|
||||
if (result.is_error()) {
|
||||
if (result.error() == -EEXIST)
|
||||
return KResult(-EADDRINUSE);
|
||||
return result.error();
|
||||
}
|
||||
m_file = move(result.value());
|
||||
|
||||
ASSERT(m_file->inode());
|
||||
m_file->inode()->bind_socket(*this);
|
||||
|
||||
m_address = local_address;
|
||||
m_bound = true;
|
||||
return KSuccess;
|
||||
}
|
||||
|
||||
KResult LocalSocket::connect(const sockaddr* address, socklen_t address_size)
|
||||
{
|
||||
ASSERT(!m_bound);
|
||||
if (address_size != sizeof(sockaddr_un))
|
||||
return KResult(-EINVAL);
|
||||
if (address->sa_family != AF_LOCAL)
|
||||
return KResult(-EINVAL);
|
||||
|
||||
const sockaddr_un& local_address = *reinterpret_cast<const sockaddr_un*>(address);
|
||||
char safe_address[sizeof(local_address.sun_path) + 1];
|
||||
memcpy(safe_address, local_address.sun_path, sizeof(local_address.sun_path));
|
||||
|
||||
#ifdef DEBUG_LOCAL_SOCKET
|
||||
kprintf("%s(%u) LocalSocket{%p} connect(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
|
||||
#endif
|
||||
|
||||
auto descriptor_or_error = VFS::the().open(safe_address, 0, 0, current->process().cwd_inode());
|
||||
if (descriptor_or_error.is_error())
|
||||
return KResult(-ECONNREFUSED);
|
||||
m_file = move(descriptor_or_error.value());
|
||||
|
||||
ASSERT(m_file->inode());
|
||||
if (!m_file->inode()->socket())
|
||||
return KResult(-ECONNREFUSED);
|
||||
|
||||
m_address = local_address;
|
||||
|
||||
auto peer = m_file->inode()->socket();
|
||||
auto result = peer->queue_connection_from(*this);
|
||||
if (result.is_error())
|
||||
return result;
|
||||
|
||||
return current->wait_for_connect(*this);
|
||||
}
|
||||
|
||||
void LocalSocket::attach_fd(SocketRole role)
|
||||
{
|
||||
if (role == SocketRole::Accepted) {
|
||||
++m_accepted_fds_open;
|
||||
} else if (role == SocketRole::Connected) {
|
||||
++m_connected_fds_open;
|
||||
} else if (role == SocketRole::Connecting) {
|
||||
++m_connecting_fds_open;
|
||||
}
|
||||
}
|
||||
|
||||
void LocalSocket::detach_fd(SocketRole role)
|
||||
{
|
||||
if (role == SocketRole::Accepted) {
|
||||
ASSERT(m_accepted_fds_open);
|
||||
--m_accepted_fds_open;
|
||||
} else if (role == SocketRole::Connected) {
|
||||
ASSERT(m_connected_fds_open);
|
||||
--m_connected_fds_open;
|
||||
} else if (role == SocketRole::Connecting) {
|
||||
ASSERT(m_connecting_fds_open);
|
||||
--m_connecting_fds_open;
|
||||
}
|
||||
}
|
||||
|
||||
bool LocalSocket::can_read(SocketRole role) const
|
||||
{
|
||||
if (role == SocketRole::Listener)
|
||||
return can_accept();
|
||||
if (role == SocketRole::Accepted)
|
||||
return (!m_connected_fds_open && !m_connecting_fds_open) || !m_for_server.is_empty();
|
||||
if (role == SocketRole::Connected)
|
||||
return !m_accepted_fds_open || !m_for_client.is_empty();
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
ssize_t LocalSocket::read(SocketRole role, byte* buffer, ssize_t size)
|
||||
{
|
||||
if (role == SocketRole::Accepted)
|
||||
return m_for_server.read(buffer, size);
|
||||
if (role == SocketRole::Connected)
|
||||
return m_for_client.read(buffer, size);
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
ssize_t LocalSocket::write(SocketRole role, const byte* data, ssize_t size)
|
||||
{
|
||||
if (role == SocketRole::Accepted) {
|
||||
if (!m_accepted_fds_open)
|
||||
return -EPIPE;
|
||||
return m_for_client.write(data, size);
|
||||
}
|
||||
if (role == SocketRole::Connected) {
|
||||
if (!m_connected_fds_open && !m_connecting_fds_open)
|
||||
return -EPIPE;
|
||||
return m_for_server.write(data, size);
|
||||
}
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
bool LocalSocket::can_write(SocketRole role) const
|
||||
{
|
||||
if (role == SocketRole::Accepted)
|
||||
return (!m_connected_fds_open && !m_connecting_fds_open) || m_for_client.bytes_in_write_buffer() < 4096;
|
||||
if (role == SocketRole::Connected)
|
||||
return !m_accepted_fds_open || m_for_server.bytes_in_write_buffer() < 4096;
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
ssize_t LocalSocket::sendto(const void*, size_t, int, const sockaddr*, socklen_t)
|
||||
{
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
||||
|
||||
ssize_t LocalSocket::recvfrom(void*, size_t, int flags, sockaddr*, socklen_t*)
|
||||
{
|
||||
ASSERT_NOT_REACHED();
|
||||
}
|
40
Kernel/Net/LocalSocket.h
Normal file
40
Kernel/Net/LocalSocket.h
Normal file
|
@ -0,0 +1,40 @@
|
|||
#pragma once
|
||||
|
||||
#include <Kernel/Net/Socket.h>
|
||||
#include <Kernel/DoubleBuffer.h>
|
||||
|
||||
class FileDescriptor;
|
||||
|
||||
class LocalSocket final : public Socket {
|
||||
public:
|
||||
static Retained<LocalSocket> create(int type);
|
||||
virtual ~LocalSocket() override;
|
||||
|
||||
virtual KResult bind(const sockaddr*, socklen_t) override;
|
||||
virtual KResult connect(const sockaddr*, socklen_t) override;
|
||||
virtual bool get_address(sockaddr*, socklen_t*) override;
|
||||
virtual void attach_fd(SocketRole) override;
|
||||
virtual void detach_fd(SocketRole) override;
|
||||
virtual bool can_read(SocketRole) const override;
|
||||
virtual ssize_t read(SocketRole, byte*, ssize_t) override;
|
||||
virtual ssize_t write(SocketRole, const byte*, ssize_t) override;
|
||||
virtual bool can_write(SocketRole) const override;
|
||||
virtual ssize_t sendto(const void*, size_t, int, const sockaddr*, socklen_t) override;
|
||||
virtual ssize_t recvfrom(void*, size_t, int flags, sockaddr*, socklen_t*) override;
|
||||
|
||||
private:
|
||||
explicit LocalSocket(int type);
|
||||
virtual bool is_local() const override { return true; }
|
||||
|
||||
RetainPtr<FileDescriptor> m_file;
|
||||
|
||||
bool m_bound { false };
|
||||
int m_accepted_fds_open { 0 };
|
||||
int m_connected_fds_open { 0 };
|
||||
int m_connecting_fds_open { 0 };
|
||||
sockaddr_un m_address;
|
||||
|
||||
DoubleBuffer m_for_client;
|
||||
DoubleBuffer m_for_server;
|
||||
};
|
||||
|
122
Kernel/Net/Socket.cpp
Normal file
122
Kernel/Net/Socket.cpp
Normal file
|
@ -0,0 +1,122 @@
|
|||
#include <Kernel/Net/Socket.h>
|
||||
#include <Kernel/Net/LocalSocket.h>
|
||||
#include <Kernel/Net/IPv4Socket.h>
|
||||
#include <Kernel/UnixTypes.h>
|
||||
#include <Kernel/Process.h>
|
||||
#include <LibC/errno_numbers.h>
|
||||
|
||||
KResultOr<Retained<Socket>> Socket::create(int domain, int type, int protocol)
|
||||
{
|
||||
(void)protocol;
|
||||
switch (domain) {
|
||||
case AF_LOCAL:
|
||||
return LocalSocket::create(type & SOCK_TYPE_MASK);
|
||||
case AF_INET:
|
||||
return IPv4Socket::create(type & SOCK_TYPE_MASK, protocol);
|
||||
default:
|
||||
return KResult(-EAFNOSUPPORT);
|
||||
}
|
||||
}
|
||||
|
||||
Socket::Socket(int domain, int type, int protocol)
|
||||
: m_lock("Socket")
|
||||
, m_domain(domain)
|
||||
, m_type(type)
|
||||
, m_protocol(protocol)
|
||||
{
|
||||
m_origin_pid = current->pid();
|
||||
}
|
||||
|
||||
Socket::~Socket()
|
||||
{
|
||||
}
|
||||
|
||||
KResult Socket::listen(int backlog)
|
||||
{
|
||||
LOCKER(m_lock);
|
||||
if (m_type != SOCK_STREAM)
|
||||
return KResult(-EOPNOTSUPP);
|
||||
m_backlog = backlog;
|
||||
kprintf("Socket{%p} listening with backlog=%d\n", this, m_backlog);
|
||||
return KSuccess;
|
||||
}
|
||||
|
||||
RetainPtr<Socket> Socket::accept()
|
||||
{
|
||||
LOCKER(m_lock);
|
||||
if (m_pending.is_empty())
|
||||
return nullptr;
|
||||
auto client = m_pending.take_first();
|
||||
ASSERT(!client->is_connected());
|
||||
client->m_connected = true;
|
||||
return client;
|
||||
}
|
||||
|
||||
KResult Socket::queue_connection_from(Socket& peer)
|
||||
{
|
||||
LOCKER(m_lock);
|
||||
if (m_pending.size() >= m_backlog)
|
||||
return KResult(-ECONNREFUSED);
|
||||
m_pending.append(peer);
|
||||
return KSuccess;
|
||||
}
|
||||
|
||||
KResult Socket::setsockopt(int level, int option, const void* value, socklen_t value_size)
|
||||
{
|
||||
ASSERT(level == SOL_SOCKET);
|
||||
switch (option) {
|
||||
case SO_SNDTIMEO:
|
||||
if (value_size != sizeof(timeval))
|
||||
return KResult(-EINVAL);
|
||||
m_send_timeout = *(const timeval*)value;
|
||||
return KSuccess;
|
||||
case SO_RCVTIMEO:
|
||||
if (value_size != sizeof(timeval))
|
||||
return KResult(-EINVAL);
|
||||
m_receive_timeout = *(const timeval*)value;
|
||||
return KSuccess;
|
||||
default:
|
||||
kprintf("%s(%u): setsockopt() at SOL_SOCKET with unimplemented option %d\n", option);
|
||||
return KResult(-ENOPROTOOPT);
|
||||
}
|
||||
}
|
||||
|
||||
KResult Socket::getsockopt(int level, int option, void* value, socklen_t* value_size)
|
||||
{
|
||||
ASSERT(level == SOL_SOCKET);
|
||||
switch (option) {
|
||||
case SO_SNDTIMEO:
|
||||
if (*value_size < sizeof(timeval))
|
||||
return KResult(-EINVAL);
|
||||
*(timeval*)value = m_send_timeout;
|
||||
*value_size = sizeof(timeval);
|
||||
return KSuccess;
|
||||
case SO_RCVTIMEO:
|
||||
if (*value_size < sizeof(timeval))
|
||||
return KResult(-EINVAL);
|
||||
*(timeval*)value = m_receive_timeout;
|
||||
*value_size = sizeof(timeval);
|
||||
return KSuccess;
|
||||
default:
|
||||
kprintf("%s(%u): getsockopt() at SOL_SOCKET with unimplemented option %d\n", option);
|
||||
return KResult(-ENOPROTOOPT);
|
||||
}
|
||||
}
|
||||
|
||||
void Socket::load_receive_deadline()
|
||||
{
|
||||
kgettimeofday(m_receive_deadline);
|
||||
m_receive_deadline.tv_sec += m_receive_timeout.tv_sec;
|
||||
m_receive_deadline.tv_usec += m_receive_timeout.tv_usec;
|
||||
m_receive_deadline.tv_sec += (m_send_timeout.tv_usec / 1000000) * 1;
|
||||
m_receive_deadline.tv_usec %= 1000000;
|
||||
}
|
||||
|
||||
void Socket::load_send_deadline()
|
||||
{
|
||||
kgettimeofday(m_send_deadline);
|
||||
m_send_deadline.tv_sec += m_send_timeout.tv_sec;
|
||||
m_send_deadline.tv_usec += m_send_timeout.tv_usec;
|
||||
m_send_deadline.tv_sec += (m_send_timeout.tv_usec / 1000000) * 1;
|
||||
m_send_deadline.tv_usec %= 1000000;
|
||||
}
|
114
Kernel/Net/Socket.h
Normal file
114
Kernel/Net/Socket.h
Normal file
|
@ -0,0 +1,114 @@
|
|||
#pragma once
|
||||
|
||||
#include <Kernel/Lock.h>
|
||||
#include <AK/Retainable.h>
|
||||
#include <AK/RetainPtr.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/Vector.h>
|
||||
#include <Kernel/UnixTypes.h>
|
||||
#include <Kernel/KResult.h>
|
||||
|
||||
enum class SocketRole { None, Listener, Accepted, Connected, Connecting };
|
||||
|
||||
class Socket : public Retainable<Socket> {
|
||||
public:
|
||||
static KResultOr<Retained<Socket>> create(int domain, int type, int protocol);
|
||||
virtual ~Socket();
|
||||
|
||||
int domain() const { return m_domain; }
|
||||
int type() const { return m_type; }
|
||||
int protocol() const { return m_protocol; }
|
||||
|
||||
bool can_accept() const { return !m_pending.is_empty(); }
|
||||
RetainPtr<Socket> accept();
|
||||
bool is_connected() const { return m_connected; }
|
||||
KResult listen(int backlog);
|
||||
|
||||
virtual KResult bind(const sockaddr*, socklen_t) = 0;
|
||||
virtual KResult connect(const sockaddr*, socklen_t) = 0;
|
||||
virtual bool get_address(sockaddr*, socklen_t*) = 0;
|
||||
virtual bool is_local() const { return false; }
|
||||
virtual bool is_ipv4() const { return false; }
|
||||
virtual void attach_fd(SocketRole) = 0;
|
||||
virtual void detach_fd(SocketRole) = 0;
|
||||
virtual bool can_read(SocketRole) const = 0;
|
||||
virtual ssize_t read(SocketRole, byte*, ssize_t) = 0;
|
||||
virtual ssize_t write(SocketRole, const byte*, ssize_t) = 0;
|
||||
virtual bool can_write(SocketRole) const = 0;
|
||||
virtual ssize_t sendto(const void*, size_t, int flags, const sockaddr*, socklen_t) = 0;
|
||||
virtual ssize_t recvfrom(void*, size_t, int flags, sockaddr*, socklen_t*) = 0;
|
||||
|
||||
KResult setsockopt(int level, int option, const void*, socklen_t);
|
||||
KResult getsockopt(int level, int option, void*, socklen_t*);
|
||||
|
||||
pid_t origin_pid() const { return m_origin_pid; }
|
||||
|
||||
timeval receive_deadline() const { return m_receive_deadline; }
|
||||
timeval send_deadline() const { return m_send_deadline; }
|
||||
|
||||
void set_connected(bool connected) { m_connected = connected; }
|
||||
|
||||
Lock& lock() { return m_lock; }
|
||||
|
||||
protected:
|
||||
Socket(int domain, int type, int protocol);
|
||||
|
||||
KResult queue_connection_from(Socket&);
|
||||
|
||||
void load_receive_deadline();
|
||||
void load_send_deadline();
|
||||
|
||||
private:
|
||||
Lock m_lock;
|
||||
pid_t m_origin_pid { 0 };
|
||||
int m_domain { 0 };
|
||||
int m_type { 0 };
|
||||
int m_protocol { 0 };
|
||||
int m_backlog { 0 };
|
||||
bool m_connected { false };
|
||||
|
||||
timeval m_receive_timeout { 0, 0 };
|
||||
timeval m_send_timeout { 0, 0 };
|
||||
|
||||
timeval m_receive_deadline { 0, 0 };
|
||||
timeval m_send_deadline { 0, 0 };
|
||||
|
||||
Vector<RetainPtr<Socket>> m_pending;
|
||||
};
|
||||
|
||||
class SocketHandle {
|
||||
public:
|
||||
SocketHandle() { }
|
||||
|
||||
SocketHandle(RetainPtr<Socket>&& socket)
|
||||
: 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; }
|
||||
|
||||
Socket* operator->() { return &socket(); }
|
||||
const Socket* operator->() const { return &socket(); }
|
||||
|
||||
Socket& socket() { return *m_socket; }
|
||||
const Socket& socket() const { return *m_socket; }
|
||||
|
||||
private:
|
||||
RetainPtr<Socket> m_socket;
|
||||
};
|
Loading…
Add table
Add a link
Reference in a new issue