summaryrefslogtreecommitdiff
path: root/Kernel/Net/IPv4Socket.cpp
blob: 916abc74d877aa6295cf641539ba35dc607b50db (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#include <Kernel/FileSystem/FileDescription.h>
#include <Kernel/Net/ARP.h>
#include <Kernel/Net/ICMP.h>
#include <Kernel/Net/IPv4.h>
#include <Kernel/Net/IPv4Socket.h>
#include <Kernel/Net/NetworkAdapter.h>
#include <Kernel/Net/Routing.h>
#include <Kernel/Net/TCP.h>
#include <Kernel/Net/TCPSocket.h>
#include <Kernel/Net/UDP.h>
#include <Kernel/Net/UDPSocket.h>
#include <Kernel/Process.h>
#include <Kernel/UnixTypes.h>
#include <LibC/errno_numbers.h>

#define IPV4_SOCKET_DEBUG

Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
{
    static Lockable<HashTable<IPv4Socket*>>* s_table;
    if (!s_table)
        s_table = new Lockable<HashTable<IPv4Socket*>>;
    return *s_table;
}

Retained<IPv4Socket> IPv4Socket::create(int type, int protocol)
{
    if (type == SOCK_STREAM)
        return TCPSocket::create(protocol);
    if (type == SOCK_DGRAM)
        return UDPSocket::create(protocol);
    return adopt(*new IPv4Socket(type, protocol));
}

IPv4Socket::IPv4Socket(int type, int protocol)
    : Socket(AF_INET, type, protocol)
{
    kprintf("%s(%u) IPv4Socket{%p} created with type=%u, protocol=%d\n", current->process().name().characters(), current->pid(), this, type, protocol);
    LOCKER(all_sockets().lock());
    all_sockets().resource().set(this);
}

IPv4Socket::~IPv4Socket()
{
    LOCKER(all_sockets().lock());
    all_sockets().resource().remove(this);
}

bool IPv4Socket::get_local_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;
    ia.sin_port = m_local_port;
    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;
    ia.sin_port = m_peer_port;
    memcpy(&ia.sin_addr, &m_peer_address, sizeof(IPv4Address));
    *address_size = sizeof(sockaddr_in);
    return true;
}

KResult IPv4Socket::bind(const sockaddr* address, socklen_t address_size)
{
    ASSERT(!is_connected());
    if (address_size != sizeof(sockaddr_in))
        return KResult(-EINVAL);
    if (address->sa_family != AF_INET)
        return KResult(-EINVAL);

    auto& ia = *(const sockaddr_in*)address;
    m_local_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
    m_local_port = ntohs(ia.sin_port);

    dbgprintf("IPv4Socket::bind %s{%p} to port %u\n", class_name(), this, m_local_port);

    return protocol_bind();
}

KResult IPv4Socket::connect(FileDescription& descriptor, const sockaddr* address, socklen_t address_size, ShouldBlock should_block)
{
    ASSERT(!m_bound);
    if (address_size != sizeof(sockaddr_in))
        return KResult(-EINVAL);
    if (address->sa_family != AF_INET)
        return KResult(-EINVAL);

    auto& ia = *(const sockaddr_in*)address;
    m_peer_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
    m_peer_port = ntohs(ia.sin_port);

    return protocol_connect(descriptor, should_block);
}

void IPv4Socket::attach(FileDescription&)
{
    ++m_attached_fds;
}

void IPv4Socket::detach(FileDescription&)
{
    --m_attached_fds;
}

bool IPv4Socket::can_read(FileDescription& descriptor) const
{
    if (descriptor.socket_role() == SocketRole::Listener)
        return can_accept();
    if (protocol_is_disconnected())
        return true;
    return m_can_read;
}

ssize_t IPv4Socket::read(FileDescription& descriptor, byte* buffer, ssize_t size)
{
    return recvfrom(descriptor, buffer, size, 0, nullptr, 0);
}

ssize_t IPv4Socket::write(FileDescription& descriptor, const byte* data, ssize_t size)
{
    return sendto(descriptor, data, size, 0, nullptr, 0);
}

