IPv4Socket.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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->process().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. int IPv4Socket::allocate_source_port_if_needed()
  98. {
  99. if (m_source_port)
  100. return m_source_port;
  101. int port = protocol_allocate_source_port();
  102. if (port < 0)
  103. return port;
  104. m_source_port = (word)port;
  105. return port;
  106. }
  107. ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
  108. {
  109. (void)flags;
  110. if (addr && addr_length != sizeof(sockaddr_in))
  111. return -EINVAL;
  112. // FIXME: Find the adapter some better way!
  113. auto* adapter = NetworkAdapter::from_ipv4_address(IPv4Address(192, 168, 5, 2));
  114. if (!adapter) {
  115. // FIXME: Figure out which error code to return.
  116. ASSERT_NOT_REACHED();
  117. }
  118. if (addr) {
  119. if (addr->sa_family != AF_INET) {
  120. kprintf("sendto: Bad address family: %u is not AF_INET!\n", addr->sa_family);
  121. return -EAFNOSUPPORT;
  122. }
  123. auto& ia = *(const sockaddr_in*)addr;
  124. m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
  125. m_destination_port = ntohs(ia.sin_port);
  126. }
  127. int rc = allocate_source_port_if_needed();
  128. if (rc < 0)
  129. return rc;
  130. kprintf("sendto: destination=%s:%u\n", m_destination_address.to_string().characters(), m_destination_port);
  131. if (type() == SOCK_RAW) {
  132. adapter->send_ipv4(MACAddress(), m_destination_address, (IPv4Protocol)protocol(), ByteBuffer::copy(data, data_length));
  133. return data_length;
  134. }
  135. return protocol_send(data, data_length);
  136. }
  137. ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
  138. {
  139. (void)flags;
  140. if (addr_length && *addr_length < sizeof(sockaddr_in))
  141. return -EINVAL;
  142. #ifdef IPV4_SOCKET_DEBUG
  143. kprintf("recvfrom: type=%d, source_port=%u\n", type(), source_port());
  144. #endif
  145. ByteBuffer packet_buffer;
  146. {
  147. LOCKER(lock());
  148. if (!m_receive_queue.is_empty()) {
  149. packet_buffer = m_receive_queue.take_first();
  150. m_can_read = !m_receive_queue.is_empty();
  151. #ifdef IPV4_SOCKET_DEBUG
  152. kprintf("IPv4Socket(%p): recvfrom without blocking %d bytes, packets in queue: %d\n", this, packet_buffer.size(), m_receive_queue.size_slow());
  153. #endif
  154. }
  155. }
  156. if (packet_buffer.is_null()) {
  157. if (protocol_is_disconnected()) {
  158. kprintf("IPv4Socket{%p} is protocol-disconnected, returning 0 in recvfrom!\n", this);
  159. return 0;
  160. }
  161. current->set_blocked_socket(this);
  162. load_receive_deadline();
  163. block(Thread::BlockedReceive);
  164. Scheduler::yield();
  165. LOCKER(lock());
  166. if (!m_can_read) {
  167. // Unblocked due to timeout.
  168. return -EAGAIN;
  169. }
  170. ASSERT(m_can_read);
  171. ASSERT(!m_receive_queue.is_empty());
  172. packet_buffer = m_receive_queue.take_first();
  173. m_can_read = !m_receive_queue.is_empty();
  174. #ifdef IPV4_SOCKET_DEBUG
  175. kprintf("IPv4Socket(%p): recvfrom with blocking %d bytes, packets in queue: %d\n", this, packet_buffer.size(), m_receive_queue.size_slow());
  176. #endif
  177. }
  178. ASSERT(!packet_buffer.is_null());
  179. auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
  180. if (addr) {
  181. auto& ia = *(sockaddr_in*)addr;
  182. memcpy(&ia.sin_addr, &m_destination_address, sizeof(IPv4Address));
  183. ia.sin_family = AF_INET;
  184. ASSERT(addr_length);
  185. *addr_length = sizeof(sockaddr_in);
  186. }
  187. if (type() == SOCK_RAW) {
  188. ASSERT(buffer_length >= ipv4_packet.payload_size());
  189. memcpy(buffer, ipv4_packet.payload(), ipv4_packet.payload_size());
  190. return ipv4_packet.payload_size();
  191. }
  192. return protocol_receive(packet_buffer, buffer, buffer_length, flags, addr, addr_length);
  193. }
  194. void IPv4Socket::did_receive(ByteBuffer&& packet)
  195. {
  196. LOCKER(lock());
  197. auto packet_size = packet.size();
  198. m_receive_queue.append(move(packet));
  199. m_can_read = true;
  200. m_bytes_received += packet_size;
  201. #ifdef IPV4_SOCKET_DEBUG
  202. 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());
  203. #endif
  204. }