TCPServer.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021, Sam Atkins <atkinssj@serenityos.org>
  4. *
  5. * SPDX-License-Identifier: BSD-2-Clause
  6. */
  7. #include <AK/IPv4Address.h>
  8. #include <AK/Types.h>
  9. #include <LibCore/Notifier.h>
  10. #include <LibCore/System.h>
  11. #include <LibCore/TCPServer.h>
  12. #include <LibCore/TCPSocket.h>
  13. namespace Core {
  14. ErrorOr<NonnullRefPtr<TCPServer>> TCPServer::try_create(Object* parent)
  15. {
  16. #ifdef SOCK_NONBLOCK
  17. int fd = TRY(Core::System::socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0));
  18. #else
  19. int fd = TRY(Core::System::socket(AF_INET, SOCK_STREAM, 0));
  20. int option = 1;
  21. TRY(Core::System::ioctl(fd, FIONBIO, &option));
  22. TRY(Core::System::fcntl(fd, F_SETFD, FD_CLOEXEC));
  23. #endif
  24. return adopt_nonnull_ref_or_enomem(new (nothrow) TCPServer(fd, parent));
  25. }
  26. TCPServer::TCPServer(int fd, Object* parent)
  27. : Object(parent)
  28. , m_fd(fd)
  29. {
  30. VERIFY(m_fd >= 0);
  31. }
  32. TCPServer::~TCPServer()
  33. {
  34. MUST(Core::System::close(m_fd));
  35. }
  36. ErrorOr<void> TCPServer::listen(IPv4Address const& address, u16 port)
  37. {
  38. if (m_listening)
  39. return Error::from_errno(EADDRINUSE);
  40. auto socket_address = SocketAddress(address, port);
  41. auto in = socket_address.to_sockaddr_in();
  42. TRY(Core::System::bind(m_fd, (sockaddr const*)&in, sizeof(in)));
  43. TRY(Core::System::listen(m_fd, 5));
  44. m_listening = true;
  45. m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
  46. m_notifier->on_ready_to_read = [this] {
  47. if (on_ready_to_accept)
  48. on_ready_to_accept();
  49. };
  50. return {};
  51. }
  52. ErrorOr<void> TCPServer::set_blocking(bool blocking)
  53. {
  54. int flags = TRY(Core::System::fcntl(m_fd, F_GETFL, 0));
  55. if (blocking)
  56. TRY(Core::System::fcntl(m_fd, F_SETFL, flags & ~O_NONBLOCK));
  57. else
  58. TRY(Core::System::fcntl(m_fd, F_SETFL, flags | O_NONBLOCK));
  59. return {};
  60. }
  61. ErrorOr<NonnullOwnPtr<Stream::TCPSocket>> TCPServer::accept()
  62. {
  63. VERIFY(m_listening);
  64. sockaddr_in in;
  65. socklen_t in_size = sizeof(in);
  66. #ifndef AK_OS_MACOS
  67. int accepted_fd = TRY(Core::System::accept4(m_fd, (sockaddr*)&in, &in_size, SOCK_NONBLOCK | SOCK_CLOEXEC));
  68. #else
  69. int accepted_fd = TRY(Core::System::accept(m_fd, (sockaddr*)&in, &in_size));
  70. #endif
  71. auto socket = TRY(Stream::TCPSocket::adopt_fd(accepted_fd));
  72. #ifdef AK_OS_MACOS
  73. // FIXME: Ideally, we should let the caller decide whether it wants the
  74. // socket to be nonblocking or not, but there are currently places
  75. // which depend on this.
  76. TRY(socket->set_blocking(false));
  77. TRY(socket->set_close_on_exec(true));
  78. #endif
  79. return socket;
  80. }
  81. Optional<IPv4Address> TCPServer::local_address() const
  82. {
  83. if (m_fd == -1)
  84. return {};
  85. sockaddr_in address;
  86. socklen_t len = sizeof(address);
  87. if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
  88. return {};
  89. return IPv4Address(address.sin_addr.s_addr);
  90. }
  91. Optional<u16> TCPServer::local_port() const
  92. {
  93. if (m_fd == -1)
  94. return {};
  95. sockaddr_in address;
  96. socklen_t len = sizeof(address);
  97. if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
  98. return {};
  99. return ntohs(address.sin_port);
  100. }
  101. }