TCPServer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. /*
  2. * Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  16. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  19. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  21. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  22. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  23. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  24. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <AK/IPv4Address.h>
  27. #include <AK/Types.h>
  28. #include <LibCore/Notifier.h>
  29. #include <LibCore/TCPServer.h>
  30. #include <LibCore/TCPSocket.h>
  31. #include <stdio.h>
  32. #include <sys/socket.h>
  33. #ifndef SOCK_NONBLOCK
  34. # include <fcntl.h>
  35. # include <sys/ioctl.h>
  36. #endif
  37. namespace Core {
  38. TCPServer::TCPServer(Object* parent)
  39. : Object(parent)
  40. {
  41. #ifdef SOCK_NONBLOCK
  42. m_fd = socket(AF_INET, SOCK_STREAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  43. #else
  44. m_fd = socket(AF_INET, SOCK_STREAM, 0);
  45. int option = 1;
  46. ioctl(m_fd, FIONBIO, &option);
  47. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  48. #endif
  49. ASSERT(m_fd >= 0);
  50. }
  51. TCPServer::~TCPServer()
  52. {
  53. ::close(m_fd);
  54. }
  55. bool TCPServer::listen(const IPv4Address& address, u16 port)
  56. {
  57. if (m_listening)
  58. return false;
  59. auto socket_address = SocketAddress(address, port);
  60. auto in = socket_address.to_sockaddr_in();
  61. if (::bind(m_fd, (const sockaddr*)&in, sizeof(in)) < 0) {
  62. perror("TCPServer::listen: bind");
  63. return false;
  64. }
  65. if (::listen(m_fd, 5) < 0) {
  66. perror("TCPServer::listen: listen");
  67. return false;
  68. }
  69. m_listening = true;
  70. m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
  71. m_notifier->on_ready_to_read = [this] {
  72. if (on_ready_to_accept)
  73. on_ready_to_accept();
  74. };
  75. return true;
  76. }
  77. RefPtr<TCPSocket> TCPServer::accept()
  78. {
  79. ASSERT(m_listening);
  80. sockaddr_in in;
  81. socklen_t in_size = sizeof(in);
  82. int accepted_fd = ::accept(m_fd, (sockaddr*)&in, &in_size);
  83. if (accepted_fd < 0) {
  84. perror("accept");
  85. return nullptr;
  86. }
  87. return TCPSocket::construct(accepted_fd);
  88. }
  89. Optional<IPv4Address> TCPServer::local_address() const
  90. {
  91. if (m_fd == -1)
  92. return {};
  93. sockaddr_in address;
  94. socklen_t len = sizeof(address);
  95. if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
  96. return {};
  97. return IPv4Address(address.sin_addr.s_addr);
  98. }
  99. Optional<u16> TCPServer::local_port() const
  100. {
  101. if (m_fd == -1)
  102. return {};
  103. sockaddr_in address;
  104. socklen_t len = sizeof(address);
  105. if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
  106. return {};
  107. return ntohs(address.sin_port);
  108. }
  109. }