IPv4Socket.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. #include <Kernel/FileSystem/FileDescription.h>
  2. #include <Kernel/Net/ARP.h>
  3. #include <Kernel/Net/ICMP.h>
  4. #include <Kernel/Net/IPv4.h>
  5. #include <Kernel/Net/IPv4Socket.h>
  6. #include <Kernel/Net/NetworkAdapter.h>
  7. #include <Kernel/Net/Routing.h>
  8. #include <Kernel/Net/TCP.h>
  9. #include <Kernel/Net/TCPSocket.h>
  10. #include <Kernel/Net/UDP.h>
  11. #include <Kernel/Net/UDPSocket.h>
  12. #include <Kernel/Process.h>
  13. #include <Kernel/UnixTypes.h>
  14. #include <LibC/errno_numbers.h>
  15. #define IPV4_SOCKET_DEBUG
  16. Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
  17. {
  18. static Lockable<HashTable<IPv4Socket*>>* s_table;
  19. if (!s_table)
  20. s_table = new Lockable<HashTable<IPv4Socket*>>;
  21. return *s_table;
  22. }
  23. NonnullRefPtr<IPv4Socket> IPv4Socket::create(int type, int protocol)
  24. {
  25. if (type == SOCK_STREAM)
  26. return TCPSocket::create(protocol);
  27. if (type == SOCK_DGRAM)
  28. return UDPSocket::create(protocol);
  29. return adopt(*new IPv4Socket(type, protocol));
  30. }
  31. IPv4Socket::IPv4Socket(int type, int protocol)
  32. : Socket(AF_INET, type, protocol)
  33. {
  34. kprintf("%s(%u) IPv4Socket{%p} created with type=%u, protocol=%d\n", current->process().name().characters(), current->pid(), this, type, protocol);
  35. LOCKER(all_sockets().lock());
  36. all_sockets().resource().set(this);
  37. }
  38. IPv4Socket::~IPv4Socket()
  39. {
  40. LOCKER(all_sockets().lock());
  41. all_sockets().resource().remove(this);
  42. }
  43. bool IPv4Socket::get_local_address(sockaddr* address, socklen_t* address_size)
  44. {
  45. // FIXME: Look into what fallback behavior we should have here.
  46. if (*address_size < sizeof(sockaddr_in))
  47. return false;
  48. auto& ia = (sockaddr_in&)*address;
  49. ia.sin_family = AF_INET;
  50. ia.sin_port = m_local_port;
  51. memcpy(&ia.sin_addr, &m_local_address, sizeof(IPv4Address));
  52. *address_size = sizeof(sockaddr_in);
  53. return true;
  54. }
  55. bool IPv4Socket::get_peer_address(sockaddr* address, socklen_t* address_size)
  56. {
  57. // FIXME: Look into what fallback behavior we should have here.
  58. if (*address_size < sizeof(sockaddr_in))
  59. return false;
  60. auto& ia = (sockaddr_in&)*address;
  61. ia.sin_family = AF_INET;
  62. ia.sin_port = m_peer_port;
  63. memcpy(&ia.sin_addr, &m_peer_address, sizeof(IPv4Address));
  64. *address_size = sizeof(sockaddr_in);
  65. return true;
  66. }
  67. KResult IPv4Socket::bind(const sockaddr* address, socklen_t address_size)
  68. {
  69. ASSERT(!is_connected());
  70. if (address_size != sizeof(sockaddr_in))
  71. return KResult(-EINVAL);
  72. if (address->sa_family != AF_INET)
  73. return KResult(-EINVAL);
  74. auto& ia = *(const sockaddr_in*)address;
  75. m_local_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
  76. m_local_port = ntohs(ia.sin_port);
  77. dbgprintf("IPv4Socket::bind %s{%p} to port %u\n", class_name(), this, m_local_port);
  78. return protocol_bind();
  79. }
  80. KResult IPv4Socket::connect(FileDescription& description, const sockaddr* address, socklen_t address_size, ShouldBlock should_block)
  81. {
  82. ASSERT(!m_bound);
  83. if (address_size != sizeof(sockaddr_in))
  84. return KResult(-EINVAL);
  85. if (address->sa_family != AF_INET)
  86. return KResult(-EINVAL);
  87. auto& ia = *(const sockaddr_in*)address;
  88. m_peer_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
  89. m_peer_port = ntohs(ia.sin_port);
  90. return protocol_connect(description, should_block);
  91. }
  92. void IPv4Socket::attach(FileDescription&)
  93. {
  94. ++m_attached_fds;
  95. }
  96. void IPv4Socket::detach(FileDescription&)
  97. {
  98. --m_attached_fds;
  99. }
  100. bool IPv4Socket::can_read(FileDescription& description) const
  101. {
  102. if (description.socket_role() == SocketRole::Listener)
  103. return can_accept();
  104. if (protocol_is_disconnected())
  105. return true;
  106. return m_can_read;
  107. }
  108. ssize_t IPv4Socket::read(FileDescription& description, byte* buffer, ssize_t size)
  109. {
  110. return recvfrom(description, buffer, size, 0, nullptr, 0);
  111. }
  112. ssize_t IPv4Socket::write(FileDescription& description, const byte* data, ssize_t size)
  113. {
  114. return sendto(description, data, size, 0, nullptr, 0);
  115. }
  116. bool IPv4Socket::can_write(FileDescription&) const
  117. {
  118. return is_connected();
  119. }
  120. int IPv4Socket::allocate_local_port_if_needed()
  121. {
  122. if (m_local_port)
  123. return m_local_port;
  124. int port = protocol_allocate_local_port();
  125. if (port < 0)
  126. return port;
  127. m_local_port = (word)port;
  128. return port;
  129. }
  130. ssize_t IPv4Socket::sendto(FileDescription&, const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
  131. {
  132. (void)flags;
  133. if (addr && addr_length != sizeof(sockaddr_in))
  134. return -EINVAL;
  135. if (addr) {
  136. if (addr->sa_family != AF_INET) {
  137. kprintf("sendto: Bad address family: %u is not AF_INET!\n", addr->sa_family);
  138. return -EAFNOSUPPORT;
  139. }
  140. auto& ia = *(const sockaddr_in*)addr;
  141. m_peer_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
  142. m_peer_port = ntohs(ia.sin_port);
  143. }
  144. auto* adapter = adapter_for_route_to(m_peer_address);
  145. if (!adapter)
  146. return -EHOSTUNREACH;
  147. int rc = allocate_local_port_if_needed();
  148. if (rc < 0)
  149. return rc;
  150. kprintf("sendto: destination=%s:%u\n", m_peer_address.to_string().characters(), m_peer_port);
  151. if (type() == SOCK_RAW) {
  152. adapter->send_ipv4(MACAddress(), m_peer_address, (IPv4Protocol)protocol(), ByteBuffer::copy(data, data_length));
  153. return data_length;
  154. }
  155. return protocol_send(data, data_length);
  156. }
  157. ssize_t IPv4Socket::recvfrom(FileDescription& description, void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
  158. {
  159. (void)flags;
  160. if (addr_length && *addr_length < sizeof(sockaddr_in))
  161. return -EINVAL;
  162. #ifdef IPV4_SOCKET_DEBUG
  163. kprintf("recvfrom: type=%d, local_port=%u\n", type(), local_port());
  164. #endif
  165. ReceivedPacket packet;
  166. {
  167. LOCKER(lock());
  168. if (!m_receive_queue.is_empty()) {
  169. packet = m_receive_queue.take_first();
  170. m_can_read = !m_receive_queue.is_empty();
  171. #ifdef IPV4_SOCKET_DEBUG
  172. kprintf("IPv4Socket(%p): recvfrom without blocking %d bytes, packets in queue: %d\n", this, packet.data.size(), m_receive_queue.size_slow());
  173. #endif
  174. }
  175. }
  176. if (packet.data.is_null()) {
  177. if (protocol_is_disconnected()) {
  178. kprintf("IPv4Socket{%p} is protocol-disconnected, returning 0 in recvfrom!\n", this);
  179. return 0;
  180. }
  181. load_receive_deadline();
  182. current->block(Thread::BlockedReceive, description);
  183. LOCKER(lock());
  184. if (!m_can_read) {
  185. // Unblocked due to timeout.
  186. return -EAGAIN;
  187. }
  188. ASSERT(m_can_read);
  189. ASSERT(!m_receive_queue.is_empty());
  190. packet = m_receive_queue.take_first();
  191. m_can_read = !m_receive_queue.is_empty();
  192. #ifdef IPV4_SOCKET_DEBUG
  193. kprintf("IPv4Socket(%p): recvfrom with blocking %d bytes, packets in queue: %d\n", this, packet.data.size(), m_receive_queue.size_slow());
  194. #endif
  195. }
  196. ASSERT(!packet.data.is_null());
  197. auto& ipv4_packet = *(const IPv4Packet*)(packet.data.pointer());
  198. if (addr) {
  199. dbgprintf("Incoming packet is from: %s:%u\n", packet.peer_address.to_string().characters(), packet.peer_port);
  200. auto& ia = *(sockaddr_in*)addr;
  201. memcpy(&ia.sin_addr, &packet.peer_address, sizeof(IPv4Address));
  202. ia.sin_port = htons(packet.peer_port);
  203. ia.sin_family = AF_INET;
  204. ASSERT(addr_length);
  205. *addr_length = sizeof(sockaddr_in);
  206. }
  207. if (type() == SOCK_RAW) {
  208. ASSERT(buffer_length >= ipv4_packet.payload_size());
  209. memcpy(buffer, ipv4_packet.payload(), ipv4_packet.payload_size());
  210. return ipv4_packet.payload_size();
  211. }
  212. return protocol_receive(packet.data, buffer, buffer_length, flags);
  213. }
  214. void IPv4Socket::did_receive(const IPv4Address& source_address, word source_port, ByteBuffer&& packet)
  215. {
  216. LOCKER(lock());
  217. auto packet_size = packet.size();
  218. m_receive_queue.append({ source_address, source_port, move(packet) });
  219. m_can_read = true;
  220. m_bytes_received += packet_size;
  221. #ifdef IPV4_SOCKET_DEBUG
  222. kprintf("IPv4Socket(%p): did_receive %d bytes, total_received=%u, packets in queue: %d\n", this, packet_size, m_bytes_received, m_receive_queue.size_slow());
  223. #endif
  224. }