TCPServer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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/Socket.h>
  11. #include <LibCore/System.h>
  12. #include <LibCore/TCPServer.h>
  13. namespace Core {
  14. ErrorOr<NonnullRefPtr<TCPServer>> TCPServer::try_create(EventReceiver* 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, EventReceiver* parent)
  27. : EventReceiver(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, AllowAddressReuse allow_address_reuse)
  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. if (allow_address_reuse == AllowAddressReuse::Yes) {
  43. int option = 1;
  44. TRY(Core::System::setsockopt(m_fd, SOL_SOCKET, SO_REUSEADDR, &option, sizeof(option)));
  45. }
  46. TRY(Core::System::bind(m_fd, (sockaddr const*)&in, sizeof(in)));
  47. TRY(Core::System::listen(m_fd, 5));
  48. m_listening = true;
  49. m_notifier = Notifier::construct(m_fd, Notifier::Type::Read, this);
  50. m_notifier->on_activation = [this] {
  51. if (on_ready_to_accept)
  52. on_ready_to_accept();
  53. };
  54. return {};
  55. }
  56. ErrorOr<void> TCPServer::set_blocking(bool blocking)
  57. {
  58. int flags = TRY(Core::System::fcntl(m_fd, F_GETFL, 0));
  59. if (blocking)
  60. TRY(Core::System::fcntl(m_fd, F_SETFL, flags & ~O_NONBLOCK));
  61. else
  62. TRY(Core::System::fcntl(m_fd, F_SETFL, flags | O_NONBLOCK));
  63. return {};
  64. }
  65. ErrorOr<NonnullOwnPtr<TCPSocket>> TCPServer::accept()
  66. {
  67. VERIFY(m_listening);
  68. sockaddr_in in;
  69. socklen_t in_size = sizeof(in);
  70. #ifndef AK_OS_MACOS
  71. int accepted_fd = TRY(Core::System::accept4(m_fd, (sockaddr*)&in, &in_size, SOCK_NONBLOCK | SOCK_CLOEXEC));
  72. #else
  73. int accepted_fd = TRY(Core::System::accept(m_fd, (sockaddr*)&in, &in_size));
  74. #endif
  75. auto socket = TRY(TCPSocket::adopt_fd(accepted_fd));
  76. #ifdef AK_OS_MACOS
  77. // FIXME: Ideally, we should let the caller decide whether it wants the
  78. // socket to be nonblocking or not, but there are currently places
  79. // which depend on this.
  80. TRY(socket->set_blocking(false));
  81. TRY(socket->set_close_on_exec(true));
  82. #endif
  83. return socket;
  84. }
  85. Optional<IPv4Address> TCPServer::local_address() const
  86. {
  87. if (m_fd == -1)
  88. return {};
  89. sockaddr_in address;
  90. socklen_t len = sizeof(address);
  91. if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
  92. return {};
  93. return IPv4Address(address.sin_addr.s_addr);
  94. }
  95. Optional<u16> TCPServer::local_port() const
  96. {
  97. if (m_fd == -1)
  98. return {};
  99. sockaddr_in address;
  100. socklen_t len = sizeof(address);
  101. if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
  102. return {};
  103. return ntohs(address.sin_port);
  104. }
  105. }