Skip to content

Commit

Permalink
ProcFS: Expose UDP sockets in /proc/net/udp
Browse files Browse the repository at this point in the history
  • Loading branch information
bugaevc authored and awesomekling committed Aug 9, 2019
1 parent d06f429 commit be48594
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
21 changes: 21 additions & 0 deletions Kernel/FileSystem/ProcFS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#include <Kernel/KParams.h>
#include <Kernel/Net/NetworkAdapter.h>
#include <Kernel/Net/TCPSocket.h>
#include <Kernel/Net/UDPSocket.h>
#include <Kernel/Net/LocalSocket.h>
#include <Kernel/PCI.h>
#include <Kernel/VM/MemoryManager.h>
#include <Kernel/kmalloc.h>
Expand Down Expand Up @@ -53,6 +55,7 @@ enum ProcFileType {

FI_Root_net_adapters,
FI_Root_net_tcp,
FI_Root_net_udp,

FI_PID,

Expand Down Expand Up @@ -311,6 +314,20 @@ Optional<KBuffer> procfs$net_tcp(InodeIdentifier)
return json.serialized<KBufferBuilder>();
}

Optional<KBuffer> procfs$net_udp(InodeIdentifier)
{
JsonArray json;
UDPSocket::for_each([&json](auto& socket) {
JsonObject obj;
obj.set("local_address", socket.local_address().to_string());
obj.set("local_port", socket.local_port());
obj.set("peer_address", socket.peer_address().to_string());
obj.set("peer_port", socket.peer_port());
json.append(obj);
});
return json.serialized<KBufferBuilder>();
}

Optional<KBuffer> procfs$pid_vmo(InodeIdentifier identifier)
{
auto handle = ProcessInspectionHandle::from_pid(to_pid(identifier));
Expand Down Expand Up @@ -917,6 +934,7 @@ bool ProcFSInode::traverse_as_directory(Function<bool(const FS::DirectoryEntry&)
case FI_Root_net:
callback({ "adapters", 8, to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_adapters), 0 });
callback({ "tcp", 3, to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_tcp), 0 });
callback({ "udp", 3, to_identifier(fsid(), PDI_Root_net, 0, FI_Root_net_udp), 0 });
break;

case FI_PID: {
Expand Down Expand Up @@ -1003,6 +1021,8 @@ InodeIdentifier ProcFSInode::lookup(StringView name)
return to_identifier(fsid(), PDI_Root, 0, FI_Root_net_adapters);
if (name == "tcp")
return to_identifier(fsid(), PDI_Root, 0, FI_Root_net_tcp);
if (name == "udp")
return to_identifier(fsid(), PDI_Root, 0, FI_Root_net_udp);
return {};
}

Expand Down Expand Up @@ -1114,6 +1134,7 @@ ProcFS::ProcFS()

m_entries[FI_Root_net_adapters] = { "adapters", FI_Root_net_adapters, procfs$net_adapters };
m_entries[FI_Root_net_tcp] = { "tcp", FI_Root_net_tcp, procfs$net_tcp };
m_entries[FI_Root_net_udp] = { "udp", FI_Root_net_udp, procfs$net_udp };

m_entries[FI_PID_vm] = { "vm", FI_PID_vm, procfs$pid_vm };
m_entries[FI_PID_vmo] = { "vmo", FI_PID_vmo, procfs$pid_vmo };
Expand Down
7 changes: 7 additions & 0 deletions Kernel/Net/UDPSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@
#include <Kernel/Net/UDPSocket.h>
#include <Kernel/Process.h>

void UDPSocket::for_each(Function<void(UDPSocket&)> callback)
{
LOCKER(sockets_by_port().lock());
for (auto it : sockets_by_port().resource())
callback(*it.value);
}

Lockable<HashMap<u16, UDPSocket*>>& UDPSocket::sockets_by_port()
{
static Lockable<HashMap<u16, UDPSocket*>>* s_map;
Expand Down
1 change: 1 addition & 0 deletions Kernel/Net/UDPSocket.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ class UDPSocket final : public IPv4Socket {
virtual ~UDPSocket() override;

static SocketHandle<UDPSocket> from_port(u16);
static void for_each(Function<void(UDPSocket&)>);

private:
explicit UDPSocket(int protocol);
Expand Down

0 comments on commit be48594

Please sign in to comment.