TCPServer.cpp 3.3 KB

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