]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-icmp.c
ICMP: Make a few cppcheck-inspired fixups.
[tcpdump] / print-icmp.c
index d5563db41611608cab3dd5608e07bb0bc6bfe34a..0a949518d57f8039b7417246e1f9241464f7e887 100644 (file)
@@ -19,6 +19,8 @@
  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  */
 
+/* \summary: Internet Control Message Protocol (ICMP) printer */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
@@ -398,7 +400,7 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
 
                        default:
                                (void)snprintf(buf, sizeof(buf),
-                                       "%s protocol %d port %d unreachable",
+                                       "%s protocol %u port %u unreachable",
                                        ipaddr_string(ndo, &oip->ip_dst),
                                        oip->ip_p, dport);
                                break;
@@ -504,7 +506,7 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
                        break;
 
                default:
-                       (void)snprintf(buf, sizeof(buf), "time exceeded-#%d",
+                       (void)snprintf(buf, sizeof(buf), "time exceeded-#%u",
                            dp->icmp_code);
                        break;
                }
@@ -513,11 +515,11 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
        case ICMP_PARAMPROB:
                if (dp->icmp_code)
                        (void)snprintf(buf, sizeof(buf),
-                           "parameter problem - code %d", dp->icmp_code);
+                           "parameter problem - code %u", dp->icmp_code);
                else {
                        ND_TCHECK(dp->icmp_pptr);
                        (void)snprintf(buf, sizeof(buf),
-                           "parameter problem - octet %d", dp->icmp_pptr);
+                           "parameter problem - octet %u", dp->icmp_pptr);
                }
                break;
 
@@ -555,14 +557,14 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
        }
        ND_PRINT((ndo, "ICMP %s, length %u", str, plen));
        if (ndo->ndo_vflag && !fragmented) { /* don't attempt checksumming if this is a frag */
-               uint16_t sum, icmp_sum;
-
                if (ND_TTEST2(*bp, plen)) {
+                       uint16_t sum;
+
                        vec[0].ptr = (const uint8_t *)(const void *)dp;
                        vec[0].len = plen;
                        sum = in_cksum(vec, 1);
                        if (sum != 0) {
-                               icmp_sum = EXTRACT_16BITS(&dp->icmp_cksum);
+                               uint16_t icmp_sum = EXTRACT_16BITS(&dp->icmp_cksum);
                                ND_PRINT((ndo, " (wrong icmp cksum %x (->%x)!)",
                                             icmp_sum,
                                             in_cksum_shouldbe(icmp_sum, sum)));
@@ -578,8 +580,8 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
                bp += 8;
                ND_PRINT((ndo, "\n\t"));
                ip = (const struct ip *)bp;
-               ndo->ndo_snaplen = ndo->ndo_snapend - bp;
                 snapend_save = ndo->ndo_snapend;
+               ND_TCHECK_16BITS(&ip->ip_len);
                ip_print(ndo, bp, EXTRACT_16BITS(&ip->ip_len));
                 ndo->ndo_snapend = snapend_save;
        }
@@ -597,7 +599,8 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
              * to check if an extension header is present. This is expedient,
              * however not all implementations set the length field proper.
              */
-            if (!ext_dp->icmp_length) {
+            if (!ext_dp->icmp_length &&
+                ND_TTEST2(ext_dp->icmp_ext_version_res, plen - ICMP_EXTD_MINLEN)) {
                 vec[0].ptr = (const uint8_t *)(const void *)&ext_dp->icmp_ext_version_res;
                 vec[0].len = plen - ICMP_EXTD_MINLEN;
                 if (in_cksum(vec, 1)) {
@@ -618,12 +621,14 @@ icmp_print(netdissect_options *ndo, const u_char *bp, u_int plen, const u_char *
             }
 
             hlen = plen - ICMP_EXTD_MINLEN;
-            vec[0].ptr = (const uint8_t *)(const void *)&ext_dp->icmp_ext_version_res;
-            vec[0].len = hlen;
-            ND_PRINT((ndo, ", checksum 0x%04x (%scorrect), length %u",
-                   EXTRACT_16BITS(ext_dp->icmp_ext_checksum),
-                   in_cksum(vec, 1) ? "in" : "",
-                   hlen));
+            if (ND_TTEST2(ext_dp->icmp_ext_version_res, hlen)) {
+                vec[0].ptr = (const uint8_t *)(const void *)&ext_dp->icmp_ext_version_res;
+                vec[0].len = hlen;
+                ND_PRINT((ndo, ", checksum 0x%04x (%scorrect), length %u",
+                       EXTRACT_16BITS(ext_dp->icmp_ext_checksum),
+                       in_cksum(vec, 1) ? "in" : "",
+                       hlen));
+            }
 
             hlen -= 4; /* subtract common header size */
             obj_tptr = (const uint8_t *)ext_dp->icmp_ext_data;