IPv4Socket.cpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. #include <Kernel/IPv4Socket.h>
  2. #include <Kernel/TCPSocket.h>
  3. #include <Kernel/UnixTypes.h>
  4. #include <Kernel/Process.h>
  5. #include <Kernel/NetworkAdapter.h>
  6. #include <Kernel/IPv4.h>
  7. #include <Kernel/ICMP.h>
  8. #include <Kernel/TCP.h>
  9. #include <Kernel/UDP.h>
  10. #include <Kernel/ARP.h>
  11. #include <LibC/errno_numbers.h>
  12. #define IPV4_SOCKET_DEBUG
  13. Lockable<HashMap<word, IPv4Socket*>>& IPv4Socket::sockets_by_udp_port()
  14. {
  15. static Lockable<HashMap<word, IPv4Socket*>>* s_map;
  16. if (!s_map)
  17. s_map = new Lockable<HashMap<word, IPv4Socket*>>;
  18. return *s_map;
  19. }
  20. Lockable<HashMap<word, TCPSocket*>>& IPv4Socket::sockets_by_tcp_port()
  21. {
  22. static Lockable<HashMap<word, TCPSocket*>>* s_map;
  23. if (!s_map)
  24. s_map = new Lockable<HashMap<word, TCPSocket*>>;
  25. return *s_map;
  26. }
  27. TCPSocketHandle IPv4Socket::from_tcp_port(word port)
  28. {
  29. RetainPtr<TCPSocket> socket;
  30. {
  31. LOCKER(sockets_by_tcp_port().lock());
  32. auto it = sockets_by_tcp_port().resource().find(port);
  33. if (it == sockets_by_tcp_port().resource().end())
  34. return { };
  35. socket = (*it).value;
  36. ASSERT(socket);
  37. }
  38. return { move(socket) };
  39. }
  40. IPv4SocketHandle IPv4Socket::from_udp_port(word port)
  41. {
  42. RetainPtr<IPv4Socket> socket;
  43. {
  44. LOCKER(sockets_by_udp_port().lock());
  45. auto it = sockets_by_udp_port().resource().find(port);
  46. if (it == sockets_by_udp_port().resource().end())
  47. return { };
  48. socket = (*it).value;
  49. ASSERT(socket);
  50. }
  51. return { move(socket) };
  52. }
  53. Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
  54. {
  55. static Lockable<HashTable<IPv4Socket*>>* s_table;
  56. if (!s_table)
  57. s_table = new Lockable<HashTable<IPv4Socket*>>;
  58. return *s_table;
  59. }
  60. Retained<IPv4Socket> IPv4Socket::create(int type, int protocol)
  61. {
  62. if (type == SOCK_STREAM)
  63. return TCPSocket::create(protocol);
  64. return adopt(*new IPv4Socket(type, protocol));
  65. }
  66. IPv4Socket::IPv4Socket(int type, int protocol)
  67. : Socket(AF_INET, type, protocol)
  68. {
  69. kprintf("%s(%u) IPv4Socket{%p} created with type=%u, protocol=%d\n", current->name().characters(), current->pid(), this, type, protocol);
  70. LOCKER(all_sockets().lock());
  71. all_sockets().resource().set(this);
  72. }
  73. IPv4Socket::~IPv4Socket()
  74. {
  75. {
  76. LOCKER(all_sockets().lock());
  77. all_sockets().resource().remove(this);
  78. }
  79. if (type() == SOCK_DGRAM) {
  80. LOCKER(sockets_by_udp_port().lock());
  81. sockets_by_udp_port().resource().remove(m_source_port);
  82. }
  83. if (type() == SOCK_STREAM) {
  84. LOCKER(sockets_by_tcp_port().lock());
  85. sockets_by_tcp_port().resource().remove(m_source_port);
  86. }
  87. }
  88. bool IPv4Socket::get_address(sockaddr* address, socklen_t* address_size)
  89. {
  90. // FIXME: Look into what fallback behavior we should have here.
  91. if (*address_size != sizeof(sockaddr_in))
  92. return false;
  93. memcpy(address, &m_destination_address, sizeof(sockaddr_in));
  94. *address_size = sizeof(sockaddr_in);
  95. return true;
  96. }
  97. KResult IPv4Socket::bind(const sockaddr* address, socklen_t address_size)
  98. {
  99. ASSERT(!is_connected());
  100. if (address_size != sizeof(sockaddr_in))
  101. return KResult(-EINVAL);
  102. if (address->sa_family != AF_INET)
  103. return KResult(-EINVAL);
  104. ASSERT_NOT_REACHED();
  105. }
  106. KResult IPv4Socket::connect(const sockaddr* address, socklen_t address_size)
  107. {
  108. ASSERT(!m_bound);
  109. if (address_size != sizeof(sockaddr_in))
  110. return KResult(-EINVAL);
  111. if (address->sa_family != AF_INET)
  112. return KResult(-EINVAL);
  113. auto& ia = *(const sockaddr_in*)address;
  114. m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
  115. m_destination_port = ntohs(ia.sin_port);
  116. return protocol_connect();
  117. }
  118. void IPv4Socket::attach_fd(SocketRole)
  119. {
  120. ++m_attached_fds;
  121. }
  122. void IPv4Socket::detach_fd(SocketRole)
  123. {
  124. --m_attached_fds;
  125. }
  126. bool IPv4Socket::can_read(SocketRole) const
  127. {
  128. return m_can_read;
  129. }
  130. ssize_t IPv4Socket::read(SocketRole, byte*, ssize_t)
  131. {
  132. ASSERT_NOT_REACHED();
  133. }
  134. ssize_t IPv4Socket::write(SocketRole, const byte*, ssize_t)
  135. {
  136. ASSERT_NOT_REACHED();
  137. }
  138. bool IPv4Socket::can_write(SocketRole) const
  139. {
  140. ASSERT_NOT_REACHED();
  141. }
  142. void IPv4Socket::allocate_source_port_if_needed()
  143. {
  144. if (m_source_port)
  145. return;
  146. if (type() == SOCK_DGRAM) {
  147. // This is not a very efficient allocation algorithm.
  148. // FIXME: Replace it with a bitmap or some other fast-paced looker-upper.
  149. LOCKER(sockets_by_udp_port().lock());
  150. for (word port = 2000; port < 60000; ++port) {
  151. auto it = sockets_by_udp_port().resource().find(port);
  152. if (it == sockets_by_udp_port().resource().end()) {
  153. m_source_port = port;
  154. sockets_by_udp_port().resource().set(port, this);
  155. return;
  156. }
  157. }
  158. ASSERT_NOT_REACHED();
  159. }
  160. if (type() == SOCK_STREAM) {
  161. // This is not a very efficient allocation algorithm.
  162. // FIXME: Replace it with a bitmap or some other fast-paced looker-upper.
  163. LOCKER(sockets_by_tcp_port().lock());
  164. for (word port = 2000; port < 60000; ++port) {
  165. auto it = sockets_by_tcp_port().resource().find(port);
  166. if (it == sockets_by_tcp_port().resource().end()) {
  167. m_source_port = port;
  168. sockets_by_tcp_port().resource().set(port, static_cast<TCPSocket*>(this));
  169. return;
  170. }
  171. }
  172. ASSERT_NOT_REACHED();
  173. }
  174. }
  175. ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
  176. {
  177. (void)flags;
  178. if (addr && addr_length != sizeof(sockaddr_in))
  179. return -EINVAL;
  180. // FIXME: Find the adapter some better way!
  181. auto* adapter = NetworkAdapter::from_ipv4_address(IPv4Address(192, 168, 5, 2));
  182. if (!adapter) {
  183. // FIXME: Figure out which error code to return.
  184. ASSERT_NOT_REACHED();
  185. }
  186. if (addr) {
  187. if (addr->sa_family != AF_INET) {
  188. kprintf("sendto: Bad address family: %u is not AF_INET!\n", addr->sa_family);
  189. return -EAFNOSUPPORT;
  190. }
  191. auto& ia = *(const sockaddr_in*)addr;
  192. m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
  193. m_destination_port = ntohs(ia.sin_port);
  194. }
  195. allocate_source_port_if_needed();
  196. kprintf("sendto: destination=%s:%u\n", m_destination_address.to_string().characters(), m_destination_port);
  197. if (type() == SOCK_RAW) {
  198. adapter->send_ipv4(MACAddress(), m_destination_address, (IPv4Protocol)protocol(), ByteBuffer::copy((const byte*)data, data_length));
  199. return data_length;
  200. }
  201. if (type() == SOCK_DGRAM) {
  202. auto buffer = ByteBuffer::create_zeroed(sizeof(UDPPacket) + data_length);
  203. auto& udp_packet = *(UDPPacket*)(buffer.pointer());
  204. udp_packet.set_source_port(m_source_port);
  205. udp_packet.set_destination_port(m_destination_port);
  206. udp_packet.set_length(sizeof(UDPPacket) + data_length);
  207. memcpy(udp_packet.payload(), data, data_length);
  208. kprintf("sending as udp packet from %s:%u to %s:%u!\n",
  209. adapter->ipv4_address().to_string().characters(),
  210. source_port(),
  211. m_destination_address.to_string().characters(),
  212. m_destination_port);
  213. adapter->send_ipv4(MACAddress(), m_destination_address, IPv4Protocol::UDP, move(buffer));
  214. return data_length;
  215. }
  216. if (type() == SOCK_STREAM)
  217. return protocol_send(data, data_length);
  218. ASSERT_NOT_REACHED();
  219. }
  220. ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
  221. {
  222. (void)flags;
  223. if (addr_length && *addr_length < sizeof(sockaddr_in))
  224. return -EINVAL;
  225. #ifdef IPV4_SOCKET_DEBUG
  226. kprintf("recvfrom: type=%d, source_port=%u\n", type(), source_port());
  227. #endif
  228. ByteBuffer packet_buffer;
  229. {
  230. LOCKER(lock());
  231. if (!m_receive_queue.is_empty()) {
  232. packet_buffer = m_receive_queue.take_first();
  233. m_can_read = !m_receive_queue.is_empty();
  234. }
  235. }
  236. if (packet_buffer.is_null()) {
  237. current->set_blocked_socket(this);
  238. load_receive_deadline();
  239. block(Process::BlockedReceive);
  240. Scheduler::yield();
  241. LOCKER(lock());
  242. if (!m_can_read) {
  243. // Unblocked due to timeout.
  244. return -EAGAIN;
  245. }
  246. ASSERT(m_can_read);
  247. ASSERT(!m_receive_queue.is_empty());
  248. packet_buffer = m_receive_queue.take_first();
  249. m_can_read = !m_receive_queue.is_empty();
  250. }
  251. ASSERT(!packet_buffer.is_null());
  252. auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
  253. if (addr) {
  254. auto& ia = *(sockaddr_in*)addr;
  255. memcpy(&ia.sin_addr, &m_destination_address, sizeof(IPv4Address));
  256. ia.sin_family = AF_INET;
  257. ASSERT(addr_length);
  258. *addr_length = sizeof(sockaddr_in);
  259. }
  260. if (type() == SOCK_RAW) {
  261. ASSERT(buffer_length >= ipv4_packet.payload_size());
  262. memcpy(buffer, ipv4_packet.payload(), ipv4_packet.payload_size());
  263. return ipv4_packet.payload_size();
  264. }
  265. if (type() == SOCK_DGRAM) {
  266. auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
  267. ASSERT(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
  268. ASSERT(buffer_length >= (udp_packet.length() - sizeof(UDPPacket)));
  269. if (addr) {
  270. auto& ia = *(sockaddr_in*)addr;
  271. ia.sin_port = htons(udp_packet.destination_port());
  272. }
  273. memcpy(buffer, udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket));
  274. return udp_packet.length() - sizeof(UDPPacket);
  275. }
  276. if (type() == SOCK_STREAM)
  277. return protocol_receive(packet_buffer, buffer, buffer_length, flags, addr, addr_length);
  278. ASSERT_NOT_REACHED();
  279. }
  280. void IPv4Socket::did_receive(ByteBuffer&& packet)
  281. {
  282. LOCKER(lock());
  283. m_receive_queue.append(move(packet));
  284. m_can_read = true;
  285. #ifdef IPV4_SOCKET_DEBUG
  286. kprintf("IPv4Socket(%p): did_receive %d bytes, packets in queue: %d\n", this, packet.size(), m_receive_queue.size_slow());
  287. #endif
  288. }