UDPSocket.cpp 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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 <Kernel/Devices/RandomDevice.h>
  27. #include <Kernel/Net/NetworkAdapter.h>
  28. #include <Kernel/Net/Routing.h>
  29. #include <Kernel/Net/UDP.h>
  30. #include <Kernel/Net/UDPSocket.h>
  31. #include <Kernel/Process.h>
  32. #include <Kernel/Random.h>
  33. void UDPSocket::for_each(Function<void(UDPSocket&)> callback)
  34. {
  35. LOCKER(sockets_by_port().lock());
  36. for (auto it : sockets_by_port().resource())
  37. callback(*it.value);
  38. }
  39. Lockable<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port()
  40. {
  41. static Lockable<HashMap<u16, UDPSocket*>>* s_map;
  42. if (!s_map)
  43. s_map = new Lockable<HashMap<u16, UDPSocket*>>;
  44. return *s_map;
  45. }
  46. SocketHandle<UDPSocket> UDPSocket::from_port(u16 port)
  47. {
  48. RefPtr<UDPSocket> socket;
  49. {
  50. LOCKER(sockets_by_port().lock());
  51. auto it = sockets_by_port().resource().find(port);
  52. if (it == sockets_by_port().resource().end())
  53. return {};
  54. socket = (*it).value;
  55. ASSERT(socket);
  56. }
  57. return { *socket };
  58. }
  59. UDPSocket::UDPSocket(int protocol)
  60. : IPv4Socket(SOCK_DGRAM, protocol)
  61. {
  62. }
  63. UDPSocket::~UDPSocket()
  64. {
  65. LOCKER(sockets_by_port().lock());
  66. sockets_by_port().resource().remove(local_port());
  67. }
  68. NonnullRefPtr<UDPSocket> UDPSocket::create(int protocol)
  69. {
  70. return adopt(*new UDPSocket(protocol));
  71. }
  72. int UDPSocket::protocol_receive(const KBuffer& packet_buffer, void* buffer, size_t buffer_size, int flags)
  73. {
  74. (void)flags;
  75. auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.data());
  76. auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
  77. ASSERT(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
  78. ASSERT(buffer_size >= (udp_packet.length() - sizeof(UDPPacket)));
  79. memcpy(buffer, udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket));
  80. return udp_packet.length() - sizeof(UDPPacket);
  81. }
  82. int UDPSocket::protocol_send(const void* data, int data_length)
  83. {
  84. auto routing_decision = route_to(peer_address(), local_address());
  85. if (routing_decision.is_zero())
  86. return -EHOSTUNREACH;
  87. auto buffer = ByteBuffer::create_zeroed(sizeof(UDPPacket) + data_length);
  88. auto& udp_packet = *(UDPPacket*)(buffer.data());
  89. udp_packet.set_source_port(local_port());
  90. udp_packet.set_destination_port(peer_port());
  91. udp_packet.set_length(sizeof(UDPPacket) + data_length);
  92. memcpy(udp_packet.payload(), data, data_length);
  93. kprintf("sending as udp packet from %s:%u to %s:%u!\n",
  94. routing_decision.adapter->ipv4_address().to_string().characters(),
  95. local_port(),
  96. peer_address().to_string().characters(),
  97. peer_port());
  98. routing_decision.adapter->send_ipv4(routing_decision.next_hop, peer_address(), IPv4Protocol::UDP, buffer.data(), buffer.size(), ttl());
  99. return data_length;
  100. }
  101. KResult UDPSocket::protocol_connect(FileDescription&, ShouldBlock)
  102. {
  103. m_role = Role::Connected;
  104. set_connected(true);
  105. return KSuccess;
  106. }
  107. int UDPSocket::protocol_allocate_local_port()
  108. {
  109. static const u16 first_ephemeral_port = 32768;
  110. static const u16 last_ephemeral_port = 60999;
  111. static const u16 ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port;
  112. u16 first_scan_port = first_ephemeral_port + get_good_random<u16>() % ephemeral_port_range_size;
  113. LOCKER(sockets_by_port().lock());
  114. for (u16 port = first_scan_port;;) {
  115. auto it = sockets_by_port().resource().find(port);
  116. if (it == sockets_by_port().resource().end()) {
  117. set_local_port(port);
  118. sockets_by_port().resource().set(port, this);
  119. return port;
  120. }
  121. ++port;
  122. if (port > last_ephemeral_port)
  123. port = first_ephemeral_port;
  124. if (port == first_scan_port)
  125. break;
  126. }
  127. return -EADDRINUSE;
  128. }
  129. KResult UDPSocket::protocol_bind()
  130. {
  131. LOCKER(sockets_by_port().lock());
  132. if (sockets_by_port().resource().contains(local_port()))
  133. return KResult(-EADDRINUSE);
  134. sockets_by_port().resource().set(local_port(), this);
  135. return KSuccess;
  136. }