]> The Tcpdump Group git mirrors - tcpdump/blobdiff - print-hncp.c
CI: Add warning exemptions for Sun C (suncc-5.14) on Solaris 10
[tcpdump] / print-hncp.c
index c4baec0e54f17e78a7210946895de29e3aa193a9..2dc3147ac4a4c7801ef1906b995dd8ffff8954a1 100644 (file)
 
 /* \summary: Home Networking Control Protocol (HNCP) printer */
 
-#ifdef HAVE_CONFIG_H
-#include "config.h"
-#endif
+#include <config.h>
 
-#include <netdissect-stdinc.h>
+#include "netdissect-stdinc.h"
 
-#include <stdlib.h>
 #include <string.h>
 
 #include "netdissect.h"
@@ -49,7 +46,8 @@ void
 hncp_print(netdissect_options *ndo,
            const u_char *cp, u_int length)
 {
-    ND_PRINT((ndo, "hncp (%d)", length));
+    ndo->ndo_protocol = "hncp";
+    ND_PRINT("hncp (%u)", length);
     hncp_print_rec(ndo, cp, length, 1);
 }
 
@@ -68,8 +66,8 @@ hncp_print(netdissect_options *ndo,
 #define HNCP_EXTERNAL_CONNECTION   33
 #define HNCP_DELEGATED_PREFIX      34
 #define HNCP_PREFIX_POLICY         43
-#define HNCP_DHCPV4_DATA           37
-#define HNCP_DHCPV6_DATA           38
+#define HNCP_DHCPV4_DATA           37 /* This is correct, see RFC 7788 Errata ID 5113. */
+#define HNCP_DHCPV6_DATA           38 /* idem */
 #define HNCP_ASSIGNED_PREFIX       35
 #define HNCP_NODE_ADDRESS          36
 #define HNCP_DNS_DELEGATED_ZONE    39
@@ -136,28 +134,48 @@ static const struct tok dh6opt_str[] = {
     { 0, NULL }
 };
 
+/*
+ * For IPv4-mapped IPv6 addresses, length of the prefix that precedes
+ * the 4 bytes of IPv4 address at the end of the IPv6 address.
+ */
+#define IPV4_MAPPED_HEADING_LEN    12
+
+/*
+ * Is an IPv6 address an IPv4-mapped address?
+ */
+static int
+is_ipv4_mapped_address(const u_char *addr)
+{
+    /* The value of the prefix */
+    static const u_char ipv4_mapped_heading[IPV4_MAPPED_HEADING_LEN] =
+        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
+
+    return memcmp(addr, ipv4_mapped_heading, IPV4_MAPPED_HEADING_LEN) == 0;
+}
+
 static const char *
-format_nid(const u_char *data)
+format_nid(netdissect_options *ndo, const u_char *data)
 {
-    static char buf[4][11+5];
+    static char buf[4][sizeof("01:01:01:01")];
     static int i = 0;
     i = (i + 1) % 4;
-    snprintf(buf[i], 16, "%02x:%02x:%02x:%02x",
-             data[0], data[1], data[2], data[3]);
+    snprintf(buf[i], sizeof(buf[i]), "%02x:%02x:%02x:%02x",
+             GET_U_1(data), GET_U_1(data + 1), GET_U_1(data + 2),
+             GET_U_1(data + 3));
     return buf[i];
 }
 
 static const char *
-format_256(const u_char *data)
+format_256(netdissect_options *ndo, const u_char *data)
 {
-    static char buf[4][64+5];
+    static char buf[4][sizeof("0123456789abcdef0123456789abcdef0123456789abcdef0123456789abcdef")];
     static int i = 0;
     i = (i + 1) % 4;
-    snprintf(buf[i], 28, "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64,
-         EXTRACT_64BITS(data),
-         EXTRACT_64BITS(data + 8),
-         EXTRACT_64BITS(data + 16),
-         EXTRACT_64BITS(data + 24)
+    snprintf(buf[i], sizeof(buf[i]), "%016" PRIx64 "%016" PRIx64 "%016" PRIx64 "%016" PRIx64,
+         GET_BE_U_8(data),
+         GET_BE_U_8(data + 8),
+         GET_BE_U_8(data + 16),
+         GET_BE_U_8(data + 24)
     );
     return buf[i];
 }
@@ -175,10 +193,10 @@ format_interval(const uint32_t n)
 static const char *
 format_ip6addr(netdissect_options *ndo, const u_char *cp)
 {
-    if (EXTRACT_64BITS(cp) == 0x0 && EXTRACT_32BITS(cp+8) == 0xffff)
-        return ipaddr_string(ndo, cp + 12);
+    if (is_ipv4_mapped_address(cp))
+        return GET_IPADDR_STRING(cp + IPV4_MAPPED_HEADING_LEN);
     else
-        return ip6addr_string(ndo, cp);
+        return GET_IP6ADDR_STRING(cp);
 }
 
 static int
@@ -186,35 +204,35 @@ print_prefix(netdissect_options *ndo, const u_char *prefix, u_int max_length)
 {
     int plenbytes;
     char buf[sizeof("xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx:xxxx::/128")];
-    /* First 12 bytes of an IPv4-mapped IPv6 address */
-    static const u_char ipv4_mapped_heading[12] =
-        { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xFF, 0xFF };
 
-    if (prefix[0] >= 96 && max_length >= 13 && memcmp(prefix+1, ipv4_mapped_heading, 12) == 0) {
-        struct in_addr addr;
+    if (GET_U_1(prefix) >= 96 && max_length >= IPV4_MAPPED_HEADING_LEN + 1 &&
+        is_ipv4_mapped_address(prefix + 1)) {
+        nd_ipv4 addr;
         u_int plen;
 
-        plen = prefix[0]-96;
+        plen = GET_U_1(prefix) - 96;
         if (32 < plen)
             return -1;
         max_length -= 1;
 
         memset(&addr, 0, sizeof(addr));
         plenbytes = (plen + 7) / 8;
-        if (max_length < (u_int)plenbytes)
+        if (max_length < (u_int)plenbytes + IPV4_MAPPED_HEADING_LEN)
             return -3;
-        memcpy(&addr, &prefix[13], plenbytes);
+        memcpy(&addr, prefix + IPV4_MAPPED_HEADING_LEN + 1, plenbytes);
         if (plen % 8) {
                ((u_char *)&addr)[plenbytes - 1] &=
                        ((0xff00 >> (plen % 8)) & 0xff);
        }
-       snprintf(buf, sizeof(buf), "%s/%d", ipaddr_string(ndo, &addr), plen);
-        plenbytes += 13;
+       snprintf(buf, sizeof(buf), "%s/%u", ipaddr_string(ndo, (const u_char *)&addr), plen); /* local buffer, not packet data; don't use GET_IPADDR_STRING() */
+        plenbytes += 1 + IPV4_MAPPED_HEADING_LEN;
     } else {
         plenbytes = decode_prefix6(ndo, prefix, max_length, buf, sizeof(buf));
+        if (plenbytes < 0)
+            return plenbytes;
     }
 
-    ND_PRINT((ndo, "%s", buf));
+    ND_PRINT("%s", buf);
     return plenbytes;
 }
 
@@ -224,22 +242,23 @@ print_dns_label(netdissect_options *ndo,
 {
     u_int length = 0;
     while (length < max_length) {
-        u_int lab_length = cp[length++];
+        u_int lab_length = GET_U_1(cp + length);
+        length++;
         if (lab_length == 0)
             return (int)length;
         if (length > 1 && print)
-            safeputchar(ndo, '.');
+            ND_PRINT(".");
         if (length+lab_length > max_length) {
             if (print)
-                safeputs(ndo, cp+length, max_length-length);
+                nd_printjnp(ndo, cp+length, max_length-length);
             break;
         }
         if (print)
-            safeputs(ndo, cp+length, lab_length);
+            nd_printjnp(ndo, cp+length, lab_length);
         length += lab_length;
     }
     if (print)
-        ND_PRINT((ndo, "[|DNS]"));
+        ND_PRINT("[|DNS]");
     return -1;
 }
 
@@ -248,22 +267,26 @@ dhcpv4_print(netdissect_options *ndo,
              const u_char *cp, u_int length, int indent)
 {
     u_int i, t;
-    const u_char *tlv, *value;
+    const uint8_t *tlv, *value;
     uint8_t type, optlen;
 
     i = 0;
     while (i < length) {
+        if (i + 2 > length)
+            return -1;
         tlv = cp + i;
-        type = (uint8_t)tlv[0];
-        optlen = (uint8_t)tlv[1];
+        type = GET_U_1(tlv);
+        optlen = GET_U_1(tlv + 1);
         value = tlv + 2;
 
-        ND_PRINT((ndo, "\n"));
+        ND_PRINT("\n");
         for (t = indent; t > 0; t--)
-            ND_PRINT((ndo, "\t"));
+            ND_PRINT("\t");
 
-        ND_PRINT((ndo, "%s", tok2str(dh4opt_str, "Unknown", type)));
-        ND_PRINT((ndo," (%u)", optlen + 2 ));
+        ND_PRINT("%s", tok2str(dh4opt_str, "Unknown", type));
+        ND_PRINT(" (%u)", optlen + 2 );
+        if (i + 2 + optlen > length)
+            return -1;
 
         switch (type) {
         case DH4OPT_DNS_SERVERS:
@@ -272,14 +295,14 @@ dhcpv4_print(netdissect_options *ndo,
                 return -1;
             }
             for (t = 0; t < optlen; t += 4)
-                ND_PRINT((ndo, " %s", ipaddr_string(ndo, value + t)));
+                ND_PRINT(" %s", GET_IPADDR_STRING(value + t));
         }
             break;
         case DH4OPT_DOMAIN_SEARCH: {
             const u_char *tp = value;
             while (tp < value + optlen) {
-                ND_PRINT((ndo, " "));
-                if ((tp = ns_nprint(ndo, tp, value + optlen)) == NULL)
+                ND_PRINT(" ");
+                if ((tp = fqdn_print(ndo, tp, value + optlen)) == NULL)
                     return -1;
             }
         }
@@ -301,34 +324,38 @@ dhcpv6_print(netdissect_options *ndo,
 
     i = 0;
     while (i < length) {
+        if (i + 4 > length)
+            return -1;
         tlv = cp + i;
-        type = EXTRACT_16BITS(tlv);
-        optlen = EXTRACT_16BITS(tlv + 2);
+        type = GET_BE_U_2(tlv);
+        optlen = GET_BE_U_2(tlv + 2);
         value = tlv + 4;
 
-        ND_PRINT((ndo, "\n"));
+        ND_PRINT("\n");
         for (t = indent; t > 0; t--)
-            ND_PRINT((ndo, "\t"));
+            ND_PRINT("\t");
 
-        ND_PRINT((ndo, "%s", tok2str(dh6opt_str, "Unknown", type)));
-        ND_PRINT((ndo," (%u)", optlen + 4 ));
+        ND_PRINT("%s", tok2str(dh6opt_str, "Unknown", type));
+        ND_PRINT(" (%u)", optlen + 4 );
+        if (i + 4 + optlen > length)
+            return -1;
 
         switch (type) {
             case DH6OPT_DNS_SERVERS:
             case DH6OPT_SNTP_SERVERS: {
                 if (optlen % 16 != 0) {
-                    ND_PRINT((ndo, " %s", istr));
+                    nd_print_invalid(ndo);
                     return -1;
                 }
                 for (t = 0; t < optlen; t += 16)
-                    ND_PRINT((ndo, " %s", ip6addr_string(ndo, value + t)));
+                    ND_PRINT(" %s", GET_IP6ADDR_STRING(value + t));
             }
                 break;
             case DH6OPT_DOMAIN_LIST: {
                 const u_char *tp = value;
                 while (tp < value + optlen) {
-                    ND_PRINT((ndo, " "));
-                    if ((tp = ns_nprint(ndo, tp, value + optlen)) == NULL)
+                    ND_PRINT(" ");
+                    if ((tp = fqdn_print(ndo, tp, value + optlen)) == NULL)
                         return -1;
                 }
             }
@@ -356,22 +383,22 @@ print_type_in_line(netdissect_options *ndo,
             *first_one = 0;
             if (indent > 1) {
                 u_int t;
-                ND_PRINT((ndo, "\n"));
+                ND_PRINT("\n");
                 for (t = indent; t > 0; t--)
-                    ND_PRINT((ndo, "\t"));
+                    ND_PRINT("\t");
             } else {
-                ND_PRINT((ndo, " "));
+                ND_PRINT(" ");
             }
         } else {
-            ND_PRINT((ndo, ", "));
+            ND_PRINT(", ");
         }
-        ND_PRINT((ndo, "%s", tok2str(type_values, "Easter Egg", type)));
+        ND_PRINT("%s", tok2str(type_values, "Easter Egg", type));
         if (count > 1)
-            ND_PRINT((ndo, " (x%d)", count));
+            ND_PRINT(" (x%d)", count);
     }
 }
 
-void
+static void
 hncp_print_rec(netdissect_options *ndo,
                const u_char *cp, u_int length, int indent)
 {
@@ -383,7 +410,7 @@ hncp_print_rec(netdissect_options *ndo,
     uint32_t last_type_mask = 0xffffffffU;
     int last_type_count = -1;
 
-    const u_char *tlv, *value;
+    const uint8_t *tlv, *value;
     uint16_t type, bodylen;
     uint32_t type_mask;
 
@@ -392,19 +419,19 @@ hncp_print_rec(netdissect_options *ndo,
         tlv = cp + i;
 
         if (!in_line) {
-            ND_PRINT((ndo, "\n"));
+            ND_PRINT("\n");
             for (t = indent; t > 0; t--)
-                ND_PRINT((ndo, "\t"));
+                ND_PRINT("\t");
         }
 
-        ND_TCHECK2(*tlv, 4);
+        ND_TCHECK_4(tlv);
         if (i + 4 > length)
             goto invalid;
 
-        type = EXTRACT_16BITS(tlv);
-        bodylen = EXTRACT_16BITS(tlv + 2);
+        type = GET_BE_U_2(tlv);
+        bodylen = GET_BE_U_2(tlv + 2);
         value = tlv + 4;
-        ND_TCHECK2(*value, bodylen);
+        ND_TCHECK_LEN(value, bodylen);
         if (i + bodylen + 4 > length)
             goto invalid;
 
@@ -442,27 +469,27 @@ hncp_print_rec(netdissect_options *ndo,
             goto skip_multiline;
         }
 
-        ND_PRINT((ndo,"%s", tok2str(type_values, "Easter Egg (42)", type_mask) ));
+        ND_PRINT("%s", tok2str(type_values, "Easter Egg (42)", type_mask) );
         if (type_mask > 0xffff)
-            ND_PRINT((ndo,": type=%u", type ));
-        ND_PRINT((ndo," (%u)", bodylen + 4 ));
+            ND_PRINT(": type=%u", type );
+        ND_PRINT(" (%u)", bodylen + 4 );
 
         switch (type_mask) {
 
         case DNCP_REQUEST_NETWORK_STATE: {
             if (bodylen != 0)
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
         }
             break;
 
         case DNCP_REQUEST_NODE_STATE: {
             const char *node_identifier;
             if (bodylen != 4) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            node_identifier = format_nid(value);
-            ND_PRINT((ndo, " NID: %s", node_identifier));
+            node_identifier = format_nid(ndo, value);
+            ND_PRINT(" NID: %s", node_identifier);
         }
             break;
 
@@ -470,26 +497,26 @@ hncp_print_rec(netdissect_options *ndo,
             const char *node_identifier;
             uint32_t endpoint_identifier;
             if (bodylen != 8) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            node_identifier = format_nid(value);
-            endpoint_identifier = EXTRACT_32BITS(value + 4);
-            ND_PRINT((ndo, " NID: %s EPID: %08x",
+            node_identifier = format_nid(ndo, value);
+            endpoint_identifier = GET_BE_U_4(value + 4);
+            ND_PRINT(" NID: %s EPID: %08x",
                 node_identifier,
                 endpoint_identifier
-            ));
+            );
         }
             break;
 
         case DNCP_NETWORK_STATE: {
             uint64_t hash;
             if (bodylen != 8) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            hash = EXTRACT_64BITS(value);
-            ND_PRINT((ndo, " hash: %016" PRIx64, hash));
+            hash = GET_BE_U_8(value);
+            ND_PRINT(" hash: %016" PRIx64, hash);
         }
             break;
 
@@ -498,19 +525,19 @@ hncp_print_rec(netdissect_options *ndo,
             uint32_t sequence_number;
             uint64_t hash;
             if (bodylen < 20) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            node_identifier = format_nid(value);
-            sequence_number = EXTRACT_32BITS(value + 4);
-            interval = format_interval(EXTRACT_32BITS(value + 8));
-            hash = EXTRACT_64BITS(value + 12);
-            ND_PRINT((ndo, " NID: %s seqno: %u %s hash: %016" PRIx64,
+            node_identifier = format_nid(ndo, value);
+            sequence_number = GET_BE_U_4(value + 4);
+            interval = format_interval(GET_BE_U_4(value + 8));
+            hash = GET_BE_U_8(value + 12);
+            ND_PRINT(" NID: %s seqno: %u %s hash: %016" PRIx64,
                 node_identifier,
                 sequence_number,
                 interval,
                 hash
-            ));
+            );
             hncp_print_rec(ndo, value+20, bodylen-20, indent+1);
         }
             break;
@@ -519,17 +546,17 @@ hncp_print_rec(netdissect_options *ndo,
             const char *peer_node_identifier;
             uint32_t peer_endpoint_identifier, endpoint_identifier;
             if (bodylen != 12) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            peer_node_identifier = format_nid(value);
-            peer_endpoint_identifier = EXTRACT_32BITS(value + 4);
-            endpoint_identifier = EXTRACT_32BITS(value + 8);
-            ND_PRINT((ndo, " Peer-NID: %s Peer-EPID: %08x Local-EPID: %08x",
+            peer_node_identifier = format_nid(ndo, value);
+            peer_endpoint_identifier = GET_BE_U_4(value + 4);
+            endpoint_identifier = GET_BE_U_4(value + 8);
+            ND_PRINT(" Peer-NID: %s Peer-EPID: %08x Local-EPID: %08x",
                 peer_node_identifier,
                 peer_endpoint_identifier,
                 endpoint_identifier
-            ));
+            );
         }
             break;
 
@@ -537,27 +564,27 @@ hncp_print_rec(netdissect_options *ndo,
             uint32_t endpoint_identifier;
             const char *interval;
             if (bodylen < 8) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            endpoint_identifier = EXTRACT_32BITS(value);
-            interval = format_interval(EXTRACT_32BITS(value + 4));
-            ND_PRINT((ndo, " EPID: %08x Interval: %s",
+            endpoint_identifier = GET_BE_U_4(value);
+            interval = format_interval(GET_BE_U_4(value + 4));
+            ND_PRINT(" EPID: %08x Interval: %s",
                 endpoint_identifier,
                 interval
-            ));
+            );
         }
             break;
 
         case DNCP_TRUST_VERDICT: {
             if (bodylen <= 36) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            ND_PRINT((ndo, " Verdict: %u Fingerprint: %s Common Name: ",
-                *value,
-                format_256(value + 4)));
-            safeputs(ndo, value + 36, bodylen - 36);
+            ND_PRINT(" Verdict: %u Fingerprint: %s Common Name: ",
+                GET_U_1(value),
+                format_256(ndo, value + 4));
+            nd_printjnp(ndo, value + 36, bodylen - 36);
         }
             break;
 
@@ -565,18 +592,18 @@ hncp_print_rec(netdissect_options *ndo,
             uint16_t capabilities;
             uint8_t M, P, H, L;
             if (bodylen < 5) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            capabilities = EXTRACT_16BITS(value + 2);
+            capabilities = GET_BE_U_2(value + 2);
             M = (uint8_t)((capabilities >> 12) & 0xf);
             P = (uint8_t)((capabilities >> 8) & 0xf);
             H = (uint8_t)((capabilities >> 4) & 0xf);
             L = (uint8_t)(capabilities & 0xf);
-            ND_PRINT((ndo, " M: %u P: %u H: %u L: %u User-agent: ",
+            ND_PRINT(" M: %u P: %u H: %u L: %u User-agent: ",
                 M, P, H, L
-            ));
-            safeputs(ndo, value + 4, bodylen - 4);
+            );
+            nd_printjnp(ndo, value + 4, bodylen - 4);
         }
             break;
 
@@ -588,17 +615,17 @@ hncp_print_rec(netdissect_options *ndo,
 
         case HNCP_DELEGATED_PREFIX: {
             int l;
-            if (bodylen < 9 || bodylen < 9 + (value[8] + 7) / 8) {
-                ND_PRINT((ndo, " %s", istr));
+            if (bodylen < 9 || bodylen < 9 + (GET_U_1(value + 8) + 7) / 8) {
+                nd_print_invalid(ndo);
                 break;
             }
-            ND_PRINT((ndo, " VLSO: %s PLSO: %s Prefix: ",
-                format_interval(EXTRACT_32BITS(value)),
-                format_interval(EXTRACT_32BITS(value + 4))
-            ));
+            ND_PRINT(" VLSO: %s PLSO: %s Prefix: ",
+                format_interval(GET_BE_U_4(value)),
+                format_interval(GET_BE_U_4(value + 4))
+            );
             l = print_prefix(ndo, value + 8, bodylen - 8);
             if (l == -1) {
-                ND_PRINT((ndo, "(length is invalid)"));
+                ND_PRINT("(length is invalid)");
                 break;
             }
             if (l < 0) {
@@ -611,7 +638,7 @@ hncp_print_rec(netdissect_options *ndo,
                  * IPv6", or -3, meaning "the prefix runs past
                  * the end of the TLV".
                  */
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
             l += 8 + (-l & 3);
@@ -625,22 +652,22 @@ hncp_print_rec(netdissect_options *ndo,
             uint8_t policy;
             int l;
             if (bodylen < 1) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            policy = value[0];
-            ND_PRINT((ndo, " type: "));
+            policy = GET_U_1(value);
+            ND_PRINT(" type: ");
             if (policy == 0) {
                 if (bodylen != 1) {
-                    ND_PRINT((ndo, " %s", istr));
+                    nd_print_invalid(ndo);
                     break;
                 }
-                ND_PRINT((ndo, "Internet connectivity"));
+                ND_PRINT("Internet connectivity");
             } else if (policy >= 1 && policy <= 128) {
-                ND_PRINT((ndo, "Dest-Prefix: "));
+                ND_PRINT("Dest-Prefix: ");
                 l = print_prefix(ndo, value, bodylen);
                 if (l == -1) {
-                    ND_PRINT((ndo, "(length is invalid)"));
+                    ND_PRINT("(length is invalid)");
                     break;
                 }
                 if (l < 0) {
@@ -653,30 +680,30 @@ hncp_print_rec(netdissect_options *ndo,
                      * IPv6", or -3, meaning "the prefix runs past
                      * the end of the TLV".
                      */
-                    ND_PRINT((ndo, " %s", istr));
+                    nd_print_invalid(ndo);
                     break;
                 }
             } else if (policy == 129) {
-                ND_PRINT((ndo, "DNS domain: "));
+                ND_PRINT("DNS domain: ");
                 print_dns_label(ndo, value+1, bodylen-1, 1);
             } else if (policy == 130) {
-                ND_PRINT((ndo, "Opaque UTF-8: "));
-                safeputs(ndo, value + 1, bodylen - 1);
+                ND_PRINT("Opaque UTF-8: ");
+                nd_printjnp(ndo, value + 1, bodylen - 1);
             } else if (policy == 131) {
                 if (bodylen != 1) {
-                    ND_PRINT((ndo, " %s", istr));
+                    nd_print_invalid(ndo);
                     break;
                 }
-                ND_PRINT((ndo, "Restrictive assignment"));
+                ND_PRINT("Restrictive assignment");
             } else if (policy >= 132) {
-                ND_PRINT((ndo, "Unknown (%u)", policy)); /* Reserved for future additions */
+                ND_PRINT("Unknown (%u)", policy); /* Reserved for future additions */
             }
         }
             break;
 
         case HNCP_DHCPV4_DATA: {
             if (bodylen == 0) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
             if (dhcpv4_print(ndo, value, bodylen, indent+1) != 0)
@@ -686,11 +713,11 @@ hncp_print_rec(netdissect_options *ndo,
 
         case HNCP_DHCPV6_DATA: {
             if (bodylen == 0) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
             if (dhcpv6_print(ndo, value, bodylen, indent+1) != 0) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
         }
@@ -699,18 +726,18 @@ hncp_print_rec(netdissect_options *ndo,
         case HNCP_ASSIGNED_PREFIX: {
             uint8_t prty;
             int l;
-            if (bodylen < 6 || bodylen < 6 + (value[5] + 7) / 8) {
-                ND_PRINT((ndo, " %s", istr));
+            if (bodylen < 6 || bodylen < 6 + (GET_U_1(value + 5) + 7) / 8) {
+                nd_print_invalid(ndo);
                 break;
             }
-            prty = (uint8_t)(value[4] & 0xf);
-            ND_PRINT((ndo, " EPID: %08x Prty: %u",
-                EXTRACT_32BITS(value),
+            prty = GET_U_1(value + 4) & 0xf;
+            ND_PRINT(" EPID: %08x Prty: %u",
+                GET_BE_U_4(value),
                 prty
-            ));
-            ND_PRINT((ndo, " Prefix: "));
+            );
+            ND_PRINT(" Prefix: ");
             if ((l = print_prefix(ndo, value + 5, bodylen - 5)) < 0) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
             l += 5;
@@ -725,15 +752,15 @@ hncp_print_rec(netdissect_options *ndo,
             uint32_t endpoint_identifier;
             const char *ip_address;
             if (bodylen < 20) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            endpoint_identifier = EXTRACT_32BITS(value);
+            endpoint_identifier = GET_BE_U_4(value);
             ip_address = format_ip6addr(ndo, value + 4);
-            ND_PRINT((ndo, " EPID: %08x IP Address: %s",
+            ND_PRINT(" EPID: %08x IP Address: %s",
                 endpoint_identifier,
                 ip_address
-            ));
+            );
 
             hncp_print_rec(ndo, value + 20, bodylen - 20, indent+1);
         }
@@ -743,19 +770,19 @@ hncp_print_rec(netdissect_options *ndo,
             const char *ip_address;
             int len;
             if (bodylen < 17) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
             ip_address = format_ip6addr(ndo, value);
-            ND_PRINT((ndo, " IP-Address: %s %c%c%c ",
+            ND_PRINT(" IP-Address: %s %c%c%c ",
                 ip_address,
-                (value[16] & 4) ? 'l' : '-',
-                (value[16] & 2) ? 'b' : '-',
-                (value[16] & 1) ? 's' : '-'
-            ));
+                (GET_U_1(value + 16) & 4) ? 'l' : '-',
+                (GET_U_1(value + 16) & 2) ? 'b' : '-',
+                (GET_U_1(value + 16) & 1) ? 's' : '-'
+            );
             len = print_dns_label(ndo, value+17, bodylen-17, 1);
             if (len < 0) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
             len += 17;
@@ -767,10 +794,10 @@ hncp_print_rec(netdissect_options *ndo,
 
         case HNCP_DOMAIN_NAME: {
             if (bodylen == 0) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            ND_PRINT((ndo, " Domain: "));
+            ND_PRINT(" Domain: ");
             print_dns_label(ndo, value, bodylen, 1);
         }
             break;
@@ -778,26 +805,26 @@ hncp_print_rec(netdissect_options *ndo,
         case HNCP_NODE_NAME: {
             u_int l;
             if (bodylen < 17) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            l = value[16];
+            l = GET_U_1(value + 16);
             if (bodylen < 17 + l) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            ND_PRINT((ndo, " IP-Address: %s Name: ",
+            ND_PRINT(" IP-Address: %s Name: ",
                 format_ip6addr(ndo, value)
-            ));
+            );
             if (l < 64) {
-                safeputchar(ndo, '"');
-                safeputs(ndo, value + 17, l);
-                safeputchar(ndo, '"');
+                ND_PRINT("\"");
+                nd_printjnp(ndo, value + 17, l);
+                ND_PRINT("\"");
             } else {
-                ND_PRINT((ndo, "%s", istr));
+                nd_print_invalid(ndo);
             }
             l += 17;
-            l += -l & 3;
+            l = roundup2(l, 4);
             if (bodylen >= l)
                 hncp_print_rec(ndo, value + l, bodylen - l, indent+1);
         }
@@ -805,10 +832,10 @@ hncp_print_rec(netdissect_options *ndo,
 
         case HNCP_MANAGED_PSK: {
             if (bodylen < 32) {
-                ND_PRINT((ndo, " %s", istr));
+                nd_print_invalid(ndo);
                 break;
             }
-            ND_PRINT((ndo, " PSK: %s", format_256(value)));
+            ND_PRINT(" PSK: %s", format_256(ndo, value));
             hncp_print_rec(ndo, value + 32, bodylen - 32, indent+1);
         }
             break;
@@ -822,17 +849,16 @@ hncp_print_rec(netdissect_options *ndo,
         }
     skip_multiline:
 
-        i += 4 + bodylen + (-bodylen & 3);
+        i += 4 + roundup2(bodylen, 4);
     }
     print_type_in_line(ndo, last_type_mask, last_type_count, indent, &first_one);
 
     return;
 
  trunc:
-    ND_PRINT((ndo, "%s", "[|hncp]"));
+    nd_print_trunc(ndo);
     return;
 
  invalid:
-    ND_PRINT((ndo, "%s", istr));
-    return;
+    nd_print_invalid(ndo);
 }