diff --git a/src/ping/output/error.c b/src/ping/output/error.c index 1a0b009..3b4e07e 100644 --- a/src/ping/output/error.c +++ b/src/ping/output/error.c @@ -8,11 +8,28 @@ #include "icmp_types.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 */ -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); +static const char *error_msg_for(const icmp_reply_t *reply); /* -------------------- */ void @@ -23,64 +40,33 @@ ping_output_error(const icmp_reply_t *reply, 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); + 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 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); + from_str, (unsigned int)seq, + error_msg_for(reply)); } 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; + const struct 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) return (entry->msg);