UDPServer.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 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/UDPServer.h>
  30. #include <LibCore/UDPSocket.h>
  31. #include <errno.h>
  32. #include <stdio.h>
  33. #include <unistd.h>
  34. #ifndef SOCK_NONBLOCK
  35. # include <fcntl.h>
  36. # include <sys/ioctl.h>
  37. #endif
  38. namespace Core {
  39. UDPServer::UDPServer(Object* parent)
  40. : Object(parent)
  41. {
  42. #ifdef SOCK_NONBLOCK
  43. m_fd = socket(AF_INET, SOCK_DGRAM | SOCK_NONBLOCK | SOCK_CLOEXEC, 0);
  44. #else
  45. m_fd = socket(AF_INET, SOCK_DGRAM, 0);
  46. int option = 1;
  47. ioctl(m_fd, FIONBIO, &option);
  48. fcntl(m_fd, F_SETFD, FD_CLOEXEC);
  49. #endif
  50. VERIFY(m_fd >= 0);
  51. }
  52. UDPServer::~UDPServer()
  53. {
  54. ::close(m_fd);
  55. }
  56. bool UDPServer::bind(const IPv4Address& address, u16 port)
  57. {
  58. if (m_bound)
  59. return false;
  60. auto saddr = SocketAddress(address, port);
  61. auto in = saddr.to_sockaddr_in();
  62. if (::bind(m_fd, (const sockaddr*)&in, sizeof(in)) != 0) {
  63. perror("UDPServer::bind");
  64. return false;
  65. }
  66. m_bound = true;
  67. m_notifier = Notifier::construct(m_fd, Notifier::Event::Read, this);
  68. m_notifier->on_ready_to_read = [this] {
  69. if (on_ready_to_receive)
  70. on_ready_to_receive();
  71. };
  72. return true;
  73. }
  74. ByteBuffer UDPServer::receive(size_t size, sockaddr_in& in)
  75. {
  76. auto buf = ByteBuffer::create_uninitialized(size);
  77. socklen_t in_len = sizeof(in);
  78. ssize_t rlen = ::recvfrom(m_fd, buf.data(), size, 0, (sockaddr*)&in, &in_len);
  79. if (rlen < 0) {
  80. dbgln("recvfrom: {}", strerror(errno));
  81. return {};
  82. }
  83. buf.trim(rlen);
  84. return buf;
  85. }
  86. Optional<IPv4Address> UDPServer::local_address() const
  87. {
  88. if (m_fd == -1)
  89. return {};
  90. sockaddr_in address;
  91. socklen_t len = sizeof(address);
  92. if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
  93. return {};
  94. return IPv4Address(address.sin_addr.s_addr);
  95. }
  96. Optional<u16> UDPServer::local_port() const
  97. {
  98. if (m_fd == -1)
  99. return {};
  100. sockaddr_in address;
  101. socklen_t len = sizeof(address);
  102. if (getsockname(m_fd, (sockaddr*)&address, &len) != 0)
  103. return {};
  104. return ntohs(address.sin_port);
  105. }
  106. }