]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-ahcp.c
Use more the EXTRACT_U_1() macro (31/n)
[tcpdump] / print-ahcp.c
index 1d498735acb069d9a08f25eb26df58d326293551..0600290316c2f20211f0dd62764fc0e78d986601 100644 (file)
@@ -1,8 +1,4 @@
 /*
- * This module implements decoding of AHCP (Ad Hoc Configuration Protocol) based
- * on draft-chroboczek-ahcp-00 and source code of ahcpd-0.53.
- *
- *
  * Copyright (c) 2013 The TCPDUMP project
  * All rights reserved.
  *
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+/* \summary: Ad Hoc Configuration Protocol (AHCP) printer */
+
+/* Based on draft-chroboczek-ahcp-00 and source code of ahcpd-0.53 */
+
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include <tcpdump-stdinc.h>
+#include <netdissect-stdinc.h>
 
 #include "netdissect.h"
 #include "extract.h"
 #include "addrtoname.h"
 
 static const char tstr[] = " [|ahcp]";
-static const char cstr[] = "(corrupt)";
 
 #define AHCP_MAGIC_NUMBER 43
 #define AHCP_VERSION_1 1
@@ -99,15 +98,16 @@ static const struct tok ahcp1_opt_str[] = {
 };
 
 static int
-ahcp_time_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
+ahcp_time_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
+{
        time_t t;
        struct tm *tm;
        char buf[BUFSIZE];
 
        if (cp + 4 != ep)
-               goto corrupt;
-       ND_TCHECK2(*cp, 4);
-       t = EXTRACT_32BITS(cp);
+               goto invalid;
+       ND_TCHECK_4(cp);
+       t = EXTRACT_BE_U_4(cp);
        if (NULL == (tm = gmtime(&t)))
                ND_PRINT((ndo, ": gmtime() error"));
        else if (0 == strftime(buf, sizeof(buf), "%Y-%m-%d %H:%M:%S", tm))
@@ -116,8 +116,8 @@ ahcp_time_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
                ND_PRINT((ndo, ": %s UTC", buf));
        return 0;
 
-corrupt:
-       ND_PRINT((ndo, ": %s", cstr));
+invalid:
+       ND_PRINT((ndo, "%s", istr));
        ND_TCHECK2(*cp, ep - cp);
        return 0;
 trunc:
@@ -126,15 +126,16 @@ trunc:
 }
 
 static int
-ahcp_seconds_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
+ahcp_seconds_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
+{
        if (cp + 4 != ep)
-               goto corrupt;
-       ND_TCHECK2(*cp, 4);
-       ND_PRINT((ndo, ": %us", EXTRACT_32BITS(cp)));
+               goto invalid;
+       ND_TCHECK_4(cp);
+       ND_PRINT((ndo, ": %us", EXTRACT_BE_U_4(cp)));
        return 0;
 
-corrupt:
-       ND_PRINT((ndo, ": %s", cstr));
+invalid:
+       ND_PRINT((ndo, "%s", istr));
        ND_TCHECK2(*cp, ep - cp);
        return 0;
 trunc:
@@ -143,27 +144,22 @@ trunc:
 }
 
 static int
-ahcp_ipv6_addresses_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
+ahcp_ipv6_addresses_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
+{
        const char *sep = ": ";
 
        while (cp < ep) {
                if (cp + 16 > ep)
-                       goto corrupt;
-               ND_TCHECK2(*cp, 16);
-               ND_PRINT((ndo, "%s%s", sep,
-#ifdef INET6
-                      ip6addr_string(cp)
-#else
-                      "(compiled w/o IPv6)"
-#endif /* INET6 */
-                      ));
+                       goto invalid;
+               ND_TCHECK_16(cp);
+               ND_PRINT((ndo, "%s%s", sep, ip6addr_string(ndo, cp)));
                cp += 16;
                sep = ", ";
        }
        return 0;
 
-corrupt:
-       ND_PRINT((ndo, ": %s", cstr));
+invalid:
+       ND_PRINT((ndo, "%s", istr));
        ND_TCHECK2(*cp, ep - cp);
        return 0;
 trunc:
@@ -172,21 +168,22 @@ trunc:
 }
 
 static int
