TCPSocket.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. #include <Kernel/TCPSocket.h>
  2. #include <Kernel/TCP.h>
  3. #include <Kernel/NetworkAdapter.h>
  4. #include <Kernel/Process.h>
  5. Lockable<HashMap<word, TCPSocket*>>& TCPSocket::sockets_by_port()
  6. {
  7. static Lockable<HashMap<word, TCPSocket*>>* s_map;
  8. if (!s_map)
  9. s_map = new Lockable<HashMap<word, TCPSocket*>>;
  10. return *s_map;
  11. }
  12. TCPSocketHandle TCPSocket::from_port(word port)
  13. {
  14. RetainPtr<TCPSocket> socket;
  15. {
  16. LOCKER(sockets_by_port().lock());
  17. auto it = sockets_by_port().resource().find(port);
  18. if (it == sockets_by_port().resource().end())
  19. return { };
  20. socket = (*it).value;
  21. ASSERT(socket);
  22. }
  23. return { move(socket) };
  24. }
  25. TCPSocket::TCPSocket(int protocol)
  26. : IPv4Socket(SOCK_STREAM, protocol)
  27. {
  28. }
  29. TCPSocket::~TCPSocket()
  30. {
  31. LOCKER(sockets_by_port().lock());
  32. sockets_by_port().resource().remove(source_port());
  33. }
  34. Retained<TCPSocket> TCPSocket::create(int protocol)
  35. {
  36. return adopt(*new TCPSocket(protocol));
  37. }
  38. int TCPSocket::protocol_receive(const ByteBuffer& packet_buffer, void* buffer, size_t buffer_size, int flags, sockaddr* addr, socklen_t* addr_length)
  39. {
  40. (void)flags;
  41. (void)addr_length;
  42. ASSERT(!packet_buffer.is_null());
  43. auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
  44. auto& tcp_packet = *static_cast<const TCPPacket*>(ipv4_packet.payload());
  45. size_t payload_size = packet_buffer.size() - sizeof(IPv4Packet) - tcp_packet.header_size();
  46. ASSERT(buffer_size >= payload_size);
  47. if (addr) {
  48. auto& ia = *(sockaddr_in*)addr;
  49. ia.sin_port = htons(tcp_packet.destination_port());
  50. }
  51. memcpy(buffer, tcp_packet.payload(), payload_size);
  52. return payload_size;
  53. }
  54. int TCPSocket::protocol_send(const void* data, int data_length)
  55. {
  56. // FIXME: Figure out the adapter somehow differently.
  57. auto* adapter = NetworkAdapter::from_ipv4_address(IPv4Address(192, 168, 5, 2));
  58. if (!adapter)
  59. ASSERT_NOT_REACHED();
  60. send_tcp_packet(TCPFlags::PUSH | TCPFlags::ACK, data, data_length);
  61. return data_length;
  62. }
  63. void TCPSocket::send_tcp_packet(word flags, const void* payload, int payload_size)
  64. {
  65. // FIXME: Figure out the adapter somehow differently.
  66. auto& adapter = *NetworkAdapter::from_ipv4_address(IPv4Address(192, 168, 5, 2));
  67. auto buffer = ByteBuffer::create_zeroed(sizeof(TCPPacket) + payload_size);
  68. auto& tcp_packet = *(TCPPacket*)(buffer.pointer());
  69. ASSERT(source_port());
  70. tcp_packet.set_source_port(source_port());
  71. tcp_packet.set_destination_port(destination_port());
  72. tcp_packet.set_window_size(1024);
  73. tcp_packet.set_sequence_number(m_sequence_number);
  74. tcp_packet.set_data_offset(sizeof(TCPPacket) / sizeof(dword));
  75. tcp_packet.set_flags(flags);
  76. if (flags & TCPFlags::ACK)
  77. tcp_packet.set_ack_number(m_ack_number);
  78. if (flags == TCPFlags::SYN) {
  79. ++m_sequence_number;
  80. } else {
  81. m_sequence_number += payload_size;
  82. }
  83. memcpy(tcp_packet.payload(), payload, payload_size);
  84. tcp_packet.set_checksum(compute_tcp_checksum(adapter.ipv4_address(), destination_address(), tcp_packet, payload_size));
  85. kprintf("sending tcp packet from %s:%u to %s:%u with (%s %s) seq_no=%u, ack_no=%u\n",
  86. adapter.ipv4_address().to_string().characters(),
  87. source_port(),
  88. destination_address().to_string().characters(),
  89. destination_port(),
  90. tcp_packet.has_syn() ? "SYN" : "",
  91. tcp_packet.has_ack() ? "ACK" : "",
  92. tcp_packet.sequence_number(),
  93. tcp_packet.ack_number()
  94. );
  95. adapter.send_ipv4(MACAddress(), destination_address(), IPv4Protocol::TCP, move(buffer));
  96. }
  97. NetworkOrdered<word> TCPSocket::compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket& packet, word payload_size)
  98. {
  99. struct [[gnu::packed]] PseudoHeader {
  100. IPv4Address source;
  101. IPv4Address destination;
  102. byte zero;
  103. byte protocol;
  104. NetworkOrdered<word> payload_size;
  105. };
  106. PseudoHeader pseudo_header { source, destination, 0, (byte)IPv4Protocol::TCP, sizeof(TCPPacket) + payload_size };
  107. dword checksum = 0;
  108. auto* w = (const NetworkOrdered<word>*)&pseudo_header;
  109. for (size_t i = 0; i < sizeof(pseudo_header) / sizeof(word); ++i) {
  110. checksum += w[i];
  111. if (checksum > 0xffff)
  112. checksum = (checksum >> 16) + (checksum & 0xffff);
  113. }
  114. w = (const NetworkOrdered<word>*)&packet;
  115. for (size_t i = 0; i < sizeof(packet) / sizeof(word); ++i) {
  116. checksum += w[i];
  117. if (checksum > 0xffff)
  118. checksum = (checksum >> 16) + (checksum & 0xffff);
  119. }
  120. ASSERT(packet.data_offset() * 4 == sizeof(TCPPacket));
  121. w = (const NetworkOrdered<word>*)packet.payload();
  122. for (size_t i = 0; i < payload_size / sizeof(word); ++i) {
  123. checksum += w[i];
  124. if (checksum > 0xffff)
  125. checksum = (checksum >> 16) + (checksum & 0xffff);
  126. }
  127. if (payload_size & 1) {
  128. word expanded_byte = ((const byte*)packet.payload())[payload_size - 1];
  129. checksum += expanded_byte;
  130. if (checksum > 0xffff)
  131. checksum = (checksum >> 16) + (checksum & 0xffff);
  132. }
  133. return ~(checksum & 0xffff);
  134. }
  135. KResult TCPSocket::protocol_connect()
  136. {
  137. // FIXME: Figure out the adapter somehow differently.
  138. auto* adapter = NetworkAdapter::from_ipv4_address(IPv4Address(192, 168, 5, 2));
  139. if (!adapter)
  140. ASSERT_NOT_REACHED();
  141. allocate_source_port_if_needed();
  142. m_sequence_number = 0;
  143. m_ack_number = 0;
  144. send_tcp_packet(TCPFlags::SYN);
  145. m_state = State::Connecting;
  146. current->set_blocked_socket(this);
  147. block(Process::BlockedConnect);
  148. Scheduler::yield();
  149. ASSERT(is_connected());
  150. return KSuccess;
  151. }
  152. void TCPSocket::protocol_allocate_source_port()
  153. {
  154. // This is not a very efficient allocation algorithm.
  155. // FIXME: Replace it with a bitmap or some other fast-paced looker-upper.
  156. LOCKER(sockets_by_port().lock());
  157. for (word port = 2000; port < 60000; ++port) {
  158. auto it = sockets_by_port().resource().find(port);
  159. if (it == sockets_by_port().resource().end()) {
  160. set_source_port(port);
  161. sockets_by_port().resource().set(port, this);
  162. return;
  163. }
  164. }
  165. }
  166. bool TCPSocket::protocol_is_disconnected() const
  167. {
  168. return m_state == State::Disconnecting || m_state == State::Disconnected;
  169. }