net-tools/src/ping/output/error.c
lohhiiccc 770265ee80 feat: add -M (dont-fragment) option
- Add FLAG_DONT_FRAGMENT flag and cli_handle_dont_fragment handler
- Register -M/--dont-fragment option in the X-macro option table
- Apply DF bit via icmp_set_dont_fragment() before ping loop
- Expand error output: table-driven ICMP error messages and dedicated
  frag-needed output with next-hop MTU
- Add STATIC_ARRAY_FOREACH / COUNT_OF macros to compiler.h
2026-03-13 03:22:14 +01:00

89 lines
2.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"
// TODO: CLEAN THIS FILE !
/* Forward declarations */
static void output_frag_needed(const char *from, uint16_t seq,
uint16_t next_mtu);
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 (ICMP_TYPE_DEST_UNREACHABLE == reply->type
&& ICMP_CODE_FRAG_NEEDED == reply->code)
output_frag_needed(from_str, seq, offending->next_mtu);
else
dprintf(STDERR_FILENO, "From %s: icmp_seq=%u %s\n",
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 *
error_msg_for(const icmp_reply_t *reply)
{
static const struct s_error_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)
{
if (entry->type == reply->type && entry->code == reply->code)
return (entry->msg);
}
return ("Unknown ICMP error");
}