]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-udld.c
Avoiding conditional directives that break statements
[tcpdump] / print-udld.c
index 15e2bf6fae492f864fae592fe021f2e8a4c6001e..0dc10d3c62a9c4d70a97a2f798f183ad6beebe1e 100644 (file)
  * LIMITATION, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  * FOR A PARTICULAR PURPOSE.
  *
- * UNIDIRECTIONAL LINK DETECTION (UDLD) as per
- * http://www.ietf.org/internet-drafts/draft-foschiano-udld-02.txt
+ * UNIDIRECTIONAL LINK DETECTION (UDLD) as per RFC5171
  *
  * Original code by Carles Kishimoto <[email protected]>
  */
 
-#define NETDISSECT_REWORKED
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#include <tcpdump-stdinc.h>
+#include <netdissect-stdinc.h>
 
-#include "interface.h"
+#include "netdissect.h"
 #include "extract.h"
 
+static const char tstr[] = " [|udld]";
+static const char istr[] = " (invalid)";
+
 #define UDLD_HEADER_LEN                        4
 #define UDLD_DEVICE_ID_TLV             0x0001
 #define UDLD_PORT_ID_TLV               0x0002
@@ -63,9 +64,10 @@ static const struct tok udld_flags_values[] = {
 };
 
 /*
+ * UDLD's Protocol Data Unit format:
  *
- * 0                   1                   2                   3
- * 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ *  0                   1                   2                   3
+ *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  * | Ver | Opcode  |     Flags     |           Checksum            |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
@@ -73,6 +75,18 @@ static const struct tok udld_flags_values[] = {
  * |                              ...                              |
  * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  *
+ * TLV format:
+ *
+ *  0                   1                   2                   3
+ *  0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |             TYPE              |            LENGTH             |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ * |                             VALUE                             |
+ * |                              ...                              |
+ * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
+ *
+ * LENGTH: Length in bytes of the Type, Length, and Value fields.
  */
 
 #define        UDLD_EXTRACT_VERSION(x) (((x)&0xe0)>>5)
@@ -115,35 +129,48 @@ udld_print (netdissect_options *ndo, const u_char *pptr, u_int length)
     while (tptr < (pptr+length)) {
 
         ND_TCHECK2(*tptr, 4);
-
        type = EXTRACT_16BITS(tptr);
         len  = EXTRACT_16BITS(tptr+2);
-        len -= 4;
-        tptr += 4;
-
-        /* infinite loop check */
-        if (type == 0 || len == 0) {
-            return;
-        }
 
         ND_PRINT((ndo, "\n\t%s (0x%04x) TLV, length %u",
                tok2str(udld_tlv_values, "Unknown", type),
                type, len));
 
+        if (type == 0)
+            goto invalid;
+
+        /* infinite loop check */
+        if (len <= 4)
+            goto invalid;
+
+        len -= 4;
+        tptr += 4;
+
+        ND_TCHECK2(*tptr, len);
+
         switch (type) {
         case UDLD_DEVICE_ID_TLV:
         case UDLD_PORT_ID_TLV:
-        case UDLD_ECHO_TLV:
         case UDLD_DEVICE_NAME_TLV:
-            ND_PRINT((ndo, ", %s", tptr));
+            ND_PRINT((ndo, ", "));
+            fn_printzp(ndo, tptr, len, NULL);
+            break;
+
+        case UDLD_ECHO_TLV:
+            ND_PRINT((ndo, ", "));
+            (void)fn_printn(ndo, tptr, len, NULL);
             break;
 
         case UDLD_MESSAGE_INTERVAL_TLV:
         case UDLD_TIMEOUT_INTERVAL_TLV:
+            if (len != 1)
+                goto invalid;
             ND_PRINT((ndo, ", %us", (*tptr)));
             break;
 
         case UDLD_SEQ_NUMBER_TLV:
+            if (len != 4)
+                goto invalid;
             ND_PRINT((ndo, ", %u", EXTRACT_32BITS(tptr)));
             break;
 
@@ -155,8 +182,11 @@ udld_print (netdissect_options *ndo, const u_char *pptr, u_int length)
 
     return;
 
- trunc:
-    ND_PRINT((ndo, "[|udld]"));
+invalid:
+    ND_PRINT((ndo, "%s", istr));
+    return;
+trunc:
+    ND_PRINT((ndo, "%s", tstr));
 }
 
 /*