2019-08-10 19:10:36 +03:00
|
|
|
#include <AK/StringBuilder.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/FileSystem/FileDescription.h>
|
|
|
|
#include <Kernel/Net/ARP.h>
|
|
|
|
#include <Kernel/Net/ICMP.h>
|
|
|
|
#include <Kernel/Net/IPv4.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/IPv4Socket.h>
|
|
|
|
#include <Kernel/Net/NetworkAdapter.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/Routing.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/TCP.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/TCPSocket.h>
|
2019-04-02 20:54:38 +03:00
|
|
|
#include <Kernel/Net/UDP.h>
|
2019-06-07 12:43:58 +03:00
|
|
|
#include <Kernel/Net/UDPSocket.h>
|
|
|
|
#include <Kernel/Process.h>
|
|
|
|
#include <Kernel/UnixTypes.h>
|
2019-03-12 17:51:42 +03:00
|
|
|
#include <LibC/errno_numbers.h>
|
|
|
|
|
2019-08-04 11:04:06 +03:00
|
|
|
//#define IPV4_SOCKET_DEBUG
|
2019-03-13 16:22:27 +03:00
|
|
|
|
2019-03-12 19:27:07 +03:00
|
|
|
Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
|
|
|
|
{
|
|
|
|
static Lockable<HashTable<IPv4Socket*>>* s_table;
|
|
|
|
if (!s_table)
|
|
|
|
s_table = new Lockable<HashTable<IPv4Socket*>>;
|
|
|
|
return *s_table;
|
|
|
|
}
|
|
|
|
|
2019-06-21 19:37:47 +03:00
|
|
|
NonnullRefPtr<IPv4Socket> IPv4Socket::create(int type, int protocol)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2019-03-14 14:20:38 +03:00
|
|
|
if (type == SOCK_STREAM)
|
|
|
|
return TCPSocket::create(protocol);
|
2019-03-14 14:43:18 +03:00
|
|
|
if (type == SOCK_DGRAM)
|
|
|
|
return UDPSocket::create(protocol);
|
2019-03-12 17:51:42 +03:00
|
|
|
return adopt(*new IPv4Socket(type, protocol));
|
|
|
|
}
|
|
|
|
|
|
|
|
IPv4Socket::IPv4Socket(int type, int protocol)
|
|
|
|
: Socket(AF_INET, type, protocol)
|
|
|
|
{
|
2019-03-24 00:03:17 +03:00
|
|
|
kprintf("%s(%u) IPv4Socket{%p} created with type=%u, protocol=%d\n", current->process().name().characters(), current->pid(), this, type, protocol);
|
2019-03-12 19:27:07 +03:00
|
|
|
LOCKER(all_sockets().lock());
|
|
|
|
all_sockets().resource().set(this);
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
IPv4Socket::~IPv4Socket()
|
|
|
|
{
|
2019-03-14 14:43:18 +03:00
|
|
|
LOCKER(all_sockets().lock());
|
|
|
|
all_sockets().resource().remove(this);
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2019-05-20 21:33:03 +03:00
|
|
|
bool IPv4Socket::get_local_address(sockaddr* address, socklen_t* address_size)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
|
|
|
// FIXME: Look into what fallback behavior we should have here.
|
2019-05-20 21:33:03 +03:00
|
|
|
if (*address_size < sizeof(sockaddr_in))
|
2019-03-12 17:51:42 +03:00
|
|
|
return false;
|
2019-05-20 21:33:03 +03:00
|
|
|
auto& ia = (sockaddr_in&)*address;
|
|
|
|
ia.sin_family = AF_INET;
|
2019-08-10 05:23:59 +03:00
|
|
|
ia.sin_port = htons(m_local_port);
|
2019-05-20 21:33:03 +03:00
|
|
|
memcpy(&ia.sin_addr, &m_local_address, sizeof(IPv4Address));
|
|
|
|
*address_size = sizeof(sockaddr_in);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool IPv4Socket::get_peer_address(sockaddr* address, socklen_t* address_size)
|
|
|
|
{
|
|
|
|
// FIXME: Look into what fallback behavior we should have here.
|
|
|
|
if (*address_size < sizeof(sockaddr_in))
|
|
|
|
return false;
|
|
|
|
auto& ia = (sockaddr_in&)*address;
|
|
|
|
ia.sin_family = AF_INET;
|
2019-08-10 05:23:59 +03:00
|
|
|
ia.sin_port = htons(m_peer_port);
|
2019-05-20 21:33:03 +03:00
|
|
|
memcpy(&ia.sin_addr, &m_peer_address, sizeof(IPv4Address));
|
2019-03-12 17:51:42 +03:00
|
|
|
*address_size = sizeof(sockaddr_in);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
KResult IPv4Socket::bind(const sockaddr* address, socklen_t address_size)
|
|
|
|
{
|
2019-08-10 06:17:00 +03:00
|
|
|
ASSERT(setup_state() == SetupState::Unstarted);
|
2019-03-12 17:51:42 +03:00
|
|
|
if (address_size != sizeof(sockaddr_in))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
if (address->sa_family != AF_INET)
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
|
2019-05-03 22:51:40 +03:00
|
|
|
auto& ia = *(const sockaddr_in*)address;
|
2019-07-03 22:17:35 +03:00
|
|
|
m_local_address = IPv4Address((const u8*)&ia.sin_addr.s_addr);
|
2019-05-04 17:40:34 +03:00
|
|
|
m_local_port = ntohs(ia.sin_port);
|
2019-05-03 22:51:40 +03:00
|
|
|
|
2019-08-09 05:35:56 +03:00
|
|
|
dbgprintf("IPv4Socket::bind %s{%p} to %s:%u\n", class_name(), this, m_local_address.to_string().characters(), m_local_port);
|
2019-05-03 22:51:40 +03:00
|
|
|
|
|
|
|
return protocol_bind();
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
KResult IPv4Socket::listen(int backlog)
|
|
|
|
{
|
|
|
|
int rc = allocate_local_port_if_needed();
|
|
|
|
if (rc < 0)
|
|
|
|
return KResult(-EADDRINUSE);
|
|
|
|
|
|
|
|
set_backlog(backlog);
|
|
|
|
|
|
|
|
kprintf("IPv4Socket{%p} listening with backlog=%d\n", this, backlog);
|
|
|
|
|
|
|
|
return protocol_listen();
|
|
|
|
}
|
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
KResult IPv4Socket::connect(FileDescription& description, const sockaddr* address, socklen_t address_size, ShouldBlock should_block)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
|
|
|
if (address_size != sizeof(sockaddr_in))
|
|
|
|
return KResult(-EINVAL);
|
|
|
|
if (address->sa_family != AF_INET)
|
|
|
|
return KResult(-EINVAL);
|
2019-08-11 16:38:20 +03:00
|
|
|
if (m_role == Role::Connected)
|
|
|
|
return KResult(-EISCONN);
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-03-13 19:17:07 +03:00
|
|
|
auto& ia = *(const sockaddr_in*)address;
|
2019-07-03 22:17:35 +03:00
|
|
|
m_peer_address = IPv4Address((const u8*)&ia.sin_addr.s_addr);
|
2019-05-04 17:40:34 +03:00
|
|
|
m_peer_port = ntohs(ia.sin_port);
|
2019-03-13 19:17:07 +03:00
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
return protocol_connect(description, should_block);
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
void IPv4Socket::attach(FileDescription&)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
void IPv4Socket::detach(FileDescription&)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-08-11 16:38:20 +03:00
|
|
|
bool IPv4Socket::can_read(FileDescription&) const
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2019-08-11 16:38:20 +03:00
|
|
|
if (m_role == Role::Listener)
|
2019-05-03 22:51:40 +03:00
|
|
|
return can_accept();
|
2019-03-14 17:23:32 +03:00
|
|
|
if (protocol_is_disconnected())
|
|
|
|
return true;
|
2019-03-12 19:27:07 +03:00
|
|
|
return m_can_read;
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
bool IPv4Socket::can_write(FileDescription&) const
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
2019-04-08 05:52:21 +03:00
|
|
|
return is_connected();
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
|
|
|
|
2019-05-04 17:40:34 +03:00
|
|
|
int IPv4Socket::allocate_local_port_if_needed()
|
2019-03-13 18:05:56 +03:00
|
|
|
{
|
2019-05-04 17:40:34 +03:00
|
|
|
if (m_local_port)
|
|
|
|
return m_local_port;
|
|
|
|
int port = protocol_allocate_local_port();
|
2019-03-18 06:03:44 +03:00
|
|
|
if (port < 0)
|
|
|
|
return port;
|
2019-07-03 22:17:35 +03:00
|
|
|
m_local_port = (u16)port;
|
2019-03-18 06:03:44 +03:00
|
|
|
return port;
|
2019-03-13 18:05:56 +03:00
|
|
|
}
|
|
|
|
|
2019-06-07 10:36:51 +03:00
|
|
|
ssize_t IPv4Socket::sendto(FileDescription&, const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
|
2019-03-12 17:51:42 +03:00
|
|
|
{
|
|
|
|
(void)flags;
|
2019-03-14 01:14:30 +03:00
|
|
|
if (addr && addr_length != sizeof(sockaddr_in))
|
2019-03-12 17:51:42 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-03-13 19:17:07 +03:00
|
|
|
if (addr) {
|
2019-03-14 01:14:30 +03:00
|
|
|
if (addr->sa_family != AF_INET) {
|
|
|
|
kprintf("sendto: Bad address family: %u is not AF_INET!\n", addr->sa_family);
|
|
|
|
return -EAFNOSUPPORT;
|
|
|
|
}
|
|
|
|
|
2019-03-13 19:17:07 +03:00
|
|
|
auto& ia = *(const sockaddr_in*)addr;
|
2019-07-03 22:17:35 +03:00
|
|
|
m_peer_address = IPv4Address((const u8*)&ia.sin_addr.s_addr);
|
2019-05-04 17:40:34 +03:00
|
|
|
m_peer_port = ntohs(ia.sin_port);
|
2019-03-13 19:17:07 +03:00
|
|
|
}
|
2019-03-13 17:40:30 +03:00
|
|
|
|
2019-08-09 05:34:32 +03:00
|
|
|
auto adapter = adapter_for_route_to(m_peer_address);
|
2019-04-02 16:46:44 +03:00
|
|
|
if (!adapter)
|
|
|
|
return -EHOSTUNREACH;
|
|
|
|
|
2019-08-06 16:40:38 +03:00
|
|
|
if (m_local_address.to_u32() == 0)
|
|
|
|
m_local_address = adapter->ipv4_address();
|
|
|
|
|
2019-05-04 17:40:34 +03:00
|
|
|
int rc = allocate_local_port_if_needed();
|
2019-03-18 06:03:44 +03:00
|
|
|
if (rc < 0)
|
|
|
|
return rc;
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-05-04 17:40:34 +03:00
|
|
|
kprintf("sendto: destination=%s:%u\n", m_peer_address.to_string().characters(), m_peer_port);
|
2019-03-12 17:51:42 +03:00
|
|
|
|
2019-03-13 17:40:30 +03:00
|
|
|
if (type() == SOCK_RAW) {
|
2019-08-05 11:37:09 +03:00
|
|
|
adapter->send_ipv4(MACAddress(), m_peer_address, (IPv4Protocol)protocol(), (const u8*)data, data_length);
|
2019-03-13 17:40:30 +03:00
|
|
|
return data_length;
|
|
|
|
}
|
|
|
|
|
2019-03-14 14:43:18 +03:00
|
|
|
return protocol_send(data, data_length);
|
2019-03-12 17:51:42 +03:00
|
|
|
}
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2019-06-13 23:03:04 +03:00
|
|
|
ssize_t IPv4Socket::recvfrom(FileDescription& description, void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
|
2019-03-12 19:27:07 +03:00
|
|
|
{
|
|
|
|
(void)flags;
|
2019-03-14 01:14:30 +03:00
|
|
|
if (addr_length && *addr_length < sizeof(sockaddr_in))
|
2019-03-13 16:47:21 +03:00
|
|
|
return -EINVAL;
|
|
|
|
|
2019-03-13 05:26:01 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-05-04 17:40:34 +03:00
|
|
|
kprintf("recvfrom: type=%d, local_port=%u\n", type(), local_port());
|
2019-03-13 05:26:01 +03:00
|
|
|
#endif
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2019-05-04 17:40:34 +03:00
|
|
|
ReceivedPacket packet;
|
2019-03-12 19:27:07 +03:00
|
|
|
{
|
2019-03-14 02:20:44 +03:00
|
|
|
LOCKER(lock());
|
2019-03-12 19:27:07 +03:00
|
|
|
if (!m_receive_queue.is_empty()) {
|
2019-05-04 17:40:34 +03:00
|
|
|
packet = m_receive_queue.take_first();
|
2019-03-13 05:26:01 +03:00
|
|
|
m_can_read = !m_receive_queue.is_empty();
|
2019-03-14 17:23:32 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-05-04 17:40:34 +03:00
|
|
|
kprintf("IPv4Socket(%p): recvfrom without blocking %d bytes, packets in queue: %d\n", this, packet.data.size(), m_receive_queue.size_slow());
|
2019-03-14 17:23:32 +03:00
|
|
|
#endif
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|
|
|
|
}
|
2019-08-05 12:06:21 +03:00
|
|
|
if (!packet.data.has_value()) {
|
2019-03-14 17:23:32 +03:00
|
|
|
if (protocol_is_disconnected()) {
|
|
|
|
kprintf("IPv4Socket{%p} is protocol-disconnected, returning 0 in recvfrom!\n", this);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-13 15:13:23 +03:00
|
|
|
load_receive_deadline();
|
2019-07-20 12:05:52 +03:00
|
|
|
auto res = current->block<Thread::ReceiveBlocker>(description);
|
2019-03-12 19:27:07 +03:00
|
|
|
|
2019-03-14 02:20:44 +03:00
|
|
|
LOCKER(lock());
|
2019-03-13 15:13:23 +03:00
|
|
|
if (!m_can_read) {
|
2019-07-20 12:05:52 +03:00
|
|
|
if (res == Thread::BlockResult::InterruptedBySignal)
|
|
|
|
return -EINTR;
|
|
|
|
|
2019-03-13 15:13:23 +03:00
|
|
|
// Unblocked due to timeout.
|
|
|
|
return -EAGAIN;
|
|
|
|
}
|
2019-03-12 19:27:07 +03:00
|
|
|
ASSERT(m_can_read);
|
|
|
|
ASSERT(!m_receive_queue.is_empty());
|
2019-05-04 17:40:34 +03:00
|
|
|
packet = m_receive_queue.take_first();
|
2019-03-13 05:26:01 +03:00
|
|
|
m_can_read = !m_receive_queue.is_empty();
|
2019-03-14 17:23:32 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-05-04 17:40:34 +03:00
|
|
|
kprintf("IPv4Socket(%p): recvfrom with blocking %d bytes, packets in queue: %d\n", this, packet.data.size(), m_receive_queue.size_slow());
|
2019-03-14 17:23:32 +03:00
|
|
|
#endif
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|
2019-08-05 12:06:21 +03:00
|
|
|
ASSERT(packet.data.has_value());
|
|
|
|
auto& ipv4_packet = *(const IPv4Packet*)(packet.data.value().data());
|
2019-03-13 16:22:27 +03:00
|
|
|
|
2019-03-14 01:14:30 +03:00
|
|
|
if (addr) {
|
2019-05-04 17:40:34 +03:00
|
|
|
dbgprintf("Incoming packet is from: %s:%u\n", packet.peer_address.to_string().characters(), packet.peer_port);
|
2019-03-14 01:14:30 +03:00
|
|
|
auto& ia = *(sockaddr_in*)addr;
|
2019-05-04 17:40:34 +03:00
|
|
|
memcpy(&ia.sin_addr, &packet.peer_address, sizeof(IPv4Address));
|
|
|
|
ia.sin_port = htons(packet.peer_port);
|
2019-03-14 01:14:30 +03:00
|
|
|
ia.sin_family = AF_INET;
|
|
|
|
ASSERT(addr_length);
|
|
|
|
*addr_length = sizeof(sockaddr_in);
|
|
|
|
}
|
2019-03-13 16:47:21 +03:00
|
|
|
|
2019-03-13 16:22:27 +03:00
|
|
|
if (type() == SOCK_RAW) {
|
|
|
|
ASSERT(buffer_length >= ipv4_packet.payload_size());
|
|
|
|
memcpy(buffer, ipv4_packet.payload(), ipv4_packet.payload_size());
|
|
|
|
return ipv4_packet.payload_size();
|
|
|
|
}
|
|
|
|
|
2019-08-05 12:06:21 +03:00
|
|
|
return protocol_receive(packet.data.value(), buffer, buffer_length, flags);
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|
|
|
|
|
2019-08-05 12:06:21 +03:00
|
|
|
void IPv4Socket::did_receive(const IPv4Address& source_address, u16 source_port, KBuffer&& packet)
|
2019-03-12 19:27:07 +03:00
|
|
|
{
|
2019-03-14 02:20:44 +03:00
|
|
|
LOCKER(lock());
|
2019-08-05 12:06:21 +03:00
|
|
|
auto packet_size = packet.size();
|
2019-05-04 04:27:50 +03:00
|
|
|
m_receive_queue.append({ source_address, source_port, move(packet) });
|
2019-03-12 19:27:07 +03:00
|
|
|
m_can_read = true;
|
2019-03-14 17:28:23 +03:00
|
|
|
m_bytes_received += packet_size;
|
2019-03-13 15:13:23 +03:00
|
|
|
#ifdef IPV4_SOCKET_DEBUG
|
2019-03-14 17:28:23 +03:00
|
|
|
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());
|
2019-03-13 15:13:23 +03:00
|
|
|
#endif
|
2019-03-12 19:27:07 +03:00
|
|
|
}
|
2019-08-10 19:10:36 +03:00
|
|
|
|
|
|
|
String IPv4Socket::absolute_path(const FileDescription&) const
|
|
|
|
{
|
|
|
|
if (m_role == Role::None)
|
|
|
|
return "socket";
|
|
|
|
|
|
|
|
StringBuilder builder;
|
|
|
|
builder.append("socket:");
|
|
|
|
|
|
|
|
builder.appendf("%s:%d", m_local_address.to_string().characters(), m_local_port);
|
|
|
|
if (m_role == Role::Accepted || m_role == Role::Connected)
|
|
|
|
builder.appendf(" / %s:%d", m_peer_address.to_string().characters(), m_peer_port);
|
|
|
|
|
|
|
|
switch (m_role) {
|
|
|
|
case Role::Listener:
|
|
|
|
builder.append(" (listening)");
|
|
|
|
break;
|
|
|
|
case Role::Accepted:
|
|
|
|
builder.append(" (accepted)");
|
|
|
|
break;
|
|
|
|
case Role::Connected:
|
|
|
|
builder.append(" (connected)");
|
|
|
|
break;
|
|
|
|
case Role::Connecting:
|
|
|
|
builder.append(" (connecting)");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
ASSERT_NOT_REACHED();
|
|
|
|
}
|
|
|
|
|
|
|
|
return builder.to_string();
|
|
|
|
}
|