bool IPv4Socket::can_write(FileDescription&) const
{
    return is_connected();
}

int IPv4Socket::allocate_local_port_if_needed()
{
    if (m_local_port)
        return m_local_port;
    int port = protocol_allocate_local_port();
    if (port < 0)
        return port;
    m_local_port = (word)port;
    return port;
}

ssize_t IPv4Socket::sendto(FileDescription&, const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
{
    (void)flags;
    if (addr && addr_length != sizeof(sockaddr_in))
        return -EINVAL;

    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;
        m_peer_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
        m_peer_port = ntohs(ia.sin_port);
    }

    auto* adapter = adapter_for_route_to(m_peer_address);
    if (!adapter)
        return -EHOSTUNREACH;

    int rc = allocate_local_port_if_needed();
    if (rc < 0)
        return rc;

    kprintf("sendto: destination=%s:%u\n", m_peer_address.to_string().characters(), m_peer_port);

    if (type() == SOCK_RAW) {
        adapter->send_ipv4(MACAddress(), m_peer_address, (IPv4Protocol)protocol(), ByteBuffer::copy(data, data_length));
        return data_length;
    }

    return protocol_send(data, data_length);
}

ssize_t IPv4Socket::recvfrom(FileDescription& descriptor, void* buffer, size_t buffer_length, int flags, sockaddr* addr, socklen_t* addr_length)
{
    (void)flags;
    if (addr_length && *addr_length < sizeof(sockaddr_in))
        return -EINVAL;

#ifdef IPV4_SOCKET_DEBUG
    kprintf("recvfrom: type=%d, local_port=%u\n", type(), local_port());
#endif

    ReceivedPacket packet;
    {
        LOCKER(lock());
        if (!m_receive_queue.is_empty()) {
            packet = m_receive_queue.take_first();
            m_can_read = !m_receive_queue.is_empty();
#ifdef IPV4_SOCKET_DEBUG
            kprintf("IPv4Socket(%p): recvfrom without blocking %d bytes, packets in queue: %d\n", this, packet.data.size(), m_receive_queue.size_slow());
#endif
        }
    }
    if (packet.data.is_null()) {
        if (protocol_is_disconnected()) {
            kprintf("IPv4Socket{%p} is protocol-disconnected, returning 0 in recvfrom!\n", this);
            return 0;
        }

        load_receive_deadline();
        current->block(Thread::BlockedReceive, descriptor);

        LOCKER(lock());
        if (!m_can_read) {
            // Unblocked due to timeout.
            return -EAGAIN;
        }
        ASSERT(m_can_read);
        ASSERT(!m_receive_queue.is_empty());
        packet = m_receive_queue.take_first();
        m_can_read = !m_receive_queue.is_empty();
#ifdef IPV4_SOCKET_DEBUG
        kprintf("IPv4Socket(%p): recvfrom with blocking %d bytes, packets in queue: %d\n", this, packet.data.size(), m_receive_queue.size_slow());
#endif
    }
    ASSERT(!packet.data.is_null());
    auto& ipv4_packet = *(const IPv4Packet*)(packet.data.pointer());

    if (addr) {
        dbgprintf("Incoming packet is from: %s:%u\n", packet.peer_address.to_string().characters(), packet.peer_port);
        auto& ia = *(sockaddr_in*)addr;
        memcpy(&ia.sin_addr, &packet.peer_address, sizeof(IPv4Address));
        ia.sin_port = htons(packet.peer_port);
        ia.sin_family = AF_INET;
        ASSERT(addr_length);
        *addr_length = sizeof(sockaddr_in);
    }

    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();
    }

    return protocol_receive(packet.data, buffer, buffer_length, flags, addr, addr_length);
}

void IPv4Socket::did_receive(const IPv4Address& source_address, word source_port, ByteBuffer&& packet)
{
    LOCKER(lock());
    auto packet_size = packet.size();
    m_receive_queue.append({ source_address, source_port, move(packet) });
    m_can_read = true;
    m_bytes_received += packet_size;
#ifdef IPV4_SOCKET_DEBUG
    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());
#endif
}