IPv4Socket.cpp 6.8 KB

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