IPv4Socket.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #include <Kernel/IPv4Socket.h>
  2. #include <Kernel/UnixTypes.h>
  3. #include <Kernel/Process.h>
  4. #include <Kernel/NetworkAdapter.h>
  5. #include <Kernel/IPv4.h>
  6. #include <Kernel/ICMP.h>
  7. #include <Kernel/UDP.h>
  8. #include <Kernel/ARP.h>
  9. #include <LibC/errno_numbers.h>
  10. #define IPV4_SOCKET_DEBUG
  11. Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
  12. {
  13. static Lockable<HashTable<IPv4Socket*>>* s_table;
  14. if (!s_table)
  15. s_table = new Lockable<HashTable<IPv4Socket*>>;
  16. return *s_table;
  17. }
  18. Retained<IPv4Socket> IPv4Socket::create(int type, int protocol)
  19. {
  20. return adopt(*new IPv4Socket(type, protocol));
  21. }
  22. IPv4Socket::IPv4Socket(int type, int protocol)
  23. : Socket(AF_INET, type, protocol)
  24. , m_lock("IPv4Socket")
  25. {
  26. kprintf("%s(%u) IPv4Socket{%p} created with type=%u\n", current->name().characters(), current->pid(), this, type);
  27. LOCKER(all_sockets().lock());
  28. all_sockets().resource().set(this);
  29. }
  30. IPv4Socket::~IPv4Socket()
  31. {
  32. LOCKER(all_sockets().lock());
  33. all_sockets().resource().remove(this);
  34. }
  35. bool IPv4Socket::get_address(sockaddr* address, socklen_t* address_size)
  36. {
  37. // FIXME: Look into what fallback behavior we should have here.
  38. if (*address_size != sizeof(sockaddr_in))
  39. return false;
  40. memcpy(address, &m_destination_address, sizeof(sockaddr_in));
  41. *address_size = sizeof(sockaddr_in);
  42. return true;
  43. }
  44. KResult IPv4Socket::bind(const sockaddr* address, socklen_t address_size)
  45. {
  46. ASSERT(!is_connected());
  47. if (address_size != sizeof(sockaddr_in))
  48. return KResult(-EINVAL);
  49. if (address->sa_family != AF_INET)
  50. return KResult(-EINVAL);
  51. ASSERT_NOT_REACHED();
  52. }
  53. KResult IPv4Socket::connect(const sockaddr* address, socklen_t address_size)
  54. {
  55. ASSERT(!m_bound);
  56. if (address_size != sizeof(sockaddr_in))
  57. return KResult(-EINVAL);
  58. if (address->sa_family != AF_INET)
  59. return KResult(-EINVAL);
  60. ASSERT_NOT_REACHED();
  61. }
  62. void IPv4Socket::attach_fd(SocketRole)
  63. {
  64. ++m_attached_fds;
  65. }
  66. void IPv4Socket::detach_fd(SocketRole)
  67. {
  68. --m_attached_fds;
  69. }
  70. bool IPv4Socket::can_read(SocketRole) const
  71. {
  72. return m_can_read;
  73. }
  74. ssize_t IPv4Socket::read(SocketRole role, byte* buffer, ssize_t size)
  75. {
  76. ASSERT_NOT_REACHED();
  77. }
  78. ssize_t IPv4Socket::write(SocketRole role, const byte* data, ssize_t size)
  79. {
  80. ASSERT_NOT_REACHED();
  81. }
  82. bool IPv4Socket::can_write(SocketRole role) const
  83. {
  84. ASSERT_NOT_REACHED();
  85. }
  86. ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
  87. {
  88. (void)flags;
  89. if (addr_length != sizeof(sockaddr_in))
  90. return -EINVAL;
  91. // FIXME: Find the adapter some better way!
  92. auto* adapter = NetworkAdapter::from_ipv4_address(IPv4Address(192, 168, 5, 2));
  93. if (!adapter) {
  94. // FIXME: Figure out which error code to return.
  95. ASSERT_NOT_REACHED();
  96. }
  97. if (addr->sa_family != AF_INET) {
  98. kprintf("sendto: Bad address family: %u is not AF_INET!\n", addr->sa_family);
  99. return -EAFNOSUPPORT;
  100. }
  101. auto& ia = *(const sockaddr_in*)addr;
  102. m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
  103. m_destination_port = ia.sin_port;
  104. kprintf("sendto: destination=%s:%u\n", m_destination_address.to_string().characters(), m_destination_port);
  105. // FIXME: If we can't find the right MAC address, block until it's available?
  106. // I feel like this should happen in a layer below this code.
  107. MACAddress mac_address;
  108. adapter->send_ipv4(mac_address, m_destination_address, (IPv4Protocol)protocol(), ByteBuffer::copy((const byte*)data, data_length));
  109. return data_length;
  110. }
  111. ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
  112. {
  113. (void)flags;
  114. if (addr->sa_family != AF_INET) {
  115. kprintf("recvfrom: Bad address family: %u is not AF_INET!\n", addr->sa_family);
  116. return -EAFNOSUPPORT;
  117. }
  118. if (*addr_length < sizeof(sockaddr_in))
  119. return -EINVAL;
  120. *addr_length = sizeof(sockaddr_in);
  121. auto peer_address = IPv4Address((const byte*)&((const sockaddr_in*)addr)->sin_addr.s_addr);
  122. #ifdef IPV4_SOCKET_DEBUG
  123. kprintf("recvfrom: peer_address=%s\n", peer_address.to_string().characters());
  124. #endif
  125. ByteBuffer packet_buffer;
  126. {
  127. LOCKER(m_lock);
  128. if (!m_receive_queue.is_empty()) {
  129. packet_buffer = m_receive_queue.take_first();
  130. m_can_read = !m_receive_queue.is_empty();
  131. }
  132. }
  133. if (packet_buffer.is_null()) {
  134. current->set_blocked_socket(this);
  135. load_receive_deadline();
  136. block(Process::BlockedReceive);
  137. Scheduler::yield();
  138. LOCKER(m_lock);
  139. if (!m_can_read) {
  140. // Unblocked due to timeout.
  141. return -EAGAIN;
  142. }
  143. ASSERT(m_can_read);
  144. ASSERT(!m_receive_queue.is_empty());
  145. packet_buffer = m_receive_queue.take_first();
  146. m_can_read = !m_receive_queue.is_empty();
  147. }
  148. ASSERT(!packet_buffer.is_null());
  149. auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
  150. auto& ia = *(sockaddr_in*)addr;
  151. memcpy(&ia.sin_addr, &m_destination_address, sizeof(IPv4Address));
  152. if (type() == SOCK_RAW) {
  153. ASSERT(buffer_length >= ipv4_packet.payload_size());
  154. memcpy(buffer, ipv4_packet.payload(), ipv4_packet.payload_size());
  155. return ipv4_packet.payload_size();
  156. }
  157. if (type() == SOCK_DGRAM) {
  158. auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
  159. ASSERT(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
  160. ASSERT(buffer_length >= (udp_packet.length() - sizeof(UDPPacket)));
  161. ia.sin_port = udp_packet.destination_port();
  162. memcpy(buffer, udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket));
  163. return udp_packet.length() - sizeof(UDPPacket);
  164. }
  165. ASSERT_NOT_REACHED();
  166. }
  167. void IPv4Socket::did_receive(ByteBuffer&& packet)
  168. {
  169. LOCKER(m_lock);
  170. m_receive_queue.append(move(packet));
  171. m_can_read = true;
  172. #ifdef IPV4_SOCKET_DEBUG
  173. kprintf("IPv4Socket(%p): did_receive %d bytes, packets in queue: %d\n", this, packet.size(), m_receive_queue.size_slow());
  174. #endif
  175. }