2019-04-06 21:29:48 +03:00
|
|
|
#include <Kernel/Net/LocalSocket.h>
|
2019-02-14 16:17:38 +03:00
|
|
|
#include <Kernel/UnixTypes.h>
|
|
|
|
#include <Kernel/Process.h>
|
2019-04-29 05:55:54 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescriptor.h>
|
2019-04-03 13:25:24 +03:00
|
|
|
#include <Kernel/FileSystem/VirtualFileSystem.h>
|
2019-02-14 16:38:30 +03:00
|
|
|
#include <LibC/errno_numbers.h>
|
2019-02-14 16:17:38 +03:00
|
|
|
|
2019-03-01 12:51:58 +03:00
|
|
|
//#define DEBUG_LOCAL_SOCKET
|
|
|
|
|
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-03-01 12:51:58 +03:00
|
|
|
#ifdef DEBUG_LOCAL_SOCKET
|
2019-03-24 00:03:17 +03:00
|
|
|
kprintf("%s(%u) LocalSocket{%p} created with type=%u\n", current->process().name().characters(), current->pid(), this, type);
|
2019-03-01 12:51:58 +03:00
|
|
|
#endif
|
2019-02-14 16:17:38 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
LocalSocket::~LocalSocket()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-05-20 21:33:03 +03:00
|
|
|
bool LocalSocket::get_local_address(sockaddr* address, socklen_t* address_size)
|
2019-02-14 17:17:30 +03:00
|
|
|
{
|
|
|
|
// 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-05-20 21:33:03 +03:00
|
|
|
bool LocalSocket::get_peer_address(sockaddr* address, socklen_t* address_size)
|
|
|
|
{
|
|
|
|
return get_local_address(address, address_size);
|
|
|
|
}
|
|
|
|
|
2019-03-07 00:14:31 +03:00
|
|
|
KResult LocalSocket::bind(const sockaddr* address, socklen_t address_size)
|
2019-02-14 16:38:30 +03:00
|
|
|
{
|
2019-02-14 19:18:35 +03:00
|
|
|
ASSERT(!is_connected());
|
2019-03-07 00:14:31 +03:00
|
|
|
if (address_size != sizeof(sockaddr_un))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
if (address->sa_family != AF_LOCAL)
|
|
|
|
return KResult(-EINVAL);
|
2019-02-14 16:38:30 +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-03-01 12:51:58 +03:00
|
|
|
#ifdef DEBUG_LOCAL_SOCKET
|
2019-03-24 00:03:17 +03:00
|
|
|
kprintf("%s(%u) LocalSocket{%p} bind(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
|
2019-03-01 12:51:58 +03:00
|
|
|
#endif
|
2019-02-14 16:38:30 +03:00
|
|
|
|
2019-05-30 21:23:50 +03:00
|
|
|
auto result = VFS::the().open(safe_address, O_CREAT | O_EXCL, S_IFSOCK | 0666, current->process().current_directory());
|
2019-03-07 00:14:31 +03:00
|
|
|
if (result.is_error()) {
|
|
|
|
if (result.error() == -EEXIST)
|
|
|
|
return KResult(-EADDRINUSE);
|
|
|
|
return result.error();
|
2019-02-14 16:38:30 +03:00
|
|
|
}
|
2019-03-07 00:14:31 +03:00
|
|
|
m_file = move(result.value());
|
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-03-07 00:14:31 +03:00
|
|
|
return KSuccess;
|
2019-02-14 16:38:30 +03:00
|
|
|
}
|
2019-02-14 17:55:19 +03:00
|
|
|
|
2019-05-03 21:15:54 +03:00
|
|
|
KResult LocalSocket::connect(FileDescriptor& descriptor, const sockaddr* address, socklen_t address_size, ShouldBlock)
|
2019-02-14 17:55:19 +03:00
|
|
|
{
|
|
|
|
ASSERT(!m_bound);
|
2019-03-07 00:14:31 +03:00
|
|
|
if (address_size != sizeof(sockaddr_un))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
if (address->sa_family != AF_LOCAL)
|
|
|
|
return KResult(-EINVAL);
|
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-03-01 12:51:58 +03:00
|
|
|
#ifdef DEBUG_LOCAL_SOCKET
|
2019-03-24 00:03:17 +03:00
|
|
|
kprintf("%s(%u) LocalSocket{%p} connect(%s)\n", current->process().name().characters(), current->pid(), this, safe_address);
|
2019-03-01 12:51:58 +03:00
|
|
|
#endif
|
2019-02-14 17:55:19 +03:00
|
|
|
|
2019-05-30 21:23:50 +03:00
|
|
|
auto descriptor_or_error = VFS::the().open(safe_address, 0, 0, current->process().current_directory());
|
2019-03-07 00:14:31 +03:00
|
|
|
if (descriptor_or_error.is_error())
|
|
|
|
return KResult(-ECONNREFUSED);
|
|
|
|
m_file = move(descriptor_or_error.value());
|
|
|
|
|
2019-02-14 17:55:19 +03:00
|
|
|
ASSERT(m_file->inode());
|
2019-03-07 00:14:31 +03:00
|
|
|
if (!m_file->inode()->socket())
|
|
|
|
return KResult(-ECONNREFUSED);
|
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();
|
2019-03-07 00:14:31 +03:00
|
|
|
auto result = peer->queue_connection_from(*this);
|
|
|
|
if (result.is_error())
|
|
|
|
return result;
|
2019-02-14 19:18:35 +03:00
|
|
|
|
2019-05-03 21:15:54 +03:00
|
|
|
return current->wait_for_connect(descriptor);
|
2019-02-14 17:55:19 +03:00
|
|
|
}
|
2019-02-14 18:01:08 +03:00
|
|
|
|
2019-05-03 21:15:54 +03:00
|
|
|
void LocalSocket::attach(FileDescriptor& descriptor)
|
2019-02-17 02:13:47 +03:00
|
|
|
{
|
2019-05-03 21:15:54 +03:00
|
|
|
switch (descriptor.socket_role()) {
|
|
|
|
case SocketRole::Accepted:
|
2019-02-17 13:00:35 +03:00
|
|
|
++m_accepted_fds_open;
|
2019-05-03 21:15:54 +03:00
|
|
|
break;
|
|
|
|
case SocketRole::Connected:
|
2019-02-17 13:00:35 +03:00
|
|
|
++m_connected_fds_open;
|
2019-05-03 21:15:54 +03:00
|
|
|
break;
|
|
|
|
case SocketRole::Connecting:
|
2019-03-20 04:33:51 +03:00
|
|
|
++m_connecting_fds_open;
|
2019-05-03 21:15:54 +03:00
|
|
|
break;
|
2019-05-03 21:42:43 +03:00
|
|
|
default:
|
|
|
|
break;
|
2019-02-17 13:00:35 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:15:54 +03:00
|
|
|
void LocalSocket::detach(FileDescriptor& descriptor)
|
2019-02-17 13:00:35 +03:00
|
|
|
{
|
2019-05-03 21:15:54 +03:00
|
|
|
switch (descriptor.socket_role()) {
|
|
|
|
case SocketRole::Accepted:
|
2019-02-17 13:00:35 +03:00
|
|
|
ASSERT(m_accepted_fds_open);
|
|
|
|
--m_accepted_fds_open;
|
2019-05-03 21:15:54 +03:00
|
|
|
break;
|
|
|
|
case SocketRole::Connected:
|
2019-02-17 13:00:35 +03:00
|
|
|
ASSERT(m_connected_fds_open);
|
|
|
|
--m_connected_fds_open;
|
2019-05-03 21:15:54 +03:00
|
|
|
break;
|
|
|
|
case SocketRole::Connecting:
|
2019-03-20 04:33:51 +03:00
|
|
|
ASSERT(m_connecting_fds_open);
|
|
|
|
--m_connecting_fds_open;
|
2019-05-03 21:15:54 +03:00
|
|
|
break;
|
2019-05-03 21:42:43 +03:00
|
|
|
default:
|
|
|
|
break;
|
2019-02-17 13:00:35 +03:00
|
|
|
}
|
2019-02-17 02:13:47 +03:00
|
|
|
}
|
|
|
|
|
2019-05-03 21:15:54 +03:00
|
|
|
bool LocalSocket::can_read(FileDescriptor& descriptor) const
|
2019-02-14 18:01:08 +03:00
|
|
|
{
|
2019-05-03 21:15:54 +03:00
|
|
|
auto role = descriptor.socket_role();
|
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-05-20 03:54:52 +03:00
|
|
|
return !has_attached_peer(descriptor) || !m_for_server.is_empty();
|
2019-02-17 13:00:35 +03:00
|
|
|
if (role == SocketRole::Connected)
|
2019-05-20 03:54:52 +03:00
|
|
|
return !has_attached_peer(descriptor) || !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-05-03 21:15:54 +03:00
|
|
|
ssize_t LocalSocket::read(FileDescriptor& descriptor, byte* buffer, ssize_t size)
|
2019-02-14 18:01:08 +03:00
|
|
|
{
|
2019-05-03 21:15:54 +03:00
|
|
|
auto role = descriptor.socket_role();
|
2019-05-20 02:18:33 +03:00
|
|
|
if (role == SocketRole::Accepted) {
|
|
|
|
if (!descriptor.is_blocking()) {
|
|
|
|
if (m_for_server.is_empty())
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2019-02-14 18:01:08 +03:00
|
|
|
return m_for_server.read(buffer, size);
|
2019-05-20 02:18:33 +03:00
|
|
|
}
|
|
|
|
if (role == SocketRole::Connected) {
|
|
|
|
if (!descriptor.is_blocking()) {
|
|
|
|
if (m_for_client.is_empty())
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2019-02-14 18:01:08 +03:00
|
|
|
return m_for_client.read(buffer, size);
|
2019-05-20 02:18:33 +03:00
|
|
|
}
|
2019-02-17 02:13:47 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-02-14 18:01:08 +03:00
|
|
|
}
|
|
|
|
|
2019-05-20 03:54:52 +03:00
|
|
|
bool LocalSocket::has_attached_peer(const FileDescriptor& descriptor) const
|
|
|
|
{
|
|
|
|
if (descriptor.socket_role() == SocketRole::Accepted)
|
|
|
|
return m_connected_fds_open || m_connecting_fds_open;
|
|
|
|
if (descriptor.socket_role() == SocketRole::Connected)
|
|
|
|
return m_accepted_fds_open;
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
2019-05-03 21:15:54 +03:00
|
|
|
ssize_t LocalSocket::write(FileDescriptor& descriptor, const byte* data, ssize_t size)
|
2019-02-14 18:01:08 +03:00
|
|
|
{
|
2019-05-20 03:54:52 +03:00
|
|
|
if (!has_attached_peer(descriptor))
|
|
|
|
return -EPIPE;
|
|
|
|
if (descriptor.socket_role() == SocketRole::Accepted)
|
2019-02-14 18:01:08 +03:00
|
|
|
return m_for_client.write(data, size);
|
2019-05-20 03:54:52 +03:00
|
|
|
if (descriptor.socket_role() == SocketRole::Connected)
|
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
|
|
|
}
|
|
|
|
|
2019-05-03 21:15:54 +03:00
|
|
|
bool LocalSocket::can_write(FileDescriptor& descriptor) const
|
2019-02-14 18:01:08 +03:00
|
|
|
{
|
2019-05-03 21:15:54 +03:00
|
|
|
if (descriptor.socket_role() == SocketRole::Accepted)
|
2019-05-20 04:57:25 +03:00
|
|
|
return !has_attached_peer(descriptor) || m_for_client.bytes_in_write_buffer() < 16384;
|
2019-05-03 21:15:54 +03:00
|
|
|
if (descriptor.socket_role() == SocketRole::Connected)
|
2019-05-20 04:57:25 +03:00
|
|
|
return !has_attached_peer(descriptor) || m_for_server.bytes_in_write_buffer() < 16384;
|
2019-02-17 02:13:47 +03:00
|
|
|
ASSERT_NOT_REACHED();
|
2019-02-14 18:01:08 +03:00
|
|
|
}
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-05-20 04:59:30 +03:00
|
|
|
ssize_t LocalSocket::sendto(FileDescriptor& descriptor, const void* data, size_t data_size, int, const sockaddr*, socklen_t)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2019-05-20 04:59:30 +03:00
|
|
|
return write(descriptor, (const byte*)data, data_size);
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2019-05-20 04:44:45 +03:00
|
|
|
ssize_t LocalSocket::recvfrom(FileDescriptor& descriptor, void* buffer, size_t buffer_size, int, sockaddr*, socklen_t*)
|
2019-03-12 19:27:07 +03:00
|
|
|
{
|
2019-05-20 04:44:45 +03:00
|
|
|
return read(descriptor, (byte*)buffer, buffer_size);
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|