IPv4Socket.cpp 6.8 KB

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