UDPSocket.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include <Kernel/Net/UDPSocket.h>
  2. #include <Kernel/Net/UDP.h>
  3. #include <Kernel/Net/NetworkAdapter.h>
  4. #include <Kernel/Process.h>
  5. #include <Kernel/Devices/RandomDevice.h>
  6. #include <Kernel/Net/Routing.h>
  7. Lockable<HashMap<word, UDPSocket*>>& UDPSocket::sockets_by_port()
  8. {
  9. static Lockable<HashMap<word, UDPSocket*>>* s_map;
  10. if (!s_map)
  11. s_map = new Lockable<HashMap<word, UDPSocket*>>;
  12. return *s_map;
  13. }
  14. UDPSocketHandle UDPSocket::from_port(word port)
  15. {
  16. RetainPtr<UDPSocket> socket;
  17. {
  18. LOCKER(sockets_by_port().lock());
  19. auto it = sockets_by_port().resource().find(port);
  20. if (it == sockets_by_port().resource().end())
  21. return { };
  22. socket = (*it).value;
  23. ASSERT(socket);
  24. }
  25. return { move(socket) };
  26. }
  27. UDPSocket::UDPSocket(int protocol)
  28. : IPv4Socket(SOCK_DGRAM, protocol)
  29. {
  30. }
  31. UDPSocket::~UDPSocket()
  32. {
  33. LOCKER(sockets_by_port().lock());
  34. sockets_by_port().resource().remove(source_port());
  35. }
  36. Retained<UDPSocket> UDPSocket::create(int protocol)
  37. {
  38. return adopt(*new UDPSocket(protocol));
  39. }
  40. int UDPSocket::protocol_receive(const ByteBuffer& packet_buffer, void* buffer, size_t buffer_size, int flags, sockaddr* addr, socklen_t* addr_length)
  41. {
  42. (void)flags;
  43. (void)addr_length;
  44. ASSERT(!packet_buffer.is_null());
  45. auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
  46. auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
  47. ASSERT(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
  48. ASSERT(buffer_size >= (udp_packet.length() - sizeof(UDPPacket)));
  49. if (addr) {
  50. auto& ia = *(sockaddr_in*)addr;
  51. ia.sin_port = htons(udp_packet.destination_port());
  52. }
  53. memcpy(buffer, udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket));
  54. return udp_packet.length() - sizeof(UDPPacket);
  55. }
  56. int UDPSocket::protocol_send(const void* data, int data_length)
  57. {
  58. auto* adapter = adapter_for_route_to(destination_address());
  59. if (!adapter)
  60. return -EHOSTUNREACH;
  61. auto buffer = ByteBuffer::create_zeroed(sizeof(UDPPacket) + data_length);
  62. auto& udp_packet = *(UDPPacket*)(buffer.pointer());
  63. udp_packet.set_source_port(source_port());
  64. udp_packet.set_destination_port(destination_port());
  65. udp_packet.set_length(sizeof(UDPPacket) + data_length);
  66. memcpy(udp_packet.payload(), data, data_length);
  67. kprintf("sending as udp packet from %s:%u to %s:%u!\n",
  68. adapter->ipv4_address().to_string().characters(),
  69. source_port(),
  70. destination_address().to_string().characters(),
  71. destination_port());
  72. adapter->send_ipv4(MACAddress(), destination_address(), IPv4Protocol::UDP, move(buffer));
  73. return data_length;
  74. }
  75. int UDPSocket::protocol_allocate_source_port()
  76. {
  77. static const word first_ephemeral_port = 32768;
  78. static const word last_ephemeral_port = 60999;
  79. static const word ephemeral_port_range_size = last_ephemeral_port - first_ephemeral_port;
  80. word first_scan_port = first_ephemeral_port + RandomDevice::random_value() % ephemeral_port_range_size;
  81. LOCKER(sockets_by_port().lock());
  82. for (word port = first_scan_port;;) {
  83. auto it = sockets_by_port().resource().find(port);
  84. if (it == sockets_by_port().resource().end()) {
  85. set_source_port(port);
  86. sockets_by_port().resource().set(port, this);
  87. return port;
  88. }
  89. ++port;
  90. if (port > last_ephemeral_port)
  91. port = first_ephemeral_port;
  92. if (port == first_scan_port)
  93. break;
  94. }
  95. return -EADDRINUSE;
  96. }
  97. KResult UDPSocket::protocol_bind()
  98. {
  99. LOCKER(sockets_by_port().lock());
  100. if (sockets_by_port().resource().contains(source_port()))
  101. return KResult(-EADDRINUSE);
  102. sockets_by_port().resource().set(source_port(), this);
  103. return KSuccess;
  104. }