diff options
author | Andreas Kling <awesomekling@gmail.com> | 2019-02-14 15:40:04 +0100 |
---|---|---|
committer | Andreas Kling <awesomekling@gmail.com> | 2019-02-14 15:40:04 +0100 |
commit | b12ab1270a8605e7f095be18facbed3f40da7a9c (patch) | |
tree | 9650e9e9b722753a1a550e8f9e4c94c94e1b9bd0 /Kernel | |
parent | a63e8839da49a25c7badcab4d42de5a8ed167e3a (diff) | |
download | serenity-b12ab1270a8605e7f095be18facbed3f40da7a9c.zip |
Kernel: More work on sockets. Getting there..
Diffstat (limited to 'Kernel')
-rw-r--r-- | Kernel/FileDescriptor.cpp | 7 | ||||
-rw-r--r-- | Kernel/FileDescriptor.h | 8 | ||||
-rw-r--r-- | Kernel/Process.cpp | 14 |
3 files changed, 22 insertions, 7 deletions
diff --git a/Kernel/FileDescriptor.cpp b/Kernel/FileDescriptor.cpp index 5b9810339b..06c6f34991 100644 --- a/Kernel/FileDescriptor.cpp +++ b/Kernel/FileDescriptor.cpp @@ -19,9 +19,9 @@ RetainPtr<FileDescriptor> FileDescriptor::create(RetainPtr<CharacterDevice>&& de return adopt(*new FileDescriptor(move(device))); } -RetainPtr<FileDescriptor> FileDescriptor::create(RetainPtr<Socket>&& socket) +RetainPtr<FileDescriptor> FileDescriptor::create(RetainPtr<Socket>&& socket, SocketRole role) { - return adopt(*new FileDescriptor(move(socket))); + return adopt(*new FileDescriptor(move(socket), role)); } RetainPtr<FileDescriptor> FileDescriptor::create_pipe_writer(FIFO& fifo) @@ -44,8 +44,9 @@ FileDescriptor::FileDescriptor(RetainPtr<CharacterDevice>&& device) { } -FileDescriptor::FileDescriptor(RetainPtr<Socket>&& socket) +FileDescriptor::FileDescriptor(RetainPtr<Socket>&& socket, SocketRole role) : m_socket(move(socket)) + , m_socket_role(role) { } diff --git a/Kernel/FileDescriptor.h b/Kernel/FileDescriptor.h index 3a9635cca1..f89f96f325 100644 --- a/Kernel/FileDescriptor.h +++ b/Kernel/FileDescriptor.h @@ -13,9 +13,12 @@ class MasterPTY; class Process; class Socket; +enum class SocketRole { None, Accepted, Connected }; + class FileDescriptor : public Retainable<FileDescriptor> { public: - static RetainPtr<FileDescriptor> create(RetainPtr<Socket>&&); + + static RetainPtr<FileDescriptor> create(RetainPtr<Socket>&&, SocketRole = SocketRole::None); static RetainPtr<FileDescriptor> create(RetainPtr<Inode>&&); static RetainPtr<FileDescriptor> create(RetainPtr<CharacterDevice>&&); static RetainPtr<FileDescriptor> create_pipe_writer(FIFO&); @@ -79,7 +82,7 @@ public: private: friend class VFS; - explicit FileDescriptor(RetainPtr<Socket>&&); + FileDescriptor(RetainPtr<Socket>&&, SocketRole); explicit FileDescriptor(RetainPtr<Inode>&&); explicit FileDescriptor(RetainPtr<CharacterDevice>&&); FileDescriptor(FIFO&, FIFO::Direction); @@ -95,6 +98,7 @@ private: dword m_file_flags { 0 }; RetainPtr<Socket> m_socket; + SocketRole m_socket_role { SocketRole::None }; RetainPtr<FIFO> m_fifo; FIFO::Direction m_fifo_direction { FIFO::Neither }; diff --git a/Kernel/Process.cpp b/Kernel/Process.cpp index 549cea4810..2ddf71d6bd 100644 --- a/Kernel/Process.cpp +++ b/Kernel/Process.cpp @@ -2303,6 +2303,13 @@ int Process::sys$accept(int sockfd, sockaddr* address, socklen_t* address_size) return -EFAULT; if (!validate_write(address, *address_size)) return -EFAULT; + if (number_of_open_file_descriptors() >= m_max_open_file_descriptors) + return -EMFILE; + int fd = 0; + for (; fd < (int)m_max_open_file_descriptors; ++fd) { + if (!m_fds[fd]) + break; + } auto* descriptor = file_descriptor(sockfd); if (!descriptor) return -EBADF; @@ -2315,8 +2322,11 @@ int Process::sys$accept(int sockfd, sockaddr* address, socklen_t* address_size) } auto client = socket.accept(); ASSERT(client); - client->get_address(address, address_size); - return 0; + bool success = client->get_address(address, address_size); + ASSERT(success); + auto client_descriptor = FileDescriptor::create(move(client), SocketRole::Accepted); + m_fds[fd].set(move(client_descriptor)); + return fd; } int Process::sys$connect(int sockfd, const sockaddr*, socklen_t) |