]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-ip.c
Make 1-element arrays for fields that may repeat.
[tcpdump] / print-ip.c
index 64599835f96a8f38068c3285c793992678a6efc7..4f916bc67a5d98aea3224d13246dd163fc60e219 100644 (file)
@@ -56,10 +56,10 @@ static const struct tok ip_option_values[] = {
  */
 static int
 ip_printroute(netdissect_options *ndo,
-              register const u_char *cp, u_int length)
+              const u_char *cp, u_int length)
 {
-       register u_int ptr;
-       register u_int len;
+       u_int ptr;
+       u_int len;
 
        if (length < 3) {
                ND_PRINT((ndo, " [bad length %u]", length));
@@ -68,7 +68,7 @@ ip_printroute(netdissect_options *ndo,
        if ((length + 1) & 3)
                ND_PRINT((ndo, " [bad length %u]", length));
        ND_TCHECK_1(cp + 2);
-       ptr = cp[2] - 1;
+       ptr = EXTRACT_U_1(cp + 2) - 1;
        if (ptr < 3 || ((ptr + 1) & 3) || ptr > length + 1)
                ND_PRINT((ndo, " [bad ptr %u]", EXTRACT_U_1(cp + 2)));
 
@@ -98,7 +98,6 @@ ip_finddst(netdissect_options *ndo,
        int length;
        int len;
        const u_char *cp;
-       uint32_t retval;
 
        cp = (const u_char *)(ip + 1);
        length = (IP_HL(ip) << 2) - sizeof(struct ip);
@@ -107,31 +106,29 @@ ip_finddst(netdissect_options *ndo,
                int tt;
 
                ND_TCHECK_1(cp);
-               tt = *cp;
+               tt = EXTRACT_U_1(cp);
                if (tt == IPOPT_EOL)
                        break;
                else if (tt == IPOPT_NOP)
                        len = 1;
                else {
                        ND_TCHECK_1(cp + 1);
-                       len = cp[1];
+                       len = EXTRACT_U_1(cp + 1);
                        if (len < 2)
                                break;
                }
-               ND_TCHECK2(*cp, len);
+               ND_TCHECK_LEN(cp, len);
                switch (tt) {
 
                case IPOPT_SSRR:
                case IPOPT_LSRR:
                        if (len < 7)
                                break;
-                       UNALIGNED_MEMCPY(&retval, cp + len - 4, 4);
-                       return retval;
+                       return (EXTRACT_IPV4_TO_NETWORK_ORDER(cp + len - 4));
                }
        }
 trunc:
-       UNALIGNED_MEMCPY(&retval, &ip->ip_dst, sizeof(uint32_t));
-       return retval;
+       return (EXTRACT_IPV4_TO_NETWORK_ORDER(ip->ip_dst));
 }
 
 /*
@@ -155,9 +152,9 @@ nextproto4_cksum(netdissect_options *ndo,
        ph.len = htons((uint16_t)len);
        ph.mbz = 0;
        ph.proto = next_proto;
-       UNALIGNED_MEMCPY(&ph.src, &ip->ip_src, sizeof(uint32_t));
+       ph.src = EXTRACT_IPV4_TO_NETWORK_ORDER(ip->ip_src);
        if (IP_HL(ip) == 5)
-               UNALIGNED_MEMCPY(&ph.dst, &ip->ip_dst, sizeof(uint32_t));
+               ph.dst = EXTRACT_IPV4_TO_NETWORK_ORDER(ip->ip_dst);
        else
                ph.dst = ip_finddst(ndo, ip);
 
@@ -170,10 +167,10 @@ nextproto4_cksum(netdissect_options *ndo,
 
 static int
 ip_printts(netdissect_options *ndo,
-           register const u_char *cp, u_int length)
+           const u_char *cp, u_int length)
 {
-       register u_int ptr;
-       register u_int len;
+       u_int ptr;
+       u_int len;
        int hoplen;
        const char *type;
 
@@ -182,11 +179,12 @@ ip_printts(netdissect_options *ndo,
                return (0);
        }
        ND_PRINT((ndo, " TS{"));
-       hoplen = ((cp[3]&0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
+       ND_TCHECK_1(cp + 3);
+       hoplen = ((EXTRACT_U_1(cp + 3) & 0xF) != IPOPT_TS_TSONLY) ? 8 : 4;
        if ((length - 4) & (hoplen-1))
                ND_PRINT((ndo, "[bad length %u]", length));
        ND_TCHECK_1(cp + 2);
-       ptr = cp[2] - 1;
+       ptr = EXTRACT_U_1(cp + 2) - 1;
        len = 0;
        if (ptr < 4 || ((ptr - 4) & (hoplen-1)) || ptr > length + 1)
                ND_PRINT((ndo, "[bad ptr %u]", EXTRACT_U_1(cp + 2)));
@@ -219,7 +217,7 @@ ip_printts(netdissect_options *ndo,
        for (len = 4; len < length; len += hoplen) {
                if (ptr == len)
                        type = " ^ ";
-               ND_TCHECK2(cp[len], hoplen);
+               ND_TCHECK_LEN(cp + len, hoplen);
                ND_PRINT((ndo, "%s%d@%s", type, EXTRACT_BE_U_4(cp + len + hoplen - 4),
                          hoplen!=8 ? "" : ipaddr_string(ndo, cp + len)));
                type = " ";
@@ -243,9 +241,9 @@ trunc:
  */
 static void
 ip_optprint(netdissect_options *ndo,
-            register const u_char *cp, u_int length)
+            const u_char *cp, u_int length)
 {
-       register u_int option_len;
+       u_int option_len;
        const char *sep = "";
 
        for (; length > 0; cp += option_len, length -= option_len) {
@@ -255,7 +253,7 @@ ip_optprint(netdissect_options *ndo,
                sep = ",";
 
                ND_TCHECK_1(cp);
-               option_code = *cp;
+               option_code = EXTRACT_U_1(cp);
 
                ND_PRINT((ndo, "%s",
                          tok2str(ip_option_values,"unknown %u",option_code)));
@@ -266,7 +264,7 @@ ip_optprint(netdissect_options *ndo,
 
                else {
                        ND_TCHECK_1(cp + 1);
-                       option_len = cp[1];
+                       option_len = EXTRACT_U_1(cp + 1);
                        if (option_len < 2) {
                                ND_PRINT((ndo, " [bad length %u]", option_len));
                                return;
@@ -278,7 +276,7 @@ ip_optprint(netdissect_options *ndo,
                        return;
                }
 
-               ND_TCHECK2(*cp, option_len);
+               ND_TCHECK_LEN(cp, option_len);
 
                switch (option_code) {
                case IPOPT_EOL:
@@ -345,11 +343,11 @@ again:
        switch (ipds->nh) {
 
        case IPPROTO_AH:
-               if (!ND_TTEST(*ipds->cp)) {
+               if (!ND_TTEST_1(ipds->cp)) {
                        ND_PRINT((ndo, "[|AH]"));
                        break;
                }
-               ipds->nh = *ipds->cp;
+               ipds->nh = EXTRACT_U_1(ipds->cp);
                ipds->advance = ah_print(ndo, ipds->cp);
                if (ipds->advance <= 0)
                        break;
@@ -483,14 +481,16 @@ again:
                                ND_PRINT((ndo, "carp %s > %s: ",
                                             ipaddr_string(ndo, &ipds->ip->ip_src),
                                             ipaddr_string(ndo, &ipds->ip->ip_dst)));
-                       carp_print(ndo, ipds->cp, ipds->len, ipds->ip->ip_ttl);
+                       carp_print(ndo, ipds->cp, ipds->len,
+                               EXTRACT_U_1(ipds->ip->ip_ttl));
                } else {
                        if (ndo->ndo_vflag)
                                ND_PRINT((ndo, "vrrp %s > %s: ",
                                             ipaddr_string(ndo, &ipds->ip->ip_src),
                                             ipaddr_string(ndo, &ipds->ip->ip_dst)));
                        vrrp_print(ndo, ipds->cp, ipds->len,
-                               (const u_char *)ipds->ip, ipds->ip->ip_ttl);
+                               (const u_char *)ipds->ip,
+                               EXTRACT_U_1(ipds->ip->ip_ttl));
                }
                break;
 
@@ -540,6 +540,7 @@ ip_print(netdissect_options *ndo,
        const u_char *ipend;
        u_int hlen;
        struct cksum_vec vec[1];
+       uint8_t ip_tos, ip_ttl, ip_proto;
        uint16_t sum, ip_sum;
        const char *p_name;
 
@@ -597,10 +598,13 @@ ip_print(netdissect_options *ndo,
 
        ipds->off = EXTRACT_BE_U_2(&ipds->ip->ip_off);
 
+        ip_proto = EXTRACT_U_1(ipds->ip->ip_p);
+
         if (ndo->ndo_vflag) {
-            ND_PRINT((ndo, "(tos 0x%x", ipds->ip->ip_tos));
+            ip_tos = EXTRACT_U_1(ipds->ip->ip_tos);
+            ND_PRINT((ndo, "(tos 0x%x", ip_tos));
             /* ECN bits */
-            switch (ipds->ip->ip_tos & 0x03) {
+            switch (ip_tos & 0x03) {
 
             case 0:
                 break;
@@ -618,23 +622,23 @@ ip_print(netdissect_options *ndo,
                 break;
             }
 
-            if (ipds->ip->ip_ttl >= 1)
-                ND_PRINT((ndo, ", ttl %u", ipds->ip->ip_ttl));
+            ip_ttl = EXTRACT_U_1(ipds->ip->ip_ttl);
+            if (ip_ttl >= 1)
+                ND_PRINT((ndo, ", ttl %u", ip_ttl));
 
            /*
             * for the firewall guys, print id, offset.
              * On all but the last stick a "+" in the flags portion.
             * For unfragmented datagrams, note the don't fragment flag.
             */
-
            ND_PRINT((ndo, ", id %u, offset %u, flags [%s], proto %s (%u)",
                          EXTRACT_BE_U_2(&ipds->ip->ip_id),
                          (ipds->off & 0x1fff) * 8,
                          bittok2str(ip_frag_values, "none", ipds->off&0xe000),
-                         tok2str(ipproto_values,"unknown",ipds->ip->ip_p),
-                         ipds->ip->ip_p));
+                         tok2str(ipproto_values, "unknown", ip_proto),
+                         ip_proto));
 
-            ND_PRINT((ndo, ", length %u", EXTRACT_BE_U_2(&ipds->ip->ip_len)));
+            ND_PRINT((ndo, ", length %u", EXTRACT_BE_U_2(ipds->ip->ip_len)));
 
             if ((hlen - sizeof(struct ip)) > 0) {
                 ND_PRINT((ndo, ", options ("));
@@ -647,13 +651,13 @@ ip_print(netdissect_options *ndo,
                vec[0].len = hlen;
                sum = in_cksum(vec, 1);
                if (sum != 0) {
-                   ip_sum = EXTRACT_BE_U_2(&ipds->ip->ip_sum);
+                   ip_sum = EXTRACT_BE_U_2(ipds->ip->ip_sum);
                    ND_PRINT((ndo, ", bad cksum %x (->%x)!", ip_sum,
                             in_cksum_shouldbe(ip_sum, sum)));
                }
            }
 
-               ND_PRINT((ndo, ")\n    "));
+           ND_PRINT((ndo, ")\n    "));
        }
 
        /*
@@ -662,7 +666,7 @@ ip_print(netdissect_options *ndo,
         */
        if ((ipds->off & 0x1fff) == 0) {
                ipds->cp = (const u_char *)ipds->ip + hlen;
-               ipds->nh = ipds->ip->ip_p;
+               ipds->nh = EXTRACT_U_1(ipds->ip->ip_p);
 
                if (ipds->nh != IPPROTO_TCP && ipds->nh != IPPROTO_UDP &&
                    ipds->nh != IPPROTO_SCTP && ipds->nh != IPPROTO_DCCP) {
@@ -686,10 +690,10 @@ ip_print(netdissect_options *ndo,
                 */
                ND_PRINT((ndo, "%s > %s:", ipaddr_string(ndo, &ipds->ip->ip_src),
                          ipaddr_string(ndo, &ipds->ip->ip_dst)));
-               if (!ndo->ndo_nflag && (p_name = netdb_protoname(ipds->ip->ip_p)) != NULL)
+               if (!ndo->ndo_nflag && (p_name = netdb_protoname(ip_proto)) != NULL)
                        ND_PRINT((ndo, " %s", p_name));
                else
-                       ND_PRINT((ndo, " ip-proto-%d", ipds->ip->ip_p));
+                       ND_PRINT((ndo, " ip-proto-%u", ip_proto));
        }
        return;
 
@@ -699,7 +703,7 @@ trunc:
 }
 
 void
-ipN_print(netdissect_options *ndo, register const u_char *bp, register u_int length)
+ipN_print(netdissect_options *ndo, const u_char *bp, u_int length)
 {
        if (length < 1) {
                ND_PRINT((ndo, "truncated-ip %d", length));
@@ -707,7 +711,7 @@ ipN_print(netdissect_options *ndo, register const u_char *bp, register u_int len
        }
 
        ND_TCHECK_1(bp);
-       switch (*bp & 0xF0) {
+       switch (EXTRACT_U_1(bp) & 0xF0) {
        case 0x40:
                ip_print (ndo, bp, length);
                break;
@@ -715,7 +719,7 @@ ipN_print(netdissect_options *ndo, register const u_char *bp, register u_int len
                ip6_print (ndo, bp, length);
                break;
        default:
-               ND_PRINT((ndo, "unknown ip %d", (EXTRACT_U_1(bp) & 0xF0) >> 4));
+               ND_PRINT((ndo, "unknown ip %u", (EXTRACT_U_1(bp) & 0xF0) >> 4));
                break;
        }
        return;