TCPServer.cpp 3.1 KB

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