-ahcp_ipv4_addresses_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
+ahcp_ipv4_addresses_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
+{
        const char *sep = ": ";
 
        while (cp < ep) {
                if (cp + 4 > ep)
-                       goto corrupt;
-               ND_TCHECK2(*cp, 4);
-               ND_PRINT((ndo, "%s%s", sep, ipaddr_string(cp)));
+                       goto invalid;
+               ND_TCHECK_4(cp);
+               ND_PRINT((ndo, "%s%s", sep, ipaddr_string(ndo, cp)));
                cp += 4;
                sep = ", ";
        }
        return 0;
 
-corrupt:
-       ND_PRINT((ndo, ": %s", cstr));
+invalid:
+       ND_PRINT((ndo, "%s", istr));
        ND_TCHECK2(*cp, ep - cp);
        return 0;
 trunc:
@@ -195,27 +192,22 @@ trunc:
 }
 
 static int
-ahcp_ipv6_prefixes_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
+ahcp_ipv6_prefixes_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
+{
        const char *sep = ": ";
 
        while (cp < ep) {
                if (cp + 17 > ep)
-                       goto corrupt;
+                       goto invalid;
                ND_TCHECK2(*cp, 17);
-               ND_PRINT((ndo, "%s%s/%u", sep,
-#ifdef INET6
-                      ip6addr_string(cp),
-#else
-                      "(compiled w/o IPv6)",
-#endif /* INET6 */
-                      *(cp + 16)));
+               ND_PRINT((ndo, "%s%s/%u", sep, ip6addr_string(ndo, cp), EXTRACT_U_1(cp + 16)));
                cp += 17;
                sep = ", ";
        }
        return 0;
 
-corrupt:
-       ND_PRINT((ndo, ": %s", cstr));
+invalid:
+       ND_PRINT((ndo, "%s", istr));
        ND_TCHECK2(*cp, ep - cp);
        return 0;
 trunc:
@@ -224,21 +216,22 @@ trunc:
 }
 
 static int
-ahcp_ipv4_prefixes_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
+ahcp_ipv4_prefixes_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
+{
        const char *sep = ": ";
 
        while (cp < ep) {
                if (cp + 5 > ep)
-                       goto corrupt;
-               ND_TCHECK2(*cp, 5);
-               ND_PRINT((ndo, "%s%s/%u", sep, ipaddr_string(cp), *(cp + 4)));
+                       goto invalid;
+               ND_TCHECK_5(cp);
+               ND_PRINT((ndo, "%s%s/%u", sep, ipaddr_string(ndo, cp), EXTRACT_U_1(cp + 4)));
                cp += 5;
                sep = ", ";
        }
        return 0;
 
-corrupt:
-       ND_PRINT((ndo, ": %s", cstr));
+invalid:
+       ND_PRINT((ndo, "%s", istr));
        ND_TCHECK2(*cp, ep - cp);
        return 0;
 trunc:
@@ -266,12 +259,13 @@ static int
 };
 
 static void
