net-tools/src/ping/output/error.c
2026-03-18 16:57:54 +01:00

75 lines
2 KiB
C

#include <stdio.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "compiler.h"
#include "icmp.h"
#include "icmp_types.h"
#include "internal/ping/output.h"
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 */
static const char *error_msg_for(const icmp_reply_t *reply);
/* -------------------- */
void
ping_output_error(const icmp_reply_t *reply,
const icmp_offending_packet_t *offending,
uint16_t seq, __unused const struct ping_config *config)
{
char from_str[INET_ADDRSTRLEN];
inet_ntop(AF_INET, &reply->from, from_str, sizeof(from_str));
if (reply->type == ICMP_TYPE_DEST_UNREACHABLE
&& reply->code == ICMP_CODE_FRAG_NEEDED)
{
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
dprintf(STDERR_FILENO, "From %s: icmp_seq=%u %s\n",
from_str, (unsigned int)seq,
error_msg_for(reply));
}
static const char *
error_msg_for(const icmp_reply_t *reply)
{
const struct error_entry *entry;
STATIC_ARRAY_FOREACH(g_error_table, entry)
{
if (entry->type == reply->type && entry->code == reply->code)
return (entry->msg);
}
return ("Unknown ICMP error");
}