IPv4: Dynamically allocate the UDP source port if needed.

This commit is contained in:
Andreas Kling 2019-03-13 16:05:56 +01:00
parent 4dddf949c8
commit 209a16bb7f
Notes: sideshowbarker 2024-07-19 15:04:10 +09:00
3 changed files with 47 additions and 1 deletions

View file

@ -22,6 +22,12 @@ struct Traits<unsigned> {
static void dump(unsigned u) { kprintf("%u", u); }
};
template<>
struct Traits<word> {
static unsigned hash(unsigned u) { return int_hash(u); }
static void dump(unsigned u) { kprintf("%u", u); }
};
template<typename T>
struct Traits<T*> {
static unsigned hash(const T* p)

View file

@ -10,6 +10,22 @@
#define IPV4_SOCKET_DEBUG
Lockable<HashMap<word, IPv4Socket*>>& IPv4Socket::sockets_by_udp_port()
{
static Lockable<HashMap<word, IPv4Socket*>>* s_map;
if (!s_map)
s_map = new Lockable<HashMap<word, IPv4Socket*>>;
return *s_map;
}
Lockable<HashMap<word, IPv4Socket*>>& IPv4Socket::sockets_by_tcp_port()
{
static Lockable<HashMap<word, IPv4Socket*>>* s_map;
if (!s_map)
s_map = new Lockable<HashMap<word, IPv4Socket*>>;
return *s_map;
}
Lockable<HashTable<IPv4Socket*>>& IPv4Socket::all_sockets()
{
static Lockable<HashTable<IPv4Socket*>>* s_table;
@ -100,6 +116,25 @@ bool IPv4Socket::can_write(SocketRole role) const
ASSERT_NOT_REACHED();
}
void IPv4Socket::allocate_source_port_if_needed()
{
if (m_source_port)
return;
if (type() == SOCK_DGRAM) {
// This is not a very efficient allocation algorithm.
// FIXME: Replace it with a bitmap or some other fast-paced looker-upper.
LOCKER(sockets_by_udp_port().lock());
for (word port = 2000; port < 60000; ++port) {
auto it = sockets_by_udp_port().resource().find(port);
if (it == sockets_by_udp_port().resource().end()) {
m_source_port = port;
return;
}
}
ASSERT_NOT_REACHED();
}
}
ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, const sockaddr* addr, socklen_t addr_length)
{
(void)flags;
@ -121,7 +156,7 @@ ssize_t IPv4Socket::sendto(const void* data, size_t data_length, int flags, cons
m_destination_address = IPv4Address((const byte*)&ia.sin_addr.s_addr);
m_destination_port = ntohs(ia.sin_port);
m_source_port = 2413;
allocate_source_port_if_needed();
kprintf("sendto: destination=%s:%u\n", m_destination_address.to_string().characters(), m_destination_port);

View file

@ -3,6 +3,7 @@
#include <Kernel/Socket.h>
#include <Kernel/DoubleBuffer.h>
#include <Kernel/IPv4.h>
#include <AK/HashMap.h>
#include <AK/Lock.h>
#include <AK/SinglyLinkedList.h>
@ -12,6 +13,8 @@ public:
virtual ~IPv4Socket() override;
static Lockable<HashTable<IPv4Socket*>>& all_sockets();
static Lockable<HashMap<word, IPv4Socket*>>& sockets_by_udp_port();
static Lockable<HashMap<word, IPv4Socket*>>& sockets_by_tcp_port();
virtual KResult bind(const sockaddr*, socklen_t) override;
virtual KResult connect(const sockaddr*, socklen_t) override;
@ -36,6 +39,8 @@ private:
IPv4Socket(int type, int protocol);
virtual bool is_ipv4() const override { return true; }
void allocate_source_port_if_needed();
bool m_bound { false };
int m_attached_fds { 0 };
IPv4Address m_destination_address;