From 5fa2efa74543436e7a23d1154bdea41e58f8d242 Mon Sep 17 00:00:00 2001 From: Martin Stein Date: Fri, 30 Apr 2021 17:03:00 +0200 Subject: [PATCH] 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 --- repos/os/src/lib/net/dhcp.cc | 54 ++++++++++++++++++++++++++++++++++-- 1 file changed, 52 insertions(+), 2 deletions(-) diff --git a/repos/os/src/lib/net/dhcp.cc b/repos/os/src/lib/net/dhcp.cc index 2f9567617e..afb1060223 100644 --- a/repos/os/src/lib/net/dhcp.cc +++ b/repos/os/src/lib/net/dhcp.cc @@ -15,9 +15,59 @@ #include #include +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 { - Genode::print(output, "\033[32mDHCP\033[0m ", client_mac(), - " > ", siaddr(), " cmd ", op()); + bool msg_type_found { false }; + 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(&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()); + } }