refactor(error): improve output error code

This commit is contained in:
lohhiiccc 2026-03-18 16:57:54 +01:00
parent dd6bafe132
commit 07588298cd

View file

@ -8,11 +8,28 @@
#include "icmp_types.h" #include "icmp_types.h"
#include "internal/ping/output.h" #include "internal/ping/output.h"
// TODO: CLEAN THIS FILE ! static const struct error_entry
{
uint8_t type;
uint8_t code;
const char *msg;
} g_error_table[] = {
{ ICMP_TYPE_TIME_EXCEEDED, ICMP_CODE_TTL_EXCEEDED,
"Time to live exceeded" },
{ ICMP_TYPE_TIME_EXCEEDED, ICMP_CODE_FRAG_REASM_EXCEEDED,
"Frag reassembly time exceeded" },
{ ICMP_TYPE_DEST_UNREACHABLE, ICMP_CODE_NET_UNREACHABLE,
"Destination Net Unreachable" },
{ ICMP_TYPE_DEST_UNREACHABLE, ICMP_CODE_HOST_UNREACHABLE,
"Destination Host Unreachable" },
{ ICMP_TYPE_DEST_UNREACHABLE, ICMP_CODE_PROTOCOL_UNREACHABLE,
"Destination Protocol Unreachable" },
{ ICMP_TYPE_DEST_UNREACHABLE, ICMP_CODE_PORT_UNREACHABLE,
"Destination Port Unreachable" },
};
/* Forward declarations */ /* Forward declarations */
static void output_frag_needed(const char *from, uint16_t seq, static const char *error_msg_for(const icmp_reply_t *reply);
uint16_t next_mtu);
static const char *error_msg_for(const icmp_reply_t *reply);
/* -------------------- */ /* -------------------- */
void void
@ -23,64 +40,33 @@ ping_output_error(const icmp_reply_t *reply,
char from_str[INET_ADDRSTRLEN]; char from_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &reply->from, from_str, sizeof(from_str)); inet_ntop(AF_INET, &reply->from, from_str, sizeof(from_str));
if (ICMP_TYPE_DEST_UNREACHABLE == reply->type if (reply->type == ICMP_TYPE_DEST_UNREACHABLE
&& ICMP_CODE_FRAG_NEEDED == reply->code) && reply->code == ICMP_CODE_FRAG_NEEDED)
output_frag_needed(from_str, seq, offending->next_mtu); {
if (offending->next_mtu > 0)
dprintf(STDERR_FILENO,
"From %s: icmp_seq=%u "
"Frag needed and DF set (mtu = %u)\n",
from_str, (unsigned int)seq,
(unsigned int)offending->next_mtu);
else
dprintf(STDERR_FILENO,
"From %s: icmp_seq=%u "
"Frag needed and DF set (mtu unknown)\n",
from_str, (unsigned int)seq);
}
else else
dprintf(STDERR_FILENO, "From %s: icmp_seq=%u %s\n", dprintf(STDERR_FILENO, "From %s: icmp_seq=%u %s\n",
from_str, (unsigned int)seq, error_msg_for(reply)); from_str, (unsigned int)seq,
} error_msg_for(reply));
static void
output_frag_needed(const char *from, uint16_t seq, uint16_t next_mtu)
{
if (next_mtu > 0)
dprintf(STDERR_FILENO,
"From %s: icmp_seq=%u Frag needed and DF set (mtu = %u)\n",
from, (unsigned int)seq, (unsigned int)next_mtu);
else
dprintf(STDERR_FILENO,
"From %s: icmp_seq=%u Frag needed and DF set (mtu unknown)\n",
from, (unsigned int)seq);
} }
static const char * static const char *
error_msg_for(const icmp_reply_t *reply) error_msg_for(const icmp_reply_t *reply)
{ {
static const struct s_error_entry const struct error_entry *entry;
{
uint8_t type;
uint8_t code;
const char *msg;
} error_table[] = {
{
ICMP_TYPE_TIME_EXCEEDED, ICMP_CODE_TTL_EXCEEDED,
"Time to live exceeded"
},
{
ICMP_TYPE_TIME_EXCEEDED, ICMP_CODE_FRAG_REASM_EXCEEDED,
"Frag reassembly time exceeded"
},
{
ICMP_TYPE_DEST_UNREACHABLE, ICMP_CODE_NET_UNREACHABLE,
"Destination Net Unreachable"
},
{
ICMP_TYPE_DEST_UNREACHABLE, ICMP_CODE_HOST_UNREACHABLE,
"Destination Host Unreachable"
},
{
ICMP_TYPE_DEST_UNREACHABLE, ICMP_CODE_PROTOCOL_UNREACHABLE,
"Destination Protocol Unreachable"
},
{
ICMP_TYPE_DEST_UNREACHABLE, ICMP_CODE_PORT_UNREACHABLE,
"Destination Port Unreachable"
},
};
const struct s_error_entry *entry;
STATIC_ARRAY_FOREACH(error_table, entry) STATIC_ARRAY_FOREACH(g_error_table, entry)
{ {
if (entry->type == reply->type && entry->code == reply->code) if (entry->type == reply->type && entry->code == reply->code)
return (entry->msg); return (entry->msg);