2019-02-14 16:17:38 +03:00
|
|
|
#include <Kernel/LocalSocket.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
|
|
|
#include <Kernel/Process.h>
|
2019-02-14 16:38:30 +03:00
|
|
|
#include <Kernel/VirtualFileSystem.h>
|
|
|
|
#include <LibC/errno_numbers.h>
|
2019-02-14 16:17:38 +03:00
|
|
|
|
2019-02-25 14:43:52 +03:00
|
|
|
Retained<LocalSocket> LocalSocket::create(int type)
|
2019-02-14 16:17:38 +03:00
|
|
|
{
|
|
|
|
return adopt(*new LocalSocket(type));
|
|
|
|
}
|
|
|
|
|
|
|
|
LocalSocket::LocalSocket(int type)
|
|
|
|
: Socket(AF_LOCAL, type, 0)
|
|
|
|
{
|
2019-02-14 19:43:47 +03:00
|
|
|
kprintf("%s(%u) LocalSocket{%p} created with type=%u\n", current->name().characters(), current->pid(), this, type);
|
2019-02-14 16:17:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LocalSocket::~LocalSocket()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-14 17:17:30 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-02-14 16:38:30 +03:00
|
|
|
bool LocalSocket::bind(const sockaddr* address, socklen_t address_size, int& error)
|
|
|
|
{
|
2019-02-14 19:18:35 +03:00
|
|
|
ASSERT(!is_connected());
|
2019-02-14 16:38:30 +03:00
|
|
|
if (address_size != sizeof(sockaddr_un)) {
|
|
|
|
error = -EINVAL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (address->sa_family != AF_LOCAL) {
|
|
|
|
error = -EINVAL;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
2019-02-14 19:18:35 +03:00
|
|
|
kprintf("%s(%u) LocalSocket{%p} bind(%s)\n", current->name().characters(), current->pid(), this, safe_address);
|
2019-02-14 16:38:30 +03:00
|
|
|
|
2019-02-21 18:19:07 +03:00
|
|
|
m_file = VFS::the().open(safe_address, error, O_CREAT | O_EXCL, S_IFSOCK | 0666, current->cwd_inode());
|
2019-02-14 17:17:30 +03:00
|
|
|
if (!m_file) {
|
2019-02-14 16:38:30 +03:00
|
|
|
if (error == -EEXIST)
|
|
|
|
error = -EADDRINUSE;
|
2019-02-14 17:55:19 +03:00
|
|
|
return false;
|
2019-02-14 16:38:30 +03:00
|
|
|
}
|
2019-02-14 17:17:30 +03:00
|
|
|
|
2019-02-14 17:55:19 +03:00
|
|
|
ASSERT(m_file->inode());
|
|
|
|
m_file->inode()->bind_socket(*this);
|
|
|
|
|
2019-02-14 17:17:30 +03:00
|
|
|
m_address = local_address;
|
|
|
|
m_bound = true;
|
2019-02-14 16:38:30 +03:00
|
|
|
return true;
|
|
|
|
}
|
2019-02-14 17:55:19 +03:00
|
|
|
|
2019-02-14 19:18:35 +03:00
|
|
|
bool LocalSocket::connect(const sockaddr* address, socklen_t address_size, int& error)
|
2019-02-14 17:55:19 +03:00
|
|
|
{
|
|
|
|
ASSERT(!m_bound);
|
|
|
|
if (address_size != sizeof(sockaddr_un)) {
|
|
|
|
error = -EINVAL;
|
2019-02-14 19:18:35 +03:00
|
|
|
return false;
|
2019-02-14 17:55:19 +03:00
|
|
|
}
|
|
|
|
if (address->sa_family != AF_LOCAL) {
|
|
|
|
error = -EINVAL;
|
2019-02-14 19:18:35 +03:00
|
|
|
return false;
|
2019-02-14 17:55:19 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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));
|
|
|
|
|
2019-02-14 19:18:35 +03:00
|
|
|
kprintf("%s(%u) LocalSocket{%p} connect(%s)\n", current->name().characters(), current->pid(), this, safe_address);
|
2019-02-14 17:55:19 +03:00
|
|
|
|
2019-02-21 18:19:07 +03:00
|
|
|
m_file = VFS::the().open(safe_address, error, 0, 0, current->cwd_inode());
|
2019-02-14 17:55:19 +03:00
|
|
|
if (!m_file) {
|
|
|
|
error = -ECONNREFUSED;
|
2019-02-14 19:18:35 +03:00
|
|
|
return false;
|
2019-02-14 17:55:19 +03:00
|
|
|
}
|
|
|
|
ASSERT(m_file->inode());
|
2019-02-14 19:18:35 +03:00
|
|
|
if (!m_file->inode()->socket()) {
|
|
|
|
error = -ECONNREFUSED;
|
|
|
|
return false;
|
|
|
|
}
|
2019-02-14 17:55:19 +03:00
|
|
|
|
|
|
|
m_address = local_address;
|
2019-02-14 19:18:35 +03:00
|
|
|
|
|
|
|
auto peer = m_file->inode()->socket();
|
|
|
|
kprintf("Queueing up connection\n");
|
|
|
|
if (!peer->queue_connection_from(*this, error))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
kprintf("Waiting for connect...\n");
|
|
|
|
if (!current->wait_for_connect(*this, error))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
kprintf("CONNECTED!\n");
|
|
|
|
return true;
|
2019-02-14 17:55:19 +03:00
|
|
|
}
|
2019-02-14 18:01:08 +03:00
|
|
|
|
2019-02-17 13:00:35 +03:00
|
|
|
void LocalSocket::attach_fd(SocketRole role)
|
2019-02-17 02:13:47 +03:00
|
|
|
{
|
2019-02-17 13:00:35 +03:00
|
|
|
if (role == SocketRole::Accepted) {
|
|
|
|
++m_accepted_fds_open;
|
|
|
|
} else if (role == SocketRole::Connected) {
|
|
|
|
++m_connected_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;
|
|
|
|
}
|
2019-02-17 02:13:47 +03:00
|
|
|
}
|
|
|
|
|
2019-02-14 18:01:08 +03:00
|
|
|
bool LocalSocket::can_read(SocketRole role) const
|
|
|
|
{
|
2019-02-17 13:00:35 +03:00
|
|
|
if (role == SocketRole::Listener)
|
2019-02-14 18:03:37 +03:00
|
|
|
return can_accept();
|
2019-02-14 18:01:08 +03:00
|
|
|
if (role == SocketRole::Accepted)
|
2019-02-17 13:00:35 +03:00
|
|
|
return !m_connected_fds_open || !m_for_server.is_empty();
|
|
|
|
if (role == SocketRole::Connected)
|
|
|
|
return !m_accepted_fds_open || !m_for_client.is_empty();
|
2019-02-17 02:13:47 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-02-14 18:01:08 +03:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:19:57 +03:00
|
|
|
ssize_t LocalSocket::read(SocketRole role, byte* buffer, ssize_t size)
|
2019-02-14 18:01:08 +03:00
|
|
|
{
|
|
|
|
if (role == SocketRole::Accepted)
|
|
|
|
return m_for_server.read(buffer, size);
|
2019-02-17 13:00:35 +03:00
|
|
|
if (role == SocketRole::Connected)
|
2019-02-14 18:01:08 +03:00
|
|
|
return m_for_client.read(buffer, size);
|
2019-02-17 02:13:47 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-02-14 18:01:08 +03:00
|
|
|
}
|
|
|
|
|
2019-02-25 23:19:57 +03:00
|
|
|
ssize_t LocalSocket::write(SocketRole role, const byte* data, ssize_t size)
|
2019-02-14 18:01:08 +03:00
|
|
|
{
|
2019-02-17 02:13:47 +03:00
|
|
|
if (role == SocketRole::Accepted) {
|
2019-02-17 13:00:35 +03:00
|
|
|
if (!m_accepted_fds_open)
|
2019-02-17 02:13:47 +03:00
|
|
|
return -EPIPE;
|
2019-02-14 18:01:08 +03:00
|
|
|
return m_for_client.write(data, size);
|
2019-02-17 13:00:35 +03:00
|
|
|
}
|
|
|
|
if (role == SocketRole::Connected) {
|
|
|
|
if (!m_connected_fds_open)
|
2019-02-17 02:13:47 +03:00
|
|
|
return -EPIPE;
|
2019-02-14 18:01:08 +03:00
|
|
|
return m_for_server.write(data, size);
|
2019-02-17 02:13:47 +03:00
|
|
|
}
|
|
|
|
ASSERT_NOT_REACHED();
|
2019-02-14 18:01:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool LocalSocket::can_write(SocketRole role) const
|
|
|
|
{
|
|
|
|
if (role == SocketRole::Accepted)
|
2019-02-17 13:00:35 +03:00
|
|
|
return !m_connected_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;
|
2019-02-17 02:13:47 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-02-14 18:01:08 +03:00
|
|
|
}
|