|
@@ -4,6 +4,7 @@
|
|
#include <Kernel/NetworkAdapter.h>
|
|
#include <Kernel/NetworkAdapter.h>
|
|
#include <Kernel/IPv4.h>
|
|
#include <Kernel/IPv4.h>
|
|
#include <Kernel/ICMP.h>
|
|
#include <Kernel/ICMP.h>
|
|
|
|
+#include <Kernel/TCP.h>
|
|
#include <Kernel/UDP.h>
|
|
#include <Kernel/UDP.h>
|
|
#include <Kernel/ARP.h>
|
|
#include <Kernel/ARP.h>
|
|
#include <LibC/errno_numbers.h>
|
|
#include <LibC/errno_numbers.h>
|
|
@@ -97,6 +98,16 @@ KResult IPv4Socket::connect(const sockaddr* address, socklen_t address_size)
|
|
m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
|
m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
|
m_destination_port = ntohs(ia.sin_port);
|
|
m_destination_port = ntohs(ia.sin_port);
|
|
|
|
|
|
|
|
+ if (type() == SOCK_STREAM) {
|
|
|
|
+ // FIXME: Figure out the adapter somehow differently.
|
|
|
|
+ auto* adapter = NetworkAdapter::from_ipv4_address(IPv4Address(192, 168, 5, 2));
|
|
|
|
+ if (!adapter)
|
|
|
|
+ ASSERT_NOT_REACHED();
|
|
|
|
+ send_tcp_packet(*adapter, TCPFlags::SYN);
|
|
|
|
+ m_tcp_state = TCPState::Connecting1;
|
|
|
|
+ return KSuccess;
|
|
|
|
+ }
|
|
|
|
+
|
|
return KSuccess;
|
|
return KSuccess;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -164,10 +175,77 @@ void IPv4Socket::allocate_source_port_if_needed()
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+struct [[gnu::packed]] TCPPseudoHeader {
|
|
|
|
+ IPv4Address source;
|
|
|
|
+ IPv4Address destination;
|
|
|
|
+ byte zero;
|
|
|
|
+ byte protocol;
|
|
|
|
+ NetworkOrdered<word> payload_size;
|
|
|
|
+};
|
|
|
|
+
|
|
|
|
+NetworkOrdered<word> IPv4Socket::compute_tcp_checksum(const IPv4Address& source, const IPv4Address& destination, const TCPPacket& packet, word payload_size)
|
|
|
|
+{
|
|
|
|
+ TCPPseudoHeader pseudo_header { source, destination, 0, (byte)IPv4Protocol::TCP, sizeof(TCPPacket) + payload_size };
|
|
|
|
+
|
|
|
|
+ dword checksum = 0;
|
|
|
|
+ auto* w = (const NetworkOrdered<word>*)&pseudo_header;
|
|
|
|
+ for (size_t i = 0; i < sizeof(pseudo_header) / sizeof(word); ++i) {
|
|
|
|
+ checksum += w[i];
|
|
|
|
+ if (checksum > 0xffff)
|
|
|
|
+ checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
+ }
|
|
|
|
+ w = (const NetworkOrdered<word>*)&packet;
|
|
|
|
+ for (size_t i = 0; i < sizeof(packet) / sizeof(word); ++i) {
|
|
|
|
+ checksum += w[i];
|
|
|
|
+ if (checksum > 0xffff)
|
|
|
|
+ checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
+ }
|
|
|
|
+ ASSERT(packet.data_offset() * 4 == sizeof(TCPPacket));
|
|
|
|
+ w = (const NetworkOrdered<word>*)packet.payload();
|
|
|
|
+ for (size_t i = 0; i < payload_size / sizeof(word); ++i) {
|
|
|
|
+ checksum += w[i];
|
|
|
|
+ if (checksum > 0xffff)
|
|
|
|
+ checksum = (checksum >> 16) + (checksum & 0xffff);
|
|
|
|
+ }
|
|
|
|
+ if (payload_size & 1)
|
|
|
|
+ ASSERT_NOT_REACHED();
|
|
|
|
+ return ~(checksum & 0xffff);
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+void IPv4Socket::send_tcp_packet(NetworkAdapter& adapter, word flags, const void* payload, size_t payload_size)
|
|
|
|
+{
|
|
|
|
+ auto buffer = ByteBuffer::create_zeroed(sizeof(TCPPacket) + payload_size);
|
|
|
|
+ auto& tcp_packet = *(TCPPacket*)(buffer.pointer());
|
|
|
|
+ tcp_packet.set_source_port(m_source_port);
|
|
|
|
+ tcp_packet.set_destination_port(m_destination_port);
|
|
|
|
+ tcp_packet.set_window_size(1024);
|
|
|
|
+ tcp_packet.set_sequence_number(m_tcp_sequence_number);
|
|
|
|
+ tcp_packet.set_data_offset(5);
|
|
|
|
+ tcp_packet.set_flags(flags);
|
|
|
|
+
|
|
|
|
+ if (flags & TCPFlags::ACK)
|
|
|
|
+ tcp_packet.set_ack_number(m_tcp_ack_number);
|
|
|
|
+
|
|
|
|
+ if (flags == TCPFlags::SYN) {
|
|
|
|
+ ++m_tcp_sequence_number;
|
|
|
|
+ } else {
|
|
|
|
+ m_tcp_sequence_number += payload_size;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ memcpy(tcp_packet.payload(), payload, payload_size);
|
|
|
|
+ tcp_packet.set_checksum(compute_tcp_checksum(adapter.ipv4_address(), m_destination_address, tcp_packet, payload_size));
|
|
|
|
+ kprintf("sending tcp packet from %s:%u to %s:%u!\n",
|
|
|
|
+ adapter.ipv4_address().to_string().characters(),
|
|
|
|
+ source_port(),
|
|
|
|
+ m_destination_address.to_string().characters(),
|
|
|
|
+ m_destination_port);
|
|
|
|
+ adapter.send_ipv4(MACAddress(), m_destination_address, IPv4Protocol::TCP, move(buffer));
|
|
|
|
+}
|
|
|
|
+
|
|
ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
|
|
ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
|
|
{
|
|
{
|
|
(void)flags;
|
|
(void)flags;
|
|
- if (addr_length != sizeof(sockaddr_in))
|
|
|
|
|
|
+ if (addr && addr_length != sizeof(sockaddr_in))
|
|
return -EINVAL;
|
|
return -EINVAL;
|
|
// FIXME: Find the adapter some better way!
|
|
// FIXME: Find the adapter some better way!
|
|
auto* adapter = NetworkAdapter::from_ipv4_address(IPv4Address(192, 168, 5, 2));
|
|
auto* adapter = NetworkAdapter::from_ipv4_address(IPv4Address(192, 168, 5, 2));
|
|
@@ -176,12 +254,12 @@ ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, cons
|
|
ASSERT_NOT_REACHED();
|
|
ASSERT_NOT_REACHED();
|
|
}
|
|
}
|
|
|
|
|
|
- if (addr->sa_family != AF_INET) {
|
|
|
|
- kprintf("sendto: Bad address family: %u is not AF_INET!\n", addr->sa_family);
|
|
|
|
- return -EAFNOSUPPORT;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
if (addr) {
|
|
if (addr) {
|
|
|
|
+ if (addr->sa_family != AF_INET) {
|
|
|
|
+ kprintf("sendto: Bad address family: %u is not AF_INET!\n", addr->sa_family);
|
|
|
|
+ return -EAFNOSUPPORT;
|
|
|
|
+ }
|
|
|
|
+
|
|
auto& ia = *(const sockaddr_in*)addr;
|
|
auto& ia = *(const sockaddr_in*)addr;
|
|
m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
|
m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
|
m_destination_port = ntohs(ia.sin_port);
|
|
m_destination_port = ntohs(ia.sin_port);
|
|
@@ -191,12 +269,8 @@ ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, cons
|
|
|
|
|
|
kprintf("sendto: destination=%s:%u\n", m_destination_address.to_string().characters(), m_destination_port);
|
|
kprintf("sendto: destination=%s:%u\n", m_destination_address.to_string().characters(), m_destination_port);
|
|
|
|
|
|
- // FIXME: If we can't find the right MAC address, block until it's available?
|
|
|
|
- // I feel like this should happen in a layer below this code.
|
|
|
|
- MACAddress mac_address;
|
|
|
|
-
|
|
|
|
if (type() == SOCK_RAW) {
|
|
if (type() == SOCK_RAW) {
|
|
- adapter->send_ipv4(mac_address, m_destination_address, (IPv4Protocol)protocol(), ByteBuffer::copy((const byte*)data, data_length));
|
|
|
|
|
|
+ adapter->send_ipv4(MACAddress(), m_destination_address, (IPv4Protocol)protocol(), ByteBuffer::copy((const byte*)data, data_length));
|
|
return data_length;
|
|
return data_length;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -212,7 +286,12 @@ ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, cons
|
|
source_port(),
|
|
source_port(),
|
|
m_destination_address.to_string().characters(),
|
|
m_destination_address.to_string().characters(),
|
|
m_destination_port);
|
|
m_destination_port);
|
|
- adapter->send_ipv4(mac_address, m_destination_address, IPv4Protocol::UDP, move(buffer));
|
|
|
|
|
|
+ adapter->send_ipv4(MACAddress(), m_destination_address, IPv4Protocol::UDP, move(buffer));
|
|
|
|
+ return data_length;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (type() == SOCK_STREAM) {
|
|
|
|
+ send_tcp_packet(*adapter, 0, data, data_length);
|
|
return data_length;
|
|
return data_length;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -222,7 +301,7 @@ ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, cons
|
|
ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
|
|
ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
|
|
{
|
|
{
|
|
(void)flags;
|
|
(void)flags;
|
|
- if (*addr_length < sizeof(sockaddr_in))
|
|
|
|
|
|
+ if (addr_length && *addr_length < sizeof(sockaddr_in))
|
|
return -EINVAL;
|
|
return -EINVAL;
|
|
|
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
|
#ifdef IPV4_SOCKET_DEBUG
|
|
@@ -256,10 +335,13 @@ ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, sock
|
|
ASSERT(!packet_buffer.is_null());
|
|
ASSERT(!packet_buffer.is_null());
|
|
auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
|
|
auto& ipv4_packet = *(const IPv4Packet*)(packet_buffer.pointer());
|
|
|
|
|
|
- auto& ia = *(sockaddr_in*)addr;
|
|
|
|
- memcpy(&ia.sin_addr, &m_destination_address, sizeof(IPv4Address));
|
|
|
|
- ia.sin_family = AF_INET;
|
|
|
|
- *addr_length = sizeof(sockaddr_in);
|
|
|
|
|
|
+ if (addr) {
|
|
|
|
+ auto& ia = *(sockaddr_in*)addr;
|
|
|
|
+ memcpy(&ia.sin_addr, &m_destination_address, sizeof(IPv4Address));
|
|
|
|
+ ia.sin_family = AF_INET;
|
|
|
|
+ ASSERT(addr_length);
|
|
|
|
+ *addr_length = sizeof(sockaddr_in);
|
|
|
|
+ }
|
|
|
|
|
|
if (type() == SOCK_RAW) {
|
|
if (type() == SOCK_RAW) {
|
|
ASSERT(buffer_length >= ipv4_packet.payload_size());
|
|
ASSERT(buffer_length >= ipv4_packet.payload_size());
|
|
@@ -271,11 +353,26 @@ ssize_t IPv4Socket::recvfrom(void* buffer, size_t buffer_length, int flags, sock
|
|
auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
|
|
auto& udp_packet = *static_cast<const UDPPacket*>(ipv4_packet.payload());
|
|
ASSERT(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
|
|
ASSERT(udp_packet.length() >= sizeof(UDPPacket)); // FIXME: This should be rejected earlier.
|
|
ASSERT(buffer_length >= (udp_packet.length() - sizeof(UDPPacket)));
|
|
ASSERT(buffer_length >= (udp_packet.length() - sizeof(UDPPacket)));
|
|
- ia.sin_port = htons(udp_packet.destination_port());
|
|
|
|
|
|
+ if (addr) {
|
|
|
|
+ auto& ia = *(sockaddr_in*)addr;
|
|
|
|
+ ia.sin_port = htons(udp_packet.destination_port());
|
|
|
|
+ }
|
|
memcpy(buffer, udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket));
|
|
memcpy(buffer, udp_packet.payload(), udp_packet.length() - sizeof(UDPPacket));
|
|
return udp_packet.length() - sizeof(UDPPacket);
|
|
return udp_packet.length() - sizeof(UDPPacket);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ if (type() == SOCK_STREAM) {
|
|
|
|
+ auto& tcp_packet = *static_cast<const TCPPacket*>(ipv4_packet.payload());
|
|
|
|
+ size_t payload_size = packet_buffer.size() - sizeof(TCPPacket);
|
|
|
|
+ ASSERT(buffer_length >= payload_size);
|
|
|
|
+ if (addr) {
|
|
|
|
+ auto& ia = *(sockaddr_in*)addr;
|
|
|
|
+ ia.sin_port = htons(tcp_packet.destination_port());
|
|
|
|
+ }
|
|
|
|
+ memcpy(buffer, tcp_packet.payload(), payload_size);
|
|
|
|
+ return payload_size;
|
|
|
|
+ }
|
|
|
|
+
|
|
ASSERT_NOT_REACHED();
|
|
ASSERT_NOT_REACHED();
|
|
}
|
|
}
|
|
|
|
|
|
@@ -287,4 +384,5 @@ void IPv4Socket::did_receive(ByteBuffer&& packet)
|
|
#ifdef IPV4_SOCKET_DEBUG
|
|
#ifdef IPV4_SOCKET_DEBUG
|
|
kprintf("IPv4Socket(%p): did_receive %d bytes, packets in queue: %d\n", this, packet.size(), m_receive_queue.size_slow());
|
|
kprintf("IPv4Socket(%p): did_receive %d bytes, packets in queue: %d\n", this, packet.size(), m_receive_queue.size_slow());
|
|
#endif
|
|
#endif
|
|
|
|
+
|
|
}
|
|
}
|