-ahcp1_options_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
+ahcp1_options_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
+{
        uint8_t option_no, option_len;
 
        while (cp < ep) {
                /* Option no */
-               ND_TCHECK2(*cp, 1);
+               ND_TCHECK_1(cp);
                option_no = *cp;
                cp += 1;
                ND_PRINT((ndo, "\n\t %s", tok2str(ahcp1_opt_str, "Unknown-%u", option_no)));
@@ -279,12 +273,12 @@ ahcp1_options_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
                        continue;
                /* Length */
                if (cp + 1 > ep)
-                       goto corrupt;
-               ND_TCHECK2(*cp, 1);
+                       goto invalid;
+               ND_TCHECK_1(cp);
                option_len = *cp;
                cp += 1;
                if (cp + option_len > ep)
-                       goto corrupt;
+                       goto invalid;
                /* Value */
                if (option_no <= AHCP1_OPT_MAX && data_decoders[option_no] != NULL) {
                        if (data_decoders[option_no](ndo, cp, cp + option_len) < 0)
@@ -297,8 +291,8 @@ ahcp1_options_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
        }
        return;
 
-corrupt:
-       ND_PRINT((ndo, " %s", cstr));
+invalid:
+       ND_PRINT((ndo, "%s", istr));
        ND_TCHECK2(*cp, ep - cp);
        return;
 trunc:
@@ -306,23 +300,24 @@ trunc:
 }
 
 static void
-ahcp1_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
+ahcp1_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep)
+{
        uint8_t type, mbz;
        uint16_t body_len;
 
        if (cp + AHCP1_BODY_MIN_LEN > ep)
-               goto corrupt;
+               goto invalid;
        /* Type */
-       ND_TCHECK2(*cp, 1);
+       ND_TCHECK_1(cp);
        type = *cp;
        cp += 1;
        /* MBZ */
-       ND_TCHECK2(*cp, 1);
+       ND_TCHECK_1(cp);
        mbz = *cp;
        cp += 1;
        /* Length */
-       ND_TCHECK2(*cp, 2);
-       body_len = EXTRACT_16BITS(cp);
+       ND_TCHECK_2(cp);
+       body_len = EXTRACT_BE_U_2(cp);
        cp += 2;
 
        if (ndo->ndo_vflag) {
@@ -332,7 +327,7 @@ ahcp1_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
                ND_PRINT((ndo, ", Length %u", body_len));
        }
        if (cp + body_len > ep)
-               goto corrupt;
+               goto invalid;
 
        /* Options */
        if (ndo->ndo_vflag >= 2)
@@ -341,8 +336,8 @@ ahcp1_body_print(netdissect_options *ndo, const u_char *cp, const u_char *ep) {
                ND_TCHECK2(*cp, body_len);
        return;
 
-corrupt:
-       ND_PRINT((ndo, " %s", cstr));
+invalid:
+       ND_PRINT((ndo, "%s", istr));
        ND_TCHECK2(*cp, ep - cp);
        return;
 trunc:
@@ -350,50 +345,51 @@ trunc:
 }
 
 void
-ahcp_print(netdissect_options *ndo, const u_char *cp, const u_int len) {
+ahcp_print(netdissect_options *ndo, const u_char *cp, const u_int len)
+{
        const u_char *ep = cp + len;
        uint8_t version;
 
        ND_PRINT((ndo, "AHCP"));
        if (len < 2)
-               goto corrupt;
+               goto invalid;
        /* Magic */
-       ND_TCHECK2(*cp, 1);
+       ND_TCHECK_1(cp);
        if (*cp != AHCP_MAGIC_NUMBER)
-               goto corrupt;
+               goto invalid;
        cp += 1;
        /* Version */
-       ND_TCHECK2(*cp, 1);
+       ND_TCHECK_1(cp);
        version = *cp;
        cp += 1;
        switch (version) {
                case AHCP_VERSION_1: {
                        ND_PRINT((ndo, " Version 1"));
                        if (len < AHCP1_HEADER_FIX_LEN)
-                               goto corrupt;
+                               goto invalid;
                        if (!ndo->ndo_vflag) {
                                ND_TCHECK2(*cp, AHCP1_HEADER_FIX_LEN - 2);
                                cp += AHCP1_HEADER_FIX_LEN - 2;
                        } else {
                                /* Hopcount */
-                               ND_TCHECK2(*cp, 1);
-                               ND_PRINT((ndo, "\n\tHopcount %u", *cp));
+                               ND_TCHECK_1(cp);
+                               ND_PRINT((ndo, "\n\tHopcount %u", EXTRACT_U_1(cp)));
                                cp += 1;
                                /* Original Hopcount */
-                               ND_TCHECK2(*cp, 1);
-                               ND_PRINT((ndo, ", Original Hopcount %u", *cp));
+                               ND_TCHECK_1(cp);
+                               ND_PRINT((ndo, ", Original Hopcount %u", EXTRACT_U_1(cp)));
                                cp += 1;
                                /* Nonce */
-                               ND_TCHECK2(*cp, 4);
-                               ND_PRINT((ndo, ", Nonce 0x%08x", EXTRACT_32BITS(cp)));
+                               ND_TCHECK_4(cp);
+                               ND_PRINT((ndo, ", Nonce 0x%08x", EXTRACT_BE_U_4(cp)));
                                cp += 4;
                                /* Source Id */
-                               ND_TCHECK2(*cp, 8);
-                               ND_PRINT((ndo, ", Source Id %s", linkaddr_string(cp, 0, 8)));
+                               ND_TCHECK_8(cp);
+                               ND_PRINT((ndo, ", Source Id %s", linkaddr_string(ndo, cp, 0, 8)));
                                cp += 8;
                                /* Destination Id */
-                               ND_TCHECK2(*cp, 8);
-                               ND_PRINT((ndo, ", Destination Id %s", linkaddr_string(cp, 0, 8)));
+                               ND_TCHECK_8(cp);
+                               ND_PRINT((ndo, ", Destination Id %s", linkaddr_string(ndo, cp, 0, 8)));
                                cp += 8;
                        }
                        /* Body */
@@ -406,11 +402,10 @@ ahcp_print(netdissect_options *ndo, const u_char *cp, const u_int len) {
        }
        return;
 
-corrupt:
-       ND_PRINT((ndo, " %s", cstr));
+invalid:
+       ND_PRINT((ndo, "%s", istr));
        ND_TCHECK2(*cp, ep - cp);
        return;
 trunc:
        ND_PRINT((ndo, "%s", tstr));
 }
-