net/dhcp: print readable message types

When a DHCP packet is printed out, it first tries to determine the most
specific message type from the DHCP options and print its human-readable name
right after the protocol name. If finding the message type fails, the less
specific opcode is printed instead, but also in a human-readable way.

Fixes #4131
This commit is contained in:
Martin Stein 2021-04-30 17:03:00 +02:00 committed by Christian Helmuth
parent ad847d0543
commit 5fa2efa745

View File

@ -15,9 +15,59 @@
#include <net/dhcp.h> #include <net/dhcp.h>
#include <base/output.h> #include <base/output.h>
using namespace Genode;
using Message_type = Net::Dhcp_packet::Message_type;
static char const *msg_type_to_string(Message_type type)
{
switch (type) {
case Message_type::DISCOVER: return "DISCOVER";
case Message_type::OFFER : return "OFFER";
case Message_type::REQUEST : return "REQUEST";
case Message_type::DECLINE : return "DECLINE";
case Message_type::ACK : return "ACK";
case Message_type::NAK : return "NAK";
case Message_type::RELEASE : return "RELEASE";
case Message_type::INFORM : return "INFORM";
}
class Never_reached { };
throw Never_reached { };
}
static char const *opcode_to_string(uint8_t op)
{
switch (op) {
case 1: return "REPLY";
case 2: return "REQUEST";
default: return "?";
}
class Never_reached { };
throw Never_reached { };
}
void Net::Dhcp_packet::print(Genode::Output &output) const void Net::Dhcp_packet::print(Genode::Output &output) const
{ {
Genode::print(output, "\033[32mDHCP\033[0m ", client_mac(), bool msg_type_found { false };
" > ", siaddr(), " cmd ", op()); for_each_option([&] (Option const &opt) {
if (opt.code() == Option::Code::MSG_TYPE) {
msg_type_found = true;
Message_type_option const &msg_type {
*reinterpret_cast<Message_type_option const *>(&opt) };
Genode::print(output, "\033[32mDHCP ",
msg_type_to_string(msg_type.value()), "\033[0m ",
client_mac(), " > ", siaddr());
}
});
if (!msg_type_found) {
Genode::print(output, "\033[32mDHCP ", opcode_to_string(op()),
"\033[0m ", client_mac(), " > ", siaddr());
}
} }