AK+Kernel: Return KString from IPv4Address::to_string() in the Kernel

This lets us safely handle allocation failure.
This commit is contained in:
Idan Horowitz 2022-02-15 19:35:32 +02:00 committed by Andreas Kling
parent bc98ad9cc1
commit 6098ffa120
2 changed files with 47 additions and 10 deletions

View file

@ -7,11 +7,18 @@
#pragma once
#include <AK/Endian.h>
#include <AK/Format.h>
#include <AK/Optional.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/Vector.h>
#ifdef KERNEL
# include <AK/Error.h>
# include <Kernel/KString.h>
#else
# include <AK/String.h>
#endif
namespace AK {
class [[gnu::packed]] IPv4Address {
@ -48,6 +55,16 @@ public:
return octet(SubnetClass(i));
}
#ifdef KERNEL
ErrorOr<NonnullOwnPtr<Kernel::KString>> to_string() const
{
return Kernel::KString::formatted("{}.{}.{}.{}",
octet(SubnetClass::A),
octet(SubnetClass::B),
octet(SubnetClass::C),
octet(SubnetClass::D));
}
#else
String to_string() const
{
return String::formatted("{}.{}.{}.{}",
@ -65,6 +82,7 @@ public:
octet(SubnetClass::B),
octet(SubnetClass::A));
}
#endif
static Optional<IPv4Address> from_string(StringView string)
{
@ -131,6 +149,15 @@ struct Traits<IPv4Address> : public GenericTraits<IPv4Address> {
static constexpr unsigned hash(const IPv4Address& address) { return int_hash(address.to_u32()); }
};
#ifdef KERNEL
template<>
struct Formatter<IPv4Address> : Formatter<ErrorOr<NonnullOwnPtr<Kernel::KString>>> {
ErrorOr<void> format(FormatBuilder& builder, IPv4Address value)
{
return Formatter<ErrorOr<NonnullOwnPtr<Kernel::KString>>>::format(builder, value.to_string());
}
};
#else
template<>
struct Formatter<IPv4Address> : Formatter<String> {
ErrorOr<void> format(FormatBuilder& builder, IPv4Address value)
@ -138,6 +165,7 @@ struct Formatter<IPv4Address> : Formatter<String> {
return Formatter<String>::format(builder, value.to_string());
}
};
#endif
}

View file

@ -47,11 +47,15 @@ private:
obj.add("class_name", adapter.class_name());
obj.add("mac_address", adapter.mac_address().to_string());
if (!adapter.ipv4_address().is_zero()) {
obj.add("ipv4_address", adapter.ipv4_address().to_string());
obj.add("ipv4_netmask", adapter.ipv4_netmask().to_string());
auto ipv4_address = adapter.ipv4_address().to_string().release_value_but_fixme_should_propagate_errors();
obj.add("ipv4_address", ipv4_address->view());
auto ipv4_netmask = adapter.ipv4_netmask().to_string().release_value_but_fixme_should_propagate_errors();
obj.add("ipv4_netmask", ipv4_netmask->view());
}
if (!adapter.ipv4_gateway().is_zero()) {
auto ipv4_gateway = adapter.ipv4_gateway().to_string().release_value_but_fixme_should_propagate_errors();
obj.add("ipv4_gateway", ipv4_gateway->view());
}
if (!adapter.ipv4_gateway().is_zero())
obj.add("ipv4_gateway", adapter.ipv4_gateway().to_string());
obj.add("packets_in", adapter.packets_in());
obj.add("bytes_in", adapter.bytes_in());
obj.add("packets_out", adapter.packets_out());
@ -78,7 +82,8 @@ private:
arp_table().for_each([&](const auto& it) {
auto obj = array.add_object();
obj.add("mac_address", it.value.to_string());
obj.add("ip_address", it.key.to_string());
auto ip_address = it.key.to_string().release_value_but_fixme_should_propagate_errors();
obj.add("ip_address", ip_address->view());
});
array.finish();
return {};
@ -96,9 +101,11 @@ private:
JsonArraySerializer array { builder };
TCPSocket::for_each([&array](auto& socket) {
auto obj = array.add_object();
obj.add("local_address", socket.local_address().to_string());
auto local_address = socket.local_address().to_string().release_value_but_fixme_should_propagate_errors();
obj.add("local_address", local_address->view());
obj.add("local_port", socket.local_port());
obj.add("peer_address", socket.peer_address().to_string());
auto peer_address = socket.peer_address().to_string().release_value_but_fixme_should_propagate_errors();
obj.add("peer_address", peer_address->view());
obj.add("peer_port", socket.peer_port());
obj.add("state", TCPSocket::to_string(socket.state()));
obj.add("ack_number", socket.ack_number());
@ -153,9 +160,11 @@ private:
JsonArraySerializer array { builder };
UDPSocket::for_each([&array](auto& socket) {
auto obj = array.add_object();
obj.add("local_address", socket.local_address().to_string());
auto local_address = socket.local_address().to_string().release_value_but_fixme_should_propagate_errors();
obj.add("local_address", local_address->view());
obj.add("local_port", socket.local_port());
obj.add("peer_address", socket.peer_address().to_string());
auto peer_address = socket.peer_address().to_string().release_value_but_fixme_should_propagate_errors();
obj.add("peer_address", peer_address->view());
obj.add("peer_port", socket.peer_port());
if (Process::current().is_superuser() || Process::current().uid() == socket.origin_uid()) {
obj.add("origin_pid", socket.origin_pid().value());