mirror of
https://github.com/RGBCube/serenity
synced 2025-05-22 14:35:07 +00:00
IPv4: Implement bind() for TCP and UDP sockets.
We can't accept connections just yet, but this patch makes it possible to bind() to a given source address/port.
This commit is contained in:
parent
6a5d92f0ad
commit
abb5c890e0
6 changed files with 34 additions and 2 deletions
|
@ -11,6 +11,7 @@
|
|||
#include <Kernel/Net/ARP.h>
|
||||
#include <Kernel/Net/Routing.h>
|
||||
#include <LibC/errno_numbers.h>
|
||||
#include <Kernel/FileSystem/FileDescriptor.h>
|
||||
|
||||
#define IPV4_SOCKET_DEBUG
|
||||
|
||||
|
@ -63,7 +64,13 @@ KResult IPv4Socket::bind(const sockaddr* address, socklen_t address_size)
|
|||
if (address->sa_family != AF_INET)
|
||||
return KResult(-EINVAL);
|
||||
|
||||
ASSERT_NOT_REACHED();
|
||||
auto& ia = *(const sockaddr_in*)address;
|
||||
m_source_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
|
||||
m_source_port = ntohs(ia.sin_port);
|
||||
|
||||
dbgprintf("IPv4Socket::bind %s{%p} to port %u\n", class_name(), this, m_source_port);
|
||||
|
||||
return protocol_bind();
|
||||
}
|
||||
|
||||
KResult IPv4Socket::connect(FileDescriptor& descriptor, const sockaddr* address, socklen_t address_size, ShouldBlock should_block)
|
||||
|
@ -91,8 +98,10 @@ void IPv4Socket::detach(FileDescriptor&)
|
|||
--m_attached_fds;
|
||||
}
|
||||
|
||||
bool IPv4Socket::can_read(FileDescriptor&) const
|
||||
bool IPv4Socket::can_read(FileDescriptor& descriptor) const
|
||||
{
|
||||
if (descriptor.socket_role() == SocketRole::Listener)
|
||||
return can_accept();
|
||||
if (protocol_is_disconnected())
|
||||
return true;
|
||||
return m_can_read;
|
||||
|
|
|
@ -48,6 +48,7 @@ protected:
|
|||
|
||||
int allocate_source_port_if_needed();
|
||||
|
||||
virtual KResult protocol_bind() { return KSuccess; }
|
||||
virtual int protocol_receive(const ByteBuffer&, void*, size_t, int, sockaddr*, socklen_t*) { return -ENOTIMPL; }
|
||||
virtual int protocol_send(const void*, int) { return -ENOTIMPL; }
|
||||
virtual KResult protocol_connect(FileDescriptor&, ShouldBlock) { return KSuccess; }
|
||||
|
@ -59,6 +60,8 @@ private:
|
|||
|
||||
bool m_bound { false };
|
||||
int m_attached_fds { 0 };
|
||||
|
||||
IPv4Address m_source_address;
|
||||
IPv4Address m_destination_address;
|
||||
|
||||
DoubleBuffer m_for_client;
|
||||
|
|
|
@ -203,3 +203,12 @@ bool TCPSocket::protocol_is_disconnected() const
|
|||
{
|
||||
return m_state == State::Disconnecting || m_state == State::Disconnected;
|
||||
}
|
||||
|
||||
KResult TCPSocket::protocol_bind()
|
||||
{
|
||||
LOCKER(sockets_by_port().lock());
|
||||
if (sockets_by_port().resource().contains(source_port()))
|
||||
return KResult(-EADDRINUSE);
|
||||
sockets_by_port().resource().set(source_port(), this);
|
||||
return KSuccess;
|
||||
}
|
||||
|
|
|
@ -38,6 +38,7 @@ private:
|
|||
virtual KResult protocol_connect(FileDescriptor&, ShouldBlock) override;
|
||||
virtual int protocol_allocate_source_port() override;
|
||||
virtual bool protocol_is_disconnected() const override;
|
||||
virtual KResult protocol_bind() override;
|
||||
|
||||
dword m_sequence_number { 0 };
|
||||
dword m_ack_number { 0 };
|
||||
|
|
|
@ -104,3 +104,12 @@ int UDPSocket::protocol_allocate_source_port()
|
|||
}
|
||||
return -EADDRINUSE;
|
||||
}
|
||||
|
||||
KResult UDPSocket::protocol_bind()
|
||||
{
|
||||
LOCKER(sockets_by_port().lock());
|
||||
if (sockets_by_port().resource().contains(source_port()))
|
||||
return KResult(-EADDRINUSE);
|
||||
sockets_by_port().resource().set(source_port(), this);
|
||||
return KSuccess;
|
||||
}
|
||||
|
|
|
@ -20,6 +20,7 @@ private:
|
|||
virtual int protocol_send(const void*, int) override;
|
||||
virtual KResult protocol_connect(FileDescriptor&, ShouldBlock) override { return KSuccess; }
|
||||
virtual int protocol_allocate_source_port() override;
|
||||
virtual KResult protocol_bind() override;
|
||||
};
|
||||
|
||||
class UDPSocketHandle : public SocketHandle {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue